]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* crypto/asn1/asn1_lib.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
3 | * All rights reserved. |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 8 | * |
d02b48c6 RE |
9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 15 | * |
d02b48c6 RE |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 22 | * |
d02b48c6 RE |
23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
d02b48c6 RE |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
0f113f3e | 52 | * |
d02b48c6 RE |
53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
f9082268 | 60 | #include <limits.h> |
b39fc560 | 61 | #include "internal/cryptlib.h" |
ec577822 | 62 | #include <openssl/asn1.h> |
d02b48c6 | 63 | |
0f113f3e MC |
64 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
65 | int max); | |
d02b48c6 | 66 | static void asn1_put_length(unsigned char **pp, int length); |
d02b48c6 | 67 | |
875a644a | 68 | static int _asn1_check_infinite_end(const unsigned char **p, long len) |
0f113f3e MC |
69 | { |
70 | /* | |
71 | * If there is 0 or 1 byte left, the length check should pick things up | |
72 | */ | |
73 | if (len <= 0) | |
74 | return (1); | |
75 | else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { | |
76 | (*p) += 2; | |
77 | return (1); | |
78 | } | |
79 | return (0); | |
80 | } | |
d02b48c6 | 81 | |
6343829a | 82 | int ASN1_check_infinite_end(unsigned char **p, long len) |
0f113f3e MC |
83 | { |
84 | return _asn1_check_infinite_end((const unsigned char **)p, len); | |
85 | } | |
d02b48c6 | 86 | |
6343829a | 87 | int ASN1_const_check_infinite_end(const unsigned char **p, long len) |
0f113f3e MC |
88 | { |
89 | return _asn1_check_infinite_end(p, len); | |
90 | } | |
875a644a | 91 | |
6343829a | 92 | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
0f113f3e MC |
93 | int *pclass, long omax) |
94 | { | |
95 | int i, ret; | |
96 | long l; | |
97 | const unsigned char *p = *pp; | |
98 | int tag, xclass, inf; | |
99 | long max = omax; | |
100 | ||
101 | if (!max) | |
102 | goto err; | |
103 | ret = (*p & V_ASN1_CONSTRUCTED); | |
104 | xclass = (*p & V_ASN1_PRIVATE); | |
105 | i = *p & V_ASN1_PRIMITIVE_TAG; | |
106 | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ | |
107 | p++; | |
108 | if (--max == 0) | |
109 | goto err; | |
110 | l = 0; | |
111 | while (*p & 0x80) { | |
112 | l <<= 7L; | |
113 | l |= *(p++) & 0x7f; | |
114 | if (--max == 0) | |
115 | goto err; | |
116 | if (l > (INT_MAX >> 7L)) | |
117 | goto err; | |
118 | } | |
119 | l <<= 7L; | |
120 | l |= *(p++) & 0x7f; | |
121 | tag = (int)l; | |
122 | if (--max == 0) | |
123 | goto err; | |
124 | } else { | |
125 | tag = i; | |
126 | p++; | |
127 | if (--max == 0) | |
128 | goto err; | |
129 | } | |
130 | *ptag = tag; | |
131 | *pclass = xclass; | |
132 | if (!asn1_get_length(&p, &inf, plength, (int)max)) | |
133 | goto err; | |
134 | ||
135 | if (inf && !(ret & V_ASN1_CONSTRUCTED)) | |
136 | goto err; | |
398e99fe | 137 | |
0f113f3e MC |
138 | if (*plength > (omax - (p - *pp))) { |
139 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG); | |
140 | /* | |
141 | * Set this so that even if things are not long enough the values are | |
142 | * set correctly | |
143 | */ | |
144 | ret |= 0x80; | |
145 | } | |
146 | *pp = p; | |
147 | return (ret | inf); | |
148 | err: | |
149 | ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG); | |
150 | return (0x80); | |
151 | } | |
152 | ||
153 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, | |
154 | int max) | |
155 | { | |
156 | const unsigned char *p = *pp; | |
157 | unsigned long ret = 0; | |
158 | unsigned int i; | |
159 | ||
160 | if (max-- < 1) | |
161 | return (0); | |
162 | if (*p == 0x80) { | |
163 | *inf = 1; | |
164 | ret = 0; | |
165 | p++; | |
166 | } else { | |
167 | *inf = 0; | |
168 | i = *p & 0x7f; | |
169 | if (*(p++) & 0x80) { | |
170 | if (max < (int)i) | |
171 | return 0; | |
172 | /* Skip leading zeroes */ | |
173 | while (i && *p == 0) { | |
174 | p++; | |
175 | i--; | |
176 | } | |
177 | if (i > sizeof(long)) | |
178 | return 0; | |
179 | while (i-- > 0) { | |
180 | ret <<= 8L; | |
181 | ret |= *(p++); | |
182 | } | |
183 | } else | |
184 | ret = i; | |
185 | } | |
186 | if (ret > LONG_MAX) | |
187 | return 0; | |
188 | *pp = p; | |
189 | *rl = (long)ret; | |
190 | return (1); | |
191 | } | |
192 | ||
193 | /* | |
194 | * class 0 is constructed constructed == 2 for indefinite length constructed | |
195 | */ | |
6343829a | 196 | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
0f113f3e MC |
197 | int xclass) |
198 | { | |
199 | unsigned char *p = *pp; | |
200 | int i, ttag; | |
201 | ||
202 | i = (constructed) ? V_ASN1_CONSTRUCTED : 0; | |
203 | i |= (xclass & V_ASN1_PRIVATE); | |
204 | if (tag < 31) | |
205 | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); | |
206 | else { | |
207 | *(p++) = i | V_ASN1_PRIMITIVE_TAG; | |
208 | for (i = 0, ttag = tag; ttag > 0; i++) | |
209 | ttag >>= 7; | |
210 | ttag = i; | |
211 | while (i-- > 0) { | |
212 | p[i] = tag & 0x7f; | |
213 | if (i != (ttag - 1)) | |
214 | p[i] |= 0x80; | |
215 | tag >>= 7; | |
216 | } | |
217 | p += ttag; | |
218 | } | |
219 | if (constructed == 2) | |
220 | *(p++) = 0x80; | |
221 | else | |
222 | asn1_put_length(&p, length); | |
223 | *pp = p; | |
224 | } | |
d02b48c6 | 225 | |
230fd6b7 | 226 | int ASN1_put_eoc(unsigned char **pp) |
0f113f3e MC |
227 | { |
228 | unsigned char *p = *pp; | |
229 | *p++ = 0; | |
230 | *p++ = 0; | |
231 | *pp = p; | |
232 | return 2; | |
233 | } | |
230fd6b7 | 234 | |
6b691a5c | 235 | static void asn1_put_length(unsigned char **pp, int length) |
0f113f3e MC |
236 | { |
237 | unsigned char *p = *pp; | |
238 | int i, l; | |
239 | if (length <= 127) | |
240 | *(p++) = (unsigned char)length; | |
241 | else { | |
242 | l = length; | |
243 | for (i = 0; l > 0; i++) | |
244 | l >>= 8; | |
245 | *(p++) = i | 0x80; | |
246 | l = i; | |
247 | while (i-- > 0) { | |
248 | p[i] = length & 0xff; | |
249 | length >>= 8; | |
250 | } | |
251 | p += l; | |
252 | } | |
253 | *pp = p; | |
254 | } | |
d02b48c6 | 255 | |
6343829a | 256 | int ASN1_object_size(int constructed, int length, int tag) |
0f113f3e MC |
257 | { |
258 | int ret; | |
259 | ||
260 | ret = length; | |
261 | ret++; | |
262 | if (tag >= 31) { | |
263 | while (tag > 0) { | |
264 | tag >>= 7; | |
265 | ret++; | |
266 | } | |
267 | } | |
268 | if (constructed == 2) | |
269 | return ret + 3; | |
270 | ret++; | |
271 | if (length > 127) { | |
272 | while (length > 0) { | |
273 | length >>= 8; | |
274 | ret++; | |
275 | } | |
276 | } | |
277 | return (ret); | |
278 | } | |
d02b48c6 | 279 | |
18f54773 | 280 | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) |
0f113f3e MC |
281 | { |
282 | if (str == NULL) | |
283 | return 0; | |
284 | dst->type = str->type; | |
285 | if (!ASN1_STRING_set(dst, str->data, str->length)) | |
286 | return 0; | |
287 | dst->flags = str->flags; | |
288 | return 1; | |
289 | } | |
18f54773 | 290 | |
6384e46d | 291 | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) |
0f113f3e MC |
292 | { |
293 | ASN1_STRING *ret; | |
294 | if (!str) | |
295 | return NULL; | |
296 | ret = ASN1_STRING_new(); | |
297 | if (!ret) | |
298 | return NULL; | |
299 | if (!ASN1_STRING_copy(ret, str)) { | |
300 | ASN1_STRING_free(ret); | |
301 | return NULL; | |
302 | } | |
303 | return ret; | |
304 | } | |
d02b48c6 | 305 | |
6343829a | 306 | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) |
0f113f3e MC |
307 | { |
308 | unsigned char *c; | |
309 | const char *data = _data; | |
310 | ||
311 | if (len < 0) { | |
312 | if (data == NULL) | |
313 | return (0); | |
314 | else | |
315 | len = strlen(data); | |
316 | } | |
317 | if ((str->length < len) || (str->data == NULL)) { | |
318 | c = str->data; | |
2d29e2df | 319 | str->data = OPENSSL_realloc(c, len + 1); |
0f113f3e MC |
320 | if (str->data == NULL) { |
321 | ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); | |
322 | str->data = c; | |
323 | return (0); | |
324 | } | |
325 | } | |
326 | str->length = len; | |
327 | if (data != NULL) { | |
328 | memcpy(str->data, data, len); | |
329 | /* an allowance for strings :-) */ | |
330 | str->data[len] = '\0'; | |
331 | } | |
332 | return (1); | |
333 | } | |
d02b48c6 | 334 | |
6343829a | 335 | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) |
0f113f3e | 336 | { |
b548a1f1 | 337 | OPENSSL_free(str->data); |
0f113f3e MC |
338 | str->data = data; |
339 | str->length = len; | |
340 | } | |
399a6f0b | 341 | |
6b691a5c | 342 | ASN1_STRING *ASN1_STRING_new(void) |
0f113f3e MC |
343 | { |
344 | return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); | |
345 | } | |
d02b48c6 | 346 | |
6b691a5c | 347 | ASN1_STRING *ASN1_STRING_type_new(int type) |
0f113f3e MC |
348 | { |
349 | ASN1_STRING *ret; | |
350 | ||
64b25758 | 351 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
352 | if (ret == NULL) { |
353 | ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE); | |
354 | return (NULL); | |
355 | } | |
0f113f3e | 356 | ret->type = type; |
0f113f3e MC |
357 | return (ret); |
358 | } | |
d02b48c6 | 359 | |
6b691a5c | 360 | void ASN1_STRING_free(ASN1_STRING *a) |
0f113f3e MC |
361 | { |
362 | if (a == NULL) | |
363 | return; | |
b548a1f1 | 364 | if (!(a->flags & ASN1_STRING_FLAG_NDEF)) |
0f113f3e MC |
365 | OPENSSL_free(a->data); |
366 | OPENSSL_free(a); | |
367 | } | |
d02b48c6 | 368 | |
a8ae0891 DSH |
369 | void ASN1_STRING_clear_free(ASN1_STRING *a) |
370 | { | |
0dfb9398 RS |
371 | if (a == NULL) |
372 | return; | |
373 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) | |
a8ae0891 DSH |
374 | OPENSSL_cleanse(a->data, a->length); |
375 | ASN1_STRING_free(a); | |
376 | } | |
377 | ||
6384e46d | 378 | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) |
0f113f3e MC |
379 | { |
380 | int i; | |
381 | ||
382 | i = (a->length - b->length); | |
383 | if (i == 0) { | |
384 | i = memcmp(a->data, b->data, a->length); | |
385 | if (i == 0) | |
386 | return (a->type - b->type); | |
387 | else | |
388 | return (i); | |
389 | } else | |
390 | return (i); | |
391 | } | |
d02b48c6 | 392 | |
6343829a | 393 | int ASN1_STRING_length(const ASN1_STRING *x) |
0f113f3e | 394 | { |
f422a514 | 395 | return x->length; |
0f113f3e | 396 | } |
08e9c1af | 397 | |
6343829a | 398 | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
0f113f3e | 399 | { |
f422a514 | 400 | x->length = len; |
0f113f3e | 401 | } |
08e9c1af DSH |
402 | |
403 | int ASN1_STRING_type(ASN1_STRING *x) | |
0f113f3e | 404 | { |
f422a514 | 405 | return x->type; |
0f113f3e MC |
406 | } |
407 | ||
408 | unsigned char *ASN1_STRING_data(ASN1_STRING *x) | |
409 | { | |
f422a514 | 410 | return x->data; |
0f113f3e | 411 | } |