Coverage Report

Created: 2023-09-25 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
44.7k
{
29
44.7k
    const unsigned char *p;
30
44.7k
    unsigned long value;
31
44.7k
    int ret;
32
44.7k
    if (len <= 0)
33
0
        return 0;
34
44.7k
    p = str;
35
36
    /* Check syntax and work out the encoded value (if correct) */
37
44.7k
    if ((*p & 0x80) == 0) {
38
32.0k
        value = *p++ & 0x7f;
39
32.0k
        ret = 1;
40
32.0k
    } else if ((*p & 0xe0) == 0xc0) {
41
840
        if (len < 2)
42
198
            return -1;
43
642
        if ((p[1] & 0xc0) != 0x80)
44
229
            return -3;
45
413
        value = (*p++ & 0x1f) << 6;
46
413
        value |= *p++ & 0x3f;
47
413
        if (value < 0x80)
48
87
            return -4;
49
326
        ret = 2;
50
11.8k
    } else if ((*p & 0xf0) == 0xe0) {
51
1.07k
        if (len < 3)
52
224
            return -1;
53
849
        if (((p[1] & 0xc0) != 0x80)
54
849
            || ((p[2] & 0xc0) != 0x80))
55
363
            return -3;
56
486
        value = (*p++ & 0xf) << 12;
57
486
        value |= (*p++ & 0x3f) << 6;
58
486
        value |= *p++ & 0x3f;
59
486
        if (value < 0x800)
60
76
            return -4;
61
410
        ret = 3;
62
10.8k
    } else if ((*p & 0xf8) == 0xf0) {
63
1.50k
        if (len < 4)
64
198
            return -1;
65
1.30k
        if (((p[1] & 0xc0) != 0x80)
66
1.30k
            || ((p[2] & 0xc0) != 0x80)
67
1.30k
            || ((p[3] & 0xc0) != 0x80))
68
652
            return -3;
69
657
        value = ((unsigned long)(*p++ & 0x7)) << 18;
70
657
        value |= (*p++ & 0x3f) << 12;
71
657
        value |= (*p++ & 0x3f) << 6;
72
657
        value |= *p++ & 0x3f;
73
657
        if (value < 0x10000)
74
88
            return -4;
75
569
        ret = 4;
76
9.30k
    } else if ((*p & 0xfc) == 0xf8) {
77
3.40k
        if (len < 5)
78
202
            return -1;
79
3.20k
        if (((p[1] & 0xc0) != 0x80)
80
3.20k
            || ((p[2] & 0xc0) != 0x80)
81
3.20k
            || ((p[3] & 0xc0) != 0x80)
82
3.20k
            || ((p[4] & 0xc0) != 0x80))
83
444
            return -3;
84
2.75k
        value = ((unsigned long)(*p++ & 0x3)) << 24;
85
2.75k
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
86
2.75k
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
87
2.75k
        value |= (*p++ & 0x3f) << 6;
88
2.75k
        value |= *p++ & 0x3f;
89
2.75k
        if (value < 0x200000)
90
105
            return -4;
91
2.65k
        ret = 5;
92
5.89k
    } else if ((*p & 0xfe) == 0xfc) {
93
5.76k
        if (len < 6)
94
70
            return -1;
95
5.69k
        if (((p[1] & 0xc0) != 0x80)
96
5.69k
            || ((p[2] & 0xc0) != 0x80)
97
5.69k
            || ((p[3] & 0xc0) != 0x80)
98
5.69k
            || ((p[4] & 0xc0) != 0x80)
99
5.69k
            || ((p[5] & 0xc0) != 0x80))
100
765
            return -3;
101
4.92k
        value = ((unsigned long)(*p++ & 0x1)) << 30;
102
4.92k
        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
103
4.92k
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
104
4.92k
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
105
4.92k
        value |= (*p++ & 0x3f) << 6;
106
4.92k
        value |= *p++ & 0x3f;
107
4.92k
        if (value < 0x4000000)
108
564
            return -4;
109
4.36k
        ret = 6;
110
4.36k
    } else
111
136
        return -2;
112
40.3k
    *val = value;
113
40.3k
    return ret;
114
44.7k
}
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
5.84M
{
126
5.84M
    if (!str)
127
2.92M
        len = 6;                /* Maximum we will need */
128
2.92M
    else if (len <= 0)
129
0
        return -1;
130
5.84M
    if (value < 0x80) {
131
694k
        if (str)
132
347k
            *str = (unsigned char)value;
133
694k
        return 1;
134
694k
    }
135
5.14M
    if (value < 0x800) {
136
39.5k
        if (len < 2)
137
0
            return -1;
138
39.5k
        if (str) {
139
19.7k
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
140
19.7k
            *str = (unsigned char)((value & 0x3f) | 0x80);
141
19.7k
        }
142
39.5k
        return 2;
143
39.5k
    }
144
5.10M
    if (value < 0x10000) {
145
12.5k
        if (len < 3)
146
0
            return -1;
147
12.5k
        if (str) {
148
6.27k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
149
6.27k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
150
6.27k
            *str = (unsigned char)((value & 0x3f) | 0x80);
151
6.27k
        }
152
12.5k
        return 3;
153
12.5k
    }
154
5.09M
    if (value < 0x200000) {
155
15.6k
        if (len < 4)
156
0
            return -1;
157
15.6k
        if (str) {
158
7.80k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
159
7.80k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
160
7.80k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
161
7.80k
            *str = (unsigned char)((value & 0x3f) | 0x80);
162
7.80k
        }
163
15.6k
        return 4;
164
15.6k
    }
165
5.08M
    if (value < 0x4000000) {
166
601k
        if (len < 5)
167
0
            return -1;
168
601k
        if (str) {
169
300k
            *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
170
300k
            *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
171
300k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
172
300k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
173
300k
            *str = (unsigned char)((value & 0x3f) | 0x80);
174
300k
        }
175
601k
        return 5;
176
601k
    }
177
4.47M
    if (len < 6)
178
0
        return -1;
179
4.47M
    if (str) {
180
2.23M
        *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
181
2.23M
        *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
182
2.23M
        *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
183
2.23M
        *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
184
2.23M
        *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
185
2.23M
        *str = (unsigned char)((value & 0x3f) | 0x80);
186
2.23M
    }
187
4.47M
    return 6;
188
4.47M
}