-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_handlers.go
374 lines (338 loc) · 12.1 KB
/
client_handlers.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
package dtls
import (
"bytes"
"fmt"
)
func clientHandshakeHandler(c *Conn) error {
handleSingleHandshake := func(buf []byte) error {
rawHandshake := &handshake{}
if err := rawHandshake.Unmarshal(buf); err != nil {
return err
}
c.log.Tracef("[handshake] <- %s", rawHandshake.handshakeMessage.handshakeType().String())
switch h := rawHandshake.handshakeMessage.(type) {
case *handshakeMessageHelloVerifyRequest:
c.cookie = append([]byte{}, h.cookie...)
case *handshakeMessageServerHello:
for _, extension := range h.extensions {
if e, ok := extension.(*extensionUseSRTP); ok {
profile, ok := findMatchingSRTPProfile(e.protectionProfiles, c.localSRTPProtectionProfiles)
if !ok {
return fmt.Errorf("Server responded with SRTP Profile we do not support")
}
c.state.srtpProtectionProfile = profile
}
}
if len(c.localSRTPProtectionProfiles) > 0 && c.state.srtpProtectionProfile == 0 {
return fmt.Errorf("SRTP support was requested but server did not respond with use_srtp extension")
}
c.state.cipherSuite = h.cipherSuite
c.state.remoteRandom = h.random
c.log.Tracef("[handshake] use cipher suite: %s", h.cipherSuite.String())
case *handshakeMessageCertificate:
c.state.remoteCertificate = h.certificate
case *handshakeMessageServerKeyExchange:
c.remoteKeypair = &namedCurveKeypair{h.namedCurve, h.publicKey, nil}
clientRandom, err := c.state.localRandom.Marshal()
if err != nil {
return err
}
serverRandom, err := c.state.remoteRandom.Marshal()
if err != nil {
return err
}
c.localKeypair, err = generateKeypair(h.namedCurve)
if err != nil {
return err
}
preMasterSecret, err := prfPreMasterSecret(c.remoteKeypair.publicKey, c.localKeypair.privateKey, c.localKeypair.curve)
if err != nil {
return err
}
c.state.masterSecret, err = prfMasterSecret(preMasterSecret, clientRandom, serverRandom, c.state.cipherSuite.hashFunc())
if err != nil {
return err
}
if err := c.state.cipherSuite.init(c.state.masterSecret, clientRandom, serverRandom /* isClient */, true); err != nil {
return err
}
expectedHash := valueKeySignature(clientRandom, serverRandom, h.publicKey, h.namedCurve, h.hashAlgorithm)
if err := verifyKeySignature(expectedHash, h.signature, h.hashAlgorithm, c.state.remoteCertificate); err != nil {
return err
}
case *handshakeMessageCertificateRequest:
c.remoteRequestedCertificate = true
case *handshakeMessageServerHelloDone:
case *handshakeMessageFinished:
plainText := c.handshakeCache.pullAndMerge(
handshakeCachePullRule{handshakeTypeClientHello, true},
handshakeCachePullRule{handshakeTypeServerHello, false},
handshakeCachePullRule{handshakeTypeCertificate, false},
handshakeCachePullRule{handshakeTypeServerKeyExchange, false},
handshakeCachePullRule{handshakeTypeCertificateRequest, false},
handshakeCachePullRule{handshakeTypeServerHelloDone, false},
handshakeCachePullRule{handshakeTypeCertificate, true},
handshakeCachePullRule{handshakeTypeClientKeyExchange, true},
handshakeCachePullRule{handshakeTypeCertificateVerify, true},
handshakeCachePullRule{handshakeTypeFinished, true},
)
expectedVerifyData, err := prfVerifyDataServer(c.state.masterSecret, plainText, c.state.cipherSuite.hashFunc())
if err != nil {
return err
}
if !bytes.Equal(expectedVerifyData, h.verifyData) {
return errVerifyDataMismatch
}
default:
return fmt.Errorf("unhandled handshake %d", h.handshakeType())
}
return nil
}
switch c.currFlight.get() {
case flight1:
// HelloVerifyRequest can be skipped by the server, so allow ServerHello during flight1 also
expectedMessages := c.handshakeCache.pull(
handshakeCachePullRule{handshakeTypeHelloVerifyRequest, false},
handshakeCachePullRule{handshakeTypeServerHello, false},
)
switch {
case expectedMessages[0] != nil:
if err := handleSingleHandshake(expectedMessages[0].data); err != nil {
return err
}
c.state.localSequenceNumber++
case expectedMessages[1] != nil:
if err := handleSingleHandshake(expectedMessages[1].data); err != nil {
return err
}
default:
return nil // We have no messages we can handle yet
}
c.log.Tracef("[handshake] Flight 1 changed to %s", flight3.String())
if err := c.currFlight.set(flight3); err != nil {
return err
}
case flight3:
expectedMessages := c.handshakeCache.pull(
handshakeCachePullRule{handshakeTypeServerHello, false},
handshakeCachePullRule{handshakeTypeCertificate, false},
handshakeCachePullRule{handshakeTypeServerKeyExchange, false},
handshakeCachePullRule{handshakeTypeCertificateRequest, false},
handshakeCachePullRule{handshakeTypeServerHelloDone, false},
)
// We don't have enough data to even assert validity
if expectedMessages[0] == nil {
return nil
}
expectedSeqnum := expectedMessages[0].messageSequence
for i, msg := range expectedMessages {
switch {
// handshakeMessageCertificateRequest can be nil, just make sure we have no gaps
case i == 3 && msg == nil:
continue
case msg == nil:
return nil // We don't have all messages yet, try again later
case msg.messageSequence != expectedSeqnum:
return nil // We have a gap, still waiting on messages
}
expectedSeqnum++
}
for _, msg := range expectedMessages {
if msg != nil {
if err := handleSingleHandshake(msg.data); err != nil {
return err
}
}
}
c.state.localSequenceNumber++
c.log.Tracef("[handshake] Flight 3 changed to %s", flight5.String())
if err := c.currFlight.set(flight5); err != nil {
return err
}
case flight5:
expectedMessages := c.handshakeCache.pull(
handshakeCachePullRule{handshakeTypeFinished, false},
)
if expectedMessages[0] == nil {
return nil
} else if err := handleSingleHandshake(expectedMessages[0].data); err != nil {
return err
}
c.setLocalEpoch(1)
c.state.localSequenceNumber = 1
c.signalHandshakeComplete()
default:
return fmt.Errorf("client asked to handle unknown flight (%d)", c.currFlight.get())
}
return nil
}
func clientFlightHandler(c *Conn) (bool, error) {
c.lock.Lock()
defer c.lock.Unlock()
switch c.currFlight.get() {
case flight1:
fallthrough
case flight3:
extensions := []extension{
&extensionSupportedEllipticCurves{
ellipticCurves: []namedCurve{namedCurveX25519, namedCurveP256},
},
&extensionSupportedPointFormats{
pointFormats: []ellipticCurvePointFormat{ellipticCurvePointFormatUncompressed},
},
&extensionSupportedSignatureAlgorithms{
signatureHashAlgorithms: []signatureHashAlgorithm{
{HashAlgorithmSHA256, signatureAlgorithmECDSA},
{HashAlgorithmSHA384, signatureAlgorithmECDSA},
{HashAlgorithmSHA512, signatureAlgorithmECDSA},
{HashAlgorithmSHA256, signatureAlgorithmRSA},
{HashAlgorithmSHA384, signatureAlgorithmRSA},
{HashAlgorithmSHA512, signatureAlgorithmRSA},
},
},
}
if len(c.localSRTPProtectionProfiles) > 0 {
extensions = append(extensions, &extensionUseSRTP{
protectionProfiles: c.localSRTPProtectionProfiles,
})
}
c.internalSend(&recordLayer{
recordLayerHeader: recordLayerHeader{
sequenceNumber: c.state.localSequenceNumber,
protocolVersion: protocolVersion1_2,
},
content: &handshake{
// sequenceNumber and messageSequence line up, may need to be re-evaluated
handshakeHeader: handshakeHeader{
messageSequence: uint16(c.state.localSequenceNumber),
},
handshakeMessage: &handshakeMessageClientHello{
version: protocolVersion1_2,
cookie: c.cookie,
random: c.state.localRandom,
cipherSuites: clientCipherSuites(),
compressionMethods: defaultCompressionMethods,
extensions: extensions,
}},
}, false)
case flight5:
// TODO: Better way to end handshake
if c.getRemoteEpoch() != 0 && c.getLocalEpoch() == 1 {
// Handshake is done
return true, nil
}
sequenceNumber := c.state.localSequenceNumber
if c.remoteRequestedCertificate {
c.internalSend(&recordLayer{
recordLayerHeader: recordLayerHeader{
sequenceNumber: c.state.localSequenceNumber,
protocolVersion: protocolVersion1_2,
},
content: &handshake{
// sequenceNumber and messageSequence line up, may need to be re-evaluated
handshakeHeader: handshakeHeader{
messageSequence: uint16(c.state.localSequenceNumber),
},
handshakeMessage: &handshakeMessageCertificate{
certificate: c.localCertificate,
}},
}, false)
sequenceNumber++
}
c.internalSend(&recordLayer{
recordLayerHeader: recordLayerHeader{
sequenceNumber: sequenceNumber,
protocolVersion: protocolVersion1_2,
},
content: &handshake{
// sequenceNumber and messageSequence line up, may need to be re-evaluated
handshakeHeader: handshakeHeader{
messageSequence: uint16(sequenceNumber),
},
handshakeMessage: &handshakeMessageClientKeyExchange{
publicKey: c.localKeypair.publicKey,
}},
}, false)
sequenceNumber++
if c.remoteRequestedCertificate {
if len(c.localCertificateVerify) == 0 {
plainText := c.handshakeCache.pullAndMerge(
handshakeCachePullRule{handshakeTypeClientHello, true},
handshakeCachePullRule{handshakeTypeServerHello, false},
handshakeCachePullRule{handshakeTypeCertificate, false},
handshakeCachePullRule{handshakeTypeServerKeyExchange, false},
handshakeCachePullRule{handshakeTypeCertificateRequest, false},
handshakeCachePullRule{handshakeTypeServerHelloDone, false},
handshakeCachePullRule{handshakeTypeCertificate, true},
handshakeCachePullRule{handshakeTypeClientKeyExchange, true},
)
certVerify, err := generateCertificateVerify(plainText, c.localPrivateKey)
if err != nil {
return false, err
}
c.localCertificateVerify = certVerify
}
c.internalSend(&recordLayer{
recordLayerHeader: recordLayerHeader{
sequenceNumber: sequenceNumber,
protocolVersion: protocolVersion1_2,
},
content: &handshake{
// sequenceNumber and messageSequence line up, may need to be re-evaluated
handshakeHeader: handshakeHeader{
messageSequence: uint16(sequenceNumber),
},
handshakeMessage: &handshakeMessageCertificateVerify{
hashAlgorithm: HashAlgorithmSHA256,
signatureAlgorithm: signatureAlgorithmECDSA,
signature: c.localCertificateVerify,
}},
}, false)
sequenceNumber++
}
c.internalSend(&recordLayer{
recordLayerHeader: recordLayerHeader{
sequenceNumber: sequenceNumber,
protocolVersion: protocolVersion1_2,
},
content: &changeCipherSpec{},
}, false)
if len(c.localVerifyData) == 0 {
plainText := c.handshakeCache.pullAndMerge(
handshakeCachePullRule{handshakeTypeClientHello, true},
handshakeCachePullRule{handshakeTypeServerHello, false},
handshakeCachePullRule{handshakeTypeCertificate, false},
handshakeCachePullRule{handshakeTypeServerKeyExchange, false},
handshakeCachePullRule{handshakeTypeCertificateRequest, false},
handshakeCachePullRule{handshakeTypeServerHelloDone, false},
handshakeCachePullRule{handshakeTypeCertificate, true},
handshakeCachePullRule{handshakeTypeClientKeyExchange, true},
handshakeCachePullRule{handshakeTypeCertificateVerify, true},
)
var err error
c.localVerifyData, err = prfVerifyDataClient(c.state.masterSecret, plainText, c.state.cipherSuite.hashFunc())
if err != nil {
return false, err
}
}
// TODO: Fix hard-coded epoch & sequenceNumber, taking retransmitting into account.
c.internalSend(&recordLayer{
recordLayerHeader: recordLayerHeader{
epoch: 1,
sequenceNumber: 0, // sequenceNumber restarts per epoch
protocolVersion: protocolVersion1_2,
},
content: &handshake{
// sequenceNumber and messageSequence line up, may need to be re-evaluated
handshakeHeader: handshakeHeader{
messageSequence: uint16(sequenceNumber), // KeyExchange + 1
},
handshakeMessage: &handshakeMessageFinished{
verifyData: c.localVerifyData,
}},
}, true)
default:
return false, fmt.Errorf("unhandled flight %s", c.currFlight.get())
}
return false, nil
}