Skip to content

Commit

Permalink
advancedtls: add CipherSuites to Options (grpc#7269)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewstevenson88 authored May 29, 2024
1 parent a4593c5 commit 11872f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
12 changes: 9 additions & 3 deletions security/advancedtls/advancedtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ type Options struct {
// which is currently TLS 1.3. This default may be changed over time
// affecting backwards compatibility.
MaxTLSVersion uint16
// CipherSuites is an unordered list of supported TLS 1.0–1.2
// ciphersuites. TLS 1.3 ciphersuites are not configurable. If nil, a
// safe default list is used.
CipherSuites []uint16
// serverNameOverride is for testing only and only relevant on the client
// side. If set to a non-empty string, it will override the virtual host
// name of authority (e.g. :authority header field) in requests and the
Expand Down Expand Up @@ -353,6 +357,7 @@ func (o *Options) clientConfig() (*tls.Config, error) {
InsecureSkipVerify: true,
MinVersion: o.MinTLSVersion,
MaxVersion: o.MaxTLSVersion,
CipherSuites: o.CipherSuites,
}
// Propagate root-certificate-related fields in tls.Config.
switch {
Expand Down Expand Up @@ -467,9 +472,10 @@ func (o *Options) serverConfig() (*tls.Config, error) {
o.MaxTLSVersion = tls.VersionTLS13
}
config := &tls.Config{
ClientAuth: clientAuth,
MinVersion: o.MinTLSVersion,
MaxVersion: o.MaxTLSVersion,
ClientAuth: clientAuth,
MinVersion: o.MinTLSVersion,
MaxVersion: o.MaxTLSVersion,
CipherSuites: o.CipherSuites,
}
// Propagate root-certificate-related fields in tls.Config.
switch {
Expand Down
35 changes: 35 additions & 0 deletions security/advancedtls/advancedtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
lru "github.com/hashicorp/golang-lru"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/tls/certprovider"
Expand Down Expand Up @@ -172,6 +173,7 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
RootOptions RootCertificateOptions
MinVersion uint16
MaxVersion uint16
cipherSuites []uint16
}{
{
desc: "Use system default if no fields in RootCertificateOptions is specified",
Expand All @@ -196,6 +198,15 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
RootCACerts: x509.NewCertPool(),
},
},
{
desc: "Ciphersuite plumbing through client options",
cipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
},
},
}
for _, test := range tests {
test := test
Expand All @@ -206,6 +217,7 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
RootOptions: test.RootOptions,
MinTLSVersion: test.MinVersion,
MaxTLSVersion: test.MaxVersion,
CipherSuites: test.cipherSuites,
}
clientConfig, err := clientOptions.clientConfig()
if err != nil {
Expand Down Expand Up @@ -237,6 +249,9 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
t.Fatalf("Default max tls version not set correctly")
}
}
if diff := cmp.Diff(clientConfig.CipherSuites, test.cipherSuites); diff != "" {
t.Errorf("cipherSuites diff (-want +got):\n%s", diff)
}
})
}
}
Expand Down Expand Up @@ -335,6 +350,7 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
RootOptions RootCertificateOptions
MinVersion uint16
MaxVersion uint16
cipherSuites []uint16
}{
{
desc: "Use system default if no fields in RootCertificateOptions is specified",
Expand Down Expand Up @@ -368,6 +384,21 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
RootCACerts: x509.NewCertPool(),
},
},
{
desc: "Ciphersuite plumbing through server options",
IdentityOptions: IdentityCertificateOptions{
Certificates: []tls.Certificate{},
},
RootOptions: RootCertificateOptions{
RootCACerts: x509.NewCertPool(),
},
cipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
},
},
}
for _, test := range tests {
test := test
Expand All @@ -379,6 +410,7 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
RootOptions: test.RootOptions,
MinTLSVersion: test.MinVersion,
MaxTLSVersion: test.MaxVersion,
CipherSuites: test.cipherSuites,
}
serverConfig, err := serverOptions.serverConfig()
if err != nil {
Expand All @@ -392,6 +424,9 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
t.Fatalf("Failed to assign system-provided certificates on the server side.")
}
}
if diff := cmp.Diff(serverConfig.CipherSuites, test.cipherSuites); diff != "" {
t.Errorf("cipherSuites diff (-want +got):\n%s", diff)
}
})
}
}
Expand Down

0 comments on commit 11872f1

Please sign in to comment.