/src/openssl/ssl/quic/quic_lcidm.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023-2026 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/quic_lcidm.h" |
11 | | #include "internal/quic_types.h" |
12 | | #include "internal/quic_vlint.h" |
13 | | #include "internal/common.h" |
14 | | #include "internal/hashfunc.h" |
15 | | #include "crypto/siphash.h" |
16 | | #include <openssl/lhash.h> |
17 | | #include <openssl/rand.h> |
18 | | #include <openssl/err.h> |
19 | | |
20 | | /* |
21 | | * QUIC Local Connection ID Manager |
22 | | * ================================ |
23 | | */ |
24 | | |
25 | | typedef struct quic_lcidm_conn_st QUIC_LCIDM_CONN; |
26 | | |
27 | | enum { |
28 | | LCID_TYPE_ODCID, /* This LCID is the ODCID from the peer */ |
29 | | LCID_TYPE_INITIAL, /* This is our Initial SCID */ |
30 | | LCID_TYPE_NCID /* This LCID was issued via a NCID frame */ |
31 | | }; |
32 | | |
33 | | typedef struct quic_lcid_st { |
34 | | QUIC_CONN_ID cid; |
35 | | uint64_t seq_num; |
36 | | |
37 | | /* copy of the hash key from lcidm */ |
38 | | uint64_t *hash_key; |
39 | | |
40 | | /* Back-pointer to the owning QUIC_LCIDM_CONN structure. */ |
41 | | QUIC_LCIDM_CONN *conn; |
42 | | |
43 | | /* LCID_TYPE_* */ |
44 | | unsigned int type : 2; |
45 | | } QUIC_LCID; |
46 | | |
47 | | DEFINE_LHASH_OF_EX(QUIC_LCID); |
48 | | DEFINE_LHASH_OF_EX(QUIC_LCIDM_CONN); |
49 | | |
50 | | struct quic_lcidm_conn_st { |
51 | | size_t num_active_lcid; |
52 | | LHASH_OF(QUIC_LCID) *lcids; |
53 | | void *opaque; |
54 | | QUIC_LCID *odcid_lcid_obj; |
55 | | uint64_t next_seq_num; |
56 | | |
57 | | /* Have we enrolled an ODCID? */ |
58 | | unsigned int done_odcid : 1; |
59 | | }; |
60 | | |
61 | | struct quic_lcidm_st { |
62 | | OSSL_LIB_CTX *libctx; |
63 | | uint64_t hash_key[2]; /* random key for siphash */ |
64 | | LHASH_OF(QUIC_LCID) *lcids; /* (QUIC_CONN_ID) -> (QUIC_LCID *) */ |
65 | | LHASH_OF(QUIC_LCIDM_CONN) *conns; /* (void *opaque) -> (QUIC_LCIDM_CONN *) */ |
66 | | size_t lcid_len; /* Length in bytes for all LCIDs */ |
67 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
68 | | QUIC_CONN_ID next_lcid; |
69 | | #endif |
70 | | }; |
71 | | |
72 | | static unsigned long lcid_hash(const QUIC_LCID *lcid_obj) |
73 | 0 | { |
74 | 0 | SIPHASH siphash = { |
75 | 0 | 0, |
76 | 0 | }; |
77 | 0 | unsigned long hashval = 0; |
78 | 0 | unsigned char digest[SIPHASH_MIN_DIGEST_SIZE]; |
79 | | |
80 | | /* Use a supported SipHash digest size (8 or 16); 8 is sufficient here. */ |
81 | 0 | if (!SipHash_set_hash_size(&siphash, SIPHASH_MIN_DIGEST_SIZE)) |
82 | 0 | goto out; |
83 | 0 | if (!SipHash_Init(&siphash, (uint8_t *)lcid_obj->hash_key, 0, 0)) |
84 | 0 | goto out; |
85 | 0 | SipHash_Update(&siphash, lcid_obj->cid.id, lcid_obj->cid.id_len); |
86 | 0 | if (!SipHash_Final(&siphash, digest, SIPHASH_MIN_DIGEST_SIZE)) |
87 | 0 | goto out; |
88 | | |
89 | | /* |
90 | | * Truncate the 64-bit SipHash digest into an unsigned long. |
91 | | */ |
92 | 0 | memcpy(&hashval, digest, sizeof(hashval) < sizeof(digest) ? sizeof(hashval) : sizeof(digest)); |
93 | 0 | out: |
94 | 0 | return hashval; |
95 | 0 | } |
96 | | |
97 | | static int lcid_comp(const QUIC_LCID *a, const QUIC_LCID *b) |
98 | 0 | { |
99 | 0 | return !ossl_quic_conn_id_eq(&a->cid, &b->cid); |
100 | 0 | } |
101 | | |
102 | | static unsigned long lcidm_conn_hash(const QUIC_LCIDM_CONN *conn) |
103 | 0 | { |
104 | 0 | return ossl_lh_hash_ptr(conn->opaque); |
105 | 0 | } |
106 | | |
107 | | static int lcidm_conn_comp(const QUIC_LCIDM_CONN *a, const QUIC_LCIDM_CONN *b) |
108 | 0 | { |
109 | 0 | return a->opaque != b->opaque; |
110 | 0 | } |
111 | | |
112 | | QUIC_LCIDM *ossl_quic_lcidm_new(OSSL_LIB_CTX *libctx, size_t lcid_len) |
113 | 0 | { |
114 | 0 | QUIC_LCIDM *lcidm = NULL; |
115 | |
|
116 | 0 | if (lcid_len > QUIC_MAX_CONN_ID_LEN) |
117 | 0 | goto err; |
118 | | |
119 | 0 | if ((lcidm = OPENSSL_zalloc(sizeof(*lcidm))) == NULL) |
120 | 0 | goto err; |
121 | | |
122 | | /* generate a random key for the hash tables hash function */ |
123 | 0 | if (!RAND_bytes_ex(libctx, (unsigned char *)&lcidm->hash_key, |
124 | 0 | sizeof(uint64_t) * 2, 0)) |
125 | 0 | goto err; |
126 | | |
127 | 0 | if ((lcidm->lcids = lh_QUIC_LCID_new(lcid_hash, lcid_comp)) == NULL) |
128 | 0 | goto err; |
129 | | |
130 | 0 | if ((lcidm->conns = lh_QUIC_LCIDM_CONN_new(lcidm_conn_hash, |
131 | 0 | lcidm_conn_comp)) |
132 | 0 | == NULL) |
133 | 0 | goto err; |
134 | | |
135 | 0 | lcidm->libctx = libctx; |
136 | 0 | lcidm->lcid_len = lcid_len; |
137 | 0 | return lcidm; |
138 | | |
139 | 0 | err: |
140 | 0 | if (lcidm != NULL) { |
141 | 0 | lh_QUIC_LCID_free(lcidm->lcids); |
142 | 0 | lh_QUIC_LCIDM_CONN_free(lcidm->conns); |
143 | 0 | OPENSSL_free(lcidm); |
144 | 0 | } |
145 | 0 | return NULL; |
146 | 0 | } |
147 | | |
148 | | static void lcidm_delete_conn(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn); |
149 | | |
150 | | static void lcidm_delete_conn_(QUIC_LCIDM_CONN *conn, void *arg) |
151 | 0 | { |
152 | 0 | lcidm_delete_conn((QUIC_LCIDM *)arg, conn); |
153 | 0 | } |
154 | | |
155 | | void ossl_quic_lcidm_free(QUIC_LCIDM *lcidm) |
156 | 0 | { |
157 | 0 | if (lcidm == NULL) |
158 | 0 | return; |
159 | | |
160 | | /* |
161 | | * Calling OPENSSL_lh_delete during a doall call is unsafe with our |
162 | | * current LHASH implementation for several reasons: |
163 | | * |
164 | | * - firstly, because deletes can cause the hashtable to be contracted, |
165 | | * resulting in rehashing which might cause items in later buckets to |
166 | | * move to earlier buckets, which might cause doall to skip an item, |
167 | | * resulting in a memory leak; |
168 | | * |
169 | | * - secondly, because doall in general is not safe across hashtable |
170 | | * size changes, as it caches hashtable size and pointer values |
171 | | * while operating. |
172 | | * |
173 | | * The fix for this is to disable hashtable contraction using the following |
174 | | * call, which guarantees that no rehashing will occur so long as we only |
175 | | * call delete and not insert. |
176 | | */ |
177 | 0 | lh_QUIC_LCIDM_CONN_set_down_load(lcidm->conns, 0); |
178 | |
|
179 | 0 | lh_QUIC_LCIDM_CONN_doall_arg(lcidm->conns, lcidm_delete_conn_, lcidm); |
180 | |
|
181 | 0 | lh_QUIC_LCID_free(lcidm->lcids); |
182 | 0 | lh_QUIC_LCIDM_CONN_free(lcidm->conns); |
183 | 0 | OPENSSL_free(lcidm); |
184 | 0 | } |
185 | | |
186 | | static QUIC_LCID *lcidm_get0_lcid(const QUIC_LCIDM *lcidm, const QUIC_CONN_ID *lcid) |
187 | 0 | { |
188 | 0 | QUIC_LCID key; |
189 | |
|
190 | 0 | key.cid = *lcid; |
191 | 0 | key.hash_key = (uint64_t *)lcidm->hash_key; |
192 | |
|
193 | 0 | if (key.cid.id_len > QUIC_MAX_CONN_ID_LEN) |
194 | 0 | return NULL; |
195 | | |
196 | 0 | return lh_QUIC_LCID_retrieve(lcidm->lcids, &key); |
197 | 0 | } |
198 | | |
199 | | static QUIC_LCIDM_CONN *lcidm_get0_conn(const QUIC_LCIDM *lcidm, void *opaque) |
200 | 0 | { |
201 | 0 | QUIC_LCIDM_CONN key; |
202 | |
|
203 | 0 | key.opaque = opaque; |
204 | |
|
205 | 0 | return lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key); |
206 | 0 | } |
207 | | |
208 | | static QUIC_LCIDM_CONN *lcidm_upsert_conn(const QUIC_LCIDM *lcidm, void *opaque) |
209 | 0 | { |
210 | 0 | QUIC_LCIDM_CONN *conn = lcidm_get0_conn(lcidm, opaque); |
211 | |
|
212 | 0 | if (conn != NULL) |
213 | 0 | return conn; |
214 | | |
215 | 0 | if ((conn = OPENSSL_zalloc(sizeof(*conn))) == NULL) |
216 | 0 | goto err; |
217 | | |
218 | 0 | if ((conn->lcids = lh_QUIC_LCID_new(lcid_hash, lcid_comp)) == NULL) |
219 | 0 | goto err; |
220 | | |
221 | 0 | conn->opaque = opaque; |
222 | |
|
223 | 0 | lh_QUIC_LCIDM_CONN_insert(lcidm->conns, conn); |
224 | 0 | if (lh_QUIC_LCIDM_CONN_error(lcidm->conns)) |
225 | 0 | goto err; |
226 | | |
227 | 0 | return conn; |
228 | | |
229 | 0 | err: |
230 | 0 | if (conn != NULL) { |
231 | 0 | lh_QUIC_LCID_free(conn->lcids); |
232 | 0 | OPENSSL_free(conn); |
233 | 0 | } |
234 | 0 | return NULL; |
235 | 0 | } |
236 | | |
237 | | static void lcidm_delete_conn_lcid(QUIC_LCIDM *lcidm, QUIC_LCID *lcid_obj) |
238 | 0 | { |
239 | 0 | lh_QUIC_LCID_delete(lcidm->lcids, lcid_obj); |
240 | 0 | lh_QUIC_LCID_delete(lcid_obj->conn->lcids, lcid_obj); |
241 | 0 | assert(lcid_obj->conn->num_active_lcid > 0); |
242 | 0 | --lcid_obj->conn->num_active_lcid; |
243 | 0 | OPENSSL_free(lcid_obj); |
244 | 0 | } |
245 | | |
246 | | /* doall_arg wrapper */ |
247 | | static void lcidm_delete_conn_lcid_(QUIC_LCID *lcid_obj, void *arg) |
248 | 0 | { |
249 | 0 | lcidm_delete_conn_lcid((QUIC_LCIDM *)arg, lcid_obj); |
250 | 0 | } |
251 | | |
252 | | static void lcidm_delete_conn(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn) |
253 | 0 | { |
254 | | /* See comment in ossl_quic_lcidm_free */ |
255 | 0 | lh_QUIC_LCID_set_down_load(conn->lcids, 0); |
256 | |
|
257 | 0 | lh_QUIC_LCID_doall_arg(conn->lcids, lcidm_delete_conn_lcid_, lcidm); |
258 | 0 | lh_QUIC_LCIDM_CONN_delete(lcidm->conns, conn); |
259 | 0 | lh_QUIC_LCID_free(conn->lcids); |
260 | 0 | OPENSSL_free(conn); |
261 | 0 | } |
262 | | |
263 | | static QUIC_LCID *lcidm_conn_new_lcid(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn, |
264 | | const QUIC_CONN_ID *lcid) |
265 | 0 | { |
266 | 0 | QUIC_LCID *lcid_obj = NULL; |
267 | |
|
268 | 0 | if (lcid->id_len > QUIC_MAX_CONN_ID_LEN) |
269 | 0 | return NULL; |
270 | | |
271 | 0 | if ((lcid_obj = OPENSSL_zalloc(sizeof(*lcid_obj))) == NULL) |
272 | 0 | goto err; |
273 | | |
274 | 0 | lcid_obj->cid = *lcid; |
275 | 0 | lcid_obj->conn = conn; |
276 | 0 | lcid_obj->hash_key = lcidm->hash_key; |
277 | |
|
278 | 0 | lh_QUIC_LCID_insert(conn->lcids, lcid_obj); |
279 | 0 | if (lh_QUIC_LCID_error(conn->lcids)) |
280 | 0 | goto err; |
281 | | |
282 | 0 | lh_QUIC_LCID_insert(lcidm->lcids, lcid_obj); |
283 | 0 | if (lh_QUIC_LCID_error(lcidm->lcids)) { |
284 | 0 | lh_QUIC_LCID_delete(conn->lcids, lcid_obj); |
285 | 0 | goto err; |
286 | 0 | } |
287 | | |
288 | 0 | ++conn->num_active_lcid; |
289 | 0 | return lcid_obj; |
290 | | |
291 | 0 | err: |
292 | 0 | OPENSSL_free(lcid_obj); |
293 | 0 | return NULL; |
294 | 0 | } |
295 | | |
296 | | size_t ossl_quic_lcidm_get_lcid_len(const QUIC_LCIDM *lcidm) |
297 | 0 | { |
298 | 0 | return lcidm->lcid_len; |
299 | 0 | } |
300 | | |
301 | | size_t ossl_quic_lcidm_get_num_active_lcid(const QUIC_LCIDM *lcidm, |
302 | | void *opaque) |
303 | 0 | { |
304 | 0 | QUIC_LCIDM_CONN *conn; |
305 | |
|
306 | 0 | conn = lcidm_get0_conn(lcidm, opaque); |
307 | 0 | if (conn == NULL) |
308 | 0 | return 0; |
309 | | |
310 | 0 | return conn->num_active_lcid; |
311 | 0 | } |
312 | | |
313 | | static int lcidm_generate_cid(QUIC_LCIDM *lcidm, |
314 | | QUIC_CONN_ID *cid) |
315 | 0 | { |
316 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
317 | 0 | int i; |
318 | |
|
319 | 0 | lcidm->next_lcid.id_len = (unsigned char)lcidm->lcid_len; |
320 | 0 | *cid = lcidm->next_lcid; |
321 | |
|
322 | 0 | for (i = lcidm->lcid_len - 1; i >= 0; --i) |
323 | 0 | if (++lcidm->next_lcid.id[i] != 0) |
324 | 0 | break; |
325 | |
|
326 | 0 | return 1; |
327 | | #else |
328 | | return ossl_quic_gen_rand_conn_id(lcidm->libctx, lcidm->lcid_len, cid); |
329 | | #endif |
330 | 0 | } |
331 | | |
332 | | static int lcidm_generate(QUIC_LCIDM *lcidm, |
333 | | void *opaque, |
334 | | unsigned int type, |
335 | | QUIC_CONN_ID *lcid_out, |
336 | | uint64_t *seq_num) |
337 | 0 | { |
338 | 0 | QUIC_LCIDM_CONN *conn; |
339 | 0 | QUIC_LCID key, *lcid_obj; |
340 | 0 | size_t i; |
341 | 0 | #define MAX_RETRIES 8 |
342 | |
|
343 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
344 | 0 | return 0; |
345 | | |
346 | 0 | if ((type == LCID_TYPE_INITIAL && conn->next_seq_num > 0) |
347 | 0 | || conn->next_seq_num > OSSL_QUIC_VLINT_MAX) |
348 | 0 | return 0; |
349 | | |
350 | 0 | i = 0; |
351 | 0 | do { |
352 | 0 | if (i++ >= MAX_RETRIES) |
353 | | /* |
354 | | * Too many retries; should not happen but if it does, don't loop |
355 | | * endlessly. |
356 | | */ |
357 | 0 | return 0; |
358 | | |
359 | 0 | if (!lcidm_generate_cid(lcidm, lcid_out)) |
360 | 0 | return 0; |
361 | | |
362 | 0 | key.cid = *lcid_out; |
363 | 0 | key.hash_key = lcidm->hash_key; |
364 | | |
365 | | /* If a collision occurs, retry. */ |
366 | 0 | } while (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL); |
367 | | |
368 | 0 | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid_out)) == NULL) |
369 | 0 | return 0; |
370 | | |
371 | 0 | lcid_obj->seq_num = conn->next_seq_num; |
372 | 0 | lcid_obj->type = type; |
373 | |
|
374 | 0 | if (seq_num != NULL) |
375 | 0 | *seq_num = lcid_obj->seq_num; |
376 | |
|
377 | 0 | ++conn->next_seq_num; |
378 | 0 | return 1; |
379 | 0 | } |
380 | | |
381 | | int ossl_quic_lcidm_enrol_odcid(QUIC_LCIDM *lcidm, |
382 | | void *opaque, |
383 | | const QUIC_CONN_ID *initial_odcid) |
384 | 0 | { |
385 | 0 | QUIC_LCIDM_CONN *conn; |
386 | 0 | QUIC_LCID key, *lcid_obj; |
387 | |
|
388 | 0 | if (initial_odcid == NULL || initial_odcid->id_len < QUIC_MIN_ODCID_LEN |
389 | 0 | || initial_odcid->id_len > QUIC_MAX_CONN_ID_LEN) |
390 | 0 | return 0; |
391 | | |
392 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
393 | 0 | return 0; |
394 | | |
395 | 0 | if (conn->done_odcid) |
396 | 0 | return 0; |
397 | | |
398 | 0 | key.cid = *initial_odcid; |
399 | 0 | key.hash_key = lcidm->hash_key; |
400 | 0 | if (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL) |
401 | 0 | return 0; |
402 | | |
403 | 0 | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, initial_odcid)) == NULL) |
404 | 0 | return 0; |
405 | | |
406 | 0 | lcid_obj->seq_num = LCIDM_ODCID_SEQ_NUM; |
407 | 0 | lcid_obj->type = LCID_TYPE_ODCID; |
408 | |
|
409 | 0 | conn->odcid_lcid_obj = lcid_obj; |
410 | 0 | conn->done_odcid = 1; |
411 | 0 | return 1; |
412 | 0 | } |
413 | | |
414 | | int ossl_quic_lcidm_generate_initial(QUIC_LCIDM *lcidm, |
415 | | void *opaque, |
416 | | QUIC_CONN_ID *initial_lcid) |
417 | 0 | { |
418 | 0 | return lcidm_generate(lcidm, opaque, LCID_TYPE_INITIAL, |
419 | 0 | initial_lcid, NULL); |
420 | 0 | } |
421 | | |
422 | | int ossl_quic_lcidm_bind_channel(QUIC_LCIDM *lcidm, void *opaque, |
423 | | const QUIC_CONN_ID *lcid) |
424 | 0 | { |
425 | 0 | QUIC_LCIDM_CONN *conn; |
426 | 0 | QUIC_LCID *lcid_obj; |
427 | | |
428 | | /* |
429 | | * the plan is simple: |
430 | | * make sure the lcid is still unused. |
431 | | * do the same business as ossl_quic_lcidm_gnerate_initial() does, |
432 | | * except we will use lcid instead of generating a new one. |
433 | | */ |
434 | 0 | if (ossl_quic_lcidm_lookup(lcidm, lcid, NULL, NULL) != 0) |
435 | 0 | return 0; |
436 | | |
437 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
438 | 0 | return 0; |
439 | | |
440 | 0 | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid)) == NULL) { |
441 | 0 | lcidm_delete_conn(lcidm, conn); |
442 | 0 | return 0; |
443 | 0 | } |
444 | | |
445 | 0 | lcid_obj->seq_num = conn->next_seq_num; |
446 | 0 | lcid_obj->type = LCID_TYPE_INITIAL; |
447 | 0 | conn->next_seq_num++; |
448 | |
|
449 | 0 | return 1; |
450 | 0 | } |
451 | | |
452 | | int ossl_quic_lcidm_generate(QUIC_LCIDM *lcidm, |
453 | | void *opaque, |
454 | | OSSL_QUIC_FRAME_NEW_CONN_ID *ncid_frame) |
455 | 0 | { |
456 | 0 | ncid_frame->seq_num = 0; |
457 | 0 | ncid_frame->retire_prior_to = 0; |
458 | |
|
459 | 0 | return lcidm_generate(lcidm, opaque, LCID_TYPE_NCID, |
460 | 0 | &ncid_frame->conn_id, |
461 | 0 | &ncid_frame->seq_num); |
462 | 0 | } |
463 | | |
464 | | int ossl_quic_lcidm_retire_odcid(QUIC_LCIDM *lcidm, void *opaque) |
465 | 0 | { |
466 | 0 | QUIC_LCIDM_CONN *conn; |
467 | |
|
468 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
469 | 0 | return 0; |
470 | | |
471 | 0 | if (conn->odcid_lcid_obj == NULL) |
472 | 0 | return 0; |
473 | | |
474 | 0 | lcidm_delete_conn_lcid(lcidm, conn->odcid_lcid_obj); |
475 | 0 | conn->odcid_lcid_obj = NULL; |
476 | 0 | return 1; |
477 | 0 | } |
478 | | |
479 | | struct retire_args { |
480 | | QUIC_LCID *earliest_seq_num_lcid_obj; |
481 | | uint64_t earliest_seq_num, retire_prior_to; |
482 | | }; |
483 | | |
484 | | static void retire_for_conn(QUIC_LCID *lcid_obj, void *arg) |
485 | 0 | { |
486 | 0 | struct retire_args *args = arg; |
487 | | |
488 | | /* ODCID LCID cannot be retired via this API */ |
489 | 0 | if (lcid_obj->type == LCID_TYPE_ODCID |
490 | 0 | || lcid_obj->seq_num >= args->retire_prior_to) |
491 | 0 | return; |
492 | | |
493 | 0 | if (lcid_obj->seq_num < args->earliest_seq_num) { |
494 | 0 | args->earliest_seq_num = lcid_obj->seq_num; |
495 | 0 | args->earliest_seq_num_lcid_obj = lcid_obj; |
496 | 0 | } |
497 | 0 | } |
498 | | |
499 | | int ossl_quic_lcidm_retire(QUIC_LCIDM *lcidm, |
500 | | void *opaque, |
501 | | uint64_t retire_prior_to, |
502 | | const QUIC_CONN_ID *containing_pkt_dcid, |
503 | | QUIC_CONN_ID *retired_lcid, |
504 | | uint64_t *retired_seq_num, |
505 | | int *did_retire) |
506 | 0 | { |
507 | 0 | QUIC_LCIDM_CONN key, *conn; |
508 | 0 | struct retire_args args = { 0 }; |
509 | |
|
510 | 0 | key.opaque = opaque; |
511 | |
|
512 | 0 | if (did_retire == NULL) |
513 | 0 | return 0; |
514 | | |
515 | 0 | *did_retire = 0; |
516 | 0 | if ((conn = lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key)) == NULL) |
517 | 0 | return 1; |
518 | | |
519 | 0 | args.retire_prior_to = retire_prior_to; |
520 | 0 | args.earliest_seq_num = UINT64_MAX; |
521 | |
|
522 | 0 | lh_QUIC_LCID_doall_arg(conn->lcids, retire_for_conn, &args); |
523 | 0 | if (args.earliest_seq_num_lcid_obj == NULL) |
524 | 0 | return 1; |
525 | | |
526 | 0 | if (containing_pkt_dcid != NULL |
527 | 0 | && ossl_quic_conn_id_eq(&args.earliest_seq_num_lcid_obj->cid, |
528 | 0 | containing_pkt_dcid)) |
529 | 0 | return 0; |
530 | | |
531 | 0 | *did_retire = 1; |
532 | 0 | if (retired_lcid != NULL) |
533 | 0 | *retired_lcid = args.earliest_seq_num_lcid_obj->cid; |
534 | 0 | if (retired_seq_num != NULL) |
535 | 0 | *retired_seq_num = args.earliest_seq_num_lcid_obj->seq_num; |
536 | |
|
537 | 0 | lcidm_delete_conn_lcid(lcidm, args.earliest_seq_num_lcid_obj); |
538 | 0 | return 1; |
539 | 0 | } |
540 | | |
541 | | int ossl_quic_lcidm_cull(QUIC_LCIDM *lcidm, void *opaque) |
542 | 0 | { |
543 | 0 | QUIC_LCIDM_CONN key, *conn; |
544 | |
|
545 | 0 | key.opaque = opaque; |
546 | |
|
547 | 0 | if ((conn = lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key)) == NULL) |
548 | 0 | return 0; |
549 | | |
550 | 0 | lcidm_delete_conn(lcidm, conn); |
551 | 0 | return 1; |
552 | 0 | } |
553 | | |
554 | | int ossl_quic_lcidm_lookup(QUIC_LCIDM *lcidm, |
555 | | const QUIC_CONN_ID *lcid, |
556 | | uint64_t *seq_num, |
557 | | void **opaque) |
558 | 0 | { |
559 | 0 | QUIC_LCID *lcid_obj; |
560 | |
|
561 | 0 | if (lcid == NULL) |
562 | 0 | return 0; |
563 | | |
564 | 0 | if ((lcid_obj = lcidm_get0_lcid(lcidm, lcid)) == NULL) |
565 | 0 | return 0; |
566 | | |
567 | 0 | if (seq_num != NULL) |
568 | 0 | *seq_num = lcid_obj->seq_num; |
569 | |
|
570 | 0 | if (opaque != NULL) |
571 | 0 | *opaque = lcid_obj->conn->opaque; |
572 | |
|
573 | 0 | return 1; |
574 | 0 | } |
575 | | |
576 | | int ossl_quic_lcidm_debug_remove(QUIC_LCIDM *lcidm, |
577 | | const QUIC_CONN_ID *lcid) |
578 | 0 | { |
579 | 0 | QUIC_LCID key, *lcid_obj; |
580 | |
|
581 | 0 | key.cid = *lcid; |
582 | 0 | key.hash_key = lcidm->hash_key; |
583 | 0 | if ((lcid_obj = lh_QUIC_LCID_retrieve(lcidm->lcids, &key)) == NULL) |
584 | 0 | return 0; |
585 | | |
586 | 0 | lcidm_delete_conn_lcid(lcidm, lcid_obj); |
587 | 0 | return 1; |
588 | 0 | } |
589 | | |
590 | | int ossl_quic_lcidm_debug_add(QUIC_LCIDM *lcidm, void *opaque, |
591 | | const QUIC_CONN_ID *lcid, |
592 | | uint64_t seq_num) |
593 | 0 | { |
594 | 0 | QUIC_LCIDM_CONN *conn; |
595 | 0 | QUIC_LCID key, *lcid_obj; |
596 | |
|
597 | 0 | if (lcid == NULL || lcid->id_len > QUIC_MAX_CONN_ID_LEN) |
598 | 0 | return 0; |
599 | | |
600 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
601 | 0 | return 0; |
602 | | |
603 | 0 | key.cid = *lcid; |
604 | 0 | key.hash_key = lcidm->hash_key; |
605 | 0 | if (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL) |
606 | 0 | return 0; |
607 | | |
608 | 0 | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid)) == NULL) |
609 | 0 | return 0; |
610 | | |
611 | 0 | lcid_obj->seq_num = seq_num; |
612 | 0 | lcid_obj->type = LCID_TYPE_NCID; |
613 | 0 | return 1; |
614 | 0 | } |
615 | | |
616 | | int ossl_quic_lcidm_get_unused_cid(QUIC_LCIDM *lcidm, QUIC_CONN_ID *cid) |
617 | 0 | { |
618 | 0 | int i; |
619 | |
|
620 | 0 | for (i = 0; i < 10; i++) { |
621 | 0 | if (lcidm_generate_cid(lcidm, cid) |
622 | 0 | && lcidm_get0_lcid(lcidm, cid) == NULL) |
623 | 0 | return 1; /* not found <=> radomly generated cid is unused */ |
624 | 0 | } |
625 | | |
626 | 0 | return 0; |
627 | 0 | } |