Skip to content

Commit c119b2a

Browse files
authored
[configtls] add new config to specify TLS curve preferences (#12174)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Allow users to mention their preferred curve types for ECDHE handshake. Add a new config option for this. This will provide users with more control over specific settings during the handshake process. <!--Describe what testing was performed and which tests were added.--> #### Testing Added <!--Describe the documentation added.--> #### Documentation Updated readme<!--Please delete paragraphs that you did not use before submitting.--> Note: Please let me know if I need to open an issue for this.
1 parent 3203167 commit c119b2a

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: configtls
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Allow users to mention their preferred curve types for ECDHE handshake
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12174]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

config/configtls/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ By default, TLS is enabled:
1414
the exporter's HTTPs or gRPC connection. See
1515
[grpc.WithInsecure()](https://godoc.org/google.golang.org/grpc#WithInsecure)
1616
for gRPC.
17+
- `curve_preferences` (default = []): specify your curve preferences that will
18+
be used in an ECDHE handshake, in preference order. Accepted values are:
19+
- X25519
20+
- P521
21+
- P256
22+
- P384
1723

1824
As a result, the following parameters are also required:
1925

config/configtls/configtls.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ type Config struct {
7070
// ReloadInterval specifies the duration after which the certificate will be reloaded
7171
// If not set, it will never be reloaded (optional)
7272
ReloadInterval time.Duration `mapstructure:"reload_interval"`
73+
74+
// contains the elliptic curves that will be used in
75+
// an ECDHE handshake, in preference order
76+
// Defaults to empty list and "crypto/tls" defaults are used, internally.
77+
CurvePreferences []string `mapstructure:"curve_preferences"`
7378
}
7479

7580
// NewDefaultConfig creates a new TLSSetting with any default values set.
@@ -231,6 +236,14 @@ func (c Config) loadTLSConfig() (*tls.Config, error) {
231236
if err != nil {
232237
return nil, err
233238
}
239+
curvePreferences := make([]tls.CurveID, 0, len(c.CurvePreferences))
240+
for _, curve := range c.CurvePreferences {
241+
curveID, ok := tlsCurveTypes[curve]
242+
if !ok {
243+
return nil, fmt.Errorf("invalid curve type: %s. Expected values are [P-256, P-384, P-521, X25519]", curveID)
244+
}
245+
curvePreferences = append(curvePreferences, curveID)
246+
}
234247

235248
return &tls.Config{
236249
RootCAs: certPool,
@@ -239,6 +252,7 @@ func (c Config) loadTLSConfig() (*tls.Config, error) {
239252
MinVersion: minTLS,
240253
MaxVersion: maxTLS,
241254
CipherSuites: cipherSuites,
255+
CurvePreferences: curvePreferences,
242256
}, nil
243257
}
244258

@@ -448,3 +462,10 @@ var tlsVersions = map[string]uint16{
448462
"1.2": tls.VersionTLS12,
449463
"1.3": tls.VersionTLS13,
450464
}
465+
466+
var tlsCurveTypes = map[string]tls.CurveID{
467+
"P256": tls.CurveP256,
468+
"P384": tls.CurveP384,
469+
"P521": tls.CurveP521,
470+
"X25519": tls.X25519,
471+
}

config/configtls/configtls_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,3 +871,53 @@ func TestSystemCertPool_loadCert(t *testing.T) {
871871
})
872872
}
873873
}
874+
875+
func TestCurvePreferences(t *testing.T) {
876+
tests := []struct {
877+
name string
878+
preferences []string
879+
expectedCurveIDs []tls.CurveID
880+
expectedErr string
881+
}{
882+
{
883+
name: "X25519",
884+
preferences: []string{"X25519"},
885+
expectedCurveIDs: []tls.CurveID{tls.X25519},
886+
},
887+
{
888+
name: "P521",
889+
preferences: []string{"P521"},
890+
expectedCurveIDs: []tls.CurveID{tls.CurveP521},
891+
},
892+
{
893+
name: "P-256",
894+
preferences: []string{"P256"},
895+
expectedCurveIDs: []tls.CurveID{tls.CurveP256},
896+
},
897+
{
898+
name: "multiple",
899+
preferences: []string{"P256", "P521", "X25519"},
900+
expectedCurveIDs: []tls.CurveID{tls.CurveP256, tls.CurveP521, tls.X25519},
901+
},
902+
{
903+
name: "invalid-curve",
904+
preferences: []string{"P25223236"},
905+
expectedCurveIDs: []tls.CurveID{},
906+
expectedErr: "invalid curve type",
907+
},
908+
}
909+
for _, test := range tests {
910+
tlsSetting := ClientConfig{
911+
Config: Config{
912+
CurvePreferences: test.preferences,
913+
},
914+
}
915+
config, err := tlsSetting.LoadTLSConfig(context.Background())
916+
if test.expectedErr == "" {
917+
require.NoError(t, err)
918+
require.ElementsMatchf(t, test.expectedCurveIDs, config.CurvePreferences, "expected %v, got %v", test.expectedCurveIDs, config.CurvePreferences)
919+
} else {
920+
require.ErrorContains(t, err, test.expectedErr)
921+
}
922+
}
923+
}

0 commit comments

Comments
 (0)