xref: /dragonfly/crypto/libressl/crypto/asn1/a_int.c (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
1 /* $OpenBSD: a_int.c,v 1.46 2022/08/28 17:49:25 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 *
ASN1_INTEGER_new(void)78 ASN1_INTEGER_new(void)
79 {
80           return (ASN1_INTEGER *)ASN1_item_new(&ASN1_INTEGER_it);
81 }
82 
83 static void
asn1_aint_clear(ASN1_INTEGER * aint)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
ASN1_INTEGER_free(ASN1_INTEGER * a)94 ASN1_INTEGER_free(ASN1_INTEGER *a)
95 {
96           ASN1_item_free((ASN1_VALUE *)a, &ASN1_INTEGER_it);
97 }
98 
99 static int
ASN1_INTEGER_valid(const ASN1_INTEGER * a)100 ASN1_INTEGER_valid(const ASN1_INTEGER *a)
101 {
102           return (a != NULL && a->length >= 0);
103 }
104 
105 ASN1_INTEGER *
ASN1_INTEGER_dup(const ASN1_INTEGER * x)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
ASN1_INTEGER_cmp(const ASN1_INTEGER * a,const ASN1_INTEGER * b)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
asn1_aint_get_uint64(CBS * cbs,uint64_t * out_val)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
asn1_aint_set_uint64(uint64_t val,uint8_t ** out_data,int * out_len)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
asn1_aint_get_int64(CBS * cbs,int negative,int64_t * out_val)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
ASN1_INTEGER_get_uint64(uint64_t * out_val,const ASN1_INTEGER * aint)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
ASN1_INTEGER_set_uint64(ASN1_INTEGER * aint,uint64_t val)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
ASN1_INTEGER_get_int64(int64_t * out_val,const ASN1_INTEGER * aint)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
ASN1_INTEGER_set_int64(ASN1_INTEGER * aint,int64_t val)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
ASN1_INTEGER_get(const ASN1_INTEGER * aint)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
ASN1_INTEGER_set(ASN1_INTEGER * aint,long val)317 ASN1_INTEGER_set(ASN1_INTEGER *aint, long val)
318 {
319           return ASN1_INTEGER_set_int64(aint, val);
320 }
321 
322 ASN1_INTEGER *
BN_to_ASN1_INTEGER(const BIGNUM * bn,ASN1_INTEGER * ai)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 *
ASN1_INTEGER_to_BN(const ASN1_INTEGER * ai,BIGNUM * bn)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
i2a_ASN1_INTEGER(BIO * bp,const ASN1_INTEGER * a)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
a2i_ASN1_INTEGER(BIO * bp,ASN1_INTEGER * bs,char * buf,int size)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 static void
asn1_aint_twos_complement(uint8_t * data,size_t data_len)514 asn1_aint_twos_complement(uint8_t *data, size_t data_len)
515 {
516           uint8_t carry = 1;
517           ssize_t i;
518 
519           for (i = data_len - 1; i >= 0; i--) {
520                     data[i] = (data[i] ^ 0xff) + carry;
521                     if (data[i] != 0)
522                               carry = 0;
523           }
524 }
525 
526 static int
asn1_aint_keep_twos_padding(const uint8_t * data,size_t data_len)527 asn1_aint_keep_twos_padding(const uint8_t *data, size_t data_len)
528 {
529           size_t i;
530 
531           /*
532            * If a two's complement value has a padding byte (0xff) and the rest
533            * of the value is all zeros, the padding byte cannot be removed as when
534            * converted from two's complement this becomes 0x01 (in the place of
535            * the padding byte) followed by the same number of zero bytes.
536            */
537           if (data_len <= 1 || data[0] != 0xff)
538                     return 0;
539           for (i = 1; i < data_len; i++) {
540                     if (data[i] != 0)
541                               return 0;
542           }
543           return 1;
544 }
545 
546 static int
i2c_ASN1_INTEGER_cbb(ASN1_INTEGER * aint,CBB * cbb)547 i2c_ASN1_INTEGER_cbb(ASN1_INTEGER *aint, CBB *cbb)
548 {
549           uint8_t *data = NULL;
550           size_t data_len = 0;
551           uint8_t padding, val;
552           uint8_t msb;
553           CBS cbs;
554           int ret = 0;
555 
556           if (aint->length < 0)
557                     goto err;
558           if (aint->data == NULL && aint->length != 0)
559                     goto err;
560 
561           if ((aint->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED &&
562               (aint->type & ~V_ASN1_NEG) != V_ASN1_INTEGER)
563                     goto err;
564 
565           CBS_init(&cbs, aint->data, aint->length);
566 
567           /* Find the first non-zero byte. */
568           while (CBS_len(&cbs) > 0) {
569                     if (!CBS_peek_u8(&cbs, &val))
570                               goto err;
571                     if (val != 0)
572                               break;
573                     if (!CBS_skip(&cbs, 1))
574                               goto err;
575           }
576 
577           /* A zero value is encoded as a single octet. */
578           if (CBS_len(&cbs) == 0) {
579                     if (!CBB_add_u8(cbb, 0))
580                               goto err;
581                     goto done;
582           }
583 
584           if (!CBS_stow(&cbs, &data, &data_len))
585                     goto err;
586 
587           if ((aint->type & V_ASN1_NEG) != 0)
588                     asn1_aint_twos_complement(data, data_len);
589 
590           /* Topmost bit indicates sign, padding is all zeros or all ones. */
591           msb = (data[0] >> 7);
592           padding = (msb - 1) & 0xff;
593 
594           /* See if we need a padding octet to avoid incorrect sign. */
595           if (((aint->type & V_ASN1_NEG) == 0 && msb == 1) ||
596               ((aint->type & V_ASN1_NEG) != 0 && msb == 0)) {
597                     if (!CBB_add_u8(cbb, padding))
598                               goto err;
599           }
600           if (!CBB_add_bytes(cbb, data, data_len))
601                     goto err;
602 
603  done:
604           ret = 1;
605 
606  err:
607           freezero(data, data_len);
608 
609           return ret;
610 }
611 
612 int
i2c_ASN1_INTEGER(ASN1_INTEGER * aint,unsigned char ** pp)613 i2c_ASN1_INTEGER(ASN1_INTEGER *aint, unsigned char **pp)
614 {
615           uint8_t *data = NULL;
616           size_t data_len = 0;
617           CBB cbb;
618           int ret = -3;
619 
620           if (!CBB_init(&cbb, 0))
621                     goto err;
622           if (!i2c_ASN1_INTEGER_cbb(aint, &cbb))
623                     goto err;
624           if (!CBB_finish(&cbb, &data, &data_len))
625                     goto err;
626           if (data_len > INT_MAX)
627                     goto err;
628 
629           if (pp != NULL) {
630                     if ((uintptr_t)*pp > UINTPTR_MAX - data_len)
631                               goto err;
632                     memcpy(*pp, data, data_len);
633                     *pp += data_len;
634           }
635 
636           ret = data_len;
637 
638  err:
639           freezero(data, data_len);
640           CBB_cleanup(&cbb);
641 
642           return ret;
643 }
644 
645 int
c2i_ASN1_INTEGER_cbs(ASN1_INTEGER ** out_aint,CBS * cbs)646 c2i_ASN1_INTEGER_cbs(ASN1_INTEGER **out_aint, CBS *cbs)
647 {
648           ASN1_INTEGER *aint = NULL;
649           uint8_t *data = NULL;
650           size_t data_len = 0;
651           uint8_t padding, val;
652           uint8_t negative;
653           int ret = 0;
654 
655           if (out_aint == NULL)
656                     goto err;
657 
658           if (*out_aint != NULL) {
659                     ASN1_INTEGER_free(*out_aint);
660                     *out_aint = NULL;
661           }
662 
663           if (CBS_len(cbs) == 0) {
664                     /* XXX INVALID ENCODING? */
665                     ASN1error(ERR_R_ASN1_LENGTH_MISMATCH);
666                     goto err;
667           }
668           if (!CBS_peek_u8(cbs, &val))
669                     goto err;
670 
671           /* Topmost bit indicates sign, padding is all zeros or all ones. */
672           negative = (val >> 7);
673           padding = ~(negative - 1) & 0xff;
674 
675           /*
676            * Ensure that the first 9 bits are not all zero or all one, as per
677            * X.690 section 8.3.2. Remove the padding octet if possible.
678            */
679           if (CBS_len(cbs) > 1 && val == padding) {
680                     if (!asn1_aint_keep_twos_padding(CBS_data(cbs), CBS_len(cbs))) {
681                               if (!CBS_get_u8(cbs, &padding))
682                                         goto err;
683                               if (!CBS_peek_u8(cbs, &val))
684                                         goto err;
685                               if ((val >> 7) == (padding >> 7)) {
686                                         /* XXX INVALID ENCODING? */
687                                         ASN1error(ERR_R_ASN1_LENGTH_MISMATCH);
688                                         goto err;
689                               }
690                     }
691           }
692 
693           if (!CBS_stow(cbs, &data, &data_len))
694                     goto err;
695           if (data_len > INT_MAX)
696                     goto err;
697 
698           if ((aint = ASN1_INTEGER_new()) == NULL)
699                     goto err;
700 
701           /*
702            * Negative integers are handled as a separate type - convert from
703            * two's complement for internal representation.
704            */
705           if (negative) {
706                     aint->type = V_ASN1_NEG_INTEGER;
707                     asn1_aint_twos_complement(data, data_len);
708           }
709 
710           aint->data = data;
711           aint->length = (int)data_len;
712           data = NULL;
713 
714           *out_aint = aint;
715           aint = NULL;
716 
717           ret = 1;
718 
719  err:
720           ASN1_INTEGER_free(aint);
721           freezero(data, data_len);
722 
723           return ret;
724 }
725 
726 ASN1_INTEGER *
c2i_ASN1_INTEGER(ASN1_INTEGER ** out_aint,const unsigned char ** pp,long len)727 c2i_ASN1_INTEGER(ASN1_INTEGER **out_aint, const unsigned char **pp, long len)
728 {
729           ASN1_INTEGER *aint = NULL;
730           CBS content;
731 
732           if (out_aint != NULL) {
733                     ASN1_INTEGER_free(*out_aint);
734                     *out_aint = NULL;
735           }
736 
737           if (len < 0) {
738                     ASN1error(ASN1_R_LENGTH_ERROR);
739                     return NULL;
740           }
741 
742           CBS_init(&content, *pp, len);
743 
744           if (!c2i_ASN1_INTEGER_cbs(&aint, &content))
745                     return NULL;
746 
747           *pp = CBS_data(&content);
748 
749           if (out_aint != NULL)
750                     *out_aint = aint;
751 
752           return aint;
753 }
754 
755 int
i2d_ASN1_INTEGER(ASN1_INTEGER * a,unsigned char ** out)756 i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **out)
757 {
758           return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_INTEGER_it);
759 }
760 
761 ASN1_INTEGER *
d2i_ASN1_INTEGER(ASN1_INTEGER ** a,const unsigned char ** in,long len)762 d2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **in, long len)
763 {
764           return (ASN1_INTEGER *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
765               &ASN1_INTEGER_it);
766 }
767 
768 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
769  * ASN1 integers: some broken software can encode a positive INTEGER
770  * with its MSB set as negative (it doesn't add a padding zero).
771  */
772 
773 ASN1_INTEGER *
d2i_ASN1_UINTEGER(ASN1_INTEGER ** a,const unsigned char ** pp,long length)774 d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, long length)
775 {
776           ASN1_INTEGER *ret = NULL;
777           const unsigned char *p;
778           unsigned char *s;
779           long len;
780           int inf, tag, xclass;
781           int i;
782 
783           if ((a == NULL) || ((*a) == NULL)) {
784                     if ((ret = ASN1_INTEGER_new()) == NULL)
785                               return (NULL);
786           } else
787                     ret = (*a);
788 
789           if (!ASN1_INTEGER_valid(ret)) {
790                     i = ERR_R_ASN1_LENGTH_MISMATCH;
791                     goto err;
792           }
793 
794           p = *pp;
795           inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
796           if (inf & 0x80) {
797                     i = ASN1_R_BAD_OBJECT_HEADER;
798                     goto err;
799           }
800 
801           if (tag != V_ASN1_INTEGER) {
802                     i = ASN1_R_EXPECTING_AN_INTEGER;
803                     goto err;
804           }
805 
806           /* We must malloc stuff, even for 0 bytes otherwise it
807            * signifies a missing NULL parameter. */
808           if (len < 0 || len > INT_MAX) {
809                     i = ERR_R_ASN1_LENGTH_MISMATCH;
810                     goto err;
811           }
812           s = malloc(len + 1);
813           if (s == NULL) {
814                     i = ERR_R_MALLOC_FAILURE;
815                     goto err;
816           }
817           ret->type = V_ASN1_INTEGER;
818           if (len) {
819                     if ((*p == 0) && (len != 1)) {
820                               p++;
821                               len--;
822                     }
823                     memcpy(s, p, len);
824                     p += len;
825           }
826 
827           free(ret->data);
828           ret->data = s;
829           ret->length = (int)len;
830           if (a != NULL)
831                     (*a) = ret;
832           *pp = p;
833           return (ret);
834 
835  err:
836           ASN1error(i);
837           if (a == NULL || *a != ret)
838                     ASN1_INTEGER_free(ret);
839           return (NULL);
840 }
841