use of java.security.KeyStore.Entry in project android_frameworks_base by ParanoidAndroid.
the class AndroidKeyStoreTest method testKeyStore_SetKeyEntry_Encrypted_Success.
public void testKeyStore_SetKeyEntry_Encrypted_Success() throws Exception {
setupPassword();
mKeyStore.load(null, null);
final CertificateFactory f = CertificateFactory.getInstance("X.509");
final Certificate caCert = f.generateCertificate(new ByteArrayInputStream(FAKE_CA_1));
KeyFactory keyFact = KeyFactory.getInstance("RSA");
PrivateKey privKey = keyFact.generatePrivate(new PKCS8EncodedKeySpec(FAKE_KEY_1));
final Certificate[] chain = new Certificate[2];
chain[0] = f.generateCertificate(new ByteArrayInputStream(FAKE_USER_1));
chain[1] = caCert;
mKeyStore.setKeyEntry(TEST_ALIAS_1, privKey, null, chain);
Entry actualEntry = mKeyStore.getEntry(TEST_ALIAS_1, null);
assertNotNull("Retrieved entry should exist", actualEntry);
assertTrue("Retrieved entry should be of type PrivateKeyEntry", actualEntry instanceof PrivateKeyEntry);
PrivateKeyEntry actual = (PrivateKeyEntry) actualEntry;
assertPrivateKeyEntryEquals(actual, FAKE_KEY_1, FAKE_USER_1, FAKE_CA_1);
}
use of java.security.KeyStore.Entry in project platformlayer by platformlayer.
the class KeyStoreEncryptionStore method main.
public static void main(String[] args) throws Exception {
if (!args[0].equals("explode")) {
throw new IllegalStateException();
}
char[] password = "notasecret".toCharArray();
ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
KeyStore keyStore = KeyStoreUtils.load(new File(args[1]));
File dest = new File(args[2]);
dest.mkdirs();
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isKeyEntry(alias)) {
Entry entry = keyStore.getEntry(alias, protParam);
PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) entry;
{
X509Certificate[] certificateChain = toX509(privateKeyEntry.getCertificateChain());
String encoded = CertificateUtils.toPem(certificateChain);
File out = new File(dest, alias + ".crt");
Files.write(encoded, out, Charsets.UTF_8);
}
{
PrivateKey key = privateKeyEntry.getPrivateKey();
String encoded = PrivateKeys.toPem(key);
File out = new File(dest, alias + ".key");
Files.write(encoded, out, Charsets.UTF_8);
}
}
if (keyStore.isCertificateEntry(alias)) {
Entry entry = keyStore.getEntry(alias, null);
TrustedCertificateEntry trustedCertificateEntry = (TrustedCertificateEntry) entry;
X509Certificate cert = (X509Certificate) trustedCertificateEntry.getTrustedCertificate();
String encoded = CertificateUtils.toPem(cert);
File out = new File(dest, alias + ".crt");
Files.write(encoded, out, Charsets.UTF_8);
}
}
}
use of java.security.KeyStore.Entry in project robovm by robovm.
the class KeyStore4Test method testGetEntry.
public void testGetEntry() {
try {
Entry entry = keyStore.getEntry("certalias", null);
assertNotNull("entry is null", entry);
assertTrue("entry is not cert entry", entry instanceof KeyStore.TrustedCertificateEntry);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (UnrecoverableEntryException e) {
fail("unexpected exception: " + e);
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
}
try {
Entry entry = keyStore.getEntry("certalias", new KeyStore.ProtectionParameter() {
});
assertNotNull(entry);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (UnrecoverableEntryException e) {
fail("unexpected exception: " + e);
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (UnsupportedOperationException e) {
// ok
}
try {
Entry entry = keyStore.getEntry("keyalias", new KeyStore.PasswordProtection(new char[] {}));
assertNotNull(entry);
assertTrue(entry instanceof KeyStore.SecretKeyEntry);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (UnrecoverableEntryException e) {
fail("unexpected exception: " + e);
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.getEntry("unknownalias", new KeyStore.PasswordProtection(new char[] {}));
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
} catch (UnrecoverableEntryException e) {
fail("unexpected exception: " + e);
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (UnsupportedOperationException e) {
// also ok
}
try {
keyStore.getEntry(null, new KeyStore.ProtectionParameter() {
});
fail("expected NullPointerException");
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (UnrecoverableEntryException e) {
fail("unexpected exception: " + e);
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (NullPointerException e) {
// ok
}
}
use of java.security.KeyStore.Entry in project platform_frameworks_base by android.
the class AndroidKeyStoreTest method testKeyStore_GetEntry_NullParams_Encrypted_Success.
public void testKeyStore_GetEntry_NullParams_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));
Entry entry = mKeyStore.getEntry(TEST_ALIAS_1, null);
assertNotNull("Entry should exist", entry);
assertTrue("Should be a PrivateKeyEntry", entry instanceof PrivateKeyEntry);
PrivateKeyEntry keyEntry = (PrivateKeyEntry) entry;
assertPrivateKeyEntryEquals(keyEntry, "RSA", FAKE_RSA_KEY_1, FAKE_RSA_USER_1, FAKE_RSA_CA_1);
}
use of java.security.KeyStore.Entry in project platform_frameworks_base by android.
the class AndroidKeyStoreTest method testKeyStore_KeyOperations_Wrap_Encrypted_Success.
public void testKeyStore_KeyOperations_Wrap_Encrypted_Success() throws Exception {
setupPassword();
mKeyStore.load(null, null);
setupKey();
// Test key usage
Entry e = mKeyStore.getEntry(TEST_ALIAS_1, null);
assertNotNull(e);
assertTrue(e instanceof PrivateKeyEntry);
PrivateKeyEntry privEntry = (PrivateKeyEntry) e;
PrivateKey privKey = privEntry.getPrivateKey();
assertNotNull(privKey);
PublicKey pubKey = privEntry.getCertificate().getPublicKey();
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.WRAP_MODE, pubKey);
byte[] expectedKey = new byte[] { 0x00, 0x05, (byte) 0xAA, (byte) 0x0A5, (byte) 0xFF, 0x55, 0x0A };
SecretKey expectedSecret = new SecretKeySpec(expectedKey, "AES");
byte[] wrappedExpected = c.wrap(expectedSecret);
c.init(Cipher.UNWRAP_MODE, privKey);
SecretKey actualSecret = (SecretKey) c.unwrap(wrappedExpected, "AES", Cipher.SECRET_KEY);
assertEquals(Arrays.toString(expectedSecret.getEncoded()), Arrays.toString(actualSecret.getEncoded()));
}
Aggregations