-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PKCS#7 / CMS Signature validation failure with unordered attributes #1365
Comments
So it's 1, and 2 is fair enough. Have a look at
MyRightSignerInformation will show you what to do. Keep in mind if you are accepting messages like this people can reorder the data and add extras as it suits them - the purpose of the DER encoding is to ensure only one encoding of data can be used to verify the signature. It's better to get people to correct what they are doing if they are producing invalid messages. |
Only the authenticated attributes are signed, so modification of the other parts of the message is always possible (except the content). At the moment it would be possible for someone to sign the ordered attributes and then a third party could theoretically reorder them and BC would validate the signature (despite the signed attributes being tampered with), when I suspect openssl would fail as the actual bytes wouldn't verify. However, this is academic and, I'd suggest, meaningless as the data would still have been signed, just someone had arbitrarily (but not meaningfully) interfered with the order of the authenticated attributes. I have submitted a fix for this issue in the upstream library too: digitalbazaar/forge#1025 |
In fact, I think this is an example of a PKCS#7 message that would fail validation in That means any library verifying signatures over the actual bytes in the message will fail, but any calculating it over expected bytes will validate it. Bad PKCS#7 may pass in BC
|
Frankly, I'm surprised OpenSSL would validate an invalid message. Likewise I am extremely surprised that it would not validate a valid message with disordered signed attributes. If it's any help, RFC 5652, section 5.4 (and it's predecessors) is quite explicit about the need to DER encode signed attributes, the message with unsorted attributes is valid providing the signature is calculated over the DER encoding. As I rule we would only write things out in DER encoding as it means the message will validate even if the parser checking it is broken, but there are occasions where people do not save the attributes in DER ordering. Glad to hear it will be fixed. |
Interesting, thanks. So then the BC behaviour appears to be correct (calculate over theoretical bytes not supplied bytes) and OpenSSL is wrong. Thanks for the feedback on this and hopefully this will help anyone else who stumbles upon this problem. |
Firstly, apologies if this issue is known or has been raised and closed. I had a look through existing issues and found a couple of things that could be related, but don't explicitly get to the bottom of it. #286 #761
I have a service producing detached CMS payloads, these payloads validate successfully when using
openssl
, but when using Bouncy Castle, the signature fails to validate. NB: I am not the one using BC, so apologies that I can't provide full errors.After much debugging, it was found that the issue is the service generating the CMS payloads is not strictly compliant with the specification which says that the authenticated attributes must be a DER encoded
SET OF
, the DER spec says that aSET OF
must be ordered:I wondered why this was an issue in BC but not in
openssl
and after a bit of digging, I think the issue comes down to the fact that BC is parsing and then reconstructing what it thinks the signed ASN.1 structure should be and not what it actually is. There is a decode/encode step that causes information (order) loss and therefore the signature is not calculated over the bytes that are supplied causing an invalid signature to be calculated.I can see that there are two valid viewpoints on this:
I don't think these are mutually exclusive and both are true; BC should not be calculating the signatures except from the actual supplied bytes. Ultimately, the signature over the bytes supplied is valid, the bytes supplied just don't adhere strictly to the spec.
Version: org.bouncycastle:bcpkix-jdk15on:1.70
Example - here is a certificate and signature that passes with openssl but should fail with bouncy castle:
certificate.pem:
pkcs7-bad.pem:
openssl command to verify:
Example - pkcs7 that passes with both openssl and BC:
pkcs7-good.pem:
openssl command to verify:
Example failing code:
The text was updated successfully, but these errors were encountered: