1 /* $OpenBSD: a_mbstr.c,v 1.23 2017/01/29 17:49:22 beck Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
63 #include <openssl/asn1.h>
64 #include <openssl/err.h>
66 #include "asn1_locl.h"
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
);
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.
88 ASN1_mbstring_copy(ASN1_STRING
**out
, const unsigned char *in
, int len
,
89 int inform
, unsigned long mask
)
91 return ASN1_mbstring_ncopy(out
, in
, len
, inform
, mask
, 0, 0);
95 ASN1_mbstring_ncopy(ASN1_STRING
**out
, const unsigned char *in
, int len
,
96 int inform
, unsigned long mask
, long minsize
, long maxsize
)
101 int outform
, outlen
= 0;
105 int (*cpyfunc
)(unsigned long, void *) = NULL
;
108 len
= strlen((const char *)in
);
110 mask
= DIRSTRING_TYPE
;
112 /* First do a string check and work out the number of characters */
116 ASN1error(ASN1_R_INVALID_BMPSTRING_LENGTH
);
124 ASN1error(ASN1_R_INVALID_UNIVERSALSTRING_LENGTH
);
132 /* This counts the characters and does utf8 syntax checking */
133 ret
= traverse_string(in
, len
, MBSTRING_UTF8
, in_utf8
, &nchar
);
135 ASN1error(ASN1_R_INVALID_UTF8STRING
);
145 ASN1error(ASN1_R_UNKNOWN_FORMAT
);
149 if ((minsize
> 0) && (nchar
< minsize
)) {
150 ASN1error(ASN1_R_STRING_TOO_SHORT
);
151 ERR_asprintf_error_data("minsize=%ld", minsize
);
155 if ((maxsize
> 0) && (nchar
> maxsize
)) {
156 ASN1error(ASN1_R_STRING_TOO_LONG
);
157 ERR_asprintf_error_data("maxsize=%ld", maxsize
);
161 /* Now work out minimal type (if any) */
162 if (traverse_string(in
, len
, inform
, type_str
, &mask
) < 0) {
163 ASN1error(ASN1_R_ILLEGAL_CHARACTERS
);
168 /* Now work out output format and string type */
169 outform
= MBSTRING_ASC
;
170 if (mask
& B_ASN1_PRINTABLESTRING
)
171 str_type
= V_ASN1_PRINTABLESTRING
;
172 else if (mask
& B_ASN1_IA5STRING
)
173 str_type
= V_ASN1_IA5STRING
;
174 else if (mask
& B_ASN1_T61STRING
)
175 str_type
= V_ASN1_T61STRING
;
176 else if (mask
& B_ASN1_BMPSTRING
) {
177 str_type
= V_ASN1_BMPSTRING
;
178 outform
= MBSTRING_BMP
;
179 } else if (mask
& B_ASN1_UNIVERSALSTRING
) {
180 str_type
= V_ASN1_UNIVERSALSTRING
;
181 outform
= MBSTRING_UNIV
;
183 str_type
= V_ASN1_UTF8STRING
;
184 outform
= MBSTRING_UTF8
;
196 dest
->type
= str_type
;
199 dest
= ASN1_STRING_type_new(str_type
);
201 ASN1error(ERR_R_MALLOC_FAILURE
);
206 /* If both the same type just copy across */
207 if (inform
== outform
) {
208 if (!ASN1_STRING_set(dest
, in
, len
)) {
209 ASN1error(ERR_R_MALLOC_FAILURE
);
215 /* Work out how much space the destination will need */
234 if (traverse_string(in
, len
, inform
, out_utf8
, &outlen
) < 0) {
235 ASN1error(ASN1_R_ILLEGAL_CHARACTERS
);
241 if (!(p
= malloc(outlen
+ 1))) {
242 ASN1error(ERR_R_MALLOC_FAILURE
);
245 dest
->length
= outlen
;
248 traverse_string(in
, len
, inform
, cpyfunc
, &p
);
253 ASN1_STRING_free(dest
);
259 /* This function traverses a string and passes the value of each character
260 * to an optional function along with a void * argument.
264 traverse_string(const unsigned char *p
, int len
, int inform
,
265 int (*rfunc
)(unsigned long value
, void *in
), void *arg
)
279 /* BMP is explictly defined to not support surrogates */
280 if (UNICODE_IS_SURROGATE(value
))
285 value
= (unsigned long)*p
++ << 24;
289 if (value
> UNICODE_MAX
|| UNICODE_IS_SURROGATE(value
))
294 ret
= UTF8_getc(p
, len
, &value
);
302 ret
= rfunc(value
, arg
);
310 /* Various utility functions for traverse_string */
312 /* Just count number of characters */
315 in_utf8(unsigned long value
, void *arg
)
324 /* Determine size of output as a UTF8 String */
327 out_utf8(unsigned long value
, void *arg
)
333 ret
= UTF8_putc(NULL
, -1, value
);
340 /* Determine the "type" of a string: check each character against a
345 type_str(unsigned long value
, void *arg
)
349 types
= *((unsigned long *)arg
);
350 if ((types
& B_ASN1_PRINTABLESTRING
) && !is_printable(value
))
351 types
&= ~B_ASN1_PRINTABLESTRING
;
352 if ((types
& B_ASN1_IA5STRING
) && (value
> 127))
353 types
&= ~B_ASN1_IA5STRING
;
354 if ((types
& B_ASN1_T61STRING
) && (value
> 0xff))
355 types
&= ~B_ASN1_T61STRING
;
356 if ((types
& B_ASN1_BMPSTRING
) && (value
> 0xffff))
357 types
&= ~B_ASN1_BMPSTRING
;
360 *((unsigned long *)arg
) = types
;
364 /* Copy one byte per character ASCII like strings */
367 cpy_asc(unsigned long value
, void *arg
)
369 unsigned char **p
, *q
;
378 /* Copy two byte per character BMPStrings */
381 cpy_bmp(unsigned long value
, void *arg
)
383 unsigned char **p
, *q
;
387 *q
++ = (value
>> 8) & 0xff;
393 /* Copy four byte per character UniversalStrings */
396 cpy_univ(unsigned long value
, void *arg
)
398 unsigned char **p
, *q
;
402 *q
++ = (value
>> 24) & 0xff;
403 *q
++ = (value
>> 16) & 0xff;
404 *q
++ = (value
>> 8) & 0xff;
410 /* Copy to a UTF8String */
413 cpy_utf8(unsigned long value
, void *arg
)
419 /* We already know there is enough room so pass 0xff as the length */
420 ret
= UTF8_putc(*p
, 0xff, value
);
425 /* Return 1 if the character is permitted in a PrintableString */
427 is_printable(unsigned long value
)
435 /* Note: we can't use 'isalnum' because certain accented
436 * characters may count as alphanumeric in some environments.
438 if ((ch
>= 'a') && (ch
<= 'z'))
440 if ((ch
>= 'A') && (ch
<= 'Z'))
442 if ((ch
>= '0') && (ch
<= '9'))
444 if ((ch
== ' ') || strchr("'()+,-./:=?", ch
))