-
Notifications
You must be signed in to change notification settings - Fork 162
/
tls_handshake.go
107 lines (96 loc) · 3.23 KB
/
tls_handshake.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
// Copyright 2020 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trust
import (
"context"
"crypto/tls"
"crypto/x509"
"time"
"github.com/scionproto/scion/go/lib/scrypto/cppki"
"github.com/scionproto/scion/go/lib/serrors"
)
const defaultTimeout = 5 * time.Second
// X509KeyPairLoader provides a certificate to be presented during TLS handshake.
type X509KeyPairLoader interface {
LoadX509KeyPair() (*tls.Certificate, error)
}
// TLSCryptoManager implements callbacks which will be called during TLS handshake.
type TLSCryptoManager struct {
Loader X509KeyPairLoader
DB DB
Timeout time.Duration
}
// NewTLSCryptoManager returns a new instance with the defaultTimeout.
func NewTLSCryptoManager(loader X509KeyPairLoader, db DB) *TLSCryptoManager {
return &TLSCryptoManager{
DB: db,
Loader: loader,
Timeout: defaultTimeout,
}
}
// GetCertificate retrieves a certificate to be presented during TLS handshake.
func (m *TLSCryptoManager) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
c, err := m.Loader.LoadX509KeyPair()
if err != nil {
return nil, serrors.WrapStr("loading server key pair", err)
}
return c, nil
}
// GetClientCertificate retrieves a client certificate to be presented during TLS handshake.
func (m *TLSCryptoManager) GetClientCertificate(_ *tls.CertificateRequestInfo) (*tls.Certificate,
error) {
c, err := m.Loader.LoadX509KeyPair()
if err != nil {
return nil, serrors.WrapStr("loading client key pair", err)
}
return c, nil
}
// VerifyPeerCertificate verifies the certificate presented by the peer during TLS handshake,
// based on the TRC.
func (m *TLSCryptoManager) VerifyPeerCertificate(rawCerts [][]byte,
_ [][]*x509.Certificate) error {
chain := make([]*x509.Certificate, len(rawCerts))
for i, asn1Data := range rawCerts {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
return serrors.WrapStr("parsing peer certificate", err)
}
chain[i] = cert
}
ia, err := cppki.ExtractIA(chain[0].Subject)
if err != nil {
return serrors.WrapStr("extracting ISD-AS from peer certificate", err)
} else if ia == nil {
return serrors.New("ISD-AS no present in peer certificate")
}
ctx, cancel := context.WithTimeout(context.Background(), m.Timeout)
defer cancel()
trcs, _, err := activeTRCs(ctx, m.DB, ia.I)
if err != nil {
return serrors.WrapStr("loading TRCs", err)
}
if err := verifyChain(chain, trcs); err != nil {
return serrors.WrapStr("verifying chains", err)
}
return nil
}
func verifyChain(chain []*x509.Certificate, trcs []cppki.SignedTRC) error {
var errs serrors.List
for _, trc := range trcs {
if err := cppki.VerifyChain(chain, cppki.VerifyOptions{TRC: &trc.TRC}); err != nil {
errs = append(errs, err)
}
}
return errs.ToError()
}