forked from vzex/dog-tunnel
/
stun.go
488 lines (434 loc) · 11.1 KB
/
stun.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Package stun implements a subset of the Session Traversal Utilities
// for NAT (STUN) protocol, described in RFC 5389. Notably absent is
// support for long-term credentials.
package stun
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"net"
)
type Class uint8
type Method uint16
// Possible classes and methods for a STUN packet.
const (
ClassRequest = iota
ClassIndication
ClassSuccess
ClassError
MethodBinding = 1
)
// A Packet presents select information about a STUN packet.
type Packet struct {
Class Class
Method Method
Tid [12]byte
Addr *net.UDPAddr
HasMac bool
Software string
UseCandidate bool
Error *PacketError
Alternate *net.UDPAddr
}
func RandomTid() ([]byte, error) {
ret := make([]byte, 12)
_, err := io.ReadFull(rand.Reader, ret)
if err != nil {
return nil, err
}
return ret, nil
}
func InformReady(tid []byte, addr *net.UDPAddr, macKey []byte) ([]byte, error) {
if len(tid) != 12 {
panic("Wrong length for tid")
}
var hdr header
hdr.TypeCode = typeCode(ClassIndication, MethodBinding)
hdr.Magic = magic
copy(hdr.Tid[:], tid)
var buf bytes.Buffer
if addr != nil {
ip := addr.IP.To4()
family := 1
if ip == nil {
ip = addr.IP
family++
}
binary.Write(&buf, binary.BigEndian, []uint16{
attrAddress,
uint16(4 + len(ip)),
uint16(family),
uint16(addr.Port)})
buf.Write(ip)
}
return buildPacket(hdr, buf.Bytes(), macKey, false)
}
// BindRequest constructs and returns a Binding Request STUN packet.
//
// tid must be 12 bytes long. If a macKey is provided, the returned
// packet is signed.
func BindRequest(tid []byte, addr *net.UDPAddr, macKey []byte, compat bool, useCandidate bool) ([]byte, error) {
if len(tid) != 12 {
panic("Wrong length for tid")
}
var hdr header
hdr.TypeCode = typeCode(ClassRequest, MethodBinding)
hdr.Magic = magic
copy(hdr.Tid[:], tid)
var buf bytes.Buffer
if useCandidate {
binary.Write(&buf, binary.BigEndian, []uint16{
attrUseCandidate,
uint16(0)})
}
if addr != nil {
ip := addr.IP.To4()
family := 1
if ip == nil {
ip = addr.IP
family++
}
binary.Write(&buf, binary.BigEndian, []uint16{
attrAddress,
uint16(4 + len(ip)),
uint16(family),
uint16(addr.Port)})
buf.Write(ip)
}
return buildPacket(hdr, buf.Bytes(), macKey, compat)
}
// BindResponse constructs and returns a Binding Success STUN packet.
//
// tid must be 12 bytes long. If a macKey is provided, the returned
// packet is signed.
func BindResponse(tid []byte, addr *net.UDPAddr, macKey []byte, compat bool) ([]byte, error) {
if len(tid) != 12 {
panic("Wrong length for tid")
}
var hdr header
hdr.TypeCode = typeCode(ClassSuccess, MethodBinding)
hdr.Magic = magic
copy(hdr.Tid[:], tid)
ip := addr.IP.To4()
family := 1
if ip == nil {
ip = addr.IP
family++
}
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, []uint16{
attrXorAddress,
uint16(4 + len(ip)),
uint16(family),
uint16(addr.Port ^ magic>>16)})
buf.Write(ip)
attrs := buf.Bytes()
for i := range magicBytes {
attrs[8+i] ^= magicBytes[i]
}
for i := range attrs[12:] {
attrs[12+i] ^= tid[i]
}
return buildPacket(hdr, attrs, macKey, compat)
}
// ParsePacket parses a byte slice as a STUN packet.
//
// If a macKey is provided, only packets correctly signed with that
// key will be accepted. If no macKey is provided, only unsigned
// packets will be accepted.
func ParsePacket(raw []byte, macKey []byte) (*Packet, error) {
var hdr header
if err := binary.Read(bytes.NewBuffer(raw[:headerLen]), binary.BigEndian, &hdr); err != nil {
return nil, err
}
// Initial sanity checks: verify initial bits, magic, length and
// optional fingerprint.
if hdr.TypeCode&0xC000 != 0 || int(hdr.Length+20) != len(raw) || hdr.Magic != magic {
return nil, MalformedPacket{}
}
if hdr.Length >= fpLen {
if present, valid := checkFp(raw); present {
if !valid {
return nil, MalformedPacket{}
}
raw = raw[:len(raw)-fpLen]
}
}
pkt := &Packet{
Class: typeCodeClass(hdr.TypeCode),
Method: typeCodeMethod(hdr.TypeCode),
Tid: hdr.Tid,
}
attrReader := bytes.NewBuffer(raw[headerLen:])
var haveXor bool
for {
if attrReader.Len() == 0 {
break
}
var ahdr attrHeader
if err := binary.Read(attrReader, binary.BigEndian, &ahdr); err != nil {
return nil, err
}
if ahdr.Length > 500 || attrReader.Len() < int(ahdr.Length) {
return nil, MalformedPacket{}
}
value := attrReader.Next(int(ahdr.Length))
if ahdr.Length%4 != 0 {
attrReader.Next(int(4 - ahdr.Length%4))
}
switch ahdr.Type {
case attrAddress:
if !haveXor {
ip, port, err := parseAddress(value)
if err != nil {
return nil, err
}
pkt.Addr = &net.UDPAddr{IP:ip, Port:port}
}
case attrXorAddress:
ip, port, err := parseAddress(value)
if err != nil {
return nil, err
}
for i := range ip {
ip[i] ^= raw[4+i]
}
port ^= int(binary.BigEndian.Uint16(raw[4:]))
pkt.Addr = &net.UDPAddr{IP:ip, Port:port}
haveXor = true
case attrUseCandidate:
pkt.UseCandidate = true
case attrFingerprint:
return nil, MalformedPacket{}
case attrIntegrity:
if len(macKey) == 0 {
return nil, UnverifiableMac{}
}
tocheck := raw[:len(raw)-attrReader.Len()-macLen]
binary.BigEndian.PutUint16(tocheck[2:4], uint16(len(tocheck)+macLen-headerLen))
macer := hmac.New(sha1.New, macKey)
if _, err := macer.Write(tocheck); err != nil {
return nil, err
}
mac := make([]byte, 0, 20)
mac = macer.Sum(mac)
if !bytes.Equal(mac, value) {
return nil, BadMac{}
}
pkt.HasMac = true
return pkt, nil
case attrErrCode:
code := uint16(value[2])*100 + uint16(value[3])
reason := string(value[4:])
pkt.Error = &PacketError{code, reason}
case attrUnknownAttrs:
// Ignored
case attrSoftware:
pkt.Software = string(value)
case attrAlternate:
ip, port, err := parseAddress(value)
if err != nil {
return nil, err
}
pkt.Alternate = &net.UDPAddr{IP:ip, Port:port}
case attrUsername:
case attrRealm:
case attrNonce:
return nil, errors.New("Unsupported STUN attribute")
}
}
if len(macKey) > 0 {
return nil, MissingMac{}
}
return pkt, nil
}
// A MalformedPacket error is returned by ParsePacket when it
// encounters structural malformations in the STUN packet.
//
// On a network endpoing where STUN coexists with another protocol,
// this error can be used to differentiate STUN and non-STUN traffic.
type MalformedPacket struct{}
func (m MalformedPacket) Error() string {
return "Malformed STUN packet"
}
// A BadMac error is returned by ParsePacket when a structurally sound
// STUN packet is received with a signature not matching the provided
// macKey.
type BadMac struct{}
func (b BadMac) Error() string {
return "Incorrect MAC on packet"
}
// A MissingMac error is returned by ParsePacket when it receives a
// valid but unsigned STUN packet where it expected a signed packet.
type MissingMac struct{}
func (m MissingMac) Error() string {
return "MAC expected but missing"
}
// An UnverifiableMac error is returned by ParsePacket when it
// encounters a valid and signed STUN packet, and no macKey was
// provided.
type UnverifiableMac struct{}
func (u UnverifiableMac) Error() string {
return "MAC found but no key given"
}
// A PacketError describes an error returned by a STUN server.
type PacketError struct {
Code uint16
Reason string
}
func (p PacketError) Error() string {
var genericErr string
switch p.Code {
case errTryAlternate:
genericErr = "Try Alternate"
case errBadRequest:
genericErr = "Bad Request"
case errUnauthorized:
genericErr = "Unauthorized"
case errUnknownAttribute:
genericErr = "Unknown Attribute(s)"
case errStaleNonce:
genericErr = "Stale Nonce"
case errServerInternal:
genericErr = "Internal Server Error"
default:
genericErr = fmt.Sprintf("Error %d", p.Code)
}
if len(p.Reason) == 0 {
return genericErr
}
return fmt.Sprintf("%s: %s", genericErr, p.Reason)
}
func buildPacket(hdr header, attributes, macKey []byte, compat bool) ([]byte, error) {
var buf bytes.Buffer
if len(macKey) > 0 {
hdr.Length = uint16(len(attributes) + macLen)
macer := hmac.New(sha1.New, macKey)
if err := binary.Write(macer, binary.BigEndian, hdr); err != nil {
return nil, err
}
if _, err := macer.Write(attributes); err != nil {
return nil, err
}
if err := binary.Write(&buf, binary.BigEndian, attrHeader{attrIntegrity, 20}); err != nil {
return nil, err
}
attributes = append(attributes, macer.Sum(buf.Bytes())...)
buf.Reset()
}
hdr.Length = uint16(len(attributes))
if !compat {
hdr.Length += fpLen
}
if err := binary.Write(&buf, binary.BigEndian, hdr); err != nil {
return nil, err
}
buf.Write(attributes)
if !compat {
var fp fpAttr
fp.Type = attrFingerprint
fp.Length = 4
fp.Crc = crc32.ChecksumIEEE(buf.Bytes()) ^ 0x5354554e
if err := binary.Write(&buf, binary.BigEndian, fp); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
func parseAddress(raw []byte) (net.IP, int, error) {
if len(raw) != 8 && len(raw) != 20 {
return nil, 0, MalformedPacket{}
}
var family int
switch int(raw[1]) {
case 1:
family = 4
case 2:
family = 16
default:
return nil, 0, MalformedPacket{}
}
port := binary.BigEndian.Uint16(raw[2:])
ip := make([]byte, len(raw[4:]))
copy(ip, raw[4:])
if len(ip) != family {
return nil, 0, MalformedPacket{}
}
return net.IP(ip), int(port), nil
}
func checkFp(raw []byte) (present, valid bool) {
split := len(raw) - fpLen
var fp fpAttr
if err := binary.Read(bytes.NewBuffer(raw[split:]), binary.BigEndian, &fp); err != nil {
return false, false
}
if fp.Type != attrFingerprint || fp.Length != 4 {
return false, false
}
if fp.Crc != (crc32.ChecksumIEEE(raw[:split]) ^ 0x5354554e) {
return true, false
}
return true, true
}
func typeCode(class uint8, method uint16) uint16 {
return method<<2&0xFE00 | uint16(class)&2<<7 | method<<1&0x00E0 | uint16(class)&1<<4 | method&0xF
}
func typeCodeClass(typeCode uint16) Class {
return Class(typeCode>>4&1 | typeCode>>7&2)
}
func typeCodeMethod(typeCode uint16) Method {
return Method(typeCode&0xF | typeCode&0xE0>>1 | typeCode&0xFE00>>2)
}
// Parsing structs
type header struct {
TypeCode uint16
Length uint16
Magic uint32
Tid [12]byte
}
type attrHeader struct {
Type uint16
Length uint16
}
type fpAttr struct {
attrHeader
Crc uint32
}
// Constants
const (
magic = 0x2112a442
headerLen = 20
fpLen = 8
macLen = 24
)
var magicBytes = []byte{0x21, 0x12, 0xa4, 0x42}
const (
// Comprehension required
attrAddress = 0x01 //
attrUsername = 0x06 //
attrIntegrity = 0x08 //
attrErrCode = 0x09 //
attrUnknownAttrs = 0x0A //
attrRealm = 0x14 //
attrNonce = 0x15 //
attrXorAddress = 0x20 //
attrUseCandidate = 0x25 //
// Comprehension optional
attrSoftware = 0x8022 //
attrAlternate = 0x8023 //
attrFingerprint = 0x8028 //
)
const (
errTryAlternate = 300
errBadRequest = 400
errUnauthorized = 401
errUnknownAttribute = 420
errStaleNonce = 438
errServerInternal = 500
)