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;
}
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);
}
}
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);
}
}
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());
}
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());
}
Aggregations