Documentation of Test Sign Encrypt
package org.bouncycastle.mime.test;
import java.io.ByteArrayOutputStream;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.io.Streams;
-public class TestSMIMESignEncrypt
+public class TestSMIMESignEncrypt_commented
extends TestCase
{
private static final String BC = BouncyCastleProvider.PROVIDER_NAME;
@@ -99,10 +100,12 @@ private static void init()
_initialised = true;
+ //create certificate of the sender(signature certificate)
_signDN = "O=Bouncy Castle, C=AU";
_signKP = CMSTestUtil.makeKeyPair();
_signCert = CMSTestUtil.makeCertificate(_signKP, _signDN, _signKP, _signDN);
+ //create certificate of the receiver (encryption certificate)
_reciDN = "CN=Doug, OU=Sales, O=Bouncy Castle, C=AU";
_reciKP = CMSTestUtil.makeKeyPair();
_reciCert = CMSTestUtil.makeCertificate(_reciKP, _reciDN, _signKP, _signDN);
@@ -118,13 +121,13 @@ public void setUp()
public void testSignThenEncrypt()
throws Exception
{
- //
- // output
- //
+
+ //output that will contain signed and encrypted content
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
SMIMEEnvelopedWriter.Builder envBldr = new SMIMEEnvelopedWriter.Builder();
+ //specify encryption certificate
envBldr.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));
SMIMEEnvelopedWriter envWrt = envBldr.build(bOut, new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider(BC).build());
@@ -133,24 +136,28 @@ public void testSignThenEncrypt()
SMIMESignedWriter.Builder sigBldr = new SMIMESignedWriter.Builder();
+ //specify signature certificate
sigBldr.addCertificate(new JcaX509CertificateHolder(_signCert));
sigBldr.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider(BC).build("SHA256withRSA", _signKP.getPrivate(), _signCert));
+ //add the encryption stream to the signature stream
SMIMESignedWriter sigWrt = sigBldr.build(envOut);
OutputStream sigOut = sigWrt.getContentStream();
sigOut.write(simpleMessage);
+ //sign file using sender private key
sigOut.close();
+ //write full message to the byte array output stream before actually closing the SMIME Enveloped Writer (before this, bOut contains only the headers?)
envOut.close();
bOut.close();
//
- // parse
+ // parse / decrypt and compare to original file
//
final TestDoneFlag dataParsed = new TestDoneFlag();
@@ -167,6 +174,7 @@ public void envelopedData(MimeParserContext parserContext, Headers headers, Orig
assertNotNull(recipInfo);
+ //decrypt the file using the receiver's private key before verifying signature
byte[] content = recipInfo.getContent(new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()));
MimeParserProvider provider = new SMimeParserProvider("7bit", new BcDigestCalculatorProvider());
