v3_prn.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* X509 v3 extension utilities */
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. /* Extension printing routines */
  15. static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
  16. unsigned long flag, int indent, int supported);
  17. /* Print out a name+value stack */
  18. void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent,
  19. int ml)
  20. {
  21. int i;
  22. CONF_VALUE *nval;
  23. if (!val)
  24. return;
  25. if (!ml || !sk_CONF_VALUE_num(val)) {
  26. BIO_printf(out, "%*s", indent, "");
  27. if (!sk_CONF_VALUE_num(val))
  28. BIO_puts(out, "<EMPTY>\n");
  29. }
  30. for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
  31. if (ml)
  32. BIO_printf(out, "%*s", indent, "");
  33. else if (i > 0)
  34. BIO_printf(out, ", ");
  35. nval = sk_CONF_VALUE_value(val, i);
  36. if (!nval->name)
  37. BIO_puts(out, nval->value);
  38. else if (!nval->value)
  39. BIO_puts(out, nval->name);
  40. #ifndef CHARSET_EBCDIC
  41. else
  42. BIO_printf(out, "%s:%s", nval->name, nval->value);
  43. #else
  44. else {
  45. int len;
  46. char *tmp;
  47. len = strlen(nval->value) + 1;
  48. tmp = OPENSSL_malloc(len);
  49. if (tmp != NULL) {
  50. ascii2ebcdic(tmp, nval->value, len);
  51. BIO_printf(out, "%s:%s", nval->name, tmp);
  52. OPENSSL_free(tmp);
  53. }
  54. }
  55. #endif
  56. if (ml)
  57. BIO_puts(out, "\n");
  58. }
  59. }
  60. /* Main routine: print out a general extension */
  61. int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
  62. int indent)
  63. {
  64. void *ext_str = NULL;
  65. char *value = NULL;
  66. ASN1_OCTET_STRING *extoct;
  67. const unsigned char *p;
  68. int extlen;
  69. const X509V3_EXT_METHOD *method;
  70. STACK_OF(CONF_VALUE) *nval = NULL;
  71. int ok = 1;
  72. extoct = X509_EXTENSION_get_data(ext);
  73. p = ASN1_STRING_get0_data(extoct);
  74. extlen = ASN1_STRING_length(extoct);
  75. if ((method = X509V3_EXT_get(ext)) == NULL)
  76. return unknown_ext_print(out, p, extlen, flag, indent, 0);
  77. if (method->it)
  78. ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
  79. else
  80. ext_str = method->d2i(NULL, &p, extlen);
  81. if (!ext_str)
  82. return unknown_ext_print(out, p, extlen, flag, indent, 1);
  83. if (method->i2s) {
  84. if ((value = method->i2s(method, ext_str)) == NULL) {
  85. ok = 0;
  86. goto err;
  87. }
  88. #ifndef CHARSET_EBCDIC
  89. BIO_printf(out, "%*s%s", indent, "", value);
  90. #else
  91. {
  92. int len;
  93. char *tmp;
  94. len = strlen(value) + 1;
  95. tmp = OPENSSL_malloc(len);
  96. if (tmp != NULL) {
  97. ascii2ebcdic(tmp, value, len);
  98. BIO_printf(out, "%*s%s", indent, "", tmp);
  99. OPENSSL_free(tmp);
  100. }
  101. }
  102. #endif
  103. } else if (method->i2v) {
  104. if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
  105. ok = 0;
  106. goto err;
  107. }
  108. X509V3_EXT_val_prn(out, nval, indent,
  109. method->ext_flags & X509V3_EXT_MULTILINE);
  110. } else if (method->i2r) {
  111. if (!method->i2r(method, ext_str, out, indent))
  112. ok = 0;
  113. } else
  114. ok = 0;
  115. err:
  116. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  117. OPENSSL_free(value);
  118. if (method->it)
  119. ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
  120. else
  121. method->ext_free(ext_str);
  122. return ok;
  123. }
  124. int X509V3_extensions_print(BIO *bp, const char *title,
  125. const STACK_OF(X509_EXTENSION) *exts,
  126. unsigned long flag, int indent)
  127. {
  128. int i, j;
  129. if (sk_X509_EXTENSION_num(exts) <= 0)
  130. return 1;
  131. if (title) {
  132. BIO_printf(bp, "%*s%s:\n", indent, "", title);
  133. indent += 4;
  134. }
  135. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  136. ASN1_OBJECT *obj;
  137. X509_EXTENSION *ex;
  138. ex = sk_X509_EXTENSION_value(exts, i);
  139. if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
  140. return 0;
  141. obj = X509_EXTENSION_get_object(ex);
  142. i2a_ASN1_OBJECT(bp, obj);
  143. j = X509_EXTENSION_get_critical(ex);
  144. if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
  145. return 0;
  146. if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
  147. BIO_printf(bp, "%*s", indent + 4, "");
  148. ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
  149. }
  150. if (BIO_write(bp, "\n", 1) <= 0)
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
  156. unsigned long flag, int indent, int supported)
  157. {
  158. switch (flag & X509V3_EXT_UNKNOWN_MASK) {
  159. case X509V3_EXT_DEFAULT:
  160. return 0;
  161. case X509V3_EXT_ERROR_UNKNOWN:
  162. if (supported)
  163. BIO_printf(out, "%*s<Parse Error>", indent, "");
  164. else
  165. BIO_printf(out, "%*s<Not Supported>", indent, "");
  166. return 1;
  167. case X509V3_EXT_PARSE_UNKNOWN:
  168. return ASN1_parse_dump(out, ext, extlen, indent, -1);
  169. case X509V3_EXT_DUMP_UNKNOWN:
  170. return BIO_dump_indent(out, (const char *)ext, extlen, indent);
  171. default:
  172. return 1;
  173. }
  174. }
  175. #ifndef OPENSSL_NO_STDIO
  176. int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
  177. {
  178. BIO *bio_tmp;
  179. int ret;
  180. if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
  181. return 0;
  182. ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
  183. BIO_free(bio_tmp);
  184. return ret;
  185. }
  186. #endif