Search in sources :

Example 16 with AdaptrisSecurityException

use of com.adaptris.security.exc.AdaptrisSecurityException 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)

Example 17 with AdaptrisSecurityException

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

the class KeystoreProxyImp method setCertificate.

/**
 * Assign the given InputStream (contaning a certificate) to the given alias.
 * <p>
 * The InputStream is expected to contain a PEM or DER encoded certificate
 * </p>
 *
 * @param alias the alias of the Certificate
 * @param in the InputStream containing the certificate
 * @throws AdaptrisSecurityException for any error
 * @see #setCertificate(String, Certificate)
 */
public void setCertificate(String alias, InputStream in) throws AdaptrisSecurityException {
    try {
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        Certificate x509 = (X509Certificate) factory.generateCertificate(in);
        this.setCertificate(alias, x509);
    } catch (Exception e) {
        throw KeystoreProxy.wrapException(e);
    }
}
Also used : CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) 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)

Example 18 with AdaptrisSecurityException

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

the class CompositeKeystore method getPrivateKey.

/**
 * @see KeystoreProxy#getPrivateKey(String, char[])
 */
public PrivateKey getPrivateKey(String alias, char[] password) throws AdaptrisSecurityException {
    PrivateKey pk = null;
    if (aliasCache == null) {
        load();
    }
    if (!containsAlias(alias)) {
        return null;
    }
    String lca = alias.toLowerCase();
    AliasListEntry kk = aliasCache.get(lca);
    if (kk != null) {
        if (password == null) {
            logR.trace("No private key password passed as parameter, " + "using keystore password as key password");
        }
        try {
            char[] pw = password == null ? kk.getLocation().getKeystorePassword() : password;
            pk = kk.getProxy().getPrivateKey(lca, pw);
        } catch (Exception e) {
            if (AdaptrisSecurityException.class.isAssignableFrom(e.getClass())) {
                throw (AdaptrisSecurityException) e;
            } else {
                throw new KeystoreException(e);
            }
        }
    }
    return pk;
}
Also used : PrivateKey(java.security.PrivateKey) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) KeystoreException(com.adaptris.security.exc.KeystoreException) KeystoreException(com.adaptris.security.exc.KeystoreException) IOException(java.io.IOException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) GeneralSecurityException(java.security.GeneralSecurityException)

Example 19 with AdaptrisSecurityException

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

the class VersionedHttpsProduceConnection method initialiseClient.

/**
 * @see HttpClientConnection#initialiseClient(java.lang.String)
 */
@Override
public HttpClientTransport initialiseClient(String url) throws HttpException {
    HttpsClient client = new HttpsClient(new URLString(url), protocolVersion);
    try {
        if (getKeystore() != null) {
            KeystoreFactory ksf = KeystoreFactory.getDefault();
            KeystoreLocation ksl = null;
            if (getKeystorePassword() != null) {
                ksl = ksf.create(getKeystore(), Password.decode(getKeystorePassword()).toCharArray());
            } else {
                ksl = ksf.create(getKeystore());
            }
            char[] pkpw = PasswordOverride.discoverPrivateKeyPassword(ksl, getPrivateKeyPasswordProvider());
            if (pkpw != null) {
                client.registerPrivateKeyPassword(pkpw);
            }
            client.registerKeystore(ksf.create(ksl));
        }
    } catch (AdaptrisSecurityException e) {
        throw new HttpException(e);
    }
    client.setAlwaysTrust(getAlwaysTrust());
    return client;
}
Also used : KeystoreLocation(com.adaptris.security.keystore.KeystoreLocation) HttpsClient(com.adaptris.http.HttpsClient) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) KeystoreFactory(com.adaptris.security.keystore.KeystoreFactory) HttpException(com.adaptris.http.HttpException) URLString(com.adaptris.util.URLString)

Example 20 with AdaptrisSecurityException

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

the class EncryptionServiceCase method testFailedEncryptionWithBranch.

@Test
public void testFailedEncryptionWithBranch() throws Exception {
    String url = createKeystore();
    CoreSecurityService input = create();
    applyConfigForTests(input, url);
    input.setFailId(FAIL);
    input.setSuccessId(SUCCESS);
    input.setLocalPartner(NON_EXISTENT_ALIAS);
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(EXAMPLE_MSG);
    execute(input, msg);
    assertEquals(FAIL, msg.getNextServiceId());
    assertTrue(msg.getObjectHeaders().containsKey(CoreConstants.OBJ_METADATA_EXCEPTION));
    assertTrue(msg.getObjectHeaders().get(CoreConstants.OBJ_METADATA_EXCEPTION) instanceof AdaptrisSecurityException);
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) Test(org.junit.Test)

Aggregations

AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)23 CertException (com.adaptris.security.exc.CertException)11 KeystoreException (com.adaptris.security.exc.KeystoreException)10 EncryptException (com.adaptris.security.exc.EncryptException)7 VerifyException (com.adaptris.security.exc.VerifyException)7 IOException (java.io.IOException)7 DecryptException (com.adaptris.security.exc.DecryptException)5 GeneralSecurityException (java.security.GeneralSecurityException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchProviderException (java.security.NoSuchProviderException)5 PrivateKey (java.security.PrivateKey)5 X509Certificate (java.security.cert.X509Certificate)4 Test (org.junit.Test)4 Certificate (java.security.cert.Certificate)3 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)2 HttpException (com.adaptris.http.HttpException)2 HttpsClient (com.adaptris.http.HttpsClient)2 Alias (com.adaptris.security.keystore.Alias)2 KeystoreFactory (com.adaptris.security.keystore.KeystoreFactory)2 KeystoreLocation (com.adaptris.security.keystore.KeystoreLocation)2