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