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