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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 <time.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/err.h>
  60. #include <openssl/obj.h>
  61. #include <openssl/mem.h>
  62. #include "asn1_locl.h"
  63. /* Print routines.
  64. */
  65. /* ASN1_PCTX routines */
  66. ASN1_PCTX default_pctx =
  67. {
  68. ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
  69. 0, /* nm_flags */
  70. 0, /* cert_flags */
  71. 0, /* oid_flags */
  72. 0 /* str_flags */
  73. };
  74. ASN1_PCTX *ASN1_PCTX_new(void)
  75. {
  76. ASN1_PCTX *ret;
  77. ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
  78. if (ret == NULL)
  79. {
  80. OPENSSL_PUT_ERROR(ASN1, ASN1_PCTX_new, ERR_R_MALLOC_FAILURE);
  81. return NULL;
  82. }
  83. ret->flags = 0;
  84. ret->nm_flags = 0;
  85. ret->cert_flags = 0;
  86. ret->oid_flags = 0;
  87. ret->str_flags = 0;
  88. return ret;
  89. }
  90. void ASN1_PCTX_free(ASN1_PCTX *p)
  91. {
  92. OPENSSL_free(p);
  93. }
  94. unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
  95. {
  96. return p->flags;
  97. }
  98. void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
  99. {
  100. p->flags = flags;
  101. }
  102. unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
  103. {
  104. return p->nm_flags;
  105. }
  106. void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
  107. {
  108. p->nm_flags = flags;
  109. }
  110. unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
  111. {
  112. return p->cert_flags;
  113. }
  114. void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
  115. {
  116. p->cert_flags = flags;
  117. }
  118. unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
  119. {
  120. return p->oid_flags;
  121. }
  122. void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
  123. {
  124. p->oid_flags = flags;
  125. }
  126. unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
  127. {
  128. return p->str_flags;
  129. }
  130. void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
  131. {
  132. p->str_flags = flags;
  133. }
  134. /* Main print routines */
  135. static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  136. const ASN1_ITEM *it,
  137. const char *fname, const char *sname,
  138. int nohdr, const ASN1_PCTX *pctx);
  139. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  140. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
  141. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  142. const ASN1_ITEM *it, int indent,
  143. const char *fname, const char *sname,
  144. const ASN1_PCTX *pctx);
  145. static int asn1_print_fsname(BIO *out, int indent,
  146. const char *fname, const char *sname,
  147. const ASN1_PCTX *pctx);
  148. int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
  149. const ASN1_ITEM *it, const ASN1_PCTX *pctx)
  150. {
  151. const char *sname;
  152. if (pctx == NULL)
  153. pctx = &default_pctx;
  154. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  155. sname = NULL;
  156. else
  157. sname = it->sname;
  158. return asn1_item_print_ctx(out, &ifld, indent, it,
  159. NULL, sname, 0, pctx);
  160. }
  161. static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  162. const ASN1_ITEM *it,
  163. const char *fname, const char *sname,
  164. int nohdr, const ASN1_PCTX *pctx)
  165. {
  166. const ASN1_TEMPLATE *tt;
  167. const ASN1_EXTERN_FUNCS *ef;
  168. ASN1_VALUE **tmpfld;
  169. const ASN1_AUX *aux = it->funcs;
  170. ASN1_aux_cb *asn1_cb;
  171. ASN1_PRINT_ARG parg;
  172. int i;
  173. if (aux && aux->asn1_cb)
  174. {
  175. parg.out = out;
  176. parg.indent = indent;
  177. parg.pctx = pctx;
  178. asn1_cb = aux->asn1_cb;
  179. }
  180. else asn1_cb = 0;
  181. if(*fld == NULL)
  182. {
  183. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
  184. {
  185. if (!nohdr && !asn1_print_fsname(out, indent,
  186. fname, sname, pctx))
  187. return 0;
  188. if (BIO_puts(out, "<ABSENT>\n") <= 0)
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. switch(it->itype)
  194. {
  195. case ASN1_ITYPE_PRIMITIVE:
  196. if(it->templates)
  197. {
  198. if (!asn1_template_print_ctx(out, fld, indent,
  199. it->templates, pctx))
  200. return 0;
  201. }
  202. /* fall thru */
  203. case ASN1_ITYPE_MSTRING:
  204. if (!asn1_primitive_print(out, fld, it,
  205. indent, fname, sname,pctx))
  206. return 0;
  207. break;
  208. case ASN1_ITYPE_EXTERN:
  209. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  210. return 0;
  211. /* Use new style print routine if possible */
  212. ef = it->funcs;
  213. if (ef && ef->asn1_ex_print)
  214. {
  215. i = ef->asn1_ex_print(out, fld, indent, "", pctx);
  216. if (!i)
  217. return 0;
  218. if ((i == 2) && (BIO_puts(out, "\n") <= 0))
  219. return 0;
  220. return 1;
  221. }
  222. else if (sname &&
  223. BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
  224. return 0;
  225. break;
  226. case ASN1_ITYPE_CHOICE:
  227. #if 0
  228. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  229. return 0;
  230. #endif
  231. /* CHOICE type, get selector */
  232. i = asn1_get_choice_selector(fld, it);
  233. /* This should never happen... */
  234. if((i < 0) || (i >= it->tcount))
  235. {
  236. if (BIO_printf(out,
  237. "ERROR: selector [%d] invalid\n", i) <= 0)
  238. return 0;
  239. return 1;
  240. }
  241. tt = it->templates + i;
  242. tmpfld = asn1_get_field_ptr(fld, tt);
  243. if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
  244. return 0;
  245. break;
  246. case ASN1_ITYPE_SEQUENCE:
  247. case ASN1_ITYPE_NDEF_SEQUENCE:
  248. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  249. return 0;
  250. if (fname || sname)
  251. {
  252. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  253. {
  254. if (BIO_puts(out, " {\n") <= 0)
  255. return 0;
  256. }
  257. else
  258. {
  259. if (BIO_puts(out, "\n") <= 0)
  260. return 0;
  261. }
  262. }
  263. if (asn1_cb)
  264. {
  265. i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
  266. if (i == 0)
  267. return 0;
  268. if (i == 2)
  269. return 1;
  270. }
  271. /* Print each field entry */
  272. for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
  273. {
  274. const ASN1_TEMPLATE *seqtt;
  275. seqtt = asn1_do_adb(fld, tt, 1);
  276. tmpfld = asn1_get_field_ptr(fld, seqtt);
  277. if (!asn1_template_print_ctx(out, tmpfld,
  278. indent + 2, seqtt, pctx))
  279. return 0;
  280. }
  281. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  282. {
  283. if (BIO_printf(out, "%*s}\n", indent, "") < 0)
  284. return 0;
  285. }
  286. if (asn1_cb)
  287. {
  288. i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
  289. if (i == 0)
  290. return 0;
  291. }
  292. break;
  293. default:
  294. BIO_printf(out, "Unprocessed type %d\n", it->itype);
  295. return 0;
  296. }
  297. return 1;
  298. }
  299. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  300. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
  301. {
  302. int flags;
  303. size_t i;
  304. const char *sname, *fname;
  305. flags = tt->flags;
  306. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
  307. sname = ASN1_ITEM_ptr(tt->item)->sname;
  308. else
  309. sname = NULL;
  310. if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  311. fname = NULL;
  312. else
  313. fname = tt->field_name;
  314. if(flags & ASN1_TFLG_SK_MASK)
  315. {
  316. const char *tname;
  317. ASN1_VALUE *skitem;
  318. STACK_OF(ASN1_VALUE) *stack;
  319. /* SET OF, SEQUENCE OF */
  320. if (fname)
  321. {
  322. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
  323. {
  324. if(flags & ASN1_TFLG_SET_OF)
  325. tname = "SET";
  326. else
  327. tname = "SEQUENCE";
  328. if (BIO_printf(out, "%*s%s OF %s {\n",
  329. indent, "", tname, tt->field_name) <= 0)
  330. return 0;
  331. }
  332. else if (BIO_printf(out, "%*s%s:\n", indent, "",
  333. fname) <= 0)
  334. return 0;
  335. }
  336. stack = (STACK_OF(ASN1_VALUE) *)*fld;
  337. for(i = 0; i < sk_ASN1_VALUE_num(stack); i++)
  338. {
  339. if ((i > 0) && (BIO_puts(out, "\n") <= 0))
  340. return 0;
  341. skitem = sk_ASN1_VALUE_value(stack, i);
  342. if (!asn1_item_print_ctx(out, &skitem, indent + 2,
  343. ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
  344. return 0;
  345. }
  346. if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
  347. return 0;
  348. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  349. {
  350. if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
  351. return 0;
  352. }
  353. return 1;
  354. }
  355. return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
  356. fname, sname, 0, pctx);
  357. }
  358. static int asn1_print_fsname(BIO *out, int indent,
  359. const char *fname, const char *sname,
  360. const ASN1_PCTX *pctx)
  361. {
  362. static char spaces[] = " ";
  363. const int nspaces = sizeof(spaces) - 1;
  364. #if 0
  365. if (!sname && !fname)
  366. return 1;
  367. #endif
  368. while (indent > nspaces)
  369. {
  370. if (BIO_write(out, spaces, nspaces) != nspaces)
  371. return 0;
  372. indent -= nspaces;
  373. }
  374. if (BIO_write(out, spaces, indent) != indent)
  375. return 0;
  376. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  377. sname = NULL;
  378. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  379. fname = NULL;
  380. if (!sname && !fname)
  381. return 1;
  382. if (fname)
  383. {
  384. if (BIO_puts(out, fname) <= 0)
  385. return 0;
  386. }
  387. if (sname)
  388. {
  389. if (fname)
  390. {
  391. if (BIO_printf(out, " (%s)", sname) <= 0)
  392. return 0;
  393. }
  394. else
  395. {
  396. if (BIO_puts(out, sname) <= 0)
  397. return 0;
  398. }
  399. }
  400. if (BIO_write(out, ": ", 2) != 2)
  401. return 0;
  402. return 1;
  403. }
  404. static int asn1_print_boolean_ctx(BIO *out, int boolval,
  405. const ASN1_PCTX *pctx)
  406. {
  407. const char *str;
  408. switch (boolval)
  409. {
  410. case -1:
  411. str = "BOOL ABSENT";
  412. break;
  413. case 0:
  414. str = "FALSE";
  415. break;
  416. default:
  417. str = "TRUE";
  418. break;
  419. }
  420. if (BIO_puts(out, str) <= 0)
  421. return 0;
  422. return 1;
  423. }
  424. static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
  425. const ASN1_PCTX *pctx)
  426. {
  427. BIGNUM *bn = NULL;
  428. char *s = NULL;
  429. int ret = 1;
  430. bn = ASN1_INTEGER_to_BN(str, NULL);
  431. if (bn == NULL) {
  432. return 0;
  433. }
  434. s = BN_bn2dec(bn);
  435. BN_free(bn);
  436. if (s == NULL) {
  437. return 0;
  438. }
  439. if (BIO_puts(out, s) <= 0) {
  440. ret = 0;
  441. }
  442. OPENSSL_free(s);
  443. return ret;
  444. }
  445. static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
  446. const ASN1_PCTX *pctx)
  447. {
  448. char objbuf[80];
  449. const char *ln;
  450. ln = OBJ_nid2ln(OBJ_obj2nid(oid));
  451. if(!ln)
  452. ln = "";
  453. OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
  454. if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
  455. return 0;
  456. return 1;
  457. }
  458. static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
  459. const ASN1_PCTX *pctx)
  460. {
  461. if (str->type == V_ASN1_BIT_STRING)
  462. {
  463. if (BIO_printf(out, " (%ld unused bits)\n",
  464. str->flags & 0x7) <= 0)
  465. return 0;
  466. }
  467. else if (BIO_puts(out, "\n") <= 0)
  468. return 0;
  469. if (str->length > 0 && !BIO_hexdump(out, str->data, str->length, indent + 2)) {
  470. return 0;
  471. }
  472. return 1;
  473. }
  474. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  475. const ASN1_ITEM *it, int indent,
  476. const char *fname, const char *sname,
  477. const ASN1_PCTX *pctx)
  478. {
  479. long utype;
  480. ASN1_STRING *str;
  481. int ret = 1, needlf = 1;
  482. const char *pname;
  483. const ASN1_PRIMITIVE_FUNCS *pf;
  484. pf = it->funcs;
  485. if (!asn1_print_fsname(out, indent, fname, sname, pctx))
  486. return 0;
  487. if (pf && pf->prim_print)
  488. return pf->prim_print(out, fld, it, indent, pctx);
  489. str = (ASN1_STRING *)*fld;
  490. if (it->itype == ASN1_ITYPE_MSTRING)
  491. utype = str->type & ~V_ASN1_NEG;
  492. else
  493. utype = it->utype;
  494. if (utype == V_ASN1_ANY)
  495. {
  496. ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
  497. utype = atype->type;
  498. fld = &atype->value.asn1_value;
  499. str = (ASN1_STRING *)*fld;
  500. if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
  501. pname = NULL;
  502. else
  503. pname = ASN1_tag2str(utype);
  504. }
  505. else
  506. {
  507. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
  508. pname = ASN1_tag2str(utype);
  509. else
  510. pname = NULL;
  511. }
  512. if (utype == V_ASN1_NULL)
  513. {
  514. if (BIO_puts(out, "NULL\n") <= 0)
  515. return 0;
  516. return 1;
  517. }
  518. if (pname)
  519. {
  520. if (BIO_puts(out, pname) <= 0)
  521. return 0;
  522. if (BIO_puts(out, ":") <= 0)
  523. return 0;
  524. }
  525. switch (utype)
  526. {
  527. case V_ASN1_BOOLEAN:
  528. {
  529. int boolval = *(int *)fld;
  530. if (boolval == -1)
  531. boolval = it->size;
  532. ret = asn1_print_boolean_ctx(out, boolval, pctx);
  533. }
  534. break;
  535. case V_ASN1_INTEGER:
  536. case V_ASN1_ENUMERATED:
  537. ret = asn1_print_integer_ctx(out, str, pctx);
  538. break;
  539. case V_ASN1_UTCTIME:
  540. ret = ASN1_UTCTIME_print(out, str);
  541. break;
  542. case V_ASN1_GENERALIZEDTIME:
  543. ret = ASN1_GENERALIZEDTIME_print(out, str);
  544. break;
  545. case V_ASN1_OBJECT:
  546. ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
  547. break;
  548. case V_ASN1_OCTET_STRING:
  549. case V_ASN1_BIT_STRING:
  550. ret = asn1_print_obstring_ctx(out, str, indent, pctx);
  551. needlf = 0;
  552. break;
  553. case V_ASN1_SEQUENCE:
  554. case V_ASN1_SET:
  555. case V_ASN1_OTHER:
  556. if (BIO_puts(out, "\n") <= 0)
  557. return 0;
  558. if (ASN1_parse_dump(out, str->data, str->length,
  559. indent, 0) <= 0)
  560. ret = 0;
  561. needlf = 0;
  562. break;
  563. default:
  564. ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
  565. }
  566. if (!ret)
  567. return 0;
  568. if (needlf && BIO_puts(out, "\n") <= 0)
  569. return 0;
  570. return 1;
  571. }