Skip to content

Commit af4c24d

Browse files
Add SecureOptions.VerifyConnection (#236)
Linting flagged a gap in TLS certificate verification using the SecureOptions.VerifyCertificate function, since this is not invoked when connections are resumed. This change introduces a new SecureOptions.VerifyConnection verification function that uses more recent Go standard library features to perform verification on both connection and resume. It also provides access to additional information for more thorough verification. The VerifyConnection function replaces the older VerififyCertificate method of certification verification. The old method is retained for compatibility. Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
1 parent 5f35bdf commit af4c24d

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

.golangci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ linters:
3333
presets:
3434
- common-false-positives
3535
- std-error-handling
36-
rules:
37-
- path: "_test.go$"
38-
text: "^G703:"
39-
linters:
40-
- gosec
4136
formatters:
4237
enable:
4338
- gofmt

pkg/network/network.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,51 @@ func NewGRPCClient(config ClientConfig) (*GRPCClient, error) {
178178
// GRPCServer or GRPCClient instance
179179
type SecureOptions struct {
180180
// VerifyCertificate, if not nil, is called after normal
181-
// certificate verification by either a TLS client or server.
182-
// If it returns a non-nil error, the handshake is aborted and that error results.
181+
// certificate verification by either a TLS client or server. It
182+
// receives the raw ASN.1 certificates provided by the peer and also
183+
// any verified chains that normal processing found. If it returns a
184+
// non-nil error, the handshake is aborted and that error results.
185+
//
186+
// This callback is not invoked on resumed connections. It is recommended
187+
// to use [SecureOptions.VerifyConnection] instead.
183188
VerifyCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error
189+
190+
// VerifyConnection, if not nil, is called after normal certificate
191+
// verification and after VerifyPeerCertificate by either a TLS client
192+
// or server. If it returns a non-nil error, the handshake is aborted
193+
// and that error results.
194+
//
195+
// If normal verification fails then the handshake will abort before
196+
// considering this callback. This callback will run for all connections,
197+
// including resumptions, regardless of InsecureSkipVerify or ClientAuth
198+
// settings.
199+
//
200+
// For a usage example, see: https://pkg.go.dev/crypto/tls#example-Config-VerifyConnection
201+
VerifyConnection func(tls.ConnectionState) error
202+
184203
// PEM-encoded X509 public key to be used for TLS communication
185204
Certificate []byte
205+
186206
// PEM-encoded private key to be used for TLS communication
187207
Key []byte
208+
188209
// Set of PEM-encoded X509 certificate authorities used by clients to
189210
// verify server certificates
190211
ServerRootCAs [][]byte
212+
191213
// Set of PEM-encoded X509 certificate authorities used by servers to
192214
// verify client certificates
193215
ClientRootCAs [][]byte
216+
194217
// Whether or not to use TLS for communication
195218
UseTLS bool
219+
196220
// Whether or not TLS client must present certificates for authentication
197221
RequireClientCert bool
222+
198223
// CipherSuites is a list of supported cipher suites for TLS
199224
CipherSuites []uint16
225+
200226
// TimeShift makes TLS handshakes time sampling shift to the past by a given duration
201227
TimeShift time.Duration
202228
}
@@ -209,6 +235,7 @@ func (client *GRPCClient) parseSecureOptions(opts SecureOptions) error {
209235

210236
client.tlsConfig = &tls.Config{
211237
VerifyPeerCertificate: opts.VerifyCertificate,
238+
VerifyConnection: opts.VerifyConnection,
212239
MinVersion: tls.VersionTLS12} // TLS 1.2 only
213240

214241
if serverRootCAs, err := newRootCACertPool(opts); err == nil {

0 commit comments

Comments
 (0)