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