v3_prn.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* v3_prn.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 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. /* X509 v3 extension utilities */
  60. #include <stdio.h>
  61. #include "cryptlib.h"
  62. #include <openssl/conf.h>
  63. #include <openssl/x509v3.h>
  64. /* Extension printing routines */
  65. static int unknown_ext_print(BIO *out, X509_EXTENSION *ext,
  66. unsigned long flag, int indent, int supported);
  67. /* Print out a name+value stack */
  68. void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent,
  69. int ml)
  70. {
  71. int i;
  72. CONF_VALUE *nval;
  73. if (!val)
  74. return;
  75. if (!ml || !sk_CONF_VALUE_num(val)) {
  76. BIO_printf(out, "%*s", indent, "");
  77. if (!sk_CONF_VALUE_num(val))
  78. BIO_puts(out, "<EMPTY>\n");
  79. }
  80. for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
  81. if (ml)
  82. BIO_printf(out, "%*s", indent, "");
  83. else if (i > 0)
  84. BIO_printf(out, ", ");
  85. nval = sk_CONF_VALUE_value(val, i);
  86. if (!nval->name)
  87. BIO_puts(out, nval->value);
  88. else if (!nval->value)
  89. BIO_puts(out, nval->name);
  90. #ifndef CHARSET_EBCDIC
  91. else
  92. BIO_printf(out, "%s:%s", nval->name, nval->value);
  93. #else
  94. else {
  95. int len;
  96. char *tmp;
  97. len = strlen(nval->value) + 1;
  98. tmp = OPENSSL_malloc(len);
  99. if (tmp) {
  100. ascii2ebcdic(tmp, nval->value, len);
  101. BIO_printf(out, "%s:%s", nval->name, tmp);
  102. OPENSSL_free(tmp);
  103. }
  104. }
  105. #endif
  106. if (ml)
  107. BIO_puts(out, "\n");
  108. }
  109. }
  110. /* Main routine: print out a general extension */
  111. int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
  112. int indent)
  113. {
  114. void *ext_str = NULL;
  115. char *value = NULL;
  116. const unsigned char *p;
  117. const X509V3_EXT_METHOD *method;
  118. STACK_OF(CONF_VALUE) *nval = NULL;
  119. int ok = 1;
  120. if (!(method = X509V3_EXT_get(ext)))
  121. return unknown_ext_print(out, ext, flag, indent, 0);
  122. p = ext->value->data;
  123. if (method->it)
  124. ext_str =
  125. ASN1_item_d2i(NULL, &p, ext->value->length,
  126. ASN1_ITEM_ptr(method->it));
  127. else
  128. ext_str = method->d2i(NULL, &p, ext->value->length);
  129. if (!ext_str)
  130. return unknown_ext_print(out, ext, flag, indent, 1);
  131. if (method->i2s) {
  132. if (!(value = method->i2s(method, ext_str))) {
  133. ok = 0;
  134. goto err;
  135. }
  136. #ifndef CHARSET_EBCDIC
  137. BIO_printf(out, "%*s%s", indent, "", value);
  138. #else
  139. {
  140. int len;
  141. char *tmp;
  142. len = strlen(value) + 1;
  143. tmp = OPENSSL_malloc(len);
  144. if (tmp) {
  145. ascii2ebcdic(tmp, value, len);
  146. BIO_printf(out, "%*s%s", indent, "", tmp);
  147. OPENSSL_free(tmp);
  148. }
  149. }
  150. #endif
  151. } else if (method->i2v) {
  152. if (!(nval = method->i2v(method, ext_str, NULL))) {
  153. ok = 0;
  154. goto err;
  155. }
  156. X509V3_EXT_val_prn(out, nval, indent,
  157. method->ext_flags & X509V3_EXT_MULTILINE);
  158. } else if (method->i2r) {
  159. if (!method->i2r(method, ext_str, out, indent))
  160. ok = 0;
  161. } else
  162. ok = 0;
  163. err:
  164. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  165. if (value)
  166. OPENSSL_free(value);
  167. if (method->it)
  168. ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
  169. else
  170. method->ext_free(ext_str);
  171. return ok;
  172. }
  173. int X509V3_extensions_print(BIO *bp, char *title,
  174. STACK_OF(X509_EXTENSION) *exts,
  175. unsigned long flag, int indent)
  176. {
  177. int i, j;
  178. if (sk_X509_EXTENSION_num(exts) <= 0)
  179. return 1;
  180. if (title) {
  181. BIO_printf(bp, "%*s%s:\n", indent, "", title);
  182. indent += 4;
  183. }
  184. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  185. ASN1_OBJECT *obj;
  186. X509_EXTENSION *ex;
  187. ex = sk_X509_EXTENSION_value(exts, i);
  188. if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
  189. return 0;
  190. obj = X509_EXTENSION_get_object(ex);
  191. i2a_ASN1_OBJECT(bp, obj);
  192. j = X509_EXTENSION_get_critical(ex);
  193. if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
  194. return 0;
  195. if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
  196. BIO_printf(bp, "%*s", indent + 4, "");
  197. M_ASN1_OCTET_STRING_print(bp, ex->value);
  198. }
  199. if (BIO_write(bp, "\n", 1) <= 0)
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. static int unknown_ext_print(BIO *out, X509_EXTENSION *ext,
  205. unsigned long flag, int indent, int supported)
  206. {
  207. switch (flag & X509V3_EXT_UNKNOWN_MASK) {
  208. case X509V3_EXT_DEFAULT:
  209. return 0;
  210. case X509V3_EXT_ERROR_UNKNOWN:
  211. if (supported)
  212. BIO_printf(out, "%*s<Parse Error>", indent, "");
  213. else
  214. BIO_printf(out, "%*s<Not Supported>", indent, "");
  215. return 1;
  216. case X509V3_EXT_PARSE_UNKNOWN:
  217. return ASN1_parse_dump(out,
  218. ext->value->data, ext->value->length, indent,
  219. -1);
  220. case X509V3_EXT_DUMP_UNKNOWN:
  221. return BIO_dump_indent(out, (char *)ext->value->data,
  222. ext->value->length, indent);
  223. default:
  224. return 1;
  225. }
  226. }
  227. #ifndef OPENSSL_NO_FP_API
  228. int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
  229. {
  230. BIO *bio_tmp;
  231. int ret;
  232. if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)))
  233. return 0;
  234. ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
  235. BIO_free(bio_tmp);
  236. return ret;
  237. }
  238. #endif