Coverage Report

Created: 2024-07-27 06:39

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