Skip to content

crypto/x509: Verification of ECDSA signed x509 cert, sanitized to LowS certs fails verification with go 1.19 #54550

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/crypto/x509/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,17 +865,18 @@ func alreadyInChain(candidate *Certificate, chain []*Certificate) bool {
if !candidate.PublicKey.(pubKeyEqual).Equal(cert.PublicKey) {
continue
}
if !bytes.Equal(candidate.Signature, cert.Signature) {
continue
}
var certSAN *pkix.Extension
for _, ext := range cert.Extensions {
if ext.Id.Equal(oidExtensionSubjectAltName) {
certSAN = &ext
break
}
}
if candidateSAN == nil && certSAN == nil {
return true
} else if candidateSAN == nil || certSAN == nil {
return false
if candidateSAN == nil || certSAN == nil {
return candidateSAN == certSAN
}
if bytes.Equal(candidateSAN.Value, certSAN.Value) {
return true
Expand Down
90 changes: 90 additions & 0 deletions src/crypto/x509/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
Expand Down Expand Up @@ -2620,5 +2621,94 @@ func TestVerifyEKURootAsLeaf(t *testing.T) {
}
})
}
}

const rootCertHightS = `-----BEGIN CERTIFICATE-----
MIICUjCCAfegAwIBAgIQQsXmbjAQVrOBjL2Z+jFp0jAKBggqhkjOPQQDAjBzMQsw
CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy
YW5jaXNjbzEZMBcGA1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VQQKExBvcmcxLmV4YW1wbGUuY29tMRwwGgYDVQQD
ExNjYS5vcmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE
KGHxCO8p0mpmpZ85V0EK1nthHWcbxPOUVb+NI/ORkxU5umFdEMyBPOCHlEfUlRa3
aHZJROyrziQojvDx3gstb6NtMGswDgYDVR0PAQH/BAQDAgGmMB0GA1UdJQQWMBQG
CCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdDgQiBCD/
MLvrYcATgbFpu+X+Z5Osa8OafniFEdeUNqj3VTo5BjAKBggqhkjOPQQDAgNIADBF
AiEAkrAT1sBFIHIw+A8/bC/NuBoQhFGjFro2WpTc9buIQmkCIFZvWZkXypjhwCOY
CpI+MoS3xx/ff3BNaWnGXCTBmE/v
-----END CERTIFICATE-----`

func Test2CertHightSAndLowS(t *testing.T) {
blockHS, _ := pem.Decode([]byte(rootCertHightS))
if blockHS == nil {
t.Fatal("failed to decode block")
}

certHightS, err := x509.ParseCertificate(blockHS.Bytes)
if err != nil {
t.Error(err)
}
if certHightS == nil {
t.Fatal("failed to parse cert")
}

blockLS, _ := pem.Decode([]byte(rootCertLowS))
if blockLS == nil {
t.Fatal("failed to decode block")
}

certLowS, err := x509.ParseCertificate(blockLS.Bytes)
if err != nil {
t.Error(err)
}
if certLowS == nil {
t.Fatal("failed to parse cert")
}

if certHightS.Equal(certLowS) {
t.Fatal("failed compare tow cert")
}

optsHightS := &x509.VerifyOptions{Roots: x509.NewCertPool(), Intermediates: x509.NewCertPool()}
optsHightS.Roots.AddCert(certHightS)
optsHightS.CurrentTime = certHightS.NotBefore.Add(time.Second)
optsLowS := &x509.VerifyOptions{Roots: x509.NewCertPool(), Intermediates: x509.NewCertPool()}
optsLowS.Roots.AddCert(certLowS)
optsLowS.CurrentTime = certLowS.NotBefore.Add(time.Second)

_, err = certHightS.Verify(*optsHightS)
if err != nil {
t.Error(err)
}

_, err = certLowS.Verify(*optsLowS)
if err != nil {
t.Error(err)
}

_, err = certLowS.Verify(*optsHightS)
if err != nil {
t.Error(err)
}
_, err = certHightS.Verify(*optsLowS)
if err != nil {
t.Error(err)
}
}