xref: /openssl/include/internal/quic_types.h (revision 29fbdfaf)
1 /*
2  * Copyright 2022-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 #ifndef OSSL_QUIC_TYPES_H
11 # define OSSL_QUIC_TYPES_H
12 
13 # include <openssl/ssl.h>
14 # include <internal/ssl.h>
15 # include <assert.h>
16 # include <string.h>
17 
18 # ifndef OPENSSL_NO_QUIC
19 
20 /* QUIC encryption levels. */
21 enum {
22     QUIC_ENC_LEVEL_INITIAL = 0,
23     QUIC_ENC_LEVEL_HANDSHAKE,
24     QUIC_ENC_LEVEL_0RTT,
25     QUIC_ENC_LEVEL_1RTT,
26     QUIC_ENC_LEVEL_NUM       /* Must be the ultimate entry */
27 };
28 
29 /* QUIC packet number spaces. */
30 enum {
31     QUIC_PN_SPACE_INITIAL = 0,
32     QUIC_PN_SPACE_HANDSHAKE,
33     /* New entries must go here, so that QUIC_PN_SPACE_APP is the penultimate */
34     QUIC_PN_SPACE_APP,
35     QUIC_PN_SPACE_NUM       /* Must be the ultimate entry */
36 };
37 
38 static ossl_unused ossl_inline uint32_t
ossl_quic_enc_level_to_pn_space(uint32_t enc_level)39 ossl_quic_enc_level_to_pn_space(uint32_t enc_level)
40 {
41     switch (enc_level) {
42     case QUIC_ENC_LEVEL_INITIAL:
43         return QUIC_PN_SPACE_INITIAL;
44     case QUIC_ENC_LEVEL_HANDSHAKE:
45         return QUIC_PN_SPACE_HANDSHAKE;
46     case QUIC_ENC_LEVEL_0RTT:
47     case QUIC_ENC_LEVEL_1RTT:
48         return QUIC_PN_SPACE_APP;
49     default:
50         assert(0);
51         return QUIC_PN_SPACE_APP;
52     }
53 }
54 
55 /* QUIC packet number representation. */
56 typedef uint64_t QUIC_PN;
57 #  define QUIC_PN_INVALID            UINT64_MAX
58 
ossl_quic_pn_max(QUIC_PN a,QUIC_PN b)59 static ossl_unused ossl_inline QUIC_PN ossl_quic_pn_max(QUIC_PN a, QUIC_PN b)
60 {
61     return a > b ? a : b;
62 }
63 
ossl_quic_pn_min(QUIC_PN a,QUIC_PN b)64 static ossl_unused ossl_inline QUIC_PN ossl_quic_pn_min(QUIC_PN a, QUIC_PN b)
65 {
66     return a < b ? a : b;
67 }
68 
ossl_quic_pn_valid(QUIC_PN pn)69 static ossl_unused ossl_inline int ossl_quic_pn_valid(QUIC_PN pn)
70 {
71     return pn < (((QUIC_PN)1) << 62);
72 }
73 
74 /* QUIC connection ID representation. */
75 #  define QUIC_MAX_CONN_ID_LEN   20
76 #  define QUIC_MIN_ODCID_LEN     8   /* RFC 9000 s. 7.2 */
77 
78 typedef struct quic_conn_id_st {
79     unsigned char id_len, id[QUIC_MAX_CONN_ID_LEN];
80 } QUIC_CONN_ID;
81 
ossl_quic_conn_id_eq(const QUIC_CONN_ID * a,const QUIC_CONN_ID * b)82 static ossl_unused ossl_inline int ossl_quic_conn_id_eq(const QUIC_CONN_ID *a,
83                                                         const QUIC_CONN_ID *b)
84 {
85     if (a->id_len != b->id_len || a->id_len > QUIC_MAX_CONN_ID_LEN)
86         return 0;
87     return memcmp(a->id, b->id, a->id_len) == 0;
88 }
89 
90 /*
91  * Generates a random CID of the given length. libctx may be NULL.
92  * Returns 1 on success or 0 on failure.
93  */
94 int ossl_quic_gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len,
95                                QUIC_CONN_ID *cid);
96 
97 #  define QUIC_MIN_INITIAL_DGRAM_LEN  1200
98 
99 #  define QUIC_DEFAULT_ACK_DELAY_EXP  3
100 #  define QUIC_MAX_ACK_DELAY_EXP      20
101 
102 #  define QUIC_DEFAULT_MAX_ACK_DELAY  25
103 
104 #  define QUIC_MIN_ACTIVE_CONN_ID_LIMIT   2
105 
106 /* Arbitrary choice of default idle timeout (not an RFC value). */
107 #  define QUIC_DEFAULT_IDLE_TIMEOUT   30000
108 
109 #  define QUIC_STATELESS_RESET_TOKEN_LEN    16
110 
111 typedef struct {
112     unsigned char token[QUIC_STATELESS_RESET_TOKEN_LEN];
113 } QUIC_STATELESS_RESET_TOKEN;
114 
115 /*
116  * An encoded preferred_addr transport parameter cannot be shorter or longer
117  * than these lengths in bytes.
118  */
119 #  define QUIC_MIN_ENCODED_PREFERRED_ADDR_LEN   41
120 #  define QUIC_MAX_ENCODED_PREFERRED_ADDR_LEN   61
121 
122 # endif
123 
124 #endif
125