blob: edd45e76a5e7c6fe6dc79fbb8e7c8b148fc8788e [file] [log] [blame]
Amaury Denoyellef3c40f82022-09-30 17:37:38 +02001#include <haproxy/quic_tls.h>
2
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003#include <string.h>
4
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005#include <openssl/evp.h>
6#include <openssl/kdf.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +02007#include <openssl/ssl.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01008
9#include <haproxy/buf.h>
10#include <haproxy/chunk.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020011#include <haproxy/pool.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020012#include <haproxy/quic_conn-t.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010013
14
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +010015DECLARE_POOL(pool_head_quic_tls_secret, "quic_tls_secret", QUIC_TLS_SECRET_LEN);
16DECLARE_POOL(pool_head_quic_tls_iv, "quic_tls_iv", QUIC_TLS_IV_LEN);
17DECLARE_POOL(pool_head_quic_tls_key, "quic_tls_key", QUIC_TLS_KEY_LEN);
18
Amaury Denoyellea19bb6f2022-09-30 17:31:18 +020019/* Initial salt depending on QUIC version to derive client/server initial secrets.
20 * This one is for draft-29 QUIC version.
21 */
22const unsigned char initial_salt_draft_29[20] = {
23 0xaf, 0xbf, 0xec, 0x28, 0x99, 0x93, 0xd2, 0x4c,
24 0x9e, 0x97, 0x86, 0xf1, 0x9c, 0x61, 0x11, 0xe0,
25 0x43, 0x90, 0xa8, 0x99
26};
27
28const unsigned char initial_salt_v1[20] = {
29 0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3,
30 0x4d, 0x17, 0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad,
31 0xcc, 0xbb, 0x7f, 0x0a
32};
33
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +010034const unsigned char initial_salt_v2[20] = {
35 0x0d, 0xed, 0xe3, 0xde, 0xf7, 0x00, 0xa6, 0xdb,
36 0x81, 0x93, 0x81, 0xbe, 0x6e, 0x26, 0x9d, 0xcb,
37 0xf9, 0xbd, 0x2e, 0xd9
Amaury Denoyellea19bb6f2022-09-30 17:31:18 +020038};
39
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040/* Dump the RX/TX secrets of <secs> QUIC TLS secrets. */
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +010041void quic_tls_keys_hexdump(struct buffer *buf,
42 const struct quic_tls_secrets *secs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010043{
44 int i;
45 size_t aead_keylen = (size_t)EVP_CIPHER_key_length(secs->aead);
46 size_t aead_ivlen = (size_t)EVP_CIPHER_iv_length(secs->aead);
47 size_t hp_len = (size_t)EVP_CIPHER_key_length(secs->hp);
48
49 chunk_appendf(buf, "\n key=");
50 for (i = 0; i < aead_keylen; i++)
51 chunk_appendf(buf, "%02x", secs->key[i]);
52 chunk_appendf(buf, "\n iv=");
53 for (i = 0; i < aead_ivlen; i++)
54 chunk_appendf(buf, "%02x", secs->iv[i]);
55 chunk_appendf(buf, "\n hp=");
56 for (i = 0; i < hp_len; i++)
57 chunk_appendf(buf, "%02x", secs->hp_key[i]);
58}
59
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +010060/* Dump the RX/TX secrets of <kp> QUIC TLS key phase */
61void quic_tls_kp_keys_hexdump(struct buffer *buf,
62 const struct quic_tls_kp *kp)
63{
64 int i;
65
66 chunk_appendf(buf, "\n secret=");
67 for (i = 0; i < kp->secretlen; i++)
68 chunk_appendf(buf, "%02x", kp->secret[i]);
69 chunk_appendf(buf, "\n key=");
70 for (i = 0; i < kp->keylen; i++)
71 chunk_appendf(buf, "%02x", kp->key[i]);
72 chunk_appendf(buf, "\n iv=");
73 for (i = 0; i < kp->ivlen; i++)
74 chunk_appendf(buf, "%02x", kp->iv[i]);
75}
76
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010077/* Dump <secret> TLS secret. */
78void quic_tls_secret_hexdump(struct buffer *buf,
79 const unsigned char *secret, size_t secret_len)
80{
81 int i;
82
83 chunk_appendf(buf, " secret=");
84 for (i = 0; i < secret_len; i++)
85 chunk_appendf(buf, "%02x", secret[i]);
86}
87
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010088int quic_hkdf_extract(const EVP_MD *md,
Frédéric Lécaille4ba3b4e2022-05-10 18:40:19 +020089 unsigned char *buf, size_t buflen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010090 const unsigned char *key, size_t keylen,
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +020091 const unsigned char *salt, size_t saltlen)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010092{
93 EVP_PKEY_CTX *ctx;
94
95 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
96 if (!ctx)
97 return 0;
98
99 if (EVP_PKEY_derive_init(ctx) <= 0 ||
100 EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) <= 0 ||
101 EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 ||
102 EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, saltlen) <= 0 ||
103 EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 ||
Frédéric Lécaille4ba3b4e2022-05-10 18:40:19 +0200104 EVP_PKEY_derive(ctx, buf, &buflen) <= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100105 goto err;
106
107 EVP_PKEY_CTX_free(ctx);
108 return 1;
109
110 err:
111 EVP_PKEY_CTX_free(ctx);
112 return 0;
113}
114
115int quic_hkdf_expand(const EVP_MD *md,
116 unsigned char *buf, size_t buflen,
117 const unsigned char *key, size_t keylen,
118 const unsigned char *label, size_t labellen)
119{
120 EVP_PKEY_CTX *ctx;
121
122 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
123 if (!ctx)
124 return 0;
125
126 if (EVP_PKEY_derive_init(ctx) <= 0 ||
127 EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) <= 0 ||
128 EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 ||
129 EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 ||
130 EVP_PKEY_CTX_add1_hkdf_info(ctx, label, labellen) <= 0 ||
131 EVP_PKEY_derive(ctx, buf, &buflen) <= 0)
132 goto err;
133
134 EVP_PKEY_CTX_free(ctx);
135 return 1;
136
137 err:
138 EVP_PKEY_CTX_free(ctx);
139 return 0;
140}
Frédéric Lécaille7b92c812022-05-06 09:54:48 +0200141
142/* Extracts a peudo-random secret key from <key> which is eventually not
143 * pseudo-random and expand it to a new pseudo-random key into
144 * <buf> with <buflen> as key length according to HKDF specifications
145 * (https://datatracker.ietf.org/doc/html/rfc5869).
146 * According to this specifications it is highly recommended to use
147 * a salt, even if optional (NULL value).
148 * Return 1 if succeeded, 0 if not.
149 */
150int quic_hkdf_extract_and_expand(const EVP_MD *md,
151 unsigned char *buf, size_t buflen,
152 const unsigned char *key, size_t keylen,
153 const unsigned char *salt, size_t saltlen,
154 const unsigned char *label, size_t labellen)
155{
156 EVP_PKEY_CTX *ctx;
157
158 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
159 if (!ctx)
160 return 0;
161
162 if (EVP_PKEY_derive_init(ctx) <= 0 ||
163 EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) <= 0 ||
164 EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 ||
165 EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, saltlen) <= 0 ||
166 EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 ||
167 EVP_PKEY_CTX_add1_hkdf_info(ctx, label, labellen) <= 0 ||
168 EVP_PKEY_derive(ctx, buf, &buflen) <= 0)
169 goto err;
170
171 EVP_PKEY_CTX_free(ctx);
172 return 1;
173
174 err:
175 EVP_PKEY_CTX_free(ctx);
176 return 0;
177}
178
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100179/* https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#protection-keys
180 * refers to:
181 *
182 * https://tools.ietf.org/html/rfc8446#section-7.1:
183 * 7.1. Key Schedule
184 *
185 * The key derivation process makes use of the HKDF-Extract and
186 * HKDF-Expand functions as defined for HKDF [RFC5869], as well as the
187 * functions defined below:
188 *
189 * HKDF-Expand-Label(Secret, Label, Context, Length) =
190 * HKDF-Expand(Secret, HkdfLabel, Length)
191 *
192 * Where HkdfLabel is specified as:
193 *
194 * struct {
195 * uint16 length = Length;
196 * opaque label<7..255> = "tls13 " + Label;
197 * opaque context<0..255> = Context;
198 * } HkdfLabel;
199 *
200 * Derive-Secret(Secret, Label, Messages) =
201 * HKDF-Expand-Label(Secret, Label,
202 * Transcript-Hash(Messages), Hash.length)
203 *
204 */
205int quic_hkdf_expand_label(const EVP_MD *md,
206 unsigned char *buf, size_t buflen,
207 const unsigned char *key, size_t keylen,
208 const unsigned char *label, size_t labellen)
209{
210 unsigned char hdkf_label[256], *pos;
211 const unsigned char hdkf_label_label[] = "tls13 ";
212 size_t hdkf_label_label_sz = sizeof hdkf_label_label - 1;
213
214 pos = hdkf_label;
215 *pos++ = buflen >> 8;
216 *pos++ = buflen & 0xff;
217 *pos++ = hdkf_label_label_sz + labellen;
218 memcpy(pos, hdkf_label_label, hdkf_label_label_sz);
219 pos += hdkf_label_label_sz;
220 memcpy(pos, label, labellen);
221 pos += labellen;
222 *pos++ = '\0';
223
224 return quic_hkdf_expand(md, buf, buflen,
225 key, keylen, hdkf_label, pos - hdkf_label);
226}
227
228/*
229 * This function derives two keys from <secret> is <ctx> as TLS cryptographic context.
230 * ->key is the TLS key to be derived to encrypt/decrypt data at TLS level.
231 * ->iv is the initialization vector to be used with ->key.
232 * ->hp_key is the key to be derived for header protection.
233 * Obviouly these keys have the same size becaused derived with the same TLS cryptographic context.
234 */
235int quic_tls_derive_keys(const EVP_CIPHER *aead, const EVP_CIPHER *hp,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200236 const EVP_MD *md, const struct quic_version *qv,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100237 unsigned char *key, size_t keylen,
238 unsigned char *iv, size_t ivlen,
239 unsigned char *hp_key, size_t hp_keylen,
240 const unsigned char *secret, size_t secretlen)
241{
242 size_t aead_keylen = (size_t)EVP_CIPHER_key_length(aead);
243 size_t aead_ivlen = (size_t)EVP_CIPHER_iv_length(aead);
Frédéric Lécaille6e351d62021-11-30 11:06:41 +0100244 size_t hp_len = hp ? (size_t)EVP_CIPHER_key_length(hp) : 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245
246 if (aead_keylen > keylen || aead_ivlen > ivlen || hp_len > hp_keylen)
247 return 0;
248
249 if (!quic_hkdf_expand_label(md, key, aead_keylen, secret, secretlen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200250 qv->key_label,qv->key_label_len) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100251 !quic_hkdf_expand_label(md, iv, aead_ivlen, secret, secretlen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200252 qv->iv_label, qv->iv_label_len) ||
Frédéric Lécaille6e351d62021-11-30 11:06:41 +0100253 (hp_key && !quic_hkdf_expand_label(md, hp_key, hp_len, secret, secretlen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200254 qv->hp_label, qv->hp_label_len)))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100255 return 0;
256
257 return 1;
258}
259
260/*
261 * Derive the initial secret from <secret> and QUIC version dependent salt.
262 * Returns the size of the derived secret if succeeded, 0 if not.
263 */
264int quic_derive_initial_secret(const EVP_MD *md,
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +0200265 const unsigned char *initial_salt, size_t initial_salt_sz,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100266 unsigned char *initial_secret, size_t initial_secret_sz,
267 const unsigned char *secret, size_t secret_sz)
268{
Frédéric Lécaille4ba3b4e2022-05-10 18:40:19 +0200269 if (!quic_hkdf_extract(md, initial_secret, initial_secret_sz, secret, secret_sz,
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +0200270 initial_salt, initial_salt_sz))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100271 return 0;
272
273 return 1;
274}
275
276/*
277 * Derive the client initial secret from the initial secret.
278 * Returns the size of the derived secret if succeeded, 0 if not.
279 */
280int quic_tls_derive_initial_secrets(const EVP_MD *md,
281 unsigned char *rx, size_t rx_sz,
282 unsigned char *tx, size_t tx_sz,
283 const unsigned char *secret, size_t secret_sz,
284 int server)
285{
286 const unsigned char client_label[] = "client in";
287 const unsigned char server_label[] = "server in";
288 const unsigned char *tx_label, *rx_label;
289 size_t rx_label_sz, tx_label_sz;
290
291 if (server) {
292 rx_label = client_label;
293 rx_label_sz = sizeof client_label;
294 tx_label = server_label;
295 tx_label_sz = sizeof server_label;
296 }
297 else {
298 rx_label = server_label;
299 rx_label_sz = sizeof server_label;
300 tx_label = client_label;
301 tx_label_sz = sizeof client_label;
302 }
303
304 if (!quic_hkdf_expand_label(md, rx, rx_sz, secret, secret_sz,
305 rx_label, rx_label_sz - 1) ||
306 !quic_hkdf_expand_label(md, tx, tx_sz, secret, secret_sz,
307 tx_label, tx_label_sz - 1))
308 return 0;
309
310 return 1;
311}
312
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100313/* Update <sec> secret key into <new_sec> according to RFC 9001 6.1.
314 * Always succeeds.
315 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200316int quic_tls_sec_update(const EVP_MD *md, const struct quic_version *qv,
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100317 unsigned char *new_sec, size_t new_seclen,
318 const unsigned char *sec, size_t seclen)
319{
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100320 return quic_hkdf_expand_label(md, new_sec, new_seclen, sec, seclen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200321 qv->ku_label, qv->ku_label_len);
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100322}
323
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324/*
325 * Build an IV into <iv> buffer with <ivlen> as size from <aead_iv> with
326 * <aead_ivlen> as size depending on <pn> packet number.
327 * This is the function which must be called to build an AEAD IV for the AEAD cryptographic algorithm
328 * used to encrypt/decrypt the QUIC packet payloads depending on the packet number <pn>.
329 * This function fails and return 0 only if the two buffer lengths are different, 1 if not.
330 */
331int quic_aead_iv_build(unsigned char *iv, size_t ivlen,
332 unsigned char *aead_iv, size_t aead_ivlen, uint64_t pn)
333{
334 int i;
335 unsigned int shift;
336 unsigned char *pos = iv;
337
338 if (ivlen != aead_ivlen)
339 return 0;
340
341 for (i = 0; i < ivlen - sizeof pn; i++)
342 *pos++ = *aead_iv++;
343
344 /* Only the remaining (sizeof pn) bytes are XOR'ed. */
345 shift = 56;
346 for (i = aead_ivlen - sizeof pn; i < aead_ivlen ; i++, shift -= 8)
347 *pos++ = *aead_iv++ ^ (pn >> shift);
348
349 return 1;
350}
351
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200352/* Initialize the cipher context for RX part of <tls_ctx> QUIC TLS context.
353 * Return 1 if succeeded, 0 if not.
354 */
355int quic_tls_rx_ctx_init(EVP_CIPHER_CTX **rx_ctx,
356 const EVP_CIPHER *aead, unsigned char *key)
357{
358 EVP_CIPHER_CTX *ctx;
359 int aead_nid = EVP_CIPHER_nid(aead);
360
361 ctx = EVP_CIPHER_CTX_new();
362 if (!ctx)
363 return 0;
364
365 if (!EVP_DecryptInit_ex(ctx, aead, NULL, NULL, NULL) ||
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +0200366 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, QUIC_TLS_IV_LEN, NULL) ||
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200367 (aead_nid == NID_aes_128_ccm &&
368 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) ||
369 !EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL))
370 goto err;
371
372 *rx_ctx = ctx;
373
374 return 1;
375
376 err:
377 EVP_CIPHER_CTX_free(ctx);
378 return 0;
379}
380
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200381/* Initialize <*aes_ctx> AES cipher context with <key> as key for encryption */
382int quic_tls_enc_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx,
383 const EVP_CIPHER *aes, unsigned char *key)
384{
385 EVP_CIPHER_CTX *ctx;
386
387 ctx = EVP_CIPHER_CTX_new();
388 if (!ctx)
389 return 0;
390
391 if (!EVP_EncryptInit_ex(ctx, aes, NULL, key, NULL))
392 goto err;
393
394 *aes_ctx = ctx;
395 return 1;
396
397 err:
398 EVP_CIPHER_CTX_free(ctx);
399 return 0;
400}
401
402/* Encrypt <inlen> bytes from <in> buffer into <out> with <ctx> as AES
cui flitera94bedc2022-08-29 14:42:57 +0800403 * cipher context. This is the responsibility of the caller to check there
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200404 * is at least <inlen> bytes of available space in <out> buffer.
405 * Return 1 if succeeded, 0 if not.
406 */
407int quic_tls_aes_encrypt(unsigned char *out,
408 const unsigned char *in, size_t inlen,
409 EVP_CIPHER_CTX *ctx)
410{
411 int ret = 0;
412
413 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, in) ||
414 !EVP_EncryptUpdate(ctx, out, &ret, out, inlen) ||
415 !EVP_EncryptFinal_ex(ctx, out, &ret))
416 return 0;
417
418 return 1;
419}
420
421/* Initialize <*aes_ctx> AES cipher context with <key> as key for decryption */
422int quic_tls_dec_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx,
423 const EVP_CIPHER *aes, unsigned char *key)
424{
425 EVP_CIPHER_CTX *ctx;
426
427 ctx = EVP_CIPHER_CTX_new();
428 if (!ctx)
429 return 0;
430
431 if (!EVP_DecryptInit_ex(ctx, aes, NULL, key, NULL))
432 goto err;
433
434 *aes_ctx = ctx;
435 return 1;
436
437 err:
438 EVP_CIPHER_CTX_free(ctx);
439 return 0;
440}
441
442/* Decrypt <in> data into <out> with <ctx> as AES cipher context.
cui flitera94bedc2022-08-29 14:42:57 +0800443 * This is the responsibility of the caller to check there is at least
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200444 * <outlen> bytes into <in> buffer.
445 * Return 1 if succeeded, 0 if not.
446 */
447int quic_tls_aes_decrypt(unsigned char *out,
448 const unsigned char *in, size_t inlen,
449 EVP_CIPHER_CTX *ctx)
450{
451 int ret = 0;
452
453 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, in) ||
454 !EVP_DecryptUpdate(ctx, out, &ret, out, inlen) ||
455 !EVP_DecryptFinal_ex(ctx, out, &ret))
456 return 0;
457
458 return 1;
459}
460
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200461/* Initialize the cipher context for TX part of <tls_ctx> QUIC TLS context.
462 * Return 1 if succeeded, 0 if not.
463 */
464int quic_tls_tx_ctx_init(EVP_CIPHER_CTX **tx_ctx,
465 const EVP_CIPHER *aead, unsigned char *key)
466{
467 EVP_CIPHER_CTX *ctx;
468 int aead_nid = EVP_CIPHER_nid(aead);
469
470 ctx = EVP_CIPHER_CTX_new();
471 if (!ctx)
472 return 0;
473
474 if (!EVP_EncryptInit_ex(ctx, aead, NULL, NULL, NULL) ||
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +0200475 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, QUIC_TLS_IV_LEN, NULL) ||
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200476 (aead_nid == NID_aes_128_ccm &&
477 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) ||
478 !EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
479 goto err;
480
481 *tx_ctx = ctx;
482
483 return 1;
484
485 err:
486 EVP_CIPHER_CTX_free(ctx);
487 return 0;
488}
489
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100490/*
491 * https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#aead
492 *
493 * 5.3. AEAD Usage
494 *
495 * Packets are protected prior to applying header protection (Section 5.4).
496 * The unprotected packet header is part of the associated data (A). When removing
497 * packet protection, an endpoint first removes the header protection.
498 * (...)
499 * These ciphersuites have a 16-byte authentication tag and produce an output 16
500 * bytes larger than their input.
501 * The key and IV for the packet are computed as described in Section 5.1. The nonce,
502 * N, is formed by combining the packet protection IV with the packet number. The 62
503 * bits of the reconstructed QUIC packet number in network byte order are left-padded
504 * with zeros to the size of the IV. The exclusive OR of the padded packet number and
505 * the IV forms the AEAD nonce.
506 *
507 * The associated data, A, for the AEAD is the contents of the QUIC header, starting
508 * from the flags byte in either the short or long header, up to and including the
509 * unprotected packet number.
510 *
511 * The input plaintext, P, for the AEAD is the payload of the QUIC packet, as described
512 * in [QUIC-TRANSPORT].
513 *
514 * The output ciphertext, C, of the AEAD is transmitted in place of P.
515 *
516 * Some AEAD functions have limits for how many packets can be encrypted under the same
517 * key and IV (see for example [AEBounds]). This might be lower than the packet number limit.
518 * An endpoint MUST initiate a key update (Section 6) prior to exceeding any limit set for
519 * the AEAD that is in use.
520 */
521
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200522/* Encrypt in place <buf> plaintext with <len> as length with QUIC_TLS_TAG_LEN
523 * included tailing bytes for the tag.
524 * Note that for CCM mode, we must set the the ciphertext length if AAD data
525 * are provided from <aad> buffer with <aad_len> as length. This is always the
526 * case here. So the caller of this function must provide <aad>.
527 *
528 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
529 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530int quic_tls_encrypt(unsigned char *buf, size_t len,
531 const unsigned char *aad, size_t aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200532 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
533 const unsigned char *key, const unsigned char *iv)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534{
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200535 int outlen;
536 int aead_nid = EVP_CIPHER_nid(aead);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100537
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200538 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
539 (aead_nid == NID_aes_128_ccm &&
540 !EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, len)) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100541 !EVP_EncryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
542 !EVP_EncryptUpdate(ctx, buf, &outlen, buf, len) ||
543 !EVP_EncryptFinal_ex(ctx, buf + outlen, &outlen) ||
544 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, buf + len))
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200545 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100546
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200547 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100548}
549
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200550/* Decrypt in place <buf> ciphertext with <len> as length with QUIC_TLS_TAG_LEN
551 * included tailing bytes for the tag.
552 * Note that for CCM mode, we must set the the ciphertext length if AAD data
553 * are provided from <aad> buffer with <aad_len> as length. This is always the
554 * case here. So the caller of this function must provide <aad>. Also not the
555 * there is no need to call EVP_DecryptFinal_ex for CCM mode.
556 *
557 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
558 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100559int quic_tls_decrypt(unsigned char *buf, size_t len,
560 unsigned char *aad, size_t aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200561 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
562 const unsigned char *key, const unsigned char *iv)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100563{
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200564 int outlen;
565 int aead_nid = EVP_CIPHER_nid(aead);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100566
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200567 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
568 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100569 buf + len - QUIC_TLS_TAG_LEN) ||
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200570 (aead_nid == NID_aes_128_ccm &&
571 !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len - QUIC_TLS_TAG_LEN)) ||
572 !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
573 !EVP_DecryptUpdate(ctx, buf, &outlen, buf, len - QUIC_TLS_TAG_LEN) ||
574 (aead_nid != NID_aes_128_ccm &&
575 !EVP_DecryptFinal_ex(ctx, buf + outlen, &outlen)))
576 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100577
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200578 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100579}
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100580
Frédéric Lécaille55367c82022-05-16 10:27:57 +0200581/* Similar to quic_tls_decrypt(), except that this function does not decrypt
582 * in place its ciphertest if <out> output buffer ciphertest with <len> as length
583 * is different from <in> input buffer. This is the responbality of the caller
584 * to check that the output buffer has at least the same size as the input buffer.
585 * Note that for CCM mode, we must set the the ciphertext length if AAD data
586 * are provided from <aad> buffer with <aad_len> as length. This is always the
587 * case here. So the caller of this function must provide <aad>. Also note that
588 * there is no need to call EVP_DecryptFinal_ex for CCM mode.
589 *
590 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
591 *
592 * Return 1 if succeeded, 0 if not.
593 */
594int quic_tls_decrypt2(unsigned char *out,
595 unsigned char *in, size_t len,
596 unsigned char *aad, size_t aad_len,
597 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
598 const unsigned char *key, const unsigned char *iv)
599{
600 int outlen;
601 int aead_nid = EVP_CIPHER_nid(aead);
602
603 len -= QUIC_TLS_TAG_LEN;
604 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
605 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, in + len) ||
606 (aead_nid == NID_aes_128_ccm &&
607 !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len)) ||
608 !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
609 !EVP_DecryptUpdate(ctx, out, &outlen, in, len) ||
610 (aead_nid != NID_aes_128_ccm &&
611 !EVP_DecryptFinal_ex(ctx, out + outlen, &outlen)))
612 return 0;
613
614 return 1;
615}
616
Frédéric Lécaillea9c5d8d2022-05-12 14:44:51 +0200617/* Derive <key> and <iv> key and IV to be used to encrypt a retry token
618 * with <secret> which is not pseudo-random.
619 * Return 1 if succeeded, 0 if not.
620 */
621int quic_tls_derive_retry_token_secret(const EVP_MD *md,
622 unsigned char *key, size_t keylen,
623 unsigned char *iv, size_t ivlen,
624 const unsigned char *salt, size_t saltlen,
625 const unsigned char *secret, size_t secretlen)
626{
627 unsigned char tmpkey[QUIC_TLS_KEY_LEN];
628 const unsigned char tmpkey_label[] = "retry token";
629 const unsigned char key_label[] = "retry token key";
630 const unsigned char iv_label[] = "retry token iv";
631
632 if (!quic_hkdf_extract_and_expand(md, tmpkey, sizeof tmpkey,
633 secret, secretlen, salt, saltlen,
634 tmpkey_label, sizeof tmpkey_label - 1) ||
635 !quic_hkdf_expand(md, key, keylen, tmpkey, sizeof tmpkey,
636 key_label, sizeof key_label - 1) ||
637 !quic_hkdf_expand(md, iv, ivlen, secret, secretlen,
638 iv_label, sizeof iv_label - 1))
639 return 0;
640
641 return 1;
642}
643
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100644/* Generate the AEAD tag for the Retry packet <pkt> of <pkt_len> bytes and
645 * write it to <tag>. The tag is written just after the <pkt> area. It should
646 * be at least 16 bytes longs. <odcid> is the CID of the Initial packet
647 * received which triggers the Retry.
648 *
649 * Returns non-zero on success else zero.
650 */
Frédéric Lécaille3f96a0a2022-06-08 08:26:03 +0200651int quic_tls_generate_retry_integrity_tag(unsigned char *odcid, unsigned char odcid_len,
652 unsigned char *pkt, size_t pkt_len,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200653 const struct quic_version *qv)
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100654{
655 const EVP_CIPHER *evp = EVP_aes_128_gcm();
656 EVP_CIPHER_CTX *ctx;
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100657
658 /* encryption buffer - not used as only AEAD tag generation is proceed */
659 unsigned char *out = NULL;
660 /* address to store the AEAD tag */
661 unsigned char *tag = pkt + pkt_len;
662 int outlen, ret = 0;
663
664 ctx = EVP_CIPHER_CTX_new();
665 if (!ctx)
666 return 0;
667
668 /* rfc9001 5.8. Retry Packet Integrity
669 *
670 * AEAD is proceed over a pseudo-Retry packet used as AAD. It contains
671 * the ODCID len + data and the Retry packet itself.
672 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200673 if (!EVP_EncryptInit_ex(ctx, evp, NULL, qv->retry_tag_key, qv->retry_tag_nonce) ||
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100674 /* specify pseudo-Retry as AAD */
675 !EVP_EncryptUpdate(ctx, NULL, &outlen, &odcid_len, sizeof(odcid_len)) ||
676 !EVP_EncryptUpdate(ctx, NULL, &outlen, odcid, odcid_len) ||
677 !EVP_EncryptUpdate(ctx, NULL, &outlen, pkt, pkt_len) ||
678 /* finalize */
679 !EVP_EncryptFinal_ex(ctx, out, &outlen) ||
680 /* store the tag */
681 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, tag)) {
682 goto out;
683 }
684 ret = 1;
685
686 out:
687 EVP_CIPHER_CTX_free(ctx);
688 return ret;
689}