Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/asn1/a_utf8.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2016 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 "internal/cryptlib.h"
12
#include <openssl/asn1.h>
13
14
/* UTF8 utilities */
15
16
/*-
17
 * This parses a UTF8 string one character at a time. It is passed a pointer
18
 * to the string and the length of the string. It sets 'value' to the value of
19
 * the current character. It returns the number of characters read or a
20
 * negative error code:
21
 * -1 = string too short
22
 * -2 = illegal character
23
 * -3 = subsequent characters not of the form 10xxxxxx
24
 * -4 = character encoded incorrectly (not minimal length).
25
 */
26
27
int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
28
38.9k
{
29
38.9k
    const unsigned char *p;
30
38.9k
    unsigned long value;
31
38.9k
    int ret;
32
38.9k
    if (len <= 0)
33
0
        return 0;
34
38.9k
    p = str;
35
36
    /* Check syntax and work out the encoded value (if correct) */
37
38.9k
    if ((*p & 0x80) == 0) {
38
29.8k
        value = *p++ & 0x7f;
39
29.8k
        ret = 1;
40
29.8k
    } else if ((*p & 0xe0) == 0xc0) {
41
1.54k
        if (len < 2)
42
473
            return -1;
43
1.06k
        if ((p[1] & 0xc0) != 0x80)
44
369
            return -3;
45
700
        value = (*p++ & 0x1f) << 6;
46
700
        value |= *p++ & 0x3f;
47
700
        if (value < 0x80)
48
257
            return -4;
49
443
        ret = 2;
50
7.53k
    } else if ((*p & 0xf0) == 0xe0) {
51
1.83k
        if (len < 3)
52
417
            return -1;
53
1.41k
        if (((p[1] & 0xc0) != 0x80)
54
1.41k
            || ((p[2] & 0xc0) != 0x80))
55
441
            return -3;
56
977
        value = (*p++ & 0xf) << 12;
57
977
        value |= (*p++ & 0x3f) << 6;
58
977
        value |= *p++ & 0x3f;
59
977
        if (value < 0x800)
60
203
            return -4;
61
774
        ret = 3;
62
5.70k
    } else if ((*p & 0xf8) == 0xf0) {
63
1.32k
        if (len < 4)
64
203
            return -1;
65
1.12k
        if (((p[1] & 0xc0) != 0x80)
66
1.12k
            || ((p[2] & 0xc0) != 0x80)
67
1.12k
            || ((p[3] & 0xc0) != 0x80))
68
619
            return -3;
69
505
        value = ((unsigned long)(*p++ & 0x7)) << 18;
70
505
        value |= (*p++ & 0x3f) << 12;
71
505
        value |= (*p++ & 0x3f) << 6;
72
505
        value |= *p++ & 0x3f;
73
505
        if (value < 0x10000)
74
208
            return -4;
75
297
        ret = 4;
76
4.37k
    } else if ((*p & 0xfc) == 0xf8) {
77
1.81k
        if (len < 5)
78
236
            return -1;
79
1.57k
        if (((p[1] & 0xc0) != 0x80)
80
1.57k
            || ((p[2] & 0xc0) != 0x80)
81
1.57k
            || ((p[3] & 0xc0) != 0x80)
82
1.57k
            || ((p[4] & 0xc0) != 0x80))
83
924
            return -3;
84
653
        value = ((unsigned long)(*p++ & 0x3)) << 24;
85
653
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
86
653
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
87
653
        value |= (*p++ & 0x3f) << 6;
88
653
        value |= *p++ & 0x3f;
89
653
        if (value < 0x200000)
90
216
            return -4;
91
437
        ret = 5;
92
2.56k
    } else if ((*p & 0xfe) == 0xfc) {
93
1.91k
        if (len < 6)
94
363
            return -1;
95
1.54k
        if (((p[1] & 0xc0) != 0x80)
96
1.54k
            || ((p[2] & 0xc0) != 0x80)
97
1.54k
            || ((p[3] & 0xc0) != 0x80)
98
1.54k
            || ((p[4] & 0xc0) != 0x80)
99
1.54k
            || ((p[5] & 0xc0) != 0x80))
100
1.09k
            return -3;
101
453
        value = ((unsigned long)(*p++ & 0x1)) << 30;
102
453
        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
103
453
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
104
453
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
105
453
        value |= (*p++ & 0x3f) << 6;
106
453
        value |= *p++ & 0x3f;
107
453
        if (value < 0x4000000)
108
216
            return -4;
109
237
        ret = 6;
110
237
    } else
111
652
        return -2;
112
32.0k
    *val = value;
113
32.0k
    return ret;
114
38.9k
}
115
116
/*
117
 * This takes a character 'value' and writes the UTF8 encoded value in 'str'
118
 * where 'str' is a buffer containing 'len' characters. Returns the number of
119
 * characters written or -1 if 'len' is too small. 'str' can be set to NULL
120
 * in which case it just returns the number of characters. It will need at
121
 * most 6 characters.
122
 */
123
124
int UTF8_putc(unsigned char *str, int len, unsigned long value)
125
18.1M
{
126
18.1M
    if (!str)
127
9.06M
        len = 6;                /* Maximum we will need */
128
9.11M
    else if (len <= 0)
129
0
        return -1;
130
18.1M
    if (value < 0x80) {
131
1.15M
        if (str)
132
577k
            *str = (unsigned char)value;
133
1.15M
        return 1;
134
1.15M
    }
135
17.0M
    if (value < 0x800) {
136
16.5M
        if (len < 2)
137
0
            return -1;
138
16.5M
        if (str) {
139
8.29M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
140
8.29M
            *str = (unsigned char)((value & 0x3f) | 0x80);
141
8.29M
        }
142
16.5M
        return 2;
143
16.5M
    }
144
442k
    if (value < 0x10000) {
145
109k
        if (len < 3)
146
0
            return -1;
147
109k
        if (str) {
148
77.2k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
149
77.2k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
150
77.2k
            *str = (unsigned char)((value & 0x3f) | 0x80);
151
77.2k
        }
152
109k
        return 3;
153
109k
    }
154
333k
    if (value < 0x200000) {
155
27.7k
        if (len < 4)
156
0
            return -1;
157
27.7k
        if (str) {
158
13.8k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
159
13.8k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
160
13.8k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
161
13.8k
            *str = (unsigned char)((value & 0x3f) | 0x80);
162
13.8k
        }
163
27.7k
        return 4;
164
27.7k
    }
165
305k
    if (value < 0x4000000) {
166
51.0k
        if (len < 5)
167
0
            return -1;
168
51.0k
        if (str) {
169
25.5k
            *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
170
25.5k
            *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
171
25.5k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
172
25.5k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
173
25.5k
            *str = (unsigned char)((value & 0x3f) | 0x80);
174
25.5k
        }
175
51.0k
        return 5;
176
51.0k
    }
177
254k
    if (len < 6)
178
0
        return -1;
179
254k
    if (str) {
180
127k
        *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
181
127k
        *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
182
127k
        *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
183
127k
        *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
184
127k
        *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
185
127k
        *str = (unsigned char)((value & 0x3f) | 0x80);
186
127k
    }
187
254k
    return 6;
188
254k
}