cipher_suites.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. "hash"
  15. "golang.org/x/crypto/chacha20poly1305"
  16. )
  17. // a keyAgreement implements the client and server side of a TLS key agreement
  18. // protocol by generating and processing key exchange messages.
  19. type keyAgreement interface {
  20. // On the server side, the first two methods are called in order.
  21. // In the case that the key agreement protocol doesn't use a
  22. // ServerKeyExchange message, generateServerKeyExchange can return nil,
  23. // nil.
  24. generateServerKeyExchange(*Config, crypto.PrivateKey, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
  25. processClientKeyExchange(*Config, crypto.PrivateKey, *clientKeyExchangeMsg, uint16) ([]byte, error)
  26. // On the client side, the next two methods are called in order.
  27. // This method may not be called if the server doesn't send a
  28. // ServerKeyExchange message.
  29. processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, crypto.PublicKey, *serverKeyExchangeMsg) error
  30. generateClientKeyExchange(*Config, *clientHelloMsg, crypto.PublicKey) ([]byte, *clientKeyExchangeMsg, error)
  31. }
  32. const (
  33. // suiteECDH indicates that the cipher suite involves elliptic curve
  34. // Diffie-Hellman. This means that it should only be selected when the
  35. // client indicates that it supports ECC with a curve and point format
  36. // that we're happy with.
  37. suiteECDHE = 1 << iota
  38. // suiteECDSA indicates that the cipher suite involves an ECDSA
  39. // signature and therefore may only be selected when the server's
  40. // certificate is ECDSA. If this is not set then the cipher suite is
  41. // RSA based.
  42. suiteECDSA
  43. // suiteTLS12 indicates that the cipher suite should only be advertised
  44. // and accepted when using TLS 1.2.
  45. suiteTLS12
  46. // suiteTLS13 indicates that the ones and only cipher suites to be
  47. // advertised and accepted when using TLS 1.3.
  48. suiteTLS13
  49. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  50. // handshake hash.
  51. suiteSHA384
  52. // suiteDefaultOff indicates that this cipher suite is not included by
  53. // default.
  54. suiteDefaultOff
  55. )
  56. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  57. // function.
  58. type cipherSuite struct {
  59. id uint16
  60. // the lengths, in bytes, of the key material needed for each component.
  61. keyLen int
  62. macLen int
  63. ivLen int
  64. ka func(version uint16) keyAgreement
  65. // flags is a bitmask of the suite* values, above.
  66. flags int
  67. cipher func(key, iv []byte, isRead bool) interface{}
  68. mac func(version uint16, macKey []byte) macFunction
  69. aead func(key, fixedNonce []byte) cipher.AEAD
  70. }
  71. type CipherSuite struct {
  72. cipherSuite
  73. }
  74. func (c *CipherSuite) Hash() crypto.Hash { return hashForSuite(&c.cipherSuite) }
  75. func (c *CipherSuite) KeyLen() int { return c.keyLen }
  76. func (c *CipherSuite) IVLen() int { return c.ivLen }
  77. func (c *CipherSuite) AEAD(key, fixedNonce []byte) cipher.AEAD { return c.aead(key, fixedNonce) }
  78. var cipherSuites = []*cipherSuite{
  79. // TLS 1.3 ciphersuites specify only the AEAD and the HKDF hash.
  80. {TLS_CHACHA20_POLY1305_SHA256, 32, 0, 12, nil, suiteTLS13, nil, nil, aeadChaCha20Poly1305},
  81. {TLS_AES_128_GCM_SHA256, 16, 0, 12, nil, suiteTLS13, nil, nil, aeadAESGCM13},
  82. {TLS_AES_256_GCM_SHA384, 32, 0, 12, nil, suiteTLS13 | suiteSHA384, nil, nil, aeadAESGCM13},
  83. // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
  84. // AEADs are the top preference.
  85. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  86. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  87. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM12},
  88. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM12},
  89. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM12},
  90. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM12},
  91. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  92. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  93. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  94. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  95. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  96. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  97. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM12},
  98. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM12},
  99. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  100. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  101. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  102. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  103. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  104. // RC4-based cipher suites are disabled by default.
  105. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
  106. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
  107. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
  108. }
  109. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  110. cipher, _ := rc4.NewCipher(key)
  111. return cipher
  112. }
  113. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  114. block, _ := des.NewTripleDESCipher(key)
  115. if isRead {
  116. return cipher.NewCBCDecrypter(block, iv)
  117. }
  118. return cipher.NewCBCEncrypter(block, iv)
  119. }
  120. func cipherAES(key, iv []byte, isRead bool) interface{} {
  121. block, _ := aes.NewCipher(key)
  122. if isRead {
  123. return cipher.NewCBCDecrypter(block, iv)
  124. }
  125. return cipher.NewCBCEncrypter(block, iv)
  126. }
  127. // macSHA1 returns a macFunction for the given protocol version.
  128. func macSHA1(version uint16, key []byte) macFunction {
  129. if version == VersionSSL30 {
  130. mac := ssl30MAC{
  131. h: sha1.New(),
  132. key: make([]byte, len(key)),
  133. }
  134. copy(mac.key, key)
  135. return mac
  136. }
  137. return tls10MAC{hmac.New(newConstantTimeHash(sha1.New), key)}
  138. }
  139. // macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
  140. // so the given version is ignored.
  141. func macSHA256(version uint16, key []byte) macFunction {
  142. return tls10MAC{hmac.New(sha256.New, key)}
  143. }
  144. type macFunction interface {
  145. Size() int
  146. MAC(digestBuf, seq, header, data, extra []byte) []byte
  147. }
  148. type aead interface {
  149. cipher.AEAD
  150. // explicitIVLen returns the number of bytes used by the explicit nonce
  151. // that is included in the record. This is eight for older AEADs and
  152. // zero for modern ones.
  153. explicitNonceLen() int
  154. }
  155. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  156. // each call.
  157. type fixedNonceAEAD struct {
  158. // nonce contains the fixed part of the nonce in the first four bytes.
  159. nonce [12]byte
  160. aead cipher.AEAD
  161. }
  162. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  163. // Overhead returns the maximum difference between the lengths of a
  164. // plaintext and its ciphertext.
  165. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  166. func (f *fixedNonceAEAD) explicitNonceLen() int { return 8 }
  167. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  168. copy(f.nonce[4:], nonce)
  169. return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
  170. }
  171. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  172. copy(f.nonce[4:], nonce)
  173. return f.aead.Open(out, f.nonce[:], plaintext, additionalData)
  174. }
  175. // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
  176. // before each call.
  177. type xorNonceAEAD struct {
  178. nonceMask [12]byte
  179. aead cipher.AEAD
  180. }
  181. func (f *xorNonceAEAD) NonceSize() int { return 8 }
  182. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  183. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  184. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  185. for i, b := range nonce {
  186. f.nonceMask[4+i] ^= b
  187. }
  188. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  189. for i, b := range nonce {
  190. f.nonceMask[4+i] ^= b
  191. }
  192. return result
  193. }
  194. func (f *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  195. for i, b := range nonce {
  196. f.nonceMask[4+i] ^= b
  197. }
  198. result, err := f.aead.Open(out, f.nonceMask[:], plaintext, additionalData)
  199. for i, b := range nonce {
  200. f.nonceMask[4+i] ^= b
  201. }
  202. return result, err
  203. }
  204. func aeadAESGCM12(key, fixedNonce []byte) cipher.AEAD {
  205. aes, err := aes.NewCipher(key)
  206. if err != nil {
  207. panic(err)
  208. }
  209. aead, err := cipher.NewGCM(aes)
  210. if err != nil {
  211. panic(err)
  212. }
  213. ret := &fixedNonceAEAD{aead: aead}
  214. copy(ret.nonce[:], fixedNonce)
  215. return ret
  216. }
  217. // AEADAESGCM13 creates a new AES-GCM AEAD for TLS 1.3
  218. func AEADAESGCM13(key, fixedNonce []byte) cipher.AEAD {
  219. return aeadAESGCM13(key, fixedNonce)
  220. }
  221. func aeadAESGCM13(key, fixedNonce []byte) cipher.AEAD {
  222. aes, err := aes.NewCipher(key)
  223. if err != nil {
  224. panic(err)
  225. }
  226. aead, err := cipher.NewGCM(aes)
  227. if err != nil {
  228. panic(err)
  229. }
  230. ret := &xorNonceAEAD{aead: aead}
  231. copy(ret.nonceMask[:], fixedNonce)
  232. return ret
  233. }
  234. func aeadChaCha20Poly1305(key, fixedNonce []byte) cipher.AEAD {
  235. aead, err := chacha20poly1305.New(key)
  236. if err != nil {
  237. panic(err)
  238. }
  239. ret := &xorNonceAEAD{aead: aead}
  240. copy(ret.nonceMask[:], fixedNonce)
  241. return ret
  242. }
  243. // ssl30MAC implements the SSLv3 MAC function, as defined in
  244. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  245. type ssl30MAC struct {
  246. h hash.Hash
  247. key []byte
  248. }
  249. func (s ssl30MAC) Size() int {
  250. return s.h.Size()
  251. }
  252. var ssl30Pad1 = [48]byte{0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36}
  253. var ssl30Pad2 = [48]byte{0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c}
  254. // MAC does not offer constant timing guarantees for SSL v3.0, since it's deemed
  255. // useless considering the similar, protocol-level POODLE vulnerability.
  256. func (s ssl30MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
  257. padLength := 48
  258. if s.h.Size() == 20 {
  259. padLength = 40
  260. }
  261. s.h.Reset()
  262. s.h.Write(s.key)
  263. s.h.Write(ssl30Pad1[:padLength])
  264. s.h.Write(seq)
  265. s.h.Write(header[:1])
  266. s.h.Write(header[3:5])
  267. s.h.Write(data)
  268. digestBuf = s.h.Sum(digestBuf[:0])
  269. s.h.Reset()
  270. s.h.Write(s.key)
  271. s.h.Write(ssl30Pad2[:padLength])
  272. s.h.Write(digestBuf)
  273. return s.h.Sum(digestBuf[:0])
  274. }
  275. type constantTimeHash interface {
  276. hash.Hash
  277. ConstantTimeSum(b []byte) []byte
  278. }
  279. // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
  280. // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
  281. type cthWrapper struct {
  282. h constantTimeHash
  283. }
  284. func (c *cthWrapper) Size() int { return c.h.Size() }
  285. func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
  286. func (c *cthWrapper) Reset() { c.h.Reset() }
  287. func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
  288. func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
  289. func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
  290. return func() hash.Hash {
  291. return &cthWrapper{h().(constantTimeHash)}
  292. }
  293. }
  294. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  295. type tls10MAC struct {
  296. h hash.Hash
  297. }
  298. func (s tls10MAC) Size() int {
  299. return s.h.Size()
  300. }
  301. // MAC is guaranteed to take constant time, as long as
  302. // len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
  303. // the MAC, but is only provided to make the timing profile constant.
  304. func (s tls10MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
  305. s.h.Reset()
  306. s.h.Write(seq)
  307. s.h.Write(header)
  308. s.h.Write(data)
  309. res := s.h.Sum(digestBuf[:0])
  310. if extra != nil {
  311. s.h.Write(extra)
  312. }
  313. return res
  314. }
  315. func rsaKA(version uint16) keyAgreement {
  316. return rsaKeyAgreement{}
  317. }
  318. func ecdheECDSAKA(version uint16) keyAgreement {
  319. return &ecdheKeyAgreement{
  320. isRSA: false,
  321. version: version,
  322. }
  323. }
  324. func ecdheRSAKA(version uint16) keyAgreement {
  325. return &ecdheKeyAgreement{
  326. isRSA: true,
  327. version: version,
  328. }
  329. }
  330. // mutualCipherSuite returns a cipherSuite given a list of supported
  331. // ciphersuites and the id requested by the peer.
  332. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  333. for _, id := range have {
  334. if id == want {
  335. for _, suite := range cipherSuites {
  336. if suite.id == want {
  337. return suite
  338. }
  339. }
  340. return nil
  341. }
  342. }
  343. return nil
  344. }
  345. // A list of cipher suite IDs that are, or have been, implemented by this
  346. // package.
  347. //
  348. // Taken from http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  349. const (
  350. // TLS 1.0 - 1.2 cipher suites.
  351. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  352. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  353. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  354. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  355. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  356. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  357. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  358. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  359. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  360. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  361. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  362. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  363. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  364. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  365. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  366. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  367. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  368. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  369. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  370. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  371. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca8
  372. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca9
  373. // TLS 1.3+ cipher suites.
  374. TLS_AES_128_GCM_SHA256 uint16 = 0x1301
  375. TLS_AES_256_GCM_SHA384 uint16 = 0x1302
  376. TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
  377. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  378. // that the client is doing version fallback. See
  379. // https://tools.ietf.org/html/rfc7507.
  380. TLS_FALLBACK_SCSV uint16 = 0x5600
  381. )