blob: 3114aa623cdd10b593173aebfd41d576e9918436 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001#include <string.h>
2
3#include <openssl/ssl.h>
4
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005#include <openssl/evp.h>
6#include <openssl/kdf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007
8#include <haproxy/buf.h>
9#include <haproxy/chunk.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010010#include <haproxy/xprt_quic.h>
11
12
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +010013DECLARE_POOL(pool_head_quic_tls_secret, "quic_tls_secret", QUIC_TLS_SECRET_LEN);
14DECLARE_POOL(pool_head_quic_tls_iv, "quic_tls_iv", QUIC_TLS_IV_LEN);
15DECLARE_POOL(pool_head_quic_tls_key, "quic_tls_key", QUIC_TLS_KEY_LEN);
16
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010017__attribute__((format (printf, 3, 4)))
18void hexdump(const void *buf, size_t buflen, const char *title_fmt, ...);
19
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010020/* Dump the RX/TX secrets of <secs> QUIC TLS secrets. */
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +010021void quic_tls_keys_hexdump(struct buffer *buf,
22 const struct quic_tls_secrets *secs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010023{
24 int i;
25 size_t aead_keylen = (size_t)EVP_CIPHER_key_length(secs->aead);
26 size_t aead_ivlen = (size_t)EVP_CIPHER_iv_length(secs->aead);
27 size_t hp_len = (size_t)EVP_CIPHER_key_length(secs->hp);
28
29 chunk_appendf(buf, "\n key=");
30 for (i = 0; i < aead_keylen; i++)
31 chunk_appendf(buf, "%02x", secs->key[i]);
32 chunk_appendf(buf, "\n iv=");
33 for (i = 0; i < aead_ivlen; i++)
34 chunk_appendf(buf, "%02x", secs->iv[i]);
35 chunk_appendf(buf, "\n hp=");
36 for (i = 0; i < hp_len; i++)
37 chunk_appendf(buf, "%02x", secs->hp_key[i]);
38}
39
40/* Dump <secret> TLS secret. */
41void quic_tls_secret_hexdump(struct buffer *buf,
42 const unsigned char *secret, size_t secret_len)
43{
44 int i;
45
46 chunk_appendf(buf, " secret=");
47 for (i = 0; i < secret_len; i++)
48 chunk_appendf(buf, "%02x", secret[i]);
49}
50
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010051int quic_hkdf_extract(const EVP_MD *md,
Frédéric Lécaille4ba3b4e2022-05-10 18:40:19 +020052 unsigned char *buf, size_t buflen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053 const unsigned char *key, size_t keylen,
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +020054 const unsigned char *salt, size_t saltlen)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010055{
56 EVP_PKEY_CTX *ctx;
57
58 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
59 if (!ctx)
60 return 0;
61
62 if (EVP_PKEY_derive_init(ctx) <= 0 ||
63 EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) <= 0 ||
64 EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 ||
65 EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, saltlen) <= 0 ||
66 EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 ||
Frédéric Lécaille4ba3b4e2022-05-10 18:40:19 +020067 EVP_PKEY_derive(ctx, buf, &buflen) <= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010068 goto err;
69
70 EVP_PKEY_CTX_free(ctx);
71 return 1;
72
73 err:
74 EVP_PKEY_CTX_free(ctx);
75 return 0;
76}
77
78int quic_hkdf_expand(const EVP_MD *md,
79 unsigned char *buf, size_t buflen,
80 const unsigned char *key, size_t keylen,
81 const unsigned char *label, size_t labellen)
82{
83 EVP_PKEY_CTX *ctx;
84
85 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
86 if (!ctx)
87 return 0;
88
89 if (EVP_PKEY_derive_init(ctx) <= 0 ||
90 EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) <= 0 ||
91 EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 ||
92 EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 ||
93 EVP_PKEY_CTX_add1_hkdf_info(ctx, label, labellen) <= 0 ||
94 EVP_PKEY_derive(ctx, buf, &buflen) <= 0)
95 goto err;
96
97 EVP_PKEY_CTX_free(ctx);
98 return 1;
99
100 err:
101 EVP_PKEY_CTX_free(ctx);
102 return 0;
103}
Frédéric Lécaille7b92c812022-05-06 09:54:48 +0200104
105/* Extracts a peudo-random secret key from <key> which is eventually not
106 * pseudo-random and expand it to a new pseudo-random key into
107 * <buf> with <buflen> as key length according to HKDF specifications
108 * (https://datatracker.ietf.org/doc/html/rfc5869).
109 * According to this specifications it is highly recommended to use
110 * a salt, even if optional (NULL value).
111 * Return 1 if succeeded, 0 if not.
112 */
113int quic_hkdf_extract_and_expand(const EVP_MD *md,
114 unsigned char *buf, size_t buflen,
115 const unsigned char *key, size_t keylen,
116 const unsigned char *salt, size_t saltlen,
117 const unsigned char *label, size_t labellen)
118{
119 EVP_PKEY_CTX *ctx;
120
121 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
122 if (!ctx)
123 return 0;
124
125 if (EVP_PKEY_derive_init(ctx) <= 0 ||
126 EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) <= 0 ||
127 EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 ||
128 EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, saltlen) <= 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}
141
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142/* https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#protection-keys
143 * refers to:
144 *
145 * https://tools.ietf.org/html/rfc8446#section-7.1:
146 * 7.1. Key Schedule
147 *
148 * The key derivation process makes use of the HKDF-Extract and
149 * HKDF-Expand functions as defined for HKDF [RFC5869], as well as the
150 * functions defined below:
151 *
152 * HKDF-Expand-Label(Secret, Label, Context, Length) =
153 * HKDF-Expand(Secret, HkdfLabel, Length)
154 *
155 * Where HkdfLabel is specified as:
156 *
157 * struct {
158 * uint16 length = Length;
159 * opaque label<7..255> = "tls13 " + Label;
160 * opaque context<0..255> = Context;
161 * } HkdfLabel;
162 *
163 * Derive-Secret(Secret, Label, Messages) =
164 * HKDF-Expand-Label(Secret, Label,
165 * Transcript-Hash(Messages), Hash.length)
166 *
167 */
168int quic_hkdf_expand_label(const EVP_MD *md,
169 unsigned char *buf, size_t buflen,
170 const unsigned char *key, size_t keylen,
171 const unsigned char *label, size_t labellen)
172{
173 unsigned char hdkf_label[256], *pos;
174 const unsigned char hdkf_label_label[] = "tls13 ";
175 size_t hdkf_label_label_sz = sizeof hdkf_label_label - 1;
176
177 pos = hdkf_label;
178 *pos++ = buflen >> 8;
179 *pos++ = buflen & 0xff;
180 *pos++ = hdkf_label_label_sz + labellen;
181 memcpy(pos, hdkf_label_label, hdkf_label_label_sz);
182 pos += hdkf_label_label_sz;
183 memcpy(pos, label, labellen);
184 pos += labellen;
185 *pos++ = '\0';
186
187 return quic_hkdf_expand(md, buf, buflen,
188 key, keylen, hdkf_label, pos - hdkf_label);
189}
190
191/*
192 * This function derives two keys from <secret> is <ctx> as TLS cryptographic context.
193 * ->key is the TLS key to be derived to encrypt/decrypt data at TLS level.
194 * ->iv is the initialization vector to be used with ->key.
195 * ->hp_key is the key to be derived for header protection.
196 * Obviouly these keys have the same size becaused derived with the same TLS cryptographic context.
197 */
198int quic_tls_derive_keys(const EVP_CIPHER *aead, const EVP_CIPHER *hp,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200199 const EVP_MD *md, const struct quic_version *qv,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100200 unsigned char *key, size_t keylen,
201 unsigned char *iv, size_t ivlen,
202 unsigned char *hp_key, size_t hp_keylen,
203 const unsigned char *secret, size_t secretlen)
204{
205 size_t aead_keylen = (size_t)EVP_CIPHER_key_length(aead);
206 size_t aead_ivlen = (size_t)EVP_CIPHER_iv_length(aead);
Frédéric Lécaille6e351d62021-11-30 11:06:41 +0100207 size_t hp_len = hp ? (size_t)EVP_CIPHER_key_length(hp) : 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208
209 if (aead_keylen > keylen || aead_ivlen > ivlen || hp_len > hp_keylen)
210 return 0;
211
212 if (!quic_hkdf_expand_label(md, key, aead_keylen, secret, secretlen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200213 qv->key_label,qv->key_label_len) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 !quic_hkdf_expand_label(md, iv, aead_ivlen, secret, secretlen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200215 qv->iv_label, qv->iv_label_len) ||
Frédéric Lécaille6e351d62021-11-30 11:06:41 +0100216 (hp_key && !quic_hkdf_expand_label(md, hp_key, hp_len, secret, secretlen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200217 qv->hp_label, qv->hp_label_len)))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218 return 0;
219
220 return 1;
221}
222
223/*
224 * Derive the initial secret from <secret> and QUIC version dependent salt.
225 * Returns the size of the derived secret if succeeded, 0 if not.
226 */
227int quic_derive_initial_secret(const EVP_MD *md,
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +0200228 const unsigned char *initial_salt, size_t initial_salt_sz,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229 unsigned char *initial_secret, size_t initial_secret_sz,
230 const unsigned char *secret, size_t secret_sz)
231{
Frédéric Lécaille4ba3b4e2022-05-10 18:40:19 +0200232 if (!quic_hkdf_extract(md, initial_secret, initial_secret_sz, secret, secret_sz,
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +0200233 initial_salt, initial_salt_sz))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100234 return 0;
235
236 return 1;
237}
238
239/*
240 * Derive the client initial secret from the initial secret.
241 * Returns the size of the derived secret if succeeded, 0 if not.
242 */
243int quic_tls_derive_initial_secrets(const EVP_MD *md,
244 unsigned char *rx, size_t rx_sz,
245 unsigned char *tx, size_t tx_sz,
246 const unsigned char *secret, size_t secret_sz,
247 int server)
248{
249 const unsigned char client_label[] = "client in";
250 const unsigned char server_label[] = "server in";
251 const unsigned char *tx_label, *rx_label;
252 size_t rx_label_sz, tx_label_sz;
253
254 if (server) {
255 rx_label = client_label;
256 rx_label_sz = sizeof client_label;
257 tx_label = server_label;
258 tx_label_sz = sizeof server_label;
259 }
260 else {
261 rx_label = server_label;
262 rx_label_sz = sizeof server_label;
263 tx_label = client_label;
264 tx_label_sz = sizeof client_label;
265 }
266
267 if (!quic_hkdf_expand_label(md, rx, rx_sz, secret, secret_sz,
268 rx_label, rx_label_sz - 1) ||
269 !quic_hkdf_expand_label(md, tx, tx_sz, secret, secret_sz,
270 tx_label, tx_label_sz - 1))
271 return 0;
272
273 return 1;
274}
275
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100276/* Update <sec> secret key into <new_sec> according to RFC 9001 6.1.
277 * Always succeeds.
278 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200279int quic_tls_sec_update(const EVP_MD *md, const struct quic_version *qv,
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100280 unsigned char *new_sec, size_t new_seclen,
281 const unsigned char *sec, size_t seclen)
282{
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100283 return quic_hkdf_expand_label(md, new_sec, new_seclen, sec, seclen,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200284 qv->ku_label, qv->ku_label_len);
Frédéric Lécaille39484de2021-11-30 10:10:24 +0100285}
286
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287/*
288 * Build an IV into <iv> buffer with <ivlen> as size from <aead_iv> with
289 * <aead_ivlen> as size depending on <pn> packet number.
290 * This is the function which must be called to build an AEAD IV for the AEAD cryptographic algorithm
291 * used to encrypt/decrypt the QUIC packet payloads depending on the packet number <pn>.
292 * This function fails and return 0 only if the two buffer lengths are different, 1 if not.
293 */
294int quic_aead_iv_build(unsigned char *iv, size_t ivlen,
295 unsigned char *aead_iv, size_t aead_ivlen, uint64_t pn)
296{
297 int i;
298 unsigned int shift;
299 unsigned char *pos = iv;
300
301 if (ivlen != aead_ivlen)
302 return 0;
303
304 for (i = 0; i < ivlen - sizeof pn; i++)
305 *pos++ = *aead_iv++;
306
307 /* Only the remaining (sizeof pn) bytes are XOR'ed. */
308 shift = 56;
309 for (i = aead_ivlen - sizeof pn; i < aead_ivlen ; i++, shift -= 8)
310 *pos++ = *aead_iv++ ^ (pn >> shift);
311
312 return 1;
313}
314
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200315/* Initialize the cipher context for RX part of <tls_ctx> QUIC TLS context.
316 * Return 1 if succeeded, 0 if not.
317 */
318int quic_tls_rx_ctx_init(EVP_CIPHER_CTX **rx_ctx,
319 const EVP_CIPHER *aead, unsigned char *key)
320{
321 EVP_CIPHER_CTX *ctx;
322 int aead_nid = EVP_CIPHER_nid(aead);
323
324 ctx = EVP_CIPHER_CTX_new();
325 if (!ctx)
326 return 0;
327
328 if (!EVP_DecryptInit_ex(ctx, aead, NULL, NULL, NULL) ||
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +0200329 !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 +0200330 (aead_nid == NID_aes_128_ccm &&
331 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) ||
332 !EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL))
333 goto err;
334
335 *rx_ctx = ctx;
336
337 return 1;
338
339 err:
340 EVP_CIPHER_CTX_free(ctx);
341 return 0;
342}
343
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200344/* Initialize <*aes_ctx> AES cipher context with <key> as key for encryption */
345int quic_tls_enc_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx,
346 const EVP_CIPHER *aes, unsigned char *key)
347{
348 EVP_CIPHER_CTX *ctx;
349
350 ctx = EVP_CIPHER_CTX_new();
351 if (!ctx)
352 return 0;
353
354 if (!EVP_EncryptInit_ex(ctx, aes, NULL, key, NULL))
355 goto err;
356
357 *aes_ctx = ctx;
358 return 1;
359
360 err:
361 EVP_CIPHER_CTX_free(ctx);
362 return 0;
363}
364
365/* Encrypt <inlen> bytes from <in> buffer into <out> with <ctx> as AES
366 * cipher context. This is the responsability of the caller to check there
367 * is at least <inlen> bytes of available space in <out> buffer.
368 * Return 1 if succeeded, 0 if not.
369 */
370int quic_tls_aes_encrypt(unsigned char *out,
371 const unsigned char *in, size_t inlen,
372 EVP_CIPHER_CTX *ctx)
373{
374 int ret = 0;
375
376 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, in) ||
377 !EVP_EncryptUpdate(ctx, out, &ret, out, inlen) ||
378 !EVP_EncryptFinal_ex(ctx, out, &ret))
379 return 0;
380
381 return 1;
382}
383
384/* Initialize <*aes_ctx> AES cipher context with <key> as key for decryption */
385int quic_tls_dec_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx,
386 const EVP_CIPHER *aes, unsigned char *key)
387{
388 EVP_CIPHER_CTX *ctx;
389
390 ctx = EVP_CIPHER_CTX_new();
391 if (!ctx)
392 return 0;
393
394 if (!EVP_DecryptInit_ex(ctx, aes, NULL, key, NULL))
395 goto err;
396
397 *aes_ctx = ctx;
398 return 1;
399
400 err:
401 EVP_CIPHER_CTX_free(ctx);
402 return 0;
403}
404
405/* Decrypt <in> data into <out> with <ctx> as AES cipher context.
406 * This is the responsability of the caller to check there is at least
407 * <outlen> bytes into <in> buffer.
408 * Return 1 if succeeded, 0 if not.
409 */
410int quic_tls_aes_decrypt(unsigned char *out,
411 const unsigned char *in, size_t inlen,
412 EVP_CIPHER_CTX *ctx)
413{
414 int ret = 0;
415
416 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, in) ||
417 !EVP_DecryptUpdate(ctx, out, &ret, out, inlen) ||
418 !EVP_DecryptFinal_ex(ctx, out, &ret))
419 return 0;
420
421 return 1;
422}
423
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200424/* Initialize the cipher context for TX part of <tls_ctx> QUIC TLS context.
425 * Return 1 if succeeded, 0 if not.
426 */
427int quic_tls_tx_ctx_init(EVP_CIPHER_CTX **tx_ctx,
428 const EVP_CIPHER *aead, unsigned char *key)
429{
430 EVP_CIPHER_CTX *ctx;
431 int aead_nid = EVP_CIPHER_nid(aead);
432
433 ctx = EVP_CIPHER_CTX_new();
434 if (!ctx)
435 return 0;
436
437 if (!EVP_EncryptInit_ex(ctx, aead, NULL, NULL, NULL) ||
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +0200438 !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 +0200439 (aead_nid == NID_aes_128_ccm &&
440 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) ||
441 !EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
442 goto err;
443
444 *tx_ctx = ctx;
445
446 return 1;
447
448 err:
449 EVP_CIPHER_CTX_free(ctx);
450 return 0;
451}
452
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100453/*
454 * https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#aead
455 *
456 * 5.3. AEAD Usage
457 *
458 * Packets are protected prior to applying header protection (Section 5.4).
459 * The unprotected packet header is part of the associated data (A). When removing
460 * packet protection, an endpoint first removes the header protection.
461 * (...)
462 * These ciphersuites have a 16-byte authentication tag and produce an output 16
463 * bytes larger than their input.
464 * The key and IV for the packet are computed as described in Section 5.1. The nonce,
465 * N, is formed by combining the packet protection IV with the packet number. The 62
466 * bits of the reconstructed QUIC packet number in network byte order are left-padded
467 * with zeros to the size of the IV. The exclusive OR of the padded packet number and
468 * the IV forms the AEAD nonce.
469 *
470 * The associated data, A, for the AEAD is the contents of the QUIC header, starting
471 * from the flags byte in either the short or long header, up to and including the
472 * unprotected packet number.
473 *
474 * The input plaintext, P, for the AEAD is the payload of the QUIC packet, as described
475 * in [QUIC-TRANSPORT].
476 *
477 * The output ciphertext, C, of the AEAD is transmitted in place of P.
478 *
479 * Some AEAD functions have limits for how many packets can be encrypted under the same
480 * key and IV (see for example [AEBounds]). This might be lower than the packet number limit.
481 * An endpoint MUST initiate a key update (Section 6) prior to exceeding any limit set for
482 * the AEAD that is in use.
483 */
484
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200485/* Encrypt in place <buf> plaintext with <len> as length with QUIC_TLS_TAG_LEN
486 * included tailing bytes for the tag.
487 * Note that for CCM mode, we must set the the ciphertext length if AAD data
488 * are provided from <aad> buffer with <aad_len> as length. This is always the
489 * case here. So the caller of this function must provide <aad>.
490 *
491 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
492 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493int quic_tls_encrypt(unsigned char *buf, size_t len,
494 const unsigned char *aad, size_t aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200495 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
496 const unsigned char *key, const unsigned char *iv)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497{
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200498 int outlen;
499 int aead_nid = EVP_CIPHER_nid(aead);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200501 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
502 (aead_nid == NID_aes_128_ccm &&
503 !EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, len)) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100504 !EVP_EncryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
505 !EVP_EncryptUpdate(ctx, buf, &outlen, buf, len) ||
506 !EVP_EncryptFinal_ex(ctx, buf + outlen, &outlen) ||
507 !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 +0200508 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200510 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100511}
512
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200513/* Decrypt in place <buf> ciphertext with <len> as length with QUIC_TLS_TAG_LEN
514 * included tailing bytes for the tag.
515 * Note that for CCM mode, we must set the the ciphertext length if AAD data
516 * are provided from <aad> buffer with <aad_len> as length. This is always the
517 * case here. So the caller of this function must provide <aad>. Also not the
518 * there is no need to call EVP_DecryptFinal_ex for CCM mode.
519 *
520 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
521 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100522int quic_tls_decrypt(unsigned char *buf, size_t len,
523 unsigned char *aad, size_t aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200524 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
525 const unsigned char *key, const unsigned char *iv)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526{
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200527 int outlen;
528 int aead_nid = EVP_CIPHER_nid(aead);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100529
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200530 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
531 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 buf + len - QUIC_TLS_TAG_LEN) ||
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200533 (aead_nid == NID_aes_128_ccm &&
534 !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len - QUIC_TLS_TAG_LEN)) ||
535 !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
536 !EVP_DecryptUpdate(ctx, buf, &outlen, buf, len - QUIC_TLS_TAG_LEN) ||
537 (aead_nid != NID_aes_128_ccm &&
538 !EVP_DecryptFinal_ex(ctx, buf + outlen, &outlen)))
539 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100540
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200541 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100542}
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100543
Frédéric Lécaille55367c82022-05-16 10:27:57 +0200544/* Similar to quic_tls_decrypt(), except that this function does not decrypt
545 * in place its ciphertest if <out> output buffer ciphertest with <len> as length
546 * is different from <in> input buffer. This is the responbality of the caller
547 * to check that the output buffer has at least the same size as the input buffer.
548 * Note that for CCM mode, we must set the the ciphertext length if AAD data
549 * are provided from <aad> buffer with <aad_len> as length. This is always the
550 * case here. So the caller of this function must provide <aad>. Also note that
551 * there is no need to call EVP_DecryptFinal_ex for CCM mode.
552 *
553 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
554 *
555 * Return 1 if succeeded, 0 if not.
556 */
557int quic_tls_decrypt2(unsigned char *out,
558 unsigned char *in, size_t len,
559 unsigned char *aad, size_t aad_len,
560 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
561 const unsigned char *key, const unsigned char *iv)
562{
563 int outlen;
564 int aead_nid = EVP_CIPHER_nid(aead);
565
566 len -= QUIC_TLS_TAG_LEN;
567 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
568 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, in + len) ||
569 (aead_nid == NID_aes_128_ccm &&
570 !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len)) ||
571 !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
572 !EVP_DecryptUpdate(ctx, out, &outlen, in, len) ||
573 (aead_nid != NID_aes_128_ccm &&
574 !EVP_DecryptFinal_ex(ctx, out + outlen, &outlen)))
575 return 0;
576
577 return 1;
578}
579
Frédéric Lécaillea9c5d8d2022-05-12 14:44:51 +0200580/* Derive <key> and <iv> key and IV to be used to encrypt a retry token
581 * with <secret> which is not pseudo-random.
582 * Return 1 if succeeded, 0 if not.
583 */
584int quic_tls_derive_retry_token_secret(const EVP_MD *md,
585 unsigned char *key, size_t keylen,
586 unsigned char *iv, size_t ivlen,
587 const unsigned char *salt, size_t saltlen,
588 const unsigned char *secret, size_t secretlen)
589{
590 unsigned char tmpkey[QUIC_TLS_KEY_LEN];
591 const unsigned char tmpkey_label[] = "retry token";
592 const unsigned char key_label[] = "retry token key";
593 const unsigned char iv_label[] = "retry token iv";
594
595 if (!quic_hkdf_extract_and_expand(md, tmpkey, sizeof tmpkey,
596 secret, secretlen, salt, saltlen,
597 tmpkey_label, sizeof tmpkey_label - 1) ||
598 !quic_hkdf_expand(md, key, keylen, tmpkey, sizeof tmpkey,
599 key_label, sizeof key_label - 1) ||
600 !quic_hkdf_expand(md, iv, ivlen, secret, secretlen,
601 iv_label, sizeof iv_label - 1))
602 return 0;
603
604 return 1;
605}
606
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100607/* Generate the AEAD tag for the Retry packet <pkt> of <pkt_len> bytes and
608 * write it to <tag>. The tag is written just after the <pkt> area. It should
609 * be at least 16 bytes longs. <odcid> is the CID of the Initial packet
610 * received which triggers the Retry.
611 *
612 * Returns non-zero on success else zero.
613 */
Frédéric Lécaille3f96a0a2022-06-08 08:26:03 +0200614int quic_tls_generate_retry_integrity_tag(unsigned char *odcid, unsigned char odcid_len,
615 unsigned char *pkt, size_t pkt_len,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200616 const struct quic_version *qv)
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100617{
618 const EVP_CIPHER *evp = EVP_aes_128_gcm();
619 EVP_CIPHER_CTX *ctx;
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100620
621 /* encryption buffer - not used as only AEAD tag generation is proceed */
622 unsigned char *out = NULL;
623 /* address to store the AEAD tag */
624 unsigned char *tag = pkt + pkt_len;
625 int outlen, ret = 0;
626
627 ctx = EVP_CIPHER_CTX_new();
628 if (!ctx)
629 return 0;
630
631 /* rfc9001 5.8. Retry Packet Integrity
632 *
633 * AEAD is proceed over a pseudo-Retry packet used as AAD. It contains
634 * the ODCID len + data and the Retry packet itself.
635 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200636 if (!EVP_EncryptInit_ex(ctx, evp, NULL, qv->retry_tag_key, qv->retry_tag_nonce) ||
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100637 /* specify pseudo-Retry as AAD */
638 !EVP_EncryptUpdate(ctx, NULL, &outlen, &odcid_len, sizeof(odcid_len)) ||
639 !EVP_EncryptUpdate(ctx, NULL, &outlen, odcid, odcid_len) ||
640 !EVP_EncryptUpdate(ctx, NULL, &outlen, pkt, pkt_len) ||
641 /* finalize */
642 !EVP_EncryptFinal_ex(ctx, out, &outlen) ||
643 /* store the tag */
644 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, tag)) {
645 goto out;
646 }
647 ret = 1;
648
649 out:
650 EVP_CIPHER_CTX_free(ctx);
651 return ret;
652}