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