use of java.security.KeyPair in project GNS by MobilityFirst.
the class KeyPairUtils method generateAndSaveKeyPair.
/**
* Saves the public/private key pair to preferences for the given user.
*
* @param gnsName the name of the GNS instance (e.g. "server.gns.name:8080")
* @param username the user name or human readable name
*/
public static GuidEntry generateAndSaveKeyPair(String gnsName, String username) throws NoSuchAlgorithmException, EncryptionException {
KeyPair keyPair = KeyPairGenerator.getInstance(GNSProtocol.RSA_ALGORITHM.toString()).generateKeyPair();
String guid = SharedGuidUtils.createGuidStringFromPublicKey(keyPair.getPublic().getEncoded());
GuidEntry guidEntry = new GuidEntry(gnsName, guid, keyPair.getPublic(), keyPair.getPrivate());
saveKeyPair(gnsName, username, guid, keyPair);
return guidEntry;
}
use of java.security.KeyPair in project wildfly by wildfly.
the class SingleSignOnSessionFactoryBuilder method getValue.
@Override
public SingleSignOnSessionFactory getValue() {
KeyStore store = this.keyStore.getValue();
String alias = this.keyAlias;
CredentialSource source = this.credentialSource.getValue();
try {
if (!store.containsAlias(alias)) {
UndertowLogger.ROOT_LOGGER.missingKeyStoreEntry(alias);
}
if (!store.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
UndertowLogger.ROOT_LOGGER.keyStoreEntryNotPrivate(alias);
}
PasswordCredential credential = source.getCredential(PasswordCredential.class);
if (credential == null) {
UndertowLogger.ROOT_LOGGER.missingCredential(source.toString());
}
ClearPassword password = credential.getPassword(ClearPassword.class);
if (password == null) {
UndertowLogger.ROOT_LOGGER.credentialNotClearPassword(credential.toString());
}
KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) store.getEntry(alias, new KeyStore.PasswordProtection(password.getPassword()));
KeyPair keyPair = new KeyPair(entry.getCertificate().getPublicKey(), entry.getPrivateKey());
Optional<SSLContext> context = Optional.ofNullable(this.sslContext).map(dependency -> dependency.getValue());
return new DefaultSingleSignOnSessionFactory(this.manager.getValue(), keyPair, connection -> context.ifPresent(ctx -> connection.setSSLSocketFactory(ctx.getSocketFactory())));
} catch (GeneralSecurityException | IOException e) {
throw new IllegalArgumentException(e);
}
}
use of java.security.KeyPair in project karaf by apache.
the class Main method startAgent.
private static SshAgent startAgent(String user, URL privateKeyUrl, String keyFile, FilePasswordProvider passwordProvider) {
InputStream is = null;
try {
SshAgent agent = new AgentImpl();
is = privateKeyUrl.openStream();
ObjectInputStream r = new ObjectInputStream(is);
KeyPair keyPair = (KeyPair) r.readObject();
is.close();
agent.addIdentity(keyPair, user);
if (keyFile != null) {
FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(Paths.get(keyFile));
fileKeyPairProvider.setPasswordFinder(passwordProvider);
for (KeyPair key : fileKeyPairProvider.loadKeys()) {
agent.addIdentity(key, user);
}
}
return agent;
} catch (Throwable e) {
close(is);
System.err.println("Error starting ssh agent for: " + e.getMessage());
return null;
}
}
use of java.security.KeyPair in project karaf by apache.
the class KnownHostsManagerTest method createPubKey.
private PublicKey createPubKey() throws NoSuchAlgorithmException {
KeyPairGenerator gen = KeyPairGenerator.getInstance(ALGORITHM);
gen.initialize(KEY_SIZE);
KeyPair keyPair = gen.generateKeyPair();
return keyPair.getPublic();
}
use of java.security.KeyPair in project geode by apache.
the class GMSEncryptJUnitTest method testDHAlgo3.
@Test
public void testDHAlgo3() throws Exception {
DHParameterSpec dhSkipParamSpec;
System.out.println("Using SKIP Diffie-Hellman parameters");
dhSkipParamSpec = new DHParameterSpec(skip1024Modulus, skip1024Base);
// Alice creates her own DH key pair
System.out.println("ALICE: Generate DH keypair ...");
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
aliceKpairGen.initialize(dhSkipParamSpec);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
// Bob creates his own DH key pair
System.out.println("BOB: Generate DH keypair ...");
KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");
bobKpairGen.initialize(dhSkipParamSpec);
KeyPair bobKpair = bobKpairGen.generateKeyPair();
// Alice initialize
System.out.println("ALICE: Initialize ...");
KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
aliceKeyAgree.init(aliceKpair.getPrivate());
// Bob initialize
System.out.println("BOB : Initialize ...");
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
bobKeyAgree.init(bobKpair.getPrivate());
// Alice uses Carol's public key
aliceKeyAgree.doPhase(bobKpair.getPublic(), true);
// Bob uses Alice's public key
bobKeyAgree.doPhase(aliceKpair.getPublic(), true);
String dhKalgo = "AES";
// Alice, Bob and Carol compute their secrets
SecretKey aliceSharedSecret = aliceKeyAgree.generateSecret(dhKalgo);
System.out.println("Alice secret: " + toHexString(aliceSharedSecret.getEncoded()));
SecretKey bobSharedSecret = bobKeyAgree.generateSecret(dhKalgo);
System.out.println("Bob secret: " + toHexString(bobSharedSecret.getEncoded()));
// Compare Alice and Bob
if (!java.util.Arrays.equals(aliceSharedSecret.getEncoded(), bobSharedSecret.getEncoded()))
throw new Exception("Alice and Bob differ");
System.out.println("Alice and Bob are the same");
}
Aggregations