io.gatling.recorder.internal.bouncycastle.cms.CMSEnvelopedDataGenerator Maven / Gradle / Ivy
package io.gatling.recorder.internal.bouncycastle.cms;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import io.gatling.recorder.internal.bouncycastle.asn1.ASN1EncodableVector;
import io.gatling.recorder.internal.bouncycastle.asn1.ASN1Set;
import io.gatling.recorder.internal.bouncycastle.asn1.DERSet;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.ContentInfo;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.EncryptedContentInfo;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.EnvelopedData;
import io.gatling.recorder.internal.bouncycastle.operator.OutputAEADEncryptor;
import io.gatling.recorder.internal.bouncycastle.operator.OutputEncryptor;
/**
* General class for generating a CMS enveloped-data message.
*
* A simple example of usage.
*
*
* CMSTypedData msg = new CMSProcessableByteArray("Hello World!".getBytes());
*
* CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator();
*
* edGen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(recipientCert).setProvider("BC"));
*
* CMSEnvelopedData ed = edGen.generate(
* msg,
* new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC)
* .setProvider("BC").build());
*
*
*/
public class CMSEnvelopedDataGenerator
extends CMSEnvelopedGenerator
{
/**
* base constructor
*/
public CMSEnvelopedDataGenerator()
{
}
private CMSEnvelopedData doGenerate(
CMSTypedData content,
OutputEncryptor contentEncryptor)
throws CMSException
{
ASN1EncodableVector recipientInfos = CMSUtils.getRecipentInfos(contentEncryptor.getKey(), recipientInfoGenerators);
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try
{
OutputStream cOut = contentEncryptor.getOutputStream(bOut);
content.write(cOut);
cOut.close();
if (contentEncryptor instanceof OutputAEADEncryptor)
{
byte[] mac = ((OutputAEADEncryptor)contentEncryptor).getMAC();
bOut.write(mac, 0, mac.length);
}
}
catch (IOException e)
{
throw new CMSException("");
}
byte[] encryptedContent = bOut.toByteArray();
EncryptedContentInfo eci = CMSUtils.getEncryptedContentInfo(content, contentEncryptor, encryptedContent);
ASN1Set unprotectedAttrSet = CMSUtils.getAttrBERSet(unprotectedAttributeGenerator);
ContentInfo contentInfo = new ContentInfo(
CMSObjectIdentifiers.envelopedData,
new EnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, unprotectedAttrSet));
return new CMSEnvelopedData(contentInfo);
}
/**
* generate an enveloped object that contains an CMS Enveloped Data
* object using the given provider.
*
* @param content the content to be encrypted
* @param contentEncryptor the symmetric key based encryptor to encrypt the content with.
*/
public CMSEnvelopedData generate(
CMSTypedData content,
OutputEncryptor contentEncryptor)
throws CMSException
{
return doGenerate(content, contentEncryptor);
}
}