| 1 |
// Copyright 2016-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto/fips140" |
| 18 |
"crypto/tls" |
| 19 |
) |
| 20 |
|
| 21 |
func init() { |
| 22 |
for _, cs := range tls.CipherSuites() { |
| 23 |
cipherMap[cs.Name] = cs |
| 24 |
cipherMapByID[cs.ID] = cs |
| 25 |
} |
| 26 |
for _, cs := range tls.InsecureCipherSuites() { |
| 27 |
cipherMap[cs.Name] = cs |
| 28 |
cipherMapByID[cs.ID] = cs |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
var cipherMap = map[string]*tls.CipherSuite{} |
| 33 |
var cipherMapByID = map[uint16]*tls.CipherSuite{} |
| 34 |
|
| 35 |
func defaultCipherSuites() []uint16 { |
| 36 |
ciphers := tls.CipherSuites() |
| 37 |
defaults := make([]uint16, 0, len(ciphers)) |
| 38 |
for _, cs := range ciphers { |
| 39 |
defaults = append(defaults, cs.ID) |
| 40 |
} |
| 41 |
return defaults |
| 42 |
} |
| 43 |
|
| 44 |
// Where we maintain available curve preferences |
| 45 |
var curvePreferenceMap = map[string]tls.CurveID{ |
| 46 |
"X25519MLKEM768": tls.X25519MLKEM768, |
| 47 |
"X25519": tls.X25519, |
| 48 |
"CurveP256": tls.CurveP256, |
| 49 |
"CurveP384": tls.CurveP384, |
| 50 |
"CurveP521": tls.CurveP521, |
| 51 |
} |
| 52 |
|
| 53 |
// reorder to default to the highest level of security. See: |
| 54 |
// https://blog.bracebin.com/achieving-perfect-ssl-labs-score-with-go |
| 55 |
func defaultCurvePreferences() []tls.CurveID { |
| 56 |
if fips140.Enabled() { |
| 57 |
// X25519 is not FIPS-approved by itself, but it is when |
| 58 |
// combined with MLKEM768. |
| 59 |
return []tls.CurveID{ |
| 60 |
tls.X25519MLKEM768, // post-quantum |
| 61 |
tls.CurveP256, |
| 62 |
tls.CurveP384, |
| 63 |
tls.CurveP521, |
| 64 |
} |
| 65 |
} |
| 66 |
return []tls.CurveID{ |
| 67 |
tls.X25519MLKEM768, // post-quantum |
| 68 |
tls.X25519, // faster than P256, arguably more secure |
| 69 |
tls.CurveP256, |
| 70 |
tls.CurveP384, |
| 71 |
tls.CurveP521, |
| 72 |
} |
| 73 |
} |