-
Notifications
You must be signed in to change notification settings - Fork 55
/
helper.go
69 lines (59 loc) · 1.61 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
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
}
cert, err = tls.LoadX509KeyPair(certFile, keyFile)
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 LoadCertificatesFromEntries(certKeyFileEntries [][2]string) (certs []tls.Certificate, errs []error) {
certLen := len(certKeyFileEntries)
if certLen == 0 {
return
}
certs = make([]tls.Certificate, 0, certLen)
for i := 0; i < certLen; i++ {
cert, err := LoadCertificate(certKeyFileEntries[i][0], certKeyFileEntries[i][1])
if err != nil {
errs = append(errs, err)
} else {
certs = append(certs, cert)
}
}
return
}