xref: /openbsd-src/lib/libcrypto/asn1/asn1_lib.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: asn1_lib.c,v 1.32 2014/07/11 14:49:12 miod Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
8  *
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).
15  *
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.
22  *
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 :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
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.
52  *
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 <limits.h>
60 #include <stdio.h>
61 #include <string.h>
62 
63 #include <openssl/asn1.h>
64 #include <openssl/asn1_mac.h>
65 #include <openssl/err.h>
66 
67 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max);
68 static void asn1_put_length(unsigned char **pp, int length);
69 
70 static int
71 _asn1_check_infinite_end(const unsigned char **p, long len)
72 {
73 	/* If there is 0 or 1 byte left, the length check should pick
74 	 * things up */
75 	if (len <= 0)
76 		return (1);
77 	else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
78 		(*p) += 2;
79 		return (1);
80 	}
81 	return (0);
82 }
83 
84 int
85 ASN1_check_infinite_end(unsigned char **p, long len)
86 {
87 	return _asn1_check_infinite_end((const unsigned char **)p, len);
88 }
89 
90 int
91 ASN1_const_check_infinite_end(const unsigned char **p, long len)
92 {
93 	return _asn1_check_infinite_end(p, len);
94 }
95 
96 int
97 ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
98     int *pclass, long omax)
99 {
100 	int i, ret;
101 	long l;
102 	const unsigned char *p= *pp;
103 	int tag, xclass, inf;
104 	long max = omax;
105 
106 	if (!max)
107 		goto err;
108 	ret = (*p & V_ASN1_CONSTRUCTED);
109 	xclass = (*p & V_ASN1_PRIVATE);
110 	i= *p & V_ASN1_PRIMITIVE_TAG;
111 	if (i == V_ASN1_PRIMITIVE_TAG) {		/* high-tag */
112 		p++;
113 		if (--max == 0)
114 			goto err;
115 		l = 0;
116 		while (*p & 0x80) {
117 			l <<= 7L;
118 			l |= *(p++) & 0x7f;
119 			if (--max == 0)
120 				goto err;
121 			if (l > (INT_MAX >> 7L))
122 				goto err;
123 		}
124 		l <<= 7L;
125 		l |= *(p++) & 0x7f;
126 		tag = (int)l;
127 		if (--max == 0)
128 			goto err;
129 	} else {
130 		tag = i;
131 		p++;
132 		if (--max == 0)
133 			goto err;
134 	}
135 	*ptag = tag;
136 	*pclass = xclass;
137 	if (!asn1_get_length(&p, &inf, plength, (int)max))
138 		goto err;
139 
140 	if (inf && !(ret & V_ASN1_CONSTRUCTED))
141 		goto err;
142 
143 #if 0
144 	fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d  (%d > %d)\n",
145 	    (int)p, *plength, omax, (int)*pp, (int)(p+ *plength),
146 	    (int)(omax+ *pp));
147 
148 #endif
149 	if (*plength > (omax - (p - *pp))) {
150 		ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
151 		/* Set this so that even if things are not long enough
152 		 * the values are set correctly */
153 		ret |= 0x80;
154 	}
155 	*pp = p;
156 	return (ret | inf);
157 
158 err:
159 	ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
160 	return (0x80);
161 }
162 
163 static int
164 asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max)
165 {
166 	const unsigned char *p= *pp;
167 	unsigned long ret = 0;
168 	unsigned int i;
169 
170 	if (max-- < 1)
171 		return (0);
172 	if (*p == 0x80) {
173 		*inf = 1;
174 		ret = 0;
175 		p++;
176 	} else {
177 		*inf = 0;
178 		i= *p & 0x7f;
179 		if (*(p++) & 0x80) {
180 			if (max < (int)i)
181 				return (0);
182 			/* skip leading zeroes */
183 			while (i && *p == 0) {
184 				p++;
185 				i--;
186 			}
187 			if (i > sizeof(long))
188 				return 0;
189 			while (i-- > 0) {
190 				ret <<= 8L;
191 				ret |= *(p++);
192 			}
193 		} else
194 			ret = i;
195 	}
196 	if (ret > LONG_MAX)
197 		return 0;
198 	*pp = p;
199 	*rl = (long)ret;
200 	return (1);
201 }
202 
203 /* class 0 is constructed
204  * constructed == 2 for indefinite length constructed */
205 void
206 ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
207     int xclass)
208 {
209 	unsigned char *p= *pp;
210 	int i, ttag;
211 
212 	i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
213 	i |= (xclass & V_ASN1_PRIVATE);
214 	if (tag < 31)
215 		*(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
216 	else {
217 		*(p++) = i | V_ASN1_PRIMITIVE_TAG;
218 		for(i = 0, ttag = tag; ttag > 0; i++)
219 			ttag >>= 7;
220 		ttag = i;
221 		while (i-- > 0) {
222 			p[i] = tag & 0x7f;
223 			if (i != (ttag - 1))
224 				p[i] |= 0x80;
225 			tag >>= 7;
226 		}
227 		p += ttag;
228 	}
229 	if (constructed == 2)
230 		*(p++) = 0x80;
231 	else
232 		asn1_put_length(&p, length);
233 	*pp = p;
234 }
235 
236 int
237 ASN1_put_eoc(unsigned char **pp)
238 {
239 	unsigned char *p = *pp;
240 
241 	*p++ = 0;
242 	*p++ = 0;
243 	*pp = p;
244 	return 2;
245 }
246 
247 static void
248 asn1_put_length(unsigned char **pp, int length)
249 {
250 	unsigned char *p= *pp;
251 
252 	int i, l;
253 	if (length <= 127)
254 		*(p++) = (unsigned char)length;
255 	else {
256 		l = length;
257 		for (i = 0; l > 0; i++)
258 			l >>= 8;
259 		*(p++) = i | 0x80;
260 		l = i;
261 		while (i-- > 0) {
262 			p[i] = length & 0xff;
263 			length >>= 8;
264 		}
265 		p += l;
266 	}
267 	*pp = p;
268 }
269 
270 int
271 ASN1_object_size(int constructed, int length, int tag)
272 {
273 	int ret;
274 
275 	ret = length;
276 	ret++;
277 	if (tag >= 31) {
278 		while (tag > 0) {
279 			tag >>= 7;
280 			ret++;
281 		}
282 	}
283 	if (constructed == 2)
284 		return ret + 3;
285 	ret++;
286 	if (length > 127) {
287 		while (length > 0) {
288 			length >>= 8;
289 			ret++;
290 		}
291 	}
292 	return (ret);
293 }
294 
295 static int
296 _asn1_Finish(ASN1_const_CTX *c)
297 {
298 	if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos)) {
299 		if (!ASN1_const_check_infinite_end(&c->p, c->slen)) {
300 			c->error = ERR_R_MISSING_ASN1_EOS;
301 			return (0);
302 		}
303 	}
304 	if (((c->slen != 0) && !(c->inf & 1)) ||
305 	    ((c->slen < 0) && (c->inf & 1))) {
306 		c->error = ERR_R_ASN1_LENGTH_MISMATCH;
307 		return (0);
308 	}
309 	return (1);
310 }
311 
312 int
313 asn1_Finish(ASN1_CTX *c)
314 {
315 	return _asn1_Finish((ASN1_const_CTX *)c);
316 }
317 
318 int
319 asn1_const_Finish(ASN1_const_CTX *c)
320 {
321 	return _asn1_Finish(c);
322 }
323 
324 int
325 asn1_GetSequence(ASN1_const_CTX *c, long *length)
326 {
327 	const unsigned char *q;
328 
329 	q = c->p;
330 	c->inf = ASN1_get_object(&(c->p), &(c->slen), &(c->tag), &(c->xclass),
331 	    *length);
332 	if (c->inf & 0x80) {
333 		c->error = ERR_R_BAD_GET_ASN1_OBJECT_CALL;
334 		return (0);
335 	}
336 	if (c->tag != V_ASN1_SEQUENCE) {
337 		c->error = ERR_R_EXPECTING_AN_ASN1_SEQUENCE;
338 		return (0);
339 	}
340 	(*length) -= (c->p - q);
341 	if (c->max && (*length < 0)) {
342 		c->error = ERR_R_ASN1_LENGTH_MISMATCH;
343 		return (0);
344 	}
345 	if (c->inf == (1|V_ASN1_CONSTRUCTED))
346 		c->slen= *length+ *(c->pp) - c->p;
347 	c->eos = 0;
348 	return (1);
349 }
350 
351 int
352 ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
353 {
354 	if (str == NULL)
355 		return 0;
356 	dst->type = str->type;
357 	if (!ASN1_STRING_set(dst, str->data, str->length))
358 		return 0;
359 	dst->flags = str->flags;
360 	return 1;
361 }
362 
363 ASN1_STRING *
364 ASN1_STRING_dup(const ASN1_STRING *str)
365 {
366 	ASN1_STRING *ret;
367 
368 	if (!str)
369 		return NULL;
370 	ret = ASN1_STRING_new();
371 	if (!ret)
372 		return NULL;
373 	if (!ASN1_STRING_copy(ret, str)) {
374 		ASN1_STRING_free(ret);
375 		return NULL;
376 	}
377 	return ret;
378 }
379 
380 int
381 ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
382 {
383 	const char *data = _data;
384 
385 	if (len < 0) {
386 		if (data == NULL)
387 			return (0);
388 		else
389 			len = strlen(data);
390 	}
391 	if ((str->length < len) || (str->data == NULL)) {
392 		unsigned char *tmp;
393 		tmp = realloc(str->data, len + 1);
394 		if (tmp == NULL) {
395 			ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
396 			return (0);
397 		}
398 		str->data = tmp;
399 	}
400 	str->length = len;
401 	if (data != NULL) {
402 		memmove(str->data, data, len);
403 	}
404 	str->data[str->length]='\0';
405 	return (1);
406 }
407 
408 void
409 ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
410 {
411 	free(str->data);
412 	str->data = data;
413 	str->length = len;
414 }
415 
416 ASN1_STRING *
417 ASN1_STRING_new(void)
418 {
419 	return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
420 }
421 
422 ASN1_STRING *
423 ASN1_STRING_type_new(int type)
424 {
425 	ASN1_STRING *ret;
426 
427 	ret = malloc(sizeof(ASN1_STRING));
428 	if (ret == NULL) {
429 		ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
430 		return (NULL);
431 	}
432 	ret->length = 0;
433 	ret->type = type;
434 	ret->data = NULL;
435 	ret->flags = 0;
436 	return (ret);
437 }
438 
439 void
440 ASN1_STRING_free(ASN1_STRING *a)
441 {
442 	if (a == NULL)
443 		return;
444 	if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
445 		free(a->data);
446 	free(a);
447 }
448 
449 int
450 ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
451 {
452 	int i;
453 
454 	i = (a->length - b->length);
455 	if (i == 0) {
456 		i = memcmp(a->data, b->data, a->length);
457 		if (i == 0)
458 			return (a->type - b->type);
459 		else
460 			return (i);
461 	} else
462 		return (i);
463 }
464 
465 void
466 asn1_add_error(const unsigned char *address, int offset)
467 {
468 	ERR_asprintf_error_data("address=%p offset=%d", address, offset);
469 }
470 
471 int
472 ASN1_STRING_length(const ASN1_STRING *x)
473 {
474 	return M_ASN1_STRING_length(x);
475 }
476 
477 void
478 ASN1_STRING_length_set(ASN1_STRING *x, int len)
479 {
480 	M_ASN1_STRING_length_set(x, len);
481 	return;
482 }
483 
484 int
485 ASN1_STRING_type(ASN1_STRING *x)
486 {
487 	return M_ASN1_STRING_type(x);
488 }
489 
490 unsigned char *
491 ASN1_STRING_data(ASN1_STRING *x)
492 {
493 	return M_ASN1_STRING_data(x);
494 }
495