The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
decode.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 35c2683f41c1af01136fbc29fccbd6a0864216d5 $
19 *
20 * @file protocols/der/decode.c
21 * @brief Functions to decode DER encoded data.
22 *
23 * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @author Ethan Thompson (ethan.thompson@inkbridge.io)
25 *
26 * @copyright 2025 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
27 * @copyright 2025 Network RADIUS SAS (legal@networkradius.com)
28 */
29
30#include <freeradius-devel/io/test_point.h>
31#include <freeradius-devel/util/dbuff.h>
32#include <freeradius-devel/util/decode.h>
33#include <freeradius-devel/util/dict.h>
34#include <freeradius-devel/util/proto.h>
35#include <freeradius-devel/util/struct.h>
36
37#include "attrs.h"
38#include "der.h"
39
40#define IS_DER_TAG_CONTINUATION(_tag) (((_tag) & DER_TAG_CONTINUATION) == DER_TAG_CONTINUATION)
41#define IS_DER_TAG_CONSTRUCTED(_tag) (((_tag) & 0x20) == 0x20)
42#define IS_DER_LEN_MULTI_BYTE(_len) (((_len) & DER_LEN_MULTI_BYTE) == DER_LEN_MULTI_BYTE)
43
44typedef ssize_t (*fr_der_decode_oid_t)(uint64_t subidentifier, void *uctx, bool is_last);
45
46static ssize_t fr_der_decode_oid(fr_dbuff_t *in, fr_der_decode_oid_t func, void *uctx) CC_HINT(nonnull);
47
48static ssize_t fr_der_decode_hdr(fr_dict_attr_t const *parent, fr_dbuff_t *in, uint8_t *tag, size_t *len,
49 fr_der_tag_t expected) CC_HINT(nonnull(2,3,4));
50
52 fr_der_decode_ctx_t *decode_ctx);
53
58
59/** Function signature for DER decode functions
60 *
61 * @param[in] ctx Allocation context
62 * @param[in] out Where to store the decoded pairs.
63 * @param[in] parent Parent attribute. This should be the root of the dictionary
64 * we're using to decode DER data initially, and then nested children.
65 * @param[in] in The DER encoded data.
66 * @param[in] allowed_chars Optional array indicating which ASCII characters are allowed.
67 * @param[in] decode_ctx Any decode specific data.
68 * @return
69 * - > 0 on success. How many bytes were decoded.
70 * - 0 no bytes decoded.
71 * - < 0 on error. May be the offset (as a negative value) where the error occurred.
72 */
74 bool const allowed_chars[], fr_der_decode_ctx_t *decode_ctx) CC_HINT(nonnull(1,2,3,4,6));
75
77 UNUSED fr_der_decode_ctx_t *decode_ctx)
78{
80 fr_dbuff_t our_in = FR_DBUFF(in);
81 uint8_t value = 0;
82
83 size_t len = fr_dbuff_remaining(&our_in);
84
86
87 /*
88 * ISO/IEC 8825-1:2021
89 * 8.2 Encoding of a boolean value
90 * 8.2.1 The encoding of a boolean value shall be primitive.
91 * The contents octets shall consist of a single octet.
92 * 8.2.2 If the boolean value is:
93 * FALSE the octet shall be zero [0x00].
94 * If the boolean value is TRUE the octet shall have any non-zero value, as a sender's option.
95 *
96 * 11.1 Boolean values
97 * If the encoding represents the boolean value TRUE, its single contents octet shall have all
98 * eight bits set to one [0xff]. (Contrast with 8.2.2.)
99 */
100 if (len != 1) {
101 fr_strerror_printf_push("Boolean has incorrect length (%zu). Must be 1.", len);
102 return -1;
103 }
104
105 FR_DBUFF_OUT_RETURN(&value, &our_in);
106
108 fr_strerror_printf_push("Boolean is not correctly DER encoded (0x%02" PRIx32 " or 0x%02" PRIx32 ").", DER_BOOLEAN_FALSE,
110 return -1;
111 }
112
113 vp = fr_pair_afrom_da(ctx, parent);
114 if (unlikely(vp == NULL)) {
115 fr_strerror_const_push("Out of memory");
116 return -1;
117 }
118
119 vp->vp_bool = value > 0;
120
122
123 return fr_dbuff_set(in, &our_in);
124}
125
127 UNUSED fr_der_decode_ctx_t *decode_ctx)
128{
129 fr_pair_t *vp;
130 fr_dbuff_t our_in = FR_DBUFF(in);
131 uint64_t value = 0;
132 uint8_t sign = 0;
133 size_t i;
134
135 size_t len = fr_dbuff_remaining(&our_in);
136
137 if (parent->type != FR_TYPE_INT64) {
138 fr_strerror_printf_push("Expected parent type 'int64', got attribute %s of type %s", parent->name,
139 fr_type_to_str(parent->type));
140 return -1;
141 }
142
143 if (len > sizeof(value)) {
144 fr_strerror_printf_push("Integer too large (%zu)", len);
145 return -1;
146 }
147
148 /*
149 * ISO/IEC 8825-1:2021
150 * 8.3 Encoding of an integer value
151 * 8.3.1 The encoding of an integer value shall be primitive.
152 * The contents octets shall consist of one or more octets.
153 * 8.3.2 If the contents octets of an integer value encoding consist of more than one octet,
154 * then the bits of the first octet and bit 8 of the second octet:
155 * a) shall not all be ones; and
156 * b) shall not all be zero.
157 * NOTE - These rules ensure that an integer value is always encoded in the smallest possible number
158 * of octets. 8.3.3 The contents octets shall be a two's complement binary number equal to the
159 * integer value, and consisting of bits 8 to 1 of the first octet, followed by bits 8 to 1 of the
160 * second octet, followed by bits 8 to 1 of each octet in turn up to and including the last octet of
161 * the contents octets.
162 */
163 FR_DBUFF_OUT_RETURN(&sign, &our_in);
164
165 if (sign & 0x80) {
166 /*
167 * If the sign bit is set, this fill the upper bits with all zeros,
168 * and set the lower bits to "sign".
169 * This is important for the case where the length of the integer is less than the length of the
170 * integer type.
171 */
172 value = ~(uint64_t) 0xff;
173 }
174
175 value |= sign;
176
177 if (len > 1) {
178 /*
179 * If the length of the integer is greater than 1, we need to check that the first 9 bits:
180 * 1. are not all 0s; and
181 * 2. are not all 1s
182 * These two conditions are necessary to ensure that the integer conforms to DER.
183 */
184 uint8_t byte;
185
186 FR_DBUFF_OUT_RETURN(&byte, &our_in);
187
188 if ((((value & 0xff) == 0xff) && (byte & 0x80)) || (((~value & 0xff) == 0xff) && !(byte & 0x80))) {
189 fr_strerror_const_push("Integer is not correctly DER encoded. First two bytes are all 0s or all 1s.");
190 return -1;
191 }
192
193 value = (value << 8) | byte;
194 }
195
196 for (i = 2; i < len; i++) {
197 uint8_t byte;
198
199 FR_DBUFF_OUT_RETURN(&byte, &our_in);
200 value = (value << 8) | byte;
201 }
202
203 vp = fr_pair_afrom_da(ctx, parent);
204 if (unlikely(vp == NULL)) {
205 fr_strerror_const_push("Out of memory");
206 return -1;
207 }
208
209 vp->vp_int64 = value;
210
212
213 return fr_dbuff_set(in, &our_in);
214}
215
217 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
218{
219 fr_pair_t *vp;
220 fr_dbuff_t our_in = FR_DBUFF(in);
221 uint8_t unused_bits = 0;
222 uint8_t *data;
223
224 ssize_t data_len = 0, index = 0;
225 size_t len = fr_dbuff_remaining(&our_in);
226
228
229 /*
230 * Now we know that the parent is an octets attribute, we can decode the bitstring
231 */
232
233 /*
234 * ISO/IEC 8825-1:2021
235 * 8.6 Encoding of a bitstring value
236 * 8.6.1 The encoding of a bitstring value shall be either primitive or constructed at the option
237 * of the sender.
238 * NOTE - Where it is necessary to transfer part of a bit string before the entire
239 * bitstring is available, the constructed encoding is used.
240 * 8.6.2 The contents octets for the primitive encoding shall contain an initial octet followed
241 * by zero, one or more subsequent octets.
242 * 8.6.2.1 The bits in the bitstring value, commencing with the leading bit and proceeding
243 * to the trailing bit, shall be placed in bits 8 to 1 of the first subsequent
244 * octet, followed by bits 8 to 1 of the second subsequent octet, followed by bits
245 * 8 to 1 of each octet in turn, followed by as many bits as are needed of the
246 * final subsequent octet, commencing with bit 8.
247 * NOTE - The terms "leading bit" and "trailing bit" are defined in
248 * Rec. ITU-T X.680 | ISO/IEC 8824-1, 22.2.
249 * 8.6.2.2 The initial octet shall encode, as an unsigned binary integer with bit 1 as the
250 * least significant bit, the number of unused bits in the final subsequent octet.
251 * The number shall be in the range zero to seven.
252 * 8.6.2.3 If the bitstring is empty, there shall be no subsequent octets, and the initial
253 * octet shall be zero.
254 *
255 * 10.2 String encoding forms
256 * For bitstring, octetstring and restricted character string types, the constructed form of
257 * encoding shall not be used. (Contrast with 8.23.6.)
258 *
259 * 11.2 Unused bits 11.2.1 Each unused bit in the final octet of the encoding of a bit string value shall
260 * be set to zero.
261 */
262
263 FR_DBUFF_OUT_RETURN(&unused_bits, &our_in);
264
265 if (unlikely(unused_bits > 7)) {
266 /*
267 * This means an entire byte is unused bits. Which is not allowed.
268 */
269 fr_strerror_const_push("Invalid number of unused bits in 'bitstring'");
270 return -1;
271 }
272
273 if ((len == 1) && unused_bits) {
274 fr_strerror_const_push("Insufficient data for 'bitstring'. Missing data bytes");
275 return -1;
276 }
277
278 if (fr_type_is_struct(parent->type)) {
279 if (!len) {
280 fr_strerror_const_push("Insufficient data for 'struct'. Missing data bytes");
281 return -1;
282 }
283
284 /*
285 * If the parent is a struct attribute, we will not be adding the unused bits count to the first
286 * byte
287 */
288 data_len = len - 1;
289 } else {
290 data_len = len;
291 }
292
293 data = talloc_array(decode_ctx->tmp_ctx, uint8_t, data_len);
294 if (unlikely(!data)) {
295 fr_strerror_const_push("Out of memory");
296 return -1;
297 }
298
299 if (fr_type_is_octets(parent->type)) {
300 /*
301 * If the parent is an octets attribute, we need to add the unused bits count to the first byte
302 */
303 index = 1;
304 data[0] = unused_bits;
305 }
306
307 for (; index < data_len; index++) {
308 uint8_t byte;
309
310 FR_DBUFF_OUT_RETURN(&byte, &our_in);
311
312 data[index] = byte;
313 }
314
315 /*
316 * Remove the unused bits from the last byte
317 */
318 if (unused_bits) {
319 uint8_t mask = 0xff << unused_bits;
320
321 data[data_len - 1] &= mask;
322 }
323
324 if (fr_type_is_struct(parent->type)) {
325 ssize_t slen;
326
327 slen = fr_struct_from_network(ctx, out, parent, data, data_len, decode_ctx, NULL, NULL);
328
329 /*
330 * If the structure decoder didn't consume all the data, we need to free the data and bail out
331 */
332 if (unlikely(slen < data_len - 1)) {
333 fr_strerror_printf_push("Bitstring structure decoder didn't consume all data. Consumed %zd of %zu bytes",
334 slen, data_len);
335 error:
337 return -1;
338 }
339
341 return fr_dbuff_set(in, &our_in);
342 }
343
344 vp = fr_pair_afrom_da(ctx, parent);
345 if (unlikely(!vp)) {
346 fr_strerror_const_push("Out of memory");
347 goto error;
348 }
349
350 /*
351 * Add the bitstring to the pair value as octets
352 */
353 fr_pair_value_memdup(vp, data, len, false);
354
356
357 return fr_dbuff_set(in, &our_in);
358}
359
362{
363 fr_pair_t *vp;
364 fr_dbuff_t our_in = FR_DBUFF(in);
365 uint8_t *data = NULL;
366
367 size_t len = fr_dbuff_remaining(&our_in);
368
370
371 /*
372 * ISO/IEC 8825-1:2021
373 * 8.7 Encoding of an octetstring value
374 * 8.7.1 The encoding of an octetstring value shall be either primitive or constructed at the
375 * option of the sender.
376 * NOTE - Where it is necessary to transfer part of an octet string before the entire
377 * octetstring is available, the constructed encoding is used.
378 * 8.7.2 The primitive encoding contains zero, one or more contents octets equal in value to the
379 * octets in the data value, in the order they appear in the data value, and with the most
380 * significant bit of an octet of the data value aligned with the most significant bit of an
381 * octet of the contents octets.
382 * 8.7.3 The contents octets for the constructed encoding shall consist of zero, one, or more
383 * encodings.
384 * NOTE - Each such encoding includes identifier, length, and contents octets, and may
385 * include end-of-contents octets if it is constructed.
386 * 8.7.3.1 To encode an octetstring value in this way, it is segmented. Each segment shall
387 * consist of a series of consecutive octets of the value. There shall be no
388 * significance placed on the segment boundaries.
389 * NOTE - A segment may be of size zero, i.e. contain no octets.
390 *
391 * 10.2 String encoding forms
392 * For bitstring, octetstring and restricted character string types, the constructed form of
393 * encoding shall not be used. (Contrast with 8.23.6.)
394 */
395
396 vp = fr_pair_afrom_da(ctx, parent);
397 if (unlikely(!vp)) {
398 oom:
399 fr_strerror_const_push("Out of memory");
400 return -1;
401 }
402
403 if (unlikely(fr_pair_value_mem_alloc(vp, &data, len, false) < 0)) {
405 goto oom;
406 }
407
408 (void) fr_dbuff_out_memcpy(data, &our_in, len); /* this can never fail */
409
411
412 return fr_dbuff_set(in, &our_in);
413}
414
416 UNUSED fr_der_decode_ctx_t *decode_ctx)
417{
418 fr_pair_t *vp;
419 fr_dbuff_t our_in = FR_DBUFF(in);
420
421 if (fr_dbuff_remaining(&our_in) != 0) {
422 fr_strerror_const_push("Null has non-zero length");
423 return -1;
424 }
425
426 /*
427 * ISO/IEC 8825-1:2021
428 * 8.8 Encoding of a null value 8.8.1 The encoding of a null value shall be primitive. 8.8.2 The contents
429 * octets shall not contain any octets. NOTE - The length octet is zero.
430 */
431
432 vp = fr_pair_afrom_da(ctx, parent);
433 if (unlikely(!vp)) {
434 fr_strerror_const_push("Out of memory");
435 return -1;
436 }
437
439
440 return fr_dbuff_set(in, &our_in);
441}
442
443typedef struct {
444 int depth;
445 unsigned int oid[FR_DICT_MAX_TLV_STACK];
446} fr_der_decode_oid_to_stack_ctx_t; //!< Context for decoding an OID to a DA
447
448/** Decode an OID to an exploded list
449 *
450 * @param[in] subidentifier The subidentifier to decode
451 * @param[in] uctx User context
452 * @param[in] is_last Is this the last subidentifier in the OID
453 * @return
454 * - 1 on success
455 * - < 0 on error
456 */
457static ssize_t fr_der_decode_oid_to_stack(uint64_t subidentifier, void *uctx, UNUSED bool is_last)
458{
459 fr_der_decode_oid_to_stack_ctx_t *decode_ctx = uctx;
460
461 if (decode_ctx->depth > 20) {
462 fr_strerror_printf("OID has too many elements (%d > 20)", decode_ctx->depth);
463 return -1;
464 }
465
466
467 decode_ctx->oid[decode_ctx->depth++] = subidentifier;
468
469 return 1;
470}
471
472typedef struct {
473 TALLOC_CTX *ctx; //!< Allocation context
474 fr_dict_attr_t const *parent_da; //!< Parent dictionary attribute
475 fr_pair_list_t *parent_list; //!< Parent pair list
476} fr_der_decode_oid_to_da_ctx_t; //!< Context for decoding an OID to a dictionary attribute
477
478/** Decode an OID to a dictionary attribute
479 *
480 * @param[in] subidentifier The subidentifier to decode
481 * @param[in] uctx User context
482 * @param[in] is_last Is this the last subidentifier in the OID
483 * @return
484 * - 1 on success
485 * - < 0 on error
486 */
487static ssize_t fr_der_decode_oid_to_da(uint64_t subidentifier, void *uctx, bool is_last)
488{
489 fr_der_decode_oid_to_da_ctx_t *decode_ctx = uctx;
490 fr_pair_t *vp;
491 fr_dict_attr_t const *da;
492
493 fr_dict_attr_t const *parent_da = fr_type_is_group(decode_ctx->parent_da->type) ?
494 fr_dict_attr_ref(decode_ctx->parent_da) :
495 decode_ctx->parent_da;
496
497 FR_PROTO_TRACE("Decoding OID to dictionary attribute");
498 FR_PROTO_TRACE("decode context - Parent Name: %s Sub-Identifier %" PRIu64, parent_da->name, subidentifier);
499 FR_PROTO_TRACE("decode context - Parent Address: %p", parent_da);
500
501 da = fr_dict_attr_child_by_num(parent_da, subidentifier);
502
503 if (is_last) {
504 if (unlikely(da == NULL)) {
505 decode_ctx->parent_da = fr_dict_attr_unknown_typed_afrom_num(decode_ctx->ctx, parent_da,
506 subidentifier, FR_TYPE_OCTETS);
507
508 if (unlikely(decode_ctx->parent_da == NULL)) {
509 return -1;
510 }
511
512 FR_PROTO_TRACE("Created DA: %s", decode_ctx->parent_da->name);
513 return 1;
514 }
515
516 decode_ctx->parent_da = da;
517
518 FR_PROTO_TRACE("Created DA: %s", decode_ctx->parent_da->name);
519 return 1;
520 }
521
522 if (unlikely(da == NULL)) {
523 /*
524 * We need to create an unknown attribute for this subidentifier so we can store the raw data
525 */
526 fr_dict_attr_t *unknown_da =
527 fr_dict_attr_unknown_typed_afrom_num(decode_ctx->ctx, parent_da, subidentifier, FR_TYPE_TLV);
528
529 if (unlikely(unknown_da == NULL)) {
530 oom:
531 fr_strerror_const_push("Out of memory");
532 return -1;
533 }
534
535 vp = fr_pair_afrom_da(decode_ctx->ctx, unknown_da);
536
537 talloc_free(unknown_da);
538 } else {
539 vp = fr_pair_afrom_da(decode_ctx->ctx, da);
540 }
541
542 if (unlikely(!vp)) goto oom;
543
544 fr_pair_append(decode_ctx->parent_list, vp);
545
546 decode_ctx->ctx = vp;
547 decode_ctx->parent_da = vp->da;
548 decode_ctx->parent_list = &vp->vp_group;
549
550 FR_PROTO_TRACE("Created DA: %s", decode_ctx->parent_da->name);
551 return 1;
552}
553
554/** Decode an OID from a DER encoded buffer using a callback
555 *
556 * @param[in] in The DER encoded data.
557 * @param[in] func The callback function to call for each subidentifier.
558 * @param[in] uctx User context for the callback function.
559 * @return
560 * - 0 on success
561 * - < 0 on error
562 */
564{
565 fr_dbuff_t our_in = FR_DBUFF(in);
566 bool first;
567 uint64_t oid;
568 int magnitude, depth;
569 size_t len = fr_dbuff_remaining(&our_in); /* we decode the entire dbuff */
570
571 /*
572 * ISO/IEC 8825-1:2021
573 * 8.19 Encoding of an object identifier value
574 * 8.19.1 The encoding of an object identifier value shall be primitive.
575 * 8.19.2 The contents octets shall be an (ordered) list of encodings of subidentifiers (see 8.19.3
576 * and 8.19.4) concatenated together. Each subidentifier is represented as a series of
577 * (one or more) octets. Bit 8 of each octet indicates whether it is the last in the series: bit 8
578 * of the last octet is zero; bit 8 of each preceding octet is one. Bits 7 to 1 of the octets in
579 * the series collectively encode the subidentifier. Conceptually, these groups of bits are
580 * concatenated to form an unsigned binary number whose most significant bit is bit 7 of the first
581 * octet and whose least significant bit is bit 1 of the last octet. The subidentifier shall be
582 * encoded in the fewest possible octets, that is, the leading octet of the subidentifier shall not
583 * have the value 8016.
584 * 8.19.3 The number of subidentifiers (N) shall be one less than the number of object identifier
585 * components in the object identifier value being encoded. 8.19.4 The numerical value of the
586 * first subidentifier is derived from the values of the first two object identifier components in
587 * the object identifier value being encoded, using the formula: (X*40) + Y where X is the value
588 * of the first object identifier component and Y is the value of the second object identifier
589 * component. NOTE - This packing of the first two object identifier components recognizes that
590 * only three values are allocated from the root node, and at most 39 subsequent values from nodes
591 * reached by X = 0 and X = 1. 8.19.5 The numerical value of the ith subidentifier, (2 <= i <= N) is
592 * that of the (i + 1)th object identifier component.
593 */
594
595 /*
596 * RFC 5280 says:
597 *
598 * ...
599 * This specification mandates support for OIDs that have arc elements
600 * with values that are less than 2^28, that is, they MUST be between 0
601 * and 268,435,455, inclusive. This allows each arc element to be
602 * represented within a single 32-bit word. Implementations MUST also
603 * support OIDs where the length of the dotted decimal (see Section 1.4
604 * of [RFC4512]) string representation can be up to 100 bytes
605 * (inclusive). Implementations MUST be able to handle OIDs with up to
606 * 20 elements (inclusive).
607 * ...
608 *
609 * We support up to 2^32 for attribute numbers (unsigned int), and 24 for
610 * nesting (FR_DICT_TLV_NEST_MAX), so we're OK here.
611 *
612 */
613 FR_PROTO_TRACE("Decoding OID");
614 FR_PROTO_HEX_DUMP(fr_dbuff_current(&our_in), len, "buff in OID");
615
616 first = true;
617 oid = 0;
618 magnitude = 0;
619 depth = 0;
620
621 /*
622 * Loop until done.
623 */
624 while (len) {
625 uint8_t byte;
626
627 FR_DBUFF_OUT_RETURN(&byte, &our_in);
628
629 magnitude++;
630 if (magnitude > 4) {
631 fr_strerror_const_push("OID subidentifier too large (>32 bits)");
632 return -1;
633 }
634
635 /*
636 * Shift in the new data.
637 */
638 oid <<= 7;
639 oid |= byte & 0x7f;
640 len--;
641
642 /*
643 * There's more? The MUST be more if the high bit is set.
644 */
645 if ((byte & 0x80) != 0) {
646 if (len == 0) {
647 fr_strerror_const_push("OID subidentifier is truncated");
648 return -1;
649 }
650 continue;
651 }
652
653 depth++;
655 fr_strerror_printf_push("OID has too many elements (%d >= %d)",
657 return -1;
658 }
659
660 /*
661 * The initial packed field has the first two compenents included, as (x * 40) + y.
662 */
663 if (first) {
664 uint64_t first_component;
665
666 if (oid < 40) {
667 first_component = 0;
668
669 } else if (oid < 80) {
670 first_component = 1;
671 oid -= 40;
672
673 } else {
674 first_component = 2;
675 oid -= 80;
676 }
677 first = false;
678 depth++; /* 2 OIDs packed into the first byte */
679
680 /*
681 * Note that we allow OID=1 here. It doesn't make sense, but whatever.
682 */
683 FR_PROTO_TRACE("decode context - first OID: %" PRIu64, first_component);
684 if (unlikely(func(first_component, uctx, (len == 0)) <= 0)) return -1;
685 }
686
687 /*
688 * 32 bits is still larger than 28, so we do another check here.
689 */
690 if (oid >= ((uint64_t) 1 << 28)) {
691 fr_strerror_printf("OID subidentifier '%" PRIu64 " is invalid - it must be no more than 28 bits in side",
692 oid);
693 return -1;
694 }
695
696 FR_PROTO_TRACE("decode context - OID: %" PRIu64, oid);
697 if (unlikely(func(oid, uctx, (len == 0)) <= 0)) return -1;
698
699 /*
700 * Reset fields.
701 */
702 oid = 0;
703 magnitude = 0;
704 }
705
706 return fr_dbuff_set(in, &our_in);
707}
708
709
711 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
712{
713 /*
714 * @todo - check for valid UTF8 string.
715 */
716
717 return fr_der_decode_string(ctx, out, parent, in, NULL, decode_ctx);
718}
719
721 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
722{
723 fr_pair_t *vp;
724 fr_dict_attr_t const *child = NULL;
725 fr_dbuff_t our_in = FR_DBUFF(in);
727
729
730 /*
731 * ISO/IEC 8825-1:2021
732 * 8.9 Encoding of a sequence value
733 * 8.9.1 The encoding of a sequence value shall be constructed.
734 * 8.9.2 The contents octets shall consist of the complete encoding of one data value from each of
735 * the types listed in the ASN.1 definition of the sequence type, in the order of their
736 * appearance in the definition, unless the type was referenced with the keyword OPTIONAL
737 * or the keyword DEFAULT.
738 * 8.9.3 The encoding of a data value may, but need not, be present for a type referenced with the
739 * keyword OPTIONAL or the keyword DEFAULT. If present, it shall appear in the order of
740 * appearance of the corresponding type in the ASN.1 definition.
741 *
742 * 11.5 Set and sequence components with default value
743 * The encoding of a set value or sequence value shall not include an encoding for any component
744 * value which is equal to its default value.
745 */
746
747 if (flags->min && !fr_dbuff_remaining(&our_in)) {
748 fr_strerror_printf_push("Expected at last %d elements in %s, got 0", flags->min, parent->name);
749 return -1;
750 }
751
752 vp = fr_pair_afrom_da(ctx, parent);
753 if (unlikely(!vp)) {
754 fr_strerror_const_push("Out of memory");
755 return -1;
756 }
757
758 /*
759 * This is a sequence-of, which means it either has only one child, or it's a sequence_of=choice,
760 * and all of the children are numbered options.
761 */
762 if (unlikely(flags->is_sequence_of)) {
763 if (flags->sequence_of != FR_DER_TAG_CHOICE) {
764 child = fr_dict_attr_iterate_children(parent, &child);
765 if (!child) {
766 fr_strerror_printf_push("Sequence %s has no children", parent->name);
767 error:
769 return -1;
770 }
771 }
772
773 /*
774 * Decode all of the data.
775 */
776 while (fr_dbuff_remaining(&our_in) > 0) {
777 ssize_t slen;
778 uint8_t current_tag;
779 uint8_t tag_byte;
780 uint8_t *current_marker = fr_dbuff_current(&our_in);
781
782 FR_DBUFF_OUT_RETURN(&tag_byte, &our_in);
783
784 current_tag = (tag_byte & DER_TAG_CONTINUATION); /* always <= FR_DER_TAG_MAX */
785
786 /*
787 * If we have a choice, the children must be numbered. The class can be CONTEXT,
788 * PRIVATE, or ENTERPRISE.
789 *
790 * Otherwise the children are standard DER tags. The class must be UNIVERSAL.
791 */
792 if (unlikely(flags->sequence_of == FR_DER_TAG_CHOICE)) {
793 if ((tag_byte & DER_TAG_CLASS_MASK) == FR_DER_CLASS_UNIVERSAL) {
794 unexpected_class:
795 fr_strerror_printf_push("Tag has unexpected class %02x", tag_byte & DER_TAG_CLASS_MASK);
796 goto error;
797 }
798
799 child = fr_dict_attr_child_by_num(parent, current_tag);
800 if (!child) {
801 fr_der_attr_flags_t *child_flags;
802
803 child = fr_dict_attr_unknown_raw_afrom_num(decode_ctx->tmp_ctx, parent, current_tag);
804 if (!child) goto error;
805
806 /*
807 * Save the option and class, so that we can encode it later.
808 */
810 child_flags->is_option = true;
811 child_flags->option = current_tag;
812 child_flags->class = tag_byte & DER_TAG_CLASS_MASK;
813 }
814
815 } else if (unlikely(current_tag != flags->sequence_of)) {
816 if ((tag_byte & DER_TAG_CLASS_MASK) != FR_DER_CLASS_UNIVERSAL) {
817 goto unexpected_class;
818 }
819
820 fr_strerror_printf_push("Attribute %s is a sequence_of=%s which does not allow DER type '%s'",
821 parent->name,
822 fr_der_tag_to_str(flags->sequence_of),
823 fr_der_tag_to_str(current_tag));
824 goto error;
825 }
826
827 FR_PROTO_TRACE("decode context %s -> %s", parent->name, child->name);
828
829 fr_dbuff_set(&our_in, current_marker);
830
831 /*
832 * A child could have been encoded with zero bytes if it has a default value.
833 */
834 slen = fr_der_decode_pair_dbuff(vp, &vp->vp_group, child, &our_in, decode_ctx);
835 if (unlikely(slen < 0)) {
836 fr_strerror_printf_push("Failed decoding %s", vp->da->name);
837 goto error;
838 }
839 }
840
842
843 return fr_dbuff_set(in, &our_in);
844 }
845
846 /*
847 * Decode the children. Since it's not a sequence_of=..., we must have a random bunch of
848 * children. The children are packed in order. Some may be optional.
849 *
850 * We loop over all of the children, because some might have default values.
851 */
852 while ((child = fr_dict_attr_iterate_children(parent, &child))) {
853 ssize_t ret;
854
855 FR_PROTO_TRACE("decode context %s -> %s", parent->name, child->name);
856
857 ret = fr_der_decode_pair_dbuff(vp, &vp->vp_group, child, &our_in, decode_ctx);
858 if (unlikely(ret < 0)) {
859 fr_strerror_printf_push("Failed decoding %s", vp->da->name);
861 return ret;
862 }
863 }
864
865 /*
866 * Ensure that we grab all of the data.
867 *
868 * @todo - if there is data left over, decode it as raw octets. We then also have to keep track
869 * of the maximum child number, and create unknown attributes starting from the last one.
870 */
871 if (fr_dbuff_remaining(&our_in)) {
872 FR_PROTO_TRACE("Ignoring extra data in sequence");
874
875 (void) fr_dbuff_advance(&our_in, fr_dbuff_remaining(&our_in));
876 }
877
879
880 return fr_dbuff_set(in, &our_in);
881}
882
884 fr_der_decode_ctx_t *decode_ctx)
885{
886 fr_pair_t *vp;
887 fr_dict_attr_t const *child = NULL;
888 fr_dbuff_t our_in = FR_DBUFF(in);
889 fr_dbuff_marker_t previous_marker;
890 uint8_t previous_tag = 0x00;
891 size_t previous_len = 0;
893
895
896 /*
897 * ISO/IEC 8825-1:2021
898 * 8.11 Encoding of a set value
899 * 8.11.1 The encoding of a set value shall be constructed.
900 * 8.11.2 The contents octets shall consist of the complete encoding of one data value from each
901 * of the types listed in the ASN.1 definition of the set type, in an order chosen by the
902 * sender, unless the type was referenced with the keyword OPTIONAL or the keyword DEFAULT.
903 * 8.11.3 The encoding of a data value may, but need not, be present for a type referenced with the
904 * keyword OPTIONAL or the keyword DEFAULT.
905 *
906 * 11.5 Set and sequence components with default value
907 * The encoding of a set value or sequence value shall not include an encoding for any component
908 * value which is equal to its default value.
909 */
910
911 if (flags->min && !fr_dbuff_remaining(&our_in)) {
912 fr_strerror_printf_push("Expected at last %d elements in %s, got 0", flags->min, parent->name);
913 return -1;
914 }
915
916 vp = fr_pair_afrom_da(ctx, parent);
917 if (unlikely(!vp)) {
918 fr_strerror_const_push("Out of memory");
919 return -1;
920 }
921
922 if (flags->is_set_of) {
923 fr_dbuff_marker_t current_value_marker;
924
925 /*
926 * There should only be one child in a "set_of". We can't check this when we load
927 * the dictionaries, because there is no "finalize" callback.
928 *
929 * @todo - we would need to walk through all of the dictionary attributes, and
930 * call a new function which would check whether or not the parent had any
931 * children. And if not, return a load-time error.
932 */
933 child = NULL;
934 child = fr_dict_attr_iterate_children(parent, &child);
935 if (!child) {
936 fr_strerror_printf_push("Missing child for %s", parent->name);
937 return -1;
938 }
939
940 fr_dbuff_marker(&current_value_marker, &our_in);
941
942 while (fr_dbuff_remaining(&our_in) > 0) {
943 ssize_t ret;
944 uint8_t current_tag;
945 uint8_t *current_marker = fr_dbuff_current(&our_in);
946 size_t len;
947
948 FR_PROTO_TRACE("decode context %s -> %s", parent->name, child->name);
949
950 if (unlikely(fr_der_decode_hdr(NULL, &our_in, &current_tag, &len, flags->set_of) <= 0)) {
951 ret = -1;
952 error:
954 fr_strerror_printf_push("Failed decoding %s", parent->name);
955 return ret;
956 }
957
958 fr_dbuff_set(&current_value_marker, &our_in);
959
960 /*
961 * Ensure that the contents of the tags are sorted.
962 */
963 if (previous_tag) {
964 uint8_t prev_byte = 0, curr_byte = 0;
965 fr_dbuff_t previous_item = FR_DBUFF(&previous_marker);
966
967 fr_dbuff_set_end(&previous_item, fr_dbuff_current(&previous_marker) + previous_len);
968
969 do {
970 FR_DBUFF_OUT_RETURN(&prev_byte, &previous_item);
971 FR_DBUFF_OUT_RETURN(&curr_byte, &our_in);
972
973 if (prev_byte > curr_byte) {
974 fr_strerror_const_push("Set tags are not in ascending order");
975 ret = -1;
976 goto error;
977 }
978
979 if (prev_byte < curr_byte) {
980 break;
981 }
982
983 } while (fr_dbuff_remaining(&our_in) > 0 && fr_dbuff_remaining(&previous_item) > 0);
984
985 if (prev_byte > curr_byte && fr_dbuff_remaining(&previous_item) > 0) {
987 "Set tags are not in ascending order. Previous item has more data");
988 ret = -1;
989 goto error;
990 }
991 }
992
993 previous_tag = current_tag;
994 previous_len = len;
995
996 previous_marker = current_value_marker;
997
998 fr_dbuff_set(&our_in, current_marker);
999
1000 ret = fr_der_decode_pair_dbuff(vp, &vp->vp_group, child, &our_in, decode_ctx);
1001 if (unlikely(ret <= 0)) {
1002 fr_strerror_printf_push("Failed decoding %s", vp->da->name);
1003 goto error;
1004 }
1005 }
1006
1008
1009 return fr_dbuff_set(in, &our_in);
1010 }
1011
1012 /*
1013 * Decode the children. Since it's not a sequence_of=..., we must have a set of children. The
1014 * children are packed in order. Some may be optional.
1015 */
1016 while ((child = fr_dict_attr_iterate_children(parent, &child))) {
1017 ssize_t ret;
1018 uint8_t current_tag;
1019
1020 FR_PROTO_TRACE("decode context %s -> %s", parent->name, child->name);
1021
1022 if (fr_dbuff_remaining(&our_in)) {
1023 uint8_t *current_ptr = fr_dbuff_current(&our_in);
1024
1025 /*
1026 * Check that the tag is in ascending order
1027 */
1028 FR_DBUFF_OUT_RETURN(&current_tag, &our_in);
1029
1030 if (unlikely(current_tag < previous_tag)) {
1031 fr_strerror_const_push("Set tags are not in ascending order");
1032 talloc_free(vp);
1033 return -1;
1034 }
1035
1036 previous_tag = current_tag;
1037
1038 /*
1039 * Reset the buffer to the start of the tag
1040 */
1041 fr_dbuff_set(&our_in, current_ptr);
1042 }
1043
1044 /*
1045 * A child could have been encoded with zero bytes if it has a default value.
1046 */
1047 ret = fr_der_decode_pair_dbuff(vp, &vp->vp_group, child, &our_in, decode_ctx);
1048 if (unlikely(ret < 0)) {
1049 fr_strerror_printf_push("Failed decoding %s", vp->da->name);
1050 talloc_free(vp);
1051 return ret;
1052 }
1053 }
1054
1055 /*
1056 * Ensure that we grab all of the data.
1057 *
1058 * @todo - if there is data left over, decode it as raw octets. We then also have to keep track
1059 * of the maximum child number, and create unknown attributes starting from the last one.
1060 */
1061 if (fr_dbuff_remaining(&our_in)) {
1062 FR_PROTO_TRACE("Ignoring extra data in set");
1063 FR_PROTO_HEX_DUMP(fr_dbuff_current(&our_in), fr_dbuff_remaining(&our_in), " ");
1064
1065 (void) fr_dbuff_advance(&our_in, fr_dbuff_remaining(&our_in));
1066 }
1067
1069
1070 return fr_dbuff_set(in, &our_in);
1071}
1072
1073#ifdef __clang__
1074#pragma clang diagnostic ignored "-Wgnu-designator"
1075#endif
1076
1079{
1080 static bool const allowed_chars[SBUFF_CHAR_CLASS] = {
1081 [' '] = true, ['\''] = true, ['('] = true, [')'] = true,
1082 ['+'] = true, [','] = true, ['-'] = true, ['.'] = true,
1083 ['/'] = true, [':'] = true, ['='] = true, ['?'] = true,
1084 ['A' ... 'Z'] = true, ['a' ... 'z'] = true,
1085 ['0' ... '9'] = true,
1086 };
1087
1088 return fr_der_decode_string(ctx, out, parent, in, allowed_chars, decode_ctx);
1089}
1090
1093{
1094 static bool const allowed_chars[SBUFF_CHAR_CLASS] = {
1095 [0x08] = true, [0x0A] = true, [0x0C] = true, [0x0D] = true,
1096 [0x0E] = true, [0x0F] = true, [0x19] = true, [0x1A] = true,
1097 [0x1B] = true, [0x1D] = true, [' '] = true, ['!'] = true,
1098 ['"'] = true, ['%'] = true, ['&'] = true, ['\''] = true,
1099 ['('] = true, [')'] = true, ['*'] = true, ['+'] = true,
1100 [','] = true, ['-'] = true, ['.'] = true, ['/'] = true,
1101 [':'] = true, [';'] = true, ['<'] = true, ['='] = true,
1102 ['>'] = true, ['?'] = true, ['@'] = true, ['['] = true,
1103 [']'] = true, ['_'] = true, ['|'] = true, [0x7F] = true,
1104 [0x8B] = true, [0x8C] = true, [0x9B] = true, [0xA0] = true,
1105 [0xA1] = true, [0xA2] = true, [0xA3] = true, [0xA4] = true,
1106 [0xA5] = true, [0xA6] = true, [0xA7] = true, [0xA8] = true,
1107 [0xAB] = true, [0xB0] = true, [0xB1] = true, [0xB2] = true,
1108 [0xB3] = true, [0xB4] = true, [0xB5] = true, [0xB6] = true,
1109 [0xB7] = true, [0xB8] = true, [0xBB] = true, [0xBC] = true,
1110 [0xBD] = true, [0xBE] = true, [0xBF] = true, [0xC1] = true,
1111 [0xC2] = true, [0xC3] = true, [0xC4] = true, [0xC5] = true,
1112 [0xC6] = true, [0xC7] = true, [0xC8] = true, [0xC9] = true,
1113 [0xCA] = true, [0xCB] = true, [0xCC] = true, [0xCD] = true,
1114 [0xCE] = true, [0xCF] = true, [0xE0] = true, [0xE1] = true,
1115 [0xE2] = true, [0xE3] = true, [0xE4] = true, [0xE5] = true,
1116 [0xE7] = true, [0xE8] = true, [0xE9] = true, [0xEA] = true,
1117 [0xEB] = true, [0xEC] = true, [0xED] = true, [0xEE] = true,
1118 [0xEF] = true, [0xF0] = true, [0xF1] = true, [0xF2] = true,
1119 [0xF3] = true, [0xF4] = true, [0xF5] = true, [0xF6] = true,
1120 [0xF7] = true, [0xF8] = true, [0xF9] = true, [0xFA] = true,
1121 [0xFB] = true, [0xFC] = true, [0xFD] = true, [0xFE] = true,
1122 ['A' ... 'Z'] = true, ['a' ... 'z'] = true,
1123 ['0' ... '9'] = true,
1124 };
1125
1126 return fr_der_decode_string(ctx, out, parent, in, allowed_chars, decode_ctx);
1127}
1128
1129/*
1130 * 128 characters exactly. Identical to the first 128 characters of the ASCII alphabet.
1131 */
1134{
1135#if 0
1136 static bool const allowed_chars[SBUFF_CHAR_CLASS] = {
1137 [0x00 ... 0x7f] = true,
1138 };
1139#endif
1140
1141 return fr_der_decode_string(ctx, out, parent, in, NULL, decode_ctx);
1142}
1143
1146{
1147 fr_pair_t *vp;
1148 fr_dbuff_t our_in = FR_DBUFF(in);
1149 char timestr[DER_UTC_TIME_LEN + 1] = {};
1150 char *p;
1151 struct tm tm = {};
1152
1154
1155 /*
1156 * ISO/IEC 8825-1:2021
1157 * 8.25 Encoding for values of the useful types
1158 * The following "useful types" shall be encoded as if they had been replaced by their definitions
1159 * given in clauses 46-48 of Rec. ITU-T X.680 | ISO/IEC 8824-1:
1160 * - generalized time;
1161 * - universal time;
1162 * - object descriptor.
1163 *
1164 * 8.26 Encoding for values of the TIME type and the useful time types
1165 * 8.26 Encoding for values of the TIME type and the useful time types 8.26.1 Encoding for values
1166 * of the TIME type NOTE - The defined time types are subtypes of the TIME type, with the same
1167 * tag, and have the same encoding as the TIME type. 8.26.1.1 The encoding of the TIME type shall
1168 * be primitive. 8.26.1.2 The contents octets shall be the UTF-8 encoding of the value notation,
1169 * after the removal of initial and final QUOTATION MARK (34) characters.
1170 *
1171 * 11.8 UTCTime
1172 * 11.8.1 The encoding shall terminate with "Z", as described in the ITU-T X.680 | ISO/IEC 8824-1
1173 * clause on UTCTime.
1174 * 11.8.2 The seconds element shall always be present.
1175 * 11.8.3 Midnight (GMT) shall be represented as "YYMMDD000000Z", where "YYMMDD" represents the
1176 * day following the midnight in question.
1177 */
1178
1179 /*
1180 * The format of a UTC time is "YYMMDDhhmmssZ"
1181 * Where:
1182 * 1. YY is the year
1183 * 2. MM is the month
1184 * 3. DD is the day
1185 * 4. hh is the hour
1186 * 5. mm is the minute
1187 * 6. ss is the second (not optional in DER)
1188 * 7. Z is the timezone (UTC)
1189 */
1190
1192
1193 if (memchr(timestr, '\0', DER_UTC_TIME_LEN) != NULL) {
1194 fr_strerror_const_push("UTC time contains null byte");
1195 return -1;
1196 }
1197
1198 timestr[DER_UTC_TIME_LEN] = '\0';
1199
1200 p = strptime(timestr, "%y%m%d%H%M%SZ", &tm);
1201
1202 if (unlikely(p == NULL) || *p != '\0') {
1203 fr_strerror_const_push("Invalid UTC time format");
1204 return -1;
1205 }
1206
1207 vp = fr_pair_afrom_da(ctx, parent);
1208 if (unlikely(!vp)) {
1209 fr_strerror_const_push("Out of memory");
1210 return -1;
1211 }
1212
1213 vp->vp_date = fr_unix_time_from_tm(&tm);
1214
1216
1217 return fr_dbuff_set(in, &our_in);
1218}
1219
1222{
1223 fr_pair_t *vp;
1224 fr_dbuff_t our_in = FR_DBUFF(in);
1226 char *p;
1227 unsigned long subseconds = 0;
1228 struct tm tm = {};
1229
1230 size_t len = fr_dbuff_remaining(&our_in);
1231
1233
1234 if (len < DER_GENERALIZED_TIME_LEN_MIN) {
1235 fr_strerror_const_push("Insufficient data for generalized time or incorrect length");
1236 return -1;
1237 }
1238
1239 /*
1240 * ISO/IEC 8825-1:2021
1241 * 8.25 Encoding for values of the useful types
1242 * The following "useful types" shall be encoded as if they had been replaced by their definitions
1243 * given in clauses 46-48 of Rec. ITU-T X.680 | ISO/IEC 8824-1:
1244 * - generalized time;
1245 * - universal time;
1246 * - object descriptor.
1247 *
1248 * 8.26 Encoding for values of the TIME type and the useful time types
1249 * 8.26 Encoding for values of the TIME type and the useful time types 8.26.1 Encoding for values
1250 * of the TIME type NOTE - The defined time types are subtypes of the TIME type, with the same
1251 * tag, and have the same encoding as the TIME type. 8.26.1.1 The encoding of the TIME type shall
1252 * be primitive. 8.26.1.2 The contents octets shall be the UTF-8 encoding of the value notation,
1253 * after the removal of initial and final QUOTATION MARK (34) characters.
1254 *
1255 * 11.7 GeneralizedTime
1256 * 11.7.1 The encoding shall terminate with a "Z", as described in the Rec. ITU-T X.680 | ISO/IEC
1257 * 8824-1 clause on GeneralizedTime.
1258 * 11.7.2 The seconds element shall always be present.
1259 * 11.7.3 The fractional-seconds elements, if present, shall omit all trailing zeros; if the
1260 * elements correspond to 0, they shall be wholly omitted, and the decimal point element
1261 * also shall be omitted.
1262 */
1263
1264 /*
1265 * The format of a generalized time is "YYYYMMDDHHMMSS[.fff]Z"
1266 * Where:
1267 * 1. YYYY is the year
1268 * 2. MM is the month
1269 * 3. DD is the day
1270 * 4. HH is the hour
1271 * 5. MM is the minute
1272 * 6. SS is the second
1273 * 7. fff is the fraction of a second (optional)
1274 * 8. Z is the timezone (UTC)
1275 */
1276
1278
1279 if (memchr(timestr, '\0', DER_GENERALIZED_TIME_LEN_MIN) != NULL) {
1280 fr_strerror_const_push("Generalized time contains null byte");
1281 return -1;
1282 }
1283
1285 fr_strerror_const_push("Incorrect format for generalized time. Missing timezone");
1286 return -1;
1287 }
1288
1289 /*
1290 * Check if the fractional seconds are present
1291 */
1292 if (timestr[DER_GENERALIZED_TIME_LEN_MIN - 1] == '.') {
1293 /*
1294 * We only support subseconds up to 9 decimal places (nanoseconds)
1295 */
1296 char subsecstring[DER_GENERALIZED_TIME_PRECISION_MAX + 1];
1297
1299
1301 precision = fr_dbuff_remaining(&our_in) - 1;
1302 }
1303
1304 if (unlikely(precision == 0)) {
1305 fr_strerror_const_push("Insufficient data for subseconds");
1306 return -1;
1307 }
1308
1309 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *)subsecstring, &our_in, precision);
1310
1311 if (memchr(subsecstring, '\0', precision) != NULL) {
1312 fr_strerror_const_push("Generalized time contains null byte in subseconds");
1313 return -1;
1314 }
1315
1316 subsecstring[precision] = '\0';
1317
1318 /*
1319 * Convert the subseconds to an unsigned long
1320 */
1321 subseconds = strtoul(subsecstring, NULL, 10);
1322
1323 /*
1324 * Scale to nanoseconds based on actual precision.
1325 */
1326 {
1327 static const unsigned long nsec_multiplier[] = {
1328 [1] = 100000000,
1329 [2] = 10000000,
1330 [3] = 1000000,
1331 [4] = 100000,
1332 [5] = 10000,
1333 [6] = 1000,
1334 [7] = 100,
1335 [8] = 10,
1336 [9] = 1,
1337 };
1338 subseconds *= nsec_multiplier[precision];
1339 }
1340 }
1341
1342 /*
1343 * Make sure the timezone is UTC (Z)
1344 */
1346
1348
1349 p = strptime(timestr, "%Y%m%d%H%M%SZ", &tm);
1350
1351 if (unlikely(p == NULL)) {
1352 fr_strerror_const_push("Invalid generalized time format (strptime)");
1353 return -1;
1354 }
1355
1356 vp = fr_pair_afrom_da(ctx, parent);
1357 if (unlikely(!vp)) {
1358 fr_strerror_const_push("Out of memory");
1359 return -1;
1360 }
1361
1362 vp->vp_date = fr_unix_time_add(fr_unix_time_from_tm(&tm), fr_time_delta_wrap(subseconds));
1363
1365
1366 /*
1367 * Move to the end of the buffer
1368 * This is necessary because the fractional seconds are being ignored
1369 */
1370 fr_dbuff_advance(&our_in, fr_dbuff_remaining(&our_in));
1371
1372 return fr_dbuff_set(in, &our_in);
1373}
1374
1377{
1378 static bool const allowed_chars[SBUFF_CHAR_CLASS] = {
1379 [' '] = true, ['!'] = true, ['"'] = true, ['#'] = true,
1380 ['$'] = true, ['%'] = true, ['&'] = true, ['\''] = true,
1381 ['('] = true, [')'] = true, ['*'] = true, ['+'] = true,
1382 [','] = true, ['-'] = true, ['.'] = true, ['/'] = true,
1383 [':'] = true, [';'] = true, ['<'] = true, ['='] = true,
1384 ['>'] = true, ['?'] = true, ['@'] = true, ['['] = true,
1385 ['\\'] = true, [']'] = true, ['^'] = true, ['_'] = true,
1386 ['`'] = true, ['{'] = true, ['|'] = true, ['}'] = true,
1387 ['A' ... 'Z'] = true, ['a' ... 'z'] = true,
1388 ['0' ... '9'] = true,
1389 };
1390
1391 return fr_der_decode_string(ctx, out, parent, in, allowed_chars, decode_ctx);
1392}
1393
1394/*
1395 * We have per-type function names to make it clear that different types have different decoders.
1396 * However, the methods to decode them are the same. So rather than having trampoline functions, we just
1397 * use defines.
1398 */
1399#define fr_der_decode_enumerated fr_der_decode_integer
1400
1403{
1404 return fr_der_decode_string(ctx, out, parent, in, NULL, decode_ctx);
1405}
1406
1409{
1410 return fr_der_decode_string(ctx, out, parent, in, NULL, decode_ctx);
1411}
1412
1415{
1416 uint8_t byte;
1417 fr_pair_t *vp;
1418 fr_dbuff_t our_in = FR_DBUFF(in);
1419
1420 /*
1421 * RFC3779 Section 2.1.1.
1422 *
1423 * An IP address or prefix is encoded in the IP address delegation
1424 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
1425 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
1426 * STRING consists of the BIT STRING type (0x03), followed by (an
1427 * encoding of) the number of value octets, followed by the value. The
1428 * value consists of an "initial octet" that specifies the number of
1429 * unused bits in the last value octet, followed by the "subsequent
1430 * octets" that contain the octets of the bit string. (For IP
1431 * addresses, the encoding of the length will be just the length.)
1432 */
1433
1434 if (fr_dbuff_remaining(&our_in) != 1 + sizeof(vp->vp_ipv4addr)) {
1435 fr_strerror_printf_push("Invalid ipv4addr size. Expected %zu, got %zu",
1436 1 + sizeof(vp->vp_ipv4addr), fr_dbuff_remaining(&our_in));
1437 return -1;
1438 }
1439
1440 FR_DBUFF_OUT_RETURN(&byte, &our_in);
1441 if (byte != 0) {
1442 fr_strerror_printf_push("Invalid ipv4addr prefix is non-zero (%02x)", byte);
1443 return -1;
1444 }
1445
1446 vp = fr_pair_afrom_da(ctx, parent);
1447 if (unlikely(!vp)) {
1448 fr_strerror_const_push("Out of memory");
1449 return -1;
1450 }
1451
1452 vp->vp_ip.af = AF_INET;
1453 vp->vp_ip.prefix = 32;
1454 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *) &vp->vp_ipv4addr, &our_in, sizeof(vp->vp_ipv4addr));
1455
1457
1458 return fr_dbuff_set(in, &our_in);
1459}
1460
1463{
1464 uint8_t byte;
1465 fr_pair_t *vp;
1466 fr_dbuff_t our_in = FR_DBUFF(in);
1467 size_t len = fr_dbuff_remaining(&our_in);
1468
1469 /*
1470 * RFC3779 Section 2.1.1.
1471 *
1472 * An IP address or prefix is encoded in the IP address delegation
1473 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
1474 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
1475 * STRING consists of the BIT STRING type (0x03), followed by (an
1476 * encoding of) the number of value octets, followed by the value. The
1477 * value consists of an "initial octet" that specifies the number of
1478 * unused bits in the last value octet, followed by the "subsequent
1479 * octets" that contain the octets of the bit string. (For IP
1480 * addresses, the encoding of the length will be just the length.)
1481 */
1482
1483 if (!len || (len > 1 + sizeof(vp->vp_ipv4addr))) {
1484 fr_strerror_printf_push("Invalid ipv4prefix size. Expected 1..%zu, got %zu",
1485 1 + sizeof(vp->vp_ipv4addr), len);
1486 return -1;
1487 }
1488 len--;
1489
1490 FR_DBUFF_OUT_RETURN(&byte, &our_in);
1491 if (byte > 7) {
1492 fr_strerror_printf_push("Invalid ipv4prefix is too large (%02x)", byte);
1493 return -1;
1494 }
1495
1496 vp = fr_pair_afrom_da(ctx, parent);
1497 if (unlikely(!vp)) {
1498 fr_strerror_const_push("Out of memory");
1499 return -1;
1500 }
1501
1502 vp->vp_ip.af = AF_INET;
1503 vp->vp_ip.prefix = len * 8 - byte;
1504
1505 if (len) FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *) &vp->vp_ipv4addr, &our_in, len);
1506
1508
1509 return fr_dbuff_set(in, &our_in);
1510}
1511
1514{
1515 uint8_t byte;
1516 fr_pair_t *vp;
1517 fr_dbuff_t our_in = FR_DBUFF(in);
1518
1519 /*
1520 * RFC3779 Section 2.1.1.
1521 *
1522 * An IP address or prefix is encoded in the IP address delegation
1523 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
1524 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
1525 * STRING consists of the BIT STRING type (0x03), followed by (an
1526 * encoding of) the number of value octets, followed by the value. The
1527 * value consists of an "initial octet" that specifies the number of
1528 * unused bits in the last value octet, followed by the "subsequent
1529 * octets" that contain the octets of the bit string. (For IP
1530 * addresses, the encoding of the length will be just the length.)
1531 */
1532
1533 if (fr_dbuff_remaining(&our_in) != 1 + sizeof(vp->vp_ipv6addr)) {
1534 fr_strerror_printf_push("Invalid ipv6addr size. Expected %zu, got %zu",
1535 1 + sizeof(vp->vp_ipv6addr), fr_dbuff_remaining(&our_in));
1536 return -1;
1537 }
1538
1539 FR_DBUFF_OUT_RETURN(&byte, &our_in);
1540 if (byte != 0) {
1541 fr_strerror_printf_push("Invalid ipv6addr prefix is non-zero (%02x)", byte);
1542 return -1;
1543 }
1544
1545 vp = fr_pair_afrom_da(ctx, parent);
1546 if (unlikely(!vp)) {
1547 fr_strerror_const_push("Out of memory");
1548 return -1;
1549 }
1550
1551 vp->vp_ip.af = AF_INET6;
1552 vp->vp_ip.prefix = 128;
1553 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *) &vp->vp_ipv6addr, &our_in, sizeof(vp->vp_ipv6addr));
1554
1556
1557 return fr_dbuff_set(in, &our_in);
1558}
1559
1562{
1563 uint8_t byte;
1564 fr_pair_t *vp;
1565 fr_dbuff_t our_in = FR_DBUFF(in);
1566 size_t len = fr_dbuff_remaining(&our_in);
1567
1568 /*
1569 * RFC3779 Section 2.1.1.
1570 *
1571 * An IP address or prefix is encoded in the IP address delegation
1572 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
1573 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
1574 * STRING consists of the BIT STRING type (0x03), followed by (an
1575 * encoding of) the number of value octets, followed by the value. The
1576 * value consists of an "initial octet" that specifies the number of
1577 * unused bits in the last value octet, followed by the "subsequent
1578 * octets" that contain the octets of the bit string. (For IP
1579 * addresses, the encoding of the length will be just the length.)
1580 */
1581
1582 if (!len || (len > 1 + sizeof(vp->vp_ipv6addr))) {
1583 fr_strerror_printf_push("Invalid ipv6prefix size. Expected 1..%zu, got %zu",
1584 1 + sizeof(vp->vp_ipv6addr), len);
1585 return -1;
1586 }
1587 len--;
1588
1589 FR_DBUFF_OUT_RETURN(&byte, &our_in);
1590 if (byte > 7) {
1591 fr_strerror_printf_push("Invalid ipv6prefix is too large (%02x)", byte);
1592 return -1;
1593 }
1594
1595 vp = fr_pair_afrom_da(ctx, parent);
1596 if (unlikely(!vp)) {
1597 fr_strerror_const_push("Out of memory");
1598 return -1;
1599 }
1600
1601 vp->vp_ip.af = AF_INET6;
1602 vp->vp_ip.prefix = len * 8 - byte;
1603
1604 if (len) FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *) &vp->vp_ipv6addr, &our_in, len);
1605
1607
1608 return fr_dbuff_set(in, &our_in);
1609}
1610
1613{
1614 fr_pair_t *vp;
1615 fr_dbuff_t our_in = FR_DBUFF(in);
1616 size_t len = fr_dbuff_remaining(&our_in);
1617
1618 /*
1619 * RFC5280 Section 4.2.1.6
1620 *
1621 * When the subjectAltName extension contains an iPAddress, the address
1622 * MUST be stored in the octet string in "network byte order", as
1623 * specified in [RFC791]. The least significant bit (LSB) of each octet
1624 * is the LSB of the corresponding byte in the network address. For IP
1625 * version 4, as specified in [RFC791], the octet string MUST contain
1626 * exactly four octets. For IP version 6, as specified in
1627 * [RFC2460], the octet string MUST contain exactly sixteen octets.
1628 */
1629 if ((len != 4) && (len != 16)) {
1630 fr_strerror_printf_push("Invalid combo_ip_addr size. Expected 4 or 16, got %zu",
1631 len);
1632 return -1;
1633 }
1634
1635 vp = fr_pair_afrom_da(ctx, parent);
1636 if (unlikely(!vp)) {
1637 fr_strerror_const_push("Out of memory");
1638 return -1;
1639 }
1640
1641 if (len == 4) {
1642 vp->vp_ip.af = AF_INET;
1643 vp->vp_ip.prefix = 32;
1644 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *) &vp->vp_ipv4addr, &our_in, sizeof(vp->vp_ipv4addr));
1645
1646 } else {
1647 vp->vp_ip.af = AF_INET6;
1648 vp->vp_ip.prefix = 128;
1649 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *) &vp->vp_ipv6addr, &our_in, sizeof(vp->vp_ipv6addr));
1650 }
1651
1653
1654 return fr_dbuff_set(in, &our_in);
1655}
1656
1659{
1660 ssize_t slen;
1661 int i;
1662 fr_dict_attr_t const *da;
1663 fr_pair_t *vp;
1664
1666 .depth = 0,
1667 };
1668
1669 fr_assert(parent->type == FR_TYPE_ATTR);
1670
1671 /*
1672 * We don't use an intermediate dbuff here. We're not
1673 * doing anything with the dbuff, so an extra buffer
1674 * isn't necessary.
1675 */
1677 if (unlikely(slen <= 0)) return -1; /* OIDs of zero length are invalid */
1678
1679 vp = fr_pair_afrom_da(ctx, parent);
1680 if (unlikely(!vp)) {
1681 oom:
1682 fr_strerror_const_push("Out of memory");
1683 return -1;
1684 }
1685
1686 da = attr_oid_tree;
1687 for (i = 0; i < stack.depth; i++) {
1688 fr_dict_attr_t const *next;
1689
1690 next = fr_dict_attr_child_by_num(da, stack.oid[i]);
1691 if (!next) break;
1692 da = next;
1693 }
1694
1695 for (/* left over i */; i < stack.depth; i++) {
1697
1698 type = (i < (stack.depth - 1)) ? FR_TYPE_TLV : FR_TYPE_BOOL;
1699
1701 if (!da) {
1702 talloc_free(vp);
1703 goto oom;
1704 }
1705 }
1706
1707 vp->vp_attr = da;
1708 vp->data.enumv = attr_oid_tree;
1710 return slen;
1711}
1712
1713/** Decode an OID value pair
1714 *
1715 * @param[in] ctx Talloc context
1716 * @param[out] out Output list
1717 * @param[in] parent Parent attribute
1718 * @param[in] in Input buffer
1719 * @param[in] decode_ctx Decode context
1720 *
1721 * @return 0 on success, -1 on failure
1722 */
1724 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
1725{
1726 fr_dbuff_t our_in = FR_DBUFF(in);
1727 fr_dbuff_t oid_in;
1729 fr_pair_t *vp;
1730
1731 uint8_t tag;
1732 size_t oid_len;
1733 ssize_t slen;
1734
1735 FR_PROTO_TRACE("Decoding OID value pair");
1736
1738
1739 /*
1740 * A very common pattern in DER encoding is to have a sequence of set containing two things: an OID and a
1741 * value, where the OID is used to determine how to decode the value.
1742 * We will be decoding the OID first and then try to find the attribute associated with that OID to then
1743 * decode the value. If no attribute is found, one will be created and the value will be stored as raw
1744 * octets in the attribute.
1745 */
1746
1747 if (unlikely((slen = fr_der_decode_hdr(parent, &our_in, &tag, &oid_len, FR_DER_TAG_OID)) <= 0)) {
1748 error:
1749 fr_strerror_printf_push("Failed decoding %s OID header", parent->name);
1750 return slen;
1751 }
1752
1753 FR_PROTO_TRACE("Attribute %s, tag %u", parent->name, tag);
1754
1755 vp = fr_pair_afrom_da(ctx, parent);
1756 if (unlikely(vp == NULL)) {
1757 fr_strerror_const_push("Out of memory");
1758 return -1;
1759 }
1760
1761 uctx.ctx = vp;
1763 uctx.parent_list = &vp->vp_group;
1764
1765 fr_assert(uctx.parent_da != NULL);
1766
1767 /*
1768 * Limit the OID decoding to the length as given by the OID header.
1769 */
1770 oid_in = FR_DBUFF(&our_in);
1771 fr_dbuff_set_end(&oid_in, fr_dbuff_current(&oid_in) + oid_len);
1772
1773 slen = fr_der_decode_oid(&oid_in, fr_der_decode_oid_to_da, &uctx);
1774 if (unlikely(slen <= 0)) goto error;
1775
1776 /*
1777 * Skip the OID data.
1778 */
1779 FR_DBUFF_ADVANCE_RETURN(&our_in, oid_len);
1780
1781 if (unlikely(uctx.parent_da->flags.is_unknown)) {
1782 /*
1783 * This pair is not in the dictionary.
1784 * We will store the value as raw octets.
1785 */
1786 if (unlikely((slen = fr_der_decode_octetstring(uctx.ctx, uctx.parent_list, uctx.parent_da, &our_in,
1787 decode_ctx)) < 0)) {
1788 fr_strerror_printf_push("Failed decoding %s OID value", parent->name);
1789 return -1;
1790 }
1791 } else if (unlikely((slen = fr_der_decode_pair_dbuff(uctx.ctx, uctx.parent_list, uctx.parent_da, &our_in,
1792 decode_ctx)) < 0)) {
1793 fr_strerror_printf_push("Failed decoding %s OID value", parent->name);
1794 return -1;
1795 }
1796
1798
1799 return fr_dbuff_set(in, &our_in);
1800}
1801
1804 [FR_DER_TAG_INTEGER] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_integer },
1805 [FR_DER_TAG_OID] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_oid_wrapper },
1806 [FR_DER_TAG_BITSTRING] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_bitstring },
1808 [FR_DER_TAG_NULL] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_null },
1811 [FR_DER_TAG_SEQUENCE] = { .constructed = FR_DER_TAG_CONSTRUCTED, .decode = fr_der_decode_sequence },
1812 [FR_DER_TAG_SET] = { .constructed = FR_DER_TAG_CONSTRUCTED, .decode = fr_der_decode_set },
1817 [FR_DER_TAG_UTC_TIME] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_utc_time },
1824};
1825
1834
1838
1839/** Decode the tag and length fields of a DER encoded structure
1840 *
1841 * @param[in] parent Parent attribute
1842 * @param[in] in Input buffer
1843 * @param[out] tag Tag value
1844 * @param[out] len Length of the value field
1845 * @param[in] expected the expected / required tag
1846 *
1847 * @return 0 on success, -1 on failure
1848 */
1850 fr_der_tag_t expected)
1851{
1852 fr_dbuff_t our_in = FR_DBUFF(in);
1853 uint8_t tag_byte;
1854 uint8_t len_byte;
1855 fr_der_tag_decode_t const *func;
1856 fr_der_tag_class_t tag_class;
1857 fr_der_tag_constructed_t constructed;
1858 fr_der_attr_flags_t const *flags;
1859
1860 if (fr_dbuff_out(&tag_byte, &our_in) < 0) {
1861 error:
1862 fr_strerror_const_push("Failed decoding DER header - insufficient data");
1863 return -1;
1864 }
1865
1866 /*
1867 * Decode the tag flags
1868 */
1869 tag_class = (tag_byte & DER_TAG_CLASS_MASK);
1870 constructed = IS_DER_TAG_CONSTRUCTED(tag_byte);
1871
1872 /*
1873 * Decode the tag
1874 */
1875 if (IS_DER_TAG_CONTINUATION(tag_byte)) {
1876 /*
1877 * We have a multi-byte tag
1878 *
1879 * Note: Multi-byte tags would mean having a tag number that is greater than 30 (0x1E) (since tag
1880 * 31 would indicate a multi-byte tag). For most use-cases, this should not be needed, since all
1881 * of the basic ASN.1 types have values under 30, and if a CHOICE type were to have over 30 options
1882 * (meaning a multi-byte tag would be needed), that would be a very complex CHOICE type that
1883 * should probably be simplified.
1884 */
1885 fr_strerror_const_push("Multi-byte tags are not supported");
1886 return -1;
1887 }
1888
1889 *tag = tag_byte & DER_TAG_CONTINUATION;
1890
1891 /*
1892 * Check if the tag is not universal
1893 */
1894 switch (tag_class) {
1896 if ((*tag == FR_DER_TAG_INVALID) || (*tag >= FR_DER_TAG_VALUE_MAX)) {
1897 fr_strerror_printf_push("Invalid tag %u", *tag);
1898 return -1;
1899 }
1900
1901 if ((expected != FR_DER_TAG_INVALID) && (*tag != expected)) {
1902 fr_strerror_printf_push("Invalid tag %s. Expected tag %s",
1903 fr_der_tag_to_str(*tag), fr_der_tag_to_str(expected));
1904 return -1;
1905 }
1906 break;
1907
1908 default:
1909 /*
1910 * The data type will need to be resolved using the dictionary and the tag value
1911 */
1912 if (!parent) {
1913 fr_strerror_const_push("No parent attribute to resolve tag to class");
1914 return -1;
1915 }
1916 flags = fr_der_attr_flags(parent);
1917
1918 if (tag_class != flags->class) {
1919 fr_strerror_printf_push("Invalid DER class %02x for attribute %s. Expected DER class %02x",
1920 tag_class, parent->name, flags->class);
1921 return -1;
1922 }
1923
1924 /*
1925 * Doesn't match, check if it's optional.
1926 */
1927 if (flags->is_option) {
1928 if (*tag != flags->option) {
1929 if (flags->optional) return 0;
1930
1931 fr_strerror_printf_push("Invalid option %u for attribute %s. Expected option %u",
1932 *tag, parent->name, flags->option);
1933 return -1;
1934 }
1935
1936 *tag = flags->der_type;
1937
1938 } else {
1939 if (*tag != flags->der_type) {
1940 if (flags->optional) return 0;
1941
1942 fr_strerror_printf_push("Invalid tag %s for attribute %s. Expected tag %s",
1943 fr_der_tag_to_str(*tag), parent->name, fr_der_tag_to_str(flags->der_type));
1944 return -1;
1945 }
1946 }
1949 break;
1950 }
1951
1952 func = &tag_funcs[*tag];
1953 fr_assert(func != NULL);
1954
1955 if (unlikely(func->decode == NULL)) {
1956 fr_strerror_printf_push("No decode function for tag %u", *tag);
1957 return -1;
1958 }
1959
1960 if (IS_DER_TAG_CONSTRUCTED(func->constructed) != constructed) {
1961 fr_strerror_printf_push("Constructed flag mismatch for tag %u", *tag);
1962 return -1;
1963 }
1964
1965 if (fr_dbuff_out(&len_byte, &our_in) < 0) goto error;
1966
1967 /*
1968 * Check if the length is a multi-byte length field
1969 */
1970 if (IS_DER_LEN_MULTI_BYTE(len_byte)) {
1971 uint8_t len_len = len_byte & 0x7f;
1972 *len = 0;
1973
1974 /*
1975 * Length bits of zero is an indeterminate length field where
1976 * the length is encoded in the data instead.
1977 */
1978 if (len_len > 0) {
1979 if (unlikely(len_len > sizeof(*len))) {
1980 fr_strerror_printf_push("Length field too large (%" PRIu32 ")", len_len);
1981 return -1;
1982 }
1983
1984 while (len_len--) {
1985 if (fr_dbuff_out(&len_byte, &our_in) < 0) goto error;
1986 *len = (*len << 8) | len_byte;
1987 }
1988 } else if (!constructed) {
1989 fr_strerror_const_push("Primitive data with indefinite form length field is invalid");
1990 return -1;
1991 }
1992
1993 } else {
1994 *len = len_byte;
1995 }
1996
1997 /*
1998 * Ensure that there is the correct amount of data available to read.
1999 */
2000 if (*len && unlikely((fr_dbuff_extend_lowat(NULL, &our_in, *len) < *len))) {
2001 fr_strerror_printf_push("Insufficient data for length field (%zu)", *len);
2002 return -1;
2003 }
2004
2005 return fr_dbuff_set(in, &our_in);
2006}
2007
2008/** Decode a CHOICE type
2009 * This is where the actual decoding of the CHOICE type happens. The CHOICE type is a type that can have multiple
2010 * types, but only one of them can be present at a time. The type that is present is determined by the tag of the
2011 * data
2012 *
2013 * @param[in] ctx Talloc context
2014 * @param[in] out Output list
2015 * @param[in] parent Parent attribute
2016 * @param[in] in Input buffer
2017 * @param[in] decode_ctx Decode context
2018 */
2020 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
2021{
2022 fr_pair_t *vp;
2023 fr_dict_attr_t const *child = NULL;
2024 fr_dbuff_t our_in = FR_DBUFF(in);
2025 uint8_t tag;
2026 uint8_t tag_byte;
2027 uint8_t *current_marker = fr_dbuff_current(&our_in);
2028
2030
2031 FR_DBUFF_OUT_RETURN(&tag_byte, &our_in);
2032
2033 if (unlikely(IS_DER_TAG_CONTINUATION(tag_byte))) {
2034 fr_strerror_printf_push("Attribute %s is a choice, but received tag with continuation bit set",
2035 parent->name);
2036 return -1;
2037 }
2038
2039 tag = (tag_byte & DER_TAG_CONTINUATION);
2040
2041 child = fr_dict_attr_child_by_num(parent, tag);
2042 if (unlikely(!child)) {
2043 fr_strerror_printf_push("Attribute %s is a choice, but received unknown option %u",
2044 parent->name, tag);
2045 return -1;
2046 }
2047
2048 fr_dbuff_set(&our_in, current_marker);
2049
2050 vp = fr_pair_afrom_da(ctx, parent);
2051 if (unlikely(!vp)) {
2052 fr_strerror_const_push("Out of memory");
2053 return -1;
2054 }
2056
2057 if (unlikely(fr_der_decode_pair_dbuff(vp, &vp->vp_group, child, &our_in, decode_ctx) < 0)) {
2058 fr_strerror_printf_push("Failed decoding %s", vp->da->name);
2059 talloc_free(vp);
2060 return -1;
2061 }
2062
2064
2065 return fr_dbuff_set(in, &our_in);
2066}
2067
2068/** Decode an X509 Extentions Field
2069 *
2070 * @param[in] ctx Talloc context
2071 * @param[in] out Output list
2072 * @param[in] in Input buffer
2073 * @param[in] parent Parent attribute
2074 * @param[in] decode_ctx Decode context
2075 *
2076 * @return 0 on success, -1 on failure
2077 */
2079 fr_dict_attr_t const *parent, fr_der_decode_ctx_t *decode_ctx)
2080{
2081 fr_dbuff_t our_in = FR_DBUFF(in);
2082 fr_pair_t *vp, *vp2;
2083 fr_dict_attr_t const *ref;
2084
2085 uint8_t tag;
2086 uint64_t max;
2087 size_t len;
2088 ssize_t slen;
2089
2090 FR_PROTO_TRACE("Decoding extensions");
2091 FR_PROTO_TRACE("Attribute %s", parent->name);
2092 FR_PROTO_HEX_DUMP(fr_dbuff_current(in), fr_dbuff_remaining(in), "Top of extension decoding");
2093
2095
2096 /*
2097 * RFC 5280 Section 4.2
2098 * The extensions defined for X.509 v3 certificates provide methods for
2099 * associating additional attributes with users or public keys and for
2100 * managing relationships between CAs. The X.509 v3 certificate format
2101 * also allows communities to define private extensions to carry
2102 * information unique to those communities. Each extension in a
2103 * certificate is designated as either critical or non-critical.
2104 *
2105 * Each extension includes an OID and an ASN.1 structure. When an
2106 * extension appears in a certificate, the OID appears as the field
2107 * extnID and the corresponding ASN.1 DER encoded structure is the value
2108 * of the octet string extnValue.
2109 *
2110 * RFC 5280 Section A.1 Explicitly Tagged Module, 1988 Syntax
2111 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
2112 *
2113 * Extension ::= SEQUENCE {
2114 * extnID OBJECT IDENTIFIER,
2115 * critical BOOLEAN DEFAULT FALSE,
2116 * extnValue OCTET STRING
2117 * -- contains the DER encoding of an ASN.1 value
2118 * -- corresponding to the extension type identified
2119 * -- by extnID
2120 * }
2121 *
2122 * So the extensions are a SEQUENCE of SEQUENCEs containing an OID, a boolean and an OCTET STRING.
2123 * Note: If the boolean value is false, it should not be included in the encoding.
2124 */
2125
2126 /*
2127 * Get the overall length of the first inner sequence.
2128 * Ideally this should fill the entire outer sequence.
2129 */
2130 if (unlikely((slen = fr_der_decode_hdr(parent, &our_in, &tag, &len, FR_DER_TAG_SEQUENCE)) <= 0)) {
2131 fr_strerror_printf_push("Failed decoding %s sequence header", parent->name);
2132 return slen;
2133 }
2134
2135 if (len != fr_dbuff_remaining(&our_in)) {
2136 fr_strerror_printf_push("Inner %s x509extension sequence does not exactly fill the outer sequence",
2137 parent->name);
2138 return -1;
2139 }
2140
2141 /*
2142 * Normal extensions are decoded into the normal parent.
2143 */
2144 vp = fr_pair_afrom_da(ctx, parent);
2145 if (unlikely(!vp)) {
2146 oom:
2147 fr_strerror_const_push("Out of memory");
2148 return -1;
2149 }
2151
2152 /*
2153 * Critical extensions are decoded into the Critical parent.
2154 */
2155 ref = fr_dict_attr_ref(parent);
2156 fr_assert(ref != NULL);
2157 ref = fr_dict_attr_by_name(NULL, ref, "Critical");
2158 fr_assert(ref != NULL);
2159
2160 vp2 = fr_pair_afrom_da(vp, ref);
2161 if (unlikely(vp2 == NULL)) {
2162 talloc_free(vp);
2163 goto oom;
2164 }
2165 PAIR_ALLOCED(vp2);
2166
2167 max = fr_der_flag_max(parent); /* Maximum number of extensions which can be used here */
2168
2169 /*
2170 * Each extension is composed of a sequence containing the following objects:
2171 *
2172 * extnID OID - a printable string "1.2.3.4"
2173 * critical BOOLEAN OPTIONAL DEFAULT FALSE
2174 * extnValue OCTETSTRING - the DER encoding of the referenced ASN.1 extension
2175 */
2176 while (fr_dbuff_remaining(&our_in) > 0) {
2177 fr_dbuff_t seq_in = FR_DBUFF(&our_in);
2178 fr_dbuff_t oid_in;
2180 size_t seq_len, oid_len, ext_len;
2181
2182 FR_PROTO_HEX_DUMP(fr_dbuff_current(&our_in), fr_dbuff_remaining(&our_in), "inner x509 sequence");
2183
2184 if (!max) {
2185 fr_strerror_printf_push("Too many extensions - reached the limit of %" PRIu64, max);
2186 return -1;
2187 }
2188
2189 if (unlikely((slen = fr_der_decode_hdr(parent, &seq_in, &tag, &seq_len, FR_DER_TAG_SEQUENCE)) <= 0)) {
2190 fr_strerror_printf_push("Failed decoding %s extension inner sequence header",
2191 parent->name);
2192 error:
2193 talloc_free(vp);
2194 return slen;
2195 }
2196
2197 /*
2198 * Limit decoding for the inner sequence.
2199 */
2200 fr_dbuff_set_end(&seq_in, fr_dbuff_current(&seq_in) + seq_len);
2201
2202 /*
2203 * Start decoding the OID.
2204 */
2205 if (unlikely((slen = fr_der_decode_hdr(NULL, &seq_in, &tag, &oid_len, FR_DER_TAG_OID)) <= 0)) {
2206 fr_strerror_printf_push("Failed decoding %s OID header", parent->name);
2207 goto error;
2208 }
2209
2210 /*
2211 * Create a buffer where we can decode the OID. This lets us avoid any back and forth
2212 * with markers.
2213 *
2214 * The OID and extnValue will get decoded into a "critical" or "non-critical" vp,
2215 * depending on the value of the boolean Critical field. So we don't know where to
2216 * decode the OID until we see the Critical field. As a result, we have to save a
2217 * temporary OID buffer.
2218 */
2219 oid_in = FR_DBUFF(&seq_in);
2220 fr_dbuff_set_end(&oid_in, fr_dbuff_current(&oid_in) + oid_len);
2221
2222 FR_PROTO_TRACE("inner x509 OID length %zu", oid_len);
2223 FR_PROTO_HEX_DUMP(fr_dbuff_current(&oid_in), fr_dbuff_remaining(&oid_in), "inner x509 OID");
2224
2225 /*
2226 * Skip the OID data. We'll decode that later.
2227 */
2228 FR_DBUFF_ADVANCE_RETURN(&seq_in, oid_len);
2229
2230 /*
2231 * The next thing is either Critical, or is the extValue.
2232 */
2233 if (unlikely(fr_der_decode_hdr(NULL, &seq_in, &tag, &ext_len, FR_DER_TAG_INVALID) <= 0)) {
2234 fr_strerror_printf_push("Failed decoding %s extnValue", parent->name);
2235 slen = -1;
2236 goto error;
2237 }
2238
2239 uctx.ctx = vp;
2240 uctx.parent_da = vp->da;
2241 uctx.parent_list = &vp->vp_group;
2242
2243 /*
2244 * The optional boolean Critical field. This tells us where the extensions will be
2245 * decoded to.
2246 */
2247 if (tag == FR_DER_TAG_BOOLEAN) {
2248 uint8_t is_critical = false;
2249
2250 /*
2251 * This Extension has the Critical field.
2252 * If this value is true, we will be storing the pair in the critical list
2253 */
2254 if (unlikely(fr_dbuff_out(&is_critical, &seq_in) <= 0)) {
2255 fr_strerror_const_push("Insufficient data for isCritical field");
2256 slen = -1;
2257 goto error;
2258 }
2259
2260 /*
2261 * 0x00 is false. 0xff is true. But we don't care about invalid boolean values.
2262 */
2263 if (is_critical) {
2264 uctx.ctx = vp2;
2265 uctx.parent_da = vp2->da;
2266 uctx.parent_list = &vp2->vp_group;
2267 }
2268
2269 /*
2270 * The next header should be the extnValue
2271 */
2272 if (unlikely(fr_der_decode_hdr(NULL, &seq_in, &tag, &ext_len, FR_DER_TAG_OCTETSTRING) <= 0)) {
2273 fr_strerror_printf_push("Failed decoding %s extnValue", parent->name);
2274 slen = -1;
2275 goto error;
2276 }
2277 } else {
2278 /*
2279 * The extnValue is DER tag OCTETSTRING.
2280 */
2281 if (unlikely(tag != FR_DER_TAG_OCTETSTRING)) {
2282 fr_strerror_printf_push("Expected tag OCTETSTRING for the %s extnValue. Got tag %s",
2283 parent->name, fr_der_tag_to_str(tag));
2284 slen = -1;
2285 goto error;
2286 }
2287 }
2288
2289 /*
2290 * We leave the seq_in buffer at the extnValue field, which lets us decode it later.
2291 */
2293 "extnValue");
2294
2295 /*
2296 * Decode the OID, which gets us the DA which lets us know how to decode the extnValue.
2297 */
2298 if (unlikely((slen = fr_der_decode_oid(&oid_in, fr_der_decode_oid_to_da, &uctx)) <= 0)) {
2299 fr_strerror_const_push("Failed decoding OID in extension");
2300 goto error;
2301 }
2302
2303 /*
2304 * This has been updated with the OID reference.
2305 */
2306 fr_assert(uctx.parent_da != NULL);
2307
2308 FR_PROTO_HEX_DUMP(fr_dbuff_current(&seq_in), fr_dbuff_remaining(&seq_in), "inner x509 extnValue");
2309
2310 /*
2311 * The extension was not found in the dictionary. We will store the value as raw octets.
2312 */
2313 if (uctx.parent_da->flags.is_unknown) {
2314 slen = fr_der_decode_octetstring(uctx.ctx, uctx.parent_list, uctx.parent_da,
2315 &seq_in, decode_ctx);
2316 } else {
2317 slen = fr_der_decode_pair_dbuff(uctx.ctx, uctx.parent_list, uctx.parent_da, &seq_in,
2318 decode_ctx);
2319 }
2320 if (unlikely(slen < 0)) {
2321 fr_strerror_printf_push("Failed decoding %s extValue", parent->name);
2322 goto error;
2323 }
2324
2325 if (fr_dbuff_remaining(&seq_in)) {
2326 fr_strerror_printf_push("Failed to decode all of the data in the %s x509_extensions inner sequence",
2327 parent->name);
2328 return -1;
2329 }
2330
2332 "Remaining data after decoding all of the extension");
2333 max--;
2334
2335 (void) fr_dbuff_set(&our_in, &seq_in);
2336 }
2337
2338 if (fr_pair_list_num_elements(&vp2->children) > 0) {
2339 fr_pair_prepend(&vp->vp_group, vp2);
2340 } else {
2341 talloc_free(vp2);
2342 }
2343
2345
2346 return fr_dbuff_set(in, fr_dbuff_end(&our_in));
2347}
2348
2350 bool const allowed_chars[], UNUSED fr_der_decode_ctx_t *decode_ctx)
2351{
2352 fr_pair_t *vp;
2353 fr_dbuff_t our_in = FR_DBUFF(in);
2354 char *str = NULL;
2355
2356 size_t pos, len = fr_dbuff_remaining(&our_in);
2357
2359
2360 /*
2361 * ISO/IEC 8825-1:2021
2362 * 8.23 Encoding for values of the restricted character string types
2363 * 8.23.1 The data value consists of a string of characters from the character set specified in the ASN.1
2364 * type definition. 8.23.2 Each data value shall be encoded independently of other data values of
2365 * the same type.
2366 * 8.23.3 Each character string type shall be encoded as if it had been declared:
2367 * [UNIVERSAL x] IMPLICIT OCTET STRING
2368 * where x is the number of the universal class tag assigned to the character string type in
2369 * Rec. ITU-T X.680 | ISO/IEC 8824-1. The value of the octet string is specified in 8.23.4 and
2370 * 8.23.5.
2371 */
2372
2373 vp = fr_pair_afrom_da(ctx, parent);
2374 if (unlikely(!vp)) {
2375 oom:
2376 fr_strerror_const_push("Out of memory");
2377 return -1;
2378 }
2380
2381 if (unlikely(fr_pair_value_bstr_alloc(vp, &str, len, false) < 0)) {
2382 talloc_free(vp);
2383 goto oom;
2384 }
2385
2386 (void) fr_dbuff_out_memcpy((uint8_t *)str, &our_in, len); /* this can never fail */
2387
2388 if (allowed_chars && len) {
2389 fr_sbuff_t sbuff;
2390 sbuff = FR_SBUFF_OUT(str, len);
2391
2392 if ((pos = fr_sbuff_adv_past_allowed(&sbuff, SIZE_MAX, allowed_chars, NULL)) < len - 1) {
2393 invalid:
2394 fr_strerror_printf_push("Invalid character in a string (%" PRId32 ")", str[pos]);
2395 return -1;
2396 }
2397
2398 // Check the final character
2399 if (!allowed_chars[(uint8_t)str[pos]]) goto invalid;
2400 }
2401
2402 str[len] = '\0';
2403
2405
2406 return fr_dbuff_set(in, &our_in);
2407}
2408
2410 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
2411{
2412 fr_dbuff_t our_in = FR_DBUFF(in);
2413 fr_der_tag_decode_t const *func;
2414 ssize_t slen;
2415 uint8_t tag;
2416 size_t len;
2418
2419 /*
2420 * ISO/IEC 8825-1:2021
2421 * The structure of a DER encoding is as follows:
2422 *
2423 * +------------+--------+-------+
2424 * | IDENTIFIER | LENGTH | VALUE |
2425 * +------------+--------+-------+
2426 *
2427 * The IDENTIFIER is a tag that specifies the type of the value field and is encoded as follows:
2428 *
2429 * 8 7 6 5 4 3 2 1
2430 * +---+---+-----+---+---+---+---+---+
2431 * | Class | P/C | Tag Number |
2432 * +---+---+-----+---+---+---+---+---+
2433 * |
2434 * |- 0 = Primitive
2435 * |- 1 = Constructed
2436 *
2437 * The CLASS field specifies the encoding class of the tag and may be one of the following values:
2438 *
2439 * +------------------+-------+-------+
2440 * | Class | Bit 8 | Bit 7 |
2441 * +------------------+-------+-------+
2442 * | UNIVERSAL | 0 | 0 |
2443 * | APPLICATION | 0 | 1 |
2444 * | CONTEXT-SPECIFIC | 1 | 0 |
2445 * | PRIVATE | 1 | 1 |
2446 * +------------------+-------+-------+
2447 *
2448 * The P/C field specifies whether the value field is primitive or constructed.
2449 * The TAG NUMBER field specifies the tag number of the value field and is encoded as an unsigned binary
2450 * integer.
2451 *
2452 * The LENGTH field specifies the length of the VALUE field and is encoded as an unsigned binary integer
2453 * and may be encoded as a single byte or multiple bytes.
2454 *
2455 * The VALUE field contains LENGTH number of bytes and is encoded according to the tag.
2456 *
2457 */
2458
2459 /*
2460 * Ensure that we have at least 2 bytes for the header.
2461 */
2462 slen = fr_dbuff_extend_lowat(NULL, &our_in, 2);
2463 if (unlikely(slen < 0)) {
2464 fr_strerror_const("Failed trying to read more data");
2465 return -1;
2466 }
2467
2468 /*
2469 * One byte is not enough.
2470 */
2471 if (unlikely(slen == 1)) {
2472 fr_strerror_printf_push("Truncated header while trying to decode %s", parent->name);
2473 return -1;
2474 }
2475
2476 /*
2477 * No header, we may need to create a default value.
2478 */
2479 if (unlikely(slen == 0)) {
2480 fr_pair_t *vp;
2481
2482 if (likely(!flags->has_default_value)) return 0;
2483
2484 create_default:
2485 vp = fr_pair_afrom_da(ctx, parent);
2486 if (unlikely(!vp)) {
2487 fr_strerror_const_push("Out of memory");
2488 return -1;
2489 }
2491
2492 if (unlikely(fr_value_box_copy(vp, &vp->data, flags->default_value) < 0)) {
2493 talloc_free(vp);
2494 return -1;
2495 }
2496
2497 vp->data.enumv = vp->da;
2498
2500
2501 return 0;
2502 }
2503
2504 if (unlikely(flags->is_choice)) {
2505 slen = fr_der_decode_choice(ctx, out, parent, &our_in, decode_ctx);
2506
2507 if (unlikely(slen <= 0)) return slen;
2508
2509 return fr_dbuff_set(in, &our_in);
2510 }
2511
2512 slen = fr_der_decode_hdr(parent, &our_in, &tag, &len, FR_DER_TAG_INVALID);
2513 if ((slen == 0) && flags->optional) return 0;
2514 if (slen <= 0) {
2515 fr_strerror_printf_push("Failed decoding %s header", parent->name);
2516 return -1;
2517 }
2518
2519 FR_PROTO_TRACE("Attribute %s, tag %u", parent->name, tag);
2520
2521 /*
2522 * Limit the length of the data to be decoded.
2523 */
2524 fr_dbuff_set_end(&our_in, fr_dbuff_current(&our_in) + len);
2525
2526 /*
2527 * Unknown attributes have no defaults, and can be zero
2528 * length. We also ignore whatever tag and class is
2529 * being used.
2530 *
2531 * @todo - we need to store the tag and class somewhere,
2532 * so that re-encoding the "raw" data type will result in
2533 * the same data.
2534 */
2535 if (unlikely(parent->flags.is_unknown)) {
2537 goto decode_it;
2538 }
2539
2540 /*
2541 * No data? Try to set a default value, OR decode it as
2542 * NULL.
2543 */
2544 if (unlikely(fr_dbuff_remaining(&our_in) == 0)) {
2545 if (flags->has_default_value) goto create_default;
2546
2547 if (tag == FR_DER_TAG_NULL) {
2548 func = &tag_funcs[FR_DER_TAG_NULL];
2549 goto decode_it;
2550 }
2551
2552 }
2553
2554 /*
2555 * Hacks for serialNumber
2556 */
2557 if (unlikely((tag == FR_DER_TAG_INTEGER) && (parent->type == FR_TYPE_OCTETS))) {
2559 goto decode_it;
2560 }
2561
2562 /*
2563 * We didn't get the expected tag. If it's not allowed for this parent, OR it's not an
2564 * equivalent tag, then that is likely an error.
2565 *
2566 * The "compatible" check is to really to hack around Time and DirectoryString. It's technically
2567 * wrong, and should perhaps be fixed.
2568 *
2569 * @todo - parse 'string' and 'date', and then set flags->restrictions to allow any compatible
2570 * DER tags, as a hack. Doing that makes this a little more generic? Or, add support for data
2571 * types "Time" and "DirectoryString", and do the right thing. Or, define them as separate
2572 * attributes in dictionarty.common, and remove the "tags compatible" checks.
2573 */
2574 if (unlikely((tag != flags->der_type) &&
2575 (!fr_type_to_der_tag_valid(parent->type, tag) || !fr_der_tags_compatible(tag, flags->der_type)))) {
2576 /*
2577 * Optional or not, if we can create a default value, then do so.
2578 */
2579 if (flags->has_default_value) goto create_default;
2580
2581 /*
2582 * Optional means "decoded nothing". Otherwise it's a hard failure.
2583 */
2584 if (!flags->optional) {
2585 fr_strerror_printf_push("Failed decoding %s - got tag '%s', expected '%s'", parent->name,
2587 return -1;
2588 }
2589
2590 return 0;
2591 }
2592
2593 if (flags->is_extensions) {
2594 slen = fr_der_decode_x509_extensions(ctx, out, &our_in, parent, decode_ctx);
2595 if (slen <= 0) return slen;
2596
2597 return fr_dbuff_set(in, &our_in);
2598 }
2599
2600 func = &type_funcs[parent->type];
2601 if (!func->decode) func = &tag_funcs[tag];
2602 fr_assert(func != NULL);
2603 fr_assert(func->decode != NULL);
2604
2605 /*
2606 * Enforce limits on min/max.
2607 */
2608 switch (tag) {
2610 case FR_DER_TAG_SET:
2611 /*
2612 * min/max is the number of elements, NOT the number of bytes. The set / sequence
2613 * decoder has to validate its input.
2614 */
2615
2616 /*
2617 * If the sequence or set is an OID Value pair, then we decode it with the special OID
2618 * Value decoder.
2619 */
2620 if (flags->is_oid_and_value) func = &oid_and_value_func;
2621 break;
2622
2623 /*
2624 * min/max applies to the decoded values.
2625 */
2626 case FR_DER_TAG_INTEGER:
2628 break;
2629
2630 default:
2631 if (parent->flags.is_raw) break;
2632
2633 /*
2634 * min/max can be fixed width, but we only care for 'octets' and 'string'.
2635 *
2636 * @todo - when we support IP addresses (which DER usually encodes as strings), this
2637 * check will have to be updated.
2638 */
2639 if (parent->flags.is_known_width) {
2640 if (!fr_type_is_variable_size(parent->type)) break;
2641
2642 if (len != parent->flags.length) {
2643 fr_strerror_printf_push("Data length (%zu) is different from expected fixed size (%u)", len, parent->flags.length);
2644 return -1;
2645 }
2646
2647 break;
2648 }
2649
2650 if (flags->min && (len < flags->min)) {
2651 fr_strerror_printf_push("Data length (%zu) is smaller than expected minimum size (%u)", len, flags->min);
2652 return -1;
2653 }
2654
2655 fr_assert(flags->max <= DER_MAX_STR); /* 'max' is always set in the attr_valid() function */
2656
2657 if (unlikely(len > flags->max)) {
2658 fr_strerror_printf_push("Data length (%zu) exceeds max size (%" PRIu64 ")", len, flags->max);
2659 return -1;
2660 }
2661 break;
2662 }
2663
2664 /*
2665 * The decode function can return 0 if len==0. This is true for 'null' data types, and
2666 * for variable-sized types such as strings.
2667 */
2668decode_it:
2669 slen = func->decode(ctx, out, parent, &our_in, decode_ctx);
2670 if (unlikely(slen < 0)) return slen;
2671
2672 /*
2673 * There may be extra data, in which case we ignore it.
2674 *
2675 * @todo - if the data type is fixed size, then return an error.
2676 */
2677 if ((size_t) slen < len) {
2678 FR_PROTO_TRACE("Ignoring extra data");
2679 FR_PROTO_HEX_DUMP(fr_dbuff_current(&our_in), fr_dbuff_remaining(&our_in), " ");
2680
2681 fr_dbuff_advance(&our_in, len - (size_t) slen);
2682 }
2683
2684 return fr_dbuff_set(in, &our_in);
2685}
2686
2687static ssize_t fr_der_decode_proto(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len,
2688 void *proto_ctx)
2689{
2690 fr_dbuff_t our_in = FR_DBUFF_TMP(data, data_len);
2691
2693
2695 fr_strerror_printf_push("Invalid dictionary. DER decoding requires a specific dictionary.");
2696 return -1;
2697 }
2698
2699 return fr_der_decode_pair_dbuff(ctx, out, parent, &our_in, proto_ctx);
2700}
2701
2702/** Decode a DER structure using the specific dictionary
2703 *
2704 * @param[in] ctx to allocate new pairs in.
2705 * @param[in] out where new VPs will be added
2706 * @param[in] parent Parent attribute. This should be the root of the dictionary
2707 * we're using to decode DER data. This only specifies structures
2708 * like SEQUENCES. OID based pairs are resolved using the global
2709 * dictionary tree.
2710 * @param[in] data to decode.
2711 * @param[in] data_len Length of data.
2712 * @param[in] decode_ctx to pass to decode function.
2713 *
2714 */
2715static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data,
2716 size_t data_len, void *decode_ctx)
2717{
2719 fr_strerror_printf_push("Invalid dictionary. DER decoding requires a specific dictionary.");
2720 return -1;
2721 }
2722
2723 return fr_der_decode_pair_dbuff(ctx, out, parent, &FR_DBUFF_TMP(data, data_len), decode_ctx);
2724}
2725
2726/*
2727 * Test points
2728 */
2729static int decode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict,
2730 UNUSED fr_dict_attr_t const *root_da)
2731{
2732 fr_der_decode_ctx_t *test_ctx;
2733
2734 test_ctx = talloc_zero(ctx, fr_der_decode_ctx_t);
2735 if (!test_ctx) return -1;
2736
2737 test_ctx->tmp_ctx = talloc_new(test_ctx);
2738
2739 *out = test_ctx;
2740
2741 return 0;
2742}
2743
2749
#define unlikely(_x)
Definition build.h:384
#define UNUSED
Definition build.h:318
#define NUM_ELEMENTS(_t)
Definition build.h:340
static size_t min(size_t x, size_t y)
Definition dbuff.c:66
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition dbuff.h:1081
#define FR_DBUFF_ADVANCE_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.
Definition dbuff.h:1097
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:919
#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:1012
#define fr_dbuff_out_memcpy(_out, _dbuff_or_marker, _outlen)
Copy exactly _outlen bytes from the dbuff.
Definition dbuff.h:1741
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
Definition dbuff.h:668
#define fr_dbuff_end(_dbuff_or_marker)
Return the current 'end' position of a dbuff or marker.
Definition dbuff.h:946
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
Definition dbuff.h:751
#define FR_DBUFF_OUT_MEMCPY_RETURN(_out, _dbuff_or_marker, _outlen)
Copy outlen bytes from the dbuff returning if there's insufficient data in the dbuff.
Definition dbuff.h:1761
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:1201
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
#define FR_DBUFF_OUT_RETURN(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type returning if there is insufficient data.
Definition dbuff.h:1827
#define fr_dbuff_out(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type.
Definition dbuff.h:1808
#define fr_dbuff_set_end(_dst, _end)
Set a new 'end' position in a dbuff or marker.
Definition dbuff.h:1059
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:522
fr_der_tag_t
Enumeration describing the data types in a DER encoded structure.
Definition der.h:34
@ FR_DER_TAG_IA5_STRING
String of IA5 (7bit) chars.
Definition der.h:48
@ FR_DER_TAG_SEQUENCE
A sequence of DER encoded data (a structure).
Definition der.h:44
@ FR_DER_TAG_SET
A set of DER encoded data (a structure).
Definition der.h:45
@ FR_DER_TAG_INTEGER
Arbitrary width signed integer.
Definition der.h:37
@ FR_DER_TAG_BOOLEAN
Boolean true/false.
Definition der.h:36
@ FR_DER_TAG_CHOICE
A choice of types. Techically not a DER tag, but used to represent a choice.
Definition der.h:56
@ FR_DER_TAG_UTF8_STRING
String of UTF8 chars.
Definition der.h:43
@ FR_DER_TAG_UTC_TIME
A time in UTC "YYMMDDhhmmssZ" format.
Definition der.h:49
@ FR_DER_TAG_GENERALIZED_TIME
A time in "YYYYMMDDHHMMSS[.fff]Z" format.
Definition der.h:50
@ FR_DER_TAG_INVALID
Invalid tag.
Definition der.h:35
@ FR_DER_TAG_NULL
An empty value.
Definition der.h:40
@ FR_DER_TAG_OCTETSTRING
String of octets (length field specifies bytes).
Definition der.h:39
@ FR_DER_TAG_VISIBLE_STRING
String of visible chars.
Definition der.h:51
@ FR_DER_TAG_BITSTRING
String of bits (length field specifies bits).
Definition der.h:38
@ FR_DER_TAG_T61_STRING
String of T61 (8bit) chars.
Definition der.h:47
@ FR_DER_TAG_ENUMERATED
An enumerated value.
Definition der.h:42
@ FR_DER_TAG_UNIVERSAL_STRING
String of universal chars.
Definition der.h:53
@ FR_DER_TAG_PRINTABLE_STRING
String of printable chars.
Definition der.h:46
@ FR_DER_TAG_GENERAL_STRING
String of general chars.
Definition der.h:52
@ FR_DER_TAG_OID
Reference to an OID based attribute.
Definition der.h:41
bool optional
optional, we MUST already have set 'option'
Definition der.h:107
bool is_extensions
a list of X.509 extensions
Definition der.h:111
fr_der_tag_t der_type
the DER type, which is different from the FreeRADIUS type
Definition der.h:95
bool is_option
has an option defined
Definition der.h:106
bool is_sequence_of
sequence_of has been defined
Definition der.h:108
TALLOC_CTX * tmp_ctx
ctx under which temporary data will be allocated
Definition der.h:119
#define DER_BOOLEAN_TRUE
DER encoded boolean true value.
Definition der.h:91
bool is_set_of
set_of has been defined
Definition der.h:109
#define DER_TAG_CONTINUATION
Mask to check if the tag is a continuation.
Definition der.h:86
#define DER_BOOLEAN_FALSE
DER encoded boolean false value.
Definition der.h:90
#define DER_TAG_CLASS_MASK
Mask to extract the class from the tag.
Definition der.h:82
#define fr_der_flag_max(_da)
Definition der.h:135
#define DER_UTC_TIME_LEN
Length of the UTC time string.
Definition der.h:78
fr_der_tag_constructed_t
Definition der.h:63
@ FR_DER_TAG_CONSTRUCTED
This is a sequence or set, it contains children.
Definition der.h:65
@ FR_DER_TAG_PRIMITIVE
This is a leaf value, it contains no children.
Definition der.h:64
#define DER_GENERALIZED_TIME_PRECISION_MAX
Maximum precision of the generalized time string (nanoseconds).
Definition der.h:80
#define DER_MAX_STR
Definition der.h:76
uint8_t min
mininum count
Definition der.h:104
bool is_oid_and_value
is OID+value
Definition der.h:110
static fr_der_attr_flags_t const * fr_der_attr_flags(fr_dict_attr_t const *da)
Definition der.h:122
bool is_choice
DER name "choice".
Definition der.h:115
#define FR_DER_TAG_VALUE_MAX
tags >=max can't exist
Definition der.h:61
uint8_t option
an "attribute number" encoded in the tag field.
Definition der.h:105
bool has_default_value
a default value exists
Definition der.h:112
fr_der_tag_class_t class
tag Class
Definition der.h:94
#define DER_GENERALIZED_TIME_LEN_MIN
Minimum length of the generalized time string.
Definition der.h:79
uint64_t max
maximum count of items in a sequence, set, or string.
Definition der.h:102
fr_der_tag_class_t
Definition der.h:68
@ FR_DER_CLASS_UNIVERSAL
Definition der.h:69
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3528
#define FR_DICT_TLV_NEST_MAX
Maximum level of TLV nesting allowed.
Definition dict.h:505
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:611
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2665
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition dict.h:190
#define FR_DICT_MAX_TLV_STACK
Maximum TLV stack size.
Definition dict.h:517
fr_dict_attr_t const * fr_dict_attr_iterate_children(fr_dict_attr_t const *parent, fr_dict_attr_t const **prev)
Iterate over children of a DA.
Definition dict_util.c:5014
static fr_dict_attr_t * fr_dict_attr_unknown_typed_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int num, fr_type_t type)
Definition dict.h:596
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3593
static fr_slen_t in
Definition dict.h:882
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:121
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:148
Test enumeration values.
Definition dict_test.h:92
talloc_free(hp)
HIDDEN fr_dict_t const * dict_der
Definition base.c:38
static char * stack[MAX_STACK]
Definition radmin.c:158
fr_type_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_OCTETS
Raw octets.
long int ssize_t
unsigned char uint8_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2966
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1352
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:290
int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "string" type value pair.
Definition pair.c:2763
int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "octets" type value pair.
Definition pair.c:2915
int fr_pair_prepend(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the start of the list.
Definition pair.c:1321
static int decode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict, UNUSED fr_dict_attr_t const *root_da)
Definition decode.c:102
HIDDEN fr_dict_attr_t const * attr_oid_tree
Definition base.c:39
bool fr_der_tags_compatible(fr_der_tag_t tag1, fr_der_tag_t tag2)
Definition base.c:102
bool fr_type_to_der_tag_valid(fr_type_t type, fr_der_tag_t tag)
Definition base.c:178
char const * fr_der_tag_to_str(fr_der_tag_t tag)
Definition base.c:79
ssize_t(* fr_der_decode_oid_t)(uint64_t subidentifier, void *uctx, bool is_last)
Definition decode.c:44
static ssize_t fr_der_decode_oid_to_da(uint64_t subidentifier, void *uctx, bool is_last)
Decode an OID to a dictionary attribute.
Definition decode.c:487
#define IS_DER_TAG_CONTINUATION(_tag)
Definition decode.c:40
static ssize_t fr_der_decode_oid_and_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Decode an OID value pair.
Definition decode.c:1723
static ssize_t fr_der_decode_visible_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1375
static ssize_t fr_der_decode_generalized_time(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1220
TALLOC_CTX * ctx
Allocation context.
Definition decode.c:473
static ssize_t fr_der_decode_oid_wrapper(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1657
static ssize_t fr_der_decode_t61_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1091
fr_der_decode_t decode
Definition decode.c:56
unsigned int oid[FR_DICT_MAX_TLV_STACK]
Definition decode.c:445
static ssize_t fr_der_decode_universal_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1407
static ssize_t fr_der_decode_hdr(fr_dict_attr_t const *parent, fr_dbuff_t *in, uint8_t *tag, size_t *len, fr_der_tag_t expected))
Decode the tag and length fields of a DER encoded structure.
Definition decode.c:1849
static ssize_t fr_der_decode_ipv4_addr(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1413
#define IS_DER_LEN_MULTI_BYTE(_len)
Definition decode.c:42
static ssize_t fr_der_decode_general_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1401
fr_test_point_proto_decode_t der_tp_decode_proto
Definition decode.c:2751
static const fr_der_tag_decode_t oid_and_value_func
Definition decode.c:1835
static ssize_t fr_der_decode_proto(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, void *proto_ctx)
Definition decode.c:2687
static ssize_t fr_der_decode_ipv6_addr(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1512
static ssize_t fr_der_decode_integer(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:126
static const fr_der_tag_decode_t type_funcs[FR_TYPE_MAX]
Definition decode.c:1826
static ssize_t fr_der_decode_bitstring(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:216
static ssize_t fr_der_decode_choice(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Decode a CHOICE type This is where the actual decoding of the CHOICE type happens.
Definition decode.c:2019
fr_der_tag_constructed_t constructed
Definition decode.c:55
static ssize_t fr_der_decode_octetstring(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:360
static ssize_t fr_der_decode_ia5_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1132
ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:2409
static ssize_t fr_der_decode_null(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:415
#define fr_der_decode_enumerated
Definition decode.c:1399
fr_pair_list_t * parent_list
Parent pair list.
Definition decode.c:475
static ssize_t fr_der_decode_oid_to_stack(uint64_t subidentifier, void *uctx, UNUSED bool is_last)
Decode an OID to an exploded list.
Definition decode.c:457
static ssize_t fr_der_decode_x509_extensions(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dbuff_t *in, fr_dict_attr_t const *parent, fr_der_decode_ctx_t *decode_ctx)
Decode an X509 Extentions Field.
Definition decode.c:2078
static ssize_t fr_der_decode_utf8_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:710
static ssize_t fr_der_decode_printable_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1077
static ssize_t fr_der_decode_oid(fr_dbuff_t *in, fr_der_decode_oid_t func, void *uctx)
Decode an OID from a DER encoded buffer using a callback.
Definition decode.c:563
static ssize_t fr_der_decode_utc_time(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1144
static ssize_t fr_der_decode_ipv4_prefix(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1461
#define IS_DER_TAG_CONSTRUCTED(_tag)
Definition decode.c:41
fr_dict_attr_t const * parent_da
Parent dictionary attribute.
Definition decode.c:474
ssize_t(* fr_der_decode_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:51
static ssize_t fr_der_decode_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, bool const allowed_chars[], fr_der_decode_ctx_t *decode_ctx))
Function signature for DER decode functions.
static ssize_t fr_der_decode_set(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:883
static ssize_t fr_der_decode_ipv6_prefix(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1560
static const fr_der_tag_decode_t tag_funcs[FR_DER_TAG_VALUE_MAX]
Definition decode.c:1802
static ssize_t fr_der_decode_boolean(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:76
fr_test_point_pair_decode_t der_tp_decode_pair
Definition decode.c:2745
static ssize_t fr_der_decode_combo_ip_addr(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, UNUSED fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:1611
static ssize_t fr_der_decode_sequence(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:720
static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, NDEBUG_UNUSED fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx)
Definition decode.c:489
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
static char timestr[50]
Definition radsniff.c:54
static uint32_t mask
Definition rbmonkey.c:39
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static SBUFF_CHAR_CLASS], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1818
#define SBUFF_CHAR_CLASS
Definition sbuff.h:203
#define FR_SBUFF_OUT(_start, _len_or_end)
fr_aka_sim_id_type_t type
fr_pair_t * vp
ssize_t fr_struct_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value, fr_pair_decode_value_t decode_tlv)
Convert a STRUCT to one or more VPs.
Definition struct.c:32
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:86
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:68
Entry point for pair decoders.
Definition test_point.h:85
Entry point for protocol decoders.
Definition test_point.h:67
fr_unix_time_t fr_unix_time_from_tm(struct tm *tm)
Definition time.c:628
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_unix_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:324
#define PAIR_ALLOCED(_x)
Definition pair.h:212
size_t fr_pair_list_num_elements(fr_pair_list_t const *list)
Get the length of a list of fr_pair_t.
static fr_slen_t parent
Definition pair.h:858
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:41
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_type_is_group(_x)
Definition types.h:376
#define fr_type_is_octets(_x)
Definition types.h:349
#define fr_type_is_variable_size(_x)
Definition types.h:388
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
#define fr_type_is_date(_x)
Definition types.h:369
#define fr_type_is_string(_x)
Definition types.h:348
#define fr_type_is_bool(_x)
Definition types.h:358
#define fr_type_is_tlv(_x)
Definition types.h:372
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_is_struct(_x)
Definition types.h:373
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4408
static fr_slen_t data
Definition value.h:1340
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030