xref: /openbsd-src/lib/libcrypto/asn1/a_mbstr.c (revision a8913c44aee6c78b4770e56ab6afb429afabee6d)
1 /* $OpenBSD: a_mbstr.c,v 1.18 2014/07/10 13:58:22 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <ctype.h>
60 #include <stdio.h>
61 #include <string.h>
62 
63 #include "cryptlib.h"
64 #include <openssl/asn1.h>
65 #include "asn1_locl.h"
66 
67 static int traverse_string(const unsigned char *p, int len, int inform,
68     int (*rfunc)(unsigned long value, void *in), void *arg);
69 static int in_utf8(unsigned long value, void *arg);
70 static int out_utf8(unsigned long value, void *arg);
71 static int type_str(unsigned long value, void *arg);
72 static int cpy_asc(unsigned long value, void *arg);
73 static int cpy_bmp(unsigned long value, void *arg);
74 static int cpy_univ(unsigned long value, void *arg);
75 static int cpy_utf8(unsigned long value, void *arg);
76 static int is_printable(unsigned long value);
77 
78 /* These functions take a string in UTF8, ASCII or multibyte form and
79  * a mask of permissible ASN1 string types. It then works out the minimal
80  * type (using the order Printable < IA5 < T61 < BMP < Universal < UTF8)
81  * and creates a string of the correct type with the supplied data.
82  * Yes this is horrible: it has to be :-(
83  * The 'ncopy' form checks minimum and maximum size limits too.
84  */
85 
86 int
87 ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
88     int inform, unsigned long mask)
89 {
90 	return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
91 }
92 
93 int
94 ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
95     int inform, unsigned long mask, long minsize, long maxsize)
96 {
97 	int str_type;
98 	int ret;
99 	char free_out;
100 	int outform, outlen = 0;
101 	ASN1_STRING *dest;
102 	unsigned char *p;
103 	int nchar;
104 	int (*cpyfunc)(unsigned long, void *) = NULL;
105 
106 	if (len == -1)
107 		len = strlen((const char *)in);
108 	if (!mask)
109 		mask = DIRSTRING_TYPE;
110 
111 	/* First do a string check and work out the number of characters */
112 	switch (inform) {
113 	case MBSTRING_BMP:
114 		if (len & 1) {
115 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
116 			    ASN1_R_INVALID_BMPSTRING_LENGTH);
117 			return -1;
118 		}
119 		nchar = len >> 1;
120 		break;
121 
122 	case MBSTRING_UNIV:
123 		if (len & 3) {
124 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
125 			    ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
126 			return -1;
127 		}
128 		nchar = len >> 2;
129 		break;
130 
131 	case MBSTRING_UTF8:
132 		nchar = 0;
133 		/* This counts the characters and does utf8 syntax checking */
134 		ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
135 		if (ret < 0) {
136 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
137 			    ASN1_R_INVALID_UTF8STRING);
138 			return -1;
139 		}
140 		break;
141 
142 	case MBSTRING_ASC:
143 		nchar = len;
144 		break;
145 
146 	default:
147 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);
148 		return -1;
149 	}
150 
151 	if ((minsize > 0) && (nchar < minsize)) {
152 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);
153 		ERR_asprintf_error_data("minsize=%ld", minsize);
154 		return -1;
155 	}
156 
157 	if ((maxsize > 0) && (nchar > maxsize)) {
158 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);
159 		ERR_asprintf_error_data("maxsize=%ld", maxsize);
160 		return -1;
161 	}
162 
163 	/* Now work out minimal type (if any) */
164 	if (traverse_string(in, len, inform, type_str, &mask) < 0) {
165 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);
166 		return -1;
167 	}
168 
169 
170 	/* Now work out output format and string type */
171 	outform = MBSTRING_ASC;
172 	if (mask & B_ASN1_PRINTABLESTRING)
173 		str_type = V_ASN1_PRINTABLESTRING;
174 	else if (mask & B_ASN1_IA5STRING)
175 		str_type = V_ASN1_IA5STRING;
176 	else if (mask & B_ASN1_T61STRING)
177 		str_type = V_ASN1_T61STRING;
178 	else if (mask & B_ASN1_BMPSTRING) {
179 		str_type = V_ASN1_BMPSTRING;
180 		outform = MBSTRING_BMP;
181 	} else if (mask & B_ASN1_UNIVERSALSTRING) {
182 		str_type = V_ASN1_UNIVERSALSTRING;
183 		outform = MBSTRING_UNIV;
184 	} else {
185 		str_type = V_ASN1_UTF8STRING;
186 		outform = MBSTRING_UTF8;
187 	}
188 	if (!out)
189 		return str_type;
190 	if (*out) {
191 		free_out = 0;
192 		dest = *out;
193 		if (dest->data) {
194 			dest->length = 0;
195 			free(dest->data);
196 			dest->data = NULL;
197 		}
198 		dest->type = str_type;
199 	} else {
200 		free_out = 1;
201 		dest = ASN1_STRING_type_new(str_type);
202 		if (!dest) {
203 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
204 			    ERR_R_MALLOC_FAILURE);
205 			return -1;
206 		}
207 		*out = dest;
208 	}
209 	/* If both the same type just copy across */
210 	if (inform == outform) {
211 		if (!ASN1_STRING_set(dest, in, len)) {
212 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
213 			    ERR_R_MALLOC_FAILURE);
214 			return -1;
215 		}
216 		return str_type;
217 	}
218 
219 	/* Work out how much space the destination will need */
220 	switch (outform) {
221 	case MBSTRING_ASC:
222 		outlen = nchar;
223 		cpyfunc = cpy_asc;
224 		break;
225 
226 	case MBSTRING_BMP:
227 		outlen = nchar << 1;
228 		cpyfunc = cpy_bmp;
229 		break;
230 
231 	case MBSTRING_UNIV:
232 		outlen = nchar << 2;
233 		cpyfunc = cpy_univ;
234 		break;
235 
236 	case MBSTRING_UTF8:
237 		outlen = 0;
238 		if (traverse_string(in, len, inform, out_utf8, &outlen) < 0) {
239 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
240 			    ASN1_R_ILLEGAL_CHARACTERS);
241 			return -1;
242 		}
243 		cpyfunc = cpy_utf8;
244 		break;
245 	}
246 	if (!(p = malloc(outlen + 1))) {
247 		if (free_out)
248 			ASN1_STRING_free(dest);
249 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
250 		return -1;
251 	}
252 	dest->length = outlen;
253 	dest->data = p;
254 	p[outlen] = 0;
255 	traverse_string(in, len, inform, cpyfunc, &p);
256 	return str_type;
257 }
258 
259 /* This function traverses a string and passes the value of each character
260  * to an optional function along with a void * argument.
261  */
262 
263 static int
264 traverse_string(const unsigned char *p, int len, int inform,
265     int (*rfunc)(unsigned long value, void *in), void *arg)
266 {
267 	unsigned long value;
268 	int ret;
269 
270 	while (len) {
271 		if (inform == MBSTRING_ASC) {
272 			value = *p++;
273 			len--;
274 		} else if (inform == MBSTRING_BMP) {
275 			value = *p++ << 8;
276 			value |= *p++;
277 			/* BMP is explictly defined to not support surrogates */
278 			if (UNICODE_IS_SURROGATE(value))
279 				return -1;
280 			len -= 2;
281 		} else if (inform == MBSTRING_UNIV) {
282 			value = ((unsigned long)*p++) << 24;
283 			value |= ((unsigned long)*p++) << 16;
284 			value |= *p++ << 8;
285 			value |= *p++;
286 			if (value > UNICODE_MAX || UNICODE_IS_SURROGATE(value))
287 				return -1;
288 			len -= 4;
289 		} else {
290 			ret = UTF8_getc(p, len, &value);
291 			if (ret < 0)
292 				return -1;
293 			len -= ret;
294 			p += ret;
295 		}
296 		if (rfunc) {
297 			ret = rfunc(value, arg);
298 			if (ret <= 0)
299 				return ret;
300 		}
301 	}
302 	return 1;
303 }
304 
305 /* Various utility functions for traverse_string */
306 
307 /* Just count number of characters */
308 
309 static int
310 in_utf8(unsigned long value, void *arg)
311 {
312 	int *nchar;
313 
314 	nchar = arg;
315 	(*nchar)++;
316 	return 1;
317 }
318 
319 /* Determine size of output as a UTF8 String */
320 
321 static int
322 out_utf8(unsigned long value, void *arg)
323 {
324 	int *outlen;
325 	int ret;
326 
327 	outlen = arg;
328 	ret = UTF8_putc(NULL, -1, value);
329 	if (ret < 0)
330 		return ret;
331 	*outlen += ret;
332 	return 1;
333 }
334 
335 /* Determine the "type" of a string: check each character against a
336  * supplied "mask".
337  */
338 
339 static int
340 type_str(unsigned long value, void *arg)
341 {
342 	unsigned long types;
343 
344 	types = *((unsigned long *)arg);
345 	if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
346 		types &= ~B_ASN1_PRINTABLESTRING;
347 	if ((types & B_ASN1_IA5STRING) && (value > 127))
348 		types &= ~B_ASN1_IA5STRING;
349 	if ((types & B_ASN1_T61STRING) && (value > 0xff))
350 		types &= ~B_ASN1_T61STRING;
351 	if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
352 		types &= ~B_ASN1_BMPSTRING;
353 	if (!types)
354 		return -1;
355 	*((unsigned long *)arg) = types;
356 	return 1;
357 }
358 
359 /* Copy one byte per character ASCII like strings */
360 
361 static int
362 cpy_asc(unsigned long value, void *arg)
363 {
364 	unsigned char **p, *q;
365 
366 	p = arg;
367 	q = *p;
368 	*q = (unsigned char) value;
369 	(*p)++;
370 	return 1;
371 }
372 
373 /* Copy two byte per character BMPStrings */
374 
375 static int
376 cpy_bmp(unsigned long value, void *arg)
377 {
378 	unsigned char **p, *q;
379 
380 	p = arg;
381 	q = *p;
382 	*q++ = (unsigned char) ((value >> 8) & 0xff);
383 	*q = (unsigned char) (value & 0xff);
384 	*p += 2;
385 	return 1;
386 }
387 
388 /* Copy four byte per character UniversalStrings */
389 
390 static int
391 cpy_univ(unsigned long value, void *arg)
392 {
393 	unsigned char **p, *q;
394 
395 	p = arg;
396 	q = *p;
397 	*q++ = (unsigned char) ((value >> 24) & 0xff);
398 	*q++ = (unsigned char) ((value >> 16) & 0xff);
399 	*q++ = (unsigned char) ((value >> 8) & 0xff);
400 	*q = (unsigned char) (value & 0xff);
401 	*p += 4;
402 	return 1;
403 }
404 
405 /* Copy to a UTF8String */
406 
407 static int
408 cpy_utf8(unsigned long value, void *arg)
409 {
410 	unsigned char **p;
411 
412 	int ret;
413 	p = arg;
414 	/* We already know there is enough room so pass 0xff as the length */
415 	ret = UTF8_putc(*p, 0xff, value);
416 	*p += ret;
417 	return 1;
418 }
419 
420 /* Return 1 if the character is permitted in a PrintableString */
421 static int
422 is_printable(unsigned long value)
423 {
424 	int ch;
425 
426 	if (value > 0x7f)
427 		return 0;
428 	ch = (int)value;
429 
430 	/* Note: we can't use 'isalnum' because certain accented
431 	 * characters may count as alphanumeric in some environments.
432 	 */
433 	if ((ch >= 'a') && (ch <= 'z'))
434 		return 1;
435 	if ((ch >= 'A') && (ch <= 'Z'))
436 		return 1;
437 	if ((ch >= '0') && (ch <= '9'))
438 		return 1;
439 	if ((ch == ' ') || strchr("'()+,-./:=?", ch))
440 		return 1;
441 	return 0;
442 }
443