Search in sources :

Example 1 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method decrypt.

/**
     * Decrypts an entity with the provided certificates' private key.
     * @param encryptedEntity The entity that will be decrypted.
     * @param decryptingCertificate The certificates whose private keys will be used to decrypt the message.
     * @return A MimeEntity containing the decrypted part.
     */
public MimeEntity decrypt(MimeEntity encryptedEntity, Collection<X509CertificateEx> decryptingCertificates) {
    if (decryptingCertificates == null || decryptingCertificates.size() == 0) {
        throw new IllegalArgumentException();
    }
    MimeEntity retEntity = null;
    try {
        if (LOGGER.isDebugEnabled()) {
            final byte[] encryptedContent = encryptedEntity.getContentAsBytes();
            writePreDecrypt(encryptedContent);
        }
        final SMIMEEnveloped m = new SMIMEEnveloped(encryptedEntity);
        if (!this.isAllowedEncryptionAlgorithm(m.getEncryptionAlgOID()))
            throw new NHINDException(MimeError.DisallowedEncryptionAlgorithm, "The encryption algorithm " + m.getEncryptionAlgOID() + " is not allowed");
        for (X509CertificateEx decryptCert : decryptingCertificates) {
            final RecipientId recId = generateRecipientSelector(decryptCert);
            final RecipientInformationStore recipients = m.getRecipientInfos();
            final DirectRecipientInformation recipient = decFactory.createInstance(recipients.get(recId), m);
            if (recipient == null)
                continue;
            final byte[] decryptedPayload = recipient.getDecryptedContent(decryptCert.getPrivateKey());
            if (LOGGER.isDebugEnabled()) {
                writePostDecrypt(decryptedPayload);
            }
            final ByteArrayInputStream inStream = new ByteArrayInputStream(decryptedPayload);
            retEntity = new MimeEntity(inStream);
            break;
        }
    } catch (MessagingException e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    if (retEntity == null) {
        throw new NHINDException(MimeError.Unexpected, "None of the the provided decryption certs were found in message's RecipientsInfo set.");
    }
    return retEntity;
}
Also used : RecipientId(org.bouncycastle.cms.RecipientId) MessagingException(javax.mail.MessagingException) DirectRecipientInformation(org.nhindirect.stagent.cryptography.activekeyops.DirectRecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) NHINDException(org.nhindirect.stagent.NHINDException) MessagingException(javax.mail.MessagingException) MimeException(org.nhindirect.stagent.mail.MimeException) NHINDException(org.nhindirect.stagent.NHINDException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) RecipientInformationStore(org.bouncycastle.cms.RecipientInformationStore) MimeException(org.nhindirect.stagent.mail.MimeException)

Example 2 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class DefaultDirectRecipientInformationFactory_createInstanceTest method testInstanceTest_emptyProvider_assertDefaultProvider.

public void testInstanceTest_emptyProvider_assertDefaultProvider() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final DefaultDirectRecipientInformationFactory factory = new DefaultDirectRecipientInformationFactory();
    final DefaultDirectRecipientInformation recInfo = (DefaultDirectRecipientInformation) factory.createInstance(recipient, env);
    assertEquals(CryptoExtensions.getJCEProviderName(), recInfo.encProvider);
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 3 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class DefaultDirectRecipientInformationFactory_createInstanceTest method testInstanceTest_configedProvider_assertConfigedProvider.

public void testInstanceTest_configedProvider_assertConfigedProvider() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final DefaultDirectRecipientInformationFactory factory = new DefaultDirectRecipientInformationFactory("Hello");
    final DefaultDirectRecipientInformation recInfo = (DefaultDirectRecipientInformation) factory.createInstance(recipient, env);
    assertEquals("Hello", recInfo.encProvider);
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 4 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class DefaultDirectRecipientInformation_getDecryptedContentTest method testDecryptedContent_defaultConfig_assertDecrypted.

public void testDecryptedContent_defaultConfig_assertDecrypted() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory();
    final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
    // this won't work unless the data is successfully decrypted
    assertNotNull(recInfo.getDecryptedContent(encCert.getPrivateKey()));
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 5 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class SplitDirectRecipientInformationFactory_createInstanceTest method testInstanceTest_emptyProvider_assertDefaultProvider.

public void testInstanceTest_emptyProvider_assertDefaultProvider() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory();
    final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
    assertEquals(CryptoExtensions.getJCEProviderName(), recInfo.encProvider);
    assertEquals(CryptoExtensions.getJCESensitiveProviderName(), recInfo.keyEncProvider);
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Aggregations

SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)16 RecipientInformation (org.bouncycastle.cms.RecipientInformation)9 X509Certificate (java.security.cert.X509Certificate)5 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)4 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Key (java.security.Key)2 Certificate (java.security.cert.Certificate)2 RecipientId (org.bouncycastle.cms.RecipientId)2 RecipientInformationStore (org.bouncycastle.cms.RecipientInformationStore)2 DirectRecipientInformation (org.nhindirect.stagent.cryptography.activekeyops.DirectRecipientInformation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1