I am not an expert with encryption, but i am trying to create an CMSEnvelopedDataGenerator
with bouncycastle 1.67, where the session key is encrypted with RSAES-OAEP (1.2.840.113549.1.1.7)
For now my code looks like this:
CMSEnvelopedDataGenerator envelopedGenerator = new CMSEnvelopedDataGenerator();
JcaAlgorithmParametersConverter paramsConverter = new JcaAlgorithmParametersConverter();
OAEPParameterSpec oaepSpec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
AlgorithmIdentifier algorithmIdentifier;
algorithmIdentifier = paramsConverter.getAlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP, oaepSpec);
JceKeyTransRecipientInfoGenerator recipent = new JceKeyTransRecipientInfoGenerator(receiverCert, algorithmIdentifier).setProvider("BC");
# encrypt
CMSEnvelopedData envelopedData;
envelopedData = envelopedGenerator.generate(
new CMSProcessableByteArray(encodedSignedData),
new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC).setProvider("BC").build()
)
It runs through but when i check it via openssl asn1parse
, i see
115:d=6 hl=2 l= 9 prim: OBJECT :rsaesOaep
126:d=6 hl=2 l= 47 cons: SEQUENCE
128:d=7 hl=2 l= 15 cons: cont [ 0 ]
130:d=8 hl=2 l= 13 cons: SEQUENCE
132:d=9 hl=2 l= 9 prim: OBJECT :sha256
143:d=9 hl=2 l= 0 prim: NULL
145:d=7 hl=2 l= 28 cons: cont [ 1 ]
147:d=8 hl=2 l= 26 cons: SEQUENCE
149:d=9 hl=2 l= 9 prim: OBJECT :mgf1
160:d=9 hl=2 l= 13 cons: SEQUENCE
162:d=10 hl=2 l= 9 prim: OBJECT :sha256
and then the hex dump. On my reference file it is like:
115:d=6 hl=2 l= 9 prim: OBJECT :rsaesOaep
126:d=6 hl=2 l= 43 cons: SEQUENCE
128:d=7 hl=2 l= 13 cons: cont [ 0 ]
130:d=8 hl=2 l= 11 cons: SEQUENCE
132:d=9 hl=2 l= 9 prim: OBJECT :sha256
143:d=7 hl=2 l= 26 cons: cont [ 1 ]
145:d=8 hl=2 l= 24 cons: SEQUENCE
147:d=9 hl=2 l= 9 prim: OBJECT :mgf1
158:d=9 hl=2 l= 11 cons: SEQUENCE
160:d=10 hl=2 l= 9 prim: OBJECT :sha256
On line 143 at my file is the line
143:d=9 hl=2 l= 0 prim: NULL
I am not sure where that comes from.
When i use my decryption code, which works for my reference file, i am getting the following exceptions
exception unwrapping key: bad padding: unable to decrypt block
Caused by: org.bouncycastle.cms.CMSException: exception unwrapping key: bad padding: unable to decrypt block
at org.bouncycastle.cms.jcajce.JceKeyTransRecipient.extractSecretKey(Unknown Source)
at org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.KeyTransRecipientInformation.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContentStream(Unknown Source)
Caused by: org.bouncycastle.operator.OperatorException: bad padding: unable to decrypt block
at org.bouncycastle.operator.jcajce.JceAsymmetricKeyUnwrapper.generateUnwrappedKey(Unknown Source)
Caused by: org.bouncycastle.jcajce.provider.util.BadBlockException: unable to decrypt block
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.getOutput(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Cipher.java:2168)
Caused by: org.bouncycastle.crypto.InvalidCipherTextException: data wrong
at org.bouncycastle.crypto.encodings.OAEPEncoding.decodeBlock(Unknown Source)
at org.bouncycastle.crypto.encodings.OAEPEncoding.processBlock(Unknown Source)
I hope its not much, that is missing.
Edit:
With my wrong generated file recipient.getKeyEncryptionAlgorithm().getParameters()
results in
[[0][2.16.840.1.101.3.4.2.1, NULL], [1][1.2.840.113549.1.1.8, [2.16.840.1.101.3.4.2.1, NULL]]]
the correct file in
[[0][2.16.840.1.101.3.4.2.1], [1][1.2.840.113549.1.1.8, [2.16.840.1.101.3.4.2.1]]]
From where are these wrong NULL
for SHA-256 values coming.