tasn_prn.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* tasn_prn.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stddef.h>
  60. #include <openssl/asn1.h>
  61. #include <openssl/objects.h>
  62. #include <openssl/buffer.h>
  63. #include <openssl/err.h>
  64. #include <openssl/nasn.h>
  65. /*
  66. * Print routines. Print out a whole structure from a template.
  67. */
  68. static int asn1_item_print_nm(BIO *out, void *fld, int indent,
  69. const ASN1_ITEM *it, const char *name);
  70. int ASN1_item_print(BIO *out, void *fld, int indent, const ASN1_ITEM *it)
  71. {
  72. return asn1_item_print_nm(out, fld, indent, it, it->sname);
  73. }
  74. static int asn1_item_print_nm(BIO *out, void *fld, int indent,
  75. const ASN1_ITEM *it, const char *name)
  76. {
  77. ASN1_STRING *str;
  78. const ASN1_TEMPLATE *tt;
  79. void *tmpfld;
  80. int i;
  81. if (!fld) {
  82. BIO_printf(out, "%*s%s ABSENT\n", indent, "", name);
  83. return 1;
  84. }
  85. switch (it->itype) {
  86. case ASN1_ITYPE_PRIMITIVE:
  87. if (it->templates)
  88. return ASN1_template_print(out, fld, indent, it->templates);
  89. return asn1_primitive_print(out, fld, it->utype, indent, name);
  90. break;
  91. case ASN1_ITYPE_MSTRING:
  92. str = fld;
  93. return asn1_primitive_print(out, fld, str->type, indent, name);
  94. case ASN1_ITYPE_EXTERN:
  95. BIO_printf(out, "%*s%s:EXTERNAL TYPE %s %s\n", indent, "", name,
  96. it->sname, fld ? "" : "ABSENT");
  97. return 1;
  98. case ASN1_ITYPE_COMPAT:
  99. BIO_printf(out, "%*s%s:COMPATIBLE TYPE %s %s\n", indent, "", name,
  100. it->sname, fld ? "" : "ABSENT");
  101. return 1;
  102. case ASN1_ITYPE_CHOICE:
  103. /* CHOICE type, get selector */
  104. i = asn1_get_choice_selector(fld, it);
  105. /* This should never happen... */
  106. if ((i < 0) || (i >= it->tcount)) {
  107. BIO_printf(out, "%s selector [%d] out of range\n", it->sname, i);
  108. return 1;
  109. }
  110. tt = it->templates + i;
  111. tmpfld = asn1_get_field(fld, tt);
  112. return ASN1_template_print(out, tmpfld, indent, tt);
  113. case ASN1_ITYPE_SEQUENCE:
  114. BIO_printf(out, "%*s%s {\n", indent, "", name);
  115. /* Get each field entry */
  116. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  117. tmpfld = asn1_get_field(fld, tt);
  118. ASN1_template_print(out, tmpfld, indent + 2, tt);
  119. }
  120. BIO_printf(out, "%*s}\n", indent, "");
  121. return 1;
  122. default:
  123. return 0;
  124. }
  125. }
  126. int ASN1_template_print(BIO *out, void *fld, int indent,
  127. const ASN1_TEMPLATE *tt)
  128. {
  129. int i, flags;
  130. #if 0
  131. if (!fld)
  132. return 0;
  133. #endif
  134. flags = tt->flags;
  135. if (flags & ASN1_TFLG_SK_MASK) {
  136. char *tname;
  137. void *skitem;
  138. /* SET OF, SEQUENCE OF */
  139. if (flags & ASN1_TFLG_SET_OF)
  140. tname = "SET";
  141. else
  142. tname = "SEQUENCE";
  143. if (fld) {
  144. BIO_printf(out, "%*s%s OF %s {\n", indent, "", tname,
  145. tt->field_name);
  146. for (i = 0; i < sk_num(fld); i++) {
  147. skitem = sk_value(fld, i);
  148. asn1_item_print_nm(out, skitem, indent + 2, tt->item, "");
  149. }
  150. BIO_printf(out, "%*s}\n", indent, "");
  151. } else
  152. BIO_printf(out, "%*s%s OF %s ABSENT\n", indent, "", tname,
  153. tt->field_name);
  154. return 1;
  155. }
  156. return asn1_item_print_nm(out, fld, indent, tt->item, tt->field_name);
  157. }
  158. static int asn1_primitive_print(BIO *out, void *fld, long utype, int indent,
  159. const char *name)
  160. {
  161. ASN1_STRING *str = fld;
  162. if (fld) {
  163. if (utype == V_ASN1_BOOLEAN) {
  164. int *bool = fld;
  165. if (*bool == -1)
  166. printf("BOOL MISSING\n");
  167. BIO_printf(out, "%*s%s:%s", indent, "", "BOOLEAN",
  168. *bool ? "TRUE" : "FALSE");
  169. } else if ((utype == V_ASN1_INTEGER)
  170. || (utype == V_ASN1_ENUMERATED)) {
  171. char *s, *nm;
  172. s = i2s_ASN1_INTEGER(NULL, fld);
  173. if (utype == V_ASN1_INTEGER)
  174. nm = "INTEGER";
  175. else
  176. nm = "ENUMERATED";
  177. BIO_printf(out, "%*s%s:%s", indent, "", nm, s);
  178. OPENSSL_free(s);
  179. } else if (utype == V_ASN1_NULL) {
  180. BIO_printf(out, "%*s%s", indent, "", "NULL");
  181. } else if (utype == V_ASN1_UTCTIME) {
  182. BIO_printf(out, "%*s%s:%s:", indent, "", name, "UTCTIME");
  183. ASN1_UTCTIME_print(out, str);
  184. } else if (utype == V_ASN1_GENERALIZEDTIME) {
  185. BIO_printf(out, "%*s%s:%s:", indent, "", name, "GENERALIZEDTIME");
  186. ASN1_GENERALIZEDTIME_print(out, str);
  187. } else if (utype == V_ASN1_OBJECT) {
  188. char objbuf[80], *ln;
  189. ln = OBJ_nid2ln(OBJ_obj2nid(fld));
  190. if (!ln)
  191. ln = "";
  192. OBJ_obj2txt(objbuf, sizeof objbuf, fld, 1);
  193. BIO_printf(out, "%*s%s:%s (%s)", indent, "", "OBJECT", ln,
  194. objbuf);
  195. } else {
  196. BIO_printf(out, "%*s%s:", indent, "", name);
  197. ASN1_STRING_print_ex(out, str,
  198. ASN1_STRFLGS_DUMP_UNKNOWN |
  199. ASN1_STRFLGS_SHOW_TYPE);
  200. }
  201. BIO_printf(out, "\n");
  202. } else
  203. BIO_printf(out, "%*s%s [ABSENT]\n", indent, "", name);
  204. return 1;
  205. }