blob: 992fa8b8652372c4287f27b99abb752a43294f26 [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
344/* Initialize the cipher context for TX part of <tls_ctx> QUIC TLS context.
345 * Return 1 if succeeded, 0 if not.
346 */
347int quic_tls_tx_ctx_init(EVP_CIPHER_CTX **tx_ctx,
348 const EVP_CIPHER *aead, unsigned char *key)
349{
350 EVP_CIPHER_CTX *ctx;
351 int aead_nid = EVP_CIPHER_nid(aead);
352
353 ctx = EVP_CIPHER_CTX_new();
354 if (!ctx)
355 return 0;
356
357 if (!EVP_EncryptInit_ex(ctx, aead, NULL, NULL, NULL) ||
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +0200358 !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 +0200359 (aead_nid == NID_aes_128_ccm &&
360 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) ||
361 !EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
362 goto err;
363
364 *tx_ctx = ctx;
365
366 return 1;
367
368 err:
369 EVP_CIPHER_CTX_free(ctx);
370 return 0;
371}
372
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100373/*
374 * https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#aead
375 *
376 * 5.3. AEAD Usage
377 *
378 * Packets are protected prior to applying header protection (Section 5.4).
379 * The unprotected packet header is part of the associated data (A). When removing
380 * packet protection, an endpoint first removes the header protection.
381 * (...)
382 * These ciphersuites have a 16-byte authentication tag and produce an output 16
383 * bytes larger than their input.
384 * The key and IV for the packet are computed as described in Section 5.1. The nonce,
385 * N, is formed by combining the packet protection IV with the packet number. The 62
386 * bits of the reconstructed QUIC packet number in network byte order are left-padded
387 * with zeros to the size of the IV. The exclusive OR of the padded packet number and
388 * the IV forms the AEAD nonce.
389 *
390 * The associated data, A, for the AEAD is the contents of the QUIC header, starting
391 * from the flags byte in either the short or long header, up to and including the
392 * unprotected packet number.
393 *
394 * The input plaintext, P, for the AEAD is the payload of the QUIC packet, as described
395 * in [QUIC-TRANSPORT].
396 *
397 * The output ciphertext, C, of the AEAD is transmitted in place of P.
398 *
399 * Some AEAD functions have limits for how many packets can be encrypted under the same
400 * key and IV (see for example [AEBounds]). This might be lower than the packet number limit.
401 * An endpoint MUST initiate a key update (Section 6) prior to exceeding any limit set for
402 * the AEAD that is in use.
403 */
404
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200405/* Encrypt in place <buf> plaintext with <len> as length with QUIC_TLS_TAG_LEN
406 * included tailing bytes for the tag.
407 * Note that for CCM mode, we must set the the ciphertext length if AAD data
408 * are provided from <aad> buffer with <aad_len> as length. This is always the
409 * case here. So the caller of this function must provide <aad>.
410 *
411 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
412 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413int quic_tls_encrypt(unsigned char *buf, size_t len,
414 const unsigned char *aad, size_t aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200415 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
416 const unsigned char *key, const unsigned char *iv)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417{
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200418 int outlen;
419 int aead_nid = EVP_CIPHER_nid(aead);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100420
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200421 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
422 (aead_nid == NID_aes_128_ccm &&
423 !EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, len)) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100424 !EVP_EncryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
425 !EVP_EncryptUpdate(ctx, buf, &outlen, buf, len) ||
426 !EVP_EncryptFinal_ex(ctx, buf + outlen, &outlen) ||
427 !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 +0200428 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100429
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200430 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100431}
432
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200433/* Decrypt in place <buf> ciphertext with <len> as length with QUIC_TLS_TAG_LEN
434 * included tailing bytes for the tag.
435 * Note that for CCM mode, we must set the the ciphertext length if AAD data
436 * are provided from <aad> buffer with <aad_len> as length. This is always the
437 * case here. So the caller of this function must provide <aad>. Also not the
438 * there is no need to call EVP_DecryptFinal_ex for CCM mode.
439 *
440 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
441 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100442int quic_tls_decrypt(unsigned char *buf, size_t len,
443 unsigned char *aad, size_t aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200444 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
445 const unsigned char *key, const unsigned char *iv)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100446{
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200447 int outlen;
448 int aead_nid = EVP_CIPHER_nid(aead);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100449
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200450 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
451 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100452 buf + len - QUIC_TLS_TAG_LEN) ||
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200453 (aead_nid == NID_aes_128_ccm &&
454 !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len - QUIC_TLS_TAG_LEN)) ||
455 !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
456 !EVP_DecryptUpdate(ctx, buf, &outlen, buf, len - QUIC_TLS_TAG_LEN) ||
457 (aead_nid != NID_aes_128_ccm &&
458 !EVP_DecryptFinal_ex(ctx, buf + outlen, &outlen)))
459 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100460
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200461 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100462}
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100463
Frédéric Lécaille55367c82022-05-16 10:27:57 +0200464/* Similar to quic_tls_decrypt(), except that this function does not decrypt
465 * in place its ciphertest if <out> output buffer ciphertest with <len> as length
466 * is different from <in> input buffer. This is the responbality of the caller
467 * to check that the output buffer has at least the same size as the input buffer.
468 * Note that for CCM mode, we must set the the ciphertext length if AAD data
469 * are provided from <aad> buffer with <aad_len> as length. This is always the
470 * case here. So the caller of this function must provide <aad>. Also note that
471 * there is no need to call EVP_DecryptFinal_ex for CCM mode.
472 *
473 * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
474 *
475 * Return 1 if succeeded, 0 if not.
476 */
477int quic_tls_decrypt2(unsigned char *out,
478 unsigned char *in, size_t len,
479 unsigned char *aad, size_t aad_len,
480 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead,
481 const unsigned char *key, const unsigned char *iv)
482{
483 int outlen;
484 int aead_nid = EVP_CIPHER_nid(aead);
485
486 len -= QUIC_TLS_TAG_LEN;
487 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) ||
488 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, in + len) ||
489 (aead_nid == NID_aes_128_ccm &&
490 !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len)) ||
491 !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) ||
492 !EVP_DecryptUpdate(ctx, out, &outlen, in, len) ||
493 (aead_nid != NID_aes_128_ccm &&
494 !EVP_DecryptFinal_ex(ctx, out + outlen, &outlen)))
495 return 0;
496
497 return 1;
498}
499
Frédéric Lécaillea9c5d8d2022-05-12 14:44:51 +0200500/* Derive <key> and <iv> key and IV to be used to encrypt a retry token
501 * with <secret> which is not pseudo-random.
502 * Return 1 if succeeded, 0 if not.
503 */
504int quic_tls_derive_retry_token_secret(const EVP_MD *md,
505 unsigned char *key, size_t keylen,
506 unsigned char *iv, size_t ivlen,
507 const unsigned char *salt, size_t saltlen,
508 const unsigned char *secret, size_t secretlen)
509{
510 unsigned char tmpkey[QUIC_TLS_KEY_LEN];
511 const unsigned char tmpkey_label[] = "retry token";
512 const unsigned char key_label[] = "retry token key";
513 const unsigned char iv_label[] = "retry token iv";
514
515 if (!quic_hkdf_extract_and_expand(md, tmpkey, sizeof tmpkey,
516 secret, secretlen, salt, saltlen,
517 tmpkey_label, sizeof tmpkey_label - 1) ||
518 !quic_hkdf_expand(md, key, keylen, tmpkey, sizeof tmpkey,
519 key_label, sizeof key_label - 1) ||
520 !quic_hkdf_expand(md, iv, ivlen, secret, secretlen,
521 iv_label, sizeof iv_label - 1))
522 return 0;
523
524 return 1;
525}
526
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100527/* Generate the AEAD tag for the Retry packet <pkt> of <pkt_len> bytes and
528 * write it to <tag>. The tag is written just after the <pkt> area. It should
529 * be at least 16 bytes longs. <odcid> is the CID of the Initial packet
530 * received which triggers the Retry.
531 *
532 * Returns non-zero on success else zero.
533 */
Frédéric Lécaille3f96a0a2022-06-08 08:26:03 +0200534int quic_tls_generate_retry_integrity_tag(unsigned char *odcid, unsigned char odcid_len,
535 unsigned char *pkt, size_t pkt_len,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200536 const struct quic_version *qv)
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100537{
538 const EVP_CIPHER *evp = EVP_aes_128_gcm();
539 EVP_CIPHER_CTX *ctx;
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100540
541 /* encryption buffer - not used as only AEAD tag generation is proceed */
542 unsigned char *out = NULL;
543 /* address to store the AEAD tag */
544 unsigned char *tag = pkt + pkt_len;
545 int outlen, ret = 0;
546
547 ctx = EVP_CIPHER_CTX_new();
548 if (!ctx)
549 return 0;
550
551 /* rfc9001 5.8. Retry Packet Integrity
552 *
553 * AEAD is proceed over a pseudo-Retry packet used as AAD. It contains
554 * the ODCID len + data and the Retry packet itself.
555 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200556 if (!EVP_EncryptInit_ex(ctx, evp, NULL, qv->retry_tag_key, qv->retry_tag_nonce) ||
Amaury Denoyelle6efec292022-01-11 11:57:00 +0100557 /* specify pseudo-Retry as AAD */
558 !EVP_EncryptUpdate(ctx, NULL, &outlen, &odcid_len, sizeof(odcid_len)) ||
559 !EVP_EncryptUpdate(ctx, NULL, &outlen, odcid, odcid_len) ||
560 !EVP_EncryptUpdate(ctx, NULL, &outlen, pkt, pkt_len) ||
561 /* finalize */
562 !EVP_EncryptFinal_ex(ctx, out, &outlen) ||
563 /* store the tag */
564 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, tag)) {
565 goto out;
566 }
567 ret = 1;
568
569 out:
570 EVP_CIPHER_CTX_free(ctx);
571 return ret;
572}