-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
91 lines (76 loc) · 2.64 KB
/
load.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
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package easytls
import (
"crypto/tls"
"crypto/x509"
"github.com/go-pogo/errors"
)
const (
ErrLoadCertificate = "failed to load certificate"
)
// TLSCertificateLoader loads a [tls.Certificate] from any source.
type TLSCertificateLoader interface {
LoadTLSCertificate() (*tls.Certificate, error)
}
// TLSCertificateLoaderFunc loads a [tls.Certificate] from any source.
type TLSCertificateLoaderFunc func() (*tls.Certificate, error)
func (fn TLSCertificateLoaderFunc) LoadTLSCertificate() (*tls.Certificate, error) { return fn() }
// LoadAndAppend loads the certificates from the provided
// [TLSCertificateLoader]s and appends them to the provided [tls.Certificate]
// slice.
// Errors that occur are grouped together and returned as a single error after
// all certificates have been loaded.
func LoadAndAppend(list []tls.Certificate, certs ...TLSCertificateLoader) ([]tls.Certificate, error) {
var err error
for _, c := range certs {
if c == nil {
continue
}
var loadErr error
if list, loadErr = loadAndAppend(list, c); loadErr != nil {
err = errors.Append(err, loadErr)
continue
}
}
return list, err
}
func loadAndAppend(list []tls.Certificate, c TLSCertificateLoader) ([]tls.Certificate, error) {
if cert, err := c.LoadTLSCertificate(); err != nil {
return list, errors.WithStack(err)
} else if cert != nil {
list = append(list, *cert)
}
return list, nil
}
// X509CertificateLoader loads a [x509.Certificate] from any source.
type X509CertificateLoader interface {
LoadX509Certificate() (*x509.Certificate, error)
}
// X509CertificateLoaderFunc loads a [x509.Certificate] from any source.
type X509CertificateLoaderFunc func() (*x509.Certificate, error)
func (fn X509CertificateLoaderFunc) LoadX509Certificate() (*x509.Certificate, error) { return fn() }
// LoadAndAdd loads the certificates from the provided [X509CertificateLoader]s
// and adds them to the provided [x509.CertPool].
// Errors that occur are grouped together and returned as a single error after
// all certificates have been loaded.
func LoadAndAdd(pool *x509.CertPool, certs ...X509CertificateLoader) error {
var err error
for _, cert := range certs {
c, loadErr := cert.LoadX509Certificate()
if loadErr != nil {
err = errors.Append(err, loadErr)
continue
}
pool.AddCert(c)
}
return err
}
func LoadAndAddSystem(certs ...X509CertificateLoader) (*x509.CertPool, error) {
pool, err := x509.SystemCertPool()
if err != nil {
return nil, errors.WithStack(err)
}
return pool, LoadAndAdd(pool, certs...)
}