forked from radsecproxy/radsecproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radmsg.c
443 lines (382 loc) · 11.6 KB
/
radmsg.c
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
/* Copyright (c) 2007-2009, UNINETT AS */
/* See LICENSE for licensing information. */
#ifdef SYS_SOLARIS9
#include <sys/inttypes.h>
#else
#include <stdint.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "list.h"
#include "radmsg.h"
#include "debug.h"
#include <pthread.h>
#include <nettle/hmac.h>
#include <openssl/rand.h>
#include "raddict.h"
#include "util.h"
#define RADLEN(x) ntohs(((uint16_t *)(x))[1])
void radmsg_free(struct radmsg *msg) {
if (msg) {
freetlvlist(msg->attrs);
free(msg);
}
}
struct radmsg *radmsg_init(uint8_t code, uint8_t id, uint8_t *auth) {
struct radmsg *msg;
msg = malloc(sizeof(struct radmsg));
if (!msg)
return NULL;
memset(msg, 0, sizeof(struct radmsg));
msg->attrs = list_create();
if (!msg->attrs) {
free(msg);
return NULL;
}
msg->code = code;
msg->id = id;
if (auth)
memcpy(msg->auth, auth, 16);
else if (!RAND_bytes(msg->auth, 16)) {
free(msg);
return NULL;
}
return msg;
}
int radmsg_add(struct radmsg *msg, struct tlv *attr) {
if (!msg || !msg->attrs)
return 1;
if (!attr || attr->l > RAD_Max_Attr_Value_Length)
return 0;
return list_push(msg->attrs, attr);
}
/** Return a new list with all tlv's in \a msg of type \a type. The
* caller is responsible for freeing the list by calling \a
* list_free(). */
struct list *
radmsg_getalltype(const struct radmsg *msg, uint8_t type)
{
struct list *list = NULL;
struct list_node *node = NULL;
if (!msg || !msg->attrs)
return NULL;
list = list_create();
if (!list)
return NULL;
for (node = list_first(msg->attrs); node; node = list_next(node))
if (((struct tlv *) node->data)->t == type)
if (list_push(list, node->data) != 1) {
list_free(list);
return NULL;
}
return list;
}
/* returns first tlv of the given type */
struct tlv *radmsg_gettype(struct radmsg *msg, uint8_t type) {
struct list_node *node;
struct tlv *tlv;
if (!msg)
return NULL;
for (node = list_first(msg->attrs); node; node = list_next(node)) {
tlv = (struct tlv *)node->data;
if (tlv->t == type)
return tlv;
}
return NULL;
}
/** Copy all attributes of type \a type from \a src to \a dst.
*
* If all attributes were copied successfully, the number of
* attributes copied is returned.
*
* If copying failed, a negative number is returned. */
int radmsg_copy_attrs(struct radmsg *dst,
const struct radmsg *src,
uint8_t type)
{
struct list_node *node = NULL;
struct list *list = radmsg_getalltype(src, type);
int n = 0;
for (node = list_first(list); node; node = list_next(node)) {
if (radmsg_add(dst, copytlv((struct tlv *) node->data)) != 1) {
n = -1;
break;
}
n++;
}
list_free(list);
return n;
}
int _checkmsgauth(unsigned char *rad, uint8_t *authattr, uint8_t *secret, int secret_len) {
int result = 0; /* Fail. */
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct hmac_md5_ctx hmacctx;
uint8_t auth[16], hash[MD5_DIGEST_SIZE];
pthread_mutex_lock(&lock);
/* FIXME: Why clearing authattr during hashing? */
memcpy(auth, authattr, 16);
memset(authattr, 0, 16);
hmac_md5_set_key(&hmacctx, secret_len, secret);
hmac_md5_update(&hmacctx, RADLEN(rad), rad);
hmac_md5_digest(&hmacctx, sizeof(hash), hash);
memcpy(authattr, auth, 16);
if (memcmp(auth, hash, 16)) {
debug(DBG_WARN, "message authenticator, wrong value");
goto out;
}
result = 1; /* Success. */
out:
pthread_mutex_unlock(&lock);
return result;
}
int _validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec, int sec_len) {
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct md5_ctx mdctx;
unsigned char hash[MD5_DIGEST_SIZE];
const unsigned int len = RADLEN(rad);
int result = 0; /* Fail. */
pthread_mutex_lock(&lock);
md5_init(&mdctx);
md5_update(&mdctx, 4, rad);
md5_update(&mdctx, 16, reqauth);
if (len > 20)
md5_update(&mdctx, len - 20, rad + 20);
md5_update(&mdctx, sec_len, sec);
md5_digest(&mdctx, sizeof(hash), hash);
result = !memcmp(hash, rad + 4, 16);
pthread_mutex_unlock(&lock);
return result;
}
int _createmessageauth(unsigned char *rad, unsigned char *authattrval, uint8_t *secret, int secret_len) {
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct hmac_md5_ctx hmacctx;
if (!authattrval)
return 1;
pthread_mutex_lock(&lock);
memset(authattrval, 0, 16);
hmac_md5_set_key(&hmacctx, secret_len, secret);
hmac_md5_update(&hmacctx, RADLEN(rad), rad);
hmac_md5_digest(&hmacctx, MD5_DIGEST_SIZE, authattrval);
pthread_mutex_unlock(&lock);
return 1;
}
int _radsign(unsigned char *rad, unsigned char *sec, int sec_len) {
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct md5_ctx mdctx;
pthread_mutex_lock(&lock);
md5_init(&mdctx);
md5_update(&mdctx, RADLEN(rad), rad);
md5_update(&mdctx, sec_len, sec);
md5_digest(&mdctx, MD5_DIGEST_SIZE, rad + 4);
pthread_mutex_unlock(&lock);
return 1;
}
uint8_t *tlv2buf(uint8_t *p, const struct tlv *tlv) {
p[0] = tlv->t;
p[1] = tlv->l+2;
if (tlv->l) {
if (tlv->v)
memcpy(p+2, tlv->v, tlv->l);
else
memset(p+2, 0, tlv->l);
}
return p;
}
uint8_t *radmsg2buf(struct radmsg *msg, uint8_t *secret, int secret_len) {
struct list_node *node;
struct tlv *tlv;
int size;
uint8_t *buf, *p, *msgauth = NULL;
if (!msg || !msg->attrs)
return NULL;
size = 20;
for (node = list_first(msg->attrs); node; node = list_next(node))
size += 2 + ((struct tlv *)node->data)->l;
if (size > 65535)
return NULL;
buf = malloc(size);
if (!buf)
return NULL;
p = buf;
*p++ = msg->code;
*p++ = msg->id;
*(uint16_t *)p = htons(size);
p += 2;
memcpy(p, msg->auth, 16);
p += 16;
for (node = list_first(msg->attrs); node; node = list_next(node)) {
tlv = (struct tlv *)node->data;
p = tlv2buf(p, tlv);
if (tlv->t == RAD_Attr_Message_Authenticator && secret)
msgauth = ATTRVAL(p);
p += tlv->l + 2;
}
if (msgauth && !_createmessageauth(buf, msgauth, secret, secret_len)) {
free(buf);
return NULL;
}
if (secret) {
if ((msg->code == RAD_Access_Accept || msg->code == RAD_Access_Reject || msg->code == RAD_Access_Challenge || msg->code == RAD_Accounting_Response || msg->code == RAD_Accounting_Request) && !_radsign(buf, secret, secret_len)) {
free(buf);
return NULL;
}
if (msg->code == RAD_Accounting_Request)
memcpy(msg->auth, buf + 4, 16);
}
return buf;
}
/* if secret set we also validate message authenticator if present */
struct radmsg *buf2radmsg(uint8_t *buf, uint8_t *secret, int secret_len, uint8_t *rqauth) {
struct radmsg *msg;
uint8_t t, l, *v = NULL, *p, auth[16];
uint16_t len;
struct tlv *attr;
len = RADLEN(buf);
if (len < 20)
return NULL;
if (secret && buf[0] == RAD_Accounting_Request) {
memset(auth, 0, 16);
if (!_validauth(buf, auth, secret, secret_len)) {
debug(DBG_WARN, "buf2radmsg: Accounting-Request message authentication failed");
return NULL;
}
}
if (rqauth && secret && !_validauth(buf, rqauth, secret, secret_len)) {
debug(DBG_WARN, "buf2radmsg: Invalid auth, ignoring reply");
return NULL;
}
msg = radmsg_init(buf[0], buf[1], (uint8_t *)buf + 4);
if (!msg)
return NULL;
p = buf + 20;
while (p - buf + 2 <= len) {
t = *p++;
l = *p++;
if (l < 2 || l > 255) {
debug(DBG_WARN, "buf2radmsg: invalid attribute length %d", l);
radmsg_free(msg);
return NULL;
}
l -= 2;
if (l) {
if (p - buf + l > len) {
debug(DBG_WARN, "buf2radmsg: attribute length %d exceeds packet length", l + 2);
radmsg_free(msg);
return NULL;
}
v = p;
p += l;
}
if (t == RAD_Attr_Message_Authenticator && secret) {
if (rqauth)
memcpy(buf + 4, rqauth, 16);
if (l != 16 || !_checkmsgauth(buf, v, secret, secret_len)) {
debug(DBG_WARN, "buf2radmsg: message authentication failed");
if (rqauth)
memcpy(buf + 4, msg->auth, 16);
radmsg_free(msg);
return NULL;
}
if (rqauth)
memcpy(buf + 4, msg->auth, 16);
debug(DBG_DBG, "buf2radmsg: message auth ok");
}
attr = maketlv(t, l, v);
if (!attr || !radmsg_add(msg, attr)) {
freetlv(attr);
radmsg_free(msg);
return NULL;
}
}
return msg;
}
/* should accept both names and numeric values, only numeric right now */
uint8_t attrname2val(char *attrname) {
int val = 0;
val = atoi(attrname);
return val > 0 && val < 256 ? val : 0;
}
/* ATTRNAME is on the form vendor[:type].
If only vendor is found, TYPE is set to 256 and 1 is returned.
If type is >= 256, 1 is returned.
Otherwise, 0 is returned.
*/
/* should accept both names and numeric values, only numeric right now */
int vattrname2val(char *attrname, uint32_t *vendor, uint32_t *type) {
char *s;
*vendor = atoi(attrname);
s = strchr(attrname, ':');
if (!s) { /* Only vendor was found. */
*type = 256;
return 1;
}
*type = atoi(s + 1);
return *type < 256;
}
int attrvalidate(unsigned char *attrs, int length) {
while (length > 1) {
if (ATTRLEN(attrs) < 2) {
debug(DBG_INFO, "attrvalidate: invalid attribute length %d", ATTRLEN(attrs));
return 0;
}
length -= ATTRLEN(attrs);
if (length < 0) {
debug(DBG_INFO, "attrvalidate: attribute length %d exceeds packet length", ATTRLEN(attrs));
return 0;
}
attrs += ATTRLEN(attrs);
}
if (length)
debug(DBG_INFO, "attrvalidate: malformed packet? remaining byte after last attribute");
return 1;
}
/** Create vendor specific tlv with ATTR. ATTR is consumed (freed) if
* all is well with the new tlv, i.e. if the function returns
* !NULL. */
struct tlv *makevendortlv(uint32_t vendor, struct tlv *attr){
struct tlv *newtlv = NULL;
uint8_t l, *v;
if (!attr || attr->l > (RAD_Max_Attr_Value_Length - 6))
return NULL;
l = attr->l + 2 + 4;
v = malloc(l);
if (v) {
vendor = htonl(vendor & 0x00ffffff); /* MSB=0 according to RFC 2865. */
memcpy(v, &vendor, 4);
tlv2buf(v + 4, attr);
newtlv = maketlv(RAD_Attr_Vendor_Specific, l, v);
free(v);
if (newtlv)
freetlv(attr);
}
return newtlv;
}
int resizeattr(struct tlv *attr, uint8_t newlen) {
if (newlen > RAD_Max_Attr_Value_Length)
return 0;
if (resizetlv(attr, newlen))
return 1;
return 0;
}
const char* attrval2strdict(struct tlv *attr) {
uint32_t val;
if(!attr) return NULL;
val = tlv2longint(attr);
switch (attr->t) {
case RAD_Attr_Acct_Status_Type:
if(val < sizeof(RAD_Attr_Acct_Status_Type_Dict)/sizeof(RAD_Attr_Acct_Status_Type_Dict[0]))
return RAD_Attr_Acct_Status_Type_Dict[val] ? RAD_Attr_Acct_Status_Type_Dict[val] : RAD_Attr_Dict_Undef;
break;
case RAD_Attr_Acct_Terminate_Cause:
if(val < sizeof(RAD_Attr_Acct_Terminate_Cause_Dict)/sizeof(RAD_Attr_Acct_Terminate_Cause_Dict[0]))
return RAD_Attr_Acct_Terminate_Cause_Dict[val] ? RAD_Attr_Acct_Terminate_Cause_Dict[val] : RAD_Attr_Dict_Undef;
break;
default:
break;
}
return NULL;
}
/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */