Skip to content

Commit

Permalink
unify CMS encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Akretsch committed Apr 17, 2024
1 parent c0b2455 commit 38ed7bb
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
*/
package com.siemens.pki.cmpracomponent.cryptoservices;

import com.siemens.pki.cmpracomponent.configuration.CkgContext;
import com.siemens.pki.cmpracomponent.util.ConfigLogger;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.cms.EnvelopedData;
import org.bouncycastle.asn1.cms.SignedData;
import org.bouncycastle.cms.CMSAlgorithm;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.RecipientInfoGenerator;
import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder;

import com.siemens.pki.cmpracomponent.configuration.CkgContext;
import com.siemens.pki.cmpracomponent.util.ConfigLogger;

/**
* base class for CMS data encryption
*/
Expand Down Expand Up @@ -73,17 +74,14 @@ public EnvelopedData encrypt(final byte[] msg) throws CMSException, NoSuchAlgori
/**
* encrypt the data
*
* @param data signed data to encrypt
* @param asn1Object ASN.1 object to encrypt
* @return encrypted data
* @throws CMSException in case of an CMS processing error
* @throws IOException in case of ASN.1 encoding error
* @throws NoSuchAlgorithmException if getContentEncryptionAlg in config is
* unknown
*/
public EnvelopedData encrypt(final SignedData data) throws CMSException, IOException {
final CMSEnvelopedData cmsEnvData = envGen.generate(
new CMSProcessableByteArray(data.getEncoded()),
new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC)
.setProvider(CertUtility.getBouncyCastleProvider())
.build());
return EnvelopedData.getInstance(cmsEnvData.toASN1Structure().getContent());
public EnvelopedData encrypt(final ASN1Object asn1Object) throws CMSException, IOException, NoSuchAlgorithmException {
return encrypt(asn1Object.getEncoded());
}
}

0 comments on commit 38ed7bb

Please sign in to comment.