1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #include <openssl/asn1.h>
61 #include <openssl/err.h>
62 #include <openssl/mem.h>
64 #include "asn1_locl.h"
66 static int traverse_string(const unsigned char *p, int len, int inform,
67 int (*rfunc) (uint32_t value, void *in),
69 static int in_utf8(uint32_t value, void *arg);
70 static int out_utf8(uint32_t value, void *arg);
71 static int type_str(uint32_t value, void *arg);
72 static int cpy_asc(uint32_t value, void *arg);
73 static int cpy_bmp(uint32_t value, void *arg);
74 static int cpy_univ(uint32_t value, void *arg);
75 static int cpy_utf8(uint32_t value, void *arg);
76 static int is_printable(uint32_t 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 Printable < IA5 < T61 < BMP < Universal < UTF8) and
82 * 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) (uint32_t, 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 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
124 OPENSSL_PUT_ERROR(ASN1, 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 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING);
145 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
149 if ((minsize > 0) && (nchar < minsize)) {
150 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
151 BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
152 ERR_add_error_data(2, "minsize=", strbuf);
156 if ((maxsize > 0) && (nchar > maxsize)) {
157 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
158 BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
159 ERR_add_error_data(2, "maxsize=", strbuf);
163 /* Now work out minimal type (if any) */
164 if (traverse_string(in, len, inform, type_str, &mask) < 0) {
165 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
169 /* Now work out output format and string type */
170 outform = MBSTRING_ASC;
171 if (mask & B_ASN1_PRINTABLESTRING)
172 str_type = V_ASN1_PRINTABLESTRING;
173 else if (mask & B_ASN1_IA5STRING)
174 str_type = V_ASN1_IA5STRING;
175 else if (mask & B_ASN1_T61STRING)
176 str_type = V_ASN1_T61STRING;
177 else if (mask & B_ASN1_BMPSTRING) {
178 str_type = V_ASN1_BMPSTRING;
179 outform = MBSTRING_BMP;
180 } else if (mask & B_ASN1_UNIVERSALSTRING) {
181 str_type = V_ASN1_UNIVERSALSTRING;
182 outform = MBSTRING_UNIV;
184 str_type = V_ASN1_UTF8STRING;
185 outform = MBSTRING_UTF8;
194 OPENSSL_free(dest->data);
197 dest->type = str_type;
200 dest = ASN1_STRING_type_new(str_type);
202 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
207 /* If both the same type just copy across */
208 if (inform == outform) {
209 if (!ASN1_STRING_set(dest, in, len)) {
210 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
216 /* Work out how much space the destination will need */
235 traverse_string(in, len, inform, out_utf8, &outlen);
239 if (!(p = OPENSSL_malloc(outlen + 1))) {
241 ASN1_STRING_free(dest);
242 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
245 dest->length = outlen;
248 traverse_string(in, len, inform, cpyfunc, &p);
253 * This function traverses a string and passes the value of each character to
254 * an optional function along with a void * argument.
257 static int traverse_string(const unsigned char *p, int len, int inform,
258 int (*rfunc) (uint32_t value, void *in),
264 if (inform == MBSTRING_ASC) {
267 } else if (inform == MBSTRING_BMP) {
271 } else if (inform == MBSTRING_UNIV) {
272 value = ((uint32_t)*p++) << 24;
273 value |= ((uint32_t)*p++) << 16;
278 ret = UTF8_getc(p, len, &value);
285 ret = rfunc(value, arg);
293 /* Various utility functions for traverse_string */
295 /* Just count number of characters */
297 static int in_utf8(uint32_t value, void *arg)
305 /* Determine size of output as a UTF8 String */
307 static int out_utf8(uint32_t value, void *arg)
311 *outlen += UTF8_putc(NULL, -1, value);
316 * Determine the "type" of a string: check each character against a supplied
320 static int type_str(uint32_t value, void *arg)
323 types = *((unsigned long *)arg);
324 if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
325 types &= ~B_ASN1_PRINTABLESTRING;
326 if ((types & B_ASN1_IA5STRING) && (value > 127))
327 types &= ~B_ASN1_IA5STRING;
328 if ((types & B_ASN1_T61STRING) && (value > 0xff))
329 types &= ~B_ASN1_T61STRING;
330 if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
331 types &= ~B_ASN1_BMPSTRING;
334 *((unsigned long *)arg) = types;
338 /* Copy one byte per character ASCII like strings */
340 static int cpy_asc(uint32_t value, void *arg)
342 unsigned char **p, *q;
345 *q = (unsigned char)value;
350 /* Copy two byte per character BMPStrings */
352 static int cpy_bmp(uint32_t value, void *arg)
354 unsigned char **p, *q;
357 *q++ = (unsigned char)((value >> 8) & 0xff);
358 *q = (unsigned char)(value & 0xff);
363 /* Copy four byte per character UniversalStrings */
365 static int cpy_univ(uint32_t value, void *arg)
367 unsigned char **p, *q;
370 *q++ = (unsigned char)((value >> 24) & 0xff);
371 *q++ = (unsigned char)((value >> 16) & 0xff);
372 *q++ = (unsigned char)((value >> 8) & 0xff);
373 *q = (unsigned char)(value & 0xff);
378 /* Copy to a UTF8String */
380 static int cpy_utf8(uint32_t value, void *arg)
385 /* We already know there is enough room so pass 0xff as the length */
386 ret = UTF8_putc(*p, 0xff, value);
391 /* Return 1 if the character is permitted in a PrintableString */
392 static int is_printable(uint32_t value)
399 * Note: we can't use 'isalnum' because certain accented characters may
400 * count as alphanumeric in some environments.
402 if ((ch >= 'a') && (ch <= 'z'))
404 if ((ch >= 'A') && (ch <= 'Z'))
406 if ((ch >= '0') && (ch <= '9'))
408 if ((ch == ' ') || strchr("'()+,-./:=?", ch))