Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/asn1/a_mbstr.c

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