10Sstevel@tonic-gate /* ocsp_prn.c */
20Sstevel@tonic-gate /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
30Sstevel@tonic-gate * project. */
40Sstevel@tonic-gate
50Sstevel@tonic-gate /* History:
60Sstevel@tonic-gate This file was originally part of ocsp.c and was transfered to Richard
70Sstevel@tonic-gate Levitte from CertCo by Kathy Weinhold in mid-spring 2000 to be included
80Sstevel@tonic-gate in OpenSSL or released as a patch kit. */
90Sstevel@tonic-gate
100Sstevel@tonic-gate /* ====================================================================
110Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
140Sstevel@tonic-gate * modification, are permitted provided that the following conditions
150Sstevel@tonic-gate * are met:
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
180Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
210Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
220Sstevel@tonic-gate * the documentation and/or other materials provided with the
230Sstevel@tonic-gate * distribution.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
260Sstevel@tonic-gate * software must display the following acknowledgment:
270Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
280Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
310Sstevel@tonic-gate * endorse or promote products derived from this software without
320Sstevel@tonic-gate * prior written permission. For written permission, please contact
330Sstevel@tonic-gate * openssl-core@openssl.org.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
360Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
370Sstevel@tonic-gate * permission of the OpenSSL Project.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
400Sstevel@tonic-gate * acknowledgment:
410Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
420Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
450Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
470Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
480Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
490Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
500Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
510Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
520Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
530Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
540Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
550Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
560Sstevel@tonic-gate * ====================================================================
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
590Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
600Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
610Sstevel@tonic-gate *
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate #include <openssl/bio.h>
650Sstevel@tonic-gate #include <openssl/err.h>
660Sstevel@tonic-gate #include <openssl/ocsp.h>
670Sstevel@tonic-gate #include <openssl/pem.h>
680Sstevel@tonic-gate
ocsp_certid_print(BIO * bp,OCSP_CERTID * a,int indent)690Sstevel@tonic-gate static int ocsp_certid_print(BIO *bp, OCSP_CERTID* a, int indent)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
720Sstevel@tonic-gate indent += 2;
730Sstevel@tonic-gate BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
740Sstevel@tonic-gate i2a_ASN1_OBJECT(bp, a->hashAlgorithm->algorithm);
750Sstevel@tonic-gate BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
760Sstevel@tonic-gate i2a_ASN1_STRING(bp, a->issuerNameHash, V_ASN1_OCTET_STRING);
770Sstevel@tonic-gate BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
780Sstevel@tonic-gate i2a_ASN1_STRING(bp, a->issuerKeyHash, V_ASN1_OCTET_STRING);
790Sstevel@tonic-gate BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
800Sstevel@tonic-gate i2a_ASN1_INTEGER(bp, a->serialNumber);
810Sstevel@tonic-gate BIO_printf(bp, "\n");
820Sstevel@tonic-gate return 1;
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate typedef struct
860Sstevel@tonic-gate {
870Sstevel@tonic-gate long t;
880Sstevel@tonic-gate char *m;
890Sstevel@tonic-gate } OCSP_TBLSTR;
900Sstevel@tonic-gate
table2string(long s,OCSP_TBLSTR * ts,int len)910Sstevel@tonic-gate static char *table2string(long s, OCSP_TBLSTR *ts, int len)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate OCSP_TBLSTR *p;
940Sstevel@tonic-gate for (p=ts; p < ts + len; p++)
950Sstevel@tonic-gate if (p->t == s)
960Sstevel@tonic-gate return p->m;
970Sstevel@tonic-gate return "(UNKNOWN)";
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
OCSP_response_status_str(long s)1000Sstevel@tonic-gate char *OCSP_response_status_str(long s)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate static OCSP_TBLSTR rstat_tbl[] = {
1030Sstevel@tonic-gate { OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" },
1040Sstevel@tonic-gate { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
1050Sstevel@tonic-gate { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
1060Sstevel@tonic-gate { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
1070Sstevel@tonic-gate { OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" },
1080Sstevel@tonic-gate { OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" } };
1090Sstevel@tonic-gate return table2string(s, rstat_tbl, 6);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
OCSP_cert_status_str(long s)1120Sstevel@tonic-gate char *OCSP_cert_status_str(long s)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate static OCSP_TBLSTR cstat_tbl[] = {
1150Sstevel@tonic-gate { V_OCSP_CERTSTATUS_GOOD, "good" },
1160Sstevel@tonic-gate { V_OCSP_CERTSTATUS_REVOKED, "revoked" },
1170Sstevel@tonic-gate { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" } };
1180Sstevel@tonic-gate return table2string(s, cstat_tbl, 3);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
OCSP_crl_reason_str(long s)1210Sstevel@tonic-gate char *OCSP_crl_reason_str(long s)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate OCSP_TBLSTR reason_tbl[] = {
1240Sstevel@tonic-gate { OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" },
1250Sstevel@tonic-gate { OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" },
1260Sstevel@tonic-gate { OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" },
1270Sstevel@tonic-gate { OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged" },
1280Sstevel@tonic-gate { OCSP_REVOKED_STATUS_SUPERSEDED, "superseded" },
1290Sstevel@tonic-gate { OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation" },
1300Sstevel@tonic-gate { OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold" },
1310Sstevel@tonic-gate { OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL" } };
1320Sstevel@tonic-gate return table2string(s, reason_tbl, 8);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
OCSP_REQUEST_print(BIO * bp,OCSP_REQUEST * o,unsigned long flags)1350Sstevel@tonic-gate int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST* o, unsigned long flags)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate int i;
1380Sstevel@tonic-gate long l;
1390Sstevel@tonic-gate OCSP_CERTID* cid = NULL;
1400Sstevel@tonic-gate OCSP_ONEREQ *one = NULL;
1410Sstevel@tonic-gate OCSP_REQINFO *inf = o->tbsRequest;
1420Sstevel@tonic-gate OCSP_SIGNATURE *sig = o->optionalSignature;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (BIO_write(bp,"OCSP Request Data:\n",19) <= 0) goto err;
1450Sstevel@tonic-gate l=ASN1_INTEGER_get(inf->version);
1460Sstevel@tonic-gate if (BIO_printf(bp," Version: %lu (0x%lx)",l+1,l) <= 0) goto err;
1470Sstevel@tonic-gate if (inf->requestorName != NULL)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate if (BIO_write(bp,"\n Requestor Name: ",21) <= 0)
1500Sstevel@tonic-gate goto err;
1510Sstevel@tonic-gate GENERAL_NAME_print(bp, inf->requestorName);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate if (BIO_write(bp,"\n Requestor List:\n",21) <= 0) goto err;
1540Sstevel@tonic-gate for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate one = sk_OCSP_ONEREQ_value(inf->requestList, i);
1570Sstevel@tonic-gate cid = one->reqCert;
1580Sstevel@tonic-gate ocsp_certid_print(bp, cid, 8);
1590Sstevel@tonic-gate if (!X509V3_extensions_print(bp,
1600Sstevel@tonic-gate "Request Single Extensions",
1610Sstevel@tonic-gate one->singleRequestExtensions, flags, 8))
1620Sstevel@tonic-gate goto err;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate if (!X509V3_extensions_print(bp, "Request Extensions",
1650Sstevel@tonic-gate inf->requestExtensions, flags, 4))
1660Sstevel@tonic-gate goto err;
1670Sstevel@tonic-gate if (sig)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate X509_signature_print(bp, sig->signatureAlgorithm, sig->signature);
1700Sstevel@tonic-gate for (i=0; i<sk_X509_num(sig->certs); i++)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate X509_print(bp, sk_X509_value(sig->certs,i));
1730Sstevel@tonic-gate PEM_write_bio_X509(bp,sk_X509_value(sig->certs,i));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate return 1;
1770Sstevel@tonic-gate err:
1780Sstevel@tonic-gate return 0;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
OCSP_RESPONSE_print(BIO * bp,OCSP_RESPONSE * o,unsigned long flags)1810Sstevel@tonic-gate int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate int i, ret = 0;
1840Sstevel@tonic-gate long l;
1850Sstevel@tonic-gate unsigned char *p;
1860Sstevel@tonic-gate OCSP_CERTID *cid = NULL;
1870Sstevel@tonic-gate OCSP_BASICRESP *br = NULL;
1880Sstevel@tonic-gate OCSP_RESPID *rid = NULL;
1890Sstevel@tonic-gate OCSP_RESPDATA *rd = NULL;
1900Sstevel@tonic-gate OCSP_CERTSTATUS *cst = NULL;
1910Sstevel@tonic-gate OCSP_REVOKEDINFO *rev = NULL;
1920Sstevel@tonic-gate OCSP_SINGLERESP *single = NULL;
1930Sstevel@tonic-gate OCSP_RESPBYTES *rb = o->responseBytes;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if (BIO_puts(bp,"OCSP Response Data:\n") <= 0) goto err;
1960Sstevel@tonic-gate l=ASN1_ENUMERATED_get(o->responseStatus);
197*2139Sjp161948 if (BIO_printf(bp," OCSP Response Status: %s (0x%lx)\n",
1980Sstevel@tonic-gate OCSP_response_status_str(l), l) <= 0) goto err;
1990Sstevel@tonic-gate if (rb == NULL) return 1;
2000Sstevel@tonic-gate if (BIO_puts(bp," Response Type: ") <= 0)
2010Sstevel@tonic-gate goto err;
2020Sstevel@tonic-gate if(i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
2030Sstevel@tonic-gate goto err;
2040Sstevel@tonic-gate if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate BIO_puts(bp," (unknown response type)\n");
2070Sstevel@tonic-gate return 1;
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate p = ASN1_STRING_data(rb->response);
2110Sstevel@tonic-gate i = ASN1_STRING_length(rb->response);
2120Sstevel@tonic-gate if (!(br = OCSP_response_get1_basic(o))) goto err;
2130Sstevel@tonic-gate rd = br->tbsResponseData;
2140Sstevel@tonic-gate l=ASN1_INTEGER_get(rd->version);
2150Sstevel@tonic-gate if (BIO_printf(bp,"\n Version: %lu (0x%lx)\n",
2160Sstevel@tonic-gate l+1,l) <= 0) goto err;
2170Sstevel@tonic-gate if (BIO_puts(bp," Responder Id: ") <= 0) goto err;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate rid = rd->responderId;
2200Sstevel@tonic-gate switch (rid->type)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate case V_OCSP_RESPID_NAME:
2230Sstevel@tonic-gate X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
2240Sstevel@tonic-gate break;
2250Sstevel@tonic-gate case V_OCSP_RESPID_KEY:
2260Sstevel@tonic-gate i2a_ASN1_STRING(bp, rid->value.byKey, V_ASN1_OCTET_STRING);
2270Sstevel@tonic-gate break;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate if (BIO_printf(bp,"\n Produced At: ")<=0) goto err;
2310Sstevel@tonic-gate if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt)) goto err;
2320Sstevel@tonic-gate if (BIO_printf(bp,"\n Responses:\n") <= 0) goto err;
2330Sstevel@tonic-gate for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate if (! sk_OCSP_SINGLERESP_value(rd->responses, i)) continue;
2360Sstevel@tonic-gate single = sk_OCSP_SINGLERESP_value(rd->responses, i);
2370Sstevel@tonic-gate cid = single->certId;
2380Sstevel@tonic-gate if(ocsp_certid_print(bp, cid, 4) <= 0) goto err;
2390Sstevel@tonic-gate cst = single->certStatus;
2400Sstevel@tonic-gate if (BIO_printf(bp," Cert Status: %s",
2410Sstevel@tonic-gate OCSP_cert_status_str(cst->type)) <= 0)
2420Sstevel@tonic-gate goto err;
2430Sstevel@tonic-gate if (cst->type == V_OCSP_CERTSTATUS_REVOKED)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate rev = cst->value.revoked;
2460Sstevel@tonic-gate if (BIO_printf(bp, "\n Revocation Time: ") <= 0)
2470Sstevel@tonic-gate goto err;
2480Sstevel@tonic-gate if (!ASN1_GENERALIZEDTIME_print(bp,
2490Sstevel@tonic-gate rev->revocationTime))
2500Sstevel@tonic-gate goto err;
2510Sstevel@tonic-gate if (rev->revocationReason)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate l=ASN1_ENUMERATED_get(rev->revocationReason);
2540Sstevel@tonic-gate if (BIO_printf(bp,
255*2139Sjp161948 "\n Revocation Reason: %s (0x%lx)",
2560Sstevel@tonic-gate OCSP_crl_reason_str(l), l) <= 0)
2570Sstevel@tonic-gate goto err;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate if (BIO_printf(bp,"\n This Update: ") <= 0) goto err;
2610Sstevel@tonic-gate if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
2620Sstevel@tonic-gate goto err;
2630Sstevel@tonic-gate if (single->nextUpdate)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate if (BIO_printf(bp,"\n Next Update: ") <= 0)goto err;
2660Sstevel@tonic-gate if (!ASN1_GENERALIZEDTIME_print(bp,single->nextUpdate))
2670Sstevel@tonic-gate goto err;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate if (!BIO_write(bp,"\n",1)) goto err;
2700Sstevel@tonic-gate if (!X509V3_extensions_print(bp,
2710Sstevel@tonic-gate "Response Single Extensions",
2720Sstevel@tonic-gate single->singleExtensions, flags, 8))
2730Sstevel@tonic-gate goto err;
2740Sstevel@tonic-gate if (!BIO_write(bp,"\n",1)) goto err;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate if (!X509V3_extensions_print(bp, "Response Extensions",
2770Sstevel@tonic-gate rd->responseExtensions, flags, 4))
2780Sstevel@tonic-gate if(X509_signature_print(bp, br->signatureAlgorithm, br->signature) <= 0)
2790Sstevel@tonic-gate goto err;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate for (i=0; i<sk_X509_num(br->certs); i++)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate X509_print(bp, sk_X509_value(br->certs,i));
2840Sstevel@tonic-gate PEM_write_bio_X509(bp,sk_X509_value(br->certs,i));
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate ret = 1;
2880Sstevel@tonic-gate err:
2890Sstevel@tonic-gate OCSP_BASICRESP_free(br);
2900Sstevel@tonic-gate return ret;
2910Sstevel@tonic-gate }
292