Coverage Report

Created: 2026-08-13 06:20

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