2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
15 static int traverse_string(const unsigned char *p, int len, int inform,
16 int (*rfunc) (unsigned long value, void *in),
18 static int in_utf8(unsigned long value, void *arg);
19 static int out_utf8(unsigned long value, void *arg);
20 static int type_str(unsigned long value, void *arg);
21 static int cpy_asc(unsigned long value, void *arg);
22 static int cpy_bmp(unsigned long value, void *arg);
23 static int cpy_univ(unsigned long value, void *arg);
24 static int cpy_utf8(unsigned long value, void *arg);
25 static int is_numeric(unsigned long value);
26 static int is_printable(unsigned long value);
29 * These functions take a string in UTF8, ASCII or multibyte form and a mask
30 * of permissible ASN1 string types. It then works out the minimal type
31 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
32 * and creates a string of the correct type with the supplied data. Yes this is
33 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
37 int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
38 int inform, unsigned long mask)
40 return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
43 int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
44 int inform, unsigned long mask,
45 long minsize, long maxsize)
50 int outform, outlen = 0;
55 int (*cpyfunc) (unsigned long, void *) = NULL;
57 len = strlen((const char *)in);
59 mask = DIRSTRING_TYPE;
61 /* First do a string check and work out the number of characters */
66 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
67 ASN1_R_INVALID_BMPSTRING_LENGTH);
75 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
76 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
84 /* This counts the characters and does utf8 syntax checking */
85 ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
87 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_INVALID_UTF8STRING);
97 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);
101 if ((minsize > 0) && (nchar < minsize)) {
102 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);
103 BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
104 ERR_add_error_data(2, "minsize=", strbuf);
108 if ((maxsize > 0) && (nchar > maxsize)) {
109 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);
110 BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
111 ERR_add_error_data(2, "maxsize=", strbuf);
115 /* Now work out minimal type (if any) */
116 if (traverse_string(in, len, inform, type_str, &mask) < 0) {
117 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);
121 /* Now work out output format and string type */
122 outform = MBSTRING_ASC;
123 if (mask & B_ASN1_NUMERICSTRING)
124 str_type = V_ASN1_NUMERICSTRING;
125 else if (mask & B_ASN1_PRINTABLESTRING)
126 str_type = V_ASN1_PRINTABLESTRING;
127 else if (mask & B_ASN1_IA5STRING)
128 str_type = V_ASN1_IA5STRING;
129 else if (mask & B_ASN1_T61STRING)
130 str_type = V_ASN1_T61STRING;
131 else if (mask & B_ASN1_BMPSTRING) {
132 str_type = V_ASN1_BMPSTRING;
133 outform = MBSTRING_BMP;
134 } else if (mask & B_ASN1_UNIVERSALSTRING) {
135 str_type = V_ASN1_UNIVERSALSTRING;
136 outform = MBSTRING_UNIV;
138 str_type = V_ASN1_UTF8STRING;
139 outform = MBSTRING_UTF8;
146 OPENSSL_free(dest->data);
149 dest->type = str_type;
152 dest = ASN1_STRING_type_new(str_type);
154 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
159 /* If both the same type just copy across */
160 if (inform == outform) {
161 if (!ASN1_STRING_set(dest, in, len)) {
162 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
168 /* Work out how much space the destination will need */
187 traverse_string(in, len, inform, out_utf8, &outlen);
191 if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
193 ASN1_STRING_free(dest);
194 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
197 dest->length = outlen;
200 traverse_string(in, len, inform, cpyfunc, &p);
205 * This function traverses a string and passes the value of each character to
206 * an optional function along with a void * argument.
209 static int traverse_string(const unsigned char *p, int len, int inform,
210 int (*rfunc) (unsigned long value, void *in),
216 if (inform == MBSTRING_ASC) {
219 } else if (inform == MBSTRING_BMP) {
223 } else if (inform == MBSTRING_UNIV) {
224 value = ((unsigned long)*p++) << 24;
225 value |= ((unsigned long)*p++) << 16;
230 ret = UTF8_getc(p, len, &value);
237 ret = rfunc(value, arg);
245 /* Various utility functions for traverse_string */
247 /* Just count number of characters */
249 static int in_utf8(unsigned long value, void *arg)
257 /* Determine size of output as a UTF8 String */
259 static int out_utf8(unsigned long value, void *arg)
263 *outlen += UTF8_putc(NULL, -1, value);
268 * Determine the "type" of a string: check each character against a supplied
272 static int type_str(unsigned long value, void *arg)
275 types = *((unsigned long *)arg);
276 if ((types & B_ASN1_NUMERICSTRING) && !is_numeric(value))
277 types &= ~B_ASN1_NUMERICSTRING;
278 if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
279 types &= ~B_ASN1_PRINTABLESTRING;
280 if ((types & B_ASN1_IA5STRING) && (value > 127))
281 types &= ~B_ASN1_IA5STRING;
282 if ((types & B_ASN1_T61STRING) && (value > 0xff))
283 types &= ~B_ASN1_T61STRING;
284 if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
285 types &= ~B_ASN1_BMPSTRING;
288 *((unsigned long *)arg) = types;
292 /* Copy one byte per character ASCII like strings */
294 static int cpy_asc(unsigned long value, void *arg)
296 unsigned char **p, *q;
299 *q = (unsigned char)value;
304 /* Copy two byte per character BMPStrings */
306 static int cpy_bmp(unsigned long value, void *arg)
308 unsigned char **p, *q;
311 *q++ = (unsigned char)((value >> 8) & 0xff);
312 *q = (unsigned char)(value & 0xff);
317 /* Copy four byte per character UniversalStrings */
319 static int cpy_univ(unsigned long value, void *arg)
321 unsigned char **p, *q;
324 *q++ = (unsigned char)((value >> 24) & 0xff);
325 *q++ = (unsigned char)((value >> 16) & 0xff);
326 *q++ = (unsigned char)((value >> 8) & 0xff);
327 *q = (unsigned char)(value & 0xff);
332 /* Copy to a UTF8String */
334 static int cpy_utf8(unsigned long value, void *arg)
339 /* We already know there is enough room so pass 0xff as the length */
340 ret = UTF8_putc(*p, 0xff, value);
345 /* Return 1 if the character is permitted in a PrintableString */
346 static int is_printable(unsigned long value)
353 * Note: we can't use 'isalnum' because certain accented characters may
354 * count as alphanumeric in some environments.
356 #ifndef CHARSET_EBCDIC
357 if ((ch >= 'a') && (ch <= 'z'))
359 if ((ch >= 'A') && (ch <= 'Z'))
361 if ((ch >= '0') && (ch <= '9'))
363 if ((ch == ' ') || strchr("'()+,-./:=?", ch))
365 #else /* CHARSET_EBCDIC */
366 if ((ch >= os_toascii['a']) && (ch <= os_toascii['z']))
368 if ((ch >= os_toascii['A']) && (ch <= os_toascii['Z']))
370 if ((ch >= os_toascii['0']) && (ch <= os_toascii['9']))
372 if ((ch == os_toascii[' ']) || strchr("'()+,-./:=?", os_toebcdic[ch]))
374 #endif /* CHARSET_EBCDIC */
378 /* Return 1 if the character is a digit or space */
379 static int is_numeric(unsigned long value)
385 #ifndef CHARSET_EBCDIC
386 if (!isdigit(ch) && ch != ' ')
389 if (ch > os_toascii['9'])
391 if (ch < os_toascii['0'] && ch != os_toascii[' '])