使用AS2(http)协议实现 B2B 商用数据交换 (二) [译]

前言

设计 AS2 协议的主要目的,是基于 HTTP 协议之上实现安全的结构化电子商业数据交换。在这系列文章的第一部分,我们大体了解了 AS2 为何这么优秀。我们作为 B2B 集成平台 AS2Gateway 的开发者,已经在 AS2 协议这方面工作了很多年。在本篇文章中,我们希望给予更多的见解关于 AS2 协议,如何使用几行 java 代码和 S/MIME 格式去构造一个 AS2 消息。

废话不多说,让我们现在开始。AS2 消息的基本结构:他由 MIME 格式数据组成,并存在于 HTTP 消息体里面,再加上一些特有的 AS2 消息头部。
AS2 消息的最终结构如下图所示。在本文中,我们会从一个简单的文档开始,一步一步生成最终的加密过的 HTTP 消息体。
使用AS2(http)协议实现 B2B 商用数据交换 (二) [译]

译者注:我们看到最外层是 HTTP 数据包,AS2 消息的实际内容 (使用非对称加密算法加密过的) 是挂载到 HTTP BODY (HTTP请求体) 里面的。AS2 协议重点就在于如何生成/解析这个 Encrypted HTTP Body (加密过的 HTTP 请求体)。
解密过后的 AS2 消息中还包含了基础文档 (Functional Document) 和数字签名 (Ditital Signature),AS2 协议规定应用软件需要校验这个数据签名 (Digital Signature) 来确保数据完整性,具体做法是

  1. 使用远程客户公钥解密数字签名,得到一个散列码,记为 HASH-CODE-1
  2. 使用约定好的散列算法 (例如 MD5, SHA) 计算出基础文档 (Functional Document) 的散列码,记为 HASH-CODE-2
  3. 比较这两个散列码 HASH-CODE-1, HASH-CODE-2 从而确认数据是否被篡改

生成 MIME 消息

首先,让我们看一个 MIME 消息样例。下面的样例代码使用了 JavaMail 和 Apache Tika,用来生成一个 MIME 消息

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());

生成的 MIME 消息结构如下所示,注意 MIME 的头部信息和消息内容 (消息内容通过 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

签署 MIME 消息

现在我们看看 S/MIME 如何发挥作用。S/MIME 提供了两种安全措施,数字签名 (Digital Signature) 和信息加密 (Message Encryption)。这两项措施是 S/MIME 消息安全性的基础。数字签名提供身份认证,消息不可否认性以及数据完整性校验。信息加密服务则提供了数据机密性以及数据完整性。下面的代码片断展示了如何对 MIME 消息进行签名,这里我们使用了 Bouncy Castle S/MIME API, Bouncy Castle Crypto package, 以及 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();

签署过后,MIME 消息如下如示
译者注:第一部分为实际内容 "sample text content one" (经base64编码),第二部分为数字签名

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
aW5kIFJ1cGFyYXRobmEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB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BCQQxFgQUhVeT2eOCO+13
wPL/8mopwFqKuk0wDQYJKoZIhvcNAQEBBQAEggEACxCnEunose/i7kHI1fKSKAKJeUEPTprqxqIt
SmetJiffCNrGU1rf9l33h7AjKQPWHD9HkCDNHyC5F6qviezOZxEAh9e/v8uLwRn4wPorVLqP11wv
mEzPoD9ph82DzK/tCSO1Mtbu9ibB4YtirHNlSw7sFKKTyaXQU/rup2aW6YG2xjeflz6EDrxVhAh+
lgRuuNZPELzpDhuDgYajmbatzxP45s6OzSSRRHfrdoxEVEpNfV915WTPSh5DQ52sCC28RWZC9u1u
wkp0Dqhhg68JrO4cuZgCsUyhdUPzEGKhZ+ibxXzqzwx0yweaw01QgHm34b1qjXVLO4LTlJCm3UIq
agAAAAAAAA==
------=_Part_2_77269878.1512983999569--

加密 MIME 消息

// 加载partner的数字证书
CertificateFactory fact = CertificateFactory.getInstance("X.509");
FileInputStream is = new FileInputStream("/home/rajind/Downloads/partner-cert.pem");
X509Certificate cert = (X509Certificate) fact.generateCertificate(is);
// 创建加密器
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());
// 进行加密
MimeBodyPart encryptedPart = encryptor.generate(finalMessage, jceCMSContentEncryptorBuilder.build());
// 设置加密后的内容
finalMessage = new MimeMessage(session);
finalMessage.setContent(encryptedPart.getContent(), encryptedPart.getContentType());
finalMessage.setHeader("Content-Transfer-Encoding", "base64");
finalMessage.saveChanges();

经过签字 (sign) 和加密 (encrypt) 后,MIME 消息变成了下面那样

Message-ID: <347808407.5.1512984099462@rajind-ENVY>
MIME-Version: 1.0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5K5yVDTcj
3MI20+HsDWXRZXf4aSYoZrOt2JUJYshfi/EW7fexATQyBxPqefRzTgE3PCljC7qNCdkmJAAAAAAA
AAAAAAA=

你可能注意到上面代码片段中硬编码的证书算法。在实际的 AS2 B2B 通信场景中,这些参数需要非常方便去配置。
下一步就是要加入与 AS2 有关的 HTTP 头部,并把消息发送出去,接着是解析收到的消息。这些将会在未来的文章中介绍。

P.S. 请注意上述代码片断只是给你一个关于 AS2 消息处理的初印象,他们也许不符合编程标准,也没有异步处理。

相关推荐