2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/bio.h>
11 #include <openssl/err.h>
12 #include <openssl/ocsp.h>
13 #include "ocsp_local.h"
14 #include "internal/cryptlib.h"
15 #include <openssl/pem.h>
17 DEFINE_STACK_OF(OCSP_ONEREQ)
18 DEFINE_STACK_OF(OCSP_SINGLERESP)
20 static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent)
22 BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
24 BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
25 i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm);
26 BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
27 i2a_ASN1_STRING(bp, &a->issuerNameHash, 0);
28 BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
29 i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0);
30 BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
31 i2a_ASN1_INTEGER(bp, &a->serialNumber);
41 static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len)
44 for (i = 0; i < len; i++, ts++)
50 #define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl))
52 const char *OCSP_response_status_str(long s)
54 static const OCSP_TBLSTR rstat_tbl[] = {
55 {OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"},
56 {OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest"},
57 {OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror"},
58 {OCSP_RESPONSE_STATUS_TRYLATER, "trylater"},
59 {OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired"},
60 {OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized"}
62 return table2string(s, rstat_tbl);
65 const char *OCSP_cert_status_str(long s)
67 static const OCSP_TBLSTR cstat_tbl[] = {
68 {V_OCSP_CERTSTATUS_GOOD, "good"},
69 {V_OCSP_CERTSTATUS_REVOKED, "revoked"},
70 {V_OCSP_CERTSTATUS_UNKNOWN, "unknown"}
72 return table2string(s, cstat_tbl);
75 const char *OCSP_crl_reason_str(long s)
77 static const OCSP_TBLSTR reason_tbl[] = {
78 {OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified"},
79 {OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise"},
80 {OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise"},
81 {OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged"},
82 {OCSP_REVOKED_STATUS_SUPERSEDED, "superseded"},
83 {OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation"},
84 {OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold"},
85 {OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL"}
87 return table2string(s, reason_tbl);
90 int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
94 OCSP_CERTID *cid = NULL;
95 OCSP_ONEREQ *one = NULL;
96 OCSP_REQINFO *inf = &o->tbsRequest;
97 OCSP_SIGNATURE *sig = o->optionalSignature;
99 if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
101 l = ASN1_INTEGER_get(inf->version);
102 if (BIO_printf(bp, " Version: %lu (0x%lx)", l + 1, l) <= 0)
104 if (inf->requestorName != NULL) {
105 if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0)
107 GENERAL_NAME_print(bp, inf->requestorName);
109 if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0)
111 for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
112 one = sk_OCSP_ONEREQ_value(inf->requestList, i);
114 ocsp_certid_print(bp, cid, 8);
115 if (!X509V3_extensions_print(bp,
116 "Request Single Extensions",
117 one->singleRequestExtensions, flags, 8))
120 if (!X509V3_extensions_print(bp, "Request Extensions",
121 inf->requestExtensions, flags, 4))
124 X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
125 for (i = 0; i < sk_X509_num(sig->certs); i++) {
126 X509_print(bp, sk_X509_value(sig->certs, i));
127 PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
135 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
139 OCSP_CERTID *cid = NULL;
140 OCSP_BASICRESP *br = NULL;
141 OCSP_RESPID *rid = NULL;
142 OCSP_RESPDATA *rd = NULL;
143 OCSP_CERTSTATUS *cst = NULL;
144 OCSP_REVOKEDINFO *rev = NULL;
145 OCSP_SINGLERESP *single = NULL;
146 OCSP_RESPBYTES *rb = o->responseBytes;
148 if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
150 l = ASN1_ENUMERATED_get(o->responseStatus);
151 if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n",
152 OCSP_response_status_str(l), l) <= 0)
156 if (BIO_puts(bp, " Response Type: ") <= 0)
158 if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
160 if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
161 BIO_puts(bp, " (unknown response type)\n");
165 if ((br = OCSP_response_get1_basic(o)) == NULL)
167 rd = &br->tbsResponseData;
168 l = ASN1_INTEGER_get(rd->version);
169 if (BIO_printf(bp, "\n Version: %lu (0x%lx)\n", l + 1, l) <= 0)
171 if (BIO_puts(bp, " Responder Id: ") <= 0)
174 rid = &rd->responderId;
176 case V_OCSP_RESPID_NAME:
177 X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
179 case V_OCSP_RESPID_KEY:
180 i2a_ASN1_STRING(bp, rid->value.byKey, 0);
184 if (BIO_printf(bp, "\n Produced At: ") <= 0)
186 if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
188 if (BIO_printf(bp, "\n Responses:\n") <= 0)
190 for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
191 if (!sk_OCSP_SINGLERESP_value(rd->responses, i))
193 single = sk_OCSP_SINGLERESP_value(rd->responses, i);
194 cid = single->certId;
195 if (ocsp_certid_print(bp, cid, 4) <= 0)
197 cst = single->certStatus;
198 if (BIO_printf(bp, " Cert Status: %s",
199 OCSP_cert_status_str(cst->type)) <= 0)
201 if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
202 rev = cst->value.revoked;
203 if (BIO_printf(bp, "\n Revocation Time: ") <= 0)
205 if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime))
207 if (rev->revocationReason) {
208 l = ASN1_ENUMERATED_get(rev->revocationReason);
210 "\n Revocation Reason: %s (0x%lx)",
211 OCSP_crl_reason_str(l), l) <= 0)
215 if (BIO_printf(bp, "\n This Update: ") <= 0)
217 if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
219 if (single->nextUpdate) {
220 if (BIO_printf(bp, "\n Next Update: ") <= 0)
222 if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
225 if (BIO_write(bp, "\n", 1) <= 0)
227 if (!X509V3_extensions_print(bp,
228 "Response Single Extensions",
229 single->singleExtensions, flags, 8))
231 if (BIO_write(bp, "\n", 1) <= 0)
234 if (!X509V3_extensions_print(bp, "Response Extensions",
235 rd->responseExtensions, flags, 4))
237 if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
240 for (i = 0; i < sk_X509_num(br->certs); i++) {
241 X509_print(bp, sk_X509_value(br->certs, i));
242 PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
247 OCSP_BASICRESP_free(br);