forked from bronze1man/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
367 lines (335 loc) · 7.55 KB
/
packet.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
package radius
import (
"crypto"
"crypto/hmac"
_ "crypto/md5"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"net"
"strconv"
)
var ErrMessageAuthenticatorCheckFail = fmt.Errorf("RADIUS Response-Authenticator verification failed")
type Packet struct {
Secret string
Code PacketCode
Identifier uint8
Authenticator [16]byte
AVPs []AVP
}
func (p *Packet) Copy() *Packet {
outP := &Packet{
Secret: p.Secret,
Code: p.Code,
Identifier: p.Identifier,
Authenticator: p.Authenticator, //这个应该是拷贝
}
outP.AVPs = make([]AVP, len(p.AVPs))
for i := range p.AVPs {
outP.AVPs[i] = p.AVPs[i].Copy()
}
return outP
}
//此方法保证不修改包的内容
func (p *Packet) Encode() (b []byte, err error) {
p = p.Copy()
p.SetAVP(AVP{
Type: MessageAuthenticator,
Value: make([]byte, 16),
})
if p.Code == AccessRequest {
_, err := rand.Read(p.Authenticator[:])
if err != nil {
return nil, err
}
}
//TODO request的时候重新计算密码
b, err = p.encodeNoHash()
if err != nil {
return
}
//计算Message-Authenticator,Message-Authenticator被放在最后面
hasher := hmac.New(crypto.MD5.New, []byte(p.Secret))
hasher.Write(b)
copy(b[len(b)-16:len(b)], hasher.Sum(nil))
// fix up the authenticator
// handle request and response stuff.
// here only handle response part.
switch p.Code {
case AccessRequest:
case AccessAccept, AccessReject, AccessChallenge, AccountingRequest, AccountingResponse:
//rfc2865 page 15 Response Authenticator
//rfc2866 page 6 Response Authenticator
//rfc2866 page 6 Request Authenticator
hasher := crypto.Hash(crypto.MD5).New()
hasher.Write(b)
hasher.Write([]byte(p.Secret))
copy(b[4:20], hasher.Sum(nil))
default:
return nil, fmt.Errorf("not handle p.Code %d", p.Code)
}
return b, err
}
func (p *Packet) encodeNoHash() (b []byte, err error) {
b = make([]byte, 4096)
b[0] = uint8(p.Code)
b[1] = uint8(p.Identifier)
copy(b[4:20], p.Authenticator[:])
written := 20
bb := b[20:]
for i, _ := range p.AVPs {
n, err := p.AVPs[i].Encode(bb)
written += n
if err != nil {
return nil, err
}
bb = bb[n:]
}
binary.BigEndian.PutUint16(b[2:4], uint16(written))
return b[:written], nil
}
func (p *Packet) HasAVP(attrType AttributeType) bool {
for i, _ := range p.AVPs {
if p.AVPs[i].Type == attrType {
return true
}
}
return false
}
/*
func (p *Packet) Attributes(attrType AttributeType) []*AVP {
ret := []*AVP(nil)
for i, _ := range p.AVPs {
if p.AVPs[i].Type == attrType {
ret = append(ret, &p.AVPs[i])
}
}
return ret
}
*/
//get one avp
func (p *Packet) GetAVP(attrType AttributeType) *AVP {
for i := range p.AVPs {
if p.AVPs[i].Type == attrType {
return &p.AVPs[i]
}
}
return nil
}
//set one avp,remove all other same type
func (p *Packet) SetAVP(avp AVP) {
p.DeleteOneType(avp.Type)
p.AddAVP(avp)
}
func (p *Packet) AddAVP(avp AVP) {
p.AVPs = append(p.AVPs, avp)
}
//删除一个AVP
func (p *Packet) DeleteAVP(avp *AVP) {
for i := range p.AVPs {
if &(p.AVPs[i]) == avp {
for j := i; j < len(p.AVPs)-1; j++ {
p.AVPs[j] = p.AVPs[j+1]
}
p.AVPs = p.AVPs[:len(p.AVPs)-1]
break
}
}
return
}
//delete all avps with this type
func (p *Packet) DeleteOneType(attrType AttributeType) {
for i := 0; i < len(p.AVPs); i++ {
if p.AVPs[i].Type == attrType {
for j := i; j < len(p.AVPs)-1; j++ {
p.AVPs[j] = p.AVPs[j+1]
}
p.AVPs = p.AVPs[:len(p.AVPs)-1]
i--
break
}
}
return
}
/*
func (p *Packet) Valid() bool {
switch p.Code {
case AccessRequest:
if !(p.Has(NASIPAddress) || p.Has(NASIdentifier)) {
return false
}
if p.Has(CHAPPassword) && p.Has(UserPassword) {
return false
}
return true
case AccessAccept:
return true
case AccessReject:
return true
case AccountingRequest:
return true
case AccountingResponse:
return true
case AccessChallenge:
return true
case StatusServer:
return true
case StatusClient:
return true
case Reserved:
return true
}
return true
}
*/
func (p *Packet) Reply() *Packet {
pac := new(Packet)
pac.Authenticator = p.Authenticator
pac.Identifier = p.Identifier
pac.Secret = p.Secret
return pac
}
func (p *Packet) Send(c net.PacketConn, addr net.Addr) error {
buf, err := p.Encode()
if err != nil {
return err
}
_, err = c.WriteTo(buf, addr)
return err
}
func DecodePacket(Secret string, buf []byte) (p *Packet, err error) {
p = &Packet{Secret: Secret}
p.Code = PacketCode(buf[0])
p.Identifier = buf[1]
copy(p.Authenticator[:], buf[4:20])
//read attributes
b := buf[20:]
for len(b) >= 2 {
length := uint8(b[1])
if int(length) > len(b) {
return nil, errors.New("invalid length")
}
attr := AVP{}
attr.Type = AttributeType(b[0])
attr.Value = append(attr.Value, b[2:length]...)
p.AVPs = append(p.AVPs, attr)
b = b[length:]
}
//验证Message-Authenticator,并且通过测试验证此处算法是正确的
err = p.checkMessageAuthenticator()
if err != nil {
return p, err
}
return p, nil
}
//如果没有MessageAuthenticator也算通过
func (p *Packet) checkMessageAuthenticator() (err error) {
Authenticator := p.GetAVP(MessageAuthenticator)
if Authenticator == nil {
return nil
}
AuthenticatorValue := Authenticator.Value
defer func() { Authenticator.Value = AuthenticatorValue }()
Authenticator.Value = make([]byte, 16)
content, err := p.encodeNoHash()
if err != nil {
return err
}
hasher := hmac.New(crypto.MD5.New, []byte(p.Secret))
hasher.Write(content)
if !hmac.Equal(hasher.Sum(nil), AuthenticatorValue) {
return ErrMessageAuthenticatorCheckFail
}
return nil
}
func (p *Packet) String() string {
s := "Code: " + p.Code.String() + "\n" +
"Identifier: " + strconv.Itoa(int(p.Identifier)) + "\n" +
"Authenticator: " + fmt.Sprintf("%#v", p.Authenticator) + "\n"
for _, avp := range p.AVPs {
s += avp.StringWithPacket(p) + "\n"
}
return s
}
func (p *Packet) GetUsername() (username string) {
avp := p.GetAVP(UserName)
if avp == nil {
return ""
}
return avp.Decode(p).(string)
}
func (p *Packet) GetPassword() (password string) {
avp := p.GetAVP(UserPassword)
if avp == nil {
return ""
}
return avp.Decode(p).(string)
}
func (p *Packet) GetNasIpAddress() (ip net.IP) {
avp := p.GetAVP(NASIPAddress)
if avp == nil {
return nil
}
return avp.Decode(p).(net.IP)
}
func (p *Packet) GetAcctStatusType() AcctStatusTypeEnum {
avp := p.GetAVP(AcctStatusType)
if avp == nil {
return AcctStatusTypeEnum(0)
}
return avp.Decode(p).(AcctStatusTypeEnum)
}
func (p *Packet) GetAcctSessionId() string {
avp := p.GetAVP(AcctSessionId)
if avp == nil {
return ""
}
return avp.Decode(p).(string)
}
func (p *Packet) GetAcctTotalOutputOctets() uint64 {
out := uint64(0)
avp := p.GetAVP(AcctOutputOctets)
if avp != nil {
out += uint64(avp.Decode(p).(uint32))
}
avp = p.GetAVP(AcctOutputGigawords)
if avp != nil {
out += uint64(avp.Decode(p).(uint32))*2 ^ 32
}
return out
}
func (p *Packet) GetAcctTotalInputOctets() uint64 {
out := uint64(0)
avp := p.GetAVP(AcctInputOctets)
if avp != nil {
out += uint64(avp.Decode(p).(uint32))
}
avp = p.GetAVP(AcctInputGigawords)
if avp != nil {
out += uint64(avp.Decode(p).(uint32))*2 ^ 32
}
return out
}
// it is ike_id in strongswan client
func (p *Packet) GetNASPort() uint32 {
avp := p.GetAVP(NASPort)
if avp == nil {
return 0
}
return avp.Decode(p).(uint32)
}
func (p *Packet) GetNASIdentifier() string {
avp := p.GetAVP(NASIdentifier)
if avp == nil {
return ""
}
return avp.Decode(p).(string)
}
func (p *Packet) GetEAPMessage() *EapPacket {
avp := p.GetAVP(EAPMessage)
if avp == nil {
return nil
}
return avp.Decode(p).(*EapPacket)
}