/src/openssl/crypto/asn1/a_print.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "crypto/ctype.h" |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/asn1.h> |
14 | | |
15 | | #include <crypto/asn1.h> |
16 | | |
17 | | /** |
18 | | * @brief Determine the narrowest ASN.1 string type that can represent bytes. |
19 | | * |
20 | | * Unlike ASN1_PRINTABLE_type() this requires an explicit length and has no |
21 | | * legacy path that treats its input as a NUL terminated C string, so it may |
22 | | * be called on ASN1_STRING data. |
23 | | * |
24 | | * @param s pointer to the bytes to classify |
25 | | * @param len the number of bytes available at s |
26 | | * @returns V_ASN1_T61STRING, V_ASN1_IA5STRING or V_ASN1_PRINTABLESTRING |
27 | | */ |
28 | | static int printable_type(const unsigned char *s, size_t len) |
29 | 0 | { |
30 | 0 | int ia5 = 0; |
31 | 0 | int t61 = 0; |
32 | 0 | size_t i; |
33 | |
|
34 | 0 | for (i = 0; i < len; i++) { |
35 | 0 | int c = s[i]; |
36 | |
|
37 | 0 | if (!ossl_isasn1print(c)) |
38 | 0 | ia5 = 1; |
39 | 0 | if (!ossl_isascii(c)) |
40 | 0 | t61 = 1; |
41 | 0 | } |
42 | 0 | if (t61) |
43 | 0 | return V_ASN1_T61STRING; |
44 | 0 | if (ia5) |
45 | 0 | return V_ASN1_IA5STRING; |
46 | 0 | return V_ASN1_PRINTABLESTRING; |
47 | 0 | } |
48 | | |
49 | | int ASN1_PRINTABLE_type(const unsigned char *s, int len) |
50 | 0 | { |
51 | 0 | if (s == NULL) |
52 | 0 | return V_ASN1_PRINTABLESTRING; |
53 | | |
54 | 0 | if (len < 0) |
55 | 0 | len = (int)strlen((const char *)s); |
56 | |
|
57 | 0 | return printable_type(s, (size_t)len); |
58 | 0 | } |
59 | | |
60 | | int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s) |
61 | 0 | { |
62 | 0 | int i; |
63 | 0 | unsigned char *p; |
64 | |
|
65 | 0 | if (s->type != V_ASN1_UNIVERSALSTRING) |
66 | 0 | return 0; |
67 | 0 | if (s->length < 0 || (s->length % 4) != 0) |
68 | 0 | return 0; |
69 | 0 | p = s->data; |
70 | 0 | for (i = 0; i < s->length; i += 4) { |
71 | 0 | if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0')) |
72 | 0 | break; |
73 | 0 | else |
74 | 0 | p += 4; |
75 | 0 | } |
76 | 0 | if (i < s->length) |
77 | 0 | return 0; |
78 | 0 | p = s->data; |
79 | 0 | for (i = 3; i < s->length; i += 4) { |
80 | 0 | *(p++) = s->data[i]; |
81 | 0 | } |
82 | 0 | if (s->length > 0) |
83 | 0 | *p = '\0'; |
84 | 0 | s->length /= 4; |
85 | 0 | s->type = printable_type(s->data, (size_t)s->length); |
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | | int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) |
90 | 0 | { |
91 | 0 | int i, n; |
92 | 0 | char buf[80]; |
93 | 0 | const char *p; |
94 | |
|
95 | 0 | if (v == NULL) |
96 | 0 | return 0; |
97 | 0 | n = 0; |
98 | 0 | p = (const char *)v->data; |
99 | 0 | for (i = 0; i < v->length; i++) { |
100 | 0 | if ((p[i] > '~') || ((p[i] < ' ') && (p[i] != '\n') && (p[i] != '\r'))) |
101 | 0 | buf[n] = '.'; |
102 | 0 | else |
103 | 0 | buf[n] = p[i]; |
104 | 0 | n++; |
105 | 0 | if (n >= 80) { |
106 | 0 | if (BIO_write(bp, buf, n) <= 0) |
107 | 0 | return 0; |
108 | 0 | n = 0; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | if (n > 0) |
112 | 0 | if (BIO_write(bp, buf, n) <= 0) |
113 | 0 | return 0; |
114 | 0 | return 1; |
115 | 0 | } |