cipher_suites.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tls
  5. import (
  6. "crypto"
  7. "crypto/aes"
  8. "crypto/cipher"
  9. "crypto/des"
  10. "crypto/hmac"
  11. // "crypto/internal/boring"
  12. "crypto/rc4"
  13. "crypto/sha1"
  14. "crypto/sha256"
  15. "fmt"
  16. "hash"
  17. "runtime"
  18. "github.com/Psiphon-Labs/utls/internal/boring"
  19. "golang.org/x/sys/cpu"
  20. "golang.org/x/crypto/chacha20poly1305"
  21. )
  22. // CipherSuite is a TLS cipher suite. Note that most functions in this package
  23. // accept and expose cipher suite IDs instead of this type.
  24. type CipherSuite struct {
  25. ID uint16
  26. Name string
  27. // Supported versions is the list of TLS protocol versions that can
  28. // negotiate this cipher suite.
  29. SupportedVersions []uint16
  30. // Insecure is true if the cipher suite has known security issues
  31. // due to its primitives, design, or implementation.
  32. Insecure bool
  33. }
  34. var (
  35. supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
  36. supportedOnlyTLS12 = []uint16{VersionTLS12}
  37. supportedOnlyTLS13 = []uint16{VersionTLS13}
  38. )
  39. // CipherSuites returns a list of cipher suites currently implemented by this
  40. // package, excluding those with security issues, which are returned by
  41. // [InsecureCipherSuites].
  42. //
  43. // The list is sorted by ID. Note that the default cipher suites selected by
  44. // this package might depend on logic that can't be captured by a static list,
  45. // and might not match those returned by this function.
  46. func CipherSuites() []*CipherSuite {
  47. return []*CipherSuite{
  48. {TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
  49. {TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
  50. {TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
  51. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  52. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  53. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  54. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  55. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  56. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  57. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  58. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  59. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
  60. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
  61. }
  62. }
  63. // InsecureCipherSuites returns a list of cipher suites currently implemented by
  64. // this package and which have security issues.
  65. //
  66. // Most applications should not use the cipher suites in this list, and should
  67. // only use those returned by [CipherSuites].
  68. func InsecureCipherSuites() []*CipherSuite {
  69. // This list includes RC4, CBC_SHA256, and 3DES cipher suites. See
  70. // cipherSuitesPreferenceOrder for details.
  71. return []*CipherSuite{
  72. {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  73. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
  74. {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, true},
  75. {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, true},
  76. {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  77. {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, true},
  78. {TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, true},
  79. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  80. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  81. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
  82. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  83. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  84. }
  85. }
  86. // CipherSuiteName returns the standard name for the passed cipher suite ID
  87. // (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
  88. // of the ID value if the cipher suite is not implemented by this package.
  89. func CipherSuiteName(id uint16) string {
  90. for _, c := range CipherSuites() {
  91. if c.ID == id {
  92. return c.Name
  93. }
  94. }
  95. for _, c := range InsecureCipherSuites() {
  96. if c.ID == id {
  97. return c.Name
  98. }
  99. }
  100. return fmt.Sprintf("0x%04X", id)
  101. }
  102. const (
  103. // suiteECDHE indicates that the cipher suite involves elliptic curve
  104. // Diffie-Hellman. This means that it should only be selected when the
  105. // client indicates that it supports ECC with a curve and point format
  106. // that we're happy with.
  107. suiteECDHE = 1 << iota
  108. // suiteECSign indicates that the cipher suite involves an ECDSA or
  109. // EdDSA signature and therefore may only be selected when the server's
  110. // certificate is ECDSA or EdDSA. If this is not set then the cipher suite
  111. // is RSA based.
  112. suiteECSign
  113. // suiteTLS12 indicates that the cipher suite should only be advertised
  114. // and accepted when using TLS 1.2.
  115. suiteTLS12
  116. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  117. // handshake hash.
  118. suiteSHA384
  119. )
  120. // A cipherSuite is a TLS 1.0–1.2 cipher suite, and defines the key exchange
  121. // mechanism, as well as the cipher+MAC pair or the AEAD.
  122. type cipherSuite struct {
  123. id uint16
  124. // the lengths, in bytes, of the key material needed for each component.
  125. keyLen int
  126. macLen int
  127. ivLen int
  128. ka func(version uint16) keyAgreement
  129. // flags is a bitmask of the suite* values, above.
  130. flags int
  131. cipher func(key, iv []byte, isRead bool) any
  132. mac func(key []byte) hash.Hash
  133. aead func(key, fixedNonce []byte) aead
  134. }
  135. var cipherSuites = []*cipherSuite{ // TODO: replace with a map, since the order doesn't matter.
  136. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  137. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  138. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  139. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
  140. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  141. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  142. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
  143. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  144. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, cipherAES, macSHA256, nil},
  145. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
  146. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  147. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
  148. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  149. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  150. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
  151. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  152. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  153. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  154. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  155. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
  156. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
  157. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherRC4, macSHA1, nil},
  158. }
  159. // selectCipherSuite returns the first TLS 1.0–1.2 cipher suite from ids which
  160. // is also in supportedIDs and passes the ok filter.
  161. func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
  162. for _, id := range ids {
  163. candidate := cipherSuiteByID(id)
  164. if candidate == nil || !ok(candidate) {
  165. continue
  166. }
  167. for _, suppID := range supportedIDs {
  168. if id == suppID {
  169. return candidate
  170. }
  171. }
  172. }
  173. return nil
  174. }
  175. // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
  176. // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
  177. type cipherSuiteTLS13 struct {
  178. id uint16
  179. keyLen int
  180. aead func(key, fixedNonce []byte) aead
  181. hash crypto.Hash
  182. }
  183. var cipherSuitesTLS13 = []*cipherSuiteTLS13{ // TODO: replace with a map.
  184. {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
  185. {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
  186. {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
  187. }
  188. // cipherSuitesPreferenceOrder is the order in which we'll select (on the
  189. // server) or advertise (on the client) TLS 1.0–1.2 cipher suites.
  190. //
  191. // Cipher suites are filtered but not reordered based on the application and
  192. // peer's preferences, meaning we'll never select a suite lower in this list if
  193. // any higher one is available. This makes it more defensible to keep weaker
  194. // cipher suites enabled, especially on the server side where we get the last
  195. // word, since there are no known downgrade attacks on cipher suites selection.
  196. //
  197. // The list is sorted by applying the following priority rules, stopping at the
  198. // first (most important) applicable one:
  199. //
  200. // - Anything else comes before RC4
  201. //
  202. // RC4 has practically exploitable biases. See https://www.rc4nomore.com.
  203. //
  204. // - Anything else comes before CBC_SHA256
  205. //
  206. // SHA-256 variants of the CBC ciphersuites don't implement any Lucky13
  207. // countermeasures. See http://www.isg.rhul.ac.uk/tls/Lucky13.html and
  208. // https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
  209. //
  210. // - Anything else comes before 3DES
  211. //
  212. // 3DES has 64-bit blocks, which makes it fundamentally susceptible to
  213. // birthday attacks. See https://sweet32.info.
  214. //
  215. // - ECDHE comes before anything else
  216. //
  217. // Once we got the broken stuff out of the way, the most important
  218. // property a cipher suite can have is forward secrecy. We don't
  219. // implement FFDHE, so that means ECDHE.
  220. //
  221. // - AEADs come before CBC ciphers
  222. //
  223. // Even with Lucky13 countermeasures, MAC-then-Encrypt CBC cipher suites
  224. // are fundamentally fragile, and suffered from an endless sequence of
  225. // padding oracle attacks. See https://eprint.iacr.org/2015/1129,
  226. // https://www.imperialviolet.org/2014/12/08/poodleagain.html, and
  227. // https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/.
  228. //
  229. // - AES comes before ChaCha20
  230. //
  231. // When AES hardware is available, AES-128-GCM and AES-256-GCM are faster
  232. // than ChaCha20Poly1305.
  233. //
  234. // When AES hardware is not available, AES-128-GCM is one or more of: much
  235. // slower, way more complex, and less safe (because not constant time)
  236. // than ChaCha20Poly1305.
  237. //
  238. // We use this list if we think both peers have AES hardware, and
  239. // cipherSuitesPreferenceOrderNoAES otherwise.
  240. //
  241. // - AES-128 comes before AES-256
  242. //
  243. // The only potential advantages of AES-256 are better multi-target
  244. // margins, and hypothetical post-quantum properties. Neither apply to
  245. // TLS, and AES-256 is slower due to its four extra rounds (which don't
  246. // contribute to the advantages above).
  247. //
  248. // - ECDSA comes before RSA
  249. //
  250. // The relative order of ECDSA and RSA cipher suites doesn't matter,
  251. // as they depend on the certificate. Pick one to get a stable order.
  252. var cipherSuitesPreferenceOrder = []uint16{
  253. // AEADs w/ ECDHE
  254. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  255. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  256. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  257. // CBC w/ ECDHE
  258. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  259. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  260. // AEADs w/o ECDHE
  261. TLS_RSA_WITH_AES_128_GCM_SHA256,
  262. TLS_RSA_WITH_AES_256_GCM_SHA384,
  263. // CBC w/o ECDHE
  264. TLS_RSA_WITH_AES_128_CBC_SHA,
  265. TLS_RSA_WITH_AES_256_CBC_SHA,
  266. // 3DES
  267. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  268. TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  269. // CBC_SHA256
  270. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  271. TLS_RSA_WITH_AES_128_CBC_SHA256,
  272. // RC4
  273. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  274. TLS_RSA_WITH_RC4_128_SHA,
  275. }
  276. var cipherSuitesPreferenceOrderNoAES = []uint16{
  277. // ChaCha20Poly1305
  278. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  279. // AES-GCM w/ ECDHE
  280. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  281. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  282. // The rest of cipherSuitesPreferenceOrder.
  283. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  284. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  285. TLS_RSA_WITH_AES_128_GCM_SHA256,
  286. TLS_RSA_WITH_AES_256_GCM_SHA384,
  287. TLS_RSA_WITH_AES_128_CBC_SHA,
  288. TLS_RSA_WITH_AES_256_CBC_SHA,
  289. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  290. TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  291. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  292. TLS_RSA_WITH_AES_128_CBC_SHA256,
  293. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  294. TLS_RSA_WITH_RC4_128_SHA,
  295. }
  296. // disabledCipherSuites are not used unless explicitly listed in Config.CipherSuites.
  297. var disabledCipherSuites = map[uint16]bool{
  298. // CBC_SHA256
  299. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: true,
  300. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: true,
  301. TLS_RSA_WITH_AES_128_CBC_SHA256: true,
  302. // RC4
  303. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA: true,
  304. TLS_ECDHE_RSA_WITH_RC4_128_SHA: true,
  305. TLS_RSA_WITH_RC4_128_SHA: true,
  306. }
  307. // rsaKexCiphers contains the ciphers which use RSA based key exchange,
  308. // which we also disable by default unless a GODEBUG is set.
  309. var rsaKexCiphers = map[uint16]bool{
  310. TLS_RSA_WITH_RC4_128_SHA: true,
  311. TLS_RSA_WITH_3DES_EDE_CBC_SHA: true,
  312. TLS_RSA_WITH_AES_128_CBC_SHA: true,
  313. TLS_RSA_WITH_AES_256_CBC_SHA: true,
  314. TLS_RSA_WITH_AES_128_CBC_SHA256: true,
  315. TLS_RSA_WITH_AES_128_GCM_SHA256: true,
  316. TLS_RSA_WITH_AES_256_GCM_SHA384: true,
  317. }
  318. var defaultCipherSuites []uint16
  319. var defaultCipherSuitesWithRSAKex []uint16
  320. func init() {
  321. defaultCipherSuites = make([]uint16, 0, len(cipherSuitesPreferenceOrder))
  322. defaultCipherSuitesWithRSAKex = make([]uint16, 0, len(cipherSuitesPreferenceOrder))
  323. for _, c := range cipherSuitesPreferenceOrder {
  324. if disabledCipherSuites[c] {
  325. continue
  326. }
  327. if !rsaKexCiphers[c] {
  328. defaultCipherSuites = append(defaultCipherSuites, c)
  329. }
  330. defaultCipherSuitesWithRSAKex = append(defaultCipherSuitesWithRSAKex, c)
  331. }
  332. }
  333. // defaultCipherSuitesTLS13 is also the preference order, since there are no
  334. // disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
  335. // cipherSuitesPreferenceOrder applies.
  336. var defaultCipherSuitesTLS13 = []uint16{
  337. TLS_AES_128_GCM_SHA256,
  338. TLS_AES_256_GCM_SHA384,
  339. TLS_CHACHA20_POLY1305_SHA256,
  340. }
  341. var defaultCipherSuitesTLS13NoAES = []uint16{
  342. TLS_CHACHA20_POLY1305_SHA256,
  343. TLS_AES_128_GCM_SHA256,
  344. TLS_AES_256_GCM_SHA384,
  345. }
  346. var (
  347. hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
  348. hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
  349. // Keep in sync with crypto/aes/cipher_s390x.go.
  350. hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
  351. (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
  352. hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
  353. runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
  354. runtime.GOARCH == "s390x" && hasGCMAsmS390X
  355. )
  356. var aesgcmCiphers = map[uint16]bool{
  357. // TLS 1.2
  358. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: true,
  359. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: true,
  360. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
  361. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
  362. // TLS 1.3
  363. TLS_AES_128_GCM_SHA256: true,
  364. TLS_AES_256_GCM_SHA384: true,
  365. }
  366. // aesgcmPreferred returns whether the first known cipher in the preference list
  367. // is an AES-GCM cipher, implying the peer has hardware support for it.
  368. func aesgcmPreferred(ciphers []uint16) bool {
  369. for _, cID := range ciphers {
  370. if c := cipherSuiteByID(cID); c != nil {
  371. return aesgcmCiphers[cID]
  372. }
  373. if c := cipherSuiteTLS13ByID(cID); c != nil {
  374. return aesgcmCiphers[cID]
  375. }
  376. }
  377. return false
  378. }
  379. func cipherRC4(key, iv []byte, isRead bool) any {
  380. cipher, _ := rc4.NewCipher(key)
  381. return cipher
  382. }
  383. func cipher3DES(key, iv []byte, isRead bool) any {
  384. block, _ := des.NewTripleDESCipher(key)
  385. if isRead {
  386. return cipher.NewCBCDecrypter(block, iv)
  387. }
  388. return cipher.NewCBCEncrypter(block, iv)
  389. }
  390. func cipherAES(key, iv []byte, isRead bool) any {
  391. block, _ := aes.NewCipher(key)
  392. if isRead {
  393. return cipher.NewCBCDecrypter(block, iv)
  394. }
  395. return cipher.NewCBCEncrypter(block, iv)
  396. }
  397. // macSHA1 returns a SHA-1 based constant time MAC.
  398. func macSHA1(key []byte) hash.Hash {
  399. h := sha1.New
  400. // The BoringCrypto SHA1 does not have a constant-time
  401. // checksum function, so don't try to use it.
  402. if !boring.Enabled {
  403. h = newConstantTimeHash(h)
  404. }
  405. return hmac.New(h, key)
  406. }
  407. // macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
  408. // is currently only used in disabled-by-default cipher suites.
  409. func macSHA256(key []byte) hash.Hash {
  410. return hmac.New(sha256.New, key)
  411. }
  412. type aead interface {
  413. cipher.AEAD
  414. // explicitNonceLen returns the number of bytes of explicit nonce
  415. // included in each record. This is eight for older AEADs and
  416. // zero for modern ones.
  417. explicitNonceLen() int
  418. }
  419. const (
  420. aeadNonceLength = 12
  421. noncePrefixLength = 4
  422. )
  423. // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  424. // each call.
  425. type prefixNonceAEAD struct {
  426. // nonce contains the fixed part of the nonce in the first four bytes.
  427. nonce [aeadNonceLength]byte
  428. aead cipher.AEAD
  429. }
  430. func (f *prefixNonceAEAD) NonceSize() int { return aeadNonceLength - noncePrefixLength }
  431. func (f *prefixNonceAEAD) Overhead() int { return f.aead.Overhead() }
  432. func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
  433. func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  434. copy(f.nonce[4:], nonce)
  435. return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
  436. }
  437. func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  438. copy(f.nonce[4:], nonce)
  439. return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
  440. }
  441. // xorNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
  442. // before each call.
  443. type xorNonceAEAD struct {
  444. nonceMask [aeadNonceLength]byte
  445. aead cipher.AEAD
  446. }
  447. func (f *xorNonceAEAD) NonceSize() int { return 8 } // 64-bit sequence number
  448. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  449. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  450. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  451. for i, b := range nonce {
  452. f.nonceMask[4+i] ^= b
  453. }
  454. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  455. for i, b := range nonce {
  456. f.nonceMask[4+i] ^= b
  457. }
  458. return result
  459. }
  460. func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  461. for i, b := range nonce {
  462. f.nonceMask[4+i] ^= b
  463. }
  464. result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
  465. for i, b := range nonce {
  466. f.nonceMask[4+i] ^= b
  467. }
  468. return result, err
  469. }
  470. func aeadAESGCM(key, noncePrefix []byte) aead {
  471. if len(noncePrefix) != noncePrefixLength {
  472. panic("tls: internal error: wrong nonce length")
  473. }
  474. aes, err := aes.NewCipher(key)
  475. if err != nil {
  476. panic(err)
  477. }
  478. var aead cipher.AEAD
  479. if boring.Enabled {
  480. aead, err = boring.NewGCMTLS(aes)
  481. } else {
  482. boring.Unreachable()
  483. aead, err = cipher.NewGCM(aes)
  484. }
  485. if err != nil {
  486. panic(err)
  487. }
  488. ret := &prefixNonceAEAD{aead: aead}
  489. copy(ret.nonce[:], noncePrefix)
  490. return ret
  491. }
  492. func aeadAESGCMTLS13(key, nonceMask []byte) aead {
  493. if len(nonceMask) != aeadNonceLength {
  494. panic("tls: internal error: wrong nonce length")
  495. }
  496. aes, err := aes.NewCipher(key)
  497. if err != nil {
  498. panic(err)
  499. }
  500. var aead cipher.AEAD
  501. if boring.Enabled {
  502. aead, err = boring.NewGCMTLS13(aes)
  503. } else {
  504. boring.Unreachable()
  505. aead, err = cipher.NewGCM(aes)
  506. }
  507. if err != nil {
  508. panic(err)
  509. }
  510. ret := &xorNonceAEAD{aead: aead}
  511. copy(ret.nonceMask[:], nonceMask)
  512. return ret
  513. }
  514. func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
  515. if len(nonceMask) != aeadNonceLength {
  516. panic("tls: internal error: wrong nonce length")
  517. }
  518. aead, err := chacha20poly1305.New(key)
  519. if err != nil {
  520. panic(err)
  521. }
  522. ret := &xorNonceAEAD{aead: aead}
  523. copy(ret.nonceMask[:], nonceMask)
  524. return ret
  525. }
  526. type constantTimeHash interface {
  527. hash.Hash
  528. ConstantTimeSum(b []byte) []byte
  529. }
  530. // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
  531. // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
  532. type cthWrapper struct {
  533. h constantTimeHash
  534. }
  535. func (c *cthWrapper) Size() int { return c.h.Size() }
  536. func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
  537. func (c *cthWrapper) Reset() { c.h.Reset() }
  538. func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
  539. func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
  540. func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
  541. boring.Unreachable()
  542. return func() hash.Hash {
  543. return &cthWrapper{h().(constantTimeHash)}
  544. }
  545. }
  546. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
  547. func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
  548. h.Reset()
  549. h.Write(seq)
  550. h.Write(header)
  551. h.Write(data)
  552. res := h.Sum(out)
  553. if extra != nil {
  554. h.Write(extra)
  555. }
  556. return res
  557. }
  558. func rsaKA(version uint16) keyAgreement {
  559. return rsaKeyAgreement{}
  560. }
  561. func ecdheECDSAKA(version uint16) keyAgreement {
  562. return &ecdheKeyAgreement{
  563. isRSA: false,
  564. version: version,
  565. }
  566. }
  567. func ecdheRSAKA(version uint16) keyAgreement {
  568. return &ecdheKeyAgreement{
  569. isRSA: true,
  570. version: version,
  571. }
  572. }
  573. // mutualCipherSuite returns a cipherSuite given a list of supported
  574. // ciphersuites and the id requested by the peer.
  575. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  576. for _, id := range have {
  577. if id == want {
  578. return cipherSuiteByID(id)
  579. }
  580. }
  581. return nil
  582. }
  583. func cipherSuiteByID(id uint16) *cipherSuite {
  584. for _, cipherSuite := range utlsSupportedCipherSuites {
  585. if cipherSuite.id == id {
  586. return cipherSuite
  587. }
  588. }
  589. return nil
  590. }
  591. func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
  592. for _, id := range have {
  593. if id == want {
  594. return cipherSuiteTLS13ByID(id)
  595. }
  596. }
  597. return nil
  598. }
  599. func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
  600. for _, cipherSuite := range cipherSuitesTLS13 {
  601. if cipherSuite.id == id {
  602. return cipherSuite
  603. }
  604. }
  605. return nil
  606. }
  607. // A list of cipher suite IDs that are, or have been, implemented by this
  608. // package.
  609. //
  610. // See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  611. const (
  612. // TLS 1.0 - 1.2 cipher suites.
  613. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  614. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  615. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  616. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  617. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  618. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  619. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  620. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  621. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  622. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  623. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  624. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  625. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  626. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  627. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  628. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  629. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  630. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  631. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  632. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  633. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8
  634. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
  635. // TLS 1.3 cipher suites.
  636. TLS_AES_128_GCM_SHA256 uint16 = 0x1301
  637. TLS_AES_256_GCM_SHA384 uint16 = 0x1302
  638. TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
  639. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  640. // that the client is doing version fallback. See RFC 7507.
  641. TLS_FALLBACK_SCSV uint16 = 0x5600
  642. // Legacy names for the corresponding cipher suites with the correct _SHA256
  643. // suffix, retained for backward compatibility.
  644. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  645. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  646. )