t_acert.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 2021 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509_acert.h>
  15. static int print_attribute(BIO *bp, X509_ATTRIBUTE *a)
  16. {
  17. ASN1_OBJECT *aobj;
  18. int i, j, count;
  19. int ret = 0;
  20. aobj = X509_ATTRIBUTE_get0_object(a);
  21. if (BIO_printf(bp, "%12s", "") <= 0)
  22. goto err;
  23. if ((j = i2a_ASN1_OBJECT(bp, aobj)) <= 0)
  24. goto err;
  25. count = X509_ATTRIBUTE_count(a);
  26. if (count == 0) {
  27. ERR_raise(ERR_LIB_X509, X509_R_INVALID_ATTRIBUTES);
  28. goto err;
  29. }
  30. if (j < 25 && (BIO_printf(bp, "%*s", 25 - j, " ") <= 0))
  31. goto err;
  32. if (BIO_puts(bp, ":") <= 0)
  33. goto err;
  34. for (i = 0; i < count; i++) {
  35. ASN1_TYPE *at;
  36. int type;
  37. ASN1_BIT_STRING *bs;
  38. at = X509_ATTRIBUTE_get0_type(a, i);
  39. type = at->type;
  40. switch (type) {
  41. case V_ASN1_PRINTABLESTRING:
  42. case V_ASN1_T61STRING:
  43. case V_ASN1_NUMERICSTRING:
  44. case V_ASN1_UTF8STRING:
  45. case V_ASN1_IA5STRING:
  46. bs = at->value.asn1_string;
  47. if (BIO_write(bp, (char *)bs->data, bs->length) != bs->length)
  48. goto err;
  49. if (BIO_puts(bp, "\n") <= 0)
  50. goto err;
  51. break;
  52. case V_ASN1_SEQUENCE:
  53. if (BIO_puts(bp, "\n") <= 0)
  54. goto err;
  55. ASN1_parse_dump(bp, at->value.sequence->data,
  56. at->value.sequence->length, i, 1);
  57. break;
  58. default:
  59. if (BIO_printf(bp, "unable to print attribute of type 0x%X\n",
  60. type) < 0)
  61. goto err;
  62. break;
  63. }
  64. }
  65. ret = 1;
  66. err:
  67. return ret;
  68. }
  69. int X509_ACERT_print_ex(BIO *bp, X509_ACERT *x, unsigned long nmflags,
  70. unsigned long cflag)
  71. {
  72. int i;
  73. char mlch = ' ';
  74. if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  75. mlch = '\n';
  76. }
  77. if ((cflag & X509_FLAG_NO_HEADER) == 0) {
  78. if (BIO_printf(bp, "Attribute Certificate:\n") <= 0)
  79. goto err;
  80. if (BIO_printf(bp, "%4sData:\n", "") <= 0)
  81. goto err;
  82. }
  83. if ((cflag & X509_FLAG_NO_VERSION) == 0) {
  84. long l;
  85. l = X509_ACERT_get_version(x);
  86. if (l == X509_ACERT_VERSION_2) {
  87. if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
  88. (unsigned long)l) <= 0)
  89. goto err;
  90. } else {
  91. if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
  92. goto err;
  93. }
  94. }
  95. if ((cflag & X509_FLAG_NO_SERIAL) == 0) {
  96. const ASN1_INTEGER *serial;
  97. serial = X509_ACERT_get0_serialNumber(x);
  98. if (BIO_printf(bp, "%8sSerial Number: ", "") <= 0)
  99. goto err;
  100. if (i2a_ASN1_INTEGER(bp, serial) <= 0)
  101. goto err;
  102. if (BIO_write(bp, "\n", 1) <= 0)
  103. goto err;
  104. }
  105. if ((cflag & X509_FLAG_NO_SUBJECT) == 0) {
  106. const GENERAL_NAMES *holderEntities;
  107. const OSSL_ISSUER_SERIAL *holder_bcid;
  108. const X509_NAME *holderIssuer = NULL;
  109. if (BIO_printf(bp, "%8sHolder:\n", "") <= 0)
  110. goto err;
  111. holderEntities = X509_ACERT_get0_holder_entityName(x);
  112. if (holderEntities != NULL) {
  113. for (i = 0; i < sk_GENERAL_NAME_num(holderEntities); i++) {
  114. GENERAL_NAME *entity;
  115. entity = sk_GENERAL_NAME_value(holderEntities, i);
  116. if (BIO_printf(bp, "%12sName:%c", "", mlch) <= 0)
  117. goto err;
  118. if (GENERAL_NAME_print(bp, entity) <= 0)
  119. goto err;
  120. if (BIO_write(bp, "\n", 1) <= 0)
  121. goto err;
  122. }
  123. }
  124. if ((holder_bcid = X509_ACERT_get0_holder_baseCertId(x)) != NULL)
  125. holderIssuer = OSSL_ISSUER_SERIAL_get0_issuer(holder_bcid);
  126. if (holderIssuer != NULL) {
  127. const ASN1_INTEGER *holder_serial;
  128. const ASN1_BIT_STRING *iuid;
  129. if (BIO_printf(bp, "%12sIssuer:%c", "", mlch) <= 0)
  130. goto err;
  131. if (X509_NAME_print_ex(bp, holderIssuer, 0, nmflags) <= 0)
  132. goto err;
  133. if (BIO_write(bp, "\n", 1) <= 0)
  134. goto err;
  135. if (BIO_printf(bp, "%12sSerial: ", "") <= 0)
  136. goto err;
  137. holder_serial = OSSL_ISSUER_SERIAL_get0_serial(holder_bcid);
  138. if (i2a_ASN1_INTEGER(bp, holder_serial) <= 0)
  139. goto err;
  140. iuid = OSSL_ISSUER_SERIAL_get0_issuerUID(holder_bcid);
  141. if (iuid != NULL) {
  142. if (BIO_printf(bp, "%12sIssuer UID: ", "") <= 0)
  143. goto err;
  144. if (X509_signature_dump(bp, iuid, 24) <= 0)
  145. goto err;
  146. }
  147. if (BIO_write(bp, "\n", 1) <= 0)
  148. goto err;
  149. }
  150. }
  151. if ((cflag & X509_FLAG_NO_ISSUER) == 0) {
  152. const X509_NAME *issuer;
  153. if (BIO_printf(bp, "%8sIssuer:%c", "", mlch) <= 0)
  154. goto err;
  155. issuer = X509_ACERT_get0_issuerName(x);
  156. if (issuer) {
  157. if (X509_NAME_print_ex(bp, issuer, 0, nmflags) < 0)
  158. goto err;
  159. } else {
  160. if (BIO_printf(bp, "Unsupported Issuer Type") <= 0)
  161. goto err;
  162. }
  163. if (BIO_write(bp, "\n", 1) <= 0)
  164. goto err;
  165. }
  166. if ((cflag & X509_FLAG_NO_VALIDITY) == 0) {
  167. if (BIO_printf(bp, "%8sValidity\n", "") <= 0)
  168. goto err;
  169. if (BIO_printf(bp, "%12sNot Before: ", "") <= 0)
  170. goto err;
  171. if (ASN1_GENERALIZEDTIME_print(bp, X509_ACERT_get0_notBefore(x)) == 0)
  172. goto err;
  173. if (BIO_printf(bp, "\n%12sNot After : ", "") <= 0)
  174. goto err;
  175. if (ASN1_GENERALIZEDTIME_print(bp, X509_ACERT_get0_notAfter(x)) == 0)
  176. goto err;
  177. if (BIO_write(bp, "\n", 1) <= 0)
  178. goto err;
  179. }
  180. if ((cflag & X509_FLAG_NO_ATTRIBUTES) == 0) {
  181. if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
  182. goto err;
  183. if (X509_ACERT_get_attr_count(x) == 0) {
  184. if (BIO_printf(bp, "%12s(none)\n", "") <= 0)
  185. goto err;
  186. } else {
  187. for (i = 0; i < X509_ACERT_get_attr_count(x); i++) {
  188. if (print_attribute(bp, X509_ACERT_get_attr(x, i)) == 0)
  189. goto err;
  190. }
  191. }
  192. }
  193. if ((cflag & X509_FLAG_NO_EXTENSIONS) == 0) {
  194. const STACK_OF(X509_EXTENSION) *exts;
  195. exts = X509_ACERT_get0_extensions(x);
  196. if (exts != NULL) {
  197. if (BIO_printf(bp, "%8sExtensions:\n", "") <= 0)
  198. goto err;
  199. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  200. ASN1_OBJECT *obj;
  201. X509_EXTENSION *ex;
  202. int critical;
  203. ex = sk_X509_EXTENSION_value(exts, i);
  204. if (BIO_printf(bp, "%12s", "") <= 0)
  205. goto err;
  206. obj = X509_EXTENSION_get_object(ex);
  207. if (i2a_ASN1_OBJECT(bp, obj) <= 0)
  208. goto err;
  209. critical = X509_EXTENSION_get_critical(ex);
  210. if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
  211. goto err;
  212. if (X509V3_EXT_print(bp, ex, cflag, 20) <= 0) {
  213. if (BIO_printf(bp, "%16s", "") <= 0)
  214. goto err;
  215. if (ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex)) <= 0)
  216. goto err;
  217. }
  218. if (BIO_write(bp, "\n", 1) <= 0)
  219. goto err;
  220. }
  221. }
  222. }
  223. if ((cflag & X509_FLAG_NO_SIGDUMP) == 0) {
  224. const X509_ALGOR *sig_alg;
  225. const ASN1_BIT_STRING *sig;
  226. X509_ACERT_get0_signature(x, &sig, &sig_alg);
  227. if (X509_signature_print(bp, sig_alg, sig) <= 0)
  228. return 0;
  229. }
  230. return 1;
  231. err:
  232. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  233. return 0;
  234. }
  235. int X509_ACERT_print(BIO *bp, X509_ACERT *x)
  236. {
  237. return X509_ACERT_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  238. }