forked from tjfoc/gmsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websvr.go
183 lines (164 loc) · 4.69 KB
/
websvr.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
package websvr
import (
"crypto/tls"
x "crypto/x509"
"github.com/feifanfan/gmsm/gmtls"
"github.com/feifanfan/gmsm/x509"
"io/ioutil"
)
const (
rsaCertPath = "./certs/rsa_sign.cer"
rsaKeyPath = "./certs/rsa_sign_key.pem"
RSACaCertPath = "./certs/RSA_CA.cer"
RSAAuthCertPath = "./certs/rsa_auth_cert.cer"
RSAAuthKeyPath = "./certs/rsa_auth_key.pem"
SM2CaCertPath = "./certs/SM2_CA.cer"
SM2AuthCertPath = "./certs/sm2_auth_cert.cer"
SM2AuthKeyPath = "./certs/sm2_auth_key.pem"
sm2SignCertPath = "./certs/sm2_sign_cert.cer"
sm2SignKeyPath = "./certs/sm2_sign_key.pem"
sm2EncCertPath = "./certs/sm2_enc_cert.cer"
sm2EncKeyPath = "./certs/sm2_enc_key.pem"
)
// RSA配置
func loadRsaConfig() (*gmtls.Config, error) {
cert, err := gmtls.LoadX509KeyPair(rsaCertPath, rsaKeyPath)
if err != nil {
return nil, err
}
return &gmtls.Config{Certificates: []gmtls.Certificate{cert}}, nil
}
// SM2配置
func loadSM2Config() (*gmtls.Config, error) {
sigCert, err := gmtls.LoadX509KeyPair(sm2SignCertPath, sm2SignKeyPath)
if err != nil {
return nil, err
}
encCert, err := gmtls.LoadX509KeyPair(sm2EncCertPath, sm2EncKeyPath)
if err != nil {
return nil, err
}
return &gmtls.Config{
GMSupport: &gmtls.GMSupport{},
Certificates: []gmtls.Certificate{sigCert, encCert},
}, nil
}
// 切换GMSSL/TSL
func loadAutoSwitchConfig() (*gmtls.Config, error) {
rsaKeypair, err := gmtls.LoadX509KeyPair(rsaCertPath, rsaKeyPath)
if err != nil {
return nil, err
}
sigCert, err := gmtls.LoadX509KeyPair(sm2SignCertPath, sm2SignKeyPath)
if err != nil {
return nil, err
}
encCert, err := gmtls.LoadX509KeyPair(sm2EncCertPath, sm2EncKeyPath)
if err != nil {
return nil, err
}
return gmtls.NewBasicAutoSwitchConfig(&sigCert, &encCert, &rsaKeypair)
}
// 双向身份认证 服务端配置
func loadServerMutualTLCPAuthConfig() (*gmtls.Config, error) {
// 签名密钥对/证书 和 加密密钥对/证书
sigCert, err := gmtls.LoadX509KeyPair(sm2SignCertPath, sm2SignKeyPath)
if err != nil {
return nil, err
}
encCert, err := gmtls.LoadX509KeyPair(sm2EncCertPath, sm2EncKeyPath)
if err != nil {
return nil, err
}
// 信任的根证书
certPool := x509.NewCertPool()
cacert, err := ioutil.ReadFile(SM2CaCertPath)
if err != nil {
return nil, err
}
certPool.AppendCertsFromPEM(cacert)
return &gmtls.Config{
GMSupport: gmtls.NewGMSupport(),
Certificates: []gmtls.Certificate{sigCert, encCert},
ClientCAs: certPool,
ClientAuth: gmtls.RequireAndVerifyClientCert,
}, nil
}
// 要求客户端身份认证
func loadAutoSwitchConfigClientAuth() (*gmtls.Config, error) {
config, err := loadAutoSwitchConfig()
if err != nil {
return nil, err
}
// 设置需要客户端证书请求,标识需要进行客户端的身份认证
config.ClientAuth = gmtls.RequireAndVerifyClientCert
return config, nil
}
// 获取 客户端服务端双向身份认证 配置
func bothAuthConfig() (*gmtls.Config, error) {
// 信任的根证书
certPool := x509.NewCertPool()
cacert, err := ioutil.ReadFile(SM2CaCertPath)
if err != nil {
return nil, err
}
certPool.AppendCertsFromPEM(cacert)
authKeypair, err := gmtls.LoadX509KeyPair(SM2AuthCertPath, SM2AuthKeyPath)
if err != nil {
return nil, err
}
return &gmtls.Config{
GMSupport: &gmtls.GMSupport{},
RootCAs: certPool,
Certificates: []gmtls.Certificate{authKeypair},
InsecureSkipVerify: false,
}, nil
}
// 获取 单向身份认证(只认证服务端) 配置
func singleSideAuthConfig() (*gmtls.Config, error) {
// 信任的根证书
certPool := x509.NewCertPool()
cacert, err := ioutil.ReadFile(SM2CaCertPath)
if err != nil {
return nil, err
}
certPool.AppendCertsFromPEM(cacert)
return &gmtls.Config{
GMSupport: &gmtls.GMSupport{},
RootCAs: certPool,
}, nil
}
// 获取 客户端服务端双向身份认证 配置
func rsaBothAuthConfig() (*tls.Config, error) {
// 信任的根证书
certPool := x.NewCertPool()
cacert, err := ioutil.ReadFile(RSACaCertPath)
if err != nil {
return nil, err
}
certPool.AppendCertsFromPEM(cacert)
authKeypair, err := tls.LoadX509KeyPair(RSAAuthCertPath, RSAAuthKeyPath)
if err != nil {
return nil, err
}
return &tls.Config{
MaxVersion: tls.VersionTLS12,
RootCAs: certPool,
Certificates: []tls.Certificate{authKeypair},
InsecureSkipVerify: false,
}, nil
}
// 获取 单向身份认证(只认证服务端) 配置
func rsaSingleSideAuthConfig() (*tls.Config, error) {
// 信任的根证书
certPool := x.NewCertPool()
cacert, err := ioutil.ReadFile(RSACaCertPath)
if err != nil {
return nil, err
}
certPool.AppendCertsFromPEM(cacert)
return &tls.Config{
MaxVersion: tls.VersionTLS12,
RootCAs: certPool,
}, nil
}