/
decode.c
1692 lines (1433 loc) · 41.9 KB
/
decode.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
*
* @file protocols/radius/decode.c
* @brief Functions to decode RADIUS attributes
*
* @copyright 2000-2003,2006-2015 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/util/base.h>
#include <freeradius-devel/util/md5.h>
#include <freeradius-devel/util/struct.h>
#include <freeradius-devel/io/test_point.h>
#include "attrs.h"
static void memcpy_bounded(void * restrict dst, const void * restrict src, size_t n, const void * restrict end)
{
size_t len = n;
if (!fr_cond_assert(n <= 65535)) {
return;
}
if (!fr_cond_assert(src <= end)) {
return;
}
if (len == 0) return;
if (!fr_cond_assert(((uint8_t const * restrict) src + len) <= (uint8_t const * restrict) end)) {
len = (uint8_t const * restrict) end - (uint8_t const * restrict) src;
}
memcpy(dst, src, len);
}
/** Decode Tunnel-Password encrypted attributes
*
* Defined in RFC-2868, this uses a two char SALT along with the
* initial intermediate value, to differentiate it from the
* above.
*/
ssize_t fr_radius_decode_tunnel_password(uint8_t *passwd, size_t *pwlen,
char const *secret, uint8_t const *vector, bool tunnel_password_zeros)
{
fr_md5_ctx_t *md5_ctx, *md5_ctx_old;
uint8_t digest[RADIUS_AUTH_VECTOR_LENGTH];
int secretlen;
size_t i, n, encrypted_len, embedded_len;
encrypted_len = *pwlen;
/*
* We need at least a salt.
*/
if (encrypted_len < 2) {
fr_strerror_printf("Tunnel password is too short");
return -1;
}
/*
* There's a salt, but no password. Or, there's a salt
* and a 'data_len' octet. It's wrong, but at least we
* can figure out what it means: the password is empty.
*
* Note that this means we ignore the 'data_len' field,
* if the attribute length tells us that there's no
* more data. So the 'data_len' field may be wrong,
* but that's ok...
*/
if (encrypted_len <= 3) {
passwd[0] = 0;
*pwlen = 0;
return 0;
}
encrypted_len -= 2; /* discount the salt */
/*
* Use the secret to setup the decryption digest
*/
secretlen = talloc_array_length(secret) - 1;
md5_ctx = fr_md5_ctx_alloc(false);
md5_ctx_old = fr_md5_ctx_alloc(true);
fr_md5_update(md5_ctx, (uint8_t const *) secret, secretlen);
fr_md5_ctx_copy(md5_ctx_old, md5_ctx); /* save intermediate work */
/*
* Set up the initial key:
*
* b(1) = MD5(secret + vector + salt)
*/
fr_md5_update(md5_ctx, vector, RADIUS_AUTH_VECTOR_LENGTH);
fr_md5_update(md5_ctx, passwd, 2);
embedded_len = 0;
for (n = 0; n < encrypted_len; n += AUTH_PASS_LEN) {
size_t base;
size_t block_len = AUTH_PASS_LEN;
/*
* Ensure we don't overflow the input on MD5
*/
if ((n + 2 + AUTH_PASS_LEN) > *pwlen) {
block_len = *pwlen - n - 2;
}
if (n == 0) {
base = 1;
fr_md5_final(digest, md5_ctx);
fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
/*
* A quick check: decrypt the first octet
* of the password, which is the
* 'data_len' field. Ensure it's sane.
*/
embedded_len = passwd[2] ^ digest[0];
if (embedded_len > encrypted_len) {
fr_strerror_printf("Tunnel Password is too long for the attribute "
"(shared secret is probably incorrect!)");
fr_md5_ctx_free(&md5_ctx);
fr_md5_ctx_free(&md5_ctx_old);
return -1;
}
fr_md5_update(md5_ctx, passwd + 2, block_len);
} else {
base = 0;
fr_md5_final(digest, md5_ctx);
fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
fr_md5_update(md5_ctx, passwd + n + 2, block_len);
}
for (i = base; i < block_len; i++) {
passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
}
}
fr_md5_ctx_free(&md5_ctx);
fr_md5_ctx_free(&md5_ctx_old);
/*
* Check trailing bytes
*/
if (tunnel_password_zeros) for (i = embedded_len; i < (encrypted_len - 1); i++) { /* -1 for length field */
if (passwd[i] != 0) {
fr_strerror_printf("Trailing garbage in Tunnel Password "
"(shared secret is probably incorrect!)");
return -1;
}
}
*pwlen = embedded_len;
passwd[embedded_len] = '\0';
return embedded_len;
}
/** Decode password
*
*/
ssize_t fr_radius_decode_password(char *passwd, size_t pwlen, char const *secret, uint8_t const *vector)
{
fr_md5_ctx_t *md5_ctx, *md5_ctx_old;
uint8_t digest[RADIUS_AUTH_VECTOR_LENGTH];
int i;
size_t n, secretlen;
/*
* The RFC's say that the maximum is 128.
* The buffer we're putting it into above is 254, so
* we don't need to do any length checking.
*/
if (pwlen > 128) pwlen = 128;
/*
* Catch idiots.
*/
if (pwlen == 0) goto done;
/*
* Use the secret to setup the decryption digest
*/
secretlen = talloc_array_length(secret) - 1;
md5_ctx = fr_md5_ctx_alloc(false);
md5_ctx_old = fr_md5_ctx_alloc(true);
fr_md5_update(md5_ctx, (uint8_t const *) secret, secretlen);
fr_md5_ctx_copy(md5_ctx_old, md5_ctx); /* save intermediate work */
/*
* The inverse of the code above.
*/
for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
if (n == 0) {
fr_md5_update(md5_ctx, vector, RADIUS_AUTH_VECTOR_LENGTH);
fr_md5_final(digest, md5_ctx);
fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
if (pwlen > AUTH_PASS_LEN) {
fr_md5_update(md5_ctx, (uint8_t *) passwd, AUTH_PASS_LEN);
}
} else {
fr_md5_final(digest, md5_ctx);
fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
if (pwlen > (n + AUTH_PASS_LEN)) {
fr_md5_update(md5_ctx, (uint8_t *) passwd + n, AUTH_PASS_LEN);
}
}
for (i = 0; i < AUTH_PASS_LEN; i++) passwd[i + n] ^= digest[i];
}
fr_md5_ctx_free(&md5_ctx);
fr_md5_ctx_free(&md5_ctx_old);
done:
passwd[pwlen] = '\0';
return strlen(passwd);
}
/** Check if a set of RADIUS formatted TLVs are OK
*
*/
int fr_radius_decode_tlv_ok(uint8_t const *data, size_t length, size_t dv_type, size_t dv_length)
{
uint8_t const *end = data + length;
FR_PROTO_TRACE("Checking TLV %u/%u", (unsigned int) dv_type, (unsigned int) dv_length);
FR_PROTO_HEX_DUMP(data, length, "tlv_ok");
if ((dv_length > 2) || (dv_type == 0) || (dv_type > 4)) {
fr_strerror_printf("%s: Invalid arguments", __FUNCTION__);
return -1;
}
while (data < end) {
size_t attrlen;
if ((data + dv_type + dv_length) > end) {
fr_strerror_printf("Attribute header overflow");
return -1;
}
switch (dv_type) {
case 4:
if ((data[0] == 0) && (data[1] == 0) &&
(data[2] == 0) && (data[3] == 0)) {
zero:
fr_strerror_printf("Invalid attribute 0");
return -1;
}
if (data[0] != 0) {
fr_strerror_printf("Invalid attribute > 2^24");
return -1;
}
break;
case 2:
if ((data[0] == 0) && (data[1] == 0)) goto zero;
break;
case 1:
/*
* Zero is allowed, because the Colubris
* people are dumb and use it.
*/
break;
default:
fr_strerror_printf("Internal sanity check failed");
return -1;
}
switch (dv_length) {
case 0:
return 0;
case 2:
if (data[dv_type] != 0) {
fr_strerror_printf("Attribute is longer than 256 octets");
return -1;
}
/* FALL-THROUGH */
case 1:
attrlen = data[dv_type + dv_length - 1];
break;
default:
fr_strerror_printf("Internal sanity check failed");
return -1;
}
if (attrlen < (dv_type + dv_length)) {
fr_strerror_printf("Attribute header has invalid length");
return -1;
}
if (attrlen > length) {
fr_strerror_printf("Attribute overflows container");
return -1;
}
data += attrlen;
length -= attrlen;
}
return 0;
}
/** Convert a "concatenated" attribute to one long VP
*
*/
static ssize_t decode_concat(TALLOC_CTX *ctx, fr_cursor_t *cursor,
fr_dict_attr_t const *parent, uint8_t const *data,
size_t const packet_len)
{
size_t total;
uint8_t attr;
uint8_t const *ptr = data;
uint8_t const *end = data + packet_len;
uint8_t *p;
VALUE_PAIR *vp;
total = 0;
attr = ptr[0];
/*
* See how many consecutive attributes there are.
*/
while (ptr < end) {
if (ptr[1] <= 2) return -1;
if ((ptr + ptr[1]) > end) return -1;
total += ptr[1] - 2;
ptr += ptr[1];
if (ptr == end) break;
/*
* Attributes MUST be consecutive.
*/
if (ptr[0] != attr) break;
}
/*
* Reset the end of the data we're trying to parse
*/
end = ptr;
/*
* If there's no data, just return that we skipped the
* attribute header.
*/
if (!total) return 2;
vp = fr_pair_afrom_da(ctx, parent);
if (!vp) return -1;
p = talloc_array(vp, uint8_t, total);
if (!p) {
fr_pair_list_free(&vp);
return -1;
}
fr_pair_value_memsteal(vp, p, true);
ptr = data;
while (ptr < end) {
memcpy_bounded(p, ptr + 2, ptr[1] - 2, end);
p += ptr[1] - 2;
ptr += ptr[1];
}
fr_cursor_append(cursor, vp);
return ptr - data;
}
/** Convert TLVs to one or more VPs
*
*/
ssize_t fr_radius_decode_tlv(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t const *dict,
fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len,
void *decoder_ctx)
{
uint8_t const *p = data, *end = data + data_len;
fr_dict_attr_t const *child;
VALUE_PAIR *head = NULL;
fr_cursor_t tlv_cursor;
fr_radius_ctx_t *packet_ctx = decoder_ctx;
if (data_len < 3) return -1; /* type, length, value */
#ifdef __clang_analyzer__
if (!packet_ctx || !packet_ctx->tmp_ctx) return -1;
#endif
FR_PROTO_HEX_DUMP(p, data_len, "tlvs");
if (fr_radius_decode_tlv_ok(p, data_len, 1, 1) < 0) return -1;
/*
* Record where we were in the list when this function was called
*/
fr_cursor_init(&tlv_cursor, &head);
while (p < end) {
ssize_t tlv_len;
child = fr_dict_attr_child_by_num(parent, p[0]);
if (!child) {
FR_PROTO_TRACE("Failed to find child %u of TLV %s", p[0], parent->name);
/*
* Build an unknown attr
*/
child = fr_dict_unknown_afrom_fields(packet_ctx->tmp_ctx, parent,
fr_dict_vendor_num_by_da(parent), p[0]);
if (!child) {
error:
fr_pair_list_free(&head);
return -1;
}
}
FR_PROTO_TRACE("decode context changed %s -> %s", parent->name, child->name);
tlv_len = fr_radius_decode_pair_value(ctx, &tlv_cursor, dict,
child, p + 2, p[1] - 2, p[1] - 2,
decoder_ctx);
if (tlv_len < 0) goto error;
p += p[1];
}
fr_cursor_head(&tlv_cursor);
fr_cursor_tail(cursor);
fr_cursor_merge(cursor, &tlv_cursor); /* Wind to the end of the new pairs */
return data_len;
}
/** Convert a top-level VSA to a VP.
*
* "length" can be LONGER than just this sub-vsa.
*/
static ssize_t decode_vsa_internal(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t const *dict,
fr_dict_attr_t const *parent,
uint8_t const *data, size_t data_len,
void *decoder_ctx, fr_dict_vendor_t const *dv)
{
unsigned int attribute;
ssize_t attrlen, my_len;
fr_dict_attr_t const *da;
fr_radius_ctx_t *packet_ctx = decoder_ctx;
#ifdef __clang_analyzer__
if (!packet_ctx || !packet_ctx->tmp_ctx) return -1;
#endif
/*
* Parent must be a vendor
*/
if (!fr_cond_assert(parent->type == FR_TYPE_VENDOR)) {
fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
return -1;
}
FR_PROTO_TRACE("Length %u", (unsigned int)data_len);
#ifndef NDEBUG
if (data_len <= (dv->type + dv->length)) {
fr_strerror_printf("%s: Failure to call fr_radius_decode_tlv_ok", __FUNCTION__);
return -1;
}
#endif
switch (dv->type) {
case 4:
/* data[0] must be zero */
attribute = data[1] << 16;
attribute |= data[2] << 8;
attribute |= data[3];
break;
case 2:
attribute = data[0] << 8;
attribute |= data[1];
break;
case 1:
attribute = data[0];
break;
default:
fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
return -1;
}
switch (dv->length) {
case 2:
/* data[dv->type] must be zero, from fr_radius_decode_tlv_ok() */
attrlen = data[dv->type + 1];
break;
case 1:
attrlen = data[dv->type];
break;
case 0:
attrlen = data_len;
break;
default:
fr_strerror_printf("%s: Internal sanity check failed", __FUNCTION__);
return -1;
}
/*
* See if the VSA is known.
*/
da = fr_dict_attr_child_by_num(parent, attribute);
if (!da) da = fr_dict_unknown_afrom_fields(packet_ctx->tmp_ctx, parent, dv->pen, attribute);
if (!da) return -1;
FR_PROTO_TRACE("decode context changed %s -> %s", da->parent->name, da->name);
my_len = fr_radius_decode_pair_value(ctx, cursor, dict,
da, data + dv->type + dv->length,
attrlen - (dv->type + dv->length), attrlen - (dv->type + dv->length),
decoder_ctx);
if (my_len < 0) return my_len;
return attrlen;
}
/** Convert a fragmented extended attr to a VP
*
* Format is:
*
* attr
* length
* extended-attr
* flag
* data...
*
* But for the first fragment, we get passed a pointer to the "extended-attr"
*/
static ssize_t decode_extended(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t const *dict,
fr_dict_attr_t const *parent,
uint8_t const *data, size_t attr_len, size_t packet_len,
void *decoder_ctx)
{
ssize_t rcode;
size_t fraglen;
uint8_t *head, *tail;
uint8_t const *frag, *end;
uint8_t const *attr;
int fragments;
bool last_frag;
/*
* data = Ext-Attr Flag ...
*/
if (attr_len < 3) return -1;
/*
* No continuation, just decode the attributre in place.
*/
if ((data[1] & 0x80) == 0) {
rcode = fr_radius_decode_pair_value(ctx, cursor, dict,
parent, data + 2, attr_len - 2, attr_len - 2, decoder_ctx);
if (rcode < 0) return -1;
return attr_len;
}
/*
* Calculate the length of all of the fragments. For
* now, they MUST be contiguous in the packet, and they
* MUST be all of the same TYPE and EXTENDED-TYPE
*/
attr = data - 2;
fraglen = attr_len - 2;
frag = data + attr_len;
end = data + packet_len;
fragments = 1;
last_frag = false;
while (frag < end) {
if (last_frag ||
(frag[0] != attr[0]) ||
(frag[1] < 4) || /* too short for long-extended */
(frag[2] != attr[2]) ||
((frag + frag[1]) > end)) { /* overflow */
end = frag;
break;
}
last_frag = ((frag[3] & 0x80) == 0);
fraglen += frag[1] - 4;
frag += frag[1];
fragments++;
}
head = tail = talloc_array(ctx, uint8_t, fraglen);
if (!head) return -1;
FR_PROTO_TRACE("Fragments %d, total length %d", fragments, (int) fraglen);
/*
* And again, but faster and looser.
*
* We copy the first fragment, followed by the rest of
* the fragments.
*/
frag = attr;
while (fragments > 0) {
if (frag[1] > 4) memcpy_bounded(tail, frag + 4, frag[1] - 4, end);
tail += frag[1] - 4;
frag += frag[1];
fragments--;
}
FR_PROTO_HEX_DUMP(head, fraglen, "long-extended fragments");
rcode = fr_radius_decode_pair_value(ctx, cursor, dict,
parent, head, fraglen, fraglen, decoder_ctx);
talloc_free(head);
if (rcode < 0) return rcode;
return end - data;
}
/** Convert a Vendor-Specific WIMAX to vps
*
* @note Called ONLY for Vendor-Specific
*/
static ssize_t decode_wimax(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t const *dict,
fr_dict_attr_t const *parent,
uint8_t const *data, size_t attr_len, size_t packet_len, void *decoder_ctx, uint32_t vendor)
{
ssize_t rcode;
size_t wimax_len;
bool more;
uint8_t *head, *tail;
uint8_t const *attr, *end;
fr_dict_attr_t const *da;
fr_radius_ctx_t *packet_ctx = decoder_ctx;
#ifdef __clang_analyzer__
if (!packet_ctx || !packet_ctx->tmp_ctx) return -1;
#endif
/*
* data = VID VID VID VID WiMAX-Attr WiMAX-Len Continuation ...
*/
if (attr_len < 8) return -1;
/*
* WiMAX-Attr WiMAX-Len Continuation
*/
if (data[5] < 3) return -1;
/*
* The WiMAX-Len + 4 VID must exactly fill the attribute.
*/
if (((size_t) (data[5] + 4)) != attr_len) return -1;
da = fr_dict_attr_child_by_num(parent, data[4]);
if (!da) da = fr_dict_unknown_afrom_fields(packet_ctx->tmp_ctx, parent, vendor, data[4]);
if (!da) return -1;
FR_PROTO_TRACE("decode context changed %s -> %s", da->parent->name, da->name);
/*
* No continuation, just decode the attributre in place.
*/
if ((data[6] & 0x80) == 0) {
rcode = fr_radius_decode_pair_value(ctx, cursor, dict,
da, data + 7, data[5] - 3, data[5] - 3, decoder_ctx);
if (rcode < 0) return rcode;
return attr_len;
}
/*
* Calculate the length of all of the fragments. For
* now, they MUST be contiguous in the packet, and they
* MUST be all of the same VSA, WiMAX, and WiMAX-attr.
*
* The first fragment doesn't have a RADIUS attribute
* header.
*/
wimax_len = 0;
attr = data + 4;
end = data + packet_len;
while (attr < end) {
/*
* Not enough room for Attribute + length +
* continuation, it's bad.
*/
if ((end - attr) < 3) return -1;
/*
* Must have non-zero data in the attribute.
*/
if (attr[1] <= 3) return -1;
/*
* If the WiMAX attribute overflows the packet,
* it's bad.
*/
if ((attr + attr[1]) > end) return -1;
/*
* Check the continuation flag.
*/
more = ((attr[2] & 0x80) != 0);
/*
* Or, there's no more data, in which case we
* shorten "end" to finish at this attribute.
*/
if (!more) end = attr + attr[1];
/*
* There's more data, but we're at the end of the
* packet. The attribute is malformed!
*/
if (more && ((attr + attr[1]) == end)) return -1;
/*
* Add in the length of the data we need to
* concatenate together.
*/
wimax_len += attr[1] - 3;
/*
* Go to the next attribute, and stop if there's
* no more.
*/
attr += attr[1];
if (!more) break;
/*
* data = VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
*
* attr = Vendor-Specific VSA-Length VID VID VID VID WiMAX-Attr WimAX-Len Continuation ...
*
*/
/*
* No room for Vendor-Specific + length +
* Vendor(4) + attr + length + continuation + data
*/
if ((end - attr) < 9) return -1;
if (attr[0] != FR_VENDOR_SPECIFIC) return -1;
if (attr[1] < 9) return -1;
if ((attr + attr[1]) > end) return -1;
if (memcmp(data, attr + 2, 4) != 0) return -1; /* not WiMAX Vendor ID */
if (attr[1] != (attr[7] + 6)) return -1; /* WiMAX attr doesn't exactly fill the VSA */
if (data[4] != attr[6]) return -1; /* different WiMAX attribute */
/*
* Skip over the Vendor-Specific header, and
* continue with the WiMAX attributes.
*/
attr += 6;
}
/*
* No data in the WiMAX attribute, make a "raw" one.
*/
if (!wimax_len) return -1;
head = tail = talloc_array(ctx, uint8_t, wimax_len);
if (!head) return -1;
/*
* Copy the data over, this time trusting the attribute
* contents.
*/
attr = data;
while (attr < end) {
memcpy_bounded(tail, attr + 4 + 3, attr[4 + 1] - 3, end);
tail += attr[4 + 1] - 3;
attr += 4 + attr[4 + 1]; /* skip VID+WiMax header */
attr += 2; /* skip Vendor-Specific header */
}
FR_PROTO_HEX_DUMP(head, wimax_len, "Wimax fragments");
rcode = fr_radius_decode_pair_value(ctx, cursor, dict,
da, head, wimax_len, wimax_len, decoder_ctx);
talloc_free(head);
if (rcode < 0) return rcode;
return end - data;
}
/** Convert a top-level VSA to one or more VPs
*
*/
static ssize_t decode_vsa(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t const *dict,
fr_dict_attr_t const *parent,
uint8_t const *data, size_t attr_len, size_t packet_len,
void *decoder_ctx)
{
size_t total;
ssize_t rcode;
uint32_t vendor;
fr_dict_vendor_t const *dv;
VALUE_PAIR *head = NULL;
fr_dict_vendor_t my_dv;
fr_dict_attr_t const *vendor_da;
fr_cursor_t tlv_cursor;
fr_radius_ctx_t *packet_ctx = decoder_ctx;
#ifdef __clang_analyzer__
if (!packet_ctx || !packet_ctx->tmp_ctx) return -1;
#endif
/*
* Container must be a VSA
*/
if (!fr_cond_assert(parent->type == FR_TYPE_VSA)) return -1;
if (attr_len > packet_len) return -1;
if (attr_len < 5) return -1; /* vid, value */
if (data[0] != 0) return -1; /* we require 24-bit VIDs */
FR_PROTO_TRACE("Decoding VSA");
memcpy(&vendor, data, 4);
vendor = ntohl(vendor);
/*
* Verify that the parent (which should be a VSA)
* contains a fake attribute representing the vendor.
*
* If it doesn't then this vendor is unknown, but
* (unlike DHCP) we know vendor attributes have a
* standard format, so we can decode the data anyway.
*/
vendor_da = fr_dict_attr_child_by_num(parent, vendor);
if (!vendor_da) {
fr_dict_attr_t *n;
/*
* RFC format is 1 octet type, 1 octet length
*/
if (fr_radius_decode_tlv_ok(data + 4, attr_len - 4, 1, 1) < 0) {
FR_PROTO_TRACE("Unknown TLVs not OK: %s", fr_strerror());
return -1;
}
if (fr_dict_unknown_vendor_afrom_num(packet_ctx->tmp_ctx, &n, parent, vendor) < 0) return -1;
vendor_da = n;
/*
* Create an unknown DV too...
*/
memset(&my_dv, 0, sizeof(my_dv));
my_dv.pen = vendor;
my_dv.type = 1;
my_dv.length = 1;
dv = &my_dv;
goto create_attrs;
}
/*
* We found an attribute representing the vendor
* so it *MUST* exist in the vendor tree.
*/
dv = fr_dict_vendor_by_num(dict, vendor);
if (!fr_cond_assert(dv)) return -1;
FR_PROTO_TRACE("decode context %s -> %s", parent->name, vendor_da->name);
/*
* WiMAX craziness
*/
if ((vendor == VENDORPEC_WIMAX) && dv->flags) {
rcode = decode_wimax(ctx, cursor, dict,
vendor_da, data, attr_len, packet_len, decoder_ctx, vendor);
return rcode;
}
/*
* VSAs should normally be in TLV format.
*/
if (fr_radius_decode_tlv_ok(data + 4, attr_len - 4, dv->type, dv->length) < 0) {
FR_PROTO_TRACE("TLVs not OK: %s", fr_strerror());
return -1;
}
/*
* There may be more than one VSA in the
* Vendor-Specific. If so, loop over them all.
*/
create_attrs:
data += 4;
attr_len -= 4;
packet_len -= 4;
total = 4;
fr_cursor_init(&tlv_cursor, &head);
while (attr_len > 0) {
ssize_t vsa_len;
/*
* Vendor attributes can have subattributes (if you hadn't guessed)
*/
vsa_len = decode_vsa_internal(ctx, &tlv_cursor, dict,
vendor_da, data, attr_len, decoder_ctx, dv);
if (vsa_len < 0) {
fr_strerror_printf("%s: Internal sanity check %d", __FUNCTION__, __LINE__);
fr_pair_list_free(&head);
return -1;
}
data += vsa_len;
attr_len -= vsa_len;
packet_len -= vsa_len;
total += vsa_len;
}
fr_cursor_head(&tlv_cursor);
fr_cursor_tail(cursor);
fr_cursor_merge(cursor, &tlv_cursor);
/*
* When the unknown attributes were created by
* decode_vsa_internal, the hierachy between that unknown
* attribute and first known attribute was cloned
* meaning we can now free the unknown vendor.
*/
return total;
}
/** Create any kind of VP from the attribute contents
*
* "length" is AT LEAST the length of this attribute, as we
* expect the caller to have verified the data with
* fr_radius_packet_ok(). "length" may be up to the length of the
* packet.
*
* @return
* - Length on success.
* - -1 on failure.
*/
ssize_t fr_radius_decode_pair_value(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_t const *dict,
fr_dict_attr_t const *parent,
uint8_t const *data, size_t const attr_len, size_t const packet_len,
void *decoder_ctx)
{
int8_t tag = TAG_NONE;
size_t data_len;
ssize_t rcode;
uint32_t vendor;
fr_dict_attr_t const *child;
VALUE_PAIR *vp = NULL;
uint8_t const *p = data;
uint8_t buffer[256];