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