Skip to content

Commit b7843af

Browse files
committed
Make SSL cipher suite configurable
Add options for configuring SSL cipher suite. Fix #9691
1 parent 812a9da commit b7843af

File tree

9 files changed

+291
-53
lines changed

9 files changed

+291
-53
lines changed

cmd/web_graceful.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"crypto/tls"
98
"net"
109
"net/http"
1110
"net/http/fcgi"
@@ -20,14 +19,6 @@ func runHTTP(network, listenAddr, name string, m http.Handler) error {
2019
return graceful.HTTPListenAndServe(network, listenAddr, name, m)
2120
}
2221

23-
func runHTTPS(network, listenAddr, name, certFile, keyFile string, m http.Handler) error {
24-
return graceful.HTTPListenAndServeTLS(network, listenAddr, name, certFile, keyFile, m)
25-
}
26-
27-
func runHTTPSWithTLSConfig(network, listenAddr, name string, tlsConfig *tls.Config, m http.Handler) error {
28-
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m)
29-
}
30-
3122
// NoHTTPRedirector tells our cleanup routine that we will not be using a fallback http redirector
3223
func NoHTTPRedirector() {
3324
graceful.GetManager().InformCleanup()

cmd/web_https.go

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cmd
6+
7+
import (
8+
"crypto/tls"
9+
"net/http"
10+
"os"
11+
"strings"
12+
13+
"code.gitea.io/gitea/modules/graceful"
14+
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/setting"
16+
"github.com/klauspost/cpuid/v2"
17+
)
18+
19+
func toTLSVersion(version string) uint16 {
20+
version = strings.TrimSpace(strings.ToLower(version))
21+
switch version {
22+
case "tls10":
23+
return tls.VersionTLS10
24+
case "tls11":
25+
return tls.VersionTLS11
26+
case "tls12":
27+
return tls.VersionTLS12
28+
case "tls13":
29+
return tls.VersionTLS13
30+
default:
31+
log.Warn("Unknown tls version: %s", version)
32+
return 0
33+
}
34+
}
35+
36+
func toCurvePreferences(preferences []string) []tls.CurveID {
37+
ids := make([]tls.CurveID, 0, len(preferences))
38+
for _, pref := range preferences {
39+
pref = strings.TrimSpace(strings.ToLower(pref))
40+
var id tls.CurveID
41+
switch pref {
42+
case "x25519":
43+
id = tls.X25519
44+
case "p256":
45+
id = tls.CurveP256
46+
case "p384":
47+
id = tls.CurveP384
48+
case "p521":
49+
id = tls.CurveP521
50+
default:
51+
log.Warn("Unknown curve: %s", pref)
52+
}
53+
if id != 0 {
54+
ids = append(ids, id)
55+
}
56+
}
57+
return ids
58+
}
59+
60+
func toTLSCiphers(cipherStrings []string) []uint16 {
61+
ciphers := make([]uint16, 0, len(cipherStrings))
62+
for _, cipherString := range cipherStrings {
63+
cipherString = strings.TrimSpace(strings.ToLower(cipherString))
64+
var cipher uint16
65+
switch cipherString {
66+
case "rsa_with_rc4_128_sha":
67+
cipher = tls.TLS_RSA_WITH_RC4_128_SHA
68+
case "rsa_with_3des_ede_cbc_sha":
69+
cipher = tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA
70+
case "rsa_with_aes_128_cbc_sha":
71+
cipher = tls.TLS_RSA_WITH_AES_128_CBC_SHA
72+
case "rsa_with_aes_256_cbc_sha":
73+
cipher = tls.TLS_RSA_WITH_AES_256_CBC_SHA
74+
case "rsa_with_aes_128_cbc_sha256":
75+
cipher = tls.TLS_RSA_WITH_AES_128_CBC_SHA256
76+
case "rsa_with_aes_128_gcm_sha256":
77+
cipher = tls.TLS_RSA_WITH_AES_128_GCM_SHA256
78+
case "rsa_with_aes_256_gcm_sha384":
79+
cipher = tls.TLS_RSA_WITH_AES_256_GCM_SHA384
80+
case "ecdhe_ecdsa_with_rc4_128_sha":
81+
cipher = tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
82+
case "ecdhe_ecdsa_with_aes_128_cbc_sha":
83+
cipher = tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
84+
case "ecdhe_ecdsa_with_aes_256_cbc_sha":
85+
cipher = tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
86+
case "ecdhe_rsa_with_rc4_128_sha":
87+
cipher = tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA
88+
case "ecdhe_rsa_with_3des_ede_cbc_sha":
89+
cipher = tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
90+
case "ecdhe_rsa_with_aes_128_cbc_sha":
91+
cipher = tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
92+
case "ecdhe_rsa_with_aes_256_cbc_sha":
93+
cipher = tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
94+
case "ecdhe_ecdsa_with_aes_128_cbc_sha256":
95+
cipher = tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
96+
case "ecdhe_rsa_with_aes_128_cbc_sha256":
97+
cipher = tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
98+
case "ecdhe_rsa_with_aes_128_gcm_sha256":
99+
cipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
100+
case "ecdhe_ecdsa_with_aes_128_gcm_sha256":
101+
cipher = tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
102+
case "ecdhe_rsa_with_aes_256_gcm_sha384":
103+
cipher = tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
104+
case "ecdhe_ecdsa_with_aes_256_gcm_sha384":
105+
cipher = tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
106+
case "ecdhe_rsa_with_chacha20_poly1305_sha256":
107+
cipher = tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
108+
case "ecdhe_ecdsa_with_chacha20_poly1305_sha256":
109+
cipher = tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
110+
case "ecdhe_rsa_with_chacha20_poly1305":
111+
cipher = tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305
112+
case "ecdhe_ecdsa_with_chacha20_poly1305":
113+
cipher = tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305
114+
case "aes_128_gcm_sha256":
115+
cipher = tls.TLS_AES_128_GCM_SHA256
116+
case "aes_256_gcm_sha384":
117+
cipher = tls.TLS_AES_256_GCM_SHA384
118+
case "chacha20_poly1305_sha256":
119+
cipher = tls.TLS_CHACHA20_POLY1305_SHA256
120+
default:
121+
log.Warn("Unknown cipher: %s", cipherString)
122+
}
123+
if cipher != 0 {
124+
ciphers = append(ciphers, cipher)
125+
}
126+
}
127+
128+
return ciphers
129+
}
130+
131+
// defaultCiphers uses hardware support to check if AES is specifically
132+
// supported by the CPU.
133+
//
134+
// If it is AES ciphers will be preferred over ChaCha based ciphers
135+
func defaultCiphers() []uint16 {
136+
if cpuid.CPU.Supports(cpuid.AESNI) {
137+
return defaultCiphersAESfirst
138+
}
139+
return defaultCiphersChaChaFirst
140+
}
141+
142+
var (
143+
defaultCiphersAES = []uint16{
144+
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
145+
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
146+
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
147+
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
148+
}
149+
150+
defaultCiphersChaCha = []uint16{
151+
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
152+
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
153+
}
154+
155+
defaultCiphersAESfirst = append(defaultCiphersAES, defaultCiphersChaCha...)
156+
defaultCiphersChaChaFirst = append(defaultCiphersChaCha, defaultCiphersAES...)
157+
)
158+
159+
// runHTTPs listens on the provided network address and then calls
160+
// Serve to handle requests on incoming TLS connections.
161+
//
162+
// Filenames containing a certificate and matching private key for the server must
163+
// be provided. If the certificate is signed by a certificate authority, the
164+
// certFile should be the concatenation of the server's certificate followed by the
165+
// CA's certificate.
166+
func runHTTPS(network, listenAddr, name, certFile, keyFile string, m http.Handler) error {
167+
tlsConfig := &tls.Config{}
168+
if tlsConfig.NextProtos == nil {
169+
tlsConfig.NextProtos = []string{"h2", "http/1.1"}
170+
}
171+
172+
if version := toTLSVersion(setting.SSLMinimumVersion); version != 0 {
173+
tlsConfig.MinVersion = version
174+
}
175+
if version := toTLSVersion(setting.SSLMaximumVersion); version != 0 {
176+
tlsConfig.MaxVersion = version
177+
}
178+
179+
// Set curve preferences
180+
tlsConfig.CurvePreferences = []tls.CurveID{
181+
tls.X25519,
182+
tls.CurveP256,
183+
}
184+
if curves := toCurvePreferences(setting.SSLCurvePreferences); len(curves) > 0 {
185+
tlsConfig.CurvePreferences = curves
186+
}
187+
188+
// Set cipher suites
189+
tlsConfig.CipherSuites = defaultCiphers()
190+
if ciphers := toTLSCiphers(setting.SSLCipherSuites); len(ciphers) > 0 {
191+
tlsConfig.CipherSuites = ciphers
192+
}
193+
194+
tlsConfig.Certificates = make([]tls.Certificate, 1)
195+
196+
certPEMBlock, err := os.ReadFile(certFile)
197+
if err != nil {
198+
log.Error("Failed to load https cert file %s for %s:%s: %v", certFile, network, listenAddr, err)
199+
return err
200+
}
201+
202+
keyPEMBlock, err := os.ReadFile(keyFile)
203+
if err != nil {
204+
log.Error("Failed to load https key file %s for %s:%s: %v", keyFile, network, listenAddr, err)
205+
return err
206+
}
207+
208+
tlsConfig.Certificates[0], err = tls.X509KeyPair(certPEMBlock, keyPEMBlock)
209+
if err != nil {
210+
log.Error("Failed to create certificate from cert file %s and key file %s for %s:%s: %v", certFile, keyFile, network, listenAddr, err)
211+
return err
212+
}
213+
214+
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m)
215+
}
216+
217+
func runHTTPSWithTLSConfig(network, listenAddr, name string, tlsConfig *tls.Config, m http.Handler) error {
218+
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m)
219+
}

cmd/web_letsencrypt.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ func runLetsEncrypt(listenAddr, domain, directory, email string, m http.Handler)
5656
tlsConfig := magic.TLSConfig()
5757
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
5858

59+
if version := toTLSVersion(setting.SSLMinimumVersion); version != 0 {
60+
tlsConfig.MinVersion = version
61+
}
62+
if version := toTLSVersion(setting.SSLMaximumVersion); version != 0 {
63+
tlsConfig.MaxVersion = version
64+
}
65+
66+
// Set curve preferences
67+
if curves := toCurvePreferences(setting.SSLCurvePreferences); len(curves) > 0 {
68+
tlsConfig.CurvePreferences = curves
69+
}
70+
71+
// Set cipher suites
72+
if ciphers := toTLSCiphers(setting.SSLCipherSuites); len(ciphers) > 0 {
73+
tlsConfig.CipherSuites = ciphers
74+
}
75+
5976
if enableHTTPChallenge {
6077
go func() {
6178
log.Info("Running Let's Encrypt handler on %s", setting.HTTPAddr+":"+setting.PortToRedirect)

custom/conf/app.example.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ RUN_MODE = ; prod
5151
;REDIRECT_OTHER_PORT = false
5252
;PORT_TO_REDIRECT = 80
5353
;;
54+
;; Minimum and maximum supported TLS versions
55+
;SSL_MIN_VERSION=tls12
56+
;SSL_MAX_VERSION=
57+
;;
58+
;; SSL Curve Preferences
59+
;SSL_CURVE_PREFERENCES=X25519,P256
60+
;;
61+
;; SSL Cipher Suites
62+
;SSL_CIPHER_SUITES=; Will default to "ecdhe_ecdsa_with_aes_256_gcm_sha384,ecdhe_rsa_with_aes_256_gcm_sha384,ecdhe_ecdsa_with_aes_128_gcm_sha256,ecdhe_rsa_with_aes_128_gcm_sha256,ecdhe_ecdsa_with_chacha20_poly1305,ecdhe_rsa_with_chacha20_poly1305" if aes is supported by hardware, otherwise chacha will be first.
63+
;;
5464
;; Timeout for any write to the connection. (Set to 0 to disable all timeouts.)
5565
;PER_WRITE_TIMEOUT = 30s
5666
;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,42 @@ The following configuration set `Content-Type: application/vnd.android.package-a
310310

311311
- `REDIRECT_OTHER_PORT`: **false**: If true and `PROTOCOL` is https, allows redirecting http requests on `PORT_TO_REDIRECT` to the https port Gitea listens on.
312312
- `PORT_TO_REDIRECT`: **80**: Port for the http redirection service to listen on. Used when `REDIRECT_OTHER_PORT` is true.
313+
- `SSL_MIN_VERSION`: **tls12**: Set the minimum version of ssl support.
314+
- `SSL_MAX_VERSION`: **\<empty\>**: Set the maximum version of ssl support.
315+
- `SSL_CURVE_PREFERENCES`: **X25519,P256**: Set the prefered curves,
316+
- `SSL_CIPHER_SUITES`: **ecdhe_ecdsa_with_aes_256_gcm_sha384,ecdhe_rsa_with_aes_256_gcm_sha384,ecdhe_ecdsa_with_aes_128_gcm_sha256,ecdhe_rsa_with_aes_128_gcm_sha256,ecdhe_ecdsa_with_chacha20_poly1305,ecdhe_rsa_with_chacha20_poly1305**: Set the preferred cipher suites.
317+
- If there is not hardware support for AES suites by default the cha cha suites will be preferred over the AES suites
318+
- supported suites as of go 1.17 are:
319+
- TLS 1.0 - 1.2 cipher suites
320+
- "rsa_with_rc4_128_sha"
321+
- "rsa_with_3des_ede_cbc_sha"
322+
- "rsa_with_aes_128_cbc_sha"
323+
- "rsa_with_aes_256_cbc_sha"
324+
- "rsa_with_aes_128_cbc_sha256"
325+
- "rsa_with_aes_128_gcm_sha256"
326+
- "rsa_with_aes_256_gcm_sha384"
327+
- "ecdhe_ecdsa_with_rc4_128_sha"
328+
- "ecdhe_ecdsa_with_aes_128_cbc_sha"
329+
- "ecdhe_ecdsa_with_aes_256_cbc_sha"
330+
- "ecdhe_rsa_with_rc4_128_sha"
331+
- "ecdhe_rsa_with_3des_ede_cbc_sha"
332+
- "ecdhe_rsa_with_aes_128_cbc_sha"
333+
- "ecdhe_rsa_with_aes_256_cbc_sha"
334+
- "ecdhe_ecdsa_with_aes_128_cbc_sha256"
335+
- "ecdhe_rsa_with_aes_128_cbc_sha256"
336+
- "ecdhe_rsa_with_aes_128_gcm_sha256"
337+
- "ecdhe_ecdsa_with_aes_128_gcm_sha256"
338+
- "ecdhe_rsa_with_aes_256_gcm_sha384"
339+
- "ecdhe_ecdsa_with_aes_256_gcm_sha384"
340+
- "ecdhe_rsa_with_chacha20_poly1305_sha256"
341+
- "ecdhe_ecdsa_with_chacha20_poly1305_sha256"
342+
- TLS 1.3 cipher suites
343+
- "aes_128_gcm_sha256"
344+
- "aes_256_gcm_sha384"
345+
- "chacha20_poly1305_sha256"
346+
- Aliased names
347+
- "ecdhe_rsa_with_chacha20_poly1305" is an alias for "ecdhe_rsa_with_chacha20_poly1305_sha256"
348+
- "ecdhe_ecdsa_with_chacha20_poly1305" is alias for "ecdhe_ecdsa_with_chacha20_poly1305_sha256"
313349
- `ENABLE_LETSENCRYPT`: **false**: If enabled you must set `DOMAIN` to valid internet facing domain (ensure DNS is set and port 80 is accessible by letsencrypt validation server).
314350
By using Lets Encrypt **you must consent** to their [terms of service](https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf).
315351
- `LETSENCRYPT_ACCEPTTOS`: **false**: This is an explicit check that you accept the terms of service for Let's Encrypt.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ require (
7171
github.com/kevinburke/ssh_config v1.1.0 // indirect
7272
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
7373
github.com/klauspost/compress v1.13.1
74-
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
74+
github.com/klauspost/cpuid/v2 v2.0.9
7575
github.com/klauspost/pgzip v1.2.5 // indirect
7676
github.com/lafriks/xormstore v1.4.0
7777
github.com/lib/pq v1.10.2

modules/graceful/server.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -95,42 +95,6 @@ func (srv *Server) ListenAndServe(serve ServeFunction) error {
9595
return srv.Serve(serve)
9696
}
9797

98-
// ListenAndServeTLS listens on the provided network address and then calls
99-
// Serve to handle requests on incoming TLS connections.
100-
//
101-
// Filenames containing a certificate and matching private key for the server must
102-
// be provided. If the certificate is signed by a certificate authority, the
103-
// certFile should be the concatenation of the server's certificate followed by the
104-
// CA's certificate.
105-
func (srv *Server) ListenAndServeTLS(certFile, keyFile string, serve ServeFunction) error {
106-
config := &tls.Config{}
107-
if config.NextProtos == nil {
108-
config.NextProtos = []string{"h2", "http/1.1"}
109-
}
110-
111-
config.Certificates = make([]tls.Certificate, 1)
112-
113-
certPEMBlock, err := os.ReadFile(certFile)
114-
if err != nil {
115-
log.Error("Failed to load https cert file %s for %s:%s: %v", certFile, srv.network, srv.address, err)
116-
return err
117-
}
118-
119-
keyPEMBlock, err := os.ReadFile(keyFile)
120-
if err != nil {
121-
log.Error("Failed to load https key file %s for %s:%s: %v", keyFile, srv.network, srv.address, err)
122-
return err
123-
}
124-
125-
config.Certificates[0], err = tls.X509KeyPair(certPEMBlock, keyPEMBlock)
126-
if err != nil {
127-
log.Error("Failed to create certificate from cert file %s and key file %s for %s:%s: %v", certFile, keyFile, srv.network, srv.address, err)
128-
return err
129-
}
130-
131-
return srv.ListenAndServeTLSConfig(config, serve)
132-
}
133-
13498
// ListenAndServeTLSConfig listens on the provided network address and then calls
13599
// Serve to handle requests on incoming TLS connections.
136100
func (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFunction) error {

modules/graceful/server_http.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ func HTTPListenAndServe(network, address, name string, handler http.Handler) err
3333
return server.ListenAndServe(lHandler)
3434
}
3535

36-
// HTTPListenAndServeTLS listens on the provided network address and then calls Serve
37-
// to handle requests on incoming connections.
38-
func HTTPListenAndServeTLS(network, address, name, certFile, keyFile string, handler http.Handler) error {
39-
server, lHandler := newHTTPServer(network, address, name, handler)
40-
return server.ListenAndServeTLS(certFile, keyFile, lHandler)
41-
}
42-
4336
// HTTPListenAndServeTLSConfig listens on the provided network address and then calls Serve
4437
// to handle requests on incoming connections.
4538
func HTTPListenAndServeTLSConfig(network, address, name string, tlsConfig *tls.Config, handler http.Handler) error {

0 commit comments

Comments
 (0)