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