Skip to content

Commit 1b12350

Browse files
GustedGusted
authored andcommitted
feat: use Go's default values for TLS server (#13641)
- It's pointless to set defaults when those are _continously_ being updated by Go to incorporate newer values (e.g. PQ in the last few releases). - It's pointless to figure out a order preference, these are ignored by Go for their own internal preference. - Do less and trust [Go's cryptography principles](https://golang.org/design/cryptography-principles) to keep this safe and modern. Documentation PR: forgejo/docs!2089 ## Test 1. Obtain a certificate via ACME (self-signed is possible, can be generated via https://stackoverflow.com/a/41366949, requires setting some valid hostname). 2. Set `[server].KEY_FILE` and `[server].CERT_FILE` to the certificate. 3. Set `[server].PROTOCOL = https`. 4. Run forgejo. 5. Verify you can connect to Forgejo over HTTPS. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13641 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
1 parent 5b83822 commit 1b12350

2 files changed

Lines changed: 8 additions & 44 deletions

File tree

cmd/web_https.go

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ import (
1212
"forgejo.org/modules/graceful"
1313
"forgejo.org/modules/log"
1414
"forgejo.org/modules/setting"
15-
16-
"github.com/klauspost/cpuid/v2"
1715
)
1816

1917
var tlsVersionStringMap = map[string]uint16{
20-
"": tls.VersionTLS12, // Default to tls.VersionTLS12
2118
"tlsv1.0": tls.VersionTLS10,
2219
"tlsv1.1": tls.VersionTLS11,
2320
"tlsv1.2": tls.VersionTLS12,
@@ -34,10 +31,13 @@ func toTLSVersion(version string) uint16 {
3431
}
3532

3633
var curveStringMap = map[string]tls.CurveID{
37-
"x25519": tls.X25519,
38-
"p256": tls.CurveP256,
39-
"p384": tls.CurveP384,
40-
"p521": tls.CurveP521,
34+
"x25519": tls.X25519,
35+
"p256": tls.CurveP256,
36+
"p384": tls.CurveP384,
37+
"p521": tls.CurveP521,
38+
"x25519mlkem768": tls.X25519MLKEM768,
39+
"secp256r1mlkem768": tls.SecP256r1MLKEM768,
40+
"secp384r1mlkem1024": tls.SecP384r1MLKEM1024,
4141
}
4242

4343
func toCurvePreferences(preferences []string) []tls.CurveID {
@@ -99,35 +99,6 @@ func toTLSCiphers(cipherStrings []string) []uint16 {
9999
return ciphers
100100
}
101101

102-
// defaultCiphers uses hardware support to check if AES is specifically
103-
// supported by the CPU.
104-
//
105-
// If AES is supported AES ciphers will be preferred over ChaCha based ciphers
106-
// (This code is directly inspired by the certmagic code.)
107-
func defaultCiphers() []uint16 {
108-
if cpuid.CPU.Supports(cpuid.AESNI) {
109-
return defaultCiphersAESfirst
110-
}
111-
return defaultCiphersChaChaFirst
112-
}
113-
114-
var (
115-
defaultCiphersAES = []uint16{
116-
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
117-
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
118-
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
119-
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
120-
}
121-
122-
defaultCiphersChaCha = []uint16{
123-
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
124-
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
125-
}
126-
127-
defaultCiphersAESfirst = append(defaultCiphersAES, defaultCiphersChaCha...)
128-
defaultCiphersChaChaFirst = append(defaultCiphersChaCha, defaultCiphersAES...)
129-
)
130-
131102
// runHTTPS listens on the provided network address and then calls
132103
// Serve to handle requests on incoming TLS connections.
133104
//
@@ -148,17 +119,10 @@ func runHTTPS(network, listenAddr, name, certFile, keyFile string, m http.Handle
148119
tlsConfig.MaxVersion = version
149120
}
150121

151-
// Set curve preferences
152-
tlsConfig.CurvePreferences = []tls.CurveID{
153-
tls.X25519,
154-
tls.CurveP256,
155-
}
156122
if curves := toCurvePreferences(setting.SSLCurvePreferences); len(curves) > 0 {
157123
tlsConfig.CurvePreferences = curves
158124
}
159125

160-
// Set cipher suites
161-
tlsConfig.CipherSuites = defaultCiphers()
162126
if ciphers := toTLSCiphers(setting.SSLCipherSuites); len(ciphers) > 0 {
163127
tlsConfig.CipherSuites = ciphers
164128
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ require (
7171
github.com/json-iterator/go v1.1.12
7272
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
7373
github.com/klauspost/compress v1.19.1
74-
github.com/klauspost/cpuid/v2 v2.3.0
7574
github.com/markbates/goth v1.82.0
7675
github.com/mattn/go-isatty v0.0.24
7776
github.com/mattn/go-sqlite3 v1.14.48
@@ -196,6 +195,7 @@ require (
196195
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
197196
github.com/jackc/puddle/v2 v2.2.2 // indirect
198197
github.com/josharian/intern v1.0.0 // indirect
198+
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
199199
github.com/klauspost/crc32 v1.3.0 // indirect
200200
github.com/klauspost/pgzip v1.2.6 // indirect
201201
github.com/libdns/libdns v1.1.1 // indirect

0 commit comments

Comments
 (0)