xref: /openbsd-src/lib/libcrypto/asn1/a_mbstr.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: a_mbstr.c,v 1.19 2014/07/11 08:44:47 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 <openssl/asn1.h>
64 #include <openssl/err.h>
65 
66 #include "asn1_locl.h"
67 
68 static int traverse_string(const unsigned char *p, int len, int inform,
69     int (*rfunc)(unsigned long value, void *in), void *arg);
70 static int in_utf8(unsigned long value, void *arg);
71 static int out_utf8(unsigned long value, void *arg);
72 static int type_str(unsigned long value, void *arg);
73 static int cpy_asc(unsigned long value, void *arg);
74 static int cpy_bmp(unsigned long value, void *arg);
75 static int cpy_univ(unsigned long value, void *arg);
76 static int cpy_utf8(unsigned long value, void *arg);
77 static int is_printable(unsigned long value);
78 
79 /* These functions take a string in UTF8, ASCII or multibyte form and
80  * a mask of permissible ASN1 string types. It then works out the minimal
81  * type (using the order Printable < IA5 < T61 < BMP < Universal < UTF8)
82  * and creates a string of the correct type with the supplied data.
83  * Yes this is horrible: it has to be :-(
84  * The 'ncopy' form checks minimum and maximum size limits too.
85  */
86 
87 int
88 ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
89     int inform, unsigned long mask)
90 {
91 	return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
92 }
93 
94 int
95 ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
96     int inform, unsigned long mask, long minsize, long maxsize)
97 {
98 	int str_type;
99 	int ret;
100 	char free_out;
101 	int outform, outlen = 0;
102 	ASN1_STRING *dest;
103 	unsigned char *p;
104 	int nchar;
105 	int (*cpyfunc)(unsigned long, void *) = NULL;
106 
107 	if (len == -1)
108 		len = strlen((const char *)in);
109 	if (!mask)
110 		mask = DIRSTRING_TYPE;
111 
112 	/* First do a string check and work out the number of characters */
113 	switch (inform) {
114 	case MBSTRING_BMP:
115 		if (len & 1) {
116 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
117 			    ASN1_R_INVALID_BMPSTRING_LENGTH);
118 			return -1;
119 		}
120 		nchar = len >> 1;
121 		break;
122 
123 	case MBSTRING_UNIV:
124 		if (len & 3) {
125 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
126 			    ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
127 			return -1;
128 		}
129 		nchar = len >> 2;
130 		break;
131 
132 	case MBSTRING_UTF8:
133 		nchar = 0;
134 		/* This counts the characters and does utf8 syntax checking */
135 		ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
136 		if (ret < 0) {
137 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
138 			    ASN1_R_INVALID_UTF8STRING);
139 			return -1;
140 		}
141 		break;
142 
143 	case MBSTRING_ASC:
144 		nchar = len;
145 		break;
146 
147 	default:
148 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);
149 		return -1;
150 	}
151 
152 	if ((minsize > 0) && (nchar < minsize)) {
153 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);
154 		ERR_asprintf_error_data("minsize=%ld", minsize);
155 		return -1;
156 	}
157 
158 	if ((maxsize > 0) && (nchar > maxsize)) {
159 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);
160 		ERR_asprintf_error_data("maxsize=%ld", maxsize);
161 		return -1;
162 	}
163 
164 	/* Now work out minimal type (if any) */
165 	if (traverse_string(in, len, inform, type_str, &mask) < 0) {
166 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);
167 		return -1;
168 	}
169 
170 
171 	/* Now work out output format and string type */
172 	outform = MBSTRING_ASC;
173 	if (mask & B_ASN1_PRINTABLESTRING)
174 		str_type = V_ASN1_PRINTABLESTRING;
175 	else if (mask & B_ASN1_IA5STRING)
176 		str_type = V_ASN1_IA5STRING;
177 	else if (mask & B_ASN1_T61STRING)
178 		str_type = V_ASN1_T61STRING;
179 	else if (mask & B_ASN1_BMPSTRING) {
180 		str_type = V_ASN1_BMPSTRING;
181 		outform = MBSTRING_BMP;
182 	} else if (mask & B_ASN1_UNIVERSALSTRING) {
183 		str_type = V_ASN1_UNIVERSALSTRING;
184 		outform = MBSTRING_UNIV;
185 	} else {
186 		str_type = V_ASN1_UTF8STRING;
187 		outform = MBSTRING_UTF8;
188 	}
189 	if (!out)
190 		return str_type;
191 	if (*out) {
192 		free_out = 0;
193 		dest = *out;
194 		if (dest->data) {
195 			dest->length = 0;
196 			free(dest->data);
197 			dest->data = NULL;
198 		}
199 		dest->type = str_type;
200 	} else {
201 		free_out = 1;
202 		dest = ASN1_STRING_type_new(str_type);
203 		if (!dest) {
204 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
205 			    ERR_R_MALLOC_FAILURE);
206 			return -1;
207 		}
208 		*out = dest;
209 	}
210 	/* If both the same type just copy across */
211 	if (inform == outform) {
212 		if (!ASN1_STRING_set(dest, in, len)) {
213 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
214 			    ERR_R_MALLOC_FAILURE);
215 			return -1;
216 		}
217 		return str_type;
218 	}
219 
220 	/* Work out how much space the destination will need */
221 	switch (outform) {
222 	case MBSTRING_ASC:
223 		outlen = nchar;
224 		cpyfunc = cpy_asc;
225 		break;
226 
227 	case MBSTRING_BMP:
228 		outlen = nchar << 1;
229 		cpyfunc = cpy_bmp;
230 		break;
231 
232 	case MBSTRING_UNIV:
233 		outlen = nchar << 2;
234 		cpyfunc = cpy_univ;
235 		break;
236 
237 	case MBSTRING_UTF8:
238 		outlen = 0;
239 		if (traverse_string(in, len, inform, out_utf8, &outlen) < 0) {
240 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
241 			    ASN1_R_ILLEGAL_CHARACTERS);
242 			return -1;
243 		}
244 		cpyfunc = cpy_utf8;
245 		break;
246 	}
247 	if (!(p = malloc(outlen + 1))) {
248 		if (free_out)
249 			ASN1_STRING_free(dest);
250 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
251 		return -1;
252 	}
253 	dest->length = outlen;
254 	dest->data = p;
255 	p[outlen] = 0;
256 	traverse_string(in, len, inform, cpyfunc, &p);
257 	return str_type;
258 }
259 
260 /* This function traverses a string and passes the value of each character
261  * to an optional function along with a void * argument.
262  */
263 
264 static int
265 traverse_string(const unsigned char *p, int len, int inform,
266     int (*rfunc)(unsigned long value, void *in), void *arg)
267 {
268 	unsigned long value;
269 	int ret;
270 
271 	while (len) {
272 		if (inform == MBSTRING_ASC) {
273 			value = *p++;
274 			len--;
275 		} else if (inform == MBSTRING_BMP) {
276 			value = *p++ << 8;
277 			value |= *p++;
278 			/* BMP is explictly defined to not support surrogates */
279 			if (UNICODE_IS_SURROGATE(value))
280 				return -1;
281 			len -= 2;
282 		} else if (inform == MBSTRING_UNIV) {
283 			value = ((unsigned long)*p++) << 24;
284 			value |= ((unsigned long)*p++) << 16;
285 			value |= *p++ << 8;
286 			value |= *p++;
287 			if (value > UNICODE_MAX || UNICODE_IS_SURROGATE(value))
288 				return -1;
289 			len -= 4;
290 		} else {
291 			ret = UTF8_getc(p, len, &value);
292 			if (ret < 0)
293 				return -1;
294 			len -= ret;
295 			p += ret;
296 		}
297 		if (rfunc) {
298 			ret = rfunc(value, arg);
299 			if (ret <= 0)
300 				return ret;
301 		}
302 	}
303 	return 1;
304 }
305 
306 /* Various utility functions for traverse_string */
307 
308 /* Just count number of characters */
309 
310 static int
311 in_utf8(unsigned long value, void *arg)
312 {
313 	int *nchar;
314 
315 	nchar = arg;
316 	(*nchar)++;
317 	return 1;
318 }
319 
320 /* Determine size of output as a UTF8 String */
321 
322 static int
323 out_utf8(unsigned long value, void *arg)
324 {
325 	int *outlen;
326 	int ret;
327 
328 	outlen = arg;
329 	ret = UTF8_putc(NULL, -1, value);
330 	if (ret < 0)
331 		return ret;
332 	*outlen += ret;
333 	return 1;
334 }
335 
336 /* Determine the "type" of a string: check each character against a
337  * supplied "mask".
338  */
339 
340 static int
341 type_str(unsigned long value, void *arg)
342 {
343 	unsigned long types;
344 
345 	types = *((unsigned long *)arg);
346 	if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
347 		types &= ~B_ASN1_PRINTABLESTRING;
348 	if ((types & B_ASN1_IA5STRING) && (value > 127))
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)
355 		return -1;
356 	*((unsigned long *)arg) = types;
357 	return 1;
358 }
359 
360 /* Copy one byte per character ASCII like strings */
361 
362 static int
363 cpy_asc(unsigned long value, void *arg)
364 {
365 	unsigned char **p, *q;
366 
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
377 cpy_bmp(unsigned long value, void *arg)
378 {
379 	unsigned char **p, *q;
380 
381 	p = arg;
382 	q = *p;
383 	*q++ = (unsigned char) ((value >> 8) & 0xff);
384 	*q = (unsigned char) (value & 0xff);
385 	*p += 2;
386 	return 1;
387 }
388 
389 /* Copy four byte per character UniversalStrings */
390 
391 static int
392 cpy_univ(unsigned long value, void *arg)
393 {
394 	unsigned char **p, *q;
395 
396 	p = arg;
397 	q = *p;
398 	*q++ = (unsigned char) ((value >> 24) & 0xff);
399 	*q++ = (unsigned char) ((value >> 16) & 0xff);
400 	*q++ = (unsigned char) ((value >> 8) & 0xff);
401 	*q = (unsigned char) (value & 0xff);
402 	*p += 4;
403 	return 1;
404 }
405 
406 /* Copy to a UTF8String */
407 
408 static int
409 cpy_utf8(unsigned long value, void *arg)
410 {
411 	unsigned char **p;
412 
413 	int ret;
414 	p = arg;
415 	/* We already know there is enough room so pass 0xff as the length */
416 	ret = UTF8_putc(*p, 0xff, value);
417 	*p += ret;
418 	return 1;
419 }
420 
421 /* Return 1 if the character is permitted in a PrintableString */
422 static int
423 is_printable(unsigned long value)
424 {
425 	int ch;
426 
427 	if (value > 0x7f)
428 		return 0;
429 	ch = (int)value;
430 
431 	/* Note: we can't use 'isalnum' because certain accented
432 	 * characters may count as alphanumeric in some environments.
433 	 */
434 	if ((ch >= 'a') && (ch <= 'z'))
435 		return 1;
436 	if ((ch >= 'A') && (ch <= 'Z'))
437 		return 1;
438 	if ((ch >= '0') && (ch <= '9'))
439 		return 1;
440 	if ((ch == ' ') || strchr("'()+,-./:=?", ch))
441 		return 1;
442 	return 0;
443 }
444