forked from pion/dtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight6handler.go
76 lines (67 loc) · 2.49 KB
/
flight6handler.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
package dtls
import (
"context"
)
func flight6Parse(ctx context.Context, c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) (flightVal, *alert, error) {
_, msgs, ok := cache.fullPullMap(state.handshakeRecvSequence-1,
handshakeCachePullRule{handshakeTypeFinished, cfg.initialEpoch + 1, true, false},
)
if !ok {
// No valid message received. Keep reading
return 0, nil, nil
}
if _, ok = msgs[handshakeTypeFinished].(*handshakeMessageFinished); !ok {
return 0, &alert{alertLevelFatal, alertInternalError}, nil
}
// Other party retransmitted the last flight.
return flight6, nil, nil
}
func flight6Generate(c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) ([]*packet, *alert, error) {
var pkts []*packet
pkts = append(pkts,
&packet{
record: &recordLayer{
recordLayerHeader: recordLayerHeader{
protocolVersion: protocolVersion1_2,
},
content: &changeCipherSpec{},
},
})
if len(state.localVerifyData) == 0 {
plainText := cache.pullAndMerge(
handshakeCachePullRule{handshakeTypeClientHello, cfg.initialEpoch, true, false},
handshakeCachePullRule{handshakeTypeServerHello, cfg.initialEpoch, false, false},
handshakeCachePullRule{handshakeTypeCertificate, cfg.initialEpoch, false, false},
handshakeCachePullRule{handshakeTypeServerKeyExchange, cfg.initialEpoch, false, false},
handshakeCachePullRule{handshakeTypeCertificateRequest, cfg.initialEpoch, false, false},
handshakeCachePullRule{handshakeTypeServerHelloDone, cfg.initialEpoch, false, false},
handshakeCachePullRule{handshakeTypeCertificate, cfg.initialEpoch, true, false},
handshakeCachePullRule{handshakeTypeClientKeyExchange, cfg.initialEpoch, true, false},
handshakeCachePullRule{handshakeTypeCertificateVerify, cfg.initialEpoch, true, false},
handshakeCachePullRule{handshakeTypeFinished, cfg.initialEpoch + 1, true, false},
)
var err error
state.localVerifyData, err = prfVerifyDataServer(state.masterSecret, plainText, state.cipherSuite.hashFunc())
if err != nil {
return nil, &alert{alertLevelFatal, alertInternalError}, err
}
}
pkts = append(pkts,
&packet{
record: &recordLayer{
recordLayerHeader: recordLayerHeader{
protocolVersion: protocolVersion1_2,
epoch: 1,
},
content: &handshake{
handshakeMessage: &handshakeMessageFinished{
verifyData: state.localVerifyData,
},
},
},
shouldEncrypt: true,
resetLocalSequenceNumber: true,
},
)
return pkts, nil, nil
}