a_int.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #include <openssl/asn1.h>
  57. #include <string.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "../internal.h"
  61. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
  62. {
  63. return M_ASN1_INTEGER_dup(x);
  64. }
  65. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
  66. {
  67. int neg, ret;
  68. /* Compare signs */
  69. neg = x->type & V_ASN1_NEG;
  70. if (neg != (y->type & V_ASN1_NEG)) {
  71. if (neg)
  72. return -1;
  73. else
  74. return 1;
  75. }
  76. ret = ASN1_STRING_cmp(x, y);
  77. if (neg)
  78. return -ret;
  79. else
  80. return ret;
  81. }
  82. /*
  83. * This converts an ASN1 INTEGER into its content encoding.
  84. * The internal representation is an ASN1_STRING whose data is a big endian
  85. * representation of the value, ignoring the sign. The sign is determined by
  86. * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
  87. *
  88. * Positive integers are no problem: they are almost the same as the DER
  89. * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
  90. *
  91. * Negative integers are a bit trickier...
  92. * The DER representation of negative integers is in 2s complement form.
  93. * The internal form is converted by complementing each octet and finally
  94. * adding one to the result. This can be done less messily with a little trick.
  95. * If the internal form has trailing zeroes then they will become FF by the
  96. * complement and 0 by the add one (due to carry) so just copy as many trailing
  97. * zeros to the destination as there are in the source. The carry will add one
  98. * to the last none zero octet: so complement this octet and add one and finally
  99. * complement any left over until you get to the start of the string.
  100. *
  101. * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
  102. * with 0xff. However if the first byte is 0x80 and one of the following bytes
  103. * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
  104. * followed by optional zeros isn't padded.
  105. */
  106. int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
  107. {
  108. int pad = 0, ret, i, neg;
  109. unsigned char *p, *n, pb = 0;
  110. if (a == NULL)
  111. return (0);
  112. neg = a->type & V_ASN1_NEG;
  113. if (a->length == 0)
  114. ret = 1;
  115. else {
  116. ret = a->length;
  117. i = a->data[0];
  118. if (ret == 1 && i == 0)
  119. neg = 0;
  120. if (!neg && (i > 127)) {
  121. pad = 1;
  122. pb = 0;
  123. } else if (neg) {
  124. if (i > 128) {
  125. pad = 1;
  126. pb = 0xFF;
  127. } else if (i == 128) {
  128. /*
  129. * Special case: if any other bytes non zero we pad:
  130. * otherwise we don't.
  131. */
  132. for (i = 1; i < a->length; i++)
  133. if (a->data[i]) {
  134. pad = 1;
  135. pb = 0xFF;
  136. break;
  137. }
  138. }
  139. }
  140. ret += pad;
  141. }
  142. if (pp == NULL)
  143. return (ret);
  144. p = *pp;
  145. if (pad)
  146. *(p++) = pb;
  147. if (a->length == 0)
  148. *(p++) = 0;
  149. else if (!neg)
  150. OPENSSL_memcpy(p, a->data, (unsigned int)a->length);
  151. else {
  152. /* Begin at the end of the encoding */
  153. n = a->data + a->length - 1;
  154. p += a->length - 1;
  155. i = a->length;
  156. /* Copy zeros to destination as long as source is zero */
  157. while (!*n && i > 1) {
  158. *(p--) = 0;
  159. n--;
  160. i--;
  161. }
  162. /* Complement and increment next octet */
  163. *(p--) = ((*(n--)) ^ 0xff) + 1;
  164. i--;
  165. /* Complement any octets left */
  166. for (; i > 0; i--)
  167. *(p--) = *(n--) ^ 0xff;
  168. }
  169. *pp += ret;
  170. return (ret);
  171. }
  172. /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
  173. ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  174. long len)
  175. {
  176. ASN1_INTEGER *ret = NULL;
  177. const unsigned char *p, *pend;
  178. unsigned char *to, *s;
  179. int i;
  180. if ((a == NULL) || ((*a) == NULL)) {
  181. if ((ret = M_ASN1_INTEGER_new()) == NULL)
  182. return (NULL);
  183. ret->type = V_ASN1_INTEGER;
  184. } else
  185. ret = (*a);
  186. p = *pp;
  187. pend = p + len;
  188. /*
  189. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  190. * a missing NULL parameter.
  191. */
  192. s = (unsigned char *)OPENSSL_malloc((int)len + 1);
  193. if (s == NULL) {
  194. i = ERR_R_MALLOC_FAILURE;
  195. goto err;
  196. }
  197. to = s;
  198. if (!len) {
  199. /*
  200. * Strictly speaking this is an illegal INTEGER but we tolerate it.
  201. */
  202. ret->type = V_ASN1_INTEGER;
  203. } else if (*p & 0x80) { /* a negative number */
  204. ret->type = V_ASN1_NEG_INTEGER;
  205. if ((*p == 0xff) && (len != 1)) {
  206. p++;
  207. len--;
  208. }
  209. i = len;
  210. p += i - 1;
  211. to += i - 1;
  212. while ((!*p) && i) {
  213. *(to--) = 0;
  214. i--;
  215. p--;
  216. }
  217. /*
  218. * Special case: if all zeros then the number will be of the form FF
  219. * followed by n zero bytes: this corresponds to 1 followed by n zero
  220. * bytes. We've already written n zeros so we just append an extra
  221. * one and set the first byte to a 1. This is treated separately
  222. * because it is the only case where the number of bytes is larger
  223. * than len.
  224. */
  225. if (!i) {
  226. *s = 1;
  227. s[len] = 0;
  228. len++;
  229. } else {
  230. *(to--) = (*(p--) ^ 0xff) + 1;
  231. i--;
  232. for (; i > 0; i--)
  233. *(to--) = *(p--) ^ 0xff;
  234. }
  235. } else {
  236. ret->type = V_ASN1_INTEGER;
  237. if ((*p == 0) && (len != 1)) {
  238. p++;
  239. len--;
  240. }
  241. OPENSSL_memcpy(s, p, (int)len);
  242. }
  243. if (ret->data != NULL)
  244. OPENSSL_free(ret->data);
  245. ret->data = s;
  246. ret->length = (int)len;
  247. if (a != NULL)
  248. (*a) = ret;
  249. *pp = pend;
  250. return (ret);
  251. err:
  252. OPENSSL_PUT_ERROR(ASN1, i);
  253. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  254. M_ASN1_INTEGER_free(ret);
  255. return (NULL);
  256. }
  257. /*
  258. * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
  259. * integers: some broken software can encode a positive INTEGER with its MSB
  260. * set as negative (it doesn't add a padding zero).
  261. */
  262. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  263. long length)
  264. {
  265. ASN1_INTEGER *ret = NULL;
  266. const unsigned char *p;
  267. unsigned char *s;
  268. long len;
  269. int inf, tag, xclass;
  270. int i;
  271. if ((a == NULL) || ((*a) == NULL)) {
  272. if ((ret = M_ASN1_INTEGER_new()) == NULL)
  273. return (NULL);
  274. ret->type = V_ASN1_INTEGER;
  275. } else
  276. ret = (*a);
  277. p = *pp;
  278. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  279. if (inf & 0x80) {
  280. i = ASN1_R_BAD_OBJECT_HEADER;
  281. goto err;
  282. }
  283. if (tag != V_ASN1_INTEGER) {
  284. i = ASN1_R_EXPECTING_AN_INTEGER;
  285. goto err;
  286. }
  287. /*
  288. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  289. * a missing NULL parameter.
  290. */
  291. s = (unsigned char *)OPENSSL_malloc((int)len + 1);
  292. if (s == NULL) {
  293. i = ERR_R_MALLOC_FAILURE;
  294. goto err;
  295. }
  296. ret->type = V_ASN1_INTEGER;
  297. if (len) {
  298. if ((*p == 0) && (len != 1)) {
  299. p++;
  300. len--;
  301. }
  302. OPENSSL_memcpy(s, p, (int)len);
  303. p += len;
  304. }
  305. if (ret->data != NULL)
  306. OPENSSL_free(ret->data);
  307. ret->data = s;
  308. ret->length = (int)len;
  309. if (a != NULL)
  310. (*a) = ret;
  311. *pp = p;
  312. return (ret);
  313. err:
  314. OPENSSL_PUT_ERROR(ASN1, i);
  315. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  316. M_ASN1_INTEGER_free(ret);
  317. return (NULL);
  318. }
  319. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
  320. {
  321. int j, k;
  322. unsigned int i;
  323. unsigned char buf[sizeof(long) + 1];
  324. long d;
  325. a->type = V_ASN1_INTEGER;
  326. if (a->length < (int)(sizeof(long) + 1)) {
  327. if (a->data != NULL)
  328. OPENSSL_free(a->data);
  329. if ((a->data =
  330. (unsigned char *)OPENSSL_malloc(sizeof(long) + 1)) != NULL)
  331. OPENSSL_memset((char *)a->data, 0, sizeof(long) + 1);
  332. }
  333. if (a->data == NULL) {
  334. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  335. return (0);
  336. }
  337. d = v;
  338. if (d < 0) {
  339. d = -d;
  340. a->type = V_ASN1_NEG_INTEGER;
  341. }
  342. for (i = 0; i < sizeof(long); i++) {
  343. if (d == 0)
  344. break;
  345. buf[i] = (int)d & 0xff;
  346. d >>= 8;
  347. }
  348. j = 0;
  349. for (k = i - 1; k >= 0; k--)
  350. a->data[j++] = buf[k];
  351. a->length = j;
  352. return (1);
  353. }
  354. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  355. {
  356. int neg = 0, i;
  357. long r = 0;
  358. if (a == NULL)
  359. return (0L);
  360. i = a->type;
  361. if (i == V_ASN1_NEG_INTEGER)
  362. neg = 1;
  363. else if (i != V_ASN1_INTEGER)
  364. return -1;
  365. if (a->length > (int)sizeof(long)) {
  366. /* hmm... a bit ugly, return all ones */
  367. return -1;
  368. }
  369. if (a->data == NULL)
  370. return 0;
  371. for (i = 0; i < a->length; i++) {
  372. r <<= 8;
  373. r |= (unsigned char)a->data[i];
  374. }
  375. if (neg)
  376. r = -r;
  377. return (r);
  378. }
  379. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  380. {
  381. ASN1_INTEGER *ret;
  382. int len, j;
  383. if (ai == NULL)
  384. ret = M_ASN1_INTEGER_new();
  385. else
  386. ret = ai;
  387. if (ret == NULL) {
  388. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  389. goto err;
  390. }
  391. if (BN_is_negative(bn) && !BN_is_zero(bn))
  392. ret->type = V_ASN1_NEG_INTEGER;
  393. else
  394. ret->type = V_ASN1_INTEGER;
  395. j = BN_num_bits(bn);
  396. len = ((j == 0) ? 0 : ((j / 8) + 1));
  397. if (ret->length < len + 4) {
  398. unsigned char *new_data = OPENSSL_realloc(ret->data, len + 4);
  399. if (!new_data) {
  400. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  401. goto err;
  402. }
  403. ret->data = new_data;
  404. }
  405. ret->length = BN_bn2bin(bn, ret->data);
  406. /* Correct zero case */
  407. if (!ret->length) {
  408. ret->data[0] = 0;
  409. ret->length = 1;
  410. }
  411. return (ret);
  412. err:
  413. if (ret != ai)
  414. M_ASN1_INTEGER_free(ret);
  415. return (NULL);
  416. }
  417. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  418. {
  419. BIGNUM *ret;
  420. if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
  421. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
  422. else if (ai->type == V_ASN1_NEG_INTEGER)
  423. BN_set_negative(ret, 1);
  424. return (ret);
  425. }