use of java.security.KeyStore.PasswordProtection in project robovm by robovm.
the class TestKeyStore method privateKey.
/**
* Return the only private key in a keystore for the given
* algorithms. Throws IllegalStateException if there are are more
* or less than one.
*/
public static PrivateKeyEntry privateKey(KeyStore keyStore, char[] keyPassword, String keyAlgorithm, String signatureAlgorithm) {
try {
PrivateKeyEntry found = null;
PasswordProtection password = new PasswordProtection(keyPassword);
for (String alias : Collections.list(keyStore.aliases())) {
if (!keyStore.entryInstanceOf(alias, PrivateKeyEntry.class)) {
continue;
}
PrivateKeyEntry privateKey = (PrivateKeyEntry) keyStore.getEntry(alias, password);
if (!privateKey.getPrivateKey().getAlgorithm().equals(keyAlgorithm)) {
continue;
}
X509Certificate certificate = (X509Certificate) privateKey.getCertificate();
if (!certificate.getSigAlgName().contains(signatureAlgorithm)) {
continue;
}
if (found != null) {
throw new IllegalStateException("KeyStore has more than one private key for " + " keyAlgorithm: " + keyAlgorithm + " signatureAlgorithm: " + signatureAlgorithm + "\nfirst: " + found.getPrivateKey() + "\nsecond: " + privateKey.getPrivateKey());
}
found = privateKey;
}
if (found == null) {
throw new IllegalStateException("KeyStore contained no private key for " + " keyAlgorithm: " + keyAlgorithm + " signatureAlgorithm: " + signatureAlgorithm);
}
return found;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.security.KeyStore.PasswordProtection in project robovm by robovm.
the class KeyStoreTest method testKeyStoreCreate.
public void testKeyStoreCreate() {
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(algorithmName);
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
keyStore.load(null, "the secret password".toCharArray());
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (CertificateException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
CertificateFactory certificateFactory = null;
try {
certificateFactory = CertificateFactory.getInstance("X.509");
} catch (CertificateException e) {
fail(e.getMessage());
}
Certificate certificate = null;
try {
certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(encodedCertificate.getBytes()));
} catch (CertificateException e) {
fail(e.getMessage());
}
KeyPairGenerator generator = null;
try {
generator = KeyPairGenerator.getInstance(certificate.getPublicKey().getAlgorithm());
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
}
KeyPair keyPair = generator.generateKeyPair();
PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(keyPair.getPrivate(), new Certificate[] { certificate });
try {
keyStore.setEntry("aPrivateKey", privateKeyEntry, new PasswordProtection("the key password".toCharArray()));
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
assertTrue(keyStore.containsAlias("aPrivateKey"));
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
PrivateKeyEntry entry = (PrivateKeyEntry) keyStore.getEntry("aPrivateKey", new PasswordProtection("the key password".toCharArray()));
PrivateKey privateKey = entry.getPrivateKey();
assertEquals(keyPair.getPrivate(), privateKey);
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (UnrecoverableEntryException e) {
fail(e.getMessage());
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
keyStore.store(stream, "the keystore password".toCharArray());
assertTrue("keystore not written", stream.size() > 0);
} catch (KeyStoreException e) {
fail(e.getMessage());
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (CertificateException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
}
Aggregations