use of java.security.KeyPair in project spring-security-oauth by spring-projects.
the class JwtAccessTokenConverterTests method rsaKeyPair.
@Test
public void rsaKeyPair() throws Exception {
KeyStoreKeyFactory factory = new KeyStoreKeyFactory(new ClassPathResource("keystore.jks"), "foobar".toCharArray());
KeyPair keys = factory.getKeyPair("test");
tokenEnhancer.setKeyPair(keys);
tokenEnhancer.afterPropertiesSet();
assertTrue(tokenEnhancer.getKey().get("value").contains("BEGIN PUBLIC"));
}
use of java.security.KeyPair in project spring-security-oauth by spring-projects.
the class KeyStoreKeyFactory method getKeyPair.
public KeyPair getKeyPair(String alias, char[] password) {
try {
synchronized (lock) {
if (store == null) {
synchronized (lock) {
store = KeyStore.getInstance("jks");
store.load(resource.getInputStream(), this.password);
}
}
}
RSAPrivateCrtKey key = (RSAPrivateCrtKey) store.getKey(alias, password);
RSAPublicKeySpec spec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
return new KeyPair(publicKey, key);
} catch (Exception e) {
throw new IllegalStateException("Cannot load keys from store: " + resource, e);
}
}
use of java.security.KeyPair in project syncany by syncany.
the class WebServer method generateNewKeyPairAndCertificate.
public static void generateNewKeyPairAndCertificate(String certificateCommonName) {
try {
logger.log(Level.INFO, "(Re-)generating keypair and certificate for hostname " + certificateCommonName + " ...");
// Generate key pair and certificate
KeyPair keyPair = CipherUtil.generateRsaKeyPair();
X509Certificate certificate = CipherUtil.generateSelfSignedCertificate(certificateCommonName, keyPair);
// Add key and certificate to key store
UserConfig.getUserKeyStore().setKeyEntry(CipherParams.CERTIFICATE_IDENTIFIER, keyPair.getPrivate(), new char[0], new Certificate[] { certificate });
UserConfig.storeUserKeyStore();
// Add certificate to trust store (for CLI->API connection)
UserConfig.getUserTrustStore().setCertificateEntry(CipherParams.CERTIFICATE_IDENTIFIER, certificate);
UserConfig.storeTrustStore();
} catch (Exception e) {
throw new RuntimeException("Unable to read key store or generate self-signed certificate.", e);
}
}
use of java.security.KeyPair in project wycheproof by google.
the class DhTest method testDh.
/** Check that key agreement using DH works. */
@SuppressWarnings("InsecureCryptoUsage")
public void testDh() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
DHParameterSpec dhparams = ike2048();
keyGen.initialize(dhparams);
KeyPair keyPairA = keyGen.generateKeyPair();
KeyPair keyPairB = keyGen.generateKeyPair();
KeyAgreement kaA = KeyAgreement.getInstance("DH");
KeyAgreement kaB = KeyAgreement.getInstance("DH");
kaA.init(keyPairA.getPrivate());
kaB.init(keyPairB.getPrivate());
kaA.doPhase(keyPairB.getPublic(), true);
kaB.doPhase(keyPairA.getPublic(), true);
byte[] kAB = kaA.generateSecret();
byte[] kBA = kaB.generateSecret();
assertEquals(TestUtil.bytesToHex(kAB), TestUtil.bytesToHex(kBA));
}
use of java.security.KeyPair in project wycheproof by google.
the class DhTest method testDHDistinctParameters.
/** This test tries a key agreement with keys using distinct parameters. */
@SuppressWarnings("InsecureCryptoUsage")
public void testDHDistinctParameters() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
keyGen.initialize(ike1536());
KeyPair keyPairA = keyGen.generateKeyPair();
keyGen.initialize(ike2048());
KeyPair keyPairB = keyGen.generateKeyPair();
KeyAgreement kaA = KeyAgreement.getInstance("DH");
kaA.init(keyPairA.getPrivate());
try {
kaA.doPhase(keyPairB.getPublic(), true);
byte[] kAB = kaA.generateSecret();
fail("Generated secrets with mixed keys " + TestUtil.bytesToHex(kAB) + ", ");
} catch (java.security.GeneralSecurityException ex) {
// This is expected.
}
}
Aggregations