@Test public void testEncryptCTE_Base64 () throws Exception { final MimeBodyPart aPart = new MimeBodyPart (); aPart.setText ("Hello world"); final MimeBodyPart aEncrypted = AS2Helper.getCryptoHelper () .encrypt (aPart, CERT_ENCRYPT, ECryptoAlgorithmCrypt.CRYPT_3DES, EContentTransferEncoding.BASE64); final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream (); aEncrypted.writeTo (aBAOS); final String sExpectedStart = "Content-Type: application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=\"smime.p7m\"\r\n" + "Content-Description: S/MIME Encrypted Message\r\n" + "\r\n" + "MIAGCSqGSIb3DQEHA6CAMIACAQAxggHgMIIB3AIBADCBwzCBujEjMCEGCSqGSIb3DQEJARYUc2Vy\r\n" + "dmljZUBtZW5kZWxzb24uZGUxCzAJBgNVBAYTAkRFMQ8wDQYDVQQIDAZCZXJsaW4xDzANBgNVBAcM\r\n" + "BkJlcmxpbjEiMCAGA1UECgwZbWVuZGVsc29uLWUtY29tbWVyY2UgR21iSDEhMB8GA1UECwwYRG8g\r\n" + "bm90IHVzZSBpbiBwcm9kdWN0aW9uMR0wGwYDVQQDDBRtZW5kZWxzb24gdGVzdCBrZXkgNAIEWipb\r\n"; final String sReal = aBAOS.getAsString (StandardCharsets.ISO_8859_1); assertTrue (sReal.startsWith (sExpectedStart)); }
@Nonnull public MimeBodyPart encrypt (@Nonnull final MimeBodyPart aPart, @Nonnull final X509Certificate aX509Cert, @Nonnull final ECryptoAlgorithmCrypt eAlgorithm, @Nonnull final EContentTransferEncoding eCTE) throws GeneralSecurityException, SMIMEException, CMSException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.encrypt; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ASN1ObjectIdentifier aEncAlg = eAlgorithm.getOID (); final SMIMEEnvelopedGenerator aGen = new SMIMEEnvelopedGenerator (); aGen.addRecipientInfoGenerator (new JceKeyTransRecipientInfoGenerator (aX509Cert).setProvider (m_sSecurityProviderName)); aGen.setContentTransferEncoding (eCTE.getID ()); final OutputEncryptor aEncryptor = new JceCMSContentEncryptorBuilder (aEncAlg).setProvider (m_sSecurityProviderName).build (); final MimeBodyPart aEncryptedPart = aGen.generate (aPart, aEncryptor); return aEncryptedPart; }
BCCryptoHelper implements ICryptoHelper { @Nonnull public MimeBodyPart encrypt (@Nonnull final MimeBodyPart aPart, @Nonnull final X509Certificate aX509Cert, @Nonnull final ECryptoAlgorithmCrypt eAlgorithm, @Nonnull final EContentTransferEncoding eCTE) throws GeneralSecurityException, SMIMEException, CMSException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.encrypt; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ASN1ObjectIdentifier aEncAlg = eAlgorithm.getOID (); final SMIMEEnvelopedGenerator aGen = new SMIMEEnvelopedGenerator (); aGen.addRecipientInfoGenerator (new JceKeyTransRecipientInfoGenerator (aX509Cert).setProvider (m_sSecurityProviderName)); aGen.setContentTransferEncoding (eCTE.getID ()); final OutputEncryptor aEncryptor = new JceCMSContentEncryptorBuilder (aEncAlg).setProvider (m_sSecurityProviderName).build (); final MimeBodyPart aEncryptedPart = aGen.generate (aPart, aEncryptor); return aEncryptedPart; } }
BCCryptoHelper implements ICryptoHelper { @Nonnull public MimeBodyPart encrypt (@Nonnull final MimeBodyPart aPart, @Nonnull final X509Certificate aX509Cert, @Nonnull final ECryptoAlgorithmCrypt eAlgorithm, @Nonnull final EContentTransferEncoding eCTE) throws GeneralSecurityException, SMIMEException, CMSException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.encrypt; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ASN1ObjectIdentifier aEncAlg = eAlgorithm.getOID (); final SMIMEEnvelopedGenerator aGen = new SMIMEEnvelopedGenerator (); aGen.addRecipientInfoGenerator (new JceKeyTransRecipientInfoGenerator (aX509Cert).setProvider (m_sSecurityProviderName)); aGen.setContentTransferEncoding (eCTE.getID ()); final OutputEncryptor aEncryptor = new JceCMSContentEncryptorBuilder (aEncAlg).setProvider (m_sSecurityProviderName).build (); final MimeBodyPart aEncryptedPart = aGen.generate (aPart, aEncryptor); return aEncryptedPart; } BCCryptoHelper(); }
BCCryptoHelper implements ICryptoHelper { @Nonnull public MimeBodyPart encrypt (@Nonnull final MimeBodyPart aPart, @Nonnull final X509Certificate aX509Cert, @Nonnull final ECryptoAlgorithmCrypt eAlgorithm, @Nonnull final EContentTransferEncoding eCTE) throws GeneralSecurityException, SMIMEException, CMSException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.encrypt; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ASN1ObjectIdentifier aEncAlg = eAlgorithm.getOID (); final SMIMEEnvelopedGenerator aGen = new SMIMEEnvelopedGenerator (); aGen.addRecipientInfoGenerator (new JceKeyTransRecipientInfoGenerator (aX509Cert).setProvider (m_sSecurityProviderName)); aGen.setContentTransferEncoding (eCTE.getID ()); final OutputEncryptor aEncryptor = new JceCMSContentEncryptorBuilder (aEncAlg).setProvider (m_sSecurityProviderName).build (); final MimeBodyPart aEncryptedPart = aGen.generate (aPart, aEncryptor); return aEncryptedPart; } BCCryptoHelper(); @Nonnull @Nonempty String getSecurityProviderName(); @Nonnull BCCryptoHelper setSecurityProviderName(@Nonnull @Nonempty final String sSecurityProviderName); @Nonnull KeyStore createNewKeyStore(@Nonnull final IKeyStoreType aKeyStoreType); @Nonnull KeyStore loadKeyStore(@Nonnull final IKeyStoreType aKeyStoreType,
                                @Nullable @WillNotClose final InputStream aIS,
                                @Nonnull final char [] aPassword); boolean isEncrypted(@Nonnull final MimeBodyPart aPart); boolean isSigned(@Nonnull final MimeBodyPart aPart); boolean isCompressed(@Nonnull final String sContentType); @Nonnull MIC calculateMIC(@Nonnull final MimeBodyPart aPart,
                           @Nonnull final ECryptoAlgorithmSign eDigestAlgorithm,
                           final boolean bIncludeHeaders); @Nonnull MimeBodyPart decrypt(@Nonnull final MimeBodyPart aPart,
                               @Nonnull final X509Certificate aX509Cert,
                               @Nonnull final PrivateKey aPrivateKey,
                               final boolean bForceDecrypt,
                               @Nonnull final AS2ResourceHelper aResHelper); @Nonnull MimeBodyPart encrypt(@Nonnull final MimeBodyPart aPart,
                               @Nonnull final X509Certificate aX509Cert,
                               @Nonnull final ECryptoAlgorithmCrypt eAlgorithm,
                               @Nonnull final EContentTransferEncoding eCTE); @Nonnull MimeBodyPart sign(@Nonnull final MimeBodyPart aPart,
                            @Nonnull final X509Certificate aX509Cert,
                            @Nonnull final PrivateKey aPrivateKey,
                            @Nonnull final ECryptoAlgorithmSign eAlgorithm,
                            final boolean bIncludeCertificateInSignedContent,
                            final boolean bUseOldRFC3851MicAlgs,
                            @Nonnull final EContentTransferEncoding eCTE); @Nonnull MimeBodyPart verify(@Nonnull final MimeBodyPart aPart,
                              @Nullable final X509Certificate aX509Cert,
                              final boolean bUseCertificateInBodyPart,
                              final boolean bForceVerify,
                              @Nullable final Consumer <X509Certificate> aEffectiveCertificateConsumer,
                              @Nonnull final AS2ResourceHelper aResHelper); }
BCCryptoHelper implements ICryptoHelper { @Nonnull public MimeBodyPart encrypt (@Nonnull final MimeBodyPart aPart, @Nonnull final X509Certificate aX509Cert, @Nonnull final ECryptoAlgorithmCrypt eAlgorithm, @Nonnull final EContentTransferEncoding eCTE) throws GeneralSecurityException, SMIMEException, CMSException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.encrypt; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ASN1ObjectIdentifier aEncAlg = eAlgorithm.getOID (); final SMIMEEnvelopedGenerator aGen = new SMIMEEnvelopedGenerator (); aGen.addRecipientInfoGenerator (new JceKeyTransRecipientInfoGenerator (aX509Cert).setProvider (m_sSecurityProviderName)); aGen.setContentTransferEncoding (eCTE.getID ()); final OutputEncryptor aEncryptor = new JceCMSContentEncryptorBuilder (aEncAlg).setProvider (m_sSecurityProviderName).build (); final MimeBodyPart aEncryptedPart = aGen.generate (aPart, aEncryptor); return aEncryptedPart; } BCCryptoHelper(); @Nonnull @Nonempty String getSecurityProviderName(); @Nonnull BCCryptoHelper setSecurityProviderName(@Nonnull @Nonempty final String sSecurityProviderName); @Nonnull KeyStore createNewKeyStore(@Nonnull final IKeyStoreType aKeyStoreType); @Nonnull KeyStore loadKeyStore(@Nonnull final IKeyStoreType aKeyStoreType,
                                @Nullable @WillNotClose final InputStream aIS,
                                @Nonnull final char [] aPassword); boolean isEncrypted(@Nonnull final MimeBodyPart aPart); boolean isSigned(@Nonnull final MimeBodyPart aPart); boolean isCompressed(@Nonnull final String sContentType); @Nonnull MIC calculateMIC(@Nonnull final MimeBodyPart aPart,
                           @Nonnull final ECryptoAlgorithmSign eDigestAlgorithm,
                           final boolean bIncludeHeaders); @Nonnull MimeBodyPart decrypt(@Nonnull final MimeBodyPart aPart,
                               @Nonnull final X509Certificate aX509Cert,
                               @Nonnull final PrivateKey aPrivateKey,
                               final boolean bForceDecrypt,
                               @Nonnull final AS2ResourceHelper aResHelper); @Nonnull MimeBodyPart encrypt(@Nonnull final MimeBodyPart aPart,
                               @Nonnull final X509Certificate aX509Cert,
                               @Nonnull final ECryptoAlgorithmCrypt eAlgorithm,
                               @Nonnull final EContentTransferEncoding eCTE); @Nonnull MimeBodyPart sign(@Nonnull final MimeBodyPart aPart,
                            @Nonnull final X509Certificate aX509Cert,
                            @Nonnull final PrivateKey aPrivateKey,
                            @Nonnull final ECryptoAlgorithmSign eAlgorithm,
                            final boolean bIncludeCertificateInSignedContent,
                            final boolean bUseOldRFC3851MicAlgs,
                            @Nonnull final EContentTransferEncoding eCTE); @Nonnull MimeBodyPart verify(@Nonnull final MimeBodyPart aPart,
                              @Nullable final X509Certificate aX509Cert,
                              final boolean bUseCertificateInBodyPart,
                              final boolean bForceVerify,
                              @Nullable final Consumer <X509Certificate> aEffectiveCertificateConsumer,
                              @Nonnull final AS2ResourceHelper aResHelper);  }