a_int.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. /*
  236. * Most significant bit is guaranteed to be clear, negation
  237. * is guaranteed to be meaningful in platform-neutral sense.
  238. */
  239. *pr = -(int64_t)r;
  240. } else if (r == ABS_INT64_MIN) {
  241. /*
  242. * This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
  243. * on ones'-complement system.
  244. */
  245. *pr = (int64_t)(0 - r);
  246. } else {
  247. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
  248. return 0;
  249. }
  250. } else {
  251. if (r <= INT64_MAX) {
  252. *pr = (int64_t)r;
  253. } else {
  254. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  255. return 0;
  256. }
  257. }
  258. return 1;
  259. }
  260. /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
  261. ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  262. long len)
  263. {
  264. ASN1_INTEGER *ret = NULL;
  265. size_t r;
  266. int neg;
  267. r = c2i_ibuf(NULL, NULL, *pp, len);
  268. if (r == 0)
  269. return NULL;
  270. if ((a == NULL) || ((*a) == NULL)) {
  271. ret = ASN1_INTEGER_new();
  272. if (ret == NULL)
  273. return NULL;
  274. ret->type = V_ASN1_INTEGER;
  275. } else
  276. ret = *a;
  277. if (ASN1_STRING_set(ret, NULL, r) == 0) {
  278. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  279. goto err;
  280. }
  281. c2i_ibuf(ret->data, &neg, *pp, len);
  282. if (neg != 0)
  283. ret->type |= V_ASN1_NEG;
  284. else
  285. ret->type &= ~V_ASN1_NEG;
  286. *pp += len;
  287. if (a != NULL)
  288. (*a) = ret;
  289. return ret;
  290. err:
  291. if (a == NULL || *a != ret)
  292. ASN1_INTEGER_free(ret);
  293. return NULL;
  294. }
  295. static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
  296. {
  297. if (a == NULL) {
  298. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  299. return 0;
  300. }
  301. if ((a->type & ~V_ASN1_NEG) != itype) {
  302. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
  303. return 0;
  304. }
  305. return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
  306. }
  307. static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
  308. {
  309. unsigned char tbuf[sizeof(r)];
  310. size_t off;
  311. a->type = itype;
  312. if (r < 0) {
  313. /*
  314. * Most obvious '-r' triggers undefined behaviour for most
  315. * common INT64_MIN. Even though below '0 - (uint64_t)r' can
  316. * appear two's-complement centric, it does produce correct/
  317. * expected result even on ones' complement. This is because
  318. * cast to unsigned has to change bit pattern...
  319. */
  320. off = asn1_put_uint64(tbuf, 0 - (uint64_t)r);
  321. a->type |= V_ASN1_NEG;
  322. } else {
  323. off = asn1_put_uint64(tbuf, r);
  324. a->type &= ~V_ASN1_NEG;
  325. }
  326. return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
  327. }
  328. static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
  329. int itype)
  330. {
  331. if (a == NULL) {
  332. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  333. return 0;
  334. }
  335. if ((a->type & ~V_ASN1_NEG) != itype) {
  336. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
  337. return 0;
  338. }
  339. if (a->type & V_ASN1_NEG) {
  340. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  341. return 0;
  342. }
  343. return asn1_get_uint64(pr, a->data, a->length);
  344. }
  345. static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
  346. {
  347. unsigned char tbuf[sizeof(r)];
  348. size_t off;
  349. a->type = itype;
  350. off = asn1_put_uint64(tbuf, r);
  351. return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
  352. }
  353. /*
  354. * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
  355. * integers: some broken software can encode a positive INTEGER with its MSB
  356. * set as negative (it doesn't add a padding zero).
  357. */
  358. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  359. long length)
  360. {
  361. ASN1_INTEGER *ret = NULL;
  362. const unsigned char *p;
  363. unsigned char *s;
  364. long len = 0;
  365. int inf, tag, xclass;
  366. int i = 0;
  367. if ((a == NULL) || ((*a) == NULL)) {
  368. if ((ret = ASN1_INTEGER_new()) == NULL)
  369. return NULL;
  370. ret->type = V_ASN1_INTEGER;
  371. } else
  372. ret = (*a);
  373. p = *pp;
  374. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  375. if (inf & 0x80) {
  376. i = ASN1_R_BAD_OBJECT_HEADER;
  377. goto err;
  378. }
  379. if (tag != V_ASN1_INTEGER) {
  380. i = ASN1_R_EXPECTING_AN_INTEGER;
  381. goto err;
  382. }
  383. if (len < 0) {
  384. i = ASN1_R_ILLEGAL_NEGATIVE_VALUE;
  385. goto err;
  386. }
  387. /*
  388. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  389. * a missing NULL parameter.
  390. */
  391. s = OPENSSL_malloc((int)len + 1);
  392. if (s == NULL)
  393. goto err;
  394. ret->type = V_ASN1_INTEGER;
  395. if (len) {
  396. if ((*p == 0) && (len != 1)) {
  397. p++;
  398. len--;
  399. }
  400. memcpy(s, p, (int)len);
  401. p += len;
  402. }
  403. ASN1_STRING_set0(ret, s, (int)len);
  404. if (a != NULL)
  405. (*a) = ret;
  406. *pp = p;
  407. return ret;
  408. err:
  409. if (i != 0)
  410. ERR_raise(ERR_LIB_ASN1, i);
  411. if ((a == NULL) || (*a != ret))
  412. ASN1_INTEGER_free(ret);
  413. return NULL;
  414. }
  415. static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
  416. int atype)
  417. {
  418. ASN1_INTEGER *ret;
  419. int len;
  420. if (ai == NULL) {
  421. ret = ASN1_STRING_type_new(atype);
  422. } else {
  423. ret = ai;
  424. ret->type = atype;
  425. }
  426. if (ret == NULL) {
  427. ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
  428. goto err;
  429. }
  430. if (BN_is_negative(bn) && !BN_is_zero(bn))
  431. ret->type |= V_ASN1_NEG_INTEGER;
  432. len = BN_num_bytes(bn);
  433. if (len == 0)
  434. len = 1;
  435. if (ASN1_STRING_set(ret, NULL, len) == 0) {
  436. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  437. goto err;
  438. }
  439. /* Correct zero case */
  440. if (BN_is_zero(bn))
  441. ret->data[0] = 0;
  442. else
  443. len = BN_bn2bin(bn, ret->data);
  444. ret->length = len;
  445. return ret;
  446. err:
  447. if (ret != ai)
  448. ASN1_INTEGER_free(ret);
  449. return NULL;
  450. }
  451. static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
  452. int itype)
  453. {
  454. BIGNUM *ret;
  455. if ((ai->type & ~V_ASN1_NEG) != itype) {
  456. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
  457. return NULL;
  458. }
  459. ret = BN_bin2bn(ai->data, ai->length, bn);
  460. if (ret == NULL) {
  461. ERR_raise(ERR_LIB_ASN1, ASN1_R_BN_LIB);
  462. return NULL;
  463. }
  464. if (ai->type & V_ASN1_NEG)
  465. BN_set_negative(ret, 1);
  466. return ret;
  467. }
  468. int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
  469. {
  470. return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
  471. }
  472. int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
  473. {
  474. return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
  475. }
  476. int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
  477. {
  478. return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
  479. }
  480. int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
  481. {
  482. return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
  483. }
  484. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
  485. {
  486. return ASN1_INTEGER_set_int64(a, v);
  487. }
  488. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  489. {
  490. int i;
  491. int64_t r;
  492. if (a == NULL)
  493. return 0;
  494. i = ASN1_INTEGER_get_int64(&r, a);
  495. if (i == 0)
  496. return -1;
  497. if (r > LONG_MAX || r < LONG_MIN)
  498. return -1;
  499. return (long)r;
  500. }
  501. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  502. {
  503. return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
  504. }
  505. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  506. {
  507. return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
  508. }
  509. int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
  510. {
  511. return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
  512. }
  513. int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
  514. {
  515. return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
  516. }
  517. int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
  518. {
  519. return ASN1_ENUMERATED_set_int64(a, v);
  520. }
  521. long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
  522. {
  523. int i;
  524. int64_t r;
  525. if (a == NULL)
  526. return 0;
  527. if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
  528. return -1;
  529. if (a->length > (int)sizeof(long))
  530. return 0xffffffffL;
  531. i = ASN1_ENUMERATED_get_int64(&r, a);
  532. if (i == 0)
  533. return -1;
  534. if (r > LONG_MAX || r < LONG_MIN)
  535. return -1;
  536. return (long)r;
  537. }
  538. ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
  539. {
  540. return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
  541. }
  542. BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
  543. {
  544. return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
  545. }
  546. /* Internal functions used by x_int64.c */
  547. int ossl_c2i_uint64_int(uint64_t *ret, int *neg,
  548. const unsigned char **pp, long len)
  549. {
  550. unsigned char buf[sizeof(uint64_t)];
  551. size_t buflen;
  552. buflen = c2i_ibuf(NULL, NULL, *pp, len);
  553. if (buflen == 0)
  554. return 0;
  555. if (buflen > sizeof(uint64_t)) {
  556. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  557. return 0;
  558. }
  559. (void)c2i_ibuf(buf, neg, *pp, len);
  560. return asn1_get_uint64(ret, buf, buflen);
  561. }
  562. int ossl_i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
  563. {
  564. unsigned char buf[sizeof(uint64_t)];
  565. size_t off;
  566. off = asn1_put_uint64(buf, r);
  567. return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
  568. }