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)
19 DEFINE_STACK_OF(OCSP_SINGLERESP)
21 static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent)
23 BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
25 BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
26 i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm);
27 BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
28 i2a_ASN1_STRING(bp, &a->issuerNameHash, 0);
29 BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
30 i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0);
31 BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
32 i2a_ASN1_INTEGER(bp, &a->serialNumber);
42 static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len)
45 for (i = 0; i < len; i++, ts++)
51 #define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl))
53 const char *OCSP_response_status_str(long s)
55 static const OCSP_TBLSTR rstat_tbl[] = {
56 {OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"},
57 {OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest"},
58 {OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror"},
59 {OCSP_RESPONSE_STATUS_TRYLATER, "trylater"},
60 {OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired"},
61 {OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized"}
63 return table2string(s, rstat_tbl);
66 const char *OCSP_cert_status_str(long s)
68 static const OCSP_TBLSTR cstat_tbl[] = {
69 {V_OCSP_CERTSTATUS_GOOD, "good"},
70 {V_OCSP_CERTSTATUS_REVOKED, "revoked"},
71 {V_OCSP_CERTSTATUS_UNKNOWN, "unknown"}
73 return table2string(s, cstat_tbl);
76 const char *OCSP_crl_reason_str(long s)
78 static const OCSP_TBLSTR reason_tbl[] = {
79 {OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified"},
80 {OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise"},
81 {OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise"},
82 {OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged"},
83 {OCSP_REVOKED_STATUS_SUPERSEDED, "superseded"},
84 {OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation"},
85 {OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold"},
86 {OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL"}
88 return table2string(s, reason_tbl);
91 int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
95 OCSP_CERTID *cid = NULL;
96 OCSP_ONEREQ *one = NULL;
97 OCSP_REQINFO *inf = &o->tbsRequest;
98 OCSP_SIGNATURE *sig = o->optionalSignature;
100 if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
102 l = ASN1_INTEGER_get(inf->version);
103 if (BIO_printf(bp, " Version: %lu (0x%lx)", l + 1, l) <= 0)
105 if (inf->requestorName != NULL) {
106 if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0)
108 GENERAL_NAME_print(bp, inf->requestorName);
110 if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0)
112 for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
113 one = sk_OCSP_ONEREQ_value(inf->requestList, i);
115 ocsp_certid_print(bp, cid, 8);
116 if (!X509V3_extensions_print(bp,
117 "Request Single Extensions",
118 one->singleRequestExtensions, flags, 8))
121 if (!X509V3_extensions_print(bp, "Request Extensions",
122 inf->requestExtensions, flags, 4))
125 X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
126 for (i = 0; i < sk_X509_num(sig->certs); i++) {
127 X509_print(bp, sk_X509_value(sig->certs, i));
128 PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
136 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
140 OCSP_CERTID *cid = NULL;
141 OCSP_BASICRESP *br = NULL;
142 OCSP_RESPID *rid = NULL;
143 OCSP_RESPDATA *rd = NULL;
144 OCSP_CERTSTATUS *cst = NULL;
145 OCSP_REVOKEDINFO *rev = NULL;
146 OCSP_SINGLERESP *single = NULL;
147 OCSP_RESPBYTES *rb = o->responseBytes;
149 if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
151 l = ASN1_ENUMERATED_get(o->responseStatus);
152 if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n",
153 OCSP_response_status_str(l), l) <= 0)
157 if (BIO_puts(bp, " Response Type: ") <= 0)
159 if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
161 if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
162 BIO_puts(bp, " (unknown response type)\n");
166 if ((br = OCSP_response_get1_basic(o)) == NULL)
168 rd = &br->tbsResponseData;
169 l = ASN1_INTEGER_get(rd->version);
170 if (BIO_printf(bp, "\n Version: %lu (0x%lx)\n", l + 1, l) <= 0)
172 if (BIO_puts(bp, " Responder Id: ") <= 0)
175 rid = &rd->responderId;
177 case V_OCSP_RESPID_NAME:
178 X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
180 case V_OCSP_RESPID_KEY:
181 i2a_ASN1_STRING(bp, rid->value.byKey, 0);
185 if (BIO_printf(bp, "\n Produced At: ") <= 0)
187 if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
189 if (BIO_printf(bp, "\n Responses:\n") <= 0)
191 for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
192 if (!sk_OCSP_SINGLERESP_value(rd->responses, i))
194 single = sk_OCSP_SINGLERESP_value(rd->responses, i);
195 cid = single->certId;
196 if (ocsp_certid_print(bp, cid, 4) <= 0)
198 cst = single->certStatus;
199 if (BIO_printf(bp, " Cert Status: %s",
200 OCSP_cert_status_str(cst->type)) <= 0)
202 if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
203 rev = cst->value.revoked;
204 if (BIO_printf(bp, "\n Revocation Time: ") <= 0)
206 if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime))
208 if (rev->revocationReason) {
209 l = ASN1_ENUMERATED_get(rev->revocationReason);
211 "\n Revocation Reason: %s (0x%lx)",
212 OCSP_crl_reason_str(l), l) <= 0)
216 if (BIO_printf(bp, "\n This Update: ") <= 0)
218 if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
220 if (single->nextUpdate) {
221 if (BIO_printf(bp, "\n Next Update: ") <= 0)
223 if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
226 if (BIO_write(bp, "\n", 1) <= 0)
228 if (!X509V3_extensions_print(bp,
229 "Response Single Extensions",
230 single->singleExtensions, flags, 8))
232 if (BIO_write(bp, "\n", 1) <= 0)
235 if (!X509V3_extensions_print(bp, "Response Extensions",
236 rd->responseExtensions, flags, 4))
238 if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
241 for (i = 0; i < sk_X509_num(br->certs); i++) {
242 X509_print(bp, sk_X509_value(br->certs, i));
243 PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
248 OCSP_BASICRESP_free(br);