|
1 | 1 | package goVirtualHost |
2 | 2 |
|
3 | | -import "crypto/tls" |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "errors" |
| 6 | +) |
4 | 7 |
|
5 | | -func LoadCertificate(certFile, keyFile string) (*tls.Certificate, error) { |
6 | | - cert, err := tls.LoadX509KeyPair(certFile, keyFile) |
7 | | - if err != nil { |
8 | | - return nil, err |
| 8 | +var MissingCertFileAndKeyFile = errors.New("missing certificate file and key file") |
| 9 | +var MissingCertFile = errors.New("missing certificate file") |
| 10 | +var MissingKeyFile = errors.New("missing key file") |
| 11 | +var CertKeyFileCountNotMatch = errors.New("certificate file count and key file count not match") |
| 12 | + |
| 13 | +func LoadCertificate(certFile, keyFile string) (cert tls.Certificate, err error) { |
| 14 | + if len(certFile) == 0 && len(keyFile) == 0 { |
| 15 | + err = MissingCertFileAndKeyFile |
| 16 | + return |
| 17 | + } else if len(certFile) == 0 { |
| 18 | + err = MissingCertFile |
| 19 | + return |
| 20 | + } else if len(keyFile) == 0 { |
| 21 | + err = MissingKeyFile |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + cert, err = tls.LoadX509KeyPair(certFile, keyFile) |
| 26 | + return |
| 27 | +} |
| 28 | + |
| 29 | +func LoadCertificates(certFiles, keyFiles []string) (certs []tls.Certificate, errs []error) { |
| 30 | + certLen := len(certFiles) |
| 31 | + if certLen != len(keyFiles) { |
| 32 | + errs = append(errs, CertKeyFileCountNotMatch) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if certLen == 0 { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + certs = make([]tls.Certificate, 0, certLen) |
| 41 | + for i := 0; i < certLen; i++ { |
| 42 | + cert, err := LoadCertificate(certFiles[i], keyFiles[i]) |
| 43 | + if err != nil { |
| 44 | + errs = append(errs, err) |
| 45 | + } else { |
| 46 | + certs = append(certs, cert) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return |
| 51 | +} |
| 52 | + |
| 53 | +func LoadCertificatesFromEntries(certKeyFileEntries [][2]string) (certs []tls.Certificate, errs []error) { |
| 54 | + certLen := len(certKeyFileEntries) |
| 55 | + if certLen == 0 { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + certs = make([]tls.Certificate, 0, certLen) |
| 60 | + for i := 0; i < certLen; i++ { |
| 61 | + cert, err := LoadCertificate(certKeyFileEntries[i][0], certKeyFileEntries[i][1]) |
| 62 | + if err != nil { |
| 63 | + errs = append(errs, err) |
| 64 | + } else { |
| 65 | + certs = append(certs, cert) |
| 66 | + } |
9 | 67 | } |
10 | | - return &cert, nil |
| 68 | + return |
11 | 69 | } |
0 commit comments