From 38ed7bbcf56daaf65e16906984a74aaaa7997659 Mon Sep 17 00:00:00 2001 From: Andreas Kretschmer Date: Wed, 17 Apr 2024 09:42:09 +0200 Subject: [PATCH] unify CMS encryption --- .../cryptoservices/CmsEncryptorBase.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/siemens/pki/cmpracomponent/cryptoservices/CmsEncryptorBase.java b/src/main/java/com/siemens/pki/cmpracomponent/cryptoservices/CmsEncryptorBase.java index 7104090c..d82ae951 100644 --- a/src/main/java/com/siemens/pki/cmpracomponent/cryptoservices/CmsEncryptorBase.java +++ b/src/main/java/com/siemens/pki/cmpracomponent/cryptoservices/CmsEncryptorBase.java @@ -17,13 +17,11 @@ */ 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; @@ -31,6 +29,9 @@ 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 */ @@ -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()); } }