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