-
Notifications
You must be signed in to change notification settings - Fork 182
/
ssl.go
262 lines (223 loc) · 5.94 KB
/
ssl.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
package teaconfigs
import (
"crypto/tls"
"crypto/x509"
"errors"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/types"
"io/ioutil"
"net"
"regexp"
"strconv"
"strings"
)
// TLS Version
type TLSVersion = string
// Cipher Suites
type TLSCipherSuite = string
// SSL配置
type SSLConfig struct {
On bool `yaml:"on" json:"on"` // 是否开启
Certificate string `yaml:"certificate" json:"certificate"` // 证书文件, deprecated in v0.1.4
CertificateKey string `yaml:"certificateKey" json:"certificateKey"` // 密钥, deprecated in v0.1.4
Certs []*SSLCertConfig `yaml:"certs" json:"certs"`
CertTasks []*SSLCertTask `yaml:"certTasks" json:"certTasks"`
ClientAuthType SSLClientAuthType `yaml:"clientAuthType" json:"clientAuthType"` // 客户端认证类型
ClientCACertIds []string `yaml:"clientCACertIds" json:"clientCACertIds"` // 客户端认证CA
Listen []string `yaml:"listen" json:"listen"` // 网络地址
MinVersion TLSVersion `yaml:"minVersion" json:"minVersion"` // 支持的最小版本
CipherSuites []TLSCipherSuite `yaml:"cipherSuites" json:"cipherSuites"` // 加密算法套件
HSTS *HSTSConfig `yaml:"hsts2" json:"hsts"` // HSTS配置,yaml之所以使用hsts2,是因为要和以前的版本分开
HTTP2Disabled bool `yaml:"http2Disabled" json:"http2Disabled"` // 是否禁用HTTP2
nameMapping map[string]*tls.Certificate // dnsName => cert
minVersion uint16
cipherSuites []uint16
clientCAPool *x509.CertPool
}
// 获取新对象
func NewSSLConfig() *SSLConfig {
return &SSLConfig{}
}
// 校验配置
func (this *SSLConfig) Validate() error {
if !this.On {
return nil
}
if len(this.Certs) == 0 {
if len(this.Certificate) > 0 && len(this.CertificateKey) > 0 {
newCert := NewSSLCertConfig(this.Certificate, this.CertificateKey)
newCert.Id = "old_version_cert"
this.Certs = append(this.Certs, newCert)
}
}
if len(this.Certs) == 0 {
return errors.New("no certificates in https config")
}
for _, cert := range this.Certs {
err := cert.Validate()
if err != nil {
return err
}
}
if this.Listen == nil {
this.Listen = []string{}
} else {
for index, addr := range this.Listen {
_, _, err := net.SplitHostPort(addr)
if err != nil {
this.Listen[index] = strings.TrimSuffix(addr, ":") + ":443"
}
}
}
// min version
this.convertMinVersion()
// cipher suite categories
this.initCipherSuites()
// hsts
if this.HSTS != nil {
err := this.HSTS.Validate()
if err != nil {
return err
}
}
// CA证书
if len(this.ClientCACertIds) > 0 && this.ClientAuthType != SSLClientAuthTypeNoClientCert {
this.clientCAPool = x509.NewCertPool()
list := SharedSSLCertList()
for _, certId := range this.ClientCACertIds {
cert := list.FindCert(certId)
if cert == nil {
continue
}
if !cert.On {
continue
}
data, err := ioutil.ReadFile(cert.FullCertPath())
if err != nil {
return err
}
this.clientCAPool.AppendCertsFromPEM(data)
}
}
return nil
}
// 取得最小版本
func (this *SSLConfig) TLSMinVersion() uint16 {
return this.minVersion
}
// 套件
func (this *SSLConfig) TLSCipherSuites() []uint16 {
return this.cipherSuites
}
// 校验是否匹配某个域名
func (this *SSLConfig) MatchDomain(domain string) (cert *tls.Certificate, ok bool) {
for _, cert := range this.Certs {
if cert.MatchDomain(domain) {
return cert.CertObject(), true
}
}
return nil, false
}
// 取得第一个证书
func (this *SSLConfig) FirstCert() *tls.Certificate {
for _, cert := range this.Certs {
return cert.CertObject()
}
return nil
}
// 是否包含某个证书或密钥路径
func (this *SSLConfig) ContainsFile(file string) bool {
for _, cert := range this.Certs {
if cert.CertFile == file || cert.KeyFile == file {
return true
}
}
return false
}
// 删除证书文件
func (this *SSLConfig) DeleteFiles() error {
var resultErr error = nil
if len(this.Certificate) > 0 {
err := files.NewFile(Tea.ConfigFile(this.Certificate)).Delete()
if err != nil {
resultErr = err
}
}
if len(this.CertificateKey) > 0 {
err := files.NewFile(Tea.ConfigFile(this.CertificateKey)).Delete()
if err != nil {
resultErr = err
}
}
for _, cert := range this.Certs {
err := cert.DeleteFiles()
if err != nil {
resultErr = err
}
}
return resultErr
}
// 查找单个证书配置
func (this *SSLConfig) FindCert(certId string) *SSLCertConfig {
for _, cert := range this.Certs {
if cert.Id == certId {
return cert
}
}
return nil
}
// 添加证书
func (this *SSLConfig) AddCert(cert *SSLCertConfig) {
this.Certs = append(this.Certs, cert)
}
// 添加证书任务
func (this *SSLConfig) AddCertTask(certTask *SSLCertTask) {
this.CertTasks = append(this.CertTasks, certTask)
}
// 删除证书任务
func (this *SSLConfig) RemoveCertTask(certTaskId string) {
result := []*SSLCertTask{}
for _, task := range this.CertTasks {
if task.Id == certTaskId {
continue
}
result = append(result, task)
}
this.CertTasks = result
}
// 查找证书任务
func (this *SSLConfig) FindCertTask(certTaskId string) *SSLCertTask {
for _, task := range this.CertTasks {
if task.Id == certTaskId {
return task
}
}
return nil
}
// CA证书Pool,用于TLS对客户端进行认证
func (this *SSLConfig) CAPool() *x509.CertPool {
return this.clientCAPool
}
// 分解所有监听地址
func (this *SSLConfig) ParseListenAddresses() []string {
result := []string{}
var reg = regexp.MustCompile(`\[\s*(\d+)\s*[,:-]\s*(\d+)\s*]$`)
for _, addr := range this.Listen {
match := reg.FindStringSubmatch(addr)
if len(match) == 0 {
result = append(result, addr)
} else {
min := types.Int(match[1])
max := types.Int(match[2])
if min > max {
min, max = max, min
}
for i := min; i <= max; i++ {
newAddr := reg.ReplaceAllString(addr, ":"+strconv.Itoa(i))
result = append(result, newAddr)
}
}
}
return result
}