1
15
16 package gmtls
17
18 import (
19 "crypto/aes"
20 "crypto/cipher"
21 "crypto/des"
22 "crypto/hmac"
23 "crypto/rc4"
24 "crypto/sha1"
25 "crypto/sha256"
26 "hash"
27
28 "github.com/tjfoc/gmsm/x509"
29
30 "golang.org/x/crypto/chacha20poly1305"
31 )
32
33
34
35 type keyAgreement interface {
36
37
38
39
40
41 generateServerKeyExchange(*Config, *Certificate, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
42 processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
43
44
45
46
47
48 processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
49 generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
50 }
51
52 const (
53
54
55
56
57 suiteECDHE = 1 << iota
58
59
60
61
62 suiteECDSA
63
64
65 suiteTLS12
66
67
68 suiteSHA384
69
70
71 suiteDefaultOff
72 )
73
74
75
76 type cipherSuite struct {
77 id uint16
78
79 keyLen int
80 macLen int
81 ivLen int
82 ka func(version uint16) keyAgreement
83
84 flags int
85 cipher func(key, iv []byte, isRead bool) interface{}
86 mac func(version uint16, macKey []byte) macFunction
87 aead func(key, fixedNonce []byte) cipher.AEAD
88 }
89
90 var cipherSuites = []*cipherSuite{
91
92
93 {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
94 {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
95 {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
96 {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
97 {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
98 {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
99 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
100 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
101 {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
102 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
103 {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
104 {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
105 {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
106 {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
107 {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
108 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
109 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
110
111
112 {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
113 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
114 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
115 }
116
117 func cipherRC4(key, iv []byte, isRead bool) interface{} {
118 cipher, _ := rc4.NewCipher(key)
119 return cipher
120 }
121
122 func cipher3DES(key, iv []byte, isRead bool) interface{} {
123 block, _ := des.NewTripleDESCipher(key)
124 if isRead {
125 return cipher.NewCBCDecrypter(block, iv)
126 }
127 return cipher.NewCBCEncrypter(block, iv)
128 }
129
130 func cipherAES(key, iv []byte, isRead bool) interface{} {
131 block, _ := aes.NewCipher(key)
132 if isRead {
133 return cipher.NewCBCDecrypter(block, iv)
134 }
135 return cipher.NewCBCEncrypter(block, iv)
136 }
137
138
139 func macSHA1(version uint16, key []byte) macFunction {
140 if version == VersionSSL30 {
141 mac := ssl30MAC{
142 h: sha1.New(),
143 key: make([]byte, len(key)),
144 }
145 copy(mac.key, key)
146 return mac
147 }
148 return tls10MAC{hmac.New(newConstantTimeHash(sha1.New), key)}
149 }
150
151
152
153 func macSHA256(version uint16, key []byte) macFunction {
154 return tls10MAC{hmac.New(sha256.New, key)}
155 }
156
157 type macFunction interface {
158 Size() int
159 MAC(digestBuf, seq, header, data, extra []byte) []byte
160 }
161
162 type aead interface {
163 cipher.AEAD
164
165
166
167
168 explicitNonceLen() int
169 }
170
171
172
173 type fixedNonceAEAD struct {
174
175 nonce [12]byte
176 aead cipher.AEAD
177 }
178
179 func (f *fixedNonceAEAD) NonceSize() int { return 8 }
180 func (f *fixedNonceAEAD) Overhead() int { return f.aead.Overhead() }
181 func (f *fixedNonceAEAD) explicitNonceLen() int { return 8 }
182
183 func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
184 copy(f.nonce[4:], nonce)
185 return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
186 }
187
188 func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
189 copy(f.nonce[4:], nonce)
190 return f.aead.Open(out, f.nonce[:], plaintext, additionalData)
191 }
192
193
194
195 type xorNonceAEAD struct {
196 nonceMask [12]byte
197 aead cipher.AEAD
198 }
199
200 func (f *xorNonceAEAD) NonceSize() int { return 8 }
201 func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
202 func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
203
204 func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
205 for i, b := range nonce {
206 f.nonceMask[4+i] ^= b
207 }
208 result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
209 for i, b := range nonce {
210 f.nonceMask[4+i] ^= b
211 }
212
213 return result
214 }
215
216 func (f *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
217 for i, b := range nonce {
218 f.nonceMask[4+i] ^= b
219 }
220 result, err := f.aead.Open(out, f.nonceMask[:], plaintext, additionalData)
221 for i, b := range nonce {
222 f.nonceMask[4+i] ^= b
223 }
224
225 return result, err
226 }
227
228 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
229 aes, err := aes.NewCipher(key)
230 if err != nil {
231 panic(err)
232 }
233 aead, err := cipher.NewGCM(aes)
234 if err != nil {
235 panic(err)
236 }
237
238 ret := &fixedNonceAEAD{aead: aead}
239 copy(ret.nonce[:], fixedNonce)
240 return ret
241 }
242
243 func aeadChaCha20Poly1305(key, fixedNonce []byte) cipher.AEAD {
244 aead, err := chacha20poly1305.New(key)
245 if err != nil {
246 panic(err)
247 }
248
249 ret := &xorNonceAEAD{aead: aead}
250 copy(ret.nonceMask[:], fixedNonce)
251 return ret
252 }
253
254
255
256 type ssl30MAC struct {
257 h hash.Hash
258 key []byte
259 }
260
261 func (s ssl30MAC) Size() int {
262 return s.h.Size()
263 }
264
265 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}
266
267 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}
268
269
270
271 func (s ssl30MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
272 padLength := 48
273 if s.h.Size() == 20 {
274 padLength = 40
275 }
276
277 s.h.Reset()
278 s.h.Write(s.key)
279 s.h.Write(ssl30Pad1[:padLength])
280 s.h.Write(seq)
281 s.h.Write(header[:1])
282 s.h.Write(header[3:5])
283 s.h.Write(data)
284 digestBuf = s.h.Sum(digestBuf[:0])
285
286 s.h.Reset()
287 s.h.Write(s.key)
288 s.h.Write(ssl30Pad2[:padLength])
289 s.h.Write(digestBuf)
290 return s.h.Sum(digestBuf[:0])
291 }
292
293 type constantTimeHash interface {
294 hash.Hash
295 ConstantTimeSum(b []byte) []byte
296 }
297
298
299
300 type cthWrapper struct {
301 h constantTimeHash
302 }
303
304 func (c *cthWrapper) Size() int { return c.h.Size() }
305 func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
306 func (c *cthWrapper) Reset() { c.h.Reset() }
307 func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
308 func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
309
310 func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
311 return func() hash.Hash {
312 return &cthWrapper{h().(constantTimeHash)}
313 }
314 }
315
316
317 type tls10MAC struct {
318 h hash.Hash
319 }
320
321 func (s tls10MAC) Size() int {
322 return s.h.Size()
323 }
324
325
326
327
328 func (s tls10MAC) MAC(digestBuf, seq, header, data, extra []byte) []byte {
329 s.h.Reset()
330 s.h.Write(seq)
331 s.h.Write(header)
332 s.h.Write(data)
333 res := s.h.Sum(digestBuf[:0])
334 if extra != nil {
335 s.h.Write(extra)
336 }
337 return res
338 }
339
340 func rsaKA(version uint16) keyAgreement {
341 return rsaKeyAgreement{}
342 }
343
344 func ecdheECDSAKA(version uint16) keyAgreement {
345 return &ecdheKeyAgreement{
346 isRSA: false,
347 version: version,
348 }
349 }
350
351 func ecdheRSAKA(version uint16) keyAgreement {
352 return &ecdheKeyAgreement{
353 isRSA: true,
354 version: version,
355 }
356 }
357
358
359
360 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
361 for _, id := range have {
362 if id == want {
363 for _, suite := range cipherSuites {
364 if suite.id == want {
365 return suite
366 }
367 }
368 return nil
369 }
370 }
371 return nil
372 }
373
374
375
376
377
378 const (
379 TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
380 TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
381 TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
382 TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
383 TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
384 TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
385 TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
386 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
387 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
388 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
389 TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
390 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
391 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
392 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
393 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
394 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
395 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
396 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
397 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
398 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
399 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca8
400 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca9
401
402
403
404
405 TLS_FALLBACK_SCSV uint16 = 0x5600
406 )
407
View as plain text