Search in sources :

Example 6 with RSAKey

use of java.security.interfaces.RSAKey in project android_frameworks_base by DirtyUnicorns.

the class AndroidKeyPairGeneratorTest method assertKeyPairCorrect.

private void assertKeyPairCorrect(KeyPair pair, String alias, String keyType, int keySize, AlgorithmParameterSpec spec, X500Principal dn, BigInteger serial, Date start, Date end) throws Exception {
    final PublicKey pubKey = pair.getPublic();
    assertNotNull("The PublicKey for the KeyPair should be not null", pubKey);
    assertEquals(keyType, pubKey.getAlgorithm());
    if ("EC".equalsIgnoreCase(keyType)) {
        assertEquals("Curve should be what was specified during initialization", keySize, ((ECPublicKey) pubKey).getParams().getCurve().getField().getFieldSize());
    } else if ("RSA".equalsIgnoreCase(keyType)) {
        RSAPublicKey rsaPubKey = (RSAPublicKey) pubKey;
        assertEquals("Modulus size should be what is specified during initialization", (keySize + 7) & ~7, (rsaPubKey.getModulus().bitLength() + 7) & ~7);
        if (spec != null) {
            RSAKeyGenParameterSpec params = (RSAKeyGenParameterSpec) spec;
            assertEquals((keySize + 7) & ~7, (params.getKeysize() + 7) & ~7);
            assertEquals(params.getPublicExponent(), rsaPubKey.getPublicExponent());
        }
    }
    final PrivateKey privKey = pair.getPrivate();
    assertNotNull("The PrivateKey for the KeyPair should be not null", privKey);
    assertEquals(keyType, privKey.getAlgorithm());
    if ("EC".equalsIgnoreCase(keyType)) {
        assertTrue("EC private key must be instanceof ECKey: " + privKey.getClass().getName(), privKey instanceof ECKey);
        assertEquals("Private and public key must have the same EC parameters", ((ECKey) pubKey).getParams(), ((ECKey) privKey).getParams());
    } else if ("RSA".equalsIgnoreCase(keyType)) {
        assertTrue("RSA private key must be instance of RSAKey: " + privKey.getClass().getName(), privKey instanceof RSAKey);
        assertEquals("Private and public key must have the same RSA modulus", ((RSAKey) pubKey).getModulus(), ((RSAKey) privKey).getModulus());
    }
    final byte[] userCertBytes = mAndroidKeyStore.get(Credentials.USER_CERTIFICATE + alias);
    assertNotNull("The user certificate should exist for the generated entry", userCertBytes);
    final CertificateFactory cf = CertificateFactory.getInstance("X.509");
    final Certificate userCert = cf.generateCertificate(new ByteArrayInputStream(userCertBytes));
    assertTrue("Certificate should be in X.509 format", userCert instanceof X509Certificate);
    final X509Certificate x509userCert = (X509Certificate) userCert;
    assertEquals("Public key used to sign certificate should have the same algorithm as in KeyPair", pubKey.getAlgorithm(), x509userCert.getPublicKey().getAlgorithm());
    assertEquals("PublicKey used to sign certificate should match one returned in KeyPair", pubKey, AndroidKeyStoreProvider.getAndroidKeyStorePublicKey(Credentials.USER_PRIVATE_KEY + alias, KeyStore.UID_SELF, x509userCert.getPublicKey().getAlgorithm(), x509userCert.getPublicKey().getEncoded()));
    assertEquals("The Subject DN should be the one passed into the params", dn, x509userCert.getSubjectDN());
    assertEquals("The Issuer DN should be the same as the Subject DN", dn, x509userCert.getIssuerDN());
    assertEquals("The Serial should be the one passed into the params", serial, x509userCert.getSerialNumber());
    assertDateEquals("The notBefore date should be the one passed into the params", start, x509userCert.getNotBefore());
    assertDateEquals("The notAfter date should be the one passed into the params", end, x509userCert.getNotAfter());
    // Assert that the cert's signature verifies using the public key from generated KeyPair
    x509userCert.verify(pubKey);
    // Assert that the cert's signature verifies using the public key from the cert itself.
    x509userCert.verify(x509userCert.getPublicKey());
    final byte[] caCerts = mAndroidKeyStore.get(Credentials.CA_CERTIFICATE + alias);
    assertNull("A list of CA certificates should not exist for the generated entry", caCerts);
    ExportResult exportResult = mAndroidKeyStore.exportKey(Credentials.USER_PRIVATE_KEY + alias, KeymasterDefs.KM_KEY_FORMAT_X509, null, null);
    assertEquals(KeyStore.NO_ERROR, exportResult.resultCode);
    final byte[] pubKeyBytes = exportResult.exportData;
    assertNotNull("The keystore should return the public key for the generated key", pubKeyBytes);
    assertTrue("Public key X.509 format should be as expected", Arrays.equals(pubKey.getEncoded(), pubKeyBytes));
}
Also used : RSAKey(java.security.interfaces.RSAKey) PrivateKey(java.security.PrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) ECKey(java.security.interfaces.ECKey) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) ExportResult(android.security.keymaster.ExportResult)

Example 7 with RSAKey

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

the class SpecTest method specTest.

/**
     *
     * @param kpair test key pair
     * @param pubExponent expected public exponent.
     * @return true if test passed. false if test failed.
     */
private static boolean specTest(KeyPair kpair, BigInteger pubExponent) {
    boolean passed = true;
    RSAPrivateKey priv = (RSAPrivateKey) kpair.getPrivate();
    RSAPublicKey pub = (RSAPublicKey) kpair.getPublic();
    // test the getModulus method
    if ((priv instanceof RSAKey) && (pub instanceof RSAKey)) {
        if (!priv.getModulus().equals(pub.getModulus())) {
            System.err.println("priv.getModulus() = " + priv.getModulus());
            System.err.println("pub.getModulus() = " + pub.getModulus());
            passed = false;
        }
        if (!pubExponent.equals(pub.getPublicExponent())) {
            System.err.println("pubExponent = " + pubExponent);
            System.err.println("pub.getPublicExponent() = " + pub.getPublicExponent());
            passed = false;
        }
    }
    return passed;
}
Also used : RSAKey(java.security.interfaces.RSAKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 8 with RSAKey

use of java.security.interfaces.RSAKey in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreTest method testKeyStore_GetKey_NoPassword_Encrypted_Success.

public void testKeyStore_GetKey_NoPassword_Encrypted_Success() throws Exception {
    setupPassword();
    mKeyStore.load(null, null);
    assertTrue(mAndroidKeyStore.importKey(Credentials.USER_PRIVATE_KEY + TEST_ALIAS_1, FAKE_RSA_KEY_1, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
    assertTrue(mAndroidKeyStore.put(Credentials.USER_CERTIFICATE + TEST_ALIAS_1, FAKE_RSA_USER_1, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
    assertTrue(mAndroidKeyStore.put(Credentials.CA_CERTIFICATE + TEST_ALIAS_1, FAKE_RSA_CA_1, KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED));
    Key key = mKeyStore.getKey(TEST_ALIAS_1, null);
    assertNotNull("Key should exist", key);
    assertTrue("Should be a PrivateKey", key instanceof PrivateKey);
    assertTrue("Should be a RSAKey", key instanceof RSAKey);
    KeyFactory keyFact = KeyFactory.getInstance("RSA");
    PrivateKey expectedKey = keyFact.generatePrivate(new PKCS8EncodedKeySpec(FAKE_RSA_KEY_1));
    assertEquals("Inserted key should be same as retrieved key", ((RSAKey) expectedKey).getModulus(), ((RSAKey) key).getModulus());
}
Also used : RSAKey(java.security.interfaces.RSAKey) PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) RSAKey(java.security.interfaces.RSAKey) ECKey(java.security.interfaces.ECKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) KeyFactory(java.security.KeyFactory)

Example 9 with RSAKey

use of java.security.interfaces.RSAKey in project karaf by apache.

the class PublickeyLoginModule method getString.

private String getString(PublicKey key) throws FailedLoginException {
    try {
        if (key instanceof DSAPublicKey) {
            DSAPublicKey dsa = (DSAPublicKey) key;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            write(dos, "ssh-dss");
            write(dos, dsa.getParams().getP());
            write(dos, dsa.getParams().getQ());
            write(dos, dsa.getParams().getG());
            write(dos, dsa.getY());
            dos.close();
            return base64Encode(baos.toByteArray());
        } else if (key instanceof RSAKey) {
            RSAPublicKey rsa = (RSAPublicKey) key;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            write(dos, "ssh-rsa");
            write(dos, rsa.getPublicExponent());
            write(dos, rsa.getModulus());
            dos.close();
            return base64Encode(baos.toByteArray());
        } else {
            throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
        }
    } catch (IOException e) {
        throw new FailedLoginException("Unable to check public key");
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) FailedLoginException(javax.security.auth.login.FailedLoginException) RSAPublicKey(java.security.interfaces.RSAPublicKey) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 10 with RSAKey

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

the class KeyUtil method getKeySize.

/**
     * Returns the key size of the given key object in bits.
     *
     * @param key the key object, cannot be null
     * @return the key size of the given key object in bits, or -1 if the
     *       key size is not accessible
     */
public static final int getKeySize(Key key) {
    int size = -1;
    if (key instanceof Length) {
        try {
            Length ruler = (Length) key;
            size = ruler.length();
        } catch (UnsupportedOperationException usoe) {
        // ignore the exception
        }
        if (size >= 0) {
            return size;
        }
    }
    // try to parse the length from key specification
    if (key instanceof SecretKey) {
        SecretKey sk = (SecretKey) key;
        String format = sk.getFormat();
        if ("RAW".equals(format) && sk.getEncoded() != null) {
            size = (sk.getEncoded().length * 8);
        }
    // Otherwise, it may be a unextractable key of PKCS#11, or
    // a key we are not able to handle.
    } else if (key instanceof RSAKey) {
        RSAKey pubk = (RSAKey) key;
        size = pubk.getModulus().bitLength();
    } else if (key instanceof ECKey) {
        ECKey pubk = (ECKey) key;
        size = pubk.getParams().getOrder().bitLength();
    } else if (key instanceof DSAKey) {
        DSAKey pubk = (DSAKey) key;
        // params can be null
        DSAParams params = pubk.getParams();
        size = (params != null) ? params.getP().bitLength() : -1;
    } else if (key instanceof DHKey) {
        DHKey pubk = (DHKey) key;
        size = pubk.getParams().getP().bitLength();
    }
    return size;
}
Also used : SecretKey(javax.crypto.SecretKey) RSAKey(java.security.interfaces.RSAKey) DSAKey(java.security.interfaces.DSAKey) ECKey(java.security.interfaces.ECKey) DSAParams(java.security.interfaces.DSAParams) DHKey(javax.crypto.interfaces.DHKey)

Aggregations

RSAKey (java.security.interfaces.RSAKey)30 PublicKey (java.security.PublicKey)17 ECKey (java.security.interfaces.ECKey)17 PrivateKey (java.security.PrivateKey)15 KeyFactory (java.security.KeyFactory)14 SecretKey (javax.crypto.SecretKey)11 Key (java.security.Key)10 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 RSAPublicKey (java.security.interfaces.RSAPublicKey)8 X509Certificate (java.security.cert.X509Certificate)7 Certificate (java.security.cert.Certificate)6 ExportResult (android.security.keymaster.ExportResult)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 CertificateFactory (java.security.cert.CertificateFactory)5 ECPublicKey (java.security.interfaces.ECPublicKey)5 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)5 IOException (java.io.IOException)4 BigInteger (java.math.BigInteger)4 CERTRecord (org.xbill.DNS.CERTRecord)4 DSAKey (java.security.interfaces.DSAKey)3