You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

415 lines
15 KiB

  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 main
  5. import (
  6. "crypto/aes"
  7. "crypto/cipher"
  8. "crypto/des"
  9. "crypto/hmac"
  10. "crypto/md5"
  11. "crypto/rc4"
  12. "crypto/sha1"
  13. "crypto/sha256"
  14. "crypto/sha512"
  15. "crypto/x509"
  16. "hash"
  17. )
  18. // a keyAgreement implements the client and server side of a TLS key agreement
  19. // protocol by generating and processing key exchange messages.
  20. type keyAgreement interface {
  21. // On the server side, the first two methods are called in order.
  22. // In the case that the key agreement protocol doesn't use a
  23. // ServerKeyExchange message, generateServerKeyExchange can return nil,
  24. // nil.
  25. generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
  26. processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
  27. // On the client side, the next two methods are called in order.
  28. // This method may not be called if the server doesn't send a
  29. // ServerKeyExchange message.
  30. processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
  31. generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
  32. }
  33. const (
  34. // suiteECDH indicates that the cipher suite involves elliptic curve
  35. // Diffie-Hellman. This means that it should only be selected when the
  36. // client indicates that it supports ECC with a curve and point format
  37. // that we're happy with.
  38. suiteECDHE = 1 << iota
  39. // suiteECDSA indicates that the cipher suite involves an ECDSA
  40. // signature and therefore may only be selected when the server's
  41. // certificate is ECDSA. If this is not set then the cipher suite is
  42. // RSA based.
  43. suiteECDSA
  44. // suiteTLS12 indicates that the cipher suite should only be advertised
  45. // and accepted when using TLS 1.2.
  46. suiteTLS12
  47. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  48. // handshake hash.
  49. suiteSHA384
  50. // suiteNoDTLS indicates that the cipher suite cannot be used
  51. // in DTLS.
  52. suiteNoDTLS
  53. // suitePSK indicates that the cipher suite authenticates with
  54. // a pre-shared key rather than a server private key.
  55. suitePSK
  56. )
  57. type tlsAead struct {
  58. cipher.AEAD
  59. explicitNonce bool
  60. }
  61. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  62. // function. All cipher suites currently assume RSA key agreement.
  63. type cipherSuite struct {
  64. id uint16
  65. // the lengths, in bytes, of the key material needed for each component.
  66. keyLen int
  67. macLen int
  68. ivLen int
  69. ka func(version uint16) keyAgreement
  70. // flags is a bitmask of the suite* values, above.
  71. flags int
  72. cipher func(key, iv []byte, isRead bool) interface{}
  73. mac func(version uint16, macKey []byte) macFunction
  74. aead func(key, fixedNonce []byte) *tlsAead
  75. }
  76. var cipherSuites = []*cipherSuite{
  77. // Ciphersuite order is chosen so that ECDHE comes before plain RSA
  78. // and RC4 comes before AES (because of the Lucky13 attack).
  79. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadCHACHA20POLY1305},
  80. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 0, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadCHACHA20POLY1305},
  81. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  82. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
  83. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  84. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  85. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteNoDTLS, cipherRC4, macSHA1, nil},
  86. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteNoDTLS, cipherRC4, macSHA1, nil},
  87. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
  88. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, cipherAES, macSHA256, nil},
  89. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  90. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  91. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil},
  92. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil},
  93. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  94. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  95. {TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 32, 0, 0, dheRSAKA, suiteTLS12, nil, nil, aeadCHACHA20POLY1305},
  96. {TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, dheRSAKA, suiteTLS12, nil, nil, aeadAESGCM},
  97. {TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, dheRSAKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  98. {TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, dheRSAKA, suiteTLS12, cipherAES, macSHA256, nil},
  99. {TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, dheRSAKA, suiteTLS12, cipherAES, macSHA256, nil},
  100. {TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
  101. {TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
  102. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  103. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  104. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteNoDTLS, cipherRC4, macSHA1, nil},
  105. {TLS_RSA_WITH_RC4_128_MD5, 16, 16, 0, rsaKA, suiteNoDTLS, cipherRC4, macMD5, nil},
  106. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
  107. {TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
  108. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  109. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  110. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  111. {TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, dheRSAKA, 0, cipher3DES, macSHA1, nil},
  112. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  113. {TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdhePSKKA, suiteECDHE | suiteTLS12 | suitePSK, nil, nil, aeadAESGCM},
  114. {TLS_PSK_WITH_RC4_128_SHA, 16, 20, 0, pskKA, suiteNoDTLS | suitePSK, cipherRC4, macSHA1, nil},
  115. {TLS_PSK_WITH_AES_128_CBC_SHA, 16, 20, 16, pskKA, suitePSK, cipherAES, macSHA1, nil},
  116. {TLS_PSK_WITH_AES_256_CBC_SHA, 32, 20, 16, pskKA, suitePSK, cipherAES, macSHA1, nil},
  117. }
  118. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  119. cipher, _ := rc4.NewCipher(key)
  120. return cipher
  121. }
  122. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  123. block, _ := des.NewTripleDESCipher(key)
  124. if isRead {
  125. return cipher.NewCBCDecrypter(block, iv)
  126. }
  127. return cipher.NewCBCEncrypter(block, iv)
  128. }
  129. func cipherAES(key, iv []byte, isRead bool) interface{} {
  130. block, _ := aes.NewCipher(key)
  131. if isRead {
  132. return cipher.NewCBCDecrypter(block, iv)
  133. }
  134. return cipher.NewCBCEncrypter(block, iv)
  135. }
  136. // macSHA1 returns a macFunction for the given protocol version.
  137. func macSHA1(version uint16, key []byte) macFunction {
  138. if version == VersionSSL30 {
  139. mac := ssl30MAC{
  140. h: sha1.New(),
  141. key: make([]byte, len(key)),
  142. }
  143. copy(mac.key, key)
  144. return mac
  145. }
  146. return tls10MAC{hmac.New(sha1.New, key)}
  147. }
  148. func macMD5(version uint16, key []byte) macFunction {
  149. if version == VersionSSL30 {
  150. mac := ssl30MAC{
  151. h: md5.New(),
  152. key: make([]byte, len(key)),
  153. }
  154. copy(mac.key, key)
  155. return mac
  156. }
  157. return tls10MAC{hmac.New(md5.New, key)}
  158. }
  159. func macSHA256(version uint16, key []byte) macFunction {
  160. if version == VersionSSL30 {
  161. mac := ssl30MAC{
  162. h: sha256.New(),
  163. key: make([]byte, len(key)),
  164. }
  165. copy(mac.key, key)
  166. return mac
  167. }
  168. return tls10MAC{hmac.New(sha256.New, key)}
  169. }
  170. func macSHA384(version uint16, key []byte) macFunction {
  171. if version == VersionSSL30 {
  172. mac := ssl30MAC{
  173. h: sha512.New384(),
  174. key: make([]byte, len(key)),
  175. }
  176. copy(mac.key, key)
  177. return mac
  178. }
  179. return tls10MAC{hmac.New(sha512.New384, key)}
  180. }
  181. type macFunction interface {
  182. Size() int
  183. MAC(digestBuf, seq, header, length, data []byte) []byte
  184. }
  185. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  186. // each call.
  187. type fixedNonceAEAD struct {
  188. // sealNonce and openNonce are buffers where the larger nonce will be
  189. // constructed. Since a seal and open operation may be running
  190. // concurrently, there is a separate buffer for each.
  191. sealNonce, openNonce []byte
  192. aead cipher.AEAD
  193. }
  194. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  195. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  196. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  197. copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
  198. return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
  199. }
  200. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  201. copy(f.openNonce[len(f.openNonce)-8:], nonce)
  202. return f.aead.Open(out, f.openNonce, plaintext, additionalData)
  203. }
  204. func aeadAESGCM(key, fixedNonce []byte) *tlsAead {
  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. nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
  214. copy(nonce1, fixedNonce)
  215. copy(nonce2, fixedNonce)
  216. return &tlsAead{&fixedNonceAEAD{nonce1, nonce2, aead}, true}
  217. }
  218. func aeadCHACHA20POLY1305(key, fixedNonce []byte) *tlsAead {
  219. aead, err := newChaCha20Poly1305(key)
  220. if err != nil {
  221. panic(err)
  222. }
  223. return &tlsAead{aead, false}
  224. }
  225. // ssl30MAC implements the SSLv3 MAC function, as defined in
  226. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  227. type ssl30MAC struct {
  228. h hash.Hash
  229. key []byte
  230. }
  231. func (s ssl30MAC) Size() int {
  232. return s.h.Size()
  233. }
  234. 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}
  235. 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}
  236. func (s ssl30MAC) MAC(digestBuf, seq, header, length, data []byte) []byte {
  237. padLength := 48
  238. if s.h.Size() == 20 {
  239. padLength = 40
  240. }
  241. s.h.Reset()
  242. s.h.Write(s.key)
  243. s.h.Write(ssl30Pad1[:padLength])
  244. s.h.Write(seq)
  245. s.h.Write(header[:1])
  246. s.h.Write(length)
  247. s.h.Write(data)
  248. digestBuf = s.h.Sum(digestBuf[:0])
  249. s.h.Reset()
  250. s.h.Write(s.key)
  251. s.h.Write(ssl30Pad2[:padLength])
  252. s.h.Write(digestBuf)
  253. return s.h.Sum(digestBuf[:0])
  254. }
  255. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  256. type tls10MAC struct {
  257. h hash.Hash
  258. }
  259. func (s tls10MAC) Size() int {
  260. return s.h.Size()
  261. }
  262. func (s tls10MAC) MAC(digestBuf, seq, header, length, data []byte) []byte {
  263. s.h.Reset()
  264. s.h.Write(seq)
  265. s.h.Write(header)
  266. s.h.Write(length)
  267. s.h.Write(data)
  268. return s.h.Sum(digestBuf[:0])
  269. }
  270. func rsaKA(version uint16) keyAgreement {
  271. return &rsaKeyAgreement{version: version}
  272. }
  273. func ecdheECDSAKA(version uint16) keyAgreement {
  274. return &ecdheKeyAgreement{
  275. auth: &signedKeyAgreement{
  276. sigType: signatureECDSA,
  277. version: version,
  278. },
  279. }
  280. }
  281. func ecdheRSAKA(version uint16) keyAgreement {
  282. return &ecdheKeyAgreement{
  283. auth: &signedKeyAgreement{
  284. sigType: signatureRSA,
  285. version: version,
  286. },
  287. }
  288. }
  289. func dheRSAKA(version uint16) keyAgreement {
  290. return &dheKeyAgreement{
  291. auth: &signedKeyAgreement{
  292. sigType: signatureRSA,
  293. version: version,
  294. },
  295. }
  296. }
  297. func pskKA(version uint16) keyAgreement {
  298. return &pskKeyAgreement{
  299. base: &nilKeyAgreement{},
  300. }
  301. }
  302. func ecdhePSKKA(version uint16) keyAgreement {
  303. return &pskKeyAgreement{
  304. base: &ecdheKeyAgreement{
  305. auth: &nilKeyAgreementAuthentication{},
  306. },
  307. }
  308. }
  309. // mutualCipherSuite returns a cipherSuite given a list of supported
  310. // ciphersuites and the id requested by the peer.
  311. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  312. for _, id := range have {
  313. if id == want {
  314. for _, suite := range cipherSuites {
  315. if suite.id == want {
  316. return suite
  317. }
  318. }
  319. return nil
  320. }
  321. }
  322. return nil
  323. }
  324. // A list of the possible cipher suite ids. Taken from
  325. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  326. const (
  327. TLS_RSA_WITH_RC4_128_MD5 uint16 = 0x0004
  328. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  329. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  330. TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x0016
  331. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  332. TLS_DHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0x0033
  333. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  334. TLS_DHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0039
  335. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  336. TLS_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x003d
  337. TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x0067
  338. TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x006b
  339. TLS_PSK_WITH_RC4_128_SHA uint16 = 0x008a
  340. TLS_PSK_WITH_AES_128_CBC_SHA uint16 = 0x008c
  341. TLS_PSK_WITH_AES_256_CBC_SHA uint16 = 0x008d
  342. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  343. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  344. TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009e
  345. TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009f
  346. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  347. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  348. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  349. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  350. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  351. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  352. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  353. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  354. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc024
  355. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  356. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc028
  357. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  358. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  359. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  360. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  361. fallbackSCSV uint16 = 0x5600
  362. )
  363. // Additional cipher suite IDs, not IANA-assigned.
  364. const (
  365. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 uint16 = 0xcafe
  366. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcc13
  367. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcc14
  368. TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcc15
  369. )