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