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

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