Validate TLS certificates and peer identity
Validate TLS certificates and peer identity
TLS encrypts traffic and authenticates a server when certificate chain and hostname checks succeed. Use trusted roots, the expected server name and library defaults or a reviewed minimum version. Do not disable verification to fix certificate errors. This lab uses httptest only as a ready-made local TLS server fixture; no HTTP knowledge or external CA is required. Its test certificate is not a production trust anchor.
package main
import ( "crypto/tls" "crypto/x509" "fmt" "io" "log" "net" "net/http" "net/http/httptest" "time")
func check(e error) { if e != nil { panic(e) }}func fixture() (*httptest.Server, *x509.CertPool) { s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })) s.Config.ErrorLog = log.New(io.Discard, "", 0) s.StartTLS() roots := x509.NewCertPool() roots.AddCert(s.Certificate()) return s, roots}func connect(s *httptest.Server, roots *x509.CertPool, name string) (*tls.Conn, error) { d := net.Dialer{Timeout: 2 * time.Second} return tls.DialWithDialer(&d, "tcp", s.Listener.Addr().String(), &tls.Config{RootCAs: roots, ServerName: name, MinVersion: tls.VersionTLS12})}func main() { s, roots := fixture() defer s.Close() c, e := connect(s, roots, "127.0.0.1") check(e) defer c.Close() state := c.ConnectionState() fmt.Println(state.HandshakeComplete, len(state.VerifiedChains) > 0)}NewCertPool creates an explicit test trust store. AddCert adds the known fixture certificate only. ServerName must match a certificate identity; the local fixture includes loopback IP. DialWithDialer bounds connection and handshake establishment. VerifiedChains reports successful chain verification; the server fixture requires neither credentials nor external access. The fixture discards expected test-server handshake logs to keep output deterministic, not as production logging policy.
Expected output:
true trueReject an untrusted root and wrong name
package main
import ( "crypto/tls" "crypto/x509" "errors" "fmt" "io" "log" "net" "net/http" "net/http/httptest" "time")
func check(e error) { if e != nil { panic(e) }}func fixture() (*httptest.Server, *x509.CertPool) { s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })) s.Config.ErrorLog = log.New(io.Discard, "", 0) s.StartTLS() roots := x509.NewCertPool() roots.AddCert(s.Certificate()) return s, roots}func connect(s *httptest.Server, roots *x509.CertPool, name string) (*tls.Conn, error) { d := net.Dialer{Timeout: 2 * time.Second} return tls.DialWithDialer(&d, "tcp", s.Listener.Addr().String(), &tls.Config{RootCAs: roots, ServerName: name, MinVersion: tls.VersionTLS12})}func main() { s, roots := fixture() defer s.Close() c, e := connect(s, x509.NewCertPool(), "127.0.0.1") if c != nil { c.Close() } var unknown x509.UnknownAuthorityError fmt.Println(errors.As(e, &unknown)) c, e = connect(s, roots, "wrong.example.test") if c != nil { c.Close() } var mismatch x509.HostnameError fmt.Println(errors.As(e, &mismatch))}The first attempt trusts no root; the second trusts the fixture but requests a different name. Both must fail. In a real deployment use the system trust store or properly managed private CA, correct DNS identity and valid certificates. TLS server authentication does not replace application authorization. Local verification does not test real PKI rotation, revocation policy or every TLS configuration.
Expected output:
truetrueTry it locally
Connect to the local TLS fixture with its trusted certificate and correct name; print whether the negotiated TLS version is at least TLS 1.2.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Networking 07: Validate TLS certificates and peer identity
Check the lesson objective before moving on.
What is required beyond a trusted certificate chain?
How should an unknown-authority error be handled?