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