Coverage Report

Created: 2023-06-08 06:41

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