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.
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.
24 // In the case that the key agreement protocol doesn't use a
25 // ServerKeyExchange message, generateServerKeyExchange can return nil,
27 generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
28 processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
30 // On the client side, the next two methods are called in order.
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)
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
49 // suiteTLS12 indicates that the cipher suite should only be advertised
50 // and accepted when using TLS 1.2.
52 // suiteSHA384 indicates that the cipher suite uses SHA384 as the
55 // suiteDefaultOff indicates that this cipher suite is not included by
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 {
64 // the lengths, in bytes, of the key material needed for each component.
68 ka func(version uint16) keyAgreement
69 // flags is a bitmask of the suite* values, above.
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
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},
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},
103 func cipherRC4(key, iv []byte, isRead bool) interface{} {
104 cipher, _ := rc4.NewCipher(key)
108 func cipher3DES(key, iv []byte, isRead bool) interface{} {
109 block, _ := des.NewTripleDESCipher(key)
111 return cipher.NewCBCDecrypter(block, iv)
113 return cipher.NewCBCEncrypter(block, iv)
116 func cipherAES(key, iv []byte, isRead bool) interface{} {
117 block, _ := aes.NewCipher(key)
119 return cipher.NewCBCDecrypter(block, iv)
121 return cipher.NewCBCEncrypter(block, iv)
124 // macSHA1 returns a macFunction for the given protocol version.
125 func macSHA1(version uint16, key []byte) macFunction {
126 if version == VersionSSL30 {
129 key: make([]byte, len(key)),
134 return tls10MAC{hmac.New(sha1.New, key)}
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)}
143 type macFunction interface {
145 MAC(digestBuf, seq, header, data []byte) []byte
148 // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
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
158 func (f *fixedNonceAEAD) NonceSize() int { return 8 }
159 func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
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)
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)
171 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
172 aes, err := aes.NewCipher(key)
176 aead, err := cipher.NewGCM(aes)
181 nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
182 copy(nonce1, fixedNonce)
183 copy(nonce2, fixedNonce)
185 return &fixedNonceAEAD{nonce1, nonce2, aead}
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 {
195 func (s ssl30MAC) Size() int {
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}
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}
203 func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte {
205 if s.h.Size() == 20 {
211 s.h.Write(ssl30Pad1[:padLength])
213 s.h.Write(header[:1])
214 s.h.Write(header[3:5])
216 digestBuf = s.h.Sum(digestBuf[:0])
220 s.h.Write(ssl30Pad2[:padLength])
222 return s.h.Sum(digestBuf[:0])
225 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
226 type tls10MAC struct {
230 func (s tls10MAC) Size() int {
234 func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte {
239 return s.h.Sum(digestBuf[:0])
242 func rsaKA(version uint16) keyAgreement {
243 return rsaKeyAgreement{}
246 func ecdheECDSAKA(version uint16) keyAgreement {
247 return &ecdheKeyAgreement{
248 sigType: signatureECDSA,
253 func ecdheRSAKA(version uint16) keyAgreement {
254 return &ecdheKeyAgreement{
255 sigType: signatureRSA,
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 {
265 for _, suite := range cipherSuites {
266 if suite.id == want {
276 // A list of cipher suite IDs that are, or have been, implemented by this
279 // Taken from http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
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
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