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