-
Notifications
You must be signed in to change notification settings - Fork 7
/
radiuspacket.go
463 lines (347 loc) · 8.74 KB
/
radiuspacket.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
package goradius
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"log"
"net"
)
var (
HEADER_SIZE = 20
)
type RadiusHeader struct {
Code uint8
Identifier uint8
Length uint16
Authenticator [authenticatorLength]byte
}
// includes VSA attributes
type RadiusAttribute struct {
Type uint8
Length uint8
VendorId uint32
VendorType uint8
VendorLength uint8
Value []byte
}
type RadiusPacket struct {
RadiusHeader
Attributes []RadiusAttribute
Addr *net.UDPAddr
}
type VendorSpecificAttribute struct {
VendorId uint32
VendorType uint8
VendorLength uint8
}
/*
* RadiusHeader
*/
func (r RadiusHeader) String() string {
return fmt.Sprintf("Type: '%v' Identifier: %v Length: %v Authenticator: %x",
request_type_to_string[r.Code], r.Identifier, r.Length, r.Authenticator)
}
/*
* RadiusAttribute
*/
func (r RadiusAttribute) Bytes() []byte {
buf := bytes.NewBuffer([]byte{})
err := binary.Write(buf, binary.BigEndian, &r.Type)
if err != nil {
log.Fatal(err)
}
r.Length = uint8(len(r.Value) + 2)
err = binary.Write(buf, binary.BigEndian, &r.Length)
if err != nil {
log.Fatal(err)
}
err = binary.Write(buf, binary.BigEndian, r.Value[:])
if err != nil {
log.Fatal(err)
}
return buf.Bytes()
}
func (r RadiusAttribute) String() string {
return fmt.Sprintf("%v: %x", code_to_attributes[r.Type], r.Value)
}
/*
* RadiusPacket
*/
func NewRadiusPacket() *RadiusPacket {
var p RadiusPacket
return &p
}
func (r RadiusPacket) String() string {
return fmt.Sprintf("RadiusPacket{%v %v}", r.RadiusHeader, r.Attributes)
}
func (r *RadiusPacket) Duplicate() *RadiusPacket {
dest := RadiusPacket{}
dest.Code = r.Code
dest.Identifier = r.Identifier
dest.Length = r.Length
dest.Authenticator = r.Authenticator
for _, attr := range r.Attributes {
dest.Attributes = append(dest.Attributes, attr)
}
return &dest
}
func (p *RadiusPacket) AddAttribute(attrTypeStr string, value []byte) error {
var err error
if attrTypeCode, ok := attributes_to_code[attrTypeStr]; ok {
attr := RadiusAttribute{
Type: attrTypeCode,
Value: value,
}
p.Attributes = append(p.Attributes, attr)
err = nil
} else {
vsa_attr, err := CreateVSA(attrTypeStr, value)
if err != nil {
return err
}
p.Attributes = append(p.Attributes, vsa_attr)
err = nil
}
return err
}
func (p *RadiusPacket) AddAttributeByType(attrType uint8, value []byte) {
attr := RadiusAttribute{
Type: attrType,
Value: value,
}
p.Attributes = append(p.Attributes, attr)
}
func (p *RadiusPacket) GetAttribute(attrType string) [][]byte {
var attrs [][]byte
if attrTypeCode, ok := attributes_to_code[attrType]; ok {
for _, v := range p.Attributes {
if v.Type == attrTypeCode {
// if v.Type == VendorSpecific {
// reader := bytes.NewBuffer(v.Value)
// vsa := VendorSpecificAttribute{}
// err := binary.Read(reader, binary.BigEndian, &vsa)
// if err != nil {
// log.Fatal(err)
// }
// value := reader.Next(int(vsa.VendorLength))
// attrs = append(attrs, value)
// } else {
// }
attrs = append(attrs, v.Value)
}
}
} else {
log.Printf("Looking for VSA")
vsa, err := FindVSA(attrType)
if err == nil {
for _, v := range p.Attributes {
if v.Type == VendorSpecific {
if vsa.VendorId == v.VendorId && vsa.VendorType == v.VendorType {
attrs = append(attrs, v.Value)
}
}
}
}
}
return attrs
}
func (p *RadiusPacket) GetFirstAttribute(attrType string) []byte {
var attr []byte
attrs := p.GetAttribute(attrType)
if len(attrs) > 0 {
attr = attrs[0]
}
return attr
}
func (p *RadiusPacket) GetFirstAttributeAsString(attrType string) string {
return string(p.GetFirstAttribute(attrType))
}
func encodeVendorSpecificAttr(attr RadiusAttribute) []byte {
vsa := VendorSpecificAttribute{
VendorId: attr.VendorId,
VendorType: attr.VendorType,
VendorLength: uint8(len(attr.Value) + 2),
}
buf := bytes.NewBuffer([]byte{})
err := binary.Write(buf, binary.BigEndian, vsa)
if err != nil {
panic(err)
}
err = binary.Write(buf, binary.BigEndian, attr.Value)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func VendorAttribute(attrName string, value []byte) RadiusAttribute {
attr, err := CreateVSA(attrName, value)
if err != nil {
log.Fatal(err)
}
return attr
}
func CreateVSA(attrName string, value []byte) (RadiusAttribute, error) {
vsa, err := FindVSA(attrName)
if err != nil {
return RadiusAttribute{}, err
}
rattr := RadiusAttribute{
Type: uint8(26),
VendorId: vsa.VendorId,
VendorType: vsa.VendorType,
Value: value,
}
rattr.Length = uint8(2 + len(rattr.Value))
return rattr, nil
}
func (r *RadiusPacket) encodeAttrs(secret string) []byte {
buf := bytes.NewBuffer([]byte{})
for _, attr := range r.Attributes {
encoded_ok := true
switch attr.Type {
case UserPassword:
// We usually wanna decode the password because if we proxy it
// we will need to re-encode with the new secret anyways
password_data := xorPassword(secret, r.Authenticator, attr.Value, false)
attr.Length = uint8(len(password_data))
attr.Value = password_data[:]
case VendorSpecific:
vendor_data := encodeVendorSpecificAttr(attr)
attr.Length = uint8(len(vendor_data) + 2)
attr.Value = vendor_data[:]
default:
attr.Length = uint8(len(attr.Value))
}
if encoded_ok {
buf.Write(attr.Bytes())
}
}
return buf.Bytes()
}
func (r *RadiusPacket) EncodePacket(secret string) ([]byte, error) {
// encode all attrs first
attrs_data := r.encodeAttrs(secret)
attrs_size := len(attrs_data)
r.Length = uint16(attrs_size + HEADER_SIZE)
buf := bytes.NewBuffer([]byte{})
err := binary.Write(buf, binary.BigEndian, &r.RadiusHeader)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.BigEndian, attrs_data)
return buf.Bytes(), err
}
func ParseRADIUSPacket(rawMsg []byte, secret string) (*RadiusPacket, error) {
packet := NewRadiusPacket()
reader := bytes.NewReader(rawMsg)
err := binary.Read(reader, binary.BigEndian, &packet.RadiusHeader)
if err != nil {
return nil, err
}
rawAttributesBytes := rawMsg[headerEnd:]
rawAttributes := parseAttributes(rawAttributesBytes, packet.Authenticator, secret)
for _, attr := range rawAttributes {
packet.Attributes = append(packet.Attributes, attr)
}
return packet, nil
}
func parseAttributes(data []byte, requestAuthenticator [16]byte, secret string) []RadiusAttribute {
var attrs []RadiusAttribute
reader := bytes.NewBuffer(data)
for {
var err error
attr := RadiusAttribute{}
ok := false
attr.Type, err = reader.ReadByte()
if err == io.EOF {
break
}
attr.Length, err = reader.ReadByte()
if err == io.EOF {
break
}
switch attr.Type {
case uint8(0):
log.Printf("attr_type 0?")
case uint8(UserPassword):
val := reader.Next(int(attr.Length) - 2)
attr.Value = xorPassword(secret, requestAuthenticator, val, true)
ok = true
case uint8(VendorSpecific):
vsa := VendorSpecificAttribute{}
err = binary.Read(reader, binary.BigEndian, &vsa)
if err != nil {
log.Fatal(err)
}
attr.VendorId = vsa.VendorId
attr.VendorType = vsa.VendorType
attr.VendorLength = vsa.VendorLength
attr.Value = reader.Next(int(vsa.VendorLength - 2))
ok = true
// log.Printf("praseAttrs: %+v", vsa)
// log.Printf("VSA: %+v", vsa)
// log.Printf("Value: %v", attr.Value)
// log.Printf("Value hex: %x", attr.Value)
default:
attr.Value = reader.Next(int(attr.Length) - 2)
ok = true
}
if ok {
attrs = append(attrs, attr)
}
}
return attrs
}
func GenerateRandomAuthenticator() [16]byte {
authenticator := [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
_, err := rand.Read(authenticator[:])
if err != nil {
panic(err)
}
return authenticator
}
func paddAttr(data []byte, size int) []byte {
padded := make([]byte, size)
for i, b := range data {
padded[i] = b
}
return padded
}
func xorPassword(secret string, authenticator [16]byte, password []byte, decrypt bool) []byte {
padding := 16
fields := 1
for {
res := fields * 16
if res >= len(password) {
padding = res
break
}
fields += 1
}
paddedPassword := paddAttr(password, padding)
second_one_way_hash := authenticator
var password_out []byte
for i := 0; i < padding; i = i + 16 {
xored := make([]byte, 16)
newHash := md5.New()
newHash.Write([]byte(secret))
newHash.Write(second_one_way_hash[:])
newHashSum := newHash.Sum(nil)
for j := 0; j < 16; j++ {
xored[j] = paddedPassword[i+j] ^ newHashSum[j]
if xored[j] == 0x00 {
break
}
}
if decrypt {
copy(second_one_way_hash[:], paddedPassword[i:i+16])
} else {
copy(second_one_way_hash[:], xored[:])
}
password_out = append(password_out, xored...)
}
return password_out
}