Open
Description
Go version
go version go1.24.3 linux/amd64
Output of go env
in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2784876717=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/user/Code/go-test/go.mod'
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.3'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
Playground: https://go.dev/play/p/OoaeG_APMGI
What did you see happen?
Golang unmarshals incorrectly-sorted ASN.1 SET values. The example certificate has a multi-valued RDN that is incorrectly sorted. This is invalid DER and should not have been parsed.
What did you expect to see?
Golang should have errored when parsing the certificate.
See https://www.itu.int/rec/T-REC-X.690-202102-I/en, section 11.6:
The encodings of the component values of a set-of value shall appear in ascending order, the encodings being compared as
octet strings with the shorter components being padded at their trailing end with 0-octets.
Activity
gabyhelp commentedon May 16, 2025
Related Issues
Related Code Changes
Related Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
mateusz834 commentedon May 16, 2025
Looking at the example you provided, i think that this is more related to
crypto/x509
since thex509
parser does not useencoding/asn1
, butx/crypto/cryptobyte
.Looking at the parsing code for RDNs, it does not check the ordering of the
SET
values:go/src/crypto/x509/parser.go
Lines 141 to 181 in 045b5c1
But is also seems like
encoding/asn1
accepts such encoding:[-]encoding/asn1: Unmarshal allows unsorted `SET` values[/-][+]crypto/x509: ParseCertificate allows unsorted `SET` values in RDNs[/+]mateusz834 commentedon May 16, 2025
CC @rolandshoemaker
gopherbot commentedon May 31, 2025
Change https://go.dev/cl/677796 mentions this issue:
crypto/x509: Validate certificates with unsorted SET values in RDNs
rolandshoemaker commentedon Jun 2, 2025
I'm not 100% convinced that we need to enforce this property. Generally we try to follow the specs to the letter when we encode certificates, but when parsing them we can be a little more lax. In this particular case, incorrect ordering does not really affect our processing of the certificate at all, so outright rejecting it feels a little extreme.
I am somewhat forgetting the exact context for why we didn't enforce this originally. I suspect (but don't have good evidence on hand) that encoding implementations used to get this wrong, so it was safer to just accept out of order SETs. It may be worthwhile re-checking the current ecosystem to see if there are still certificates which violate this property or not floating around.