OSDN Git Service

1134f36258374b24a642e8fe87b56c7229da34b4
[pf3gnuchains/gcc-fork.git] / libgo / go / 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/x509"
15         "hash"
16 )
17
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.
22
23         // In the case that the key agreement protocol doesn't use a
24         // ServerKeyExchange message, generateServerKeyExchange can return nil,
25         // nil.
26         generateServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
27         processClientKeyExchange(*Config, *clientKeyExchangeMsg, uint16) ([]byte, error)
28
29         // On the client side, the next two methods are called in order.
30
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)
35 }
36
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.
41         keyLen int
42         macLen int
43         ivLen  int
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.
48         elliptic bool
49         cipher   func(key, iv []byte, isRead bool) interface{}
50         mac      func(version uint16, macKey []byte) macFunction
51 }
52
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},
60 }
61
62 func cipherRC4(key, iv []byte, isRead bool) interface{} {
63         cipher, _ := rc4.NewCipher(key)
64         return cipher
65 }
66
67 func cipher3DES(key, iv []byte, isRead bool) interface{} {
68         block, _ := des.NewTripleDESCipher(key)
69         if isRead {
70                 return cipher.NewCBCDecrypter(block, iv)
71         }
72         return cipher.NewCBCEncrypter(block, iv)
73 }
74
75 func cipherAES(key, iv []byte, isRead bool) interface{} {
76         block, _ := aes.NewCipher(key)
77         if isRead {
78                 return cipher.NewCBCDecrypter(block, iv)
79         }
80         return cipher.NewCBCEncrypter(block, iv)
81 }
82
83 // macSHA1 returns a macFunction for the given protocol version.
84 func macSHA1(version uint16, key []byte) macFunction {
85         if version == versionSSL30 {
86                 mac := ssl30MAC{
87                         h:   sha1.New(),
88                         key: make([]byte, len(key)),
89                 }
90                 copy(mac.key, key)
91                 return mac
92         }
93         return tls10MAC{hmac.NewSHA1(key)}
94 }
95
96 type macFunction interface {
97         Size() int
98         MAC(seq, data []byte) []byte
99 }
100
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 {
104         h   hash.Hash
105         key []byte
106 }
107
108 func (s ssl30MAC) Size() int {
109         return s.h.Size()
110 }
111
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}
113
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}
115
116 func (s ssl30MAC) MAC(seq, record []byte) []byte {
117         padLength := 48
118         if s.h.Size() == 20 {
119                 padLength = 40
120         }
121
122         s.h.Reset()
123         s.h.Write(s.key)
124         s.h.Write(ssl30Pad1[:padLength])
125         s.h.Write(seq)
126         s.h.Write(record[:1])
127         s.h.Write(record[3:5])
128         s.h.Write(record[recordHeaderLen:])
129         digest := s.h.Sum()
130
131         s.h.Reset()
132         s.h.Write(s.key)
133         s.h.Write(ssl30Pad2[:padLength])
134         s.h.Write(digest)
135         return s.h.Sum()
136 }
137
138 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
139 type tls10MAC struct {
140         h hash.Hash
141 }
142
143 func (s tls10MAC) Size() int {
144         return s.h.Size()
145 }
146
147 func (s tls10MAC) MAC(seq, record []byte) []byte {
148         s.h.Reset()
149         s.h.Write(seq)
150         s.h.Write(record)
151         return s.h.Sum()
152 }
153
154 func rsaKA() keyAgreement {
155         return rsaKeyAgreement{}
156 }
157
158 func ecdheRSAKA() keyAgreement {
159         return new(ecdheRSAKeyAgreement)
160 }
161
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 {
166                 if id == want {
167                         return cipherSuites[id], id
168                 }
169         }
170         return
171 }
172
173 // A list of the possible cipher suite ids. Taken from
174 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
175 const (
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
182 )