-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.go
178 lines (154 loc) · 3.91 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
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
package minq
import (
"crypto"
"crypto/x509"
"encoding/hex"
"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([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()
config.InsecureSkipVerify = true // TODO([email protected]): 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
}
func NewTlsConfig(serverName string) TlsConfig {
return TlsConfig{
ServerName: serverName,
}
}
type tlsConn struct {
config *TlsConfig
conn *connBuffer
tls *mint.Conn
finished bool
cs *mint.CipherSuiteParams
}
func newTlsConn(conf *TlsConfig, role Role) *tlsConn {
isClient := true
if role == RoleServer {
isClient = false
}
c := newConnBuffer()
conf2 := *conf
return &tlsConn{
&conf2,
c,
mint.NewConn(c, conf2.toMint(), isClient),
false,
nil,
}
}
func (c *tlsConn) setTransportParametersHandler(h *transportParametersHandler) {
c.config.mintConfig.ExtensionHandler = 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.ConnectionState()
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) readPostHandshake(input []byte) error {
// TODO([email protected]): Fix this
/*
logf(logTypeTls, "TLS post-handshake input len=%v", len(input))
if input != nil {
err := c.conn.input(input)
if err != nil {
return err
}
}
buf := make([]byte, 1)
n, err := c.tls.Read(buf)
if n != 0 {
return fmt.Errorf("Received TLS application data")
}
if err != mint.AlertWouldBlock || err == mint.WouldBlock {
return err
}*/
return 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()
}