This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
handshake.go
309 lines (250 loc) · 7.46 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
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
package wireguard
import (
"crypto/rand"
"encoding/binary"
"errors"
"io"
"sync"
"sync/atomic"
"time"
"github.com/flynn/noise"
"github.com/flynn/wireguard-go/internal/skip32"
"github.com/flynn/wireguard-go/internal/tai64n"
)
const (
handshakeStateConsumedInitiation = iota
handshakeStateCreatedResponse
handshakeStateCreatedInitiation
handshakeStateConsumedResponse
handshakeStateZeroed
)
func init() {
key := make([]byte, 10)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
panic(err)
}
// The SKIP32 cipher obfuscates the counter, which is used in packet headers
// to to identify the handshake, so that it cannot be easily predicted and
// does not trivially expose the actual count of handshakes.
handshakeCounterCipher, _ = skip32.New(key)
}
var keypairCounter uint64
var handshakeCounter uint32
var handshakeCounterCipher *skip32.Skip32
func getHandshakeID() uint32 {
return handshakeCounterCipher.Obfus(atomic.AddUint32(&handshakeCounter, 1))
}
type noiseHandshake struct {
sync.RWMutex
senderIndex uint32
remoteIndex uint32
latestTimestamp tai64n.TAI64N
lastInitiationConsumption time.Time
hs *noise.HandshakeState
state int
sendingCipher noise.Cipher
receivingCipher noise.Cipher
remoteStatic [32]byte
peer *peer
}
func (h *noiseHandshake) clear() {
h.remoteIndex = 0
h.senderIndex = 0
h.hs = nil
h.sendingCipher = nil
h.receivingCipher = nil
h.state = handshakeStateZeroed
}
type noiseKeypair struct {
initiator bool
internalID uint64
senderIndex uint32
remoteIndex uint32
sending noise.Cipher
receiving noise.Cipher
peer *peer
}
type noiseKeypairs struct {
previous, current, next *noiseKeypair
sync.RWMutex
}
var (
noiseCiphersuite = noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2s)
noisePrologue = []byte("WireGuard v0 zx2c4 [email protected]")
errUnknownPeer = errors.New("wireguard: peer is unknown")
errUnknownHandshake = errors.New("wireguard: handshake is unknown")
errAttack = errors.New("wireguard: handshake is considered an attack")
errNoIdentity = errors.New("wireguard: no identity is configured")
errInvalidState = errors.New("wireguard: handshake is in invalid state")
)
func (f *Interface) handshakeCreateInitiation(handshake *noiseHandshake) []byte {
f.identityMtx.RLock()
defer f.identityMtx.RUnlock()
if len(f.staticKey.Private) == 0 {
return nil
}
handshake.Lock()
defer handshake.Unlock()
handshake.hs = noise.NewHandshakeState(noise.Config{
CipherSuite: noiseCiphersuite,
Random: rand.Reader,
Pattern: noise.HandshakeIK,
Initiator: true,
Prologue: noisePrologue,
PresharedKey: f.presharedKey,
StaticKeypair: f.staticKey,
PeerStatic: handshake.remoteStatic[:],
})
res := make([]byte, 5, messageHandshakeInitiationLen)
res[0] = byte(messageHandshakeInitiation)
var taiBuf [12]byte
tai64n.Now().WriteStorage(taiBuf[:])
handshake.hs.WriteMessage(res, taiBuf[:])
handshake.senderIndex = getHandshakeID()
binary.LittleEndian.PutUint32(res[1:], handshake.senderIndex)
handshake.state = handshakeStateCreatedInitiation
f.handshakesMtx.Lock()
f.handshakes[handshake.senderIndex] = handshake
f.handshakesMtx.Unlock()
return res
}
const minInitiationInterval = time.Second / 2
func (f *Interface) handshakeConsumeInitiation(data []byte) (*peer, error) {
f.identityMtx.RLock()
defer f.identityMtx.RUnlock()
if len(f.staticKey.Private) == 0 {
return nil, errNoIdentity
}
hs := noise.NewHandshakeState(noise.Config{
CipherSuite: noiseCiphersuite,
Random: rand.Reader,
Pattern: noise.HandshakeIK,
Initiator: false,
Prologue: noisePrologue,
PresharedKey: f.presharedKey,
StaticKeypair: f.staticKey,
})
var taiBuf [12]byte
tai, _, _, err := hs.ReadMessage(taiBuf[:0], data[5:])
if err != nil {
return nil, err
}
var t tai64n.TAI64N
t.ReadStorage(tai)
var s publicKey
copy(s[:], hs.PeerStatic())
f.peersMtx.RLock()
peer, ok := f.peers[s]
f.peersMtx.RUnlock()
if !ok {
return nil, errUnknownPeer
}
peer.handshake.RLock()
replayAttack := !t.After(peer.handshake.latestTimestamp)
floodAttack := !peer.handshake.lastInitiationConsumption.IsZero() && time.Now().Before(peer.handshake.lastInitiationConsumption.Add(minInitiationInterval))
peer.handshake.RUnlock()
if replayAttack || floodAttack {
return nil, errAttack
}
peer.handshake.Lock()
peer.handshake.hs = hs
peer.handshake.latestTimestamp = t
peer.handshake.remoteIndex = binary.LittleEndian.Uint32(data[1:])
peer.handshake.lastInitiationConsumption = time.Now()
peer.handshake.state = handshakeStateConsumedInitiation
peer.handshake.Lock()
return peer, nil
}
func (f *Interface) handshakeCreateResponse(handshake *noiseHandshake) []byte {
handshake.Lock()
defer handshake.Unlock()
if handshake.state != handshakeStateConsumedInitiation {
return nil
}
res := make([]byte, 9, messageHandshakeResponseLen)
res[0] = byte(messageHandshakeResponse)
binary.LittleEndian.PutUint32(res[5:], handshake.remoteIndex)
res, cs1, cs2 := handshake.hs.WriteMessage(res[9:], nil)
handshake.receivingCipher = cs1.Cipher()
handshake.sendingCipher = cs2.Cipher()
handshake.senderIndex = getHandshakeID()
binary.LittleEndian.PutUint32(res[1:], handshake.senderIndex)
f.handshakesMtx.Lock()
f.handshakes[handshake.senderIndex] = handshake
f.handshakesMtx.Unlock()
handshake.state = handshakeStateCreatedResponse
return res
}
func (f *Interface) handshakeConsumeResponse(data []byte) (*peer, error) {
f.identityMtx.RLock()
defer f.identityMtx.RUnlock()
if len(f.staticKey.Private) == 0 {
return nil, errNoIdentity
}
receiverIndex := binary.LittleEndian.Uint32(data[5:])
f.handshakesMtx.Lock()
handshake, ok := f.handshakes[receiverIndex]
f.handshakesMtx.Unlock()
if !ok {
return nil, errUnknownHandshake
}
handshake.Lock()
defer handshake.Unlock()
if handshake.state != handshakeStateCreatedInitiation {
return nil, errInvalidState
}
_, cs1, cs2, err := handshake.hs.ReadMessage(nil, data[9:])
if err != nil {
return nil, err
}
handshake.receivingCipher = cs2.Cipher()
handshake.sendingCipher = cs1.Cipher()
handshake.state = handshakeStateConsumedResponse
handshake.remoteIndex = binary.LittleEndian.Uint32(data[1:])
return handshake.peer, nil
}
func (f *Interface) handshakeBeginSession(handshake *noiseHandshake, keypairs *noiseKeypairs, initiator bool) {
handshake.Lock()
defer handshake.Unlock()
keypair := &noiseKeypair{
internalID: atomic.AddUint64(&keypairCounter, 1),
remoteIndex: handshake.remoteIndex,
senderIndex: handshake.senderIndex,
peer: handshake.peer,
initiator: initiator,
receiving: handshake.receivingCipher,
sending: handshake.sendingCipher,
}
f.handshakesMtx.Lock()
delete(f.handshakes, handshake.senderIndex)
f.handshakesMtx.Unlock()
handshake.clear()
keypairs.Lock()
if initiator {
if keypairs.next != nil {
keypairs.previous = keypairs.next
keypairs.next = nil
} else {
keypairs.previous = keypairs.current
}
keypairs.current = keypair
} else {
keypairs.next = keypair
keypairs.previous = nil
}
keypairs.Unlock()
f.keypairsMtx.Lock()
f.keypairs[keypair.senderIndex] = keypair
f.keypairsMtx.Unlock()
}
func (f *Interface) receivedWithKeypair(keypairs *noiseKeypairs, receivedKeypair *noiseKeypair) (next bool) {
keypairs.Lock()
defer keypairs.Unlock()
if receivedKeypair == keypairs.next {
keypairs.previous = keypairs.current
keypairs.current = receivedKeypair
keypairs.next = nil
next = true
}
return
}