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