1 /* crypto/asn1/a_int.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
60 #include "internal/cryptlib.h"
61 #include "internal/numbers.h"
63 #include <openssl/asn1.h>
64 #include <openssl/bn.h>
65 #include "asn1_locl.h"
67 ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
69 return ASN1_STRING_dup(x);
72 int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
76 neg = x->type & V_ASN1_NEG;
77 if (neg != (y->type & V_ASN1_NEG)) {
84 ret = ASN1_STRING_cmp(x, y);
93 * This converts a big endian buffer and sign into its content encoding.
94 * This is used for INTEGER and ENUMERATED types.
95 * The internal representation is an ASN1_STRING whose data is a big endian
96 * representation of the value, ignoring the sign. The sign is determined by
97 * the type: if type & V_ASN1_NEG is true it is negative, otherwise positive.
99 * Positive integers are no problem: they are almost the same as the DER
100 * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
102 * Negative integers are a bit trickier...
103 * The DER representation of negative integers is in 2s complement form.
104 * The internal form is converted by complementing each octet and finally
105 * adding one to the result. This can be done less messily with a little trick.
106 * If the internal form has trailing zeroes then they will become FF by the
107 * complement and 0 by the add one (due to carry) so just copy as many trailing
108 * zeros to the destination as there are in the source. The carry will add one
109 * to the last none zero octet: so complement this octet and add one and finally
110 * complement any left over until you get to the start of the string.
112 * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
113 * with 0xff. However if the first byte is 0x80 and one of the following bytes
114 * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
115 * followed by optional zeros isn't padded.
118 static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
123 unsigned char *p, pb = 0;
124 const unsigned char *n;
126 if (b == NULL || blen == 0)
131 if (ret == 1 && i == 0)
133 if (!neg && (i > 127)) {
140 } else if (i == 128) {
142 * Special case: if any other bytes non zero we pad:
143 * otherwise we don't.
145 for (i = 1; i < blen; i++)
161 if (b == NULL || blen == 0)
166 /* Begin at the end of the encoding */
170 /* Copy zeros to destination as long as source is zero */
171 while (!*n && i > 1) {
176 /* Complement and increment next octet */
177 *(p--) = ((*(n--)) ^ 0xff) + 1;
179 /* Complement any octets left */
181 *(p--) = *(n--) ^ 0xff;
189 * convert content octets into a big endian buffer. Returns the length
190 * of buffer or 0 on error: for malformed INTEGER. If output bufer is
191 * NULL just return length.
194 static size_t c2i_ibuf(unsigned char *b, int *pneg,
195 const unsigned char *p, size_t plen)
199 /* Zero content length is illegal */
201 ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
207 /* Handle common case where length is 1 octet separately */
211 b[0] = (p[0] ^ 0xFF) + 1;
217 if (p[0] == 0 || p[0] == 0xFF)
221 /* reject illegal padding: first two octets MSB can't match */
222 if (pad && (neg == (p[1] & 0x80))) {
223 ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
226 /* If positive just copy across */
229 memcpy(b, p + pad, plen - pad);
234 /* check is any following octets are non zero */
235 for (i = 1; i < plen; i++) {
239 /* if all bytes are zero handle as special case */
243 memset(b + 1, 0, plen - 1);
250 /* Must be negative: calculate twos complement */
252 const unsigned char *from = p + plen - 1 + pad;
253 unsigned char *to = b + plen - 1;
255 while (*from == 0 && i) {
260 *to-- = (*from-- ^ 0xff) + 1;
261 OPENSSL_assert(i != 0);
264 *to-- = *from-- ^ 0xff;
269 int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
271 return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
274 /* Convert big endian buffer into uint64_t, return 0 on error */
275 static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
278 if (blen > sizeof(*pr)) {
279 ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
285 for (i = 0; i < blen; i++) {
292 static size_t asn1_put_uint64(unsigned char *b, uint64_t r)
299 /* Work out how many bytes we need */
305 /* Copy from end to beginning */
316 b[0] = (unsigned char)r;
322 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as it produces
326 #define ABS_INT64_MIN \
327 ((uint64_t)INT64_MAX + (uint64_t)(-(INT64_MIN + INT64_MAX)))
329 /* signed version of asn1_get_uint64 */
330 static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
334 if (asn1_get_uint64(&r, b, blen) == 0)
337 if (r > ABS_INT64_MIN) {
338 ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
344 ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
352 /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
353 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
356 ASN1_INTEGER *ret = NULL;
360 r = c2i_ibuf(NULL, NULL, *pp, len);
365 if ((a == NULL) || ((*a) == NULL)) {
366 ret = ASN1_INTEGER_new();
369 ret->type = V_ASN1_INTEGER;
373 if (ASN1_STRING_set(ret, NULL, r) == 0)
376 c2i_ibuf(ret->data, &neg, *pp, len);
379 ret->type |= V_ASN1_NEG;
386 ASN1err(ASN1_F_C2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
387 if ((a == NULL) || (*a != ret))
388 ASN1_INTEGER_free(ret);
392 static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
395 ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
398 if ((a->type & ~V_ASN1_NEG) != itype) {
399 ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ASN1_R_WRONG_INTEGER_TYPE);
402 return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
405 static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
407 unsigned char tbuf[sizeof(r)];
411 l = asn1_put_uint64(tbuf, -r);
412 a->type |= V_ASN1_NEG;
414 l = asn1_put_uint64(tbuf, r);
415 a->type &= ~V_ASN1_NEG;
419 return ASN1_STRING_set(a, tbuf, l);
422 static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
426 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ERR_R_PASSED_NULL_PARAMETER);
429 if ((a->type & ~V_ASN1_NEG) != itype) {
430 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_WRONG_INTEGER_TYPE);
433 if (a->type & V_ASN1_NEG) {
434 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
437 return asn1_get_uint64(pr, a->data, a->length);
440 static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
442 unsigned char tbuf[sizeof(r)];
445 l = asn1_put_uint64(tbuf, r);
448 return ASN1_STRING_set(a, tbuf, l);
452 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
453 * integers: some broken software can encode a positive INTEGER with its MSB
454 * set as negative (it doesn't add a padding zero).
457 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
460 ASN1_INTEGER *ret = NULL;
461 const unsigned char *p;
464 int inf, tag, xclass;
467 if ((a == NULL) || ((*a) == NULL)) {
468 if ((ret = ASN1_INTEGER_new()) == NULL)
470 ret->type = V_ASN1_INTEGER;
475 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
477 i = ASN1_R_BAD_OBJECT_HEADER;
481 if (tag != V_ASN1_INTEGER) {
482 i = ASN1_R_EXPECTING_AN_INTEGER;
487 * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
488 * a missing NULL parameter.
490 s = OPENSSL_malloc((int)len + 1);
492 i = ERR_R_MALLOC_FAILURE;
495 ret->type = V_ASN1_INTEGER;
497 if ((*p == 0) && (len != 1)) {
501 memcpy(s, p, (int)len);
505 OPENSSL_free(ret->data);
507 ret->length = (int)len;
513 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
514 if ((a == NULL) || (*a != ret))
515 ASN1_INTEGER_free(ret);
519 static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
526 ret = ASN1_STRING_type_new(atype);
533 ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_NESTED_ASN1_ERROR);
537 if (BN_is_negative(bn) && !BN_is_zero(bn))
538 ret->type |= V_ASN1_NEG_INTEGER;
540 len = BN_num_bytes(bn);
545 if (ASN1_STRING_set(ret, NULL, len) == 0) {
546 ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_MALLOC_FAILURE);
550 /* Correct zero case */
554 len = BN_bn2bin(bn, ret->data);
559 ASN1_INTEGER_free(ret);
563 static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
568 if ((ai->type & ~V_ASN1_NEG) != itype) {
569 ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
573 ret = BN_bin2bn(ai->data, ai->length, bn);
575 ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
578 if (ai->type & V_ASN1_NEG)
579 BN_set_negative(ret, 1);
583 int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
585 return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
588 int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
590 return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
593 int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
595 return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
598 int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
600 return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
603 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
605 return ASN1_INTEGER_set_int64(a, v);
608 long ASN1_INTEGER_get(const ASN1_INTEGER *a)
614 i = ASN1_INTEGER_get_int64(&r, a);
617 if (r > LONG_MAX || r < LONG_MIN)
622 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
624 return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
627 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
629 return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
632 int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
634 return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
637 int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
639 return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
642 int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
644 return ASN1_ENUMERATED_set_int64(a, v);
647 long ASN1_ENUMERATED_get(ASN1_ENUMERATED *a)
653 if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
655 if (a->length > (int)sizeof(long))
657 i = ASN1_ENUMERATED_get_int64(&r, a);
660 if (r > LONG_MAX || r < LONG_MIN)
665 ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
667 return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
670 BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
672 return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);