cipher_suites.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/aes"
  7. "crypto/cipher"
  8. "crypto/des"
  9. "crypto/hmac"
  10. "crypto/rc4"
  11. "crypto/sha1"
  12. "crypto/sha256"
  13. "crypto/x509"
  14. "hash"
  15. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tls/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, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
  25. processClientKeyExchange(*Config, *Certificate, *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, *x509.Certificate, *serverKeyExchangeMsg) error
  30. generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]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. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  47. // handshake hash.
  48. suiteSHA384
  49. // suiteDefaultOff indicates that this cipher suite is not included by
  50. // default.
  51. suiteDefaultOff
  52. )
  53. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  54. // function. All cipher suites currently assume RSA key agreement.
  55. type cipherSuite struct {
  56. id uint16
  57. // the lengths, in bytes, of the key material needed for each component.
  58. keyLen int
  59. macLen int
  60. ivLen int
  61. ka func(version uint16) keyAgreement
  62. // flags is a bitmask of the suite* values, above.
  63. flags int
  64. cipher func(key, iv []byte, isRead bool) interface{}
  65. mac func(version uint16, macKey []byte) macFunction
  66. aead func(key, fixedNonce []byte) cipher.AEAD
  67. }
  68. var cipherSuites = []*cipherSuite{
  69. // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
  70. // AEADs are the top preference.
  71. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  72. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  73. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  74. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
  75. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  76. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  77. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  78. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  79. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  80. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  81. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  82. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  83. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  84. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  85. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  86. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  87. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  88. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  89. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  90. // RC4-based cipher suites are disabled by default.
  91. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
  92. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
  93. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
  94. // [Psiphon]
  95. // TLS_..._CHACHA20_POLY1305_OLD are required for EmulateChrome.
  96. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD, 32, 0, 12, ecdheRSAKA, suiteDefaultOff | suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  97. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD, 32, 0, 12, ecdheECDSAKA, suiteDefaultOff | suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  98. }
  99. // [Psiphon]
  100. // The following are not stock golang cipher suites and must be ignored
  101. // when running automated tests against pre-recorded "testdata".
  102. var ignoreCipherSuites = []uint16{
  103. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
  104. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
  105. }
  106. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  107. cipher, _ := rc4.NewCipher(key)
  108. return cipher
  109. }
  110. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  111. block, _ := des.NewTripleDESCipher(key)
  112. if isRead {
  113. return cipher.NewCBCDecrypter(block, iv)
  114. }
  115. return cipher.NewCBCEncrypter(block, iv)
  116. }
  117. func cipherAES(key, iv []byte, isRead bool) interface{} {
  118. block, _ := aes.NewCipher(key)
  119. if isRead {
  120. return cipher.NewCBCDecrypter(block, iv)
  121. }
  122. return cipher.NewCBCEncrypter(block, iv)
  123. }
  124. // macSHA1 returns a macFunction for the given protocol version.
  125. func macSHA1(version uint16, key []byte) macFunction {
  126. if version == VersionSSL30 {
  127. mac := ssl30MAC{
  128. h: sha1.New(),
  129. key: make([]byte, len(key)),
  130. }
  131. copy(mac.key, key)
  132. return mac
  133. }
  134. return tls10MAC{hmac.New(newConstantTimeHash(sha1.New), key)}
  135. }
  136. // macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
  137. // so the given version is ignored.
  138. func macSHA256(version uint16, key []byte) macFunction {
  139. return tls10MAC{hmac.New(sha256.New, key)}
  140. }
  141. type macFunction interface {
  142. Size() int
  143. MAC(digestBuf, seq, header, data, extra []byte) []byte
  144. }
  145. type aead interface {
  146. cipher.AEAD
  147. // explicitIVLen returns the number of bytes used by the explicit nonce
  148. // that is included in the record. This is eight for older AEADs and
  149. // zero for modern ones.
  150. explicitNonceLen() int
  151. }
  152. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  153. // each call.
  154. type fixedNonceAEAD struct {
  155. // nonce contains the fixed part of the nonce in the first four bytes.
  156. nonce [12]byte
  157. aead cipher.AEAD
  158. }
  159. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  160. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  161. func (f *fixedNonceAEAD) explicitNonceLen() int { return 8 }
  162. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  163. copy(f.nonce[4:], nonce)
  164. return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
  165. }
  166. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  167. copy(f.nonce[4:], nonce)
  168. return f.aead.Open(out, f.nonce[:], plaintext, additionalData)
  169. }
  170. // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
  171. // before each call.
  172. type xorNonceAEAD struct {
  173. nonceMask [12]byte
  174. aead cipher.AEAD
  175. }
  176. func (f *xorNonceAEAD) NonceSize() int { return 8 }
  177. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  178. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  179. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  180. for i, b := range nonce {
  181. f.nonceMask[4+i] ^= b
  182. }
  183. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  184. for i, b := range nonce {
  185. f.nonceMask[4+i] ^= b
  186. }
  187. return result
  188. }
  189. func (f *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  190. for i, b := range nonce {
  191. f.nonceMask[4+i] ^= b
  192. }
  193. result, err := f.aead.Open(out, f.nonceMask[:], plaintext, additionalData)
  194. for i, b := range nonce {
  195. f.nonceMask[4+i] ^= b
  196. }
  197. return result, err
  198. }
  199. func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
  200. aes, err := aes.NewCipher(key)
  201. if err != nil {
  202. panic(err)
  203. }
  204. aead, err := cipher.NewGCM(aes)
  205. if err != nil {
  206. panic(err)
  207. }
  208. ret := &fixedNonceAEAD{aead: aead}
  209. copy(ret.nonce[:], fixedNonce)
  210. return ret
  211. }
  212. func aeadChaCha20Poly1305(key, fixedNonce []byte) cipher.AEAD {
  213. aead, err := chacha20poly1305.New(key)
  214. if err != nil {
  215. panic(err)
  216. }
  217. ret := &xorNonceAEAD{aead: aead}
  218. copy(ret.nonceMask[:], fixedNonce)
  219. return ret
  220. }
  221. // ssl30MAC implements the SSLv3 MAC function, as defined in
  222. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  223. type ssl30MAC struct {
  224. h hash.Hash
  225. key []byte
  226. }
  227. func (s ssl30MAC) Size() int {
  228. return s.h.Size()
  229. }
  230. 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}
  231. 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}
  232. // MAC does not offer constant timing guarantees for SSL v3.0, since it's deemed
  233. // useless considering the similar, protocol-level POODLE vulnerability.
  234. func (s ssl30MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
  235. padLength := 48
  236. if s.h.Size() == 20 {
  237. padLength = 40
  238. }
  239. s.h.Reset()
  240. s.h.Write(s.key)
  241. s.h.Write(ssl30Pad1[:padLength])
  242. s.h.Write(seq)
  243. s.h.Write(header[:1])
  244. s.h.Write(header[3:5])
  245. s.h.Write(data)
  246. digestBuf = s.h.Sum(digestBuf[:0])
  247. s.h.Reset()
  248. s.h.Write(s.key)
  249. s.h.Write(ssl30Pad2[:padLength])
  250. s.h.Write(digestBuf)
  251. return s.h.Sum(digestBuf[:0])
  252. }
  253. type constantTimeHash interface {
  254. hash.Hash
  255. ConstantTimeSum(b []byte) []byte
  256. }
  257. // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
  258. // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
  259. type cthWrapper struct {
  260. h constantTimeHash
  261. }
  262. func (c *cthWrapper) Size() int { return c.h.Size() }
  263. func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
  264. func (c *cthWrapper) Reset() { c.h.Reset() }
  265. func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
  266. func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
  267. func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
  268. return func() hash.Hash {
  269. return &cthWrapper{h().(constantTimeHash)}
  270. }
  271. }
  272. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  273. type tls10MAC struct {
  274. h hash.Hash
  275. }
  276. func (s tls10MAC) Size() int {
  277. return s.h.Size()
  278. }
  279. // MAC is guaranteed to take constant time, as long as
  280. // len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
  281. // the MAC, but is only provided to make the timing profile constant.
  282. func (s tls10MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
  283. s.h.Reset()
  284. s.h.Write(seq)
  285. s.h.Write(header)
  286. s.h.Write(data)
  287. res := s.h.Sum(digestBuf[:0])
  288. if extra != nil {
  289. s.h.Write(extra)
  290. }
  291. return res
  292. }
  293. func rsaKA(version uint16) keyAgreement {
  294. return rsaKeyAgreement{}
  295. }
  296. func ecdheECDSAKA(version uint16) keyAgreement {
  297. return &ecdheKeyAgreement{
  298. sigType: signatureECDSA,
  299. version: version,
  300. }
  301. }
  302. func ecdheRSAKA(version uint16) keyAgreement {
  303. return &ecdheKeyAgreement{
  304. sigType: signatureRSA,
  305. version: version,
  306. }
  307. }
  308. // mutualCipherSuite returns a cipherSuite given a list of supported
  309. // ciphersuites and the id requested by the peer.
  310. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  311. for _, id := range have {
  312. if id == want {
  313. for _, suite := range cipherSuites {
  314. if suite.id == want {
  315. return suite
  316. }
  317. }
  318. return nil
  319. }
  320. }
  321. return nil
  322. }
  323. // A list of cipher suite IDs that are, or have been, implemented by this
  324. // package.
  325. //
  326. // Taken from http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  327. const (
  328. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  329. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  330. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  331. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  332. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  333. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  334. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  335. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  336. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  337. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  338. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  339. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  340. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  341. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  342. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  343. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  344. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  345. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  346. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  347. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  348. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca8
  349. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca9
  350. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  351. // that the client is doing version fallback. See
  352. // https://tools.ietf.org/html/rfc7507.
  353. TLS_FALLBACK_SCSV uint16 = 0x5600
  354. // Psiphon suites for indistinguishable TLS.
  355. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD uint16 = 0xcc13
  356. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD uint16 = 0xcc14
  357. TLS_GREASE_0A0A uint16 = 0x1A1A
  358. TLS_GREASE_1A1A uint16 = 0x2A2A
  359. TLS_GREASE_2A2A uint16 = 0x3A3A
  360. TLS_GREASE_3A3A uint16 = 0x4A4A
  361. TLS_GREASE_4A4A uint16 = 0x5A5A
  362. TLS_GREASE_5A5A uint16 = 0x6A6A
  363. TLS_GREASE_6A6A uint16 = 0x7A7A
  364. TLS_GREASE_7A7A uint16 = 0x8A8A
  365. TLS_GREASE_8A8A uint16 = 0x9A9A
  366. TLS_GREASE_9A9A uint16 = 0xAAAA
  367. TLS_GREASE_AAAA uint16 = 0xBABA
  368. TLS_GREASE_BABA uint16 = 0xCACA
  369. TLS_GREASE_CACA uint16 = 0xDADA
  370. TLS_GREASE_DADA uint16 = 0xEAEA
  371. TLS_GREASE_EAEA uint16 = 0xFAFA
  372. TLS_GREASE_FAFA uint16 = 0x0A0A
  373. )