Search in sources :

Example 6 with CertException

use of com.adaptris.security.exc.CertException in project interlok by adaptris.

the class KeystoreProxyImp method importPrivateKey.

/**
 * Import a private key from an inputstream, and assign it to the given alias.
 * <p>
 * The key is protected by the given key password
 * </p>
 * <p>
 * The inputstream is expected to contain a PKCS12 object exported from
 * Netscape Navigator / Internet Explorer
 * </p>
 *
 * @param alias the alias of the Certificate
 * @param keyPassword the password to protect the private key
 * @param in InputStream containing the PKCS12 object
 * @param filePassword The password protecting the PKCS12
 * @throws AdaptrisSecurityException for any error
 * @see #setPrivateKey(String, PrivateKey, char[], Certificate[])
 */
public void importPrivateKey(String alias, char[] keyPassword, InputStream in, char[] filePassword) throws AdaptrisSecurityException {
    try {
        // ,, Constants.SECURITY_PROVIDER);
        KeyStore keystore = KeyStore.getInstance(Constants.KEYSTORE_PKCS12);
        keystore.load(in, filePassword);
        Key key = keystore.getKey(alias, keyPassword);
        if (key instanceof PrivateKey) {
            Certificate[] certChain = keystore.getCertificateChain(alias);
            this.setPrivateKey(alias, (PrivateKey) key, keyPassword, certChain);
        }
    } catch (AdaptrisSecurityException e) {
        throw e;
    } catch (Exception e) {
        throw new CertException(e.getMessage(), e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) CertException(com.adaptris.security.exc.CertException) KeyStore(java.security.KeyStore) Key(java.security.Key) PrivateKey(java.security.PrivateKey) KeystoreException(com.adaptris.security.exc.KeystoreException) IOException(java.io.IOException) CertException(com.adaptris.security.exc.CertException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) GeneralSecurityException(java.security.GeneralSecurityException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

CertException (com.adaptris.security.exc.CertException)6 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)4 CertificateHandler (com.adaptris.security.certificate.CertificateHandler)2 KeystoreException (com.adaptris.security.exc.KeystoreException)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnknownHostException (java.net.UnknownHostException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 X509Certificate (java.security.cert.X509Certificate)2 CertificationRequest (org.bouncycastle.asn1.pkcs.CertificationRequest)2 PKCS10CertificationRequest (org.bouncycastle.pkcs.PKCS10CertificationRequest)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 Key (java.security.Key)1 KeyStore (java.security.KeyStore)1 CertificateFactory (java.security.cert.CertificateFactory)1