Search in sources :

Example 11 with UnrecoverableEntryException

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());
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyPairGenerator(java.security.KeyPairGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) UnrecoverableEntryException(java.security.UnrecoverableEntryException) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) PasswordProtection(java.security.KeyStore.PasswordProtection) Certificate(java.security.cert.Certificate)

Example 12 with UnrecoverableEntryException

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");
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 13 with UnrecoverableEntryException

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);
    }
}
Also used : UnrecoverableEntryException(java.security.UnrecoverableEntryException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 14 with UnrecoverableEntryException

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());
}
Also used : UnrecoverableEntryException(java.security.UnrecoverableEntryException)

Example 15 with UnrecoverableEntryException

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());
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyPairGenerator(java.security.KeyPairGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) UnrecoverableEntryException(java.security.UnrecoverableEntryException) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) PasswordProtection(java.security.KeyStore.PasswordProtection) Certificate(java.security.cert.Certificate)

Aggregations

UnrecoverableEntryException (java.security.UnrecoverableEntryException)18 KeyStoreException (java.security.KeyStoreException)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)16 IOException (java.io.IOException)10 CertificateException (java.security.cert.CertificateException)10 KeyStore (java.security.KeyStore)8 KeyManagementException (java.security.KeyManagementException)5 TrustManager (javax.net.ssl.TrustManager)4 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)4 X509TrustManager (javax.net.ssl.X509TrustManager)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PasswordProtection (java.security.KeyStore.PasswordProtection)3 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)3 Certificate (java.security.cert.Certificate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 KeyPair (java.security.KeyPair)2 KeyPairGenerator (java.security.KeyPairGenerator)2 Entry (java.security.KeyStore.Entry)2 ProtectionParameter (java.security.KeyStore.ProtectionParameter)2