Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_mbstr.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
6 #include <limits.h>
7 #include <string.h>
8
9 #include <openssl/bytestring.h>
10 #include <openssl/err.h>
11 #include <openssl/mem.h>
12
13 #include "../bytestring/internal.h"
14 #include "internal.h"
15
16 // These functions take a string in UTF8, ASCII or multibyte form and a mask
17 // of permissible ASN1 string types. It then works out the minimal type
18 // (using the order Printable < IA5 < T61 < BMP < Universal < UTF8) and
19 // creates a string of the correct type with the supplied data. Yes this is
20 // horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
21 // size limits too.
22
23 int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in,
24 ossl_ssize_t len, int inform, unsigned long mask) {
25 return ASN1_mbstring_ncopy(out, in, len, inform, mask, /*minsize=*/0,
26 /*maxsize=*/0);
27 }
28
29 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_BMPSTRING)
30 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UNIVERSALSTRING)
31 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UTF8STRING)
32
33 int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in,
34 ossl_ssize_t len, int inform, unsigned long mask,
35 ossl_ssize_t minsize, ossl_ssize_t maxsize) {
36 if (len == -1) {
37 len = strlen((const char *)in);
38 }
39 if (!mask) {
40 mask = DIRSTRING_TYPE;
41 }
42
43 int (*decode_func)(CBS *, uint32_t *);
44 int error;
45 switch (inform) {
46 case MBSTRING_BMP:
47 decode_func = cbs_get_ucs2_be;
48 error = ASN1_R_INVALID_BMPSTRING;
49 break;
50
51 case MBSTRING_UNIV:
52 decode_func = cbs_get_utf32_be;
53 error = ASN1_R_INVALID_UNIVERSALSTRING;
54 break;
55
56 case MBSTRING_UTF8:
57 decode_func = cbs_get_utf8;
58 error = ASN1_R_INVALID_UTF8STRING;
59 break;
60
61 case MBSTRING_ASC:
62 decode_func = cbs_get_latin1;
63 error = ERR_R_INTERNAL_ERROR; // Latin-1 inputs are never invalid.
64 break;
65
66 default:
67 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
68 return -1;
69 }
70
71 // Check |minsize| and |maxsize| and work out the minimal type, if any.
72 CBS cbs;
73 CBS_init(&cbs, in, len);
74 size_t utf8_len = 0, nchar = 0;
75 while (CBS_len(&cbs) != 0) {
76 uint32_t c;
77 if (!decode_func(&cbs, &c)) {
78 OPENSSL_PUT_ERROR(ASN1, error);
79 return -1;
80 }
81 if (nchar == 0 && (inform == MBSTRING_BMP || inform == MBSTRING_UNIV) &&
82 c == 0xfeff) {
83 // Reject byte-order mark. We could drop it but that would mean
84 // adding ambiguity around whether a BOM was included or not when
85 // matching strings.
86 //
87 // For a little-endian UCS-2 string, the BOM will appear as 0xfffe
88 // and will be rejected as noncharacter, below.
89 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
90 return -1;
91 }
92
93 // Update which output formats are still possible.
94 if ((mask & B_ASN1_PRINTABLESTRING) && !asn1_is_printable(c)) {
95 mask &= ~B_ASN1_PRINTABLESTRING;
96 }
97 if ((mask & B_ASN1_IA5STRING) && (c > 127)) {
98 mask &= ~B_ASN1_IA5STRING;
99 }
100 if ((mask & B_ASN1_T61STRING) && (c > 0xff)) {
101 mask &= ~B_ASN1_T61STRING;
102 }
103 if ((mask & B_ASN1_BMPSTRING) && (c > 0xffff)) {
104 mask &= ~B_ASN1_BMPSTRING;
105 }
106 if (!mask) {
107 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
108 return -1;
109 }
110
111 nchar++;
112 utf8_len += cbb_get_utf8_len(c);
113 if (maxsize > 0 && nchar > (size_t)maxsize) {
114 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
115 #if defined(OPENSSL_WINDOWS)
116 ERR_add_error_dataf("maxsize=%lu", (unsigned long)maxsize);
117 #else
118 ERR_add_error_dataf("maxsize=%zu", (size_t)maxsize);
119 #endif
120 return -1;
121 }
122 }
123
124 if (minsize > 0 && nchar < (size_t)minsize) {
125 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
126 #if defined(OPENSSL_WINDOWS)
127 ERR_add_error_dataf("minsize=%lu", (unsigned long)minsize);
128 #else
129 ERR_add_error_dataf("minsize=%zu", (size_t)minsize);
130 #endif
131 return -1;
132 }
133
134 // Now work out output format and string type
135 int str_type;
136 int (*encode_func)(CBB *, uint32_t) = cbb_add_latin1;
137 size_t size_estimate = nchar;
138 int outform = MBSTRING_ASC;
139 if (mask & B_ASN1_PRINTABLESTRING) {
140 str_type = V_ASN1_PRINTABLESTRING;
141 } else if (mask & B_ASN1_IA5STRING) {
142 str_type = V_ASN1_IA5STRING;
143 } else if (mask & B_ASN1_T61STRING) {
144 str_type = V_ASN1_T61STRING;
145 } else if (mask & B_ASN1_BMPSTRING) {
146 str_type = V_ASN1_BMPSTRING;
147 outform = MBSTRING_BMP;
148 encode_func = cbb_add_ucs2_be;
149 size_estimate = 2 * nchar;
150 } else if (mask & B_ASN1_UNIVERSALSTRING) {
151 str_type = V_ASN1_UNIVERSALSTRING;
152 encode_func = cbb_add_utf32_be;
153 size_estimate = 4 * nchar;
154 outform = MBSTRING_UNIV;
155 } else if (mask & B_ASN1_UTF8STRING) {
156 str_type = V_ASN1_UTF8STRING;
157 outform = MBSTRING_UTF8;
158 encode_func = cbb_add_utf8;
159 size_estimate = utf8_len;
160 } else {
161 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
162 return -1;
163 }
164
165 if (!out) {
166 return str_type;
167 }
168
169 int free_dest = 0;
170 ASN1_STRING *dest;
171 if (*out) {
172 dest = *out;
173 } else {
174 free_dest = 1;
175 dest = ASN1_STRING_type_new(str_type);
176 if (!dest) {
177 return -1;
178 }
179 }
180
181 CBB cbb;
182 CBB_zero(&cbb);
183 // If both the same type just copy across
184 if (inform == outform) {
185 if (!ASN1_STRING_set(dest, in, len)) {
186 goto err;
187 }
188 dest->type = str_type;
189 *out = dest;
190 return str_type;
191 }
192 if (!CBB_init(&cbb, size_estimate + 1)) {
193 goto err;
194 }
195 CBS_init(&cbs, in, len);
196 while (CBS_len(&cbs) != 0) {
197 uint32_t c;
198 if (!decode_func(&cbs, &c) || !encode_func(&cbb, c)) {
199 OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
200 goto err;
201 }
202 }
203 uint8_t *data = NULL;
204 size_t data_len;
205 if (// OpenSSL historically NUL-terminated this value with a single byte,
206 // even for |MBSTRING_BMP| and |MBSTRING_UNIV|.
207 !CBB_add_u8(&cbb, 0) || !CBB_finish(&cbb, &data, &data_len) ||
208 data_len < 1 || data_len > INT_MAX) {
209 OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
210 OPENSSL_free(data);
211 goto err;
212 }
213 dest->type = str_type;
214 ASN1_STRING_set0(dest, data, (int)data_len - 1);
215 *out = dest;
216 return str_type;
217
218 err:
219 if (free_dest) {
220 ASN1_STRING_free(dest);
221 }
222 CBB_cleanup(&cbb);
223 return -1;
224 }
225
226 int asn1_is_printable(uint32_t value) {
227 if (value > 0x7f) {
228 return 0;
229 }
230 return OPENSSL_isalnum(value) || //
231 value == ' ' || value == '\'' || value == '(' || value == ')' ||
232 value == '+' || value == ',' || value == '-' || value == '.' ||
233 value == '/' || value == ':' || value == '=' || value == '?';
234 }