You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

web_https.go 7.0KB

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