Coverage Report

Created: 2025-10-13 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/picotls/include/picotls.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
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 picotls_h
23
#define picotls_h
24
25
#ifdef __cplusplus
26
extern "C" {
27
#endif
28
29
#ifdef _WINDOWS
30
#include "wincompat.h"
31
#endif
32
33
#include <assert.h>
34
#include <inttypes.h>
35
#include <string.h>
36
#include <sys/types.h>
37
#ifndef _WINDOWS
38
#include <netinet/in.h>
39
#include <arpa/inet.h>
40
#endif
41
42
#if __GNUC__ >= 3
43
6.66k
#define PTLS_LIKELY(x) __builtin_expect(!!(x), 1)
44
50.8k
#define PTLS_UNLIKELY(x) __builtin_expect(!!(x), 0)
45
983
#define PTLS_BUILD_ASSERT_EXPR(cond) (sizeof(char[2 * !!(!__builtin_constant_p(cond) || (cond)) - 1]) != 0)
46
0
#define PTLS_BUILD_ASSERT(cond) ((void)PTLS_BUILD_ASSERT_EXPR(cond))
47
#else
48
#define PTLS_LIKELY(x) (x)
49
#define PTLS_UNLIKELY(x) (x)
50
#define PTLS_BUILD_ASSERT(cond) 1
51
#endif
52
53
/* __builtin_types_compatible_p yields incorrect results when older versions of GCC is used; see #303.
54
 * Clang with Xcode 9.4 or prior is known to not work correctly when a pointer is const-qualified; see
55
 * https://github.com/h2o/quicly/pull/306#issuecomment-626037269. Older versions of clang upstream works fine, but we do not need
56
 * best coverage. This macro is for preventing misuse going into the master branch, having it work one of the compilers supported in
57
 * our CI is enough.
58
 */
59
#if ((defined(__clang__) && __clang_major__ >= 10) || __GNUC__ >= 6) && !defined(__cplusplus)
60
983
#define PTLS_ASSERT_IS_ARRAY_EXPR(a) PTLS_BUILD_ASSERT_EXPR(__builtin_types_compatible_p(__typeof__(a[0])[], __typeof__(a)))
61
#else
62
#define PTLS_ASSERT_IS_ARRAY_EXPR(a) 1
63
#endif
64
65
983
#define PTLS_ELEMENTSOF(x) (PTLS_ASSERT_IS_ARRAY_EXPR(x) * sizeof(x) / sizeof((x)[0]))
66
67
#ifdef _WINDOWS
68
#define PTLS_THREADLOCAL __declspec(thread)
69
#else
70
#define PTLS_THREADLOCAL __thread
71
#endif
72
73
#ifndef PTLS_HAVE_LOG
74
#ifdef _WINDOWS
75
#define PTLS_HAVE_LOG 0
76
#else
77
#define PTLS_HAVE_LOG 1
78
#endif
79
#endif
80
81
#ifndef PTLS_FUZZ_HANDSHAKE
82
#define PTLS_FUZZ_HANDSHAKE 0
83
#endif
84
85
5.90k
#define PTLS_HELLO_RANDOM_SIZE 32
86
87
0
#define PTLS_AES128_KEY_SIZE 16
88
#define PTLS_AES256_KEY_SIZE 32
89
0
#define PTLS_AES_BLOCK_SIZE 16
90
#define PTLS_AES_IV_SIZE 16
91
#define PTLS_AESGCM_IV_SIZE 12
92
#define PTLS_AESGCM_TAG_SIZE 16
93
#define PTLS_AESGCM_CONFIDENTIALITY_LIMIT 0x2000000            /* 2^25 */
94
#define PTLS_AESGCM_INTEGRITY_LIMIT UINT64_C(0x40000000000000) /* 2^54 */
95
#define PTLS_AESCCM_CONFIDENTIALITY_LIMIT 0xB504F3             /* 2^23.5 */
96
#define PTLS_AESCCM_INTEGRITY_LIMIT 0xB504F3                   /* 2^23.5 */
97
98
#define PTLS_CHACHA20_KEY_SIZE 32
99
#define PTLS_CHACHA20_IV_SIZE 16 /* contrary to RFC 7539, follow OpenSSL way of using first 32 bits as ctr and latter 96 as IV */
100
#define PTLS_CHACHA20POLY1305_IV_SIZE 12
101
#define PTLS_CHACHA20POLY1305_TAG_SIZE 16
102
#define PTLS_CHACHA20POLY1305_CONFIDENTIALITY_LIMIT UINT64_MAX       /* at least 2^64 */
103
#define PTLS_CHACHA20POLY1305_INTEGRITY_LIMIT UINT64_C(0x1000000000) /* 2^36 */
104
105
#define PTLS_AEGIS128L_KEY_SIZE 16
106
#define PTLS_AEGIS128L_IV_SIZE 16
107
#define PTLS_AEGIS128L_TAG_SIZE 16
108
#define PTLS_AEGIS128L_CONFIDENTIALITY_LIMIT UINT64_MAX          /* at least 2^64 */
109
#define PTLS_AEGIS128L_INTEGRITY_LIMIT UINT64_C(0x1000000000000) /* 2^48 */
110
111
#define PTLS_AEGIS256_KEY_SIZE 32
112
#define PTLS_AEGIS256_IV_SIZE 32
113
#define PTLS_AEGIS256_TAG_SIZE 16
114
#define PTLS_AEGIS256_CONFIDENTIALITY_LIMIT UINT64_MAX          /* at least 2^64 */
115
#define PTLS_AEGIS256_INTEGRITY_LIMIT UINT64_C(0x1000000000000) /* 2^48 */
116
117
#define PTLS_BLOWFISH_KEY_SIZE 16
118
#define PTLS_BLOWFISH_BLOCK_SIZE 8
119
120
#define PTLS_QUICLB_KEY_SIZE 16 /* same as the underlying aes128ecb */
121
0
#define PTLS_QUICLB_MIN_BLOCK_SIZE 7
122
#define PTLS_QUICLB_MAX_BLOCK_SIZE 19 /* inclusive */
123
#define PTLS_QUICLB_DEFAULT_BLOCK_SIZE                                                                                             \
124
    8 /* when the quiclb cipher is used, the blob passed to ptls_cipher_encrypt can be anything between the min and max above;     \
125
         however, 8 is the default set in `ptls_cipher_algorithm_t::block_size` */
126
127
#define PTLS_SHA256_BLOCK_SIZE 64
128
#define PTLS_SHA256_DIGEST_SIZE 32
129
130
#define PTLS_SHA384_BLOCK_SIZE 128
131
#define PTLS_SHA384_DIGEST_SIZE 48
132
133
#define PTLS_SHA512_BLOCK_SIZE 128
134
#define PTLS_SHA512_DIGEST_SIZE 64
135
136
0
#define PTLS_MAX_SECRET_SIZE 32
137
#define PTLS_MAX_IV_SIZE 32
138
0
#define PTLS_MAX_DIGEST_SIZE 64
139
140
/* versions */
141
0
#define PTLS_PROTOCOL_VERSION_TLS12 0x0303
142
0
#define PTLS_PROTOCOL_VERSION_TLS13 0x0304
143
144
/* cipher-suites */
145
#define PTLS_CIPHER_SUITE_AES_128_GCM_SHA256 0x1301
146
#define PTLS_CIPHER_SUITE_NAME_AES_128_GCM_SHA256 "TLS_AES_128_GCM_SHA256"
147
#define PTLS_CIPHER_SUITE_AES_256_GCM_SHA384 0x1302
148
#define PTLS_CIPHER_SUITE_NAME_AES_256_GCM_SHA384 "TLS_AES_256_GCM_SHA384"
149
0
#define PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256 0x1303
150
#define PTLS_CIPHER_SUITE_NAME_CHACHA20_POLY1305_SHA256 "TLS_CHACHA20_POLY1305_SHA256"
151
#define PTLS_CIPHER_SUITE_AEGIS256_SHA512 0x1306
152
#define PTLS_CIPHER_SUITE_NAME_AEGIS256_SHA512 "TLS_AEGIS_256_SHA512"
153
#define PTLS_CIPHER_SUITE_AEGIS128L_SHA256 0x1307
154
#define PTLS_CIPHER_SUITE_NAME_AEGIS128L_SHA256 "TLS_AEGIS_128L_SHA256"
155
156
/* TLS/1.2 cipher-suites that we support (for compatibility, OpenSSL names are used) */
157
#define PTLS_CIPHER_SUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xc02b
158
#define PTLS_CIPHER_SUITE_NAME_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 "ECDHE-ECDSA-AES128-GCM-SHA256"
159
#define PTLS_CIPHER_SUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xc02c
160
#define PTLS_CIPHER_SUITE_NAME_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 "ECDHE-ECDSA-AES256-GCM-SHA384"
161
#define PTLS_CIPHER_SUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xc02f
162
#define PTLS_CIPHER_SUITE_NAME_ECDHE_RSA_WITH_AES_128_GCM_SHA256 "ECDHE-RSA-AES128-GCM-SHA256"
163
#define PTLS_CIPHER_SUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xc030
164
#define PTLS_CIPHER_SUITE_NAME_ECDHE_RSA_WITH_AES_256_GCM_SHA384 "ECDHE-RSA-AES256-GCM-SHA384"
165
#define PTLS_CIPHER_SUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xcca8
166
#define PTLS_CIPHER_SUITE_NAME_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 "ECDHE-RSA-CHACHA20-POLY1305"
167
#define PTLS_CIPHER_SUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 0xcca9
168
#define PTLS_CIPHER_SUITE_NAME_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 "ECDHE-ECDSA-CHACHA20-POLY1305"
169
170
/* negotiated_groups */
171
#define PTLS_GROUP_SECP256R1 23
172
#define PTLS_GROUP_NAME_SECP256R1 "secp256r1"
173
#define PTLS_GROUP_SECP384R1 24
174
#define PTLS_GROUP_NAME_SECP384R1 "secp384r1"
175
#define PTLS_GROUP_SECP521R1 25
176
#define PTLS_GROUP_NAME_SECP521R1 "secp521r1"
177
#define PTLS_GROUP_X25519 29
178
#define PTLS_GROUP_NAME_X25519 "x25519"
179
#define PTLS_GROUP_X448 30
180
#define PTLS_GROUP_NAME_X448 "x448"
181
#define PTLS_GROUP_SECP256R1MLKEM768 4587
182
#define PTLS_GROUP_NAME_SECP256R1MLKEM768 "SecP256r1MLKEM768"
183
#define PTLS_GROUP_X25519MLKEM768 4588
184
#define PTLS_GROUP_NAME_X25519MLKEM768 "X25519MLKEM768"
185
#define PTLS_GROUP_SECP384R1MLKEM1024 4589
186
#define PTLS_GROUP_NAME_SECP384R1MLKEM1024 "SecP384r1MLKEM1024"
187
#define PTLS_GROUP_MLKEM512 512
188
#define PTLS_GROUP_NAME_MLKEM512 "MLKEM512"
189
#define PTLS_GROUP_MLKEM768 513
190
#define PTLS_GROUP_NAME_MLKEM768 "MLKEM768"
191
#define PTLS_GROUP_MLKEM1024 514
192
#define PTLS_GROUP_NAME_MLKEM1024 "MLKEM1024"
193
194
/* signature algorithms */
195
0
#define PTLS_SIGNATURE_RSA_PKCS1_SHA1 0x0201
196
0
#define PTLS_SIGNATURE_RSA_PKCS1_SHA256 0x0401
197
0
#define PTLS_SIGNATURE_ECDSA_SECP256R1_SHA256 0x0403
198
#define PTLS_SIGNATURE_ECDSA_SECP384R1_SHA384 0x0503
199
#define PTLS_SIGNATURE_ECDSA_SECP521R1_SHA512 0x0603
200
0
#define PTLS_SIGNATURE_RSA_PSS_RSAE_SHA256 0x0804
201
#define PTLS_SIGNATURE_RSA_PSS_RSAE_SHA384 0x0805
202
#define PTLS_SIGNATURE_RSA_PSS_RSAE_SHA512 0x0806
203
#define PTLS_SIGNATURE_ED25519 0x0807
204
205
/* HPKE */
206
#define PTLS_HPKE_MODE_BASE 0
207
#define PTLS_HPKE_MODE_PSK 1
208
#define PTLS_HPKE_MODE_AUTH 2
209
#define PTLS_HPKE_MODE_AUTH_PSK 3
210
#define PTLS_HPKE_KEM_P256_SHA256 16
211
#define PTLS_HPKE_KEM_P384_SHA384 17
212
0
#define PTLS_HPKE_KEM_X25519_SHA256 32
213
0
#define PTLS_HPKE_HKDF_SHA256 1
214
#define PTLS_HPKE_HKDF_SHA384 2
215
#define PTLS_HPKE_HKDF_SHA512 3
216
0
#define PTLS_HPKE_AEAD_AES_128_GCM 1
217
#define PTLS_HPKE_AEAD_AES_256_GCM 2
218
#define PTLS_HPKE_AEAD_CHACHA20POLY1305 3
219
220
/* error classes and macros */
221
1.54k
#define PTLS_ERROR_CLASS_SELF_ALERT 0
222
1.44k
#define PTLS_ERROR_CLASS_PEER_ALERT 0x100
223
128k
#define PTLS_ERROR_CLASS_INTERNAL 0x200
224
225
2.96k
#define PTLS_ERROR_GET_CLASS(e) ((e) & ~0xff)
226
#define PTLS_ALERT_TO_SELF_ERROR(e) ((e) + PTLS_ERROR_CLASS_SELF_ALERT)
227
28
#define PTLS_ALERT_TO_PEER_ERROR(e) ((e) + PTLS_ERROR_CLASS_PEER_ALERT)
228
#define PTLS_ERROR_TO_ALERT(e) ((e) & 0xff)
229
230
/* the HKDF prefix */
231
#define PTLS_HKDF_EXPAND_LABEL_PREFIX "tls13 "
232
233
/* alerts */
234
#define PTLS_ALERT_LEVEL_WARNING 1
235
1.40k
#define PTLS_ALERT_LEVEL_FATAL 2
236
237
0
#define PTLS_ALERT_CLOSE_NOTIFY 0
238
39
#define PTLS_ALERT_UNEXPECTED_MESSAGE 10
239
4.17k
#define PTLS_ALERT_BAD_RECORD_MAC 20
240
119
#define PTLS_ALERT_HANDSHAKE_FAILURE 40
241
0
#define PTLS_ALERT_BAD_CERTIFICATE 42
242
8
#define PTLS_ALERT_UNSUPPORTED_CERTIFICATE 43
243
0
#define PTLS_ALERT_CERTIFICATE_REVOKED 44
244
0
#define PTLS_ALERT_CERTIFICATE_EXPIRED 45
245
0
#define PTLS_ALERT_CERTIFICATE_UNKNOWN 46
246
45
#define PTLS_ALERT_ILLEGAL_PARAMETER 47
247
0
#define PTLS_ALERT_UNKNOWN_CA 48
248
#define PTLS_ALERT_ACCESS_DENIED 49
249
525
#define PTLS_ALERT_DECODE_ERROR 50
250
4
#define PTLS_ALERT_DECRYPT_ERROR 51
251
70
#define PTLS_ALERT_PROTOCOL_VERSION 70
252
1.40k
#define PTLS_ALERT_INTERNAL_ERROR 80
253
#define PTLS_ALERT_USER_CANCELED 90
254
228
#define PTLS_ALERT_MISSING_EXTENSION 109
255
#define PTLS_ALERT_UNSUPPORTED_EXTENSION 110
256
#define PTLS_ALERT_UNRECOGNIZED_NAME 112
257
0
#define PTLS_ALERT_UNKNOWN_PSK_IDENTITY 115
258
0
#define PTLS_ALERT_CERTIFICATE_REQUIRED 116
259
#define PTLS_ALERT_NO_APPLICATION_PROTOCOL 120
260
1.41k
#define PTLS_ALERT_ECH_REQUIRED 121
261
262
/* TLS 1.2 */
263
0
#define PTLS_TLS12_MASTER_SECRET_SIZE 48
264
#define PTLS_TLS12_AAD_SIZE 13
265
#define PTLS_TLS12_AESGCM_FIXED_IV_SIZE 4
266
#define PTLS_TLS12_AESGCM_RECORD_IV_SIZE 8
267
#define PTLS_TLS12_CHACHAPOLY_FIXED_IV_SIZE 12
268
#define PTLS_TLS12_CHACHAPOLY_RECORD_IV_SIZE 0
269
270
/* internal errors */
271
0
#define PTLS_ERROR_NO_MEMORY (PTLS_ERROR_CLASS_INTERNAL + 1)
272
183k
#define PTLS_ERROR_IN_PROGRESS (PTLS_ERROR_CLASS_INTERNAL + 2)
273
0
#define PTLS_ERROR_LIBRARY (PTLS_ERROR_CLASS_INTERNAL + 3)
274
0
#define PTLS_ERROR_INCOMPATIBLE_KEY (PTLS_ERROR_CLASS_INTERNAL + 4)
275
#define PTLS_ERROR_SESSION_NOT_FOUND (PTLS_ERROR_CLASS_INTERNAL + 5)
276
416
#define PTLS_ERROR_STATELESS_RETRY (PTLS_ERROR_CLASS_INTERNAL + 6)
277
0
#define PTLS_ERROR_NOT_AVAILABLE (PTLS_ERROR_CLASS_INTERNAL + 7)
278
#define PTLS_ERROR_COMPRESSION_FAILURE (PTLS_ERROR_CLASS_INTERNAL + 8)
279
0
#define PTLS_ERROR_REJECT_EARLY_DATA (PTLS_ERROR_CLASS_INTERNAL + 9)
280
0
#define PTLS_ERROR_DELEGATE (PTLS_ERROR_CLASS_INTERNAL + 10)
281
635
#define PTLS_ERROR_ASYNC_OPERATION (PTLS_ERROR_CLASS_INTERNAL + 11)
282
0
#define PTLS_ERROR_BLOCK_OVERFLOW (PTLS_ERROR_CLASS_INTERNAL + 12)
283
284
#define PTLS_ERROR_INCORRECT_BASE64 (PTLS_ERROR_CLASS_INTERNAL + 50)
285
#define PTLS_ERROR_PEM_LABEL_NOT_FOUND (PTLS_ERROR_CLASS_INTERNAL + 51)
286
#define PTLS_ERROR_BER_INCORRECT_ENCODING (PTLS_ERROR_CLASS_INTERNAL + 52)
287
#define PTLS_ERROR_BER_MALFORMED_TYPE (PTLS_ERROR_CLASS_INTERNAL + 53)
288
#define PTLS_ERROR_BER_MALFORMED_LENGTH (PTLS_ERROR_CLASS_INTERNAL + 54)
289
#define PTLS_ERROR_BER_EXCESSIVE_LENGTH (PTLS_ERROR_CLASS_INTERNAL + 55)
290
#define PTLS_ERROR_BER_ELEMENT_TOO_SHORT (PTLS_ERROR_CLASS_INTERNAL + 56)
291
#define PTLS_ERROR_BER_UNEXPECTED_EOC (PTLS_ERROR_CLASS_INTERNAL + 57)
292
#define PTLS_ERROR_DER_INDEFINITE_LENGTH (PTLS_ERROR_CLASS_INTERNAL + 58)
293
#define PTLS_ERROR_INCORRECT_ASN1_SYNTAX (PTLS_ERROR_CLASS_INTERNAL + 59)
294
#define PTLS_ERROR_INCORRECT_PEM_KEY_VERSION (PTLS_ERROR_CLASS_INTERNAL + 60)
295
#define PTLS_ERROR_INCORRECT_PEM_ECDSA_KEY_VERSION (PTLS_ERROR_CLASS_INTERNAL + 61)
296
#define PTLS_ERROR_INCORRECT_PEM_ECDSA_CURVE (PTLS_ERROR_CLASS_INTERNAL + 62)
297
#define PTLS_ERROR_INCORRECT_PEM_ECDSA_KEYSIZE (PTLS_ERROR_CLASS_INTERNAL + 63)
298
#define PTLS_ERROR_INCORRECT_ASN1_ECDSA_KEY_SYNTAX (PTLS_ERROR_CLASS_INTERNAL + 64)
299
300
3.31k
#define PTLS_HANDSHAKE_TYPE_CLIENT_HELLO 1
301
0
#define PTLS_HANDSHAKE_TYPE_SERVER_HELLO 2
302
0
#define PTLS_HANDSHAKE_TYPE_NEW_SESSION_TICKET 4
303
0
#define PTLS_HANDSHAKE_TYPE_END_OF_EARLY_DATA 5
304
0
#define PTLS_HANDSHAKE_TYPE_ENCRYPTED_EXTENSIONS 8
305
0
#define PTLS_HANDSHAKE_TYPE_CERTIFICATE 11
306
0
#define PTLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST 13
307
0
#define PTLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY 15
308
56
#define PTLS_HANDSHAKE_TYPE_FINISHED 20
309
0
#define PTLS_HANDSHAKE_TYPE_KEY_UPDATE 24
310
0
#define PTLS_HANDSHAKE_TYPE_COMPRESSED_CERTIFICATE 25
311
121
#define PTLS_HANDSHAKE_TYPE_MESSAGE_HASH 254
312
#define PTLS_HANDSHAKE_TYPE_PSEUDO_HRR -1
313
314
2.48k
#define PTLS_CERTIFICATE_TYPE_X509 0
315
0
#define PTLS_CERTIFICATE_TYPE_RAW_PUBLIC_KEY 2
316
317
#define PTLS_ZERO_DIGEST_SHA256                                                                                                    \
318
    {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,                               \
319
     0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}
320
321
#define PTLS_ZERO_DIGEST_SHA384                                                                                                    \
322
    {0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,                               \
323
     0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,                               \
324
     0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b}
325
326
#define PTLS_ZERO_DIGEST_SHA512                                                                                                    \
327
    {0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,                               \
328
     0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,                               \
329
     0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,                               \
330
     0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e}
331
332
13.3k
#define PTLS_TO__STR(n) #n
333
13.3k
#define PTLS_TO_STR(n) PTLS_TO__STR(n)
334
335
/**
336
 * default maximum of tickets to send (see ptls_context_t::ticket_requests.server.max_count)
337
 */
338
188
#define PTLS_DEFAULT_MAX_TICKETS_TO_SERVE 4
339
340
typedef struct st_ptls_t ptls_t;
341
typedef struct st_ptls_context_t ptls_context_t;
342
typedef struct st_ptls_key_schedule_t ptls_key_schedule_t;
343
344
/**
345
 * represents a sequence of octets
346
 */
347
typedef struct st_ptls_iovec_t {
348
    uint8_t *base;
349
    size_t len;
350
} ptls_iovec_t;
351
352
/**
353
 * used for storing output
354
 */
355
typedef struct st_ptls_buffer_t {
356
    uint8_t *base;
357
    size_t capacity;
358
    size_t off;
359
    uint8_t is_allocated; /* boolean */
360
    uint8_t align_bits;   /* if particular alignment is required, set to log2(alignment); otherwize zero */
361
} ptls_buffer_t;
362
363
/**
364
 * key exchange context built by ptls_key_exchange_algorithm::create.
365
 */
366
typedef struct st_ptls_key_exchange_context_t {
367
    /**
368
     * the underlying algorithm
369
     */
370
    const struct st_ptls_key_exchange_algorithm_t *algo;
371
    /**
372
     * public key of this context
373
     */
374
    ptls_iovec_t pubkey;
375
    /**
376
     * This function can be used for deriving a shared secret or for destroying the context.
377
     * When `secret` is non-NULL, this callback derives the shared secret using the private key of the context and the peer key
378
     * being given, and sets the value in `secret`. The memory pointed to by `secret->base` must be freed by the caller by calling
379
     * `free`. When `release` is set, the callee frees resources allocated to the context and set *keyex to NULL. Upon failure
380
     * (i.e., when an PTLS error code is returned), `*pubkey` and `*secret` either remain unchanged or are zero-cleared.
381
     */
382
    int (*on_exchange)(struct st_ptls_key_exchange_context_t **keyex, int release, ptls_iovec_t *secret, ptls_iovec_t peerkey);
383
} ptls_key_exchange_context_t;
384
385
/**
386
 * A key exchange algorithm.
387
 */
388
typedef const struct st_ptls_key_exchange_algorithm_t {
389
    /**
390
     * ID defined by the TLS specification
391
     */
392
    uint16_t id;
393
    /**
394
     * Creates a context for asynchronous key exchange. The function is called when ClientHello is generated. The on_exchange
395
     * callback of the created context is called when the client receives ServerHello.
396
     */
397
    int (*create)(const struct st_ptls_key_exchange_algorithm_t *algo, ptls_key_exchange_context_t **ctx);
398
    /**
399
     * Implements synchronous key exchange. Called when ServerHello is generated.
400
     * Given a public key provided by the peer (`peerkey`), this callback generates an ephemeral private and public key, and returns
401
     * the public key (`pubkey`) and a secret (`secret`) derived from the peerkey and private key.
402
     * Upon failure (i.e., when an PTLS error code is returned), `*pubkey` and `*secret` either remain unchanged or are
403
     * zero-cleared.
404
     */
405
    int (*exchange)(const struct st_ptls_key_exchange_algorithm_t *algo, ptls_iovec_t *pubkey, ptls_iovec_t *secret,
406
                    ptls_iovec_t peerkey);
407
    /**
408
     * crypto-specific data
409
     */
410
    intptr_t data;
411
    /**
412
     * Description as defined in the IANA TLS registry
413
     */
414
    const char *name;
415
} ptls_key_exchange_algorithm_t;
416
417
/**
418
 * context of a symmetric cipher
419
 */
420
typedef struct st_ptls_cipher_context_t {
421
    const struct st_ptls_cipher_algorithm_t *algo;
422
    /* field above this line must not be altered by the crypto binding */
423
    void (*do_dispose)(struct st_ptls_cipher_context_t *ctx);
424
    void (*do_init)(struct st_ptls_cipher_context_t *ctx, const void *iv);
425
    void (*do_transform)(struct st_ptls_cipher_context_t *ctx, void *output, const void *input, size_t len);
426
} ptls_cipher_context_t;
427
428
/**
429
 * a symmetric cipher
430
 */
431
typedef const struct st_ptls_cipher_algorithm_t {
432
    const char *name;
433
    size_t key_size;
434
    size_t block_size;
435
    size_t iv_size;
436
    size_t context_size;
437
    int (*setup_crypto)(ptls_cipher_context_t *ctx, int is_enc, const void *key);
438
} ptls_cipher_algorithm_t;
439
440
/**
441
 * This object specifies symmetric cipher to be calculated alongside the AEAD encryption.
442
 * QUIC stacks can use this object to apply QUIC header protection and AEAD encryption in one shot.
443
 */
444
typedef struct st_ptls_aead_supplementary_encryption_t {
445
    /**
446
     * Cipher context to be used.
447
     */
448
    ptls_cipher_context_t *ctx;
449
    /**
450
     * Input to the cipher.
451
     * This field may point to the output of AEAD encryption, in which case the input will be read after AEAD encryption is
452
     * complete.
453
     */
454
    const void *input;
455
    /**
456
     * Output.
457
     */
458
    uint8_t output[16];
459
} ptls_aead_supplementary_encryption_t;
460
461
/**
462
 * AEAD context.
463
 * AEAD implementations are allowed to stuff data at the end of the struct; see `ptls_aead_algorithm_t::setup_crypto`.
464
 * Ciphers for TLS over TCP MUST implement `do_encrypt`, `do_encrypt_v`, `do_decrypt`.
465
 * `do_encrypt_init`, `~update`, `~final` are obsolete, and therefore may not be available.
466
 */
467
typedef struct st_ptls_aead_context_t {
468
    /**
469
     * Points to the algorithm. This field is governed by picotls core; backends must not alter.
470
     */
471
    const struct st_ptls_aead_algorithm_t *algo;
472
    /**
473
     * Mandatory callback that disposes of all the backend-specific data.
474
     */
475
    void (*dispose_crypto)(struct st_ptls_aead_context_t *ctx);
476
    /**
477
     * Mandatory callback that returns the static IV. The size of IV is available as `ptls_aead_algorithm_t::iv_size`.
478
     */
479
    void (*do_get_iv)(struct st_ptls_aead_context_t *ctx, void *iv);
480
    /**
481
     * Mandatory callback that sets the static IV. The size of IV is available as `ptls_aead_algorithm_t::iv_size`.
482
     */
483
    void (*do_set_iv)(struct st_ptls_aead_context_t *ctx, const void *iv);
484
    /**
485
     * Deprecated.
486
     */
487
    void (*do_encrypt_init)(struct st_ptls_aead_context_t *ctx, uint64_t seq, const void *aad, size_t aadlen);
488
    /**
489
     * Deprecated.
490
     */
491
    size_t (*do_encrypt_update)(struct st_ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen);
492
    /**
493
     * Deprecated.
494
     */
495
    size_t (*do_encrypt_final)(struct st_ptls_aead_context_t *ctx, void *output);
496
    /**
497
     * Mandatory callback that does "one-shot" encryption of an AEAD block.
498
     * When `supp` is set to non-NULL, the callback must also encrypt the supplementary block.
499
     * Backends may set this field to `ptls_aead__do_encrypt` that calls `do_encrypt_v` and `ptls_cipher_*` functions for handling
500
     * the supplimentary block.
501
     */
502
    void (*do_encrypt)(struct st_ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
503
                       const void *aad, size_t aadlen, ptls_aead_supplementary_encryption_t *supp);
504
    /**
505
     * Variant of `do_encrypt` that gathers input from multiple blocks. Support for this callback is also mandatory.
506
     * Legacy backends may set this field to `ptls_aead__do_encrypt_v` that calls `do_encrypt_init`, `do_encrypt_update`,
507
     * `do_encrypt_final`.
508
     */
509
    void (*do_encrypt_v)(struct st_ptls_aead_context_t *ctx, void *output, ptls_iovec_t *input, size_t incnt, uint64_t seq,
510
                         const void *aad, size_t aadlen);
511
    /**
512
     * Mandatory callback for decrypting an AEAD block.
513
     * If successful, returns the amount of cleartext bytes being written to output. Otherwise, returns SIZE_MAX.
514
     */
515
    size_t (*do_decrypt)(struct st_ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
516
                         const void *aad, size_t aadlen);
517
} ptls_aead_context_t;
518
519
/**
520
 * An AEAD cipher.
521
 */
522
typedef const struct st_ptls_aead_algorithm_t {
523
    /**
524
     * name (following the convention of `openssl ciphers -v ALL`)
525
     */
526
    const char *name;
527
    /**
528
     * confidentiality_limit (max records / packets sent before re-key)
529
     */
530
    const uint64_t confidentiality_limit;
531
    /**
532
     * integrity_limit (max decryption failure records / packets before re-key)
533
     */
534
    const uint64_t integrity_limit;
535
    /**
536
     * the underlying key stream
537
     */
538
    ptls_cipher_algorithm_t *ctr_cipher;
539
    /**
540
     * the underlying ecb cipher (might not be available)
541
     */
542
    ptls_cipher_algorithm_t *ecb_cipher;
543
    /**
544
     * key size
545
     */
546
    size_t key_size;
547
    /**
548
     * size of the IV
549
     */
550
    size_t iv_size;
551
    /**
552
     * size of the tag
553
     */
554
    size_t tag_size;
555
    /**
556
     * TLS/1.2 Security Parameters (AEAD without support for TLS 1.2 must set both values to 0)
557
     */
558
    struct {
559
        size_t fixed_iv_size;
560
        size_t record_iv_size;
561
    } tls12;
562
    /**
563
     * if encrypted bytes are going to be written using non-temporal store instructions (i.e., skip cache)
564
     */
565
    unsigned non_temporal : 1;
566
    /**
567
     * log2(alignment) being required
568
     */
569
    uint8_t align_bits;
570
    /**
571
     * size of memory allocated for `ptls_aead_context_t`
572
     */
573
    size_t context_size;
574
    /**
575
     * Backend callback called to setup `ptls_aead_context_t`.
576
     * Backends are allowed to stuff arbitrary data at the end of `ptls_aead_context_t`; actual size of the memory chunk being
577
     * allocated is that specified by `ptls_aead_algorithm_t::context_size`. When the `setup_crypto` callback is called, all the
578
     * fields outside of `ptls_aead_context_t` will be in undefined state; it is the responsibility of the callback to initialize
579
     * them, as well as the callbacks of `ptls_aead_context_t` that the backend supports.
580
     * A non-zero return value indicates failure, in which case the error will propagate as `ptls_aead_new` returning NULL.
581
     */
582
    int (*setup_crypto)(ptls_aead_context_t *ctx, int is_enc, const void *key, const void *iv);
583
} ptls_aead_algorithm_t;
584
585
/**
586
 *
587
 */
588
typedef enum en_ptls_hash_final_mode_t {
589
    /**
590
     * obtains the digest and frees the context
591
     */
592
    PTLS_HASH_FINAL_MODE_FREE = 0,
593
    /**
594
     * obtains the digest and reset the context to initial state
595
     */
596
    PTLS_HASH_FINAL_MODE_RESET = 1,
597
    /**
598
     * obtains the digest while leaving the context as-is
599
     */
600
    PTLS_HASH_FINAL_MODE_SNAPSHOT = 2
601
} ptls_hash_final_mode_t;
602
603
/**
604
 * A hash context.
605
 */
606
typedef struct st_ptls_hash_context_t {
607
    /**
608
     * feeds additional data into the hash context
609
     */
610
    void (*update)(struct st_ptls_hash_context_t *ctx, const void *src, size_t len);
611
    /**
612
     * returns the digest and performs necessary operation specified by mode
613
     */
614
    void (*final)(struct st_ptls_hash_context_t *ctx, void *md, ptls_hash_final_mode_t mode);
615
    /**
616
     * creates a copy of the hash context
617
     */
618
    struct st_ptls_hash_context_t *(*clone_)(struct st_ptls_hash_context_t *src);
619
} ptls_hash_context_t;
620
621
/**
622
 * A hash algorithm and its properties.
623
 */
624
typedef const struct st_ptls_hash_algorithm_t {
625
    /**
626
     * name of the hash algorithm
627
     */
628
    const char *name;
629
    /**
630
     * block size
631
     */
632
    size_t block_size;
633
    /**
634
     * digest size
635
     */
636
    size_t digest_size;
637
    /**
638
     * constructor that creates the hash context
639
     */
640
    ptls_hash_context_t *(*create)(void);
641
    /**
642
     * digest of zero-length octets
643
     */
644
    uint8_t empty_digest[PTLS_MAX_DIGEST_SIZE];
645
} ptls_hash_algorithm_t;
646
647
typedef const struct st_ptls_cipher_suite_t {
648
    /**
649
     * ID as defined by the TLS Cipher Suites registry
650
     */
651
    uint16_t id;
652
    /**
653
     * underlying AEAD algorithm
654
     */
655
    ptls_aead_algorithm_t *aead;
656
    /**
657
     * underlying hash algorithm
658
     */
659
    ptls_hash_algorithm_t *hash;
660
    /**
661
     * value of the "Description" field of the TLS Cipher Suites registry
662
     */
663
    const char *name;
664
} ptls_cipher_suite_t;
665
666
struct st_ptls_traffic_protection_t;
667
668
typedef struct st_ptls_message_emitter_t {
669
    ptls_buffer_t *buf;
670
    struct st_ptls_traffic_protection_t *enc;
671
    size_t record_header_length;
672
    int (*begin_message)(struct st_ptls_message_emitter_t *self);
673
    int (*commit_message)(struct st_ptls_message_emitter_t *self);
674
} ptls_message_emitter_t;
675
676
/**
677
 * HPKE KEM
678
 */
679
typedef const struct st_ptls_hpke_kem_t {
680
    uint16_t id;
681
    ptls_key_exchange_algorithm_t *keyex;
682
    ptls_hash_algorithm_t *hash;
683
} ptls_hpke_kem_t;
684
685
typedef struct st_ptls_hpke_cipher_suite_id_t {
686
    uint16_t kdf;
687
    uint16_t aead;
688
} ptls_hpke_cipher_suite_id_t;
689
690
typedef const struct st_ptls_hpke_cipher_suite_t {
691
    ptls_hpke_cipher_suite_id_t id;
692
    const char *name; /* in form of "<kdf>/<aead>" using the sames specified in IANA HPKE registry */
693
    ptls_hash_algorithm_t *hash;
694
    ptls_aead_algorithm_t *aead;
695
} ptls_hpke_cipher_suite_t;
696
697
#define PTLS_CALLBACK_TYPE0(ret, name)                                                                                             \
698
    typedef struct st_ptls_##name##_t {                                                                                            \
699
        ret (*cb)(struct st_ptls_##name##_t * self);                                                                               \
700
    } ptls_##name##_t
701
702
#define PTLS_CALLBACK_TYPE(ret, name, ...)                                                                                         \
703
    typedef struct st_ptls_##name##_t {                                                                                            \
704
        ret (*cb)(struct st_ptls_##name##_t * self, __VA_ARGS__);                                                                  \
705
    } ptls_##name##_t
706
707
typedef struct st_ptls_client_hello_psk_identity_t {
708
    ptls_iovec_t identity;
709
    uint32_t obfuscated_ticket_age;
710
    ptls_iovec_t binder;
711
} ptls_client_hello_psk_identity_t;
712
713
/**
714
 * arguments passsed to the on_client_hello callback
715
 */
716
typedef struct st_ptls_on_client_hello_parameters_t {
717
    /**
718
     * SNI value received from the client. The value is {NULL, 0} if the extension was absent.
719
     */
720
    ptls_iovec_t server_name;
721
    /**
722
     * Raw value of the client_hello message.
723
     */
724
    ptls_iovec_t raw_message;
725
    /**
726
     * points to the cipher-suites section of the raw_message (see above)
727
     */
728
    ptls_iovec_t cipher_suites;
729
    /**
730
     *
731
     */
732
    struct {
733
        ptls_iovec_t *list;
734
        size_t count;
735
    } negotiated_protocols;
736
    struct {
737
        const uint16_t *list;
738
        size_t count;
739
    } signature_algorithms;
740
    struct {
741
        const uint16_t *list;
742
        size_t count;
743
    } certificate_compression_algorithms;
744
    struct {
745
        const uint8_t *list;
746
        size_t count;
747
    } server_certificate_types;
748
    struct {
749
        const ptls_client_hello_psk_identity_t *list;
750
        size_t count;
751
    } psk_identities;
752
    /**
753
     * set to 1 if ClientHello is too old (or too new) to be handled by picotls
754
     */
755
    unsigned incompatible_version : 1;
756
} ptls_on_client_hello_parameters_t;
757
758
/**
759
 * returns current time in milliseconds (ptls_get_time can be used to return the physical time)
760
 */
761
PTLS_CALLBACK_TYPE0(uint64_t, get_time);
762
/**
763
 * after receiving ClientHello, the core calls the optional callback to give a chance to the swap the context depending on the input
764
 * values. The callback is required to call `ptls_set_server_name` if an SNI extension needs to be sent to the client.
765
 */
766
PTLS_CALLBACK_TYPE(int, on_client_hello, ptls_t *tls, ptls_on_client_hello_parameters_t *params);
767
/**
768
 * callback to generate the certificate message. `ptls_context::certificates` are set when the callback is set to NULL.
769
 */
770
PTLS_CALLBACK_TYPE(int, emit_certificate, ptls_t *tls, ptls_message_emitter_t *emitter, ptls_key_schedule_t *key_sched,
771
                   ptls_iovec_t context, int push_status_request, const uint16_t *compress_algos, size_t num_compress_algos);
772
/**
773
 * An object that represents an asynchronous task (e.g., RSA signature generation).
774
 * When `ptls_handshake` returns `PTLS_ERROR_ASYNC_OPERATION`, it has an associated task in flight. The user should obtain the
775
 * reference to the associated task by calling `ptls_get_async_job`, then either wait for the file descriptor obtained from
776
 * the `get_fd` callback to become readable, or set a completion callback via `set_completion_callback` and wait for its
777
 * invocation. Once notified, the user should invoke `ptls_handshake` again.
778
 * Async jobs typically provide support for only one of the two methods.
779
 */
780
typedef struct st_ptls_async_job_t {
781
    void (*destroy_)(struct st_ptls_async_job_t *self);
782
    /**
783
     * optional callback returning a file descriptor that becomes readable when the job is complete
784
     */
785
    int (*get_fd)(struct st_ptls_async_job_t *self);
786
    /**
787
     * optional callback for setting a completion callback
788
     */
789
    void (*set_completion_callback)(struct st_ptls_async_job_t *self, void (*cb)(void *), void *cbdata);
790
} ptls_async_job_t;
791
/**
792
 * When gerenating CertificateVerify, the core calls the callback to sign the handshake context using the certificate. This callback
793
 * supports asynchronous mode; see `ptls_openssl_sign_certificate_t` for more information.
794
 */
795
PTLS_CALLBACK_TYPE(int, sign_certificate, ptls_t *tls, ptls_async_job_t **async, uint16_t *selected_algorithm,
796
                   ptls_buffer_t *output, ptls_iovec_t input, const uint16_t *algorithms, size_t num_algorithms);
797
/**
798
 * after receiving Certificate, the core calls the callback to verify the certificate chain and to obtain a pointer to a
799
 * callback that should be used for verifying CertificateVerify. If an error occurs between a successful return from this
800
 * callback to the invocation of the verify_sign callback, verify_sign is called with both data and sign set to an empty buffer.
801
 * The implementor of the callback should use that as the opportunity to free any temporary data allocated for the verify_sign
802
 * callback.
803
 * The name of the server to be verified, if any, is provided explicitly as `server_name`. When ECH is offered by the client but
804
 * the was rejected by the server, this value can be different from that being sent via `ptls_get_server_name`.
805
 */
806
typedef struct st_ptls_verify_certificate_t {
807
    int (*cb)(struct st_ptls_verify_certificate_t *self, ptls_t *tls, const char *server_name,
808
              int (**verify_sign)(void *verify_ctx, uint16_t algo, ptls_iovec_t data, ptls_iovec_t sign), void **verify_data,
809
              ptls_iovec_t *certs, size_t num_certs);
810
    /**
811
     * list of signature algorithms being supported, terminated by UINT16_MAX
812
     */
813
    const uint16_t *algos;
814
} ptls_verify_certificate_t;
815
/**
816
 * Encrypt-and-signs (or verify-and-decrypts) a ticket (server-only).
817
 * When used for encryption (i.e., is_encrypt being set), the function should return 0 if successful, or else a non-zero value.
818
 * When used for decryption, the function should return 0 (successful), PTLS_ERROR_REJECT_EARLY_DATA (successful, but 0-RTT is
819
 * forbidden), or any other value to indicate failure.
820
 */
821
PTLS_CALLBACK_TYPE(int, encrypt_ticket, ptls_t *tls, int is_encrypt, ptls_buffer_t *dst, ptls_iovec_t src);
822
/**
823
 * saves a ticket (client-only)
824
 */
825
PTLS_CALLBACK_TYPE(int, save_ticket, ptls_t *tls, ptls_iovec_t input);
826
/**
827
 * event logging (incl. secret logging)
828
 */
829
typedef struct st_ptls_log_event_t {
830
    void (*cb)(struct st_ptls_log_event_t *self, ptls_t *tls, const char *type, const char *fmt, ...)
831
        __attribute__((format(printf, 4, 5)));
832
} ptls_log_event_t;
833
/**
834
 * reference counting
835
 */
836
PTLS_CALLBACK_TYPE(void, update_open_count, ssize_t delta);
837
/**
838
 * applications that have their own record layer can set this function to derive their own traffic keys from the traffic secret.
839
 * The cipher-suite that is being associated to the connection can be obtained by calling the ptls_get_cipher function.
840
 */
841
PTLS_CALLBACK_TYPE(int, update_traffic_key, ptls_t *tls, int is_enc, size_t epoch, const void *secret);
842
/**
843
 * callback for every extension detected during decoding
844
 */
845
PTLS_CALLBACK_TYPE(int, on_extension, ptls_t *tls, uint8_t hstype, uint16_t exttype, ptls_iovec_t extdata);
846
/**
847
 *
848
 */
849
typedef struct st_ptls_decompress_certificate_t {
850
    /**
851
     * list of supported algorithms terminated by UINT16_MAX
852
     */
853
    const uint16_t *supported_algorithms;
854
    /**
855
     * callback that decompresses the message
856
     */
857
    int (*cb)(struct st_ptls_decompress_certificate_t *self, ptls_t *tls, uint16_t algorithm, ptls_iovec_t output,
858
              ptls_iovec_t input);
859
} ptls_decompress_certificate_t;
860
/**
861
 * ECH: creates the AEAD context to be used for "Open"-ing inner CH. Given `config_id`, the callback looks up the ECH config and the
862
 * corresponding private key, invokes `ptls_hpke_setup_base_r` with provided `cipher`, `enc`, and `info_prefix` (which will be
863
 * "tls ech" || 00).
864
 */
865
PTLS_CALLBACK_TYPE(ptls_aead_context_t *, ech_create_opener, ptls_hpke_kem_t **kem, ptls_hpke_cipher_suite_t **cipher, ptls_t *tls,
866
                   uint8_t config_id, ptls_hpke_cipher_suite_id_t cipher_id, ptls_iovec_t enc, ptls_iovec_t info_prefix);
867
868
/**
869
 * the configuration
870
 */
871
struct st_ptls_context_t {
872
    /**
873
     * PRNG to be used
874
     */
875
    void (*random_bytes)(void *buf, size_t len);
876
    /**
877
     *
878
     */
879
    ptls_get_time_t *get_time;
880
    /**
881
     * list of supported key-exchange algorithms terminated by NULL
882
     */
883
    ptls_key_exchange_algorithm_t **key_exchanges;
884
    /**
885
     * list of supported cipher-suites terminated by NULL
886
     */
887
    ptls_cipher_suite_t **cipher_suites;
888
    /**
889
     * list of certificates
890
     */
891
    struct {
892
        ptls_iovec_t *list;
893
        size_t count;
894
    } certificates;
895
    /**
896
     * External pre-shared key used for mutual authentication. Unless when using PSK, all the fields must be set to NULL / 0.
897
     */
898
    struct {
899
        ptls_iovec_t identity;
900
        ptls_iovec_t secret;
901
        /**
902
         * (mandatory) hash algorithm associated to the PSK; cipher-suites not sharing the same `ptls_hash_algorithm_t` will be
903
         * ignored
904
         */
905
        ptls_hash_algorithm_t *hash;
906
    } pre_shared_key;
907
    /**
908
     * ECH
909
     */
910
    struct {
911
        struct {
912
            /**
913
             * list of HPKE symmetric cipher-suites (set to NULL to disable ECH altogether)
914
             */
915
            ptls_hpke_cipher_suite_t **ciphers;
916
            /**
917
             * KEMs being supported
918
             */
919
            ptls_hpke_kem_t **kems;
920
        } client;
921
        struct {
922
            /**
923
             * callback that does ECDH key exchange and returns the AEAD context
924
             */
925
            ptls_ech_create_opener_t *create_opener;
926
            /**
927
             * ECHConfigList to be sent to the client when there is mismatch (or when the client sends a grease)
928
             */
929
            ptls_iovec_t retry_configs;
930
        } server;
931
    } ech;
932
    /**
933
     *
934
     */
935
    ptls_on_client_hello_t *on_client_hello;
936
    /**
937
     *
938
     */
939
    ptls_emit_certificate_t *emit_certificate;
940
    /**
941
     *
942
     */
943
    ptls_sign_certificate_t *sign_certificate;
944
    /**
945
     *
946
     */
947
    ptls_verify_certificate_t *verify_certificate;
948
    /**
949
     * lifetime of a session ticket (server-only)
950
     */
951
    uint32_t ticket_lifetime;
952
    /**
953
     * maximum permitted size of early data (server-only)
954
     */
955
    uint32_t max_early_data_size;
956
    /**
957
     * maximum size of the message buffer (default: 0 = unlimited = 3 + 2^24 bytes)
958
     */
959
    size_t max_buffer_size;
960
    /**
961
     * this field is obsolete and ignored
962
     */
963
    const char *hkdf_label_prefix__obsolete;
964
    /**
965
     * if set, psk handshakes use (ec)dhe
966
     */
967
    unsigned require_dhe_on_psk : 1;
968
    /**
969
     * if exporter master secrets should be recorded
970
     */
971
    unsigned use_exporter : 1;
972
    /**
973
     * if ChangeCipherSpec record should be sent during handshake. If the client sends CCS, the server sends one in response
974
     * regardless of the value of this flag. See RFC 8446 Appendix D.3.
975
     */
976
    unsigned send_change_cipher_spec : 1;
977
    /**
978
     * if set, the server requests client certificates to authenticate the client
979
     */
980
    unsigned require_client_authentication : 1;
981
    /**
982
     * if set, EOED will not be emitted or accepted
983
     */
984
    unsigned omit_end_of_early_data : 1;
985
    /**
986
     * This option turns on support for Raw Public Keys (RFC 7250).
987
     *
988
     * When running as a client, this option instructs the client to request the server to send raw public keys in place of X.509
989
     * certificate chain. The client should set its `certificate_verify` callback to one that is capable of validating the raw
990
     * public key that will be sent by the server.
991
     *
992
     * When running as a server, this option instructs the server to only handle clients requesting the use of raw public keys. If
993
     * the client does not, the handshake is rejected. Note however that the rejection happens only after the `on_client_hello`
994
     * callback is being called. Therefore, applications can support both X.509 and raw public keys by swapping `ptls_context_t` to
995
     * the correct one when that callback is being called (like handling swapping the contexts based on the value of SNI).
996
     */
997
    unsigned use_raw_public_keys : 1;
998
    /**
999
     * boolean indicating if the cipher-suite should be chosen based on server's preference
1000
     */
1001
    unsigned server_cipher_preference : 1;
1002
    /**
1003
     * boolean indicating if ChaCha20-Poly1305 should be reprioritized to the top of the server cipher list if a ChaCha20-Poly1305
1004
     * cipher is at the top of the client cipher list
1005
     */
1006
    unsigned server_cipher_chacha_priority : 1;
1007
    /**
1008
     *
1009
     */
1010
    ptls_encrypt_ticket_t *encrypt_ticket;
1011
    /**
1012
     *
1013
     */
1014
    ptls_save_ticket_t *save_ticket;
1015
    /**
1016
     *
1017
     */
1018
    ptls_log_event_t *log_event;
1019
    /**
1020
     *
1021
     */
1022
    ptls_update_open_count_t *update_open_count;
1023
    /**
1024
     *
1025
     */
1026
    ptls_update_traffic_key_t *update_traffic_key;
1027
    /**
1028
     *
1029
     */
1030
    ptls_decompress_certificate_t *decompress_certificate;
1031
    /**
1032
     *
1033
     */
1034
    ptls_on_extension_t *on_extension;
1035
    /**
1036
     * (optional) list of supported tls12 cipher-suites terminated by NULL
1037
     */
1038
    ptls_cipher_suite_t **tls12_cipher_suites;
1039
    /**
1040
     * (optional) session ID Context to segment resumption
1041
     */
1042
    struct {
1043
        uint8_t bytes[PTLS_SHA256_DIGEST_SIZE];
1044
        unsigned is_set : 1;
1045
    } ticket_context;
1046
    /**
1047
     * (optional) list of CAs advertised to clients as supported in the CertificateRequest message; each item must be DNs in DER
1048
     * format. The values are sent to the client only when `ptls_context_t::require_client_authentication` is set to true.
1049
     */
1050
    struct {
1051
        const ptls_iovec_t *list;
1052
        size_t count;
1053
    } client_ca_names;
1054
    /**
1055
     * (optional)
1056
     */
1057
    struct {
1058
        /**
1059
         * if set to non-zero and if the save_ticket callback is provided, a ticket_request extension containing the specified
1060
         * values is sent
1061
         */
1062
        struct {
1063
            uint8_t new_session_count;
1064
            uint8_t resumption_count;
1065
        } client;
1066
        /**
1067
         * if set to non-zero, the maximum number of tickets being sent is capped to the specifed value; if set to zero, the maximum
1068
         * adopted is PTLS_DEFAULT_MAX_TICKETS_TO_SERVE.
1069
         */
1070
        struct {
1071
            uint8_t max_count;
1072
        } server;
1073
    } ticket_requests;
1074
};
1075
1076
typedef struct st_ptls_raw_extension_t {
1077
    uint16_t type;
1078
    ptls_iovec_t data;
1079
} ptls_raw_extension_t;
1080
1081
typedef enum en_ptls_early_data_acceptance_t {
1082
    PTLS_EARLY_DATA_ACCEPTANCE_UNKNOWN = 0,
1083
    PTLS_EARLY_DATA_REJECTED,
1084
    PTLS_EARLY_DATA_ACCEPTED
1085
} ptls_early_data_acceptance_t;
1086
1087
/**
1088
 * optional arguments to client-driven handshake
1089
 */
1090
#ifdef _WINDOWS
1091
/* suppress warning C4201: nonstandard extension used: nameless struct/union */
1092
#pragma warning(push)
1093
#pragma warning(disable : 4201)
1094
#endif
1095
typedef struct st_ptls_handshake_properties_t {
1096
    union {
1097
        struct {
1098
            /**
1099
             * list of protocols offered through ALPN
1100
             */
1101
            struct {
1102
                const ptls_iovec_t *list;
1103
                size_t count;
1104
            } negotiated_protocols;
1105
            /**
1106
             * session ticket sent to the application via save_ticket callback
1107
             */
1108
            ptls_iovec_t session_ticket;
1109
            /**
1110
             * pointer to store the maximum size of early-data that can be sent immediately. If set to non-NULL, the first call to
1111
             * ptls_handshake (or ptls_handle_message) will set `*max_early_data` to the value obtained from the session ticket, or
1112
             * to zero if early-data cannot be sent. If NULL, early data will not be used.
1113
             */
1114
            size_t *max_early_data_size;
1115
            /**
1116
             * If early-data has been accepted by peer, or if the state is still unknown. The state changes anytime after handshake
1117
             * keys become available. Applications can peek the tri-state variable every time it calls `ptls_hanshake` or
1118
             * `ptls_handle_message` to determine the result at the earliest moment. This is an output parameter.
1119
             */
1120
            ptls_early_data_acceptance_t early_data_acceptance;
1121
            /**
1122
             * negotiate the key exchange method before sending key_share
1123
             */
1124
            unsigned negotiate_before_key_exchange : 1;
1125
            /**
1126
             * ECH
1127
             */
1128
            struct {
1129
                /**
1130
                 * Config offered by server e.g., by HTTPS RR. If config.base is non-NULL but config.len is zero, a grease ECH will
1131
                 * be sent, assuming that X25519-SHA256 KEM and SHA256-AES-128-GCM HPKE cipher is available.
1132
                 */
1133
                ptls_iovec_t configs;
1134
                /**
1135
                 * slot to save the config obtained from server on mismatch; user must free the returned blob by calling `free`
1136
                 */
1137
                ptls_iovec_t *retry_configs;
1138
            } ech;
1139
        } client;
1140
        struct {
1141
            /**
1142
             * psk binder being selected (len is set to zero if none)
1143
             */
1144
            struct {
1145
                uint8_t base[PTLS_MAX_DIGEST_SIZE];
1146
                size_t len;
1147
            } selected_psk_binder;
1148
            /**
1149
             * parameters related to use of the Cookie extension
1150
             */
1151
            struct {
1152
                /**
1153
                 * HMAC key to protect the integrity of the cookie. The key should be as long as the digest size of the first
1154
                 * ciphersuite specified in ptls_context_t (i.e. the hash algorithm of the best ciphersuite that can be chosen).
1155
                 */
1156
                const void *key;
1157
                /**
1158
                 * additional data to be used for verifying the cookie
1159
                 */
1160
                ptls_iovec_t additional_data;
1161
            } cookie;
1162
            /**
1163
             * if HRR should always be sent
1164
             */
1165
            unsigned enforce_retry : 1;
1166
            /**
1167
             * if retry should be stateless (cookie.key MUST be set when this option is used)
1168
             */
1169
            unsigned retry_uses_cookie : 1;
1170
        } server;
1171
    };
1172
    /**
1173
     * an optional list of additional extensions to send either in CH or EE, terminated by type == UINT16_MAX
1174
     */
1175
    ptls_raw_extension_t *additional_extensions;
1176
    /**
1177
     * an optional callback that returns a boolean value indicating if a particular extension should be collected
1178
     */
1179
    int (*collect_extension)(ptls_t *tls, struct st_ptls_handshake_properties_t *properties, uint16_t type);
1180
    /**
1181
     * an optional callback that reports the extensions being collected
1182
     */
1183
    int (*collected_extensions)(ptls_t *tls, struct st_ptls_handshake_properties_t *properties, ptls_raw_extension_t *extensions);
1184
} ptls_handshake_properties_t;
1185
#ifdef _WINDOWS
1186
#pragma warning(pop)
1187
#endif
1188
#ifdef _WINDOWS
1189
/* suppress warning C4293: >> shift count negative or too big */
1190
#pragma warning(disable : 4293)
1191
#endif
1192
/**
1193
 * builds a new ptls_iovec_t instance using the supplied parameters
1194
 */
1195
static ptls_iovec_t ptls_iovec_init(const void *p, size_t len);
1196
/**
1197
 * initializes a buffer, setting the default destination to the small buffer provided as the argument.
1198
 */
1199
static void ptls_buffer_init(ptls_buffer_t *buf, void *smallbuf, size_t smallbuf_size);
1200
/**
1201
 * disposes a buffer, freeing resources allocated by the buffer itself (if any)
1202
 */
1203
static void ptls_buffer_dispose(ptls_buffer_t *buf);
1204
/**
1205
 * internal
1206
 */
1207
void ptls_buffer__release_memory(ptls_buffer_t *buf);
1208
/**
1209
 * reserves space for additional amount of memory
1210
 */
1211
int ptls_buffer_reserve(ptls_buffer_t *buf, size_t delta);
1212
/**
1213
 * reserves space for additional amount of memory, requiring `buf->base` to follow specified alignment
1214
 */
1215
int ptls_buffer_reserve_aligned(ptls_buffer_t *buf, size_t delta, uint8_t align_bits);
1216
/**
1217
 * internal
1218
 */
1219
int ptls_buffer__do_pushv(ptls_buffer_t *buf, const void *src, size_t len);
1220
/**
1221
 * internal
1222
 */
1223
int ptls_buffer__adjust_quic_blocksize(ptls_buffer_t *buf, size_t body_size);
1224
/**
1225
 * internal
1226
 */
1227
int ptls_buffer__adjust_asn1_blocksize(ptls_buffer_t *buf, size_t body_size);
1228
/**
1229
 * pushes an unsigned bigint
1230
 */
1231
int ptls_buffer_push_asn1_ubigint(ptls_buffer_t *buf, const void *bignum, size_t size);
1232
/**
1233
 * encodes a quic varint (maximum length is PTLS_ENCODE_QUICINT_CAPACITY)
1234
 */
1235
static uint8_t *ptls_encode_quicint(uint8_t *p, uint64_t v);
1236
#define PTLS_ENCODE_QUICINT_CAPACITY 8
1237
1238
#define PTLS_QUICINT_MAX 4611686018427387903 // (1 << 62) - 1
1239
#define PTLS_QUICINT_LONGEST_STR "4611686018427387903"
1240
1241
#define ptls_buffer_pushv(buf, src, len)                                                                                           \
1242
10.1k
    do {                                                                                                                           \
1243
20.3k
        if ((ret = ptls_buffer__do_pushv((buf), (src), (len))) != 0)                                                               \
1244
10.1k
            goto Exit;                                                                                                             \
1245
10.1k
    } while (0)
1246
1247
#define ptls_buffer_push(buf, ...)                                                                                                 \
1248
7.35k
    do {                                                                                                                           \
1249
7.35k
        if ((ret = ptls_buffer__do_pushv((buf), (uint8_t[]){__VA_ARGS__}, sizeof((uint8_t[]){__VA_ARGS__}))) != 0)                 \
1250
7.35k
            goto Exit;                                                                                                             \
1251
7.35k
    } while (0)
1252
1253
#define ptls_buffer_push16(buf, v)                                                                                                 \
1254
3.38k
    do {                                                                                                                           \
1255
3.38k
        uint16_t _v = (v);                                                                                                         \
1256
3.38k
        ptls_buffer_push(buf, (uint8_t)(_v >> 8), (uint8_t)_v);                                                                    \
1257
3.38k
    } while (0)
1258
1259
#define ptls_buffer_push24(buf, v)                                                                                                 \
1260
    do {                                                                                                                           \
1261
        uint32_t _v = (v);                                                                                                         \
1262
        ptls_buffer_push(buf, (uint8_t)(_v >> 16), (uint8_t)(_v >> 8), (uint8_t)_v);                                               \
1263
    } while (0)
1264
1265
#define ptls_buffer_push32(buf, v)                                                                                                 \
1266
    do {                                                                                                                           \
1267
        uint32_t _v = (v);                                                                                                         \
1268
        ptls_buffer_push(buf, (uint8_t)(_v >> 24), (uint8_t)(_v >> 16), (uint8_t)(_v >> 8), (uint8_t)_v);                          \
1269
    } while (0)
1270
1271
#define ptls_buffer_push64(buf, v)                                                                                                 \
1272
0
    do {                                                                                                                           \
1273
0
        uint64_t _v = (v);                                                                                                         \
1274
0
        ptls_buffer_push(buf, (uint8_t)(_v >> 56), (uint8_t)(_v >> 48), (uint8_t)(_v >> 40), (uint8_t)(_v >> 32),                  \
1275
0
                         (uint8_t)(_v >> 24), (uint8_t)(_v >> 16), (uint8_t)(_v >> 8), (uint8_t)_v);                               \
1276
0
    } while (0)
1277
1278
#define ptls_buffer_push_quicint(buf, v)                                                                                           \
1279
    do {                                                                                                                           \
1280
        if ((ret = ptls_buffer_reserve((buf), PTLS_ENCODE_QUICINT_CAPACITY)) != 0)                                                 \
1281
            goto Exit;                                                                                                             \
1282
        uint8_t *d = ptls_encode_quicint((buf)->base + (buf)->off, (v));                                                           \
1283
        (buf)->off = d - (buf)->base;                                                                                              \
1284
    } while (0)
1285
1286
#define ptls_buffer_push_block(buf, _capacity, block)                                                                              \
1287
10.1k
    do {                                                                                                                           \
1288
10.1k
        size_t capacity = (_capacity);                                                                                             \
1289
10.1k
        ptls_buffer_pushv((buf), (uint8_t *)"\0\0\0\0\0\0\0", capacity != -1 ? capacity : 1);                                      \
1290
10.1k
        size_t body_start = (buf)->off;                                                                                            \
1291
10.1k
        do {                                                                                                                       \
1292
144k
            block                                                                                                                  \
1293
10.1k
        } while (0);                                                                                                               \
1294
10.1k
        size_t body_size = (buf)->off - body_start;                                                                                \
1295
10.1k
        if (capacity != -1) {                                                                                                      \
1296
10.1k
            if (capacity < sizeof(size_t) && body_size >= (size_t)1 << (capacity * 8)) {                                           \
1297
0
                ret = PTLS_ERROR_BLOCK_OVERFLOW;                                                                                   \
1298
0
                goto Exit;                                                                                                         \
1299
0
            }                                                                                                                      \
1300
25.0k
            for (; capacity != 0; --capacity)                                                                                      \
1301
14.8k
                (buf)->base[body_start - capacity] = (uint8_t)(body_size >> (8 * (capacity - 1)));                                 \
1302
10.1k
        } else {                                                                                                                   \
1303
0
            if ((ret = ptls_buffer__adjust_quic_blocksize((buf), body_size)) != 0)                                                 \
1304
0
                goto Exit;                                                                                                         \
1305
0
        }                                                                                                                          \
1306
10.1k
    } while (0)
1307
1308
#define ptls_buffer_push_asn1_block(buf, block)                                                                                    \
1309
0
    do {                                                                                                                           \
1310
0
        ptls_buffer_push((buf), 0xff); /* dummy */                                                                                 \
1311
0
        size_t body_start = (buf)->off;                                                                                            \
1312
0
        do {                                                                                                                       \
1313
0
            block                                                                                                                  \
1314
0
        } while (0);                                                                                                               \
1315
0
        size_t body_size = (buf)->off - body_start;                                                                                \
1316
0
        if (body_size < 128) {                                                                                                     \
1317
0
            (buf)->base[body_start - 1] = (uint8_t)body_size;                                                                      \
1318
0
        } else {                                                                                                                   \
1319
0
            if ((ret = ptls_buffer__adjust_asn1_blocksize((buf), body_size)) != 0)                                                 \
1320
0
                goto Exit;                                                                                                         \
1321
0
        }                                                                                                                          \
1322
0
    } while (0)
1323
1324
#define ptls_buffer_push_asn1_sequence(buf, block)                                                                                 \
1325
    do {                                                                                                                           \
1326
        ptls_buffer_push((buf), 0x30);                                                                                             \
1327
        ptls_buffer_push_asn1_block((buf), block);                                                                                 \
1328
    } while (0)
1329
1330
#define ptls_buffer_push_message_body(buf, key_sched, type, block)                                                                 \
1331
1.31k
    do {                                                                                                                           \
1332
1.31k
        ptls_buffer_t *_buf = (buf);                                                                                               \
1333
1.31k
        ptls_key_schedule_t *_key_sched = (key_sched);                                                                             \
1334
1.31k
        size_t mess_start = _buf->off;                                                                                             \
1335
1.31k
        ptls_buffer_push(_buf, (type));                                                                                            \
1336
1.31k
        ptls_buffer_push_block(_buf, 3, block);                                                                                    \
1337
1.31k
        if (_key_sched != NULL)                                                                                                    \
1338
1.31k
            ptls__key_schedule_update_hash(_key_sched, _buf->base + mess_start, _buf->off - mess_start, 0);                        \
1339
1.31k
    } while (0)
1340
1341
#define ptls_push_message(emitter, key_sched, type, block)                                                                         \
1342
1.16k
    do {                                                                                                                           \
1343
1.16k
        ptls_message_emitter_t *_emitter = (emitter);                                                                              \
1344
1.16k
        if ((ret = _emitter->begin_message(_emitter)) != 0)                                                                        \
1345
1.16k
            goto Exit;                                                                                                             \
1346
1.16k
        ptls_buffer_push_message_body(_emitter->buf, (key_sched), (type), block);                                                  \
1347
1.16k
        if ((ret = _emitter->commit_message(_emitter)) != 0)                                                                       \
1348
1.16k
            goto Exit;                                                                                                             \
1349
1.16k
    } while (0)
1350
1351
int ptls_decode8(uint8_t *value, const uint8_t **src, const uint8_t *end);
1352
int ptls_decode16(uint16_t *value, const uint8_t **src, const uint8_t *end);
1353
int ptls_decode24(uint32_t *value, const uint8_t **src, const uint8_t *end);
1354
int ptls_decode32(uint32_t *value, const uint8_t **src, const uint8_t *end);
1355
int ptls_decode64(uint64_t *value, const uint8_t **src, const uint8_t *end);
1356
uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
1357
1358
#define ptls_decode_open_block(src, end, capacity, block)                                                                          \
1359
9.34k
    do {                                                                                                                           \
1360
9.34k
        size_t _capacity = (capacity);                                                                                             \
1361
9.34k
        size_t _block_size;                                                                                                        \
1362
9.34k
        if (_capacity == -1) {                                                                                                     \
1363
0
            uint64_t _block_size64;                                                                                                \
1364
0
            const uint8_t *_src = (src);                                                                                           \
1365
0
            if ((_block_size64 = ptls_decode_quicint(&_src, end)) == UINT64_MAX ||                                                 \
1366
0
                (sizeof(size_t) < 8 && (_block_size64 >> (8 * sizeof(size_t))) != 0)) {                                            \
1367
0
                ret = PTLS_ALERT_DECODE_ERROR;                                                                                     \
1368
0
                goto Exit;                                                                                                         \
1369
0
            }                                                                                                                      \
1370
0
            (src) = _src;                                                                                                          \
1371
0
            _block_size = (size_t)_block_size64;                                                                                   \
1372
9.34k
        } else {                                                                                                                   \
1373
9.34k
            if (_capacity > (size_t)(end - (src))) {                                                                               \
1374
33
                ret = PTLS_ALERT_DECODE_ERROR;                                                                                     \
1375
33
                goto Exit;                                                                                                         \
1376
33
            }                                                                                                                      \
1377
9.34k
            _block_size = 0;                                                                                                       \
1378
15.5k
            do {                                                                                                                   \
1379
15.5k
                _block_size = _block_size << 8 | *(src)++;                                                                         \
1380
15.5k
            } while (--_capacity != 0);                                                                                            \
1381
9.31k
        }                                                                                                                          \
1382
9.34k
        if (_block_size > (size_t)(end - (src))) {                                                                                 \
1383
211
            ret = PTLS_ALERT_DECODE_ERROR;                                                                                         \
1384
211
            goto Exit;                                                                                                             \
1385
211
        }                                                                                                                          \
1386
9.31k
        do {                                                                                                                       \
1387
9.09k
            const uint8_t *const end = (src) + _block_size;                                                                        \
1388
9.09k
            do {                                                                                                                   \
1389
816k
                block                                                                                                              \
1390
8.10k
            } while (0);                                                                                                           \
1391
9.09k
            if ((src) != end) {                                                                                                    \
1392
0
                ret = PTLS_ALERT_DECODE_ERROR;                                                                                     \
1393
0
                goto Exit;                                                                                                         \
1394
0
            }                                                                                                                      \
1395
8.10k
        } while (0);                                                                                                               \
1396
9.09k
    } while (0)
1397
1398
#define ptls_decode_assert_block_close(src, end)                                                                                   \
1399
1.65k
    do {                                                                                                                           \
1400
1.65k
        if ((src) != end) {                                                                                                        \
1401
34
            ret = PTLS_ALERT_DECODE_ERROR;                                                                                         \
1402
34
            goto Exit;                                                                                                             \
1403
34
        }                                                                                                                          \
1404
1.65k
    } while (0);
1405
1406
#define ptls_decode_block(src, end, capacity, block)                                                                               \
1407
1.19k
    do {                                                                                                                           \
1408
1.19k
        ptls_decode_open_block((src), end, capacity, block);                                                                       \
1409
1.19k
        ptls_decode_assert_block_close((src), end);                                                                                \
1410
890
    } while (0)
1411
1412
#if PTLS_HAVE_LOG
1413
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, get_sni_arg, add_time, block)                                          \
1414
0
    do {                                                                                                                           \
1415
0
        int ptlslog_include_appdata = 0;                                                                                           \
1416
0
        do {                                                                                                                       \
1417
0
            ptls_log__do_write_start(&logpoint, (add_time));                                                                       \
1418
0
            do {                                                                                                                   \
1419
0
                block                                                                                                              \
1420
0
            } while (0);                                                                                                           \
1421
0
            ptlslog_include_appdata =                                                                                              \
1422
0
                ptls_log__do_write_end(&logpoint, (conn_state), (get_sni), (get_sni_arg), ptlslog_include_appdata);                \
1423
0
        } while (PTLS_UNLIKELY(ptlslog_include_appdata));                                                                          \
1424
0
    } while (0)
1425
#else
1426
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, get_sni_arg, add_time, block) /* don't generate code */
1427
#endif
1428
1429
#define PTLS_LOG_DEFINE_POINT(_module, _name, _var)                                                                                \
1430
6.66k
    static struct st_ptls_log_point_t _var = {.name = PTLS_TO_STR(_module) ":" PTLS_TO_STR(_name)}
1431
1432
#define PTLS_LOG(module, name, block)                                                                                              \
1433
    do {                                                                                                                           \
1434
        PTLS_LOG_DEFINE_POINT(module, name, logpoint);                                                                             \
1435
        if (PTLS_LIKELY(ptls_log_point_maybe_active(&logpoint) == 0))                                                              \
1436
            break;                                                                                                                 \
1437
        PTLS_LOG__DO_LOG(module, name, NULL, NULL, NULL, 1, {block});                                                              \
1438
    } while (0)
1439
1440
#define PTLS_LOG_CONN(name, tls, block)                                                                                            \
1441
6.66k
    do {                                                                                                                           \
1442
6.66k
        PTLS_LOG_DEFINE_POINT(picotls, name, logpoint);                                                                            \
1443
6.66k
        uint32_t active = ptls_log_point_maybe_active(&logpoint);                                                                  \
1444
6.66k
        if (PTLS_LIKELY(active == 0))                                                                                              \
1445
6.66k
            break;                                                                                                                 \
1446
6.66k
        ptls_t *_tls = (tls);                                                                                                      \
1447
0
        ptls_log_conn_state_t *conn_state = ptls_get_log_state(_tls);                                                              \
1448
0
        active &= ptls_log_conn_maybe_active(conn_state, (const char *(*)(void *))ptls_get_server_name, _tls);                     \
1449
0
        if (PTLS_LIKELY(active == 0))                                                                                              \
1450
0
            break;                                                                                                                 \
1451
0
        PTLS_LOG__DO_LOG(picotls, name, conn_state, (const char *(*)(void *))ptls_get_server_name, _tls, 1, {                      \
1452
0
            PTLS_LOG_ELEMENT_PTR(tls, _tls);                                                                                       \
1453
0
            do {                                                                                                                   \
1454
0
                block                                                                                                              \
1455
0
            } while (0);                                                                                                           \
1456
0
        });                                                                                                                        \
1457
0
    } while (0)
1458
1459
#define PTLS_LOG__ELEMENT_PREFIX_CORE(lit) ",\"" lit "\":"
1460
#define PTLS_LOG__ELEMENT_PREFIX(lit) PTLS_LOG__ELEMENT_PREFIX_CORE(lit), sizeof(PTLS_LOG__ELEMENT_PREFIX_CORE(lit)) - 1
1461
#define PTLS_LOG_ELEMENT_SAFESTR(name, value)                                                                                      \
1462
    do {                                                                                                                           \
1463
        const char *value_ = (value);                                                                                              \
1464
        ptls_log__do_push_element_safestr(PTLS_LOG__ELEMENT_PREFIX(PTLS_TO_STR(name)), (value_), strlen(value_));                  \
1465
    } while (0)
1466
#define PTLS_LOG_ELEMENT_UNSAFESTR(name, value, value_len)                                                                         \
1467
    ptls_log__do_push_element_unsafestr(PTLS_LOG__ELEMENT_PREFIX(PTLS_TO_STR(name)), (value), (value_len))
1468
#define PTLS_LOG_ELEMENT_HEXDUMP(name, value, value_len)                                                                           \
1469
    ptls_log__do_push_element_hexdump(PTLS_LOG__ELEMENT_PREFIX(PTLS_TO_STR(name)), (value), (value_len))
1470
#define PTLS_LOG_ELEMENT_PTR(name, value) PTLS_LOG_ELEMENT_UNSIGNED(name, (uint64_t)(value))
1471
#define PTLS_LOG_ELEMENT_SIGNED(name, value)                                                                                       \
1472
    do {                                                                                                                           \
1473
        if (sizeof(value) <= sizeof(int32_t)) {                                                                                    \
1474
            ptls_log__do_push_element_signed32(PTLS_LOG__ELEMENT_PREFIX(PTLS_TO_STR(name)), (value));                              \
1475
        } else {                                                                                                                   \
1476
            ptls_log__do_push_element_signed64(PTLS_LOG__ELEMENT_PREFIX(PTLS_TO_STR(name)), (value));                              \
1477
        }                                                                                                                          \
1478
    } while (0)
1479
#define PTLS_LOG__DO_ELEMENT_UNSIGNED(lit, value)                                                                                  \
1480
    do {                                                                                                                           \
1481
        if (sizeof(value) <= sizeof(uint32_t)) {                                                                                   \
1482
            ptls_log__do_push_element_unsigned32(PTLS_LOG__ELEMENT_PREFIX(lit), (value));                                          \
1483
        } else {                                                                                                                   \
1484
            ptls_log__do_push_element_unsigned64(PTLS_LOG__ELEMENT_PREFIX(lit), (value));                                          \
1485
        }                                                                                                                          \
1486
    } while (0)
1487
#define PTLS_LOG_ELEMENT_UNSIGNED(name, value) PTLS_LOG__DO_ELEMENT_UNSIGNED(PTLS_TO_STR(name), (value))
1488
#define PTLS_LOG_ELEMENT_BOOL(name, value) ptls_log__do_push_element_bool(PTLS_LOG__ELEMENT_PREFIX(PTLS_TO_STR(name)), (value))
1489
#define PTLS_LOG_APPDATA_ELEMENT_UNSAFESTR(name, value, value_len)                                                                 \
1490
    do {                                                                                                                           \
1491
        if (ptlslog_include_appdata) {                                                                                             \
1492
            PTLS_LOG_ELEMENT_UNSAFESTR(name, value, value_len);                                                                    \
1493
        } else {                                                                                                                   \
1494
            PTLS_LOG__DO_ELEMENT_UNSIGNED(PTLS_TO_STR(name) "_len", value_len);                                                    \
1495
        }                                                                                                                          \
1496
    } while (0)
1497
#define PTLS_LOG_APPDATA_ELEMENT_HEXDUMP(name, value, value_len)                                                                   \
1498
    do {                                                                                                                           \
1499
        if (ptlslog_include_appdata) {                                                                                             \
1500
            PTLS_LOG_ELEMENT_HEXDUMP(name, value, value_len);                                                                      \
1501
        } else {                                                                                                                   \
1502
            PTLS_LOG__DO_ELEMENT_UNSIGNED(PTLS_TO_STR(name) "_len", value_len);                                                    \
1503
        }                                                                                                                          \
1504
    } while (0)
1505
1506
/**
1507
 * retains a list of connections that are bound to the object
1508
 */
1509
struct st_ptls_log_state_t {
1510
    /**
1511
     * bit array of connections (1 is active)
1512
     */
1513
    uint32_t active_conns;
1514
    /**
1515
     * generation counter used for staleness check; see `ptls_log._generation`
1516
     */
1517
    uint64_t generation;
1518
};
1519
1520
/**
1521
 * represents a log point identified by name (`module:type`)
1522
 */
1523
struct st_ptls_log_point_t {
1524
    const char *name;
1525
    struct st_ptls_log_state_t state;
1526
};
1527
1528
/**
1529
 * represents a logging state of each connection
1530
 */
1531
typedef struct st_ptls_log_conn_state_t {
1532
    /**
1533
     * random value between 0 (inclusive) and 1 (non-inclusive) used to determine the ratio of sampling-based logging; see
1534
     * `ptls_add_fd'`. To disable logging entirely, use `ptls_log.dummy_conn_state`, or set the value exactly to 1.
1535
     */
1536
    float random_;
1537
    /**
1538
     * represents peer address; ipv4 addresses are stored using the mapped form (::ffff:192.0.2.1)
1539
     */
1540
    struct in6_addr address;
1541
    struct st_ptls_log_state_t state;
1542
} ptls_log_conn_state_t;
1543
1544
/**
1545
 * see `ptls_get_log_state`
1546
 */
1547
extern PTLS_THREADLOCAL ptls_log_conn_state_t *ptls_log_conn_state_override;
1548
1549
/**
1550
 * global variables exposed
1551
 */
1552
extern struct st_ptls_log_t {
1553
    /**
1554
     * if application-data (e.g., payload) should be emitted as well
1555
     */
1556
    volatile unsigned may_include_appdata : 1;
1557
    /**
1558
     * endpoints that want to disable logging entirely can provide this value to the loggers
1559
     */
1560
    ptls_log_conn_state_t dummy_conn_state;
1561
    /**
1562
     * generation counter that is incremented whenever the state of loggers change; see `st_ptls_log_state_t::generation`
1563
     */
1564
    volatile uint64_t _generation;
1565
} ptls_log;
1566
1567
/**
1568
 * initializes a ptls_log_conn_state_t
1569
 */
1570
void ptls_log_init_conn_state(ptls_log_conn_state_t *state, void (*random_bytes)(void *, size_t));
1571
/**
1572
 * forces recalculation of the log state (should be called when SNI is determined)
1573
 */
1574
static void ptls_log_recalc_conn_state(ptls_log_conn_state_t *state);
1575
/**
1576
 * returns a bitmap indicating the loggers active for given log point
1577
 */
1578
static uint32_t ptls_log_point_maybe_active(struct st_ptls_log_point_t *point);
1579
/**
1580
 * returns a bitmap indicating the loggers active for given connection
1581
 */
1582
static uint32_t ptls_log_conn_maybe_active(ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), void *get_sni_arg);
1583
1584
/**
1585
 * Returns the number of log events that were unable to be emitted.
1586
 */
1587
size_t ptls_log_num_lost(void);
1588
/**
1589
 * Registers an fd to the logger. A registered fd is automatically closed and removed when it is closed by the peer.
1590
 * @param sample_ratio  sampling ratio between 0 and 1
1591
 * @param points        list of points to log, in the form of p1\0p2\0\0 (i.e., concatenated list of C strings with an empty string
1592
 *                      marking the end). An empty list means attach to all.
1593
 * @param snis          list of SNIs to log, using the same form as points
1594
 * @param addresses     list of IPv4/v6 addresses to log, using the same form as points
1595
 */
1596
int ptls_log_add_fd(int fd, float sample_ratio, const char *points, const char *snis, const char *addresses, int appdata);
1597
1598
void ptls_log__recalc_point(int caller_locked, struct st_ptls_log_point_t *point);
1599
void ptls_log__recalc_conn(int caller_locked, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *),
1600
                           void *get_sni_arg);
1601
void ptls_log__do_push_element_safestr(const char *prefix, size_t prefix_len, const char *s, size_t l);
1602
void ptls_log__do_push_element_unsafestr(const char *prefix, size_t prefix_len, const char *s, size_t l);
1603
void ptls_log__do_push_element_hexdump(const char *prefix, size_t prefix_len, const void *s, size_t l);
1604
void ptls_log__do_push_element_signed32(const char *prefix, size_t prefix_len, int32_t v);
1605
void ptls_log__do_push_element_signed64(const char *prefix, size_t prefix_len, int64_t v);
1606
void ptls_log__do_push_element_unsigned32(const char *prefix, size_t prefix_len, uint32_t v);
1607
void ptls_log__do_push_element_unsigned64(const char *prefix, size_t prefix_len, uint64_t v);
1608
void ptls_log__do_push_element_bool(const char *prefix, size_t prefix_len, int v);
1609
void ptls_log__do_push_appdata_element_unsafestr(int includes_appdata, const char *prefix, size_t prefix_len, const char *s,
1610
                                                 size_t l);
1611
void ptls_log__do_push_appdata_element_hexdump(int includes_appdata, const char *prefix, size_t prefix_len, const void *s,
1612
                                               size_t l);
1613
void ptls_log__do_write_start(struct st_ptls_log_point_t *point, int add_time);
1614
int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *),
1615
                           void *get_sni_arg, int includes_appdata);
1616
1617
/**
1618
 * create a client object to handle new TLS connection
1619
 */
1620
ptls_t *ptls_client_new(ptls_context_t *ctx);
1621
/**
1622
 * create a server object to handle new TLS connection
1623
 */
1624
ptls_t *ptls_server_new(ptls_context_t *ctx);
1625
/**
1626
 * creates an object handle new TLS connection
1627
 */
1628
static ptls_t *ptls_new(ptls_context_t *ctx, int is_server);
1629
/**
1630
 * creates TLS 1.2 record layer for post-handshake communication
1631
 */
1632
int ptls_build_tls12_export_params(ptls_context_t *ctx, ptls_buffer_t *output, int is_server, int session_reused,
1633
                                   ptls_cipher_suite_t *cipher, const void *master_secret, const void *hello_randoms,
1634
                                   uint64_t next_send_record_iv, const char *server_name, ptls_iovec_t negotiated_protocol);
1635
/**
1636
 * store the parameters of a post-handshake TLS connection so that it can be reconstructed later
1637
 */
1638
int ptls_export(ptls_t *tls, ptls_buffer_t *output);
1639
/**
1640
 * create a post-handshake TLS connection object using given parameters
1641
 */
1642
int ptls_import(ptls_context_t *ctx, ptls_t **tls, ptls_iovec_t params);
1643
/**
1644
 * releases all resources associated to the object
1645
 */
1646
void ptls_free(ptls_t *tls);
1647
/**
1648
 * returns address of the crypto callbacks that the connection is using
1649
 */
1650
ptls_context_t *ptls_get_context(ptls_t *tls);
1651
/**
1652
 * updates the context of a connection. Can be called from `on_client_hello` callback.
1653
 */
1654
void ptls_set_context(ptls_t *tls, ptls_context_t *ctx);
1655
/**
1656
 * get the signature context
1657
 */
1658
ptls_async_job_t *ptls_get_async_job(ptls_t *tls);
1659
/**
1660
 * returns the client-random
1661
 */
1662
ptls_iovec_t ptls_get_client_random(ptls_t *tls);
1663
/**
1664
 * returns the cipher-suite being used
1665
 */
1666
ptls_cipher_suite_t *ptls_get_cipher(ptls_t *tls);
1667
/**
1668
 * returns a supported cipher-suite given an id
1669
 */
1670
ptls_cipher_suite_t *ptls_find_cipher_suite(ptls_cipher_suite_t **cipher_suites, uint16_t id);
1671
/**
1672
 * Returns protocol version (e.g., 0x0303 for TLS 1.2, 0x0304 for TLS 1.3). The result may be unstable prior to handshake
1673
 * completion.
1674
 */
1675
uint16_t ptls_get_protocol_version(ptls_t *tls);
1676
/**
1677
 * Returns current state of traffic keys. The cipher-suite being used, as well as the length of the traffic keys, can be obtained
1678
 * via `ptls_get_cipher`.
1679
 * TODO: Even in case of offloading just the TX side, there should be API for handling key updates, sending Close aleart.
1680
 */
1681
int ptls_get_traffic_keys(ptls_t *tls, int is_enc, uint8_t *key, uint8_t *iv, uint64_t *seq);
1682
/**
1683
 * returns the server-name (NULL if SNI is not used or failed to negotiate)
1684
 */
1685
const char *ptls_get_server_name(ptls_t *tls);
1686
/**
1687
 * sets the server-name associated to the TLS connection. If server_name_len is zero, then strlen(server_name) is called to
1688
 * determine the length of the name.
1689
 * On the client-side, the value is used for certificate validation. The value will be also sent as an SNI extension, if it looks
1690
 * like a DNS name.
1691
 * On the server-side, it can be called from on_client_hello to indicate the acceptance of the SNI extension to the client.
1692
 */
1693
int ptls_set_server_name(ptls_t *tls, const char *server_name, size_t server_name_len);
1694
/**
1695
 * returns the negotiated protocol (or NULL)
1696
 */
1697
const char *ptls_get_negotiated_protocol(ptls_t *tls);
1698
/**
1699
 * sets the negotiated protocol. If protocol_len is zero, strlen(protocol) is called to determine the length of the protocol name.
1700
 */
1701
int ptls_set_negotiated_protocol(ptls_t *tls, const char *protocol, size_t protocol_len);
1702
/**
1703
 * returns if the handshake has been completed
1704
 */
1705
int ptls_handshake_is_complete(ptls_t *tls);
1706
/**
1707
 * returns if a PSK (or PSK-DHE) handshake was performed
1708
 */
1709
int ptls_is_psk_handshake(ptls_t *tls);
1710
/**
1711
 * return if a ECH handshake was performed, as well as optionally the kem and cipher-suite being used
1712
 * FIXME: this function always return false when the TLS session is exported and imported
1713
 */
1714
int ptls_is_ech_handshake(ptls_t *tls, uint8_t *config_id, ptls_hpke_kem_t **kem, ptls_hpke_cipher_suite_t **cipher);
1715
/**
1716
 * returns a pointer to user data pointer (client is reponsible for freeing the associated data prior to calling ptls_free)
1717
 */
1718
void **ptls_get_data_ptr(ptls_t *tls);
1719
/**
1720
 * Returns `ptls_log_conn_state_t` of `ptls_t`. By default, the state is initialized by calling `ptls_log_init_conn_state`, but the
1721
 * behavior can be overidden by setting `ptls_log_conn_state_override`.
1722
 * This value can be changed by setting `ptls_log_random_override` or by calling `ptls_set_log_random`.
1723
 */
1724
ptls_log_conn_state_t *ptls_get_log_state(ptls_t *tls);
1725
/**
1726
 * proceeds with the handshake, optionally taking some input from peer. The function returns zero in case the handshake completed
1727
 * successfully. PTLS_ERROR_IN_PROGRESS is returned in case the handshake is incomplete. Otherwise, an error value is returned. The
1728
 * contents of sendbuf should be sent to the client, regardless of whether if an error is returned. inlen is an argument used for
1729
 * both input and output. As an input, the arguments takes the size of the data available as input. Upon return the value is updated
1730
 * to the number of bytes consumed by the handshake. In case the returned value is PTLS_ERROR_IN_PROGRESS there is a guarantee that
1731
 * all the input are consumed (i.e. the value of inlen does not change).
1732
 */
1733
int ptls_handshake(ptls_t *tls, ptls_buffer_t *sendbuf, const void *input, size_t *inlen, ptls_handshake_properties_t *args);
1734
/**
1735
 * decrypts the first record within given buffer
1736
 */
1737
int ptls_receive(ptls_t *tls, ptls_buffer_t *plaintextbuf, const void *input, size_t *len);
1738
/**
1739
 * encrypts given buffer into multiple TLS records
1740
 */
1741
int ptls_send(ptls_t *tls, ptls_buffer_t *sendbuf, const void *input, size_t inlen);
1742
/**
1743
 * updates the send traffic key (as well as asks the peer to update)
1744
 */
1745
int ptls_update_key(ptls_t *tls, int request_update);
1746
/**
1747
 * Returns if the context is a server context.
1748
 */
1749
int ptls_is_server(ptls_t *tls);
1750
/**
1751
 * returns per-record overhead
1752
 */
1753
size_t ptls_get_record_overhead(ptls_t *tls);
1754
/**
1755
 * sends an alert
1756
 */
1757
int ptls_send_alert(ptls_t *tls, ptls_buffer_t *sendbuf, uint8_t level, uint8_t description);
1758
/**
1759
 *
1760
 */
1761
int ptls_export_secret(ptls_t *tls, void *output, size_t outlen, const char *label, ptls_iovec_t context_value, int is_early);
1762
/**
1763
 * build the body of a Certificate message. Can be called with tls set to NULL in order to create a precompressed message.
1764
 */
1765
int ptls_build_certificate_message(ptls_buffer_t *buf, ptls_iovec_t request_context, ptls_iovec_t *certificates,
1766
                                   size_t num_certificates, ptls_iovec_t ocsp_status);
1767
/**
1768
 *
1769
 */
1770
int ptls_calc_hash(ptls_hash_algorithm_t *algo, void *output, const void *src, size_t len);
1771
/**
1772
 *
1773
 */
1774
ptls_hash_context_t *ptls_hmac_create(ptls_hash_algorithm_t *algo, const void *key, size_t key_size);
1775
/**
1776
 *
1777
 */
1778
int ptls_hkdf_extract(ptls_hash_algorithm_t *hash, void *output, ptls_iovec_t salt, ptls_iovec_t ikm);
1779
/**
1780
 *
1781
 */
1782
int ptls_hkdf_expand(ptls_hash_algorithm_t *hash, void *output, size_t outlen, ptls_iovec_t prk, ptls_iovec_t info);
1783
/**
1784
 *
1785
 */
1786
int ptls_hkdf_expand_label(ptls_hash_algorithm_t *algo, void *output, size_t outlen, ptls_iovec_t secret, const char *label,
1787
                           ptls_iovec_t hash_value, const char *label_prefix);
1788
/**
1789
 * The expansion function of TLS 1.2 defined in RFC 5426 section 5. When `label` is NULL, acts as P_<hash>, or if non-NULL, as PRF.
1790
 */
1791
int ptls_tls12_phash(ptls_hash_algorithm_t *algo, void *output, size_t outlen, ptls_iovec_t secret, const char *label,
1792
                     ptls_iovec_t seed);
1793
/**
1794
 * instantiates a symmetric cipher
1795
 */
1796
ptls_cipher_context_t *ptls_cipher_new(ptls_cipher_algorithm_t *algo, int is_enc, const void *key);
1797
/**
1798
 * destroys a symmetric cipher
1799
 */
1800
void ptls_cipher_free(ptls_cipher_context_t *ctx);
1801
/**
1802
 * initializes the IV; this function must be called prior to calling ptls_cipher_encrypt
1803
 */
1804
static void ptls_cipher_init(ptls_cipher_context_t *ctx, const void *iv);
1805
/**
1806
 * Encrypts given text. The function must be used in a way that the output length would be equal to the input length. For example,
1807
 * when using a block cipher in ECB mode, `len` must be a multiple of the block size when using a block cipher. The length can be
1808
 * of any value when using a stream cipher or a block cipher in CTR mode.
1809
 */
1810
static void ptls_cipher_encrypt(ptls_cipher_context_t *ctx, void *output, const void *input, size_t len);
1811
/**
1812
 * instantiates an AEAD cipher given a secret, which is expanded using hkdf to a set of key and iv
1813
 * @param aead
1814
 * @param hash
1815
 * @param is_enc 1 if creating a context for encryption, 0 if creating a context for decryption
1816
 * @param secret the secret. The size must be the digest length of the hash algorithm
1817
 * @return pointer to an AEAD context if successful, otherwise NULL
1818
 */
1819
ptls_aead_context_t *ptls_aead_new(ptls_aead_algorithm_t *aead, ptls_hash_algorithm_t *hash, int is_enc, const void *secret,
1820
                                   const char *label_prefix);
1821
/**
1822
 * instantiates an AEAD cipher given key and iv
1823
 * @param aead
1824
 * @param is_enc 1 if creating a context for encryption, 0 if creating a context for decryption
1825
 * @return pointer to an AEAD context if successful, otherwise NULL
1826
 */
1827
ptls_aead_context_t *ptls_aead_new_direct(ptls_aead_algorithm_t *aead, int is_enc, const void *key, const void *iv);
1828
/**
1829
 * destroys an AEAD cipher context
1830
 */
1831
void ptls_aead_free(ptls_aead_context_t *ctx);
1832
/**
1833
 * Permutes the static IV by applying given bytes using bit-wise XOR. This API can be used for supplying nonces longer than 64-
1834
 * bits.
1835
 */
1836
void ptls_aead_xor_iv(ptls_aead_context_t *ctx, const void *bytes, size_t len);
1837
static void ptls_aead_get_iv(ptls_aead_context_t *ctx, void *iv);
1838
static void ptls_aead_set_iv(ptls_aead_context_t *ctx, const void *iv);
1839
/**
1840
 * Encrypts one AEAD block, given input and output vectors.
1841
 */
1842
static size_t ptls_aead_encrypt(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
1843
                                const void *aad, size_t aadlen);
1844
/**
1845
 * Encrypts one AEAD block, as well as one block of ECB (for QUIC / DTLS packet number encryption). Depending on the AEAD engine
1846
 * being used, the two operations might run simultaneously.
1847
 */
1848
static void ptls_aead_encrypt_s(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
1849
                                const void *aad, size_t aadlen, ptls_aead_supplementary_encryption_t *supp);
1850
/**
1851
 * Encrypts one AEAD block, given a vector of vectors.
1852
 */
1853
static void ptls_aead_encrypt_v(ptls_aead_context_t *ctx, void *output, ptls_iovec_t *input, size_t incnt, uint64_t seq,
1854
                                const void *aad, size_t aadlen);
1855
/**
1856
 * Obsolete; new applications should use one of: `ptls_aead_encrypt`, `ptls_aead_encrypt_s`, `ptls_aead_encrypt_v`.
1857
 */
1858
static void ptls_aead_encrypt_init(ptls_aead_context_t *ctx, uint64_t seq, const void *aad, size_t aadlen);
1859
/**
1860
 * Obsolete; see `ptls_aead_encrypt_init`.
1861
 */
1862
static size_t ptls_aead_encrypt_update(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen);
1863
/**
1864
 * Obsolete; see `ptls_aead_encrypt_init`.
1865
 */
1866
static size_t ptls_aead_encrypt_final(ptls_aead_context_t *ctx, void *output);
1867
/**
1868
 * decrypts an AEAD record
1869
 * @return number of bytes emitted to output if successful, or SIZE_MAX if the input is invalid (e.g. broken MAC)
1870
 */
1871
static size_t ptls_aead_decrypt(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
1872
                                const void *aad, size_t aadlen);
1873
/**
1874
 * Return the current read epoch (i.e., that of the message being received or to be)
1875
 */
1876
size_t ptls_get_read_epoch(ptls_t *tls);
1877
/**
1878
 * Runs the handshake by dealing directly with handshake messages. Callers MUST delay supplying input to this function until the
1879
 * epoch of the input becomes equal to the value returned by `ptls_get_read_epoch()`.
1880
 * @param tls            the TLS context
1881
 * @param sendbuf        buffer to which the output will be written
1882
 * @param epoch_offsets  start and end offset of the messages in each epoch. For example, when the server emits ServerHello between
1883
 *                       offset 0 and 38, the following handshake messages between offset 39 and 348, and a post-handshake message
1884
 *                       between 349 and 451, epoch_offsets will be {0,39,39,349,452} and the length of the sendbuf will be 452.
1885
 *                       This argument is an I/O argument. Applications can either reset sendbuf to empty and epoch_offsets and to
1886
 *                       all zero every time they invoke the function, or retain the values until the handshake completes so that
1887
 *                       data will be appended to sendbuf and epoch_offsets will be adjusted.
1888
 * @param in_epoch       epoch of the input
1889
 * @param input          input bytes (must be NULL when starting the handshake on the client side)
1890
 * @param inlen          length of the input
1891
 * @param properties     properties specific to the running handshake
1892
 * @return same as `ptls_handshake`
1893
 */
1894
int ptls_handle_message(ptls_t *tls, ptls_buffer_t *sendbuf, size_t epoch_offsets[5], size_t in_epoch, const void *input,
1895
                        size_t inlen, ptls_handshake_properties_t *properties);
1896
int ptls_client_handle_message(ptls_t *tls, ptls_buffer_t *sendbuf, size_t epoch_offsets[5], size_t in_epoch, const void *input,
1897
                               size_t inlen, ptls_handshake_properties_t *properties);
1898
int ptls_server_handle_message(ptls_t *tls, ptls_buffer_t *sendbuf, size_t epoch_offsets[5], size_t in_epoch, const void *input,
1899
                               size_t inlen, ptls_handshake_properties_t *properties);
1900
/**
1901
 * internal
1902
 */
1903
void ptls_aead__build_iv(ptls_aead_algorithm_t *algo, uint8_t *iv, const uint8_t *static_iv, uint64_t seq);
1904
/**
1905
 *
1906
 */
1907
static void ptls_aead__do_encrypt(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
1908
                                  const void *aad, size_t aadlen, ptls_aead_supplementary_encryption_t *supp);
1909
/**
1910
 *
1911
 */
1912
static void ptls_aead__do_encrypt_v(ptls_aead_context_t *ctx, void *_output, ptls_iovec_t *input, size_t incnt, uint64_t seq,
1913
                                    const void *aad, size_t aadlen);
1914
/**
1915
 * internal
1916
 */
1917
void ptls__key_schedule_update_hash(ptls_key_schedule_t *sched, const uint8_t *msg, size_t msglen, int use_outer);
1918
/**
1919
 * clears memory
1920
 */
1921
extern void (*volatile ptls_clear_memory)(void *p, size_t len);
1922
/**
1923
 * constant-time memcmp
1924
 */
1925
extern int (*volatile ptls_mem_equal)(const void *x, const void *y, size_t len);
1926
/**
1927
 * checks if a server name is an IP address.
1928
 */
1929
int ptls_server_name_is_ipaddr(const char *name);
1930
/**
1931
 * encodes one ECH Config
1932
 */
1933
int ptls_ech_encode_config(ptls_buffer_t *buf, uint8_t config_id, ptls_hpke_kem_t *kem, ptls_iovec_t public_key,
1934
                           ptls_hpke_cipher_suite_t **ciphers, uint8_t max_name_length, const char *public_name);
1935
/**
1936
 * loads a certificate chain to ptls_context_t::certificates. `certificate.list` and each element of the list is allocated by
1937
 * malloc.  It is the responsibility of the user to free them when discarding the TLS context.
1938
 */
1939
int ptls_load_certificates(ptls_context_t *ctx, char const *cert_pem_file);
1940
/**
1941
 * SetupBaseS function of RFC 9180. Given `kem`, `algo`, `info`, and receiver's public key, returns an ephemeral public key and an
1942
 * AEAD context used for encrypting data.
1943
 */
1944
int ptls_hpke_setup_base_s(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_iovec_t *pk_s, ptls_aead_context_t **ctx,
1945
                           ptls_iovec_t pk_r, ptls_iovec_t info);
1946
/**
1947
 * SetupBaseR function of RFC 9180. Given `kem`, `algo`, `info`, receiver's private key (`keyex`), and the esnder's public key,
1948
 * returns the AEAD context to be used for decrypting data.
1949
 */
1950
int ptls_hpke_setup_base_r(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_key_exchange_context_t *keyex,
1951
                           ptls_aead_context_t **ctx, ptls_iovec_t pk_s, ptls_iovec_t info);
1952
/**
1953
 *
1954
 */
1955
char *ptls_hexdump(char *dst, const void *src, size_t len);
1956
/**
1957
 * Builds a JSON-safe string without double quotes. Supplied buffer MUST be at least 6x + 1 bytes larger than the input.
1958
 */
1959
char *ptls_jsonescape(char *buf, const char *s, size_t len);
1960
/**
1961
 * builds a v4-mapped address (i.e., ::ffff:192.0.2.1)
1962
 */
1963
void ptls_build_v4_mapped_v6_address(struct in6_addr *v6, const struct in_addr *v4);
1964
1965
/**
1966
 * the default get_time callback
1967
 */
1968
extern ptls_get_time_t ptls_get_time;
1969
/**
1970
 * default hash clone function that calls memcpy
1971
 */
1972
static void ptls_hash_clone_memcpy(void *dst, const void *src, size_t size);
1973
1974
/* inline functions */
1975
1976
inline uint32_t ptls_log_point_maybe_active(struct st_ptls_log_point_t *point)
1977
6.66k
{
1978
6.66k
#if PTLS_HAVE_LOG
1979
6.66k
    if (PTLS_UNLIKELY(point->state.generation != ptls_log._generation))
1980
6
        ptls_log__recalc_point(0, point);
1981
6.66k
    return point->state.active_conns;
1982
#else
1983
    return 0;
1984
#endif
1985
6.66k
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_log_point_maybe_active
picotls.c:ptls_log_point_maybe_active
Line
Count
Source
1977
6.66k
{
1978
6.66k
#if PTLS_HAVE_LOG
1979
6.66k
    if (PTLS_UNLIKELY(point->state.generation != ptls_log._generation))
1980
6
        ptls_log__recalc_point(0, point);
1981
6.66k
    return point->state.active_conns;
1982
#else
1983
    return 0;
1984
#endif
1985
6.66k
}
Unexecuted instantiation: hpke.c:ptls_log_point_maybe_active
Unexecuted instantiation: openssl.c:ptls_log_point_maybe_active
1986
1987
inline void ptls_log_recalc_conn_state(ptls_log_conn_state_t *state)
1988
0
{
1989
0
    state->state.generation = 0;
1990
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_log_recalc_conn_state
Unexecuted instantiation: picotls.c:ptls_log_recalc_conn_state
Unexecuted instantiation: hpke.c:ptls_log_recalc_conn_state
Unexecuted instantiation: openssl.c:ptls_log_recalc_conn_state
1991
1992
inline uint32_t ptls_log_conn_maybe_active(ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), void *get_sni_arg)
1993
0
{
1994
0
#if PTLS_HAVE_LOG
1995
0
    if (PTLS_UNLIKELY(conn->state.generation != ptls_log._generation))
1996
0
        ptls_log__recalc_conn(0, conn, get_sni, get_sni_arg);
1997
0
    return conn->state.active_conns;
1998
#else
1999
    return 0;
2000
#endif
2001
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_log_conn_maybe_active
Unexecuted instantiation: picotls.c:ptls_log_conn_maybe_active
Unexecuted instantiation: hpke.c:ptls_log_conn_maybe_active
Unexecuted instantiation: openssl.c:ptls_log_conn_maybe_active
2002
2003
inline ptls_t *ptls_new(ptls_context_t *ctx, int is_server)
2004
1.83k
{
2005
1.83k
    return is_server ? ptls_server_new(ctx) : ptls_client_new(ctx);
2006
1.83k
}
fuzz-client-hello.c:ptls_new
Line
Count
Source
2004
1.83k
{
2005
1.83k
    return is_server ? ptls_server_new(ctx) : ptls_client_new(ctx);
2006
1.83k
}
Unexecuted instantiation: picotls.c:ptls_new
Unexecuted instantiation: hpke.c:ptls_new
Unexecuted instantiation: openssl.c:ptls_new
2007
2008
inline ptls_iovec_t ptls_iovec_init(const void *p, size_t len)
2009
23.4k
{
2010
    /* avoid the "return (ptls_iovec_t){(uint8_t *)p, len};" construct because it requires C99
2011
     * and triggers a warning "C4204: nonstandard extension used: non-constant aggregate initializer"
2012
     * in Visual Studio */
2013
23.4k
    ptls_iovec_t r;
2014
23.4k
    r.base = (uint8_t *)p;
2015
23.4k
    r.len = len;
2016
23.4k
    return r;
2017
23.4k
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_iovec_init
picotls.c:ptls_iovec_init
Line
Count
Source
2009
23.4k
{
2010
    /* avoid the "return (ptls_iovec_t){(uint8_t *)p, len};" construct because it requires C99
2011
     * and triggers a warning "C4204: nonstandard extension used: non-constant aggregate initializer"
2012
     * in Visual Studio */
2013
23.4k
    ptls_iovec_t r;
2014
23.4k
    r.base = (uint8_t *)p;
2015
23.4k
    r.len = len;
2016
23.4k
    return r;
2017
23.4k
}
Unexecuted instantiation: hpke.c:ptls_iovec_init
Unexecuted instantiation: openssl.c:ptls_iovec_init
2018
2019
inline void ptls_buffer_init(ptls_buffer_t *buf, void *smallbuf, size_t smallbuf_size)
2020
9.43k
{
2021
9.43k
    assert(smallbuf != NULL);
2022
9.43k
    buf->base = (uint8_t *)smallbuf;
2023
9.43k
    buf->off = 0;
2024
9.43k
    buf->capacity = smallbuf_size;
2025
9.43k
    buf->is_allocated = 0;
2026
9.43k
    buf->align_bits = 0;
2027
9.43k
}
fuzz-client-hello.c:ptls_buffer_init
Line
Count
Source
2020
2.02k
{
2021
2.02k
    assert(smallbuf != NULL);
2022
2.02k
    buf->base = (uint8_t *)smallbuf;
2023
2.02k
    buf->off = 0;
2024
2.02k
    buf->capacity = smallbuf_size;
2025
2.02k
    buf->is_allocated = 0;
2026
2.02k
    buf->align_bits = 0;
2027
2.02k
}
picotls.c:ptls_buffer_init
Line
Count
Source
2020
7.40k
{
2021
7.40k
    assert(smallbuf != NULL);
2022
7.40k
    buf->base = (uint8_t *)smallbuf;
2023
7.40k
    buf->off = 0;
2024
7.40k
    buf->capacity = smallbuf_size;
2025
7.40k
    buf->is_allocated = 0;
2026
7.40k
    buf->align_bits = 0;
2027
7.40k
}
Unexecuted instantiation: hpke.c:ptls_buffer_init
Unexecuted instantiation: openssl.c:ptls_buffer_init
2028
2029
inline void ptls_buffer_dispose(ptls_buffer_t *buf)
2030
120k
{
2031
120k
    ptls_buffer__release_memory(buf);
2032
120k
    *buf = (ptls_buffer_t){NULL, 0, 0, 0, 0};
2033
120k
}
fuzz-client-hello.c:ptls_buffer_dispose
Line
Count
Source
2030
2.02k
{
2031
2.02k
    ptls_buffer__release_memory(buf);
2032
    *buf = (ptls_buffer_t){NULL, 0, 0, 0, 0};
2033
2.02k
}
picotls.c:ptls_buffer_dispose
Line
Count
Source
2030
118k
{
2031
118k
    ptls_buffer__release_memory(buf);
2032
    *buf = (ptls_buffer_t){NULL, 0, 0, 0, 0};
2033
118k
}
Unexecuted instantiation: hpke.c:ptls_buffer_dispose
Unexecuted instantiation: openssl.c:ptls_buffer_dispose
2034
2035
inline uint8_t *ptls_encode_quicint(uint8_t *p, uint64_t v)
2036
0
{
2037
0
    if (PTLS_UNLIKELY(v > 63)) {
2038
0
        if (PTLS_UNLIKELY(v > 16383)) {
2039
0
            unsigned sb;
2040
0
            if (PTLS_UNLIKELY(v > 1073741823)) {
2041
0
                assert(v <= 4611686018427387903);
2042
0
                *p++ = 0xc0 | (uint8_t)(v >> 56);
2043
0
                sb = 6 * 8;
2044
0
            } else {
2045
0
                *p++ = 0x80 | (uint8_t)(v >> 24);
2046
0
                sb = 2 * 8;
2047
0
            }
2048
0
            do {
2049
0
                *p++ = (uint8_t)(v >> sb);
2050
0
            } while ((sb -= 8) != 0);
2051
0
        } else {
2052
0
            *p++ = 0x40 | (uint8_t)((uint16_t)v >> 8);
2053
0
        }
2054
0
    }
2055
0
    *p++ = (uint8_t)v;
2056
0
    return p;
2057
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_encode_quicint
Unexecuted instantiation: picotls.c:ptls_encode_quicint
Unexecuted instantiation: hpke.c:ptls_encode_quicint
Unexecuted instantiation: openssl.c:ptls_encode_quicint
2058
2059
inline void ptls_cipher_init(ptls_cipher_context_t *ctx, const void *iv)
2060
0
{
2061
0
    ctx->do_init(ctx, iv);
2062
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_cipher_init
Unexecuted instantiation: picotls.c:ptls_cipher_init
Unexecuted instantiation: hpke.c:ptls_cipher_init
Unexecuted instantiation: openssl.c:ptls_cipher_init
2063
2064
inline void ptls_cipher_encrypt(ptls_cipher_context_t *ctx, void *output, const void *input, size_t len)
2065
0
{
2066
0
    ctx->do_transform(ctx, output, input, len);
2067
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_cipher_encrypt
Unexecuted instantiation: picotls.c:ptls_cipher_encrypt
Unexecuted instantiation: hpke.c:ptls_cipher_encrypt
Unexecuted instantiation: openssl.c:ptls_cipher_encrypt
2068
2069
inline void ptls_aead_get_iv(ptls_aead_context_t *ctx, void *iv)
2070
0
{
2071
0
    ctx->do_get_iv(ctx, iv);
2072
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_get_iv
Unexecuted instantiation: picotls.c:ptls_aead_get_iv
Unexecuted instantiation: hpke.c:ptls_aead_get_iv
Unexecuted instantiation: openssl.c:ptls_aead_get_iv
2073
2074
inline void ptls_aead_set_iv(ptls_aead_context_t *ctx, const void *iv)
2075
0
{
2076
0
    ctx->do_set_iv(ctx, iv);
2077
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_set_iv
Unexecuted instantiation: picotls.c:ptls_aead_set_iv
Unexecuted instantiation: hpke.c:ptls_aead_set_iv
Unexecuted instantiation: openssl.c:ptls_aead_set_iv
2078
2079
inline size_t ptls_aead_encrypt(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
2080
                                const void *aad, size_t aadlen)
2081
0
{
2082
0
    ctx->do_encrypt(ctx, output, input, inlen, seq, aad, aadlen, NULL);
2083
0
    return inlen + ctx->algo->tag_size;
2084
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_encrypt
Unexecuted instantiation: picotls.c:ptls_aead_encrypt
Unexecuted instantiation: hpke.c:ptls_aead_encrypt
Unexecuted instantiation: openssl.c:ptls_aead_encrypt
2085
2086
inline void ptls_aead_encrypt_s(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
2087
                                const void *aad, size_t aadlen, ptls_aead_supplementary_encryption_t *supp)
2088
0
{
2089
0
    ctx->do_encrypt(ctx, output, input, inlen, seq, aad, aadlen, supp);
2090
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_encrypt_s
Unexecuted instantiation: picotls.c:ptls_aead_encrypt_s
Unexecuted instantiation: hpke.c:ptls_aead_encrypt_s
Unexecuted instantiation: openssl.c:ptls_aead_encrypt_s
2091
2092
inline void ptls_aead_encrypt_v(ptls_aead_context_t *ctx, void *output, ptls_iovec_t *input, size_t incnt, uint64_t seq,
2093
                                const void *aad, size_t aadlen)
2094
0
{
2095
0
    ctx->do_encrypt_v(ctx, output, input, incnt, seq, aad, aadlen);
2096
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_encrypt_v
Unexecuted instantiation: picotls.c:ptls_aead_encrypt_v
Unexecuted instantiation: hpke.c:ptls_aead_encrypt_v
Unexecuted instantiation: openssl.c:ptls_aead_encrypt_v
2097
2098
inline void ptls_aead_encrypt_init(ptls_aead_context_t *ctx, uint64_t seq, const void *aad, size_t aadlen)
2099
0
{
2100
0
    ctx->do_encrypt_init(ctx, seq, aad, aadlen);
2101
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_encrypt_init
Unexecuted instantiation: picotls.c:ptls_aead_encrypt_init
Unexecuted instantiation: hpke.c:ptls_aead_encrypt_init
Unexecuted instantiation: openssl.c:ptls_aead_encrypt_init
2102
2103
inline size_t ptls_aead_encrypt_update(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen)
2104
0
{
2105
0
    return ctx->do_encrypt_update(ctx, output, input, inlen);
2106
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_encrypt_update
Unexecuted instantiation: picotls.c:ptls_aead_encrypt_update
Unexecuted instantiation: hpke.c:ptls_aead_encrypt_update
Unexecuted instantiation: openssl.c:ptls_aead_encrypt_update
2107
2108
inline size_t ptls_aead_encrypt_final(ptls_aead_context_t *ctx, void *output)
2109
0
{
2110
0
    return ctx->do_encrypt_final(ctx, output);
2111
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_encrypt_final
Unexecuted instantiation: picotls.c:ptls_aead_encrypt_final
Unexecuted instantiation: hpke.c:ptls_aead_encrypt_final
Unexecuted instantiation: openssl.c:ptls_aead_encrypt_final
2112
2113
inline void ptls_aead__do_encrypt(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
2114
                                  const void *aad, size_t aadlen, ptls_aead_supplementary_encryption_t *supp)
2115
0
{
2116
0
    ptls_iovec_t invec = ptls_iovec_init(input, inlen);
2117
0
    ctx->do_encrypt_v(ctx, output, &invec, 1, seq, aad, aadlen);
2118
2119
0
    if (supp != NULL) {
2120
0
        ptls_cipher_init(supp->ctx, supp->input);
2121
0
        memset(supp->output, 0, sizeof(supp->output));
2122
0
        ptls_cipher_encrypt(supp->ctx, supp->output, supp->output, sizeof(supp->output));
2123
0
    }
2124
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead__do_encrypt
Unexecuted instantiation: picotls.c:ptls_aead__do_encrypt
Unexecuted instantiation: hpke.c:ptls_aead__do_encrypt
Unexecuted instantiation: openssl.c:ptls_aead__do_encrypt
2125
2126
inline void ptls_aead__do_encrypt_v(ptls_aead_context_t *ctx, void *_output, ptls_iovec_t *input, size_t incnt, uint64_t seq,
2127
                                    const void *aad, size_t aadlen)
2128
0
{
2129
0
    uint8_t *output = (uint8_t *)_output;
2130
2131
0
    ctx->do_encrypt_init(ctx, seq, aad, aadlen);
2132
0
    for (size_t i = 0; i < incnt; ++i)
2133
0
        output += ctx->do_encrypt_update(ctx, output, input[i].base, input[i].len);
2134
0
    ctx->do_encrypt_final(ctx, output);
2135
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead__do_encrypt_v
Unexecuted instantiation: picotls.c:ptls_aead__do_encrypt_v
Unexecuted instantiation: hpke.c:ptls_aead__do_encrypt_v
Unexecuted instantiation: openssl.c:ptls_aead__do_encrypt_v
2136
2137
inline size_t ptls_aead_decrypt(ptls_aead_context_t *ctx, void *output, const void *input, size_t inlen, uint64_t seq,
2138
                                const void *aad, size_t aadlen)
2139
0
{
2140
0
    return ctx->do_decrypt(ctx, output, input, inlen, seq, aad, aadlen);
2141
0
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_aead_decrypt
Unexecuted instantiation: picotls.c:ptls_aead_decrypt
Unexecuted instantiation: hpke.c:ptls_aead_decrypt
Unexecuted instantiation: openssl.c:ptls_aead_decrypt
2142
2143
inline void ptls_hash_clone_memcpy(void *dst, const void *src, size_t size)
2144
144
{
2145
144
    memcpy(dst, src, size);
2146
144
}
Unexecuted instantiation: fuzz-client-hello.c:ptls_hash_clone_memcpy
Unexecuted instantiation: picotls.c:ptls_hash_clone_memcpy
Unexecuted instantiation: hpke.c:ptls_hash_clone_memcpy
openssl.c:ptls_hash_clone_memcpy
Line
Count
Source
2144
144
{
2145
144
    memcpy(dst, src, size);
2146
144
}
2147
2148
#define ptls_define_hash(name, ctx_type, init_func, update_func, final_func)                                                       \
2149
    ptls_define_hash6(name, ctx_type, init_func, update_func, final_func, ptls_hash_clone_memcpy)
2150
#define ptls_define_hash6(name, ctx_type, init_func, update_func, final_func, clone_func)                                          \
2151
                                                                                                                                   \
2152
    struct name##_context_t {                                                                                                      \
2153
        ptls_hash_context_t super;                                                                                                 \
2154
        ctx_type ctx;                                                                                                              \
2155
    };                                                                                                                             \
2156
                                                                                                                                   \
2157
    static void name##_update(ptls_hash_context_t *_ctx, const void *src, size_t len)                                              \
2158
27.4k
    {                                                                                                                              \
2159
27.4k
        struct name##_context_t *ctx = (struct name##_context_t *)_ctx;                                                            \
2160
27.4k
        update_func(&ctx->ctx, src, len);                                                                                          \
2161
27.4k
    }                                                                                                                              \
openssl.c:sha256_update
Line
Count
Source
2158
8.79k
    {                                                                                                                              \
2159
8.79k
        struct name##_context_t *ctx = (struct name##_context_t *)_ctx;                                                            \
2160
8.79k
        update_func(&ctx->ctx, src, len);                                                                                          \
2161
8.79k
    }                                                                                                                              \
openssl.c:sha384_update
Line
Count
Source
2158
18.6k
    {                                                                                                                              \
2159
18.6k
        struct name##_context_t *ctx = (struct name##_context_t *)_ctx;                                                            \
2160
18.6k
        update_func(&ctx->ctx, src, len);                                                                                          \
2161
18.6k
    }                                                                                                                              \
Unexecuted instantiation: openssl.c:sha512_update
2162
                                                                                                                                   \
2163
    static void name##_final(ptls_hash_context_t *_ctx, void *md, ptls_hash_final_mode_t mode)                                     \
2164
14.9k
    {                                                                                                                              \
2165
14.9k
        struct name##_context_t *ctx = (struct name##_context_t *)_ctx;                                                            \
2166
14.9k
        if (mode == PTLS_HASH_FINAL_MODE_SNAPSHOT) {                                                                               \
2167
1.41k
            ctx_type copy = ctx->ctx;                                                                                              \
2168
1.41k
            final_func(&copy, md);                                                                                                 \
2169
1.41k
            ptls_clear_memory(&copy, sizeof(copy));                                                                                \
2170
1.41k
            return;                                                                                                                \
2171
1.41k
        }                                                                                                                          \
2172
14.9k
        if (md != NULL)                                                                                                            \
2173
13.5k
            final_func(&ctx->ctx, md);                                                                                             \
2174
13.5k
        switch (mode) {                                                                                                            \
2175
5.39k
        case PTLS_HASH_FINAL_MODE_FREE:                                                                                            \
2176
5.39k
            ptls_clear_memory(&ctx->ctx, sizeof(ctx->ctx));                                                                        \
2177
5.39k
            free(ctx);                                                                                                             \
2178
5.39k
            break;                                                                                                                 \
2179
8.15k
        case PTLS_HASH_FINAL_MODE_RESET:                                                                                           \
2180
8.15k
            init_func(&ctx->ctx);                                                                                                  \
2181
8.15k
            break;                                                                                                                 \
2182
0
        default:                                                                                                                   \
2183
0
            assert(!"FIXME");                                                                                                      \
2184
0
            break;                                                                                                                 \
2185
13.5k
        }                                                                                                                          \
2186
13.5k
    }                                                                                                                              \
2187
                                                                                                                                   \
2188
    static ptls_hash_context_t *name##_clone(ptls_hash_context_t *_src)                                                            \
2189
144
    {                                                                                                                              \
2190
144
        struct name##_context_t *dst, *src = (struct name##_context_t *)_src;                                                      \
2191
144
        if ((dst = malloc(sizeof(*dst))) == NULL)                                                                                  \
2192
144
            return NULL;                                                                                                           \
2193
144
        dst->super = src->super;                                                                                                   \
2194
144
        clone_func(&dst->ctx, &src->ctx, sizeof(dst->ctx));                                                                        \
2195
144
        return &dst->super;                                                                                                        \
2196
144
    }                                                                                                                              \
2197
                                                                                                                                   \
2198
    static ptls_hash_context_t *name##_create(void)                                                                                \
2199
5.25k
    {                                                                                                                              \
2200
5.25k
        struct name##_context_t *ctx;                                                                                              \
2201
5.25k
        if ((ctx = malloc(sizeof(*ctx))) == NULL)                                                                                  \
2202
5.25k
            return NULL;                                                                                                           \
2203
5.25k
        ctx->super = (ptls_hash_context_t){name##_update, name##_final, name##_clone};                                             \
2204
5.25k
        init_func(&ctx->ctx);                                                                                                      \
2205
5.25k
        return &ctx->super;                                                                                                        \
2206
5.25k
    }
2207
2208
#ifdef __cplusplus
2209
}
2210
#endif
2211
2212
#endif