/src/openssl36/crypto/asn1/a_mbstr.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-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 "internal/unicode.h" |
14 | | #include <openssl/asn1.h> |
15 | | |
16 | | static int traverse_string(const unsigned char *p, int len, int inform, |
17 | | int (*rfunc)(unsigned long value, void *in), |
18 | | void *arg); |
19 | | static int in_utf8(unsigned long value, void *arg); |
20 | | static int out_utf8(unsigned long value, void *arg); |
21 | | static int type_str(unsigned long value, void *arg); |
22 | | static int cpy_asc(unsigned long value, void *arg); |
23 | | static int cpy_bmp(unsigned long value, void *arg); |
24 | | static int cpy_univ(unsigned long value, void *arg); |
25 | | static int cpy_utf8(unsigned long value, void *arg); |
26 | | |
27 | | /* |
28 | | * These functions take a string in UTF8, ASCII or multibyte form and a mask |
29 | | * of permissible ASN1 string types. It then works out the minimal type |
30 | | * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8) |
31 | | * and creates a string of the correct type with the supplied data. Yes this is |
32 | | * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum |
33 | | * size limits too. |
34 | | */ |
35 | | |
36 | | int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len, |
37 | | int inform, unsigned long mask) |
38 | 10.2M | { |
39 | 10.2M | return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0); |
40 | 10.2M | } |
41 | | |
42 | | int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len, |
43 | | int inform, unsigned long mask, |
44 | | long minsize, long maxsize) |
45 | 8.65M | { |
46 | 8.65M | int str_type; |
47 | 8.65M | int ret; |
48 | 8.65M | char free_out; |
49 | 8.65M | int outform, outlen = 0; |
50 | 8.65M | ASN1_STRING *dest; |
51 | 8.65M | unsigned char *p; |
52 | 8.65M | int nchar; |
53 | 8.65M | int (*cpyfunc)(unsigned long, void *) = NULL; |
54 | 8.65M | if (len == -1) { |
55 | 0 | size_t len_s = strlen((const char *)in); |
56 | |
|
57 | 0 | if (len_s >= INT_MAX) { |
58 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG); |
59 | 0 | return -1; |
60 | 0 | } |
61 | 0 | len = (int)len_s; |
62 | 0 | } |
63 | 8.65M | if (!mask) |
64 | 0 | mask = DIRSTRING_TYPE; |
65 | 8.65M | if (len < 0) { |
66 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT); |
67 | 0 | return -1; |
68 | 8.65M | } else if (len >= INT_MAX) { |
69 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG); |
70 | 0 | return -1; |
71 | 0 | } |
72 | | |
73 | | /* First do a string check and work out the number of characters */ |
74 | 8.65M | switch (inform) { |
75 | | |
76 | 1.21M | case MBSTRING_BMP: |
77 | 1.21M | if (len & 1) { |
78 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH); |
79 | 0 | return -1; |
80 | 0 | } |
81 | 1.21M | nchar = len >> 1; |
82 | 1.21M | break; |
83 | | |
84 | 1.14M | case MBSTRING_UNIV: |
85 | 1.14M | if (len & 3) { |
86 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH); |
87 | 0 | return -1; |
88 | 0 | } |
89 | 1.14M | nchar = len >> 2; |
90 | 1.14M | break; |
91 | | |
92 | 1.48M | case MBSTRING_UTF8: |
93 | 1.48M | nchar = 0; |
94 | | /* This counts the characters and does utf8 syntax checking */ |
95 | 1.48M | ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar); |
96 | 1.48M | if (ret < 0) { |
97 | 23.2k | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING); |
98 | 23.2k | return -1; |
99 | 23.2k | } |
100 | 1.46M | break; |
101 | | |
102 | 4.80M | case MBSTRING_ASC: |
103 | 4.80M | nchar = len; |
104 | 4.80M | break; |
105 | | |
106 | 0 | default: |
107 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT); |
108 | 0 | return -1; |
109 | 8.65M | } |
110 | | |
111 | 8.63M | if ((minsize > 0) && (nchar < minsize)) { |
112 | 0 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT, |
113 | 0 | "minsize=%ld", minsize); |
114 | 0 | return -1; |
115 | 0 | } |
116 | | |
117 | 8.63M | if ((maxsize > 0) && (nchar > maxsize)) { |
118 | 0 | ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG, |
119 | 0 | "maxsize=%ld", maxsize); |
120 | 0 | return -1; |
121 | 0 | } |
122 | | |
123 | | /* Now work out minimal type (if any) */ |
124 | 8.63M | if (traverse_string(in, len, inform, type_str, &mask) < 0) { |
125 | 3.33k | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS); |
126 | 3.33k | return -1; |
127 | 3.33k | } |
128 | | |
129 | | /* |
130 | | * Now work out output format and string type. |
131 | | * These checks should be in sync with the checks in type_str. |
132 | | */ |
133 | 8.63M | outform = MBSTRING_ASC; |
134 | 8.63M | if (mask & B_ASN1_NUMERICSTRING) |
135 | 0 | str_type = V_ASN1_NUMERICSTRING; |
136 | 8.63M | else if (mask & B_ASN1_PRINTABLESTRING) |
137 | 0 | str_type = V_ASN1_PRINTABLESTRING; |
138 | 8.63M | else if (mask & B_ASN1_IA5STRING) |
139 | 0 | str_type = V_ASN1_IA5STRING; |
140 | 8.63M | else if (mask & B_ASN1_T61STRING) |
141 | 0 | str_type = V_ASN1_T61STRING; |
142 | 8.63M | else if (mask & B_ASN1_BMPSTRING) { |
143 | 0 | str_type = V_ASN1_BMPSTRING; |
144 | 0 | outform = MBSTRING_BMP; |
145 | 8.63M | } else if (mask & B_ASN1_UNIVERSALSTRING) { |
146 | 0 | str_type = V_ASN1_UNIVERSALSTRING; |
147 | 0 | outform = MBSTRING_UNIV; |
148 | 8.63M | } else { |
149 | 8.63M | str_type = V_ASN1_UTF8STRING; |
150 | 8.63M | outform = MBSTRING_UTF8; |
151 | 8.63M | } |
152 | 8.63M | if (!out) |
153 | 0 | return str_type; |
154 | 8.63M | if (*out) { |
155 | 8.63M | free_out = 0; |
156 | 8.63M | dest = *out; |
157 | 8.63M | ASN1_STRING_set0(dest, NULL, 0); |
158 | 8.63M | dest->type = str_type; |
159 | 8.63M | } else { |
160 | 0 | free_out = 1; |
161 | 0 | dest = ASN1_STRING_type_new(str_type); |
162 | 0 | if (dest == NULL) { |
163 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
164 | 0 | return -1; |
165 | 0 | } |
166 | 0 | *out = dest; |
167 | 0 | } |
168 | | /* If both the same type just copy across */ |
169 | 8.63M | if (inform == outform) { |
170 | 1.46M | if (!ASN1_STRING_set(dest, in, len)) { |
171 | 0 | if (free_out) { |
172 | 0 | ASN1_STRING_free(dest); |
173 | 0 | *out = NULL; |
174 | 0 | } |
175 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
176 | 0 | return -1; |
177 | 0 | } |
178 | 1.46M | return str_type; |
179 | 1.46M | } |
180 | | |
181 | | /* Work out how much space the destination will need */ |
182 | 7.16M | switch (outform) { |
183 | 0 | case MBSTRING_ASC: |
184 | 0 | outlen = nchar; |
185 | 0 | cpyfunc = cpy_asc; |
186 | 0 | break; |
187 | | |
188 | 0 | case MBSTRING_BMP: |
189 | 0 | if (nchar > INT_MAX / 2) { |
190 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG); |
191 | 0 | if (free_out) { |
192 | 0 | ASN1_STRING_free(dest); |
193 | 0 | *out = NULL; |
194 | 0 | } |
195 | 0 | return -1; |
196 | 0 | } |
197 | 0 | outlen = nchar << 1; |
198 | 0 | cpyfunc = cpy_bmp; |
199 | 0 | break; |
200 | | |
201 | 0 | case MBSTRING_UNIV: |
202 | 0 | if (nchar > INT_MAX / 4) { |
203 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG); |
204 | 0 | if (free_out) { |
205 | 0 | ASN1_STRING_free(dest); |
206 | 0 | *out = NULL; |
207 | 0 | } |
208 | 0 | return -1; |
209 | 0 | } |
210 | 0 | outlen = nchar << 2; |
211 | 0 | cpyfunc = cpy_univ; |
212 | 0 | break; |
213 | | |
214 | 7.16M | case MBSTRING_UTF8: |
215 | 7.16M | outlen = 0; |
216 | 7.16M | ret = traverse_string(in, len, inform, out_utf8, &outlen); |
217 | 7.16M | if (ret < 0) { /* error already raised in out_utf8() */ |
218 | 0 | if (free_out) { |
219 | 0 | ASN1_STRING_free(dest); |
220 | 0 | *out = NULL; |
221 | 0 | } |
222 | 0 | return -1; |
223 | 0 | } |
224 | 7.16M | cpyfunc = cpy_utf8; |
225 | 7.16M | break; |
226 | 7.16M | } |
227 | 7.16M | if ((p = OPENSSL_malloc(outlen + 1)) == NULL) { |
228 | 0 | if (free_out) { |
229 | 0 | ASN1_STRING_free(dest); |
230 | 0 | *out = NULL; |
231 | 0 | } |
232 | 0 | return -1; |
233 | 0 | } |
234 | 7.16M | dest->length = outlen; |
235 | 7.16M | dest->data = p; |
236 | 7.16M | p[outlen] = 0; |
237 | 7.16M | traverse_string(in, len, inform, cpyfunc, &p); |
238 | 7.16M | return str_type; |
239 | 7.16M | } |
240 | | |
241 | | /* |
242 | | * This function traverses a string and passes the value of each character to |
243 | | * an optional function along with a void * argument. |
244 | | */ |
245 | | |
246 | | static int traverse_string(const unsigned char *p, int len, int inform, |
247 | | int (*rfunc)(unsigned long value, void *in), |
248 | | void *arg) |
249 | 28.8M | { |
250 | 28.8M | unsigned long value; |
251 | 28.8M | int ret; |
252 | 1.72G | while (len) { |
253 | 1.69G | if (inform == MBSTRING_ASC) { |
254 | 1.60G | value = *p++; |
255 | 1.60G | len--; |
256 | 1.60G | } else if (inform == MBSTRING_BMP) { |
257 | 70.8M | value = *p++ << 8; |
258 | 70.8M | value |= *p++; |
259 | 70.8M | len -= 2; |
260 | 70.8M | } else if (inform == MBSTRING_UNIV) { |
261 | 174k | value = ((unsigned long)*p++) << 24; |
262 | 174k | value |= ((unsigned long)*p++) << 16; |
263 | 174k | value |= *p++ << 8; |
264 | 174k | value |= *p++; |
265 | 174k | len -= 4; |
266 | 20.3M | } else { |
267 | 20.3M | ret = UTF8_getc(p, len, &value); |
268 | 20.3M | if (ret < 0) |
269 | 26.6k | return -1; |
270 | 20.2M | len -= ret; |
271 | 20.2M | p += ret; |
272 | 20.2M | } |
273 | 1.69G | if (rfunc) { |
274 | 1.69G | ret = rfunc(value, arg); |
275 | 1.69G | if (ret <= 0) |
276 | 4.07k | return ret; |
277 | 1.69G | } |
278 | 1.69G | } |
279 | 28.8M | return 1; |
280 | 28.8M | } |
281 | | |
282 | | /* Various utility functions for traverse_string */ |
283 | | |
284 | | /* Just count number of characters */ |
285 | | |
286 | | static int in_utf8(unsigned long value, void *arg) |
287 | 10.8M | { |
288 | 10.8M | int *nchar; |
289 | | |
290 | 10.8M | if (!is_unicode_valid(value)) |
291 | 0 | return -2; |
292 | 10.8M | nchar = arg; |
293 | 10.8M | (*nchar)++; |
294 | 10.8M | return 1; |
295 | 10.8M | } |
296 | | |
297 | | /* Determine size of output as a UTF8 String */ |
298 | | |
299 | | static int out_utf8(unsigned long value, void *arg) |
300 | 456M | { |
301 | 456M | int *outlen, len; |
302 | | |
303 | 456M | len = UTF8_putc(NULL, -1, value); |
304 | 456M | if (len <= 0) { |
305 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING); |
306 | 0 | return len; |
307 | 0 | } |
308 | 456M | outlen = arg; |
309 | 456M | if (*outlen >= INT_MAX - len) { |
310 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG); |
311 | 0 | return -1; |
312 | 0 | } |
313 | 456M | *outlen += len; |
314 | 456M | return 1; |
315 | 456M | } |
316 | | |
317 | | /* |
318 | | * Determine the "type" of a string: check each character against a supplied |
319 | | * "mask". |
320 | | */ |
321 | | |
322 | | static int type_str(unsigned long value, void *arg) |
323 | 569M | { |
324 | 569M | unsigned long usable_types = *((unsigned long *)arg); |
325 | 569M | unsigned long types = usable_types; |
326 | 569M | const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value); |
327 | | |
328 | | /* |
329 | | * Clear out all the types which are not checked later. If any of those |
330 | | * is present in the mask, then the UTF8 type will be added and checked |
331 | | * below. |
332 | | */ |
333 | 569M | types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING |
334 | 569M | | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING |
335 | 569M | | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING; |
336 | | |
337 | | /* |
338 | | * If any other types were in the input mask, they're effectively treated |
339 | | * as UTF8 |
340 | | */ |
341 | 569M | if (types != usable_types) |
342 | 0 | types |= B_ASN1_UTF8STRING; |
343 | | |
344 | | /* |
345 | | * These checks should be in sync with ASN1_mbstring_ncopy. |
346 | | */ |
347 | 569M | if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' ')) |
348 | 0 | types &= ~B_ASN1_NUMERICSTRING; |
349 | 569M | if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native)) |
350 | 0 | types &= ~B_ASN1_PRINTABLESTRING; |
351 | 569M | if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native)) |
352 | 0 | types &= ~B_ASN1_IA5STRING; |
353 | 569M | if ((types & B_ASN1_T61STRING) && (value > 0xff)) |
354 | 0 | types &= ~B_ASN1_T61STRING; |
355 | 569M | if ((types & B_ASN1_BMPSTRING) && (value > 0xffff)) |
356 | 0 | types &= ~B_ASN1_BMPSTRING; |
357 | 569M | if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value)) |
358 | 4.07k | types &= ~B_ASN1_UTF8STRING; |
359 | 569M | if (!types) |
360 | 4.07k | return -1; |
361 | 569M | *((unsigned long *)arg) = types; |
362 | 569M | return 1; |
363 | 569M | } |
364 | | |
365 | | /* Copy one byte per character ASCII like strings */ |
366 | | |
367 | | static int cpy_asc(unsigned long value, void *arg) |
368 | 0 | { |
369 | 0 | unsigned char **p, *q; |
370 | 0 | p = arg; |
371 | 0 | q = *p; |
372 | 0 | *q = (unsigned char)value; |
373 | 0 | (*p)++; |
374 | 0 | return 1; |
375 | 0 | } |
376 | | |
377 | | /* Copy two byte per character BMPStrings */ |
378 | | |
379 | | static int cpy_bmp(unsigned long value, void *arg) |
380 | 0 | { |
381 | 0 | unsigned char **p, *q; |
382 | 0 | p = arg; |
383 | 0 | q = *p; |
384 | 0 | *q++ = (unsigned char)((value >> 8) & 0xff); |
385 | 0 | *q = (unsigned char)(value & 0xff); |
386 | 0 | *p += 2; |
387 | 0 | return 1; |
388 | 0 | } |
389 | | |
390 | | /* Copy four byte per character UniversalStrings */ |
391 | | |
392 | | static int cpy_univ(unsigned long value, void *arg) |
393 | 0 | { |
394 | 0 | unsigned char **p, *q; |
395 | 0 | p = arg; |
396 | 0 | q = *p; |
397 | 0 | *q++ = (unsigned char)((value >> 24) & 0xff); |
398 | 0 | *q++ = (unsigned char)((value >> 16) & 0xff); |
399 | 0 | *q++ = (unsigned char)((value >> 8) & 0xff); |
400 | 0 | *q = (unsigned char)(value & 0xff); |
401 | 0 | *p += 4; |
402 | 0 | return 1; |
403 | 0 | } |
404 | | |
405 | | /* Copy to a UTF8String */ |
406 | | |
407 | | static int cpy_utf8(unsigned long value, void *arg) |
408 | 559M | { |
409 | 559M | unsigned char **p; |
410 | 559M | int ret; |
411 | 559M | p = arg; |
412 | | /* We already know there is enough room so pass 0xff as the length */ |
413 | 559M | ret = UTF8_putc(*p, 0xff, value); |
414 | 559M | if (ret < 0) |
415 | 0 | return ret; |
416 | 559M | *p += ret; |
417 | 559M | return 1; |
418 | 559M | } |