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