forked from mkieweg/minq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
138 lines (118 loc) · 2.95 KB
/
tls.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
package minq
import (
"crypto"
"crypto/x509"
"encoding/hex"
"fmt"
"github.com/bifurcation/mint"
)
type TlsConfig struct {
ServerName string
CertificateChain []*x509.Certificate
Key crypto.Signer
mintConfig *mint.Config
ForceHrr bool
}
func (c *TlsConfig) init() {
_ = c.toMint()
}
func (c *TlsConfig) toMint() *mint.Config {
if c.mintConfig == nil {
// TODO([email protected]): Provide a real config
config := mint.Config{
ServerName: c.ServerName,
NonBlocking: true,
NextProtos: []string{kQuicALPNToken},
SendSessionTickets: true,
}
if c.ForceHrr {
config.RequireCookie = true
}
config.CookieProtector, _ = mint.NewDefaultCookieProtector()
if c.CertificateChain != nil && c.Key != nil {
config.Certificates =
[]*mint.Certificate{
&mint.Certificate{
Chain: c.CertificateChain,
PrivateKey: c.Key,
},
}
}
config.Init(false)
c.mintConfig = &config
}
return c.mintConfig
}
func NewTlsConfig(serverName string) TlsConfig {
return TlsConfig{
ServerName: serverName,
}
}
type tlsConn struct {
conn *connBuffer
tls *mint.Conn
finished bool
cs *mint.CipherSuiteParams
}
func newTlsConn(conf TlsConfig, role uint8) *tlsConn {
isClient := true
if role == RoleServer {
isClient = false
}
c := newConnBuffer()
return &tlsConn{
c,
mint.NewConn(c, conf.toMint(), isClient),
false,
nil,
}
}
func (c *tlsConn) setTransportParametersHandler(h *transportParametersHandler) {
c.tls.SetExtensionHandler(h)
}
func (c *tlsConn) handshake(input []byte) ([]byte, error) {
logf(logTypeTls, "TLS handshake input len=%v", len(input))
logf(logTypeTrace, "TLS handshake input = %v", hex.EncodeToString(input))
if input != nil {
err := c.conn.input(input)
if err != nil {
return nil, err
}
}
outer:
for {
logf(logTypeTls, "Calling Mint handshake")
alert := c.tls.Handshake()
hst := c.tls.GetHsState()
switch alert {
case mint.AlertNoAlert, mint.AlertStatelessRetry:
if hst == mint.StateServerConnected || hst == mint.StateClientConnected {
st := c.tls.State()
logf(logTypeTls, "TLS handshake complete")
logf(logTypeTls, "Negotiated ALPN = %v", st.NextProto)
// TODO([email protected]): Abort on ALPN mismatch when others do.
if st.NextProto != kQuicALPNToken {
logf(logTypeTls, "ALPN mismatch %v != %v", st.NextProto, kQuicALPNToken)
}
cs := st.CipherSuite
c.cs = &cs
c.finished = true
break outer
}
// Loop
case mint.AlertWouldBlock:
logf(logTypeTls, "TLS would have blocked")
break outer
default:
return nil, fmt.Errorf("TLS sent an alert %v", alert)
}
}
logf(logTypeTls, "TLS wrote %d bytes", c.conn.OutputLen())
return c.conn.getOutput(), nil
}
func (c *tlsConn) computeExporter(label string) ([]byte, error) {
return c.tls.ComputeExporter(label, []byte{}, c.cs.Hash.Size())
}
func (c *tlsConn) getHsState() string {
return c.tls.GetHsState().String()
}