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