This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handshake_context.go
163 lines (148 loc) · 4.39 KB
/
handshake_context.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
package dtls
import (
"fmt"
"log"
"os"
)
type handshakeContext interface {
beginHandshake()
continueHandshake(*handshake) (bool, error)
}
type baseHandshakeContext struct {
*Conn
isServer bool
nextReceiveSequenceNumber uint16
sequenceNumber uint16
currentFlight int
sessionID []byte
cookie []byte
cipherSuite cipherSuite
clientRandom random
serverRandom random
keyAgreement keyAgreement
masterSecret []byte
finishedHash finishedHash
//We omit the pre-flight, i.e. HelloVerify because otherwise we would need to keep state
//defeating the purpose of HelloVerify
//Flight 1
clientHello *handshake
//Flight 2
serverHello *handshake
serverCertificate *handshake
serverKeyExchange *handshake
certificateRequest *handshake
serverHelloDone *handshake
//Flight 3
clientCertificate *handshake
clientKeyExchange *handshake
certificateVerify *handshake
clientFinished *handshake
//Flight 4
serverFinished *handshake
handshakeMessageBuffer map[uint16]*handshakeFragmentList
}
func (hc *baseHandshakeContext) receiveMessage(message *handshake) {
if message.MessageSeq < hc.nextReceiveSequenceNumber {
log.Printf("Received handshake message with lower sequence number than next expected")
return
}
if message.MessageSeq == hc.nextReceiveSequenceNumber &&
message.FragmentOffset == 0 &&
message.FragmentLength == message.Length {
hc.storeMessage(message)
hc.nextReceiveSequenceNumber += 1
}
if hfl, ok := hc.handshakeMessageBuffer[message.MessageSeq]; ok {
hfl.InsertFragment(message)
} else {
hc.handshakeMessageBuffer[message.MessageSeq] = newHandshakeFragmentList(message)
}
hc.maybeReceiveNextBufferedMessage()
}
func (hc *baseHandshakeContext) maybeReceiveNextBufferedMessage() {
for {
hfl, ok := hc.handshakeMessageBuffer[hc.nextReceiveSequenceNumber]
if !ok {
return
}
if !hfl.IsComplete() {
return
}
hc.storeMessage(hfl.GetCompleteHandshake())
delete(hc.handshakeMessageBuffer, hc.nextReceiveSequenceNumber)
hc.nextReceiveSequenceNumber += 1
}
}
func (hc *baseHandshakeContext) storeMessage(message *handshake) {
if hc.currentFlight == 1 && message.MsgType == clientHello {
hc.clientHello = message
return
//TODO: handle out of order handshake messages?
}
if hc.currentFlight == 2 {
switch message.MsgType {
case serverHello:
hc.serverHello = message
case certificate:
hc.serverCertificate = message
case serverKeyExchange:
hc.serverKeyExchange = message
case certificateRequest:
hc.certificateRequest = message
case serverHelloDone:
hc.serverHelloDone = message
default:
log.Printf("Unable to store received handshake message!")
//TODO: how do we handle invalid handshake messages?
}
return
}
if hc.currentFlight == 3 {
switch message.MsgType {
case certificate:
hc.clientCertificate = message
case clientKeyExchange:
hc.clientKeyExchange = message
case certificateVerify:
hc.certificateVerify = message
case finished:
hc.clientFinished = message
default:
log.Printf("Unable to store received handshake message!")
//TODO: how do we handle invalid handshake messages?
}
return
}
if hc.currentFlight == 4 && message.MsgType == finished {
hc.serverFinished = message
//TODO: handle out of order handshake messages?
return
}
log.Printf("Unable to store received handshake message!")
}
func (hc *baseHandshakeContext) buildNextHandshakeMessage(typ handshakeType, handshakeMessage []byte) *handshake {
hdshk := &handshake{
MsgType: typ,
Length: uint32(len(handshakeMessage)),
MessageSeq: hc.sequenceNumber,
FragmentOffset: 0,
FragmentLength: uint32(len(handshakeMessage)),
Fragment: handshakeMessage,
}
hc.sequenceNumber += 1
return hdshk
}
func (hc *baseHandshakeContext) sendHandshakeMessage(message *handshake) {
hc.Conn.sendRecord(typeHandshake, message.Bytes())
}
func logMasterSecret(clientRandom, masterSecret []byte) {
f, err := os.OpenFile("/home/maufl/.dtls-secrets", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Printf("Unable to open log file for DTLS master secret: %s", err)
return
}
defer f.Close()
if _, err = f.WriteString(fmt.Sprintf("CLIENT_RANDOM %x %x\n", clientRandom, masterSecret)); err != nil {
log.Printf("Unable to write master secret to log file: %s", err)
}
}