-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls.go
151 lines (132 loc) · 3.25 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
139
140
141
142
143
144
145
146
147
148
149
150
151
package minq
import (
"crypto"
"crypto/x509"
"fmt"
"log"
"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(ekr@rtfm.com): Provide a real config
config := mint.Config{
ServerName: c.ServerName,
NonBlocking: true,
NextProtos: []string{kQuicALPNToken},
SendSessionTickets: true,
AllowEarlyData: true,
}
if c.ForceHrr {
config.RequireCookie = true
}
config.CookieProtector, _ = mint.NewDefaultCookieProtector()
config.InsecureSkipVerify = true // TODO(ekr@rtfm.com): This is horribly insecure, but Minq is right now for testing
if c.CertificateChain != nil && c.Key != nil {
config.Certificates =
[]*mint.Certificate{
&mint.Certificate{
Chain: c.CertificateChain,
PrivateKey: c.Key,
},
}
} else {
priv, cert, err := mint.MakeNewSelfSignedCert(c.ServerName, mint.ECDSA_P256_SHA256)
if err != nil {
log.Fatalf("Couldn't make self-signed cert %v", err)
}
config.Certificates = []*mint.Certificate{
{
Chain: []*x509.Certificate{cert},
PrivateKey: priv,
},
}
}
config.Init(false)
c.mintConfig = &config
}
return c.mintConfig.Clone()
}
func NewTlsConfig(serverName string) TlsConfig {
return TlsConfig{
ServerName: serverName,
}
}
type tlsConn struct {
config *TlsConfig
conn *Connection
mintConfig *mint.Config
tls *mint.Conn
finished bool
cs *mint.CipherSuiteParams
}
func newTlsConn(conn *Connection, conf *TlsConfig, role Role) *tlsConn {
isClient := true
if role == RoleServer {
isClient = false
}
mc := conf.toMint()
mc.RecordLayer = newRecordLayerFactory(conn)
return &tlsConn{
conf,
conn,
mc,
mint.NewConn(nil, mc, isClient),
false,
nil,
}
}
func (c *tlsConn) setTransportParametersHandler(h *transportParametersHandler) {
c.mintConfig.ExtensionHandler = h
}
func (c *tlsConn) handshake() error {
outer:
for {
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.ConnectionState()
logf(logTypeTls, "TLS handshake complete")
logf(logTypeTls, "Negotiated ALPN = %v", st.NextProto)
// TODO(ekr@rtfm.com): 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 fmt.Errorf("TLS sent an alert %v", alert)
}
}
return nil
}
func (c *tlsConn) postHandshake() error {
b := make([]byte, 1)
n, err := c.tls.Read(b)
assert(n == 0) // This can't happen
if err == nil || err == mint.AlertWouldBlock {
return nil
}
return ErrorProtocolViolation
}
func (c *tlsConn) getHsState() string {
return c.tls.GetHsState().String()
}