Coverage Report

Created: 2023-06-08 06:40

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