1 /* $OpenBSD: ocsp_prn.c,v 1.9 2022/01/07 09:45:52 tb Exp $ */ 2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL 3 * project. */ 4 5 /* History: 6 This file was originally part of ocsp.c and was transfered to Richard 7 Levitte from CertCo by Kathy Weinhold in mid-spring 2000 to be included 8 in OpenSSL or released as a patch kit. */ 9 10 /* ==================================================================== 11 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. All advertising materials mentioning features or use of this 26 * software must display the following acknowledgment: 27 * "This product includes software developed by the OpenSSL Project 28 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 29 * 30 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 31 * endorse or promote products derived from this software without 32 * prior written permission. For written permission, please contact 33 * openssl-core@openssl.org. 34 * 35 * 5. Products derived from this software may not be called "OpenSSL" 36 * nor may "OpenSSL" appear in their names without prior written 37 * permission of the OpenSSL Project. 38 * 39 * 6. Redistributions of any form whatsoever must retain the following 40 * acknowledgment: 41 * "This product includes software developed by the OpenSSL Project 42 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 45 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 47 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 48 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 49 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 53 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 55 * OF THE POSSIBILITY OF SUCH DAMAGE. 56 * ==================================================================== 57 * 58 * This product includes cryptographic software written by Eric Young 59 * (eay@cryptsoft.com). This product includes software written by Tim 60 * Hudson (tjh@cryptsoft.com). 61 * 62 */ 63 64 #include <openssl/bio.h> 65 #include <openssl/err.h> 66 #include <openssl/ocsp.h> 67 #include <openssl/pem.h> 68 69 #include "ocsp_local.h" 70 71 static int 72 ocsp_certid_print(BIO *bp, OCSP_CERTID* a, int indent) 73 { 74 BIO_printf(bp, "%*sCertificate ID:\n", indent, ""); 75 indent += 2; 76 BIO_printf(bp, "%*sHash Algorithm: ", indent, ""); 77 i2a_ASN1_OBJECT(bp, a->hashAlgorithm->algorithm); 78 BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, ""); 79 i2a_ASN1_STRING(bp, a->issuerNameHash, V_ASN1_OCTET_STRING); 80 BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, ""); 81 i2a_ASN1_STRING(bp, a->issuerKeyHash, V_ASN1_OCTET_STRING); 82 BIO_printf(bp, "\n%*sSerial Number: ", indent, ""); 83 i2a_ASN1_INTEGER(bp, a->serialNumber); 84 BIO_printf(bp, "\n"); 85 return 1; 86 } 87 88 typedef struct { 89 long t; 90 const char *m; 91 } OCSP_TBLSTR; 92 93 static const char * 94 table2string(long s, const OCSP_TBLSTR *ts, int len) 95 { 96 const OCSP_TBLSTR *p; 97 98 for (p = ts; p < ts + len; p++) 99 if (p->t == s) 100 return p->m; 101 return "(UNKNOWN)"; 102 } 103 104 const char * 105 OCSP_response_status_str(long s) 106 { 107 static const OCSP_TBLSTR rstat_tbl[] = { 108 { OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" }, 109 { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" }, 110 { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" }, 111 { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" }, 112 { OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" }, 113 { OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" } 114 }; 115 return table2string(s, rstat_tbl, 6); 116 } 117 118 const char * 119 OCSP_cert_status_str(long s) 120 { 121 static const OCSP_TBLSTR cstat_tbl[] = { 122 { V_OCSP_CERTSTATUS_GOOD, "good" }, 123 { V_OCSP_CERTSTATUS_REVOKED, "revoked" }, 124 { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" } 125 }; 126 return table2string(s, cstat_tbl, 3); 127 } 128 129 const char * 130 OCSP_crl_reason_str(long s) 131 { 132 static const OCSP_TBLSTR reason_tbl[] = { 133 { OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" }, 134 { OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" }, 135 { OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" }, 136 { OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged" }, 137 { OCSP_REVOKED_STATUS_SUPERSEDED, "superseded" }, 138 { OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation" }, 139 { OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold" }, 140 { OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL" } 141 }; 142 return table2string(s, reason_tbl, 8); 143 } 144 145 int 146 OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST* o, unsigned long flags) 147 { 148 int i; 149 long l; 150 OCSP_CERTID* cid = NULL; 151 OCSP_ONEREQ *one = NULL; 152 OCSP_REQINFO *inf = o->tbsRequest; 153 OCSP_SIGNATURE *sig = o->optionalSignature; 154 155 if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0) 156 goto err; 157 l = ASN1_INTEGER_get(inf->version); 158 if (BIO_printf(bp, " Version: %lu (0x%lx)", l+1, l) <= 0) 159 goto err; 160 if (inf->requestorName != NULL) { 161 if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0) 162 goto err; 163 GENERAL_NAME_print(bp, inf->requestorName); 164 } 165 if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0) 166 goto err; 167 for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) { 168 one = sk_OCSP_ONEREQ_value(inf->requestList, i); 169 cid = one->reqCert; 170 ocsp_certid_print(bp, cid, 8); 171 if (!X509V3_extensions_print(bp, "Request Single Extensions", 172 one->singleRequestExtensions, flags, 8)) 173 goto err; 174 } 175 if (!X509V3_extensions_print(bp, "Request Extensions", 176 inf->requestExtensions, flags, 4)) 177 goto err; 178 if (sig) { 179 if (X509_signature_print(bp, sig->signatureAlgorithm, 180 sig->signature) == 0) 181 goto err; 182 for (i = 0; i < sk_X509_num(sig->certs); i++) { 183 if (X509_print(bp, sk_X509_value(sig->certs, i)) == 0) 184 goto err; 185 if (PEM_write_bio_X509(bp, 186 sk_X509_value(sig->certs, i)) == 0) 187 goto err; 188 } 189 } 190 return 1; 191 192 err: 193 return 0; 194 } 195 196 int 197 OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags) 198 { 199 int i, ret = 0; 200 long l; 201 OCSP_CERTID *cid = NULL; 202 OCSP_BASICRESP *br = NULL; 203 OCSP_RESPID *rid = NULL; 204 OCSP_RESPDATA *rd = NULL; 205 OCSP_CERTSTATUS *cst = NULL; 206 OCSP_REVOKEDINFO *rev = NULL; 207 OCSP_SINGLERESP *single = NULL; 208 OCSP_RESPBYTES *rb = o->responseBytes; 209 210 if (BIO_puts(bp, "OCSP Response Data:\n") <= 0) 211 goto err; 212 l = ASN1_ENUMERATED_get(o->responseStatus); 213 if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n", 214 OCSP_response_status_str(l), l) <= 0) 215 goto err; 216 if (rb == NULL) 217 return 1; 218 if (BIO_puts(bp, " Response Type: ") <= 0) 219 goto err; 220 if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0) 221 goto err; 222 if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) { 223 BIO_puts(bp, " (unknown response type)\n"); 224 return 1; 225 } 226 227 i = ASN1_STRING_length(rb->response); 228 if (!(br = OCSP_response_get1_basic(o))) 229 goto err; 230 rd = br->tbsResponseData; 231 l = ASN1_INTEGER_get(rd->version); 232 if (BIO_printf(bp, "\n Version: %lu (0x%lx)\n", l+1, l) <= 0) 233 goto err; 234 if (BIO_puts(bp, " Responder Id: ") <= 0) 235 goto err; 236 237 rid = rd->responderId; 238 switch (rid->type) { 239 case V_OCSP_RESPID_NAME: 240 X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE); 241 break; 242 case V_OCSP_RESPID_KEY: 243 i2a_ASN1_STRING(bp, rid->value.byKey, V_ASN1_OCTET_STRING); 244 break; 245 } 246 247 if (BIO_printf(bp, "\n Produced At: ")<=0) 248 goto err; 249 if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt)) 250 goto err; 251 if (BIO_printf(bp, "\n Responses:\n") <= 0) 252 goto err; 253 for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) { 254 if (! sk_OCSP_SINGLERESP_value(rd->responses, i)) 255 continue; 256 single = sk_OCSP_SINGLERESP_value(rd->responses, i); 257 cid = single->certId; 258 if (ocsp_certid_print(bp, cid, 4) <= 0) 259 goto err; 260 cst = single->certStatus; 261 if (BIO_printf(bp, " Cert Status: %s", 262 OCSP_cert_status_str(cst->type)) <= 0) 263 goto err; 264 if (cst->type == V_OCSP_CERTSTATUS_REVOKED) { 265 rev = cst->value.revoked; 266 if (BIO_printf(bp, "\n Revocation Time: ") <= 0) 267 goto err; 268 if (!ASN1_GENERALIZEDTIME_print(bp, 269 rev->revocationTime)) 270 goto err; 271 if (rev->revocationReason) { 272 l = ASN1_ENUMERATED_get(rev->revocationReason); 273 if (BIO_printf(bp, 274 "\n Revocation Reason: %s (0x%lx)", 275 OCSP_crl_reason_str(l), l) <= 0) 276 goto err; 277 } 278 } 279 if (BIO_printf(bp, "\n This Update: ") <= 0) 280 goto err; 281 if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate)) 282 goto err; 283 if (single->nextUpdate) { 284 if (BIO_printf(bp, "\n Next Update: ") <= 0) 285 goto err; 286 if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate)) 287 goto err; 288 } 289 if (BIO_write(bp, "\n", 1) <= 0) 290 goto err; 291 if (!X509V3_extensions_print(bp, "Response Single Extensions", 292 single->singleExtensions, flags, 8)) 293 goto err; 294 if (BIO_write(bp, "\n", 1) <= 0) 295 goto err; 296 } 297 if (!X509V3_extensions_print(bp, "Response Extensions", 298 rd->responseExtensions, flags, 4)) 299 goto err; 300 if (X509_signature_print(bp, br->signatureAlgorithm, br->signature) <= 301 0) 302 goto err; 303 304 for (i = 0; i < sk_X509_num(br->certs); i++) { 305 X509_print(bp, sk_X509_value(br->certs, i)); 306 PEM_write_bio_X509(bp, sk_X509_value(br->certs, i)); 307 } 308 309 ret = 1; 310 311 err: 312 OCSP_BASICRESP_free(br); 313 return ret; 314 } 315