use of com.axway.ats.action.model.ActionException in project ats-framework by Axway.
the class MailSender method initSession.
/**
* Initialize the SMTP session
*
* @throws ActionException
*/
private void initSession() throws ActionException {
// initialize the mail session with the current properties
session = Session.getInstance(mailProperties);
// user can get more debug info with the session's debug mode
session.setDebug(configurator.getMailSessionDebugMode());
// initialize the SMPT transport
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) {
throw new ActionException(e);
}
}
use of com.axway.ats.action.model.ActionException in project ats-framework by Axway.
the class SMimePackageEncryptor method getKeystore.
private KeyStore getKeystore() throws ActionException {
KeyStore ks = null;
try (FileInputStream fis = new FileInputStream(certLocation)) {
ks = KeyStore.getInstance(PKCS12_KEYSTORE_TYPE, BouncyCastleProvider.PROVIDER_NAME);
ks.load(fis, certPassword.toCharArray());
if (aliasOrCN == null) {
Enumeration<String> aliases = ks.aliases();
String alias = aliases.nextElement();
aliasOrCN = alias;
}
} catch (Exception e) {
throw new ActionException(e);
}
return ks;
}
use of com.axway.ats.action.model.ActionException in project ats-framework by Axway.
the class SMimePackageEncryptor method encrypt.
@PublicAtsApi
public Package encrypt(Package source) throws ActionException {
try {
MimeMessage encryptedMessage = new MimeMessage(Session.getInstance(new Properties()));
MimeMessage originalMessage = getMimeMessage(source);
Enumeration<?> hdrEnum = originalMessage.getAllHeaders();
while (hdrEnum.hasMoreElements()) {
Header current = (Header) hdrEnum.nextElement();
encryptedMessage.setHeader(current.getName(), current.getValue());
}
KeyStore ks = getKeystore();
Certificate cer = ks.getCertificate(aliasOrCN);
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
encrypter.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator((X509Certificate) cer).setProvider(BouncyCastleProvider.PROVIDER_NAME));
ASN1ObjectIdentifier encryption = null;
if (encryptionCipher == null) {
// set default. Was CMSAlgorithm.RC2_CBC
encryption = CMSAlgorithm.AES128_CBC;
} else {
encryption = encryptionCipher;
}
MimeBodyPart mp = encrypter.generate(originalMessage, new JceCMSContentEncryptorBuilder(encryption).setProvider(BouncyCastleProvider.PROVIDER_NAME).build());
encryptedMessage.setContent(mp.getContent(), mp.getContentType());
Enumeration<?> mpEnum = mp.getAllHeaders();
while (mpEnum.hasMoreElements()) {
Header current = (Header) mpEnum.nextElement();
encryptedMessage.setHeader(current.getName(), current.getValue());
}
encryptedMessage.saveChanges();
return new MimePackage(encryptedMessage);
} catch (Exception e) {
throw new ActionException(ENCRYPTION_EXCEPTION, e);
}
}
use of com.axway.ats.action.model.ActionException in project ats-framework by Axway.
the class SMimePackageEncryptor method getKeystoreAliases.
@PublicAtsApi
public String[] getKeystoreAliases(String keystoreType) throws ActionException {
FileInputStream is = null;
try {
is = new FileInputStream(new File(this.certLocation));
// Load the keystore
KeyStore keystore = null;
if (PKCS12_KEYSTORE_TYPE.equalsIgnoreCase(keystoreType)) {
Provider bcProvider = (BouncyCastleProvider) Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
if (bcProvider == null || !(bcProvider instanceof BouncyCastleProvider)) {
throw new RuntimeException("BounceCastle security provider seems not to be registered anymore " + "as it is done on SMimePackageEncryptor loading. It is required in order to use secure " + "mail operations");
}
keystore = KeyStore.getInstance(keystoreType, bcProvider);
} else {
keystore = KeyStore.getInstance(keystoreType);
}
keystore.load(is, this.certPassword.toCharArray());
// get the aliases
List<String> aliases = new ArrayList<String>();
Enumeration<String> alEnum = keystore.aliases();
while (alEnum.hasMoreElements()) {
aliases.add(alEnum.nextElement());
}
return aliases.toArray(new String[aliases.size()]);
} catch (Exception e) {
throw new ActionException(e);
} finally {
IoUtils.closeStream(is);
}
}
use of com.axway.ats.action.model.ActionException in project ats-framework by Axway.
the class SMimePackageEncryptor method decrypt.
@PublicAtsApi
public Package decrypt(Package sourcePackage) throws ActionException {
// for connection management to IMAP store
boolean storeReconnected = false;
if (sourcePackage instanceof MimePackage) {
try {
storeReconnected = ((MimePackage) sourcePackage).reconnectStoreIfClosed();
} catch (MessagingException ex) {
throw new ActionException("Could not reopen IMAP connection", ex);
}
}
try {
KeyStore ks = getKeystore();
RecipientId recId = new JceKeyTransRecipientId((X509Certificate) ks.getCertificate(aliasOrCN));
MimeMessage msg = getMimeMessage(sourcePackage);
SMIMEEnveloped m = new SMIMEEnveloped(msg);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
PrivateKey privateKey = (PrivateKey) ks.getKey(aliasOrCN, certPassword.toCharArray());
JceKeyTransRecipient jceKey = new JceKeyTransEnvelopedRecipient(privateKey).setProvider(BouncyCastleProvider.PROVIDER_NAME);
MimeBodyPart result = null;
try {
result = SMIMEUtil.toMimeBodyPart(recipient.getContent(jceKey));
if (LOG.isDebugEnabled()) {
LOG.debug("Successfully decrypted message with subject '" + msg.getSubject() + "' with private key alias: " + aliasOrCN);
}
} catch (SMIMEException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Could not decrypt message with subject '" + sourcePackage.getSubject() + "' with private key alias '" + aliasOrCN + "'", e);
}
}
SMIMESigned signedMessage = null;
MimeMessage decryptedMsg = new MimeMessage(Session.getInstance(new Properties()));
if (result != null) {
Object content = result.getContent();
Enumeration<?> hLineEnum = msg.getAllHeaderLines();
while (hLineEnum.hasMoreElements()) {
decryptedMsg.addHeaderLine((String) hLineEnum.nextElement());
}
decryptedMsg.setContent(content, result.getContentType());
// in order getPlainTextBody getHtmlTextBody to work as they do not work with attachments
decryptedMsg.removeHeader("Content-Disposition");
// check if the message is signed
try {
if (content instanceof MimeMultipart) {
MimeMultipart multipartContent = (MimeMultipart) content;
if (multipartContent.getContentType() != null && multipartContent.getContentType().toLowerCase().contains(CONTENT_TYPE_MULTIPART_SIGNED)) {
signedMessage = new SMIMESigned(multipartContent);
}
} else if (content instanceof SMIMESigned) {
signedMessage = (SMIMESigned) content;
} else if (content instanceof BASE64DecoderStream) {
// com.sun.mail.util.BASE64DecoderStream - JavaMail API dependency. Seems still available
// in JavaMail 2.0 so not an issue if using other non-Oracle/OpenJDK JVMs
// will throw exception if not signed
signedMessage = new SMIMESigned(decryptedMsg);
}
} catch (Exception e) {
// the message is not signed
// log.debug( "Could not construct signed message instance", e );
}
}
if (signedMessage != null) {
// remove signature from the message
decryptedMsg.setContent(signedMessage.getContent().getContent(), signedMessage.getContent().getContentType());
MimePackage mimePackage = new MimePackage(decryptedMsg);
// keep the SMIMESigned message for further signature verification
mimePackage.setSMIMESignedMessage(signedMessage);
return mimePackage;
}
return new MimePackage(decryptedMsg);
} catch (Exception e) {
throw new ActionException(DECRYPTION_EXCEPTION, e);
} finally {
if (storeReconnected) {
// and sourcePackage should be instanceof MimePackage
try {
((MimePackage) sourcePackage).closeStoreConnection(true);
} catch (MessagingException ex) {
// do not hide possible exception thrown in catch block
LOG.debug(ex);
}
}
}
}
Aggregations