-
Notifications
You must be signed in to change notification settings - Fork 181
/
packet.go
183 lines (165 loc) · 4.82 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
package radius
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/binary"
"errors"
)
// MaxPacketLength is the maximum wire length of a RADIUS packet.
const MaxPacketLength = 4096
// Packet is a RADIUS packet.
type Packet struct {
Code Code
Identifier byte
Authenticator [16]byte
Secret []byte
Attributes
}
// New creates a new packet with the Code, Secret fields set to the given
// values. The returned packet's Identifier and Authenticator fields are filled
// with random values.
//
// The function panics if not enough random data could be generated.
func New(code Code, secret []byte) *Packet {
var buff [17]byte
if _, err := rand.Read(buff[:]); err != nil {
panic(err)
}
packet := &Packet{
Code: code,
Identifier: buff[0],
Secret: secret,
}
copy(packet.Authenticator[:], buff[1:])
return packet
}
// Parse parses an encoded RADIUS packet b. An error is returned if the packet
// is malformed.
func Parse(b, secret []byte) (*Packet, error) {
if len(b) < 20 {
return nil, errors.New("radius: packet not at least 20 bytes long")
}
length := int(binary.BigEndian.Uint16(b[2:4]))
if length < 20 || length > MaxPacketLength || len(b) < length {
return nil, errors.New("radius: invalid packet length")
}
attrs, err := ParseAttributes(b[20:length])
if err != nil {
return nil, err
}
packet := &Packet{
Code: Code(b[0]),
Identifier: b[1],
Secret: secret,
Attributes: attrs,
}
copy(packet.Authenticator[:], b[4:20])
return packet, nil
}
// Response returns a new packet that has the same identifier, secret, and
// authenticator as the current packet.
func (p *Packet) Response(code Code) *Packet {
q := &Packet{
Code: code,
Identifier: p.Identifier,
Secret: p.Secret,
}
copy(q.Authenticator[:], p.Authenticator[:])
return q
}
// Encode encodes the RADIUS packet to wire format that can then
// be sent to a RADIUS client.
//
// If the RADIUS packet code requires it, the authenticator in
// the returned data will be a hash calculation based off of the packet
// data and secret. Use MarshalBinary() to get the packet in wire
// format without the hash calculation.
//
// An error is returned if the encoded packet is too long (due to its Attributes),
// or if the packet has an unknown Code.
func (p *Packet) Encode() ([]byte, error) {
b, err := p.MarshalBinary()
if err != nil {
return nil, err
}
switch p.Code {
case CodeAccessRequest, CodeStatusServer:
// Authenticator is sent as-is
case CodeAccessAccept, CodeAccessReject, CodeAccountingRequest, CodeAccountingResponse, CodeAccessChallenge, CodeDisconnectRequest, CodeDisconnectACK, CodeDisconnectNAK, CodeCoARequest, CodeCoAACK, CodeCoANAK:
hash := md5.New()
hash.Write(b[:4])
switch p.Code {
case CodeAccountingRequest, CodeDisconnectRequest, CodeCoARequest:
var nul [16]byte
hash.Write(nul[:])
default:
hash.Write(p.Authenticator[:])
}
hash.Write(b[20:])
hash.Write(p.Secret)
hash.Sum(b[4:4:20])
default:
return nil, errors.New("radius: unknown Packet Code")
}
return b, nil
}
// MarshalBinary returns the packet in wire format.
//
// The authenticator in the returned data is copied from p.Authenticator
// without any hash calculation. Use Encode() if the packet is intended
// to be sent to a RADIUS client and requires the authenticator to be
// calculated.
func (p *Packet) MarshalBinary() ([]byte, error) {
attributesLen, err := AttributesEncodedLen(p.Attributes)
if err != nil {
return nil, err
}
size := 20 + attributesLen
if size > MaxPacketLength {
return nil, errors.New("radius: packet is too large")
}
b := make([]byte, size)
b[0] = byte(p.Code)
b[1] = p.Identifier
binary.BigEndian.PutUint16(b[2:4], uint16(size))
copy(b[4:20], p.Authenticator[:])
p.Attributes.encodeTo(b[20:])
return b, nil
}
// IsAuthenticResponse returns if the given RADIUS response is an authentic
// response to the given request.
func IsAuthenticResponse(response, request, secret []byte) bool {
if len(response) < 20 || len(request) < 20 || len(secret) == 0 {
return false
}
hash := md5.New()
hash.Write(response[:4])
hash.Write(request[4:20])
hash.Write(response[20:])
hash.Write(secret)
var sum [md5.Size]byte
return bytes.Equal(hash.Sum(sum[:0]), response[4:20])
}
// IsAuthenticRequest returns if the given RADIUS request is an authentic
// request using the given secret.
func IsAuthenticRequest(request, secret []byte) bool {
if len(request) < 20 || len(secret) == 0 {
return false
}
switch Code(request[0]) {
case CodeAccessRequest, CodeStatusServer:
return true
case CodeAccountingRequest, CodeDisconnectRequest, CodeCoARequest:
hash := md5.New()
hash.Write(request[:4])
var nul [16]byte
hash.Write(nul[:])
hash.Write(request[20:])
hash.Write(secret)
var sum [md5.Size]byte
return bytes.Equal(hash.Sum(sum[:0]), request[4:20])
default:
return false
}
}