Coverage Report

Created: 2018-08-29 13:53

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