-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.go
115 lines (88 loc) · 2.69 KB
/
tls.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package archercl
import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
)
const (
_DEFAULT_SERVERNAME = "archer"
_CFG_CERT = "cert"
_CFG_KEY = "key"
_CFG_ROOTCAS = "rootCAs"
)
type TLSConfigOptions struct {
CertFilename string
KeyFilename string
RootCAsFilename string
}
func createDefaultTLSConfig() *tls.Config {
c := &tls.Config{
// Force use of the most recent version
MinVersion: tls.VersionTLS12,
Certificates: make([]tls.Certificate, 1),
// We are always talking to / being a server named simply "archer". Effectively
// we aren't using host name verification functionality from TLS
ServerName: _DEFAULT_SERVERNAME,
RootCAs: x509.NewCertPool(),
ClientAuth: tls.RequireAndVerifyClientCert,
// Removing the RC4 and 3DES cipher suites from the defaults
// ^^^ good move
CipherSuites: []uint16{
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
},
}
// Use the same CAs to verify the clients as we do for verifying servers since we
// are a closed system
c.ClientCAs = c.RootCAs
return c
}
func CreateTLSConfig(o *TLSConfigOptions) (c *tls.Config, err error) {
if o == nil {
return nil, errors.New("No TLSConfigOptions value was provided")
}
if len(o.CertFilename) == 0 {
return nil, errors.New("No cert value was found with a filename for a certificate .pem file")
}
if len(o.KeyFilename) == 0 {
return nil, errors.New("No key value was found with a filename for a key .pem file")
}
if len(o.RootCAsFilename) == 0 {
return nil, errors.New("No rootCAs value was found with a filename for a root CA chain .pem file")
}
c = createDefaultTLSConfig()
c.Certificates[0], err = tls.LoadX509KeyPair(o.CertFilename, o.KeyFilename)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(o.RootCAsFilename)
if err != nil {
return nil, err
}
if !c.RootCAs.AppendCertsFromPEM(data) {
return nil, errors.New("Unable to parse root CAs from " + o.RootCAsFilename)
}
return c, nil
}
func (self *AclNode) TLSConfigOptions() (*TLSConfigOptions, error) {
o := &TLSConfigOptions{
CertFilename: self.ChildAsString(_CFG_CERT),
KeyFilename: self.ChildAsString(_CFG_KEY),
RootCAsFilename: self.ChildAsString(_CFG_ROOTCAS),
}
return o, nil
}
func (self *AclNode) TLSConfig() (c *tls.Config, err error) {
tlsConfigOptions, err := self.TLSConfigOptions()
if err != nil {
return nil, err
}
return CreateTLSConfig(tlsConfigOptions)
}