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