-
Notifications
You must be signed in to change notification settings - Fork 4
/
tlsfactory.go
115 lines (109 loc) · 3.13 KB
/
tlsfactory.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 main
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"strconv"
"time"
"golang.org/x/sync/semaphore"
)
type TLSConnFactory struct {
addr string
tlsConfig *tls.Config
dialer *net.Dialer
sem *semaphore.Weighted
logger *CondLogger
}
func NewTLSConnFactory(host string, port uint16, timeout time.Duration,
certfile, keyfile string, cafile string, hostname_check bool,
tls_servername string, dialers uint, sessionCache tls.ClientSessionCache, logger *CondLogger) (*TLSConnFactory, error) {
if !hostname_check && cafile == "" {
return nil, errors.New("Hostname check should not be disabled in absence of custom CA file")
}
if certfile != "" && keyfile == "" || certfile == "" && keyfile != "" {
return nil, errors.New("Certificate file and key file must be specified only together")
}
var certs []tls.Certificate
if certfile != "" && keyfile != "" {
cert, err := tls.LoadX509KeyPair(certfile, keyfile)
if err != nil {
return nil, err
}
certs = append(certs, cert)
}
var roots *x509.CertPool
if cafile != "" {
roots = x509.NewCertPool()
certs, err := ioutil.ReadFile(cafile)
if err != nil {
return nil, err
}
if ok := roots.AppendCertsFromPEM(certs); !ok {
return nil, errors.New("Failed to load CA certificates")
}
}
servername := host
if tls_servername != "" {
servername = tls_servername
}
tlsConfig := tls.Config{
RootCAs: roots,
ServerName: servername,
Certificates: certs,
ClientSessionCache: sessionCache,
}
if !hostname_check {
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyPeerCertificate = func(certificates [][]byte, _ [][]*x509.Certificate) error {
certs := make([]*x509.Certificate, len(certificates))
for i, asn1Data := range certificates {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
return errors.New("tls: failed to parse certificate from server: " + err.Error())
}
certs[i] = cert
}
opts := x509.VerifyOptions{
Roots: roots, // On the server side, use config.ClientCAs.
DNSName: "", // No hostname check
Intermediates: x509.NewCertPool(),
}
for _, cert := range certs[1:] {
opts.Intermediates.AddCert(cert)
}
_, err := certs[0].Verify(opts)
return err
}
}
return &TLSConnFactory{
addr: net.JoinHostPort(host, strconv.Itoa(int(port))),
tlsConfig: &tlsConfig,
dialer: &net.Dialer{Timeout: timeout},
sem: semaphore.NewWeighted(int64(dialers)),
logger: logger,
}, nil
}
func (cf *TLSConnFactory) DialContext(ctx context.Context) (WrappedConn, error) {
if cf.sem.Acquire(ctx, 1) != nil {
return nil, errors.New("Context was cancelled")
}
defer cf.sem.Release(1)
netConn, err := cf.dialer.DialContext(ctx, "tcp", cf.addr)
if err != nil {
return nil, fmt.Errorf("cf.dialer.DialContext(ctx, \"tcp\", %q) failed: %v", err)
}
tlsConn := tls.Client(netConn, cf.tlsConfig)
err = tlsConn.HandshakeContext(ctx)
if err != nil {
netConn.Close()
return nil, fmt.Errorf("tlsConn.HandshakeContext(ctx) failed: %v", err)
}
return &wrappedConn{
conn: tlsConn,
logger: cf.logger,
}, nil
}