Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/ocsp/ocsp_prn.c

raw
1 /*
2 * Copyright 2000-2023 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 static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent)
18 {
19 BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
20 indent += 2;
21 BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
22 i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm);
23 BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
24 i2a_ASN1_STRING(bp, &a->issuerNameHash, 0);
25 BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
26 i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0);
27 BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
28 i2a_ASN1_INTEGER(bp, &a->serialNumber);
29 BIO_printf(bp, "\n");
30 return 1;
31 }
32
33 typedef struct {
34 long t;
35 const char *m;
36 } OCSP_TBLSTR;
37
38 static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len)
39 {
40 size_t i;
41 for (i = 0; i < len; i++, ts++)
42 if (ts->t == s)
43 return ts->m;
44 return "(UNKNOWN)";
45 }
46
47 #define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl))
48
49 const char *OCSP_response_status_str(long s)
50 {
51 static const OCSP_TBLSTR rstat_tbl[] = {
52 { OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" },
53 { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
54 { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
55 { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
56 { OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" },
57 { OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" }
58 };
59 return table2string(s, rstat_tbl);
60 }
61
62 const char *OCSP_cert_status_str(long s)
63 {
64 static const OCSP_TBLSTR cstat_tbl[] = {
65 { V_OCSP_CERTSTATUS_GOOD, "good" },
66 { V_OCSP_CERTSTATUS_REVOKED, "revoked" },
67 { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" }
68 };
69 return table2string(s, cstat_tbl);
70 }
71
72 const char *OCSP_crl_reason_str(long s)
73 {
74 static const OCSP_TBLSTR reason_tbl[] = {
75 { OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" },
76 { OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" },
77 { OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" },
78 { OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged" },
79 { OCSP_REVOKED_STATUS_SUPERSEDED, "superseded" },
80 { OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation" },
81 { OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold" },
82 { OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL" },
83 { OCSP_REVOKED_STATUS_PRIVILEGEWITHDRAWN, "privilegeWithdrawn" },
84 { OCSP_REVOKED_STATUS_AACOMPROMISE, "aACompromise" }
85 };
86 return table2string(s, reason_tbl);
87 }
88
89 int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags)
90 {
91 int i;
92 long l;
93 OCSP_CERTID *cid = NULL;
94 OCSP_ONEREQ *one = NULL;
95 OCSP_REQINFO *inf = &o->tbsRequest;
96 OCSP_SIGNATURE *sig = o->optionalSignature;
97
98 if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0)
99 goto err;
100 l = ASN1_INTEGER_get(inf->version);
101 if (BIO_printf(bp, " Version: %lu (0x%lx)", l + 1, l) <= 0)
102 goto err;
103 if (inf->requestorName != NULL) {
104 if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0)
105 goto err;
106 GENERAL_NAME_print(bp, inf->requestorName);
107 }
108 if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0)
109 goto err;
110 for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) {
111 one = sk_OCSP_ONEREQ_value(inf->requestList, i);
112 cid = one->reqCert;
113 ocsp_certid_print(bp, cid, 8);
114 if (!X509V3_extensions_print(bp,
115 "Request Single Extensions",
116 one->singleRequestExtensions, flags, 8))
117 goto err;
118 }
119 if (!X509V3_extensions_print(bp, "Request Extensions",
120 inf->requestExtensions, flags, 4))
121 goto err;
122 if (sig) {
123 X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature);
124 for (i = 0; i < sk_X509_num(sig->certs); i++) {
125 X509_print(bp, sk_X509_value(sig->certs, i));
126 PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i));
127 }
128 }
129 return 1;
130 err:
131 return 0;
132 }
133
134 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags)
135 {
136 int i, ret = 0;
137 long l;
138 OCSP_CERTID *cid = NULL;
139 OCSP_BASICRESP *br = NULL;
140 OCSP_RESPID *rid = NULL;
141 OCSP_RESPDATA *rd = NULL;
142 OCSP_CERTSTATUS *cst = NULL;
143 OCSP_REVOKEDINFO *rev = NULL;
144 OCSP_SINGLERESP *single = NULL;
145 OCSP_RESPBYTES *rb = o->responseBytes;
146
147 if (BIO_puts(bp, "OCSP Response Data:\n") <= 0)
148 goto err;
149 l = ASN1_ENUMERATED_get(o->responseStatus);
150 if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n",
151 OCSP_response_status_str(l), l)
152 <= 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))
200 <= 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)
213 <= 0)
214 goto err;
215 }
216 }
217 if (BIO_printf(bp, "\n This Update: ") <= 0)
218 goto err;
219 if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate))
220 goto err;
221 if (single->nextUpdate) {
222 if (BIO_printf(bp, "\n Next Update: ") <= 0)
223 goto err;
224 if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate))
225 goto err;
226 }
227 if (BIO_write(bp, "\n", 1) <= 0)
228 goto err;
229 if (!X509V3_extensions_print(bp,
230 "Response Single Extensions",
231 single->singleExtensions, flags, 8))
232 goto err;
233 if (BIO_write(bp, "\n", 1) <= 0)
234 goto err;
235 }
236 if (!X509V3_extensions_print(bp, "Response Extensions",
237 rd->responseExtensions, flags, 4))
238 goto err;
239 if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0)
240 goto err;
241
242 for (i = 0; i < sk_X509_num(br->certs); i++) {
243 X509_print(bp, sk_X509_value(br->certs, i));
244 PEM_write_bio_X509(bp, sk_X509_value(br->certs, i));
245 }
246
247 ret = 1;
248 err:
249 OCSP_BASICRESP_free(br);
250 return ret;
251 }