/
tlsclient.go
303 lines (282 loc) · 7.47 KB
/
tlsclient.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package tlsclient
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/cozy/cozy-stack/pkg/utils"
)
// HTTPEndpoint is a struct for specifying which parameters to use when
// connecting to a HTTP(S) endpoint
type HTTPEndpoint struct {
Host string
Port int
Timeout time.Duration
EnvPrefix string
RootCAFile string
ClientCertificateFiles ClientCertificateFilePair
PinnedKey string
InsecureSkipValidation bool
MaxIdleConnsPerHost int
DisableCompression bool
}
// ClientCertificateFilePair is a struct with a certificate and a key pair
type ClientCertificateFilePair struct {
KeyFile string
CertificateFile string
}
type tlsConfig struct {
clientCertificates []tls.Certificate
rootCAs []*x509.Certificate
pinnedKeys [][]byte
skipVerification bool
}
func generateURL(host string, port int) (*url.URL, error) {
u, err := url.Parse(host)
if err != nil {
return nil, err
}
if u.Scheme == "" {
u = &url.URL{
Scheme: "http",
Host: net.JoinHostPort(host, strconv.Itoa(port)),
}
}
return u, nil
}
// NewHTTPClient creates a http.Client and an url.URL for the given HTTP endpoint
func NewHTTPClient(opt HTTPEndpoint) (client *http.Client, u *url.URL, err error) {
if opt.Host != "" || opt.Port > 0 {
u, err = generateURL(opt.Host, opt.Port)
if err != nil {
return
}
}
c := &tlsConfig{}
if u != nil {
c, u, err = fromURL(c, u)
if err != nil {
return
}
}
if opt.EnvPrefix != "" {
c, err = fromEnv(c, opt.EnvPrefix)
if err != nil {
return
}
}
if opt.RootCAFile != "" {
if err = c.LoadRootCAFile(opt.RootCAFile); err != nil {
return
}
}
if opt.ClientCertificateFiles.CertificateFile != "" {
if err = c.LoadClientCertificateFile(
opt.ClientCertificateFiles.CertificateFile,
opt.ClientCertificateFiles.KeyFile,
); err != nil {
return
}
}
if opt.PinnedKey != "" {
if err = c.AddHexPinnedKey(opt.PinnedKey); err != nil {
return
}
}
if opt.InsecureSkipValidation {
c.SetInsecureSkipValidation()
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = c.Config()
if opt.MaxIdleConnsPerHost > 0 {
transport.MaxIdleConnsPerHost = opt.MaxIdleConnsPerHost
}
if opt.DisableCompression {
transport.DisableCompression = true
}
client = &http.Client{
Timeout: opt.Timeout,
Transport: transport,
}
return
}
func fromURL(c *tlsConfig, u *url.URL) (conf *tlsConfig, uCopy *url.URL, err error) {
uCopy = utils.CloneURL(u)
q := uCopy.Query()
if u.Scheme == "https" {
if rootCAFile := q.Get("ca"); rootCAFile != "" {
if err = c.LoadRootCAFile(rootCAFile); err != nil {
return
}
}
if certFile := q.Get("cert"); certFile != "" {
if keyFile := q.Get("key"); keyFile != "" {
if err = c.LoadClientCertificateFile(certFile, keyFile); err != nil {
return
}
}
}
if hexPinnedKey := q.Get("fp"); hexPinnedKey != "" {
if err = c.AddHexPinnedKey(hexPinnedKey); err != nil {
return
}
}
if t := q.Get("validate"); t == "0" || t == "false" || t == "FALSE" {
c.SetInsecureSkipValidation()
}
}
q.Del("ca")
q.Del("cert")
q.Del("key")
q.Del("fp")
q.Del("validate")
uCopy.RawQuery = q.Encode()
return c, uCopy, nil
}
func fromEnv(c *tlsConfig, envPrefix string) (conf *tlsConfig, err error) {
if rootCAFile := os.Getenv(envPrefix + "_CA"); rootCAFile != "" {
if err = c.LoadRootCAFile(rootCAFile); err != nil {
return
}
}
if certFile := os.Getenv(envPrefix + "_CERT"); certFile != "" {
if keyFile := os.Getenv(envPrefix + "_KEY"); keyFile != "" {
if err = c.LoadClientCertificateFile(certFile, keyFile); err != nil {
return
}
}
}
if hexPinnedKey := os.Getenv(envPrefix + "_FINGERPRINT"); hexPinnedKey != "" {
if err = c.AddHexPinnedKey(hexPinnedKey); err != nil {
return
}
}
if t := os.Getenv(envPrefix + "_VALIDATE"); t == "0" || t == "false" || t == "FALSE" {
c.SetInsecureSkipValidation()
}
return c, nil
}
func (s *tlsConfig) LoadClientCertificateFile(certFile, keyFile string) error {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return fmt.Errorf("tlsclient: could not load client certificate file: %s", err)
}
s.clientCertificates = append(s.clientCertificates, cert)
return nil
}
func (s *tlsConfig) LoadClientCertificate(certPEMBlock, keyPEMBlock []byte) error {
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return fmt.Errorf("tlsclient: could not load client certificate file: %s", err)
}
s.clientCertificates = append(s.clientCertificates, cert)
return nil
}
func (s *tlsConfig) LoadRootCA(rootCA []byte) error {
cert, err := x509.ParseCertificate(rootCA)
if err != nil {
return err
}
s.rootCAs = append(s.rootCAs, cert)
return nil
}
func (s *tlsConfig) LoadRootCAFile(rootCAFile string) error {
pemCerts, err := os.ReadFile(rootCAFile)
if err != nil {
return fmt.Errorf("tlsclient: could not load root CA file %q: %s", rootCAFile, err)
}
ok := false
for len(pemCerts) > 0 {
var block *pem.Block
block, pemCerts = pem.Decode(pemCerts)
if block == nil {
break
}
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
continue
}
if err = s.LoadRootCA(block.Bytes); err != nil {
continue
}
ok = true
}
if !ok {
return fmt.Errorf("tlsclient: could not load any certificate from the given ROOTCA file: %q", rootCAFile)
}
return nil
}
func (s *tlsConfig) SetInsecureSkipValidation() {
s.skipVerification = true
}
func (s *tlsConfig) AddHexPinnedKey(hexPinnedKey string) error {
pinnedKey, err := hex.DecodeString(hexPinnedKey)
if err != nil {
return fmt.Errorf("tlsclient: invalid hexadecimal fingerprint: %s", err)
}
expected := sha256.Size
given := len(pinnedKey)
if given != expected {
return fmt.Errorf("tlsclient: invalid fingerprint size for %s, expected %d got %d", hexPinnedKey,
expected, given)
}
s.pinnedKeys = append(s.pinnedKeys, pinnedKey)
return nil
}
func (s *tlsConfig) Config() *tls.Config {
conf := &tls.Config{}
conf.InsecureSkipVerify = s.skipVerification
if len(s.rootCAs) > 0 {
rootCAs := x509.NewCertPool()
for _, cert := range s.rootCAs {
rootCAs.AddCert(cert)
}
conf.RootCAs = rootCAs
}
if len(s.clientCertificates) > 0 {
conf.Certificates = make([]tls.Certificate, len(s.clientCertificates))
copy(conf.Certificates, s.clientCertificates)
}
if len(s.pinnedKeys) > 0 {
conf.VerifyPeerCertificate = verifyCertificatePinnedKey(s.pinnedKeys)
}
return conf
}
func verifyCertificatePinnedKey(pinnedKeys [][]byte) func(certs [][]byte, verifiedChains [][]*x509.Certificate) error {
return func(certs [][]byte, verifiedChains [][]*x509.Certificate) error {
// Check for leaf pinning first
for _, asn1 := range certs {
cert, err := x509.ParseCertificate(asn1)
if err != nil {
return err
}
fingerPrint := sha256.Sum256(cert.RawSubjectPublicKeyInfo)
for _, pinnedKey := range pinnedKeys {
if bytes.Equal(pinnedKey, fingerPrint[:]) {
return nil
}
}
}
// Then check for intermediate pinning
for _, verifiedChain := range verifiedChains {
if len(verifiedChain) > 0 {
verifiedCert := verifiedChain[0]
fingerPrint := sha256.Sum256(verifiedCert.RawSubjectPublicKeyInfo)
for _, pinnedKey := range pinnedKeys {
if bytes.Equal(pinnedKey, fingerPrint[:]) {
return nil
}
}
}
}
return fmt.Errorf("tlsclient: could not find the valid pinned key from proposed ones")
}
}