/src/openssl/crypto/asn1/a_utf8.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 "internal/unicode.h" |
13 | | #include <openssl/asn1.h> |
14 | | #include <crypto/asn1.h> |
15 | | |
16 | | /* UTF8 utilities */ |
17 | | |
18 | | /*- |
19 | | * This parses a UTF8 string one character at a time. It is passed a pointer |
20 | | * to the string and the length of the string. It sets 'value' to the value of |
21 | | * the current character. It returns the number of characters read or a |
22 | | * negative error code: |
23 | | * -1 = string too short |
24 | | * -2 = illegal character |
25 | | * -3 = subsequent characters not of the form 10xxxxxx |
26 | | * -4 = character encoded incorrectly (not minimal length). |
27 | | */ |
28 | | |
29 | | int ossl_utf8_getc_internal(const unsigned char *str, int len, uint32_t *val) |
30 | 0 | { |
31 | 0 | const unsigned char *p; |
32 | 0 | uint32_t value; |
33 | 0 | int ret; |
34 | 0 | if (len <= 0) |
35 | 0 | return 0; |
36 | 0 | p = str; |
37 | | |
38 | | /* Check syntax and work out the encoded value (if correct) */ |
39 | 0 | if ((*p & 0x80) == 0) { |
40 | 0 | value = *p++ & 0x7f; |
41 | 0 | ret = 1; |
42 | 0 | } else if ((*p & 0xe0) == 0xc0) { |
43 | 0 | if (len < 2) |
44 | 0 | return -1; |
45 | 0 | if ((p[1] & 0xc0) != 0x80) |
46 | 0 | return -3; |
47 | 0 | value = (*p++ & 0x1f) << 6; |
48 | 0 | value |= *p++ & 0x3f; |
49 | 0 | if (value < 0x80) |
50 | 0 | return -4; |
51 | 0 | ret = 2; |
52 | 0 | } else if ((*p & 0xf0) == 0xe0) { |
53 | 0 | if (len < 3) |
54 | 0 | return -1; |
55 | 0 | if (((p[1] & 0xc0) != 0x80) |
56 | 0 | || ((p[2] & 0xc0) != 0x80)) |
57 | 0 | return -3; |
58 | 0 | value = (*p++ & 0xf) << 12; |
59 | 0 | value |= (*p++ & 0x3f) << 6; |
60 | 0 | value |= *p++ & 0x3f; |
61 | 0 | if (value < 0x800) |
62 | 0 | return -4; |
63 | 0 | if (is_unicode_surrogate(value)) |
64 | 0 | return -2; |
65 | 0 | ret = 3; |
66 | 0 | } else if ((*p & 0xf8) == 0xf0) { |
67 | 0 | if (len < 4) |
68 | 0 | return -1; |
69 | 0 | if (((p[1] & 0xc0) != 0x80) |
70 | 0 | || ((p[2] & 0xc0) != 0x80) |
71 | 0 | || ((p[3] & 0xc0) != 0x80)) |
72 | 0 | return -3; |
73 | 0 | value = ((unsigned long)(*p++ & 0x7)) << 18; |
74 | 0 | value |= (*p++ & 0x3f) << 12; |
75 | 0 | value |= (*p++ & 0x3f) << 6; |
76 | 0 | value |= *p++ & 0x3f; |
77 | 0 | if (value < 0x10000 || value >= UNICODE_LIMIT) |
78 | 0 | return -4; |
79 | 0 | ret = 4; |
80 | 0 | } else |
81 | 0 | return -2; |
82 | 0 | *val = value; |
83 | 0 | return ret; |
84 | 0 | } |
85 | | |
86 | | #if !defined(OPENSSL_NO_DEPRECATED_4_1) |
87 | | int UTF8_getc(const unsigned char *str, int len, unsigned long *val) |
88 | 0 | { |
89 | 0 | uint32_t value = 0; |
90 | 0 | int ret; |
91 | |
|
92 | 0 | ret = ossl_utf8_getc_internal(str, len, &value); |
93 | |
|
94 | 0 | if (ret) |
95 | 0 | *val = (unsigned long)value; |
96 | |
|
97 | 0 | return ret; |
98 | 0 | } |
99 | | #endif /* !defined(OPENSSL_NO_DEPRECATED_4_1) */ |
100 | | |
101 | | /* |
102 | | * This takes a character 'value' and writes the UTF8 encoded value in 'str' |
103 | | * where 'str' is a buffer containing 'len' characters. Returns the number of |
104 | | * characters written, -1 if 'len' is too small or -2 if 'value' is out of |
105 | | * range. 'str' can be set to NULL in which case it just returns the number of |
106 | | * characters. It will need at most 4 characters. |
107 | | */ |
108 | | |
109 | | int ossl_utf8_putc_internal(unsigned char *str, int len, uint32_t value) |
110 | 0 | { |
111 | 0 | if (!str) |
112 | 0 | len = 4; /* Maximum we will need */ |
113 | 0 | else if (len <= 0) |
114 | 0 | return -1; |
115 | 0 | if (value < 0x80) { |
116 | 0 | if (str) |
117 | 0 | *str = (unsigned char)value; |
118 | 0 | return 1; |
119 | 0 | } |
120 | 0 | if (value < 0x800) { |
121 | 0 | if (len < 2) |
122 | 0 | return -1; |
123 | 0 | if (str) { |
124 | 0 | *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0); |
125 | 0 | *str = (unsigned char)((value & 0x3f) | 0x80); |
126 | 0 | } |
127 | 0 | return 2; |
128 | 0 | } |
129 | 0 | if (value < 0x10000) { |
130 | 0 | if (is_unicode_surrogate(value)) |
131 | 0 | return -2; |
132 | 0 | if (len < 3) |
133 | 0 | return -1; |
134 | 0 | if (str) { |
135 | 0 | *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0); |
136 | 0 | *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); |
137 | 0 | *str = (unsigned char)((value & 0x3f) | 0x80); |
138 | 0 | } |
139 | 0 | return 3; |
140 | 0 | } |
141 | 0 | if (value < UNICODE_LIMIT) { |
142 | 0 | if (len < 4) |
143 | 0 | return -1; |
144 | 0 | if (str) { |
145 | 0 | *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0); |
146 | 0 | *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80); |
147 | 0 | *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80); |
148 | 0 | *str = (unsigned char)((value & 0x3f) | 0x80); |
149 | 0 | } |
150 | 0 | return 4; |
151 | 0 | } |
152 | 0 | return -2; |
153 | 0 | } |
154 | | |
155 | | #if !defined(OPENSSL_NO_DEPRECATED_4_1) |
156 | | int UTF8_putc(unsigned char *str, int len, unsigned long value) |
157 | 0 | { |
158 | 0 | return ossl_utf8_putc_internal(str, len, (uint32_t)value); |
159 | 0 | } |
160 | | #endif |