-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintransport.go
462 lines (409 loc) · 14.3 KB
/
intransport.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package intransport
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
"golang.org/x/crypto/ocsp"
)
// StatusRequestExtension - status_request
const StatusRequestExtension = 5
var (
// ErrNoPeerCerts - this is returned when there are no peer certs presented.
ErrNoPeerCerts = errors.New("no peer certificates presented")
// ErrInvalidChainLength - this is returned when the chain length is less than 2 for a "chains" entry,
// IOW there must be at leat one peer cert in addition to the leaf.
ErrInvalidChainLength = errors.New("invalid chain length")
// ErrInvalidChainsLength - this is returned when the chains length is less than 1
ErrInvalidChainsLength = errors.New("invalid chains length")
// ErrOCSPNotStapled - this is returned when the OCSP Must Staple extension is present but a valid
// OCSP staple was not found.
ErrOCSPNotStapled = errors.New("certificate was marked with OCSP must-staple and no staple could be verified")
// ErrNoCertificates - this is returned in the unlikely event that no
// peer certificates are provided whatsoever. This should never be
// seen.
ErrNoCertificates = errors.New("no certificates supplied")
// MustStapleValue is the value in the MustStaple extension.
// DER encoding of []int{5}.
// https://tools.ietf.org/html/rfc6066#section-1.1
MustStapleValue, _ = asn1.Marshal([]int{StatusRequestExtension})
// MustStapleOID is the OID of the must staple.
// Must staple oid is id-pe-tlsfeature as defined here
// https://tools.ietf.org/html/rfc7633#section-6
MustStapleOID = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 24}
)
// peerCertViewer - this is a method type that is plugged into a tls.Config.VerifyPeerCertificate,
// or into our NextVerifyPeerCertificate.
type peerCertViewer func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error
// NewInTransportHTTPClient - generate an http client with sensible defaults.
// Optionally pass a *tls.Config that will be used as a basis for tls configuration.
func NewInTransportHTTPClient(tlsc *tls.Config) *http.Client {
return &http.Client{
Transport: NewInTransport(tlsc),
}
}
// NewInTransport - create a new http transport suitable for client connections.
// InTransport implements http.RoundTripper, and can be used like so:
//
// it := intransport.NewInTranport(nil)
// c := &http.Client{
// Transport: it,
// }
func NewInTransport(tlsc *tls.Config) *InTransport {
return NewInTransportFromTransport(nil, nil, tlsc)
}
// NewInTransportFromTransport - use t, dialer and tlsc as templates. Any can be nil and sane defaults
// will be used. If tlsc.VerifyPeerCertificate is specified, it will be called with the same semantics
// as before, but after we fetch intermediates and validate chains (if necessary). Similarly, if
// tlsc.VerifyConnection is specified, it will be called with the same semantics as before,
// but after we validate stapled ocsp.
func NewInTransportFromTransport(t *http.Transport, dialer *net.Dialer, tlsc *tls.Config) *InTransport {
return NewInTransportFromTransportWithCache(t, dialer, tlsc, nil)
}
// NewInTransportFromTransportWithCache - Same as NewInTransportFromTransport, with the option of specifying
// a cache implementation for fetched intermediates. If nil, the default cacher will use the map cache implementation.
func NewInTransportFromTransportWithCache(t *http.Transport, dialer *net.Dialer, tlsc *tls.Config, cache Cacher) *InTransport {
if dialer == nil {
dialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
}
if t == nil {
t = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
} else {
t = t.Clone()
if t.DialContext == nil {
t.DialContext = dialer.DialContext
}
}
if tlsc == nil {
tlsc = new(tls.Config)
} else {
tlsc = tlsc.Clone()
}
if cache == nil {
cache = NewMapCache()
}
it := &InTransport{
t: t,
NextVerifyPeerCertificate: tlsc.VerifyPeerCertificate,
NextVerifyConnection: tlsc.VerifyConnection,
dialer: dialer,
IntermediateHTTPClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 0,
}).DialContext,
// Since we cache responses, all http activity should be
// one-and-done.
DisableKeepAlives: true,
// This shouldn't be needed, since I don't believe
// the server url locations are ever tlsc enabled?
TLSHandshakeTimeout: 3 * time.Second,
// This also shouldn't be needed, but doesn't hurt anything
ExpectContinueTimeout: 1 * time.Second,
},
},
cache: cache,
}
it.tlsc = tlsc
t.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
h, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
conf := it.tlsc.Clone()
// Must configure InsecureSkipVerify to ensure that VerifyPeerCertificate
// is always called, which allows us to fetch missing intermediates.
conf.InsecureSkipVerify = true
conf.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return it.verifyPeerCertificate(h, rawCerts, verifiedChains)
}
// As of go 1.15, we can now validate OCSP during handshake.
conf.VerifyConnection = func(state tls.ConnectionState) error {
err := it.validateOCSP(h, &state)
if err != nil {
return err
}
if it.NextVerifyConnection != nil {
return it.NextVerifyConnection(state)
}
return nil
}
d := tls.Dialer{
NetDialer: it.dialer,
Config: conf,
}
return d.DialContext(ctx, network, addr)
}
return it
}
// InTransport - this implements an http.RoundTripper and handles the fetching
// of missing intermediate certificates, and verifying OCSP stapling, and
// in the event there is a "must staple" set on the certificate it will fail on
// missing staple.
type InTransport struct {
// Specify this method in the situation where you might otherwise have wanted to
// install your own VerifyPeerCertificate hook into tls.Config. If specified,
// This method will be called after a successful InTransport verification,
// and verifiedChains will contain appropriate data including any intermediates
// that needed to be downloaded. This is also set when a VerifyPeerCertificate
// method is present on the passed tls.Config to one of the New methods above.
NextVerifyPeerCertificate peerCertViewer
// NextVerifyConnection is similar to NextVerityPeerCertificate, but for the VerifyConnection instead.
// Similarly to above, this is automatically set if VerifyConnection is set on
// tls.Config passed to any of the New methods above.
NextVerifyConnection func(cs tls.ConnectionState) error
// IntermediateHTTPClient is an http client used for fetching intermediate certs
// that are missing. The default created with the New* methods is probably
// sane for almost all use cases, but exporting this just in case. If
// you need to set this, do it before you start using InTransport as an
// http.Roundtripper.
IntermediateHTTPClient *http.Client
t *http.Transport
tlsc *tls.Config
dialer *net.Dialer
cache Cacher
}
// RoundTrip - implement http.RoundTripper interface
func (it *InTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return it.t.RoundTrip(req)
}
func (it *InTransport) validateOCSP(serverName string, connState *tls.ConnectionState) error {
peers := connState.PeerCertificates
if len(peers) == 0 {
return ErrNoPeerCerts
}
crt := peers[0]
mustStaple := false
for _, ext := range crt.Extensions {
if ext.Id.Equal(MustStapleOID) {
if bytes.Equal(ext.Value, MustStapleValue) {
mustStaple = true
} else {
// technically the value is a DER encoded SEQUENCE OF INTEGER,
// so see if there is more than one integer specified. doubt
// this will be seen in the wild, currently there is only one
// defined value per RFC. but hey, due diligence. all that.
var tlsExts []int
_, err := asn1.Unmarshal(ext.Value, &tlsExts)
if err != nil {
return fmt.Errorf("malformed must staple extension: %w", err)
}
for _, tlsExt := range tlsExts {
if tlsExt == StatusRequestExtension {
mustStaple = true
break
}
}
}
break
}
}
validatedStaple := false
if connState.OCSPResponse != nil {
// Validate the staple if present
// Let's grab the chain
chains, err := it.verifyChains(serverName, connState.PeerCertificates)
if err != nil {
return err
}
var chain []*x509.Certificate
if len(chains) < 1 {
err = ErrInvalidChainsLength
} else {
chain = chains[0]
if len(chain) < 2 {
err = ErrInvalidChainLength
}
}
if err != nil {
return err
}
ocspResp, err := ocsp.ParseResponseForCert(connState.OCSPResponse, crt, chain[1])
if err != nil {
return err
}
if ocspResp.Status != ocsp.Good {
return fmt.Errorf("invalid ocsp validation: %s", ocsp.ResponseStatus(ocspResp.Status).String())
}
if ocspResp.NextUpdate.After(time.Now()) {
// for now, don't fail on an expired staple unless must staple is specified.
// maybe revisit this
validatedStaple = true
}
}
if mustStaple && !validatedStaple {
return ErrOCSPNotStapled
}
return nil
}
// verifyPeerCertificate - The difference between this
// and the default tlsc verification is that missing intermediates will be
// fetched until either a valid path to a trusted root is found or no further
// intermediates can be found. If a chain cannot be established, the
// connection will fail . If a chain can be established, then the optional
// NextVerifyPeerCertificate() method will be called, if specified. If this
// method returns an error, it will stop the connection.
func (it *InTransport) verifyPeerCertificate(serverName string, rawCerts [][]byte, _ [][]*x509.Certificate) error {
if len(rawCerts) == 0 {
return ErrNoCertificates
}
PeerCertificates := make([]*x509.Certificate, 0, len(rawCerts))
for _, raw := range rawCerts {
cert, err := x509.ParseCertificate(raw)
if err != nil {
return fmt.Errorf("intransport: error parsing certificate: %w", err)
}
PeerCertificates = append(PeerCertificates, cert)
}
var err error
var verifiedChains [][]*x509.Certificate
verifiedChains, err = it.verifyChains(serverName, PeerCertificates)
if err != nil {
return fmt.Errorf("intransport: validation error: %w", err)
}
if it.NextVerifyPeerCertificate != nil {
err = it.NextVerifyPeerCertificate(rawCerts, verifiedChains)
if err != nil {
err = fmt.Errorf("intransport: NextVerifyPeerCertificate() failed: %w", err)
}
}
return err
}
// verifyChains - this takes cert(s) and does its best to find a path to a recognized root,
// fetching intermediate certs that may be missing.
func (it *InTransport) verifyChains(serverName string, certs []*x509.Certificate) (chains [][]*x509.Certificate, err error) {
cp := x509.NewCertPool()
if len(certs) > 1 {
for _, cert := range certs[1:] {
cp.AddCert(cert)
}
}
// Validate hostname first, because chains are comparatively expensive. Also the Verify
// below won't fail on an empty serverName unless we explicitly check it here.
if err := certs[0].VerifyHostname(serverName); err != nil {
return nil, err
}
// Now check the chains.
chains, err = certs[0].Verify(x509.VerifyOptions{
Roots: it.tlsc.RootCAs,
Intermediates: cp,
DNSName: serverName,
})
if err != nil {
// This will be a chain failure. Try to fetch intermediates now.
var fetched []*x509.Certificate
fetched, err = it.buildMissingChain(certs[len(certs)-1])
if err != nil {
return nil, fmt.Errorf("failed to find chain: %w", err)
}
for _, cert := range fetched {
cp.AddCert(cert)
}
chains, err = certs[0].Verify(x509.VerifyOptions{
Roots: it.tlsc.RootCAs,
Intermediates: cp,
DNSName: serverName,
})
if err != nil {
return nil, fmt.Errorf("chain failed verification after fetch: %w", err)
}
}
return
}
// This attempts to build the missing links of the chain, and returns any intermediates it may have fetched.
func (it *InTransport) buildMissingChain(cert *x509.Certificate) ([]*x509.Certificate, error) {
tmpCert := cert
var retval []*x509.Certificate
var lastError error
for i := 0; i < 5; i++ {
_, lastError = tmpCert.Verify(x509.VerifyOptions{
Roots: it.tlsc.RootCAs,
// We don't care about dns names here
})
if lastError == nil {
break
}
var err error
tmpCert, err = it.fetchIssuingCert(tmpCert)
if err != nil {
return nil, err
}
retval = append(retval, tmpCert)
}
if lastError != nil {
return nil, lastError
}
return retval, nil
}
// This grabs the issuing cert from the issuing certificate extension.
func (it *InTransport) fetchIssuingCert(cert *x509.Certificate) (*x509.Certificate, error) {
// this attempts to do two things:
// 1) avoid stampede problem - minimizes fetches of a cert on cache miss
// 2) avoid long locks on the outer map.
if len(cert.IssuingCertificateURL) == 0 {
return nil, fmt.Errorf("cert has empty IssuingCertificateURL: %s",
cert.Subject.CommonName)
}
var mapKey string
if len(cert.AuthorityKeyId) > 0 {
enc := base64.RawStdEncoding.EncodeToString(cert.AuthorityKeyId)
mapKey = cert.Issuer.CommonName + ":" + enc
} else {
mapKey = cert.Issuer.CommonName
}
cce := it.cache.LockedCachedCert(mapKey)
var crt *x509.Certificate
if crt = cce.Cert(); crt != nil && crt.NotAfter.After(time.Now()) {
cce.Unlock()
return crt, nil
}
defer cce.Unlock()
// I've yet to see more than one IssuingCertificateURL,
// but just in case...
var err error
var fetchedCert *x509.Certificate
for _, urlString := range cert.IssuingCertificateURL {
var resp *http.Response
resp, err = it.IntermediateHTTPClient.Get(urlString)
if err != nil {
continue
}
var raw []byte
raw, err = ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
continue
}
fetchedCert, err = x509.ParseCertificate(raw)
if err != nil {
continue
}
cce.SetCert(fetchedCert)
break
}
if err != nil {
return nil, fmt.Errorf("failed to fetch issuing certificate: %w", err)
}
return fetchedCert, nil
}