1/*
2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10#include <stdio.h>
11#include "crypto/ctype.h"
12#include "internal/cryptlib.h"
13#include "internal/unicode.h"
14#include <openssl/asn1.h>
15
16static int traverse_string(const unsigned char *p, int len, int inform,
17                           int (*rfunc) (unsigned long value, void *in),
18                           void *arg);
19static int in_utf8(unsigned long value, void *arg);
20static int out_utf8(unsigned long value, void *arg);
21static int type_str(unsigned long value, void *arg);
22static int cpy_asc(unsigned long value, void *arg);
23static int cpy_bmp(unsigned long value, void *arg);
24static int cpy_univ(unsigned long value, void *arg);
25static int cpy_utf8(unsigned long value, void *arg);
26
27/*
28 * These functions take a string in UTF8, ASCII or multibyte form and a mask
29 * of permissible ASN1 string types. It then works out the minimal type
30 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)
31 * and creates a string of the correct type with the supplied data. Yes this is
32 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
33 * size limits too.
34 */
35
36int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
37                       int inform, unsigned long mask)
38{
39    return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
40}
41
42int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
43                        int inform, unsigned long mask,
44                        long minsize, long maxsize)
45{
46    int str_type;
47    int ret;
48    char free_out;
49    int outform, outlen = 0;
50    ASN1_STRING *dest;
51    unsigned char *p;
52    int nchar;
53    int (*cpyfunc) (unsigned long, void *) = NULL;
54    if (len == -1)
55        len = strlen((const char *)in);
56    if (!mask)
57        mask = DIRSTRING_TYPE;
58    if (len < 0)
59        return -1;
60
61    /* First do a string check and work out the number of characters */
62    switch (inform) {
63
64    case MBSTRING_BMP:
65        if (len & 1) {
66            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
67            return -1;
68        }
69        nchar = len >> 1;
70        break;
71
72    case MBSTRING_UNIV:
73        if (len & 3) {
74            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
75            return -1;
76        }
77        nchar = len >> 2;
78        break;
79
80    case MBSTRING_UTF8:
81        nchar = 0;
82        /* This counts the characters and does utf8 syntax checking */
83        ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
84        if (ret < 0) {
85            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UTF8STRING);
86            return -1;
87        }
88        break;
89
90    case MBSTRING_ASC:
91        nchar = len;
92        break;
93
94    default:
95        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
96        return -1;
97    }
98
99    if ((minsize > 0) && (nchar < minsize)) {
100        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_SHORT,
101                       "minsize=%ld", minsize);
102        return -1;
103    }
104
105    if ((maxsize > 0) && (nchar > maxsize)) {
106        ERR_raise_data(ERR_LIB_ASN1, ASN1_R_STRING_TOO_LONG,
107                       "maxsize=%ld", maxsize);
108        return -1;
109    }
110
111    /* Now work out minimal type (if any) */
112    if (traverse_string(in, len, inform, type_str, &mask) < 0) {
113        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_CHARACTERS);
114        return -1;
115    }
116
117    /* Now work out output format and string type */
118    outform = MBSTRING_ASC;
119    if (mask & B_ASN1_NUMERICSTRING)
120        str_type = V_ASN1_NUMERICSTRING;
121    else if (mask & B_ASN1_PRINTABLESTRING)
122        str_type = V_ASN1_PRINTABLESTRING;
123    else if (mask & B_ASN1_IA5STRING)
124        str_type = V_ASN1_IA5STRING;
125    else if (mask & B_ASN1_T61STRING)
126        str_type = V_ASN1_T61STRING;
127    else if (mask & B_ASN1_BMPSTRING) {
128        str_type = V_ASN1_BMPSTRING;
129        outform = MBSTRING_BMP;
130    } else if (mask & B_ASN1_UNIVERSALSTRING) {
131        str_type = V_ASN1_UNIVERSALSTRING;
132        outform = MBSTRING_UNIV;
133    } else {
134        str_type = V_ASN1_UTF8STRING;
135        outform = MBSTRING_UTF8;
136    }
137    if (!out)
138        return str_type;
139    if (*out) {
140        free_out = 0;
141        dest = *out;
142        OPENSSL_free(dest->data);
143        dest->data = NULL;
144        dest->length = 0;
145        dest->type = str_type;
146    } else {
147        free_out = 1;
148        dest = ASN1_STRING_type_new(str_type);
149        if (dest == NULL) {
150            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
151            return -1;
152        }
153        *out = dest;
154    }
155    /* If both the same type just copy across */
156    if (inform == outform) {
157        if (!ASN1_STRING_set(dest, in, len)) {
158            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
159            return -1;
160        }
161        return str_type;
162    }
163
164    /* Work out how much space the destination will need */
165    switch (outform) {
166    case MBSTRING_ASC:
167        outlen = nchar;
168        cpyfunc = cpy_asc;
169        break;
170
171    case MBSTRING_BMP:
172        outlen = nchar << 1;
173        cpyfunc = cpy_bmp;
174        break;
175
176    case MBSTRING_UNIV:
177        outlen = nchar << 2;
178        cpyfunc = cpy_univ;
179        break;
180
181    case MBSTRING_UTF8:
182        outlen = 0;
183        traverse_string(in, len, inform, out_utf8, &outlen);
184        cpyfunc = cpy_utf8;
185        break;
186    }
187    if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
188        if (free_out)
189            ASN1_STRING_free(dest);
190        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
191        return -1;
192    }
193    dest->length = outlen;
194    dest->data = p;
195    p[outlen] = 0;
196    traverse_string(in, len, inform, cpyfunc, &p);
197    return str_type;
198}
199
200/*
201 * This function traverses a string and passes the value of each character to
202 * an optional function along with a void * argument.
203 */
204
205static int traverse_string(const unsigned char *p, int len, int inform,
206                           int (*rfunc) (unsigned long value, void *in),
207                           void *arg)
208{
209    unsigned long value;
210    int ret;
211    while (len) {
212        if (inform == MBSTRING_ASC) {
213            value = *p++;
214            len--;
215        } else if (inform == MBSTRING_BMP) {
216            value = *p++ << 8;
217            value |= *p++;
218            len -= 2;
219        } else if (inform == MBSTRING_UNIV) {
220            value = ((unsigned long)*p++) << 24;
221            value |= ((unsigned long)*p++) << 16;
222            value |= *p++ << 8;
223            value |= *p++;
224            len -= 4;
225        } else {
226            ret = UTF8_getc(p, len, &value);
227            if (ret < 0)
228                return -1;
229            len -= ret;
230            p += ret;
231        }
232        if (rfunc) {
233            ret = rfunc(value, arg);
234            if (ret <= 0)
235                return ret;
236        }
237    }
238    return 1;
239}
240
241/* Various utility functions for traverse_string */
242
243/* Just count number of characters */
244
245static int in_utf8(unsigned long value, void *arg)
246{
247    int *nchar;
248
249    if (!is_unicode_valid(value))
250        return -2;
251    nchar = arg;
252    (*nchar)++;
253    return 1;
254}
255
256/* Determine size of output as a UTF8 String */
257
258static int out_utf8(unsigned long value, void *arg)
259{
260    int *outlen, len;
261
262    len = UTF8_putc(NULL, -1, value);
263    if (len <= 0)
264        return len;
265    outlen = arg;
266    *outlen += len;
267    return 1;
268}
269
270/*
271 * Determine the "type" of a string: check each character against a supplied
272 * "mask".
273 */
274
275static int type_str(unsigned long value, void *arg)
276{
277    unsigned long types = *((unsigned long *)arg);
278    const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
279
280    if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native)
281                                            || native == ' '))
282        types &= ~B_ASN1_NUMERICSTRING;
283    if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
284        types &= ~B_ASN1_PRINTABLESTRING;
285    if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
286        types &= ~B_ASN1_IA5STRING;
287    if ((types & B_ASN1_T61STRING) && (value > 0xff))
288        types &= ~B_ASN1_T61STRING;
289    if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
290        types &= ~B_ASN1_BMPSTRING;
291    if ((types & B_ASN1_UTF8STRING) && !is_unicode_valid(value))
292        types &= ~B_ASN1_UTF8STRING;
293    if (!types)
294        return -1;
295    *((unsigned long *)arg) = types;
296    return 1;
297}
298
299/* Copy one byte per character ASCII like strings */
300
301static int cpy_asc(unsigned long value, void *arg)
302{
303    unsigned char **p, *q;
304    p = arg;
305    q = *p;
306    *q = (unsigned char)value;
307    (*p)++;
308    return 1;
309}
310
311/* Copy two byte per character BMPStrings */
312
313static int cpy_bmp(unsigned long value, void *arg)
314{
315    unsigned char **p, *q;
316    p = arg;
317    q = *p;
318    *q++ = (unsigned char)((value >> 8) & 0xff);
319    *q = (unsigned char)(value & 0xff);
320    *p += 2;
321    return 1;
322}
323
324/* Copy four byte per character UniversalStrings */
325
326static int cpy_univ(unsigned long value, void *arg)
327{
328    unsigned char **p, *q;
329    p = arg;
330    q = *p;
331    *q++ = (unsigned char)((value >> 24) & 0xff);
332    *q++ = (unsigned char)((value >> 16) & 0xff);
333    *q++ = (unsigned char)((value >> 8) & 0xff);
334    *q = (unsigned char)(value & 0xff);
335    *p += 4;
336    return 1;
337}
338
339/* Copy to a UTF8String */
340
341static int cpy_utf8(unsigned long value, void *arg)
342{
343    unsigned char **p;
344    int ret;
345    p = arg;
346    /* We already know there is enough room so pass 0xff as the length */
347    ret = UTF8_putc(*p, 0xff, value);
348    *p += ret;
349    return 1;
350}
351