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.
 
 
 
 
 
 

292 line
10 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 tls
  5. import (
  6. "crypto/aes"
  7. "crypto/cipher"
  8. "crypto/des"
  9. "crypto/hmac"
  10. "crypto/rc4"
  11. "crypto/sha1"
  12. "crypto/x509"
  13. "hash"
  14. )
  15. // a keyAgreement implements the client and server side of a TLS key agreement
  16. // protocol by generating and processing key exchange messages.
  17. type keyAgreement interface {
  18. // On the server side, the first two methods are called in order.
  19. // In the case that the key agreement protocol doesn't use a
  20. // ServerKeyExchange message, generateServerKeyExchange can return nil,
  21. // nil.
  22. generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
  23. processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
  24. // On the client side, the next two methods are called in order.
  25. // This method may not be called if the server doesn't send a
  26. // ServerKeyExchange message.
  27. processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
  28. generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
  29. }
  30. const (
  31. // suiteECDH indicates that the cipher suite involves elliptic curve
  32. // Diffie-Hellman. This means that it should only be selected when the
  33. // client indicates that it supports ECC with a curve and point format
  34. // that we're happy with.
  35. suiteECDHE = 1 << iota
  36. // suiteECDSA indicates that the cipher suite involves an ECDSA
  37. // signature and therefore may only be selected when the server's
  38. // certificate is ECDSA. If this is not set then the cipher suite is
  39. // RSA based.
  40. suiteECDSA
  41. // suiteTLS12 indicates that the cipher suite should only be advertised
  42. // and accepted when using TLS 1.2.
  43. suiteTLS12
  44. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  45. // handshake hash.
  46. suiteSHA384
  47. // suiteDefaultOff indicates that this cipher suite is not included by
  48. // default.
  49. suiteDefaultOff
  50. )
  51. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  52. // function. All cipher suites currently assume RSA key agreement.
  53. type cipherSuite struct {
  54. id uint16
  55. // the lengths, in bytes, of the key material needed for each component.
  56. keyLen int
  57. macLen int
  58. ivLen int
  59. ka func(version uint16) keyAgreement
  60. // flags is a bitmask of the suite* values, above.
  61. flags int
  62. cipher func(key, iv []byte, isRead bool) interface{}
  63. mac func(version uint16, macKey []byte) macFunction
  64. aead func(key, fixedNonce []byte) cipher.AEAD
  65. }
  66. var cipherSuites = []*cipherSuite{
  67. // Ciphersuite order is chosen so that ECDHE comes before plain RSA
  68. // and RC4 comes before AES-CBC (because of the Lucky13 attack).
  69. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  70. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
  71. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  72. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  73. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
  74. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
  75. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  76. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  77. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  78. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  79. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  80. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  81. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
  82. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  83. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  84. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  85. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  86. }
  87. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  88. cipher, _ := rc4.NewCipher(key)
  89. return cipher
  90. }
  91. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  92. block, _ := des.NewTripleDESCipher(key)
  93. if isRead {
  94. return cipher.NewCBCDecrypter(block, iv)
  95. }
  96. return cipher.NewCBCEncrypter(block, iv)
  97. }
  98. func cipherAES(key, iv []byte, isRead bool) interface{} {
  99. block, _ := aes.NewCipher(key)
  100. if isRead {
  101. return cipher.NewCBCDecrypter(block, iv)
  102. }
  103. return cipher.NewCBCEncrypter(block, iv)
  104. }
  105. // macSHA1 returns a macFunction for the given protocol version.
  106. func macSHA1(version uint16, key []byte) macFunction {
  107. if version == VersionSSL30 {
  108. mac := ssl30MAC{
  109. h: sha1.New(),
  110. key: make([]byte, len(key)),
  111. }
  112. copy(mac.key, key)
  113. return mac
  114. }
  115. return tls10MAC{hmac.New(sha1.New, key)}
  116. }
  117. type macFunction interface {
  118. Size() int
  119. MAC(digestBuf, seq, header, data []byte) []byte
  120. }
  121. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  122. // each call.
  123. type fixedNonceAEAD struct {
  124. // sealNonce and openNonce are buffers where the larger nonce will be
  125. // constructed. Since a seal and open operation may be running
  126. // concurrently, there is a separate buffer for each.
  127. sealNonce, openNonce []byte
  128. aead cipher.AEAD
  129. }
  130. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  131. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  132. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  133. copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
  134. return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
  135. }
  136. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  137. copy(f.openNonce[len(f.openNonce)-8:], nonce)
  138. return f.aead.Open(out, f.openNonce, plaintext, additionalData)
  139. }
  140. func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
  141. aes, err := aes.NewCipher(key)
  142. if err != nil {
  143. panic(err)
  144. }
  145. aead, err := cipher.NewGCM(aes)
  146. if err != nil {
  147. panic(err)
  148. }
  149. nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
  150. copy(nonce1, fixedNonce)
  151. copy(nonce2, fixedNonce)
  152. return &fixedNonceAEAD{nonce1, nonce2, aead}
  153. }
  154. // ssl30MAC implements the SSLv3 MAC function, as defined in
  155. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  156. type ssl30MAC struct {
  157. h hash.Hash
  158. key []byte
  159. }
  160. func (s ssl30MAC) Size() int {
  161. return s.h.Size()
  162. }
  163. 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}
  164. 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}
  165. func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte {
  166. padLength := 48
  167. if s.h.Size() == 20 {
  168. padLength = 40
  169. }
  170. s.h.Reset()
  171. s.h.Write(s.key)
  172. s.h.Write(ssl30Pad1[:padLength])
  173. s.h.Write(seq)
  174. s.h.Write(header[:1])
  175. s.h.Write(header[3:5])
  176. s.h.Write(data)
  177. digestBuf = s.h.Sum(digestBuf[:0])
  178. s.h.Reset()
  179. s.h.Write(s.key)
  180. s.h.Write(ssl30Pad2[:padLength])
  181. s.h.Write(digestBuf)
  182. return s.h.Sum(digestBuf[:0])
  183. }
  184. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  185. type tls10MAC struct {
  186. h hash.Hash
  187. }
  188. func (s tls10MAC) Size() int {
  189. return s.h.Size()
  190. }
  191. func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte {
  192. s.h.Reset()
  193. s.h.Write(seq)
  194. s.h.Write(header)
  195. s.h.Write(data)
  196. return s.h.Sum(digestBuf[:0])
  197. }
  198. func rsaKA(version uint16) keyAgreement {
  199. return rsaKeyAgreement{}
  200. }
  201. func ecdheECDSAKA(version uint16) keyAgreement {
  202. return &ecdheKeyAgreement{
  203. sigType: signatureECDSA,
  204. version: version,
  205. }
  206. }
  207. func ecdheRSAKA(version uint16) keyAgreement {
  208. return &ecdheKeyAgreement{
  209. sigType: signatureRSA,
  210. version: version,
  211. }
  212. }
  213. // mutualCipherSuite returns a cipherSuite given a list of supported
  214. // ciphersuites and the id requested by the peer.
  215. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  216. for _, id := range have {
  217. if id == want {
  218. for _, suite := range cipherSuites {
  219. if suite.id == want {
  220. return suite
  221. }
  222. }
  223. return nil
  224. }
  225. }
  226. return nil
  227. }
  228. // A list of cipher suite IDs that are, or have been, implemented by this
  229. // package.
  230. //
  231. // Taken from http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  232. const (
  233. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  234. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  235. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  236. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  237. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  238. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  239. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  240. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  241. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  242. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  243. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  244. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  245. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  246. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  247. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  248. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  249. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  250. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  251. // that the client is doing version fallback. See
  252. // https://tools.ietf.org/html/rfc7507.
  253. TLS_FALLBACK_SCSV uint16 = 0x5600
  254. )