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);
}
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations