Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/ocsp/ocsp_print.c

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