Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

271 řádky
9.1 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. )
  45. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  46. // function. All cipher suites currently assume RSA key agreement.
  47. type cipherSuite struct {
  48. id uint16
  49. // the lengths, in bytes, of the key material needed for each component.
  50. keyLen int
  51. macLen int
  52. ivLen int
  53. ka func(version uint16) keyAgreement
  54. // flags is a bitmask of the suite* values, above.
  55. flags int
  56. cipher func(key, iv []byte, isRead bool) interface{}
  57. mac func(version uint16, macKey []byte) macFunction
  58. aead func(key, fixedNonce []byte) cipher.AEAD
  59. }
  60. var cipherSuites = []*cipherSuite{
  61. // Ciphersuite order is chosen so that ECDHE comes before plain RSA
  62. // and RC4 comes before AES (because of the Lucky13 attack).
  63. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  64. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
  65. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
  66. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherRC4, macSHA1, nil},
  67. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  68. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  69. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  70. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  71. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
  72. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  73. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  74. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  75. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  76. }
  77. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  78. cipher, _ := rc4.NewCipher(key)
  79. return cipher
  80. }
  81. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  82. block, _ := des.NewTripleDESCipher(key)
  83. if isRead {
  84. return cipher.NewCBCDecrypter(block, iv)
  85. }
  86. return cipher.NewCBCEncrypter(block, iv)
  87. }
  88. func cipherAES(key, iv []byte, isRead bool) interface{} {
  89. block, _ := aes.NewCipher(key)
  90. if isRead {
  91. return cipher.NewCBCDecrypter(block, iv)
  92. }
  93. return cipher.NewCBCEncrypter(block, iv)
  94. }
  95. // macSHA1 returns a macFunction for the given protocol version.
  96. func macSHA1(version uint16, key []byte) macFunction {
  97. if version == VersionSSL30 {
  98. mac := ssl30MAC{
  99. h: sha1.New(),
  100. key: make([]byte, len(key)),
  101. }
  102. copy(mac.key, key)
  103. return mac
  104. }
  105. return tls10MAC{hmac.New(sha1.New, key)}
  106. }
  107. type macFunction interface {
  108. Size() int
  109. MAC(digestBuf, seq, header, data []byte) []byte
  110. }
  111. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  112. // each call.
  113. type fixedNonceAEAD struct {
  114. // sealNonce and openNonce are buffers where the larger nonce will be
  115. // constructed. Since a seal and open operation may be running
  116. // concurrently, there is a separate buffer for each.
  117. sealNonce, openNonce []byte
  118. aead cipher.AEAD
  119. }
  120. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  121. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  122. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  123. copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
  124. return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
  125. }
  126. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  127. copy(f.openNonce[len(f.openNonce)-8:], nonce)
  128. return f.aead.Open(out, f.openNonce, plaintext, additionalData)
  129. }
  130. func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
  131. aes, err := aes.NewCipher(key)
  132. if err != nil {
  133. panic(err)
  134. }
  135. aead, err := cipher.NewGCM(aes)
  136. if err != nil {
  137. panic(err)
  138. }
  139. nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
  140. copy(nonce1, fixedNonce)
  141. copy(nonce2, fixedNonce)
  142. return &fixedNonceAEAD{nonce1, nonce2, aead}
  143. }
  144. // ssl30MAC implements the SSLv3 MAC function, as defined in
  145. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  146. type ssl30MAC struct {
  147. h hash.Hash
  148. key []byte
  149. }
  150. func (s ssl30MAC) Size() int {
  151. return s.h.Size()
  152. }
  153. 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}
  154. 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}
  155. func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte {
  156. padLength := 48
  157. if s.h.Size() == 20 {
  158. padLength = 40
  159. }
  160. s.h.Reset()
  161. s.h.Write(s.key)
  162. s.h.Write(ssl30Pad1[:padLength])
  163. s.h.Write(seq)
  164. s.h.Write(header[:1])
  165. s.h.Write(header[3:5])
  166. s.h.Write(data)
  167. digestBuf = s.h.Sum(digestBuf[:0])
  168. s.h.Reset()
  169. s.h.Write(s.key)
  170. s.h.Write(ssl30Pad2[:padLength])
  171. s.h.Write(digestBuf)
  172. return s.h.Sum(digestBuf[:0])
  173. }
  174. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  175. type tls10MAC struct {
  176. h hash.Hash
  177. }
  178. func (s tls10MAC) Size() int {
  179. return s.h.Size()
  180. }
  181. func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte {
  182. s.h.Reset()
  183. s.h.Write(seq)
  184. s.h.Write(header)
  185. s.h.Write(data)
  186. return s.h.Sum(digestBuf[:0])
  187. }
  188. func rsaKA(version uint16) keyAgreement {
  189. return rsaKeyAgreement{}
  190. }
  191. func ecdheECDSAKA(version uint16) keyAgreement {
  192. return &ecdheKeyAgreement{
  193. sigType: signatureECDSA,
  194. version: version,
  195. }
  196. }
  197. func ecdheRSAKA(version uint16) keyAgreement {
  198. return &ecdheKeyAgreement{
  199. sigType: signatureRSA,
  200. version: version,
  201. }
  202. }
  203. // mutualCipherSuite returns a cipherSuite given a list of supported
  204. // ciphersuites and the id requested by the peer.
  205. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  206. for _, id := range have {
  207. if id == want {
  208. for _, suite := range cipherSuites {
  209. if suite.id == want {
  210. return suite
  211. }
  212. }
  213. return nil
  214. }
  215. }
  216. return nil
  217. }
  218. // A list of the possible cipher suite ids. Taken from
  219. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  220. const (
  221. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  222. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  223. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  224. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  225. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  226. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  227. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  228. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  229. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  230. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  231. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  232. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  233. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  234. )