Skip to content

Commit 9309106

Browse files
sxdleonardocemnencia
authored
refactor(tls): switch verification to VerifyConnection (#10478)
Go's `VerifyPeerCertificate` callback is skipped on resumed TLS 1.3 sessions (PSK / session tickets), while `VerifyConnection` runs on every completed handshake. Session resumption is not enabled anywhere in the codebase today (no `ClientSessionCache` is configured), so this has no observable effect, but it aligns the verification hook with the one the Go team documents as resumption-safe and satisfies the golangci-lint 2.11.4 check that flagged the combination. Closes #10477 Signed-off-by: Jonathan Gonzalez V. <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
1 parent 2b29b8e commit 9309106

2 files changed

Lines changed: 44 additions & 31 deletions

File tree

‎pkg/certs/tls.go‎

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ func newTLSConfigFromSecret(
6161
return NewTLSConfigFromCertPool(caCertPool), nil
6262
}
6363

64+
// verifyCertificates validates the peer certificate chain against the trusted CA pool.
65+
func verifyCertificates(certPool *x509.CertPool, certs []*x509.Certificate) error {
66+
if len(certs) == 0 {
67+
return fmt.Errorf("no certificates provided")
68+
}
69+
opts := x509.VerifyOptions{
70+
Roots: certPool,
71+
Intermediates: x509.NewCertPool(),
72+
}
73+
for _, cert := range certs[1:] {
74+
opts.Intermediates.AddCert(cert)
75+
}
76+
_, err := certs[0].Verify(opts)
77+
if err != nil {
78+
return &tls.CertificateVerificationError{UnverifiedCertificates: certs, Err: err}
79+
}
80+
81+
return nil
82+
}
83+
6484
// NewTLSConfigFromCertPool creates a tls.Config object from X509 cert pool
6585
// containing the expected server CA
6686
func NewTLSConfigFromCertPool(
@@ -70,35 +90,11 @@ func NewTLSConfigFromCertPool(
7090
MinVersion: tls.VersionTLS13,
7191
RootCAs: certPool,
7292
InsecureSkipVerify: true, //#nosec G402 -- we are verifying the certificate ourselves
73-
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
74-
// Code adapted from https://go.dev/src/crypto/tls/handshake_client.go#L986
75-
if len(rawCerts) == 0 {
76-
return fmt.Errorf("no raw certificates provided")
77-
}
78-
79-
certs := make([]*x509.Certificate, len(rawCerts))
80-
for i, rawCert := range rawCerts {
81-
cert, err := x509.ParseCertificate(rawCert)
82-
if err != nil {
83-
return fmt.Errorf("failed to parse certificate: %v", err)
84-
}
85-
certs[i] = cert
86-
}
87-
88-
opts := x509.VerifyOptions{
89-
Roots: certPool,
90-
Intermediates: x509.NewCertPool(),
91-
}
92-
93-
for _, cert := range certs[1:] {
94-
opts.Intermediates.AddCert(cert)
95-
}
96-
_, err := certs[0].Verify(opts)
97-
if err != nil {
98-
return &tls.CertificateVerificationError{UnverifiedCertificates: certs, Err: err}
99-
}
100-
101-
return nil
93+
// VerifyConnection runs on every completed handshake, including resumed
94+
// TLS 1.3 sessions where no certificate exchange occurs but the original
95+
// peer certificates remain available in tls.ConnectionState.
96+
VerifyConnection: func(conn tls.ConnectionState) error {
97+
return verifyCertificates(certPool, conn.PeerCertificates)
10298
},
10399
}
104100

‎pkg/certs/tls_test.go‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package certs
2222
import (
2323
"context"
2424
"crypto/tls"
25+
"crypto/x509"
2526
"encoding/pem"
2627
"errors"
2728
"fmt"
@@ -120,6 +121,14 @@ IRh7fVEH8WBfMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDaAAwZQIwBdrw
120121
Expect(tlsConfig.RootCAs).ToNot(BeNil())
121122
})
122123

124+
It("should reject connections with no certificates", func(ctx context.Context) {
125+
tlsConfig, err := newTLSConfigFromSecret(ctx, c, caSecret)
126+
Expect(err).NotTo(HaveOccurred())
127+
err = tlsConfig.VerifyConnection(tls.ConnectionState{})
128+
Expect(err).To(HaveOccurred())
129+
Expect(err.Error()).To(ContainSubstring("no certificates provided"))
130+
})
131+
123132
It("should validate good certificates", func(ctx context.Context) {
124133
tlsConfig, err := newTLSConfigFromSecret(ctx, c, caSecret)
125134
Expect(err).NotTo(HaveOccurred())
@@ -191,7 +200,11 @@ rx8M7UTZeZJC1KjcnJuxJl7+6A8fjqHdZh/y/IFyiZNC9XRqouqWTT3JqI7BQAIw
191200
Zxj1fxVSmUy1TBXz6H0sUvtFh/Fgb6v4qUPdRE6xNJw3lbZUZxHr2xXk5Op/Cw6O
192201
-----END CERTIFICATE-----
193202
`))
194-
err = tlsConfig.VerifyPeerCertificate([][]byte{serverBlock.Bytes}, nil)
203+
cert, err := x509.ParseCertificate(serverBlock.Bytes)
204+
Expect(err).NotTo(HaveOccurred())
205+
err = tlsConfig.VerifyConnection(tls.ConnectionState{
206+
PeerCertificates: []*x509.Certificate{cert},
207+
})
195208
Expect(err).ShouldNot(HaveOccurred())
196209
})
197210

@@ -266,7 +279,11 @@ MQCKGqId+Xj6O6gnoi9xhu0rbzSnMjrURoa1v2d5+O5XssE7LGtJdIKrd2p7EuwE
266279
6dk=
267280
-----END CERTIFICATE-----
268281
`))
269-
err = tlsConfig.VerifyPeerCertificate([][]byte{badServerBlock.Bytes}, nil)
282+
badCert, err := x509.ParseCertificate(badServerBlock.Bytes)
283+
Expect(err).NotTo(HaveOccurred())
284+
err = tlsConfig.VerifyConnection(tls.ConnectionState{
285+
PeerCertificates: []*x509.Certificate{badCert},
286+
})
270287
var certError *tls.CertificateVerificationError
271288
Expect(errors.As(err, &certError)).To(BeTrue())
272289
})

0 commit comments

Comments
 (0)