Search in sources :

Example 51 with KeyPair

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"));
}
Also used : KeyPair(java.security.KeyPair) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 52 with KeyPair

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);
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 53 with KeyPair

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);
    }
}
Also used : KeyPair(java.security.KeyPair) X509Certificate(java.security.cert.X509Certificate)

Example 54 with KeyPair

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));
}
Also used : KeyPair(java.security.KeyPair) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyAgreement(javax.crypto.KeyAgreement)

Example 55 with KeyPair

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.
    }
}
Also used : KeyPair(java.security.KeyPair) GeneralSecurityException(java.security.GeneralSecurityException) KeyPairGenerator(java.security.KeyPairGenerator) KeyAgreement(javax.crypto.KeyAgreement)

Aggregations

KeyPair (java.security.KeyPair)313 KeyPairGenerator (java.security.KeyPairGenerator)146 X509Certificate (java.security.cert.X509Certificate)61 PrivateKey (java.security.PrivateKey)60 PublicKey (java.security.PublicKey)59 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)36 IOException (java.io.IOException)34 SecureRandom (java.security.SecureRandom)31 Test (org.junit.Test)26 KeyFactory (java.security.KeyFactory)23 KeyStore (java.security.KeyStore)23 Date (java.util.Date)22 BigInteger (java.math.BigInteger)21 Signature (java.security.Signature)19 File (java.io.File)17 Cipher (javax.crypto.Cipher)17 KeyPairGeneratorSpec (android.security.KeyPairGeneratorSpec)16 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)16 RSAPublicKey (java.security.interfaces.RSAPublicKey)16 HashMap (java.util.HashMap)16