@Test public void testSign_Base64 () throws Exception { final MimeBodyPart aPart = new MimeBodyPart (); aPart.setText ("Hello world", StandardCharsets.ISO_8859_1.name ()); final MimeBodyPart aSigned = AS2Helper.getCryptoHelper () .sign (aPart, (X509Certificate) PKE.getCertificate (), PKE.getPrivateKey (), ECryptoAlgorithmSign.DIGEST_SHA_256, false, false, EContentTransferEncoding.BASE64); assertNotNull (aSigned); final String sBoundary = AS2HttpHelper.parseContentType (aSigned.getContentType ()).getParameter ("boundary"); assertNotNull (sBoundary); final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream (); aSigned.writeTo (aBAOS); final String sExpectedStart = "Content-Type: multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha-256; \r\n" + "\tboundary=\"" + sBoundary + "\"\r\n" + "\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: text/plain; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "Hello world\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=\"smime.p7s\"\r\n" + "Content-Description: S/MIME Cryptographic Signature\r\n" + "\r\n" + "MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0BBwEAADGCAqgw\r\n" + "ggKkAgEBMIHDMIG6MSMwIQYJKoZIhvcNAQkBFhRzZXJ2aWNlQG1lbmRlbHNvbi5kZTELMAkGA1UE\r\n" + "BhMCREUxDzANBgNVBAgMBkJlcmxpbjEPMA0GA1UEBwwGQmVybGluMSIwIAYDVQQKDBltZW5kZWxz\r\n" + "b24tZS1jb21tZXJjZSBHbWJIMSEwHwYDVQQLDBhEbyBub3QgdXNlIGluIHByb2R1Y3Rpb24xHTAb\r\n" + "BgNVBAMMFG1lbmRlbHNvbiB0ZXN0IGtleSAzAgRaKlscMA0GCWCGSAFlAwQCAQUAoIG2MBgGCSqG\r\n"; final String sExpectedEnd = "\r\n" + "--" + sBoundary + "--\r\n"; final String sReal = aBAOS.getAsString (StandardCharsets.ISO_8859_1); assertTrue (sReal.startsWith (sExpectedStart)); assertTrue (sReal.endsWith (sExpectedEnd)); }
@Nonnull public 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) throws GeneralSecurityException, SMIMEException, MessagingException, OperatorCreationException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (aPrivateKey, "PrivateKey"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.sign; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; includeCertificateInSignedContent=" + bIncludeCertificateInSignedContent + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ICommonsList <X509Certificate> aCertList = new CommonsArrayList <> (aX509Cert); final JcaCertStore aCertStore = new JcaCertStore (aCertList); final ASN1EncodableVector aSignedAttrs = new ASN1EncodableVector (); final SMIMECapabilityVector aCapabilities = new SMIMECapabilityVector (); aCapabilities.addCapability (eAlgorithm.getOID ()); aSignedAttrs.add (new SMIMECapabilitiesAttribute (aCapabilities)); final SMIMESignedGenerator aSGen = new SMIMESignedGenerator (bUseOldRFC3851MicAlgs ? SMIMESignedGenerator.RFC3851_MICALGS : SMIMESignedGenerator.RFC5751_MICALGS); aSGen.setContentTransferEncoding (eCTE.getID ()); aSGen.addSignerInfoGenerator (new JcaSimpleSignerInfoGeneratorBuilder ().setProvider (m_sSecurityProviderName) .setSignedAttributeGenerator (new AttributeTable (aSignedAttrs)) .build (eAlgorithm.getSignAlgorithmName (), aPrivateKey, aX509Cert)); if (bIncludeCertificateInSignedContent) { aSGen.addCertificates (aCertStore); } final MimeMultipart aSignedData = aSGen.generate (aPart); final MimeBodyPart aSignedPart = new MimeBodyPart (); aSignedPart.setContent (aSignedData); aSignedPart.setHeader (CHttpHeader.CONTENT_TYPE, aSignedData.getContentType ()); return aSignedPart; }
BCCryptoHelper implements ICryptoHelper { @Nonnull public 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) throws GeneralSecurityException, SMIMEException, MessagingException, OperatorCreationException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (aPrivateKey, "PrivateKey"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.sign; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; includeCertificateInSignedContent=" + bIncludeCertificateInSignedContent + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ICommonsList <X509Certificate> aCertList = new CommonsArrayList <> (aX509Cert); final JcaCertStore aCertStore = new JcaCertStore (aCertList); final ASN1EncodableVector aSignedAttrs = new ASN1EncodableVector (); final SMIMECapabilityVector aCapabilities = new SMIMECapabilityVector (); aCapabilities.addCapability (eAlgorithm.getOID ()); aSignedAttrs.add (new SMIMECapabilitiesAttribute (aCapabilities)); final SMIMESignedGenerator aSGen = new SMIMESignedGenerator (bUseOldRFC3851MicAlgs ? SMIMESignedGenerator.RFC3851_MICALGS : SMIMESignedGenerator.RFC5751_MICALGS); aSGen.setContentTransferEncoding (eCTE.getID ()); aSGen.addSignerInfoGenerator (new JcaSimpleSignerInfoGeneratorBuilder ().setProvider (m_sSecurityProviderName) .setSignedAttributeGenerator (new AttributeTable (aSignedAttrs)) .build (eAlgorithm.getSignAlgorithmName (), aPrivateKey, aX509Cert)); if (bIncludeCertificateInSignedContent) { aSGen.addCertificates (aCertStore); } final MimeMultipart aSignedData = aSGen.generate (aPart); final MimeBodyPart aSignedPart = new MimeBodyPart (); aSignedPart.setContent (aSignedData); aSignedPart.setHeader (CHttpHeader.CONTENT_TYPE, aSignedData.getContentType ()); return aSignedPart; } }
BCCryptoHelper implements ICryptoHelper { @Nonnull public 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) throws GeneralSecurityException, SMIMEException, MessagingException, OperatorCreationException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (aPrivateKey, "PrivateKey"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.sign; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; includeCertificateInSignedContent=" + bIncludeCertificateInSignedContent + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ICommonsList <X509Certificate> aCertList = new CommonsArrayList <> (aX509Cert); final JcaCertStore aCertStore = new JcaCertStore (aCertList); final ASN1EncodableVector aSignedAttrs = new ASN1EncodableVector (); final SMIMECapabilityVector aCapabilities = new SMIMECapabilityVector (); aCapabilities.addCapability (eAlgorithm.getOID ()); aSignedAttrs.add (new SMIMECapabilitiesAttribute (aCapabilities)); final SMIMESignedGenerator aSGen = new SMIMESignedGenerator (bUseOldRFC3851MicAlgs ? SMIMESignedGenerator.RFC3851_MICALGS : SMIMESignedGenerator.RFC5751_MICALGS); aSGen.setContentTransferEncoding (eCTE.getID ()); aSGen.addSignerInfoGenerator (new JcaSimpleSignerInfoGeneratorBuilder ().setProvider (m_sSecurityProviderName) .setSignedAttributeGenerator (new AttributeTable (aSignedAttrs)) .build (eAlgorithm.getSignAlgorithmName (), aPrivateKey, aX509Cert)); if (bIncludeCertificateInSignedContent) { aSGen.addCertificates (aCertStore); } final MimeMultipart aSignedData = aSGen.generate (aPart); final MimeBodyPart aSignedPart = new MimeBodyPart (); aSignedPart.setContent (aSignedData); aSignedPart.setHeader (CHttpHeader.CONTENT_TYPE, aSignedData.getContentType ()); return aSignedPart; } BCCryptoHelper(); }
BCCryptoHelper implements ICryptoHelper { @Nonnull public 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) throws GeneralSecurityException, SMIMEException, MessagingException, OperatorCreationException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (aPrivateKey, "PrivateKey"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.sign; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; includeCertificateInSignedContent=" + bIncludeCertificateInSignedContent + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ICommonsList <X509Certificate> aCertList = new CommonsArrayList <> (aX509Cert); final JcaCertStore aCertStore = new JcaCertStore (aCertList); final ASN1EncodableVector aSignedAttrs = new ASN1EncodableVector (); final SMIMECapabilityVector aCapabilities = new SMIMECapabilityVector (); aCapabilities.addCapability (eAlgorithm.getOID ()); aSignedAttrs.add (new SMIMECapabilitiesAttribute (aCapabilities)); final SMIMESignedGenerator aSGen = new SMIMESignedGenerator (bUseOldRFC3851MicAlgs ? SMIMESignedGenerator.RFC3851_MICALGS : SMIMESignedGenerator.RFC5751_MICALGS); aSGen.setContentTransferEncoding (eCTE.getID ()); aSGen.addSignerInfoGenerator (new JcaSimpleSignerInfoGeneratorBuilder ().setProvider (m_sSecurityProviderName) .setSignedAttributeGenerator (new AttributeTable (aSignedAttrs)) .build (eAlgorithm.getSignAlgorithmName (), aPrivateKey, aX509Cert)); if (bIncludeCertificateInSignedContent) { aSGen.addCertificates (aCertStore); } final MimeMultipart aSignedData = aSGen.generate (aPart); final MimeBodyPart aSignedPart = new MimeBodyPart (); aSignedPart.setContent (aSignedData); aSignedPart.setHeader (CHttpHeader.CONTENT_TYPE, aSignedData.getContentType ()); return aSignedPart; } 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 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) throws GeneralSecurityException, SMIMEException, MessagingException, OperatorCreationException { ValueEnforcer.notNull (aPart, "MimeBodyPart"); ValueEnforcer.notNull (aX509Cert, "X509Cert"); ValueEnforcer.notNull (aPrivateKey, "PrivateKey"); ValueEnforcer.notNull (eAlgorithm, "Algorithm"); ValueEnforcer.notNull (eCTE, "ContentTransferEncoding"); if (LOGGER.isDebugEnabled ()) LOGGER.debug ("BCCryptoHelper.sign; X509 subject=" + aX509Cert.getSubjectX500Principal ().getName () + "; algorithm=" + eAlgorithm + "; includeCertificateInSignedContent=" + bIncludeCertificateInSignedContent + "; CTE=" + eCTE); aX509Cert.checkValidity (); final ICommonsList <X509Certificate> aCertList = new CommonsArrayList <> (aX509Cert); final JcaCertStore aCertStore = new JcaCertStore (aCertList); final ASN1EncodableVector aSignedAttrs = new ASN1EncodableVector (); final SMIMECapabilityVector aCapabilities = new SMIMECapabilityVector (); aCapabilities.addCapability (eAlgorithm.getOID ()); aSignedAttrs.add (new SMIMECapabilitiesAttribute (aCapabilities)); final SMIMESignedGenerator aSGen = new SMIMESignedGenerator (bUseOldRFC3851MicAlgs ? SMIMESignedGenerator.RFC3851_MICALGS : SMIMESignedGenerator.RFC5751_MICALGS); aSGen.setContentTransferEncoding (eCTE.getID ()); aSGen.addSignerInfoGenerator (new JcaSimpleSignerInfoGeneratorBuilder ().setProvider (m_sSecurityProviderName) .setSignedAttributeGenerator (new AttributeTable (aSignedAttrs)) .build (eAlgorithm.getSignAlgorithmName (), aPrivateKey, aX509Cert)); if (bIncludeCertificateInSignedContent) { aSGen.addCertificates (aCertStore); } final MimeMultipart aSignedData = aSGen.generate (aPart); final MimeBodyPart aSignedPart = new MimeBodyPart (); aSignedPart.setContent (aSignedData); aSignedPart.setHeader (CHttpHeader.CONTENT_TYPE, aSignedData.getContentType ()); return aSignedPart; } 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);  }