Skip to content

Commit bd18c09

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Adam Langley <[email protected]>
1 parent a25d0d8 commit bd18c09

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/crypto/tls/tls.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,14 @@ func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (Certificate, error) {
237237
skippedBlockTypes = append(skippedBlockTypes, keyDERBlock.Type)
238238
}
239239

240-
var err error
241-
cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes)
240+
// We don't need to parse the public key for TLS, but we so do anyway
241+
// to check that it looks sane and matches the private key.
242+
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
242243
if err != nil {
243244
return fail(err)
244245
}
245246

246-
// We don't need to parse the public key for TLS, but we so do anyway
247-
// to check that it looks sane and matches the private key.
248-
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
247+
cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes)
249248
if err != nil {
250249
return fail(err)
251250
}

0 commit comments

Comments
 (0)