forked from tjfoc/gmsm
/
gm_support.go
489 lines (429 loc) · 14.6 KB
/
gm_support.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gmtls
import (
"crypto"
"crypto/cipher"
"crypto/hmac"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"strings"
"sync"
"github.com/feifanfan/gmsm/sm2"
"github.com/feifanfan/gmsm/sm3"
"github.com/feifanfan/gmsm/sm4"
"github.com/feifanfan/gmsm/x509"
)
const VersionGMSSL = 0x0101 // GM/T 0024-2014
const noncePrefixLength = 4 // RFC 5116 & RFC 5288
var pemCAs = []struct {
name string
pem string
}{
{
name: "CFCA",
pem: `-----BEGIN CERTIFICATE-----
MIICezCCAh6gAwIBAgIQJRABs1dlPn+86pb7bT74wjAMBggqgRzPVQGDdQUAMFgx
CzAJBgNVBAYTAkNOMTAwLgYDVQQKDCdDaGluYSBGaW5hbmNpYWwgQ2VydGlmaWNh
dGlvbiBBdXRob3JpdHkxFzAVBgNVBAMMDkNGQ0EgQ1MgU00yIENBMB4XDTE1MDcx
MTAzMTUxM1oXDTM1MDcwNDAzMTUxM1owJTELMAkGA1UEBhMCQ04xFjAUBgNVBAoM
DUNGQ0EgU00yIE9DQTEwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAAR8mpCijT4m
jIJHLSaxLZibTvrydXWlIu4r261LWKPfyhtYPKarSgxFHBTMMwRAjX0JqOjclSgY
XE6+wD5ha7dco4H6MIH3MB8GA1UdIwQYMBaAFOSO3dSj57YP7h0nls113CUlcmnd
MA8GA1UdEwEB/wQFMAMBAf8wgZMGA1UdHwSBizCBiDBVoFOgUaRPME0xCzAJBgNV
BAYTAkNOMRMwEQYDVQQKDApDRkNBIENTIENBMQwwCgYDVQQLDANDUkwxDDAKBgNV
BAsMA1NNMjENMAsGA1UEAwwEY3JsMTAvoC2gK4YpaHR0cDovL2NybC5jZmNhLmNv
bS5jbi9jc3JjYS9TTTIvY3JsMS5jcmwwDgYDVR0PAQH/BAQDAgEGMB0GA1UdDgQW
BBRck1ggW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m9ybmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNp
c2NvMRQwEgYDVQQKEwtleGFtcGxlLmNvbTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5j
b20wWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAARAp0oXL9xvWjkipnru0gsuL95g
jpjscT5fQw0bHXPBSzYSq0+hoJf7C3t6tzjnI6pN0156KZg8Y1Bg7fx9xxOHo18w
XTAOBgNVHQ8BAf8EBAMCAaYwDwYDVR0lBAgwBgYEVR0lADAPBgNVHRMBAf8EBTAD
AQH/MCkGA1UdDgQiBCAt1zWEo9mUfmTAZlZthCkppNjgQlpQ9A77ylguCH4tRDAK
BggqgRzPVQGDdQNIADBFAiBFx066bqQswz5eFA6IWZjj7GmdAyypq48IUaI8cs+b
AwIhAPKX+rTHK3IHmZ3MHU2ajoJcGwq0h7aWpcpljF6cld4r
-----END CERTIFICATE-----`,
},
}
var certCAs []*x509.Certificate
var initonce sync.Once
func getCAs() []*x509.Certificate {
// mod by syl remove pre insert ca certs
return nil
initonce.Do(func() {
for _, pemca := range pemCAs {
block, _ := pem.Decode([]byte(pemca.pem))
ca, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic(err)
}
certCAs = append(certCAs, ca)
}
})
return certCAs
}
// A list of cipher suite IDs that are, or have been, implemented by this
// package.
const (
//GM crypto suites ID Taken from GM/T 0024-2014
GMTLS_ECDHE_SM2_WITH_SM1_SM3 uint16 = 0xe001
GMTLS_SM2_WITH_SM1_SM3 uint16 = 0xe003
GMTLS_IBSDH_WITH_SM1_SM3 uint16 = 0xe005
GMTLS_IBC_WITH_SM1_SM3 uint16 = 0xe007
GMTLS_RSA_WITH_SM1_SM3 uint16 = 0xe009
GMTLS_RSA_WITH_SM1_SHA1 uint16 = 0xe00a
GMTLS_ECDHE_SM2_WITH_SM4_SM3 uint16 = 0xe011
GMTLS_ECDHE_SM4_CBC_SM3 uint16 = 0xe011
GMTLS_ECDHE_SM4_GCM_SM3 uint16 = 0xe051
GMTLS_SM2_WITH_SM4_SM3 uint16 = 0xe013
GMTLS_ECC_SM4_CBC_SM3 uint16 = 0xe013
GMTLS_ECC_SM4_GCM_SM3 uint16 = 0xe053
GMTLS_IBSDH_WITH_SM4_SM3 uint16 = 0xe015
GMTLS_IBC_WITH_SM4_SM3 uint16 = 0xe017
GMTLS_RSA_WITH_SM4_SM3 uint16 = 0xe019
GMTLS_RSA_WITH_SM4_SHA1 uint16 = 0xe01a
)
var gmCipherSuites = []*cipherSuite{
{GMTLS_ECC_SM4_CBC_SM3, 16, 32, 16, eccGMKA, suiteECDSA, cipherSM4, macSM3, nil},
{GMTLS_ECC_SM4_GCM_SM3, 16, 0, 4, eccGMKA, suiteECDSA, nil, nil, aeadSM4GCM},
{GMTLS_ECDHE_SM4_CBC_SM3, 16, 32, 16, ecdheGMKA, suiteECDHE | suiteECDSA, cipherSM4, macSM3, nil},
{GMTLS_ECDHE_SM4_GCM_SM3, 16, 0, 4, ecdheGMKA, suiteECDHE | suiteECDSA, nil, nil, aeadSM4GCM},
}
// aeadSM4GCM SM4 GCM向前加解密函数
// key: 对称密钥
// nonce: 隐式随机数 (implicit nonce 4 Byte)
func aeadSM4GCM(key []byte, nonce []byte) cipher.AEAD {
if len(nonce) != noncePrefixLength {
panic("tls: internal error: wrong implicit nonce length")
}
block, err := sm4.NewCipher(key)
if err != nil {
panic(err)
}
aead, err := cipher.NewGCMWithNonceSize(block, 12)
if err != nil {
panic(err)
}
// AEAD 使用的随机数应由显式和隐式两部分构成,
// 显式部分即 nonce explicit,客户端和服务端使用隐式部分
// 分别来自 client_write_iv 和 server_write_iv。
// AEAD使用的随机数和计数器的构造参见 RFC 5116
ret := &fixedNonceAEAD{aead: aead}
copy(ret.nonce[:], nonce)
return ret
}
func getCipherSuites(c *Config) []uint16 {
s := c.CipherSuites
if s == nil {
s = []uint16{
GMTLS_ECC_SM4_CBC_SM3,
GMTLS_ECC_SM4_GCM_SM3,
GMTLS_ECDHE_SM4_CBC_SM3,
GMTLS_ECDHE_SM4_GCM_SM3,
}
}
return s
}
func cipherSM4(key, iv []byte, isRead bool) interface{} {
block, _ := sm4.NewCipher(key)
if isRead {
return cipher.NewCBCDecrypter(block, iv)
}
return cipher.NewCBCEncrypter(block, iv)
}
// macSHA1 returns a macFunction for the given protocol version.
func macSM3(version uint16, key []byte) macFunction {
return tls10MAC{hmac.New(sm3.New, key)}
}
// used for adapt the demand of finishHash write
type nilMD5Hash struct{}
func (nilMD5Hash) Write(p []byte) (n int, err error) {
return 0, nil
}
func (nilMD5Hash) Sum(b []byte) []byte {
return nil
}
func (nilMD5Hash) Reset() {
}
func (nilMD5Hash) Size() int {
return 0
}
func (nilMD5Hash) BlockSize() int {
return 0
}
func newFinishedHashGM(cipherSuite *cipherSuite) finishedHash {
return finishedHash{sm3.New(), sm3.New(), new(nilMD5Hash), new(nilMD5Hash), []byte{}, VersionGMSSL, prf12(sm3.New)}
}
func ecdheGMKA(version uint16) keyAgreement {
return &ecdheKeyAgreementGM{
version: version,
}
}
func eccGMKA(version uint16) keyAgreement {
return &eccKeyAgreementGM{
version: version,
}
}
// mutualCipherSuite returns a cipherSuite given a list of supported
// ciphersuites and the id requested by the peer.
func mutualCipherSuiteGM(have []uint16, want uint16) *cipherSuite {
for _, id := range have {
if id == want {
for _, suite := range gmCipherSuites {
if suite.id == want {
return suite
}
}
return nil
}
}
return nil
}
const (
ModeGMSSLOnly = "GMSSLOnly" // 仅支持 国密SSL模式
ModeAutoSwitch = "AutoSwitch" // GMSSL/TLS 自动切换模式
)
type GMSupport struct {
WorkMode string // 工作模式
}
func NewGMSupport() *GMSupport {
return &GMSupport{WorkMode: ModeGMSSLOnly}
}
func (support *GMSupport) GetVersion() uint16 {
return VersionGMSSL
}
func (support *GMSupport) IsAvailable() bool {
return true
}
func (support *GMSupport) cipherSuites() []*cipherSuite {
return gmCipherSuites
}
// EnableMixMode 启用 GMSSL/TLS 自动切换的工作模式
func (support *GMSupport) EnableMixMode() {
support.WorkMode = ModeAutoSwitch
}
// IsAutoSwitchMode 是否处于混合工作模式
// return true - GMSSL/TLS 均支持, false - 不处于混合模式
func (support *GMSupport) IsAutoSwitchMode() bool {
return support.WorkMode == ModeAutoSwitch
}
// LoadGMX509KeyPairs reads and parses two public/private key pairs from pairs
// of files. The files must contain PEM encoded data. The certificate file
// may contain intermediate certificates following the leaf certificate to
// form a certificate chain. On successful return, Certificate.Leaf will
// be nil because the parsed form of the certificate is not retained.
func LoadGMX509KeyPairs(certFile, keyFile, encCertFile, encKeyFile string) (Certificate, error) {
certPEMBlock, err := ioutil.ReadFile(certFile)
if err != nil {
return Certificate{}, err
}
keyPEMBlock, err := ioutil.ReadFile(keyFile)
if err != nil {
return Certificate{}, err
}
encCertPEMBlock, err := ioutil.ReadFile(encCertFile)
if err != nil {
return Certificate{}, err
}
encKeyPEMBlock, err := ioutil.ReadFile(encKeyFile)
if err != nil {
return Certificate{}, err
}
return GMX509KeyPairs(certPEMBlock, keyPEMBlock, encCertPEMBlock, encKeyPEMBlock)
}
// add by syl add sigle key pair sitiation
func LoadGMX509KeyPair(certFile, keyFile string) (Certificate, error) {
certPEMBlock, err := ioutil.ReadFile(certFile)
if err != nil {
return Certificate{}, err
}
keyPEMBlock, err := ioutil.ReadFile(keyFile)
if err != nil {
return Certificate{}, err
}
return GMX509KeyPairsSingle(certPEMBlock, keyPEMBlock)
}
////load sign/enc certs and sign/enc privatekey from one single file respectively
//func LoadGMX509KeyPairs2(certFile, keyFile string) (Certificate, error) {
// certPEMBlock, err := ioutil.ReadFile(certFile)
// if err != nil {
// return Certificate{}, err
// }
// keyPEMBlock, err := ioutil.ReadFile(keyFile)
// if err != nil {
// return Certificate{}, err
// }
// encCertPEMBlock, err := ioutil.ReadFile(encCertFile)
// if err != nil {
// return Certificate{}, err
// }
// encKeyPEMBlock, err := ioutil.ReadFile(encKeyFile)
// if err != nil {
// return Certificate{}, err
// }
//
// return GMX509KeyPairs(certPEMBlock, keyPEMBlock, encCertPEMBlock, encKeyPEMBlock)
//}
func getCert(certPEMBlock []byte) ([][]byte, error) {
var certs [][]byte
var skippedBlockTypes []string
for {
var certDERBlock *pem.Block
certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
if certDERBlock == nil {
break
}
if certDERBlock.Type == "CERTIFICATE" {
certs = append(certs, certDERBlock.Bytes)
} else {
skippedBlockTypes = append(skippedBlockTypes, certDERBlock.Type)
}
}
if len(certs) == 0 {
if len(skippedBlockTypes) == 0 {
return nil, errors.New("tls: failed to find any PEM data in certificate input")
}
if len(skippedBlockTypes) == 1 && strings.HasSuffix(skippedBlockTypes[0], "PRIVATE KEY") {
return nil, errors.New("tls: failed to find certificate PEM data in certificate input, but did find a private key; PEM inputs may have been switched")
}
return nil, fmt.Errorf("tls: failed to find \"CERTIFICATE\" PEM block in certificate input after skipping PEM blocks of the following types: %v", skippedBlockTypes)
}
return certs, nil
}
func getKey(keyPEMBlock []byte) (*pem.Block, error) {
var skippedBlockTypes []string
var keyDERBlock *pem.Block
for {
keyDERBlock, keyPEMBlock = pem.Decode(keyPEMBlock)
if keyDERBlock == nil {
if len(skippedBlockTypes) == 0 {
return nil, errors.New("tls: failed to find any PEM data in key input")
}
if len(skippedBlockTypes) == 1 && skippedBlockTypes[0] == "CERTIFICATE" {
return nil, errors.New("tls: found a certificate rather than a key in the PEM for the private key")
}
return nil, fmt.Errorf("tls: failed to find PEM block with type ending in \"PRIVATE KEY\" in key input after skipping PEM blocks of the following types: %v", skippedBlockTypes)
}
if keyDERBlock.Type == "PRIVATE KEY" || strings.HasSuffix(keyDERBlock.Type, " PRIVATE KEY") {
break
}
skippedBlockTypes = append(skippedBlockTypes, keyDERBlock.Type)
}
return keyDERBlock, nil
}
func matchKeyCert(keyDERBlock *pem.Block, certDERBlock []byte) (crypto.PrivateKey, error) {
// We don't need to parse the public key for TLS, but we so do anyway
// to check that it looks sane and matches the private key.
x509Cert, err := x509.ParseCertificate(certDERBlock)
if err != nil {
return nil, err
}
privateKey, err := parsePrivateKey(keyDERBlock.Bytes)
if err != nil {
return nil, err
}
switch pub := x509Cert.PublicKey.(type) {
case *sm2.PublicKey:
priv, ok := privateKey.(*sm2.PrivateKey)
if !ok {
return nil, errors.New("tls: private key type does not match public key type")
}
if pub.X.Cmp(priv.X) != 0 || pub.Y.Cmp(priv.Y) != 0 {
return nil, errors.New("tls: private key does not match public key")
}
default:
return nil, errors.New("tls: unknown public key algorithm")
}
return privateKey, nil
}
// X509KeyPair parses a public/private key pair from a pair of
// PEM encoded data. On successful return, Certificate.Leaf will be nil because
// the parsed form of the certificate is not retained.
func GMX509KeyPairs(certPEMBlock, keyPEMBlock, encCertPEMBlock, encKeyPEMBlock []byte) (Certificate, error) {
fail := func(err error) (Certificate, error) { return Certificate{}, err }
var certificate Certificate
signCerts, err := getCert(certPEMBlock)
if err != nil {
return certificate, err
}
if len(signCerts) == 0 {
return certificate, errors.New("tls: failed to find any sign cert PEM data in cert input")
}
certificate.Certificate = append(certificate.Certificate, signCerts[0])
encCerts, err := getCert(encCertPEMBlock)
if err != nil {
return certificate, err
}
if len(encCerts) == 0 {
return certificate, errors.New("tls: failed to find any enc cert PEM data in cert input")
}
certificate.Certificate = append(certificate.Certificate, encCerts[0])
keyDERBlock, err := getKey(keyPEMBlock)
if err != nil {
return certificate, err
}
certificate.PrivateKey, err = matchKeyCert(keyDERBlock, certificate.Certificate[0])
if err != nil {
return fail(err)
}
return certificate, nil
}
// one cert for enc and sign
func GMX509KeyPairsSingle(certPEMBlock, keyPEMBlock []byte) (Certificate, error) {
fail := func(err error) (Certificate, error) { return Certificate{}, err }
var certificate Certificate
certs, err := getCert(certPEMBlock)
if err != nil {
return certificate, err
}
if len(certs) == 0 {
return certificate, errors.New("tls: failed to find any sign cert PEM data in cert input")
}
checkCert, err := x509.ParseCertificate(certs[0])
if err != nil {
return certificate, errors.New("tls: failed to parse certificate")
}
//if cert is not for GM, use default X509KeyPair
if checkCert.PublicKeyAlgorithm != x509.SM2 {
return X509KeyPair(certPEMBlock, keyPEMBlock)
}
certificate.Certificate = append(certificate.Certificate, certs[0]) //this is for sign and env
keyDERBlock, err := getKey(keyPEMBlock)
if err != nil {
return certificate, err
}
certificate.PrivateKey, err = matchKeyCert(keyDERBlock, certificate.Certificate[0])
if err != nil {
return fail(err)
}
return certificate, nil
}