From: Filippo Valsorda Date: Sun, 28 Jan 2018 22:30:48 +0000 (+0100) Subject: crypto/tls: parse certificate first in X509KeyPair to get better errors X-Git-Tag: go1.11beta1~1076 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bd18c09095ed432f9638b3b1181b3c5390c8c51c;p=gostls13.git crypto/tls: parse certificate first in X509KeyPair to get better errors parsePrivateKey can't return useful error messages because it does trial decoding of multiple formats. Try ParseCertificate first in case it offers a useful error message. Fixes #23591 Change-Id: I380490a5850bee593a7d2f584a27b2a14153d768 Reviewed-on: https://go-review.googlesource.com/90435 Run-TryBot: Filippo Valsorda TryBot-Result: Gobot Gobot Reviewed-by: Adam Langley --- diff --git a/src/crypto/tls/tls.go b/src/crypto/tls/tls.go index 615d1e5576..8fd4294315 100644 --- a/src/crypto/tls/tls.go +++ b/src/crypto/tls/tls.go @@ -237,15 +237,14 @@ func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (Certificate, error) { skippedBlockTypes = append(skippedBlockTypes, keyDERBlock.Type) } - var err error - cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes) + // We don't need to parse the public key for TLS, but we so do anyway + // to check that it looks sane and matches the private key. + x509Cert, err := x509.ParseCertificate(cert.Certificate[0]) if err != nil { return fail(err) } - // We don't need to parse the public key for TLS, but we so do anyway - // to check that it looks sane and matches the private key. - x509Cert, err := x509.ParseCertificate(cert.Certificate[0]) + cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes) if err != nil { return fail(err) }