-
Notifications
You must be signed in to change notification settings - Fork 8
/
tlsutil.go
49 lines (42 loc) · 942 Bytes
/
tlsutil.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
package tlsutil
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
)
type TLSConfig struct {
Config *tls.Config
}
func NewTLSConfig() TLSConfig {
return TLSConfig{
Config: &tls.Config{
Certificates: []tls.Certificate{},
},
}
}
func (tc *TLSConfig) LoadX509KeyPair(cert_filepath, key_filepath string) error {
cert, err := tls.LoadX509KeyPair(cert_filepath, key_filepath)
if err != nil {
return err
}
tc.Config.Certificates = append(tc.Config.Certificates, cert)
return nil
}
func (tc *TLSConfig) LoadCACert(ca_cert_filepath string) error {
cert, err := ioutil.ReadFile(ca_cert_filepath)
if err != nil {
return err
}
if tc.Config.RootCAs == nil {
tc.Config.RootCAs = x509.NewCertPool()
}
ok := tc.Config.RootCAs.AppendCertsFromPEM(cert)
if !ok {
return fmt.Errorf("Cannot add Root CA cert %v", ca_cert_filepath)
}
return nil
}
func (tc *TLSConfig) Inflate() {
tc.Config.BuildNameToCertificate()
}