ocsp_prn.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_lcl.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. };
  76. return table2string(s, reason_tbl);
  77. }
  78. int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
  79. {
  80. int i;
  81. long l;
  82. OCSP_CERTID *cid = NULL;
  83. OCSP_ONEREQ *one = NULL;
  84. OCSP_REQINFO *inf = &o->tbsRequest;
  85. OCSP_SIGNATURE *sig = o->optionalSignature;
  86. if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
  87. goto err;
  88. l = ASN1_INTEGER_get(inf->version);
  89. if (BIO_printf(bp, " Version: %lu (0x%lx)", l + 1, l) <= 0)
  90. goto err;
  91. if (inf->requestorName != NULL) {
  92. if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0)
  93. goto err;
  94. GENERAL_NAME_print(bp, inf->requestorName);
  95. }
  96. if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0)
  97. goto err;
  98. for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
  99. one = sk_OCSP_ONEREQ_value(inf->requestList, i);
  100. cid = one->reqCert;
  101. ocsp_certid_print(bp, cid, 8);
  102. if (!X509V3_extensions_print(bp,
  103. "Request Single Extensions",
  104. one->singleRequestExtensions, flags, 8))
  105. goto err;
  106. }
  107. if (!X509V3_extensions_print(bp, "Request Extensions",
  108. inf->requestExtensions, flags, 4))
  109. goto err;
  110. if (sig) {
  111. X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
  112. for (i = 0; i < sk_X509_num(sig->certs); i++) {
  113. X509_print(bp, sk_X509_value(sig->certs, i));
  114. PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
  115. }
  116. }
  117. return 1;
  118. err:
  119. return 0;
  120. }
  121. int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
  122. {
  123. int i, ret = 0;
  124. long l;
  125. OCSP_CERTID *cid = NULL;
  126. OCSP_BASICRESP *br = NULL;
  127. OCSP_RESPID *rid = NULL;
  128. OCSP_RESPDATA *rd = NULL;
  129. OCSP_CERTSTATUS *cst = NULL;
  130. OCSP_REVOKEDINFO *rev = NULL;
  131. OCSP_SINGLERESP *single = NULL;
  132. OCSP_RESPBYTES *rb = o->responseBytes;
  133. if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
  134. goto err;
  135. l = ASN1_ENUMERATED_get(o->responseStatus);
  136. if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n",
  137. OCSP_response_status_str(l), l) <= 0)
  138. goto err;
  139. if (rb == NULL)
  140. return 1;
  141. if (BIO_puts(bp, " Response Type: ") <= 0)
  142. goto err;
  143. if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
  144. goto err;
  145. if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
  146. BIO_puts(bp, " (unknown response type)\n");
  147. return 1;
  148. }
  149. if ((br = OCSP_response_get1_basic(o)) == NULL)
  150. goto err;
  151. rd = &br->tbsResponseData;
  152. l = ASN1_INTEGER_get(rd->version);
  153. if (BIO_printf(bp, "\n Version: %lu (0x%lx)\n", l + 1, l) <= 0)
  154. goto err;
  155. if (BIO_puts(bp, " Responder Id: ") <= 0)
  156. goto err;
  157. rid = &rd->responderId;
  158. switch (rid->type) {
  159. case V_OCSP_RESPID_NAME:
  160. X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
  161. break;
  162. case V_OCSP_RESPID_KEY:
  163. i2a_ASN1_STRING(bp, rid->value.byKey, 0);
  164. break;
  165. }
  166. if (BIO_printf(bp, "\n Produced At: ") <= 0)
  167. goto err;
  168. if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
  169. goto err;
  170. if (BIO_printf(bp, "\n Responses:\n") <= 0)
  171. goto err;
  172. for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
  173. if (!sk_OCSP_SINGLERESP_value(rd->responses, i))
  174. continue;
  175. single = sk_OCSP_SINGLERESP_value(rd->responses, i);
  176. cid = single->certId;
  177. if (ocsp_certid_print(bp, cid, 4) <= 0)
  178. goto err;
  179. cst = single->certStatus;
  180. if (BIO_printf(bp, " Cert Status: %s",
  181. OCSP_cert_status_str(cst->type)) <= 0)
  182. goto err;
  183. if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
  184. rev = cst->value.revoked;
  185. if (BIO_printf(bp, "\n Revocation Time: ") <= 0)
  186. goto err;
  187. if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime))
  188. goto err;
  189. if (rev->revocationReason) {
  190. l = ASN1_ENUMERATED_get(rev->revocationReason);
  191. if (BIO_printf(bp,
  192. "\n Revocation Reason: %s (0x%lx)",
  193. OCSP_crl_reason_str(l), l) <= 0)
  194. goto err;
  195. }
  196. }
  197. if (BIO_printf(bp, "\n This Update: ") <= 0)
  198. goto err;
  199. if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
  200. goto err;
  201. if (single->nextUpdate) {
  202. if (BIO_printf(bp, "\n Next Update: ") <= 0)
  203. goto err;
  204. if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
  205. goto err;
  206. }
  207. if (BIO_write(bp, "\n", 1) <= 0)
  208. goto err;
  209. if (!X509V3_extensions_print(bp,
  210. "Response Single Extensions",
  211. single->singleExtensions, flags, 8))
  212. goto err;
  213. if (BIO_write(bp, "\n", 1) <= 0)
  214. goto err;
  215. }
  216. if (!X509V3_extensions_print(bp, "Response Extensions",
  217. rd->responseExtensions, flags, 4))
  218. goto err;
  219. if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
  220. goto err;
  221. for (i = 0; i < sk_X509_num(br->certs); i++) {
  222. X509_print(bp, sk_X509_value(br->certs, i));
  223. PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
  224. }
  225. ret = 1;
  226. err:
  227. OCSP_BASICRESP_free(br);
  228. return ret;
  229. }