a_int.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* crypto/asn1/a_int.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/asn1.h>
  61. #include <openssl/bn.h>
  62. #include "asn1_locl.h"
  63. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
  64. {
  65. return ASN1_STRING_dup(x);
  66. }
  67. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
  68. {
  69. int neg, ret;
  70. /* Compare signs */
  71. neg = x->type & V_ASN1_NEG;
  72. if (neg != (y->type & V_ASN1_NEG)) {
  73. if (neg)
  74. return -1;
  75. else
  76. return 1;
  77. }
  78. ret = ASN1_STRING_cmp(x, y);
  79. if (neg)
  80. return -ret;
  81. else
  82. return ret;
  83. }
  84. /*-
  85. * This converts an ASN1 INTEGER into its content encoding.
  86. * The internal representation is an ASN1_STRING whose data is a big endian
  87. * representation of the value, ignoring the sign. The sign is determined by
  88. * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
  89. *
  90. * Positive integers are no problem: they are almost the same as the DER
  91. * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
  92. *
  93. * Negative integers are a bit trickier...
  94. * The DER representation of negative integers is in 2s complement form.
  95. * The internal form is converted by complementing each octet and finally
  96. * adding one to the result. This can be done less messily with a little trick.
  97. * If the internal form has trailing zeroes then they will become FF by the
  98. * complement and 0 by the add one (due to carry) so just copy as many trailing
  99. * zeros to the destination as there are in the source. The carry will add one
  100. * to the last none zero octet: so complement this octet and add one and finally
  101. * complement any left over until you get to the start of the string.
  102. *
  103. * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
  104. * with 0xff. However if the first byte is 0x80 and one of the following bytes
  105. * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
  106. * followed by optional zeros isn't padded.
  107. */
  108. int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
  109. {
  110. int pad = 0, ret, i, neg;
  111. unsigned char *p, *n, pb = 0;
  112. if (a == NULL)
  113. return (0);
  114. neg = a->type & V_ASN1_NEG;
  115. if (a->length == 0)
  116. ret = 1;
  117. else {
  118. ret = a->length;
  119. i = a->data[0];
  120. if (ret == 1 && i == 0)
  121. neg = 0;
  122. if (!neg && (i > 127)) {
  123. pad = 1;
  124. pb = 0;
  125. } else if (neg) {
  126. if (i > 128) {
  127. pad = 1;
  128. pb = 0xFF;
  129. } else if (i == 128) {
  130. /*
  131. * Special case: if any other bytes non zero we pad:
  132. * otherwise we don't.
  133. */
  134. for (i = 1; i < a->length; i++)
  135. if (a->data[i]) {
  136. pad = 1;
  137. pb = 0xFF;
  138. break;
  139. }
  140. }
  141. }
  142. ret += pad;
  143. }
  144. if (pp == NULL)
  145. return (ret);
  146. p = *pp;
  147. if (pad)
  148. *(p++) = pb;
  149. if (a->length == 0)
  150. *(p++) = 0;
  151. else if (!neg)
  152. memcpy(p, a->data, (unsigned int)a->length);
  153. else {
  154. /* Begin at the end of the encoding */
  155. n = a->data + a->length - 1;
  156. p += a->length - 1;
  157. i = a->length;
  158. /* Copy zeros to destination as long as source is zero */
  159. while (!*n && i > 1) {
  160. *(p--) = 0;
  161. n--;
  162. i--;
  163. }
  164. /* Complement and increment next octet */
  165. *(p--) = ((*(n--)) ^ 0xff) + 1;
  166. i--;
  167. /* Complement any octets left */
  168. for (; i > 0; i--)
  169. *(p--) = *(n--) ^ 0xff;
  170. }
  171. *pp += ret;
  172. return (ret);
  173. }
  174. /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
  175. ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  176. long len)
  177. {
  178. ASN1_INTEGER *ret = NULL;
  179. const unsigned char *p, *pend;
  180. unsigned char *to, *s;
  181. int i;
  182. if ((a == NULL) || ((*a) == NULL)) {
  183. if ((ret = ASN1_INTEGER_new()) == NULL)
  184. return (NULL);
  185. ret->type = V_ASN1_INTEGER;
  186. } else
  187. ret = (*a);
  188. p = *pp;
  189. pend = p + len;
  190. /*
  191. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  192. * a missing NULL parameter.
  193. */
  194. s = OPENSSL_malloc((int)len + 1);
  195. if (s == NULL) {
  196. i = ERR_R_MALLOC_FAILURE;
  197. goto err;
  198. }
  199. to = s;
  200. if (!len) {
  201. /*
  202. * Strictly speaking this is an illegal INTEGER but we tolerate it.
  203. */
  204. ret->type = V_ASN1_INTEGER;
  205. } else if (*p & 0x80) { /* a negative number */
  206. ret->type = V_ASN1_NEG_INTEGER;
  207. if ((*p == 0xff) && (len != 1)) {
  208. p++;
  209. len--;
  210. }
  211. i = len;
  212. p += i - 1;
  213. to += i - 1;
  214. while ((!*p) && i) {
  215. *(to--) = 0;
  216. i--;
  217. p--;
  218. }
  219. /*
  220. * Special case: if all zeros then the number will be of the form FF
  221. * followed by n zero bytes: this corresponds to 1 followed by n zero
  222. * bytes. We've already written n zeros so we just append an extra
  223. * one and set the first byte to a 1. This is treated separately
  224. * because it is the only case where the number of bytes is larger
  225. * than len.
  226. */
  227. if (!i) {
  228. *s = 1;
  229. s[len] = 0;
  230. len++;
  231. } else {
  232. *(to--) = (*(p--) ^ 0xff) + 1;
  233. i--;
  234. for (; i > 0; i--)
  235. *(to--) = *(p--) ^ 0xff;
  236. }
  237. } else {
  238. ret->type = V_ASN1_INTEGER;
  239. if ((*p == 0) && (len != 1)) {
  240. p++;
  241. len--;
  242. }
  243. memcpy(s, p, (int)len);
  244. }
  245. OPENSSL_free(ret->data);
  246. ret->data = s;
  247. ret->length = (int)len;
  248. if (a != NULL)
  249. (*a) = ret;
  250. *pp = pend;
  251. return (ret);
  252. err:
  253. ASN1err(ASN1_F_C2I_ASN1_INTEGER, i);
  254. if ((a == NULL) || (*a != ret))
  255. ASN1_INTEGER_free(ret);
  256. return (NULL);
  257. }
  258. /*
  259. * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
  260. * integers: some broken software can encode a positive INTEGER with its MSB
  261. * set as negative (it doesn't add a padding zero).
  262. */
  263. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  264. long length)
  265. {
  266. ASN1_INTEGER *ret = NULL;
  267. const unsigned char *p;
  268. unsigned char *s;
  269. long len;
  270. int inf, tag, xclass;
  271. int i;
  272. if ((a == NULL) || ((*a) == NULL)) {
  273. if ((ret = ASN1_INTEGER_new()) == NULL)
  274. return (NULL);
  275. ret->type = V_ASN1_INTEGER;
  276. } else
  277. ret = (*a);
  278. p = *pp;
  279. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  280. if (inf & 0x80) {
  281. i = ASN1_R_BAD_OBJECT_HEADER;
  282. goto err;
  283. }
  284. if (tag != V_ASN1_INTEGER) {
  285. i = ASN1_R_EXPECTING_AN_INTEGER;
  286. goto err;
  287. }
  288. /*
  289. * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
  290. * a missing NULL parameter.
  291. */
  292. s = OPENSSL_malloc((int)len + 1);
  293. if (s == NULL) {
  294. i = ERR_R_MALLOC_FAILURE;
  295. goto err;
  296. }
  297. ret->type = V_ASN1_INTEGER;
  298. if (len) {
  299. if ((*p == 0) && (len != 1)) {
  300. p++;
  301. len--;
  302. }
  303. memcpy(s, p, (int)len);
  304. p += len;
  305. }
  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. ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
  315. if ((a == NULL) || (*a != ret))
  316. 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. if (a->length < (int)(sizeof(long) + 1)) {
  325. OPENSSL_free(a->data);
  326. if ((a->data = OPENSSL_malloc(sizeof(long) + 1)) != NULL)
  327. memset(a->data, 0, sizeof(long) + 1);
  328. }
  329. if (a->data == NULL) {
  330. ASN1err(ASN1_F_ASN1_INTEGER_SET, ERR_R_MALLOC_FAILURE);
  331. return (0);
  332. }
  333. if (v < 0) {
  334. v = -v;
  335. a->type = V_ASN1_NEG_INTEGER;
  336. } else
  337. a->type = V_ASN1_INTEGER;
  338. for (i = 0; i < sizeof(long); i++) {
  339. if (v == 0)
  340. break;
  341. buf[i] = (int)v & 0xff;
  342. v >>= 8;
  343. }
  344. j = 0;
  345. for (k = i - 1; k >= 0; k--)
  346. a->data[j++] = buf[k];
  347. a->length = j;
  348. return (1);
  349. }
  350. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  351. {
  352. int neg = 0, i;
  353. long r = 0;
  354. if (a == NULL)
  355. return (0L);
  356. i = a->type;
  357. if (i == V_ASN1_NEG_INTEGER)
  358. neg = 1;
  359. else if (i != V_ASN1_INTEGER)
  360. return -1;
  361. if (a->length > (int)sizeof(long)) {
  362. /* hmm... a bit ugly, return all ones */
  363. return -1;
  364. }
  365. if (a->data == NULL)
  366. return 0;
  367. for (i = 0; i < a->length; i++) {
  368. r <<= 8;
  369. r |= (unsigned char)a->data[i];
  370. }
  371. if (neg)
  372. r = -r;
  373. return (r);
  374. }
  375. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  376. {
  377. ASN1_INTEGER *ret;
  378. int len, j;
  379. if (ai == NULL)
  380. ret = ASN1_INTEGER_new();
  381. else
  382. ret = ai;
  383. if (ret == NULL) {
  384. ASN1err(ASN1_F_BN_TO_ASN1_INTEGER, ERR_R_NESTED_ASN1_ERROR);
  385. goto err;
  386. }
  387. if (BN_is_negative(bn) && !BN_is_zero(bn))
  388. ret->type = V_ASN1_NEG_INTEGER;
  389. else
  390. ret->type = V_ASN1_INTEGER;
  391. j = BN_num_bits(bn);
  392. len = ((j == 0) ? 0 : ((j / 8) + 1));
  393. if (ret->length < len + 4) {
  394. unsigned char *new_data = OPENSSL_realloc(ret->data, len + 4);
  395. if (!new_data) {
  396. ASN1err(ASN1_F_BN_TO_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  397. goto err;
  398. }
  399. ret->data = new_data;
  400. }
  401. ret->length = BN_bn2bin(bn, ret->data);
  402. /* Correct zero case */
  403. if (!ret->length) {
  404. ret->data[0] = 0;
  405. ret->length = 1;
  406. }
  407. return (ret);
  408. err:
  409. if (ret != ai)
  410. ASN1_INTEGER_free(ret);
  411. return (NULL);
  412. }
  413. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  414. {
  415. BIGNUM *ret;
  416. if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
  417. ASN1err(ASN1_F_ASN1_INTEGER_TO_BN, ASN1_R_BN_LIB);
  418. else if (ai->type == V_ASN1_NEG_INTEGER)
  419. BN_set_negative(ret, 1);
  420. return (ret);
  421. }