/src/zeek/src/file_analysis/analyzer/x509/OCSP.cc
Line | Count | Source |
1 | | |
2 | | // See the file "COPYING" in the main distribution directory for copyright. |
3 | | |
4 | | #include "zeek/file_analysis/analyzer/x509/OCSP.h" |
5 | | |
6 | | #include <openssl/asn1.h> |
7 | | #include <openssl/opensslconf.h> |
8 | | #include <openssl/x509.h> |
9 | | #include <openssl/x509v3.h> |
10 | | #include <string> |
11 | | |
12 | | #include "zeek/Event.h" |
13 | | #include "zeek/Reporter.h" |
14 | | #include "zeek/file_analysis/File.h" |
15 | | #include "zeek/file_analysis/Manager.h" |
16 | | #include "zeek/file_analysis/analyzer/x509/X509.h" |
17 | | #include "zeek/file_analysis/analyzer/x509/consts.bif.h" |
18 | | #include "zeek/file_analysis/analyzer/x509/ocsp_events.bif.h" |
19 | | |
20 | | // helper function of sk_X509_value to avoid namespace problem |
21 | | // sk_X509_value(X,Y) = > SKM_sk_value(X509,X,Y) |
22 | | // X509 => file_analysis::X509 |
23 | 0 | X509* helper_sk_X509_value(const STACK_OF(X509) * certs, int i) { return sk_X509_value(certs, i); } |
24 | | |
25 | | namespace zeek::file_analysis::detail { |
26 | | |
27 | | static constexpr size_t OCSP_STRING_BUF_SIZE = 2048; |
28 | | |
29 | 0 | static bool OCSP_RESPID_bio(OCSP_BASICRESP* basic_resp, BIO* bio) { |
30 | 0 | const ASN1_OCTET_STRING* key = nullptr; |
31 | 0 | const X509_NAME* name = nullptr; |
32 | |
|
33 | 0 | if ( ! OCSP_resp_get0_id(basic_resp, &key, &name) ) |
34 | 0 | return false; |
35 | | |
36 | 0 | if ( name ) |
37 | 0 | X509_NAME_print_ex(bio, name, 0, XN_FLAG_ONELINE); |
38 | 0 | else |
39 | 0 | i2a_ASN1_STRING(bio, key, V_ASN1_OCTET_STRING); |
40 | |
|
41 | 0 | return true; |
42 | 0 | } |
43 | | |
44 | 0 | static bool ocsp_add_cert_id(const OCSP_CERTID* cert_id, zeek::Args* vl, BIO* bio) { |
45 | 0 | ASN1_OBJECT* hash_alg = nullptr; |
46 | 0 | ASN1_OCTET_STRING* issuer_name_hash = nullptr; |
47 | 0 | ASN1_OCTET_STRING* issuer_key_hash = nullptr; |
48 | 0 | ASN1_INTEGER* serial_number = nullptr; |
49 | |
|
50 | 0 | auto res = OCSP_id_get0_info(&issuer_name_hash, &hash_alg, &issuer_key_hash, &serial_number, |
51 | 0 | const_cast<OCSP_CERTID*>(cert_id)); |
52 | |
|
53 | 0 | if ( ! res ) { |
54 | 0 | reporter->Weird("OpenSSL failed to get OCSP_CERTID info"); |
55 | 0 | vl->emplace_back(val_mgr->EmptyString()); |
56 | 0 | vl->emplace_back(val_mgr->EmptyString()); |
57 | 0 | vl->emplace_back(val_mgr->EmptyString()); |
58 | 0 | vl->emplace_back(val_mgr->EmptyString()); |
59 | 0 | return false; |
60 | 0 | } |
61 | | |
62 | 0 | char buf[OCSP_STRING_BUF_SIZE]; |
63 | 0 | memset(buf, 0, sizeof(buf)); |
64 | |
|
65 | 0 | i2a_ASN1_OBJECT(bio, hash_alg); |
66 | 0 | int len = BIO_read(bio, buf, sizeof(buf)); |
67 | 0 | vl->emplace_back(make_intrusive<StringVal>(len, buf)); |
68 | 0 | BIO_reset(bio); |
69 | |
|
70 | 0 | i2a_ASN1_STRING(bio, issuer_name_hash, V_ASN1_OCTET_STRING); |
71 | 0 | len = BIO_read(bio, buf, sizeof(buf)); |
72 | 0 | vl->emplace_back(make_intrusive<StringVal>(len, buf)); |
73 | 0 | BIO_reset(bio); |
74 | |
|
75 | 0 | i2a_ASN1_STRING(bio, issuer_key_hash, V_ASN1_OCTET_STRING); |
76 | 0 | len = BIO_read(bio, buf, sizeof(buf)); |
77 | 0 | vl->emplace_back(make_intrusive<StringVal>(len, buf)); |
78 | 0 | BIO_reset(bio); |
79 | |
|
80 | 0 | i2a_ASN1_INTEGER(bio, serial_number); |
81 | 0 | len = BIO_read(bio, buf, sizeof(buf)); |
82 | 0 | vl->emplace_back(make_intrusive<StringVal>(len, buf)); |
83 | 0 | BIO_reset(bio); |
84 | |
|
85 | 0 | return true; |
86 | 0 | } |
87 | | |
88 | 0 | file_analysis::Analyzer* OCSP::InstantiateRequest(RecordValPtr args, file_analysis::File* file) { |
89 | 0 | return new OCSP(std::move(args), file, true); |
90 | 0 | } |
91 | | |
92 | 201k | file_analysis::Analyzer* OCSP::InstantiateReply(RecordValPtr args, file_analysis::File* file) { |
93 | 201k | return new OCSP(std::move(args), file, false); |
94 | 201k | } |
95 | | |
96 | | OCSP::OCSP(RecordValPtr args, file_analysis::File* file, bool arg_request) |
97 | 201k | : X509Common::X509Common(file_mgr->GetComponentTag("OCSP"), std::move(args), file), request(arg_request) {} |
98 | | |
99 | 201k | bool OCSP::DeliverStream(const u_char* data, uint64_t len) { |
100 | 201k | if ( zeek::BifConst::OCSP::max_bytes > 0 && ocsp_data.size() + len > zeek::BifConst::OCSP::max_bytes ) { |
101 | 0 | reporter->Weird(GetFile(), "ocsp_response_too_large"); |
102 | 0 | return false; |
103 | 0 | } |
104 | | |
105 | 201k | ocsp_data.append(reinterpret_cast<const char*>(data), len); |
106 | 201k | return true; |
107 | 201k | } |
108 | | |
109 | 0 | bool OCSP::Undelivered(uint64_t offset, uint64_t len) { return false; } |
110 | | |
111 | | // we parse the entire OCSP response in EOF, because we just pass it on |
112 | | // to OpenSSL. |
113 | 201k | bool OCSP::EndOfFile() { |
114 | 201k | const unsigned char* ocsp_char = reinterpret_cast<const unsigned char*>(ocsp_data.data()); |
115 | | |
116 | 201k | if ( request ) { |
117 | 0 | OCSP_REQUEST* req = d2i_OCSP_REQUEST(nullptr, &ocsp_char, ocsp_data.size()); |
118 | |
|
119 | 0 | if ( ! req ) { |
120 | 0 | reporter->Weird(GetFile(), "openssl_ocsp_request_parse_error"); |
121 | 0 | return false; |
122 | 0 | } |
123 | | |
124 | 0 | ParseRequest(req); |
125 | 0 | OCSP_REQUEST_free(req); |
126 | 0 | } |
127 | 201k | else { |
128 | 201k | OCSP_RESPONSE* resp = d2i_OCSP_RESPONSE(nullptr, &ocsp_char, ocsp_data.size()); |
129 | | |
130 | 201k | if ( ! resp ) { |
131 | 201k | reporter->Weird(GetFile(), "openssl_ocsp_response_parse_error"); |
132 | 201k | return false; |
133 | 201k | } |
134 | | |
135 | 0 | ParseResponse(resp); |
136 | 0 | OCSP_RESPONSE_free(resp); |
137 | 0 | } |
138 | | |
139 | 0 | return true; |
140 | 201k | } |
141 | | |
142 | | struct ASN1Seq { |
143 | 0 | ASN1Seq(const unsigned char** der_in, long length) { decoded = d2i_ASN1_SEQUENCE_ANY(nullptr, der_in, length); } |
144 | | |
145 | 0 | ~ASN1Seq() { sk_ASN1_TYPE_pop_free(decoded, ASN1_TYPE_free); } |
146 | | |
147 | 0 | explicit operator bool() const { return decoded; } |
148 | | |
149 | 0 | operator ASN1_SEQUENCE_ANY*() const { return decoded; } |
150 | | |
151 | | ASN1_SEQUENCE_ANY* decoded; |
152 | | }; |
153 | | |
154 | | // Re-encode and then parse out ASN1 structures to get at what we need... |
155 | | /*- BasicOCSPResponse ::= SEQUENCE { |
156 | | * tbsResponseData ResponseData, |
157 | | * signatureAlgorithm AlgorithmIdentifier, |
158 | | * signature BIT STRING, |
159 | | * certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } |
160 | | typedef struct ocsp_basic_response_st { |
161 | | OCSP_RESPDATA *tbsResponseData; |
162 | | X509_ALGOR *signatureAlgorithm; |
163 | | ASN1_BIT_STRING *signature; |
164 | | STACK_OF(X509) *certs; |
165 | | } OCSP_BASICRESP; |
166 | | */ |
167 | 0 | static StringValPtr parse_basic_resp_sig_alg(OCSP_BASICRESP* basic_resp, BIO* bio, char* buf, size_t buf_len) { |
168 | 0 | int der_basic_resp_len = 0; |
169 | 0 | unsigned char* der_basic_resp_dat = nullptr; |
170 | |
|
171 | 0 | der_basic_resp_len = i2d_OCSP_BASICRESP(basic_resp, &der_basic_resp_dat); |
172 | |
|
173 | 0 | if ( der_basic_resp_len <= 0 ) |
174 | 0 | return val_mgr->EmptyString(); |
175 | | |
176 | 0 | const unsigned char* const_der_basic_resp_dat = der_basic_resp_dat; |
177 | |
|
178 | 0 | ASN1Seq bseq{&const_der_basic_resp_dat, der_basic_resp_len}; |
179 | |
|
180 | 0 | if ( ! bseq ) { |
181 | 0 | OPENSSL_free(der_basic_resp_dat); |
182 | 0 | return val_mgr->EmptyString(); |
183 | 0 | } |
184 | | |
185 | 0 | if ( sk_ASN1_TYPE_num(bseq) < 3 ) { |
186 | 0 | OPENSSL_free(der_basic_resp_dat); |
187 | 0 | return val_mgr->EmptyString(); |
188 | 0 | } |
189 | | |
190 | 0 | auto constexpr sig_alg_idx = 1u; |
191 | 0 | auto aseq_type = sk_ASN1_TYPE_value(bseq, sig_alg_idx); |
192 | |
|
193 | 0 | if ( ASN1_TYPE_get(aseq_type) != V_ASN1_SEQUENCE ) { |
194 | 0 | OPENSSL_free(der_basic_resp_dat); |
195 | 0 | return val_mgr->EmptyString(); |
196 | 0 | } |
197 | | |
198 | 0 | auto aseq_str = aseq_type->value.asn1_string; |
199 | 0 | auto aseq_len = ASN1_STRING_length(aseq_str); |
200 | 0 | auto aseq_dat = ASN1_STRING_get0_data(aseq_str); |
201 | |
|
202 | 0 | ASN1Seq aseq{&aseq_dat, aseq_len}; |
203 | |
|
204 | 0 | if ( ! aseq ) { |
205 | 0 | OPENSSL_free(der_basic_resp_dat); |
206 | 0 | return val_mgr->EmptyString(); |
207 | 0 | } |
208 | | |
209 | 0 | if ( sk_ASN1_TYPE_num(aseq) < 1 ) { |
210 | 0 | OPENSSL_free(der_basic_resp_dat); |
211 | 0 | return val_mgr->EmptyString(); |
212 | 0 | } |
213 | | |
214 | 0 | auto constexpr alg_obj_idx = 0u; |
215 | 0 | auto alg_obj_type = sk_ASN1_TYPE_value(aseq, alg_obj_idx); |
216 | |
|
217 | 0 | if ( ASN1_TYPE_get(alg_obj_type) != V_ASN1_OBJECT ) { |
218 | 0 | OPENSSL_free(der_basic_resp_dat); |
219 | 0 | return val_mgr->EmptyString(); |
220 | 0 | } |
221 | | |
222 | 0 | auto alg_obj = alg_obj_type->value.object; |
223 | 0 | i2a_ASN1_OBJECT(bio, alg_obj); |
224 | 0 | auto alg_len = BIO_read(bio, buf, buf_len); |
225 | 0 | auto rval = make_intrusive<StringVal>(alg_len, buf); |
226 | 0 | BIO_reset(bio); |
227 | |
|
228 | 0 | OPENSSL_free(der_basic_resp_dat); |
229 | 0 | return rval; |
230 | 0 | } |
231 | | |
232 | 0 | static ValPtr parse_basic_resp_data_version(OCSP_BASICRESP* basic_resp) { |
233 | 0 | int der_basic_resp_len = 0; |
234 | 0 | unsigned char* der_basic_resp_dat = nullptr; |
235 | |
|
236 | 0 | der_basic_resp_len = i2d_OCSP_BASICRESP(basic_resp, &der_basic_resp_dat); |
237 | |
|
238 | 0 | if ( der_basic_resp_len <= 0 ) |
239 | 0 | return val_mgr->Count(-1); |
240 | | |
241 | 0 | const unsigned char* const_der_basic_resp_dat = der_basic_resp_dat; |
242 | |
|
243 | 0 | ASN1Seq bseq{&const_der_basic_resp_dat, der_basic_resp_len}; |
244 | |
|
245 | 0 | if ( ! bseq ) { |
246 | 0 | OPENSSL_free(der_basic_resp_dat); |
247 | 0 | return val_mgr->Count(-1); |
248 | 0 | } |
249 | | |
250 | 0 | if ( sk_ASN1_TYPE_num(bseq) < 3 ) { |
251 | 0 | OPENSSL_free(der_basic_resp_dat); |
252 | 0 | return val_mgr->Count(-1); |
253 | 0 | } |
254 | | |
255 | 0 | auto constexpr resp_data_idx = 0u; |
256 | 0 | auto dseq_type = sk_ASN1_TYPE_value(bseq, resp_data_idx); |
257 | |
|
258 | 0 | if ( ASN1_TYPE_get(dseq_type) != V_ASN1_SEQUENCE ) { |
259 | 0 | OPENSSL_free(der_basic_resp_dat); |
260 | 0 | return val_mgr->Count(-1); |
261 | 0 | } |
262 | | |
263 | 0 | auto dseq_str = dseq_type->value.asn1_string; |
264 | 0 | auto dseq_len = ASN1_STRING_length(dseq_str); |
265 | 0 | auto dseq_dat = ASN1_STRING_get0_data(dseq_str); |
266 | |
|
267 | 0 | ASN1Seq dseq{&dseq_dat, dseq_len}; |
268 | |
|
269 | 0 | if ( ! dseq ) { |
270 | 0 | OPENSSL_free(der_basic_resp_dat); |
271 | 0 | return val_mgr->Count(-1); |
272 | 0 | } |
273 | | |
274 | 0 | if ( sk_ASN1_TYPE_num(dseq) < 1 ) { |
275 | 0 | OPENSSL_free(der_basic_resp_dat); |
276 | 0 | return val_mgr->Count(-1); |
277 | 0 | } |
278 | | |
279 | | /*- ResponseData ::= SEQUENCE { |
280 | | * version [0] EXPLICIT Version DEFAULT v1, |
281 | | * responderID ResponderID, |
282 | | * producedAt GeneralizedTime, |
283 | | * responses SEQUENCE OF SingleResponse, |
284 | | * responseExtensions [1] EXPLICIT Extensions OPTIONAL } |
285 | | */ |
286 | | |
287 | 0 | auto constexpr version_idx = 0u; |
288 | 0 | auto version_type = sk_ASN1_TYPE_value(dseq, version_idx); |
289 | |
|
290 | 0 | if ( ASN1_TYPE_get(version_type) != V_ASN1_INTEGER ) { |
291 | 0 | OPENSSL_free(der_basic_resp_dat); |
292 | | // Not present, use default value. |
293 | 0 | return val_mgr->Count(0); |
294 | 0 | } |
295 | | |
296 | 0 | uint64_t asn1_int = ASN1_INTEGER_get(version_type->value.integer); |
297 | 0 | OPENSSL_free(der_basic_resp_dat); |
298 | 0 | return val_mgr->Count(asn1_int); |
299 | 0 | } |
300 | | |
301 | 0 | static uint64_t parse_request_version(OCSP_REQUEST* req) { |
302 | 0 | int der_req_len = 0; |
303 | 0 | unsigned char* der_req_dat = nullptr; |
304 | 0 | der_req_len = i2d_OCSP_REQUEST(req, &der_req_dat); |
305 | 0 | const unsigned char* const_der_req_dat = der_req_dat; |
306 | |
|
307 | 0 | if ( ! der_req_dat ) |
308 | 0 | return -1; |
309 | | |
310 | 0 | ASN1Seq rseq{&const_der_req_dat, der_req_len}; |
311 | |
|
312 | 0 | if ( ! rseq ) { |
313 | 0 | OPENSSL_free(der_req_dat); |
314 | 0 | return -1; |
315 | 0 | } |
316 | | |
317 | 0 | if ( sk_ASN1_TYPE_num(rseq) < 1 ) { |
318 | 0 | OPENSSL_free(der_req_dat); |
319 | 0 | return -1; |
320 | 0 | } |
321 | | |
322 | 0 | auto constexpr version_idx = 0u; |
323 | 0 | auto version_type = sk_ASN1_TYPE_value(rseq, version_idx); |
324 | |
|
325 | 0 | if ( ASN1_TYPE_get(version_type) != V_ASN1_INTEGER ) { |
326 | 0 | OPENSSL_free(der_req_dat); |
327 | | // Not present, use default value. |
328 | 0 | return 0; |
329 | 0 | } |
330 | | |
331 | 0 | uint64_t asn1_int = ASN1_INTEGER_get(version_type->value.integer); |
332 | 0 | OPENSSL_free(der_req_dat); |
333 | 0 | return asn1_int; |
334 | 0 | } |
335 | | |
336 | 0 | void OCSP::ParseRequest(OCSP_REQUEST* req) { |
337 | 0 | char buf[OCSP_STRING_BUF_SIZE]; // we need a buffer for some of the openssl functions |
338 | 0 | memset(buf, 0, sizeof(buf)); |
339 | |
|
340 | 0 | uint64_t version = 0; |
341 | |
|
342 | 0 | version = parse_request_version(req); |
343 | | // TODO: try to parse out general name ? |
344 | |
|
345 | 0 | if ( ocsp_request ) |
346 | 0 | event_mgr.Enqueue(ocsp_request, GetFile()->ToVal(), val_mgr->Count(version)); |
347 | |
|
348 | 0 | BIO* bio = BIO_new(BIO_s_mem()); |
349 | 0 | BIO_set_mem_eof_return(bio, 0); // don't return -1 if empty |
350 | |
|
351 | 0 | int req_count = OCSP_request_onereq_count(req); |
352 | 0 | for ( int i = 0; i < req_count; i++ ) { |
353 | 0 | zeek::Args rvl; |
354 | 0 | rvl.reserve(5); |
355 | 0 | rvl.emplace_back(GetFile()->ToVal()); |
356 | |
|
357 | 0 | OCSP_ONEREQ* one_req = OCSP_request_onereq_get0(req, i); |
358 | 0 | OCSP_CERTID* cert_id = OCSP_onereq_get0_id(one_req); |
359 | |
|
360 | 0 | ocsp_add_cert_id(cert_id, &rvl, bio); |
361 | |
|
362 | 0 | if ( ocsp_request_certificate ) |
363 | 0 | event_mgr.Enqueue(ocsp_request_certificate, std::move(rvl)); |
364 | 0 | } |
365 | |
|
366 | 0 | BIO_free(bio); |
367 | 0 | } |
368 | | |
369 | 0 | void OCSP::ParseResponse(OCSP_RESPONSE* resp) { |
370 | | // OCSP_RESPBYTES *resp_bytes = resp->responseBytes; |
371 | 0 | OCSP_BASICRESP* basic_resp = nullptr; |
372 | 0 | OCSP_RESPDATA* resp_data = nullptr; |
373 | 0 | OCSP_RESPID* resp_id = nullptr; |
374 | 0 | const ASN1_GENERALIZEDTIME* produced_at = nullptr; |
375 | 0 | const STACK_OF(X509)* certs = nullptr; |
376 | |
|
377 | 0 | int resp_count; |
378 | 0 | int num_ext = 0; |
379 | 0 | VectorVal* certs_vector = nullptr; |
380 | 0 | int len = 0; |
381 | |
|
382 | 0 | char buf[OCSP_STRING_BUF_SIZE]; |
383 | 0 | memset(buf, 0, sizeof(buf)); |
384 | |
|
385 | 0 | const char* status_str = OCSP_response_status_str(OCSP_response_status(resp)); |
386 | 0 | auto status_val = make_intrusive<StringVal>(strlen(status_str), status_str); |
387 | |
|
388 | 0 | if ( ocsp_response_status ) |
389 | 0 | event_mgr.Enqueue(ocsp_response_status, GetFile()->ToVal(), status_val); |
390 | | |
391 | | // if (!resp_bytes) |
392 | | // { |
393 | | // Unref(status_val); |
394 | | // return; |
395 | | // } |
396 | |
|
397 | 0 | BIO* bio = BIO_new(BIO_s_mem()); |
398 | 0 | BIO_set_mem_eof_return(bio, 0); // don't return -1 if empty |
399 | | // i2a_ASN1_OBJECT(bio, resp_bytes->responseType); |
400 | | // int len = BIO_read(bio, buf, sizeof(buf)); |
401 | | // BIO_reset(bio); |
402 | |
|
403 | 0 | zeek::Args vl; |
404 | 0 | vl.reserve(8); |
405 | | |
406 | | // get the basic response |
407 | 0 | basic_resp = OCSP_response_get1_basic(resp); |
408 | 0 | if ( ! basic_resp ) |
409 | 0 | goto clean_up; |
410 | | |
411 | 0 | vl.emplace_back(GetFile()->ToVal()); |
412 | 0 | vl.emplace_back(std::move(status_val)); |
413 | |
|
414 | 0 | vl.emplace_back(parse_basic_resp_data_version(basic_resp)); |
415 | | |
416 | | // responderID |
417 | 0 | if ( OCSP_RESPID_bio(basic_resp, bio) ) { |
418 | 0 | len = BIO_read(bio, buf, sizeof(buf)); |
419 | 0 | vl.emplace_back(make_intrusive<StringVal>(len, buf)); |
420 | 0 | BIO_reset(bio); |
421 | 0 | } |
422 | 0 | else { |
423 | 0 | reporter->Weird("OpenSSL failed to get OCSP responder id"); |
424 | 0 | vl.emplace_back(val_mgr->EmptyString()); |
425 | 0 | } |
426 | | |
427 | | // producedAt |
428 | 0 | produced_at = OCSP_resp_get0_produced_at(basic_resp); |
429 | |
|
430 | 0 | vl.emplace_back(make_intrusive<TimeVal>(GetTimeFromAsn1(produced_at, GetFile(), reporter))); |
431 | | |
432 | | // responses |
433 | |
|
434 | 0 | resp_count = OCSP_resp_count(basic_resp); |
435 | |
|
436 | 0 | for ( int i = 0; i < resp_count; i++ ) { |
437 | 0 | OCSP_SINGLERESP* single_resp = OCSP_resp_get0(basic_resp, i); |
438 | |
|
439 | 0 | if ( ! single_resp ) |
440 | 0 | continue; |
441 | | |
442 | 0 | zeek::Args rvl; |
443 | 0 | rvl.reserve(10); |
444 | 0 | rvl.emplace_back(GetFile()->ToVal()); |
445 | | |
446 | | // cert id |
447 | 0 | const OCSP_CERTID* cert_id = nullptr; |
448 | |
|
449 | 0 | cert_id = OCSP_SINGLERESP_get0_id(single_resp); |
450 | |
|
451 | 0 | ocsp_add_cert_id(cert_id, &rvl, bio); |
452 | 0 | BIO_reset(bio); |
453 | | |
454 | | // certStatus |
455 | 0 | int status = V_OCSP_CERTSTATUS_UNKNOWN; |
456 | 0 | int reason = OCSP_REVOKED_STATUS_NOSTATUS; |
457 | 0 | ASN1_GENERALIZEDTIME* revoke_time = nullptr; |
458 | 0 | ASN1_GENERALIZEDTIME* this_update = nullptr; |
459 | 0 | ASN1_GENERALIZEDTIME* next_update = nullptr; |
460 | |
|
461 | 0 | if ( ! OCSP_resp_find_status(basic_resp, const_cast<OCSP_CERTID*>(cert_id), &status, &reason, &revoke_time, |
462 | 0 | &this_update, &next_update) ) |
463 | 0 | reporter->Weird("OpenSSL failed to find status of OCSP response"); |
464 | |
|
465 | 0 | const char* cert_status_str = OCSP_cert_status_str(status); |
466 | 0 | rvl.emplace_back(make_intrusive<StringVal>(strlen(cert_status_str), cert_status_str)); |
467 | | |
468 | | // revocation time and reason if revoked |
469 | 0 | if ( status == V_OCSP_CERTSTATUS_REVOKED ) { |
470 | 0 | rvl.emplace_back(make_intrusive<TimeVal>(GetTimeFromAsn1(revoke_time, GetFile(), reporter))); |
471 | |
|
472 | 0 | if ( reason != OCSP_REVOKED_STATUS_NOSTATUS ) { |
473 | 0 | const char* revoke_reason = OCSP_crl_reason_str(reason); |
474 | |
|
475 | 0 | #if OPENSSL_VERSION_NUMBER < 0x30200000L |
476 | | // OpenSSL 3.2.0 and later return the right strings for |
477 | | // OCSP_REVOKED_STATUS_PRIVILEGEWITHDRAWN (9) and |
478 | | // OCSP_REVOKED_STATUS_AACOMPROMISE (10). |
479 | | // |
480 | | // For versions older than that, fix it up by hand. |
481 | 0 | if ( (reason == 9 || reason == 10) && zeek::util::streq(revoke_reason, "(UNKNOWN)") ) { |
482 | 0 | revoke_reason = reason == 9 ? "privilegeWithdrawn" : "aACompromise"; |
483 | 0 | } |
484 | 0 | #endif |
485 | 0 | rvl.emplace_back(make_intrusive<StringVal>(strlen(revoke_reason), revoke_reason)); |
486 | 0 | } |
487 | 0 | else |
488 | 0 | rvl.emplace_back(make_intrusive<StringVal>(0, "")); |
489 | 0 | } |
490 | 0 | else { |
491 | 0 | rvl.emplace_back(make_intrusive<TimeVal>(0.0)); |
492 | 0 | rvl.emplace_back(make_intrusive<StringVal>(0, "")); |
493 | 0 | } |
494 | |
|
495 | 0 | if ( this_update ) |
496 | 0 | rvl.emplace_back(make_intrusive<TimeVal>(GetTimeFromAsn1(this_update, GetFile(), reporter))); |
497 | 0 | else |
498 | 0 | rvl.emplace_back(make_intrusive<TimeVal>(0.0)); |
499 | |
|
500 | 0 | if ( next_update ) |
501 | 0 | rvl.emplace_back(make_intrusive<TimeVal>(GetTimeFromAsn1(next_update, GetFile(), reporter))); |
502 | 0 | else |
503 | 0 | rvl.emplace_back(make_intrusive<TimeVal>(0.0)); |
504 | |
|
505 | 0 | if ( ocsp_response_certificate ) |
506 | 0 | event_mgr.Enqueue(ocsp_response_certificate, std::move(rvl)); |
507 | |
|
508 | 0 | num_ext = OCSP_SINGLERESP_get_ext_count(single_resp); |
509 | 0 | for ( int k = 0; k < num_ext; ++k ) { |
510 | 0 | auto* ex = OCSP_SINGLERESP_get_ext(single_resp, k); |
511 | 0 | if ( ! ex ) |
512 | 0 | continue; |
513 | | |
514 | 0 | ParseExtension(ex, ocsp_extension, false); |
515 | 0 | } |
516 | 0 | } |
517 | |
|
518 | 0 | vl.emplace_back(parse_basic_resp_sig_alg(basic_resp, bio, buf, sizeof(buf))); |
519 | | |
520 | | // i2a_ASN1_OBJECT(bio, basic_resp->signature); |
521 | | // len = BIO_read(bio, buf, sizeof(buf)); |
522 | | // ocsp_resp_record->Assign(7, make_intrusive<StringVal>(len, buf)); |
523 | | // BIO_reset(bio); |
524 | |
|
525 | 0 | certs_vector = new VectorVal(id::find_type<VectorType>("x509_opaque_vector")); |
526 | 0 | vl.emplace_back(AdoptRef{}, certs_vector); |
527 | |
|
528 | 0 | certs = OCSP_resp_get0_certs(basic_resp); |
529 | |
|
530 | 0 | if ( certs ) { |
531 | 0 | int num_certs = sk_X509_num(certs); |
532 | 0 | for ( int i = 0; i < num_certs; i++ ) { |
533 | 0 | ::X509* this_cert = X509_dup(helper_sk_X509_value(certs, i)); |
534 | | //::X509 *this_cert = X509_dup(sk_X509_value(certs, i)); |
535 | 0 | if ( this_cert ) |
536 | 0 | certs_vector->Assign(i, make_intrusive<X509Val>(this_cert)); |
537 | 0 | else |
538 | 0 | reporter->Weird("OpenSSL returned null certificate"); |
539 | 0 | } |
540 | 0 | } |
541 | |
|
542 | 0 | if ( ocsp_response_bytes ) |
543 | 0 | event_mgr.Enqueue(ocsp_response_bytes, std::move(vl)); |
544 | | |
545 | | // ok, now that we are done with the actual certificate - let's parse extensions :) |
546 | 0 | num_ext = OCSP_BASICRESP_get_ext_count(basic_resp); |
547 | 0 | for ( int k = 0; k < num_ext; ++k ) { |
548 | 0 | auto* ex = OCSP_BASICRESP_get_ext(basic_resp, k); |
549 | 0 | if ( ! ex ) |
550 | 0 | continue; |
551 | | |
552 | 0 | ParseExtension(ex, ocsp_extension, true); |
553 | 0 | } |
554 | |
|
555 | 0 | clean_up: |
556 | 0 | if ( basic_resp ) |
557 | 0 | OCSP_BASICRESP_free(basic_resp); |
558 | 0 | BIO_free(bio); |
559 | 0 | } |
560 | | |
561 | 0 | void OCSP::ParseExtensionsSpecific(openssl_x509_ext_t* ex, bool global, openssl_asn1_obj_t* ext_asn, const char* oid) { |
562 | | // In OpenSSL 1.0.2+, we can get the extension by using NID_ct_cert_scts. |
563 | | // In OpenSSL <= 1.0.1, this is not yet defined yet, so we have to manually |
564 | | // look it up by performing a string comparison on the oid. |
565 | 0 | #ifdef NID_ct_cert_scts |
566 | 0 | if ( OBJ_obj2nid(ext_asn) == NID_ct_cert_scts ) |
567 | | #else |
568 | | if ( strcmp(oid, "1.3.6.1.4.1.11129.2.4.5") == 0 ) |
569 | | #endif |
570 | 0 | ParseSignedCertificateTimestamps(ex); |
571 | 0 | } |
572 | | |
573 | | } // namespace zeek::file_analysis::detail |