You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"crypto/tls""crypto/x509""errors"
)
funcmain() {
// VerifyPeerCertificate can be used to replace and customize certificate// verification. This example shows a VerifyPeerCertificate implementation// that will be approximately equivalent to what crypto/tls does normally.config:=&tls.Config{
// Set InsecureSkipVerify to skip the default validation we are// replacing. This will not disable VerifyPeerCertificate.InsecureSkipVerify: true,
// While packages like net/http will implicitly set ServerName, the// VerifyPeerCertificate callback can't access that value, so it has to be set// explicitly here or in VerifyPeerCertificate on the client side. If in// an http.Transport DialTLS callback, this can be obtained by passing// the addr argument to net.SplitHostPort.ServerName: "example.com",
// On the server side, set ClientAuth to require client certificates (or// VerifyPeerCertificate will run anyway and panic accessing certs[0])// but not verify them with the default verifier.// ClientAuth: tls.RequireAnyClientCert,
}
config.VerifyPeerCertificate=func(certificates [][]byte, _ [][]*x509.Certificate) error {
certs:=make([]*x509.Certificate, len(certificates))
fori, asn1Data:=rangecertificates {
cert, err:=x509.ParseCertificate(asn1Data)
iferr!=nil {
returnerrors.New("tls: failed to parse certificate from server: "+err.Error())
}
certs[i] =cert
}
opts:= x509.VerifyOptions{
Roots: config.RootCAs, // On the server side, use config.ClientCAs.DNSName: config.ServerName,
Intermediates: x509.NewCertPool(),
// On the server side, set KeyUsages to ExtKeyUsageClientAuth. The// default value is appropriate for clients side verification.// KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
for_, cert:=rangecerts[1:] {
opts.Intermediates.AddCert(cert)
}
_, err:=certs[0].Verify(opts)
returnerr
}
// Note that when InsecureSkipVerify and VerifyPeerCertificate are in use,// ConnectionState.VerifiedChains will be nil.
}
Instead of c.config.time() we could directly use time.Now(), in fact:
This seems to be doable by using the following code:
copied from the standard library as the body of the
VerifyConnection
callback oftls.Config
, as shown in the following example code:Instead of
c.config.time()
we could directly usetime.Now()
, in fact:or we can just leave it empty as in the example, because:
The text was updated successfully, but these errors were encountered: