The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
encode.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: aa540cb9de0785f0ad88559f42ca17dd7aba5c55 $
19 *
20 * @file protocols/radius/encode.c
21 * @brief Functions to encode RADIUS attributes
22 *
23 * @copyright 2000-2003,2006-2015 The FreeRADIUS server project
24 */
26RCSID("$Id: aa540cb9de0785f0ad88559f42ca17dd7aba5c55 $")
27
28#include <freeradius-devel/util/dbuff.h>
29#include <freeradius-devel/util/md5.h>
30#include <freeradius-devel/util/struct.h>
31#include <freeradius-devel/io/test_point.h>
32#include <freeradius-devel/protocol/radius/freeradius.internal.h>
33#include "attrs.h"
34
35#define TAG_VALID(x) ((x) > 0 && (x) < 0x20)
36
38 [ 0 ] = true, /* only for testing */
41};
42
43
44static ssize_t encode_value(fr_dbuff_t *dbuff,
45 fr_da_stack_t *da_stack, unsigned int depth,
46 fr_dcursor_t *cursor, void *encode_ctx);
47
48static ssize_t encode_child(fr_dbuff_t *dbuff,
49 fr_da_stack_t *da_stack, unsigned int depth,
50 fr_dcursor_t *cursor, void *encode_ctx);
51
52/** "encrypt" a password RADIUS style
53 *
54 * Input and output buffers can be identical if in-place encryption is needed.
55 */
57{
58 fr_md5_ctx_t *md5_ctx, *md5_ctx_old;
60 uint8_t passwd[256] = {0};
61 size_t i, n;
62 size_t len;
63
64 if (!packet_ctx->request_authenticator) {
65 fr_strerror_const("Request Authenticator is required to encode User-Password attributes");
66 return -1;
67 }
68
69 /*
70 * If the length is zero, round it up.
71 */
72 len = inlen;
73
75 fr_strerror_const("User-Password is too long (253 octets max)");
76 return -1;
77 }
78
79 (void) fr_dbuff_out_memcpy(passwd, input, len);
80 if (len < sizeof(passwd)) memset(passwd + len, 0, sizeof(passwd) - len);
81
82 if (len == 0) len = AUTH_PASS_LEN;
83 else if ((len & 0x0f) != 0) {
84 len += 0x0f;
85 len &= ~0x0f;
86 }
87
89 md5_ctx_old = fr_md5_ctx_alloc_from_list();
90
91 fr_md5_update(md5_ctx, (uint8_t const *) packet_ctx->common->secret, packet_ctx->common->secret_length);
92 fr_md5_ctx_copy(md5_ctx_old, md5_ctx);
93
94 /*
95 * Do first pass.
96 */
97 fr_md5_update(md5_ctx, packet_ctx->request_authenticator, AUTH_PASS_LEN);
98
99 for (n = 0; n < len; n += AUTH_PASS_LEN) {
100 if (n > 0) {
101 fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
102 fr_md5_update(md5_ctx, passwd + n - AUTH_PASS_LEN, AUTH_PASS_LEN);
103 }
104
105 fr_md5_final(digest, md5_ctx);
106
107 /*
108 * We encode up to 253 octets, in 16 byte chunks.
109 */
110 for (i = 0; i < AUTH_PASS_LEN; i++) {
111 passwd[i + n] ^= digest[i];
112
113 if ((i + n) == RADIUS_MAX_STRING_LENGTH) {
115 break;
116 }
117 }
118 }
119
121 fr_md5_ctx_free_from_list(&md5_ctx_old);
122
123 return fr_dbuff_in_memcpy(dbuff, passwd, len);
124}
125
126
128{
129 fr_md5_ctx_t *md5_ctx, *md5_ctx_old;
132 size_t i, n;
133 uint32_t r;
134 size_t output_len, encrypted_len, padding;
135 ssize_t slen;
137
138 if (!packet_ctx->request_authenticator) {
139 fr_strerror_const("Request Authenticator is required to encode Tunnel-Password attributes");
140 return -1;
141 }
142
143 /*
144 * Limit the maximum size of the input password. 2 bytes
145 * are taken up by the salt, and one by the encoded
146 * "length" field.
147 */
148 if (inlen > (RADIUS_MAX_STRING_LENGTH - 3)) {
149 fail:
150 fr_strerror_const("Input password is too large for tunnel password encoding");
151 return -1;
152 }
153
154 /*
155 * Length of the encrypted data is the clear-text
156 * password length plus one byte which encodes the length
157 * of the password. We round up to the nearest encoding
158 * block, and bound it by the size of the output buffer,
159 * while accounting for 2 bytes of salt.
160 *
161 * And also ensuring that we don't truncate the input
162 * password.
163 */
164 encrypted_len = ROUND_UP(inlen + 1, 16);
165 if (encrypted_len > (RADIUS_MAX_STRING_LENGTH - 2)) encrypted_len = (RADIUS_MAX_STRING_LENGTH - 2);
166
167 /*
168 * Get the number of padding bytes in the last block.
169 */
170 padding = encrypted_len - (inlen + 1);
171
172 output_len = encrypted_len + 2; /* account for the salt */
173
174 /*
175 * We will have up to 253 octets of data in the output
176 * buffer, some of which are padding.
177 *
178 * If we over-run the output buffer, see if we can drop
179 * some of the padding bytes. If not, we return an error
180 * instead of truncating the password.
181 *
182 * Otherwise we lower the amount of data we copy into the
183 * output buffer, because the last bit is just padding,
184 * and can be safely discarded.
185 */
186 slen = fr_dbuff_set(&work_dbuff, output_len);
187 if (slen < 0) {
188 if (((size_t) -slen) > padding) goto fail;
189
190 output_len += slen;
191 }
192 fr_dbuff_set_to_start(&work_dbuff);
193
194 /*
195 * Copy the password over, and fill the remainder with random data.
196 */
197 (void) fr_dbuff_out_memcpy(tpasswd + 3, in, inlen);
198
199 for (i = 3 + inlen; i < sizeof(tpasswd); i++) {
200 tpasswd[i] = fr_fast_rand(&packet_ctx->rand_ctx);
201 }
202
203 /*
204 * Generate salt. The RFCs say:
205 *
206 * The high bit of salt[0] must be set, each salt in a
207 * packet should be unique, and they should be random
208 *
209 * So we get 15 bytes of randomness, and set the high bit.
210 */
211 r = fr_fast_rand(&packet_ctx->rand_ctx);
212 tpasswd[0] = (0x80 | ((r >> 8) & 0x7f));
213 tpasswd[1] = r & 0xff;
214 tpasswd[2] = inlen; /* length of the password string */
215
216 md5_ctx = fr_md5_ctx_alloc_from_list();
217 md5_ctx_old = fr_md5_ctx_alloc_from_list();
218
219 fr_md5_update(md5_ctx, (uint8_t const *) packet_ctx->common->secret, packet_ctx->common->secret_length);
220 fr_md5_ctx_copy(md5_ctx_old, md5_ctx);
221
222 fr_md5_update(md5_ctx, packet_ctx->request_authenticator, RADIUS_AUTH_VECTOR_LENGTH);
223 fr_md5_update(md5_ctx, &tpasswd[0], 2);
224
225 /*
226 * Do various hashing, and XOR the length+password with
227 * the output of the hash blocks.
228 */
229 for (n = 0; n < encrypted_len; n += AUTH_PASS_LEN) {
230 size_t block_len;
231
232 if (n > 0) {
233 fr_md5_ctx_copy(md5_ctx, md5_ctx_old);
234 fr_md5_update(md5_ctx, tpasswd + 2 + n - AUTH_PASS_LEN, AUTH_PASS_LEN);
235 }
236 fr_md5_final(digest, md5_ctx);
237
238 block_len = encrypted_len - n;
239 if (block_len > AUTH_PASS_LEN) block_len = AUTH_PASS_LEN;
240
241 for (i = 0; i < block_len; i++) {
242#ifdef __COVERITY__
243 /*
244 * Coverity is not doing the calculations correctly - it doesn't see
245 * that setting block_len = encrypted_len - n puts a safe boundary
246 * on block_len so the access to tpasswd won't overflow.
247 */
248 if ((i + 2 + n) >= RADIUS_MAX_STRING_LENGTH) break;
249#endif
250 tpasswd[i + 2 + n] ^= digest[i];
251 }
252 }
253
255 fr_md5_ctx_free_from_list(&md5_ctx_old);
256
257 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, tpasswd, output_len);
258
259 return fr_dbuff_set(dbuff, &work_dbuff);
260}
261
262/*
263 * Encode the contents of an attribute of type TLV.
264 */
266 fr_da_stack_t *da_stack, unsigned int depth,
267 fr_dcursor_t *cursor, void *encode_ctx)
268{
269 ssize_t slen;
270 fr_pair_t const *vp = fr_dcursor_current(cursor);
271 fr_dict_attr_t const *da = da_stack->da[depth];
273
274 for (;;) {
275 FR_PROTO_STACK_PRINT(da_stack, depth);
276
277 /*
278 * This attribute carries sub-TLVs. The sub-TLVs
279 * can only carry a total of 253 bytes of data.
280 */
281
282 /*
283 * Determine the nested type and call the appropriate encoder
284 */
285 if (!da_stack->da[depth + 1]) {
286 fr_dcursor_t child_cursor;
287
288 if (vp->da != da_stack->da[depth]) {
289 fr_strerror_printf("%s: Can't encode empty TLV", __FUNCTION__);
290 return 0;
291 }
292
293 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->vp_group, cursor);
294 vp = fr_dcursor_current(&child_cursor);
295 if (!vp) goto next;
296
297 fr_proto_da_stack_build(da_stack, vp->da);
298
299 /*
300 * Call ourselves recursively to encode children.
301 */
302 slen = encode_tlv(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
303 if (slen < 0) return slen;
304
305 next:
306 vp = fr_dcursor_next(cursor);
307 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
308
309 } else {
310 slen = encode_child(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
311 if (slen < 0) return slen;
312 }
313
314 /*
315 * If nothing updated the attribute, stop
316 */
317 if (!fr_dcursor_current(cursor) || (vp == fr_dcursor_current(cursor))) break;
318
319 /*
320 * We can encode multiple sub TLVs, if after
321 * rebuilding the TLV Stack, the attribute
322 * at this depth is the same.
323 */
324 if ((da != da_stack->da[depth]) || (da_stack->depth < da->depth)) break;
325 vp = fr_dcursor_current(cursor);
326 }
327
328 return fr_dbuff_set(dbuff, &work_dbuff);
329}
330
331static ssize_t encode_pairs(fr_dbuff_t *dbuff, fr_pair_list_t const *vps, void *encode_ctx)
332{
333 ssize_t slen;
334 fr_pair_t const *vp;
335 fr_dcursor_t cursor;
336
337 /*
338 * Note that we skip tags inside of tags!
339 */
341 while ((vp = fr_dcursor_current(&cursor))) {
343
344 /*
345 * Encode an individual VP
346 */
347 slen = fr_radius_encode_pair(dbuff, &cursor, encode_ctx);
348 if (slen < 0) return slen;
349 }
350
351 return fr_dbuff_used(dbuff);
352}
353
354
355/** Encodes the data portion of an attribute
356 *
357 * @return
358 * > 0, Length of the data portion.
359 * = 0, we could not encode anything, skip this attribute (and don't encode the header)
360 * unless it's one of a list of exceptions.
361 * < 0, How many additional bytes we'd need as a negative integer.
362 * PAIR_ENCODE_FATAL_ERROR - Abort encoding the packet.
363 */
365 fr_da_stack_t *da_stack, unsigned int depth,
366 fr_dcursor_t *cursor, void *encode_ctx)
367{
368 ssize_t slen;
369 size_t len;
370 fr_pair_t const *vp = fr_dcursor_current(cursor);
371 fr_dict_attr_t const *da = da_stack->da[depth];
373 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
374 fr_dbuff_t value_dbuff;
375 fr_dbuff_marker_t value_start, src, dest;
376 bool encrypted = false;
377
379 FR_PROTO_STACK_PRINT(da_stack, depth);
380
381 /*
382 * TLVs are just another type of value.
383 */
384 if (da->type == FR_TYPE_TLV) return encode_tlv(dbuff, da_stack, depth, cursor, encode_ctx);
385
386 if (da->type == FR_TYPE_GROUP) return fr_pair_ref_to_network(dbuff, da_stack, depth, cursor);
387
388 /*
389 * Catch errors early on.
390 */
392 fr_strerror_const("Asked to encrypt attribute, but no packet context provided");
394 }
395
396 /*
397 * This has special requirements.
398 */
399 if ((vp->vp_type == FR_TYPE_STRUCT) || (da->type == FR_TYPE_STRUCT)) {
400 slen = fr_struct_to_network(&work_dbuff, da_stack, depth, cursor, encode_ctx, encode_value, encode_child);
401 if (slen <= 0) return slen;
402
403 vp = fr_dcursor_current(cursor);
404 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
405 return fr_dbuff_set(dbuff, &work_dbuff);
406 }
407
408 /*
409 * If it's not a TLV, it should be a value type RFC
410 * attribute make sure that it is.
411 */
412 if (da_stack->da[depth + 1] != NULL) {
413 fr_strerror_printf("%s: Encoding value but not at top of stack", __FUNCTION__);
415 }
416
417 if (vp->da != da) {
418 fr_strerror_printf("%s: Top of stack does not match vp->da", __FUNCTION__);
420 }
421
422 if (fr_type_is_structural(da->type)) {
423 fr_strerror_printf("%s: Called with structural type %s", __FUNCTION__,
424 fr_type_to_str(da_stack->da[depth]->type));
426 }
427
428 /*
429 * Write tag byte
430 *
431 * The Tag field is one octet in length and is intended to provide a
432 * means of grouping attributes in the same packet which refer to the
433 * same tunnel. If the value of the Tag field is greater than 0x00
434 * and less than or equal to 0x1F, it SHOULD be interpreted as
435 * indicating which tunnel (of several alternatives) this attribute
436 * pertains. If the Tag field is greater than 0x1F, it SHOULD be
437 * interpreted as the first byte of the following String field.
438 *
439 * If the first byte of the string value looks like a
440 * tag, then we always encode a tag byte, even one that
441 * is zero.
442 *
443 * Tunnel-Password has a salt, which can be confused with the tag. So we always encode a tag
444 * byte. Except in RADIUS/1.1, where there's no salt.
445 */
446 if ((vp->vp_type == FR_TYPE_STRING) && fr_radius_flag_has_tag(vp->da)) {
447 if (!packet_ctx) {
448 fr_strerror_const("The RADIUS packet encoder needs an encode_ctx to encode tagged attributes");
449 return -1;
450 }
451
452 if (packet_ctx->tag) {
453 FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t)packet_ctx->tag);
454 } else if (TAG_VALID(vp->vp_strvalue[0]) ||
455 (!packet_ctx->radius_1_1 &&
457 FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t)0x00);
458 }
459 }
460
461 /*
462 * Starting here is a value that may require encryption.
463 */
464 value_dbuff = FR_DBUFF(&work_dbuff);
465 fr_dbuff_marker(&value_start, &value_dbuff);
466 fr_dbuff_marker(&src, &value_dbuff);
467 fr_dbuff_marker(&dest, &value_dbuff);
468
469 switch (vp->vp_type) {
470 /*
471 * IPv4 addresses are normal, but IPv6 addresses are special to RADIUS.
472 */
474 if (vp->vp_ip.af == AF_INET) goto encode;
476
477 /*
478 * Common encoder might add scope byte, which we don't want.
479 */
481 FR_DBUFF_IN_MEMCPY_RETURN(&value_dbuff, vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
482 break;
483
485 if (vp->vp_ip.af == AF_INET) goto ipv4_prefix;
487
488 /*
489 * Common encoder doesn't add reserved byte
490 */
492 len = fr_bytes_from_bits(vp->vp_ip.prefix);
493 FR_DBUFF_IN_BYTES_RETURN(&value_dbuff, 0x00, vp->vp_ip.prefix);
494 /* Only copy the minimum number of address bytes required */
495 FR_DBUFF_IN_MEMCPY_RETURN(&value_dbuff, (uint8_t const *)vp->vp_ipv6addr, len);
496 break;
497
498 /*
499 * Common encoder doesn't add reserved byte, so we add one here to be compliant with RFC 8044
500 * Section 3.11.
501 */
503 ipv4_prefix:
504 if (!vp->vp_ipv4addr) {
505 /*
506 * If the ipaddr is all zeros, then the prefix length MUST be set to 32.
507 */
508 FR_DBUFF_IN_BYTES_RETURN(&value_dbuff, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00);
509 } else {
510 uint32_t ipaddr = vp->vp_ipv4addr;
511
512 FR_DBUFF_IN_BYTES_RETURN(&value_dbuff, 0x00, vp->vp_ip.prefix);
513
514 if (vp->vp_ip.prefix == 0) {
515 ipaddr = 0;
516
517 } else if (vp->vp_ip.prefix < 32) {
518 ipaddr &= htonl(~((1UL << (32 - vp->vp_ip.prefix)) - 1));
519
520 } /* else leave ipaddr alone */
521
522 FR_DBUFF_IN_MEMCPY_RETURN(&value_dbuff, (uint8_t const *) &ipaddr, sizeof(ipaddr));
523 }
524 break;
525
526 /*
527 * Special handling for "abinary". Otherwise, fall
528 * through to using the common encoder.
529 */
530 case FR_TYPE_STRING:
531 if (fr_radius_flag_abinary(da)) {
532 slen = fr_radius_encode_abinary(vp, &value_dbuff);
533 if (slen < 0) return slen;
534 break;
535 }
537
538 case FR_TYPE_OCTETS:
539
540 /*
541 * Simple data types use the common encoder.
542 */
543 default:
544 encode:
545 slen = fr_value_box_to_network(&value_dbuff, &vp->data);
546 if (slen < 0) return slen;
547 break;
548 }
549
550 /*
551 * No data: don't encode the value. The type and length should still
552 * be written.
553 */
554 if (fr_dbuff_used(&value_dbuff) == 0) {
555 return_0:
556 vp = fr_dcursor_next(cursor);
557 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
558 return 0;
559 }
560
561 /*
562 * We don't encode encrypted attributes in foreign protocols.
563 */
564 if (packet_ctx && packet_ctx->foreign && (fr_radius_flag_encrypted(da) != RADIUS_FLAG_ENCRYPT_NONE)) goto return_0;
565
566 /*
567 * Do horrible 1993 style obfuscation, unless we're doing RADIUS/1.1
568 *
569 * Attributes with obfuscated values MUST be less than 128 bytes long.
570 */
571 if (packet_ctx && !packet_ctx->radius_1_1) switch (fr_radius_flag_encrypted(da)) {
573 /*
574 * Encode the password in place
575 */
576 slen = encode_password(&work_dbuff, &value_start, fr_dbuff_used(&value_dbuff), packet_ctx);
577 if (slen < 0) return slen;
578 encrypted = true;
579 break;
580
584 fr_strerror_printf("Attributes with 'encrypt=Tunnel-Password' set cannot go into %s.",
586 goto return_0;
587 }
588
589 slen = encode_tunnel_password(&work_dbuff, &value_start, fr_dbuff_used(&value_dbuff), packet_ctx);
590 if (slen < 0) return slen;
591
592 encrypted = true;
593 break;
594
595 /*
596 * The code above ensures that this attribute
597 * always fits.
598 */
600 /*
601 * @todo radius decoding also uses fr_radius_ascend_secret() (Vernam cipher
602 * is its own inverse). As part of converting decode, make sure the caller
603 * there can pass a marker so we can use it here, too.
604 */
605 slen = fr_radius_ascend_secret(&work_dbuff, fr_dbuff_current(&value_start), fr_dbuff_used(&value_dbuff),
606 packet_ctx->common->secret, packet_ctx->common->secret_length,
607 packet_ctx->request_authenticator);
608 if (slen < 0) return slen;
609 encrypted = true;
610 break;
611
613 break;
614
616 fr_strerror_const("Invalid encryption type");
618 }
619
620 if (!encrypted) {
621 fr_dbuff_set(&work_dbuff, &value_dbuff);
622 fr_dbuff_set(&value_start, fr_dbuff_start(&value_dbuff));
623 }
624
625 /*
626 * High byte of 32bit integers gets set to the tag
627 * value.
628 *
629 * The Tag field is one octet in length and is intended to provide a
630 * means of grouping attributes in the same packet which refer to the
631 * same tunnel. Valid values for this field are 0x01 through 0x1F,
632 * inclusive. If the Tag field is unused, it MUST be zero (0x00).
633 */
634 if ((vp->vp_type == FR_TYPE_UINT32) && fr_radius_flag_has_tag(vp->da)) {
635 uint8_t msb = 0;
636 /*
637 * Only 24bit integers are allowed here
638 */
639 fr_dbuff_set(&src, &value_start);
640 (void) fr_dbuff_out(&msb, &src);
641 if (msb != 0) {
642 fr_strerror_const("Integer overflow for tagged uint32 attribute");
643 goto return_0;
644 }
645 fr_dbuff_set(&dest, &value_start);
646 fr_dbuff_in(&dest, packet_ctx->tag);
647 }
648
649 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "value %s",
650 fr_type_to_str(vp->vp_type));
651
652 /*
653 * Rebuilds the TLV stack for encoding the next attribute
654 */
655 vp = fr_dcursor_next(cursor);
656 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
657
658 return fr_dbuff_set(dbuff, &work_dbuff);
659}
660
661/** Breaks down large data into pieces, each with a header
662 *
663 * @param[out] data we're fragmenting.
664 * @param[in] data_len the amount of data in the dbuff that makes up the value we're
665 * splitting.
666 * @param[in,out] hdr marker that points at said header
667 * @param[in] hdr_len length of the headers that will be added
668 * @param[in] flag_offset offset within header of a flag byte whose MSB is set for all
669 * but the last piece.
670 * @param[in] vsa_offset if non-zero, the offset of a length field in a (sub?)-header
671 * of size 3 that also needs to be adjusted to include the number
672 * of bytes of data in the piece
673 * @return
674 * - <0 the number of bytes we would have needed to create
675 * space for another attribute header in the buffer.
676 * - 0 data was not modified.
677 * - >0 the number additional bytes we used inserting extra
678 * headers.
679 */
680static ssize_t attr_fragment(fr_dbuff_t *data, size_t data_len, fr_dbuff_marker_t *hdr, size_t hdr_len,
681 size_t flag_offset, size_t vsa_offset)
682{
683 unsigned int num_fragments, i = 0;
684 size_t max_frag_data = UINT8_MAX - hdr_len;
685 fr_dbuff_t frag_data = FR_DBUFF_ABS(hdr);
686 fr_dbuff_marker_t frag_hdr, frag_hdr_p;
687
688 if (unlikely(!data_len)) return 0; /* Shouldn't have been called */
689
690 num_fragments = ROUND_UP_DIV(data_len, max_frag_data);
691 if (num_fragments == 1) return 0; /* Nothing to do */
692
693 fr_dbuff_marker(&frag_hdr, &frag_data);
694 fr_dbuff_marker(&frag_hdr_p, &frag_data);
695
696 fr_dbuff_advance(&frag_data, hdr_len);
697
698 FR_PROTO_HEX_DUMP(fr_dbuff_current(hdr), hdr_len + data_len, "attr_fragment in");
699 for (;;) {
700 bool last = (i + 1) == num_fragments;
701 uint8_t frag_len;
702
703 /*
704 * How long is this fragment?
705 */
706 if (last) {
707 frag_len = (data_len - (max_frag_data * (num_fragments - 1)));
708 } else {
709 frag_len = max_frag_data;
710 }
711
712 /*
713 * Update the "outer" header to reflect the actual
714 * length of the fragment
715 */
716 fr_dbuff_set(&frag_hdr_p, &frag_hdr);
717 fr_dbuff_advance(&frag_hdr_p, 1);
718 fr_dbuff_in(&frag_hdr_p, (uint8_t)(hdr_len + frag_len));
719
720 /*
721 * Update the "inner" header. The length here is
722 * the inner VSA header length (3) + the fragment
723 * length.
724 */
725 if (vsa_offset) {
726 fr_dbuff_set(&frag_hdr_p, fr_dbuff_current(&frag_hdr) + vsa_offset);
727 fr_dbuff_in(&frag_hdr_p, (uint8_t)(2 + frag_len + (flag_offset != 0)));
728 }
729
730 /*
731 * Just over-ride the flag field. Nothing else
732 * uses it.
733 */
734 if (flag_offset) {
735 fr_dbuff_set(&frag_hdr_p, fr_dbuff_current(&frag_hdr) + flag_offset);
736 fr_dbuff_in(&frag_hdr_p, (uint8_t)(!last << 7));
737 }
738
739 FR_PROTO_HEX_DUMP(fr_dbuff_current(hdr), frag_len + hdr_len,
740 "attr_fragment fragment %u/%u", i + 1, num_fragments);
741
742 fr_dbuff_advance(&frag_data, frag_len); /* Go to the start of the next fragment */
743 if (last) break;
744
745 /*
746 * There's still trailing data after this
747 * fragment. Move the trailing data to *past*
748 * the next header. And after there's room, copy
749 * the header over.
750 *
751 * This process leaves the next header in place,
752 * ready for the next iteration of the loop.
753 *
754 * Yes, moving things multiple times is less than
755 * efficient. Oh well. it's ~1K memmoved()
756 * maybe 4 times. We are nowhere near the CPU /
757 * electrical requirements of Bitcoin.
758 */
759 i++;
760
761 fr_dbuff_set(&frag_hdr, &frag_data); /* Remember where the header should be */
762 FR_DBUFF_ADVANCE_EXTEND_RETURN(&frag_data, hdr_len); /* Advance past the header */
763
764 /*
765 * Shift remaining data by hdr_len.
766 */
767 FR_DBUFF_IN_MEMCPY_RETURN(&FR_DBUFF(&frag_data), &frag_hdr, data_len - (i * max_frag_data));
768 fr_dbuff_in_memcpy(&FR_DBUFF(&frag_hdr), hdr, hdr_len); /* Copy the old header over */
769 }
770
771 return fr_dbuff_set(data, &frag_data);
772}
773
774/** Encode an "extended" attribute
775 *
776 */
778 fr_da_stack_t *da_stack, NDEBUG_UNUSED unsigned int depth,
779 fr_dcursor_t *cursor, void *encode_ctx)
780{
781 ssize_t slen;
782 uint8_t hlen;
783 size_t vendor_hdr;
784 bool extra;
785 int my_depth;
786 fr_dict_attr_t const *da;
787 fr_dbuff_marker_t hdr, length_field;
788 fr_pair_t const *vp = fr_dcursor_current(cursor);
789 fr_dbuff_t work_dbuff;
790
792 FR_PROTO_STACK_PRINT(da_stack, depth);
793
794 extra = fr_radius_flag_long_extended(da_stack->da[0]);
795
796 /*
797 * The data used here can be more than 255 bytes, but only for the
798 * "long" extended type.
799 */
800 if (extra) {
801 work_dbuff = FR_DBUFF_BIND_CURRENT(dbuff);
802 } else {
803 work_dbuff = FR_DBUFF_MAX_BIND_CURRENT(dbuff, UINT8_MAX);
804 }
805 fr_dbuff_marker(&hdr, &work_dbuff);
806
807 /*
808 * Encode the header for "short" or "long" attributes
809 */
810 hlen = 3 + extra;
811 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)da_stack->da[0]->attr);
812 fr_dbuff_marker(&length_field, &work_dbuff);
813 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, hlen); /* this gets overwritten later*/
814
815 /*
816 * Encode which extended attribute it is.
817 */
818 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)da_stack->da[1]->attr);
819
820 if (extra) FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, 0x00); /* flags start off at zero */
821
822 FR_PROTO_STACK_PRINT(da_stack, depth);
823
824 /*
825 * Handle VSA as "VENDOR + attr"
826 */
827 if (da_stack->da[1]->type == FR_TYPE_VSA) {
828 fr_assert(da_stack->da[2]);
829 fr_assert(da_stack->da[2]->type == FR_TYPE_VENDOR);
830
831 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t) da_stack->da[2]->attr);
832
833 fr_assert(da_stack->da[3]);
834
835 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)da_stack->da[3]->attr);
836
837 hlen += 5;
838 vendor_hdr = 5;
839
840 FR_PROTO_STACK_PRINT(da_stack, depth);
841 FR_PROTO_HEX_DUMP(fr_dbuff_current(&hdr), hlen, "header extended vendor specific");
842
843 my_depth = 3;
844 } else {
845 vendor_hdr = 0;
846 FR_PROTO_HEX_DUMP(fr_dbuff_current(&hdr), hlen, "header extended");
847
848 my_depth = 1;
849 }
850
851 /*
852 * We're at the point where we need to encode something.
853 */
854 da = da_stack->da[my_depth];
855 fr_assert(vp->da == da);
856
857 if (da->type != FR_TYPE_STRUCT) {
858 slen = encode_value(&work_dbuff, da_stack, my_depth, cursor, encode_ctx);
859
860 } else {
861 slen = fr_struct_to_network(&work_dbuff, da_stack, my_depth, cursor, encode_ctx, encode_value, encode_child);
862 }
863 if (slen <= 0) return slen;
864
865 /*
866 * There may be more than 255 octets of data encoded in
867 * the attribute. If so, move the data up in the packet,
868 * and copy the existing header over. Set the "M" flag ONLY
869 * after copying the rest of the data.
870 *
871 * Note that we add "vendor_hdr" to the length of the
872 * encoded data. That 5 octet field is logically part of
873 * the data, and not part of the header.
874 */
875 if (slen > (UINT8_MAX - hlen)) {
876 slen = attr_fragment(&work_dbuff, (size_t)vendor_hdr + slen, &hdr, 4, 3, 0);
877 if (slen <= 0) return slen;
878
879 return fr_dbuff_set(dbuff, &work_dbuff);
880 }
881
882 fr_dbuff_in_bytes(&length_field, (uint8_t) fr_dbuff_used(&work_dbuff));
883 FR_PROTO_HEX_DUMP(fr_dbuff_current(&hdr), hlen, "header extended");
884
885 return fr_dbuff_set(dbuff, &work_dbuff);
886}
887
888/*
889 * The encode_extended() function expects to see the TLV or
890 * STRUCT inside of the extended attribute, in which case it
891 * creates the attribute header and calls encode_value() for the
892 * leaf type, or child TLV / struct.
893 *
894 * If we see VSA or VENDOR, then we recurse past that to a child
895 * which is either a leaf, or a TLV, or a STRUCT.
896 */
898 fr_da_stack_t *da_stack, unsigned int depth,
899 fr_dcursor_t *cursor, void *encode_ctx)
900{
901 ssize_t slen;
903 fr_dcursor_t child_cursor;
904 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
905
906 parent = fr_dcursor_current(cursor);
908
909 (void) fr_pair_dcursor_child_iter_init(&child_cursor, &parent->vp_group, cursor);
910
911 FR_PROTO_STACK_PRINT(da_stack, depth);
912
913 while ((vp = fr_dcursor_current(&child_cursor)) != NULL) {
914 if ((vp->vp_type == FR_TYPE_VSA) || (vp->vp_type == FR_TYPE_VENDOR)) {
915 slen = encode_extended_nested(&work_dbuff, da_stack, depth + 1, &child_cursor, encode_ctx);
916
917 } else {
918 fr_proto_da_stack_build(da_stack, vp->da);
919 slen = encode_extended(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
920 }
921
922 if (slen < 0) return slen;
923 }
924
925 vp = fr_dcursor_next(cursor);
926
927 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
928
929 return fr_dbuff_set(dbuff, &work_dbuff);
930}
931
932
933/** Encode an RFC format attribute, with the "concat" flag set
934 *
935 * If there isn't enough freespace in the packet, the data is
936 * truncated to fit.
937 *
938 * The attribute is split on 253 byte boundaries, with a header
939 * prepended to each chunk.
940 */
942 fr_da_stack_t *da_stack, unsigned int depth,
943 fr_dcursor_t *cursor, UNUSED void *encode_ctx)
944{
945 uint8_t const *p;
946 size_t data_len;
947 fr_pair_t const *vp = fr_dcursor_current(cursor);
948 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
950
951 FR_PROTO_STACK_PRINT(da_stack, depth);
952
953 p = vp->vp_octets;
954 data_len = vp->vp_length;
955 fr_dbuff_marker(&hdr, &work_dbuff);
956
957 while (data_len > 0) {
958 size_t frag_len = (data_len > RADIUS_MAX_STRING_LENGTH) ? RADIUS_MAX_STRING_LENGTH : data_len;
959
960 fr_dbuff_set(&hdr, &work_dbuff);
961 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) da_stack->da[depth]->attr, 0x00);
962
963 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, p, frag_len);
964
965 fr_dbuff_advance(&hdr, 1);
966 fr_dbuff_in(&hdr, (uint8_t) (2 + frag_len));
967
968 FR_PROTO_HEX_DUMP(fr_dbuff_current(&hdr) - 1, 2 + frag_len, "encode_concat fragment");
969
970 p += frag_len;
971 data_len -= frag_len;
972 }
973
974 vp = fr_dcursor_next(cursor);
975
976 /*
977 * @fixme: attributes with 'concat' MUST of type
978 * 'octets', and therefore CANNOT have any TLV data in them.
979 */
980 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
981
982 return fr_dbuff_set(dbuff, &work_dbuff);
983}
984
985/** Encode an RFC format attribute.
986 *
987 * This could be a standard attribute, or a TLV data type.
988 * If it's a standard attribute, then vp->da->attr == attribute.
989 * Otherwise, attribute may be something else.
990 */
992 fr_da_stack_t *da_stack, unsigned int depth,
993 fr_dcursor_t *cursor, void *encode_ctx)
994{
995 ssize_t slen;
996 uint8_t hlen;
998 fr_dbuff_t work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
999
1000 FR_PROTO_STACK_PRINT(da_stack, depth);
1001
1002 fr_assert(da_stack->da[depth] != NULL);
1003
1004 fr_dbuff_marker(&hdr, &work_dbuff);
1005
1006 hlen = 2;
1007 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)da_stack->da[depth]->attr, hlen);
1008
1009 slen = encode_value(&work_dbuff, da_stack, depth, cursor, encode_ctx);
1010 if (slen <= 0) return slen;
1011
1012 fr_dbuff_advance(&hdr, 1);
1013 fr_dbuff_in_bytes(&hdr, (uint8_t)(hlen + slen));
1014
1015 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), 2, "header rfc");
1016
1017 return fr_dbuff_set(dbuff, &work_dbuff);
1018}
1019
1020
1021/** Encode one full Vendor-Specific + Vendor-ID + Vendor-Attr + Vendor-Length + ...
1022 */
1024 fr_da_stack_t *da_stack, unsigned int depth,
1025 fr_dcursor_t *cursor, void *encode_ctx)
1026{
1027 ssize_t slen;
1028 size_t hdr_len;
1029 fr_dbuff_marker_t hdr, length_field, vsa_length_field;
1030 fr_dict_attr_t const *da, *dv;
1031 fr_dbuff_t work_dbuff;
1032
1033 FR_PROTO_STACK_PRINT(da_stack, depth);
1034
1035 dv = da_stack->da[depth++];
1036
1037 if (dv->type != FR_TYPE_VENDOR) {
1038 fr_strerror_const("Expected Vendor");
1040 }
1041
1042 /*
1043 * Now we encode one vendor attribute.
1044 */
1045 da = da_stack->da[depth];
1046 fr_assert(da != NULL);
1047
1048 /*
1049 * Most VSAs get limited to the one attribute. Only refs
1050 * (e.g. DHCPv4, DHCpv6) can get fragmented.
1051 */
1052 if (da->type != FR_TYPE_GROUP) {
1053 work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
1054 } else {
1055 work_dbuff = FR_DBUFF(dbuff);
1056 }
1057
1058 fr_dbuff_marker(&hdr, &work_dbuff);
1059
1060 /*
1061 * Build the Vendor-Specific header
1062 */
1063 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_VENDOR_SPECIFIC);
1064
1065 fr_dbuff_marker(&length_field, &work_dbuff);
1066 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, 0);
1067
1068 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t)dv->attr); /* Copy in the 32bit vendor ID */
1069
1070
1071 hdr_len = dv->flags.type_size + dv->flags.length;
1072
1073 /*
1074 * Vendors use different widths for their
1075 * attribute number fields.
1076 */
1077 switch (dv->flags.type_size) {
1078 default:
1079 fr_strerror_printf("%s: Internal sanity check failed, type %u", __FUNCTION__, (unsigned) dv->flags.type_size);
1081
1082 case 4:
1083 fr_dbuff_in(&work_dbuff, (uint32_t)da->attr);
1084 break;
1085
1086 case 2:
1087 fr_dbuff_in(&work_dbuff, (uint16_t)da->attr);
1088 break;
1089
1090 case 1:
1091 fr_dbuff_in(&work_dbuff, (uint8_t)da->attr);
1092 break;
1093 }
1094
1095 /*
1096 * The length fields will get over-written later.
1097 */
1098 switch (dv->flags.length) {
1099 default:
1100 fr_strerror_printf("%s: Internal sanity check failed, length %u", __FUNCTION__, (unsigned) dv->flags.length);
1102
1103 case 0:
1104 break;
1105
1106 case 2:
1107 fr_dbuff_in_bytes(&work_dbuff, 0);
1109
1110 case 1:
1111 /*
1112 * Length fields are set to zero, because they
1113 * will get over-ridden later.
1114 */
1115 fr_dbuff_marker(&vsa_length_field, &work_dbuff);
1116 fr_dbuff_in_bytes(&work_dbuff, 0);
1117 break;
1118 }
1119
1120 slen = encode_value(&work_dbuff, da_stack, depth, cursor, encode_ctx);
1121 if (slen <= 0) return slen;
1122
1123 /*
1124 * There may be more than 253 octets of data encoded in
1125 * the attribute. If so, move the data up in the packet,
1126 * and copy the existing header over. Set the "C" flag
1127 * ONLY after copying the rest of the data.
1128 *
1129 * Note that we do NOT check 'slen' here, as it's only
1130 * the size of the sub-sub attribute, and doesn't include
1131 * the RADIUS attribute header, or Vendor-ID.
1132 */
1133 if (fr_dbuff_used(&work_dbuff) > UINT8_MAX) {
1134 size_t length_offset = 0;
1135
1136 if (dv->flags.length) length_offset = 6 + hdr_len - 1;
1137
1138 slen = attr_fragment(&work_dbuff, (size_t)slen, &hdr, 6 + hdr_len, 0, length_offset);
1139 if (slen <= 0) return slen;
1140 } else {
1141 if (dv->flags.length) {
1142 fr_dbuff_in(&vsa_length_field, (uint8_t)(hdr_len + slen));
1143 }
1144
1145 fr_dbuff_in(&length_field, (uint8_t) fr_dbuff_used(&work_dbuff));
1146 }
1147
1148 FR_PROTO_HEX_DUMP(fr_dbuff_current(&hdr), 6 + hdr_len, "header vsa");
1149
1150 return fr_dbuff_set(dbuff, &work_dbuff);
1151}
1152
1153/** Encode a WiMAX attribute
1154 *
1155 */
1157 fr_da_stack_t *da_stack, unsigned int depth,
1158 fr_dcursor_t *cursor, void *encode_ctx)
1159{
1160 ssize_t slen;
1161 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
1162 fr_dbuff_marker_t hdr, length_field, vsa_length_field;
1163 fr_dict_attr_t const *dv;
1164 fr_pair_t const *vp = fr_dcursor_current(cursor);
1165
1166 fr_dbuff_marker(&hdr, &work_dbuff);
1167
1168 PAIR_VERIFY(vp);
1169 FR_PROTO_STACK_PRINT(da_stack, depth);
1170
1171 dv = da_stack->da[depth++];
1172
1173 if (dv->type != FR_TYPE_VENDOR) {
1174 fr_strerror_const("Expected Vendor");
1176 }
1177
1178 FR_PROTO_STACK_PRINT(da_stack, depth);
1179
1180 /*
1181 * Build the Vendor-Specific header
1182 */
1183 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_VENDOR_SPECIFIC);
1184 fr_dbuff_marker(&length_field, &work_dbuff);
1185 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, 0x09);
1186
1187 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t) dv->attr);
1188
1189 /*
1190 * Encode the first attribute
1191 */
1192 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)da_stack->da[depth]->attr);
1193
1194 fr_dbuff_marker(&vsa_length_field, &work_dbuff);
1195 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, 0x03, 0x00); /* length + continuation, both may be overwritten later */
1196
1197 /*
1198 * We don't bound the size of work_dbuff; it can use more than UINT8_MAX bytes
1199 * because of the "continuation" byte.
1200 */
1201 slen = encode_value(&work_dbuff, da_stack, depth, cursor, encode_ctx);
1202 if (slen <= 0) return slen;
1203
1204 /*
1205 * There may be more than 253 octets of data encoded in
1206 * the attribute. If so, move the data up in the packet,
1207 * and copy the existing header over. Set the "C" flag
1208 * ONLY after copying the rest of the data.
1209 *
1210 * Note that we do NOT check 'slen' here, as it's only
1211 * the size of the sub-sub attribute, and doesn't include
1212 * the RADIUS attribute header, or Vendor-ID.
1213 */
1214 if (fr_dbuff_used(&work_dbuff) > UINT8_MAX) {
1215 slen = attr_fragment(&work_dbuff, (size_t)slen, &hdr, 9, 8, 7);
1216 if (slen <= 0) return slen;
1217
1218 return fr_dbuff_set(dbuff, &work_dbuff);
1219 }
1220
1221 fr_dbuff_in_bytes(&vsa_length_field, (uint8_t) (fr_dbuff_used(&work_dbuff) - 6));
1222 fr_dbuff_in_bytes(&length_field, (uint8_t) fr_dbuff_used(&work_dbuff));
1223
1224 FR_PROTO_HEX_DUMP(fr_dbuff_current(&hdr), 9, "header wimax");
1225
1226 return fr_dbuff_set(dbuff, &work_dbuff);
1227}
1228
1230 fr_da_stack_t *da_stack, unsigned int depth,
1231 fr_dcursor_t *cursor, void *encode_ctx)
1232{
1233 fr_dict_attr_t const *da = da_stack->da[depth];
1234 ssize_t slen;
1235 fr_pair_t *vp;
1236 fr_dict_vendor_t const *dv;
1237 fr_dcursor_t child_cursor;
1238 fr_dbuff_t work_dbuff;
1239
1240 FR_PROTO_STACK_PRINT(da_stack, depth);
1241
1242 if (da->type != FR_TYPE_VENDOR) {
1243 fr_strerror_printf("%s: Expected type \"vendor\" got \"%s\"", __FUNCTION__,
1244 fr_type_to_str(da->type));
1246 }
1247
1248 dv = fr_dict_vendor_by_da(da_stack->da[depth]);
1249
1250 /*
1251 * Flat hierarchy, encode one attribute at a time.
1252 *
1253 * Note that there's no attempt to encode multiple VSAs
1254 * into one attribute. We can add that back as a flag,
1255 * once all of the nested attribute conversion has been
1256 * done.
1257 */
1258 if (da_stack->da[depth + 1]) {
1259 if (dv && dv->continuation) {
1260 return encode_wimax(dbuff, da_stack, depth, cursor, encode_ctx);
1261 }
1262
1263 return encode_vendor_attr(dbuff, da_stack, depth, cursor, encode_ctx);
1264 }
1265
1266 /*
1267 * Loop over the children of this attribute of type Vendor.
1268 */
1269 vp = fr_dcursor_current(cursor);
1270 fr_assert(vp->da == da);
1271 work_dbuff = FR_DBUFF(dbuff);
1272
1273 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->vp_group, cursor);
1274 while ((vp = fr_dcursor_current(&child_cursor)) != NULL) {
1275 fr_proto_da_stack_build(da_stack, vp->da);
1276
1277 if (dv && dv->continuation) {
1278 slen = encode_wimax(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
1279 } else {
1280 slen = encode_vendor_attr(&work_dbuff, da_stack, depth, &child_cursor, encode_ctx);
1281 }
1282 if (slen < 0) return slen;
1283 }
1284
1285 vp = fr_dcursor_next(cursor);
1286 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
1287
1288 return fr_dbuff_set(dbuff, &work_dbuff);
1289}
1290
1291/** Encode a Vendor-Specific attribute
1292 *
1293 */
1295 fr_da_stack_t *da_stack, unsigned int depth,
1296 fr_dcursor_t *cursor, void *encode_ctx)
1297{
1298 ssize_t slen;
1299 fr_pair_t *vp;
1300 fr_dcursor_t child_cursor;
1301 fr_dict_attr_t const *da = da_stack->da[depth];
1302 fr_dbuff_t work_dbuff;
1303
1304 FR_PROTO_STACK_PRINT(da_stack, depth);
1305
1306 if (da->type != FR_TYPE_VSA) {
1307 fr_strerror_printf("%s: Expected type \"vsa\" got \"%s\"", __FUNCTION__,
1308 fr_type_to_str(da->type));
1310 }
1311
1312 /*
1313 * Loop over the contents of Vendor-Specific, each of
1314 * which MUST be of type FR_TYPE_VENDOR.
1315 */
1316 if (da_stack->da[depth + 1]) {
1317 return encode_vendor(dbuff, da_stack, depth + 1, cursor, encode_ctx);
1318 }
1319
1320 work_dbuff = FR_DBUFF(dbuff);
1321
1322 vp = fr_dcursor_current(cursor);
1323 if (vp->da != da_stack->da[depth]) {
1324 fr_strerror_printf("%s: Can't encode empty Vendor-Specific", __FUNCTION__);
1325 return 0;
1326 }
1327
1328 /*
1329 * Loop over the children of this Vendor-Specific
1330 * attribute.
1331 */
1332 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->vp_group, cursor);
1333 while ((vp = fr_dcursor_current(&child_cursor)) != NULL) {
1334 fr_proto_da_stack_build(da_stack, vp->da);
1335
1336 fr_assert(da_stack->da[depth + 1]->type == FR_TYPE_VENDOR);
1337
1338 slen = encode_vendor(&work_dbuff, da_stack, depth + 1, &child_cursor, encode_ctx);
1339 if (slen < 0) return slen;
1340 }
1341
1342 /*
1343 * Fix up the da stack, and return the data we've encoded.
1344 */
1345 vp = fr_dcursor_next(cursor);
1346 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
1347
1348 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), 6, "header vsa");
1349
1350 return fr_dbuff_set(dbuff, &work_dbuff);
1351}
1352
1353/** Encode NAS-Filter-Rule
1354 *
1355 * Concatenating the string attributes together, separated by a 0x00 byte,
1356 */
1358 fr_da_stack_t *da_stack, NDEBUG_UNUSED unsigned int depth,
1359 fr_dcursor_t *cursor, UNUSED void *encode_ctx)
1360{
1361 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
1362 fr_dbuff_marker_t hdr, frag_hdr;
1363 fr_pair_t *vp = fr_dcursor_current(cursor);
1364 size_t attr_len = 2;
1365
1366 FR_PROTO_STACK_PRINT(da_stack, depth);
1367
1368 fr_assert(vp);
1369 fr_assert(vp->da);
1370
1371 fr_dbuff_marker(&hdr, &work_dbuff);
1372 fr_dbuff_marker(&frag_hdr, &work_dbuff);
1373 fr_dbuff_advance(&hdr, 1);
1374 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)vp->da->attr, 0x00);
1375
1377
1378 while (true) {
1379 size_t data_len = vp->vp_length;
1380 size_t frag_len;
1381 char const *p = vp->vp_strvalue;
1382
1383 /*
1384 * Keep encoding this attribute until it's done.
1385 */
1386 while (data_len > 0) {
1387 frag_len = data_len;
1388
1389 /*
1390 * This fragment doesn't overflow the
1391 * attribute. Copy it over, update the
1392 * length, but leave the marker at the
1393 * current header.
1394 */
1395 if ((attr_len + frag_len) <= UINT8_MAX) {
1396 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, p, frag_len);
1397 attr_len += frag_len;
1398
1399 fr_dbuff_set(&frag_hdr, &hdr);
1400 fr_dbuff_in(&frag_hdr, (uint8_t) attr_len); /* there's no fr_dbuff_in_no_advance() */
1401 break;
1402 }
1403
1404 /*
1405 * This fragment overflows the attribute.
1406 * Copy the fragment in, and create a new
1407 * attribute header.
1408 */
1409 frag_len = UINT8_MAX - attr_len;
1410 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, p, frag_len);
1411 fr_dbuff_in(&hdr, (uint8_t) UINT8_MAX);
1412
1413 fr_dbuff_set(&hdr, &work_dbuff);
1414 fr_dbuff_advance(&hdr, 1);
1415 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)vp->da->attr, 0x02);
1416 attr_len = 2;
1417
1418 p += frag_len;
1419 data_len -= frag_len;
1420 }
1421
1422 /*
1423 * If we have nothing more to do here, then stop.
1424 */
1425 vp = fr_dcursor_next(cursor);
1426 if (!vp || (vp->da != attr_nas_filter_rule)) {
1427 break;
1428 }
1429
1430 /*
1431 * We have to add a zero byte. If it doesn't
1432 * overflow the current attribute, then just add
1433 * it in.
1434 */
1435 if (attr_len < UINT8_MAX) {
1436 attr_len++;
1437 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, 0x00);
1438
1439 fr_dbuff_set(&frag_hdr, &hdr);
1440 fr_dbuff_in(&frag_hdr, (uint8_t) attr_len); /* there's no fr_dbuff_in_no_advance() */
1441 continue;
1442 }
1443
1444 /*
1445 * The zero byte causes the current attribute to
1446 * overflow. Create a new header with the zero
1447 * byte already populated, and keep going.
1448 */
1449 fr_dbuff_set(&hdr, &work_dbuff);
1450 fr_dbuff_advance(&hdr, 1);
1451 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)vp->da->attr, 0x00, 0x00);
1452 attr_len = 3;
1453 }
1454
1455 vp = fr_dcursor_current(cursor);
1456 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
1457
1458 return fr_dbuff_set(dbuff, &work_dbuff);
1459}
1460
1461/** Encode an RFC standard attribute 1..255
1462 *
1463 * This function is not the same as encode_child(), because this
1464 * one treats some "top level" attributes as special. e.g.
1465 * Message-Authenticator.
1466 */
1467static ssize_t encode_rfc(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth,
1468 fr_dcursor_t *cursor, void *encode_ctx)
1469{
1470 fr_pair_t const *vp = fr_dcursor_current(cursor);
1471 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
1472 fr_dbuff_marker_t start;
1474
1475 fr_dbuff_marker(&start, &work_dbuff);
1476
1477 /*
1478 * Sanity checks
1479 */
1480 PAIR_VERIFY(vp);
1481 FR_PROTO_STACK_PRINT(da_stack, depth);
1482
1483 switch (da_stack->da[depth]->type) {
1484 case FR_TYPE_TLV:
1485 case FR_TYPE_VSA:
1486 case FR_TYPE_VENDOR:
1487 /* FR_TYPE_STRUCT is actually allowed... */
1488 fr_strerror_printf("%s: Expected leaf type got \"%s\"", __FUNCTION__,
1489 fr_type_to_str(da_stack->da[depth]->type));
1491
1492 default:
1493 /*
1494 * Attribute 0 is fine as a TLV leaf, or VSA, but not
1495 * in the original standards space.
1496 */
1497 if (((fr_dict_vendor_num_by_da(da_stack->da[depth]) == 0) && (da_stack->da[depth]->attr == 0)) ||
1498 (da_stack->da[depth]->attr > UINT8_MAX)) {
1499 (void) fr_dcursor_next(cursor);
1500 return 0;
1501 }
1502 break;
1503 }
1504
1505 /*
1506 * Only CUI is allowed to have zero length.
1507 * Thank you, WiMAX!
1508 */
1509 if ((vp->da == attr_chargeable_user_identity) && (vp->vp_length == 0)) {
1510 fr_dbuff_in_bytes(&work_dbuff, (uint8_t)vp->da->attr, 0x02);
1511
1512 FR_PROTO_HEX_DUMP(fr_dbuff_current(&start), 2, "header rfc");
1513
1514 vp = fr_dcursor_next(cursor);
1515 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
1516 return fr_dbuff_set(dbuff, &work_dbuff);
1517 }
1518
1519 /*
1520 * Message-Authenticator is calculated automatically.
1521 *
1522 * We skip if we've already seen it, _or_ it's RADIUS/1.1, where it's not used.
1523 */
1525 if (!packet_ctx->radius_1_1 && !packet_ctx->seen_message_authenticator) {
1526 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t)vp->da->attr, 18);
1528
1530 "message-authenticator");
1531 FR_PROTO_HEX_DUMP(fr_dbuff_current(&start), 2, "header rfc");
1532
1533 packet_ctx->seen_message_authenticator = true;
1534 }
1535
1536 vp = fr_dcursor_next(cursor);
1537 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
1538 return fr_dbuff_set(dbuff, &work_dbuff);
1539 }
1540
1541 /*
1542 * NAS-Filter-Rule has a stupid format in order to save
1543 * one byte per attribute.
1544 */
1545 if (vp->da == attr_nas_filter_rule) {
1546 return encode_nas_filter_rule(dbuff, da_stack, depth, cursor, encode_ctx);
1547 }
1548
1549 /*
1550 * Once we've checked for various top-level magic, RFC attributes are just TLVs.
1551 */
1552 return encode_child(dbuff, da_stack, depth, cursor, encode_ctx);
1553}
1554
1555/** Encode a data structure into a RADIUS attribute
1556 *
1557 * This is the main entry point into the encoder. It sets up the encoder array
1558 * we use for tracking our TLV/VSA nesting and then calls the appropriate
1559 * dispatch function.
1560 *
1561 * @param[out] dbuff Where to write encoded data.
1562 * @param[in] cursor Specifying attribute to encode.
1563 * @param[in] encode_ctx Additional data such as the shared secret to use.
1564 * @return
1565 * - >0 The number of bytes written to out.
1566 * - 0 Nothing to encode (or attribute skipped).
1567 * - <0 an error occurred.
1568 */
1570{
1571 fr_pair_t const *vp;
1572 ssize_t slen;
1573 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
1574
1575 fr_da_stack_t da_stack;
1576 fr_dict_attr_t const *da = NULL;
1577
1578 if (!cursor) return PAIR_ENCODE_FATAL_ERROR;
1579
1580 vp = fr_dcursor_current(cursor);
1581 if (!vp) return 0;
1582
1583 PAIR_VERIFY(vp);
1584
1585 if (vp->da->depth > FR_DICT_MAX_TLV_STACK) {
1586 fr_strerror_printf("%s: Attribute depth %u exceeds maximum nesting depth %i",
1587 __FUNCTION__, vp->da->depth, FR_DICT_MAX_TLV_STACK);
1589 }
1590
1591 /*
1592 * Tags are *top-level*, and are never nested.
1593 */
1594 if ((vp->vp_type == FR_TYPE_GROUP) && vp->da->flags.internal &&
1595 (vp->da->attr > FR_TAG_BASE) && (vp->da->attr < (FR_TAG_BASE + 0x20))) {
1597
1598 packet_ctx->tag = vp->da->attr - FR_TAG_BASE;
1599 fr_assert(packet_ctx->tag > 0);
1600 fr_assert(packet_ctx->tag < 0x20);
1601
1602 // recurse to encode the children of this attribute
1603 slen = encode_pairs(&work_dbuff, &vp->vp_group, encode_ctx);
1604 packet_ctx->tag = 0;
1605 if (slen < 0) return slen;
1606
1607 fr_dcursor_next(cursor); /* skip the tag attribute */
1608 return fr_dbuff_set(dbuff, &work_dbuff);
1609 }
1610
1611 /*
1612 * Check for zero-length attributes.
1613 */
1614 switch (vp->vp_type) {
1615 default:
1616 break;
1617
1618 /*
1619 * Only variable length data types can be
1620 * variable sized. All others have fixed size.
1621 */
1622 case FR_TYPE_STRING:
1623 case FR_TYPE_OCTETS:
1624 /*
1625 * Zero-length strings are allowed for CUI
1626 * (thanks WiMAX!), and for
1627 * Message-Authenticator, because we will
1628 * automagically generate that one ourselves.
1629 */
1630 if ((vp->vp_length == 0) &&
1633 fr_dcursor_next(cursor);
1634 fr_strerror_const("Zero length string attributes not allowed");
1635 return 0;
1636 }
1637 break;
1638 }
1639
1640 /*
1641 * Nested structures of attributes can't be longer than
1642 * 255 bytes, so each call to an encode function can
1643 * only use 255 bytes of buffer space at a time.
1644 */
1645
1646 /*
1647 * Fast path for the common case.
1648 */
1649 if (vp->da->parent->flags.is_root && fr_radius_flag_encrypted(vp->da)) {
1650 switch (vp->vp_type) {
1651 case FR_TYPE_LEAF:
1652 da_stack.da[0] = vp->da;
1653 da_stack.da[1] = NULL;
1654 da_stack.depth = 1;
1655 FR_PROTO_STACK_PRINT(&da_stack, 0);
1656 slen = encode_rfc(&work_dbuff, &da_stack, 0, cursor, encode_ctx);
1657 if (slen < 0) return slen;
1658 return fr_dbuff_set(dbuff, &work_dbuff);
1659
1660 default:
1661 break;
1662 }
1663 }
1664
1665 /*
1666 * Do more work to set up the stack for the complex case.
1667 */
1668 fr_proto_da_stack_build(&da_stack, vp->da);
1669 FR_PROTO_STACK_PRINT(&da_stack, 0);
1670
1671 /*
1672 * Top-level attributes get treated specially. Things
1673 * like VSAs inside of extended attributes are handled
1674 * inside of type-specific encoders.
1675 */
1676 da = da_stack.da[0];
1677 switch (da->type) {
1678 case FR_TYPE_OCTETS:
1679 if (fr_radius_flag_concat(da)) {
1680 /*
1681 * Attributes like EAP-Message are marked as
1682 * "concat", which means that they are fragmented
1683 * using a different scheme than the "long
1684 * extended" one.
1685 */
1686 slen = encode_concat(&work_dbuff, &da_stack, 0, cursor, encode_ctx);
1687 if (slen < 0) return slen;
1688 break;
1689 }
1691
1692 default:
1693 slen = encode_rfc(&work_dbuff, &da_stack, 0, cursor, encode_ctx);
1694 if (slen < 0) return slen;
1695 break;
1696
1697 case FR_TYPE_VSA:
1698 slen = encode_vsa(&work_dbuff, &da_stack, 0, cursor, encode_ctx);
1699 if (slen < 0) return slen;
1700 break;
1701
1702 case FR_TYPE_TLV:
1703 if (!fr_radius_flag_extended(da)) {
1704 slen = encode_child(&work_dbuff, &da_stack, 0, cursor, encode_ctx);
1705
1706 } else if (vp->da != da) {
1707 fr_strerror_printf("extended attributes must be nested");
1709
1710 } else {
1711 slen = encode_extended_nested(&work_dbuff, &da_stack, 0, cursor, encode_ctx);
1712 }
1713 if (slen < 0) return slen;
1714 break;
1715
1716 case FR_TYPE_NULL:
1717 case FR_TYPE_VENDOR:
1718 case FR_TYPE_MAX:
1719 fr_strerror_printf("%s: Cannot encode attribute %s", __FUNCTION__, vp->da->name);
1721 }
1722
1723 /*
1724 * We didn't encode any data, continue.
1725 */
1726 if (!slen) {
1727 fr_assert(fr_dcursor_current(cursor) != vp);
1728 return 0;
1729 }
1730
1731 /*
1732 * We couldn't do it, so we didn't do anything.
1733 */
1734 if (fr_dcursor_current(cursor) == vp) {
1735 fr_strerror_printf("%s: Nested attribute structure too large to encode", __FUNCTION__);
1737 }
1738
1739 return fr_dbuff_set(dbuff, &work_dbuff);
1740}
1741
1743{
1744 fr_radius_ctx_t common_ctx = {};
1746 .common = &common_ctx,
1747 .foreign = true, /* we are being called from a foreign protocol */
1748 };
1749
1750 /*
1751 * Just in case we need random numbers.
1752 */
1753 encode_ctx.rand_ctx.a = fr_rand();
1754 encode_ctx.rand_ctx.b = fr_rand();
1755
1756 /*
1757 * Encode the pairs.
1758 */
1759 return encode_pairs(dbuff, list, &encode_ctx);
1760}
1761
1762
1763static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict,
1765{
1766 static uint8_t vector[RADIUS_AUTH_VECTOR_LENGTH] = {
1767 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1768 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
1769
1771 fr_radius_ctx_t *common;
1772
1773 test_ctx = talloc_zero(ctx, fr_radius_encode_ctx_t);
1774 if (!test_ctx) return -1;
1775
1776 test_ctx->common = common = talloc_zero(test_ctx, fr_radius_ctx_t);
1777
1778 common->secret = talloc_strdup(common, "testing123");
1779 common->secret_length = talloc_strlen(common->secret);
1780
1781 /*
1782 * We don't want to automatically add Message-Authenticator
1783 */
1784 common->secure_transport = true;
1785
1786 test_ctx->request_authenticator = vector;
1787 test_ctx->rand_ctx.a = 6809;
1788 test_ctx->rand_ctx.b = 2112;
1789
1790 *out = test_ctx;
1791
1792 return 0;
1793}
1794
1795static ssize_t fr_radius_encode_proto(TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, void *proto_ctx)
1796{
1797 fr_radius_encode_ctx_t *packet_ctx = talloc_get_type_abort(proto_ctx, fr_radius_encode_ctx_t);
1799 fr_pair_t *vp;
1800 ssize_t slen;
1801 uint8_t const *request_authenticator = NULL;
1802
1804 if (vp) {
1805 packet_type = vp->vp_uint32;
1806
1807 if (!FR_RADIUS_PACKET_CODE_VALID(packet_type)) {
1808 fr_strerror_printf("Invalid packet code %u", packet_type);
1809 return -1;
1810 }
1811 }
1812
1813 /*
1814 * If we find a Packet-Token, then it must be RADIUS/1.1.
1815 */
1817 if (vp && !packet_ctx->radius_1_1) {
1818 uint8_t *p;
1819
1820 packet_ctx->radius_1_1 = true;
1821
1822 MEM(packet_ctx->request_authenticator = p = talloc_zero_array(packet_ctx, uint8_t, 16));
1823 fr_nbo_from_uint32(p, vp->vp_uint32);
1824 packet_ctx->token = vp->vp_uint32;
1825
1826 } else {
1827 packet_ctx->radius_1_1 = false;
1828 }
1829
1830 /*
1831 * Force specific values for testing.
1832 */
1833 if ((packet_type == FR_RADIUS_CODE_ACCESS_REQUEST) || (packet_type == FR_RADIUS_CODE_STATUS_SERVER)) {
1835 if (!vp) {
1837 packet_ctx->request_authenticator, RADIUS_AUTH_VECTOR_LENGTH, false);
1838 }
1839 }
1840
1841 packet_ctx->code = packet_type;
1842 packet_ctx->request_code = allowed_replies[packet_type];
1843 packet_ctx->seen_message_authenticator = false;
1844 if (packet_ctx->request_code) request_authenticator = packet_ctx->request_authenticator;
1845
1846 /*
1847 * @todo - pass in packet_ctx to this function, so that we
1848 * can leverage a consistent random number generator.
1849 */
1850 slen = fr_radius_encode(&FR_DBUFF_TMP(data, data_len), vps, packet_ctx);
1851 if (slen <= 0) return slen;
1852
1853 /*
1854 * RADIUS/1.1 packets are not signed.
1855 */
1856 if (!packet_ctx->radius_1_1 &&
1857 (fr_radius_sign(data, request_authenticator,
1858 (uint8_t const *) packet_ctx->common->secret, talloc_strlen(packet_ctx->common->secret)) < 0)) {
1859 return -1;
1860 }
1861
1862 return slen;
1863}
1864
1865/*
1866 * No one else should be using this.
1867 */
1868extern void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *to_eval, void *uctx);
1869
1870/*
1871 * Test points
1872 */
1879
1880
ssize_t fr_radius_encode_abinary(fr_pair_t const *vp, fr_dbuff_t *dbuff)
Encode a string to abinary.
Definition abinary.c:1206
int n
Definition acutest.h:642
static int packet_ctx
the tests pass only the address of packet_ctx
#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_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition dbuff.h:1121
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:819
#define FR_DBUFF_ABS(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:239
#define FR_DBUFF_ADVANCE_EXTEND_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes (extending if necessary) returning if...
Definition dbuff.h:1203
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:963
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1056
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
#define fr_dbuff_set_to_start(_dbuff_or_marker)
Reset the 'current' position of the dbuff or marker to the 'start' of the buffer.
Definition dbuff.h:1208
#define fr_dbuff_out_memcpy(_out, _dbuff_or_marker, _outlen)
Copy exactly _outlen bytes from the dbuff.
Definition dbuff.h:1785
#define FR_DBUFF_BIND_CURRENT(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:248
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition dbuff.h:1561
#define fr_dbuff_in_bytes(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker.
Definition dbuff.h:1518
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition dbuff.h:1245
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1435
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1403
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1620
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.
Definition dbuff.h:1638
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition dbuff.h:334
#define FR_DBUFF_MAX_BIND_CURRENT(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition dbuff.h:351
#define fr_dbuff_out(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type.
Definition dbuff.h:1852
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1525
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define MEM(x)
Definition debug.h:38
@ FR_RADIUS_CODE_ACCESS_REQUEST
RFC2865 - Access-Request.
Definition defs.h:33
@ FR_RADIUS_CODE_MAX
Maximum possible protocol code.
Definition defs.h:53
@ FR_RADIUS_CODE_STATUS_SERVER
RFC2865/RFC5997 - Status Server (request)
Definition defs.h:44
@ FR_RADIUS_CODE_COA_REQUEST
RFC3575/RFC5176 - CoA-Request.
Definition defs.h:49
@ FR_RADIUS_CODE_ACCESS_ACCEPT
RFC2865 - Access-Accept.
Definition defs.h:34
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
#define FR_DICT_MAX_TLV_STACK
Maximum TLV stack size.
Definition dict.h:541
fr_dict_vendor_t const * fr_dict_vendor_by_da(fr_dict_attr_t const *da)
Look up a vendor by one of its child attributes.
Definition dict_util.c:2972
Private enterprise.
Definition dict.h:293
static uint32_t fr_dict_vendor_num_by_da(fr_dict_attr_t const *da)
Return the vendor number for an attribute.
Definition dict_ext.h:177
#define PAIR_ENCODE_FATAL_ERROR
Fatal encoding error.
Definition pair.h:36
static ssize_t encode_value(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encodes the data portion of an attribute.
Definition encode.c:276
static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:744
static ssize_t encode_rfc(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encode an RFC format attribute header.
Definition encode.c:597
ssize_t fr_pair_ref_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor)
Encode a foreign reference to the network.
Definition encode.c:117
#define ROUND_UP_DIV(_x, _y)
Get the ceiling value of integer division.
Definition math.h:214
#define ROUND_UP(_num, _mul)
Round up - Works in all cases, but is slower.
Definition math.h:209
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
unsigned short uint16_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ 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_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ 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
#define UINT8_MAX
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
static int encode(bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t id)
Definition bio.c:1923
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition nbo.h:243
static void fr_nbo_from_uint32(uint8_t out[static sizeof(uint32_t)], uint32_t num)
Write out an unsigned 32bit integer in wire format (big endian)
Definition nbo.h:61
#define RADIUS_AUTH_VECTOR_LENGTH
Definition net.h:89
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:716
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
void * fr_proto_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Implements the default iterator to encode pairs belonging to a specific dictionary that are not inter...
Definition proto.c:100
static fr_internal_encode_ctx_t encode_ctx
static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict, UNUSED fr_dict_attr_t const *root_da)
Definition encode.c:165
static ssize_t encode_child(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:361
HIDDEN fr_dict_attr_t const * attr_packet_authentication_vector
Definition base.c:55
HIDDEN fr_dict_attr_t const * attr_chargeable_user_identity
Definition base.c:59
HIDDEN fr_dict_attr_t const * attr_packet_token
Definition base.c:56
HIDDEN const fr_radius_packet_code_t allowed_replies[FR_RADIUS_CODE_MAX]
If we get a reply, the request must come from one of a small number of packet types.
Definition base.c:179
HIDDEN fr_dict_attr_t const * attr_nas_filter_rule
Definition base.c:64
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
int fr_radius_sign(uint8_t *packet, uint8_t const *vector, uint8_t const *secret, size_t secret_len)
Sign a previously encoded packet.
Definition base.c:365
ssize_t fr_radius_encode(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_radius_encode_ctx_t *packet_ctx)
Definition base.c:1047
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition base.c:119
static const bool allow_tunnel_passwords[FR_RADIUS_CODE_MAX]
Definition encode.c:37
static ssize_t encode_vsa(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encode a Vendor-Specific attribute.
Definition encode.c:1294
static ssize_t encode_wimax(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encode a WiMAX attribute.
Definition encode.c:1156
static ssize_t encode_pairs(fr_dbuff_t *dbuff, fr_pair_list_t const *vps, void *encode_ctx)
Definition encode.c:331
static ssize_t encode_tunnel_password(fr_dbuff_t *dbuff, fr_dbuff_marker_t *in, size_t inlen, fr_radius_encode_ctx_t *packet_ctx)
Definition encode.c:127
static ssize_t fr_radius_encode_proto(TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, void *proto_ctx)
Definition encode.c:1795
#define TAG_VALID(x)
Definition encode.c:35
static ssize_t encode_concat(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Encode an RFC format attribute, with the "concat" flag set.
Definition encode.c:941
static ssize_t encode_extended(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, NDEBUG_UNUSED unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encode an "extended" attribute.
Definition encode.c:777
static ssize_t encode_extended_nested(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:897
ssize_t fr_radius_encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a data structure into a RADIUS attribute.
Definition encode.c:1569
fr_test_point_pair_encode_t radius_tp_encode_pair
Definition encode.c:1874
fr_test_point_proto_encode_t radius_tp_encode_proto
Definition encode.c:1882
ssize_t fr_radius_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition encode.c:1742
void * fr_radius_next_encodable(fr_dcursor_t *cursor, void *to_eval, void *uctx)
Definition base.c:1030
static ssize_t attr_fragment(fr_dbuff_t *data, size_t data_len, fr_dbuff_marker_t *hdr, size_t hdr_len, size_t flag_offset, size_t vsa_offset)
Breaks down large data into pieces, each with a header.
Definition encode.c:680
static ssize_t encode_password(fr_dbuff_t *dbuff, fr_dbuff_marker_t *input, size_t inlen, fr_radius_encode_ctx_t *packet_ctx)
"encrypt" a password RADIUS style
Definition encode.c:56
static ssize_t encode_nas_filter_rule(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, NDEBUG_UNUSED unsigned int depth, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Encode NAS-Filter-Rule.
Definition encode.c:1357
static ssize_t encode_vendor(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Definition encode.c:1229
static ssize_t encode_vendor_attr(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encode one full Vendor-Specific + Vendor-ID + Vendor-Attr + Vendor-Length + ...
Definition encode.c:1023
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
static fr_dict_attr_t const * attr_message_authenticator
Definition radclient.c:128
bool secure_transport
for TLS
Definition radius.h:130
#define AUTH_PASS_LEN
Definition radius.h:53
@ RADIUS_FLAG_ENCRYPT_INVALID
Invalid encryption flag.
Definition radius.h:187
@ 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
#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
#define RADIUS_MESSAGE_AUTHENTICATOR_LENGTH
Definition radius.h:38
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
#define FR_RADIUS_PACKET_CODE_VALID(_x)
Definition radius.h:51
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
static fr_radius_bio_verify_t test_ctx(void)
Make a verify context which allows Access-Request and Access-Accept.
static fr_dict_t const * dict_radius
Definition radsniff.c:93
uint32_t fr_fast_rand(fr_fast_rand_t *ctx)
Definition rand.c:303
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:128
fr_pair_t * vp
ssize_t fr_struct_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *parent_cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:760
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:94
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:76
Entry point for pair encoders.
Definition test_point.h:93
Entry point for protocol encoders.
Definition test_point.h:75
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:585
static fr_pair_t * fr_pair_dcursor_child_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dcursor_t const *parent)
Initializes a child dcursor from a parent cursor, with an iteration function.
Definition pair.h:623
#define PAIR_VERIFY(_x)
Definition pair.h:209
#define fr_pair_list_append_by_da_len(_ctx, _vp, _list, _attr, _val, _len, _tainted)
Append a pair to a list, assigning its value.
Definition pair.h:333
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_STACK_PRINT(_stack, _depth)
Definition proto.h:44
uint8_t depth
Deepest attribute in the stack.
Definition proto.h:56
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:57
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:55
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_type_is_structural(_x)
Definition types.h:392
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_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
Encode a single value box, serializing its contents in generic network format.
Definition value.c:1534
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
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