tasn_prn.c 6.8 KB

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