go icon indicating copy to clipboard operation
go copied to clipboard

crypto/x509: panics on invalid curve instead of returning error

Open FnuGk opened this issue 2 years ago • 3 comments

What version of Go are you using (go version)?

$ go version 1.19

What did you do?

In go 1.19 crypto/elliptic will now panic when operating on invalid curve points. This is great as it avoids undefined behavior, but crypto/x509 will now panic in marshalPublicKey in the case *ecdsa.PublicKey: where it previosly would return errors.New("x509: unsupported elliptic curve") as it calls elliptic.Marshal(pub.Curve, pub.X, pub.Y) before it checks oidFromNamedCurve(pub.Curve).

So simply switching those two calls around should fix the issue.

Meanwhile clients have to manually check if their key satisfies IsOnCurve before calling something like x509.MarshalPKIXPublicKey where in previous versions of go you could rely on an error being returned instead of a panic.

FnuGk avatar Aug 05 '22 09:08 FnuGk

Change https://go.dev/cl/421617 mentions this issue: crypto/x509: err instead of panic on invalid curve

gopherbot avatar Aug 05 '22 09:08 gopherbot

Ah, yeah, functions with an error return value should definitely return an error, not panic. I'll do a pass of all the marshal-side paths, and see if there are other issues like this.

@gopherbot please open a backport issue to Go 1.19. I don't think this is a security issue because the attacker can't control the curve of a certificate being marshaled, but panic'ing where we were returning an error is a regression and we should quash it.

FiloSottile avatar Aug 05 '22 14:08 FiloSottile

Backport issue(s) opened: #54295 (for 1.19).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

gopherbot avatar Aug 05 '22 14:08 gopherbot

Change https://go.dev/cl/422298 mentions this issue: crypto/x509: don't panic marshaling invalid ECDSA keys

gopherbot avatar Aug 09 '22 20:08 gopherbot

Ok. I had already opened #54290 along with this issue, anything else i should do on my end?

FnuGk avatar Aug 10 '22 09:08 FnuGk

@FnuGk no, we've got it, thank you for the report! https://go.dev/cl/422298 is a slightly broader fix that is now under review.

FiloSottile avatar Aug 11 '22 00:08 FiloSottile

Change https://go.dev/cl/425634 mentions this issue: [release-branch.go1.19] crypto/x509: don't panic marshaling invalid ECDSA keys

gopherbot avatar Aug 25 '22 19:08 gopherbot