@@ -1106,20 +1106,28 @@ func (c *Config) time() time.Time {
1106
1106
return t ()
1107
1107
}
1108
1108
1109
- func (c * Config ) cipherSuites () []uint16 {
1109
+ func (c * Config ) cipherSuites (aesGCMPreferred bool ) []uint16 {
1110
+ var cipherSuites []uint16
1110
1111
if c .CipherSuites == nil {
1111
- if fips140tls .Required () {
1112
- return defaultCipherSuitesFIPS
1113
- }
1114
- return defaultCipherSuites ()
1112
+ cipherSuites = defaultCipherSuites (aesGCMPreferred )
1113
+ } else {
1114
+ cipherSuites = supportedCipherSuites (aesGCMPreferred )
1115
+ cipherSuites = slices .DeleteFunc (cipherSuites , func (id uint16 ) bool {
1116
+ return ! slices .Contains (c .CipherSuites , id )
1117
+ })
1115
1118
}
1116
1119
if fips140tls .Required () {
1117
- cipherSuites := slices .Clone (c .CipherSuites )
1118
- return slices .DeleteFunc (cipherSuites , func (id uint16 ) bool {
1119
- return ! slices .Contains (defaultCipherSuitesFIPS , id )
1120
+ cipherSuites = slices .DeleteFunc (cipherSuites , func (id uint16 ) bool {
1121
+ return ! slices .Contains (allowedCipherSuitesFIPS , id )
1120
1122
})
1121
1123
}
1122
- return c .CipherSuites
1124
+ return cipherSuites
1125
+ }
1126
+
1127
+ // supportedCipherSuites returns the supported TLS 1.0–1.2 cipher suites in an
1128
+ // undefined order. For preference ordering, use [Config.cipherSuites].
1129
+ func (c * Config ) supportedCipherSuites () []uint16 {
1130
+ return c .cipherSuites (false )
1123
1131
}
1124
1132
1125
1133
var supportedVersions = []uint16 {
@@ -1139,7 +1147,7 @@ var tls10server = godebug.New("tls10server")
1139
1147
func (c * Config ) supportedVersions (isClient bool ) []uint16 {
1140
1148
versions := make ([]uint16 , 0 , len (supportedVersions ))
1141
1149
for _ , v := range supportedVersions {
1142
- if fips140tls .Required () && ! slices .Contains (defaultSupportedVersionsFIPS , v ) {
1150
+ if fips140tls .Required () && ! slices .Contains (allowedSupportedVersionsFIPS , v ) {
1143
1151
continue
1144
1152
}
1145
1153
if (c == nil || c .MinVersion == 0 ) && v < VersionTLS12 {
@@ -1184,11 +1192,11 @@ func supportedVersionsFromMax(maxVersion uint16) []uint16 {
1184
1192
}
1185
1193
1186
1194
func (c * Config ) curvePreferences (version uint16 ) []CurveID {
1187
- var curvePreferences [] CurveID
1195
+ curvePreferences := defaultCurvePreferences ()
1188
1196
if fips140tls .Required () {
1189
- curvePreferences = slices .Clone ( defaultCurvePreferencesFIPS )
1190
- } else {
1191
- curvePreferences = defaultCurvePreferences ( )
1197
+ curvePreferences = slices .DeleteFunc ( curvePreferences , func ( x CurveID ) bool {
1198
+ return ! slices . Contains ( allowedCurvePreferencesFIPS , x )
1199
+ } )
1192
1200
}
1193
1201
if c != nil && len (c .CurvePreferences ) != 0 {
1194
1202
curvePreferences = slices .DeleteFunc (curvePreferences , func (x CurveID ) bool {
@@ -1202,23 +1210,16 @@ func (c *Config) curvePreferences(version uint16) []CurveID {
1202
1210
}
1203
1211
1204
1212
func (c * Config ) supportsCurve (version uint16 , curve CurveID ) bool {
1205
- for _ , cc := range c .curvePreferences (version ) {
1206
- if cc == curve {
1207
- return true
1208
- }
1209
- }
1210
- return false
1213
+ return slices .Contains (c .curvePreferences (version ), curve )
1211
1214
}
1212
1215
1213
1216
// mutualVersion returns the protocol version to use given the advertised
1214
1217
// versions of the peer. Priority is given to the peer preference order.
1215
1218
func (c * Config ) mutualVersion (isClient bool , peerVersions []uint16 ) (uint16 , bool ) {
1216
1219
supportedVersions := c .supportedVersions (isClient )
1217
- for _ , peerVersion := range peerVersions {
1218
- for _ , v := range supportedVersions {
1219
- if v == peerVersion {
1220
- return v , true
1221
- }
1220
+ for _ , v := range peerVersions {
1221
+ if slices .Contains (supportedVersions , v ) {
1222
+ return v , true
1222
1223
}
1223
1224
}
1224
1225
return 0 , false
@@ -1339,7 +1340,7 @@ func (chi *ClientHelloInfo) SupportsCertificate(c *Certificate) error {
1339
1340
}
1340
1341
// Finally, there needs to be a mutual cipher suite that uses the static
1341
1342
// RSA key exchange instead of ECDHE.
1342
- rsaCipherSuite := selectCipherSuite (chi .CipherSuites , config .cipherSuites (), func (c * cipherSuite ) bool {
1343
+ rsaCipherSuite := selectCipherSuite (chi .CipherSuites , config .supportedCipherSuites (), func (c * cipherSuite ) bool {
1343
1344
if c .flags & suiteECDHE != 0 {
1344
1345
return false
1345
1346
}
@@ -1416,7 +1417,7 @@ func (chi *ClientHelloInfo) SupportsCertificate(c *Certificate) error {
1416
1417
// Make sure that there is a mutually supported cipher suite that works with
1417
1418
// this certificate. Cipher suite selection will then apply the logic in
1418
1419
// reverse to pick it. See also serverHandshakeState.cipherSuiteOk.
1419
- cipherSuite := selectCipherSuite (chi .CipherSuites , config .cipherSuites (), func (c * cipherSuite ) bool {
1420
+ cipherSuite := selectCipherSuite (chi .CipherSuites , config .supportedCipherSuites (), func (c * cipherSuite ) bool {
1420
1421
if c .flags & suiteECDHE == 0 {
1421
1422
return false
1422
1423
}
@@ -1660,19 +1661,14 @@ func unexpectedMessageError(wanted, got any) error {
1660
1661
1661
1662
// supportedSignatureAlgorithms returns the supported signature algorithms.
1662
1663
func supportedSignatureAlgorithms () []SignatureScheme {
1663
- if ! fips140tls .Required () {
1664
- return defaultSupportedSignatureAlgorithms
1664
+ if fips140tls .Required () {
1665
+ return allowedSupportedSignatureAlgorithmsFIPS
1665
1666
}
1666
- return defaultSupportedSignatureAlgorithmsFIPS
1667
+ return defaultSupportedSignatureAlgorithms
1667
1668
}
1668
1669
1669
1670
func isSupportedSignatureAlgorithm (sigAlg SignatureScheme , supportedSignatureAlgorithms []SignatureScheme ) bool {
1670
- for _ , s := range supportedSignatureAlgorithms {
1671
- if s == sigAlg {
1672
- return true
1673
- }
1674
- }
1675
- return false
1671
+ return slices .Contains (supportedSignatureAlgorithms , sigAlg )
1676
1672
}
1677
1673
1678
1674
// CertificateVerificationError is returned when certificate verification fails during the handshake.
@@ -1721,24 +1717,10 @@ func fipsAllowChain(chain []*x509.Certificate) bool {
1721
1717
}
1722
1718
1723
1719
for _ , cert := range chain {
1724
- if ! fipsAllowCert (cert ) {
1720
+ if ! isCertificateAllowedFIPS (cert ) {
1725
1721
return false
1726
1722
}
1727
1723
}
1728
1724
1729
1725
return true
1730
1726
}
1731
-
1732
- func fipsAllowCert (c * x509.Certificate ) bool {
1733
- // The key must be RSA 2048, RSA 3072, RSA 4096,
1734
- // or ECDSA P-256, P-384, P-521.
1735
- switch k := c .PublicKey .(type ) {
1736
- case * rsa.PublicKey :
1737
- size := k .N .BitLen ()
1738
- return size == 2048 || size == 3072 || size == 4096
1739
- case * ecdsa.PublicKey :
1740
- return k .Curve == elliptic .P256 () || k .Curve == elliptic .P384 () || k .Curve == elliptic .P521 ()
1741
- }
1742
-
1743
- return false
1744
- }
0 commit comments