/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 | outlen = nchar << 1; |
191 | 0 | cpyfunc = cpy_bmp; |
192 | 0 | break; |
193 | | |
194 | 0 | case MBSTRING_UNIV: |
195 | 0 | outlen = nchar << 2; |
196 | 0 | cpyfunc = cpy_univ; |
197 | 0 | break; |
198 | | |
199 | 0 | case MBSTRING_UTF8: |
200 | 0 | outlen = 0; |
201 | 0 | ret = traverse_string(in, len, inform, out_utf8, &outlen); |
202 | 0 | if (ret < 0) { |
203 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING); |
204 | 0 | return -1; |
205 | 0 | } |
206 | 0 | cpyfunc = cpy_utf8; |
207 | 0 | break; |
208 | 0 | } |
209 | 0 | if ((p = OPENSSL_malloc(outlen + 1)) == NULL) { |
210 | 0 | if (free_out) { |
211 | 0 | ASN1_STRING_free(dest); |
212 | 0 | *out = NULL; |
213 | 0 | } |
214 | 0 | return -1; |
215 | 0 | } |
216 | 0 | dest->length = outlen; |
217 | 0 | dest->data = p; |
218 | 0 | p[outlen] = 0; |
219 | 0 | traverse_string(in, len, inform, cpyfunc, &p); |
220 | 0 | return str_type; |
221 | 0 | } |
222 | | |
223 | | /* |
224 | | * This function traverses a string and passes the value of each character to |
225 | | * an optional function along with a void * argument. |
226 | | */ |
227 | | |
228 | | static int traverse_string(const unsigned char *p, int len, int inform, |
229 | | int (*rfunc)(uint32_t value, void *in), |
230 | | void *arg) |
231 | 0 | { |
232 | 0 | uint32_t value; |
233 | 0 | int ret; |
234 | 0 | while (len) { |
235 | 0 | if (inform == MBSTRING_ASC) { |
236 | 0 | value = *p++; |
237 | 0 | len--; |
238 | 0 | } else if (inform == MBSTRING_BMP) { |
239 | 0 | uint16_t tmp; |
240 | 0 | p = OPENSSL_load_u16_be(&tmp, p); |
241 | 0 | value = tmp; |
242 | 0 | len -= 2; |
243 | 0 | } else if (inform == MBSTRING_UNIV) { |
244 | 0 | p = OPENSSL_load_u32_be(&value, p); |
245 | 0 | len -= 4; |
246 | 0 | } else { |
247 | 0 | ret = ossl_utf8_getc_internal(p, len, &value); |
248 | 0 | if (ret < 0) |
249 | 0 | return -1; |
250 | 0 | len -= ret; |
251 | 0 | p += ret; |
252 | 0 | } |
253 | 0 | if (rfunc) { |
254 | 0 | ret = rfunc(value, arg); |
255 | 0 | if (ret <= 0) |
256 | 0 | return ret; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | return 1; |
260 | 0 | } |
261 | | |
262 | | /* Various utility functions for traverse_string */ |
263 | | |
264 | | /* Just count number of characters */ |
265 | | |
266 | | static int in_utf8(uint32_t value, void *arg) |
267 | 0 | { |
268 | 0 | int *nchar; |
269 | |
|
270 | 0 | if (!is_unicode_valid(value)) |
271 | 0 | return -2; |
272 | 0 | nchar = arg; |
273 | 0 | (*nchar)++; |
274 | 0 | return 1; |
275 | 0 | } |
276 | | |
277 | | /* Determine size of output as a UTF8 String */ |
278 | | |
279 | | static int out_utf8(uint32_t value, void *arg) |
280 | 0 | { |
281 | 0 | int *outlen, len; |
282 | |
|
283 | 0 | len = ossl_utf8_putc_internal(NULL, -1, value); |
284 | 0 | if (len <= 0) |
285 | 0 | return len; |
286 | 0 | outlen = arg; |
287 | 0 | *outlen += len; |
288 | 0 | return 1; |
289 | 0 | } |
290 | | |
291 | | /* |
292 | | * Determine the "type" of a string: check each character against a supplied |
293 | | * "mask". |
294 | | */ |
295 | | |
296 | | static int type_str(uint32_t value, void *arg) |
297 | 0 | { |
298 | 0 | uint32_t usable_types = *((uint32_t *)arg); |
299 | 0 | uint32_t types = usable_types; |
300 | 0 | const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value); |
301 | | |
302 | | /* |
303 | | * Clear out all the types which are not checked later. If any of those |
304 | | * is present in the mask, then the UTF8 type will be added and checked |
305 | | * below. |
306 | | */ |
307 | 0 | types &= B_ASN1_NUMERICSTRING | B_ASN1_PRINTABLESTRING |
308 | 0 | | B_ASN1_IA5STRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING |
309 | 0 | | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING; |
310 | | |
311 | | /* |
312 | | * If any other types were in the input mask, they're effectively treated |
313 | | * as UTF8 |
314 | | */ |
315 | 0 | if (types != usable_types) |
316 | 0 | types |= B_ASN1_UTF8STRING; |
317 | | |
318 | | /* |
319 | | * These checks should be in sync with ASN1_mbstring_ncopy. |
320 | | */ |
321 | 0 | if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native) || native == ' ')) |
322 | 0 | types &= ~B_ASN1_NUMERICSTRING; |
323 | 0 | if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native)) |
324 | 0 | types &= ~B_ASN1_PRINTABLESTRING; |
325 | 0 | if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native)) |
326 | 0 | types &= ~B_ASN1_IA5STRING; |
327 | 0 | if ((types & B_ASN1_T61STRING) && (value > 0xff)) |
328 | 0 | types &= ~B_ASN1_T61STRING; |
329 | 0 | if ((types & B_ASN1_BMPSTRING) && (value > 0xffff)) |
330 | 0 | types &= ~B_ASN1_BMPSTRING; |
331 | 0 | if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value)) |
332 | 0 | types &= ~B_ASN1_UTF8STRING; |
333 | 0 | if (!types) |
334 | 0 | return -1; |
335 | 0 | *((uint32_t *)arg) = types; |
336 | 0 | return 1; |
337 | 0 | } |
338 | | |
339 | | /* Copy one byte per character ASCII like strings */ |
340 | | |
341 | | static int cpy_asc(uint32_t value, void *arg) |
342 | 0 | { |
343 | 0 | unsigned char **p, *q; |
344 | 0 | p = arg; |
345 | 0 | q = *p; |
346 | 0 | *q = (unsigned char)value; |
347 | 0 | (*p)++; |
348 | 0 | return 1; |
349 | 0 | } |
350 | | |
351 | | /* Copy two byte per character BMPStrings */ |
352 | | |
353 | | static int cpy_bmp(uint32_t value, void *arg) |
354 | 0 | { |
355 | 0 | unsigned char **p, *q; |
356 | 0 | p = arg; |
357 | 0 | q = *p; |
358 | 0 | *q++ = (unsigned char)((value >> 8) & 0xff); |
359 | 0 | *q = (unsigned char)(value & 0xff); |
360 | 0 | *p += 2; |
361 | 0 | return 1; |
362 | 0 | } |
363 | | |
364 | | /* Copy four byte per character UniversalStrings */ |
365 | | |
366 | | static int cpy_univ(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 >> 24) & 0xff); |
372 | 0 | *q++ = (unsigned char)((value >> 16) & 0xff); |
373 | 0 | *q++ = (unsigned char)((value >> 8) & 0xff); |
374 | 0 | *q = (unsigned char)(value & 0xff); |
375 | 0 | *p += 4; |
376 | 0 | return 1; |
377 | 0 | } |
378 | | |
379 | | /* Copy to a UTF8String */ |
380 | | |
381 | | static int cpy_utf8(uint32_t value, void *arg) |
382 | 0 | { |
383 | 0 | unsigned char **p; |
384 | 0 | int ret; |
385 | 0 | p = arg; |
386 | | /* We already know there is enough room so pass 0xff as the length */ |
387 | 0 | ret = ossl_utf8_putc_internal(*p, 0xff, value); |
388 | 0 | if (ret < 0) |
389 | 0 | return ret; |
390 | 0 | *p += ret; |
391 | 0 | return 1; |
392 | 0 | } |