Alternative TLS implementation in Go
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

5 лет назад
crypto/tls: decouple handshake signatures from the handshake hash. Prior to TLS 1.2, the handshake had a pleasing property that one could incrementally hash it and, from that, get the needed hashes for both the CertificateVerify and Finished messages. TLS 1.2 introduced negotiation for the signature and hash and it became possible for the handshake hash to be, say, SHA-384, but for the CertificateVerify to sign the handshake with SHA-1. The problem is that one doesn't know in advance which hashes will be needed and thus the handshake needs to be buffered. Go ignored this, always kept a single handshake hash, and any signatures over the handshake had to use that hash. However, there are a set of servers that inspect the client's offered signature hash functions and will abort the handshake if one of the server's certificates is signed with a hash function outside of that set. https://robertsspaceindustries.com/ is an example of such a server. Clearly not a lot of thought happened when that server code was written, but its out there and we have to deal with it. This change decouples the handshake hash from the CertificateVerify hash. This lays the groundwork for advertising support for SHA-384 but doesn't actually make that change in the interests of reviewability. Updating the advertised hash functions will cause changes in many of the testdata/ files and some errors might get lost in the noise. This change only needs to update four testdata/ files: one because a SHA-384-based handshake is now being signed with SHA-256 and the others because the TLS 1.2 CertificateRequest message now includes SHA-1. This change also has the effect of adding support for client-certificates in SSLv3 servers. However, SSLv3 is now disabled by default so this should be moot. It would be possible to avoid much of this change and just support SHA-384 for the ServerKeyExchange as the SKX only signs over the nonces and SKX params (a design mistake in TLS). However, that would leave Go in the odd situation where it advertised support for SHA-384, but would only use the handshake hash when signing client certificates. I fear that'll just cause problems in the future. Much of this code was written by davidben@ for the purposes of testing BoringSSL. Partly addresses #9757 Change-Id: I5137a472b6076812af387a5a69fc62c7373cd485 Reviewed-on: https://go-review.googlesource.com/9415 Run-TryBot: Adam Langley <agl@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
9 лет назад
7 лет назад
crypto/tls: decouple handshake signatures from the handshake hash. Prior to TLS 1.2, the handshake had a pleasing property that one could incrementally hash it and, from that, get the needed hashes for both the CertificateVerify and Finished messages. TLS 1.2 introduced negotiation for the signature and hash and it became possible for the handshake hash to be, say, SHA-384, but for the CertificateVerify to sign the handshake with SHA-1. The problem is that one doesn't know in advance which hashes will be needed and thus the handshake needs to be buffered. Go ignored this, always kept a single handshake hash, and any signatures over the handshake had to use that hash. However, there are a set of servers that inspect the client's offered signature hash functions and will abort the handshake if one of the server's certificates is signed with a hash function outside of that set. https://robertsspaceindustries.com/ is an example of such a server. Clearly not a lot of thought happened when that server code was written, but its out there and we have to deal with it. This change decouples the handshake hash from the CertificateVerify hash. This lays the groundwork for advertising support for SHA-384 but doesn't actually make that change in the interests of reviewability. Updating the advertised hash functions will cause changes in many of the testdata/ files and some errors might get lost in the noise. This change only needs to update four testdata/ files: one because a SHA-384-based handshake is now being signed with SHA-256 and the others because the TLS 1.2 CertificateRequest message now includes SHA-1. This change also has the effect of adding support for client-certificates in SSLv3 servers. However, SSLv3 is now disabled by default so this should be moot. It would be possible to avoid much of this change and just support SHA-384 for the ServerKeyExchange as the SKX only signs over the nonces and SKX params (a design mistake in TLS). However, that would leave Go in the odd situation where it advertised support for SHA-384, but would only use the handshake hash when signing client certificates. I fear that'll just cause problems in the future. Much of this code was written by davidben@ for the purposes of testing BoringSSL. Partly addresses #9757 Change-Id: I5137a472b6076812af387a5a69fc62c7373cd485 Reviewed-on: https://go-review.googlesource.com/9415 Run-TryBot: Adam Langley <agl@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
9 лет назад
crypto/tls: decouple handshake signatures from the handshake hash. Prior to TLS 1.2, the handshake had a pleasing property that one could incrementally hash it and, from that, get the needed hashes for both the CertificateVerify and Finished messages. TLS 1.2 introduced negotiation for the signature and hash and it became possible for the handshake hash to be, say, SHA-384, but for the CertificateVerify to sign the handshake with SHA-1. The problem is that one doesn't know in advance which hashes will be needed and thus the handshake needs to be buffered. Go ignored this, always kept a single handshake hash, and any signatures over the handshake had to use that hash. However, there are a set of servers that inspect the client's offered signature hash functions and will abort the handshake if one of the server's certificates is signed with a hash function outside of that set. https://robertsspaceindustries.com/ is an example of such a server. Clearly not a lot of thought happened when that server code was written, but its out there and we have to deal with it. This change decouples the handshake hash from the CertificateVerify hash. This lays the groundwork for advertising support for SHA-384 but doesn't actually make that change in the interests of reviewability. Updating the advertised hash functions will cause changes in many of the testdata/ files and some errors might get lost in the noise. This change only needs to update four testdata/ files: one because a SHA-384-based handshake is now being signed with SHA-256 and the others because the TLS 1.2 CertificateRequest message now includes SHA-1. This change also has the effect of adding support for client-certificates in SSLv3 servers. However, SSLv3 is now disabled by default so this should be moot. It would be possible to avoid much of this change and just support SHA-384 for the ServerKeyExchange as the SKX only signs over the nonces and SKX params (a design mistake in TLS). However, that would leave Go in the odd situation where it advertised support for SHA-384, but would only use the handshake hash when signing client certificates. I fear that'll just cause problems in the future. Much of this code was written by davidben@ for the purposes of testing BoringSSL. Partly addresses #9757 Change-Id: I5137a472b6076812af387a5a69fc62c7373cd485 Reviewed-on: https://go-review.googlesource.com/9415 Run-TryBot: Adam Langley <agl@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
9 лет назад
crypto/tls: decouple handshake signatures from the handshake hash. Prior to TLS 1.2, the handshake had a pleasing property that one could incrementally hash it and, from that, get the needed hashes for both the CertificateVerify and Finished messages. TLS 1.2 introduced negotiation for the signature and hash and it became possible for the handshake hash to be, say, SHA-384, but for the CertificateVerify to sign the handshake with SHA-1. The problem is that one doesn't know in advance which hashes will be needed and thus the handshake needs to be buffered. Go ignored this, always kept a single handshake hash, and any signatures over the handshake had to use that hash. However, there are a set of servers that inspect the client's offered signature hash functions and will abort the handshake if one of the server's certificates is signed with a hash function outside of that set. https://robertsspaceindustries.com/ is an example of such a server. Clearly not a lot of thought happened when that server code was written, but its out there and we have to deal with it. This change decouples the handshake hash from the CertificateVerify hash. This lays the groundwork for advertising support for SHA-384 but doesn't actually make that change in the interests of reviewability. Updating the advertised hash functions will cause changes in many of the testdata/ files and some errors might get lost in the noise. This change only needs to update four testdata/ files: one because a SHA-384-based handshake is now being signed with SHA-256 and the others because the TLS 1.2 CertificateRequest message now includes SHA-1. This change also has the effect of adding support for client-certificates in SSLv3 servers. However, SSLv3 is now disabled by default so this should be moot. It would be possible to avoid much of this change and just support SHA-384 for the ServerKeyExchange as the SKX only signs over the nonces and SKX params (a design mistake in TLS). However, that would leave Go in the odd situation where it advertised support for SHA-384, but would only use the handshake hash when signing client certificates. I fear that'll just cause problems in the future. Much of this code was written by davidben@ for the purposes of testing BoringSSL. Partly addresses #9757 Change-Id: I5137a472b6076812af387a5a69fc62c7373cd485 Reviewed-on: https://go-review.googlesource.com/9415 Run-TryBot: Adam Langley <agl@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
9 лет назад
crypto/tls: decouple handshake signatures from the handshake hash. Prior to TLS 1.2, the handshake had a pleasing property that one could incrementally hash it and, from that, get the needed hashes for both the CertificateVerify and Finished messages. TLS 1.2 introduced negotiation for the signature and hash and it became possible for the handshake hash to be, say, SHA-384, but for the CertificateVerify to sign the handshake with SHA-1. The problem is that one doesn't know in advance which hashes will be needed and thus the handshake needs to be buffered. Go ignored this, always kept a single handshake hash, and any signatures over the handshake had to use that hash. However, there are a set of servers that inspect the client's offered signature hash functions and will abort the handshake if one of the server's certificates is signed with a hash function outside of that set. https://robertsspaceindustries.com/ is an example of such a server. Clearly not a lot of thought happened when that server code was written, but its out there and we have to deal with it. This change decouples the handshake hash from the CertificateVerify hash. This lays the groundwork for advertising support for SHA-384 but doesn't actually make that change in the interests of reviewability. Updating the advertised hash functions will cause changes in many of the testdata/ files and some errors might get lost in the noise. This change only needs to update four testdata/ files: one because a SHA-384-based handshake is now being signed with SHA-256 and the others because the TLS 1.2 CertificateRequest message now includes SHA-1. This change also has the effect of adding support for client-certificates in SSLv3 servers. However, SSLv3 is now disabled by default so this should be moot. It would be possible to avoid much of this change and just support SHA-384 for the ServerKeyExchange as the SKX only signs over the nonces and SKX params (a design mistake in TLS). However, that would leave Go in the odd situation where it advertised support for SHA-384, but would only use the handshake hash when signing client certificates. I fear that'll just cause problems in the future. Much of this code was written by davidben@ for the purposes of testing BoringSSL. Partly addresses #9757 Change-Id: I5137a472b6076812af387a5a69fc62c7373cd485 Reviewed-on: https://go-review.googlesource.com/9415 Run-TryBot: Adam Langley <agl@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
9 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 th5
  5. import (
  6. "crypto"
  7. "crypto/aes"
  8. "crypto/cipher"
  9. "crypto/des"
  10. "crypto/hmac"
  11. "crypto/rc4"
  12. "crypto/sha1"
  13. "crypto/sha256"
  14. "hash"
  15. "golang.org/x/crypto/chacha20poly1305"
  16. )
  17. // a keyAgreement implements the client and server side of a TLS key agreement
  18. // protocol by generating and processing key exchange messages.
  19. type keyAgreement interface {
  20. // On the server side, the first two methods are called in order.
  21. // In the case that the key agreement protocol doesn't use a
  22. // ServerKeyExchange message, generateServerKeyExchange can return nil,
  23. // nil.
  24. generateServerKeyExchange(*Config, crypto.PrivateKey, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
  25. processClientKeyExchange(*Config, crypto.PrivateKey, *clientKeyExchangeMsg, uint16) ([]byte, error)
  26. // On the client side, the next two methods are called in order.
  27. // This method may not be called if the server doesn't send a
  28. // ServerKeyExchange message.
  29. processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, crypto.PublicKey, *serverKeyExchangeMsg) error
  30. generateClientKeyExchange(*Config, *clientHelloMsg, crypto.PublicKey) ([]byte, *clientKeyExchangeMsg, error)
  31. // Returns ID of negotiated group
  32. NegotiatedGroup() CurveID
  33. }
  34. const (
  35. // suiteECDH indicates that the cipher suite involves elliptic curve
  36. // Diffie-Hellman. This means that it should only be selected when the
  37. // client indicates that it supports ECC with a curve and point format
  38. // that we're happy with.
  39. suiteECDHE = 1 << iota
  40. // suiteECDSA indicates that the cipher suite involves an ECDSA
  41. // signature and therefore may only be selected when the server's
  42. // certificate is ECDSA. If this is not set then the cipher suite is
  43. // RSA based.
  44. suiteECDSA
  45. // suiteTLS12 indicates that the cipher suite should only be advertised
  46. // and accepted when using TLS 1.2.
  47. suiteTLS12
  48. // suiteTLS13 indicates that the ones and only cipher suites to be
  49. // advertised and accepted when using TLS 1.3.
  50. suiteTLS13
  51. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  52. // handshake hash.
  53. suiteSHA384
  54. // suiteDefaultOff indicates that this cipher suite is not included by
  55. // default.
  56. suiteDefaultOff
  57. )
  58. // A cipherSuite is a specific combination of key agreement, cipher and MAC
  59. // function.
  60. type cipherSuite struct {
  61. id uint16
  62. // the lengths, in bytes, of the key material needed for each component.
  63. keyLen int
  64. macLen int
  65. ivLen int
  66. ka func(version uint16) keyAgreement
  67. // flags is a bitmask of the suite* values, above.
  68. flags int
  69. cipher func(key, iv []byte, isRead bool) interface{}
  70. mac func(version uint16, macKey []byte) macFunction
  71. aead func(key, fixedNonce []byte) cipher.AEAD
  72. }
  73. var cipherSuites = []*cipherSuite{
  74. // TLS 1.3 ciphersuites specify only the AEAD and the HKDF hash.
  75. {TLS_CHACHA20_POLY1305_SHA256, 32, 0, 12, nil, suiteTLS13, nil, nil, aeadChaCha20Poly1305},
  76. {TLS_AES_128_GCM_SHA256, 16, 0, 12, nil, suiteTLS13, nil, nil, aeadAESGCM13},
  77. {TLS_AES_256_GCM_SHA384, 32, 0, 12, nil, suiteTLS13 | suiteSHA384, nil, nil, aeadAESGCM13},
  78. // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
  79. // AEADs are the top preference.
  80. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  81. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  82. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM12},
  83. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM12},
  84. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM12},
  85. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM12},
  86. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  87. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  88. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  89. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  90. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  91. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
  92. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM12},
  93. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM12},
  94. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
  95. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  96. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  97. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  98. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  99. // RC4-based cipher suites are disabled by default.
  100. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
  101. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
  102. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
  103. }
  104. func cipherRC4(key, iv []byte, isRead bool) interface{} {
  105. cipher, _ := rc4.NewCipher(key)
  106. return cipher
  107. }
  108. func cipher3DES(key, iv []byte, isRead bool) interface{} {
  109. block, _ := des.NewTripleDESCipher(key)
  110. if isRead {
  111. return cipher.NewCBCDecrypter(block, iv)
  112. }
  113. return cipher.NewCBCEncrypter(block, iv)
  114. }
  115. func cipherAES(key, iv []byte, isRead bool) interface{} {
  116. block, _ := aes.NewCipher(key)
  117. if isRead {
  118. return cipher.NewCBCDecrypter(block, iv)
  119. }
  120. return cipher.NewCBCEncrypter(block, iv)
  121. }
  122. // macSHA1 returns a macFunction for the given protocol version.
  123. func macSHA1(version uint16, key []byte) macFunction {
  124. if version == VersionSSL30 {
  125. mac := ssl30MAC{
  126. h: sha1.New(),
  127. key: make([]byte, len(key)),
  128. }
  129. copy(mac.key, key)
  130. return mac
  131. }
  132. return tls10MAC{hmac.New(newConstantTimeHash(sha1.New), key)}
  133. }
  134. // macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
  135. // so the given version is ignored.
  136. func macSHA256(version uint16, key []byte) macFunction {
  137. return tls10MAC{hmac.New(sha256.New, key)}
  138. }
  139. type macFunction interface {
  140. Size() int
  141. MAC(digestBuf, seq, header, data, extra []byte) []byte
  142. }
  143. type aead interface {
  144. cipher.AEAD
  145. // explicitIVLen returns the number of bytes used by the explicit nonce
  146. // that is included in the record. This is eight for older AEADs and
  147. // zero for modern ones.
  148. explicitNonceLen() int
  149. }
  150. // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  151. // each call.
  152. type fixedNonceAEAD struct {
  153. // nonce contains the fixed part of the nonce in the first four bytes.
  154. nonce [12]byte
  155. aead cipher.AEAD
  156. }
  157. func (f *fixedNonceAEAD) NonceSize() int { return 8 }
  158. // Overhead returns the maximum difference between the lengths of a
  159. // plaintext and its ciphertext.
  160. func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
  161. func (f *fixedNonceAEAD) explicitNonceLen() int { return 8 }
  162. func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  163. copy(f.nonce[4:], nonce)
  164. return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
  165. }
  166. func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  167. copy(f.nonce[4:], nonce)
  168. return f.aead.Open(out, f.nonce[:], plaintext, additionalData)
  169. }
  170. // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
  171. // before each call.
  172. type xorNonceAEAD struct {
  173. nonceMask [12]byte
  174. aead cipher.AEAD
  175. }
  176. func (f *xorNonceAEAD) NonceSize() int { return 8 }
  177. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  178. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  179. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  180. for i, b := range nonce {
  181. f.nonceMask[4+i] ^= b
  182. }
  183. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  184. for i, b := range nonce {
  185. f.nonceMask[4+i] ^= b
  186. }
  187. return result
  188. }
  189. func (f *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
  190. for i, b := range nonce {
  191. f.nonceMask[4+i] ^= b
  192. }
  193. result, err := f.aead.Open(out, f.nonceMask[:], plaintext, additionalData)
  194. for i, b := range nonce {
  195. f.nonceMask[4+i] ^= b
  196. }
  197. return result, err
  198. }
  199. func aeadAESGCM12(key, fixedNonce []byte) cipher.AEAD {
  200. aes, err := aes.NewCipher(key)
  201. if err != nil {
  202. panic(err)
  203. }
  204. aead, err := cipher.NewGCM(aes)
  205. if err != nil {
  206. panic(err)
  207. }
  208. ret := &fixedNonceAEAD{aead: aead}
  209. copy(ret.nonce[:], fixedNonce)
  210. return ret
  211. }
  212. func aeadAESGCM13(key, fixedNonce []byte) cipher.AEAD {
  213. aes, err := aes.NewCipher(key)
  214. if err != nil {
  215. panic(err)
  216. }
  217. aead, err := cipher.NewGCM(aes)
  218. if err != nil {
  219. panic(err)
  220. }
  221. ret := &xorNonceAEAD{aead: aead}
  222. copy(ret.nonceMask[:], fixedNonce)
  223. return ret
  224. }
  225. func aeadChaCha20Poly1305(key, fixedNonce []byte) cipher.AEAD {
  226. aead, err := chacha20poly1305.New(key)
  227. if err != nil {
  228. panic(err)
  229. }
  230. ret := &xorNonceAEAD{aead: aead}
  231. copy(ret.nonceMask[:], fixedNonce)
  232. return ret
  233. }
  234. // ssl30MAC implements the SSLv3 MAC function, as defined in
  235. // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
  236. type ssl30MAC struct {
  237. h hash.Hash
  238. key []byte
  239. }
  240. func (s ssl30MAC) Size() int {
  241. return s.h.Size()
  242. }
  243. 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}
  244. 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}
  245. // MAC does not offer constant timing guarantees for SSL v3.0, since it's deemed
  246. // useless considering the similar, protocol-level POODLE vulnerability.
  247. func (s ssl30MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
  248. padLength := 48
  249. if s.h.Size() == 20 {
  250. padLength = 40
  251. }
  252. s.h.Reset()
  253. s.h.Write(s.key)
  254. s.h.Write(ssl30Pad1[:padLength])
  255. s.h.Write(seq)
  256. s.h.Write(header[:1])
  257. s.h.Write(header[3:5])
  258. s.h.Write(data)
  259. digestBuf = s.h.Sum(digestBuf[:0])
  260. s.h.Reset()
  261. s.h.Write(s.key)
  262. s.h.Write(ssl30Pad2[:padLength])
  263. s.h.Write(digestBuf)
  264. return s.h.Sum(digestBuf[:0])
  265. }
  266. type constantTimeHash interface {
  267. hash.Hash
  268. ConstantTimeSum(b []byte) []byte
  269. }
  270. // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
  271. // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
  272. type cthWrapper struct {
  273. h constantTimeHash
  274. }
  275. func (c *cthWrapper) Size() int { return c.h.Size() }
  276. func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
  277. func (c *cthWrapper) Reset() { c.h.Reset() }
  278. func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
  279. func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
  280. func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
  281. return func() hash.Hash {
  282. return &cthWrapper{h().(constantTimeHash)}
  283. }
  284. }
  285. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
  286. type tls10MAC struct {
  287. h hash.Hash
  288. }
  289. func (s tls10MAC) Size() int {
  290. return s.h.Size()
  291. }
  292. // MAC is guaranteed to take constant time, as long as
  293. // len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
  294. // the MAC, but is only provided to make the timing profile constant.
  295. func (s tls10MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
  296. s.h.Reset()
  297. s.h.Write(seq)
  298. s.h.Write(header)
  299. s.h.Write(data)
  300. res := s.h.Sum(digestBuf[:0])
  301. if extra != nil {
  302. s.h.Write(extra)
  303. }
  304. return res
  305. }
  306. func rsaKA(version uint16) keyAgreement {
  307. return rsaKeyAgreement{}
  308. }
  309. func ecdheECDSAKA(version uint16) keyAgreement {
  310. return &ecdheKeyAgreement{
  311. isRSA: false,
  312. version: version,
  313. }
  314. }
  315. func ecdheRSAKA(version uint16) keyAgreement {
  316. return &ecdheKeyAgreement{
  317. isRSA: true,
  318. version: version,
  319. }
  320. }
  321. // mutualCipherSuite returns a cipherSuite given a list of supported
  322. // ciphersuites and the id requested by the peer.
  323. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  324. for _, id := range have {
  325. if id == want {
  326. for _, suite := range cipherSuites {
  327. if suite.id == want {
  328. return suite
  329. }
  330. }
  331. return nil
  332. }
  333. }
  334. return nil
  335. }
  336. // A list of cipher suite IDs that are, or have been, implemented by this
  337. // package.
  338. //
  339. // Taken from http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  340. const (
  341. // TLS 1.0 - 1.2 cipher suites.
  342. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  343. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  344. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  345. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  346. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  347. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  348. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  349. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  350. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  351. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  352. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  353. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  354. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  355. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  356. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  357. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  358. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  359. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  360. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  361. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  362. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca8
  363. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca9
  364. // TLS 1.3+ cipher suites.
  365. TLS_AES_128_GCM_SHA256 uint16 = 0x1301
  366. TLS_AES_256_GCM_SHA384 uint16 = 0x1302
  367. TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
  368. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  369. // that the client is doing version fallback. See
  370. // https://tools.ietf.org/html/rfc7507.
  371. TLS_FALLBACK_SCSV uint16 = 0x5600
  372. )