This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tlsconfig.go
71 lines (68 loc) · 1.67 KB
/
tlsconfig.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
package proto
import (
"crypto"
"crypto/tls"
"crypto/x509"
"fmt"
)
func certPool(certs [][]byte) (*x509.CertPool, error) {
ret := x509.NewCertPool()
for _, der := range certs {
crt, err := x509.ParseCertificate(der)
if err != nil {
return nil, err
}
ret.AddCert(crt)
}
return ret, nil
}
func (m *TLSConfig) Config(getKey func(string) (crypto.PrivateKey, error)) (cfg *tls.Config, err error) {
if m == nil {
return nil, nil
}
cfg = new(tls.Config)
for _, t := range m.Certificates {
key, err := getKey(t.KeyID)
if err != nil {
return nil, err
}
cfg.Certificates = append(cfg.Certificates, tls.Certificate{
Certificate: t.Certificate,
PrivateKey: key,
OCSPStaple: t.OCSPStaple,
})
}
cfg.RootCAs, err = certPool(m.RootCAs)
if err != nil {
return nil, err
}
cfg.NextProtos = m.NextProtos
cfg.ServerName = m.ServerName
cfg.ClientAuth = tls.ClientAuthType(m.ClientAuth)
cfg.ClientCAs, err = certPool(m.ClientCAs)
if err != nil {
return nil, err
}
for _, cs := range m.CipherSuites {
cfg.CipherSuites = append(cfg.CipherSuites, uint16(cs))
}
cfg.PreferServerCipherSuites = m.PreferServerCipherSuites
cfg.SessionTicketsDisabled = !m.SessionTicketsEnabled
if m.SessionTicketKeyID != "" {
stk, err := getKey(m.SessionTicketKeyID)
if err != nil {
return nil, err
}
stk32, ok := stk.([32]byte)
if !ok {
return nil, fmt.Errorf("SessionTicketKey must be [32]byte, got %T (%v)", stk, stk)
}
cfg.SessionTicketKey = stk32
}
cfg.MinVersion = uint16(m.MinVersion)
cfg.MaxVersion = uint16(m.MaxVersion)
for _, cid := range m.CurvePreferences {
cfg.CurvePreferences = append(cfg.CurvePreferences, tls.CurveID(cid))
}
return cfg, nil
}