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