LoadX509KeyPair and X509KeyPair are documented to return a tls.Certificate with a nil Leaf *x509.Certificate field.
// X509KeyPair parses a public/private key pair from a pair of
// PEM encoded data. On successful return, Certificate.Leaf will be nil because
// the parsed form of the certificate is not retained.
func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (Certificate, error)
This was done intentionally out of concern that servers that manage a lot of certificates would be wasting significant amounts of memory to keep the parsed version of the certificates around.
Originally, the parsed certificate was rarely used in crypto/tls. However, the new automatic certificate selection also needs that, causing significant slowdown if the certificates need to be re-parsed every time. See #35504.
Moreover, it seems that despite the documentation note, Leaf being nil catches some users by surprise. #35504 (comment)
I believe setting Leaf in LoadX509KeyPair and X509KeyPair would be a more natural API, and servers that load very large amounts of certificates and need to save memory can explicitly set it to nil.
Now that we have GODEBUGs, I think we should make the change. It's very unlikely there are programs that will break because a field is not nil.
/cc @golang/security @golang/proposal-review
LoadX509KeyPair and X509KeyPair are documented to return a tls.Certificate with a nil
Leaf *x509.Certificatefield.This was done intentionally out of concern that servers that manage a lot of certificates would be wasting significant amounts of memory to keep the parsed version of the certificates around.
Originally, the parsed certificate was rarely used in crypto/tls. However, the new automatic certificate selection also needs that, causing significant slowdown if the certificates need to be re-parsed every time. See #35504.
Moreover, it seems that despite the documentation note, Leaf being nil catches some users by surprise. #35504 (comment)
I believe setting Leaf in LoadX509KeyPair and X509KeyPair would be a more natural API, and servers that load very large amounts of certificates and need to save memory can explicitly set it to nil.
Now that we have GODEBUGs, I think we should make the change. It's very unlikely there are programs that will break because a field is not nil.
/cc @golang/security @golang/proposal-review