use of com.adaptris.security.exc.AdaptrisSecurityException in project interlok by adaptris.
the class KeystoreProxyImp method importCertificateChain.
/**
* Import a certificate chain from a file, giving it the assigned alias.
* <p>
* This deals with certificate chains as used by Netscape Navigator and
* Microsoft Internet Explorer.
* <p>
* Certificate Chains are only appropriate for keystore <code>keyEntry</code>
* types.
* <p>
* This assumes that a <code>keyEntry</code> with the alias
* <code>alias</code> has already been created, and the secret key
* associated with this <code>keyEntry</code> is protected by
* <code>keyPassword</code>
*
* @param keyPassword the password to access the private key
* @param alias the alias to be assigned
* @param in the Certificate Chain file to be imported
* @throws AdaptrisSecurityException for any error
* @see #setPrivateKey(String, PrivateKey, char[], Certificate[])
*/
public void importCertificateChain(String alias, char[] keyPassword, InputStream in) throws AdaptrisSecurityException {
try (PemReader pemReader = new PemReader(new InputStreamReader(in))) {
// ,, Constants.SECURITY_PROVIDER);
CertificateFactory cf = CertificateFactory.getInstance(Constants.KEYSTORE_X509);
Collection<?> certs = cf.generateCertificates(in);
Certificate[] pkcs7b = certs.toArray(new Certificate[0]);
PrivateKey pkey = this.getPrivateKey(alias, keyPassword);
if (pkey == null) {
throw new Exception("No Private key for alias " + alias);
}
this.setPrivateKey(alias, pkey, keyPassword, pkcs7b);
} 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 StdOutput method formatBase64.
/**
* Return the encrypted message ready for immediate writing to file.
*
* @return the bytes ready for writing.
* @throws AdaptrisSecurityException if an error occurs
*/
private byte[] formatBase64() throws EncryptException {
DataOutputStream out = null;
ByteArrayOutputStream byteStream = null;
byte[] returnBytes = null;
try {
byteStream = new ByteArrayOutputStream();
out = new DataOutputStream(byteStream);
write(out, getSessionVector());
write(out, getSessionKey());
write(out, getEncryptedData(false) == null ? getDecryptedData(false) : getEncryptedData(false));
write(out, getSignature());
returnBytes = Base64.encodeBase64(byteStream.toByteArray());
} catch (Exception e) {
throw new EncryptException(e);
} finally {
try {
if (out != null) {
out.close();
}
if (byteStream != null) {
byteStream.close();
}
} catch (Exception ignored) {
;
}
}
return returnBytes;
}
use of com.adaptris.security.exc.AdaptrisSecurityException in project interlok by adaptris.
the class StdSecurityService method encrypt.
private Output encrypt(byte[] payload, PrivateKey pk, CertificateHandler ch) throws AdaptrisSecurityException {
StdOutput output = new StdOutput(Output.ENCRYPTED);
try {
KeyGenerator kg = KeyGenerator.getInstance(getCipherName(alg.getAlgorithm()));
kg.init(alg.getKeyLength(), SecurityUtil.getSecureRandom());
SecretKey sessionKey = kg.generateKey();
Cipher dataCipher = Cipher.getInstance(alg.getAlgorithm());
/*,
Constants.SECURITY_PROVIDER);*/
dataCipher.init(Cipher.ENCRYPT_MODE, sessionKey);
byte[] encryptedBody = dataCipher.doFinal(payload);
Cipher keyCipher = Cipher.getInstance(ch.getKeyAlgorithm());
/*,
Constants.SECURITY_PROVIDER);*/
keyCipher.init(Cipher.ENCRYPT_MODE, ch.getPublicKey(), SecurityUtil.getSecureRandom());
byte[] encryptedSessionKey = keyCipher.doFinal(sessionKey.getEncoded());
output.setSessionKey(encryptedSessionKey);
output.setSessionVector(dataCipher.getIV());
output.setEncryptedData(encryptedBody);
} catch (Exception e) {
throw new EncryptException(e);
}
return output;
}
use of com.adaptris.security.exc.AdaptrisSecurityException in project interlok by adaptris.
the class CoreSecurityService method retrieveRemotePartner.
final Alias retrieveRemotePartner(AdaptrisMessage m) throws AdaptrisSecurityException {
Alias rpa = remotePartnerAlias;
if (m.headersContainsKey(getRemotePartnerMetadataKey())) {
String aliasName = m.getMetadataValue(getRemotePartnerMetadataKey());
log.debug("Message metadata overrides configured remote partner with [" + aliasName + "]");
rpa = new Alias(aliasName);
}
if (rpa == null) {
throw new AdaptrisSecurityException("No Remote Partner alias");
}
return rpa;
}
use of com.adaptris.security.exc.AdaptrisSecurityException in project interlok by adaptris.
the class CoreSecurityService method initService.
@Override
protected final void initService() throws CoreException {
try {
pkPassword = getPrivateKeyPasswordProvider().retrievePrivateKeyPassword();
} catch (PasswordException e) {
throw new CoreException("Could not get password using " + getPrivateKeyPasswordProvider().getClass().getCanonicalName(), e);
}
try {
if (isEmpty(localPartner)) {
throw new CoreException("No Local Partner configured");
}
localPartnerAlias = new Alias(localPartner, pkPassword);
if (isEmpty(remotePartner)) {
log.warn("Remote partner not configured, " + "must be set individually as message metadata");
} else {
remotePartnerAlias = new Alias(remotePartner);
}
SecurityServiceFactory factory = securityFactory;
if (factory == null) {
factory = SecurityServiceFactory.defaultInstance();
}
service = factory.createService();
for (Iterator i = keystoreUrls.iterator(); i.hasNext(); ) {
ConfiguredKeystore url = (ConfiguredKeystore) i.next();
service.registerKeystore(url);
}
service.setEncryptionAlgorithm(encryptionAlgorithm);
if (successId != null && failId != null) {
branchingEnabled = true;
} else {
log.debug("No Success Id or Fail Id, branching disabled");
}
} catch (AdaptrisSecurityException e) {
throw new CoreException(e);
}
}
Aggregations