2 * Copyright 1995-2017 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.
70 * If |pad| is zero, the operation is effectively reduced to memcpy,
71 * and if |pad| is 0xff, then it performs two's complement, ~dst + 1.
72 * Note that in latter case sequence of zeros yields itself, and so
73 * does 0x80 followed by any number of zeros. These properties are
74 * used elsewhere below...
76 static void twos_complement(unsigned char *dst, const unsigned char *src,
77 size_t len, unsigned char pad)
79 unsigned int carry = pad & 1;
81 /* Begin at the end of the encoding */
84 /* two's complement value: ~value + 1 */
86 *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
91 static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
96 unsigned char *p, pb = 0;
98 if (b != NULL && blen) {
101 if (!neg && (i > 127)) {
108 } else if (i == 128) {
110 * Special case [of minimal negative for given length]:
111 * if any other bytes non zero we pad, otherwise we don't.
113 for (pad = 0, i = 1; i < blen; i++)
115 pb = pad != 0 ? 0xffU : 0;
122 blen = 0; /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
125 if (pp == NULL || (p = *pp) == NULL)
129 * This magically handles all corner cases, such as '(b == NULL ||
130 * blen == 0)', non-negative value, "negative" zero, 0x80 followed
131 * by any number of zeros...
134 p += pad; /* yes, p[0] can be written twice, but it's little
135 * price to pay for eliminated branches */
136 twos_complement(p, b, blen, pb);
143 * convert content octets into a big endian buffer. Returns the length
144 * of buffer or 0 on error: for malformed INTEGER. If output buffer is
145 * NULL just return length.
148 static size_t c2i_ibuf(unsigned char *b, int *pneg,
149 const unsigned char *p, size_t plen)
152 /* Zero content length is illegal */
154 ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
160 /* Handle common case where length is 1 octet separately */
164 b[0] = (p[0] ^ 0xFF) + 1;
170 if (p[0] == 0 || p[0] == 0xFF)
174 /* reject illegal padding: first two octets MSB can't match */
175 if (pad && (neg == (p[1] & 0x80))) {
176 ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
185 twos_complement(b, p, plen, neg ? 0xffU : 0);
190 int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
192 return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
195 /* Convert big endian buffer into uint64_t, return 0 on error */
196 static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
201 if (blen > sizeof(*pr)) {
202 ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
208 for (r = 0, i = 0; i < blen; i++) {
217 * Write uint64_t to big endian buffer and return offset to first
218 * written octet. In other words it returns offset in range from 0
219 * to 7, with 0 denoting 8 written octets and 7 - one.
221 static size_t asn1_put_uint64(unsigned char b[sizeof(uint64_t)], uint64_t r)
223 size_t off = sizeof(uint64_t);
226 b[--off] = (unsigned char)r;
233 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as it produces
237 #define ABS_INT64_MIN \
238 ((uint64_t)INT64_MAX + (uint64_t)(-(INT64_MIN + INT64_MAX)))
240 /* signed version of asn1_get_uint64 */
241 static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
245 if (asn1_get_uint64(&r, b, blen) == 0)
248 if (r > ABS_INT64_MIN) {
249 ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
252 *pr = 0 - (uint64_t)r;
255 ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
263 /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
264 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
267 ASN1_INTEGER *ret = NULL;
271 r = c2i_ibuf(NULL, NULL, *pp, len);
276 if ((a == NULL) || ((*a) == NULL)) {
277 ret = ASN1_INTEGER_new();
280 ret->type = V_ASN1_INTEGER;
284 if (ASN1_STRING_set(ret, NULL, r) == 0)
287 c2i_ibuf(ret->data, &neg, *pp, len);
290 ret->type |= V_ASN1_NEG;
297 ASN1err(ASN1_F_C2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
298 if ((a == NULL) || (*a != ret))
299 ASN1_INTEGER_free(ret);
303 static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
306 ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
309 if ((a->type & ~V_ASN1_NEG) != itype) {
310 ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ASN1_R_WRONG_INTEGER_TYPE);
313 return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
316 static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
318 unsigned char tbuf[sizeof(r)];
323 off = asn1_put_uint64(tbuf, -r);
324 a->type |= V_ASN1_NEG;
326 off = asn1_put_uint64(tbuf, r);
327 a->type &= ~V_ASN1_NEG;
329 return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
332 static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
336 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ERR_R_PASSED_NULL_PARAMETER);
339 if ((a->type & ~V_ASN1_NEG) != itype) {
340 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_WRONG_INTEGER_TYPE);
343 if (a->type & V_ASN1_NEG) {
344 ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
347 return asn1_get_uint64(pr, a->data, a->length);
350 static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
352 unsigned char tbuf[sizeof(r)];
356 off = asn1_put_uint64(tbuf, r);
357 return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
361 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
362 * integers: some broken software can encode a positive INTEGER with its MSB
363 * set as negative (it doesn't add a padding zero).
366 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
369 ASN1_INTEGER *ret = NULL;
370 const unsigned char *p;
373 int inf, tag, xclass;
376 if ((a == NULL) || ((*a) == NULL)) {
377 if ((ret = ASN1_INTEGER_new()) == NULL)
379 ret->type = V_ASN1_INTEGER;
384 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
386 i = ASN1_R_BAD_OBJECT_HEADER;
390 if (tag != V_ASN1_INTEGER) {
391 i = ASN1_R_EXPECTING_AN_INTEGER;
396 * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
397 * a missing NULL parameter.
399 s = OPENSSL_malloc((int)len + 1);
401 i = ERR_R_MALLOC_FAILURE;
404 ret->type = V_ASN1_INTEGER;
406 if ((*p == 0) && (len != 1)) {
410 memcpy(s, p, (int)len);
414 OPENSSL_free(ret->data);
416 ret->length = (int)len;
422 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
423 if ((a == NULL) || (*a != ret))
424 ASN1_INTEGER_free(ret);
428 static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
435 ret = ASN1_STRING_type_new(atype);
442 ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_NESTED_ASN1_ERROR);
446 if (BN_is_negative(bn) && !BN_is_zero(bn))
447 ret->type |= V_ASN1_NEG_INTEGER;
449 len = BN_num_bytes(bn);
454 if (ASN1_STRING_set(ret, NULL, len) == 0) {
455 ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_MALLOC_FAILURE);
459 /* Correct zero case */
463 len = BN_bn2bin(bn, ret->data);
468 ASN1_INTEGER_free(ret);
472 static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
477 if ((ai->type & ~V_ASN1_NEG) != itype) {
478 ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
482 ret = BN_bin2bn(ai->data, ai->length, bn);
484 ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
487 if (ai->type & V_ASN1_NEG)
488 BN_set_negative(ret, 1);
492 int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
494 return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
497 int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
499 return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
502 int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
504 return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
507 int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
509 return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
512 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
514 return ASN1_INTEGER_set_int64(a, v);
517 long ASN1_INTEGER_get(const ASN1_INTEGER *a)
523 i = ASN1_INTEGER_get_int64(&r, a);
526 if (r > LONG_MAX || r < LONG_MIN)
531 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
533 return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
536 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
538 return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
541 int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
543 return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
546 int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
548 return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
551 int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
553 return ASN1_ENUMERATED_set_int64(a, v);
556 long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
562 if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
564 if (a->length > (int)sizeof(long))
566 i = ASN1_ENUMERATED_get_int64(&r, a);
569 if (r > LONG_MAX || r < LONG_MIN)
574 ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
576 return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
579 BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
581 return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
584 /* Internal functions used by x_int64.c */
585 int c2i_uint64_int(uint64_t *ret, int *neg, const unsigned char **pp, long len)
587 unsigned char buf[sizeof(uint64_t)];
590 buflen = c2i_ibuf(NULL, NULL, *pp, len);
593 if (buflen > sizeof(uint64_t)) {
594 ASN1err(ASN1_F_C2I_UINT64_INT, ASN1_R_TOO_LARGE);
597 (void)c2i_ibuf(buf, neg, *pp, len);
598 return asn1_get_uint64(ret, buf, buflen);
601 int i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
603 unsigned char buf[sizeof(uint64_t)];
606 off = asn1_put_uint64(buf, r);
607 return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);