v3_prn.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "internal/cryptlib.h"
  62. #include <openssl/conf.h>
  63. #include <openssl/x509v3.h>
  64. /* Extension printing routines */
  65. static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
  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. ASN1_OCTET_STRING *extoct;
  117. const unsigned char *p;
  118. int extlen;
  119. const X509V3_EXT_METHOD *method;
  120. STACK_OF(CONF_VALUE) *nval = NULL;
  121. int ok = 1;
  122. extoct = X509_EXTENSION_get_data(ext);
  123. p = ASN1_STRING_data(extoct);
  124. extlen = ASN1_STRING_length(extoct);
  125. if ((method = X509V3_EXT_get(ext)) == NULL)
  126. return unknown_ext_print(out, p, extlen, flag, indent, 0);
  127. if (method->it)
  128. ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
  129. else
  130. ext_str = method->d2i(NULL, &p, extlen);
  131. if (!ext_str)
  132. return unknown_ext_print(out, p, extlen, flag, indent, 1);
  133. if (method->i2s) {
  134. if ((value = method->i2s(method, ext_str)) == NULL) {
  135. ok = 0;
  136. goto err;
  137. }
  138. #ifndef CHARSET_EBCDIC
  139. BIO_printf(out, "%*s%s", indent, "", value);
  140. #else
  141. {
  142. int len;
  143. char *tmp;
  144. len = strlen(value) + 1;
  145. tmp = OPENSSL_malloc(len);
  146. if (tmp) {
  147. ascii2ebcdic(tmp, value, len);
  148. BIO_printf(out, "%*s%s", indent, "", tmp);
  149. OPENSSL_free(tmp);
  150. }
  151. }
  152. #endif
  153. } else if (method->i2v) {
  154. if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
  155. ok = 0;
  156. goto err;
  157. }
  158. X509V3_EXT_val_prn(out, nval, indent,
  159. method->ext_flags & X509V3_EXT_MULTILINE);
  160. } else if (method->i2r) {
  161. if (!method->i2r(method, ext_str, out, indent))
  162. ok = 0;
  163. } else
  164. ok = 0;
  165. err:
  166. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  167. OPENSSL_free(value);
  168. if (method->it)
  169. ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
  170. else
  171. method->ext_free(ext_str);
  172. return ok;
  173. }
  174. int X509V3_extensions_print(BIO *bp, char *title,
  175. STACK_OF(X509_EXTENSION) *exts,
  176. unsigned long flag, int indent)
  177. {
  178. int i, j;
  179. if (sk_X509_EXTENSION_num(exts) <= 0)
  180. return 1;
  181. if (title) {
  182. BIO_printf(bp, "%*s%s:\n", indent, "", title);
  183. indent += 4;
  184. }
  185. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  186. ASN1_OBJECT *obj;
  187. X509_EXTENSION *ex;
  188. ex = sk_X509_EXTENSION_value(exts, i);
  189. if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
  190. return 0;
  191. obj = X509_EXTENSION_get_object(ex);
  192. i2a_ASN1_OBJECT(bp, obj);
  193. j = X509_EXTENSION_get_critical(ex);
  194. if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
  195. return 0;
  196. if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
  197. BIO_printf(bp, "%*s", indent + 4, "");
  198. ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
  199. }
  200. if (BIO_write(bp, "\n", 1) <= 0)
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
  206. unsigned long flag, int indent, int supported)
  207. {
  208. switch (flag & X509V3_EXT_UNKNOWN_MASK) {
  209. case X509V3_EXT_DEFAULT:
  210. return 0;
  211. case X509V3_EXT_ERROR_UNKNOWN:
  212. if (supported)
  213. BIO_printf(out, "%*s<Parse Error>", indent, "");
  214. else
  215. BIO_printf(out, "%*s<Not Supported>", indent, "");
  216. return 1;
  217. case X509V3_EXT_PARSE_UNKNOWN:
  218. return ASN1_parse_dump(out, ext, extlen, indent, -1);
  219. case X509V3_EXT_DUMP_UNKNOWN:
  220. return BIO_dump_indent(out, (char *)ext, extlen, indent);
  221. default:
  222. return 1;
  223. }
  224. }
  225. #ifndef OPENSSL_NO_STDIO
  226. int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
  227. {
  228. BIO *bio_tmp;
  229. int ret;
  230. if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
  231. return 0;
  232. ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
  233. BIO_free(bio_tmp);
  234. return ret;
  235. }
  236. #endif