a_int.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include "internal/cryptlib.h"
  59. #include "internal/numbers.h"
  60. #include <limits.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/bn.h>
  63. #include "asn1_locl.h"
  64. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
  65. {
  66. return ASN1_STRING_dup(x);
  67. }
  68. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
  69. {
  70. int neg, ret;
  71. /* Compare signs */
  72. neg = x->type & V_ASN1_NEG;
  73. if (neg != (y->type & V_ASN1_NEG)) {
  74. if (neg)
  75. return -1;
  76. else
  77. return 1;
  78. }
  79. ret = ASN1_STRING_cmp(x, y);
  80. if (neg)
  81. return -ret;
  82. else
  83. return ret;
  84. }
  85. /*-
  86. * This converts a big endian buffer and sign into its content encoding.
  87. * This is used for INTEGER and ENUMERATED types.
  88. * The internal representation is an ASN1_STRING whose data is a big endian
  89. * representation of the value, ignoring the sign. The sign is determined by
  90. * the type: if type & V_ASN1_NEG is true it is negative, otherwise positive.
  91. *
  92. * Positive integers are no problem: they are almost the same as the DER
  93. * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
  94. *
  95. * Negative integers are a bit trickier...
  96. * The DER representation of negative integers is in 2s complement form.
  97. * The internal form is converted by complementing each octet and finally
  98. * adding one to the result. This can be done less messily with a little trick.
  99. * If the internal form has trailing zeroes then they will become FF by the
  100. * complement and 0 by the add one (due to carry) so just copy as many trailing
  101. * zeros to the destination as there are in the source. The carry will add one
  102. * to the last none zero octet: so complement this octet and add one and finally
  103. * complement any left over until you get to the start of the string.
  104. *
  105. * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
  106. * with 0xff. However if the first byte is 0x80 and one of the following bytes
  107. * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
  108. * followed by optional zeros isn't padded.
  109. */
  110. static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
  111. unsigned char **pp)
  112. {
  113. int pad = 0;
  114. size_t ret, i;
  115. unsigned char *p, pb = 0;
  116. const unsigned char *n;
  117. if (b == NULL || blen == 0)
  118. ret = 1;
  119. else {
  120. ret = blen;
  121. i = b[0];
  122. if (ret == 1 && i == 0)
  123. neg = 0;
  124. if (!neg && (i > 127)) {
  125. pad = 1;
  126. pb = 0;
  127. } else if (neg) {
  128. if (i > 128) {
  129. pad = 1;
  130. pb = 0xFF;
  131. } else if (i == 128) {
  132. /*
  133. * Special case: if any other bytes non zero we pad:
  134. * otherwise we don't.
  135. */
  136. for (i = 1; i < blen; i++)
  137. if (b[i]) {
  138. pad = 1;
  139. pb = 0xFF;
  140. break;
  141. }
  142. }
  143. }
  144. ret += pad;
  145. }
  146. if (pp == NULL)
  147. return ret;
  148. p = *pp;
  149. if (pad)
  150. *(p++) = pb;
  151. if (b == NULL || blen == 0)
  152. *p = 0;
  153. else if (!neg)
  154. memcpy(p, b, blen);
  155. else {
  156. /* Begin at the end of the encoding */
  157. n = b + blen - 1;
  158. p += blen - 1;
  159. i = blen;
  160. /* Copy zeros to destination as long as source is zero */
  161. while (!*n && i > 1) {
  162. *(p--) = 0;
  163. n--;
  164. i--;
  165. }
  166. /* Complement and increment next octet */
  167. *(p--) = ((*(n--)) ^ 0xff) + 1;
  168. i--;
  169. /* Complement any octets left */
  170. for (; i > 0; i--)
  171. *(p--) = *(n--) ^ 0xff;
  172. }
  173. *pp += ret;
  174. return ret;
  175. }
  176. /*
  177. * convert content octets into a big endian buffer. Returns the length
  178. * of buffer or 0 on error: for malformed INTEGER. If output bufer is
  179. * NULL just return length.
  180. */
  181. static size_t c2i_ibuf(unsigned char *b, int *pneg,
  182. const unsigned char *p, size_t plen)
  183. {
  184. size_t i;
  185. int neg, pad;
  186. /* Zero content length is illegal */
  187. if (plen == 0) {
  188. ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
  189. return 0;
  190. }
  191. neg = p[0] & 0x80;
  192. if (pneg)
  193. *pneg = neg;
  194. /* Handle common case where length is 1 octet separately */
  195. if (plen == 1) {
  196. if (b) {
  197. if (neg)
  198. b[0] = (p[0] ^ 0xFF) + 1;
  199. else
  200. b[0] = p[0];
  201. }
  202. return 1;
  203. }
  204. if (p[0] == 0 || p[0] == 0xFF)
  205. pad = 1;
  206. else
  207. pad = 0;
  208. /* reject illegal padding: first two octets MSB can't match */
  209. if (pad && (neg == (p[1] & 0x80))) {
  210. ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
  211. return 0;
  212. }
  213. /* If positive just copy across */
  214. if (neg == 0) {
  215. if (b)
  216. memcpy(b, p + pad, plen - pad);
  217. return plen - pad;
  218. }
  219. if (neg && pad) {
  220. /* check is any following octets are non zero */
  221. for (i = 1; i < plen; i++) {
  222. if (p[i] != 0)
  223. break;
  224. }
  225. /* if all bytes are zero handle as special case */
  226. if (i == plen) {
  227. if (b) {
  228. b[0] = 1;
  229. memset(b + 1, 0, plen - 1);
  230. }
  231. return plen;
  232. }
  233. }
  234. plen -= pad;
  235. /* Must be negative: calculate twos complement */
  236. if (b) {
  237. const unsigned char *from = p + plen - 1 + pad;
  238. unsigned char *to = b + plen - 1;
  239. i = plen;
  240. while (*from == 0 && i) {
  241. *to-- = 0;
  242. i--;
  243. from--;
  244. }
  245. *to-- = (*from-- ^ 0xff) + 1;
  246. OPENSSL_assert(i != 0);
  247. i--;
  248. for (; i > 0; i--)
  249. *to-- = *from-- ^ 0xff;
  250. }
  251. return plen;
  252. }
  253. int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
  254. {
  255. return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
  256. }
  257. /* Convert big endian buffer into uint64_t, return 0 on error */
  258. static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
  259. {
  260. size_t i;
  261. if (blen > sizeof(*pr)) {
  262. ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
  263. return 0;
  264. }
  265. *pr = 0;
  266. if (b == NULL)
  267. return 0;
  268. for (i = 0; i < blen; i++) {
  269. *pr <<= 8;
  270. *pr |= b[i];
  271. }
  272. return 1;
  273. }
  274. static size_t asn1_put_uint64(unsigned char *b, uint64_t r)
  275. {
  276. if (r >= 0x100) {
  277. unsigned char *p;
  278. uint64_t rtmp = r;
  279. size_t i = 0;
  280. /* Work out how many bytes we need */
  281. while (rtmp) {
  282. rtmp >>= 8;
  283. i++;
  284. }
  285. /* Copy from end to beginning */
  286. p = b + i - 1;
  287. do {
  288. *p-- = r & 0xFF;
  289. r >>= 8;
  290. } while (p >= b);
  291. return i;
  292. }
  293. b[0] = (unsigned char)r;
  294. return 1;
  295. }
  296. /*
  297. * Absolute value of INT64_MIN: we can't just use -INT64_MIN as it produces
  298. * overflow warnings.
  299. */
  300. #define ABS_INT64_MIN \
  301. ((uint64_t)INT64_MAX + (uint64_t)(-(INT64_MIN + INT64_MAX)))
  302. /* signed version of asn1_get_uint64 */
  303. static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
  304. int neg)
  305. {
  306. uint64_t r;
  307. if (asn1_get_uint64(&r, b, blen) == 0)
  308. return 0;
  309. if (neg) {
  310. if (r > ABS_INT64_MIN) {
  311. ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
  312. return 0;
  313. }
  314. *pr = -(int64_t)r;
  315. } else {
  316. if (r > INT64_MAX) {
  317. ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
  318. return 0;
  319. }
  320. *pr = (int64_t)r;
  321. }
  322. return 1;
  323. }
  324. /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
  325. ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  326. long len)
  327. {
  328. ASN1_INTEGER *ret = NULL;
  329. size_t r;
  330. int neg;
  331. r = c2i_ibuf(NULL, NULL, *pp, len);
  332. if (r == 0)
  333. return NULL;
  334. if ((a == NULL) || ((*a) == NULL)) {
  335. ret = ASN1_INTEGER_new();
  336. if (ret == NULL)
  337. return NULL;
  338. ret->type = V_ASN1_INTEGER;
  339. } else
  340. ret = *a;
  341. if (ASN1_STRING_set(ret, NULL, r) == 0)
  342. goto err;
  343. c2i_ibuf(ret->data, &neg, *pp, len);
  344. if (neg)
  345. ret->type |= V_ASN1_NEG;
  346. *pp += len;
  347. if (a != NULL)
  348. (*a) = ret;
  349. return ret;
  350. err:
  351. ASN1err(ASN1_F_C2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  352. if ((a == NULL) || (*a != ret))
  353. ASN1_INTEGER_free(ret);
  354. return NULL;
  355. }
  356. static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
  357. {
  358. if (a == NULL) {
  359. ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
  360. return 0;
  361. }
  362. if ((a->type & ~V_ASN1_NEG) != itype) {
  363. ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ASN1_R_WRONG_INTEGER_TYPE);
  364. return 0;
  365. }
  366. return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
  367. }
  368. static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
  369. {
  370. unsigned char tbuf[sizeof(r)];
  371. size_t l;
  372. a->type = itype;
  373. if (r < 0) {
  374. l = asn1_put_uint64(tbuf, -r);
  375. a->type |= V_ASN1_NEG;
  376. } else {
  377. l = asn1_put_uint64(tbuf, r);
  378. a->type &= ~V_ASN1_NEG;
  379. }
  380. if (l == 0)
  381. return 0;
  382. return ASN1_STRING_set(a, tbuf, l);
  383. }
  384. static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
  385. int itype)
  386. {
  387. if (a == NULL) {
  388. ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ERR_R_PASSED_NULL_PARAMETER);
  389. return 0;
  390. }
  391. if ((a->type & ~V_ASN1_NEG) != itype) {
  392. ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_WRONG_INTEGER_TYPE);
  393. return 0;
  394. }
  395. if (a->type & V_ASN1_NEG) {
  396. ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  397. return 0;
  398. }
  399. return asn1_get_uint64(pr, a->data, a->length);
  400. }
  401. static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
  402. {
  403. unsigned char tbuf[sizeof(r)];
  404. size_t l;
  405. a->type = itype;
  406. l = asn1_put_uint64(tbuf, r);
  407. if (l == 0)
  408. return 0;
  409. return ASN1_STRING_set(a, tbuf, l);
  410. }
  411. /*
  412. * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
  413. * integers: some broken software can encode a positive INTEGER with its MSB
  414. * set as negative (it doesn't add a padding zero).
  415. */
  416. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  417. long length)
  418. {
  419. ASN1_INTEGER *ret = NULL;
  420. const unsigned char *p;
  421. unsigned char *s;
  422. long len;
  423. int inf, tag, xclass;
  424. int i;
  425. if ((a == NULL) || ((*a) == NULL)) {
  426. if ((ret = ASN1_INTEGER_new()) == NULL)
  427. return (NULL);
  428. ret->type = V_ASN1_INTEGER;
  429. } else
  430. ret = (*a);
  431. p = *pp;
  432. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  433. if (inf & 0x80) {
  434. i = ASN1_R_BAD_OBJECT_HEADER;
  435. goto err;
  436. }
  437. if (tag != V_ASN1_INTEGER) {
  438. i = ASN1_R_EXPECTING_AN_INTEGER;
  439. goto err;
  440. }
  441. /*
  442. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  443. * a missing NULL parameter.
  444. */
  445. s = OPENSSL_malloc((int)len + 1);
  446. if (s == NULL) {
  447. i = ERR_R_MALLOC_FAILURE;
  448. goto err;
  449. }
  450. ret->type = V_ASN1_INTEGER;
  451. if (len) {
  452. if ((*p == 0) && (len != 1)) {
  453. p++;
  454. len--;
  455. }
  456. memcpy(s, p, (int)len);
  457. p += len;
  458. }
  459. OPENSSL_free(ret->data);
  460. ret->data = s;
  461. ret->length = (int)len;
  462. if (a != NULL)
  463. (*a) = ret;
  464. *pp = p;
  465. return (ret);
  466. err:
  467. ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
  468. if ((a == NULL) || (*a != ret))
  469. ASN1_INTEGER_free(ret);
  470. return (NULL);
  471. }
  472. static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
  473. int atype)
  474. {
  475. ASN1_INTEGER *ret;
  476. int len;
  477. if (ai == NULL) {
  478. ret = ASN1_STRING_type_new(atype);
  479. } else {
  480. ret = ai;
  481. ret->type = atype;
  482. }
  483. if (ret == NULL) {
  484. ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_NESTED_ASN1_ERROR);
  485. goto err;
  486. }
  487. if (BN_is_negative(bn) && !BN_is_zero(bn))
  488. ret->type |= V_ASN1_NEG_INTEGER;
  489. len = BN_num_bytes(bn);
  490. if (len == 0)
  491. len = 1;
  492. if (ASN1_STRING_set(ret, NULL, len) == 0) {
  493. ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_MALLOC_FAILURE);
  494. goto err;
  495. }
  496. /* Correct zero case */
  497. if (BN_is_zero(bn))
  498. ret->data[0] = 0;
  499. else
  500. len = BN_bn2bin(bn, ret->data);
  501. ret->length = len;
  502. return ret;
  503. err:
  504. if (ret != ai)
  505. ASN1_INTEGER_free(ret);
  506. return (NULL);
  507. }
  508. static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
  509. int itype)
  510. {
  511. BIGNUM *ret;
  512. if ((ai->type & ~V_ASN1_NEG) != itype) {
  513. ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
  514. return NULL;
  515. }
  516. ret = BN_bin2bn(ai->data, ai->length, bn);
  517. if (ret == 0) {
  518. ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
  519. return NULL;
  520. }
  521. if (ai->type & V_ASN1_NEG)
  522. BN_set_negative(ret, 1);
  523. return ret;
  524. }
  525. int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
  526. {
  527. return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
  528. }
  529. int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
  530. {
  531. return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
  532. }
  533. int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
  534. {
  535. return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
  536. }
  537. int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
  538. {
  539. return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
  540. }
  541. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
  542. {
  543. return ASN1_INTEGER_set_int64(a, v);
  544. }
  545. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  546. {
  547. int i;
  548. int64_t r;
  549. if (a == NULL)
  550. return 0;
  551. i = ASN1_INTEGER_get_int64(&r, a);
  552. if (i == 0)
  553. return -1;
  554. if (r > LONG_MAX || r < LONG_MIN)
  555. return -1;
  556. return (long)r;
  557. }
  558. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  559. {
  560. return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
  561. }
  562. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  563. {
  564. return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
  565. }
  566. int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
  567. {
  568. return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
  569. }
  570. int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
  571. {
  572. return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
  573. }
  574. int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
  575. {
  576. return ASN1_ENUMERATED_set_int64(a, v);
  577. }
  578. long ASN1_ENUMERATED_get(ASN1_ENUMERATED *a)
  579. {
  580. int i;
  581. int64_t r;
  582. if (a == NULL)
  583. return 0;
  584. if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
  585. return -1;
  586. if (a->length > (int)sizeof(long))
  587. return 0xffffffffL;
  588. i = ASN1_ENUMERATED_get_int64(&r, a);
  589. if (i == 0)
  590. return -1;
  591. if (r > LONG_MAX || r < LONG_MIN)
  592. return -1;
  593. return (long)r;
  594. }
  595. ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
  596. {
  597. return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
  598. }
  599. BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
  600. {
  601. return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
  602. }