No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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