gitee.com/lh-her-team/common@v1.5.1/crypto/tls/cipher_suites.go (about)

     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  
     5  package tls
     6  
     7  import (
     8  	"crypto"
     9  	"crypto/aes"
    10  	"crypto/cipher"
    11  	"crypto/des"
    12  	"crypto/hmac"
    13  	"crypto/rc4"
    14  	"crypto/sha1"
    15  	"crypto/sha256"
    16  	"fmt"
    17  	"hash"
    18  
    19  	cmx509 "gitee.com/lh-her-team/common/crypto/x509"
    20  
    21  	"golang.org/x/crypto/chacha20poly1305"
    22  )
    23  
    24  // CipherSuite is a TLS cipher suite. Note that most functions in this package
    25  // accept and expose cipher suite IDs instead of this type.
    26  type CipherSuite struct {
    27  	ID   uint16
    28  	Name string
    29  	// Supported versions is the list of TLS protocol versions that can
    30  	// negotiate this cipher suite.
    31  	SupportedVersions []uint16
    32  	// Insecure is true if the cipher suite has known security issues
    33  	// due to its primitives, design, or implementation.
    34  	Insecure bool
    35  }
    36  
    37  var (
    38  	supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
    39  	supportedOnlyTLS12 = []uint16{VersionTLS12}
    40  	supportedOnlyTLS13 = []uint16{VersionTLS13}
    41  )
    42  
    43  // CipherSuites returns a list of cipher suites currently implemented by this
    44  // package, excluding those with security issues, which are returned by
    45  // InsecureCipherSuites.
    46  //
    47  // The list is sorted by ID. Note that the default cipher suites selected by
    48  // this package might depend on logic that can't be captured by a static list.
    49  func CipherSuites() []*CipherSuite {
    50  	return []*CipherSuite{
    51  		{TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
    52  		{TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
    53  		{TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
    54  		{TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
    55  		{TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
    56  
    57  		{TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
    58  		{TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
    59  		{TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
    60  
    61  		{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
    62  		{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
    63  		{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
    64  		{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
    65  		{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
    66  		{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
    67  		{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
    68  		{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
    69  		{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
    70  		{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
    71  		{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
    72  	}
    73  }
    74  
    75  // InsecureCipherSuites returns a list of cipher suites currently implemented by
    76  // this package and which have security issues.
    77  //
    78  // Most applications should not use the cipher suites in this list, and should
    79  // only use those returned by CipherSuites.
    80  func InsecureCipherSuites() []*CipherSuite {
    81  	// RC4 suites are broken because RC4 is.
    82  	// CBC-SHA256 suites have no Lucky13 countermeasures.
    83  	return []*CipherSuite{
    84  		{TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
    85  		{TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
    86  		{TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
    87  		{TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
    88  		{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
    89  		{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
    90  	}
    91  }
    92  
    93  // CipherSuiteName returns the standard name for the passed cipher suite ID
    94  // (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
    95  // of the ID value if the cipher suite is not implemented by this package.
    96  func CipherSuiteName(id uint16) string {
    97  	for _, c := range CipherSuites() {
    98  		if c.ID == id {
    99  			return c.Name
   100  		}
   101  	}
   102  	for _, c := range InsecureCipherSuites() {
   103  		if c.ID == id {
   104  			return c.Name
   105  		}
   106  	}
   107  	return fmt.Sprintf("0x%04X", id)
   108  }
   109  
   110  // a keyAgreement implements the client and server side of a TLS key agreement
   111  // protocol by generating and processing key exchange messages.
   112  type keyAgreement interface {
   113  	// On the server side, the first two methods are called in order.
   114  
   115  	// In the case that the key agreement protocol doesn't use a
   116  	// ServerKeyExchange message, generateServerKeyExchange can return nil,
   117  	// nil.
   118  	generateServerKeyExchange(*Config, *Certificate, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
   119  	//generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
   120  	processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
   121  
   122  	// On the client side, the next two methods are called in order.
   123  
   124  	// This method may not be called if the server doesn't send a
   125  	// ServerKeyExchange message.
   126  	processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *cmx509.Certificate, *serverKeyExchangeMsg) error
   127  	generateClientKeyExchange(*Config, *clientHelloMsg, *cmx509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
   128  }
   129  
   130  const (
   131  	// suiteECDHE indicates that the cipher suite involves elliptic curve
   132  	// Diffie-Hellman. This means that it should only be selected when the
   133  	// client indicates that it supports ECC with a curve and point format
   134  	// that we're happy with.
   135  	suiteECDHE = 1 << iota
   136  	// suiteECSign indicates that the cipher suite involves an ECDSA or
   137  	// EdDSA signature and therefore may only be selected when the server's
   138  	// certificate is ECDSA or EdDSA. If this is not set then the cipher suite
   139  	// is RSA based.
   140  	suiteECSign
   141  	// suiteTLS12 indicates that the cipher suite should only be advertised
   142  	// and accepted when using TLS 1.2.
   143  	suiteTLS12
   144  	// suiteSHA384 indicates that the cipher suite uses SHA384 as the
   145  	// handshake hash.
   146  	suiteSHA384
   147  	// suiteDefaultOff indicates that this cipher suite is not included by
   148  	// default.
   149  	suiteDefaultOff
   150  )
   151  
   152  // A cipherSuite is a specific combination of key agreement, cipher and MAC function.
   153  type cipherSuite struct {
   154  	id uint16
   155  	// the lengths, in bytes, of the key material needed for each component.
   156  	keyLen int
   157  	macLen int
   158  	ivLen  int
   159  	ka     func(version uint16) keyAgreement
   160  	// flags is a bitmask of the suite* values, above.
   161  	flags  int
   162  	cipher func(key, iv []byte, isRead bool) interface{}
   163  	mac    func(version uint16, macKey []byte) macFunction
   164  	aead   func(key, fixedNonce []byte) cipher.AEAD
   165  }
   166  
   167  var cipherSuites = []*cipherSuite{
   168  	// Ciphersuite order is chosen so that ECDHE comes before plain RSA and
   169  	// AEADs are the top preference.
   170  	{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
   171  	{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
   172  	{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
   173  	{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
   174  	{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
   175  	{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
   176  	{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
   177  	{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
   178  	{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
   179  	{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
   180  	{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
   181  	{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
   182  	{TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
   183  	{TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
   184  	{TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
   185  	{TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
   186  	{TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
   187  	{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
   188  	{TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
   189  
   190  	// RC4-based cipher suites are disabled by default.
   191  	{TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
   192  	{TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
   193  	{TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteDefaultOff, cipherRC4, macSHA1, nil},
   194  }
   195  
   196  // selectCipherSuite returns the first cipher suite from ids which is also in
   197  // supportedIDs and passes the ok filter.
   198  func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
   199  	for _, id := range ids {
   200  		candidate := cipherSuiteByID(id)
   201  		if candidate == nil || !ok(candidate) {
   202  			continue
   203  		}
   204  		for _, suppID := range supportedIDs {
   205  			if id == suppID {
   206  				return candidate
   207  			}
   208  		}
   209  	}
   210  	return nil
   211  }
   212  
   213  // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
   214  // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
   215  type cipherSuiteTLS13 struct {
   216  	id     uint16
   217  	keyLen int
   218  	aead   func(key, fixedNonce []byte) cipher.AEAD
   219  	hash   crypto.Hash
   220  }
   221  
   222  var cipherSuitesTLS13 = []*cipherSuiteTLS13{
   223  	{TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
   224  	{TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
   225  	{TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
   226  }
   227  
   228  func cipherRC4(key, iv []byte, isRead bool) interface{} {
   229  	cipher, _ := rc4.NewCipher(key)
   230  	return cipher
   231  }
   232  
   233  func cipher3DES(key, iv []byte, isRead bool) interface{} {
   234  	block, _ := des.NewTripleDESCipher(key)
   235  	if isRead {
   236  		return cipher.NewCBCDecrypter(block, iv)
   237  	}
   238  	return cipher.NewCBCEncrypter(block, iv)
   239  }
   240  
   241  func cipherAES(key, iv []byte, isRead bool) interface{} {
   242  	block, _ := aes.NewCipher(key)
   243  	if isRead {
   244  		return cipher.NewCBCDecrypter(block, iv)
   245  	}
   246  	return cipher.NewCBCEncrypter(block, iv)
   247  }
   248  
   249  // macSHA1 returns a macFunction for the given protocol version.
   250  func macSHA1(version uint16, key []byte) macFunction {
   251  	return tls10MAC{h: hmac.New(newConstantTimeHash(sha1.New), key)}
   252  }
   253  
   254  // macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
   255  // so the given version is ignored.
   256  func macSHA256(version uint16, key []byte) macFunction {
   257  	return tls10MAC{h: hmac.New(sha256.New, key)}
   258  }
   259  
   260  type macFunction interface {
   261  	// Size returns the length of the MAC.
   262  	Size() int
   263  	// MAC appends the MAC of (seq, header, data) to out. The extra data is fed
   264  	// into the MAC after obtaining the result to normalize timing. The result
   265  	// is only valid until the next invocation of MAC as the buffer is reused.
   266  	MAC(seq, header, data, extra []byte) []byte
   267  }
   268  
   269  type aead interface {
   270  	cipher.AEAD
   271  	// explicitNonceLen returns the number of bytes of explicit nonce
   272  	// included in each record. This is eight for older AEADs and
   273  	// zero for modern ones.
   274  	explicitNonceLen() int
   275  }
   276  
   277  const (
   278  	aeadNonceLength   = 12
   279  	noncePrefixLength = 4
   280  )
   281  
   282  // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
   283  // each call.
   284  type prefixNonceAEAD struct {
   285  	// nonce contains the fixed part of the nonce in the first four bytes.
   286  	nonce [aeadNonceLength]byte
   287  	aead  cipher.AEAD
   288  }
   289  
   290  func (f *prefixNonceAEAD) NonceSize() int        { return aeadNonceLength - noncePrefixLength }
   291  func (f *prefixNonceAEAD) Overhead() int         { return f.aead.Overhead() }
   292  func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
   293  
   294  func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
   295  	copy(f.nonce[4:], nonce)
   296  	return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
   297  }
   298  
   299  func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
   300  	copy(f.nonce[4:], nonce)
   301  	return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
   302  }
   303  
   304  // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
   305  // before each call.
   306  type xorNonceAEAD struct {
   307  	nonceMask [aeadNonceLength]byte
   308  	aead      cipher.AEAD
   309  }
   310  
   311  func (f *xorNonceAEAD) NonceSize() int        { return 8 } // 64-bit sequence number
   312  func (f *xorNonceAEAD) Overhead() int         { return f.aead.Overhead() }
   313  func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
   314  
   315  func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
   316  	for i, b := range nonce {
   317  		f.nonceMask[4+i] ^= b
   318  	}
   319  	result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
   320  	for i, b := range nonce {
   321  		f.nonceMask[4+i] ^= b
   322  	}
   323  	return result
   324  }
   325  
   326  func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
   327  	for i, b := range nonce {
   328  		f.nonceMask[4+i] ^= b
   329  	}
   330  	result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
   331  	for i, b := range nonce {
   332  		f.nonceMask[4+i] ^= b
   333  	}
   334  	return result, err
   335  }
   336  
   337  func aeadAESGCM(key, noncePrefix []byte) cipher.AEAD {
   338  	if len(noncePrefix) != noncePrefixLength {
   339  		panic("tls: internal error: wrong nonce length")
   340  	}
   341  	aes, err := aes.NewCipher(key)
   342  	if err != nil {
   343  		panic(err)
   344  	}
   345  	aead, err := cipher.NewGCM(aes)
   346  	if err != nil {
   347  		panic(err)
   348  	}
   349  	ret := &prefixNonceAEAD{aead: aead}
   350  	copy(ret.nonce[:], noncePrefix)
   351  	return ret
   352  }
   353  
   354  func aeadAESGCMTLS13(key, nonceMask []byte) cipher.AEAD {
   355  	if len(nonceMask) != aeadNonceLength {
   356  		panic("tls: internal error: wrong nonce length")
   357  	}
   358  	aes, err := aes.NewCipher(key)
   359  	if err != nil {
   360  		panic(err)
   361  	}
   362  	aead, err := cipher.NewGCM(aes)
   363  	if err != nil {
   364  		panic(err)
   365  	}
   366  	ret := &xorNonceAEAD{aead: aead}
   367  	copy(ret.nonceMask[:], nonceMask)
   368  	return ret
   369  }
   370  
   371  func aeadChaCha20Poly1305(key, nonceMask []byte) cipher.AEAD {
   372  	if len(nonceMask) != aeadNonceLength {
   373  		panic("tls: internal error: wrong nonce length")
   374  	}
   375  	aead, err := chacha20poly1305.New(key)
   376  	if err != nil {
   377  		panic(err)
   378  	}
   379  	ret := &xorNonceAEAD{aead: aead}
   380  	copy(ret.nonceMask[:], nonceMask)
   381  	return ret
   382  }
   383  
   384  type constantTimeHash interface {
   385  	hash.Hash
   386  	ConstantTimeSum(b []byte) []byte
   387  }
   388  
   389  // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
   390  // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
   391  type cthWrapper struct {
   392  	h constantTimeHash
   393  }
   394  
   395  func (c *cthWrapper) Size() int                   { return c.h.Size() }
   396  func (c *cthWrapper) BlockSize() int              { return c.h.BlockSize() }
   397  func (c *cthWrapper) Reset()                      { c.h.Reset() }
   398  func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
   399  func (c *cthWrapper) Sum(b []byte) []byte         { return c.h.ConstantTimeSum(b) }
   400  
   401  func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
   402  	return func() hash.Hash {
   403  		return &cthWrapper{h().(constantTimeHash)}
   404  	}
   405  }
   406  
   407  // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
   408  type tls10MAC struct {
   409  	h   hash.Hash
   410  	buf []byte
   411  }
   412  
   413  func (s tls10MAC) Size() int {
   414  	return s.h.Size()
   415  }
   416  
   417  // MAC is guaranteed to take constant time, as long as
   418  // len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
   419  // the MAC, but is only provided to make the timing profile constant.
   420  func (s tls10MAC) MAC(seq, header, data, extra []byte) []byte {
   421  	s.h.Reset()
   422  	s.h.Write(seq)
   423  	s.h.Write(header)
   424  	s.h.Write(data)
   425  	res := s.h.Sum(s.buf[:0])
   426  	if extra != nil {
   427  		s.h.Write(extra)
   428  	}
   429  	return res
   430  }
   431  
   432  func rsaKA(version uint16) keyAgreement {
   433  	return rsaKeyAgreement{}
   434  }
   435  
   436  func ecdheECDSAKA(version uint16) keyAgreement {
   437  	return &ecdheKeyAgreement{
   438  		isRSA:   false,
   439  		version: version,
   440  	}
   441  }
   442  
   443  func ecdheRSAKA(version uint16) keyAgreement {
   444  	return &ecdheKeyAgreement{
   445  		isRSA:   true,
   446  		version: version,
   447  	}
   448  }
   449  
   450  // mutualCipherSuite returns a cipherSuite given a list of supported
   451  // ciphersuites and the id requested by the peer.
   452  func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
   453  	for _, id := range have {
   454  		if id == want {
   455  			return cipherSuiteByID(id)
   456  		}
   457  	}
   458  	return nil
   459  }
   460  
   461  func cipherSuiteByID(id uint16) *cipherSuite {
   462  	for _, cipherSuite := range cipherSuites {
   463  		if cipherSuite.id == id {
   464  			return cipherSuite
   465  		}
   466  	}
   467  	return nil
   468  }
   469  
   470  func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
   471  	for _, id := range have {
   472  		if id == want {
   473  			return cipherSuiteTLS13ByID(id)
   474  		}
   475  	}
   476  	return nil
   477  }
   478  
   479  func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
   480  	for _, cipherSuite := range cipherSuitesTLS13 {
   481  		if cipherSuite.id == id {
   482  			return cipherSuite
   483  		}
   484  	}
   485  	return nil
   486  }
   487  
   488  // A list of cipher suite IDs that are, or have been, implemented by this
   489  // package.
   490  //
   491  // See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
   492  const (
   493  	// TLS 1.0 - 1.2 cipher suites.
   494  	TLS_RSA_WITH_RC4_128_SHA                      uint16 = 0x0005
   495  	TLS_RSA_WITH_3DES_EDE_CBC_SHA                 uint16 = 0x000a
   496  	TLS_RSA_WITH_AES_128_CBC_SHA                  uint16 = 0x002f
   497  	TLS_RSA_WITH_AES_256_CBC_SHA                  uint16 = 0x0035
   498  	TLS_RSA_WITH_AES_128_CBC_SHA256               uint16 = 0x003c
   499  	TLS_RSA_WITH_AES_128_GCM_SHA256               uint16 = 0x009c
   500  	TLS_RSA_WITH_AES_256_GCM_SHA384               uint16 = 0x009d
   501  	TLS_ECDHE_ECDSA_WITH_RC4_128_SHA              uint16 = 0xc007
   502  	TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA          uint16 = 0xc009
   503  	TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA          uint16 = 0xc00a
   504  	TLS_ECDHE_RSA_WITH_RC4_128_SHA                uint16 = 0xc011
   505  	TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0xc012
   506  	TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA            uint16 = 0xc013
   507  	TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA            uint16 = 0xc014
   508  	TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256       uint16 = 0xc023
   509  	TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0xc027
   510  	TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0xc02f
   511  	TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256       uint16 = 0xc02b
   512  	TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0xc030
   513  	TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384       uint16 = 0xc02c
   514  	TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xcca8
   515  	TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
   516  
   517  	// TLS 1.3 cipher suites.
   518  	TLS_AES_128_GCM_SHA256       uint16 = 0x1301
   519  	TLS_AES_256_GCM_SHA384       uint16 = 0x1302
   520  	TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
   521  
   522  	// TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
   523  	// that the client is doing version fallback. See RFC 7507.
   524  	TLS_FALLBACK_SCSV uint16 = 0x5600
   525  
   526  	// Legacy names for the corresponding cipher suites with the correct _SHA256
   527  	// suffix, retained for backward compatibility.
   528  	TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305   = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
   529  	TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
   530  )