Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

193 lignes
6.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. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  31. // function. All cipher suites currently assume RSA key agreement.
  32. type cipherSuite struct {
  33. id uint16
  34. // the lengths, in bytes, of the key material needed for each component.
  35. keyLen int
  36. macLen int
  37. ivLen int
  38. ka func() keyAgreement
  39. // If elliptic is set, a server will only consider this ciphersuite if
  40. // the ClientHello indicated that the client supports an elliptic curve
  41. // and point format that we can handle.
  42. elliptic bool
  43. cipher func(key, iv []byte, isRead bool) interface{}
  44. mac func(version uint16, macKey []byte) macFunction
  45. }
  46. var cipherSuites = []*cipherSuite{
  47. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, false, cipherRC4, macSHA1},
  48. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, false, cipher3DES, macSHA1},
  49. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, false, cipherAES, macSHA1},
  50. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, false, cipherAES, macSHA1},
  51. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, true, cipherRC4, macSHA1},
  52. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, true, cipher3DES, macSHA1},
  53. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, true, cipherAES, macSHA1},
  54. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, true, cipherAES, macSHA1},
  55. }
  56. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  57. cipher, _ := rc4.NewCipher(key)
  58. return cipher
  59. }
  60. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  61. block, _ := des.NewTripleDESCipher(key)
  62. if isRead {
  63. return cipher.NewCBCDecrypter(block, iv)
  64. }
  65. return cipher.NewCBCEncrypter(block, iv)
  66. }
  67. func cipherAES(key, iv []byte, isRead bool) interface{} {
  68. block, _ := aes.NewCipher(key)
  69. if isRead {
  70. return cipher.NewCBCDecrypter(block, iv)
  71. }
  72. return cipher.NewCBCEncrypter(block, iv)
  73. }
  74. // macSHA1 returns a macFunction for the given protocol version.
  75. func macSHA1(version uint16, key []byte) macFunction {
  76. if version == versionSSL30 {
  77. mac := ssl30MAC{
  78. h: sha1.New(),
  79. key: make([]byte, len(key)),
  80. }
  81. copy(mac.key, key)
  82. return mac
  83. }
  84. return tls10MAC{hmac.New(sha1.New, key)}
  85. }
  86. type macFunction interface {
  87. Size() int
  88. MAC(digestBuf, seq, data []byte) []byte
  89. }
  90. // ssl30MAC implements the SSLv3 MAC function, as defined in
  91. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  92. type ssl30MAC struct {
  93. h hash.Hash
  94. key []byte
  95. }
  96. func (s ssl30MAC) Size() int {
  97. return s.h.Size()
  98. }
  99. 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}
  100. 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}
  101. func (s ssl30MAC) MAC(digestBuf, seq, record []byte) []byte {
  102. padLength := 48
  103. if s.h.Size() == 20 {
  104. padLength = 40
  105. }
  106. s.h.Reset()
  107. s.h.Write(s.key)
  108. s.h.Write(ssl30Pad1[:padLength])
  109. s.h.Write(seq)
  110. s.h.Write(record[:1])
  111. s.h.Write(record[3:5])
  112. s.h.Write(record[recordHeaderLen:])
  113. digestBuf = s.h.Sum(digestBuf[:0])
  114. s.h.Reset()
  115. s.h.Write(s.key)
  116. s.h.Write(ssl30Pad2[:padLength])
  117. s.h.Write(digestBuf)
  118. return s.h.Sum(digestBuf[:0])
  119. }
  120. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  121. type tls10MAC struct {
  122. h hash.Hash
  123. }
  124. func (s tls10MAC) Size() int {
  125. return s.h.Size()
  126. }
  127. func (s tls10MAC) MAC(digestBuf, seq, record []byte) []byte {
  128. s.h.Reset()
  129. s.h.Write(seq)
  130. s.h.Write(record)
  131. return s.h.Sum(digestBuf[:0])
  132. }
  133. func rsaKA() keyAgreement {
  134. return rsaKeyAgreement{}
  135. }
  136. func ecdheRSAKA() keyAgreement {
  137. return new(ecdheRSAKeyAgreement)
  138. }
  139. // mutualCipherSuite returns a cipherSuite given a list of supported
  140. // ciphersuites and the id requested by the peer.
  141. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  142. for _, id := range have {
  143. if id == want {
  144. for _, suite := range cipherSuites {
  145. if suite.id == want {
  146. return suite
  147. }
  148. }
  149. return nil
  150. }
  151. }
  152. return nil
  153. }
  154. // A list of the possible cipher suite ids. Taken from
  155. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  156. const (
  157. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  158. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  159. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  160. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  161. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  162. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  163. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  164. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  165. )