]> Cypherpunks.ru repositories - gostls13.git/blob - src/crypto/tls/cipher_suites.go
crypto/tls: support AES-128-CBC cipher suites with SHA-256.
[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 (
8         "crypto/aes"
9         "crypto/cipher"
10         "crypto/des"
11         "crypto/hmac"
12         "crypto/rc4"
13         "crypto/sha1"
14         "crypto/sha256"
15         "crypto/x509"
16         "hash"
17 )
18
19 // a keyAgreement implements the client and server side of a TLS key agreement
20 // protocol by generating and processing key exchange messages.
21 type keyAgreement interface {
22         // On the server side, the first two methods are called in order.
23
24         // In the case that the key agreement protocol doesn't use a
25         // ServerKeyExchange message, generateServerKeyExchange can return nil,
26         // nil.
27         generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
28         processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
29
30         // On the client side, the next two methods are called in order.
31
32         // This method may not be called if the server doesn't send a
33         // ServerKeyExchange message.
34         processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
35         generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
36 }
37
38 const (
39         // suiteECDH indicates that the cipher suite involves elliptic curve
40         // Diffie-Hellman. This means that it should only be selected when the
41         // client indicates that it supports ECC with a curve and point format
42         // that we're happy with.
43         suiteECDHE = 1 << iota
44         // suiteECDSA indicates that the cipher suite involves an ECDSA
45         // signature and therefore may only be selected when the server's
46         // certificate is ECDSA. If this is not set then the cipher suite is
47         // RSA based.
48         suiteECDSA
49         // suiteTLS12 indicates that the cipher suite should only be advertised
50         // and accepted when using TLS 1.2.
51         suiteTLS12
52         // suiteSHA384 indicates that the cipher suite uses SHA384 as the
53         // handshake hash.
54         suiteSHA384
55         // suiteDefaultOff indicates that this cipher suite is not included by
56         // default.
57         suiteDefaultOff
58 )
59
60 // A cipherSuite is a specific combination of key agreement, cipher and MAC
61 // function. All cipher suites currently assume RSA key agreement.
62 type cipherSuite struct {
63         id uint16
64         // the lengths, in bytes, of the key material needed for each component.
65         keyLen int
66         macLen int
67         ivLen  int
68         ka     func(version uint16) keyAgreement
69         // flags is a bitmask of the suite* values, above.
70         flags  int
71         cipher func(key, iv []byte, isRead bool) interface{}
72         mac    func(version uint16, macKey []byte) macFunction
73         aead   func(key, fixedNonce []byte) cipher.AEAD
74 }
75
76 var cipherSuites = []*cipherSuite{
77         // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
78         // GCM is top preference.
79         {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
80         {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
81         {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
82         {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
83         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
84         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
85         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, cipherAES, macSHA256, nil},
86         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
87         {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
88         {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
89         {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
90         {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
91         {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
92         {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
93         {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
94         {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
95         {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
96
97         // RC4-based cipher suites are disabled by default.
98         {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
99         {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
100         {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
101 }
102
103 func cipherRC4(key, iv []byte, isRead bool) interface{} {
104         cipher, _ := rc4.NewCipher(key)
105         return cipher
106 }
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
116 func cipherAES(key, iv []byte, isRead bool) interface{} {
117         block, _ := aes.NewCipher(key)
118         if isRead {
119                 return cipher.NewCBCDecrypter(block, iv)
120         }
121         return cipher.NewCBCEncrypter(block, iv)
122 }
123
124 // macSHA1 returns a macFunction for the given protocol version.
125 func macSHA1(version uint16, key []byte) macFunction {
126         if version == VersionSSL30 {
127                 mac := ssl30MAC{
128                         h:   sha1.New(),
129                         key: make([]byte, len(key)),
130                 }
131                 copy(mac.key, key)
132                 return mac
133         }
134         return tls10MAC{hmac.New(sha1.New, key)}
135 }
136
137 // macSHA1 returns a SHA-256 based MAC. These are only supported in TLS 1.2 so
138 // the given version is ignored.
139 func macSHA256(version uint16, key []byte) macFunction {
140         return tls10MAC{hmac.New(sha256.New, key)}
141 }
142
143 type macFunction interface {
144         Size() int
145         MAC(digestBuf, seq, header, data []byte) []byte
146 }
147
148 // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
149 // each call.
150 type fixedNonceAEAD struct {
151         // sealNonce and openNonce are buffers where the larger nonce will be
152         // constructed. Since a seal and open operation may be running
153         // concurrently, there is a separate buffer for each.
154         sealNonce, openNonce []byte
155         aead                 cipher.AEAD
156 }
157
158 func (f *fixedNonceAEAD) NonceSize() int { return 8 }
159 func (f *fixedNonceAEAD) Overhead() int  { return f.aead.Overhead() }
160
161 func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
162         copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
163         return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
164 }
165
166 func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
167         copy(f.openNonce[len(f.openNonce)-8:], nonce)
168         return f.aead.Open(out, f.openNonce, plaintext, additionalData)
169 }
170
171 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
172         aes, err := aes.NewCipher(key)
173         if err != nil {
174                 panic(err)
175         }
176         aead, err := cipher.NewGCM(aes)
177         if err != nil {
178                 panic(err)
179         }
180
181         nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
182         copy(nonce1, fixedNonce)
183         copy(nonce2, fixedNonce)
184
185         return &fixedNonceAEAD{nonce1, nonce2, aead}
186 }
187
188 // ssl30MAC implements the SSLv3 MAC function, as defined in
189 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
190 type ssl30MAC struct {
191         h   hash.Hash
192         key []byte
193 }
194
195 func (s ssl30MAC) Size() int {
196         return s.h.Size()
197 }
198
199 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}
200
201 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}
202
203 func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte {
204         padLength := 48
205         if s.h.Size() == 20 {
206                 padLength = 40
207         }
208
209         s.h.Reset()
210         s.h.Write(s.key)
211         s.h.Write(ssl30Pad1[:padLength])
212         s.h.Write(seq)
213         s.h.Write(header[:1])
214         s.h.Write(header[3:5])
215         s.h.Write(data)
216         digestBuf = s.h.Sum(digestBuf[:0])
217
218         s.h.Reset()
219         s.h.Write(s.key)
220         s.h.Write(ssl30Pad2[:padLength])
221         s.h.Write(digestBuf)
222         return s.h.Sum(digestBuf[:0])
223 }
224
225 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
226 type tls10MAC struct {
227         h hash.Hash
228 }
229
230 func (s tls10MAC) Size() int {
231         return s.h.Size()
232 }
233
234 func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte {
235         s.h.Reset()
236         s.h.Write(seq)
237         s.h.Write(header)
238         s.h.Write(data)
239         return s.h.Sum(digestBuf[:0])
240 }
241
242 func rsaKA(version uint16) keyAgreement {
243         return rsaKeyAgreement{}
244 }
245
246 func ecdheECDSAKA(version uint16) keyAgreement {
247         return &ecdheKeyAgreement{
248                 sigType: signatureECDSA,
249                 version: version,
250         }
251 }
252
253 func ecdheRSAKA(version uint16) keyAgreement {
254         return &ecdheKeyAgreement{
255                 sigType: signatureRSA,
256                 version: version,
257         }
258 }
259
260 // mutualCipherSuite returns a cipherSuite given a list of supported
261 // ciphersuites and the id requested by the peer.
262 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
263         for _, id := range have {
264                 if id == want {
265                         for _, suite := range cipherSuites {
266                                 if suite.id == want {
267                                         return suite
268                                 }
269                         }
270                         return nil
271                 }
272         }
273         return nil
274 }
275
276 // A list of cipher suite IDs that are, or have been, implemented by this
277 // package.
278 //
279 // Taken from http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
280 const (
281         TLS_RSA_WITH_RC4_128_SHA                uint16 = 0x0005
282         TLS_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0x000a
283         TLS_RSA_WITH_AES_128_CBC_SHA            uint16 = 0x002f
284         TLS_RSA_WITH_AES_256_CBC_SHA            uint16 = 0x0035
285         TLS_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0x003c
286         TLS_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0x009c
287         TLS_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0x009d
288         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA        uint16 = 0xc007
289         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA    uint16 = 0xc009
290         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA    uint16 = 0xc00a
291         TLS_ECDHE_RSA_WITH_RC4_128_SHA          uint16 = 0xc011
292         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA     uint16 = 0xc012
293         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA      uint16 = 0xc013
294         TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA      uint16 = 0xc014
295         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
296         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256   uint16 = 0xc027
297         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256   uint16 = 0xc02f
298         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
299         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384   uint16 = 0xc030
300         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
301
302         // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
303         // that the client is doing version fallback. See
304         // https://tools.ietf.org/html/rfc7507.
305         TLS_FALLBACK_SCSV uint16 = 0x5600
306 )