Search in sources :

Example 1 with ActionException

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);
    }
}
Also used : ActionException(com.axway.ats.action.model.ActionException) NoSuchProviderException(javax.mail.NoSuchProviderException)

Example 2 with ActionException

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;
}
Also used : ActionException(com.axway.ats.action.model.ActionException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) MessagingException(javax.mail.MessagingException) ActionException(com.axway.ats.action.model.ActionException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException)

Example 3 with ActionException

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);
    }
}
Also used : JceCMSContentEncryptorBuilder(org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder) ActionException(com.axway.ats.action.model.ActionException) Properties(java.util.Properties) KeyStore(java.security.KeyStore) MessagingException(javax.mail.MessagingException) ActionException(com.axway.ats.action.model.ActionException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) JceKeyTransRecipientInfoGenerator(org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator) MimePackage(com.axway.ats.action.objects.MimePackage) Header(javax.mail.Header) SMIMEEnvelopedGenerator(org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator) MimeMessage(javax.mail.internet.MimeMessage) MimeBodyPart(javax.mail.internet.MimeBodyPart) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 4 with ActionException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) ActionException(com.axway.ats.action.model.ActionException) File(java.io.File) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) MessagingException(javax.mail.MessagingException) ActionException(com.axway.ats.action.model.ActionException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) Provider(java.security.Provider) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 5 with ActionException

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);
            }
        }
    }
}
Also used : JceKeyTransRecipient(org.bouncycastle.cms.jcajce.JceKeyTransRecipient) SMIMESigned(org.bouncycastle.mail.smime.SMIMESigned) JceKeyTransRecipientId(org.bouncycastle.cms.jcajce.JceKeyTransRecipientId) RecipientId(org.bouncycastle.cms.RecipientId) PrivateKey(java.security.PrivateKey) MessagingException(javax.mail.MessagingException) BASE64DecoderStream(com.sun.mail.util.BASE64DecoderStream) JceKeyTransRecipientId(org.bouncycastle.cms.jcajce.JceKeyTransRecipientId) ActionException(com.axway.ats.action.model.ActionException) JceKeyTransEnvelopedRecipient(org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient) Properties(java.util.Properties) KeyStore(java.security.KeyStore) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) MessagingException(javax.mail.MessagingException) ActionException(com.axway.ats.action.model.ActionException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) MimePackage(com.axway.ats.action.objects.MimePackage) RecipientInformation(org.bouncycastle.cms.RecipientInformation) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) RecipientInformationStore(org.bouncycastle.cms.RecipientInformationStore) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) MimeBodyPart(javax.mail.internet.MimeBodyPart) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

ActionException (com.axway.ats.action.model.ActionException)8 MessagingException (javax.mail.MessagingException)7 KeyStore (java.security.KeyStore)6 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)6 MimePackage (com.axway.ats.action.objects.MimePackage)5 PublicAtsApi (com.axway.ats.common.PublicAtsApi)5 FileInputStream (java.io.FileInputStream)3 PrivateKey (java.security.PrivateKey)3 X509Certificate (java.security.cert.X509Certificate)3 MimeMessage (javax.mail.internet.MimeMessage)3 Certificate (java.security.cert.Certificate)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 MailTransportListener (com.axway.ats.action.mail.model.MailTransportListener)1 DELIVERY_STATE (com.axway.ats.action.mail.model.MailTransportListener.DELIVERY_STATE)1 WrongPackageException (com.axway.ats.action.objects.model.WrongPackageException)1 BASE64DecoderStream (com.sun.mail.util.BASE64DecoderStream)1