1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58
59 #include <openssl/err.h>
60 #include <openssl/mem.h>
61
62 /* UTF8 utilities */
63
64 /*
65 * This parses a UTF8 string one character at a time. It is passed a pointer
66 * to the string and the length of the string. It sets 'value' to the value
67 * of the current character. It returns the number of characters read or a
68 * negative error code: -1 = string too short -2 = illegal character -3 =
69 * subsequent characters not of the form 10xxxxxx -4 = character encoded
70 * incorrectly (not minimal length).
71 */
72
UTF8_getc(const unsigned char * str,int len,unsigned long * val)73 int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
74 {
75 const unsigned char *p;
76 unsigned long value;
77 int ret;
78 if (len <= 0)
79 return 0;
80 p = str;
81
82 /* Check syntax and work out the encoded value (if correct) */
83 if ((*p & 0x80) == 0) {
84 value = *p++ & 0x7f;
85 ret = 1;
86 } else if ((*p & 0xe0) == 0xc0) {
87 if (len < 2)
88 return -1;
89 if ((p[1] & 0xc0) != 0x80)
90 return -3;
91 value = (*p++ & 0x1f) << 6;
92 value |= *p++ & 0x3f;
93 if (value < 0x80)
94 return -4;
95 ret = 2;
96 } else if ((*p & 0xf0) == 0xe0) {
97 if (len < 3)
98 return -1;
99 if (((p[1] & 0xc0) != 0x80)
100 || ((p[2] & 0xc0) != 0x80))
101 return -3;
102 value = (*p++ & 0xf) << 12;
103 value |= (*p++ & 0x3f) << 6;
104 value |= *p++ & 0x3f;
105 if (value < 0x800)
106 return -4;
107 ret = 3;
108 } else if ((*p & 0xf8) == 0xf0) {
109 if (len < 4)
110 return -1;
111 if (((p[1] & 0xc0) != 0x80)
112 || ((p[2] & 0xc0) != 0x80)
113 || ((p[3] & 0xc0) != 0x80))
114 return -3;
115 value = ((unsigned long)(*p++ & 0x7)) << 18;
116 value |= (*p++ & 0x3f) << 12;
117 value |= (*p++ & 0x3f) << 6;
118 value |= *p++ & 0x3f;
119 if (value < 0x10000)
120 return -4;
121 ret = 4;
122 } else if ((*p & 0xfc) == 0xf8) {
123 if (len < 5)
124 return -1;
125 if (((p[1] & 0xc0) != 0x80)
126 || ((p[2] & 0xc0) != 0x80)
127 || ((p[3] & 0xc0) != 0x80)
128 || ((p[4] & 0xc0) != 0x80))
129 return -3;
130 value = ((unsigned long)(*p++ & 0x3)) << 24;
131 value |= ((unsigned long)(*p++ & 0x3f)) << 18;
132 value |= ((unsigned long)(*p++ & 0x3f)) << 12;
133 value |= (*p++ & 0x3f) << 6;
134 value |= *p++ & 0x3f;
135 if (value < 0x200000)
136 return -4;
137 ret = 5;
138 } else if ((*p & 0xfe) == 0xfc) {
139 if (len < 6)
140 return -1;
141 if (((p[1] & 0xc0) != 0x80)
142 || ((p[2] & 0xc0) != 0x80)
143 || ((p[3] & 0xc0) != 0x80)
144 || ((p[4] & 0xc0) != 0x80)
145 || ((p[5] & 0xc0) != 0x80))
146 return -3;
147 value = ((unsigned long)(*p++ & 0x1)) << 30;
148 value |= ((unsigned long)(*p++ & 0x3f)) << 24;
149 value |= ((unsigned long)(*p++ & 0x3f)) << 18;
150 value |= ((unsigned long)(*p++ & 0x3f)) << 12;
151 value |= (*p++ & 0x3f) << 6;
152 value |= *p++ & 0x3f;
153 if (value < 0x4000000)
154 return -4;
155 ret = 6;
156 } else
157 return -2;
158 *val = value;
159 return ret;
160 }
161
162 /*
163 * This takes a character 'value' and writes the UTF8 encoded value in 'str'
164 * where 'str' is a buffer containing 'len' characters. Returns the number of
165 * characters written or -1 if 'len' is too small. 'str' can be set to NULL
166 * in which case it just returns the number of characters. It will need at
167 * most 6 characters.
168 */
169
UTF8_putc(unsigned char * str,int len,unsigned long value)170 int UTF8_putc(unsigned char *str, int len, unsigned long value)
171 {
172 if (!str)
173 len = 6; /* Maximum we will need */
174 else if (len <= 0)
175 return -1;
176 if (value < 0x80) {
177 if (str)
178 *str = (unsigned char)value;
179 return 1;
180 }
181 if (value < 0x800) {
182 if (len < 2)
183 return -1;
184 if (str) {
185 *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
186 *str = (unsigned char)((value & 0x3f) | 0x80);
187 }
188 return 2;
189 }
190 if (value < 0x10000) {
191 if (len < 3)
192 return -1;
193 if (str) {
194 *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
195 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
196 *str = (unsigned char)((value & 0x3f) | 0x80);
197 }
198 return 3;
199 }
200 if (value < 0x200000) {
201 if (len < 4)
202 return -1;
203 if (str) {
204 *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
205 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
206 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
207 *str = (unsigned char)((value & 0x3f) | 0x80);
208 }
209 return 4;
210 }
211 if (value < 0x4000000) {
212 if (len < 5)
213 return -1;
214 if (str) {
215 *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
216 *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
217 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
218 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
219 *str = (unsigned char)((value & 0x3f) | 0x80);
220 }
221 return 5;
222 }
223 if (len < 6)
224 return -1;
225 if (str) {
226 *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
227 *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
228 *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
229 *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
230 *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
231 *str = (unsigned char)((value & 0x3f) | 0x80);
232 }
233 return 6;
234 }
235