Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

396 linhas
14 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. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  58. // function. All cipher suites currently assume RSA key agreement.
  59. type cipherSuite struct {
  60. id uint16
  61. // the lengths, in bytes, of the key material needed for each component.
  62. keyLen int
  63. macLen int
  64. ivLen int
  65. ka func(version uint16) keyAgreement
  66. // flags is a bitmask of the suite* values, above.
  67. flags int
  68. cipher func(key, iv []byte, isRead bool) interface{}
  69. mac func(version uint16, macKey []byte) macFunction
  70. aead func(key, fixedNonce []byte) cipher.AEAD
  71. }
  72. var cipherSuites = []*cipherSuite{
  73. // Ciphersuite order is chosen so that ECDHE comes before plain RSA
  74. // and RC4 comes before AES (because of the Lucky13 attack).
  75. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  76. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
  77. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  78. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  79. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteNoDTLS, cipherRC4, macSHA1, nil},
  80. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteNoDTLS, cipherRC4, macSHA1, nil},
  81. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
  82. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, cipherAES, macSHA256, nil},
  83. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  84. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  85. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil},
  86. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 32, 48, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, cipherAES, macSHA384, nil},
  87. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  88. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  89. {TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, dheRSAKA, suiteTLS12, nil, nil, aeadAESGCM},
  90. {TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, dheRSAKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  91. {TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, dheRSAKA, suiteTLS12, cipherAES, macSHA256, nil},
  92. {TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, dheRSAKA, suiteTLS12, cipherAES, macSHA256, nil},
  93. {TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
  94. {TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
  95. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  96. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  97. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteNoDTLS, cipherRC4, macSHA1, nil},
  98. {TLS_RSA_WITH_RC4_128_MD5, 16, 16, 0, rsaKA, suiteNoDTLS, cipherRC4, macMD5, nil},
  99. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
  100. {TLS_RSA_WITH_AES_256_CBC_SHA256, 32, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
  101. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  102. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  103. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  104. {TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, dheRSAKA, 0, cipher3DES, macSHA1, nil},
  105. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  106. {TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdhePSKKA, suiteECDHE | suiteTLS12 | suitePSK, nil, nil, aeadAESGCM},
  107. {TLS_PSK_WITH_RC4_128_SHA, 16, 20, 0, pskKA, suiteNoDTLS | suitePSK, cipherRC4, macSHA1, nil},
  108. {TLS_PSK_WITH_AES_128_CBC_SHA, 16, 20, 16, pskKA, suitePSK, cipherAES, macSHA1, nil},
  109. {TLS_PSK_WITH_AES_256_CBC_SHA, 32, 20, 16, pskKA, suitePSK, cipherAES, macSHA1, nil},
  110. }
  111. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  112. cipher, _ := rc4.NewCipher(key)
  113. return cipher
  114. }
  115. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  116. block, _ := des.NewTripleDESCipher(key)
  117. if isRead {
  118. return cipher.NewCBCDecrypter(block, iv)
  119. }
  120. return cipher.NewCBCEncrypter(block, iv)
  121. }
  122. func cipherAES(key, iv []byte, isRead bool) interface{} {
  123. block, _ := aes.NewCipher(key)
  124. if isRead {
  125. return cipher.NewCBCDecrypter(block, iv)
  126. }
  127. return cipher.NewCBCEncrypter(block, iv)
  128. }
  129. // macSHA1 returns a macFunction for the given protocol version.
  130. func macSHA1(version uint16, key []byte) macFunction {
  131. if version == VersionSSL30 {
  132. mac := ssl30MAC{
  133. h: sha1.New(),
  134. key: make([]byte, len(key)),
  135. }
  136. copy(mac.key, key)
  137. return mac
  138. }
  139. return tls10MAC{hmac.New(sha1.New, key)}
  140. }
  141. func macMD5(version uint16, key []byte) macFunction {
  142. if version == VersionSSL30 {
  143. mac := ssl30MAC{
  144. h: md5.New(),
  145. key: make([]byte, len(key)),
  146. }
  147. copy(mac.key, key)
  148. return mac
  149. }
  150. return tls10MAC{hmac.New(md5.New, key)}
  151. }
  152. func macSHA256(version uint16, key []byte) macFunction {
  153. if version == VersionSSL30 {
  154. mac := ssl30MAC{
  155. h: sha256.New(),
  156. key: make([]byte, len(key)),
  157. }
  158. copy(mac.key, key)
  159. return mac
  160. }
  161. return tls10MAC{hmac.New(sha256.New, key)}
  162. }
  163. func macSHA384(version uint16, key []byte) macFunction {
  164. if version == VersionSSL30 {
  165. mac := ssl30MAC{
  166. h: sha512.New384(),
  167. key: make([]byte, len(key)),
  168. }
  169. copy(mac.key, key)
  170. return mac
  171. }
  172. return tls10MAC{hmac.New(sha512.New384, key)}
  173. }
  174. type macFunction interface {
  175. Size() int
  176. MAC(digestBuf, seq, header, length, data []byte) []byte
  177. }
  178. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  179. // each call.
  180. type fixedNonceAEAD struct {
  181. // sealNonce and openNonce are buffers where the larger nonce will be
  182. // constructed. Since a seal and open operation may be running
  183. // concurrently, there is a separate buffer for each.
  184. sealNonce, openNonce []byte
  185. aead cipher.AEAD
  186. }
  187. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  188. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  189. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  190. copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
  191. return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
  192. }
  193. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  194. copy(f.openNonce[len(f.openNonce)-8:], nonce)
  195. return f.aead.Open(out, f.openNonce, plaintext, additionalData)
  196. }
  197. func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
  198. aes, err := aes.NewCipher(key)
  199. if err != nil {
  200. panic(err)
  201. }
  202. aead, err := cipher.NewGCM(aes)
  203. if err != nil {
  204. panic(err)
  205. }
  206. nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
  207. copy(nonce1, fixedNonce)
  208. copy(nonce2, fixedNonce)
  209. return &fixedNonceAEAD{nonce1, nonce2, aead}
  210. }
  211. // ssl30MAC implements the SSLv3 MAC function, as defined in
  212. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  213. type ssl30MAC struct {
  214. h hash.Hash
  215. key []byte
  216. }
  217. func (s ssl30MAC) Size() int {
  218. return s.h.Size()
  219. }
  220. 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}
  221. 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}
  222. func (s ssl30MAC) MAC(digestBuf, seq, header, length, data []byte) []byte {
  223. padLength := 48
  224. if s.h.Size() == 20 {
  225. padLength = 40
  226. }
  227. s.h.Reset()
  228. s.h.Write(s.key)
  229. s.h.Write(ssl30Pad1[:padLength])
  230. s.h.Write(seq)
  231. s.h.Write(header[:1])
  232. s.h.Write(length)
  233. s.h.Write(data)
  234. digestBuf = s.h.Sum(digestBuf[:0])
  235. s.h.Reset()
  236. s.h.Write(s.key)
  237. s.h.Write(ssl30Pad2[:padLength])
  238. s.h.Write(digestBuf)
  239. return s.h.Sum(digestBuf[:0])
  240. }
  241. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  242. type tls10MAC struct {
  243. h hash.Hash
  244. }
  245. func (s tls10MAC) Size() int {
  246. return s.h.Size()
  247. }
  248. func (s tls10MAC) MAC(digestBuf, seq, header, length, data []byte) []byte {
  249. s.h.Reset()
  250. s.h.Write(seq)
  251. s.h.Write(header)
  252. s.h.Write(length)
  253. s.h.Write(data)
  254. return s.h.Sum(digestBuf[:0])
  255. }
  256. func rsaKA(version uint16) keyAgreement {
  257. return &rsaKeyAgreement{}
  258. }
  259. func ecdheECDSAKA(version uint16) keyAgreement {
  260. return &ecdheKeyAgreement{
  261. auth: &signedKeyAgreement{
  262. sigType: signatureECDSA,
  263. version: version,
  264. },
  265. }
  266. }
  267. func ecdheRSAKA(version uint16) keyAgreement {
  268. return &ecdheKeyAgreement{
  269. auth: &signedKeyAgreement{
  270. sigType: signatureRSA,
  271. version: version,
  272. },
  273. }
  274. }
  275. func dheRSAKA(version uint16) keyAgreement {
  276. return &dheKeyAgreement{
  277. auth: &signedKeyAgreement{
  278. sigType: signatureRSA,
  279. version: version,
  280. },
  281. }
  282. }
  283. func pskKA(version uint16) keyAgreement {
  284. return &pskKeyAgreement{
  285. base: &nilKeyAgreement{},
  286. }
  287. }
  288. func ecdhePSKKA(version uint16) keyAgreement {
  289. return &pskKeyAgreement{
  290. base: &ecdheKeyAgreement{
  291. auth: &nilKeyAgreementAuthentication{},
  292. },
  293. }
  294. }
  295. // mutualCipherSuite returns a cipherSuite given a list of supported
  296. // ciphersuites and the id requested by the peer.
  297. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  298. for _, id := range have {
  299. if id == want {
  300. for _, suite := range cipherSuites {
  301. if suite.id == want {
  302. return suite
  303. }
  304. }
  305. return nil
  306. }
  307. }
  308. return nil
  309. }
  310. // A list of the possible cipher suite ids. Taken from
  311. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  312. const (
  313. TLS_RSA_WITH_RC4_128_MD5 uint16 = 0x0004
  314. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  315. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  316. TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x0016
  317. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  318. TLS_DHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0x0033
  319. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  320. TLS_DHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0039
  321. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  322. TLS_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x003d
  323. TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x0067
  324. TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x006b
  325. TLS_PSK_WITH_RC4_128_SHA uint16 = 0x008a
  326. TLS_PSK_WITH_AES_128_CBC_SHA uint16 = 0x008c
  327. TLS_PSK_WITH_AES_256_CBC_SHA uint16 = 0x008d
  328. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  329. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  330. TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009e
  331. TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009f
  332. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  333. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  334. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  335. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  336. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  337. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  338. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  339. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  340. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc024
  341. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  342. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 uint16 = 0xc028
  343. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  344. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  345. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  346. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  347. fallbackSCSV uint16 = 0x5600
  348. )
  349. // Additional cipher suite IDs, not IANA-assigned.
  350. const (
  351. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 uint16 = 0xcafe
  352. )