/src/openssl/ssl/quic/qlog.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2023-2024 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 "internal/qlog.h" |
11 | | #include "internal/json_enc.h" |
12 | | #include "internal/common.h" |
13 | | #include "internal/cryptlib.h" |
14 | | #include "crypto/ctype.h" |
15 | | |
16 | 0 | #define BITS_PER_WORD (sizeof(size_t) * 8) |
17 | | #define NUM_ENABLED_W ((QLOG_EVENT_TYPE_NUM + BITS_PER_WORD - 1) / BITS_PER_WORD) |
18 | | |
19 | | static ossl_unused ossl_inline int bit_get(const size_t *p, uint32_t bit_no) |
20 | 0 | { |
21 | 0 | return p[bit_no / BITS_PER_WORD] & (((size_t)1) << (bit_no % BITS_PER_WORD)); |
22 | 0 | } |
23 | | |
24 | | static ossl_unused ossl_inline void bit_set(size_t *p, uint32_t bit_no, int enable) |
25 | 0 | { |
26 | 0 | size_t mask = (((size_t)1) << (bit_no % BITS_PER_WORD)); |
27 | |
|
28 | 0 | if (enable) |
29 | 0 | p[bit_no / BITS_PER_WORD] |= mask; |
30 | 0 | else |
31 | 0 | p[bit_no / BITS_PER_WORD] &= ~mask; |
32 | 0 | } |
33 | | |
34 | | struct qlog_st { |
35 | | QLOG_TRACE_INFO info; |
36 | | |
37 | | BIO *bio; |
38 | | size_t enabled[NUM_ENABLED_W]; |
39 | | uint32_t event_type; |
40 | | const char *event_cat, *event_name, *event_combined_name; |
41 | | OSSL_TIME event_time, prev_event_time; |
42 | | OSSL_JSON_ENC json; |
43 | | int header_done, first_event_done; |
44 | | }; |
45 | | |
46 | | static OSSL_TIME default_now(void *arg) |
47 | 0 | { |
48 | 0 | return ossl_time_now(); |
49 | 0 | } |
50 | | |
51 | | /* |
52 | | * Construction |
53 | | * ============ |
54 | | */ |
55 | | QLOG *ossl_qlog_new(const QLOG_TRACE_INFO *info) |
56 | 0 | { |
57 | 0 | QLOG *qlog = OPENSSL_zalloc(sizeof(QLOG)); |
58 | |
|
59 | 0 | if (qlog == NULL) |
60 | 0 | return NULL; |
61 | | |
62 | 0 | qlog->info.odcid = info->odcid; |
63 | 0 | qlog->info.is_server = info->is_server; |
64 | 0 | qlog->info.now_cb = info->now_cb; |
65 | 0 | qlog->info.now_cb_arg = info->now_cb_arg; |
66 | 0 | qlog->info.override_process_id = info->override_process_id; |
67 | |
|
68 | 0 | if (info->title != NULL |
69 | 0 | && (qlog->info.title = OPENSSL_strdup(info->title)) == NULL) |
70 | 0 | goto err; |
71 | | |
72 | 0 | if (info->description != NULL |
73 | 0 | && (qlog->info.description = OPENSSL_strdup(info->description)) == NULL) |
74 | 0 | goto err; |
75 | | |
76 | 0 | if (info->group_id != NULL |
77 | 0 | && (qlog->info.group_id = OPENSSL_strdup(info->group_id)) == NULL) |
78 | 0 | goto err; |
79 | | |
80 | 0 | if (info->override_impl_name != NULL |
81 | 0 | && (qlog->info.override_impl_name |
82 | 0 | = OPENSSL_strdup(info->override_impl_name)) == NULL) |
83 | 0 | goto err; |
84 | | |
85 | 0 | if (!ossl_json_init(&qlog->json, NULL, |
86 | 0 | OSSL_JSON_FLAG_IJSON | OSSL_JSON_FLAG_SEQ)) |
87 | 0 | goto err; |
88 | | |
89 | 0 | if (qlog->info.now_cb == NULL) |
90 | 0 | qlog->info.now_cb = default_now; |
91 | |
|
92 | 0 | return qlog; |
93 | | |
94 | 0 | err: |
95 | 0 | if (qlog != NULL) { |
96 | 0 | OPENSSL_free((char *)qlog->info.title); |
97 | 0 | OPENSSL_free((char *)qlog->info.description); |
98 | 0 | OPENSSL_free((char *)qlog->info.group_id); |
99 | 0 | OPENSSL_free((char *)qlog->info.override_impl_name); |
100 | 0 | OPENSSL_free(qlog); |
101 | 0 | } |
102 | 0 | return NULL; |
103 | 0 | } |
104 | | |
105 | | QLOG *ossl_qlog_new_from_env(const QLOG_TRACE_INFO *info) |
106 | 0 | { |
107 | 0 | QLOG *qlog = NULL; |
108 | 0 | const char *qlogdir = ossl_safe_getenv("QLOGDIR"); |
109 | 0 | const char *qfilter = ossl_safe_getenv("OSSL_QFILTER"); |
110 | 0 | char qlogdir_sep, *filename = NULL; |
111 | 0 | size_t i, l, strl; |
112 | |
|
113 | 0 | if (info == NULL || qlogdir == NULL) |
114 | 0 | return NULL; |
115 | | |
116 | 0 | l = strlen(qlogdir); |
117 | 0 | if (l == 0) |
118 | 0 | return NULL; |
119 | | |
120 | 0 | qlogdir_sep = ossl_determine_dirsep(qlogdir); |
121 | | |
122 | | /* dir; [sep]; ODCID; _; strlen("client" / "server"); strlen(".sqlog"); NUL */ |
123 | 0 | strl = l + 1 + info->odcid.id_len * 2 + 1 + 6 + 6 + 1; |
124 | 0 | filename = OPENSSL_malloc(strl); |
125 | 0 | if (filename == NULL) |
126 | 0 | return NULL; |
127 | | |
128 | 0 | memcpy(filename, qlogdir, l); |
129 | 0 | if (qlogdir_sep != '\0') |
130 | 0 | filename[l++] = qlogdir_sep; |
131 | |
|
132 | 0 | for (i = 0; i < info->odcid.id_len; ++i) |
133 | 0 | l += BIO_snprintf(filename + l, strl - l, "%02x", info->odcid.id[i]); |
134 | |
|
135 | 0 | l += BIO_snprintf(filename + l, strl - l, "_%s.sqlog", |
136 | 0 | info->is_server ? "server" : "client"); |
137 | |
|
138 | 0 | qlog = ossl_qlog_new(info); |
139 | 0 | if (qlog == NULL) |
140 | 0 | goto err; |
141 | | |
142 | 0 | if (!ossl_qlog_set_sink_filename(qlog, filename)) |
143 | 0 | goto err; |
144 | | |
145 | 0 | if (qfilter == NULL || qfilter[0] == '\0') |
146 | 0 | qfilter = "*"; |
147 | |
|
148 | 0 | if (!ossl_qlog_set_filter(qlog, qfilter)) |
149 | 0 | goto err; |
150 | | |
151 | 0 | OPENSSL_free(filename); |
152 | 0 | return qlog; |
153 | | |
154 | 0 | err: |
155 | 0 | OPENSSL_free(filename); |
156 | 0 | ossl_qlog_free(qlog); |
157 | 0 | return NULL; |
158 | 0 | } |
159 | | |
160 | | void ossl_qlog_free(QLOG *qlog) |
161 | 0 | { |
162 | 0 | if (qlog == NULL) |
163 | 0 | return; |
164 | | |
165 | 0 | ossl_json_flush_cleanup(&qlog->json); |
166 | 0 | BIO_free_all(qlog->bio); |
167 | 0 | OPENSSL_free((char *)qlog->info.title); |
168 | 0 | OPENSSL_free((char *)qlog->info.description); |
169 | 0 | OPENSSL_free((char *)qlog->info.group_id); |
170 | 0 | OPENSSL_free((char *)qlog->info.override_impl_name); |
171 | 0 | OPENSSL_free(qlog); |
172 | 0 | } |
173 | | |
174 | | /* |
175 | | * Configuration |
176 | | * ============= |
177 | | */ |
178 | | int ossl_qlog_set_sink_bio(QLOG *qlog, BIO *bio) |
179 | 0 | { |
180 | 0 | if (qlog == NULL) |
181 | 0 | return 0; |
182 | | |
183 | 0 | ossl_qlog_flush(qlog); /* best effort */ |
184 | 0 | BIO_free_all(qlog->bio); |
185 | 0 | qlog->bio = bio; |
186 | 0 | ossl_json_set0_sink(&qlog->json, bio); |
187 | 0 | return 1; |
188 | 0 | } |
189 | | |
190 | | #ifndef OPENSSL_NO_STDIO |
191 | | |
192 | | int ossl_qlog_set_sink_file(QLOG *qlog, FILE *f, int close_flag) |
193 | 0 | { |
194 | 0 | BIO *bio; |
195 | |
|
196 | 0 | if (qlog == NULL) |
197 | 0 | return 0; |
198 | | |
199 | 0 | bio = BIO_new_fp(f, BIO_CLOSE); |
200 | 0 | if (bio == NULL) |
201 | 0 | return 0; |
202 | | |
203 | 0 | if (!ossl_qlog_set_sink_bio(qlog, bio)) { |
204 | 0 | BIO_free_all(bio); |
205 | 0 | return 0; |
206 | 0 | } |
207 | | |
208 | 0 | return 1; |
209 | 0 | } |
210 | | |
211 | | #endif |
212 | | |
213 | | int ossl_qlog_set_sink_filename(QLOG *qlog, const char *filename) |
214 | 0 | { |
215 | 0 | BIO *bio; |
216 | |
|
217 | 0 | if (qlog == NULL) |
218 | 0 | return 0; |
219 | | |
220 | | /* |
221 | | * We supply our own text encoding as JSON requires UTF-8, so disable any |
222 | | * OS-specific processing here. |
223 | | */ |
224 | 0 | bio = BIO_new_file(filename, "wb"); |
225 | 0 | if (bio == NULL) |
226 | 0 | return 0; |
227 | | |
228 | 0 | if (!ossl_qlog_set_sink_bio(qlog, bio)) { |
229 | 0 | BIO_free_all(bio); |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | 0 | return 1; |
234 | 0 | } |
235 | | |
236 | | int ossl_qlog_flush(QLOG *qlog) |
237 | 0 | { |
238 | 0 | if (qlog == NULL) |
239 | 0 | return 1; |
240 | | |
241 | 0 | return ossl_json_flush(&qlog->json); |
242 | 0 | } |
243 | | |
244 | | int ossl_qlog_set_event_type_enabled(QLOG *qlog, uint32_t event_type, |
245 | | int enabled) |
246 | 0 | { |
247 | 0 | if (qlog == NULL || event_type >= QLOG_EVENT_TYPE_NUM) |
248 | 0 | return 0; |
249 | | |
250 | 0 | bit_set(qlog->enabled, event_type, enabled); |
251 | 0 | return 1; |
252 | 0 | } |
253 | | |
254 | | int ossl_qlog_enabled(QLOG *qlog, uint32_t event_type) |
255 | 0 | { |
256 | 0 | if (qlog == NULL) |
257 | 0 | return 0; |
258 | | |
259 | 0 | return bit_get(qlog->enabled, event_type) != 0; |
260 | 0 | } |
261 | | |
262 | | /* |
263 | | * Event Lifecycle |
264 | | * =============== |
265 | | */ |
266 | | static void write_str_once(QLOG *qlog, const char *key, char **p) |
267 | 0 | { |
268 | 0 | if (*p == NULL) |
269 | 0 | return; |
270 | | |
271 | 0 | ossl_json_key(&qlog->json, key); |
272 | 0 | ossl_json_str(&qlog->json, *p); |
273 | |
|
274 | 0 | OPENSSL_free(*p); |
275 | 0 | *p = NULL; |
276 | 0 | } |
277 | | |
278 | | static void qlog_event_seq_header(QLOG *qlog) |
279 | 0 | { |
280 | 0 | if (qlog->header_done) |
281 | 0 | return; |
282 | | |
283 | 0 | ossl_json_object_begin(&qlog->json); |
284 | 0 | { |
285 | 0 | ossl_json_key(&qlog->json, "qlog_version"); |
286 | 0 | ossl_json_str(&qlog->json, "0.3"); |
287 | |
|
288 | 0 | ossl_json_key(&qlog->json, "qlog_format"); |
289 | 0 | ossl_json_str(&qlog->json, "JSON-SEQ"); |
290 | |
|
291 | 0 | write_str_once(qlog, "title", (char **)&qlog->info.title); |
292 | 0 | write_str_once(qlog, "description", (char **)&qlog->info.description); |
293 | |
|
294 | 0 | ossl_json_key(&qlog->json, "trace"); |
295 | 0 | ossl_json_object_begin(&qlog->json); |
296 | 0 | { |
297 | 0 | ossl_json_key(&qlog->json, "common_fields"); |
298 | 0 | ossl_json_object_begin(&qlog->json); |
299 | 0 | { |
300 | 0 | ossl_json_key(&qlog->json, "time_format"); |
301 | 0 | ossl_json_str(&qlog->json, "delta"); |
302 | |
|
303 | 0 | ossl_json_key(&qlog->json, "protocol_type"); |
304 | 0 | ossl_json_array_begin(&qlog->json); |
305 | 0 | { |
306 | 0 | ossl_json_str(&qlog->json, "QUIC"); |
307 | 0 | } /* protocol_type */ |
308 | 0 | ossl_json_array_end(&qlog->json); |
309 | |
|
310 | 0 | write_str_once(qlog, "group_id", (char **)&qlog->info.group_id); |
311 | |
|
312 | 0 | ossl_json_key(&qlog->json, "system_info"); |
313 | 0 | ossl_json_object_begin(&qlog->json); |
314 | 0 | { |
315 | 0 | if (qlog->info.override_process_id != 0) { |
316 | 0 | ossl_json_key(&qlog->json, "process_id"); |
317 | 0 | ossl_json_u64(&qlog->json, qlog->info.override_process_id); |
318 | 0 | } else { |
319 | 0 | #if defined(OPENSSL_SYS_UNIX) |
320 | 0 | ossl_json_key(&qlog->json, "process_id"); |
321 | 0 | ossl_json_u64(&qlog->json, (uint64_t)getpid()); |
322 | | #elif defined(OPENSSL_SYS_WINDOWS) |
323 | | ossl_json_key(&qlog->json, "process_id"); |
324 | | ossl_json_u64(&qlog->json, (uint64_t)GetCurrentProcessId()); |
325 | | #endif |
326 | 0 | } |
327 | 0 | } /* system_info */ |
328 | 0 | ossl_json_object_end(&qlog->json); |
329 | 0 | } /* common_fields */ |
330 | 0 | ossl_json_object_end(&qlog->json); |
331 | |
|
332 | 0 | ossl_json_key(&qlog->json, "vantage_point"); |
333 | 0 | ossl_json_object_begin(&qlog->json); |
334 | 0 | { |
335 | 0 | char buf[128]; |
336 | 0 | const char *p = buf; |
337 | |
|
338 | 0 | if (qlog->info.override_impl_name != NULL) { |
339 | 0 | p = qlog->info.override_impl_name; |
340 | 0 | } else { |
341 | 0 | BIO_snprintf(buf, sizeof(buf), "OpenSSL/%s (%s)", |
342 | 0 | OpenSSL_version(OPENSSL_FULL_VERSION_STRING), |
343 | 0 | OpenSSL_version(OPENSSL_PLATFORM) + 10); |
344 | 0 | } |
345 | |
|
346 | 0 | ossl_json_key(&qlog->json, "type"); |
347 | 0 | ossl_json_str(&qlog->json, |
348 | 0 | qlog->info.is_server ? "server" : "client"); |
349 | |
|
350 | 0 | ossl_json_key(&qlog->json, "name"); |
351 | 0 | ossl_json_str(&qlog->json, p); |
352 | 0 | } /* vantage_point */ |
353 | 0 | ossl_json_object_end(&qlog->json); |
354 | 0 | } /* trace */ |
355 | 0 | ossl_json_object_end(&qlog->json); |
356 | 0 | } |
357 | 0 | ossl_json_object_end(&qlog->json); |
358 | |
|
359 | 0 | qlog->header_done = 1; |
360 | 0 | } |
361 | | |
362 | | static void qlog_event_prologue(QLOG *qlog) |
363 | 0 | { |
364 | 0 | qlog_event_seq_header(qlog); |
365 | |
|
366 | 0 | ossl_json_object_begin(&qlog->json); |
367 | |
|
368 | 0 | ossl_json_key(&qlog->json, "name"); |
369 | 0 | ossl_json_str(&qlog->json, qlog->event_combined_name); |
370 | |
|
371 | 0 | ossl_json_key(&qlog->json, "data"); |
372 | 0 | ossl_json_object_begin(&qlog->json); |
373 | 0 | } |
374 | | |
375 | | static void qlog_event_epilogue(QLOG *qlog) |
376 | 0 | { |
377 | 0 | ossl_json_object_end(&qlog->json); |
378 | |
|
379 | 0 | ossl_json_key(&qlog->json, "time"); |
380 | 0 | if (!qlog->first_event_done) { |
381 | 0 | ossl_json_u64(&qlog->json, ossl_time2ms(qlog->event_time)); |
382 | 0 | qlog->prev_event_time = qlog->event_time; |
383 | 0 | qlog->first_event_done = 1; |
384 | 0 | } else { |
385 | 0 | OSSL_TIME delta = ossl_time_subtract(qlog->event_time, |
386 | 0 | qlog->prev_event_time); |
387 | |
|
388 | 0 | ossl_json_u64(&qlog->json, ossl_time2ms(delta)); |
389 | 0 | qlog->prev_event_time = qlog->event_time; |
390 | 0 | } |
391 | |
|
392 | 0 | ossl_json_object_end(&qlog->json); |
393 | 0 | } |
394 | | |
395 | | int ossl_qlog_event_try_begin(QLOG *qlog, |
396 | | uint32_t event_type, |
397 | | const char *event_cat, |
398 | | const char *event_name, |
399 | | const char *event_combined_name) |
400 | 0 | { |
401 | 0 | if (qlog == NULL) |
402 | 0 | return 0; |
403 | | |
404 | 0 | if (!ossl_assert(qlog->event_type == QLOG_EVENT_TYPE_NONE) |
405 | 0 | || !ossl_qlog_enabled(qlog, event_type)) |
406 | 0 | return 0; |
407 | | |
408 | 0 | qlog->event_type = event_type; |
409 | 0 | qlog->event_cat = event_cat; |
410 | 0 | qlog->event_name = event_name; |
411 | 0 | qlog->event_combined_name = event_combined_name; |
412 | 0 | qlog->event_time = qlog->info.now_cb(qlog->info.now_cb_arg); |
413 | |
|
414 | 0 | qlog_event_prologue(qlog); |
415 | 0 | return 1; |
416 | 0 | } |
417 | | |
418 | | void ossl_qlog_event_end(QLOG *qlog) |
419 | 0 | { |
420 | 0 | if (!ossl_assert(qlog != NULL && qlog->event_type != QLOG_EVENT_TYPE_NONE)) |
421 | 0 | return; |
422 | | |
423 | 0 | qlog_event_epilogue(qlog); |
424 | 0 | qlog->event_type = QLOG_EVENT_TYPE_NONE; |
425 | 0 | } |
426 | | |
427 | | /* |
428 | | * Field Generators |
429 | | * ================ |
430 | | */ |
431 | | void ossl_qlog_group_begin(QLOG *qlog, const char *name) |
432 | 0 | { |
433 | 0 | if (name != NULL) |
434 | 0 | ossl_json_key(&qlog->json, name); |
435 | |
|
436 | 0 | ossl_json_object_begin(&qlog->json); |
437 | 0 | } |
438 | | |
439 | | void ossl_qlog_group_end(QLOG *qlog) |
440 | 0 | { |
441 | 0 | ossl_json_object_end(&qlog->json); |
442 | 0 | } |
443 | | |
444 | | void ossl_qlog_array_begin(QLOG *qlog, const char *name) |
445 | 0 | { |
446 | 0 | if (name != NULL) |
447 | 0 | ossl_json_key(&qlog->json, name); |
448 | |
|
449 | 0 | ossl_json_array_begin(&qlog->json); |
450 | 0 | } |
451 | | |
452 | | void ossl_qlog_array_end(QLOG *qlog) |
453 | 0 | { |
454 | 0 | ossl_json_array_end(&qlog->json); |
455 | 0 | } |
456 | | |
457 | | void ossl_qlog_override_time(QLOG *qlog, OSSL_TIME event_time) |
458 | 0 | { |
459 | 0 | qlog->event_time = event_time; |
460 | 0 | } |
461 | | |
462 | | void ossl_qlog_str(QLOG *qlog, const char *name, const char *value) |
463 | 0 | { |
464 | 0 | if (name != NULL) |
465 | 0 | ossl_json_key(&qlog->json, name); |
466 | |
|
467 | 0 | ossl_json_str(&qlog->json, value); |
468 | 0 | } |
469 | | |
470 | | void ossl_qlog_str_len(QLOG *qlog, const char *name, |
471 | | const char *value, size_t value_len) |
472 | 0 | { |
473 | 0 | if (name != NULL) |
474 | 0 | ossl_json_key(&qlog->json, name); |
475 | |
|
476 | 0 | ossl_json_str_len(&qlog->json, value, value_len); |
477 | 0 | } |
478 | | |
479 | | void ossl_qlog_u64(QLOG *qlog, const char *name, uint64_t value) |
480 | 0 | { |
481 | 0 | if (name != NULL) |
482 | 0 | ossl_json_key(&qlog->json, name); |
483 | |
|
484 | 0 | ossl_json_u64(&qlog->json, value); |
485 | 0 | } |
486 | | |
487 | | void ossl_qlog_i64(QLOG *qlog, const char *name, int64_t value) |
488 | 0 | { |
489 | 0 | if (name != NULL) |
490 | 0 | ossl_json_key(&qlog->json, name); |
491 | |
|
492 | 0 | ossl_json_i64(&qlog->json, value); |
493 | 0 | } |
494 | | |
495 | | void ossl_qlog_bool(QLOG *qlog, const char *name, int value) |
496 | 0 | { |
497 | 0 | if (name != NULL) |
498 | 0 | ossl_json_key(&qlog->json, name); |
499 | |
|
500 | 0 | ossl_json_bool(&qlog->json, value); |
501 | 0 | } |
502 | | |
503 | | void ossl_qlog_bin(QLOG *qlog, const char *name, |
504 | | const void *value, size_t value_len) |
505 | 0 | { |
506 | 0 | if (name != NULL) |
507 | 0 | ossl_json_key(&qlog->json, name); |
508 | |
|
509 | 0 | ossl_json_str_hex(&qlog->json, value, value_len); |
510 | 0 | } |
511 | | |
512 | | /* |
513 | | * Filter Parsing |
514 | | * ============== |
515 | | */ |
516 | | struct lexer { |
517 | | const char *p, *term_end, *end; |
518 | | }; |
519 | | |
520 | | static ossl_inline int is_term_sep_ws(char c) |
521 | 0 | { |
522 | 0 | return c == ' ' || c == '\r' || c == '\n' || c == '\t'; |
523 | 0 | } |
524 | | |
525 | | static ossl_inline int is_name_char(char c) |
526 | 0 | { |
527 | 0 | return ossl_isalpha(c) || ossl_isdigit(c) || c == '_' || c == '-'; |
528 | 0 | } |
529 | | |
530 | | static int lex_init(struct lexer *lex, const char *in, size_t in_len) |
531 | 0 | { |
532 | 0 | if (in == NULL) |
533 | 0 | return 0; |
534 | | |
535 | 0 | lex->p = in; |
536 | 0 | lex->term_end = in; |
537 | 0 | lex->end = in + in_len; |
538 | 0 | return 1; |
539 | 0 | } |
540 | | |
541 | | static int lex_do(struct lexer *lex) |
542 | 0 | { |
543 | 0 | const char *p = lex->term_end, *end = lex->end, *term_end; |
544 | |
|
545 | 0 | for (; is_term_sep_ws(*p) && p < end; ++p); |
546 | |
|
547 | 0 | if (p == end) { |
548 | 0 | lex->p = end; |
549 | 0 | lex->term_end = end; |
550 | 0 | return 0; |
551 | 0 | } |
552 | | |
553 | 0 | for (term_end = p; !is_term_sep_ws(*term_end) && term_end < end; ++term_end); |
554 | |
|
555 | 0 | lex->p = p; |
556 | 0 | lex->term_end = term_end; |
557 | 0 | return 1; |
558 | 0 | } |
559 | | |
560 | | static int lex_eot(struct lexer *lex) |
561 | 0 | { |
562 | 0 | return lex->p == lex->term_end; |
563 | 0 | } |
564 | | |
565 | | static int lex_peek_char(struct lexer *lex) |
566 | 0 | { |
567 | 0 | return lex_eot(lex) ? -1 : *lex->p; |
568 | 0 | } |
569 | | |
570 | | static int lex_skip_char(struct lexer *lex) |
571 | 0 | { |
572 | 0 | if (lex_eot(lex)) |
573 | 0 | return 0; |
574 | | |
575 | 0 | ++lex->p; |
576 | 0 | return 1; |
577 | 0 | } |
578 | | |
579 | | static int lex_match(struct lexer *lex, const char *s, size_t s_len) |
580 | 0 | { |
581 | 0 | if ((size_t)(lex->term_end - lex->p) != s_len) |
582 | 0 | return 0; |
583 | | |
584 | 0 | if (memcmp(lex->p, s, s_len)) |
585 | 0 | return 0; |
586 | | |
587 | 0 | return 1; |
588 | 0 | } |
589 | | |
590 | | static void lex_get_rest(struct lexer *lex, const char **str, size_t *str_l) |
591 | 0 | { |
592 | 0 | *str = lex->p; |
593 | 0 | *str_l = lex->term_end - lex->p; |
594 | 0 | } |
595 | | |
596 | | static int lex_extract_to(struct lexer *lex, char c, |
597 | | const char **str, size_t *str_l) |
598 | 0 | { |
599 | 0 | const char *p = lex->p, *term_end = lex->term_end, *s; |
600 | |
|
601 | 0 | for (s = p; s < term_end && *s != c; ++s); |
602 | 0 | if (s == term_end) |
603 | 0 | return 0; |
604 | | |
605 | 0 | *str = p; |
606 | 0 | *str_l = s - p; |
607 | 0 | lex->p = ++s; |
608 | 0 | return 1; |
609 | 0 | } |
610 | | |
611 | | static int ossl_unused filter_match_event(const char *cat, size_t cat_l, |
612 | | const char *event, size_t event_l, |
613 | | const char *expect_cat, |
614 | | const char *expect_event) |
615 | 0 | { |
616 | 0 | size_t expect_cat_l = strlen(expect_cat); |
617 | 0 | size_t expect_event_l = strlen(expect_event); |
618 | |
|
619 | 0 | if ((cat != NULL && cat_l != expect_cat_l) |
620 | 0 | || (event != NULL && event_l != expect_event_l) |
621 | 0 | || (cat != NULL && memcmp(cat, expect_cat, expect_cat_l)) |
622 | 0 | || (event != NULL && memcmp(event, expect_event, expect_event_l))) |
623 | 0 | return 0; |
624 | | |
625 | 0 | return 1; |
626 | 0 | } |
627 | | |
628 | | /* |
629 | | * enabled: event enablement bitmask Array of size NUM_ENABLED_W. |
630 | | * add: 1 to enable an event, 0 to disable. |
631 | | * cat: Category name/length. Not necessarily zero terminated. |
632 | | * NULL to match any. |
633 | | * event: Event name/length. Not necessarily zero terminated. |
634 | | * NULL to match any. |
635 | | */ |
636 | | static void filter_apply(size_t *enabled, int add, |
637 | | const char *cat, size_t cat_l, |
638 | | const char *event, size_t event_l) |
639 | 0 | { |
640 | | /* Find events which match the given filters. */ |
641 | 0 | # define QLOG_EVENT(e_cat, e_name) \ |
642 | 0 | if (filter_match_event(cat, cat_l, event, event_l, \ |
643 | 0 | #e_cat, #e_name)) \ |
644 | 0 | bit_set(enabled, QLOG_EVENT_TYPE_##e_cat##_##e_name, add); |
645 | 0 | # include "internal/qlog_events.h" |
646 | 0 | # undef QLOG_EVENT |
647 | 0 | } |
648 | | |
649 | | static int lex_fail(struct lexer *lex, const char *msg) |
650 | 0 | { |
651 | | /* |
652 | | * TODO(QLOG FUTURE): Determine how to print log messages about bad filter |
653 | | * strings |
654 | | */ |
655 | 0 | lex->p = lex->term_end = lex->end; |
656 | 0 | return 0; |
657 | 0 | } |
658 | | |
659 | | static int validate_name(const char **p, size_t *l) |
660 | 0 | { |
661 | 0 | const char *p_ = *p; |
662 | 0 | size_t i, l_ = *l; |
663 | |
|
664 | 0 | if (l_ == 1 && *p_ == '*') { |
665 | 0 | *p = NULL; |
666 | 0 | *l = 0; |
667 | 0 | return 1; |
668 | 0 | } |
669 | | |
670 | 0 | if (l_ == 0) |
671 | 0 | return 0; |
672 | | |
673 | 0 | for (i = 0; i < l_; ++i) |
674 | 0 | if (!is_name_char(p_[i])) |
675 | 0 | return 0; |
676 | | |
677 | 0 | return 1; |
678 | 0 | } |
679 | | |
680 | | int ossl_qlog_set_filter(QLOG *qlog, const char *filter) |
681 | 0 | { |
682 | 0 | struct lexer lex = {0}; |
683 | 0 | char c; |
684 | 0 | const char *cat, *event; |
685 | 0 | size_t cat_l, event_l, enabled[NUM_ENABLED_W]; |
686 | 0 | int add; |
687 | |
|
688 | 0 | memcpy(enabled, qlog->enabled, sizeof(enabled)); |
689 | |
|
690 | 0 | if (!lex_init(&lex, filter, strlen(filter))) |
691 | 0 | return 0; |
692 | | |
693 | 0 | while (lex_do(&lex)) { |
694 | 0 | c = lex_peek_char(&lex); |
695 | 0 | if (c == '+' || c == '-') { |
696 | 0 | add = (c == '+'); |
697 | 0 | lex_skip_char(&lex); |
698 | |
|
699 | 0 | c = lex_peek_char(&lex); |
700 | 0 | if (!is_name_char(c) && c != '*') |
701 | 0 | return lex_fail(&lex, "expected alphanumeric name or '*'" |
702 | 0 | " after +/-"); |
703 | 0 | } else if (!is_name_char(c) && c != '*') { |
704 | 0 | return lex_fail(&lex, "expected +/- or alphanumeric name or '*'"); |
705 | 0 | } else { |
706 | 0 | add = 1; |
707 | 0 | } |
708 | | |
709 | 0 | if (lex_match(&lex, "*", 1)) { |
710 | 0 | filter_apply(enabled, add, NULL, 0, NULL, 0); |
711 | 0 | continue; |
712 | 0 | } |
713 | | |
714 | 0 | if (!lex_extract_to(&lex, ':', &cat, &cat_l)) |
715 | 0 | return lex_fail(&lex, "expected ':' after category name"); |
716 | | |
717 | 0 | lex_get_rest(&lex, &event, &event_l); |
718 | 0 | if (!validate_name(&cat, &cat_l)) |
719 | 0 | return lex_fail(&lex, "expected alphanumeric category name or '*'"); |
720 | 0 | if (!validate_name(&event, &event_l)) |
721 | 0 | return lex_fail(&lex, "expected alphanumeric event name or '*'"); |
722 | | |
723 | 0 | filter_apply(enabled, add, cat, cat_l, event, event_l); |
724 | 0 | } |
725 | | |
726 | 0 | memcpy(qlog->enabled, enabled, sizeof(enabled)); |
727 | 0 | return 1; |
728 | 0 | } |