-
Notifications
You must be signed in to change notification settings - Fork 40
/
handshake.go
executable file
·255 lines (186 loc) · 4.17 KB
/
handshake.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
package ipc
import (
"bytes"
"encoding/binary"
"errors"
)
// 1st message sent from the server
// byte 0 = protocal version no.
// byte 1 = whether encryption is to be used - 0 no , 1 = encryption
func (sc *Server) handshake() error {
err := sc.one()
if err != nil {
return err
}
if sc.encryption {
err = sc.startEncryption()
if err != nil {
return err
}
}
err = sc.msgLength()
if err != nil {
return err
}
return nil
}
func (sc *Server) one() error {
buff := make([]byte, 2)
buff[0] = byte(version)
if sc.encryption {
buff[1] = byte(1)
} else {
buff[1] = byte(0)
}
_, err := sc.conn.Write(buff)
if err != nil {
return errors.New("unable to send handshake ")
}
recv := make([]byte, 1)
_, err = sc.conn.Read(recv)
if err != nil {
return errors.New("failed to received handshake reply")
}
switch result := recv[0]; result {
case 0:
return nil
case 1:
return errors.New("client has a different version number")
case 2:
return errors.New("client is enforcing encryption")
case 3:
return errors.New("server failed to get handshake reply")
}
return errors.New("other error - handshake failed")
}
func (sc *Server) startEncryption() error {
shared, err := sc.keyExchange()
if err != nil {
return err
}
gcm, err := createCipher(shared)
if err != nil {
return err
}
sc.enc = &encryption{
keyExchange: "ecdsa",
encryption: "AES-GCM-256",
cipher: gcm,
}
return nil
}
func (sc *Server) msgLength() error {
toSend := make([]byte, 4)
buff := make([]byte, 4)
binary.BigEndian.PutUint32(buff, uint32(sc.maxMsgSize))
if sc.encryption {
maxMsg, err := encrypt(*sc.enc.cipher, buff)
if err != nil {
return err
}
binary.BigEndian.PutUint32(toSend, uint32(len(maxMsg)))
toSend = append(toSend, maxMsg...)
} else {
binary.BigEndian.PutUint32(toSend, uint32(len(buff)))
toSend = append(toSend, buff...)
}
_, err := sc.conn.Write(toSend)
if err != nil {
return errors.New("unable to send max message length ")
}
reply := make([]byte, 1)
_, err = sc.conn.Read(reply)
if err != nil {
return errors.New("did not received message length reply")
}
return nil
}
// 1st message received by the client
func (cc *Client) handshake() error {
err := cc.one()
if err != nil {
return err
}
if cc.encryption {
err := cc.startEncryption()
if err != nil {
return err
}
}
err = cc.msgLength()
if err != nil {
return err
}
return nil
}
func (cc *Client) one() error {
recv := make([]byte, 2)
_, err := cc.conn.Read(recv)
if err != nil {
return errors.New("failed to received handshake message")
}
if recv[0] != version {
cc.handshakeSendReply(1)
return errors.New("server has sent a different version number")
}
if recv[1] != 1 && cc.encryptionReq {
cc.handshakeSendReply(2)
return errors.New("server tried to connect without encryption")
}
if recv[1] == 0 {
cc.encryption = false
} else {
cc.encryption = true
}
cc.handshakeSendReply(0) // 0 is ok
return nil
}
func (cc *Client) startEncryption() error {
shared, err := cc.keyExchange()
if err != nil {
return err
}
gcm, err := createCipher(shared)
if err != nil {
return err
}
cc.enc = &encryption{
keyExchange: "ECDSA",
encryption: "AES-GCM-256",
cipher: gcm,
}
return nil
}
func (cc *Client) msgLength() error {
buff := make([]byte, 4)
_, err := cc.conn.Read(buff)
if err != nil {
return errors.New("failed to received max message length 1")
}
var msgLen uint32
binary.Read(bytes.NewReader(buff), binary.BigEndian, &msgLen) // message length
buff = make([]byte, int(msgLen))
_, err = cc.conn.Read(buff)
if err != nil {
return errors.New("failed to received max message length 2")
}
var buff2 []byte
if cc.encryption {
buff2, err = decrypt(*cc.enc.cipher, buff)
if err != nil {
return errors.New("failed to received max message length 3")
}
} else {
buff2 = buff
}
var maxMsgSize uint32
binary.Read(bytes.NewReader(buff2), binary.BigEndian, &maxMsgSize) // message length
cc.maxMsgSize = int(maxMsgSize)
cc.handshakeSendReply(0)
return nil
}
func (cc *Client) handshakeSendReply(result byte) {
buff := make([]byte, 1)
buff[0] = result
cc.conn.Write(buff)
}