/
utils.go
111 lines (105 loc) · 3.37 KB
/
utils.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
package main
import (
"net"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"errors"
"time"
)
const DGRAM_BUF = int(^uint16(0)) + 1
const DGRAM_LEN_BYTES = 2
const RESOLVE_ATTEMPTS = 3
func makeServerTLSConfig(certfile, keyfile, cafile string) (*tls.Config, error) {
var cfg tls.Config
cert, err := tls.LoadX509KeyPair(certfile, keyfile)
if err != nil {
return nil, err
}
cfg.Certificates = []tls.Certificate{cert}
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")
}
cfg.ClientCAs = roots
cfg.ClientAuth = tls.VerifyClientCertIfGiven
}
return &cfg, nil
}
func makeClientTLSConfig(servername, certfile, keyfile, cafile string,
hostname_check bool) (*tls.Config, 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")
}
}
tlsConfig := tls.Config{
RootCAs: roots,
ServerName: servername,
Certificates: certs,
}
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 &tlsConfig, nil
}
func ProbeResolveTCP(address string, timeout time.Duration) (string, error) {
var (
conn net.Conn
err error
)
for i:=0; i < RESOLVE_ATTEMPTS; i++ {
conn, err = net.DialTimeout("tcp", address, timeout)
if err == nil {
resolved := conn.RemoteAddr().String()
conn.Close()
return resolved, nil
}
}
return "", err
}