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.
 
 
 
 
 
 

643 linhas
15 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 <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. break;
  202. }
  203. /* fall thru */
  204. case ASN1_ITYPE_MSTRING:
  205. if (!asn1_primitive_print(out, fld, it,
  206. indent, fname, sname,pctx))
  207. return 0;
  208. break;
  209. case ASN1_ITYPE_EXTERN:
  210. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  211. return 0;
  212. /* Use new style print routine if possible */
  213. ef = it->funcs;
  214. if (ef && ef->asn1_ex_print)
  215. {
  216. i = ef->asn1_ex_print(out, fld, indent, "", pctx);
  217. if (!i)
  218. return 0;
  219. if ((i == 2) && (BIO_puts(out, "\n") <= 0))
  220. return 0;
  221. return 1;
  222. }
  223. else if (sname &&
  224. BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
  225. return 0;
  226. break;
  227. case ASN1_ITYPE_CHOICE:
  228. #if 0
  229. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  230. return 0;
  231. #endif
  232. /* CHOICE type, get selector */
  233. i = asn1_get_choice_selector(fld, it);
  234. /* This should never happen... */
  235. if((i < 0) || (i >= it->tcount))
  236. {
  237. if (BIO_printf(out,
  238. "ERROR: selector [%d] invalid\n", i) <= 0)
  239. return 0;
  240. return 1;
  241. }
  242. tt = it->templates + i;
  243. tmpfld = asn1_get_field_ptr(fld, tt);
  244. if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
  245. return 0;
  246. break;
  247. case ASN1_ITYPE_SEQUENCE:
  248. case ASN1_ITYPE_NDEF_SEQUENCE:
  249. if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
  250. return 0;
  251. if (fname || sname)
  252. {
  253. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  254. {
  255. if (BIO_puts(out, " {\n") <= 0)
  256. return 0;
  257. }
  258. else
  259. {
  260. if (BIO_puts(out, "\n") <= 0)
  261. return 0;
  262. }
  263. }
  264. if (asn1_cb)
  265. {
  266. i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
  267. if (i == 0)
  268. return 0;
  269. if (i == 2)
  270. return 1;
  271. }
  272. /* Print each field entry */
  273. for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
  274. {
  275. const ASN1_TEMPLATE *seqtt;
  276. seqtt = asn1_do_adb(fld, tt, 1);
  277. if (!seqtt)
  278. return 0;
  279. tmpfld = asn1_get_field_ptr(fld, seqtt);
  280. if (!asn1_template_print_ctx(out, tmpfld,
  281. indent + 2, seqtt, pctx))
  282. return 0;
  283. }
  284. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  285. {
  286. if (BIO_printf(out, "%*s}\n", indent, "") < 0)
  287. return 0;
  288. }
  289. if (asn1_cb)
  290. {
  291. i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
  292. if (i == 0)
  293. return 0;
  294. }
  295. break;
  296. default:
  297. BIO_printf(out, "Unprocessed type %d\n", it->itype);
  298. return 0;
  299. }
  300. return 1;
  301. }
  302. int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
  303. const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
  304. {
  305. int flags;
  306. size_t i;
  307. const char *sname, *fname;
  308. flags = tt->flags;
  309. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
  310. sname = ASN1_ITEM_ptr(tt->item)->sname;
  311. else
  312. sname = NULL;
  313. if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  314. fname = NULL;
  315. else
  316. fname = tt->field_name;
  317. if(flags & ASN1_TFLG_SK_MASK)
  318. {
  319. const char *tname;
  320. ASN1_VALUE *skitem;
  321. STACK_OF(ASN1_VALUE) *stack;
  322. /* SET OF, SEQUENCE OF */
  323. if (fname)
  324. {
  325. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
  326. {
  327. if(flags & ASN1_TFLG_SET_OF)
  328. tname = "SET";
  329. else
  330. tname = "SEQUENCE";
  331. if (BIO_printf(out, "%*s%s OF %s {\n",
  332. indent, "", tname, tt->field_name) <= 0)
  333. return 0;
  334. }
  335. else if (BIO_printf(out, "%*s%s:\n", indent, "",
  336. fname) <= 0)
  337. return 0;
  338. }
  339. stack = (STACK_OF(ASN1_VALUE) *)*fld;
  340. for(i = 0; i < sk_ASN1_VALUE_num(stack); i++)
  341. {
  342. if ((i > 0) && (BIO_puts(out, "\n") <= 0))
  343. return 0;
  344. skitem = sk_ASN1_VALUE_value(stack, i);
  345. if (!asn1_item_print_ctx(out, &skitem, indent + 2,
  346. ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
  347. return 0;
  348. }
  349. if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
  350. return 0;
  351. if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
  352. {
  353. if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
  354. return 0;
  355. }
  356. return 1;
  357. }
  358. return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
  359. fname, sname, 0, pctx);
  360. }
  361. static int asn1_print_fsname(BIO *out, int indent,
  362. const char *fname, const char *sname,
  363. const ASN1_PCTX *pctx)
  364. {
  365. static char spaces[] = " ";
  366. const int nspaces = sizeof(spaces) - 1;
  367. #if 0
  368. if (!sname && !fname)
  369. return 1;
  370. #endif
  371. while (indent > nspaces)
  372. {
  373. if (BIO_write(out, spaces, nspaces) != nspaces)
  374. return 0;
  375. indent -= nspaces;
  376. }
  377. if (BIO_write(out, spaces, indent) != indent)
  378. return 0;
  379. if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
  380. sname = NULL;
  381. if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
  382. fname = NULL;
  383. if (!sname && !fname)
  384. return 1;
  385. if (fname)
  386. {
  387. if (BIO_puts(out, fname) <= 0)
  388. return 0;
  389. }
  390. if (sname)
  391. {
  392. if (fname)
  393. {
  394. if (BIO_printf(out, " (%s)", sname) <= 0)
  395. return 0;
  396. }
  397. else
  398. {
  399. if (BIO_puts(out, sname) <= 0)
  400. return 0;
  401. }
  402. }
  403. if (BIO_write(out, ": ", 2) != 2)
  404. return 0;
  405. return 1;
  406. }
  407. static int asn1_print_boolean_ctx(BIO *out, int boolval,
  408. const ASN1_PCTX *pctx)
  409. {
  410. const char *str;
  411. switch (boolval)
  412. {
  413. case -1:
  414. str = "BOOL ABSENT";
  415. break;
  416. case 0:
  417. str = "FALSE";
  418. break;
  419. default:
  420. str = "TRUE";
  421. break;
  422. }
  423. if (BIO_puts(out, str) <= 0)
  424. return 0;
  425. return 1;
  426. }
  427. static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
  428. const ASN1_PCTX *pctx)
  429. {
  430. BIGNUM *bn = NULL;
  431. char *s = NULL;
  432. int ret = 1;
  433. bn = ASN1_INTEGER_to_BN(str, NULL);
  434. if (bn == NULL) {
  435. return 0;
  436. }
  437. s = BN_bn2dec(bn);
  438. BN_free(bn);
  439. if (s == NULL) {
  440. return 0;
  441. }
  442. if (BIO_puts(out, s) <= 0) {
  443. ret = 0;
  444. }
  445. OPENSSL_free(s);
  446. return ret;
  447. }
  448. static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
  449. const ASN1_PCTX *pctx)
  450. {
  451. char objbuf[80];
  452. const char *ln;
  453. ln = OBJ_nid2ln(OBJ_obj2nid(oid));
  454. if(!ln)
  455. ln = "";
  456. OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
  457. if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
  458. return 0;
  459. return 1;
  460. }
  461. static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
  462. const ASN1_PCTX *pctx)
  463. {
  464. if (str->type == V_ASN1_BIT_STRING)
  465. {
  466. if (BIO_printf(out, " (%ld unused bits)\n",
  467. str->flags & 0x7) <= 0)
  468. return 0;
  469. }
  470. else if (BIO_puts(out, "\n") <= 0)
  471. return 0;
  472. if (str->length > 0 && !BIO_hexdump(out, str->data, str->length, indent + 2)) {
  473. return 0;
  474. }
  475. return 1;
  476. }
  477. static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
  478. const ASN1_ITEM *it, int indent,
  479. const char *fname, const char *sname,
  480. const ASN1_PCTX *pctx)
  481. {
  482. long utype;
  483. ASN1_STRING *str;
  484. int ret = 1, needlf = 1;
  485. const char *pname;
  486. const ASN1_PRIMITIVE_FUNCS *pf;
  487. pf = it->funcs;
  488. if (!asn1_print_fsname(out, indent, fname, sname, pctx))
  489. return 0;
  490. if (pf && pf->prim_print)
  491. return pf->prim_print(out, fld, it, indent, pctx);
  492. str = (ASN1_STRING *)*fld;
  493. if (it->itype == ASN1_ITYPE_MSTRING)
  494. utype = str->type & ~V_ASN1_NEG;
  495. else
  496. utype = it->utype;
  497. if (utype == V_ASN1_ANY)
  498. {
  499. ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
  500. utype = atype->type;
  501. fld = &atype->value.asn1_value;
  502. str = (ASN1_STRING *)*fld;
  503. if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
  504. pname = NULL;
  505. else
  506. pname = ASN1_tag2str(utype);
  507. }
  508. else
  509. {
  510. if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
  511. pname = ASN1_tag2str(utype);
  512. else
  513. pname = NULL;
  514. }
  515. if (utype == V_ASN1_NULL)
  516. {
  517. if (BIO_puts(out, "NULL\n") <= 0)
  518. return 0;
  519. return 1;
  520. }
  521. if (pname)
  522. {
  523. if (BIO_puts(out, pname) <= 0)
  524. return 0;
  525. if (BIO_puts(out, ":") <= 0)
  526. return 0;
  527. }
  528. switch (utype)
  529. {
  530. case V_ASN1_BOOLEAN:
  531. {
  532. int boolval = *(int *)fld;
  533. if (boolval == -1)
  534. boolval = it->size;
  535. ret = asn1_print_boolean_ctx(out, boolval, pctx);
  536. }
  537. break;
  538. case V_ASN1_INTEGER:
  539. case V_ASN1_ENUMERATED:
  540. ret = asn1_print_integer_ctx(out, str, pctx);
  541. break;
  542. case V_ASN1_UTCTIME:
  543. ret = ASN1_UTCTIME_print(out, str);
  544. break;
  545. case V_ASN1_GENERALIZEDTIME:
  546. ret = ASN1_GENERALIZEDTIME_print(out, str);
  547. break;
  548. case V_ASN1_OBJECT:
  549. ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
  550. break;
  551. case V_ASN1_OCTET_STRING:
  552. case V_ASN1_BIT_STRING:
  553. ret = asn1_print_obstring_ctx(out, str, indent, pctx);
  554. needlf = 0;
  555. break;
  556. case V_ASN1_SEQUENCE:
  557. case V_ASN1_SET:
  558. case V_ASN1_OTHER:
  559. if (BIO_puts(out, "\n") <= 0)
  560. return 0;
  561. if (ASN1_parse_dump(out, str->data, str->length,
  562. indent, 0) <= 0)
  563. ret = 0;
  564. needlf = 0;
  565. break;
  566. default:
  567. ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
  568. }
  569. if (!ret)
  570. return 0;
  571. if (needlf && BIO_puts(out, "\n") <= 0)
  572. return 0;
  573. return 1;
  574. }