Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

AS2 Protocol for Business Data Interchange via HTTP, Part 2

DZone's Guide to

AS2 Protocol for Business Data Interchange via HTTP, Part 2

Learn how an AS2 message is composed using the S/MIME format to transfer business data under the AS2 protocol, and why it's better than other B2B protocols.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

The main idea of the AS2 protocol defined in the RFC 4130 is how we can exchange structured business data securely using the HTTP transfer protocol. In part one of this AS2 article series, we looked at an overview of the AS2 (Applicability Statement 2) protocol and why it wins over the other B2B protocols. We, the developers of the B2B integration platform AS2Gateway, have been working with AS2 protocol for quite some time now, and today, in this article, we hope to give some insight on how an AS2 message is composed using S/MIME format with a few lines of Java code.

Without further ado, let’s jump right in. The basic structure of an AS2 message consists of a MIME format inside an HTTP message with a few additional specific AS2 headers. The final structure of the composed AS2 message will be as follows. In this article, we’ll go in a step-by-step manner to create the final encrypted HTTP body starting from a document.

Image title

MIME Message Generation

First of all, we’ll look at a sample MIME message. The following code sample can be used to generate a MIME message in Java mainly using JavaMail and Apache Tika.

Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage finalMessage = new MimeMessage(session);

Tika tika = new Tika();
File file = new File("/home/rajind/sample-text-file.txt");
String mimeType = tika.detect(file);
finalMessage.setDataHandler(new DataHandler(new FileDataSource(file)));
finalMessage.setHeader("Content-Type", mimeType);
finalMessage.setHeader("Content-Transfer-Encoding", "base64");
finalMessage.setFileName(file.getName());

The structure of the output MIME message will look as follows. Note the MIME headers and then the content (which is base64 encoded since we have used the content transfer encoding as base64).

Message-ID: <1642534850.0.1512980924095@rajind-ENVY>
MIME-Version: 1.0
Content-Type: text/plain; name=sample-text-file.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=sample-text-file.txt

c2FtcGxlIHRleHQgY29udGVudCBvbmUK

Signing The MIME Message

Now we’ll look at how S/MIME comes into play. S/MIME provides two security services, namely Digital Signatures and Message Encryption. These two services are the core of S/MIME-based message security. Digital signatures provide, Authentication, Nonrepudiation, and Data Integrity while encryption provides Confidentiality and again Data Integrity. The following snippet shows how the signing of a MIME message occurs. Here we have used the Bouncy Castle S/MIME API, Bouncy Castle Crypto package, and Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation.

// loading identity store
FileInputStream is = new FileInputStream("/home/rajind/Downloads/keystore.jks");
KeyStore identityKeystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "password";
identityKeystore.load(is, password.toCharArray());

// extracting certificate from identity store
X509Certificate signCert = (X509Certificate) identityKeystore.getCertificate("as2gx");
List certList = new ArrayList();
certList.add(signCert);
Store certs = new JcaCertStore(certList);

// create the generator for creating an smime/signed message
SMIMESignedGenerator signer = new SMIMESignedGenerator();
signer.setContentTransferEncoding("base64");

// extracting private key from identity store
Key key = identityKeystore.getKey("as2gx", password.toCharArray());
KeyPair keyPair;
if (key instanceof PrivateKey) {
   Certificate cert = identityKeystore.getCertificate("as2gx");
   PublicKey publicKey = cert.getPublicKey();
   keyPair = new KeyPair(publicKey, (PrivateKey) key);
} else {
   throw new UnrecoverableKeyException("Identity store does not contain keypair for alias " + "as2gx");
}

// add a signer to the generator
signer.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC")
                              .build("SHA1WITHRSA", keyPair.getPrivate(), signCert));

// add our pool of certs and certs (if any) to go with the signature
signer.addCertificates(certs);
MimeMultipart signedMimeMultipart = signer.generate(finalMessage, "BC");
finalMessage = new MimeMessage(session);

// set the content of the signed message
finalMessage.setContent(signedMimeMultipart);
finalMessage.saveChanges();

After signing, the MIME message will look as follows:

Message-ID: <1990160809.3.1512983999570@rajind-ENVY>
MIME-Version: 1.0
Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-1;  
    boundary="----=_Part_2_77269878.1512983999569"

------=_Part_2_77269878.1512983999569
Content-Type: text/plain; name=sample-text-file.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=sample-text-file.txt

c2FtcGxlIHRleHQgY29udGVudCBvbmUK
------=_Part_2_77269878.1512983999569
Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"
Content-Description: S/MIME Cryptographic Signature

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIAwggOLMIIC
c6ADAgECAgRzIbxvMA0GCSqGSIb3DQEBCwUAMHYxCzAJBgNVBAYTAlNMMRAwDgYDVQQIEwdXZXN0
ZXJuMRAwDgYDVQQHEwdDb2xvbWJvMRQwEgYDVQQKEwtBZHJvaXRMb2dpYzERMA8GA1UECxMIRGV2
LUFTMkcxGjAYBgNVBAMTEVJhamluZCBSdXBhcmF0aG5hMB4XDTE3MTIxMTA1Mzg0NFoXDTE4MDMx
MTA1Mzg0NFowdjELMAkGA1UEBhMCU0wxEDAOBgNVBAgTB1dlc3Rlcm4xEDAOBgNVBAcTB0NvbG9t
Ym8xFDASBgNVBAoTC0Fkcm9pdExvZ2ljMREwDwYDVQQLEwhEZXYtQVMyRzEaMBgGA1UEAxMRUmFq
aW5kIFJ1cGFyYXRobmEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCNRy9JKmdiX84V
8dkX8SUUr61WYpJuwQ3mnjHGCEd5qyLKl4ozi1TBPrfq1lIsf0b2U+y4Pno3KRJeSR1GYZJml1ED
/j2ovUvxrpf10JI0gxNJbM/FruMULmfQXed/GhU4NeKK7E6vJeJ7w7w9Jbuy7nrf92jJ7bY64bGJ
wh6xAwurjIQqw+8AsML1LUxG10KT+mI+L5ldVlJxCeyYI5WyiYMe3OG/s2mHNgHf0TXVg80vrlRR
eQizat8ax+xsG6RBGwHYSzkgYP79rQ9UaIw0XkML2N8rpzjLgMTQ0MuA83cxeCVgj/uDFowDcSnR
5BbYSdV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cuZgCsUyhdUPzEGKhZ+ibxXzqzwx0yweaw01QgHm34b1qjXVLO4LTlJCm3UIq
agAAAAAAAA==
------=_Part_2_77269878.1512983999569--

Encrypting The MIME Message

The following snippet can be used for encryption:

// loading public cert of partner
CertificateFactory fact = CertificateFactory.getInstance("X.509");
FileInputStream is = new FileInputStream("/home/rajind/Downloads/partner-cert.pem");
X509Certificate cert = (X509Certificate) fact.generateCertificate(is);

// create the encryptor
SMIMEEnvelopedGenerator encryptor = new SMIMEEnvelopedGenerator();
encryptor.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(cert).setProvider("BC"));
encryptor.setContentTransferEncoding("base64");

JceCMSContentEncryptorBuilder jceCMSContentEncryptorBuilder = 
  new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(SMIMEEnvelopedGenerator.DES_EDE3_CBC)).setProvider("BC");
jceCMSContentEncryptorBuilder.setSecureRandom(new SecureRandom());

// encrypting
MimeBodyPart encryptedPart = encryptor.generate(finalMessage, jceCMSContentEncryptorBuilder.build());

// setting encrypted content
finalMessage = new MimeMessage(session);
finalMessage.setContent(encryptedPart.getContent(), encryptedPart.getContentType());
finalMessage.setHeader("Content-Transfer-Encoding", "base64");
finalMessage.saveChanges();

After both signing and encryption, the composed MIME message will look as follows:

Message-ID: <347808407.5.1512984099462@rajind-ENVY>
MIME-Version: 1.0
Content-Type: application/pkcs7-mime; name="smime.p7m"; smime-type=enveloped-data
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHA6CAMIACAQAxggGKMIIBhgIBADBuMGYxCzAJBgNVBAYTAkFVMQwwCgYDVQQI
EwNOU1cxDzANBgNVBAcTBlNpZG5leTERMA8GA1UEChMIRGVtby1CMkIxEjAQBgNVBAsTCURlbW8t
QVMyRzERMA8GA1UEAxMIUmFqaW5kIFICBCLGK2cwDQYJKoZIhvcNAQEBBQAEggEAYFjOBGnUaozf
RCEtPQ9MFWjT4Rletb7B2LVLonBdK44Lzp0wNjyujiW/eOu5z6i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CUYalAGUAMg3i8SUKbCJDBvBE5vo6u3JExOVkBT4TbHGtuwbZ
zc0D1C5qmY6NzLryhdB4SwJI639dMrArvA8KM+TYeQlFWAIAYKqDRtC00moS/kdeCos1tQqt08d/
ePZ7TZpDQfC7CGY4Od3nrMv0B3g2h91bp7IOWbNR5E1NR6HfcyIc0Sz1aCMaUZfzogMqJRUuwk0r
JPSKx/KFpxiHN8Kn/Il75cjNo1XnVngUnUyJG+xevuqDPiRRiV+1EDjGmiq2pA5X1yhTAntgusqN
wjTy1jjGHX3vVdLRZ7def0E8ZZ6cUKLW17X6+E1pDufCU6pb1m8UpDgQawNKPZ1xQBIfW6COenrl
x8npsM6fK1yBjdCtTlgSe/URRFPgqT2iDNwAqzEX1jEik/R5oWSVNMtqgbMYztISs/X/HXbzs3xX
VGdRP6W41hwO4276AlWFJR977+5ADdo94fksYA5MIbayDfJ4s0Y/MhvTOCt6FIMDopZETHj/ZC5K
Dr3hq5e5WUbr2iDLDJtvFyX8HjcIxbH7GfkjkQRQ0bpYbPj6LkTnYcwBGpeFIidDBxFI06C2QLRJ
6wNo5YBhy+E/6/kYvXLFQfoiaaPL2uKaF4/FVcdwl1t5pVFan+1wmXwe6kW8fgbSxjm2wP20iaQW
awOT4YnGIkI39tGzrPREWXYn+gWkvVWxM9jD/xKQjL9iy2MqA8q0oX0K0QrRfbXf/ap5K5yVDTcj
3MI20+HsDWXRZXf4aSYoZrOt2JUJYshfi/EW7fexATQyBxPqefRzTgE3PCljC7qNCdkmJAAAAAAA
AAAAAAA=

That’s all for composing a MIME message with a single attachment. You might notice that there are various parameters like sign and encrypt certificates, and algorithms which were hardcoded in above snippets. In a real-world AS2 B2B communication scenario these values need to be easily configurable. AS2Gateway provides all these features with a set of simple UI configurations. Sign up and give it a try as well.

The next steps would be simply adding the AS2 related headers and sending the message over, and then parsing a received message. We’ll cover these in upcoming articles of the series.

P.S. Please note that the code snippets are just to give a quick idea of the process and they may not contain proper coding standards or exception handling.

The previous article, "AS2 Protocol for Business Data Interchange via HTTP, Part 1," can be found here.

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
as2 trading ,b2b integration ,adroitlogic ,edi

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}