Skip to content

Commit 7629190

Browse files
committed
Add fips and nofips implementations for allowed TLS curves
1 parent 995bea5 commit 7629190

File tree

4 files changed

+69
-23
lines changed

4 files changed

+69
-23
lines changed

config/configtls/configtls.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"crypto/x509"
1010
"errors"
1111
"fmt"
12+
"maps"
1213
"os"
1314
"path/filepath"
15+
"slices"
1416
"sync"
1517
"time"
1618

@@ -268,15 +270,24 @@ func (c Config) loadTLSConfig() (*tls.Config, error) {
268270
if err != nil {
269271
return nil, err
270272
}
273+
274+
allowedCurves := slices.Collect(maps.Values(tlsCurveTypes))
271275
curvePreferences := make([]tls.CurveID, 0, len(c.CurvePreferences))
272276
for _, curve := range c.CurvePreferences {
273277
curveID, ok := tlsCurveTypes[curve]
274278
if !ok {
275-
return nil, fmt.Errorf("invalid curve type: %s. Expected values are [P-256, P-384, P-521, X25519, X25519MLKEM768]", curveID)
279+
return nil, fmt.Errorf("invalid curve type: %s. Expected values are %s", curveID, allowedCurves)
276280
}
277281
curvePreferences = append(curvePreferences, curveID)
278282
}
279283

284+
// If no curve preferences were explicitly specified in the configuration, use
285+
// the ones we allow. This helps in particular with FIPS builds where not all curves
286+
// are allowed.
287+
if len(curvePreferences) == 0 {
288+
curvePreferences = allowedCurves
289+
}
290+
280291
return &tls.Config{
281292
RootCAs: certPool,
282293
GetCertificate: getCertificate,
@@ -501,11 +512,3 @@ var tlsVersions = map[string]uint16{
501512
"1.2": tls.VersionTLS12,
502513
"1.3": tls.VersionTLS13,
503514
}
504-
505-
var tlsCurveTypes = map[string]tls.CurveID{
506-
"P256": tls.CurveP256,
507-
"P384": tls.CurveP384,
508-
"P521": tls.CurveP521,
509-
"X25519": tls.X25519,
510-
"X25519MLKEM768": tls.X25519MLKEM768,
511-
}

config/configtls/configtls_test.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io"
1313
"os"
1414
"path/filepath"
15+
"strings"
1516
"testing"
1617
"time"
1718

@@ -882,22 +883,14 @@ func TestSystemCertPool_loadCert(t *testing.T) {
882883
}
883884

884885
func TestCurvePreferences(t *testing.T) {
885-
tests := []struct {
886+
type testCase struct {
886887
name string
887888
preferences []string
888889
expectedCurveIDs []tls.CurveID
889890
expectedErr string
890-
}{
891-
{
892-
name: "X25519MLKEM768",
893-
preferences: []string{"X25519MLKEM768"},
894-
expectedCurveIDs: []tls.CurveID{tls.X25519MLKEM768},
895-
},
896-
{
897-
name: "X25519",
898-
preferences: []string{"X25519"},
899-
expectedCurveIDs: []tls.CurveID{tls.X25519},
900-
},
891+
}
892+
893+
tests := []testCase{
901894
{
902895
name: "P521",
903896
preferences: []string{"P521"},
@@ -910,8 +903,8 @@ func TestCurvePreferences(t *testing.T) {
910903
},
911904
{
912905
name: "multiple",
913-
preferences: []string{"P256", "P521", "X25519"},
914-
expectedCurveIDs: []tls.CurveID{tls.CurveP256, tls.CurveP521, tls.X25519},
906+
preferences: []string{"P256", "P521"},
907+
expectedCurveIDs: []tls.CurveID{tls.CurveP256, tls.CurveP521},
915908
},
916909
{
917910
name: "invalid-curve",
@@ -920,6 +913,24 @@ func TestCurvePreferences(t *testing.T) {
920913
expectedErr: "invalid curve type",
921914
},
922915
}
916+
917+
// X25519 curves are not supported when GODEBUG=fips140=only is set, so we
918+
// detect if it is and conditionally add test cases for those curves.
919+
if !strings.Contains(os.Getenv("GODEBUG"), "fips140=only") {
920+
tests = append(tests,
921+
testCase{
922+
name: "X25519MLKEM768",
923+
preferences: []string{"X25519MLKEM768"},
924+
expectedCurveIDs: []tls.CurveID{tls.X25519MLKEM768},
925+
},
926+
testCase{
927+
name: "X25519",
928+
preferences: []string{"X25519"},
929+
expectedCurveIDs: []tls.CurveID{tls.X25519},
930+
},
931+
)
932+
}
933+
923934
for _, test := range tests {
924935
tlsSetting := ClientConfig{
925936
Config: Config{

config/configtls/curves_fips.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:build requirefips
5+
6+
package configtls
7+
8+
import "crypto/tls"
9+
10+
var tlsCurveTypes = map[string]tls.CurveID{
11+
"P256": tls.CurveP256,
12+
"P384": tls.CurveP384,
13+
"P521": tls.CurveP521,
14+
15+
// The following X25519 curves are not available in FIPS mode, so we remove them from the map.
16+
// See also https://cs.opensource.google/go/go/+/refs/tags/go1.24.6:src/crypto/ecdh/x25519.go
17+
//"X25519": tls.X25519,
18+
//"X25519MLKEM768": tls.X25519MLKEM768,
19+
}

config/configtls/curves_nofips.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !requirefips
2+
3+
package configtls
4+
5+
import "crypto/tls"
6+
7+
var tlsCurveTypes = map[string]tls.CurveID{
8+
"P256": tls.CurveP256,
9+
"P384": tls.CurveP384,
10+
"P521": tls.CurveP521,
11+
"X25519": tls.X25519,
12+
"X25519MLKEM768": tls.X25519MLKEM768,
13+
}

0 commit comments

Comments
 (0)