@@ -7,7 +7,6 @@ package tls_test
7
7
import (
8
8
"crypto/tls"
9
9
"crypto/x509"
10
- "errors"
11
10
"log"
12
11
"net/http"
13
12
"net/http/httptest"
@@ -184,54 +183,50 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
184
183
log .Fatal (srv .ListenAndServeTLS ("" , "" ))
185
184
}
186
185
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.
191
191
192
- config := & tls.Config {
192
+ // Client side configuration.
193
+ _ = & tls.Config {
193
194
// Set InsecureSkipVerify to skip the default validation we are
194
- // replacing. This will not disable VerifyPeerCertificate .
195
+ // replacing. This will not disable VerifyConnection .
195
196
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
+ },
208
208
}
209
209
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 )
216
224
}
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
+ },
233
228
}
234
229
235
- // Note that when InsecureSkipVerify and VerifyPeerCertificate are in use,
230
+ // Note that when certificates are not handled by the default verifier
236
231
// ConnectionState.VerifiedChains will be nil.
237
232
}
0 commit comments