bc-java icon indicating copy to clipboard operation
bc-java copied to clipboard

Incorrect encoding of RSA-PSS signatures in CMS

Open fencepost-error opened this issue 1 year ago • 6 comments

It looks like BC encodes RSA-PSS signatures in CMS incorrectly, see this post. Specifically, RFC 4055 defines the format as:

RSASSA-PSS-params  ::=  SEQUENCE  {
         hashAlgorithm      [0] HashAlgorithm DEFAULT
                                   sha1Identifier,
         maskGenAlgorithm   [1] MaskGenAlgorithm DEFAULT
                                   mgf1SHA1Identifier,
         saltLength         [2] INTEGER DEFAULT 20,
         trailerField       [3] INTEGER DEFAULT 1  }

However a BC-generated signature is encoded as:

1202   47:             SEQUENCE {
1204   15:               [0] {
1206   13:                 SEQUENCE {
1208    9:                   OBJECT IDENTIFIER sha-256 (2 16 840 1 101 3 4 2 1)
1219    0:                   NULL
         :                   }
         :                 }
1221   28:               [1] {
1223   26:                 SEQUENCE {
1225    9:                   OBJECT IDENTIFIER pkcs1-MGF (1 2 840 113549 1 1 8)
1236   13:                   SEQUENCE {
1238    9:                     OBJECT IDENTIFIER
         :                       sha-256 (2 16 840 1 101 3 4 2 1)
1249    0:                     NULL
         :                     }
         :                   }
         :                 }
         :               }

Note the absent saltLength, which means it has the SHA-1 value even though the signature is generated with SHA-256. Presumably the saltLength is the correct 32, however the signature is claiming it's 20 which means the signature can't be verified.

fencepost-error avatar Jun 20 '25 13:06 fencepost-error

I've been playing around with a sample signed file to try and figure out what's going on with the signature, it looks like the encoding is correct in that it's recording that BC is using SHA-1 parameters in a SHA-256 signature, but that's just moving the error to a different location, namely the RSA-PSS code.

fencepost-error avatar Jun 20 '25 14:06 fencepost-error

Regarding RSASSA-PSS-params (org.bouncycastle.asn1.pkcs1.RSASSAPSSparams): AFAICT, apart from test cases, all code in the BC libraries either uses the hash size for the salt length, or else it picks up the salt length from an externally-supplied java.security.spec.PSSParameterSpec, or an existing RSASSA-PSS-params encoding. If you claim that BC code is somehow choosing a saltLength of 20 in this case (as opposed to using what the user configured), then please show the signing code.

Note that RFC 4055 says "the recommended value of saltLength is the number of octets in the hash value", it's not a MUST (we follow the recommendation unless told otherwise). Indeed if someone naively copied rSASSA-PSS-SHA256-Params from the RFC 4055 ASN.1 module they would end up with "saltLength 20".

On the other hand, there's an extra rule in RFC 4056 section 3.3 that it might be helpful for the signed data generator to (at least optionally) validate also at signing time.

peterdettman avatar Jun 21 '25 08:06 peterdettman

I don't use BC, I just got sent a signed message created by BC, see this post, which is signed with SHA-256 but uses the SHA-1 salt length of 20 bytes in the PSS processing. I'm sort of acting as a go-between here, I'll try and get the submitter to comment since I don't know under what conditions they generated the signed message.

fencepost-error avatar Jun 22 '25 10:06 fencepost-error

Here's the parameters extract from CMS SignedData generated by the latest BC for an RSA-PSS signature using RSA-PSS with SHA-256,

               DER Sequence
                        ObjectIdentifier(1.2.840.113549.1.1.10)
                        DER Sequence
                            DER Tagged [CONTEXT 0]
                                DER Sequence
                                    ObjectIdentifier(2.16.840.1.101.3.4.2.1)
                                    NULL
                            DER Tagged [CONTEXT 1]
                                DER Sequence
                                    ObjectIdentifier(1.2.840.113549.1.1.8)
                                    DER Sequence
                                        ObjectIdentifier(2.16.840.1.101.3.4.2.1)
                                        NULL
                            DER Tagged [CONTEXT 2]
                                Integer(32)

The saltLength is clearly there.

I'd recommend trying to find out what version of BC the other party is using, or at least says they are using. It's possible there was a bug like this once, but it would have been a long time ago...

dghgit avatar Jun 23 '25 11:06 dghgit

Yup, that one's correct. I haven't heard back from the original submitter yet but as you say the two likely explanations are that they picked up the SHA-1 parameters from somewhere else or that it may have been some very old implementation that did this. In the meantime you can probably close this as not-really-an-issue, if I hear back from the OP at some point I'll provide more details on what happened to cause this.

fencepost-error avatar Jun 23 '25 11:06 fencepost-error

I am the original submitter of the post We have used bouncycastle 1.78.1. The problem with the wrong salt-value could be solved by correct parameterization.

mgerber avatar Jun 23 '25 14:06 mgerber