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