2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
14 #include <openssl/asn1.h>
15 #include <openssl/bn.h>
16 #include "asn1_locl.h"
18 ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
20 return ASN1_STRING_dup(x);
23 int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
27 neg = x->type & V_ASN1_NEG;
28 if (neg != (y->type & V_ASN1_NEG)) {
35 ret = ASN1_STRING_cmp(x, y);
44 * This converts a big endian buffer and sign into its content encoding.
45 * This is used for INTEGER and ENUMERATED types.
46 * The internal representation is an ASN1_STRING whose data is a big endian
47 * representation of the value, ignoring the sign. The sign is determined by
48 * the type: if type & V_ASN1_NEG is true it is negative, otherwise positive.
50 * Positive integers are no problem: they are almost the same as the DER
51 * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
53 * Negative integers are a bit trickier...
54 * The DER representation of negative integers is in 2s complement form.
55 * The internal form is converted by complementing each octet and finally
56 * adding one to the result. This can be done less messily with a little trick.
57 * If the internal form has trailing zeroes then they will become FF by the
58 * complement and 0 by the add one (due to carry) so just copy as many trailing
59 * zeros to the destination as there are in the source. The carry will add one
60 * to the last none zero octet: so complement this octet and add one and finally
61 * complement any left over until you get to the start of the string.
63 * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
64 * with 0xff. However if the first byte is 0x80 and one of the following bytes
65 * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
66 * followed by optional zeros isn't padded.
69 static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
74 unsigned char *p, pb = 0;
75 const unsigned char *n;
77 if (b == NULL || blen == 0)
82 if (ret == 1 && i == 0)
84 if (!neg && (i > 127)) {
91 } else if (i == 128) {
93 * Special case: if any other bytes non zero we pad:
96 for (i = 1; i < blen; i++)
112 if (b == NULL || blen == 0)
117 /* Begin at the end of the encoding */
121 /* Copy zeros to destination as long as source is zero */
122 while (!n[-1] && i > 1) {
127 /* Complement and increment next octet */
128 *(--p) = ((*(--n)) ^ 0xff) + 1;
130 /* Complement any octets left */
132 *(--p) = *(--n) ^ 0xff;
140 * convert content octets into a big endian buffer. Returns the length
141 * of buffer or 0 on error: for malformed INTEGER. If output buffer is
142 * NULL just return length.
145 static size_t c2i_ibuf(unsigned char *b, int *pneg,
146 const unsigned char *p, size_t plen)
150 /* Zero content length is illegal */
152 ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
158 /* Handle common case where length is 1 octet separately */
162 b[0] = (p[0] ^ 0xFF) + 1;
168 if (p[0] == 0 || p[0] == 0xFF)
172 /* reject illegal padding: first two octets MSB can't match */
173 if (pad && (neg == (p[1] & 0x80))) {
174 ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
177 /* If positive just copy across */
180 memcpy(b, p + pad, plen - pad);
185 /* check is any following octets are non zero */
186 for (i = 1; i < plen; i++) {
190 /* if all bytes are zero handle as special case */
194 memset(b + 1, 0, plen - 1);
201 /* Must be negative: calculate twos complement */
203 const unsigned char *from = p + plen - 1 + pad;
204 unsigned char *to = b + plen;
206 while (*from == 0 && i) {
211 *--to = (*from-- ^ 0xff) + 1;
212 OPENSSL_assert(i != 0);
215 *--to = *from-- ^ 0xff;
220 int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
222 return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
225 /* Convert big endian buffer into uint64_t, return 0 on error */
226 static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
229 if (blen > sizeof(*pr)) {
230 ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
236 for (i = 0; i < blen; i++) {
243 static size_t asn1_put_uint64(unsigned char *b, uint64_t r)
250 /* Work out how many bytes we need */
256 /* Copy from end to beginning */
267 b[0] = (unsigned char)r;
273 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as it produces
277 #define ABS_INT64_MIN \
278 ((uint64_t)INT64_MAX + (uint64_t)(-(INT64_MIN + INT64_MAX)))
280 /* signed version of asn1_get_uint64 */
281 static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
285 if (asn1_get_uint64(&r, b, blen) == 0)
288 if (r > ABS_INT64_MIN) {
289 ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
295 ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
303 /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
304 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
307 ASN1_INTEGER *ret = NULL;
311 r = c2i_ibuf(NULL, NULL, *pp, len);
316 if ((a == NULL) || ((*a) == NULL)) {
317 ret = ASN1_INTEGER_new();
320 ret->type = V_ASN1_INTEGER;
324 if (ASN1_STRING_set(ret, NULL, r) == 0)
327 c2i_ibuf(ret->data, &neg, *pp, len);
330 ret->type |= V_ASN1_NEG;
337 ASN1err(ASN1_F_C2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
338 if ((a == NULL) || (*a != ret))
339 ASN1_INTEGER_free(ret);
343 static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
346 ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
349 if ((a->type & ~V_ASN1_NEG) != itype) {
350 ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ASN1_R_WRONG_INTEGER_TYPE);
353 return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
356 static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
358 unsigned char tbuf[sizeof(r)];
362 l = asn1_put_uint64(tbuf, -r);
363 a->type |= V_ASN1_NEG;
365 l = asn1_put_uint64(tbuf, r);
366 a->type &= ~V_ASN1_NEG;
370 return ASN1_STRING_set(a, tbuf, l);
373 static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
377 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ERR_R_PASSED_NULL_PARAMETER);
380 if ((a->type & ~V_ASN1_NEG) != itype) {
381 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_WRONG_INTEGER_TYPE);
384 if (a->type & V_ASN1_NEG) {
385 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
388 return asn1_get_uint64(pr, a->data, a->length);
391 static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
393 unsigned char tbuf[sizeof(r)];
396 l = asn1_put_uint64(tbuf, r);
399 return ASN1_STRING_set(a, tbuf, l);
403 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
404 * integers: some broken software can encode a positive INTEGER with its MSB
405 * set as negative (it doesn't add a padding zero).
408 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
411 ASN1_INTEGER *ret = NULL;
412 const unsigned char *p;
415 int inf, tag, xclass;
418 if ((a == NULL) || ((*a) == NULL)) {
419 if ((ret = ASN1_INTEGER_new()) == NULL)
421 ret->type = V_ASN1_INTEGER;
426 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
428 i = ASN1_R_BAD_OBJECT_HEADER;
432 if (tag != V_ASN1_INTEGER) {
433 i = ASN1_R_EXPECTING_AN_INTEGER;
438 * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
439 * a missing NULL parameter.
441 s = OPENSSL_malloc((int)len + 1);
443 i = ERR_R_MALLOC_FAILURE;
446 ret->type = V_ASN1_INTEGER;
448 if ((*p == 0) && (len != 1)) {
452 memcpy(s, p, (int)len);
456 OPENSSL_free(ret->data);
458 ret->length = (int)len;
464 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
465 if ((a == NULL) || (*a != ret))
466 ASN1_INTEGER_free(ret);
470 static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
477 ret = ASN1_STRING_type_new(atype);
484 ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_NESTED_ASN1_ERROR);
488 if (BN_is_negative(bn) && !BN_is_zero(bn))
489 ret->type |= V_ASN1_NEG_INTEGER;
491 len = BN_num_bytes(bn);
496 if (ASN1_STRING_set(ret, NULL, len) == 0) {
497 ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_MALLOC_FAILURE);
501 /* Correct zero case */
505 len = BN_bn2bin(bn, ret->data);
510 ASN1_INTEGER_free(ret);
514 static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
519 if ((ai->type & ~V_ASN1_NEG) != itype) {
520 ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
524 ret = BN_bin2bn(ai->data, ai->length, bn);
526 ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
529 if (ai->type & V_ASN1_NEG)
530 BN_set_negative(ret, 1);
534 int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
536 return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
539 int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
541 return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
544 int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
546 return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
549 int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
551 return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
554 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
556 return ASN1_INTEGER_set_int64(a, v);
559 long ASN1_INTEGER_get(const ASN1_INTEGER *a)
565 i = ASN1_INTEGER_get_int64(&r, a);
568 if (r > LONG_MAX || r < LONG_MIN)
573 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
575 return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
578 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
580 return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
583 int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
585 return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
588 int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
590 return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
593 int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
595 return ASN1_ENUMERATED_set_int64(a, v);
598 long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
604 if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
606 if (a->length > (int)sizeof(long))
608 i = ASN1_ENUMERATED_get_int64(&r, a);
611 if (r > LONG_MAX || r < LONG_MIN)
616 ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
618 return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
621 BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
623 return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);