The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
encode.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 4f9566e7ebcc4d7873ff2955f9d885b5b813dddc $
19 *
20 * @file protocols/der/encode.c
21 * @brief Functions to encode DER
22 *
23 * @copyright 2025 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: 4f9566e7ebcc4d7873ff2955f9d885b5b813dddc $")
26
27#include <freeradius-devel/util/dbuff.h>
28#include <freeradius-devel/util/encode.h>
29#include <freeradius-devel/util/struct.h>
30
31#include <freeradius-devel/io/test_point.h>
32
33#include "der.h"
34
35extern fr_dict_attr_t const *attr_oid_tree;
36
37typedef struct {
38 uint8_t *tmp_ctx; //!< Temporary context for encoding.
40
41/** Function signature for DER encode functions
42 *
43 * @param[in] dbuff Where to encode the data.
44 * @param[in] cursor Where to encode the data from.
45 * @param[in] encode_ctx Any encode specific data.
46 * @return
47 * - > 0 on success. How many bytes were encoded.
48 * - 0 no bytes encoded.
49 * - < 0 on error. May be the offset (as a negative value) where the error occurred.
50 */
52
57
58
61
62/*
63 * We have per-type function names to make it clear that different types have different encoders.
64 * However, the methods to encode them are the same. So rather than having trampoline functions, we just
65 * use defines.
66 */
67#define fr_der_encode_enumerated fr_der_encode_integer
68
69
70static ssize_t fr_der_encode_len(fr_dbuff_t *dbuff, fr_dbuff_marker_t *length_start) CC_HINT(nonnull);
71static inline CC_HINT(always_inline) ssize_t
73 fr_der_tag_constructed_t constructed) CC_HINT(nonnull);
74static ssize_t encode_value(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx);
75
76/** Compare two pairs by their tag number.
77 *
78 * @param[in] a First pair.
79 * @param[in] b Second pair.
80 * @return -1 if a < b, 0 if a == b, 1 if a > b.
81 */
82static inline CC_HINT(always_inline) int8_t fr_der_pair_cmp_by_da_tag(void const *a, void const *b)
83{
84 fr_pair_t const *my_a = a;
85 fr_pair_t const *my_b = b;
86
88}
89
90static ssize_t encode_pair(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth, fr_dcursor_t *cursor,
91 void *encode_ctx)
92{
93 return encode_value(dbuff, cursor, encode_ctx);
94}
95
97{
98 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
99 fr_pair_t const *vp;
101
102 vp = fr_dcursor_current(cursor);
104 fr_assert(!vp->da->flags.is_raw);
105
106 /*
107 * ISO/IEC 8825-1:2021
108 * 8.2 Encoding of a boolean value
109 * 8.2.1 The encoding of a boolean value shall be primitive.
110 * The contents octets shall consist of a single octet.
111 * 8.2.2 If the boolean value is:
112 * FALSE the octet shall be zero [0x00].
113 * If the boolean value is TRUE the octet shall have any non-zero value, as a sender's option.
114 *
115 * 11.1 Boolean values
116 * If the encoding represents the boolean value TRUE, its single contents octet shall have all
117 * eight bits set to one [0xFF]. (Contrast with 8.2.2.)
118 */
119 value = vp->vp_bool;
120
122
123 return fr_dbuff_set(dbuff, &our_dbuff);
124}
125
127{
128 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
129 fr_pair_t const *vp;
130 uint64_t value;
131 uint8_t first_octet = 0;
132 size_t i, len;
133
134 vp = fr_dcursor_current(cursor);
136 fr_assert(!vp->da->flags.is_raw);
137
138 /*
139 * ISO/IEC 8825-1:2021
140 * 8.3 Encoding of an integer value
141 * 8.3.1 The encoding of an integer value shall be primitive.
142 * The contents octets shall consist of one or more octets.
143 * 8.3.2 If the contents octets of an integer value encoding consist of more than one octet,
144 * then the bits of the first octet and bit 8 of the second octet:
145 * a) shall not all be ones; and
146 * b) shall not all be zero.
147 * NOTE - These rules ensure that an integer value is always encoded in the smallest possible number
148 * of octets. 8.3.3 The contents octets shall be a two's complement binary number equal to the
149 * integer value, and consisting of bits 8 to 1 of the first octet, followed by bits 8 to 1 of the
150 * second octet, followed by bits 8 to 1 of each octet in turn up to and including the last octet of
151 * the contents octets.
152 */
153
154 /*
155 * Some 'integer' types such as serialNumber are too
156 * large for 64-bits. So we just treat them as octet
157 * strings.
158 */
159 if (vp->da->type != FR_TYPE_INT64) {
160 fr_assert(vp->da->type == FR_TYPE_OCTETS);
161 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, vp->vp_octets, vp->vp_length);
162 return fr_dbuff_set(dbuff, &our_dbuff);
163 }
164
165 /*
166 * Yes, the type is FR_TYPE_INT64. But we encode the
167 * data as-is, without caring about things like signed
168 * math.
169 */
170 value = vp->vp_uint64;
171
172 for (i = 0, len = 0; i < sizeof(value); i++) {
173 uint8_t byte = (value >> 56) & 0xff;
174
175 value <<= 8;
176
177 if (len == 0) {
178 first_octet = byte;
179 len++;
180 continue;
181
182 } else if (len == 1) {
183 /*
184 * 8.3.2 If the contents octets of an integer value encoding consist of more than one
185 * octet, then the bits of the first octet and bit 8 of the second octet: a) shall not all
186 * be ones; and b) shall not all be zero.
187 */
188 if ((first_octet == 0xff && (byte & 0x80)) || ((first_octet == 0x00) && (byte >> 7 == 0))) {
189 if (i == sizeof(value) - 1) {
190 /*
191 * If this is the only byte, then we can encode it as a single byte.
192 */
193 FR_DBUFF_IN_RETURN(&our_dbuff, byte);
194 continue;
195 }
196
197 first_octet = byte;
198 continue;
199
200 } else {
201 FR_DBUFF_IN_RETURN(&our_dbuff, first_octet);
202 FR_DBUFF_IN_RETURN(&our_dbuff, byte);
203 len++;
204 continue;
205 }
206 }
207
208 FR_DBUFF_IN_RETURN(&our_dbuff, byte);
209 len++;
210 }
211
212 return fr_dbuff_set(dbuff, &our_dbuff);
213}
214
216{
217 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
218 fr_pair_t const *vp;
219 ssize_t slen;
220 uint8_t unused_bits = 0;
221
222 vp = fr_dcursor_current(cursor);
224 fr_assert(!vp->da->flags.is_raw);
225
226 /*
227 * ISO/IEC 8825-1:2021
228 * 8.6 Encoding of a bitstring value
229 * 8.6.1 The encoding of a bitstring value shall be either primitive or constructed at the option
230 * of the sender.
231 * NOTE - Where it is necessary to transfer part of a bit string before the entire
232 * bitstring is available, the constructed encoding is used.
233 * 8.6.2 The contents octets for the primitive encoding shall contain an initial octet followed
234 * by zero, one or more subsequent octets.
235 * 8.6.2.1 The bits in the bitstring value, commencing with the leading bit and proceeding
236 * to the trailing bit, shall be placed in bits 8 to 1 of the first subsequent
237 * octet, followed by bits 8 to 1 of the second subsequent octet, followed by bits
238 * 8 to 1 of each octet in turn, followed by as many bits as are needed of the
239 * final subsequent octet, commencing with bit 8.
240 * NOTE - The terms "leading bit" and "trailing bit" are defined in
241 * Rec. ITU-T X.680 | ISO/IEC 8824-1, 22.2.
242 * 8.6.2.2 The initial octet shall encode, as an unsigned binary integer with bit 1 as the
243 * least significant bit, the number of unused bits in the final subsequent octet.
244 * The number shall be in the range zero to seven.
245 * 8.6.2.3 If the bitstring is empty, there shall be no subsequent octets, and the initial
246 * octet shall be zero.
247 *
248 * 10.2 String encoding forms
249 * For bitstring, octetstring and restricted character string types, the constructed form of
250 * encoding shall not be used. (Contrast with 8.23.6.)
251 *
252 * 11.2 Unused bits 11.2.1 Each unused bit in the final octet of the encoding of a bit string value shall
253 * be set to zero.
254 */
255
256 if (fr_type_is_struct(vp->vp_type)) {
257 /*
258 * For struct type, we need to encode the struct as a bitstring using the
259 * fr_struct_to_network function.
260 */
261 unsigned int depth = vp->da->depth - 1;
262 fr_da_stack_t da_stack;
263 fr_dbuff_t work_dbuff = FR_DBUFF(&our_dbuff);
264 fr_dbuff_marker_t unused_bits_marker;
265 uint8_t last_byte = 0;
266
267 fr_dbuff_marker(&unused_bits_marker, &work_dbuff);
268 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
269
270 fr_proto_da_stack_build(&da_stack, vp->da);
271
272 FR_PROTO_STACK_PRINT(&da_stack, depth);
273
274 slen = fr_struct_to_network(&work_dbuff, &da_stack, depth, cursor, encode_ctx, NULL, NULL);
275 if (slen < 0) {
276 fr_strerror_printf("Failed to encode struct: %s", fr_strerror());
277 return slen;
278 }
279
280 /*
281 * We need to trim any empty trailing octets
282 */
283 while ((slen > 1) && (fr_dbuff_current(&work_dbuff) != fr_dbuff_start(&work_dbuff))) {
284 uint8_t byte;
285
286 /*
287 * Move the dbuff cursor back by one byte
288 */
289 fr_dbuff_set(&work_dbuff, fr_dbuff_current(&work_dbuff) - sizeof(byte));
290
291 if (fr_dbuff_out(&byte, &work_dbuff) < 0) {
292 fr_strerror_const("Failed to read byte");
293 return -1;
294 }
295
296 if (byte != 0) break;
297
298 /*
299 * Trim this byte from the buff
300 */
301 fr_dbuff_set_end(&work_dbuff, fr_dbuff_current(&work_dbuff) - sizeof(byte));
302 fr_dbuff_set(&work_dbuff, fr_dbuff_current(&work_dbuff) - (sizeof(byte) * 2));
303 slen--;
304 }
305
306 /*
307 * Grab the last octet written to the dbuff and count the number of trailing 0 bits
308 */
309 if (fr_dbuff_out(&last_byte, &work_dbuff) < 0) {
310 fr_strerror_const("Failed to read last byte");
311 return -1;
312 }
313
314 while ((last_byte != 0) && ((last_byte & 0x01) == 0)) {
315 unused_bits++;
316 last_byte >>= 1;
317 }
318
319 /*
320 * Write the unused bits
321 */
322 fr_dbuff_set(&our_dbuff, fr_dbuff_current(&unused_bits_marker));
323 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, &unused_bits, 1);
324
325 /*
326 * Copy the work dbuff to the output dbuff
327 */
328 fr_dbuff_set(&work_dbuff, &our_dbuff);
329 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, &work_dbuff, (size_t)slen);
330
331 return fr_dbuff_set(dbuff, &our_dbuff);
332 }
333
334 /*
335 * For octets type, we do not need to write the unused bits portion
336 * because this information should be retained when encoding/decoding.
337 */
338 if (vp->vp_length == 0) {
339 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
340
341 } else {
342 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, vp->vp_octets, vp->vp_length);
343 }
344
345 return fr_dbuff_set(dbuff, &our_dbuff);
346}
347
349{
350 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
351 fr_pair_t const *vp;
352
353 vp = fr_dcursor_current(cursor);
355 fr_assert(!vp->da->flags.is_raw);
356
357 /*
358 * RFC3779 Section 2.1.1.
359 *
360 * An IP address or prefix is encoded in the IP address delegation
361 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
362 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
363 * STRING consists of the BIT STRING type (0x03), followed by (an
364 * encoding of) the number of value octets, followed by the value. The
365 * value consists of an "initial octet" that specifies the number of
366 * unused bits in the last value octet, followed by the "subsequent
367 * octets" that contain the octets of the bit string. (For IP
368 * addresses, the encoding of the length will be just the length.)
369 */
370
371 /*
372 * The number of unused bits in the last byte is always zero.
373 */
374 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
375 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv4addr, sizeof(vp->vp_ipv4addr));
376
377 return fr_dbuff_set(dbuff, &our_dbuff);
378}
379
381{
382 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
383 fr_pair_t const *vp;
384 size_t len;
385
386 vp = fr_dcursor_current(cursor);
388 fr_assert(!vp->da->flags.is_raw);
389
390 if (vp->vp_ip.prefix == 0) {
391 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
392 return fr_dbuff_set(dbuff, &our_dbuff);
393 }
394
395 /*
396 * RFC3779 Section 2.1.1.
397 *
398 * An IP address or prefix is encoded in the IP address delegation
399 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
400 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
401 * STRING consists of the BIT STRING type (0x03), followed by (an
402 * encoding of) the number of value octets, followed by the value. The
403 * value consists of an "initial octet" that specifies the number of
404 * unused bits in the last value octet, followed by the "subsequent
405 * octets" that contain the octets of the bit string. (For IP
406 * addresses, the encoding of the length will be just the length.)
407 */
408 if (vp->vp_ip.prefix == 32) {
409 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
410 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv4addr, sizeof(vp->vp_ipv4addr));
411 return fr_dbuff_set(dbuff, &our_dbuff);
412 }
413
414 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) ((8 - (vp->vp_ip.prefix & 0x07)) & 0x07));
415
416 len = (vp->vp_ip.prefix + 0x07) >> 3;
417
418 if (len) FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv4addr, len);
419
420 return fr_dbuff_set(dbuff, &our_dbuff);
421}
422
424{
425 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
426 fr_pair_t const *vp;
427
428 vp = fr_dcursor_current(cursor);
430 fr_assert(!vp->da->flags.is_raw);
431
432 /*
433 * RFC3779 Section 2.1.1.
434 *
435 * An IP address or prefix is encoded in the IP address delegation
436 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
437 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
438 * STRING consists of the BIT STRING type (0x03), followed by (an
439 * encoding of) the number of value octets, followed by the value. The
440 * value consists of an "initial octet" that specifies the number of
441 * unused bits in the last value octet, followed by the "subsequent
442 * octets" that contain the octets of the bit string. (For IP
443 * addresses, the encoding of the length will be just the length.)
444 */
445
446 /*
447 * The number of unused bits in the last byte is always zero.
448 */
449 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
450 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
451
452 return fr_dbuff_set(dbuff, &our_dbuff);
453}
454
456{
457 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
458 fr_pair_t const *vp;
459 size_t len;
460
461 vp = fr_dcursor_current(cursor);
463 fr_assert(!vp->da->flags.is_raw);
464
465 /*
466 * RFC3779 Section 2.1.1.
467 *
468 * An IP address or prefix is encoded in the IP address delegation
469 * extension as a DER-encoded ASN.1 BIT STRING containing the constant
470 * most-significant bits. Recall [X.690] that the DER encoding of a BIT
471 * STRING consists of the BIT STRING type (0x03), followed by (an
472 * encoding of) the number of value octets, followed by the value. The
473 * value consists of an "initial octet" that specifies the number of
474 * unused bits in the last value octet, followed by the "subsequent
475 * octets" that contain the octets of the bit string. (For IP
476 * addresses, the encoding of the length will be just the length.)
477 */
478
479 if (vp->vp_ip.prefix == 128) {
480 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
481 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
482 return fr_dbuff_set(dbuff, &our_dbuff);
483 }
484
485 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) ((8 - (vp->vp_ip.prefix & 0x07)) & 0x07));
486
487 len = (vp->vp_ip.prefix + 0x07) >> 3;
488
489 if (len) FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv6addr, len);
490
491 return fr_dbuff_set(dbuff, &our_dbuff);
492}
493
494
496{
497 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
498 fr_pair_t const *vp;
499
500 vp = fr_dcursor_current(cursor);
502 fr_assert(!vp->da->flags.is_raw);
503
504 /*
505 * RFC5280 Section 4.2.1.6
506 *
507 * When the subjectAltName extension contains an iPAddress, the address
508 * MUST be stored in the octet string in "network byte order", as
509 * specified in [RFC791]. The least significant bit (LSB) of each octet
510 * is the LSB of the corresponding byte in the network address. For IP
511 * version 4, as specified in [RFC791], the octet string MUST contain
512 * exactly four octets. For IP version 6, as specified in
513 * [RFC2460], the octet string MUST contain exactly sixteen octets.
514 */
515 if (vp->vp_ip.af == AF_INET) {
516 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv4addr, sizeof(vp->vp_ipv4addr));
517 } else {
518 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
519 }
520
521 return fr_dbuff_set(dbuff, &our_dbuff);
522}
523
524
527{
528 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
529 fr_pair_t const *vp;
530
531 vp = fr_dcursor_current(cursor);
533 /* can be raw! */
534
535 /*
536 * ISO/IEC 8825-1:2021
537 * 8.7 Encoding of an octetstring value
538 * 8.7.1 The encoding of an octetstring value shall be either primitive or constructed at the
539 * option of the sender.
540 * NOTE - Where it is necessary to transfer part of an octet string before the entire
541 * octetstring is available, the constructed encoding is used.
542 * 8.7.2 The primitive encoding contains zero, one or more contents octets equal in value to the
543 * octets in the data value, in the order they appear in the data value, and with the most
544 * significant bit of an octet of the data value aligned with the most significant bit of an
545 * octet of the contents octets.
546 * 8.7.3 The contents octets for the constructed encoding shall consist of zero, one, or more
547 * encodings.
548 * NOTE - Each such encoding includes identifier, length, and contents octets, and may
549 * include end-of-contents octets if it is constructed.
550 * 8.7.3.1 To encode an octetstring value in this way, it is segmented. Each segment shall
551 * consist of a series of consecutive octets of the value. There shall be no
552 * significance placed on the segment boundaries.
553 * NOTE - A segment may be of size zero, i.e. contain no octets.
554 *
555 * 10.2 String encoding forms
556 * For bitstring, octetstring and restricted character string types, the constructed form of
557 * encoding shall not be used. (Contrast with 8.23.6.)
558 */
559 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, vp->vp_octets, vp->vp_length);
560
561 return fr_dbuff_set(dbuff, &our_dbuff);
562}
563
564
567{
568 fr_pair_t const *vp;
569
570 vp = fr_dcursor_current(cursor);
572 fr_assert(!vp->da->flags.is_raw);
573
574 /*
575 * ISO/IEC 8825-1:2021
576 * 8.8 Encoding of a null value
577 * 8.8.1 The encoding of a null value shall be primitive.
578 * 8.8.2 The contents octets shall not contain any octets.
579 * NOTE - The length must be zero.
580 */
581 if (vp->vp_length != 0) {
582 fr_strerror_printf("Null has non-zero length %zu", vp->vp_length);
583 return -1;
584 }
585
586 return 0;
587}
588
589static ssize_t fr_der_encode_oid_from_value(fr_dbuff_t *dbuff, uint64_t value, uint64_t *component, int *count)
590{
591 fr_dbuff_t our_dbuff;
592 int i;
593 uint64_t oid;
594
595 /*
596 * The first subidentifier is the encoding of the first two object identifier components, encoded as:
597 * (X * 40) + Y
598 * where X is the first number and Y is the second number.
599 * The first number is 0, 1, or 2.
600 */
601 if (*count == 0) {
602 if (!((value == 0) || (value == 1) || (value == 2))) {
603 fr_strerror_printf("Invalid value %" PRIu64 " for initial component", value);
604 return -1;
605 }
606
607 *component = value;
608 (*count)++;
609 return 0;
610 }
611
612 if (*count == 1) {
613 if ((*component < 2) && (value > 40)) {
614 fr_strerror_printf("Invalid value %" PRIu64 " for second component", value);
615 return -1;
616 }
617
618 oid = *component * 40 + value;
619 } else {
620 oid = value;
621 }
622
623 our_dbuff = FR_DBUFF(dbuff);
624
625 /*
626 * Encode the number as 7-bit chunks. Just brute-force over all bits, as doing that ends
627 * up being fast enough.
628 *
629 * i.e. if we did anything else to count bits, it would end up with pretty much the same
630 * code.
631 */
632 for (i = 63; i >= 0; i -= 7) {
633 uint8_t more, part;
634
635 part = (oid >> i) & 0x7f;
636 if (!part) continue;
637
638 more = ((uint8_t) (i > 0)) << 7;
639
640 FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) (more | part));
641 }
642
643 (*count)++;
644
645 return fr_dbuff_set(dbuff, &our_dbuff);
646}
647
649{
650 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
651 fr_pair_t const *vp;
652 uint64_t component;
653 int i, count = 0;
654 fr_da_stack_t da_stack;
655
656 vp = fr_dcursor_current(cursor);
658 fr_assert(!vp->da->flags.is_raw);
659 fr_assert(vp->vp_type == FR_TYPE_ATTR);
660
661 fr_proto_da_stack_build(&da_stack, vp->vp_attr);
662 FR_PROTO_STACK_PRINT(&da_stack, da_stack.depth);
663
664 /*
665 * ISO/IEC 8825-1:2021
666 * 8.19 Encoding of an object identifier value
667 * 8.19.1 The encoding of an object identifier value shall be primitive.
668 * 8.19.2 The contents octets shall be an (ordered) list of encodings of subidentifiers (see 8.19.3
669 * and 8.19.4) concatenated together. Each subidentifier is represented as a series of
670 * (one or more) octets. Bit 8 of each octet indicates whether it is the last in the series: bit 8
671 * of the last octet is zero; bit 8 of each preceding octet is one. Bits 7 to 1 of the octets in
672 * the series collectively encode the subidentifier. Conceptually, these groups of bits are
673 * concatenated to form an unsigned binary number whose most significant bit is bit 7 of the first
674 * octet and whose least significant bit is bit 1 of the last octet. The subidentifier shall be
675 * encoded in the fewest possible octets, that is, the leading octet of the subidentifier shall not
676 * have the value 8016.
677 * 8.19.3 The number of subidentifiers (N) shall be one less than the number of object identifier
678 * components in the object identifier value being encoded. 8.19.4 The numerical value of the
679 * first subidentifier is derived from the values of the first two object identifier components in
680 * the object identifier value being encoded, using the formula: (X*40) + Y where X is the value
681 * of the first object identifier component and Y is the value of the second object identifier
682 * component. NOTE - This packing of the first two object identifier components recognizes that
683 * only three values are allocated from the root node, and at most 39 subsequent values from nodes
684 * reached by X = 0 and X = 1. 8.19.5 The numerical value of the ith subidentifier, (2 <= i <= N) is
685 * that of the (i + 1)th object identifier component.
686 */
687
688 /*
689 * Parse each OID component.
690 */
691 for (i = 0; i < da_stack.depth; i++) {
692 ssize_t slen;
693
694 if ((i == 0) && (da_stack.da[0] == attr_oid_tree)) continue; /* don't encode this */
695
696 slen = fr_der_encode_oid_from_value(&our_dbuff, da_stack.da[i]->attr, &component, &count);
697 if (slen < 0) return -1;
698 }
699
700 if (count <= 2) {
701 fr_strerror_printf("Invalid OID '%s' - too short", vp->vp_strvalue);
702 return -1;
703 }
704
705 return fr_dbuff_set(dbuff, &our_dbuff);
706}
707
709{
710 fr_pair_t *vp;
711
712 vp = fr_dcursor_current(cursor);
714 fr_assert(!vp->da->flags.is_raw);
715
716 fr_assert(fr_type_is_group(vp->vp_type) || fr_type_is_tlv(vp->vp_type));
717
718 /*
719 * ISO/IEC 8825-1:2021
720 * 8.9 Encoding of a sequence value
721 * 8.9.1 The encoding of a sequence value shall be constructed.
722 * 8.9.2 The contents octets shall consist of the complete encoding of one data value from each of
723 * the types listed in the ASN.1 definition of the sequence type, in the order of their
724 * appearance in the definition, unless the type was referenced with the keyword OPTIONAL
725 * or the keyword DEFAULT.
726 * 8.9.3 The encoding of a data value may, but need not, be present for a type referenced with the
727 * keyword OPTIONAL or the keyword DEFAULT. If present, it shall appear in the order of
728 * appearance of the corresponding type in the ASN.1 definition.
729 *
730 * 11.5 Set and sequence components with default value
731 * The encoding of a set value or sequence value shall not include an encoding for any component
732 * value which is equal to its default value.
733 */
735 return fr_der_encode_oid_and_value(dbuff, cursor, encode_ctx);
736 }
737
738 return fr_der_encode_choice(dbuff, cursor, encode_ctx);
739}
740
741typedef struct {
742 uint8_t *data; //!< Pointer to the start of the encoded item (beginning of the tag)
743 size_t len; //!< Length of the encoded item (tag + length + value)
745
746/*
747 * Lexicographically sort the set of pairs
748 */
749static int CC_HINT(nonnull) fr_der_encode_set_of_cmp(void const *one, void const *two)
750{
753
754 if (a->len >= b->len) {
755 return memcmp(a->data, b->data, a->len);
756 }
757
758 return memcmp(a->data, b->data, b->len);
759}
760
762{
763 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
764 fr_pair_t *vp;
765 ssize_t slen;
766
767 vp = fr_dcursor_current(cursor);
769 fr_assert(!vp->da->flags.is_raw);
770
771 fr_assert(fr_type_is_tlv(vp->vp_type));
772
773 /*
774 * ISO/IEC 8825-1:2021
775 * 8.11 Encoding of a set value
776 * 8.11.1 The encoding of a set value shall be constructed.
777 * 8.11.2 The contents octets shall consist of the complete encoding of one data value from each
778 * of the types listed in the ASN.1 definition of the set type, in an order chosen by the
779 * sender, unless the type was referenced with the keyword OPTIONAL or the keyword DEFAULT.
780 * 8.11.3 The encoding of a data value may, but need not, be present for a type referenced with the
781 * keyword OPTIONAL or the keyword DEFAULT.
782 *
783 * 11.5 Set and sequence components with default value
784 * The encoding of a set value or sequence value shall not include an encoding for any component
785 * value which is equal to its default value.
786 *
787 * ISO/IEC 8825-1:2021
788 * 8.12 Encoding of a set-of value
789 * 8.12.1 The encoding of a set-of value shall be constructed.
790 * 8.12.2 The text of 8.10.2 applies.
791 * 8.12.3 The order of data values need not be preserved by the encoding and subsequent decoding.
792 *
793 * 11.6 Set-of components
794 * The encodings of the component values of a set-of value shall appear in ascending order, the
795 * encodings being compared as octet strings with the shorter components being padded at their
796 * trailing end with 0-octets.
797 * NOTE - The padding octets are for comparison purposes only and do not appear in the
798 * encodings.
799 */
800
802 /*
803 * Set-of items will all have the same tag, so we need to sort them lexicographically
804 */
805 size_t i, count;
806 fr_dbuff_t work_dbuff;
808 uint8_t *buff;
809 fr_da_stack_t da_stack;
810 fr_dcursor_t child_cursor;
811
812 /*
813 * This can happen, but is possible.
814 */
815 count = fr_pair_list_num_elements(&vp->children);
816 if (unlikely(!count)) return 0;
817
818 /*
819 * Sets can be nested, so we have to use local buffers when sorting.
820 */
821 buff = talloc_array(encode_ctx->tmp_ctx, uint8_t, fr_dbuff_remaining(&our_dbuff));
822 fr_assert(buff != NULL);
823
824 ptr_pairs = talloc_array(buff, fr_der_encode_set_of_ptr_pairs_t, count);
825 if (unlikely(ptr_pairs == NULL)) {
826 fr_strerror_const("Failed to allocate memory for set of pointers");
828 return -1;
829 }
830
831 /*
832 * Now that we have our intermediate buffers, initialize the buffers and start encoding.
833 */
834 fr_dbuff_init(&work_dbuff, buff, fr_dbuff_remaining(&our_dbuff));
835
836 fr_proto_da_stack_build(&da_stack, vp->da);
837
838 FR_PROTO_STACK_PRINT(&da_stack, vp->da->depth - 1);
839
840 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->children, cursor);
841
842 for (i = 0; fr_dcursor_current(&child_cursor) != NULL; i++) {
843 ptr_pairs[i].data = fr_dbuff_current(&work_dbuff);
844
845 slen = encode_value(&work_dbuff, &child_cursor, encode_ctx);
846 if (unlikely(slen < 0)) {
847 fr_strerror_printf("Failed to encode pair: %s", fr_strerror());
849 return slen;
850 }
851
852 ptr_pairs[i].len = slen;
853 }
854
855 fr_assert(i <= count);
856 count = i;
857
858 /*
859 * If there's a "min" for this set, then we can't do anything about it.
860 */
861 if (unlikely(!count)) goto done;
862
863 /*
864 * If there's only one child, we don't need to sort it.
865 */
866 if (count > 1) qsort(ptr_pairs, count, sizeof(fr_der_encode_set_of_ptr_pairs_t), fr_der_encode_set_of_cmp);
867
868 /*
869 * The data in work_dbuff is always less than the data in the our_dbuff, so we don't need
870 * to check the return value here.
871 */
872 for (i = 0; i < count; i++) {
873 (void) fr_dbuff_in_memcpy(&our_dbuff, ptr_pairs[i].data, ptr_pairs[i].len);
874 }
875
876 done:
878
879 return fr_dbuff_set(dbuff, &our_dbuff);
880 }
881
882 /*
883 * Children of a set are ordered by tag. However, as each tag can only be used once, this is a
884 * unique order.
885 */
887
888 return fr_der_encode_choice(dbuff, cursor, encode_ctx);
889}
890
892{
893 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
894 fr_pair_t const *vp;
895 fr_sbuff_t time_sbuff;
896 char fmt_time[50] = { 0 };
897 size_t i;
898
899 fmt_time[0] = '\0';
900 time_sbuff = FR_SBUFF_OUT(fmt_time, sizeof(fmt_time));
901
902 vp = fr_dcursor_current(cursor);
904 fr_assert(!vp->da->flags.is_raw);
905
906 /*
907 * ISO/IEC 8825-1:2021
908 * 8.25 Encoding for values of the useful types
909 * The following "useful types" shall be encoded as if they had been replaced by their definitions
910 * given in clauses 46-48 of Rec. ITU-T X.680 | ISO/IEC 8824-1:
911 * - generalized time;
912 * - universal time;
913 * - object descriptor.
914 *
915 * 8.26 Encoding for values of the TIME type and the useful time types
916 * 8.26 Encoding for values of the TIME type and the useful time types 8.26.1 Encoding for values
917 * of the TIME type NOTE - The defined time types are subtypes of the TIME type, with the same
918 * tag, and have the same encoding as the TIME type. 8.26.1.1 The encoding of the TIME type shall
919 * be primitive. 8.26.1.2 The contents octets shall be the UTF-8 encoding of the value notation,
920 * after the removal of initial and final QUOTATION MARK (34) characters.
921 *
922 * 11.8 UTCTime
923 * 11.8.1 The encoding shall terminate with "Z", as described in the ITU-T X.680 | ISO/IEC 8824-1
924 * clause on UTCTime.
925 * 11.8.2 The seconds element shall always be present.
926 * 11.8.3 Midnight (GMT) shall be represented as "YYMMDD000000Z", where "YYMMDD" represents the
927 * day following the midnight in question.
928 */
929
930 /*
931 * The format of a UTC time is "YYMMDDhhmmssZ"
932 * Where:
933 * 1. YY is the year
934 * 2. MM is the month
935 * 3. DD is the day
936 * 4. hh is the hour
937 * 5. mm is the minute
938 * 6. ss is the second (not optional in DER)
939 * 7. Z is the timezone (UTC)
940 */
941 fr_unix_time_to_str(&time_sbuff, vp->vp_date, FR_TIME_RES_SEC, true);
942
943 /*
944 * Remove the century from the year
945 */
946 fr_sbuff_shift(&time_sbuff, 2, false);
947
948 /*
949 * Trim the time string of any unwanted characters
950 */
951 for (i = 0; i < sizeof(fmt_time); i++) {
952 if (fmt_time[i] == '\0') {
953 break;
954 }
955
956 if ((fmt_time[i] == '-') || (fmt_time[i] == 'T') || (fmt_time[i] == ':')) {
957 size_t j = i;
958
959 while (fmt_time[j] != '\0') {
960 fmt_time[j] = fmt_time[j + 1];
961 j++;
962 }
963
964 fmt_time[j] = '\0';
965
966 continue;
967 }
968 }
969
970 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, fmt_time, DER_UTC_TIME_LEN);
971
972 return fr_dbuff_set(dbuff, &our_dbuff);
973}
974
977{
978 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
979 fr_pair_t const *vp;
980 fr_sbuff_t time_sbuff;
981 char fmt_time[50] = { 0 };
982 size_t i;
983
984 fmt_time[0] = '\0';
985 time_sbuff = FR_SBUFF_OUT(fmt_time, sizeof(fmt_time));
986
987 vp = fr_dcursor_current(cursor);
989 fr_assert(!vp->da->flags.is_raw);
990
991 /*
992 * ISO/IEC 8825-1:2021
993 * 8.25 Encoding for values of the useful types
994 * The following "useful types" shall be encoded as if they had been replaced by their definitions
995 * given in clauses 46-48 of Rec. ITU-T X.680 | ISO/IEC 8824-1:
996 * - generalized time;
997 * - universal time;
998 * - object descriptor.
999 *
1000 * 8.26 Encoding for values of the TIME type and the useful time types
1001 * 8.26 Encoding for values of the TIME type and the useful time types 8.26.1 Encoding for values
1002 * of the TIME type NOTE - The defined time types are subtypes of the TIME type, with the same
1003 * tag, and have the same encoding as the TIME type. 8.26.1.1 The encoding of the TIME type shall
1004 * be primitive. 8.26.1.2 The contents octets shall be the UTF-8 encoding of the value notation,
1005 * after the removal of initial and final QUOTATION MARK (34) characters.
1006 *
1007 * 11.7 GeneralizedTime
1008 * 11.7.1 The encoding shall terminate with a "Z", as described in the Rec. ITU-T X.680 | ISO/IEC
1009 * 8824-1 clause on GeneralizedTime.
1010 * 11.7.2 The seconds element shall always be present.
1011 * 11.7.3 The fractional-seconds elements, if present, shall omit all trailing zeros; if the
1012 * elements correspond to 0, they shall be wholly omitted, and the decimal point element
1013 * also shall be omitted.
1014 */
1015
1016 /*
1017 * The format of a generalized time is "YYYYMMDDHHMMSS[.fff]Z"
1018 * Where:
1019 * 1. YYYY is the year
1020 * 2. MM is the month
1021 * 3. DD is the day
1022 * 4. HH is the hour
1023 * 5. MM is the minute
1024 * 6. SS is the second
1025 * 7. fff is the fraction of a second (optional)
1026 * 8. Z is the timezone (UTC)
1027 */
1028
1029 fr_unix_time_to_str(&time_sbuff, vp->vp_date, FR_TIME_RES_USEC, true);
1030
1031 /*
1032 * Trim the time string of any unwanted characters
1033 */
1034 for (i = 0; i < sizeof(fmt_time); i++) {
1035 if (fmt_time[i] == '\0') {
1036 break;
1037 }
1038
1039 if ((fmt_time[i] == '-') || (fmt_time[i] == 'T') || (fmt_time[i] == ':')) {
1040 size_t j = i;
1041
1042 while (fmt_time[j] != '\0') {
1043 fmt_time[j] = fmt_time[j + 1];
1044 j++;
1045 }
1046
1047 fmt_time[j] = '\0';
1048
1049 continue;
1050 }
1051
1052 if (fmt_time[i] == '.') {
1053 /*
1054 * Remove any trailing zeros
1055 */
1056 size_t j = strlen(fmt_time) - 2;
1057
1058 while (fmt_time[j] == '0') {
1059 fmt_time[j] = fmt_time[j + 1];
1060 fmt_time[j + 1] = '\0';
1061 j--;
1062 }
1063
1064 /*
1065 * Remove the decimal point if there are no fractional seconds
1066 */
1067 if (j == i) {
1068 fmt_time[i] = fmt_time[i + 1];
1069 fmt_time[i + 1] = '\0';
1070 }
1071 }
1072 }
1073
1074 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, fmt_time, i);
1075
1076 return fr_dbuff_set(dbuff, &our_dbuff);
1077}
1078
1079/** Encode a CHOICE type
1080 *
1081 * @param[in] dbuff Buffer to write the encoded data to
1082 * @param[in] cursor Cursor to the pair to encode
1083 * @param[in] encode_ctx Encoding context
1084 *
1085 * @return Number of bytes written to the buffer, or -1 on error
1086 */
1088{
1089 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
1090 fr_pair_t const *vp;
1091 fr_da_stack_t da_stack;
1092 fr_dcursor_t child_cursor;
1093 ssize_t slen = 0;
1094
1095 vp = fr_dcursor_current(cursor);
1096 PAIR_VERIFY(vp);
1097 fr_assert(!vp->da->flags.is_raw);
1098
1099 fr_proto_da_stack_build(&da_stack, vp->da);
1100
1101 FR_PROTO_STACK_PRINT(&da_stack, vp->da->depth - 1);
1102
1103 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->children, cursor);
1104
1105 slen = fr_pair_cursor_to_network(&our_dbuff, &da_stack, vp->da->depth - 1, &child_cursor, encode_ctx, encode_pair);
1106 if (slen < 0) return -1;
1107
1108 return fr_dbuff_set(dbuff, &our_dbuff);
1109}
1110
1112{
1113 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
1114 fr_dbuff_marker_t outer_seq_len_start;
1115 fr_dcursor_t child_cursor, root_cursor, parent_cursor;
1116 fr_pair_t const *vp;
1117 ssize_t slen = 0;
1118 size_t is_critical = 0;
1119 uint64_t max, num;
1120 fr_dbuff_marker_t length_start, inner_seq_len_start;
1121
1122 vp = fr_dcursor_current(cursor);
1123 PAIR_VERIFY(vp);
1124 fr_assert(!vp->da->flags.is_raw);
1125
1126 fr_assert(fr_type_is_group(vp->vp_type));
1127
1128 /*
1129 * RFC 5280 Section 4.2
1130 * The extensions defined for X.509 v3 certificates provide methods for
1131 * associating additional attributes with users or public keys and for
1132 * managing relationships between CAs. The X.509 v3 certificate format
1133 * also allows communities to define private extensions to carry
1134 * information unique to those communities. Each extension in a
1135 * certificate is designated as either critical or non-critical.
1136 *
1137 * Each extension includes an OID and an ASN.1 structure. When an
1138 * extension appears in a certificate, the OID appears as the field
1139 * extnID and the corresponding ASN.1 DER encoded structure is the value
1140 * of the octet string extnValue.
1141 *
1142 * RFC 5280 Section A.1 Explicitly Tagged Module, 1988 Syntax
1143 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
1144 *
1145 * Extension ::= SEQUENCE {
1146 * extnID OBJECT IDENTIFIER,
1147 * critical BOOLEAN DEFAULT FALSE,
1148 * extnValue OCTET STRING
1149 * -- contains the DER encoding of an ASN.1 value
1150 * -- corresponding to the extension type identified
1151 * -- by extnID
1152 * }
1153 *
1154 * So the extensions are a SEQUENCE of SEQUENCEs containing an OID, a boolean and an OCTET STRING.
1155 * Note: If the boolean value is false, it is not included in the encoding.
1156 */
1157
1158 max = fr_der_flag_max(vp->da); /* Maximum number of extensions specified in the dictionary */
1159 num = 0;
1160
1162 if (slen < 0) return slen;
1163
1164 fr_dbuff_marker(&outer_seq_len_start, &our_dbuff);
1165 FR_DBUFF_ADVANCE_RETURN(&our_dbuff, 1);
1166
1167 FR_PROTO_HEX_DUMP(fr_dbuff_start(&our_dbuff), fr_dbuff_used(&our_dbuff),"BEFORE encoded X509 extension");
1168
1169 fr_pair_dcursor_child_iter_init(&root_cursor, &vp->children, cursor);
1170 fr_dcursor_copy(&parent_cursor, &root_cursor);
1171
1172 fr_dbuff_marker(&inner_seq_len_start, &our_dbuff);
1173 fr_dbuff_marker(&length_start, &our_dbuff);
1174
1175 while (fr_dcursor_current(&parent_cursor)) {
1176 uint64_t component;
1177 int count;
1178 fr_pair_t *child;
1179
1180 /*
1181 * Extensions are sequences or sets containing 2 items:
1182 * 1. The first item is the OID
1183 * 2. The second item is the value
1184 *
1185 * Note: The value may be a constructed or primitive type
1186 */
1187
1188 if (num >= max) {
1189 fr_strerror_printf("Too many X509 extensions (%" PRIu64 ")", max);
1190 break;
1191 }
1192
1194 if (slen < 0) return slen;
1195
1196 fr_dbuff_set(&inner_seq_len_start, &our_dbuff);
1197 FR_DBUFF_ADVANCE_RETURN(&our_dbuff, 1);
1198
1199 /*
1200 * Encode the OID portion of the extension
1201 */
1203 if (slen < 0) return slen;
1204
1205 fr_dbuff_set(&length_start, &our_dbuff);
1206 FR_DBUFF_ADVANCE_RETURN(&our_dbuff, 1);
1207
1208 /*
1209 * Walk through the children until we find either an attribute marked as an extension, or one with
1210 * no children (which is an unknown OID).
1211 *
1212 * We will use this to construct the OID to encode, as well as to get the actual value of the
1213 * extension.
1214 */
1215 fr_dcursor_copy(&child_cursor, &parent_cursor);
1216 count = 0;
1217
1218 while ((child = fr_dcursor_current(&child_cursor)) != NULL) {
1219 PAIR_VERIFY(child);
1220
1221 FR_PROTO_TRACE("Child: %s", child->da->name);
1222
1223 if (!is_critical && (strcmp(child->da->name, "Critical") == 0)) {
1224 /*
1225 * We don't encode the critical flag
1226 */
1227 is_critical = fr_pair_list_num_elements(&child->children);
1228 FR_PROTO_TRACE("Critical flag: %zu", is_critical);
1229
1230 fr_pair_dcursor_child_iter_init(&parent_cursor, &child->children, &child_cursor);
1231 goto next;
1232 }
1233
1234 /*
1235 * If we find a normal leaf data type, we don't encode it. But we do encode leaf data
1236 * types which are marked up as needing OID leaf encoding.
1237 */
1238 if (!fr_type_is_structural(child->vp_type) && !fr_der_flag_leaf(child->da) && !child->da->flags.is_raw) {
1239 FR_PROTO_TRACE("Found non-structural child %s", child->da->name);
1240
1241 fr_dcursor_copy(&child_cursor, &parent_cursor);
1242 break;
1243 }
1244
1245 slen = fr_der_encode_oid_from_value(&our_dbuff, child->da->attr, &component, &count);
1246 if (unlikely(slen < 0)) return -1;
1247
1248 /*
1249 * We've encoded a leaf data type, or a raw one. Stop encoding it.
1250 */
1251 if (!fr_type_is_structural(child->vp_type)) break;
1252
1253 /*
1254 * Unless this was the last child (marked as an extension), there should only be one child
1255 * - representing the next OID in the extension
1256 */
1257 if (fr_pair_list_num_elements(&child->children) > 1) break;
1258
1259 next:
1260 if (fr_der_flag_leaf(child->da)) break;
1261
1262 fr_pair_dcursor_child_iter_init(&child_cursor, &child->children, &child_cursor);
1263 }
1264
1265 /*
1266 * Encode the length of the OID
1267 */
1268 slen = fr_der_encode_len(&our_dbuff, &length_start);
1269 if (slen < 0) return slen;
1270
1271 /*
1272 * Encode the critical flag
1273 */
1274 if (is_critical) {
1275 /*
1276 * Universal+Boolean flag is always 0x01. Length of a boolean is always 0x01.
1277 * True is always 0xff.
1278 */
1279 FR_DBUFF_IN_BYTES_RETURN(&our_dbuff, (uint8_t) 0x01, (uint8_t) 0x01, (uint8_t)(0xff));
1280 is_critical--;
1281 }
1282
1283 /*
1284 * Encode the value portion of the extension
1285 */
1287 if (slen < 0) return slen;
1288
1289 fr_dbuff_set(&length_start, &our_dbuff);
1290 FR_DBUFF_ADVANCE_RETURN(&our_dbuff, 1);
1291
1292 /*
1293 * Encode the data
1294 */
1295 slen = encode_value(&our_dbuff, &child_cursor, encode_ctx);
1296 if (slen < 0) return slen;
1297
1298 /*
1299 * Encode the length of the value
1300 */
1301 slen = fr_der_encode_len(&our_dbuff, &length_start);
1302 if (slen < 0) return slen;
1303
1304 /*
1305 * Encode the length of the extension (OID + Value portions)
1306 */
1307 slen = fr_der_encode_len(&our_dbuff, &inner_seq_len_start);
1308 if (slen < 0) return -1;
1309
1310 if (is_critical) {
1311 fr_dcursor_next(&parent_cursor);
1312 num++;
1313 continue;
1314 }
1315
1316 FR_PROTO_HEX_DUMP(fr_dbuff_start(&our_dbuff), fr_dbuff_behind(&outer_seq_len_start) + 2,
1317 "Encoded X509 extension");
1318
1319 fr_dcursor_next(&root_cursor);
1320 fr_dcursor_copy(&parent_cursor, &root_cursor);
1321 num++;
1322 }
1323
1324 /*
1325 * Encode the length of the extensions
1326 */
1327 slen = fr_der_encode_len(&our_dbuff, &outer_seq_len_start);
1328 if (slen < 0) return slen;
1329
1330 FR_PROTO_HEX_DUMP(fr_dbuff_start(&our_dbuff), slen, "Encoded X509 extensions");
1331
1332 return fr_dbuff_set(dbuff, &our_dbuff);
1333}
1334
1336{
1337 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
1338 fr_dbuff_marker_t length_start;
1339 fr_dcursor_t child_cursor, parent_cursor = *cursor;
1340 fr_pair_t const *vp, *child;
1341 ssize_t slen = 0;
1342 uint64_t component;
1343 int count;
1344
1345 vp = fr_dcursor_current(&parent_cursor);
1346 PAIR_VERIFY(vp);
1347 fr_assert(!vp->da->flags.is_raw);
1348
1349 fr_assert(fr_type_is_group(vp->vp_type));
1350
1351 /*
1352 * A very common pattern in DER encoding is ro have a sequence of set containing two things: an OID and a
1353 * value, where the OID is used to determine how to decode the value.
1354 * We will be decoding the OID first and then try to find the attribute associated with that OID to then
1355 * decode the value. If no attribute is found, one will be created and the value will be stored as raw
1356 * octets in the attribute.
1357 *
1358 * Note: The value may be a constructed or primitive type
1359 */
1360
1362 if (slen < 0) return slen;
1363
1364 fr_dbuff_marker(&length_start, &our_dbuff);
1365 FR_DBUFF_ADVANCE_RETURN(&our_dbuff, 1);
1366
1367 /*
1368 * Walk through the children until we find either an attribute marked as an oid leaf, or one with
1369 * no children (which is an unknown OID).
1370 *
1371 * We will use this to construct the OID to encode, as well as to get the actual value of the
1372 * pair.
1373 */
1374 fr_pair_dcursor_child_iter_init(&child_cursor, &vp->children, &parent_cursor);
1375 count = 0;
1376
1377 while ((child = fr_dcursor_current(&child_cursor)) != NULL) {
1378 PAIR_VERIFY(child);
1379
1380 /*
1381 * If we find a normal leaf data type, we don't encode it. But we do encode leaf data
1382 * types which are marked up as needing OID leaf encoding.
1383 */
1384 if (!fr_type_is_structural(child->vp_type) && !fr_der_flag_leaf(child->da) && !child->da->flags.is_raw) {
1385 FR_PROTO_TRACE("Found non-structural child %s", child->da->name);
1386
1387 fr_dcursor_copy(&child_cursor, &parent_cursor);
1388 break;
1389 }
1390
1391 slen = fr_der_encode_oid_from_value(&our_dbuff, child->da->attr, &component, &count);
1392 if (unlikely(slen < 0)) return -1;
1393
1394 /*
1395 * We've encoded a leaf data type, or a raw one. Stop encoding it.
1396 */
1397 if (!fr_type_is_structural(child->vp_type)) break;
1398
1399 /*
1400 * Some structural types can be marked as a leaf for the purposes of OID encoding.
1401 */
1402 if (fr_der_flag_leaf(child->da)) break;
1403
1404 /*
1405 * Unless this was the last child (marked as an oid leaf), there should only be one child
1406 * - representing the next OID in the pair
1407 */
1408 if (fr_pair_list_num_elements(&child->children) > 1) break;
1409
1410 fr_pair_dcursor_child_iter_init(&child_cursor, &child->children, &child_cursor);
1411 }
1412
1413 /*
1414 * Encode the length of the OID
1415 */
1416 slen = fr_der_encode_len(&our_dbuff, &length_start);
1417 if (slen < 0) return slen;
1418
1419 /*
1420 * And then encode the actual data.
1421 */
1422 slen = encode_value(&our_dbuff, &child_cursor, encode_ctx);
1423 if (slen < 0) return slen;
1424
1425 return fr_dbuff_set(dbuff, &our_dbuff);
1426}
1427
1429{
1430 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
1431 fr_pair_t const *vp;
1432
1433 vp = fr_dcursor_current(cursor);
1434 PAIR_VERIFY(vp);
1435 fr_assert(!vp->da->flags.is_raw);
1436
1437 /*
1438 * ISO/IEC 8825-1:2021
1439 * 8.23 Encoding for values of the restricted character string types
1440 * 8.23.1 The data value consists of a string of characters from the character set specified in
1441 * the ASN.1 type definition.
1442 * 8.23.2 Each data value shall be encoded independently of other data values of the same type.
1443 *
1444 * 10.2 String encoding forms
1445 * For bitstring, octetstring and restricted character string types, the constructed form of
1446 * encoding shall not be used. (Contrast with 8.23.6.)
1447 *
1448 * NOTE:
1449 * We DO NOT check for restricted character sets here. This should be done as a separate validation
1450 * step. Here we simply trust that administrators have done their job and are providing us with
1451 * valid data.
1452 */
1453
1454 FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, vp->vp_strvalue, vp->vp_length);
1455
1456 return fr_dbuff_set(dbuff, &our_dbuff);
1457}
1458
1461 [FR_DER_TAG_INTEGER] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_integer },
1462 [FR_DER_TAG_BITSTRING] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_bitstring },
1464 [FR_DER_TAG_NULL] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_null },
1465 [FR_DER_TAG_OID] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_oid },
1467 [FR_DER_TAG_UTF8_STRING] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_string },
1468 [FR_DER_TAG_SEQUENCE] = { .constructed = FR_DER_TAG_CONSTRUCTED, .encode = fr_der_encode_sequence },
1469 [FR_DER_TAG_SET] = { .constructed = FR_DER_TAG_CONSTRUCTED, .encode = fr_der_encode_set },
1471 .encode = fr_der_encode_string },
1472 [FR_DER_TAG_T61_STRING] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_string },
1473 [FR_DER_TAG_IA5_STRING] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_string },
1474 [FR_DER_TAG_UTC_TIME] = { .constructed = FR_DER_TAG_PRIMITIVE, .encode = fr_der_encode_utc_time },
1480 .encode = fr_der_encode_string },
1481};
1482
1491
1492
1493/** Encode the length field of a DER structure
1494 *
1495 * The input dbuff is composed of the following data:
1496 *
1497 * 1 byte of nothing (length_start). Where the "length length" will be written to.
1498 * N bytes of data. dbuff is pointing to the end of the encoded data.
1499 *
1500 * We have to either write a length to length_start (if it's < 0x7f),
1501 *
1502 * OR we figure out how many bytes we need to encode the length,
1503 * shift the data to the right to make room, and then encode the
1504 * length.
1505 *
1506 * @param dbuff The buffer to update with the length field
1507 * @param length_start The start of the length field
1508 * @return
1509 * - <0 for "cannot extend the input buffer by the needed "length length".
1510 * - 1 for "success". Note that 'length_start' WILL be updated after this call,
1511 * and the caller should just release it immediately.
1512 */
1514{
1515 size_t i, len_len;
1516 size_t tmp, datalen;
1517
1518 datalen = fr_dbuff_current(dbuff) - fr_dbuff_current(length_start) - 1;
1519
1520 /*
1521 * If the length can fit in a single byte, we don't need to extend the size of the length field
1522 */
1523 if (datalen <= 0x7f) {
1524 (void) fr_dbuff_in(length_start, (uint8_t) datalen);
1525 return 1;
1526 }
1527
1528 /*
1529 * Calculate the number of bytes needed to encode the length.
1530 */
1531 for (tmp = datalen, len_len = 0; tmp != 0; tmp >>= 8) {
1532 len_len++;
1533 }
1534
1535 /*
1536 * DER says that the length field cannot be more than
1537 * 0x7f. Since sizeof(datalen) == 8, we can always
1538 * encode the length field.
1539 */
1540 fr_assert(len_len > 0);
1541 fr_assert(len_len < 0x7f);
1542
1543 (void) fr_dbuff_in(length_start, (uint8_t) (0x80 | len_len));
1544
1545 /*
1546 * This is the only operation which can fail. The dbuff
1547 * is currently set to the end of the encoded data. We
1548 * need to ensure that there is sufficient room in the
1549 * dbuff to encode the additional bytes.
1550 *
1551 * fr_dbuff_set() checks if the length exceeds the input
1552 * buffer. But it does NOT extend the buffer by reading
1553 * more data, if more data is needed. So we need to
1554 * manually extend the dbuff here.
1555 */
1556 FR_DBUFF_EXTEND_LOWAT_OR_RETURN(dbuff, len_len);
1557
1558 /*
1559 * Reset the dbuff to the new start, where the data
1560 * should be.
1561 */
1562 fr_dbuff_set(dbuff, fr_dbuff_current(length_start) + len_len);
1563
1564 /*
1565 * Move the data over. Note that the move updates BOTH
1566 * input and output dbuffs. As a result, we have to wrap
1567 * 'length_start' in a temporary dbuff, so that it
1568 * doesn't get updated by the move.
1569 */
1570 fr_dbuff_move(dbuff, &FR_DBUFF(length_start), datalen);
1571
1572 /*
1573 * Encode high bits first, but only the non-zero ones.
1574 */
1575 for (i = len_len; i > 0; i--) {
1576 (void) fr_dbuff_in(length_start, (uint8_t)((datalen) >> ((i - 1) * 8)));
1577 }
1578
1579 return 1;
1580}
1581
1582/** Encode a DER tag
1583 *
1584 * @param dbuff The buffer to write the tag to
1585 * @param tag_num The tag number
1586 * @param tag_class The tag class
1587 * @param constructed Whether the tag is constructed
1588 *
1589 * @return The number of bytes written to the buffer
1590 */
1591static inline CC_HINT(always_inline) ssize_t
1593 fr_der_tag_constructed_t constructed)
1594{
1595 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
1596 uint8_t tag_byte;
1597
1598 tag_byte = (tag_class & DER_TAG_CLASS_MASK) | (constructed & DER_TAG_CONSTRUCTED_MASK) |
1599 (tag_num & DER_TAG_NUM_MASK);
1600
1601 FR_DBUFF_IN_RETURN(&our_dbuff, tag_byte);
1602
1603 return fr_dbuff_set(dbuff, &our_dbuff);
1604}
1605
1606/** Encode a DER structure
1607 *
1608 * @param[out] dbuff The buffer to write the structure to
1609 * @param[in] cursor The cursor to the structure to encode
1610 * @param[in] encode_ctx The encoding context
1611 *
1612 * @return The number of bytes written to the buffer
1613 */
1615{
1616 fr_pair_t const *vp;
1617 fr_dbuff_t our_dbuff;
1618 fr_dbuff_marker_t marker;
1619 fr_der_tag_encode_t const *func;
1620 fr_der_tag_t tag;
1621 fr_der_tag_class_t tag_class;
1623 ssize_t slen = 0;
1624 fr_der_attr_flags_t const *flags;
1625
1626 if (unlikely(cursor == NULL)) {
1627 fr_strerror_const("No cursor to encode");
1628 return -1;
1629 }
1630
1631 vp = fr_dcursor_current(cursor);
1632 if (unlikely(!vp)) {
1633 fr_strerror_const("No pair to encode");
1634 return -1;
1635 }
1636
1637 PAIR_VERIFY(vp);
1638
1639 FR_PROTO_TRACE("Encoding %s", vp->da->name);
1640
1641 flags = fr_der_attr_flags(vp->da);
1642 fr_assert(flags != NULL);
1643
1644 /*
1645 * Raw things get encoded as-is, so that we can encode the correct tag and class.
1646 */
1647 if (unlikely(vp->da->flags.is_raw)) {
1648 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
1649
1650 slen = fr_der_encode_octetstring(dbuff, cursor, encode_ctx);
1651 if (slen < 0) return 0;
1652
1653 fr_dcursor_next(cursor);
1654 return slen;
1655 }
1656
1657 our_dbuff = FR_DBUFF(dbuff);
1658
1659 /*
1660 * ISO/IEC 8825-1:2021
1661 * The structure of a DER encoding is as follows:
1662 *
1663 * +------------+--------+-------+
1664 * | IDENTIFIER | LENGTH | VALUE |
1665 * +------------+--------+-------+
1666 *
1667 * The IDENTIFIER is a tag that specifies the type of the value field and is encoded as follows:
1668 *
1669 * 8 7 6 5 4 3 2 1
1670 * +---+---+-----+---+---+---+---+---+
1671 * | Class | P/C | Tag Number |
1672 * +---+---+-----+---+---+---+---+---+
1673 * |
1674 * |- 0 = Primitive
1675 * |- 1 = Constructed
1676 *
1677 * The CLASS field specifies the encoding class of the tag and may be one of the following values:
1678 *
1679 * +------------------+-------+-------+
1680 * | Class | Bit 8 | Bit 7 |
1681 * +------------------+-------+-------+
1682 * | UNIVERSAL | 0 | 0 |
1683 * | APPLICATION | 0 | 1 |
1684 * | CONTEXT-SPECIFIC | 1 | 0 |
1685 * | PRIVATE | 1 | 1 |
1686 * +------------------+-------+-------+
1687 *
1688 * The P/C field specifies whether the value field is primitive or constructed.
1689 * The TAG NUMBER field specifies the tag number of the value field and is encoded as an unsigned binary
1690 * integer.
1691 *
1692 * The LENGTH field specifies the length of the VALUE field and is encoded as an unsigned binary integer
1693 * and may be encoded as a single byte or multiple bytes.
1694 *
1695 * The VALUE field contains LENGTH number of bytes and is encoded according to the tag.
1696 *
1697 */
1698
1699 if (flags->has_default_value) {
1700 /*
1701 * Skip encoding the default value, as per ISO/IEC 8825-1:2021 11.5
1702 */
1703 if (fr_value_box_cmp(&vp->data, flags->default_value) == 0) {
1704 FR_PROTO_TRACE("Skipping default value");
1705 fr_dcursor_next(cursor);
1706 return 0;
1707 }
1708 }
1709
1710 if (unlikely(flags->is_choice)) {
1711 slen = fr_der_encode_choice(&our_dbuff, cursor, uctx);
1712 if (slen < 0) return slen;
1713
1714 fr_dcursor_next(cursor);
1715 return fr_dbuff_set(dbuff, &our_dbuff);
1716 }
1717
1718 tag = flags->der_type;
1719 if (!tag) tag = fr_type_to_der_tag_default(vp->vp_type);
1720
1721 if (unlikely(tag == FR_DER_TAG_INVALID)) {
1722 fr_strerror_printf("No tag defined for type %s", fr_type_to_str(vp->vp_type));
1723 return -1;
1724 }
1725
1727
1728 func = &type_funcs[vp->vp_type];
1729 if (!func->encode) func = &tag_funcs[tag];
1730
1731 fr_assert(func != NULL);
1732 fr_assert(func->encode != NULL);
1733
1734 /*
1735 * Default flag class is 0, which is FR_DER_CLASS_UNIVERSAL.
1736 */
1737 tag_class = flags->class;
1738
1739 /*
1740 * We call the DER type encoding function based on its
1741 * tag, but we might need to encode an option value
1742 * instead of a tag.
1743 */
1744 if (flags->is_option) tag = flags->option;
1745
1746 slen = fr_der_encode_tag(&our_dbuff, tag, tag_class, func->constructed);
1747 if (slen < 0) return slen;
1748
1749 /*
1750 * Mark and reserve space in the buffer for the length field
1751 */
1752 fr_dbuff_marker(&marker, &our_dbuff);
1753 FR_DBUFF_ADVANCE_RETURN(&our_dbuff, 1);
1754
1755 if (flags->is_extensions) {
1756 slen = fr_der_encode_X509_extensions(&our_dbuff, cursor, uctx);
1757 } else {
1758
1759 slen = func->encode(&our_dbuff, cursor, uctx);
1760 }
1761 if (slen < 0) return slen;
1762
1763 /*
1764 * Encode the length of the value
1765 */
1766 slen = fr_der_encode_len(&our_dbuff, &marker);
1767 if (slen < 0) return slen;
1768
1769 fr_dcursor_next(cursor);
1770 return fr_dbuff_set(dbuff, &our_dbuff);
1771}
1772
1773static ssize_t fr_der_encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len,
1774 void *encode_ctx)
1775{
1776 fr_dbuff_t dbuff;
1777 fr_dcursor_t cursor;
1778 ssize_t slen;
1779
1780 fr_dbuff_init(&dbuff, data, data_len);
1781
1782 fr_pair_dcursor_init(&cursor, vps);
1783
1784 slen = encode_value(&dbuff, &cursor, encode_ctx);
1785
1786 if (slen < 0) {
1787 fr_strerror_printf("Failed to encode data: %s", fr_strerror());
1788 return -1;
1789 }
1790
1791 return slen;
1792}
1793
1794/*
1795 * Test points
1796 */
1797static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict,
1798 UNUSED fr_dict_attr_t const *root_da)
1799{
1800 fr_der_encode_ctx_t *test_ctx;
1801
1802 test_ctx = talloc_zero(ctx, fr_der_encode_ctx_t);
1803 if (!test_ctx) return -1;
1804
1805 test_ctx->tmp_ctx = talloc(test_ctx, uint8_t);
1806
1807 *out = test_ctx;
1808
1809 return 0;
1810}
1811
1817
#define RCSID(id)
Definition build.h:488
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition build.h:105
#define unlikely(_x)
Definition build.h:384
#define UNUSED
Definition build.h:318
#define fr_dbuff_behind(_dbuff_or_marker)
How many bytes the dbuff or marker is behind its parent.
Definition dbuff.h:794
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:775
#define FR_DBUFF_ADVANCE_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.
Definition dbuff.h:1097
#define FR_DBUFF_EXTEND_LOWAT_OR_RETURN(_dbuff_or_marker, _lowat)
Extend if we're below _lowat and return if we can't extend above _lowat.
Definition dbuff.h:681
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:919
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1012
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:362
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:906
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
Definition dbuff.h:751
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition dbuff.h:1201
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1391
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1359
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1576
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.
Definition dbuff.h:1594
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
#define fr_dbuff_move(_out, _in, _len)
Copy in as many bytes as possible from one dbuff or marker to another.
Definition dbuff.h:1665
#define fr_dbuff_out(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type.
Definition dbuff.h:1808
#define fr_dbuff_set_end(_dst, _end)
Set a new 'end' position in a dbuff or marker.
Definition dbuff.h:1059
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1481
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void fr_dcursor_copy(fr_dcursor_t *out, fr_dcursor_t const *in)
Copy cursor parameters and state.
Definition dcursor.h:190
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
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_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
@ FR_DER_TAG_MAX
Definition der.h:58
bool is_extensions
a list of X.509 extensions
Definition der.h:111
#define fr_der_flag_der_type(_da)
Definition der.h:130
fr_der_tag_t der_type
the DER type, which is different from the FreeRADIUS type
Definition der.h:95
bool is_option
has an option defined
Definition der.h:106
#define DER_BOOLEAN_TRUE
DER encoded boolean true value.
Definition der.h:91
#define fr_der_flag_is_oid_and_value(_da)
Definition der.h:136
#define fr_der_flag_leaf(_da)
Definition der.h:139
#define DER_BOOLEAN_FALSE
DER encoded boolean false value.
Definition der.h:90
#define fr_der_flag_is_set_of(_da)
Definition der.h:134
#define DER_TAG_CLASS_MASK
Mask to extract the class from the tag.
Definition der.h:82
#define fr_der_flag_max(_da)
Definition der.h:135
#define DER_UTC_TIME_LEN
Length of the UTC time string.
Definition der.h:78
fr_der_tag_constructed_t
Definition der.h:63
@ FR_DER_TAG_CONSTRUCTED
This is a sequence or set, it contains children.
Definition der.h:65
@ FR_DER_TAG_PRIMITIVE
This is a leaf value, it contains no children.
Definition der.h:64
#define DER_TAG_NUM_MASK
Mask to extract the tag number from the tag.
Definition der.h:84
static fr_der_attr_flags_t const * fr_der_attr_flags(fr_dict_attr_t const *da)
Definition der.h:122
bool is_choice
DER name "choice".
Definition der.h:115
uint8_t option
an "attribute number" encoded in the tag field.
Definition der.h:105
#define DER_TAG_CONSTRUCTED_MASK
Mask to check if the tag is constructed.
Definition der.h:83
bool has_default_value
a default value exists
Definition der.h:112
fr_der_tag_class_t class
tag Class
Definition der.h:94
fr_der_tag_class_t
Definition der.h:68
@ FR_DER_CLASS_UNIVERSAL
Definition der.h:69
Test enumeration values.
Definition dict_test.h:92
talloc_free(hp)
static ssize_t encode_value(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encodes the data portion of an attribute.
Definition encode.c:277
ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_pair)
Definition encode.c:73
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_OCTETS
Raw octets.
long int ssize_t
unsigned char uint8_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
static fr_internal_encode_ctx_t encode_ctx
static int encode_test_ctx(void **out, TALLOC_CTX *ctx, UNUSED fr_dict_t const *dict, UNUSED fr_dict_attr_t const *root_da)
Definition encode.c:165
static ssize_t encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Definition base.c:39
fr_der_tag_t fr_type_to_der_tag_default(fr_type_t type)
Definition base.c:892
static ssize_t fr_der_encode_ipv4_addr(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:348
static ssize_t fr_der_encode_sequence(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:708
uint8_t * tmp_ctx
Temporary context for encoding.
Definition encode.c:38
static ssize_t fr_der_encode_octetstring(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:525
static const fr_der_tag_encode_t tag_funcs[FR_DER_TAG_MAX]
Definition encode.c:1459
static int fr_der_encode_set_of_cmp(void const *one, void const *two)
Definition encode.c:749
static ssize_t fr_der_encode_choice(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Encode a CHOICE type.
Definition encode.c:1087
ssize_t(* fr_der_encode_t)(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Function signature for DER encode functions.
Definition encode.c:51
static ssize_t fr_der_encode_ipv6_addr(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:423
static ssize_t fr_der_encode_oid(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:648
static ssize_t fr_der_encode_null(UNUSED fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:565
static ssize_t fr_der_encode_boolean(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:96
static ssize_t fr_der_encode_oid_and_value(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:1335
static ssize_t fr_der_encode_oid_from_value(fr_dbuff_t *dbuff, uint64_t value, uint64_t *component, int *count)
Definition encode.c:589
static ssize_t fr_der_encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, void *encode_ctx)
Definition encode.c:1773
static ssize_t fr_der_encode_generalized_time(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:975
size_t len
Length of the encoded item (tag + length + value)
Definition encode.c:743
static ssize_t fr_der_encode_len(fr_dbuff_t *dbuff, fr_dbuff_marker_t *length_start)
Encode the length field of a DER structure.
Definition encode.c:1513
#define fr_der_encode_enumerated
Definition encode.c:67
fr_dict_attr_t const * attr_oid_tree
Definition base.c:39
static ssize_t fr_der_encode_string(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:1428
fr_der_tag_constructed_t constructed
Definition encode.c:54
static ssize_t fr_der_encode_X509_extensions(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:1111
static int8_t fr_der_pair_cmp_by_da_tag(void const *a, void const *b)
Compare two pairs by their tag number.
Definition encode.c:82
static const fr_der_tag_encode_t type_funcs[FR_TYPE_MAX]
Definition encode.c:1483
static ssize_t fr_der_encode_combo_ip(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:495
static ssize_t fr_der_encode_set(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:761
fr_test_point_pair_encode_t der_tp_encode_pair
Definition encode.c:1813
static ssize_t fr_der_encode_bitstring(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:215
fr_test_point_proto_encode_t der_tp_encode_proto
Definition encode.c:1819
static ssize_t fr_der_encode_tag(fr_dbuff_t *dbuff, fr_der_tag_t tag_num, fr_der_tag_class_t tag_class, fr_der_tag_constructed_t constructed)
Encode a DER tag.
Definition encode.c:1592
static ssize_t fr_der_encode_ipv6_prefix(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:455
static ssize_t fr_der_encode_integer(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:126
fr_der_encode_t encode
Definition encode.c:55
static ssize_t fr_der_encode_utc_time(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:891
static ssize_t fr_der_encode_ipv4_prefix(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
Definition encode.c:380
uint8_t * data
Pointer to the start of the encoded item (beginning of the tag)
Definition encode.c:742
#define fr_assert(_expr)
Definition rad_assert.h:37
static bool done
Definition radclient.c:80
size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift, bool move_end)
Shift the contents of the sbuff, returning the number of bytes we managed to shift.
Definition sbuff.c:200
#define FR_SBUFF_OUT(_start, _len_or_end)
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
return count
Definition module.c:155
fr_pair_t * vp
ssize_t fr_struct_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *parent_cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:753
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:94
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:76
Entry point for pair encoders.
Definition test_point.h:93
Entry point for protocol encoders.
Definition test_point.h:75
fr_slen_t fr_unix_time_to_str(fr_sbuff_t *out, fr_unix_time_t time, fr_time_res_t res, bool utc)
Convert unix time to string.
Definition time.c:1122
@ FR_TIME_RES_USEC
Definition time.h:59
@ FR_TIME_RES_SEC
Definition time.h:50
static fr_pair_t * fr_pair_dcursor_child_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dcursor_t const *parent)
Initializes a child dcursor from a parent cursor, with an iteration function.
Definition pair.h:622
#define PAIR_VERIFY(_x)
Definition pair.h:204
void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:604
size_t fr_pair_list_num_elements(fr_pair_list_t const *list)
Get the length of a list of fr_pair_t.
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:41
#define FR_PROTO_STACK_PRINT(_stack, _depth)
Definition proto.h:44
uint8_t depth
Deepest attribute in the stack.
Definition proto.h:56
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:57
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:55
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_type_is_group(_x)
Definition types.h:376
#define fr_type_is_structural(_x)
Definition types.h:392
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
#define fr_type_is_tlv(_x)
Definition types.h:372
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define fr_type_is_struct(_x)
Definition types.h:373
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:748
static fr_slen_t data
Definition value.h:1340
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030