You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

230 regels
7.3 KiB

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