veraison / go-cose

go library for CBOR Object Signing and Encryption (COSE)
Mozilla Public License 2.0
50 stars 26 forks source link

missing EC public key validation when creating a new Verifier #115

Closed thomas-fossati closed 1 year ago

thomas-fossati commented 1 year ago

Currently, when we instantiate a Verifier we do not check that the supplied public key EC point is on the supposed curve.

This is not best practice. See for example BCP225 in the JWS/JWT context:

"Some cryptographic operations [...] take inputs that may contain invalid values. This includes points not on the specified elliptic curve or other invalid points (e.g., [Valenta], Section 7.1). The JWS/JWE library itself must validate these inputs before using them, or it must use underlying cryptographic libraries that do so (or both!)."

It'd also result in a panic when the go-cose application is built against go1.19. See go1.19 release notes:

"Operating on invalid curve points (those for which the IsOnCurve method returns false, and which are never returned by Unmarshal or by a Curve method operating on a valid point) has always been undefined behavior and can lead to key recovery attacks. If an invalid point is supplied to Marshal, MarshalCompressed, Add, Double, or ScalarMult, they will now panic."

This can potentially widen the DoS surface of a go-cose application. Users should be shielded from such situation.

ivarprudnikov commented 1 year ago

when we instantiate a Verifier <...> This can potentially widen the DoS surface of a go-cose application

The implementer is passing a public key and currently it is on them to make sure it is a correct one. Nothing prevents the implementer from making the appropriate checks before using their own key. Furthermore, it is a reasonable expectation to be provided with a valid argument to begin with.

It is interesting though that Go went ahead with a panic and not the error in such a case.

thomas-fossati commented 1 year ago

Furthermore, it is a reasonable expectation to be provided with a valid argument to begin with.

I don't know you, but for an old-school derelict like myself input validation has always been no. 1 rule of secure coding practices 😄

It is interesting though that Go went ahead with a panic and not the error in such a case.

I agree with you that was a pretty interesting choice -- which makes upfront validation without crashing the caller even more valuable.

ivarprudnikov commented 1 year ago

input validation has always been no. 1 rule

But you get that key from somewhere, are you saying you do not verify it before using in the library?

thomas-fossati commented 1 year ago

[...] are you saying you do not verify it before using in the library?

at the moment we just check that the underlying type is as expected (see https://github.com/veraison/go-cose/blob/4dbb9a7434cb75b3cbab3a24076002e8b1ee38a5/verifier.go#L45). 064ff82 adds the "on curve" test.

ivarprudnikov commented 1 year ago

at the moment we just check that the underlying type is as expected

^^ My question was more about the production code that gets the key from somewhere and passes it to this library for a verification.

Otherwise, I am just trying to understand if it is a good idea to add this curve check to the library, because: