1 /*
2 * Copyright (c) 2020 Fastly, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 #ifndef quicly_cid_h
23 #define quicly_cid_h
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #include <stdint.h>
30 #include <string.h>
31 #include "picotls.h"
32 #include "quicly/constants.h"
33
34 /**
35 * Guard value. We would never send path_id of this value.
36 */
37 #define QUICLY_MAX_PATH_ID UINT8_MAX
38
39 typedef struct st_quicly_cid_t {
40 uint8_t cid[QUICLY_MAX_CID_LEN_V1];
41 uint8_t len;
42 } quicly_cid_t;
43
44 /**
45 * The structure of CID issued by quicly.
46 *
47 * Authentication of the CID can be done by validating if server_id and thread_id contain correct values.
48 */
49 typedef struct st_quicly_cid_plaintext_t {
50 /**
51 * the internal "connection ID" unique to each connection (rather than QUIC's CID being unique to each path)
52 */
53 uint32_t master_id;
54 /**
55 * path ID of the connection; we issue up to 255 CIDs per connection (see QUICLY_MAX_PATH_ID)
56 */
57 uint32_t path_id : 8;
58 /**
59 * for intra-node routing
60 */
61 uint32_t thread_id : 24;
62 /**
63 * for inter-node routing; available only when using a 16-byte cipher to encrypt CIDs, otherwise set to zero.
64 */
65 uint64_t node_id;
66 } quicly_cid_plaintext_t;
67
68 /**
69 * A CID used for indicating that a CID is invalid. Both .node_id and .thread_id are set to their maximum. Therefore, mappings must
70 * refrain from using those values.
71 */
72 extern quicly_cid_plaintext_t quicly_cid_plaintext_invalid;
73
74 /**
75 * CID encryption
76 */
77 typedef struct st_quicly_cid_encryptor_t {
78 /**
79 * encrypts CID and optionally generates a stateless reset token
80 */
81 void (*encrypt_cid)(struct st_quicly_cid_encryptor_t *self, quicly_cid_t *encrypted, void *stateless_reset_token,
82 const quicly_cid_plaintext_t *plaintext);
83 /**
84 * decrypts a CID
85 * @param plaintext if successful, the decoded CID will be written
86 * @param encrypt the encrypted CID
87 * @param len length of the encrypted CID when the packet is a long header packet, or 0 if it is a short header packet
88 * @return length of the CID if successful, or SIZE_MAX if failed
89 */
90 size_t (*decrypt_cid)(struct st_quicly_cid_encryptor_t *self, quicly_cid_plaintext_t *plaintext, const void *encrypted,
91 size_t len);
92 /**
93 * generates a stateless reset token (returns if generated)
94 */
95 int (*generate_stateless_reset_token)(struct st_quicly_cid_encryptor_t *self, void *token, const void *cid);
96 } quicly_cid_encryptor_t;
97
98 static void quicly_set_cid(quicly_cid_t *dest, ptls_iovec_t src);
99 static int quicly_cid_is_equal(const quicly_cid_t *cid, ptls_iovec_t vec);
100
101 /* inline functions */
102
quicly_cid_is_equal(const quicly_cid_t * cid,ptls_iovec_t vec)103 inline int quicly_cid_is_equal(const quicly_cid_t *cid, ptls_iovec_t vec)
104 {
105 return cid->len == vec.len && memcmp(cid->cid, vec.base, vec.len) == 0;
106 }
107
quicly_set_cid(quicly_cid_t * dest,ptls_iovec_t src)108 inline void quicly_set_cid(quicly_cid_t *dest, ptls_iovec_t src)
109 {
110 memcpy(dest->cid, src.base, src.len);
111 dest->len = src.len;
112 }
113
114 #ifdef __cplusplus
115 }
116 #endif
117
118 #endif
119