-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
87 lines (76 loc) · 2.07 KB
/
helper.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
package goVirtualHost
import (
"crypto/tls"
"errors"
)
var MissingCertFileAndKeyFile = errors.New("missing certificate file and key file")
var MissingCertFile = errors.New("missing certificate file")
var MissingKeyFile = errors.New("missing key file")
var CertKeyFileCountNotMatch = errors.New("certificate file count and key file count not match")
func LoadCertificate(certFile, keyFile string) (cert *tls.Certificate, err error) {
if len(certFile) == 0 && len(keyFile) == 0 {
err = MissingCertFileAndKeyFile
return
} else if len(certFile) == 0 {
err = MissingCertFile
return
} else if len(keyFile) == 0 {
err = MissingKeyFile
return
}
var tlsCert tls.Certificate
tlsCert, err = tls.LoadX509KeyPair(certFile, keyFile)
cert = &tlsCert
return
}
func LoadCertificates(certFiles, keyFiles []string) (certs []*tls.Certificate, errs []error) {
certLen := len(certFiles)
if certLen != len(keyFiles) {
errs = append(errs, CertKeyFileCountNotMatch)
return
}
if certLen == 0 {
return
}
certs = make([]*tls.Certificate, 0, certLen)
for i := 0; i < certLen; i++ {
cert, err := LoadCertificate(certFiles[i], keyFiles[i])
if err != nil {
errs = append(errs, err)
} else {
certs = append(certs, cert)
}
}
return
}
func LoadCertificatesFromPairs(certKeyFilePairs [][2]string) (certs []*tls.Certificate, errs []error) {
certLen := len(certKeyFilePairs)
if certLen == 0 {
return
}
certs = make([]*tls.Certificate, 0, certLen)
for i := 0; i < certLen; i++ {
cert, err := LoadCertificate(certKeyFilePairs[i][0], certKeyFilePairs[i][1])
if err != nil {
errs = append(errs, err)
} else {
certs = append(certs, cert)
}
}
return
}
func CertsKeysToPairs(certFiles, keyFiles []string) (certKeyFilePairs [][2]string, errs []error) {
certLen := len(certFiles)
if certLen != len(keyFiles) {
errs = []error{CertKeyFileCountNotMatch}
return
}
if certLen == 0 {
return
}
certKeyFilePairs = make([][2]string, 0, certLen)
for i := 0; i < certLen; i++ {
certKeyFilePairs = append(certKeyFilePairs, [2]string{certFiles[i], keyFiles[i]})
}
return
}