ocsp_prn.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2000-2023 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 <openssl/bio.h>
  10. #include <openssl/err.h>
  11. #include <openssl/ocsp.h>
  12. #include "ocsp_local.h"
  13. #include "internal/cryptlib.h"
  14. #include <openssl/pem.h>
  15. static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent)
  16. {
  17. BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
  18. indent += 2;
  19. BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
  20. i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm);
  21. BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
  22. i2a_ASN1_STRING(bp, &a->issuerNameHash, 0);
  23. BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
  24. i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0);
  25. BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
  26. i2a_ASN1_INTEGER(bp, &a->serialNumber);
  27. BIO_printf(bp, "\n");
  28. return 1;
  29. }
  30. typedef struct {
  31. long t;
  32. const char *m;
  33. } OCSP_TBLSTR;
  34. static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len)
  35. {
  36. size_t i;
  37. for (i = 0; i < len; i++, ts++)
  38. if (ts->t == s)
  39. return ts->m;
  40. return "(UNKNOWN)";
  41. }
  42. #define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl))
  43. const char *OCSP_response_status_str(long s)
  44. {
  45. static const OCSP_TBLSTR rstat_tbl[] = {
  46. {OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"},
  47. {OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest"},
  48. {OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror"},
  49. {OCSP_RESPONSE_STATUS_TRYLATER, "trylater"},
  50. {OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired"},
  51. {OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized"}
  52. };
  53. return table2string(s, rstat_tbl);
  54. }
  55. const char *OCSP_cert_status_str(long s)
  56. {
  57. static const OCSP_TBLSTR cstat_tbl[] = {
  58. {V_OCSP_CERTSTATUS_GOOD, "good"},
  59. {V_OCSP_CERTSTATUS_REVOKED, "revoked"},
  60. {V_OCSP_CERTSTATUS_UNKNOWN, "unknown"}
  61. };
  62. return table2string(s, cstat_tbl);
  63. }
  64. const char *OCSP_crl_reason_str(long s)
  65. {
  66. static const OCSP_TBLSTR reason_tbl[] = {
  67. {OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified"},
  68. {OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise"},
  69. {OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise"},
  70. {OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged"},
  71. {OCSP_REVOKED_STATUS_SUPERSEDED, "superseded"},
  72. {OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation"},
  73. {OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold"},
  74. {OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL"},
  75. {OCSP_REVOKED_STATUS_PRIVILEGEWITHDRAWN, "privilegeWithdrawn"},
  76. {OCSP_REVOKED_STATUS_AACOMPROMISE, "aACompromise"}
  77. };
  78. return table2string(s, reason_tbl);
  79. }
  80. int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
  81. {
  82. int i;
  83. long l;
  84. OCSP_CERTID *cid = NULL;
  85. OCSP_ONEREQ *one = NULL;
  86. OCSP_REQINFO *inf = &o->tbsRequest;
  87. OCSP_SIGNATURE *sig = o->optionalSignature;
  88. if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
  89. goto err;
  90. l = ASN1_INTEGER_get(inf->version);
  91. if (BIO_printf(bp, " Version: %lu (0x%lx)", l + 1, l) <= 0)
  92. goto err;
  93. if (inf->requestorName != NULL) {
  94. if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0)
  95. goto err;
  96. GENERAL_NAME_print(bp, inf->requestorName);
  97. }
  98. if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0)
  99. goto err;
  100. for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
  101. one = sk_OCSP_ONEREQ_value(inf->requestList, i);
  102. cid = one->reqCert;
  103. ocsp_certid_print(bp, cid, 8);
  104. if (!X509V3_extensions_print(bp,
  105. "Request Single Extensions",
  106. one->singleRequestExtensions, flags, 8))
  107. goto err;
  108. }
  109. if (!X509V3_extensions_print(bp, "Request Extensions",
  110. inf->requestExtensions, flags, 4))
  111. goto err;
  112. if (sig) {
  113. X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
  114. for (i = 0; i < sk_X509_num(sig->certs); i++) {
  115. X509_print(bp, sk_X509_value(sig->certs, i));
  116. PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
  117. }
  118. }
  119. return 1;
  120. err:
  121. return 0;
  122. }
  123. int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
  124. {
  125. int i, ret = 0;
  126. long l;
  127. OCSP_CERTID *cid = NULL;
  128. OCSP_BASICRESP *br = NULL;
  129. OCSP_RESPID *rid = NULL;
  130. OCSP_RESPDATA *rd = NULL;
  131. OCSP_CERTSTATUS *cst = NULL;
  132. OCSP_REVOKEDINFO *rev = NULL;
  133. OCSP_SINGLERESP *single = NULL;
  134. OCSP_RESPBYTES *rb = o->responseBytes;
  135. if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
  136. goto err;
  137. l = ASN1_ENUMERATED_get(o->responseStatus);
  138. if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n",
  139. OCSP_response_status_str(l), l) <= 0)
  140. goto err;
  141. if (rb == NULL)
  142. return 1;
  143. if (BIO_puts(bp, " Response Type: ") <= 0)
  144. goto err;
  145. if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
  146. goto err;
  147. if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
  148. BIO_puts(bp, " (unknown response type)\n");
  149. return 1;
  150. }
  151. if ((br = OCSP_response_get1_basic(o)) == NULL)
  152. goto err;
  153. rd = &br->tbsResponseData;
  154. l = ASN1_INTEGER_get(rd->version);
  155. if (BIO_printf(bp, "\n Version: %lu (0x%lx)\n", l + 1, l) <= 0)
  156. goto err;
  157. if (BIO_puts(bp, " Responder Id: ") <= 0)
  158. goto err;
  159. rid = &rd->responderId;
  160. switch (rid->type) {
  161. case V_OCSP_RESPID_NAME:
  162. X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
  163. break;
  164. case V_OCSP_RESPID_KEY:
  165. i2a_ASN1_STRING(bp, rid->value.byKey, 0);
  166. break;
  167. }
  168. if (BIO_printf(bp, "\n Produced At: ") <= 0)
  169. goto err;
  170. if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
  171. goto err;
  172. if (BIO_printf(bp, "\n Responses:\n") <= 0)
  173. goto err;
  174. for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
  175. if (!sk_OCSP_SINGLERESP_value(rd->responses, i))
  176. continue;
  177. single = sk_OCSP_SINGLERESP_value(rd->responses, i);
  178. cid = single->certId;
  179. if (ocsp_certid_print(bp, cid, 4) <= 0)
  180. goto err;
  181. cst = single->certStatus;
  182. if (BIO_printf(bp, " Cert Status: %s",
  183. OCSP_cert_status_str(cst->type)) <= 0)
  184. goto err;
  185. if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
  186. rev = cst->value.revoked;
  187. if (BIO_printf(bp, "\n Revocation Time: ") <= 0)
  188. goto err;
  189. if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime))
  190. goto err;
  191. if (rev->revocationReason) {
  192. l = ASN1_ENUMERATED_get(rev->revocationReason);
  193. if (BIO_printf(bp,
  194. "\n Revocation Reason: %s (0x%lx)",
  195. OCSP_crl_reason_str(l), l) <= 0)
  196. goto err;
  197. }
  198. }
  199. if (BIO_printf(bp, "\n This Update: ") <= 0)
  200. goto err;
  201. if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
  202. goto err;
  203. if (single->nextUpdate) {
  204. if (BIO_printf(bp, "\n Next Update: ") <= 0)
  205. goto err;
  206. if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
  207. goto err;
  208. }
  209. if (BIO_write(bp, "\n", 1) <= 0)
  210. goto err;
  211. if (!X509V3_extensions_print(bp,
  212. "Response Single Extensions",
  213. single->singleExtensions, flags, 8))
  214. goto err;
  215. if (BIO_write(bp, "\n", 1) <= 0)
  216. goto err;
  217. }
  218. if (!X509V3_extensions_print(bp, "Response Extensions",
  219. rd->responseExtensions, flags, 4))
  220. goto err;
  221. if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
  222. goto err;
  223. for (i = 0; i < sk_X509_num(br->certs); i++) {
  224. X509_print(bp, sk_X509_value(br->certs, i));
  225. PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
  226. }
  227. ret = 1;
  228. err:
  229. OCSP_BASICRESP_free(br);
  230. return ret;
  231. }