3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include "internal/cryptlib.h"
63 #include <openssl/asn1.h>
65 static int traverse_string(const unsigned char *p
, int len
, int inform
,
66 int (*rfunc
) (unsigned long value
, void *in
),
68 static int in_utf8(unsigned long value
, void *arg
);
69 static int out_utf8(unsigned long value
, void *arg
);
70 static int type_str(unsigned long value
, void *arg
);
71 static int cpy_asc(unsigned long value
, void *arg
);
72 static int cpy_bmp(unsigned long value
, void *arg
);
73 static int cpy_univ(unsigned long value
, void *arg
);
74 static int cpy_utf8(unsigned long value
, void *arg
);
75 static int is_numeric(unsigned long value
);
76 static int is_printable(unsigned long value
);
79 * These functions take a string in UTF8, ASCII or multibyte form and a mask
80 * of permissible ASN1 string types. It then works out the minimal type
81 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
82 * and creates a string of the correct type with the supplied data. Yes this is
83 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
87 int ASN1_mbstring_copy(ASN1_STRING
**out
, const unsigned char *in
, int len
,
88 int inform
, unsigned long mask
)
90 return ASN1_mbstring_ncopy(out
, in
, len
, inform
, mask
, 0, 0);
93 int ASN1_mbstring_ncopy(ASN1_STRING
**out
, const unsigned char *in
, int len
,
94 int inform
, unsigned long mask
,
95 long minsize
, long maxsize
)
100 int outform
, outlen
= 0;
105 int (*cpyfunc
) (unsigned long, void *) = NULL
;
107 len
= strlen((const char *)in
);
109 mask
= DIRSTRING_TYPE
;
111 /* First do a string check and work out the number of characters */
116 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
,
117 ASN1_R_INVALID_BMPSTRING_LENGTH
);
125 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
,
126 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH
);
134 /* This counts the characters and does utf8 syntax checking */
135 ret
= traverse_string(in
, len
, MBSTRING_UTF8
, in_utf8
, &nchar
);
137 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ASN1_R_INVALID_UTF8STRING
);
147 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ASN1_R_UNKNOWN_FORMAT
);
151 if ((minsize
> 0) && (nchar
< minsize
)) {
152 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ASN1_R_STRING_TOO_SHORT
);
153 BIO_snprintf(strbuf
, sizeof strbuf
, "%ld", minsize
);
154 ERR_add_error_data(2, "minsize=", strbuf
);
158 if ((maxsize
> 0) && (nchar
> maxsize
)) {
159 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ASN1_R_STRING_TOO_LONG
);
160 BIO_snprintf(strbuf
, sizeof strbuf
, "%ld", maxsize
);
161 ERR_add_error_data(2, "maxsize=", strbuf
);
165 /* Now work out minimal type (if any) */
166 if (traverse_string(in
, len
, inform
, type_str
, &mask
) < 0) {
167 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ASN1_R_ILLEGAL_CHARACTERS
);
171 /* Now work out output format and string type */
172 outform
= MBSTRING_ASC
;
173 if (mask
& B_ASN1_NUMERICSTRING
)
174 str_type
= V_ASN1_NUMERICSTRING
;
175 else if (mask
& B_ASN1_PRINTABLESTRING
)
176 str_type
= V_ASN1_PRINTABLESTRING
;
177 else if (mask
& B_ASN1_IA5STRING
)
178 str_type
= V_ASN1_IA5STRING
;
179 else if (mask
& B_ASN1_T61STRING
)
180 str_type
= V_ASN1_T61STRING
;
181 else if (mask
& B_ASN1_BMPSTRING
) {
182 str_type
= V_ASN1_BMPSTRING
;
183 outform
= MBSTRING_BMP
;
184 } else if (mask
& B_ASN1_UNIVERSALSTRING
) {
185 str_type
= V_ASN1_UNIVERSALSTRING
;
186 outform
= MBSTRING_UNIV
;
188 str_type
= V_ASN1_UTF8STRING
;
189 outform
= MBSTRING_UTF8
;
196 OPENSSL_free(dest
->data
);
199 dest
->type
= str_type
;
202 dest
= ASN1_STRING_type_new(str_type
);
204 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ERR_R_MALLOC_FAILURE
);
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
, ERR_R_MALLOC_FAILURE
);
218 /* Work out how much space the destination will need */
237 traverse_string(in
, len
, inform
, out_utf8
, &outlen
);
241 if ((p
= OPENSSL_malloc(outlen
+ 1)) == NULL
) {
243 ASN1_STRING_free(dest
);
244 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY
, ERR_R_MALLOC_FAILURE
);
247 dest
->length
= outlen
;
250 traverse_string(in
, len
, inform
, cpyfunc
, &p
);
255 * This function traverses a string and passes the value of each character to
256 * an optional function along with a void * argument.
259 static int traverse_string(const unsigned char *p
, int len
, int inform
,
260 int (*rfunc
) (unsigned long value
, void *in
),
266 if (inform
== MBSTRING_ASC
) {
269 } else if (inform
== MBSTRING_BMP
) {
273 } else if (inform
== MBSTRING_UNIV
) {
274 value
= ((unsigned long)*p
++) << 24;
275 value
|= ((unsigned long)*p
++) << 16;
280 ret
= UTF8_getc(p
, len
, &value
);
287 ret
= rfunc(value
, arg
);
295 /* Various utility functions for traverse_string */
297 /* Just count number of characters */
299 static int in_utf8(unsigned long value
, void *arg
)
307 /* Determine size of output as a UTF8 String */
309 static int out_utf8(unsigned long value
, void *arg
)
313 *outlen
+= UTF8_putc(NULL
, -1, value
);
318 * Determine the "type" of a string: check each character against a supplied
322 static int type_str(unsigned long value
, void *arg
)
325 types
= *((unsigned long *)arg
);
326 if ((types
& B_ASN1_NUMERICSTRING
) && !is_numeric(value
))
327 types
&= ~B_ASN1_NUMERICSTRING
;
328 if ((types
& B_ASN1_PRINTABLESTRING
) && !is_printable(value
))
329 types
&= ~B_ASN1_PRINTABLESTRING
;
330 if ((types
& B_ASN1_IA5STRING
) && (value
> 127))
331 types
&= ~B_ASN1_IA5STRING
;
332 if ((types
& B_ASN1_T61STRING
) && (value
> 0xff))
333 types
&= ~B_ASN1_T61STRING
;
334 if ((types
& B_ASN1_BMPSTRING
) && (value
> 0xffff))
335 types
&= ~B_ASN1_BMPSTRING
;
338 *((unsigned long *)arg
) = types
;
342 /* Copy one byte per character ASCII like strings */
344 static int cpy_asc(unsigned long value
, void *arg
)
346 unsigned char **p
, *q
;
349 *q
= (unsigned char)value
;
354 /* Copy two byte per character BMPStrings */
356 static int cpy_bmp(unsigned long value
, void *arg
)
358 unsigned char **p
, *q
;
361 *q
++ = (unsigned char)((value
>> 8) & 0xff);
362 *q
= (unsigned char)(value
& 0xff);
367 /* Copy four byte per character UniversalStrings */
369 static int cpy_univ(unsigned long value
, void *arg
)
371 unsigned char **p
, *q
;
374 *q
++ = (unsigned char)((value
>> 24) & 0xff);
375 *q
++ = (unsigned char)((value
>> 16) & 0xff);
376 *q
++ = (unsigned char)((value
>> 8) & 0xff);
377 *q
= (unsigned char)(value
& 0xff);
382 /* Copy to a UTF8String */
384 static int cpy_utf8(unsigned long value
, void *arg
)
389 /* We already know there is enough room so pass 0xff as the length */
390 ret
= UTF8_putc(*p
, 0xff, value
);
395 /* Return 1 if the character is permitted in a PrintableString */
396 static int is_printable(unsigned long value
)
403 * Note: we can't use 'isalnum' because certain accented characters may
404 * count as alphanumeric in some environments.
406 #ifndef CHARSET_EBCDIC
407 if ((ch
>= 'a') && (ch
<= 'z'))
409 if ((ch
>= 'A') && (ch
<= 'Z'))
411 if ((ch
>= '0') && (ch
<= '9'))
413 if ((ch
== ' ') || strchr("'()+,-./:=?", ch
))
415 #else /* CHARSET_EBCDIC */
416 if ((ch
>= os_toascii
['a']) && (ch
<= os_toascii
['z']))
418 if ((ch
>= os_toascii
['A']) && (ch
<= os_toascii
['Z']))
420 if ((ch
>= os_toascii
['0']) && (ch
<= os_toascii
['9']))
422 if ((ch
== os_toascii
[' ']) || strchr("'()+,-./:=?", os_toebcdic
[ch
]))
424 #endif /* CHARSET_EBCDIC */
428 /* Return 1 if the character is a digit or space */
429 static int is_numeric(unsigned long value
)
435 #ifndef CHARSET_EBCDIC
436 if (!isdigit(ch
) && ch
!= ' ')
439 if (ch
> os_toascii
['9'])
441 if (ch
< os_toascii
['0'] && ch
!= os_toascii
[' '])