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