Fix safestack issues in x509.h
[openssl.git] / crypto / ocsp / ocsp_prn.c
1 /*
2  * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/bio.h>
11 #include <openssl/err.h>
12 #include <openssl/ocsp.h>
13 #include "ocsp_local.h"
14 #include "internal/cryptlib.h"
15 #include <openssl/pem.h>
16
17 DEFINE_STACK_OF(OCSP_ONEREQ)
18 DEFINE_STACK_OF(OCSP_SINGLERESP)
19
20 static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent)
21 {
22     BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
23     indent += 2;
24     BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
25     i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm);
26     BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
27     i2a_ASN1_STRING(bp, &a->issuerNameHash, 0);
28     BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
29     i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0);
30     BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
31     i2a_ASN1_INTEGER(bp, &a->serialNumber);
32     BIO_printf(bp, "\n");
33     return 1;
34 }
35
36 typedef struct {
37     long t;
38     const char *m;
39 } OCSP_TBLSTR;
40
41 static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len)
42 {
43     size_t i;
44     for (i = 0; i < len; i++, ts++)
45         if (ts->t == s)
46             return ts->m;
47     return "(UNKNOWN)";
48 }
49
50 #define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl))
51
52 const char *OCSP_response_status_str(long s)
53 {
54     static const OCSP_TBLSTR rstat_tbl[] = {
55         {OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"},
56         {OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest"},
57         {OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror"},
58         {OCSP_RESPONSE_STATUS_TRYLATER, "trylater"},
59         {OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired"},
60         {OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized"}
61     };
62     return table2string(s, rstat_tbl);
63 }
64
65 const char *OCSP_cert_status_str(long s)
66 {
67     static const OCSP_TBLSTR cstat_tbl[] = {
68         {V_OCSP_CERTSTATUS_GOOD, "good"},
69         {V_OCSP_CERTSTATUS_REVOKED, "revoked"},
70         {V_OCSP_CERTSTATUS_UNKNOWN, "unknown"}
71     };
72     return table2string(s, cstat_tbl);
73 }
74
75 const char *OCSP_crl_reason_str(long s)
76 {
77     static const OCSP_TBLSTR reason_tbl[] = {
78         {OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified"},
79         {OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise"},
80         {OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise"},
81         {OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged"},
82         {OCSP_REVOKED_STATUS_SUPERSEDED, "superseded"},
83         {OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation"},
84         {OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold"},
85         {OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL"}
86     };
87     return table2string(s, reason_tbl);
88 }
89
90 int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
91 {
92     int i;
93     long l;
94     OCSP_CERTID *cid = NULL;
95     OCSP_ONEREQ *one = NULL;
96     OCSP_REQINFO *inf = &o->tbsRequest;
97     OCSP_SIGNATURE *sig = o->optionalSignature;
98
99     if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
100         goto err;
101     l = ASN1_INTEGER_get(inf->version);
102     if (BIO_printf(bp, "    Version: %lu (0x%lx)", l + 1, l) <= 0)
103         goto err;
104     if (inf->requestorName != NULL) {
105         if (BIO_write(bp, "\n    Requestor Name: ", 21) <= 0)
106             goto err;
107         GENERAL_NAME_print(bp, inf->requestorName);
108     }
109     if (BIO_write(bp, "\n    Requestor List:\n", 21) <= 0)
110         goto err;
111     for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
112         one = sk_OCSP_ONEREQ_value(inf->requestList, i);
113         cid = one->reqCert;
114         ocsp_certid_print(bp, cid, 8);
115         if (!X509V3_extensions_print(bp,
116                                      "Request Single Extensions",
117                                      one->singleRequestExtensions, flags, 8))
118             goto err;
119     }
120     if (!X509V3_extensions_print(bp, "Request Extensions",
121                                  inf->requestExtensions, flags, 4))
122         goto err;
123     if (sig) {
124         X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
125         for (i = 0; i < sk_X509_num(sig->certs); i++) {
126             X509_print(bp, sk_X509_value(sig->certs, i));
127             PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
128         }
129     }
130     return 1;
131  err:
132     return 0;
133 }
134
135 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
136 {
137     int i, ret = 0;
138     long l;
139     OCSP_CERTID *cid = NULL;
140     OCSP_BASICRESP *br = NULL;
141     OCSP_RESPID *rid = NULL;
142     OCSP_RESPDATA *rd = NULL;
143     OCSP_CERTSTATUS *cst = NULL;
144     OCSP_REVOKEDINFO *rev = NULL;
145     OCSP_SINGLERESP *single = NULL;
146     OCSP_RESPBYTES *rb = o->responseBytes;
147
148     if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
149         goto err;
150     l = ASN1_ENUMERATED_get(o->responseStatus);
151     if (BIO_printf(bp, "    OCSP Response Status: %s (0x%lx)\n",
152                    OCSP_response_status_str(l), l) <= 0)
153         goto err;
154     if (rb == NULL)
155         return 1;
156     if (BIO_puts(bp, "    Response Type: ") <= 0)
157         goto err;
158     if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
159         goto err;
160     if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
161         BIO_puts(bp, " (unknown response type)\n");
162         return 1;
163     }
164
165     if ((br = OCSP_response_get1_basic(o)) == NULL)
166         goto err;
167     rd = &br->tbsResponseData;
168     l = ASN1_INTEGER_get(rd->version);
169     if (BIO_printf(bp, "\n    Version: %lu (0x%lx)\n", l + 1, l) <= 0)
170         goto err;
171     if (BIO_puts(bp, "    Responder Id: ") <= 0)
172         goto err;
173
174     rid = &rd->responderId;
175     switch (rid->type) {
176     case V_OCSP_RESPID_NAME:
177         X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
178         break;
179     case V_OCSP_RESPID_KEY:
180         i2a_ASN1_STRING(bp, rid->value.byKey, 0);
181         break;
182     }
183
184     if (BIO_printf(bp, "\n    Produced At: ") <= 0)
185         goto err;
186     if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt))
187         goto err;
188     if (BIO_printf(bp, "\n    Responses:\n") <= 0)
189         goto err;
190     for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) {
191         if (!sk_OCSP_SINGLERESP_value(rd->responses, i))
192             continue;
193         single = sk_OCSP_SINGLERESP_value(rd->responses, i);
194         cid = single->certId;
195         if (ocsp_certid_print(bp, cid, 4) <= 0)
196             goto err;
197         cst = single->certStatus;
198         if (BIO_printf(bp, "    Cert Status: %s",
199                        OCSP_cert_status_str(cst->type)) <= 0)
200             goto err;
201         if (cst->type == V_OCSP_CERTSTATUS_REVOKED) {
202             rev = cst->value.revoked;
203             if (BIO_printf(bp, "\n    Revocation Time: ") <= 0)
204                 goto err;
205             if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime))
206                 goto err;
207             if (rev->revocationReason) {
208                 l = ASN1_ENUMERATED_get(rev->revocationReason);
209                 if (BIO_printf(bp,
210                                "\n    Revocation Reason: %s (0x%lx)",
211                                OCSP_crl_reason_str(l), l) <= 0)
212                     goto err;
213             }
214         }
215         if (BIO_printf(bp, "\n    This Update: ") <= 0)
216             goto err;
217         if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
218             goto err;
219         if (single->nextUpdate) {
220             if (BIO_printf(bp, "\n    Next Update: ") <= 0)
221                 goto err;
222             if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
223                 goto err;
224         }
225         if (BIO_write(bp, "\n", 1) <= 0)
226             goto err;
227         if (!X509V3_extensions_print(bp,
228                                      "Response Single Extensions",
229                                      single->singleExtensions, flags, 8))
230             goto err;
231         if (BIO_write(bp, "\n", 1) <= 0)
232             goto err;
233     }
234     if (!X509V3_extensions_print(bp, "Response Extensions",
235                                  rd->responseExtensions, flags, 4))
236         goto err;
237     if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
238         goto err;
239
240     for (i = 0; i < sk_X509_num(br->certs); i++) {
241         X509_print(bp, sk_X509_value(br->certs, i));
242         PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
243     }
244
245     ret = 1;
246  err:
247     OCSP_BASICRESP_free(br);
248     return ret;
249 }