xref: /openbsd-src/lib/libcrypto/asn1/a_int.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /* $OpenBSD: a_int.c,v 1.44 2022/07/13 20:07:44 jsing 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/asn1t.h>
65 #include <openssl/bn.h>
66 #include <openssl/buffer.h>
67 #include <openssl/err.h>
68 
69 #include "bytestring.h"
70 
71 const ASN1_ITEM ASN1_INTEGER_it = {
72 	.itype = ASN1_ITYPE_PRIMITIVE,
73 	.utype = V_ASN1_INTEGER,
74 	.sname = "ASN1_INTEGER",
75 };
76 
77 ASN1_INTEGER *
78 ASN1_INTEGER_new(void)
79 {
80 	return (ASN1_INTEGER *)ASN1_item_new(&ASN1_INTEGER_it);
81 }
82 
83 static void
84 asn1_aint_clear(ASN1_INTEGER *aint)
85 {
86 	freezero(aint->data, aint->length);
87 
88 	memset(aint, 0, sizeof(*aint));
89 
90 	aint->type = V_ASN1_INTEGER;
91 }
92 
93 void
94 ASN1_INTEGER_free(ASN1_INTEGER *a)
95 {
96 	ASN1_item_free((ASN1_VALUE *)a, &ASN1_INTEGER_it);
97 }
98 
99 static int
100 ASN1_INTEGER_valid(const ASN1_INTEGER *a)
101 {
102 	return (a != NULL && a->length >= 0);
103 }
104 
105 ASN1_INTEGER *
106 ASN1_INTEGER_dup(const ASN1_INTEGER *x)
107 {
108 	if (!ASN1_INTEGER_valid(x))
109 		return NULL;
110 
111 	return ASN1_STRING_dup(x);
112 }
113 
114 int
115 ASN1_INTEGER_cmp(const ASN1_INTEGER *a, const ASN1_INTEGER *b)
116 {
117 	int ret = 1;
118 
119 	/* Compare sign, then content. */
120 	if ((a->type & V_ASN1_NEG) == (b->type & V_ASN1_NEG))
121 		ret = ASN1_STRING_cmp(a, b);
122 
123 	if ((a->type & V_ASN1_NEG) != 0)
124 		return -ret;
125 
126 	return ret;
127 }
128 
129 int
130 asn1_aint_get_uint64(CBS *cbs, uint64_t *out_val)
131 {
132 	uint64_t val = 0;
133 	uint8_t u8;
134 
135 	*out_val = 0;
136 
137 	while (CBS_len(cbs) > 0) {
138 		if (!CBS_get_u8(cbs, &u8))
139 			return 0;
140 		if (val > (UINT64_MAX >> 8)) {
141 			ASN1error(ASN1_R_TOO_LARGE);
142 			return 0;
143 		}
144 		val = val << 8 | u8;
145 	}
146 
147 	*out_val = val;
148 
149 	return 1;
150 }
151 
152 int
153 asn1_aint_set_uint64(uint64_t val, uint8_t **out_data, int *out_len)
154 {
155 	uint8_t *data = NULL;
156 	size_t data_len = 0;
157 	int started = 0;
158 	uint8_t u8;
159 	CBB cbb;
160 	int i;
161 	int ret = 0;
162 
163 	if (!CBB_init(&cbb, sizeof(long)))
164 		goto err;
165 
166 	if (out_data == NULL || out_len == NULL)
167 		goto err;
168 	if (*out_data != NULL || *out_len != 0)
169 		goto err;
170 
171 	for (i = sizeof(uint64_t) - 1; i >= 0; i--) {
172 		u8 = (val >> (i * 8)) & 0xff;
173 		if (!started && i != 0 && u8 == 0)
174 			continue;
175 		if (!CBB_add_u8(&cbb, u8))
176 			goto err;
177 		started = 1;
178 	}
179 
180 	if (!CBB_finish(&cbb, &data, &data_len))
181 		goto err;
182 	if (data_len > INT_MAX)
183 		goto err;
184 
185 	*out_data = data;
186 	*out_len = (int)data_len;
187 	data = NULL;
188 
189 	ret = 1;
190  err:
191 	CBB_cleanup(&cbb);
192 	freezero(data, data_len);
193 
194 	return ret;
195 }
196 
197 int
198 asn1_aint_get_int64(CBS *cbs, int negative, int64_t *out_val)
199 {
200 	uint64_t val;
201 
202 	if (!asn1_aint_get_uint64(cbs, &val))
203 		return 0;
204 
205 	if (negative) {
206 		if (val > (uint64_t)INT64_MIN) {
207 			ASN1error(ASN1_R_TOO_SMALL);
208 			return 0;
209 		}
210 		*out_val = (int64_t)-val;
211 	} else {
212 		if (val > (uint64_t)INT64_MAX) {
213 			ASN1error(ASN1_R_TOO_LARGE);
214 			return 0;
215 		}
216 		*out_val = (int64_t)val;
217 	}
218 
219 	return 1;
220 }
221 
222 int
223 ASN1_INTEGER_get_uint64(uint64_t *out_val, const ASN1_INTEGER *aint)
224 {
225 	uint64_t val;
226 	CBS cbs;
227 
228 	*out_val = 0;
229 
230 	if (aint == NULL || aint->length < 0)
231 		return 0;
232 
233 	if (aint->type == V_ASN1_NEG_INTEGER) {
234 		ASN1error(ASN1_R_ILLEGAL_NEGATIVE_VALUE);
235 		return 0;
236 	}
237 	if (aint->type != V_ASN1_INTEGER) {
238 		ASN1error(ASN1_R_WRONG_INTEGER_TYPE);
239 		return 0;
240 	}
241 
242 	CBS_init(&cbs, aint->data, aint->length);
243 
244 	if (!asn1_aint_get_uint64(&cbs, &val))
245 		return 0;
246 
247 	*out_val = val;
248 
249 	return 1;
250 }
251 
252 int
253 ASN1_INTEGER_set_uint64(ASN1_INTEGER *aint, uint64_t val)
254 {
255 	asn1_aint_clear(aint);
256 
257 	return asn1_aint_set_uint64(val, &aint->data, &aint->length);
258 }
259 
260 int
261 ASN1_INTEGER_get_int64(int64_t *out_val, const ASN1_INTEGER *aint)
262 {
263 	CBS cbs;
264 
265 	*out_val = 0;
266 
267 	if (aint == NULL || aint->length < 0)
268 		return 0;
269 
270 	if (aint->type != V_ASN1_INTEGER &&
271 	    aint->type != V_ASN1_NEG_INTEGER) {
272 		ASN1error(ASN1_R_WRONG_INTEGER_TYPE);
273 		return 0;
274 	}
275 
276 	CBS_init(&cbs, aint->data, aint->length);
277 
278 	return asn1_aint_get_int64(&cbs, (aint->type == V_ASN1_NEG_INTEGER),
279 	    out_val);
280 }
281 
282 int
283 ASN1_INTEGER_set_int64(ASN1_INTEGER *aint, int64_t val)
284 {
285 	uint64_t uval;
286 
287 	asn1_aint_clear(aint);
288 
289 	uval = (uint64_t)val;
290 
291 	if (val < 0) {
292 		aint->type = V_ASN1_NEG_INTEGER;
293 		uval = -uval;
294 	}
295 
296 	return asn1_aint_set_uint64(uval, &aint->data, &aint->length);
297 }
298 
299 long
300 ASN1_INTEGER_get(const ASN1_INTEGER *aint)
301 {
302 	int64_t val;
303 
304 	if (aint == NULL)
305 		return 0;
306 	if (!ASN1_INTEGER_get_int64(&val, aint))
307 		return -1;
308 	if (val < LONG_MIN || val > LONG_MAX) {
309 		/* hmm... a bit ugly, return all ones */
310 		return -1;
311 	}
312 
313 	return (long)val;
314 }
315 
316 int
317 ASN1_INTEGER_set(ASN1_INTEGER *aint, long val)
318 {
319 	return ASN1_INTEGER_set_int64(aint, val);
320 }
321 
322 ASN1_INTEGER *
323 BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
324 {
325 	ASN1_INTEGER *ret;
326 	int len, j;
327 
328 	if (ai == NULL)
329 		ret = ASN1_INTEGER_new();
330 	else
331 		ret = ai;
332 	if (ret == NULL) {
333 		ASN1error(ERR_R_NESTED_ASN1_ERROR);
334 		goto err;
335 	}
336 
337 	if (!ASN1_INTEGER_valid(ret))
338 		goto err;
339 
340 	if (BN_is_negative(bn))
341 		ret->type = V_ASN1_NEG_INTEGER;
342 	else
343 		ret->type = V_ASN1_INTEGER;
344 	j = BN_num_bits(bn);
345 	len = ((j == 0) ? 0 : ((j / 8) + 1));
346 	if (ret->length < len + 4) {
347 		unsigned char *new_data = realloc(ret->data, len + 4);
348 		if (!new_data) {
349 			ASN1error(ERR_R_MALLOC_FAILURE);
350 			goto err;
351 		}
352 		ret->data = new_data;
353 	}
354 	ret->length = BN_bn2bin(bn, ret->data);
355 
356 	/* Correct zero case */
357 	if (!ret->length) {
358 		ret->data[0] = 0;
359 		ret->length = 1;
360 	}
361 	return (ret);
362 
363  err:
364 	if (ret != ai)
365 		ASN1_INTEGER_free(ret);
366 	return (NULL);
367 }
368 
369 BIGNUM *
370 ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
371 {
372 	BIGNUM *ret;
373 
374 	if (!ASN1_INTEGER_valid(ai))
375 		return (NULL);
376 
377 	if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
378 		ASN1error(ASN1_R_BN_LIB);
379 	else if (ai->type == V_ASN1_NEG_INTEGER)
380 		BN_set_negative(ret, 1);
381 	return (ret);
382 }
383 
384 int
385 i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
386 {
387 	int i, n = 0;
388 	static const char h[] = "0123456789ABCDEF";
389 	char buf[2];
390 
391 	if (a == NULL)
392 		return (0);
393 
394 	if (a->type & V_ASN1_NEG) {
395 		if (BIO_write(bp, "-", 1) != 1)
396 			goto err;
397 		n = 1;
398 	}
399 
400 	if (a->length == 0) {
401 		if (BIO_write(bp, "00", 2) != 2)
402 			goto err;
403 		n += 2;
404 	} else {
405 		for (i = 0; i < a->length; i++) {
406 			if ((i != 0) && (i % 35 == 0)) {
407 				if (BIO_write(bp, "\\\n", 2) != 2)
408 					goto err;
409 				n += 2;
410 			}
411 			buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
412 			buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
413 			if (BIO_write(bp, buf, 2) != 2)
414 				goto err;
415 			n += 2;
416 		}
417 	}
418 	return (n);
419 
420  err:
421 	return (-1);
422 }
423 
424 int
425 a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
426 {
427 	int ret = 0;
428 	int i, j,k, m,n, again, bufsize;
429 	unsigned char *s = NULL, *sp;
430 	unsigned char *bufp;
431 	int num = 0, slen = 0, first = 1;
432 
433 	bs->type = V_ASN1_INTEGER;
434 
435 	bufsize = BIO_gets(bp, buf, size);
436 	for (;;) {
437 		if (bufsize < 1)
438 			goto err_sl;
439 		i = bufsize;
440 		if (buf[i - 1] == '\n')
441 			buf[--i] = '\0';
442 		if (i == 0)
443 			goto err_sl;
444 		if (buf[i - 1] == '\r')
445 			buf[--i] = '\0';
446 		if (i == 0)
447 			goto err_sl;
448 		if (buf[i - 1] == '\\') {
449 			i--;
450 			again = 1;
451 		} else
452 			again = 0;
453 		buf[i] = '\0';
454 		if (i < 2)
455 			goto err_sl;
456 
457 		bufp = (unsigned char *)buf;
458 		if (first) {
459 			first = 0;
460 			if ((bufp[0] == '0') && (buf[1] == '0')) {
461 				bufp += 2;
462 				i -= 2;
463 			}
464 		}
465 		k = 0;
466 		if (i % 2 != 0) {
467 			ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS);
468 			goto err;
469 		}
470 		i /= 2;
471 		if (num + i > slen) {
472 			if ((sp = recallocarray(s, slen, num + i, 1)) == NULL) {
473 				ASN1error(ERR_R_MALLOC_FAILURE);
474 				goto err;
475 			}
476 			s = sp;
477 			slen = num + i;
478 		}
479 		for (j = 0; j < i; j++, k += 2) {
480 			for (n = 0; n < 2; n++) {
481 				m = bufp[k + n];
482 				if ((m >= '0') && (m <= '9'))
483 					m -= '0';
484 				else if ((m >= 'a') && (m <= 'f'))
485 					m = m - 'a' + 10;
486 				else if ((m >= 'A') && (m <= 'F'))
487 					m = m - 'A' + 10;
488 				else {
489 					ASN1error(ASN1_R_NON_HEX_CHARACTERS);
490 					goto err;
491 				}
492 				s[num + j] <<= 4;
493 				s[num + j] |= m;
494 			}
495 		}
496 		num += i;
497 		if (again)
498 			bufsize = BIO_gets(bp, buf, size);
499 		else
500 			break;
501 	}
502 	bs->length = num;
503 	bs->data = s;
504 	return (1);
505 
506  err_sl:
507 	ASN1error(ASN1_R_SHORT_LINE);
508  err:
509 	free(s);
510 	return (ret);
511 }
512 
513 /*
514  * This converts an ASN1 INTEGER into its content encoding.
515  * The internal representation is an ASN1_STRING whose data is a big endian
516  * representation of the value, ignoring the sign. The sign is determined by
517  * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
518  *
519  * Positive integers are no problem: they are almost the same as the DER
520  * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
521  *
522  * Negative integers are a bit trickier...
523  * The DER representation of negative integers is in 2s complement form.
524  * The internal form is converted by complementing each octet and finally
525  * adding one to the result. This can be done less messily with a little trick.
526  * If the internal form has trailing zeroes then they will become FF by the
527  * complement and 0 by the add one (due to carry) so just copy as many trailing
528  * zeros to the destination as there are in the source. The carry will add one
529  * to the last none zero octet: so complement this octet and add one and finally
530  * complement any left over until you get to the start of the string.
531  *
532  * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
533  * with 0xff. However if the first byte is 0x80 and one of the following bytes
534  * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
535  * followed by optional zeros isn't padded.
536  */
537 
538 int
539 i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
540 {
541 	int pad = 0, ret, i, neg;
542 	unsigned char *p, *n, pb = 0;
543 
544 	if (!ASN1_INTEGER_valid(a))
545 		return 0;
546 
547 	neg = a->type & V_ASN1_NEG;
548 	if (a->length == 0)
549 		ret = 1;
550 	else {
551 		ret = a->length;
552 		i = a->data[0];
553 		if (!neg && (i > 127)) {
554 			pad = 1;
555 			pb = 0;
556 		} else if (neg) {
557 			if (i > 128) {
558 				pad = 1;
559 				pb = 0xFF;
560 			} else if (i == 128) {
561 				/*
562 				 * Special case: if any other bytes non zero we pad:
563 				 * otherwise we don't.
564 				 */
565 				for (i = 1; i < a->length; i++) if (a->data[i]) {
566 					pad = 1;
567 					pb = 0xFF;
568 					break;
569 				}
570 			}
571 		}
572 		ret += pad;
573 	}
574 	if (pp == NULL)
575 		return (ret);
576 	p= *pp;
577 
578 	if (pad)
579 		*(p++) = pb;
580 	if (a->length == 0)
581 		*(p++) = 0;
582 	else if (!neg)
583 		memcpy(p, a->data, a->length);
584 	else {
585 		/* Begin at the end of the encoding */
586 		n = a->data + a->length - 1;
587 		p += a->length - 1;
588 		i = a->length;
589 		/* Copy zeros to destination as long as source is zero */
590 		while (!*n) {
591 			*(p--) = 0;
592 			n--;
593 			i--;
594 		}
595 		/* Complement and increment next octet */
596 		*(p--) = ((*(n--)) ^ 0xff) + 1;
597 		i--;
598 		/* Complement any octets left */
599 		for (; i > 0; i--)
600 			*(p--) = *(n--) ^ 0xff;
601 	}
602 
603 	*pp += ret;
604 	return (ret);
605 }
606 
607 static void
608 asn1_aint_twos_complement(uint8_t *data, size_t data_len)
609 {
610 	uint8_t carry = 1;
611 	ssize_t i;
612 
613 	for (i = data_len - 1; i >= 0; i--) {
614 		data[i] = (data[i] ^ 0xff) + carry;
615 		if (data[i] != 0)
616 			carry = 0;
617 	}
618 }
619 
620 static int
621 asn1_aint_keep_twos_padding(const uint8_t *data, size_t data_len)
622 {
623 	size_t i;
624 
625 	/*
626 	 * If a two's complement value has a padding byte (0xff) and the rest
627 	 * of the value is all zeros, the padding byte cannot be removed as when
628 	 * converted from two's complement this becomes 0x01 (in the place of
629 	 * the padding byte) followed by the same number of zero bytes.
630 	 */
631 	if (data_len <= 1 || data[0] != 0xff)
632 		return 0;
633 	for (i = 1; i < data_len; i++) {
634 		if (data[i] != 0)
635 			return 0;
636 	}
637 	return 1;
638 }
639 
640 int
641 c2i_ASN1_INTEGER_cbs(ASN1_INTEGER **out_aint, CBS *cbs)
642 {
643 	ASN1_INTEGER *aint = NULL;
644 	uint8_t *data = NULL;
645 	size_t data_len = 0;
646 	uint8_t padding, val;
647 	uint8_t negative = 0;
648 	int ret = 0;
649 
650 	if (out_aint == NULL)
651 		goto err;
652 
653 	if (*out_aint != NULL) {
654 		ASN1_INTEGER_free(*out_aint);
655 		*out_aint = NULL;
656 	}
657 
658 	if (CBS_len(cbs) == 0) {
659 		/* XXX INVALID ENCODING? */
660 		ASN1error(ERR_R_ASN1_LENGTH_MISMATCH);
661 		goto err;
662 	}
663 	if (!CBS_peek_u8(cbs, &val))
664 		goto err;
665 
666 	/* Top most bit indicates sign, padding is all zeros or all ones. */
667 	negative = (val >> 7);
668 	padding = ~(negative - 1) & 0xff;
669 
670 	/*
671 	 * Ensure that the first 9 bits are not all zero or all one, as per
672 	 * X.690 section 8.3.2. Remove the padding octet if possible.
673 	 */
674 	if (CBS_len(cbs) > 1 && val == padding) {
675 		if (!asn1_aint_keep_twos_padding(CBS_data(cbs), CBS_len(cbs))) {
676 			if (!CBS_get_u8(cbs, &padding))
677 				goto err;
678 			if (!CBS_peek_u8(cbs, &val))
679 				goto err;
680 			if ((val >> 7) == (padding >> 7)) {
681 				/* XXX INVALID ENCODING? */
682 				ASN1error(ERR_R_ASN1_LENGTH_MISMATCH);
683 				goto err;
684 			}
685 		}
686 	}
687 
688 	if (!CBS_stow(cbs, &data, &data_len))
689 		goto err;
690 	if (data_len > INT_MAX)
691 		goto err;
692 
693 	if ((aint = ASN1_INTEGER_new()) == NULL)
694 		goto err;
695 
696 	/*
697 	 * Negative integers are handled as a separate type - convert from
698 	 * two's complement for internal representation.
699 	 */
700 	if (negative) {
701 		aint->type = V_ASN1_NEG_INTEGER;
702 		asn1_aint_twos_complement(data, data_len);
703 	}
704 
705 	aint->data = data;
706 	aint->length = (int)data_len;
707 	data = NULL;
708 
709 	*out_aint = aint;
710 	aint = NULL;
711 
712 	ret = 1;
713 
714  err:
715 	ASN1_INTEGER_free(aint);
716 	freezero(data, data_len);
717 
718 	return ret;
719 }
720 
721 ASN1_INTEGER *
722 c2i_ASN1_INTEGER(ASN1_INTEGER **out_aint, const unsigned char **pp, long len)
723 {
724 	ASN1_INTEGER *aint = NULL;
725 	CBS content;
726 
727 	if (out_aint != NULL) {
728 		ASN1_INTEGER_free(*out_aint);
729 		*out_aint = NULL;
730 	}
731 
732 	if (len < 0) {
733 		ASN1error(ASN1_R_LENGTH_ERROR);
734 		return NULL;
735 	}
736 
737 	CBS_init(&content, *pp, len);
738 
739 	if (!c2i_ASN1_INTEGER_cbs(&aint, &content))
740 		return NULL;
741 
742 	*pp = CBS_data(&content);
743 
744 	if (out_aint != NULL)
745 		*out_aint = aint;
746 
747 	return aint;
748 }
749 
750 int
751 i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **out)
752 {
753 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_INTEGER_it);
754 }
755 
756 ASN1_INTEGER *
757 d2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **in, long len)
758 {
759 	return (ASN1_INTEGER *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
760 	    &ASN1_INTEGER_it);
761 }
762 
763 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
764  * ASN1 integers: some broken software can encode a positive INTEGER
765  * with its MSB set as negative (it doesn't add a padding zero).
766  */
767 
768 ASN1_INTEGER *
769 d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, long length)
770 {
771 	ASN1_INTEGER *ret = NULL;
772 	const unsigned char *p;
773 	unsigned char *s;
774 	long len;
775 	int inf, tag, xclass;
776 	int i;
777 
778 	if ((a == NULL) || ((*a) == NULL)) {
779 		if ((ret = ASN1_INTEGER_new()) == NULL)
780 			return (NULL);
781 	} else
782 		ret = (*a);
783 
784 	if (!ASN1_INTEGER_valid(ret)) {
785 		i = ERR_R_ASN1_LENGTH_MISMATCH;
786 		goto err;
787 	}
788 
789 	p = *pp;
790 	inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
791 	if (inf & 0x80) {
792 		i = ASN1_R_BAD_OBJECT_HEADER;
793 		goto err;
794 	}
795 
796 	if (tag != V_ASN1_INTEGER) {
797 		i = ASN1_R_EXPECTING_AN_INTEGER;
798 		goto err;
799 	}
800 
801 	/* We must malloc stuff, even for 0 bytes otherwise it
802 	 * signifies a missing NULL parameter. */
803 	if (len < 0 || len > INT_MAX) {
804 		i = ERR_R_ASN1_LENGTH_MISMATCH;
805 		goto err;
806 	}
807 	s = malloc(len + 1);
808 	if (s == NULL) {
809 		i = ERR_R_MALLOC_FAILURE;
810 		goto err;
811 	}
812 	ret->type = V_ASN1_INTEGER;
813 	if (len) {
814 		if ((*p == 0) && (len != 1)) {
815 			p++;
816 			len--;
817 		}
818 		memcpy(s, p, len);
819 		p += len;
820 	}
821 
822 	free(ret->data);
823 	ret->data = s;
824 	ret->length = (int)len;
825 	if (a != NULL)
826 		(*a) = ret;
827 	*pp = p;
828 	return (ret);
829 
830  err:
831 	ASN1error(i);
832 	if (a == NULL || *a != ret)
833 		ASN1_INTEGER_free(ret);
834 	return (NULL);
835 }
836