a_int.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/numbers.h"
  12. #include <limits.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/bn.h>
  15. #include "asn1_local.h"
  16. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
  17. {
  18. return ASN1_STRING_dup(x);
  19. }
  20. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
  21. {
  22. int neg, ret;
  23. /* Compare signs */
  24. neg = x->type & V_ASN1_NEG;
  25. if (neg != (y->type & V_ASN1_NEG)) {
  26. if (neg)
  27. return -1;
  28. else
  29. return 1;
  30. }
  31. ret = ASN1_STRING_cmp(x, y);
  32. if (neg)
  33. return -ret;
  34. else
  35. return ret;
  36. }
  37. /*-
  38. * This converts a big endian buffer and sign into its content encoding.
  39. * This is used for INTEGER and ENUMERATED types.
  40. * The internal representation is an ASN1_STRING whose data is a big endian
  41. * representation of the value, ignoring the sign. The sign is determined by
  42. * the type: if type & V_ASN1_NEG is true it is negative, otherwise positive.
  43. *
  44. * Positive integers are no problem: they are almost the same as the DER
  45. * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
  46. *
  47. * Negative integers are a bit trickier...
  48. * The DER representation of negative integers is in 2s complement form.
  49. * The internal form is converted by complementing each octet and finally
  50. * adding one to the result. This can be done less messily with a little trick.
  51. * If the internal form has trailing zeroes then they will become FF by the
  52. * complement and 0 by the add one (due to carry) so just copy as many trailing
  53. * zeros to the destination as there are in the source. The carry will add one
  54. * to the last none zero octet: so complement this octet and add one and finally
  55. * complement any left over until you get to the start of the string.
  56. *
  57. * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
  58. * with 0xff. However if the first byte is 0x80 and one of the following bytes
  59. * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
  60. * followed by optional zeros isn't padded.
  61. */
  62. /*
  63. * If |pad| is zero, the operation is effectively reduced to memcpy,
  64. * and if |pad| is 0xff, then it performs two's complement, ~dst + 1.
  65. * Note that in latter case sequence of zeros yields itself, and so
  66. * does 0x80 followed by any number of zeros. These properties are
  67. * used elsewhere below...
  68. */
  69. static void twos_complement(unsigned char *dst, const unsigned char *src,
  70. size_t len, unsigned char pad)
  71. {
  72. unsigned int carry = pad & 1;
  73. /* Begin at the end of the encoding */
  74. if (len != 0) {
  75. /*
  76. * if len == 0 then src/dst could be NULL, and this would be undefined
  77. * behaviour.
  78. */
  79. dst += len;
  80. src += len;
  81. }
  82. /* two's complement value: ~value + 1 */
  83. while (len-- != 0) {
  84. *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
  85. carry >>= 8;
  86. }
  87. }
  88. static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
  89. unsigned char **pp)
  90. {
  91. unsigned int pad = 0;
  92. size_t ret, i;
  93. unsigned char *p, pb = 0;
  94. if (b != NULL && blen) {
  95. ret = blen;
  96. i = b[0];
  97. if (!neg && (i > 127)) {
  98. pad = 1;
  99. pb = 0;
  100. } else if (neg) {
  101. pb = 0xFF;
  102. if (i > 128) {
  103. pad = 1;
  104. } else if (i == 128) {
  105. /*
  106. * Special case [of minimal negative for given length]:
  107. * if any other bytes non zero we pad, otherwise we don't.
  108. */
  109. for (pad = 0, i = 1; i < blen; i++)
  110. pad |= b[i];
  111. pb = pad != 0 ? 0xffU : 0;
  112. pad = pb & 1;
  113. }
  114. }
  115. ret += pad;
  116. } else {
  117. ret = 1;
  118. blen = 0; /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
  119. }
  120. if (pp == NULL || (p = *pp) == NULL)
  121. return ret;
  122. /*
  123. * This magically handles all corner cases, such as '(b == NULL ||
  124. * blen == 0)', non-negative value, "negative" zero, 0x80 followed
  125. * by any number of zeros...
  126. */
  127. *p = pb;
  128. p += pad; /* yes, p[0] can be written twice, but it's little
  129. * price to pay for eliminated branches */
  130. twos_complement(p, b, blen, pb);
  131. *pp += ret;
  132. return ret;
  133. }
  134. /*
  135. * convert content octets into a big endian buffer. Returns the length
  136. * of buffer or 0 on error: for malformed INTEGER. If output buffer is
  137. * NULL just return length.
  138. */
  139. static size_t c2i_ibuf(unsigned char *b, int *pneg,
  140. const unsigned char *p, size_t plen)
  141. {
  142. int neg, pad;
  143. /* Zero content length is illegal */
  144. if (plen == 0) {
  145. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
  146. return 0;
  147. }
  148. neg = p[0] & 0x80;
  149. if (pneg)
  150. *pneg = neg;
  151. /* Handle common case where length is 1 octet separately */
  152. if (plen == 1) {
  153. if (b != NULL) {
  154. if (neg)
  155. b[0] = (p[0] ^ 0xFF) + 1;
  156. else
  157. b[0] = p[0];
  158. }
  159. return 1;
  160. }
  161. pad = 0;
  162. if (p[0] == 0) {
  163. pad = 1;
  164. } else if (p[0] == 0xFF) {
  165. size_t i;
  166. /*
  167. * Special case [of "one less minimal negative" for given length]:
  168. * if any other bytes non zero it was padded, otherwise not.
  169. */
  170. for (pad = 0, i = 1; i < plen; i++)
  171. pad |= p[i];
  172. pad = pad != 0 ? 1 : 0;
  173. }
  174. /* reject illegal padding: first two octets MSB can't match */
  175. if (pad && (neg == (p[1] & 0x80))) {
  176. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
  177. return 0;
  178. }
  179. /* skip over pad */
  180. p += pad;
  181. plen -= pad;
  182. if (b != NULL)
  183. twos_complement(b, p, plen, neg ? 0xffU : 0);
  184. return plen;
  185. }
  186. int ossl_i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
  187. {
  188. return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
  189. }
  190. /* Convert big endian buffer into uint64_t, return 0 on error */
  191. static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
  192. {
  193. size_t i;
  194. uint64_t r;
  195. if (blen > sizeof(*pr)) {
  196. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  197. return 0;
  198. }
  199. if (b == NULL)
  200. return 0;
  201. for (r = 0, i = 0; i < blen; i++) {
  202. r <<= 8;
  203. r |= b[i];
  204. }
  205. *pr = r;
  206. return 1;
  207. }
  208. /*
  209. * Write uint64_t to big endian buffer and return offset to first
  210. * written octet. In other words it returns offset in range from 0
  211. * to 7, with 0 denoting 8 written octets and 7 - one.
  212. */
  213. static size_t asn1_put_uint64(unsigned char b[sizeof(uint64_t)], uint64_t r)
  214. {
  215. size_t off = sizeof(uint64_t);
  216. do {
  217. b[--off] = (unsigned char)r;
  218. } while (r >>= 8);
  219. return off;
  220. }
  221. /*
  222. * Absolute value of INT64_MIN: we can't just use -INT64_MIN as gcc produces
  223. * overflow warnings.
  224. */
  225. #define ABS_INT64_MIN ((uint64_t)INT64_MAX + (-(INT64_MIN + INT64_MAX)))
  226. /* signed version of asn1_get_uint64 */
  227. static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
  228. int neg)
  229. {
  230. uint64_t r;
  231. if (asn1_get_uint64(&r, b, blen) == 0)
  232. return 0;
  233. if (neg) {
  234. if (r <= INT64_MAX) {
  235. /* Most significant bit is guaranteed to be clear, negation
  236. * is guaranteed to be meaningful in platform-neutral sense. */
  237. *pr = -(int64_t)r;
  238. } else if (r == ABS_INT64_MIN) {
  239. /* This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
  240. * on ones'-complement system. */
  241. *pr = (int64_t)(0 - r);
  242. } else {
  243. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
  244. return 0;
  245. }
  246. } else {
  247. if (r <= INT64_MAX) {
  248. *pr = (int64_t)r;
  249. } else {
  250. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  251. return 0;
  252. }
  253. }
  254. return 1;
  255. }
  256. /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
  257. ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  258. long len)
  259. {
  260. ASN1_INTEGER *ret = NULL;
  261. size_t r;
  262. int neg;
  263. r = c2i_ibuf(NULL, NULL, *pp, len);
  264. if (r == 0)
  265. return NULL;
  266. if ((a == NULL) || ((*a) == NULL)) {
  267. ret = ASN1_INTEGER_new();
  268. if (ret == NULL)
  269. return NULL;
  270. ret->type = V_ASN1_INTEGER;
  271. } else
  272. ret = *a;
  273. if (ASN1_STRING_set(ret, NULL, r) == 0)
  274. goto err;
  275. c2i_ibuf(ret->data, &neg, *pp, len);
  276. if (neg != 0)
  277. ret->type |= V_ASN1_NEG;
  278. else
  279. ret->type &= ~V_ASN1_NEG;
  280. *pp += len;
  281. if (a != NULL)
  282. (*a) = ret;
  283. return ret;
  284. err:
  285. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  286. if (a == NULL || *a != ret)
  287. ASN1_INTEGER_free(ret);
  288. return NULL;
  289. }
  290. static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
  291. {
  292. if (a == NULL) {
  293. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  294. return 0;
  295. }
  296. if ((a->type & ~V_ASN1_NEG) != itype) {
  297. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
  298. return 0;
  299. }
  300. return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
  301. }
  302. static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
  303. {
  304. unsigned char tbuf[sizeof(r)];
  305. size_t off;
  306. a->type = itype;
  307. if (r < 0) {
  308. /* Most obvious '-r' triggers undefined behaviour for most
  309. * common INT64_MIN. Even though below '0 - (uint64_t)r' can
  310. * appear two's-complement centric, it does produce correct/
  311. * expected result even on one's-complement. This is because
  312. * cast to unsigned has to change bit pattern... */
  313. off = asn1_put_uint64(tbuf, 0 - (uint64_t)r);
  314. a->type |= V_ASN1_NEG;
  315. } else {
  316. off = asn1_put_uint64(tbuf, r);
  317. a->type &= ~V_ASN1_NEG;
  318. }
  319. return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
  320. }
  321. static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
  322. int itype)
  323. {
  324. if (a == NULL) {
  325. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  326. return 0;
  327. }
  328. if ((a->type & ~V_ASN1_NEG) != itype) {
  329. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
  330. return 0;
  331. }
  332. if (a->type & V_ASN1_NEG) {
  333. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  334. return 0;
  335. }
  336. return asn1_get_uint64(pr, a->data, a->length);
  337. }
  338. static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
  339. {
  340. unsigned char tbuf[sizeof(r)];
  341. size_t off;
  342. a->type = itype;
  343. off = asn1_put_uint64(tbuf, r);
  344. return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
  345. }
  346. /*
  347. * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
  348. * integers: some broken software can encode a positive INTEGER with its MSB
  349. * set as negative (it doesn't add a padding zero).
  350. */
  351. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  352. long length)
  353. {
  354. ASN1_INTEGER *ret = NULL;
  355. const unsigned char *p;
  356. unsigned char *s;
  357. long len = 0;
  358. int inf, tag, xclass;
  359. int i;
  360. if ((a == NULL) || ((*a) == NULL)) {
  361. if ((ret = ASN1_INTEGER_new()) == NULL)
  362. return NULL;
  363. ret->type = V_ASN1_INTEGER;
  364. } else
  365. ret = (*a);
  366. p = *pp;
  367. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  368. if (inf & 0x80) {
  369. i = ASN1_R_BAD_OBJECT_HEADER;
  370. goto err;
  371. }
  372. if (tag != V_ASN1_INTEGER) {
  373. i = ASN1_R_EXPECTING_AN_INTEGER;
  374. goto err;
  375. }
  376. if (len < 0) {
  377. i = ASN1_R_ILLEGAL_NEGATIVE_VALUE;
  378. goto err;
  379. }
  380. /*
  381. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  382. * a missing NULL parameter.
  383. */
  384. s = OPENSSL_malloc((int)len + 1);
  385. if (s == NULL) {
  386. i = ERR_R_MALLOC_FAILURE;
  387. goto err;
  388. }
  389. ret->type = V_ASN1_INTEGER;
  390. if (len) {
  391. if ((*p == 0) && (len != 1)) {
  392. p++;
  393. len--;
  394. }
  395. memcpy(s, p, (int)len);
  396. p += len;
  397. }
  398. OPENSSL_free(ret->data);
  399. ret->data = s;
  400. ret->length = (int)len;
  401. if (a != NULL)
  402. (*a) = ret;
  403. *pp = p;
  404. return ret;
  405. err:
  406. ERR_raise(ERR_LIB_ASN1, i);
  407. if ((a == NULL) || (*a != ret))
  408. ASN1_INTEGER_free(ret);
  409. return NULL;
  410. }
  411. static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
  412. int atype)
  413. {
  414. ASN1_INTEGER *ret;
  415. int len;
  416. if (ai == NULL) {
  417. ret = ASN1_STRING_type_new(atype);
  418. } else {
  419. ret = ai;
  420. ret->type = atype;
  421. }
  422. if (ret == NULL) {
  423. ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
  424. goto err;
  425. }
  426. if (BN_is_negative(bn) && !BN_is_zero(bn))
  427. ret->type |= V_ASN1_NEG_INTEGER;
  428. len = BN_num_bytes(bn);
  429. if (len == 0)
  430. len = 1;
  431. if (ASN1_STRING_set(ret, NULL, len) == 0) {
  432. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  433. goto err;
  434. }
  435. /* Correct zero case */
  436. if (BN_is_zero(bn))
  437. ret->data[0] = 0;
  438. else
  439. len = BN_bn2bin(bn, ret->data);
  440. ret->length = len;
  441. return ret;
  442. err:
  443. if (ret != ai)
  444. ASN1_INTEGER_free(ret);
  445. return NULL;
  446. }
  447. static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
  448. int itype)
  449. {
  450. BIGNUM *ret;
  451. if ((ai->type & ~V_ASN1_NEG) != itype) {
  452. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
  453. return NULL;
  454. }
  455. ret = BN_bin2bn(ai->data, ai->length, bn);
  456. if (ret == NULL) {
  457. ERR_raise(ERR_LIB_ASN1, ASN1_R_BN_LIB);
  458. return NULL;
  459. }
  460. if (ai->type & V_ASN1_NEG)
  461. BN_set_negative(ret, 1);
  462. return ret;
  463. }
  464. int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
  465. {
  466. return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
  467. }
  468. int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
  469. {
  470. return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
  471. }
  472. int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
  473. {
  474. return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
  475. }
  476. int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
  477. {
  478. return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
  479. }
  480. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
  481. {
  482. return ASN1_INTEGER_set_int64(a, v);
  483. }
  484. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  485. {
  486. int i;
  487. int64_t r;
  488. if (a == NULL)
  489. return 0;
  490. i = ASN1_INTEGER_get_int64(&r, a);
  491. if (i == 0)
  492. return -1;
  493. if (r > LONG_MAX || r < LONG_MIN)
  494. return -1;
  495. return (long)r;
  496. }
  497. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  498. {
  499. return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
  500. }
  501. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  502. {
  503. return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
  504. }
  505. int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
  506. {
  507. return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
  508. }
  509. int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
  510. {
  511. return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
  512. }
  513. int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
  514. {
  515. return ASN1_ENUMERATED_set_int64(a, v);
  516. }
  517. long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
  518. {
  519. int i;
  520. int64_t r;
  521. if (a == NULL)
  522. return 0;
  523. if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
  524. return -1;
  525. if (a->length > (int)sizeof(long))
  526. return 0xffffffffL;
  527. i = ASN1_ENUMERATED_get_int64(&r, a);
  528. if (i == 0)
  529. return -1;
  530. if (r > LONG_MAX || r < LONG_MIN)
  531. return -1;
  532. return (long)r;
  533. }
  534. ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
  535. {
  536. return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
  537. }
  538. BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
  539. {
  540. return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
  541. }
  542. /* Internal functions used by x_int64.c */
  543. int ossl_c2i_uint64_int(uint64_t *ret, int *neg,
  544. const unsigned char **pp, long len)
  545. {
  546. unsigned char buf[sizeof(uint64_t)];
  547. size_t buflen;
  548. buflen = c2i_ibuf(NULL, NULL, *pp, len);
  549. if (buflen == 0)
  550. return 0;
  551. if (buflen > sizeof(uint64_t)) {
  552. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  553. return 0;
  554. }
  555. (void)c2i_ibuf(buf, neg, *pp, len);
  556. return asn1_get_uint64(ret, buf, buflen);
  557. }
  558. int ossl_i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
  559. {
  560. unsigned char buf[sizeof(uint64_t)];
  561. size_t off;
  562. off = asn1_put_uint64(buf, r);
  563. return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
  564. }