Discussion:
Trouble with DESede decrypting CMSEnvelopedData
Matt Hauck
2013-01-05 10:38:57 UTC
Permalink
I've gotten a little further now and ran into another roadblock. I have a pkcs7 file that is signed and enveloped, and I have the key that was used to decrypt the enveloped contents. I have gotten it working just fine when I add BouncyCastle as a security provider. However, when I do not add BC as a provider, then I get an error:

Exception in thread "main" org.bouncycastle.cms.CMSException: key invalid in message.
at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.execute(Unknown Source)
at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.createContentCipher(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)
at org.bouncycastle.cms.RecipientInformation.getContent(Unknown Source)
at com.bigfix.mdm.CMSTest.main(CMSTest.java:39)
Caused by: java.security.InvalidKeyException: Wrong algorithm: DESede or TripleDES required
at com.ibm.crypto.fips.provider.w.a(Unknown Source)
at com.ibm.crypto.fips.provider.q.a(Unknown Source)
at com.ibm.crypto.fips.provider.DESedeCipher.a(Unknown Source)
at com.ibm.crypto.fips.provider.DESedeCipher.a(Unknown Source)
at com.ibm.crypto.fips.provider.DESedeCipher.engineInit(Unknown Source)
at javax.crypto.Cipher.a(Unknown Source)
at javax.crypto.Cipher.a(Unknown Source)
at javax.crypto.Cipher.init(Unknown Source)
at javax.crypto.Cipher.init(Unknown Source)
at org.bouncycastle.cms.jcajce.EnvelopedDataHelper$1.doInJCE(Unknown Source)
... 7 more


This seems strange to me, because my IBMJCEFIPS does indeed have a DESede algorithm. When I try to read / decrypt the same content using JRuby (which uses bouncy castle for its crypto stuff), it appears that it is using the following more specific cipher: DESede/cbc/PKCS5Padding.

It is strange to me that BC's DESede algorithm works but IBM's (and Sun's, btw) apparently does not? The reason I can't just add BC as a provider and be done with it, is for FIPS compatibility, and thus I need to depend on IBM's crypto only.

Is this a known issue? Would you expect to see this disparity? Perhaps I am just doing something wrong? Thanks for any help.


The CMSSignedData:
MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH
AaCAJIAEggMlMIAGCSqGSIb3DQEHA6CAMIACAQAxgfEwge4CAQAwVzBRMQ8w
DQYDVQQKDAZtaGF1Y2sxPjA8BgNVBAMMNW1oYXVjayBSb290IENBIChkZjgy
NzAwNC00Yzg1LTRjNjQtYWRmZS0wMTczZTVmY2YzYTkpAgIrJjANBgkqhkiG
9w0BAQEFAASBgCv/rgKyDV5UOJTGbYwdw0Salw4bB+ea+ovL6kb7HFDDR6la
icjZYvM5FQZ5jIgiFPQdFx8mlXIkjuA/JHQeCEMPKrSlSDxKJg6opFOl3dXf
F0u9I+48PV4QHPrd+iiirnpiVDJLh1m/U2DQTJZjl3d+pd6skxiWn8HhU1XS
KQD6MIAGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIy4tZgNrKyDGggASCAeAG
ezNensHrZr4AbZVEcUGy+wZvKKwQBA9Dqiu4Bu7ItfzuXleaSFDXfhCCkRS5
DBccB2Gvm09SoNy8/mAxp+kg8uDKBOgaYlp0e89R9Q4ABqieUQBgs9OmenSt
Uc8ATz6UtHWB1PYnuJ3+NygdCGemwvmzK3xQm8Wj7MnYurh7oDi6ouNyRyiy
9DtWJMlx4JVbcnIkrTpqHNnE09Q8XIm9BI5bGN+MpdMh9c9cQtSLE07QUs2m
viqRmPQvSLVT8mqDGHK7zUWmqMqVrB6sJUYsWcu0/g8gl+H+QhiqDScaaEUi
Ng0kweoT361MY7s2WCHsyU/uTFPBG+g2neI1E8V806wstLKEGWdThFsQCOzX
2XhjRPKBAsJ8w1KvEesvyT8CwjK9PeK+jjPT97+qTzcCAKvZLZ+vlveV/D+3
TMntIp2unI1N7dzqXf1tNsjIq+vej2p2PGzEoScom0IzNgP5FCZis7I/pF0s
2drUaYS7MvaPOZyVR1TdTKX33+9kc5mTdEsznaSR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wLf7fjoZdieHIy0tyOk
LW93ZI3Gra2QPNhRKsjzQQJBAPAfdn3b1nAriwe9fIadMYHCrc0sEoux5e75d651
EFe98Iw3LeIN1wpvig5UQUXuFL8ouBV2Dk9WU89/eONlN5I=
-----END RSA PRIVATE KEY-----


The code:

public static void main(String[] args) throws Exception {
String contents = KeyStoreUtils.readFile(args[0]);
PrivateKey pk = KeyStoreUtils.readPrivateKey(new File(args[1]), null);

CMSSignedData sd = new CMSSignedData(Base64.decode(contents));

CMSEnvelopedData ed = new CMSEnvelopedData(
(byte[]) sd.getSignedContent().getContent()
);

RecipientInformationStore recipients = ed.getRecipientInfos();

Collection c = recipients.getRecipients();
Iterator it = c.iterator();

if (it.hasNext()) {
RecipientInformation recipient = (RecipientInformation)it.next();

byte[] recData = recipient.getContent(
new JceKeyTransEnvelopedRecipient(pk)
);

System.out.println(new String(Base64.encode(recData)));
}
}
--
Matt Hauck
Matt Hauck
2013-01-06 23:02:47 UTC
Permalink
Ah excellent, thank you so much for your help!

FYI for the sake of the list: upgrading to version 1.47 fixed my problem.

--
Matt Hauck
Ah, you were using 1.46...
Okay, that's good I was a bit surprised when you reported the problem.
Just to fill you in on the change, the CMS classes that recover encrypted data generally create SecretKeySpec classes to recover the data, SecretKeySpec directly implements SecretKey so generally the spec object can be used directly with a Cipher, in 1.46 and earlier we were using the algorithm OID to set the algorithm type of the key, however around the time 1.46 was coming out some vendors started doing things like checking the algorithm name (which isn't entirely unreasonable... it's just you'd think if you were going to do that you'd realise anyone dealing with IETF standards probably just has an OID at key creation time). The result of this is we added a table which translates some of the common OIDs into common algorithm names. In the JCA/JCE algorithms names are supposed to be case insensitive though, so when you said it was saying "expecting DESede" I thought, oh oh... checking the message you sent indicated it should have been translated, I just needed to be sure you were seeing what I was.
Regards,
David
So it turns out that the problem I was having building and running was that I was using different versions. The bcprov jar I was using was a 146 version, and the one I was building was a 147. After downloading the latest 147 jar from the website, The built pkik jar worked, and I get the following output for sKey.getAlgorithm(): "DESEDE".
However
 everything appears to work now as well, both on Sun and IBM JREs! I'm not getting the same "wrong algorithm" error anymore, even without adding BC as a Provider. It looks like it was something fixed from 146 to 147?
--
Matt Hauck
Loading...