use of java.security.UnrecoverableEntryException in project j2objc by google.
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());
}
}
use of java.security.UnrecoverableEntryException in project OpenAttestation by OpenAttestation.
the class SslUtil method createX509TrustManagerWithKeystore.
public static X509TrustManager createX509TrustManagerWithKeystore(SimpleKeystore keystore) throws KeyManagementException {
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(createTrustedSslKeystore(keystore));
TrustManager[] tms = tmf.getTrustManagers();
for (TrustManager tm : tms) {
if (tm instanceof X509TrustManager) {
return (X509TrustManager) tm;
}
}
} catch (NoSuchAlgorithmException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (IOException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (CertificateException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (UnrecoverableEntryException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (KeyStoreException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
}
throw new IllegalArgumentException("TrustManagerFactory did not return an X509TrustManager instance");
}
use of java.security.UnrecoverableEntryException in project OpenAttestation by OpenAttestation.
the class KeystoreCertificateRepository method getCertificate.
/**
* Md5Digest happens to be the alias we use when storing so we can do a direct lookup
* @param fingerprint md5 digest of the certificate you want to retrieve
* @return the X509 Certificate if found, or null if it was not found
* @throws KeyManagementException if there were errors opening, searching, or loading certificates from the keystore
*/
// public X509Certificate getCertificate(Md5Digest fingerprint) throws KeyManagementException {
public X509Certificate getCertificate(String alias) throws KeyManagementException {
// String alias = fingerprint.toString(); // hex md5 fingerprint
try {
if (keystore.containsAlias(alias)) {
if (keystore.isCertificateEntry(alias)) {
// using null instead of password because trusted certificate entries are not password protected (only the keystore itself is protected to provide integrity over the contents); new KeyStore.PasswordProtection(password)
KeyStore.TrustedCertificateEntry entry = (KeyStore.TrustedCertificateEntry) keystore.getEntry(alias, null);
Certificate cert = entry.getTrustedCertificate();
if (cert instanceof X509Certificate) {
return (X509Certificate) cert;
}
}
}
return null;
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException e) {
throw new KeyManagementException(e);
}
}
use of java.security.UnrecoverableEntryException in project robovm by robovm.
the class UnrecoverableEntryExceptionTest method testUnrecoverableEntryException.
/*
* Class under test for void UnrecoverableEntryException()
*/
public void testUnrecoverableEntryException() {
UnrecoverableEntryException tE = new UnrecoverableEntryException();
assertNull("getMessage() must return null.", tE.getMessage());
assertNull("getCause() must return null", tE.getCause());
}
use of java.security.UnrecoverableEntryException 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