Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/asn1_par.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5 #include <openssl/bio.h>
6 #include "../internal.h"
7
8 // Forward declarations
9 static int asn1_parse2(BIO *bp, const uint8_t **pp, long len, long offset,
10 int depth, int indent, int dump);
11 static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
12 int indent);
13 static int asn1_parse_constructed_type(BIO *bp, const uint8_t **current_pos,
14 const uint8_t *total_end,
15 const uint8_t *original_start,
16 long *object_len, int parse_flags,
17 long offset, int depth, int indent,
18 int dump);
19 static int asn1_parse_primitive_type(BIO *bp, const uint8_t *object_start,
20 const uint8_t *current_pos,
21 long object_len, long header_len, int tag,
22 int dump);
23
24 const char *ASN1_tag2str(int tag) {
25 static const char *const tag2str[] = {
26 "EOC",
27 "BOOLEAN",
28 "INTEGER",
29 "BIT STRING",
30 "OCTET STRING",
31 "NULL",
32 "OBJECT",
33 "OBJECT DESCRIPTOR",
34 "EXTERNAL",
35 "REAL",
36 "ENUMERATED",
37 "<ASN1 11>",
38 "UTF8STRING",
39 "<ASN1 13>",
40 "<ASN1 14>",
41 "<ASN1 15>",
42 "SEQUENCE",
43 "SET",
44 "NUMERICSTRING",
45 "PRINTABLESTRING",
46 "T61STRING",
47 "VIDEOTEXSTRING",
48 "IA5STRING",
49 "UTCTIME",
50 "GENERALIZEDTIME",
51 "GRAPHICSTRING",
52 "VISIBLESTRING",
53 "GENERALSTRING",
54 "UNIVERSALSTRING",
55 "<ASN1 29>",
56 "BMPSTRING",
57 };
58
59 if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED)) {
60 tag &= ~V_ASN1_NEG;
61 }
62
63 if (tag < 0 || tag > 30) {
64 return "(unknown)";
65 }
66 return tag2str[tag];
67 }
68
69 int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent) {
70 GUARD_PTR(bp);
71 GUARD_PTR(pp);
72 return asn1_parse2(bp, &pp, len, 0, 0, indent, 0);
73 }
74
75 // Constants
76 #define ASN1_PARSE_MAXDEPTH 128
77 #define ASN1_DUMP_INDENT 6 // Because we know BIO_dump_indent()
78
79 // Copy of helper functions from original (these remain unchanged)
80 static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
81 int indent) {
82 static const char fmt[] = "%-18s";
83 char str[128];
84 const char *p;
85
86 if (constructed & V_ASN1_CONSTRUCTED) {
87 p = "cons: ";
88 } else {
89 p = "prim: ";
90 }
91 if (BIO_write(bp, p, 6) < 6) {
92 goto err;
93 }
94 BIO_indent(bp, indent, 128);
95
96 p = str;
97 if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE) {
98 BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
99 } else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC) {
100 BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
101 } else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION) {
102 BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
103 } else if (tag > 30) {
104 BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
105 } else {
106 p = ASN1_tag2str(tag);
107 }
108
109 if (BIO_printf(bp, fmt, p) <= 0) {
110 goto err;
111 }
112 return 1;
113 err:
114 return 0;
115 }
116
117 static int BIO_dump_indent(BIO *bp, const char *s, int len, int indent) {
118 // Use BIO_hexdump as a replacement for BIO_dump_indent
119 return BIO_hexdump(bp, (const uint8_t *)s, len, indent);
120 }
121
122 // Helper function to parse constructed ASN.1 types (SEQUENCE, SET, etc.)
123 static int asn1_parse_constructed_type(BIO *bp, const uint8_t **current_pos,
124 const uint8_t *total_end,
125 const uint8_t *original_start,
126 long *object_len, int parse_flags,
127 long offset, int depth, int indent,
128 int dump) {
129 GUARD_PTR(bp);
130 GUARD_PTR(current_pos);
131 GUARD_PTR(total_end);
132 GUARD_PTR(original_start);
133 GUARD_PTR(object_len);
134
135 if (offset < 0 || depth < 0 || indent < 0) {
136 return 0;
137 }
138
139 const uint8_t *start_pos = *current_pos;
140
141 if (BIO_write(bp, "\n", 1) <= 0) {
142 return 0;
143 }
144
145 if ((parse_flags == (V_ASN1_CONSTRUCTED | 1)) && (*object_len == 0)) {
146 // Indefinite length constructed object
147 for (;;) {
148 const int parse_result = asn1_parse2(
149 bp, current_pos, (long)(total_end - *current_pos),
150 offset + (*current_pos - original_start), depth + 1, indent, dump);
151 if (parse_result == 0) {
152 return 0;
153 }
154 if ((parse_result == 2) || (*current_pos >= total_end)) {
155 *object_len = *current_pos - start_pos;
156 break;
157 }
158 }
159 } else {
160 // Definite length constructed object
161 const uint8_t *constructed_end = *current_pos + *object_len;
162 long remaining_length = *object_len;
163
164 if (constructed_end > total_end) {
165 return 0;
166 }
167
168 while (*current_pos < constructed_end) {
169 start_pos = *current_pos;
170 const int parse_result = asn1_parse2(
171 bp, current_pos, remaining_length,
172 offset + (*current_pos - original_start), depth + 1, indent, dump);
173 if (parse_result == 0) {
174 return 0;
175 }
176 remaining_length -= *current_pos - start_pos;
177 }
178 }
179 return 1;
180 }
181
182 static int asn1_parse_string_type(BIO *bp, const uint8_t *data, long len) {
183 if (BIO_write(bp, ":", 1) <= 0) {
184 return 0;
185 }
186 if (len > INT_MAX) {
187 return 0;
188 }
189 if ((len > 0) && BIO_write(bp, (const char *)data, (int)len) != (int)len) {
190 return 0;
191 }
192 return 1;
193 }
194
195 static int asn1_parse_object_type(BIO *bp, const uint8_t *data, long len,
196 int *dump_as_hex) {
197 const uint8_t *parse_pos = data;
198 ASN1_OBJECT *asn1_object = NULL;
199 int return_code = 0;
200
201 GUARD_PTR(bp);
202 GUARD_PTR(data);
203
204 if (d2i_ASN1_OBJECT(&asn1_object, &parse_pos, len) != NULL) {
205 if (BIO_write(bp, ":", 1) <= 0) {
206 goto end;
207 }
208 i2a_ASN1_OBJECT(bp, asn1_object);
209 return_code = 1;
210 } else {
211 if (BIO_puts(bp, ":BAD OBJECT") <= 0) {
212 goto end;
213 }
214 *dump_as_hex = 1;
215 return_code = 1;
216 }
217
218 end:
219 ASN1_OBJECT_free(asn1_object);
220 return return_code;
221 }
222
223 static int asn1_parse_boolean_type(BIO *bp, const uint8_t *data, long len,
224 int *dump_as_hex) {
225 GUARD_PTR(bp);
226 GUARD_PTR(data);
227 GUARD_PTR(dump_as_hex);
228
229 if (len < 0) {
230 return 0;
231 }
232
233 if (len != 1) {
234 if (BIO_puts(bp, ":BAD BOOLEAN") <= 0) {
235 return 0;
236 }
237 *dump_as_hex = 1;
238 }
239 if (len > 0) {
240 if (BIO_printf(bp, ":%u", data[0]) <= 0) {
241 return 0;
242 }
243 }
244 return 1;
245 }
246
247 static int asn1_parse_octet_string_type(BIO *bp, const uint8_t *data, long len,
248 int dump, int *newline_printed) {
249 GUARD_PTR(bp);
250 GUARD_PTR(data);
251 GUARD_PTR(newline_printed);
252
253 if (len < 0) {
254 return 0;
255 }
256
257 const unsigned char *parse_pos = data;
258 int return_code = 0;
259 int dump_indent = 6;
260
261 ASN1_OCTET_STRING *octet_string =
262 d2i_ASN1_OCTET_STRING(NULL, &parse_pos, len);
263 if (octet_string != NULL && octet_string->length > 0) {
264 int printable = 1;
265 parse_pos = octet_string->data;
266
267 // Test whether the octet string is printable (i.e. ASCII)
268 for (int i = 0; i < octet_string->length; i++) {
269 if (((parse_pos[i] < ' ') && (parse_pos[i] != '\n') &&
270 (parse_pos[i] != '\r') && (parse_pos[i] != '\t')) ||
271 (parse_pos[i] > '~')) {
272 printable = 0;
273 break;
274 }
275 }
276
277 if (printable) {
278 // Printable string
279 if (BIO_write(bp, ":", 1) <= 0) {
280 goto end;
281 }
282 if (BIO_write(bp, (const char *)parse_pos, octet_string->length) <= 0) {
283 goto end;
284 }
285 } else if (!dump) {
286 // Not printable => print octet string as hex dump
287 if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0) {
288 goto end;
289 }
290 for (int i = 0; i < octet_string->length; i++) {
291 if (BIO_printf(bp, "%02X", parse_pos[i]) <= 0) {
292 goto end;
293 }
294 }
295 } else {
296 // Print the normal dump
297 if (!*newline_printed) {
298 if (BIO_write(bp, "\n", 1) <= 0) {
299 goto end;
300 }
301 }
302 if (BIO_dump_indent(bp, (const char *)parse_pos,
303 ((dump == -1 || dump > octet_string->length)
304 ? octet_string->length
305 : dump),
306 dump_indent) <= 0) {
307 goto end;
308 }
309 *newline_printed = 1;
310 }
311 }
312 return_code = 1;
313
314 end:
315 ASN1_OCTET_STRING_free(octet_string);
316 return return_code;
317 }
318
319 static int asn1_parse_integer_type(BIO *bp, const uint8_t *object_start,
320 long object_len, int header_len,
321 int *dump_as_hex) {
322 GUARD_PTR(bp);
323 GUARD_PTR(object_start);
324 GUARD_PTR(dump_as_hex);
325
326 if (object_len < 0 || header_len < 0) {
327 return 0;
328 }
329
330 const uint8_t *parse_pos = object_start;
331 int return_code = 0;
332
333 ASN1_INTEGER *asn1_integer =
334 d2i_ASN1_INTEGER(NULL, &parse_pos, object_len + header_len);
335 if (asn1_integer != NULL) {
336 if (BIO_write(bp, ":", 1) <= 0) {
337 goto end;
338 }
339 if (asn1_integer->type == V_ASN1_NEG_INTEGER) {
340 if (BIO_write(bp, "-", 1) <= 0) {
341 goto end;
342 }
343 }
344 for (int i = 0; i < asn1_integer->length; i++) {
345 if (BIO_printf(bp, "%02X", asn1_integer->data[i]) <= 0) {
346 goto end;
347 }
348 }
349 if (asn1_integer->length == 0) {
350 if (BIO_write(bp, "00", 2) <= 0) {
351 goto end;
352 }
353 }
354 return_code = 1;
355 } else {
356 if (BIO_puts(bp, ":BAD INTEGER") <= 0) {
357 goto end;
358 }
359 *dump_as_hex = 1;
360 return_code = 1;
361 }
362
363 end:
364 ASN1_INTEGER_free(asn1_integer);
365 return return_code;
366 }
367
368 static int asn1_parse_enumerated_type(BIO *bp, const uint8_t *object_start,
369 long object_len, int header_len,
370 int *dump_as_hex) {
371 GUARD_PTR(bp);
372 GUARD_PTR(object_start);
373 GUARD_PTR(dump_as_hex);
374
375 if (object_len < 0 || header_len < 0) {
376 return 0;
377 }
378
379 const uint8_t *parse_pos = object_start;
380 int return_code = 0;
381
382 ASN1_ENUMERATED *asn1_enumerated =
383 d2i_ASN1_ENUMERATED(NULL, &parse_pos, object_len + header_len);
384 if (asn1_enumerated != NULL) {
385 if (BIO_write(bp, ":", 1) <= 0) {
386 goto end;
387 }
388 if (asn1_enumerated->type == V_ASN1_NEG_ENUMERATED) {
389 if (BIO_write(bp, "-", 1) <= 0) {
390 goto end;
391 }
392 }
393 for (int i = 0; i < asn1_enumerated->length; i++) {
394 if (BIO_printf(bp, "%02X", asn1_enumerated->data[i]) <= 0) {
395 goto end;
396 }
397 }
398 if (asn1_enumerated->length == 0) {
399 if (BIO_write(bp, "00", 2) <= 0) {
400 goto end;
401 }
402 }
403 return_code = 1;
404 } else {
405 if (BIO_puts(bp, ":BAD ENUMERATED") <= 0) {
406 goto end;
407 }
408 *dump_as_hex = 1;
409 return_code = 1;
410 }
411
412 end:
413 ASN1_ENUMERATED_free(asn1_enumerated);
414 return return_code;
415 }
416
417 // Helper function to perform hex dump for generic types
418 static int asn1_parse_hex_dump(BIO *bp, const uint8_t *object_start,
419 const uint8_t *current_pos, long object_len,
420 int header_len, int dump, int *newline_printed) {
421 GUARD_PTR(bp);
422 GUARD_PTR(object_start);
423 GUARD_PTR(current_pos);
424 GUARD_PTR(newline_printed);
425
426 if (object_len < 0 || header_len < 0) {
427 return 0;
428 }
429
430 const int dump_indent = 6;
431
432 if (object_len > 0 && dump) {
433 if (!*newline_printed) {
434 if (BIO_write(bp, "\n", 1) <= 0) {
435 return 0;
436 }
437 }
438 if (BIO_dump_indent(bp, (const char *)current_pos,
439 ((dump == -1 || dump > object_len) ? object_len : dump),
440 dump_indent) <= 0) {
441 return 0;
442 }
443 *newline_printed = 1;
444 }
445 return 1;
446 }
447
448 // Helper function to output hex data when dump_as_hex flag is set
449 static int asn1_output_hex_data(BIO *bp, const uint8_t *object_start,
450 long object_len, int header_len) {
451 if (object_len < 0 || header_len < 0) {
452 return 0;
453 }
454
455 const uint8_t *hex_data = object_start + header_len;
456
457 if (BIO_puts(bp, ":[") <= 0) {
458 return 0;
459 }
460 for (int i = 0; i < object_len; i++) {
461 if (BIO_printf(bp, "%02X", hex_data[i]) <= 0) {
462 return 0;
463 }
464 }
465 if (BIO_puts(bp, "]") <= 0) {
466 return 0;
467 }
468 return 1;
469 }
470
471 // Refactored main function to parse primitive ASN.1 types
472 static int asn1_parse_primitive_type(BIO *bp, const uint8_t *object_start,
473 const uint8_t *current_pos,
474 long object_len, long header_len, int tag,
475 int dump) {
476 GUARD_PTR(bp);
477 GUARD_PTR(object_start);
478 GUARD_PTR(current_pos);
479
480 if (object_len < 0 || header_len < 0) {
481 return 0;
482 }
483
484 int newline_printed = 0;
485 int dump_as_hex = 0;
486 int result = 0;
487
488 // Handle different primitive types
489 if ((tag == V_ASN1_PRINTABLESTRING) || (tag == V_ASN1_T61STRING) ||
490 (tag == V_ASN1_IA5STRING) || (tag == V_ASN1_VISIBLESTRING) ||
491 (tag == V_ASN1_NUMERICSTRING) || (tag == V_ASN1_UTF8STRING) ||
492 (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
493 result = asn1_parse_string_type(bp, current_pos, object_len);
494 } else if (tag == V_ASN1_OBJECT) {
495 result = asn1_parse_object_type(bp, object_start, object_len + header_len,
496 &dump_as_hex);
497 } else if (tag == V_ASN1_BOOLEAN) {
498 result = asn1_parse_boolean_type(bp, current_pos, object_len, &dump_as_hex);
499 } else if (tag == V_ASN1_BMPSTRING) {
500 // Currently this is just a placeholder as in the original code
501 } else if (tag == V_ASN1_OCTET_STRING) {
502 result = asn1_parse_octet_string_type(
503 bp, object_start, object_len + header_len, dump, &newline_printed);
504 } else if (tag == V_ASN1_INTEGER) {
505 result = asn1_parse_integer_type(bp, object_start, object_len, header_len,
506 &dump_as_hex);
507 } else if (tag == V_ASN1_ENUMERATED) {
508 result = asn1_parse_enumerated_type(bp, object_start, object_len,
509 header_len, &dump_as_hex);
510 } else {
511 result = asn1_parse_hex_dump(bp, object_start, current_pos, object_len,
512 header_len, dump, &newline_printed);
513 }
514
515 if (!result) {
516 return 0;
517 }
518
519 // Handle hex dump output if needed
520 if (dump_as_hex) {
521 if (!asn1_output_hex_data(bp, object_start, object_len, header_len)) {
522 return 0;
523 }
524 }
525
526 // Ensure we end with a newline if one wasn't already printed
527 if (!newline_printed) {
528 if (BIO_write(bp, "\n", 1) <= 0) {
529 return 0;
530 }
531 }
532
533 return 1;
534 }
535
536 static int asn1_parse2(BIO *bp, const uint8_t **pp, long length, long offset,
537 int depth, int indent, int dump) {
538 GUARD_PTR(bp);
539 GUARD_PTR(pp);
540
541 if (length < 0 || offset < 0 || depth < 0 || indent < 0) {
542 return 0;
543 }
544
545 const uint8_t *current_pos, *total_end, *object_start;
546 long content_length = 0;
547 int tag, xclass, return_value = 0;
548 int header_length = 0, parse_flags = 0;
549
550 if (depth > ASN1_PARSE_MAXDEPTH) {
551 BIO_puts(bp, "BAD RECURSION DEPTH\n");
552 return 0;
553 }
554
555 current_pos = *pp;
556 total_end = current_pos + length;
557 while (length > 0) {
558 object_start = current_pos;
559 parse_flags =
560 ASN1_get_object(&current_pos, &content_length, &tag, &xclass, length);
561 if (parse_flags & 0x80) {
562 if (BIO_write(bp, "Error in encoding\n", 18) <= 0) {
563 goto end;
564 }
565 return_value = 0;
566 goto end;
567 }
568 header_length = (current_pos - object_start);
569 length -= header_length;
570 /*
571 * if parse_flags == (CBS_ASN1_CONSTRUCTED | 1) it is a constructed
572 * indefinite length object
573 */
574 if (BIO_printf(bp, "%5ld:", (long)offset + (long)(object_start - *pp)) <=
575 0) {
576 goto end;
577 }
578
579 if (parse_flags != (V_ASN1_CONSTRUCTED | 1)) {
580 if (BIO_printf(bp, "d=%-2d hl=%ld l=%4ld ", depth, (long)header_length,
581 content_length) <= 0) {
582 goto end;
583 }
584 } else {
585 if (BIO_printf(bp, "d=%-2d hl=%ld l=inf ", depth, (long)header_length) <=
586 0) {
587 goto end;
588 }
589 }
590 if (!asn1_print_info(bp, tag, xclass, parse_flags, (indent) ? depth : 0)) {
591 goto end;
592 }
593 if (parse_flags & V_ASN1_CONSTRUCTED) {
594 if (content_length > length) {
595 BIO_printf(bp, "length is greater than %ld\n", length);
596 return_value = 0;
597 goto end;
598 }
599 if (!asn1_parse_constructed_type(bp, &current_pos, total_end, *pp,
600 &content_length, parse_flags, offset,
601 depth, indent, dump)) {
602 return_value = 0;
603 goto end;
604 }
605 } else if (xclass != 0) {
606 current_pos += content_length;
607 if (BIO_write(bp, "\n", 1) <= 0) {
608 goto end;
609 }
610 } else {
611 if (!asn1_parse_primitive_type(bp, object_start, current_pos,
612 content_length, header_length, tag, dump)) {
613 goto end;
614 }
615 current_pos += content_length;
616 if ((tag == V_ASN1_EOC) && (xclass == 0)) {
617 return_value = 2; /* End of sequence */
618 goto end;
619 }
620 }
621 length -= content_length;
622 }
623 return_value = 1;
624 end:
625 *pp = current_pos;
626 return return_value;
627 }