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
57 // A cipherSuite is a specific combination of key agreement, cipher and MAC
58 // function. All cipher suites currently assume RSA key agreement.
59 type cipherSuite struct {
61 // the lengths, in bytes, of the key material needed for each component.
65 ka func(version uint16) keyAgreement
66 // flags is a bitmask of the suite* values, above.
68 cipher func(key, iv []byte, isRead bool) interface{}
69 mac func(version uint16, macKey []byte) macFunction
70 aead func(key, fixedNonce []byte) cipher.AEAD
73 var cipherSuites = []*cipherSuite{
74 // Ciphersuite order is chosen so that ECDHE comes before plain RSA
75 // and RC4 comes before AES (because of the Lucky13 attack).
76 {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
77 {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
78 {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
79 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
80 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherRC4, macSHA1, nil},
81 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
82 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
83 {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
84 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
85 {TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, dheRSAKA, suiteTLS12, nil, nil, aeadAESGCM},
86 {TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, dheRSAKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
87 {TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
88 {TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, dheRSAKA, 0, 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_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
92 {TLS_RSA_WITH_RC4_128_MD5, 16, 16, 0, rsaKA, 0, cipherRC4, macMD5, nil},
93 {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
94 {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
95 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
96 {TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, dheRSAKA, 0, cipher3DES, macSHA1, nil},
97 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
100 func cipherRC4(key, iv []byte, isRead bool) interface{} {
101 cipher, _ := rc4.NewCipher(key)
105 func cipher3DES(key, iv []byte, isRead bool) interface{} {
106 block, _ := des.NewTripleDESCipher(key)
108 return cipher.NewCBCDecrypter(block, iv)
110 return cipher.NewCBCEncrypter(block, iv)
113 func cipherAES(key, iv []byte, isRead bool) interface{} {
114 block, _ := aes.NewCipher(key)
116 return cipher.NewCBCDecrypter(block, iv)
118 return cipher.NewCBCEncrypter(block, iv)
121 // macSHA1 returns a macFunction for the given protocol version.
122 func macSHA1(version uint16, key []byte) macFunction {
123 if version == VersionSSL30 {
126 key: make([]byte, len(key)),
131 return tls10MAC{hmac.New(sha1.New, key)}
134 func macMD5(version uint16, key []byte) macFunction {
135 if version == VersionSSL30 {
138 key: make([]byte, len(key)),
143 return tls10MAC{hmac.New(md5.New, key)}
146 type macFunction interface {
148 MAC(digestBuf, seq, header, data []byte) []byte
151 // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
153 type fixedNonceAEAD struct {
154 // sealNonce and openNonce are buffers where the larger nonce will be
155 // constructed. Since a seal and open operation may be running
156 // concurrently, there is a separate buffer for each.
157 sealNonce, openNonce []byte
161 func (f *fixedNonceAEAD) NonceSize() int { return 8 }
162 func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
164 func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
165 copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
166 return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
169 func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
170 copy(f.openNonce[len(f.openNonce)-8:], nonce)
171 return f.aead.Open(out, f.openNonce, plaintext, additionalData)
174 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
175 aes, err := aes.NewCipher(key)
179 aead, err := cipher.NewGCM(aes)
184 nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
185 copy(nonce1, fixedNonce)
186 copy(nonce2, fixedNonce)
188 return &fixedNonceAEAD{nonce1, nonce2, aead}
191 // ssl30MAC implements the SSLv3 MAC function, as defined in
192 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
193 type ssl30MAC struct {
198 func (s ssl30MAC) Size() int {
202 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}
204 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}
206 func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte {
208 if s.h.Size() == 20 {
214 s.h.Write(ssl30Pad1[:padLength])
216 s.h.Write(header[:1])
217 s.h.Write(header[3:5])
219 digestBuf = s.h.Sum(digestBuf[:0])
223 s.h.Write(ssl30Pad2[:padLength])
225 return s.h.Sum(digestBuf[:0])
228 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
229 type tls10MAC struct {
233 func (s tls10MAC) Size() int {
237 func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte {
242 return s.h.Sum(digestBuf[:0])
245 func rsaKA(version uint16) keyAgreement {
246 return rsaKeyAgreement{}
249 func ecdheECDSAKA(version uint16) keyAgreement {
250 return &ecdheKeyAgreement{
251 signedKeyAgreement: signedKeyAgreement{
252 sigType: signatureECDSA,
258 func ecdheRSAKA(version uint16) keyAgreement {
259 return &ecdheKeyAgreement{
260 signedKeyAgreement: signedKeyAgreement{
261 sigType: signatureRSA,
267 func dheRSAKA(version uint16) keyAgreement {
268 return &dheKeyAgreement{
269 signedKeyAgreement: signedKeyAgreement{
270 sigType: signatureRSA,
276 // mutualCipherSuite returns a cipherSuite given a list of supported
277 // ciphersuites and the id requested by the peer.
278 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
279 for _, id := range have {
281 for _, suite := range cipherSuites {
282 if suite.id == want {
292 // A list of the possible cipher suite ids. Taken from
293 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
295 TLS_RSA_WITH_RC4_128_MD5 uint16 = 0x0004
296 TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
297 TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
298 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x0016
299 TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
300 TLS_DHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0x0033
301 TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
302 TLS_DHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0039
303 TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
304 TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
305 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009e
306 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009f
307 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
308 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
309 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
310 TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
311 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
312 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
313 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
314 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
315 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
316 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
317 fallbackSCSV uint16 = 0x5600