Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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