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