2
0

v3_prn.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (i > 0)
  33. BIO_printf(out, "\n");
  34. BIO_printf(out, "%*s", indent, "");
  35. }
  36. else if (i > 0)
  37. BIO_printf(out, ", ");
  38. nval = sk_CONF_VALUE_value(val, i);
  39. if (!nval->name)
  40. BIO_puts(out, nval->value);
  41. else if (!nval->value)
  42. BIO_puts(out, nval->name);
  43. #ifndef CHARSET_EBCDIC
  44. else
  45. BIO_printf(out, "%s:%s", nval->name, nval->value);
  46. #else
  47. else {
  48. int len;
  49. char *tmp;
  50. len = strlen(nval->value) + 1;
  51. tmp = OPENSSL_malloc(len);
  52. if (tmp != NULL) {
  53. ascii2ebcdic(tmp, nval->value, len);
  54. BIO_printf(out, "%s:%s", nval->name, tmp);
  55. OPENSSL_free(tmp);
  56. }
  57. }
  58. #endif
  59. }
  60. }
  61. /* Main routine: print out a general extension */
  62. int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
  63. int indent)
  64. {
  65. void *ext_str = NULL;
  66. char *value = NULL;
  67. ASN1_OCTET_STRING *extoct;
  68. const unsigned char *p;
  69. int extlen;
  70. const X509V3_EXT_METHOD *method;
  71. STACK_OF(CONF_VALUE) *nval = NULL;
  72. int ok = 1;
  73. extoct = X509_EXTENSION_get_data(ext);
  74. p = ASN1_STRING_get0_data(extoct);
  75. extlen = ASN1_STRING_length(extoct);
  76. if ((method = X509V3_EXT_get(ext)) == NULL)
  77. return unknown_ext_print(out, p, extlen, flag, indent, 0);
  78. if (method->it)
  79. ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
  80. else
  81. ext_str = method->d2i(NULL, &p, extlen);
  82. if (!ext_str)
  83. return unknown_ext_print(out, p, extlen, flag, indent, 1);
  84. if (method->i2s) {
  85. if ((value = method->i2s(method, ext_str)) == NULL) {
  86. ok = 0;
  87. goto err;
  88. }
  89. #ifndef CHARSET_EBCDIC
  90. BIO_printf(out, "%*s%s", indent, "", value);
  91. #else
  92. {
  93. int len;
  94. char *tmp;
  95. len = strlen(value) + 1;
  96. tmp = OPENSSL_malloc(len);
  97. if (tmp != NULL) {
  98. ascii2ebcdic(tmp, value, len);
  99. BIO_printf(out, "%*s%s", indent, "", tmp);
  100. OPENSSL_free(tmp);
  101. }
  102. }
  103. #endif
  104. } else if (method->i2v) {
  105. if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
  106. ok = 0;
  107. goto err;
  108. }
  109. X509V3_EXT_val_prn(out, nval, indent,
  110. method->ext_flags & X509V3_EXT_MULTILINE);
  111. } else if (method->i2r) {
  112. if (!method->i2r(method, ext_str, out, indent))
  113. ok = 0;
  114. } else
  115. ok = 0;
  116. err:
  117. sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
  118. OPENSSL_free(value);
  119. if (method->it)
  120. ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
  121. else
  122. method->ext_free(ext_str);
  123. return ok;
  124. }
  125. int X509V3_extensions_print(BIO *bp, const char *title,
  126. const STACK_OF(X509_EXTENSION) *exts,
  127. unsigned long flag, int indent)
  128. {
  129. int i, j;
  130. if (sk_X509_EXTENSION_num(exts) <= 0)
  131. return 1;
  132. if (title) {
  133. BIO_printf(bp, "%*s%s:\n", indent, "", title);
  134. indent += 4;
  135. }
  136. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  137. ASN1_OBJECT *obj;
  138. X509_EXTENSION *ex;
  139. ex = sk_X509_EXTENSION_value(exts, i);
  140. if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
  141. return 0;
  142. obj = X509_EXTENSION_get_object(ex);
  143. i2a_ASN1_OBJECT(bp, obj);
  144. j = X509_EXTENSION_get_critical(ex);
  145. if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
  146. return 0;
  147. if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
  148. BIO_printf(bp, "%*s", indent + 4, "");
  149. ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
  150. }
  151. if (BIO_write(bp, "\n", 1) <= 0)
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
  157. unsigned long flag, int indent, int supported)
  158. {
  159. switch (flag & X509V3_EXT_UNKNOWN_MASK) {
  160. case X509V3_EXT_DEFAULT:
  161. return 0;
  162. case X509V3_EXT_ERROR_UNKNOWN:
  163. if (supported)
  164. BIO_printf(out, "%*s<Parse Error>", indent, "");
  165. else
  166. BIO_printf(out, "%*s<Not Supported>", indent, "");
  167. return 1;
  168. case X509V3_EXT_PARSE_UNKNOWN:
  169. return ASN1_parse_dump(out, ext, extlen, indent, -1);
  170. case X509V3_EXT_DUMP_UNKNOWN:
  171. return BIO_dump_indent(out, (const char *)ext, extlen, indent);
  172. default:
  173. return 1;
  174. }
  175. }
  176. #ifndef OPENSSL_NO_STDIO
  177. int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
  178. {
  179. BIO *bio_tmp;
  180. int ret;
  181. if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
  182. return 0;
  183. ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
  184. BIO_free(bio_tmp);
  185. return ret;
  186. }
  187. #endif