Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

480 строки
14 KiB

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