The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
decode.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 93b6c3137b7fdc90c2c9eb49ea32b3b569c31cd6 $
19 *
20 * @file protocols/radius/decode.c
21 * @brief Functions to decode RADIUS attributes
22 *
23 * @copyright 2000-2003,2006-2015 The FreeRADIUS server project
24 */
25RCSID("$Id: 93b6c3137b7fdc90c2c9eb49ea32b3b569c31cd6 $")
26
27#include <freeradius-devel/util/md5.h>
28#include <freeradius-devel/util/struct.h>
29#include <freeradius-devel/io/test_point.h>
30#include <freeradius-devel/protocol/radius/freeradius.internal.h>
31
32#include "attrs.h"
33
34/*
35 * For all of the concat/extended attributes.
36 */
37#include <freeradius-devel/protocol/radius/rfc2869.h>
38#include <freeradius-devel/protocol/radius/rfc5904.h>
39#include <freeradius-devel/protocol/radius/rfc6929.h>
40#include <freeradius-devel/protocol/radius/rfc7268.h>
41
42static bool memcpy_bounded(void * restrict dst, const void * restrict src, size_t n, const void * restrict end)
43{
44 size_t len;
45
46 if (!fr_cond_assert(n <= 65535)) {
47 return false;
48 }
49
50 if (!fr_cond_assert(src <= end)) {
51 return false;
52 }
53
54 if (n == 0) return true;
55
56 len = ((uint8_t const * restrict) end - (uint8_t const * restrict) src);
57 if (n > len) return false;
58
59 memcpy(dst, src, n);
60 return true;
61}
62
63
64/** Decode Tunnel-Password encrypted attributes
65 *
66 * Defined in RFC-2868, this uses a two char SALT along with the
67 * initial intermediate value, to differentiate it from the
68 * above.
69 */
71{
72 fr_md5_ctx_t *md5_ctx, *md5_ctx_old;
74 size_t i, n, encrypted_len, embedded_len;
75
76 encrypted_len = *pwlen;
77
78 /*
79 * We need at least a salt.
80 */
81 if (encrypted_len < 2) {
82 fr_strerror_const("Tunnel password is too short");
83 return -1;
84 }
85
86 /*
87 * There's a salt, but no password. Or, there's a salt
88 * and a 'data_len' octet. It's wrong, but at least we
89 * can figure out what it means: the password is empty.
90 *
91 * Note that this means we ignore the 'data_len' field,
92 * if the attribute length tells us that there's no
93 * more data. So the 'data_len' field may be wrong,
94 * but that's ok...
95 */
96 if (encrypted_len <= 3) {
97 passwd[0] = 0;
98 *pwlen = 0;
99 return 0;
100 }
101
102 encrypted_len -= 2; /* discount the salt */
103
104 md5_ctx = fr_md5_ctx_alloc_from_list();
105 md5_ctx_old = fr_md5_ctx_alloc_from_list();
106
107 fr_md5_update(md5_ctx, (uint8_t const *) packet_ctx->common->secret, packet_ctx->common->secret_length);
108 fr_md5_ctx_copy(md5_ctx_old, md5_ctx); /* save intermediate work */
109
110 /*
111 * Set up the initial key:
112 *
113 * b(1) = MD5(secret + vector + salt)
114 */
115 fr_md5_update(md5_ctx, packet_ctx->request_authenticator, RADIUS_AUTH_VECTOR_LENGTH);
116 fr_md5_update(md5_ctx, passwd, 2);
117
118 embedded_len = 0;
119 for (n = 0; n < encrypted_len; n += AUTH_PASS_LEN) {
120 size_t base;
121 size_t block_len = AUTH_PASS_LEN;
122
123 /*
124 * Ensure we don't overflow the input on MD5
125 */
126 if ((n + 2 + AUTH_PASS_LEN) > *pwlen) {
127 block_len = *pwlen - n - 2;
128 }
129
130 if (n == 0) {
131 base = 1;
132
133 fr_md5_final(digest, md5_ctx);
134 fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
135
136 /*
137 * A quick check: decrypt the first octet
138 * of the password, which is the
139 * 'data_len' field. Ensure it's sane.
140 *
141 * The 'data_len' field one of the encrypted octets, so there are only
142 * (encrypted_len - 1) octets of password.
143 */
144 embedded_len = passwd[2] ^ digest[0];
145 if (embedded_len > (encrypted_len - 1)) {
146 fr_strerror_printf("Tunnel Password is too long for the attribute "
147 "(shared secret is probably incorrect!)");
149 fr_md5_ctx_free_from_list(&md5_ctx_old);
150 return -1;
151 }
152
153 fr_md5_update(md5_ctx, passwd + 2, block_len);
154
155 } else {
156 base = 0;
157
158 fr_md5_final(digest, md5_ctx);
159
160 fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
161 fr_md5_update(md5_ctx, passwd + n + 2, block_len);
162 }
163
164 for (i = base; i < block_len; i++) {
165 passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
166 }
167 }
168
170 fr_md5_ctx_free_from_list(&md5_ctx_old);
171
172 /*
173 * Check trailing bytes
174 */
175 if (packet_ctx->tunnel_password_zeros) for (i = embedded_len; i < (encrypted_len - 1); i++) { /* -1 for length field */
176 if (passwd[i] != 0) {
177 fr_strerror_printf("Trailing garbage in Tunnel Password "
178 "(shared secret is probably incorrect!)");
179
180 return -1;
181 }
182 }
183
184 *pwlen = embedded_len;
185
186 passwd[embedded_len] = '\0';
187
188 return embedded_len;
189}
190
191/** Decode password
192 *
193 */
195{
196 fr_md5_ctx_t *md5_ctx, *md5_ctx_old;
198 size_t i, n;
199
201
202 /*
203 * Catch idiots.
204 */
205 if (pwlen == 0) goto done;
206
207 md5_ctx = fr_md5_ctx_alloc_from_list();
208 md5_ctx_old = fr_md5_ctx_alloc_from_list();
209
210 fr_md5_update(md5_ctx, (uint8_t const *) packet_ctx->common->secret, packet_ctx->common->secret_length);
211 fr_md5_ctx_copy(md5_ctx_old, md5_ctx); /* save intermediate work */
212
213 /*
214 * The inverse of the code above.
215 */
216 for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
217 size_t left = (pwlen - n);
218
219 if (left > AUTH_PASS_LEN) left = AUTH_PASS_LEN;
220
221 if (n == 0) {
222 fr_md5_update(md5_ctx, packet_ctx->request_authenticator, RADIUS_AUTH_VECTOR_LENGTH);
223 fr_md5_final(digest, md5_ctx);
224
225 fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
226 if (pwlen > AUTH_PASS_LEN) {
227 fr_md5_update(md5_ctx, (uint8_t *) passwd, AUTH_PASS_LEN);
228 }
229 } else {
230 fr_md5_final(digest, md5_ctx);
231
232 fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
233 if (pwlen > (n + AUTH_PASS_LEN)) {
234 fr_md5_update(md5_ctx, (uint8_t *) passwd + n, AUTH_PASS_LEN);
235 }
236 }
237
238 for (i = 0; i < left; i++) passwd[i + n] ^= digest[i];
239 }
240
242 fr_md5_ctx_free_from_list(&md5_ctx_old);
243
244 done:
245 passwd[pwlen] = '\0';
246 return strlen((char *) passwd);
247}
248
249/** Check if a set of RADIUS formatted TLVs are OK
250 *
251 */
252int fr_radius_decode_tlv_ok(uint8_t const *data, size_t length, size_t dv_type, size_t dv_length)
253{
254 uint8_t const *end = data + length;
255
256 FR_PROTO_TRACE("Checking TLV %u/%u", (unsigned int) dv_type, (unsigned int) dv_length);
257
258 FR_PROTO_HEX_DUMP(data, length, "tlv_ok");
259
260 if ((dv_length > 2) || (dv_type == 0) || (dv_type > 4)) {
261 fr_strerror_printf("%s: Invalid arguments", __FUNCTION__);
262 return -1;
263 }
264
265 while (data < end) {
266 size_t attrlen;
267
268 if ((data + dv_type + dv_length) > end) {
269 fr_strerror_const("Attribute header overflow");
270 return -1;
271 }
272
273 switch (dv_type) {
274 case 4:
275 if ((data[0] == 0) && (data[1] == 0) &&
276 (data[2] == 0) && (data[3] == 0)) {
277 zero:
278 fr_strerror_const("Invalid attribute 0");
279 return -1;
280 }
281
282 if (data[0] != 0) {
283 fr_strerror_const("Invalid attribute > 2^24");
284 return -1;
285 }
286 break;
287
288 case 2:
289 if ((data[0] == 0) && (data[1] == 0)) goto zero;
290 break;
291
292 case 1:
293 /*
294 * Zero is allowed, because the Colubris
295 * people are dumb and use it.
296 */
297 break;
298
299 default:
300 fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
301 return -1;
302 }
303
304 switch (dv_length) {
305 case 0:
306 return 0;
307
308 case 2:
309 if (data[dv_type] != 0) {
310 fr_strerror_const("Attribute is longer than 256 octets");
311 return -1;
312 }
314 case 1:
315 attrlen = data[dv_type + dv_length - 1];
316 break;
317
318
319 default:
320 fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
321 return -1;
322 }
323
324 if (attrlen < (dv_type + dv_length)) {
325 fr_strerror_const("Attribute header has invalid length");
326 return -1;
327 }
328
329 if (attrlen > length) {
330 fr_strerror_const("Attribute overflows container");
331 return -1;
332 }
333
334 data += attrlen;
335 length -= attrlen;
336 }
337
338 return 0;
339}
340
341/** Convert a "concatenated" attribute to one long VP
342 *
343 */
344static ssize_t decode_concat(TALLOC_CTX *ctx, fr_pair_list_t *list,
345 fr_dict_attr_t const *parent, uint8_t const *data,
346 uint8_t const *end)
347{
348 size_t total;
349 uint8_t attr;
350 uint8_t const *ptr = data;
351 uint8_t *p;
352 fr_pair_t *vp;
353
355
356 total = 0;
357 attr = ptr[0];
358
359 /*
360 * See how many consecutive attributes there are.
361 */
362 while (ptr < end) {
363 if ((ptr + 2) == end) break;
364 if ((ptr + 2) > end) return -1;
365
366 /*
367 * RFC 2865 Section 5 says that the Value field is "zero or more octets". So an empty
368 * attribute is allowed. If we see one, we just ignore it.
369 */
370 if (ptr[1] < 2) return -1;
371 if ((ptr + ptr[1]) > end) return -1;
372
373 total += ptr[1] - 2;
374
375 ptr += ptr[1];
376
377 if (ptr == end) break;
378
379 /*
380 * Attributes MUST be consecutive.
381 */
382 if (ptr[0] != attr) break;
383 }
384
385 /*
386 * Reset the end of the data we're trying to parse
387 */
388 end = ptr;
389
390 /*
391 * If there's no data, just return that we skipped the
392 * attribute header.
393 */
394 if (!total) return 2;
395
396 vp = fr_pair_afrom_da(ctx, parent);
397 if (!vp) return -1;
399
400 if (fr_pair_value_mem_alloc(vp, &p, total, true) != 0) {
401 fail:
403 return -1;
404 }
405
406 ptr = data;
407 while (ptr < end) {
408 if (!memcpy_bounded(p, ptr + 2, ptr[1] - 2, end)) goto fail;
409 p += ptr[1] - 2;
410 ptr += ptr[1];
411 }
412 fr_pair_append(list, vp);
413 return ptr - data;
414}
415
416/*
417 * Short-term hack to help clean things up.
418 */
419#define decode_value fr_radius_decode_pair_value
420
421/** decode an RFC-format TLV
422 *
423 */
424static ssize_t decode_rfc(TALLOC_CTX *ctx, fr_pair_list_t *out,
425 fr_dict_attr_t const *parent,
426 uint8_t const *data, size_t const data_len, void *decode_ctx)
427{
428 unsigned int attr;
429 size_t len;
430 ssize_t slen;
431 fr_dict_attr_t const *da;
433
434#ifdef STATIC_ANALYZER
435 if (!packet_ctx || !packet_ctx->tmp_ctx) return PAIR_DECODE_FATAL_ERROR;
436#endif
437
438 fr_assert(parent != NULL);
439
440 /*
441 * Must have at least a header.
442 */
443 if ((data_len < 2) || (data[1] < 2)) {
444 fr_strerror_printf("%s: Insufficient data", __FUNCTION__);
445 return -(data_len);
446 }
447
448 /*
449 * Empty attributes are ignored.
450 */
451 if (data[1] == 2) return 2;
452
453 attr = data[0];
454 len = data[1];
455 if (len > data_len) {
456 fr_strerror_printf("%s: Attribute overflows input. "
457 "Length must be less than %zu bytes, got %zu bytes",
458 __FUNCTION__, data_len - 2, len - 2);
460 }
461
463 if (!da) {
465 if (!da) return PAIR_DECODE_FATAL_ERROR;
466 slen = fr_pair_raw_from_network(ctx, out, da, data + 2, len - 2);
467 if (slen < 0) return slen;
468 return len;
469 }
470 FR_PROTO_TRACE("decode context changed %s -> %s",da->parent->name, da->name);
471
472 if (da->flags.array) {
473 slen = fr_pair_array_from_network(ctx, out, da, data + 2, len - 2, decode_ctx, decode_value);
474
475 } else if (da->type == FR_TYPE_TLV) {
476 slen = fr_pair_tlvs_from_network(ctx, out, da, data + 2, len - 2, decode_ctx, decode_rfc, NULL, true);
477
478 } else {
479 slen = decode_value(ctx, out, da, data + 2, len - 2, decode_ctx);
480 }
481
482 if (slen > 0) return len; /* it's OK, so return that */
483
484 /*
485 * Malformed, return either that or an error.
486 */
487 slen = fr_pair_raw_from_network(ctx, out, da, data + 2, len - 2);
488 if (slen < 0) return slen;
489
490 return len;
491}
492
493
494/** Decode NAS-Filter-Rule
495 *
496 * Similar to decode_concat, but contains multiple values instead of
497 * one.
498 */
500 fr_dict_attr_t const *parent, uint8_t const *data,
501 size_t const data_len, fr_radius_decode_ctx_t *packet_ctx)
502{
503 uint8_t const *ptr = data;
504 uint8_t const *end = data + data_len;
505 uint8_t const *decode, *decode_end;
506 uint8_t *buffer = NULL;
507 size_t total = 0;
508 int attrs = 0;
509
510 /*
511 * Figure out how long the total length of the data is.
512 * This is so that we can do the decoding from a
513 * temporary buffer. Which means that we coalesce data
514 * across multiple attributes, separately from chopping
515 * the data at zero bytes.
516 */
517 while (ptr < end) {
518 if ((ptr + 2) == end) break;
519 if ((ptr + 2) > end) return -1;
520 if ((ptr[0] != FR_NAS_FILTER_RULE)) break;
521
522 /*
523 * Empty attributes are OK, and we just skip them.
524 */
525 if (ptr[1] < 2) return -1;
526 if ((ptr + ptr[1]) > end) return -1;
527
528 total += ptr[1] - 2;
529 ptr += ptr[1];
530 attrs++;
531 }
532 end = ptr;
533
534 FR_PROTO_TRACE("Coalesced NAS-Filter-Rule has %lu octets", total);
535
536 /*
537 * More than one attribute, create a temporary buffer,
538 * and copy all of the data over to it.
539 */
540 if ((attrs > 1) && total) {
541 uint8_t *p;
542
543 buffer = talloc_array(packet_ctx->tmp_ctx, uint8_t, total);
544 if (!buffer) return PAIR_DECODE_OOM;
545
546 p = buffer;
547 ptr = data;
548
549 /*
550 * Don't bother doing sanity checks, as they were
551 * already done above.
552 */
553 while (ptr < end) {
554 /*
555 * Empty attributes have nothing to copy. If the last attribute is empty,
556 * then "p" is already at the end of the buffer.
557 */
558 if (ptr[1] > 2) {
559 fr_assert((p + ptr[1] - 2) <= (buffer + total));
560 memcpy(p, ptr + 2, ptr[1] - 2);
561 p += ptr[1] - 2;
562 }
563 ptr += ptr[1];
564 }
565
566 decode = buffer;
567 decode_end = buffer + total;
568 } else {
569 /*
570 * One or all of the attributes are empty.
571 */
572 decode = data + 2;
573 decode_end = data + data[1];
574 }
575
576 FR_PROTO_HEX_DUMP(decode, decode_end - decode, "NAS-Filter-Rule coalesced");
577
578 /*
579 * And now walk through "decode", decoding to VPs.
580 */
581 while (decode < decode_end) {
582 size_t len;
583 uint8_t const *p;
584
585 p = decode;
586
587 while (p < decode_end) {
588 if (*p == 0x00) break;
589 p++;
590 }
591
592 len = (p - decode);
593 if (len) {
594 fr_pair_t *vp;
595
596 FR_PROTO_TRACE("This NAS-Filter-Rule has %lu octets", len);
597 FR_PROTO_HEX_DUMP(decode, len, "This NAS-Filter-Rule");
598 vp = fr_pair_afrom_da(ctx, parent);
599 if (!vp) {
601 return -1;
602 }
604
605 if (fr_pair_value_bstrndup(vp, (char const *) decode, len, true) != 0) {
608 return -1;
609 }
611 }
612
613 /*
614 * Skip the zero byte
615 */
616 decode = p + 1;
617 }
618
620 return end - data; /* end of the NAS-Filter-Rule */
621}
622
623
624/** Decode Digest-Attributes
625 *
626 * The VPs are nested, and consecutive Digest-Attributes attributes are decoded into the same parent.
627 */
629 fr_dict_attr_t const *parent, uint8_t const *data,
630 size_t const data_len, fr_radius_decode_ctx_t *packet_ctx)
631{
632 ssize_t slen;
633 fr_pair_t *vp;
634 uint8_t const *p = data;
635 uint8_t const *end = data + data_len;
636
637 fr_assert(parent->type == FR_TYPE_TLV);
638
639 vp = fr_pair_afrom_da(ctx, parent);
640 if (!vp) return PAIR_DECODE_OOM;
642
643 FR_PROTO_HEX_DUMP(p, end - p, "decode_digest_attributes");
644
645redo:
646 if (p[0] != FR_DIGEST_ATTRIBUTES) goto done;
647
648 /*
649 * A defensive check against malformed attributes. The raw attribute goes into "out", after the
650 * parent. It can't go into the parent, as it has the same DA as the parent.
651 */
652 if (((size_t) (end - p) < 2) || (p[1] < 2) || (p[1] > (size_t) (end - p))) {
653 raw:
654 if (fr_pair_list_empty(&vp->vp_group)) {
656 } else {
658 }
659
660 slen = fr_pair_raw_from_network(ctx, out, parent, p, end - p);
661 if (slen < 0) return slen;
662
663 return end - data;
664 }
665
666 /*
667 * Empty attributes are ignored
668 */
669 if (p[1] == 2) goto next;
670
671 slen = fr_pair_tlvs_from_network(vp, &vp->vp_group, parent, p + 2, p[1] - 2, packet_ctx, decode_rfc, NULL, false);
672 if (slen < 0) {
673 end = p + p[1];
674 p += 2;
675 goto raw;
676 }
677
678 /*
679 * Decode consecutive ones into the same parent.
680 */
681next:
682 p += p[1];
683 if (p < end) goto redo;
684
685done:
687 return p - data;
688}
689
690
691/** Convert TLVs to one or more VPs
692 *
693 */
695 fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len,
697{
698 uint8_t const *p = data, *end = data + data_len;
699 fr_dict_attr_t const *child;
701 fr_pair_list_t tlv_tmp;
702 fr_pair_t *vp;
703
705 if (data_len < 3) return -1; /* type, length, value */
706
707#ifdef STATIC_ANALYZER
708 if (!packet_ctx->tmp_ctx) return -1;
709#endif
710
711 FR_PROTO_HEX_DUMP(p, data_len, "tlvs");
712
713 if (fr_radius_decode_tlv_ok(p, data_len, 1, 1) < 0) return -1;
714
715 vp = fr_pair_afrom_da(ctx, parent);
716 if (!vp) return PAIR_DECODE_OOM;
718
719 /*
720 * Record where we were in the list when this function was called
721 * Create a temporary sub-list, so decode errors don't
722 * affect the main list.
723 */
724 fr_pair_list_init(&tlv_tmp);
725 while (p < end) {
726 ssize_t tlv_len;
727
728 /*
729 * Empty attributes are ignored.
730 *
731 * We check this before looking up the child, because an empty unknown child would
732 * otherwise pass fr_radius_decode_tlv_ok() as a TLV. It would then fail to decode, and
733 * turn the whole parent into a raw attribute.
734 */
735 if (p[1] == 2) {
736 p += 2;
737 continue;
738 }
739
740 child = fr_dict_attr_child_by_num(parent, p[0]);
741 if (!child) {
742 FR_PROTO_TRACE("Failed to find child %u of TLV %s", p[0], parent->name);
743
744 /*
745 * Child is unknown and not a TLV: build an unknown attr
746 */
747 if (fr_radius_decode_tlv_ok(p + 2, p[1] - 2, 1, 1) < 0) {
749 if (!child) {
750 error:
752 return -1;
753 }
754 } else {
755 /*
756 * Child is formed as a TLV, decode it as such
757 */
759 if (!child) goto error;
760
761 FR_PROTO_TRACE("decode context changed %s -> %s", parent->name, child->name);
762 tlv_len = fr_radius_decode_tlv(vp, &tlv_tmp, child, p + 2, p[1] - 2, packet_ctx);
763 goto check;
764 }
765 }
766 FR_PROTO_TRACE("decode context changed %s -> %s", parent->name, child->name);
767
768 tlv_len = fr_radius_decode_pair_value(vp, &tlv_tmp,
769 child, p + 2, p[1] - 2,
770 packet_ctx);
771 check:
772 if (tlv_len < 0) goto error;
773 p += p[1];
774 }
775
776 fr_pair_list_append(&vp->vp_group, &tlv_tmp);
778
779 return data_len;
780}
781
782/** Convert a top-level VSA to a VP.
783 *
784 * "length" can be LONGER than just this sub-vsa.
785 */
787 fr_dict_attr_t const *parent,
788 uint8_t const *data, size_t data_len,
790{
791 unsigned int attribute;
792 ssize_t attrlen, my_len;
793 fr_dict_attr_t const *da;
794
795#ifdef STATIC_ANALYZER
796 if (!packet_ctx->tmp_ctx) return -1;
797#endif
798
799 /*
800 * Parent must be a vendor
801 */
802 if (!fr_cond_assert(parent->type == FR_TYPE_VENDOR)) {
803 fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
804 return -1;
805 }
806
807 FR_PROTO_TRACE("Length %u", (unsigned int)data_len);
808
809 if (data_len <= (dv->type + dv->length)) {
810 fr_strerror_printf("%s: Failure to call fr_radius_decode_tlv_ok", __FUNCTION__);
811 return -1;
812 }
813
814 switch (dv->type) {
815 case 4:
816 /* data[0] must be zero */
817 attribute = data[1] << 16;
818 attribute |= data[2] << 8;
819 attribute |= data[3];
820 break;
821
822 case 2:
823 attribute = data[0] << 8;
824 attribute |= data[1];
825 break;
826
827 case 1:
828 attribute = data[0];
829 break;
830
831 default:
832 fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
833 return -1;
834 }
835
836 switch (dv->length) {
837 case 2:
838 /* data[dv->type] must be zero, from fr_radius_decode_tlv_ok() */
839 attrlen = data[dv->type + 1];
840 break;
841
842 case 1:
843 attrlen = data[dv->type];
844 break;
845
846 case 0:
847 attrlen = data_len;
848 break;
849
850 default:
851 fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
852 return -1;
853 }
854
855 /*
856 * See if the VSA is known.
857 */
858 da = fr_dict_attr_child_by_num(parent, attribute);
859 if (da) {
860 decode:
861 FR_PROTO_TRACE("decode context changed %s -> %s", da->parent->name, da->name);
862
863 my_len = fr_radius_decode_pair_value(ctx, out,
864 da, data + dv->type + dv->length,
865 attrlen - (dv->type + dv->length),
866 packet_ctx);
867 if (my_len < 0) return my_len;
868
869 /*
870 * It's unknown. Let's see if we can decode it as a TLV. While this check can sometimes
871 * (rarely) decode non-TLVs as TLVs, that situation will be rare. And it's very useful
872 * to be able to decode nested unknown TLVs.
873 *
874 * Note that if the TLV length is zero, then we have no real way to tell if the TLV is
875 * well formed, so we just go create a raw VP.
876 */
877 } else if ((dv->length == 0) || (fr_radius_decode_tlv_ok(data + dv->type + dv->length, attrlen - (dv->type + dv->length), dv->type, dv->length) < 0)) {
878 da = fr_dict_attr_unknown_raw_afrom_num(packet_ctx->tmp_ctx, parent, attribute);
879 if (!da) return -1;
880
881 goto decode;
882
883 } else {
885 if (!da) return -1;
886
887 goto decode;
888 }
889
890 return attrlen;
891}
892
893
894/** Convert a fragmented extended attr to a VP
895 *
896 * Format is:
897 *
898 * attr
899 * length
900 * extended-attr
901 * flag
902 * data...
903 *
904 * But for the first fragment, we get passed a pointer to the "extended-attr"
905 */
907 fr_dict_attr_t const *parent,
908 uint8_t const *data, size_t attr_len,
910{
911 ssize_t ret;
912 size_t fraglen;
913 uint8_t *head, *tail;
914 uint8_t const *frag, *end;
915 uint8_t const *attr;
916 int fragments;
917 bool last_frag;
918
919 /*
920 * data = Ext-Attr Flag ...
921 */
922
923 if (attr_len < 3) return -1;
924
925 /*
926 * No continuation, just decode the attribute in place.
927 */
928 if ((data[1] & 0x80) == 0) {
930 parent, data + 2, attr_len - 2, packet_ctx);
931 if (ret < 0) return -1;
932 return attr_len;
933 }
934
935 /*
936 * Calculate the length of all of the fragments. For
937 * now, they MUST be contiguous in the packet, and they
938 * MUST be all of the same TYPE and EXTENDED-TYPE
939 */
940 attr = data - 2;
941 fraglen = attr_len - 2;
942 frag = data + attr_len;
943 end = packet_ctx->end;
944 fragments = 1;
945 last_frag = false;
946
947 while (frag < end) {
948 if (last_frag || ((end - frag) < 4) ||
949 (frag[0] != attr[0]) ||
950 (frag[1] < 4) || /* too short for long_extended */
951 (frag[2] != attr[2]) ||
952 ((frag + frag[1]) > end)) { /* overflow */
953 end = frag;
954 break;
955 }
956
957 last_frag = ((frag[3] & 0x80) == 0);
958
959 fraglen += frag[1] - 4;
960 frag += frag[1];
961 fragments++;
962 }
963
964 head = tail = talloc_array(packet_ctx->tmp_ctx, uint8_t, fraglen);
965 if (!head) return -1;
966
967 FR_PROTO_TRACE("Fragments %d, total length %d", fragments, (int) fraglen);
968
969 /*
970 * And again, but faster and looser.
971 *
972 * We copy the first fragment, followed by the rest of
973 * the fragments.
974 */
975 frag = attr;
976
977 while (fragments > 0) {
978 if ((frag[1] > 4) && !memcpy_bounded(tail, frag + 4, frag[1] - 4, end)) {
980 return -1;
981 }
982 tail += frag[1] - 4;
983 frag += frag[1];
984 fragments--;
985 }
986
987 FR_PROTO_HEX_DUMP(head, fraglen, "long_extended fragments");
988
989 /*
990 * Reset the "end" pointer, because we're not passing in
991 * the real data.
992 */
993 {
994 uint8_t const *tmp = packet_ctx->end;
995 packet_ctx->end = head + fraglen;
996
998 parent, head, fraglen, packet_ctx);
999
1000 packet_ctx->end = tmp;
1001 }
1002
1004 if (ret < 0) return ret;
1005
1006 return end - data;
1007}
1008
1009/** Fast path for most extended attributes.
1010 *
1011 * data_len has already been checked by the caller, so we don't care
1012 * about it here.
1013 */
1015 fr_dict_attr_t const *da,
1016 uint8_t const *data, UNUSED size_t data_len,
1018{
1019 ssize_t slen;
1020 fr_dict_attr_t const *child;
1021 fr_pair_t *vp;
1022
1023 /*
1024 * They MUST have one byte of Extended-Type. The
1025 * case of "2" is already handled above with CUI.
1026 */
1027 if (data[1] == 3) {
1028 slen = fr_pair_raw_from_network(ctx, out, da, data + 2, 1);
1029 if (slen <= 0) return slen;
1030 return 2 + slen;
1031 }
1032
1033 /*
1034 * Get a new child.
1035 */
1036 child = fr_dict_attr_child_by_num(da, data[2]);
1037 if (!child) {
1038 fr_dict_attr_t *unknown;
1039 FR_PROTO_TRACE("Unknown extended attribute %u.%u", data[0], data[2]);
1040 unknown = fr_dict_attr_unknown_raw_afrom_num(packet_ctx->tmp_ctx, da, data[2]);
1041 if (!unknown) return -1;
1042
1043 child = unknown;
1044 }
1045
1046 /*
1047 * One byte of type, and N bytes of data.
1048 */
1050 if (fr_pair_find_or_append_by_da(ctx, &vp, out, da) < 0) return PAIR_DECODE_OOM;
1051
1052 slen = fr_radius_decode_pair_value(vp, &vp->vp_group, child, data + 3, data[1] - 3, packet_ctx);
1054 if (slen < 0 ) return slen;
1055
1056 fr_assert(slen < (1 << 16));
1057 return 3 + slen;
1058 }
1059
1060 /*
1061 * It MUST have one byte of type, and one byte of
1062 * flags. If there's no data here, we just
1063 * ignore it, whether or not the "More" bit is
1064 * set.
1065 */
1066 if (data[1] == 4) {
1068 slen = fr_pair_raw_from_network(ctx, out, da, data + 2, 2);
1069 if (slen < 0) return slen;
1070 return 4;
1071 }
1072
1073 if (fr_pair_find_or_append_by_da(ctx, &vp, out, da) < 0) return PAIR_DECODE_OOM;
1074
1075 /*
1076 * No continuation - just decode as-is.
1077 */
1078 if ((data[3] & 0x80) == 0) {
1079 slen = fr_radius_decode_pair_value(vp, &vp->vp_group, child, data + 4, data[1] - 4, packet_ctx);
1081 if (slen < 0 ) return slen;
1082 return 4 + slen;
1083 }
1084
1085 /*
1086 * Concatenate all of the fragments together, and decode the resulting thing.
1087 */
1088 slen = decode_extended_fragments(vp, &vp->vp_group, child, data + 2, data[1] - 2, packet_ctx);
1090 if (slen < 0) return slen;
1091 return 2 + slen;
1092}
1093
1094/** Convert a Vendor-Specific WIMAX to vps
1095 *
1096 * @note Called ONLY for Vendor-Specific
1097 */
1098static ssize_t decode_wimax(TALLOC_CTX *ctx, fr_pair_list_t *out,
1099 fr_dict_attr_t const *parent,
1100 uint8_t const *data, size_t attr_len,
1102{
1103 ssize_t slen;
1104 size_t wimax_len;
1105 bool more;
1106 uint8_t *head = NULL, *tail;
1107 uint8_t const *attr, *end, *tmp;
1108 fr_dict_attr_t const *da;
1109 fr_pair_t *vsa, *vendor;
1110
1111#ifdef STATIC_ANALYZER
1112 if (!packet_ctx->tmp_ctx) return -1;
1113#endif
1114
1115 fr_assert(packet_ctx->end != NULL);
1116 fr_assert((data + attr_len) <= packet_ctx->end);
1117
1118 /*
1119 * data = VID VID VID VID WiMAX-Attr WiMAX-Len Continuation ...
1120 */
1121 if (attr_len < 8) {
1122 FR_PROTO_TRACE("attribute is too small to be WiMAX");
1123 return -1;
1124 }
1125
1126 /*
1127 * WiMAX-Attr WiMAX-Len Continuation
1128 */
1129 if (data[5] < 3) {
1130 FR_PROTO_TRACE("attribute is too small to be WiMAX-Attr-WiMAX-Len Continuation");
1131 return -1;
1132 }
1133
1134 /*
1135 * The WiMAX-Len + 4 VID must exactly fill the attribute.
1136 */
1137 if (((size_t) (data[5] + 4)) != attr_len) {
1138 FR_PROTO_TRACE("WiMAX VSA does not exactly fill the attribute");
1139 return -1;
1140 }
1141
1143 if (!da) da = fr_dict_attr_unknown_raw_afrom_num(packet_ctx->tmp_ctx, parent, data[4]);
1144 if (!da) return -1;
1145 FR_PROTO_TRACE("decode context changed %s -> %s", da->parent->name, da->name);
1146
1147 /*
1148 * No continuation, just decode the attribute in place.
1149 */
1150 if ((data[6] & 0x80) == 0) {
1151 FR_PROTO_TRACE("WiMAX no continuation");
1152 tmp = packet_ctx->end;
1153 attr = data + 7;
1154 wimax_len = data[5] - 3;
1155 end = data + attr_len;
1156 goto decode_it;
1157 }
1158
1159 /*
1160 * Calculate the length of all of the fragments. For
1161 * now, they MUST be contiguous in the packet, and they
1162 * MUST be all of the same VSA, WiMAX, and WiMAX-attr.
1163 *
1164 * The first fragment doesn't have a RADIUS attribute
1165 * header.
1166 */
1167 wimax_len = 0;
1168 attr = data + 4;
1169 end = packet_ctx->end;
1170
1171 while (attr < end) {
1172 /*
1173 * Not enough room for Attribute + length +
1174 * continuation, it's bad.
1175 */
1176 if ((end - attr) < 3) {
1177 FR_PROTO_TRACE("end - attr < 3");
1178 return -1;
1179 }
1180
1181 /*
1182 * Must have non-zero data in the attribute.
1183 */
1184 if (attr[1] <= 3) {
1185 FR_PROTO_TRACE("attr[1] <= 3");
1186 return -1;
1187 }
1188
1189 /*
1190 * If the WiMAX attribute overflows the packet,
1191 * it's bad.
1192 */
1193 if ((attr + attr[1]) > end) {
1194 FR_PROTO_TRACE("attr + attr[1]) > end");
1195 return -1;
1196 }
1197
1198 /*
1199 * Check the continuation flag.
1200 */
1201 more = ((attr[2] & 0x80) != 0);
1202
1203 /*
1204 * Or, there's no more data, in which case we
1205 * shorten "end" to finish at this attribute.
1206 */
1207 if (!more) end = attr + attr[1];
1208
1209 /*
1210 * There's more data, but we're at the end of the
1211 * packet. The attribute is malformed!
1212 */
1213 if (more && ((attr + attr[1]) == end)) {
1214 FR_PROTO_TRACE("more && ((attr + attr[1]) == end)");
1215 return -1;
1216 }
1217
1218 /*
1219 * Add in the length of the data we need to
1220 * concatenate together.
1221 */
1222 wimax_len += attr[1] - 3;
1223
1224 /*
1225 * Go to the next attribute, and stop if there's
1226 * no more.
1227 */
1228 attr += attr[1];
1229 if (!more) break;
1230
1231 /*
1232 * data = VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
1233 *
1234 * attr = Vendor-Specific VSA-Length VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
1235 *
1236 */
1237
1238 /*
1239 * No room for Vendor-Specific + length +
1240 * Vendor(4) + attr + length + continuation + data
1241 */
1242 if ((end - attr) < 9) {
1243 FR_PROTO_TRACE("(end - attr) < 9");
1244 return -1;
1245 }
1246
1247 if (attr[0] != FR_VENDOR_SPECIFIC) {
1248 FR_PROTO_TRACE("attr[0] != FR_VENDOR_SPECIFIC");
1249 return -1;
1250 }
1251
1252 if (attr[1] < 9) {
1253 FR_PROTO_TRACE("attr[1] < 9");
1254 return -1;
1255 }
1256
1257 if ((attr + attr[1]) > end) {
1258 FR_PROTO_TRACE("(attr + attr[1]) > end");
1259 return -1;
1260 }
1261
1262 if (memcmp(data, attr + 2, 4) != 0) {
1263 FR_PROTO_TRACE("not the same vendor");
1264 return -1; /* not WiMAX Vendor ID */
1265 }
1266
1267 if (attr[1] != (attr[7] + 6)) {
1268 FR_PROTO_TRACE("attr[1] != (attr[7] + 6)");
1269 return -1; /* WiMAX attr doesn't exactly fill the VSA */
1270 }
1271
1272 if (data[4] != attr[6]) {
1273 FR_PROTO_TRACE("data[4] != attr[6]");
1274 return -1; /* different WiMAX attribute */
1275 }
1276
1277 /*
1278 * Skip over the Vendor-Specific header, and
1279 * continue with the WiMAX attributes.
1280 */
1281 attr += 6;
1282 }
1283
1284 /*
1285 * No data in the WiMAX attribute, make a "raw" one.
1286 */
1287 if (!wimax_len) {
1288 FR_PROTO_TRACE("!wimax_len");
1289 return -1;
1290 }
1291
1292 head = tail = talloc_array(packet_ctx->tmp_ctx, uint8_t, wimax_len);
1293 if (!head) return -1;
1294
1295 /*
1296 * Copy the data over, this time trusting the attribute
1297 * contents.
1298 */
1299 attr = data;
1300 while (attr < end) {
1301 if (!memcpy_bounded(tail, attr + 4 + 3, attr[4 + 1] - 3, end)) {
1303 return -1;
1304 }
1305 tail += attr[4 + 1] - 3;
1306 attr += 4 + attr[4 + 1]; /* skip VID+WiMax header */
1307 attr += 2; /* skip Vendor-Specific header */
1308 }
1309
1310 FR_PROTO_HEX_DUMP(head, wimax_len, "Wimax fragments");
1311
1312 /*
1313 * Reset the "end" pointer, because we're not passing in
1314 * the real data.
1315 */
1316 tmp = packet_ctx->end;
1317 packet_ctx->end = head + wimax_len;
1318 attr = head;
1319
1320 FR_PROTO_TRACE("WiMAX decode concatenated");
1321
1322decode_it:
1324 oom:
1326 return PAIR_DECODE_OOM;
1327 }
1328
1329 if (fr_pair_find_or_append_by_da(vsa, &vendor, &vsa->vp_group, parent) < 0) goto oom;
1330
1331 FR_PROTO_HEX_DUMP(attr, wimax_len, "%s", __FUNCTION__ );
1332 slen = fr_radius_decode_pair_value(vendor, &vendor->vp_group,
1333 da, attr, wimax_len, packet_ctx);
1334
1335 packet_ctx->end = tmp;
1336
1337 if (slen < 0) {
1338 slen = fr_pair_raw_from_network(vendor, &vendor->vp_group, da, attr, wimax_len);
1340 if (slen < 0) return slen;
1341 } else {
1343 }
1344
1345 return end - data;
1346}
1347
1348
1349/** Convert a top-level VSA to one or more VPs
1350 *
1351 */
1352static ssize_t CC_HINT(nonnull) decode_vsa(TALLOC_CTX *ctx, fr_pair_list_t *out,
1353 fr_dict_attr_t const *parent,
1354 uint8_t const *data, size_t attr_len,
1356{
1357 size_t total;
1358 ssize_t ret;
1359 uint32_t vendor_pen;
1360 fr_dict_vendor_t const *dv;
1362 fr_dict_vendor_t my_dv;
1363 fr_dict_attr_t const *vendor_da;
1364 fr_pair_list_t tlv_tmp;
1365 fr_pair_t *vsa, *vendor;
1366
1368
1369#ifdef STATIC_ANALYZER
1370 if (!packet_ctx->tmp_ctx) return -1;
1371#endif
1372
1373 /*
1374 * Container must be a VSA
1375 */
1376 if (!fr_cond_assert(parent->type == FR_TYPE_VSA)) return -1;
1377
1378 if ((data + attr_len) > packet_ctx->end) return -1;
1379 if (attr_len < 5) return -1; /* vid, value */
1380 if (data[0] != 0) return -1; /* we require 24-bit VIDs */
1381
1382 FR_PROTO_TRACE("Decoding VSA");
1383
1384 memcpy(&vendor_pen, data, 4);
1385 vendor_pen = ntohl(vendor_pen);
1386
1387 /*
1388 * Verify that the parent (which should be a VSA)
1389 * contains a fake attribute representing the vendor.
1390 *
1391 * If it doesn't then this vendor is unknown, but
1392 * (unlike DHCP) we know vendor attributes have a
1393 * standard format, so we can decode the data anyway.
1394 */
1395 vendor_da = fr_dict_attr_child_by_num(parent, vendor_pen);
1396 if (!vendor_da) {
1398 /*
1399 * RFC format is 1 octet type, 1 octet length
1400 */
1401 if (fr_radius_decode_tlv_ok(data + 4, attr_len - 4, 1, 1) < 0) {
1402 FR_PROTO_TRACE("Unknown TLVs not OK: %s", fr_strerror());
1403 return -1;
1404 }
1405
1407 if (!n) return -1;
1408 vendor_da = n;
1409
1410 fr_assert(vendor_da->flags.type_size == 1);
1411
1412 /*
1413 * Create an unknown DV too...
1414 */
1415 memset(&my_dv, 0, sizeof(my_dv));
1416
1417 my_dv.pen = vendor_pen;
1418 my_dv.type = 1;
1419 my_dv.length = 1;
1420
1421 dv = &my_dv;
1422
1423 goto create_attrs;
1424 }
1425
1426 /*
1427 * We found an attribute representing the vendor so it *MUST* exist in the vendor tree.
1428 */
1429 dv = fr_dict_vendor_by_num(dict_radius, vendor_pen);
1430 if (!fr_cond_assert(dv)) return -1;
1431 FR_PROTO_TRACE("decode context %s -> %s", parent->name, vendor_da->name);
1432
1433 /*
1434 * WiMAX craziness
1435 */
1436 if (dv->continuation) {
1437 ret = decode_wimax(ctx, out, vendor_da, data, attr_len, packet_ctx);
1438 return ret;
1439 }
1440
1441 /*
1442 * VSAs should normally be in TLV format.
1443 */
1444 if (fr_radius_decode_tlv_ok(data + 4, attr_len - 4, dv->type, dv->length) < 0) {
1445 FR_PROTO_TRACE("TLVs not OK: %s", fr_strerror());
1446 return -1;
1447 }
1448
1449 /*
1450 * There may be more than one VSA in the
1451 * Vendor-Specific. If so, loop over them all.
1452 */
1453create_attrs:
1454 data += 4;
1455 attr_len -= 4;
1456 total = 4;
1457
1458 /*
1459 * Decode the vendor attributes into a temporary list. The Vendor-Specific and vendor groups
1460 * are only created after the vendor attributes have been decoded.
1461 */
1462 fr_pair_list_init(&tlv_tmp);
1463 while (attr_len > 0) {
1464 ssize_t vsa_len;
1465
1466 /*
1467 * Vendor attributes can have subattributes (if you hadn't guessed)
1468 */
1469 vsa_len = decode_vsa_internal(ctx, &tlv_tmp,
1470 vendor_da, data, attr_len, packet_ctx, dv);
1471 if (vsa_len < 0) {
1472 FR_PROTO_TRACE("TLV decode failed: %s", fr_strerror());
1473 fr_strerror_printf("%s: Internal sanity check %d", __FUNCTION__, __LINE__);
1474 fr_pair_list_free(&tlv_tmp);
1475 return -1;
1476 }
1477
1478 data += vsa_len;
1479 attr_len -= vsa_len;
1480 total += vsa_len;
1481 }
1482
1483 /*
1484 * Tagged VSAs don't go into the temporary list, they go into the Tag-N group. If all of the
1485 * vendor attributes were tagged, then there's nothing to add here.
1486 */
1487 if (fr_pair_list_empty(&tlv_tmp)) return total;
1488
1489 if (fr_pair_find_or_append_by_da(ctx, &vsa, out, parent) < 0) {
1490 oom:
1491 fr_pair_list_free(&tlv_tmp);
1492 return PAIR_DECODE_OOM;
1493 }
1494
1495 if (fr_pair_find_or_append_by_da(vsa, &vendor, &vsa->vp_group, vendor_da) < 0) goto oom;
1496
1497 fr_pair_list_steal(vendor, &tlv_tmp);
1498 fr_pair_list_append(&vendor->vp_group, &tlv_tmp);
1499
1500 return total;
1501}
1502
1503/** Wrapper called by fr_struct_from_network()
1504 *
1505 * Because extended attributes can continue across the current value.
1506 * So that function needs to know both the value length, *and* the
1507 * packet length. But when we're decoding values inside of a struct,
1508 * we're not using extended attributes.
1509 */
1511 fr_dict_attr_t const *parent,
1512 uint8_t const *data, size_t data_len, void *decode_ctx)
1513{
1514 return fr_radius_decode_pair_value(ctx, out, parent, data, data_len, decode_ctx);
1515}
1516
1517/** Wrapper called by fr_struct_from_network()
1518 */
1520 fr_dict_attr_t const *parent,
1521 uint8_t const *data, size_t data_len, void *decode_ctx)
1522{
1523 FR_PROTO_HEX_DUMP(data, data_len, "%s", __FUNCTION__ );
1524
1525 return fr_radius_decode_tlv(ctx, out, parent, data, data_len, decode_ctx);
1526}
1527
1528
1529/** Free an empty Tag-N group
1530 *
1531 * The Tag-N group is created before the tagged value is decoded, but it is only added to
1532 * packet_ctx->tag_root after the value has been successfully decoded. If the tag group isn't in a list, then
1533 * we need to free it.
1534 */
1536{
1537 fr_pair_t *group;
1538
1539 if (!tag || !packet_ctx->tags || !packet_ctx->tags[tag]) return;
1540
1541 group = packet_ctx->tags[tag]->parent;
1542 if (fr_pair_parent_list(group)) return;
1543
1544 talloc_free(group);
1545 TALLOC_FREE(packet_ctx->tags[tag]);
1546}
1547
1548/** Create any kind of VP from the attribute contents
1549 *
1550 * "length" is AT LEAST the length of this attribute, as we
1551 * expect the caller to have verified the data with
1552 * fr_radius_packet_ok(). "length" may be up to the length of the
1553 * packet.
1554 *
1555 * This function will ONLY return -1 on programmer error or OOM. If
1556 * there's anything wrong with the attribute, it will ALWAYS create a
1557 * "raw" attribute.
1558 *
1559 * @return
1560 * - Length on success.
1561 * - -1 on failure.
1562 */
1564 fr_dict_attr_t const *parent,
1565 uint8_t const *data, size_t const attr_len,
1566 void *decode_ctx)
1567{
1568 int8_t tag = 0;
1569 size_t data_len;
1570 ssize_t ret;
1571 fr_dict_attr_t const *child;
1572 fr_pair_t *vp = NULL;
1573 uint8_t const *p = data;
1574 uint8_t buffer[256]; /* must be multiple of 16 */
1576 fr_radius_decode_ctx_t *packet_ctx = decode_ctx;
1577
1578 if (attr_len > 128 * 1024) {
1579 fr_strerror_printf("%s: packet is too large to be RADIUS", __FUNCTION__);
1580 return -1;
1581 }
1582
1583 if ((data + attr_len) > packet_ctx->end) {
1584 fr_strerror_printf("%s: input overflows packet", __FUNCTION__);
1585 return -1;
1586 }
1587
1588 FR_PROTO_HEX_DUMP(data, attr_len, "%s", __FUNCTION__ );
1589
1590 FR_PROTO_TRACE("Parent %s len %zu ... %zu", parent->name, attr_len, (size_t) (packet_ctx->end - data));
1591
1592 data_len = attr_len;
1593
1594 /*
1595 * Silently ignore zero-length attributes.
1596 */
1597 if (attr_len == 0) return 0;
1598
1599 /*
1600 * Hacks for tags.
1601 */
1603 /*
1604 * Check for valid tags and data types.
1605 */
1606 if (parent->type == FR_TYPE_UINT32) {
1607 if ((attr_len != 4) || (p[0] >= 0x20)) {
1608 goto raw;
1609 }
1610
1611 } else if (parent->type != FR_TYPE_STRING) {
1612 goto raw;
1613 }
1614
1615 /*
1616 * Tag values MUST be less than 32.
1617 */
1618 if (p[0] < 0x20) {
1619 /*
1620 * Only "short" attributes can be encrypted.
1621 */
1622 if (data_len >= sizeof(buffer)) goto raw;
1623
1624 if (parent->type == FR_TYPE_STRING) {
1625 memcpy(buffer, p + 1, data_len - 1);
1626 tag = p[0];
1627 data_len -= 1;
1628
1629 } else if (parent->type == FR_TYPE_UINT32) {
1630 memcpy(buffer, p, attr_len);
1631 tag = buffer[0];
1632 buffer[0] = 0;
1633 }
1634
1635 p = buffer;
1636
1637 } /* else the field is >=0x20, so it's not a tag */
1638 }
1639
1640 if (tag) {
1641 fr_radius_tag_ctx_t **new_tag_ctx = NULL;
1642
1643 if (!packet_ctx->tags) {
1644 /*
1645 * This should NOT be packet_ctx.tmp_ctx,
1646 * as that is freed after decoding every
1647 * packet. We wish to aggregate the tags
1648 * across multiple attributes.
1649 */
1650 new_tag_ctx = talloc_zero_array(NULL, fr_radius_tag_ctx_t *, 32);
1651 if (unlikely(!new_tag_ctx)) return PAIR_DECODE_OOM;
1652
1653 FR_PROTO_TRACE("Allocated tag cache %p", new_tag_ctx);
1654
1655 packet_ctx->tags = new_tag_ctx;
1656 }
1657
1658 fr_assert(tag < 0x20);
1659
1660 if (!packet_ctx->tags[tag]) {
1661 fr_pair_t *group;
1662 fr_dict_attr_t const *group_da;
1663
1664 packet_ctx->tags[tag] = talloc_zero(packet_ctx->tags, fr_radius_tag_ctx_t);
1665 if (unlikely(!packet_ctx->tags[tag])) {
1666 if (new_tag_ctx) TALLOC_FREE(packet_ctx->tags);
1667 return PAIR_DECODE_OOM;
1668 }
1669
1670 group_da = fr_dict_attr_child_by_num(fr_dict_root(dict_radius), FR_TAG_BASE + tag);
1671 if (unlikely(!group_da)) {
1672 tag_alloc_error:
1673 TALLOC_FREE(packet_ctx->tags[tag]);
1674 return PAIR_DECODE_OOM;
1675 }
1676
1677 group = fr_pair_afrom_da(packet_ctx->tag_root_ctx, group_da);
1678 if (unlikely(!group)) goto tag_alloc_error;
1679 PAIR_ALLOCED(group);
1680
1681 packet_ctx->tags[tag]->parent = group;
1682
1683 FR_PROTO_TRACE("Allocated tag attribute %p (%u)", group, tag);
1684
1685 /*
1686 * The group is added to packet_ctx->tag_root after the entire value is decoded.
1687 */
1688#ifdef TALLOC_GET_TYPE_ABORT_NOOP
1689 }
1690#else
1691 } else {
1692 talloc_get_type_abort(packet_ctx->tags, fr_radius_tag_ctx_t *);
1693 talloc_get_type_abort(packet_ctx->tags[tag], fr_radius_tag_ctx_t);
1694 talloc_get_type_abort(packet_ctx->tags[tag]->parent, fr_pair_t);
1695 }
1696#endif
1697 }
1698
1699 /*
1700 * Obfuscate attributes which are marked that way, unless
1701 * we're doing RADIUS/1.1.
1702 */
1704
1705 /*
1706 * Decrypt the attribute.
1707 */
1708 if (encrypt) {
1709 FR_PROTO_TRACE("Decrypting type %d", encrypt);
1710 /*
1711 * Encrypted attributes can only exist for the
1712 * old-style format. Extended attributes CANNOT
1713 * be encrypted.
1714 */
1715 if (data_len > 253) goto raw;
1716
1717 if (p == data) memcpy(buffer, p, data_len);
1718 p = buffer;
1719
1720 switch (encrypt) { /* can't be tagged */
1721 /*
1722 * User-Password
1723 */
1725 if (!packet_ctx->request_authenticator) goto raw;
1726
1728
1729 /*
1730 * MS-CHAP-MPPE-Keys are 24 octets, and
1731 * encrypted. Since it's binary, we can't
1732 * look for trailing zeros.
1733 */
1734 if (parent->flags.length) {
1735 if (data_len > parent->flags.length) {
1736 data_len = parent->flags.length;
1737 } /* else leave data_len alone */
1738 } else {
1739 /*
1740 * Take off trailing zeros from the END.
1741 * This allows passwords to have zeros in
1742 * the middle of a field.
1743 *
1744 * However, if the password has a zero at
1745 * the end, it will get mashed by this
1746 * code. There's really no way around
1747 * that.
1748 */
1749 while ((data_len > 0) && (buffer[data_len - 1] == '\0')) data_len--;
1750 }
1751 break;
1752
1753 /*
1754 * Tunnel-Password's go in response packets,
1755 * except for CoA-Requests. They can have a tag,
1756 * so data_len is not the same as attrlen.
1757 */
1759 if (!packet_ctx->request_authenticator) goto raw;
1760
1761 if (fr_radius_decode_tunnel_password(buffer, &data_len, packet_ctx) < 0) {
1762 goto raw;
1763 }
1764 break;
1765
1766 /*
1767 * Ascend-Send-Secret
1768 * Ascend-Receive-Secret
1769 */
1771 if (!packet_ctx->request_authenticator) goto raw;
1772
1773 fr_radius_ascend_secret(&FR_DBUFF_TMP(buffer, sizeof(buffer)), p, data_len,
1774 packet_ctx->common->secret, packet_ctx->common->secret_length,
1775 packet_ctx->request_authenticator);
1777 data_len = strlen((char *) buffer);
1778 break;
1779
1780 default:
1781 /*
1782 * Chop the attribute to its maximum length.
1783 */
1784 if ((parent->type == FR_TYPE_OCTETS) &&
1785 (parent->flags.length && (data_len > parent->flags.length))) {
1786 data_len = parent->flags.length;
1787 }
1788 break;
1789 } /* switch over encryption flags */
1790 }
1791
1792 /*
1793 * Double-check the length after decrypting the
1794 * attribute.
1795 */
1796 FR_PROTO_TRACE("Type \"%s\" (%u)", fr_type_to_str(parent->type), parent->type);
1797
1798 switch (parent->type) {
1799 case FR_TYPE_LEAF:
1800 break;
1801
1802 case FR_TYPE_VSA:
1803 /*
1804 * VSAs in the RFC space are encoded one way.
1805 * VSAs in the "extended" space are different.
1806 */
1807 if (!parent->parent || !fr_radius_flag_extended(parent->parent)) {
1808 /*
1809 * VSAs can be WiMAX, in which case they don't
1810 * fit into one attribute.
1811 */
1812 ret = decode_vsa(ctx, out, parent, p, attr_len, packet_ctx);
1813 if (ret < 0) goto raw;
1814 return ret;
1815
1816 } else {
1817 fr_dict_attr_t const *vendor_da;
1818 fr_pair_t *vsa, *vendor;
1819 uint32_t vendor_pen;
1820
1821
1822 if (data_len < 6) goto raw; /* vid, vtype, value */
1823
1824 memcpy(&vendor_pen, p, 4);
1825 vendor_pen = ntohl(vendor_pen);
1826
1827 /*
1828 * For simplicity in our attribute tree, vendors are
1829 * represented as a subtlv(ish) of an EVS or VSA
1830 * attribute.
1831 */
1832 vendor_da = fr_dict_attr_child_by_num(parent, vendor_pen);
1833 if (!vendor_da) {
1834 /*
1835 * If there's no child, it means the vendor is unknown. Create a
1836 * temporary vendor in the packet_ctx. This will be cleaned up when the
1837 * decoder exists, which is fine. Because any unknown attributes which
1838 * depend on it will copy the entire hierarchy.
1839 */
1840 vendor_da = fr_dict_attr_unknown_vendor_afrom_num(packet_ctx->tmp_ctx, parent, vendor_pen);
1841 if (!vendor_da) return PAIR_DECODE_OOM;
1842 }
1843
1844 child = fr_dict_attr_child_by_num(vendor_da, p[4]);
1845 if (!child) {
1846 /*
1847 * Vendor exists but child didn't, create an unknown child.
1848 */
1849 child = fr_dict_attr_unknown_raw_afrom_num(packet_ctx->tmp_ctx, vendor_da, p[4]);
1850 if (!child) {
1851 fr_strerror_printf_push("decoder failed creating unknown attribute in %s",
1852 parent->name);
1853 return -1;
1854 }
1855 }
1856
1857 if (fr_pair_find_or_append_by_da(ctx, &vsa, out, parent) < 0) return PAIR_DECODE_OOM;
1858
1859 if (fr_pair_find_or_append_by_da(vsa, &vendor, &vsa->vp_group, vendor_da) < 0) return PAIR_DECODE_OOM;
1860
1861 /*
1862 * Everything was found in the dictionary, we can
1863 * now recurse to decode the value.
1864 */
1865 ret = fr_radius_decode_pair_value(vendor, &vendor->vp_group,
1866 child, p + 5, attr_len - 5,
1867 packet_ctx);
1868 if (ret < 0) goto raw;
1869 return attr_len;
1870 }
1871
1872 case FR_TYPE_TLV:
1873 /*
1874 * We presume that the TLVs all fit into one
1875 * attribute, OR they've already been grouped
1876 * into a contiguous memory buffer.
1877 */
1878 ret = fr_radius_decode_tlv(ctx, out, parent, p, attr_len, packet_ctx);
1879 if (ret < 0) goto raw;
1880 return attr_len;
1881
1882 case FR_TYPE_STRUCT:
1883 /*
1884 * We presume that the struct fits into one
1885 * attribute, OR it's already been grouped
1886 * into a contiguous memory buffer.
1887 */
1888 ret = fr_struct_from_network(ctx, out, parent, p, attr_len,
1890 if (ret < 0) goto raw;
1891 return attr_len;
1892
1893 case FR_TYPE_GROUP:
1894 {
1895 fr_dict_attr_t const *ref;
1896 fr_dict_protocol_t const *proto;
1897
1898 ref = fr_dict_attr_ref(parent);
1899 if (!ref) goto raw;
1900
1901 fr_assert(ref->dict != parent->dict);
1902
1903 proto = fr_dict_protocol(ref->dict);
1904 fr_assert(proto != NULL);
1905
1906 if (!proto->decode) goto raw;
1907
1908 vp = fr_pair_afrom_da(ctx, parent);
1909 if (!vp) return -1;
1911
1912 ret = proto->decode(vp, &vp->vp_group, p, attr_len);
1913 if (ret < 0) goto raw;
1914
1915 vp->vp_tainted = true;
1916
1918 return attr_len;
1919 }
1920
1921 default:
1922 raw:
1923 /*
1924 * A Tag attribute has already been placed into the parent list, so remove it from the list before freeing it.
1925 */
1926 if (vp) {
1927 fr_pair_list_t *list;
1928
1929 list = fr_pair_parent_list(vp);
1930 if (list) {
1931 fr_pair_delete(list, vp);
1932 } else {
1933 talloc_free(vp);
1934 }
1935 }
1936
1938
1939 return fr_pair_raw_from_network(ctx, out, parent, data, attr_len);
1940 }
1941
1942 /*
1943 * And now that we've verified the basic type
1944 * information, decode the actual p.
1945 */
1946 if (!tag) {
1947 vp = fr_pair_afrom_da(ctx, parent);
1948 } else {
1949 fr_assert(packet_ctx->tags != NULL);
1950 fr_assert(packet_ctx->tags[tag] != NULL);
1951 vp = fr_pair_afrom_da_nested(packet_ctx->tags[tag]->parent, &packet_ctx->tags[tag]->parent->vp_group, parent);
1952 }
1953 if (!vp) {
1955 return -1;
1956 }
1958
1959 switch (parent->type) {
1960 /*
1961 * RFC8044 IPv4 prefix
1962 *
1963 * 0 1 2 3
1964 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1965 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1966 * | Reserved | Prefix-Length | Prefix ...
1967 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1968 * ... Prefix |
1969 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1970 *
1971 * The bits outside of the prefix mask MUST be zero.
1972 */
1974 if (data_len != 6) goto raw;
1975 if (p[0] != 0) goto raw;
1976
1978 p[1], p + 2, 4, true, true) < 0) {
1979 goto raw;
1980 }
1981 break;
1982
1983 /*
1984 * RFC8044 IPv6 prefix
1985 *
1986 * 0 1 2 3
1987 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1988 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1989 * | Type | Length | Reserved | Prefix-Length |
1990 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1991 * Prefix
1992 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1993 * Prefix
1994 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1995 * Prefix
1996 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1997 * Prefix |
1998 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1999 *
2000 * The bits outside of the prefix mask MUST be zero.
2001 */
2003 {
2004 if (data_len > 18) goto raw;
2005 if (data_len < 2) goto raw;
2006 if (p[0] != 0) goto raw; /* First byte is always 0 */
2007
2009 p[1], p + 2, data_len - 2, false, true) < 0) {
2010 goto raw;
2011 }
2012
2013 }
2014 break;
2015
2016 case FR_TYPE_STRING:
2018
2019 if (fr_radius_decode_abinary(vp, p, data_len) < 0) goto raw;
2020 break;
2021
2022 case FR_TYPE_OCTETS:
2023 /*
2024 * This attribute SHOULD have fixed size, but it
2025 * doesn't. Therefore it's malformed.
2026 */
2027 if (parent->flags.length && (data_len != parent->flags.length)) goto raw;
2029
2030 default:
2031 decode:
2032 ret = fr_value_box_from_network(vp, &vp->data, vp->vp_type, vp->da,
2033 &FR_DBUFF_TMP(p, data_len), data_len, true);
2034 if (ret < 0) {
2035 /*
2036 * Paranoid loop prevention
2037 */
2038 if (vp->da->flags.is_unknown) {
2039 talloc_free(vp);
2040 return -1;
2041 }
2042 goto raw;
2043 }
2044 break;
2045 }
2046
2047 vp->vp_tainted = true;
2048
2049 /*
2050 * Append the attribute to the output buffer.
2051 *
2052 * If it's the first tagged attribute which was decoded, then append the Tag group to the parent.
2053 * The attribute has already been added to the tag group.
2054 */
2055 if (!tag) {
2057
2058 } else {
2059 fr_pair_t *group = packet_ctx->tags[tag]->parent;
2060
2061 fr_assert(group != NULL);
2062 if (!fr_pair_parent_list(group)) fr_pair_append(packet_ctx->tag_root, group);
2063 }
2064
2065 return attr_len;
2066}
2067
2068/*
2069 * Let's try to help the CPU as much as possible. If we have a
2070 * check on a buffer, that's less work than a series of if / then
2071 * / else conditions.
2072 */
2073static const bool special[UINT8_MAX + 1] = {
2074 [FR_NAS_FILTER_RULE] = true, /* magic rules */
2075 [FR_DIGEST_ATTRIBUTES] = true, /* magic rules */
2076
2077 [FR_EAP_MESSAGE] = true, /* concat */
2078 [FR_PKM_SS_CERT] = true, /* concat */
2079 [FR_PKM_CA_CERT] = true, /* concat */
2080 [FR_EAPOL_ANNOUNCEMENT] = true, /* concat */
2081
2082 [FR_EXTENDED_ATTRIBUTE_1] = true,
2083 [FR_EXTENDED_ATTRIBUTE_2] = true,
2084 [FR_EXTENDED_ATTRIBUTE_3] = true,
2085 [FR_EXTENDED_ATTRIBUTE_4] = true,
2086 [FR_EXTENDED_ATTRIBUTE_5] = true,
2087 [FR_EXTENDED_ATTRIBUTE_6] = true,
2088};
2089
2090/** Create a "normal" fr_pair_t from the given data
2091 *
2092 */
2094 uint8_t const *data, size_t data_len, fr_radius_decode_ctx_t *packet_ctx)
2095{
2096 ssize_t ret;
2097 fr_dict_attr_t const *da;
2098
2099 if ((data_len < 2) || (data[1] < 2) || (data[1] > data_len)) {
2100 fr_strerror_printf("%s: Insufficient data", __FUNCTION__);
2101 return -1;
2102 }
2103
2104 /*
2105 * If we don't have a tag root already, then record where
2106 * we're putting the top level attributes and add the tags
2107 * there.
2108 */
2109 if (!packet_ctx->tag_root) {
2110 packet_ctx->tag_root = out;
2111 packet_ctx->tag_root_ctx = ctx;
2112 }
2113
2114 if (data[0] == 0) {
2115 fr_strerror_const("Attribute 0 is invalid");
2116 return -1;
2117 }
2118
2120 if (!da) {
2121 FR_PROTO_TRACE("Unknown attribute %u", data[0]);
2123 }
2124 if (!da) return -1;
2125 FR_PROTO_TRACE("decode context changed %s -> %s",da->parent->name, da->name);
2126
2127 /*
2128 * Empty attributes are silently ignored, except for CUI.
2129 */
2130 if (data[1] == 2) {
2131 fr_pair_t *vp;
2132
2133 if (data[0] != FR_CHARGEABLE_USER_IDENTITY) {
2134 return 2;
2135 }
2136
2137 /*
2138 * Hacks for CUI. The WiMAX spec says that it can be
2139 * zero length, even though this is forbidden by the
2140 * RADIUS specs. So... we make a special case for it.
2141 *
2142 * We can't create a zero length attribute,
2143 * because the talloc API won't let us. So, we
2144 * just create a fake attribute.
2145 */
2146 vp = fr_pair_afrom_da(ctx, da);
2147 if (!vp) return -1;
2149
2150 /*
2151 * Ensure that it has a value.
2152 */
2153 if (fr_pair_value_memdup(vp, (uint8_t const *) "", 0, false) < 0) {
2154 talloc_free(vp);
2155 return -1;
2156 }
2157
2159
2160 return 2;
2161 }
2162
2163 /*
2164 * A few attributes are special, but they're rare.
2165 */
2166 if (unlikely(special[data[0]])) {
2167 if (data[0] == FR_NAS_FILTER_RULE) {
2168 return decode_nas_filter_rule(ctx, out, da, data, data_len, packet_ctx);
2169 }
2170
2171 if (data[0] == FR_DIGEST_ATTRIBUTES) {
2172 return decode_digest_attributes(ctx, out, da, data, data_len, packet_ctx);
2173 }
2174
2175 /*
2176 * Concatenate consecutive top-level attributes together.
2177 */
2178 if (fr_radius_flag_concat(da)) {
2179 FR_PROTO_TRACE("Concat attribute");
2180 return decode_concat(ctx, out, da, data, packet_ctx->end);
2181 }
2182
2183 /*
2184 * Extended attributes have a horrible format.
2185 * Try to deal with that here, so that the rest
2186 * of the code doesn't have to.
2187 */
2188 if (fr_radius_flag_extended(da)) {
2189 return decode_extended(ctx, out, da, data, data_len, packet_ctx);
2190 }
2191
2192 /*
2193 * @todo - pre-concatenate WiMAX, if 26, and dv->continuation, too.
2194 */
2195 }
2196
2197 /*
2198 * Note that we pass the entire length, not just the
2199 * length of this attribute. The Extended or WiMAX
2200 * attributes may have the "continuation" bit set, and
2201 * will thus be more than one attribute in length.
2202 */
2204 da, data + 2, data[1] - 2,
2205 packet_ctx);
2206 if (ret < 0) return ret;
2207
2208 fr_assert(ret < (1 << 16));
2209
2210 return 2 + ret;
2211}
2212
2214 uint8_t const *data, size_t data_len)
2215{
2216 ssize_t slen;
2217 uint8_t const *attr, *end;
2218
2219 fr_radius_ctx_t common_ctx = {};
2220 fr_radius_decode_ctx_t decode_ctx = {
2221 .common = &common_ctx,
2222 .tmp_ctx = talloc(ctx, uint8_t),
2223 .end = data + data_len,
2224 };
2225
2226 fr_assert(dict_radius != NULL);
2227
2228 attr = data;
2229 end = decode_ctx.end;
2230
2231 while (attr < end) {
2232 slen = fr_radius_decode_pair(ctx, out, attr, (end - attr), &decode_ctx);
2233 if (slen < 0) {
2234 talloc_free(decode_ctx.tmp_ctx);
2235 talloc_free(decode_ctx.tags);
2236 return slen;
2237 }
2238
2239 attr += slen;
2240 talloc_free_children(decode_ctx.tmp_ctx);
2241 }
2242
2243 talloc_free(decode_ctx.tmp_ctx);
2244 talloc_free(decode_ctx.tags);
2245 return data_len;
2246}
2247
2249{
2250 TALLOC_FREE(ctx->tags);
2251
2252 return 0;
2253}
2254
2255static int decode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict,
2257{
2258 static uint8_t vector[RADIUS_AUTH_VECTOR_LENGTH] = {
2259 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2260 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
2261
2263 fr_radius_ctx_t *common;
2264
2265 test_ctx = talloc_zero(ctx, fr_radius_decode_ctx_t);
2266 test_ctx->common = common = talloc_zero(test_ctx, fr_radius_ctx_t);
2267
2268 common->secret = talloc_strdup(common, "testing123");
2269 common->secret_length = talloc_strlen(common->secret);
2270
2271 test_ctx->request_authenticator = vector;
2272 test_ctx->tmp_ctx = talloc_zero(test_ctx, uint8_t);
2273 talloc_set_destructor(test_ctx, _test_ctx_free);
2274
2275 *out = test_ctx;
2276
2277 return 0;
2278}
2279
2281 uint8_t const *data, size_t data_len, void *proto_ctx)
2282{
2283 fr_radius_decode_ctx_t *test_ctx = talloc_get_type_abort(proto_ctx, fr_radius_decode_ctx_t);
2285 fr_pair_t *vp;
2286 size_t packet_len = data_len;
2287
2288 static const uint8_t zeros[12] = {};
2289
2290 /*
2291 * Hack: if the Reserved field is all zeros, then we just mangle the ctx to RADIUS/1.1.
2292 */
2293 test_ctx->radius_1_1 = ((data_len >= RADIUS_HEADER_LENGTH) && (memcmp(data + 4 + 4, zeros, sizeof(zeros)) == 0));
2294
2295 if (!fr_radius_ok(data, &packet_len, 200, false, test_ctx->radius_1_1, &reason)) {
2296 fr_strerror_printf("Packet failed verification - %s", fr_radius_decode_fail_reason[reason]);
2297 return -1;
2298 }
2299
2300 /*
2301 * Decode the header
2302 */
2304 if (!vp) {
2305 fr_strerror_const("Failed creating Packet-Type");
2306 return -1;
2307 }
2309
2310 vp->vp_uint32 = data[0];
2312
2313 if (!test_ctx->radius_1_1) {
2315 if (!vp) {
2316 fr_strerror_const("Failed creating Packet-Authentication-Vector");
2317 return -1;
2318 }
2320
2321 (void) fr_pair_value_memdup(vp, data + 4, 16, true);
2323 } else {
2325 if (!vp) {
2326 fr_strerror_const("Failed creating Packet-Token");
2327 return -1;
2328 }
2330
2331 vp->vp_uint32 = fr_nbo_to_uint32(data + 4);
2333
2335 if (!vp) {
2336 fr_strerror_const("Failed creating Packet-Reserved");
2337 return -1;
2338 }
2340
2341 (void) fr_pair_value_memdup(vp, data + 8, 12, true);
2343 }
2344
2345 test_ctx->end = data + packet_len;
2346
2347 return fr_radius_decode(ctx, out, UNCONST(uint8_t *, data), packet_len, test_ctx);
2348}
2349
2351 uint8_t const *data, size_t data_len, void *decode_ctx)
2352{
2353 fr_radius_decode_ctx_t *packet_ctx = decode_ctx;
2354
2356
2357 packet_ctx->end = data + data_len;
2358 return fr_radius_decode_pair(ctx, out, data, data_len, decode_ctx);
2359}
2360
2361
2362/*
2363 * Test points
2364 */
2370
ssize_t fr_radius_decode_abinary(fr_pair_t *vp, uint8_t const *data, size_t data_len)
Print an Ascend binary filter attribute to a string,.
Definition abinary.c:1330
static int const char char buffer[256]
Definition acutest.h:641
int n
Definition acutest.h:642
static int packet_ctx
the tests pass only the address of packet_ctx
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define RCSID(id)
Definition build.h:609
#define NDEBUG_UNUSED
Definition build.h:426
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define unlikely(_x)
Definition build.h:492
#define UNUSED
Definition build.h:415
fr_dict_attr_t const * root_da
Definition common.c:32
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
#define FR_DIGEST_ATTRIBUTES
Definition defs.h:110
static fr_dict_attr_t const * attr_packet_type
Definition dhcpclient.c:88
bool continuation
we only have one flag for now, for WiMAX
Definition dict.h:295
size_t type
Length of type data.
Definition dict.h:296
fr_dict_attr_decode_func_t decode
for decoding attributes.
Definition dict.h:511
static fr_dict_attr_t * fr_dict_attr_unknown_vendor_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int vendor)
Definition dict.h:628
fr_dict_protocol_t const * fr_dict_protocol(fr_dict_t const *dict)
Return the protocol descriptor for the dictionary.
Definition dict_util.c:5404
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:635
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2725
uint32_t pen
Private enterprise number.
Definition dict.h:294
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
size_t length
Length of length data.
Definition dict.h:297
static fr_dict_attr_t * fr_dict_attr_unknown_typed_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int num, fr_type_t type)
Definition dict.h:620
fr_dict_vendor_t const * fr_dict_vendor_by_num(fr_dict_t const *dict, uint32_t vendor_pen)
Look up a vendor by its PEN.
Definition dict_util.c:3020
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3675
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:479
Private enterprise.
Definition dict.h:293
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:149
talloc_free(hp)
#define PAIR_DECODE_OOM
Fatal error - Out of memory.
Definition pair.h:45
#define PAIR_DECODE_FATAL_ERROR
Fatal error - Failed decoding the packet.
Definition pair.h:49
static int _test_ctx_free(UNUSED fr_aka_sim_ctx_t *ctx)
Definition decode.c:1005
ssize_t fr_pair_tlvs_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx, fr_pair_decode_value_t decode_tlv, fr_pair_tlvs_verify_t verify_tlvs, bool nested)
Decode a list of pairs from the network.
Definition decode.c:156
ssize_t fr_pair_raw_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *da, uint8_t const *data, size_t data_len)
Create a "raw" pair from malformed network data.
Definition decode.c:86
ssize_t fr_pair_array_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value)
Decode an array of values from the network.
Definition decode.c:41
static const uint8_t * zero
Definition md4.c:359
void fr_md5_ctx_free_from_list(fr_md5_ctx_t **ctx)
Free function for MD5 digest ctx.
Definition md5.c:515
fr_md5_ctx_t * fr_md5_ctx_alloc_from_list(void)
Allocation function for MD5 digest context.
Definition md5.c:470
#define fr_md5_final(_out, _ctx)
Finalise the ctx, producing the digest.
Definition md5.h:90
#define fr_md5_ctx_copy(_dst, _src)
Copy the contents of a ctx.
Definition md5.h:60
void fr_md5_ctx_t
Definition md5.h:25
#define fr_md5_update(_ctx, _in, _inlen)
Ingest plaintext into the digest.
Definition md5.h:83
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p, uint32_t max_attributes, bool require_message_authenticator, bool radius_1_1, decode_fail_t *reason)
#define UINT8_MAX
static fr_radius_decode_fail_t decode(TALLOC_CTX *ctx, fr_pair_list_t *reply, uint8_t *response_code, bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t const request_authenticator[static RADIUS_AUTH_VECTOR_LENGTH], uint8_t *data, size_t data_len)
Decode response packet data, extracting relevant information and validating the packet.
Definition bio.c:1848
static uint32_t fr_nbo_to_uint32(uint8_t const data[static sizeof(uint32_t)])
Read an unsigned 32bit integer from wire format (big endian)
Definition nbo.h:167
#define RADIUS_HEADER_LENGTH
Definition net.h:80
#define RADIUS_AUTH_VECTOR_LENGTH
Definition net.h:89
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2906
fr_pair_list_t * fr_pair_parent_list(fr_pair_t const *vp)
Return a pointer to the parent pair list.
Definition pair.c:915
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1306
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:284
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:47
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2756
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1787
int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "octets" type value pair.
Definition pair.c:2855
fr_pair_t * fr_pair_afrom_da_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:486
void fr_pair_list_steal(TALLOC_CTX *ctx, fr_pair_list_t *list)
Steal a list of pairs to a new context.
Definition pair.c:2289
static int decode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict, UNUSED fr_dict_attr_t const *root_da)
Definition decode.c:102
static ssize_t decode_value_trampoline(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
Handle arrays of DNS labels for fr_struct_from_network()
Definition decode.c:70
static ssize_t decode_vsa(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
RFC 4243 Vendor Specific Suboptions.
Definition decode.c:369
static ssize_t decode_tlv_trampoline(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
Definition decode.c:57
static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, NDEBUG_UNUSED fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx)
Definition decode.c:562
HIDDEN fr_dict_attr_t const * attr_packet_authentication_vector
Definition base.c:55
HIDDEN fr_dict_attr_t const * attr_packet_reserved
Definition base.c:57
HIDDEN fr_dict_attr_t const * attr_packet_token
Definition base.c:56
ssize_t fr_radius_ascend_secret(fr_dbuff_t *dbuff, uint8_t const *in, size_t inlen, char const *secret, size_t secret_len, uint8_t const *vector)
Do Ascend-Send / Recv-Secret calculation.
Definition base.c:254
ssize_t fr_radius_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t *packet, size_t packet_len, fr_radius_decode_ctx_t *decode_ctx)
Definition base.c:1229
char const * fr_radius_decode_fail_reason[FR_RADIUS_FAIL_MAX+1]
Definition base.c:511
fr_test_point_proto_decode_t radius_tp_decode_proto
Definition decode.c:2372
ssize_t fr_radius_decode_foreign(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len)
Definition decode.c:2213
static ssize_t decode_concat(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *parent, uint8_t const *data, uint8_t const *end)
Convert a "concatenated" attribute to one long VP.
Definition decode.c:344
static ssize_t decode_extended(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *da, uint8_t const *data, UNUSED size_t data_len, fr_radius_decode_ctx_t *packet_ctx)
Fast path for most extended attributes.
Definition decode.c:1014
static ssize_t fr_radius_decode_password(uint8_t passwd[static 256], size_t pwlen, fr_radius_decode_ctx_t *packet_ctx)
Decode password.
Definition decode.c:194
static ssize_t decode_digest_attributes(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, fr_radius_decode_ctx_t *packet_ctx)
Decode Digest-Attributes.
Definition decode.c:628
static ssize_t decode_wimax(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t attr_len, fr_radius_decode_ctx_t *packet_ctx)
Convert a Vendor-Specific WIMAX to vps.
Definition decode.c:1098
ssize_t fr_radius_decode_tlv(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, fr_radius_decode_ctx_t *packet_ctx)
Convert TLVs to one or more VPs.
Definition decode.c:694
fr_test_point_pair_decode_t radius_tp_decode_pair
Definition decode.c:2366
#define decode_value
Definition decode.c:419
ssize_t fr_radius_decode_pair_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const attr_len, void *decode_ctx)
Create any kind of VP from the attribute contents.
Definition decode.c:1563
static ssize_t decode_extended_fragments(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t attr_len, fr_radius_decode_ctx_t *packet_ctx)
Convert a fragmented extended attr to a VP.
Definition decode.c:906
static ssize_t fr_radius_decode_tunnel_password(uint8_t passwd[static 256], size_t *pwlen, fr_radius_decode_ctx_t *packet_ctx)
Decode Tunnel-Password encrypted attributes.
Definition decode.c:70
static const bool special[UINT8_MAX+1]
Definition decode.c:2073
int fr_radius_decode_tlv_ok(uint8_t const *data, size_t length, size_t dv_type, size_t dv_length)
Check if a set of RADIUS formatted TLVs are OK.
Definition decode.c:252
ssize_t fr_radius_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, fr_radius_decode_ctx_t *packet_ctx)
Create a "normal" fr_pair_t from the given data.
Definition decode.c:2093
static void tag_group_cleanup(fr_radius_decode_ctx_t *packet_ctx, int8_t tag)
Free an empty Tag-N group.
Definition decode.c:1535
static ssize_t decode_vsa_internal(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, fr_radius_decode_ctx_t *packet_ctx, fr_dict_vendor_t const *dv)
Convert a top-level VSA to a VP.
Definition decode.c:786
static ssize_t decode_rfc(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
decode an RFC-format TLV
Definition decode.c:424
static bool memcpy_bounded(void *restrict dst, const void *restrict src, size_t n, const void *restrict end)
Definition decode.c:42
static ssize_t decode_nas_filter_rule(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, fr_radius_decode_ctx_t *packet_ctx)
Decode NAS-Filter-Rule.
Definition decode.c:499
static ssize_t fr_radius_decode_proto(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, void *proto_ctx)
Definition decode.c:2280
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
static bool done
Definition radclient.c:80
fr_radius_tag_ctx_t ** tags
for decoding tagged attributes
Definition radius.h:181
#define AUTH_PASS_LEN
Definition radius.h:53
fr_radius_attr_flags_encrypt_t
Definition radius.h:186
@ RADIUS_FLAG_ENCRYPT_NONE
No encryption.
Definition radius.h:188
@ RADIUS_FLAG_ENCRYPT_USER_PASSWORD
Encrypt attribute RFC 2865 style.
Definition radius.h:189
@ RADIUS_FLAG_ENCRYPT_ASCEND_SECRET
Encrypt attribute ascend style.
Definition radius.h:191
@ RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD
Encrypt attribute RFC 2868 style.
Definition radius.h:190
char const * secret
Definition radius.h:127
static bool fr_radius_flag_long_extended(fr_dict_attr_t const *da)
Definition radius.h:257
uint8_t const * end
end of the packet
Definition radius.h:167
fr_radius_decode_fail_t
Failure reasons.
Definition radius.h:89
#define RADIUS_MAX_STRING_LENGTH
Definition radius.h:35
static bool fr_radius_flag_extended(fr_dict_attr_t const *da)
Definition radius.h:250
size_t secret_length
Definition radius.h:128
static fr_radius_attr_flags_encrypt_t fr_radius_flag_encrypted(fr_dict_attr_t const *da)
Definition radius.h:245
static bool fr_radius_flag_has_tag(fr_dict_attr_t const *da)
Definition radius.h:230
fr_radius_ctx_t const * common
Definition radius.h:162
static bool fr_radius_flag_abinary(fr_dict_attr_t const *da)
Definition radius.h:240
static bool fr_radius_flag_concat(fr_dict_attr_t const *da)
Definition radius.h:235
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition radius.h:166
static fr_radius_bio_verify_t test_ctx(void)
Make a verify context which allows Access-Request and Access-Accept.
static const uint8_t zeros[RADIUS_AUTH_VECTOR_LENGTH]
Definition radsniff.c:1257
static fr_dict_t const * dict_radius
Definition radsniff.c:93
static fr_dict_attr_t const * attr_vendor_specific
Definition radsnmp.c:115
fr_pair_t * vp
ssize_t fr_struct_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value, fr_pair_decode_value_t decode_tlv)
Convert a STRUCT to one or more VPs.
Definition struct.c:32
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:86
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:68
Entry point for pair decoders.
Definition test_point.h:85
Entry point for protocol decoders.
Definition test_point.h:67
static fr_slen_t head
Definition xlat.h:410
#define PAIR_ALLOCED(_x)
Definition pair.h:217
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
static int fr_pair_find_or_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Definition pair.h:541
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
static fr_slen_t parent
Definition pair.h:864
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:41
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const(_msg)
Definition strerror.h:223
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define FR_TYPE_LEAF
Definition types.h:317
ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t type, fr_dict_attr_t const *enumv, fr_dbuff_t *dbuff, size_t len, bool tainted)
Decode a fr_value_box_t from serialized binary data.
Definition value.c:1942
ssize_t fr_value_box_ipaddr_from_network(fr_value_box_t *dst, fr_type_t type, fr_dict_attr_t const *enumv, int prefix_len, uint8_t const *data, size_t data_len, bool fixed, bool tainted)
Decode a fr_value_box_t of type IP address / prefix.
Definition value.c:2346
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
int nonnull(2, 5))