forked from irumiha/radius
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalidation.go
318 lines (266 loc) · 8.26 KB
/
validation.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
package radigo
import (
"bytes"
"crypto/des"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"math/bits"
"strings"
"golang.org/x/crypto/md4"
"golang.org/x/text/encoding/unicode"
)
const (
UNLIMITED = -1
)
type Validation struct {
MinLength int
MaxLength int //if < 0 unlimited
Decode func(p *Packet, attr *AVP) error
}
func (v Validation) Validate(p *Packet, attr *AVP) error {
if len(attr.RawValue) < v.MinLength {
return fmt.Errorf("value too short for : %+v", attr)
}
if v.MaxLength != UNLIMITED && len(attr.RawValue) > v.MaxLength {
return fmt.Errorf("value too long for : %+v", attr)
}
if v.Decode != nil {
return v.Decode(p, attr)
}
return nil
}
func DecodeUserPassword(p *Packet, a *AVP) error {
if len(p.secret) == 0 {
return errors.New("empty secret")
}
dec := make([]byte, 0, len(a.RawValue))
hash := md5.New()
hash.Write([]byte(p.secret))
hash.Write(p.Authenticator[:])
dec = hash.Sum(dec)
for i, b := range a.RawValue[:16] {
dec[i] ^= b
}
for i := 16; i < len(a.RawValue); i += 16 {
hash.Reset()
hash.Write([]byte(p.secret))
hash.Write(a.RawValue[i-16 : i])
dec = hash.Sum(dec)
for j, b := range a.RawValue[i : i+16] {
dec[i+j] ^= b
}
}
if i := bytes.IndexByte(dec, 0); i > -1 {
a.RawValue = dec[:i]
}
a.RawValue = dec
return nil
}
var validation = map[uint8]Validation{
1: {1, UNLIMITED, nil}, //UserName
2: {16, 128, DecodeUserPassword}, //UserPassword
3: {17, 17, nil}, //CHAPPassword
4: {4, 4, nil}, //NASIPAddress
5: {1, 4, nil}, //NASPort
}
// EncodeUserPassword encodes the plaintext, where plaintext's length needs to
// be greater than 16 and a multiple of 16.
func EncodeUserPassword(plaintext, secret, requestAuthenticator []byte) []byte {
enc := make([]byte, 0, len(plaintext))
hash := md5.New()
hash.Write(secret)
hash.Write(requestAuthenticator)
enc = hash.Sum(enc)
for i, b := range plaintext[:16] {
enc[i] ^= b
}
for i := 16; i < len(plaintext); i += 16 {
hash.Reset()
hash.Write(secret)
hash.Write(enc[i-16 : i])
enc = hash.Sum(enc)
for j, b := range plaintext[i : i+16] {
enc[i+j] ^= b
}
}
return enc
}
// AuthenticateCHAP receive the password as plaintext and verify against the chap challenge
func AuthenticateCHAP(password, authenticator, chapChallenge []byte) bool {
h := md5.New()
h.Write(chapChallenge[:1])
h.Write(password)
h.Write(authenticator)
answer := h.Sum(nil)
if len(answer) != len(chapChallenge[1:]) {
return false
}
for i := range answer {
if answer[i] != chapChallenge[i+1] {
return false
}
}
return true
}
// EncodeCHAPPassword is used in test to encode CHAP-Password raw value
func EncodeCHAPPassword(password, authenticator []byte) []byte {
chapIdent := make([]byte, 1)
rand.Read(chapIdent)
h := md5.New()
h.Write(chapIdent)
h.Write(password)
h.Write(authenticator)
chapRawVal := make([]byte, 17)
copy(chapRawVal[:1], chapIdent)
copy(chapRawVal[1:], h.Sum(nil))
return chapRawVal
}
// isAuthenticReq verifies the authenticity of a given RADIUS request using an MD5 hash and the provided secret.
// It supports authentication for specific packet types: AccountingRequest, DisconnectRequest, and CoARequest.
func isAuthenticReq(request, secret []byte) bool {
if len(request) < 20 || len(secret) == 0 {
return false
}
pCode := PacketCode(request[0])
// Only proceed with authentication for specific packet types. Assume all others are authentic.
if pCode != AccountingRequest && pCode != DisconnectRequest && pCode != CoARequest {
return true
}
hash := md5.New()
hash.Write(request[:4]) // packet type, identifier and length
// Use a zero-filled slice as the authenticator for the hash computation.
hash.Write(make([]byte, 16))
hash.Write(request[20:]) // attributes
hash.Write(secret)
return bytes.Equal(hash.Sum(nil), request[4:20])
}
// ToUTF16 takes an ASCII string and turns it into a UCS-2 / UTF-16 representation
func ToUTF16(in string) ([]byte, error) {
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
return encoder.Bytes([]byte(in))
}
// GenerateNTResponse - rfc2759, 8.1
func GenerateNTResponse(authenticatorChallenge, peerChallenge []byte, username, password string) ([]byte, error) {
challenge := ChallengeHash(peerChallenge, authenticatorChallenge, username)
ucs2Password, err := ToUTF16(password)
if err != nil {
return []byte{}, err // unable to check this error
}
passwordHash := HashPassword(ucs2Password)
return ChallengeResponse(challenge, passwordHash), nil
}
// ChallengeHash - rfc2759, 8.2
func ChallengeHash(peerChallenge, authenticatorChallenge []byte, username string) []byte {
sha := sha1.New()
sha.Write(peerChallenge)
sha.Write(authenticatorChallenge)
sha.Write([]byte(username))
return sha.Sum(nil)[:8]
}
// HashPassword with MD4 - rfc2759, 8.3
func HashPassword(password []byte) []byte {
h := md4.New()
h.Write(password)
return h.Sum(nil)
}
// ChallengeResponse - rfc2759, 8.5
func ChallengeResponse(challenge, passwordHash []byte) []byte {
zPasswordHash := make([]byte, 21)
copy(zPasswordHash, passwordHash)
challengeResponse := make([]byte, 24)
copy(challengeResponse[0:], DESCrypt(zPasswordHash[0:7], challenge))
copy(challengeResponse[8:], DESCrypt(zPasswordHash[7:14], challenge))
copy(challengeResponse[16:], DESCrypt(zPasswordHash[14:21], challenge))
return challengeResponse
}
// parityPadDESKey transforms a 7-octet key into an 8-octed one by
// adding a parity at every 8th bit position.
// See https://limbenjamin.com/articles/des-key-parity-bit-calculator.html
func parityPadDESKey(inBytes []byte) []byte {
in := uint64(0)
outBytes := make([]byte, 8)
for i := 0; i < len(inBytes); i++ {
offset := uint64(8 * (len(inBytes) - i - 1))
in |= uint64(inBytes[i]) << offset
}
for i := 0; i < len(outBytes); i++ {
offset := uint64(7 * (len(outBytes) - i - 1))
outBytes[i] = byte(in>>offset) << 1
if bits.OnesCount(uint(outBytes[i]))%2 == 0 {
outBytes[i] |= 1
}
}
return outBytes
}
// DESCrypt - rfc2759, 8.6
func DESCrypt(key, clear []byte) []byte {
k := key
if len(k) == 7 {
k = parityPadDESKey(key)
}
des, err := des.NewCipher(k)
if err != nil {
panic(err)
}
b := make([]byte, 8)
des.Encrypt(b, clear)
return b
}
// GenerateAuthenticatorResponse - rfc2759, 8.7
func GenerateAuthenticatorResponse(authenticatorChallenge, peerChallenge, ntResponse []byte, username, password string) (string, error) {
ucs2Password, err := ToUTF16(password)
if err != nil {
return "", err // unable to check this error
}
passwordHash := HashPassword(ucs2Password)
passwordHashHash := HashPassword(passwordHash)
magic1 := []byte{
0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65,
0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67,
0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74}
magic2 := []byte{
0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B,
0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F,
0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E,
0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F,
0x6E}
sha := sha1.New()
sha.Write(passwordHashHash)
sha.Write(ntResponse)
sha.Write(magic1)
digest := sha.Sum(nil)
challenge := ChallengeHash(peerChallenge, authenticatorChallenge, username)
sha = sha1.New()
sha.Write(digest)
sha.Write(challenge)
sha.Write(magic2)
digest = sha.Sum(nil)
return fmt.Sprintf("S=%s", strings.ToUpper(hex.EncodeToString(digest))), nil
}
func GenerateClientMSCHAPResponse(authenticator [16]byte, userName, password string) ([]byte, error) {
// generate the Ident
chapIdent := make([]byte, 1)
rand.Read(chapIdent)
// generate 16 bytes peer Chalange
peerChallenge := make([]byte, 16)
rand.Read(peerChallenge)
// compose challenge from peerChallenge, authenticator and userName
challenge := ChallengeHash(peerChallenge, authenticator[:], userName)
ucs2Password, err := ToUTF16(password)
if err != nil {
return nil, err // unable to check this error
}
passwordHash := HashPassword(ucs2Password)
// compose peerResponse
peerResp := ChallengeResponse(challenge, passwordHash)
respVal := make([]byte, 50)
copy(respVal[:1], chapIdent)
copy(respVal[2:18], peerChallenge)
copy(respVal[26:50], peerResp)
return respVal, nil
}