Search in sources :

Example 16 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project robovm by robovm.

the class OpenSSLRSAKeyFactory method engineTranslateKey.

@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    if ((key instanceof OpenSSLRSAPublicKey) || (key instanceof OpenSSLRSAPrivateKey)) {
        return key;
    } else if (key instanceof RSAPublicKey) {
        RSAPublicKey rsaKey = (RSAPublicKey) key;
        try {
            return engineGeneratePublic(new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent()));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
        BigInteger modulus = rsaKey.getModulus();
        BigInteger publicExponent = rsaKey.getPublicExponent();
        BigInteger privateExponent = rsaKey.getPrivateExponent();
        BigInteger primeP = rsaKey.getPrimeP();
        BigInteger primeQ = rsaKey.getPrimeQ();
        BigInteger primeExponentP = rsaKey.getPrimeExponentP();
        BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
        BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
        try {
            return engineGeneratePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof RSAPrivateKey) {
        RSAPrivateKey rsaKey = (RSAPrivateKey) key;
        BigInteger modulus = rsaKey.getModulus();
        BigInteger privateExponent = rsaKey.getPrivateExponent();
        try {
            return engineGeneratePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePublic(new X509EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Key must be an RSA public or private key; was " + key.getClass().getName());
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) InvalidKeyException(java.security.InvalidKeyException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 17 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project robovm by robovm.

the class NativeCryptoTest method test_EVP_PKEY_cmp.

public void test_EVP_PKEY_cmp() throws Exception {
    try {
        NativeCrypto.EVP_PKEY_cmp(NULL, NULL);
        fail("Should throw NullPointerException when arguments are NULL");
    } catch (NullPointerException expected) {
    }
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(512);
    KeyPair kp1 = kpg.generateKeyPair();
    RSAPrivateCrtKey privKey1 = (RSAPrivateCrtKey) kp1.getPrivate();
    KeyPair kp2 = kpg.generateKeyPair();
    RSAPrivateCrtKey privKey2 = (RSAPrivateCrtKey) kp2.getPrivate();
    long pkey1 = 0, pkey1_copy = 0, pkey2 = 0;
    try {
        pkey1 = NativeCrypto.EVP_PKEY_new_RSA(privKey1.getModulus().toByteArray(), privKey1.getPublicExponent().toByteArray(), privKey1.getPrivateExponent().toByteArray(), privKey1.getPrimeP().toByteArray(), privKey1.getPrimeQ().toByteArray(), privKey1.getPrimeExponentP().toByteArray(), privKey1.getPrimeExponentQ().toByteArray(), privKey1.getCrtCoefficient().toByteArray());
        assertNotSame(NULL, pkey1);
        pkey1_copy = NativeCrypto.EVP_PKEY_new_RSA(privKey1.getModulus().toByteArray(), privKey1.getPublicExponent().toByteArray(), privKey1.getPrivateExponent().toByteArray(), privKey1.getPrimeP().toByteArray(), privKey1.getPrimeQ().toByteArray(), privKey1.getPrimeExponentP().toByteArray(), privKey1.getPrimeExponentQ().toByteArray(), privKey1.getCrtCoefficient().toByteArray());
        assertNotSame(NULL, pkey1_copy);
        pkey2 = NativeCrypto.EVP_PKEY_new_RSA(privKey2.getModulus().toByteArray(), privKey2.getPublicExponent().toByteArray(), privKey2.getPrivateExponent().toByteArray(), privKey2.getPrimeP().toByteArray(), privKey2.getPrimeQ().toByteArray(), privKey2.getPrimeExponentP().toByteArray(), privKey2.getPrimeExponentQ().toByteArray(), privKey2.getCrtCoefficient().toByteArray());
        assertNotSame(NULL, pkey2);
        try {
            NativeCrypto.EVP_PKEY_cmp(pkey1, NULL);
            fail("Should throw NullPointerException when arguments are NULL");
        } catch (NullPointerException expected) {
        }
        try {
            NativeCrypto.EVP_PKEY_cmp(NULL, pkey1);
            fail("Should throw NullPointerException when arguments are NULL");
        } catch (NullPointerException expected) {
        }
        assertEquals("Same keys should be the equal", 1, NativeCrypto.EVP_PKEY_cmp(pkey1, pkey1));
        assertEquals("Same keys should be the equal", 1, NativeCrypto.EVP_PKEY_cmp(pkey1, pkey1_copy));
        assertEquals("Different keys should not be equal", 0, NativeCrypto.EVP_PKEY_cmp(pkey1, pkey2));
    } finally {
        if (pkey1 != 0) {
            NativeCrypto.EVP_PKEY_free(pkey1);
        }
        if (pkey1_copy != 0) {
            NativeCrypto.EVP_PKEY_free(pkey1_copy);
        }
        if (pkey2 != 0) {
            NativeCrypto.EVP_PKEY_free(pkey2);
        }
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) KeyPairGenerator(java.security.KeyPairGenerator)

Example 18 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project jdk8u_jdk by JetBrains.

the class GenerateRSAPrivateCrtKey method main.

public static void main(String[] args) throws Exception {
    // Create an RSA Private Key from the CRT information
    RSAPrivateCrtKeySpec rsaCrtSpec = new RSAPrivateCrtKeySpec(new BigInteger(1, modulus), new BigInteger(1, pubExpo), new BigInteger(1, priExpo), new BigInteger(1, primeP), new BigInteger(1, primeQ), new BigInteger(1, expoP), new BigInteger(1, expoQ), new BigInteger(1, coeff));
    // Create an RSA private key from the CRT specification
    KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
    RSAPrivateCrtKey rsaPriKey = (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);
    // test resulting key against original specification
    if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
        throw new Exception("coefficients not equal");
    if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
        throw new Exception("primeExponentPs not equal");
    if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
        throw new Exception("primeExponentQs not equal");
    if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
        throw new Exception("primePs not equal");
    if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
        throw new Exception("primeQs not equal");
    if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
        throw new Exception("public exponents not equal");
    if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
        throw new Exception("private exponents not equal");
    if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
        throw new Exception("modulus not equal");
    if (!rsaPriKey.getFormat().equals("PKCS#8") && !rsaPriKey.getFormat().equals("PKCS8"))
        throw new Exception("format not PKCS#8");
    if (!rsaPriKey.getAlgorithm().equals("RSA"))
        throw new Exception("algorithm not RSA");
    if (rsaPriKey.getEncoded() == null)
        throw new Exception("encoded key is null");
    PKCS8EncodedKeySpec pkcs8Key = new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());
    RSAPrivateCrtKey rsaPriKey2 = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
    if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
        throw new Exception("encoded keys not equal");
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory)

Example 19 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project jdk8u_jdk by JetBrains.

the class Bug6415637 method check.

private static void check(String encodedBlob) throws Exception {
    byte[] blob = new byte[encodedBlob.length() * 2];
    for (int i = 0; i < blob.length; ) {
        final char ch = encodedBlob.charAt(i / 2);
        blob[i++] = (byte) (ch >> 8);
        blob[i++] = (byte) ch;
    }
    KeyStore store = KeyStore.getInstance("PKCS12");
    store.load(new ByteArrayInputStream(blob), new char[0]);
    if (!store.aliases().nextElement().equals("test"))
        throw new Exception("test alias not found");
    KeyStore.PrivateKeyEntry e = (KeyStore.PrivateKeyEntry) store.getEntry("test", new KeyStore.PasswordProtection(new char[0]));
    X509Certificate cert = (X509Certificate) e.getCertificateChain()[0];
    if (!cert.getSubjectDN().toString().equals("CN=Test Key"))
        throw new Exception("invalid certificate subject DN");
    RSAPrivateCrtKey key = (RSAPrivateCrtKey) e.getPrivateKey();
    if (!key.getPublicExponent().equals(BigInteger.valueOf(65537)))
        throw new Exception("invalid public exponent");
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 20 with RSAPrivateCrtKey

use of java.security.interfaces.RSAPrivateCrtKey in project jdk8u_jdk by JetBrains.

the class PrivateKeyEqualityTest method main.

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }
    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(rsaPrivateKeySpec);
    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(encodedKeySpec);
    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }
    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent(), rsaPrivateCrtKey.getPrivateExponent(), rsaPrivateCrtKey.getPrimeP(), rsaPrivateCrtKey.getPrimeQ(), rsaPrivateCrtKey.getPrimeExponentP(), rsaPrivateCrtKey.getPrimeExponentQ(), rsaPrivateCrtKey.getCrtCoefficient());
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Aggregations

RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)21 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)11 RSAPublicKey (java.security.interfaces.RSAPublicKey)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 InvalidKeyException (java.security.InvalidKeyException)6 PrivateKey (java.security.PrivateKey)6 PublicKey (java.security.PublicKey)6 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)6 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)6 BigInteger (java.math.BigInteger)5 KeyPair (java.security.KeyPair)5 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4 KeyFactory (java.security.KeyFactory)3 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)3 KeyPairGenerator (java.security.KeyPairGenerator)2 KeyStore (java.security.KeyStore)2 X509Certificate (java.security.cert.X509Certificate)2 DSAParams (java.security.interfaces.DSAParams)2