Amaury Denoyelle | f3c40f8 | 2022-09-30 17:37:38 +0200 | [diff] [blame] | 1 | #include <haproxy/quic_tls.h> |
| 2 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3 | #include <string.h> |
| 4 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 5 | #include <openssl/evp.h> |
| 6 | #include <openssl/kdf.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 7 | #include <openssl/ssl.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 8 | |
| 9 | #include <haproxy/buf.h> |
| 10 | #include <haproxy/chunk.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 11 | #include <haproxy/pool.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 12 | #include <haproxy/quic_conn-t.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 13 | |
| 14 | |
Frédéric Lécaille | fc768ec | 2021-11-23 21:02:04 +0100 | [diff] [blame] | 15 | DECLARE_POOL(pool_head_quic_tls_secret, "quic_tls_secret", QUIC_TLS_SECRET_LEN); |
| 16 | DECLARE_POOL(pool_head_quic_tls_iv, "quic_tls_iv", QUIC_TLS_IV_LEN); |
| 17 | DECLARE_POOL(pool_head_quic_tls_key, "quic_tls_key", QUIC_TLS_KEY_LEN); |
| 18 | |
Amaury Denoyelle | a19bb6f | 2022-09-30 17:31:18 +0200 | [diff] [blame] | 19 | /* Initial salt depending on QUIC version to derive client/server initial secrets. |
| 20 | * This one is for draft-29 QUIC version. |
| 21 | */ |
| 22 | const 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 | |
| 28 | const 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écaille | 21c4c9b | 2023-01-13 16:37:02 +0100 | [diff] [blame] | 34 | const 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 Denoyelle | a19bb6f | 2022-09-30 17:31:18 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | /* Dump the RX/TX secrets of <secs> QUIC TLS secrets. */ |
Amaury Denoyelle | 4fd53d7 | 2021-12-21 14:28:26 +0100 | [diff] [blame] | 41 | void quic_tls_keys_hexdump(struct buffer *buf, |
| 42 | const struct quic_tls_secrets *secs) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 43 | { |
| 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écaille | 51a7caf | 2023-02-23 20:38:23 +0100 | [diff] [blame] | 60 | /* Dump the RX/TX secrets of <kp> QUIC TLS key phase */ |
| 61 | void 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écaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 77 | /* Dump <secret> TLS secret. */ |
| 78 | void 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écaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 88 | int quic_hkdf_extract(const EVP_MD *md, |
Frédéric Lécaille | 4ba3b4e | 2022-05-10 18:40:19 +0200 | [diff] [blame] | 89 | unsigned char *buf, size_t buflen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 90 | const unsigned char *key, size_t keylen, |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 91 | const unsigned char *salt, size_t saltlen) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 92 | { |
| 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écaille | 4ba3b4e | 2022-05-10 18:40:19 +0200 | [diff] [blame] | 104 | EVP_PKEY_derive(ctx, buf, &buflen) <= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 105 | 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 | |
| 115 | int 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écaille | 7b92c81 | 2022-05-06 09:54:48 +0200 | [diff] [blame] | 141 | |
| 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 | */ |
| 150 | int 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écaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 179 | /* 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 | */ |
| 205 | int 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 | */ |
| 235 | int quic_tls_derive_keys(const EVP_CIPHER *aead, const EVP_CIPHER *hp, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 236 | const EVP_MD *md, const struct quic_version *qv, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 237 | 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écaille | 6e351d6 | 2021-11-30 11:06:41 +0100 | [diff] [blame] | 244 | size_t hp_len = hp ? (size_t)EVP_CIPHER_key_length(hp) : 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 245 | |
| 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écaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 250 | qv->key_label,qv->key_label_len) || |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 251 | !quic_hkdf_expand_label(md, iv, aead_ivlen, secret, secretlen, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 252 | qv->iv_label, qv->iv_label_len) || |
Frédéric Lécaille | 6e351d6 | 2021-11-30 11:06:41 +0100 | [diff] [blame] | 253 | (hp_key && !quic_hkdf_expand_label(md, hp_key, hp_len, secret, secretlen, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 254 | qv->hp_label, qv->hp_label_len))) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 255 | 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 | */ |
| 264 | int quic_derive_initial_secret(const EVP_MD *md, |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 265 | const unsigned char *initial_salt, size_t initial_salt_sz, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 266 | unsigned char *initial_secret, size_t initial_secret_sz, |
| 267 | const unsigned char *secret, size_t secret_sz) |
| 268 | { |
Frédéric Lécaille | 4ba3b4e | 2022-05-10 18:40:19 +0200 | [diff] [blame] | 269 | if (!quic_hkdf_extract(md, initial_secret, initial_secret_sz, secret, secret_sz, |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 270 | initial_salt, initial_salt_sz)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 271 | 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 | */ |
| 280 | int 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écaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 313 | /* Update <sec> secret key into <new_sec> according to RFC 9001 6.1. |
| 314 | * Always succeeds. |
| 315 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 316 | int quic_tls_sec_update(const EVP_MD *md, const struct quic_version *qv, |
Frédéric Lécaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 317 | unsigned char *new_sec, size_t new_seclen, |
| 318 | const unsigned char *sec, size_t seclen) |
| 319 | { |
Frédéric Lécaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 320 | return quic_hkdf_expand_label(md, new_sec, new_seclen, sec, seclen, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 321 | qv->ku_label, qv->ku_label_len); |
Frédéric Lécaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 324 | /* |
| 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>. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 329 | */ |
Amaury Denoyelle | 5eadc27 | 2023-05-16 18:11:01 +0200 | [diff] [blame] | 330 | void quic_aead_iv_build(unsigned char *iv, size_t ivlen, |
| 331 | unsigned char *aead_iv, size_t aead_ivlen, uint64_t pn) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 332 | { |
| 333 | int i; |
| 334 | unsigned int shift; |
| 335 | unsigned char *pos = iv; |
| 336 | |
Amaury Denoyelle | 5eadc27 | 2023-05-16 18:11:01 +0200 | [diff] [blame] | 337 | /* Input buffers must have the same size. */ |
| 338 | BUG_ON(ivlen != aead_ivlen); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 339 | |
| 340 | for (i = 0; i < ivlen - sizeof pn; i++) |
| 341 | *pos++ = *aead_iv++; |
| 342 | |
| 343 | /* Only the remaining (sizeof pn) bytes are XOR'ed. */ |
| 344 | shift = 56; |
| 345 | for (i = aead_ivlen - sizeof pn; i < aead_ivlen ; i++, shift -= 8) |
| 346 | *pos++ = *aead_iv++ ^ (pn >> shift); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 349 | /* Initialize the cipher context for RX part of <tls_ctx> QUIC TLS context. |
| 350 | * Return 1 if succeeded, 0 if not. |
| 351 | */ |
| 352 | int quic_tls_rx_ctx_init(EVP_CIPHER_CTX **rx_ctx, |
| 353 | const EVP_CIPHER *aead, unsigned char *key) |
| 354 | { |
| 355 | EVP_CIPHER_CTX *ctx; |
| 356 | int aead_nid = EVP_CIPHER_nid(aead); |
| 357 | |
| 358 | ctx = EVP_CIPHER_CTX_new(); |
| 359 | if (!ctx) |
| 360 | return 0; |
| 361 | |
| 362 | if (!EVP_DecryptInit_ex(ctx, aead, NULL, NULL, NULL) || |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 363 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, QUIC_TLS_IV_LEN, NULL) || |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 364 | (aead_nid == NID_aes_128_ccm && |
| 365 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) || |
| 366 | !EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL)) |
| 367 | goto err; |
| 368 | |
| 369 | *rx_ctx = ctx; |
| 370 | |
| 371 | return 1; |
| 372 | |
| 373 | err: |
| 374 | EVP_CIPHER_CTX_free(ctx); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 378 | /* Initialize <*aes_ctx> AES cipher context with <key> as key for encryption */ |
| 379 | int quic_tls_enc_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx, |
| 380 | const EVP_CIPHER *aes, unsigned char *key) |
| 381 | { |
| 382 | EVP_CIPHER_CTX *ctx; |
| 383 | |
| 384 | ctx = EVP_CIPHER_CTX_new(); |
| 385 | if (!ctx) |
| 386 | return 0; |
| 387 | |
| 388 | if (!EVP_EncryptInit_ex(ctx, aes, NULL, key, NULL)) |
| 389 | goto err; |
| 390 | |
| 391 | *aes_ctx = ctx; |
| 392 | return 1; |
| 393 | |
| 394 | err: |
| 395 | EVP_CIPHER_CTX_free(ctx); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* Encrypt <inlen> bytes from <in> buffer into <out> with <ctx> as AES |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 400 | * cipher context. This is the responsibility of the caller to check there |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 401 | * is at least <inlen> bytes of available space in <out> buffer. |
| 402 | * Return 1 if succeeded, 0 if not. |
| 403 | */ |
| 404 | int quic_tls_aes_encrypt(unsigned char *out, |
| 405 | const unsigned char *in, size_t inlen, |
| 406 | EVP_CIPHER_CTX *ctx) |
| 407 | { |
| 408 | int ret = 0; |
| 409 | |
| 410 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, in) || |
| 411 | !EVP_EncryptUpdate(ctx, out, &ret, out, inlen) || |
| 412 | !EVP_EncryptFinal_ex(ctx, out, &ret)) |
| 413 | return 0; |
| 414 | |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | /* Initialize <*aes_ctx> AES cipher context with <key> as key for decryption */ |
| 419 | int quic_tls_dec_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx, |
| 420 | const EVP_CIPHER *aes, unsigned char *key) |
| 421 | { |
| 422 | EVP_CIPHER_CTX *ctx; |
| 423 | |
| 424 | ctx = EVP_CIPHER_CTX_new(); |
| 425 | if (!ctx) |
| 426 | return 0; |
| 427 | |
| 428 | if (!EVP_DecryptInit_ex(ctx, aes, NULL, key, NULL)) |
| 429 | goto err; |
| 430 | |
| 431 | *aes_ctx = ctx; |
| 432 | return 1; |
| 433 | |
| 434 | err: |
| 435 | EVP_CIPHER_CTX_free(ctx); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /* Decrypt <in> data into <out> with <ctx> as AES cipher context. |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 440 | * This is the responsibility of the caller to check there is at least |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 441 | * <outlen> bytes into <in> buffer. |
| 442 | * Return 1 if succeeded, 0 if not. |
| 443 | */ |
| 444 | int quic_tls_aes_decrypt(unsigned char *out, |
| 445 | const unsigned char *in, size_t inlen, |
| 446 | EVP_CIPHER_CTX *ctx) |
| 447 | { |
| 448 | int ret = 0; |
| 449 | |
| 450 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, in) || |
| 451 | !EVP_DecryptUpdate(ctx, out, &ret, out, inlen) || |
| 452 | !EVP_DecryptFinal_ex(ctx, out, &ret)) |
| 453 | return 0; |
| 454 | |
| 455 | return 1; |
| 456 | } |
| 457 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 458 | /* Initialize the cipher context for TX part of <tls_ctx> QUIC TLS context. |
| 459 | * Return 1 if succeeded, 0 if not. |
| 460 | */ |
| 461 | int quic_tls_tx_ctx_init(EVP_CIPHER_CTX **tx_ctx, |
| 462 | const EVP_CIPHER *aead, unsigned char *key) |
| 463 | { |
| 464 | EVP_CIPHER_CTX *ctx; |
| 465 | int aead_nid = EVP_CIPHER_nid(aead); |
| 466 | |
| 467 | ctx = EVP_CIPHER_CTX_new(); |
| 468 | if (!ctx) |
| 469 | return 0; |
| 470 | |
| 471 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, NULL, NULL) || |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 472 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, QUIC_TLS_IV_LEN, NULL) || |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 473 | (aead_nid == NID_aes_128_ccm && |
| 474 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) || |
| 475 | !EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL)) |
| 476 | goto err; |
| 477 | |
| 478 | *tx_ctx = ctx; |
| 479 | |
| 480 | return 1; |
| 481 | |
| 482 | err: |
| 483 | EVP_CIPHER_CTX_free(ctx); |
| 484 | return 0; |
| 485 | } |
| 486 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 487 | /* |
| 488 | * https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#aead |
| 489 | * |
| 490 | * 5.3. AEAD Usage |
| 491 | * |
| 492 | * Packets are protected prior to applying header protection (Section 5.4). |
| 493 | * The unprotected packet header is part of the associated data (A). When removing |
| 494 | * packet protection, an endpoint first removes the header protection. |
| 495 | * (...) |
| 496 | * These ciphersuites have a 16-byte authentication tag and produce an output 16 |
| 497 | * bytes larger than their input. |
| 498 | * The key and IV for the packet are computed as described in Section 5.1. The nonce, |
| 499 | * N, is formed by combining the packet protection IV with the packet number. The 62 |
| 500 | * bits of the reconstructed QUIC packet number in network byte order are left-padded |
| 501 | * with zeros to the size of the IV. The exclusive OR of the padded packet number and |
| 502 | * the IV forms the AEAD nonce. |
| 503 | * |
| 504 | * The associated data, A, for the AEAD is the contents of the QUIC header, starting |
| 505 | * from the flags byte in either the short or long header, up to and including the |
| 506 | * unprotected packet number. |
| 507 | * |
| 508 | * The input plaintext, P, for the AEAD is the payload of the QUIC packet, as described |
| 509 | * in [QUIC-TRANSPORT]. |
| 510 | * |
| 511 | * The output ciphertext, C, of the AEAD is transmitted in place of P. |
| 512 | * |
| 513 | * Some AEAD functions have limits for how many packets can be encrypted under the same |
| 514 | * key and IV (see for example [AEBounds]). This might be lower than the packet number limit. |
| 515 | * An endpoint MUST initiate a key update (Section 6) prior to exceeding any limit set for |
| 516 | * the AEAD that is in use. |
| 517 | */ |
| 518 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 519 | /* Encrypt in place <buf> plaintext with <len> as length with QUIC_TLS_TAG_LEN |
| 520 | * included tailing bytes for the tag. |
| 521 | * Note that for CCM mode, we must set the the ciphertext length if AAD data |
| 522 | * are provided from <aad> buffer with <aad_len> as length. This is always the |
| 523 | * case here. So the caller of this function must provide <aad>. |
| 524 | * |
| 525 | * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption |
| 526 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 527 | int quic_tls_encrypt(unsigned char *buf, size_t len, |
| 528 | const unsigned char *aad, size_t aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 529 | EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead, |
Emeric Brun | e0190c6 | 2023-07-11 14:53:41 +0200 | [diff] [blame] | 530 | const unsigned char *iv) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 531 | { |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 532 | int outlen; |
| 533 | int aead_nid = EVP_CIPHER_nid(aead); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 534 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 535 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) || |
| 536 | (aead_nid == NID_aes_128_ccm && |
| 537 | !EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, len)) || |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 538 | !EVP_EncryptUpdate(ctx, NULL, &outlen, aad, aad_len) || |
| 539 | !EVP_EncryptUpdate(ctx, buf, &outlen, buf, len) || |
| 540 | !EVP_EncryptFinal_ex(ctx, buf + outlen, &outlen) || |
| 541 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, buf + len)) |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 542 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 543 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 544 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 547 | /* Decrypt in place <buf> ciphertext with <len> as length with QUIC_TLS_TAG_LEN |
| 548 | * included tailing bytes for the tag. |
| 549 | * Note that for CCM mode, we must set the the ciphertext length if AAD data |
| 550 | * are provided from <aad> buffer with <aad_len> as length. This is always the |
| 551 | * case here. So the caller of this function must provide <aad>. Also not the |
| 552 | * there is no need to call EVP_DecryptFinal_ex for CCM mode. |
| 553 | * |
| 554 | * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption |
| 555 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 556 | int quic_tls_decrypt(unsigned char *buf, size_t len, |
| 557 | unsigned char *aad, size_t aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 558 | EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead, |
| 559 | const unsigned char *key, const unsigned char *iv) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 560 | { |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 561 | int outlen; |
| 562 | int aead_nid = EVP_CIPHER_nid(aead); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 563 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 564 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) || |
| 565 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 566 | buf + len - QUIC_TLS_TAG_LEN) || |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 567 | (aead_nid == NID_aes_128_ccm && |
| 568 | !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len - QUIC_TLS_TAG_LEN)) || |
| 569 | !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) || |
| 570 | !EVP_DecryptUpdate(ctx, buf, &outlen, buf, len - QUIC_TLS_TAG_LEN) || |
| 571 | (aead_nid != NID_aes_128_ccm && |
| 572 | !EVP_DecryptFinal_ex(ctx, buf + outlen, &outlen))) |
| 573 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 574 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 575 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 576 | } |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 577 | |
Frédéric Lécaille | 55367c8 | 2022-05-16 10:27:57 +0200 | [diff] [blame] | 578 | /* Similar to quic_tls_decrypt(), except that this function does not decrypt |
| 579 | * in place its ciphertest if <out> output buffer ciphertest with <len> as length |
| 580 | * is different from <in> input buffer. This is the responbality of the caller |
| 581 | * to check that the output buffer has at least the same size as the input buffer. |
| 582 | * Note that for CCM mode, we must set the the ciphertext length if AAD data |
| 583 | * are provided from <aad> buffer with <aad_len> as length. This is always the |
| 584 | * case here. So the caller of this function must provide <aad>. Also note that |
| 585 | * there is no need to call EVP_DecryptFinal_ex for CCM mode. |
| 586 | * |
| 587 | * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption |
| 588 | * |
| 589 | * Return 1 if succeeded, 0 if not. |
| 590 | */ |
| 591 | int quic_tls_decrypt2(unsigned char *out, |
| 592 | unsigned char *in, size_t len, |
| 593 | unsigned char *aad, size_t aad_len, |
| 594 | EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead, |
| 595 | const unsigned char *key, const unsigned char *iv) |
| 596 | { |
| 597 | int outlen; |
| 598 | int aead_nid = EVP_CIPHER_nid(aead); |
| 599 | |
| 600 | len -= QUIC_TLS_TAG_LEN; |
| 601 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) || |
| 602 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, in + len) || |
| 603 | (aead_nid == NID_aes_128_ccm && |
| 604 | !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len)) || |
| 605 | !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) || |
| 606 | !EVP_DecryptUpdate(ctx, out, &outlen, in, len) || |
| 607 | (aead_nid != NID_aes_128_ccm && |
| 608 | !EVP_DecryptFinal_ex(ctx, out + outlen, &outlen))) |
| 609 | return 0; |
| 610 | |
| 611 | return 1; |
| 612 | } |
| 613 | |
Frédéric Lécaille | a9c5d8d | 2022-05-12 14:44:51 +0200 | [diff] [blame] | 614 | /* Derive <key> and <iv> key and IV to be used to encrypt a retry token |
| 615 | * with <secret> which is not pseudo-random. |
| 616 | * Return 1 if succeeded, 0 if not. |
| 617 | */ |
| 618 | int quic_tls_derive_retry_token_secret(const EVP_MD *md, |
| 619 | unsigned char *key, size_t keylen, |
| 620 | unsigned char *iv, size_t ivlen, |
| 621 | const unsigned char *salt, size_t saltlen, |
| 622 | const unsigned char *secret, size_t secretlen) |
| 623 | { |
| 624 | unsigned char tmpkey[QUIC_TLS_KEY_LEN]; |
Frédéric Lécaille | a9c5d8d | 2022-05-12 14:44:51 +0200 | [diff] [blame] | 625 | const unsigned char key_label[] = "retry token key"; |
| 626 | const unsigned char iv_label[] = "retry token iv"; |
| 627 | |
Emeric Brun | e397d86 | 2023-07-04 14:56:08 +0200 | [diff] [blame] | 628 | if (!quic_hkdf_extract(md, tmpkey, sizeof tmpkey, |
| 629 | secret, secretlen, salt, saltlen) || |
Frédéric Lécaille | a9c5d8d | 2022-05-12 14:44:51 +0200 | [diff] [blame] | 630 | !quic_hkdf_expand(md, key, keylen, tmpkey, sizeof tmpkey, |
| 631 | key_label, sizeof key_label - 1) || |
Emeric Brun | 28f430c | 2023-07-03 12:14:41 +0200 | [diff] [blame] | 632 | !quic_hkdf_expand(md, iv, ivlen, tmpkey, sizeof tmpkey, |
Frédéric Lécaille | a9c5d8d | 2022-05-12 14:44:51 +0200 | [diff] [blame] | 633 | iv_label, sizeof iv_label - 1)) |
| 634 | return 0; |
| 635 | |
| 636 | return 1; |
| 637 | } |
| 638 | |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 639 | /* Generate the AEAD tag for the Retry packet <pkt> of <pkt_len> bytes and |
| 640 | * write it to <tag>. The tag is written just after the <pkt> area. It should |
| 641 | * be at least 16 bytes longs. <odcid> is the CID of the Initial packet |
| 642 | * received which triggers the Retry. |
| 643 | * |
| 644 | * Returns non-zero on success else zero. |
| 645 | */ |
Frédéric Lécaille | 3f96a0a | 2022-06-08 08:26:03 +0200 | [diff] [blame] | 646 | int quic_tls_generate_retry_integrity_tag(unsigned char *odcid, unsigned char odcid_len, |
| 647 | unsigned char *pkt, size_t pkt_len, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 648 | const struct quic_version *qv) |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 649 | { |
| 650 | const EVP_CIPHER *evp = EVP_aes_128_gcm(); |
| 651 | EVP_CIPHER_CTX *ctx; |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 652 | |
| 653 | /* encryption buffer - not used as only AEAD tag generation is proceed */ |
| 654 | unsigned char *out = NULL; |
| 655 | /* address to store the AEAD tag */ |
| 656 | unsigned char *tag = pkt + pkt_len; |
| 657 | int outlen, ret = 0; |
| 658 | |
| 659 | ctx = EVP_CIPHER_CTX_new(); |
| 660 | if (!ctx) |
| 661 | return 0; |
| 662 | |
| 663 | /* rfc9001 5.8. Retry Packet Integrity |
| 664 | * |
| 665 | * AEAD is proceed over a pseudo-Retry packet used as AAD. It contains |
| 666 | * the ODCID len + data and the Retry packet itself. |
| 667 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 668 | if (!EVP_EncryptInit_ex(ctx, evp, NULL, qv->retry_tag_key, qv->retry_tag_nonce) || |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 669 | /* specify pseudo-Retry as AAD */ |
| 670 | !EVP_EncryptUpdate(ctx, NULL, &outlen, &odcid_len, sizeof(odcid_len)) || |
| 671 | !EVP_EncryptUpdate(ctx, NULL, &outlen, odcid, odcid_len) || |
| 672 | !EVP_EncryptUpdate(ctx, NULL, &outlen, pkt, pkt_len) || |
| 673 | /* finalize */ |
| 674 | !EVP_EncryptFinal_ex(ctx, out, &outlen) || |
| 675 | /* store the tag */ |
| 676 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, tag)) { |
| 677 | goto out; |
| 678 | } |
| 679 | ret = 1; |
| 680 | |
| 681 | out: |
| 682 | EVP_CIPHER_CTX_free(ctx); |
| 683 | return ret; |
| 684 | } |