Debian Codegraph @

external/boringssl/crypto/asn1/asn1_lib.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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 <limits.h>
60 #include <string.h>
61
62 #include <openssl/asn1_mac.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65
66 /* Cross-module errors from crypto/x509/i2d_pr.c. */
67 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE);
68
69 /* Cross-module errors from crypto/x509/algorithm.c. */
70 OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED);
71 OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
72 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
73 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM);
74 OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE);
75 /*
76 * Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
77 * these once asn1_gen.c is gone.
78 */
79 OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED);
80 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT);
81 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN);
82 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT);
83 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX);
84 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG);
85 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER);
86 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING);
87 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE);
88 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT);
89 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE);
90 OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT);
91 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER);
92 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER);
93 OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR);
94 OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE);
95 OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT);
96 OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT);
97 OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG);
98 OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT);
99 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT);
100 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG);
101 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE);
102
103 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
104 long max);
105 static void asn1_put_length(unsigned char **pp, int length);
106
107 static int _asn1_check_infinite_end(const unsigned char **p, long len)
108 {
109 /*
110 * If there is 0 or 1 byte left, the length check should pick things up
111 */
112 if (len <= 0)
113 return (1);
114 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
115 (*p) += 2;
116 return (1);
117 }
118 return (0);
119 }
120
121 int ASN1_check_infinite_end(unsigned char **p, long len)
122 {
123 return _asn1_check_infinite_end((const unsigned char **)p, len);
124 }
125
126 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
127 {
128 return _asn1_check_infinite_end(p, len);
129 }
130
131 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
132 int *pclass, long omax)
133 {
134 int i, ret;
135 long l;
136 const unsigned char *p = *pp;
137 int tag, xclass, inf;
138 long max = omax;
139
140 if (!max)
141 goto err;
142 ret = (*p & V_ASN1_CONSTRUCTED);
143 xclass = (*p & V_ASN1_PRIVATE);
144 i = *p & V_ASN1_PRIMITIVE_TAG;
145 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
146 p++;
147 if (--max == 0)
148 goto err;
149 l = 0;
150 while (*p & 0x80) {
151 l <<= 7L;
152 l |= *(p++) & 0x7f;
153 if (--max == 0)
154 goto err;
155 if (l > (INT_MAX >> 7L))
156 goto err;
157 }
158 l <<= 7L;
159 l |= *(p++) & 0x7f;
160 tag = (int)l;
161 if (--max == 0)
162 goto err;
163 } else {
164 tag = i;
165 p++;
166 if (--max == 0)
167 goto err;
168 }
169
170 /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */
171 if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL)
172 goto err;
173
174 *ptag = tag;
175 *pclass = xclass;
176 if (!asn1_get_length(&p, &inf, plength, max))
177 goto err;
178
179 if (inf && !(ret & V_ASN1_CONSTRUCTED))
180 goto err;
181
182 #if 0
183 fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
184 (int)p, *plength, omax, (int)*pp, (int)(p + *plength),
185 (int)(omax + *pp));
186
187 #endif
188 if (*plength > (omax - (p - *pp))) {
189 OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
190 /*
191 * Set this so that even if things are not long enough the values are
192 * set correctly
193 */
194 ret |= 0x80;
195 }
196 *pp = p;
197 return (ret | inf);
198 err:
199 OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
200 return (0x80);
201 }
202
203 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
204 long max)
205 {
206 const unsigned char *p = *pp;
207 unsigned long ret = 0;
208 unsigned long i;
209
210 if (max-- < 1)
211 return 0;
212 if (*p == 0x80) {
213 *inf = 1;
214 ret = 0;
215 p++;
216 } else {
217 *inf = 0;
218 i = *p & 0x7f;
219 if (*(p++) & 0x80) {
220 if (i > sizeof(ret) || max < (long)i)
221 return 0;
222 while (i-- > 0) {
223 ret <<= 8L;
224 ret |= *(p++);
225 }
226 } else
227 ret = i;
228 }
229 if (ret > LONG_MAX)
230 return 0;
231 *pp = p;
232 *rl = (long)ret;
233 return 1;
234 }
235
236 /*
237 * class 0 is constructed constructed == 2 for indefinite length constructed
238 */
239 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
240 int xclass)
241 {
242 unsigned char *p = *pp;
243 int i, ttag;
244
245 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
246 i |= (xclass & V_ASN1_PRIVATE);
247 if (tag < 31)
248 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
249 else {
250 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
251 for (i = 0, ttag = tag; ttag > 0; i++)
252 ttag >>= 7;
253 ttag = i;
254 while (i-- > 0) {
255 p[i] = tag & 0x7f;
256 if (i != (ttag - 1))
257 p[i] |= 0x80;
258 tag >>= 7;
259 }
260 p += ttag;
261 }
262 if (constructed == 2)
263 *(p++) = 0x80;
264 else
265 asn1_put_length(&p, length);
266 *pp = p;
267 }
268
269 int ASN1_put_eoc(unsigned char **pp)
270 {
271 unsigned char *p = *pp;
272 *p++ = 0;
273 *p++ = 0;
274 *pp = p;
275 return 2;
276 }
277
278 static void asn1_put_length(unsigned char **pp, int length)
279 {
280 unsigned char *p = *pp;
281 int i, l;
282 if (length <= 127)
283 *(p++) = (unsigned char)length;
284 else {
285 l = length;
286 for (i = 0; l > 0; i++)
287 l >>= 8;
288 *(p++) = i | 0x80;
289 l = i;
290 while (i-- > 0) {
291 p[i] = length & 0xff;
292 length >>= 8;
293 }
294 p += l;
295 }
296 *pp = p;
297 }
298
299 int ASN1_object_size(int constructed, int length, int tag)
300 {
301 int ret;
302
303 ret = length;
304 ret++;
305 if (tag >= 31) {
306 while (tag > 0) {
307 tag >>= 7;
308 ret++;
309 }
310 }
311 if (constructed == 2)
312 return ret + 3;
313 ret++;
314 if (length > 127) {
315 while (length > 0) {
316 length >>= 8;
317 ret++;
318 }
319 }
320 return (ret);
321 }
322
323 static int _asn1_Finish(ASN1_const_CTX *c)
324 {
325 if ((c->inf == (1 | V_ASN1_CONSTRUCTED)) && (!c->eos)) {
326 if (!ASN1_const_check_infinite_end(&c->p, c->slen)) {
327 c->error = ASN1_R_MISSING_ASN1_EOS;
328 return (0);
329 }
330 }
331 if (((c->slen != 0) && !(c->inf & 1)) || ((c->slen < 0) && (c->inf & 1))) {
332 c->error = ASN1_R_ASN1_LENGTH_MISMATCH;
333 return (0);
334 }
335 return (1);
336 }
337
338 int asn1_Finish(ASN1_CTX *c)
339 {
340 return _asn1_Finish((ASN1_const_CTX *)c);
341 }
342
343 int asn1_const_Finish(ASN1_const_CTX *c)
344 {
345 return _asn1_Finish(c);
346 }
347
348 int asn1_GetSequence(ASN1_const_CTX *c, long *length)
349 {
350 const unsigned char *q;
351
352 q = c->p;
353 c->inf = ASN1_get_object(&(c->p), &(c->slen), &(c->tag), &(c->xclass),
354 *length);
355 if (c->inf & 0x80) {
356 c->error = ASN1_R_BAD_GET_ASN1_OBJECT_CALL;
357 return (0);
358 }
359 if (c->tag != V_ASN1_SEQUENCE) {
360 c->error = ASN1_R_EXPECTING_AN_ASN1_SEQUENCE;
361 return (0);
362 }
363 (*length) -= (c->p - q);
364 if (c->max && (*length < 0)) {
365 c->error = ASN1_R_ASN1_LENGTH_MISMATCH;
366 return (0);
367 }
368 if (c->inf == (1 | V_ASN1_CONSTRUCTED))
369 c->slen = *length + *(c->pp) - c->p;
370 c->eos = 0;
371 return (1);
372 }
373
374 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
375 {
376 if (str == NULL)
377 return 0;
378 dst->type = str->type;
379 if (!ASN1_STRING_set(dst, str->data, str->length))
380 return 0;
381 dst->flags = str->flags;
382 return 1;
383 }
384
385 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
386 {
387 ASN1_STRING *ret;
388 if (!str)
389 return NULL;
390 ret = ASN1_STRING_new();
391 if (!ret)
392 return NULL;
393 if (!ASN1_STRING_copy(ret, str)) {
394 ASN1_STRING_free(ret);
395 return NULL;
396 }
397 return ret;
398 }
399
400 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
401 {
402 unsigned char *c;
403 const char *data = _data;
404
405 if (len < 0) {
406 if (data == NULL)
407 return (0);
408 else
409 len = strlen(data);
410 }
411 if ((str->length < len) || (str->data == NULL)) {
412 c = str->data;
413 if (c == NULL)
414 str->data = OPENSSL_malloc(len + 1);
415 else
416 str->data = OPENSSL_realloc(c, len + 1);
417
418 if (str->data == NULL) {
419 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
420 str->data = c;
421 return (0);
422 }
423 }
424 str->length = len;
425 if (data != NULL) {
426 memcpy(str->data, data, len);
427 /* an allowance for strings :-) */
428 str->data[len] = '\0';
429 }
430 return (1);
431 }
432
433 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
434 {
435 if (str->data)
436 OPENSSL_free(str->data);
437 str->data = data;
438 str->length = len;
439 }
440
441 ASN1_STRING *ASN1_STRING_new(void)
442 {
443 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
444 }
445
446 ASN1_STRING *ASN1_STRING_type_new(int type)
447 {
448 ASN1_STRING *ret;
449
450 ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
451 if (ret == NULL) {
452 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
453 return (NULL);
454 }
455 ret->length = 0;
456 ret->type = type;
457 ret->data = NULL;
458 ret->flags = 0;
459 return (ret);
460 }
461
462 void ASN1_STRING_free(ASN1_STRING *a)
463 {
464 if (a == NULL)
465 return;
466 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
467 OPENSSL_free(a->data);
468 OPENSSL_free(a);
469 }
470
471 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
472 {
473 int i;
474
475 i = (a->length - b->length);
476 if (i == 0) {
477 i = memcmp(a->data, b->data, a->length);
478 if (i == 0)
479 return (a->type - b->type);
480 else
481 return (i);
482 } else
483 return (i);
484 }
485
486 int ASN1_STRING_length(const ASN1_STRING *x)
487 {
488 return M_ASN1_STRING_length(x);
489 }
490
491 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
492 {
493 M_ASN1_STRING_length_set(x, len);
494 return;
495 }
496
497 int ASN1_STRING_type(ASN1_STRING *x)
498 {
499 return M_ASN1_STRING_type(x);
500 }
501
502 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
503 {
504 return M_ASN1_STRING_data(x);
505 }