Search in sources :

Example 1 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 2 with RSAKey

use of java.security.interfaces.RSAKey in project nhin-d by DirectProject.

the class DNSEntryForm method createCertRecord.

/**
	 * Creates a DNS Cert type record.
	 * @param name The record name.  Generally a fully qualified domain name such as host.example.com.
	 * @param ttl The time to live in seconds.
	 * @param ip The ip4 address that the name will resolve.
	 * @return A DNSRecord representing an A type record.
	 * @throws ConfigurationStoreException
	 */
public static DNSRecord createCertRecord(String name, long ttl, int certtype, int keytag, int alg, X509Certificate cert) throws ServiceException {
    if (!name.endsWith("."))
        name = name + ".";
    try {
        int keyTag = 0;
        if (cert.getPublicKey() instanceof RSAKey) {
            final RSAKey key = (RSAKey) cert.getPublicKey();
            byte[] modulus = key.getModulus().toByteArray();
            keyTag = (modulus[modulus.length - 2] << 8) & 0xFF00;
            keyTag |= modulus[modulus.length - 1] & 0xFF;
        }
        final CERTRecord rec = new CERTRecord(Name.fromString(name), DClass.IN, ttl, CERTRecord.PKIX, keyTag, 5, cert.getEncoded());
        return xbillToModelRecord(rec);
    } catch (Exception e) {
        throw new ServiceException("Failed to create DNS CERT record: " + e.getMessage(), e);
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) CERTRecord(org.xbill.DNS.CERTRecord) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

Example 3 with RSAKey

use of java.security.interfaces.RSAKey in project nhin-d by DirectProject.

the class DNSUtils method createX509CERTRecord.

/**
	 * Creates a DNS CERT record containing an X509 public certificate.
	 * @param address The name or address corresponding to the certificate.
	 * @param ttl The time to live in seconds.
	 * @param cert The X509 public certificate to be stored with the name/address. 
	 * @return A DNSRecord representing a CERT type record.
	 * @throws ConfigurationStoreException
	 */
public static DNSRecord createX509CERTRecord(String address, long ttl, X509Certificate cert) throws DNSRecordCreationException {
    if (!address.endsWith("."))
        address = address + ".";
    try {
        int keyTag = 0;
        if (cert.getPublicKey() instanceof RSAKey) {
            RSAKey key = (RSAKey) cert.getPublicKey();
            byte[] modulus = key.getModulus().toByteArray();
            keyTag = (modulus[modulus.length - 2] << 8) & 0xFF00;
            keyTag |= modulus[modulus.length - 1] & 0xFF;
        }
        CERTRecord rec = new CERTRecord(Name.fromString(address), DClass.IN, ttl, CERTRecord.PKIX, keyTag, 5, /*public key alg, RFC 4034*/
        cert.getEncoded());
        return fromWire(rec.toWireCanonical());
    } catch (Exception e) {
        throw new DNSRecordCreationException("Failed to create DNS CERT record: " + e.getMessage(), e);
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) CERTRecord(org.xbill.DNS.CERTRecord) DNSRecordCreationException(org.nhindirect.config.model.exceptions.DNSRecordCreationException) IOException(java.io.IOException) DNSRecordCreationException(org.nhindirect.config.model.exceptions.DNSRecordCreationException)

Example 4 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 5 with RSAKey

use of java.security.interfaces.RSAKey in project platform_frameworks_base by android.

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)

Aggregations

RSAKey (java.security.interfaces.RSAKey)37 ECKey (java.security.interfaces.ECKey)22 PublicKey (java.security.PublicKey)21 PrivateKey (java.security.PrivateKey)17 SecretKey (javax.crypto.SecretKey)15 KeyFactory (java.security.KeyFactory)14 Key (java.security.Key)13 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 DSAKey (java.security.interfaces.DSAKey)8 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 BigInteger (java.math.BigInteger)5 CertificateFactory (java.security.cert.CertificateFactory)5 ECPublicKey (java.security.interfaces.ECPublicKey)5 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)5 IOException (java.io.IOException)4 DSAParams (java.security.interfaces.DSAParams)4