cipher_suites.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 qtls
  5. import (
  6. "crypto"
  7. "crypto/aes"
  8. "crypto/cipher"
  9. "crypto/des"
  10. "crypto/hmac"
  11. "crypto/rc4"
  12. "crypto/sha1"
  13. "crypto/sha256"
  14. "crypto/x509"
  15. "fmt"
  16. "hash"
  17. "golang.org/x/crypto/chacha20poly1305"
  18. )
  19. // CipherSuite is a TLS cipher suite. Note that most functions in this package
  20. // accept and expose cipher suite IDs instead of this type.
  21. type CipherSuite struct {
  22. ID uint16
  23. Name string
  24. // Supported versions is the list of TLS protocol versions that can
  25. // negotiate this cipher suite.
  26. SupportedVersions []uint16
  27. // Insecure is true if the cipher suite has known security issues
  28. // due to its primitives, design, or implementation.
  29. Insecure bool
  30. }
  31. var (
  32. supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
  33. supportedOnlyTLS12 = []uint16{VersionTLS12}
  34. supportedOnlyTLS13 = []uint16{VersionTLS13}
  35. )
  36. // CipherSuites returns a list of cipher suites currently implemented by this
  37. // package, excluding those with security issues, which are returned by
  38. // InsecureCipherSuites.
  39. //
  40. // The list is sorted by ID. Note that the default cipher suites selected by
  41. // this package might depend on logic that can't be captured by a static list.
  42. func CipherSuites() []*CipherSuite {
  43. return []*CipherSuite{
  44. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
  45. {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  46. {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  47. {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  48. {TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  49. {TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
  50. {TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
  51. {TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
  52. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  53. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  54. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
  55. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  56. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  57. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  58. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  59. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  60. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  61. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
  62. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
  63. }
  64. }
  65. // InsecureCipherSuites returns a list of cipher suites currently implemented by
  66. // this package and which have security issues.
  67. //
  68. // Most applications should not use the cipher suites in this list, and should
  69. // only use those returned by CipherSuites.
  70. func InsecureCipherSuites() []*CipherSuite {
  71. // RC4 suites are broken because RC4 is.
  72. // CBC-SHA256 suites have no Lucky13 countermeasures.
  73. return []*CipherSuite{
  74. {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  75. {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  76. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  77. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  78. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  79. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  80. }
  81. }
  82. // CipherSuiteName returns the standard name for the passed cipher suite ID
  83. // (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
  84. // of the ID value if the cipher suite is not implemented by this package.
  85. func CipherSuiteName(id uint16) string {
  86. for _, c := range CipherSuites() {
  87. if c.ID == id {
  88. return c.Name
  89. }
  90. }
  91. for _, c := range InsecureCipherSuites() {
  92. if c.ID == id {
  93. return c.Name
  94. }
  95. }
  96. return fmt.Sprintf("0x%04X", id)
  97. }
  98. // a keyAgreement implements the client and server side of a TLS key agreement
  99. // protocol by generating and processing key exchange messages.
  100. type keyAgreement interface {
  101. // On the server side, the first two methods are called in order.
  102. // In the case that the key agreement protocol doesn't use a
  103. // ServerKeyExchange message, generateServerKeyExchange can return nil,
  104. // nil.
  105. generateServerKeyExchange(*config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
  106. processClientKeyExchange(*config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
  107. // On the client side, the next two methods are called in order.
  108. // This method may not be called if the server doesn't send a
  109. // ServerKeyExchange message.
  110. processServerKeyExchange(*config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
  111. generateClientKeyExchange(*config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
  112. }
  113. const (
  114. // suiteECDHE indicates that the cipher suite involves elliptic curve
  115. // Diffie-Hellman. This means that it should only be selected when the
  116. // client indicates that it supports ECC with a curve and point format
  117. // that we're happy with.
  118. suiteECDHE = 1 << iota
  119. // suiteECSign indicates that the cipher suite involves an ECDSA or
  120. // EdDSA signature and therefore may only be selected when the server's
  121. // certificate is ECDSA or EdDSA. If this is not set then the cipher suite
  122. // is RSA based.
  123. suiteECSign
  124. // suiteTLS12 indicates that the cipher suite should only be advertised
  125. // and accepted when using TLS 1.2.
  126. suiteTLS12
  127. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  128. // handshake hash.
  129. suiteSHA384
  130. // suiteDefaultOff indicates that this cipher suite is not included by
  131. // default.
  132. suiteDefaultOff
  133. )
  134. // A cipherSuite is a specific combination of key agreement, cipher and MAC function.
  135. type cipherSuite struct {
  136. id uint16
  137. // the lengths, in bytes, of the key material needed for each component.
  138. keyLen int
  139. macLen int
  140. ivLen int
  141. ka func(version uint16) keyAgreement
  142. // flags is a bitmask of the suite* values, above.
  143. flags int
  144. cipher func(key, iv []byte, isRead bool) interface{}
  145. mac func(key []byte) hash.Hash
  146. aead func(key, fixedNonce []byte) aead
  147. }
  148. var cipherSuites = []*cipherSuite{
  149. // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
  150. // AEADs are the top preference.
  151. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  152. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  153. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  154. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
  155. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  156. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  157. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  158. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  159. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  160. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
  161. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  162. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
  163. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  164. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  165. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  166. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  167. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  168. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  169. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  170. // RC4-based cipher suites are disabled by default.
  171. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
  172. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
  173. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteDefaultOff, cipherRC4, macSHA1, nil},
  174. }
  175. // selectCipherSuite returns the first cipher suite from ids which is also in
  176. // supportedIDs and passes the ok filter.
  177. func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
  178. for _, id := range ids {
  179. candidate := cipherSuiteByID(id)
  180. if candidate == nil || !ok(candidate) {
  181. continue
  182. }
  183. for _, suppID := range supportedIDs {
  184. if id == suppID {
  185. return candidate
  186. }
  187. }
  188. }
  189. return nil
  190. }
  191. // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
  192. // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
  193. type cipherSuiteTLS13 struct {
  194. id uint16
  195. keyLen int
  196. aead func(key, fixedNonce []byte) aead
  197. hash crypto.Hash
  198. }
  199. type CipherSuiteTLS13 struct {
  200. ID uint16
  201. KeyLen int
  202. Hash crypto.Hash
  203. AEAD func(key, fixedNonce []byte) cipher.AEAD
  204. }
  205. func (c *CipherSuiteTLS13) IVLen() int {
  206. return aeadNonceLength
  207. }
  208. var cipherSuitesTLS13 = []*cipherSuiteTLS13{
  209. {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
  210. {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
  211. {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
  212. }
  213. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  214. cipher, _ := rc4.NewCipher(key)
  215. return cipher
  216. }
  217. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  218. block, _ := des.NewTripleDESCipher(key)
  219. if isRead {
  220. return cipher.NewCBCDecrypter(block, iv)
  221. }
  222. return cipher.NewCBCEncrypter(block, iv)
  223. }
  224. func cipherAES(key, iv []byte, isRead bool) interface{} {
  225. block, _ := aes.NewCipher(key)
  226. if isRead {
  227. return cipher.NewCBCDecrypter(block, iv)
  228. }
  229. return cipher.NewCBCEncrypter(block, iv)
  230. }
  231. // macSHA1 returns a SHA-1 based constant time MAC.
  232. func macSHA1(key []byte) hash.Hash {
  233. return hmac.New(newConstantTimeHash(sha1.New), key)
  234. }
  235. // macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
  236. // is currently only used in disabled-by-default cipher suites.
  237. func macSHA256(key []byte) hash.Hash {
  238. return hmac.New(sha256.New, key)
  239. }
  240. type aead interface {
  241. cipher.AEAD
  242. // explicitNonceLen returns the number of bytes of explicit nonce
  243. // included in each record. This is eight for older AEADs and
  244. // zero for modern ones.
  245. explicitNonceLen() int
  246. }
  247. const (
  248. aeadNonceLength = 12
  249. noncePrefixLength = 4
  250. )
  251. // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  252. // each call.
  253. type prefixNonceAEAD struct {
  254. // nonce contains the fixed part of the nonce in the first four bytes.
  255. nonce [aeadNonceLength]byte
  256. aead cipher.AEAD
  257. }
  258. func (f *prefixNonceAEAD) NonceSize() int { return aeadNonceLength - noncePrefixLength }
  259. func (f *prefixNonceAEAD) Overhead() int { return f.aead.Overhead() }
  260. func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
  261. func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  262. copy(f.nonce[4:], nonce)
  263. return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
  264. }
  265. func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  266. copy(f.nonce[4:], nonce)
  267. return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
  268. }
  269. // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
  270. // before each call.
  271. type xorNonceAEAD struct {
  272. nonceMask [aeadNonceLength]byte
  273. aead cipher.AEAD
  274. }
  275. func (f *xorNonceAEAD) NonceSize() int { return 8 } // 64-bit sequence number
  276. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  277. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  278. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  279. for i, b := range nonce {
  280. f.nonceMask[4+i] ^= b
  281. }
  282. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  283. for i, b := range nonce {
  284. f.nonceMask[4+i] ^= b
  285. }
  286. return result
  287. }
  288. func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  289. for i, b := range nonce {
  290. f.nonceMask[4+i] ^= b
  291. }
  292. result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
  293. for i, b := range nonce {
  294. f.nonceMask[4+i] ^= b
  295. }
  296. return result, err
  297. }
  298. func aeadAESGCM(key, noncePrefix []byte) aead {
  299. if len(noncePrefix) != noncePrefixLength {
  300. panic("tls: internal error: wrong nonce length")
  301. }
  302. aes, err := aes.NewCipher(key)
  303. if err != nil {
  304. panic(err)
  305. }
  306. aead, err := cipher.NewGCM(aes)
  307. if err != nil {
  308. panic(err)
  309. }
  310. ret := &prefixNonceAEAD{aead: aead}
  311. copy(ret.nonce[:], noncePrefix)
  312. return ret
  313. }
  314. // AEADAESGCMTLS13 creates a new AES-GCM AEAD for TLS 1.3
  315. func AEADAESGCMTLS13(key, fixedNonce []byte) cipher.AEAD {
  316. return aeadAESGCMTLS13(key, fixedNonce)
  317. }
  318. func aeadAESGCMTLS13(key, nonceMask []byte) aead {
  319. if len(nonceMask) != aeadNonceLength {
  320. panic("tls: internal error: wrong nonce length")
  321. }
  322. aes, err := aes.NewCipher(key)
  323. if err != nil {
  324. panic(err)
  325. }
  326. aead, err := cipher.NewGCM(aes)
  327. if err != nil {
  328. panic(err)
  329. }
  330. ret := &xorNonceAEAD{aead: aead}
  331. copy(ret.nonceMask[:], nonceMask)
  332. return ret
  333. }
  334. func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
  335. if len(nonceMask) != aeadNonceLength {
  336. panic("tls: internal error: wrong nonce length")
  337. }
  338. aead, err := chacha20poly1305.New(key)
  339. if err != nil {
  340. panic(err)
  341. }
  342. ret := &xorNonceAEAD{aead: aead}
  343. copy(ret.nonceMask[:], nonceMask)
  344. return ret
  345. }
  346. type constantTimeHash interface {
  347. hash.Hash
  348. ConstantTimeSum(b []byte) []byte
  349. }
  350. // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
  351. // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
  352. type cthWrapper struct {
  353. h constantTimeHash
  354. }
  355. func (c *cthWrapper) Size() int { return c.h.Size() }
  356. func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
  357. func (c *cthWrapper) Reset() { c.h.Reset() }
  358. func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
  359. func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
  360. func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
  361. return func() hash.Hash {
  362. return &cthWrapper{h().(constantTimeHash)}
  363. }
  364. }
  365. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
  366. func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
  367. h.Reset()
  368. h.Write(seq)
  369. h.Write(header)
  370. h.Write(data)
  371. res := h.Sum(out)
  372. if extra != nil {
  373. h.Write(extra)
  374. }
  375. return res
  376. }
  377. func rsaKA(version uint16) keyAgreement {
  378. return rsaKeyAgreement{}
  379. }
  380. func ecdheECDSAKA(version uint16) keyAgreement {
  381. return &ecdheKeyAgreement{
  382. isRSA: false,
  383. version: version,
  384. }
  385. }
  386. func ecdheRSAKA(version uint16) keyAgreement {
  387. return &ecdheKeyAgreement{
  388. isRSA: true,
  389. version: version,
  390. }
  391. }
  392. // mutualCipherSuite returns a cipherSuite given a list of supported
  393. // ciphersuites and the id requested by the peer.
  394. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  395. for _, id := range have {
  396. if id == want {
  397. return cipherSuiteByID(id)
  398. }
  399. }
  400. return nil
  401. }
  402. func cipherSuiteByID(id uint16) *cipherSuite {
  403. for _, cipherSuite := range cipherSuites {
  404. if cipherSuite.id == id {
  405. return cipherSuite
  406. }
  407. }
  408. return nil
  409. }
  410. func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
  411. for _, id := range have {
  412. if id == want {
  413. return cipherSuiteTLS13ByID(id)
  414. }
  415. }
  416. return nil
  417. }
  418. func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
  419. for _, cipherSuite := range cipherSuitesTLS13 {
  420. if cipherSuite.id == id {
  421. return cipherSuite
  422. }
  423. }
  424. return nil
  425. }
  426. // A list of cipher suite IDs that are, or have been, implemented by this
  427. // package.
  428. //
  429. // See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  430. const (
  431. // TLS 1.0 - 1.2 cipher suites.
  432. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  433. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  434. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  435. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  436. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  437. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  438. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  439. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  440. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  441. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  442. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  443. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  444. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  445. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  446. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  447. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  448. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  449. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  450. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  451. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  452. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8
  453. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
  454. // TLS 1.3 cipher suites.
  455. TLS_AES_128_GCM_SHA256 uint16 = 0x1301
  456. TLS_AES_256_GCM_SHA384 uint16 = 0x1302
  457. TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
  458. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  459. // that the client is doing version fallback. See RFC 7507.
  460. TLS_FALLBACK_SCSV uint16 = 0x5600
  461. // Legacy names for the corresponding cipher suites with the correct _SHA256
  462. // suffix, retained for backward compatibility.
  463. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  464. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  465. )