Coverage Report

Created: 2023-06-08 06:40

/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
126k
{
29
126k
    const unsigned char *p;
30
126k
    unsigned long value;
31
126k
    int ret;
32
126k
    if (len <= 0)
33
0
        return 0;
34
126k
    p = str;
35
36
    /* Check syntax and work out the encoded value (if correct) */
37
126k
    if ((*p & 0x80) == 0) {
38
125k
        value = *p++ & 0x7f;
39
125k
        ret = 1;
40
125k
    } else if ((*p & 0xe0) == 0xc0) {
41
396
        if (len < 2)
42
1
            return -1;
43
395
        if ((p[1] & 0xc0) != 0x80)
44
5
            return -3;
45
390
        value = (*p++ & 0x1f) << 6;
46
390
        value |= *p++ & 0x3f;
47
390
        if (value < 0x80)
48
6
            return -4;
49
384
        ret = 2;
50
384
    } else if ((*p & 0xf0) == 0xe0) {
51
90
        if (len < 3)
52
3
            return -1;
53
87
        if (((p[1] & 0xc0) != 0x80)
54
87
            || ((p[2] & 0xc0) != 0x80))
55
10
            return -3;
56
77
        value = (*p++ & 0xf) << 12;
57
77
        value |= (*p++ & 0x3f) << 6;
58
77
        value |= *p++ & 0x3f;
59
77
        if (value < 0x800)
60
10
            return -4;
61
67
        ret = 3;
62
262
    } else if ((*p & 0xf8) == 0xf0) {
63
58
        if (len < 4)
64
1
            return -1;
65
57
        if (((p[1] & 0xc0) != 0x80)
66
57
            || ((p[2] & 0xc0) != 0x80)
67
57
            || ((p[3] & 0xc0) != 0x80))
68
9
            return -3;
69
48
        value = ((unsigned long)(*p++ & 0x7)) << 18;
70
48
        value |= (*p++ & 0x3f) << 12;
71
48
        value |= (*p++ & 0x3f) << 6;
72
48
        value |= *p++ & 0x3f;
73
48
        if (value < 0x10000)
74
11
            return -4;
75
37
        ret = 4;
76
204
    } else if ((*p & 0xfc) == 0xf8) {
77
89
        if (len < 5)
78
8
            return -1;
79
81
        if (((p[1] & 0xc0) != 0x80)
80
81
            || ((p[2] & 0xc0) != 0x80)
81
81
            || ((p[3] & 0xc0) != 0x80)
82
81
            || ((p[4] & 0xc0) != 0x80))
83
7
            return -3;
84
74
        value = ((unsigned long)(*p++ & 0x3)) << 24;
85
74
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
86
74
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
87
74
        value |= (*p++ & 0x3f) << 6;
88
74
        value |= *p++ & 0x3f;
89
74
        if (value < 0x200000)
90
18
            return -4;
91
56
        ret = 5;
92
115
    } else if ((*p & 0xfe) == 0xfc) {
93
74
        if (len < 6)
94
2
            return -1;
95
72
        if (((p[1] & 0xc0) != 0x80)
96
72
            || ((p[2] & 0xc0) != 0x80)
97
72
            || ((p[3] & 0xc0) != 0x80)
98
72
            || ((p[4] & 0xc0) != 0x80)
99
72
            || ((p[5] & 0xc0) != 0x80))
100
17
            return -3;
101
55
        value = ((unsigned long)(*p++ & 0x1)) << 30;
102
55
        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
103
55
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
104
55
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
105
55
        value |= (*p++ & 0x3f) << 6;
106
55
        value |= *p++ & 0x3f;
107
55
        if (value < 0x4000000)
108
21
            return -4;
109
34
        ret = 6;
110
34
    } else
111
41
        return -2;
112
126k
    *val = value;
113
126k
    return ret;
114
126k
}
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
133k
{
126
133k
    if (!str)
127
66.9k
        len = 6;                /* Maximum we will need */
128
66.9k
    else if (len <= 0)
129
0
        return -1;
130
133k
    if (value < 0x80) {
131
120k
        if (str)
132
60.4k
            *str = (unsigned char)value;
133
120k
        return 1;
134
120k
    }
135
13.0k
    if (value < 0x800) {
136
8.10k
        if (len < 2)
137
0
            return -1;
138
8.10k
        if (str) {
139
4.05k
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
140
4.05k
            *str = (unsigned char)((value & 0x3f) | 0x80);
141
4.05k
        }
142
8.10k
        return 2;
143
8.10k
    }
144
4.94k
    if (value < 0x10000) {
145
1.92k
        if (len < 3)
146
0
            return -1;
147
1.92k
        if (str) {
148
963
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
149
963
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
150
963
            *str = (unsigned char)((value & 0x3f) | 0x80);
151
963
        }
152
1.92k
        return 3;
153
1.92k
    }
154
3.01k
    if (value < 0x200000) {
155
276
        if (len < 4)
156
0
            return -1;
157
276
        if (str) {
158
138
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
159
138
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
160
138
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
161
138
            *str = (unsigned char)((value & 0x3f) | 0x80);
162
138
        }
163
276
        return 4;
164
276
    }
165
2.74k
    if (value < 0x4000000) {
166
540
        if (len < 5)
167
0
            return -1;
168
540
        if (str) {
169
270
            *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
170
270
            *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
171
270
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
172
270
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
173
270
            *str = (unsigned char)((value & 0x3f) | 0x80);
174
270
        }
175
540
        return 5;
176
540
    }
177
2.20k
    if (len < 6)
178
0
        return -1;
179
2.20k
    if (str) {
180
1.10k
        *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
181
1.10k
        *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
182
1.10k
        *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
183
1.10k
        *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
184
1.10k
        *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
185
1.10k
        *str = (unsigned char)((value & 0x3f) | 0x80);
186
1.10k
    }
187
2.20k
    return 6;
188
2.20k
}