Skip to content

Commit 7eb5941

Browse files
committed
crypto/tls: replace VerifyPeerCertificate example with VerifyConnection
Look at how much better it is! Updates #36736 Change-Id: I53a314a103a42dd869c05823fa50f37d70f9d283 Reviewed-on: https://go-review.googlesource.com/c/go/+/239560 Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Katie Hockman <[email protected]>
1 parent 66cb673 commit 7eb5941

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

src/crypto/tls/example_test.go

+37-42
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package tls_test
77
import (
88
"crypto/tls"
99
"crypto/x509"
10-
"errors"
1110
"log"
1211
"net/http"
1312
"net/http/httptest"
@@ -184,54 +183,50 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
184183
log.Fatal(srv.ListenAndServeTLS("", ""))
185184
}
186185

187-
func ExampleConfig_verifyPeerCertificate() {
188-
// VerifyPeerCertificate can be used to replace and customize certificate
189-
// verification. This example shows a VerifyPeerCertificate implementation
190-
// that will be approximately equivalent to what crypto/tls does normally.
186+
func ExampleConfig_verifyConnection() {
187+
// VerifyConnection can be used to replace and customize connection
188+
// verification. This example shows a VerifyConnection implementation that
189+
// will be approximately equivalent to what crypto/tls does normally to
190+
// verify the peer's certificate.
191191

192-
config := &tls.Config{
192+
// Client side configuration.
193+
_ = &tls.Config{
193194
// Set InsecureSkipVerify to skip the default validation we are
194-
// replacing. This will not disable VerifyPeerCertificate.
195+
// replacing. This will not disable VerifyConnection.
195196
InsecureSkipVerify: true,
196-
197-
// While packages like net/http will implicitly set ServerName, the
198-
// VerifyPeerCertificate callback can't access that value, so it has to be set
199-
// explicitly here or in VerifyPeerCertificate on the client side. If in
200-
// an http.Transport DialTLS callback, this can be obtained by passing
201-
// the addr argument to net.SplitHostPort.
202-
ServerName: "example.com",
203-
204-
// On the server side, set ClientAuth to require client certificates (or
205-
// VerifyPeerCertificate will run anyway and panic accessing certs[0])
206-
// but not verify them with the default verifier.
207-
// ClientAuth: tls.RequireAnyClientCert,
197+
VerifyConnection: func(cs tls.ConnectionState) error {
198+
opts := x509.VerifyOptions{
199+
DNSName: cs.ServerName,
200+
Intermediates: x509.NewCertPool(),
201+
}
202+
for _, cert := range cs.PeerCertificates[1:] {
203+
opts.Intermediates.AddCert(cert)
204+
}
205+
_, err := cs.PeerCertificates[0].Verify(opts)
206+
return err
207+
},
208208
}
209209

210-
config.VerifyPeerCertificate = func(certificates [][]byte, _ [][]*x509.Certificate) error {
211-
certs := make([]*x509.Certificate, len(certificates))
212-
for i, asn1Data := range certificates {
213-
cert, err := x509.ParseCertificate(asn1Data)
214-
if err != nil {
215-
return errors.New("tls: failed to parse certificate from server: " + err.Error())
210+
// Server side configuration.
211+
_ = &tls.Config{
212+
// Require client certificates (or VerifyConnection will run anyway and
213+
// panic accessing cs.PeerCertificates[0]) but don't verify them with the
214+
// default verifier. This will not disable VerifyConnection.
215+
ClientAuth: tls.RequireAnyClientCert,
216+
VerifyConnection: func(cs tls.ConnectionState) error {
217+
opts := x509.VerifyOptions{
218+
DNSName: cs.ServerName,
219+
Intermediates: x509.NewCertPool(),
220+
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
221+
}
222+
for _, cert := range cs.PeerCertificates[1:] {
223+
opts.Intermediates.AddCert(cert)
216224
}
217-
certs[i] = cert
218-
}
219-
220-
opts := x509.VerifyOptions{
221-
Roots: config.RootCAs, // On the server side, use config.ClientCAs.
222-
DNSName: config.ServerName,
223-
Intermediates: x509.NewCertPool(),
224-
// On the server side, set KeyUsages to ExtKeyUsageClientAuth. The
225-
// default value is appropriate for clients side verification.
226-
// KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
227-
}
228-
for _, cert := range certs[1:] {
229-
opts.Intermediates.AddCert(cert)
230-
}
231-
_, err := certs[0].Verify(opts)
232-
return err
225+
_, err := cs.PeerCertificates[0].Verify(opts)
226+
return err
227+
},
233228
}
234229

235-
// Note that when InsecureSkipVerify and VerifyPeerCertificate are in use,
230+
// Note that when certificates are not handled by the default verifier
236231
// ConnectionState.VerifiedChains will be nil.
237232
}

0 commit comments

Comments
 (0)