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