Skip to content

Commit

Permalink
handshake: clone the tls.Config returned by GetConfigForClient
Browse files Browse the repository at this point in the history
We modify this tls.Config, so we shouldn't modify it. This could cause
conflicts with how the application is using that config.
  • Loading branch information
marten-seemann committed Oct 27, 2023
1 parent e2622bf commit 85b8e39
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions internal/handshake/crypto_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func addConnToClientHelloInfo(conf *tls.Config, localAddr, remoteAddr net.Addr)
info.Conn = &conn{localAddr: localAddr, remoteAddr: remoteAddr}
c, err := gcfc(info)
if c != nil {
c = c.Clone()
// We're returning a tls.Config here, so we need to apply this recursively.
addConnToClientHelloInfo(c, localAddr, remoteAddr)
}
Expand Down
21 changes: 13 additions & 8 deletions internal/handshake/crypto_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/x509/pkix"
"math/big"
"net"
"reflect"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -148,15 +149,17 @@ var _ = Describe("Crypto Setup TLS", func() {
It("wraps GetConfigForClient, recursively", func() {
var localAddr, remoteAddr net.Addr
tlsConf := &tls.Config{}
var innerConf *tls.Config
getCert := func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.20.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)

Check failure on line 153 in internal/handshake/crypto_setup_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (Go 1.21.x)

init$1$5$3$1 - result 1 (error) is always nil (unparam)
localAddr = info.Conn.LocalAddr()
remoteAddr = info.Conn.RemoteAddr()
cert := generateCert()
return &cert, nil
}
tlsConf.GetConfigForClient = func(info *tls.ClientHelloInfo) (*tls.Config, error) {
conf := tlsConf.Clone()
conf.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
localAddr = info.Conn.LocalAddr()
remoteAddr = info.Conn.RemoteAddr()
cert := generateCert()
return &cert, nil
}
return conf, nil
innerConf = tlsConf.Clone()
innerConf.GetCertificate = getCert
return innerConf, nil
}
addConnToClientHelloInfo(tlsConf, local, remote)
conf, err := tlsConf.GetConfigForClient(&tls.ClientHelloInfo{})
Expand All @@ -165,6 +168,8 @@ var _ = Describe("Crypto Setup TLS", func() {
Expect(err).ToNot(HaveOccurred())
Expect(localAddr).To(Equal(local))
Expect(remoteAddr).To(Equal(remote))
// make sure that the tls.Config returned by GetConfigForClient isn't modified
Expect(reflect.ValueOf(innerConf.GetCertificate).Pointer() == reflect.ValueOf(getCert).Pointer()).To(BeTrue())
})
})

Expand Down

0 comments on commit 85b8e39

Please sign in to comment.