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.
18 // a keyAgreement implements the client and server side of a TLS key agreement
19 // protocol by generating and processing key exchange messages.
20 type keyAgreement interface {
21 // On the server side, the first two methods are called in order.
23 // In the case that the key agreement protocol doesn't use a
24 // ServerKeyExchange message, generateServerKeyExchange can return nil,
26 generateServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
27 processClientKeyExchange(*Config, *clientKeyExchangeMsg, uint16) ([]byte, error)
29 // On the client side, the next two methods are called in order.
31 // This method may not be called if the server doesn't send a
32 // ServerKeyExchange message.
33 processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
34 generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
37 // A cipherSuite is a specific combination of key agreement, cipher and MAC
38 // function. All cipher suites currently assume RSA key agreement.
39 type cipherSuite struct {
40 // the lengths, in bytes, of the key material needed for each component.
44 ka func() keyAgreement
45 // If elliptic is set, a server will only consider this ciphersuite if
46 // the ClientHello indicated that the client supports an elliptic curve
47 // and point format that we can handle.
49 cipher func(key, iv []byte, isRead bool) interface{}
50 mac func(version uint16, macKey []byte) macFunction
53 var cipherSuites = map[uint16]*cipherSuite{
54 TLS_RSA_WITH_RC4_128_SHA: &cipherSuite{16, 20, 0, rsaKA, false, cipherRC4, macSHA1},
55 TLS_RSA_WITH_3DES_EDE_CBC_SHA: &cipherSuite{24, 20, 8, rsaKA, false, cipher3DES, macSHA1},
56 TLS_RSA_WITH_AES_128_CBC_SHA: &cipherSuite{16, 20, 16, rsaKA, false, cipherAES, macSHA1},
57 TLS_ECDHE_RSA_WITH_RC4_128_SHA: &cipherSuite{16, 20, 0, ecdheRSAKA, true, cipherRC4, macSHA1},
58 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA: &cipherSuite{24, 20, 8, ecdheRSAKA, true, cipher3DES, macSHA1},
59 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: &cipherSuite{16, 20, 16, ecdheRSAKA, true, cipherAES, macSHA1},
62 func cipherRC4(key, iv []byte, isRead bool) interface{} {
63 cipher, _ := rc4.NewCipher(key)
67 func cipher3DES(key, iv []byte, isRead bool) interface{} {
68 block, _ := des.NewTripleDESCipher(key)
70 return cipher.NewCBCDecrypter(block, iv)
72 return cipher.NewCBCEncrypter(block, iv)
75 func cipherAES(key, iv []byte, isRead bool) interface{} {
76 block, _ := aes.NewCipher(key)
78 return cipher.NewCBCDecrypter(block, iv)
80 return cipher.NewCBCEncrypter(block, iv)
83 // macSHA1 returns a macFunction for the given protocol version.
84 func macSHA1(version uint16, key []byte) macFunction {
85 if version == versionSSL30 {
88 key: make([]byte, len(key)),
93 return tls10MAC{hmac.NewSHA1(key)}
96 type macFunction interface {
98 MAC(seq, data []byte) []byte
101 // ssl30MAC implements the SSLv3 MAC function, as defined in
102 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
103 type ssl30MAC struct {
108 func (s ssl30MAC) Size() int {
112 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}
114 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}
116 func (s ssl30MAC) MAC(seq, record []byte) []byte {
118 if s.h.Size() == 20 {
124 s.h.Write(ssl30Pad1[:padLength])
126 s.h.Write(record[:1])
127 s.h.Write(record[3:5])
128 s.h.Write(record[recordHeaderLen:])
133 s.h.Write(ssl30Pad2[:padLength])
138 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
139 type tls10MAC struct {
143 func (s tls10MAC) Size() int {
147 func (s tls10MAC) MAC(seq, record []byte) []byte {
154 func rsaKA() keyAgreement {
155 return rsaKeyAgreement{}
158 func ecdheRSAKA() keyAgreement {
159 return new(ecdheRSAKeyAgreement)
162 // mutualCipherSuite returns a cipherSuite and its id given a list of supported
163 // ciphersuites and the id requested by the peer.
164 func mutualCipherSuite(have []uint16, want uint16) (suite *cipherSuite, id uint16) {
165 for _, id := range have {
167 return cipherSuites[id], id
173 // A list of the possible cipher suite ids. Taken from
174 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
176 TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
177 TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
178 TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
179 TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
180 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
181 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013