Skip to content

Commit 1ecbd86

Browse files
authored
security: Allow passing CurvePreferences in advancedtls (grpc#9292)
This PR adds a passthrough for `tls.Config.CurvePreferences` in `advancedtls.Options`. RELEASE NOTES: * Add passthrough for `tls.Config.CurvePreferences` in `advancedtls.Options`.
1 parent 58bca5a commit 1ecbd86

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

security/advancedtls/advancedtls.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ type Options struct {
221221
// ciphersuites. TLS 1.3 ciphersuites are not configurable. If nil, a
222222
// safe default list is used.
223223
CipherSuites []uint16
224+
// CurvePreferences contains the elliptic curves that will be used during the
225+
// key exchange, in preference order. If empty, the default will be used.
226+
CurvePreferences []tls.CurveID
224227
// serverNameOverride is for testing only and only relevant on the client
225228
// side. If set to a non-empty string, it will override the virtual host
226229
// name of authority (e.g. :authority header field) in requests and the
@@ -278,6 +281,7 @@ func (o *Options) clientConfig() (*tls.Config, error) {
278281
MinVersion: o.MinTLSVersion,
279282
MaxVersion: o.MaxTLSVersion,
280283
CipherSuites: o.CipherSuites,
284+
CurvePreferences: o.CurvePreferences,
281285
}
282286
// Propagate root-certificate-related fields in tls.Config.
283287
switch {
@@ -363,10 +367,11 @@ func (o *Options) serverConfig() (*tls.Config, error) {
363367
clientAuth = tls.RequireAnyClientCert
364368
}
365369
config := &tls.Config{
366-
ClientAuth: clientAuth,
367-
MinVersion: o.MinTLSVersion,
368-
MaxVersion: o.MaxTLSVersion,
369-
CipherSuites: o.CipherSuites,
370+
ClientAuth: clientAuth,
371+
MinVersion: o.MinTLSVersion,
372+
MaxVersion: o.MaxTLSVersion,
373+
CipherSuites: o.CipherSuites,
374+
CurvePreferences: o.CurvePreferences,
370375
}
371376
// Propagate root-certificate-related fields in tls.Config.
372377
switch {

security/advancedtls/advancedtls_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
161161
MinVersion uint16
162162
MaxVersion uint16
163163
cipherSuites []uint16
164+
curvePreferences []tls.CurveID
164165
}{
165166
{
166167
desc: "Use system default if no fields in RootCertificateOptions is specified",
@@ -187,6 +188,13 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
187188
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
188189
},
189190
},
191+
{
192+
desc: "CurvePreferences plumbing through client options",
193+
curvePreferences: []tls.CurveID{
194+
tls.X25519,
195+
tls.CurveP256,
196+
},
197+
},
190198
}
191199
for _, test := range tests {
192200
test := test
@@ -198,6 +206,7 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
198206
MinTLSVersion: test.MinVersion,
199207
MaxTLSVersion: test.MaxVersion,
200208
CipherSuites: test.cipherSuites,
209+
CurvePreferences: test.curvePreferences,
201210
}
202211
clientConfig, err := clientOptions.clientConfig()
203212
if err != nil {
@@ -232,6 +241,9 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
232241
if diff := cmp.Diff(clientConfig.CipherSuites, test.cipherSuites); diff != "" {
233242
t.Errorf("cipherSuites diff (-want +got):\n%s", diff)
234243
}
244+
if diff := cmp.Diff(clientConfig.CurvePreferences, test.curvePreferences); diff != "" {
245+
t.Errorf("curvePreferences diff (-want +got):\n%s", diff)
246+
}
235247
})
236248
}
237249
}
@@ -317,6 +329,7 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
317329
MinVersion uint16
318330
MaxVersion uint16
319331
cipherSuites []uint16
332+
curvePreferences []tls.CurveID
320333
}{
321334
{
322335
desc: "Use system default if no fields in RootCertificateOptions is specified",
@@ -367,6 +380,19 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
367380
},
368381
MinVersion: tls.VersionTLS12,
369382
},
383+
{
384+
desc: "CurvePreferences plumbing through server options",
385+
IdentityOptions: IdentityCertificateOptions{
386+
Certificates: []tls.Certificate{},
387+
},
388+
RootOptions: RootCertificateOptions{
389+
RootCertificates: x509.NewCertPool(),
390+
},
391+
curvePreferences: []tls.CurveID{
392+
tls.X25519,
393+
tls.CurveP256,
394+
},
395+
},
370396
}
371397
for _, test := range tests {
372398
test := test
@@ -379,6 +405,7 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
379405
MinTLSVersion: test.MinVersion,
380406
MaxTLSVersion: test.MaxVersion,
381407
CipherSuites: test.cipherSuites,
408+
CurvePreferences: test.curvePreferences,
382409
}
383410
serverConfig, err := serverOptions.serverConfig()
384411
if err != nil {
@@ -395,6 +422,9 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
395422
if diff := cmp.Diff(serverConfig.CipherSuites, test.cipherSuites); diff != "" {
396423
t.Errorf("cipherSuites diff (-want +got):\n%s", diff)
397424
}
425+
if diff := cmp.Diff(serverConfig.CurvePreferences, test.curvePreferences); diff != "" {
426+
t.Errorf("curvePreferences diff (-want +got):\n%s", diff)
427+
}
398428
})
399429
}
400430
}

0 commit comments

Comments
 (0)