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

506 строки
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 <limits.h>
  58. #include <string.h>
  59. #include <openssl/asn1_mac.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. /* Cross-module errors from crypto/x509/i2d_pr.c. */
  63. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE);
  64. /* Cross-module errors from crypto/x509/algorithm.c. */
  65. OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED);
  66. OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  67. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
  68. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM);
  69. OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE);
  70. /*
  71. * Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
  72. * these once asn1_gen.c is gone.
  73. */
  74. OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED);
  75. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT);
  76. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN);
  77. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT);
  78. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX);
  79. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG);
  80. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER);
  81. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING);
  82. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE);
  83. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT);
  84. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE);
  85. OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT);
  86. OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER);
  87. OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER);
  88. OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR);
  89. OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE);
  90. OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT);
  91. OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT);
  92. OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG);
  93. OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT);
  94. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT);
  95. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG);
  96. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE);
  97. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  98. long max);
  99. static void asn1_put_length(unsigned char **pp, int length);
  100. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  101. {
  102. /*
  103. * If there is 0 or 1 byte left, the length check should pick things up
  104. */
  105. if (len <= 0)
  106. return (1);
  107. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
  108. (*p) += 2;
  109. return (1);
  110. }
  111. return (0);
  112. }
  113. int ASN1_check_infinite_end(unsigned char **p, long len)
  114. {
  115. return _asn1_check_infinite_end((const unsigned char **)p, len);
  116. }
  117. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  118. {
  119. return _asn1_check_infinite_end(p, len);
  120. }
  121. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  122. int *pclass, long omax)
  123. {
  124. int i, ret;
  125. long l;
  126. const unsigned char *p = *pp;
  127. int tag, xclass, inf;
  128. long max = omax;
  129. if (!max)
  130. goto err;
  131. ret = (*p & V_ASN1_CONSTRUCTED);
  132. xclass = (*p & V_ASN1_PRIVATE);
  133. i = *p & V_ASN1_PRIMITIVE_TAG;
  134. if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
  135. p++;
  136. if (--max == 0)
  137. goto err;
  138. l = 0;
  139. while (*p & 0x80) {
  140. l <<= 7L;
  141. l |= *(p++) & 0x7f;
  142. if (--max == 0)
  143. goto err;
  144. if (l > (INT_MAX >> 7L))
  145. goto err;
  146. }
  147. l <<= 7L;
  148. l |= *(p++) & 0x7f;
  149. tag = (int)l;
  150. if (--max == 0)
  151. goto err;
  152. } else {
  153. tag = i;
  154. p++;
  155. if (--max == 0)
  156. goto err;
  157. }
  158. /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */
  159. if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL)
  160. goto err;
  161. *ptag = tag;
  162. *pclass = xclass;
  163. if (!asn1_get_length(&p, &inf, plength, max))
  164. goto err;
  165. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  166. goto err;
  167. #if 0
  168. fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
  169. (int)p, *plength, omax, (int)*pp, (int)(p + *plength),
  170. (int)(omax + *pp));
  171. #endif
  172. if (*plength > (omax - (p - *pp))) {
  173. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  174. /*
  175. * Set this so that even if things are not long enough the values are
  176. * set correctly
  177. */
  178. ret |= 0x80;
  179. }
  180. *pp = p;
  181. return (ret | inf);
  182. err:
  183. OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
  184. return (0x80);
  185. }
  186. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  187. long max)
  188. {
  189. const unsigned char *p = *pp;
  190. unsigned long ret = 0;
  191. unsigned long i;
  192. if (max-- < 1)
  193. return 0;
  194. if (*p == 0x80) {
  195. *inf = 1;
  196. ret = 0;
  197. p++;
  198. } else {
  199. *inf = 0;
  200. i = *p & 0x7f;
  201. if (*(p++) & 0x80) {
  202. if (i > sizeof(ret) || max < (long)i)
  203. return 0;
  204. while (i-- > 0) {
  205. ret <<= 8L;
  206. ret |= *(p++);
  207. }
  208. } else
  209. ret = i;
  210. }
  211. if (ret > LONG_MAX)
  212. return 0;
  213. *pp = p;
  214. *rl = (long)ret;
  215. return 1;
  216. }
  217. /*
  218. * class 0 is constructed constructed == 2 for indefinite length constructed
  219. */
  220. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  221. int xclass)
  222. {
  223. unsigned char *p = *pp;
  224. int i, ttag;
  225. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  226. i |= (xclass & V_ASN1_PRIVATE);
  227. if (tag < 31)
  228. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  229. else {
  230. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  231. for (i = 0, ttag = tag; ttag > 0; i++)
  232. ttag >>= 7;
  233. ttag = i;
  234. while (i-- > 0) {
  235. p[i] = tag & 0x7f;
  236. if (i != (ttag - 1))
  237. p[i] |= 0x80;
  238. tag >>= 7;
  239. }
  240. p += ttag;
  241. }
  242. if (constructed == 2)
  243. *(p++) = 0x80;
  244. else
  245. asn1_put_length(&p, length);
  246. *pp = p;
  247. }
  248. int ASN1_put_eoc(unsigned char **pp)
  249. {
  250. unsigned char *p = *pp;
  251. *p++ = 0;
  252. *p++ = 0;
  253. *pp = p;
  254. return 2;
  255. }
  256. static void asn1_put_length(unsigned char **pp, int length)
  257. {
  258. unsigned char *p = *pp;
  259. int i, l;
  260. if (length <= 127)
  261. *(p++) = (unsigned char)length;
  262. else {
  263. l = length;
  264. for (i = 0; l > 0; i++)
  265. l >>= 8;
  266. *(p++) = i | 0x80;
  267. l = i;
  268. while (i-- > 0) {
  269. p[i] = length & 0xff;
  270. length >>= 8;
  271. }
  272. p += l;
  273. }
  274. *pp = p;
  275. }
  276. int ASN1_object_size(int constructed, int length, int tag)
  277. {
  278. int ret;
  279. ret = length;
  280. ret++;
  281. if (tag >= 31) {
  282. while (tag > 0) {
  283. tag >>= 7;
  284. ret++;
  285. }
  286. }
  287. if (constructed == 2)
  288. return ret + 3;
  289. ret++;
  290. if (length > 127) {
  291. while (length > 0) {
  292. length >>= 8;
  293. ret++;
  294. }
  295. }
  296. return (ret);
  297. }
  298. static int _asn1_Finish(ASN1_const_CTX *c)
  299. {
  300. if ((c->inf == (1 | V_ASN1_CONSTRUCTED)) && (!c->eos)) {
  301. if (!ASN1_const_check_infinite_end(&c->p, c->slen)) {
  302. c->error = ASN1_R_MISSING_ASN1_EOS;
  303. return (0);
  304. }
  305. }
  306. if (((c->slen != 0) && !(c->inf & 1)) || ((c->slen < 0) && (c->inf & 1))) {
  307. c->error = ASN1_R_ASN1_LENGTH_MISMATCH;
  308. return (0);
  309. }
  310. return (1);
  311. }
  312. int asn1_Finish(ASN1_CTX *c)
  313. {
  314. return _asn1_Finish((ASN1_const_CTX *)c);
  315. }
  316. int asn1_const_Finish(ASN1_const_CTX *c)
  317. {
  318. return _asn1_Finish(c);
  319. }
  320. int asn1_GetSequence(ASN1_const_CTX *c, long *length)
  321. {
  322. const unsigned char *q;
  323. q = c->p;
  324. c->inf = ASN1_get_object(&(c->p), &(c->slen), &(c->tag), &(c->xclass),
  325. *length);
  326. if (c->inf & 0x80) {
  327. c->error = ASN1_R_BAD_GET_ASN1_OBJECT_CALL;
  328. return (0);
  329. }
  330. if (c->tag != V_ASN1_SEQUENCE) {
  331. c->error = ASN1_R_EXPECTING_AN_ASN1_SEQUENCE;
  332. return (0);
  333. }
  334. (*length) -= (c->p - q);
  335. if (c->max && (*length < 0)) {
  336. c->error = ASN1_R_ASN1_LENGTH_MISMATCH;
  337. return (0);
  338. }
  339. if (c->inf == (1 | V_ASN1_CONSTRUCTED))
  340. c->slen = *length + *(c->pp) - c->p;
  341. c->eos = 0;
  342. return (1);
  343. }
  344. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  345. {
  346. if (str == NULL)
  347. return 0;
  348. dst->type = str->type;
  349. if (!ASN1_STRING_set(dst, str->data, str->length))
  350. return 0;
  351. dst->flags = str->flags;
  352. return 1;
  353. }
  354. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  355. {
  356. ASN1_STRING *ret;
  357. if (!str)
  358. return NULL;
  359. ret = ASN1_STRING_new();
  360. if (!ret)
  361. return NULL;
  362. if (!ASN1_STRING_copy(ret, str)) {
  363. ASN1_STRING_free(ret);
  364. return NULL;
  365. }
  366. return ret;
  367. }
  368. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  369. {
  370. unsigned char *c;
  371. const char *data = _data;
  372. if (len < 0) {
  373. if (data == NULL)
  374. return (0);
  375. else
  376. len = strlen(data);
  377. }
  378. if ((str->length < len) || (str->data == NULL)) {
  379. c = str->data;
  380. if (c == NULL)
  381. str->data = OPENSSL_malloc(len + 1);
  382. else
  383. str->data = OPENSSL_realloc(c, len + 1);
  384. if (str->data == NULL) {
  385. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  386. str->data = c;
  387. return (0);
  388. }
  389. }
  390. str->length = len;
  391. if (data != NULL) {
  392. memcpy(str->data, data, len);
  393. /* an allowance for strings :-) */
  394. str->data[len] = '\0';
  395. }
  396. return (1);
  397. }
  398. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  399. {
  400. if (str->data)
  401. OPENSSL_free(str->data);
  402. str->data = data;
  403. str->length = len;
  404. }
  405. ASN1_STRING *ASN1_STRING_new(void)
  406. {
  407. return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  408. }
  409. ASN1_STRING *ASN1_STRING_type_new(int type)
  410. {
  411. ASN1_STRING *ret;
  412. ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
  413. if (ret == NULL) {
  414. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  415. return (NULL);
  416. }
  417. ret->length = 0;
  418. ret->type = type;
  419. ret->data = NULL;
  420. ret->flags = 0;
  421. return (ret);
  422. }
  423. void ASN1_STRING_free(ASN1_STRING *a)
  424. {
  425. if (a == NULL)
  426. return;
  427. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  428. OPENSSL_free(a->data);
  429. OPENSSL_free(a);
  430. }
  431. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  432. {
  433. int i;
  434. i = (a->length - b->length);
  435. if (i == 0) {
  436. i = memcmp(a->data, b->data, a->length);
  437. if (i == 0)
  438. return (a->type - b->type);
  439. else
  440. return (i);
  441. } else
  442. return (i);
  443. }
  444. int ASN1_STRING_length(const ASN1_STRING *x)
  445. {
  446. return M_ASN1_STRING_length(x);
  447. }
  448. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  449. {
  450. M_ASN1_STRING_length_set(x, len);
  451. return;
  452. }
  453. int ASN1_STRING_type(ASN1_STRING *x)
  454. {
  455. return M_ASN1_STRING_type(x);
  456. }
  457. unsigned char *ASN1_STRING_data(ASN1_STRING *x)
  458. {
  459. return M_ASN1_STRING_data(x);
  460. }