2 * Copyright 1999-2017 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
11 #include "internal/ctype.h"
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);
27 * These functions take a string in UTF8, ASCII or multibyte form and a mask
28 * of permissible ASN1 string types. It then works out the minimal type
29 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
30 * and creates a string of the correct type with the supplied data. Yes this is
31 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
35 int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
36 int inform, unsigned long mask)
38 return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
41 int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
42 int inform, unsigned long mask,
43 long minsize, long maxsize)
48 int outform, outlen = 0;
53 int (*cpyfunc) (unsigned long, void *) = NULL;
55 len = strlen((const char *)in);
57 mask = DIRSTRING_TYPE;
59 /* First do a string check and work out the number of characters */
64 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
65 ASN1_R_INVALID_BMPSTRING_LENGTH);
73 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
74 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
82 /* This counts the characters and does utf8 syntax checking */
83 ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
85 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_INVALID_UTF8STRING);
95 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);
99 if ((minsize > 0) && (nchar < minsize)) {
100 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);
101 BIO_snprintf(strbuf, sizeof(strbuf), "%ld", minsize);
102 ERR_add_error_data(2, "minsize=", strbuf);
106 if ((maxsize > 0) && (nchar > maxsize)) {
107 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);
108 BIO_snprintf(strbuf, sizeof(strbuf), "%ld", maxsize);
109 ERR_add_error_data(2, "maxsize=", strbuf);
113 /* Now work out minimal type (if any) */
114 if (traverse_string(in, len, inform, type_str, &mask) < 0) {
115 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);
119 /* Now work out output format and string type */
120 outform = MBSTRING_ASC;
121 if (mask & B_ASN1_NUMERICSTRING)
122 str_type = V_ASN1_NUMERICSTRING;
123 else if (mask & B_ASN1_PRINTABLESTRING)
124 str_type = V_ASN1_PRINTABLESTRING;
125 else if (mask & B_ASN1_IA5STRING)
126 str_type = V_ASN1_IA5STRING;
127 else if (mask & B_ASN1_T61STRING)
128 str_type = V_ASN1_T61STRING;
129 else if (mask & B_ASN1_BMPSTRING) {
130 str_type = V_ASN1_BMPSTRING;
131 outform = MBSTRING_BMP;
132 } else if (mask & B_ASN1_UNIVERSALSTRING) {
133 str_type = V_ASN1_UNIVERSALSTRING;
134 outform = MBSTRING_UNIV;
136 str_type = V_ASN1_UTF8STRING;
137 outform = MBSTRING_UTF8;
144 OPENSSL_free(dest->data);
147 dest->type = str_type;
150 dest = ASN1_STRING_type_new(str_type);
152 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
157 /* If both the same type just copy across */
158 if (inform == outform) {
159 if (!ASN1_STRING_set(dest, in, len)) {
160 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
166 /* Work out how much space the destination will need */
185 traverse_string(in, len, inform, out_utf8, &outlen);
189 if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
191 ASN1_STRING_free(dest);
192 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
195 dest->length = outlen;
198 traverse_string(in, len, inform, cpyfunc, &p);
203 * This function traverses a string and passes the value of each character to
204 * an optional function along with a void * argument.
207 static int traverse_string(const unsigned char *p, int len, int inform,
208 int (*rfunc) (unsigned long value, void *in),
214 if (inform == MBSTRING_ASC) {
217 } else if (inform == MBSTRING_BMP) {
221 } else if (inform == MBSTRING_UNIV) {
222 value = ((unsigned long)*p++) << 24;
223 value |= ((unsigned long)*p++) << 16;
228 ret = UTF8_getc(p, len, &value);
235 ret = rfunc(value, arg);
243 /* Various utility functions for traverse_string */
245 /* Just count number of characters */
247 static int in_utf8(unsigned long value, void *arg)
255 /* Determine size of output as a UTF8 String */
257 static int out_utf8(unsigned long value, void *arg)
261 *outlen += UTF8_putc(NULL, -1, value);
266 * Determine the "type" of a string: check each character against a supplied
270 static int type_str(unsigned long value, void *arg)
272 unsigned long types = *((unsigned long *)arg);
273 const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
275 if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native)
277 types &= ~B_ASN1_NUMERICSTRING;
278 if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
279 types &= ~B_ASN1_PRINTABLESTRING;
280 if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
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);