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 | |
| 34 | const unsigned char initial_salt_v2_draft[20] = { |
| 35 | 0xa7, 0x07, 0xc2, 0x03, 0xa5, 0x9b, 0x47, 0x18, |
| 36 | 0x4a, 0x1d, 0x62, 0xca, 0x57, 0x04, 0x06, 0xea, |
| 37 | 0x7a, 0xe3, 0xe5, 0xd3 |
| 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 | |
| 60 | /* Dump <secret> TLS secret. */ |
| 61 | void quic_tls_secret_hexdump(struct buffer *buf, |
| 62 | const unsigned char *secret, size_t secret_len) |
| 63 | { |
| 64 | int i; |
| 65 | |
| 66 | chunk_appendf(buf, " secret="); |
| 67 | for (i = 0; i < secret_len; i++) |
| 68 | chunk_appendf(buf, "%02x", secret[i]); |
| 69 | } |
| 70 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 71 | int quic_hkdf_extract(const EVP_MD *md, |
Frédéric Lécaille | 4ba3b4e | 2022-05-10 18:40:19 +0200 | [diff] [blame] | 72 | unsigned char *buf, size_t buflen, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 73 | const unsigned char *key, size_t keylen, |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 74 | const unsigned char *salt, size_t saltlen) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 75 | { |
| 76 | EVP_PKEY_CTX *ctx; |
| 77 | |
| 78 | ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); |
| 79 | if (!ctx) |
| 80 | return 0; |
| 81 | |
| 82 | if (EVP_PKEY_derive_init(ctx) <= 0 || |
| 83 | EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) <= 0 || |
| 84 | EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 || |
| 85 | EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, saltlen) <= 0 || |
| 86 | 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] | 87 | EVP_PKEY_derive(ctx, buf, &buflen) <= 0) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 88 | goto err; |
| 89 | |
| 90 | EVP_PKEY_CTX_free(ctx); |
| 91 | return 1; |
| 92 | |
| 93 | err: |
| 94 | EVP_PKEY_CTX_free(ctx); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int quic_hkdf_expand(const EVP_MD *md, |
| 99 | unsigned char *buf, size_t buflen, |
| 100 | const unsigned char *key, size_t keylen, |
| 101 | const unsigned char *label, size_t labellen) |
| 102 | { |
| 103 | EVP_PKEY_CTX *ctx; |
| 104 | |
| 105 | ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); |
| 106 | if (!ctx) |
| 107 | return 0; |
| 108 | |
| 109 | if (EVP_PKEY_derive_init(ctx) <= 0 || |
| 110 | EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) <= 0 || |
| 111 | EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 || |
| 112 | EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 || |
| 113 | EVP_PKEY_CTX_add1_hkdf_info(ctx, label, labellen) <= 0 || |
| 114 | EVP_PKEY_derive(ctx, buf, &buflen) <= 0) |
| 115 | goto err; |
| 116 | |
| 117 | EVP_PKEY_CTX_free(ctx); |
| 118 | return 1; |
| 119 | |
| 120 | err: |
| 121 | EVP_PKEY_CTX_free(ctx); |
| 122 | return 0; |
| 123 | } |
Frédéric Lécaille | 7b92c81 | 2022-05-06 09:54:48 +0200 | [diff] [blame] | 124 | |
| 125 | /* Extracts a peudo-random secret key from <key> which is eventually not |
| 126 | * pseudo-random and expand it to a new pseudo-random key into |
| 127 | * <buf> with <buflen> as key length according to HKDF specifications |
| 128 | * (https://datatracker.ietf.org/doc/html/rfc5869). |
| 129 | * According to this specifications it is highly recommended to use |
| 130 | * a salt, even if optional (NULL value). |
| 131 | * Return 1 if succeeded, 0 if not. |
| 132 | */ |
| 133 | int quic_hkdf_extract_and_expand(const EVP_MD *md, |
| 134 | unsigned char *buf, size_t buflen, |
| 135 | const unsigned char *key, size_t keylen, |
| 136 | const unsigned char *salt, size_t saltlen, |
| 137 | const unsigned char *label, size_t labellen) |
| 138 | { |
| 139 | EVP_PKEY_CTX *ctx; |
| 140 | |
| 141 | ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); |
| 142 | if (!ctx) |
| 143 | return 0; |
| 144 | |
| 145 | if (EVP_PKEY_derive_init(ctx) <= 0 || |
| 146 | EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) <= 0 || |
| 147 | EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0 || |
| 148 | EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, saltlen) <= 0 || |
| 149 | EVP_PKEY_CTX_set1_hkdf_key(ctx, key, keylen) <= 0 || |
| 150 | EVP_PKEY_CTX_add1_hkdf_info(ctx, label, labellen) <= 0 || |
| 151 | EVP_PKEY_derive(ctx, buf, &buflen) <= 0) |
| 152 | goto err; |
| 153 | |
| 154 | EVP_PKEY_CTX_free(ctx); |
| 155 | return 1; |
| 156 | |
| 157 | err: |
| 158 | EVP_PKEY_CTX_free(ctx); |
| 159 | return 0; |
| 160 | } |
| 161 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 162 | /* https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#protection-keys |
| 163 | * refers to: |
| 164 | * |
| 165 | * https://tools.ietf.org/html/rfc8446#section-7.1: |
| 166 | * 7.1. Key Schedule |
| 167 | * |
| 168 | * The key derivation process makes use of the HKDF-Extract and |
| 169 | * HKDF-Expand functions as defined for HKDF [RFC5869], as well as the |
| 170 | * functions defined below: |
| 171 | * |
| 172 | * HKDF-Expand-Label(Secret, Label, Context, Length) = |
| 173 | * HKDF-Expand(Secret, HkdfLabel, Length) |
| 174 | * |
| 175 | * Where HkdfLabel is specified as: |
| 176 | * |
| 177 | * struct { |
| 178 | * uint16 length = Length; |
| 179 | * opaque label<7..255> = "tls13 " + Label; |
| 180 | * opaque context<0..255> = Context; |
| 181 | * } HkdfLabel; |
| 182 | * |
| 183 | * Derive-Secret(Secret, Label, Messages) = |
| 184 | * HKDF-Expand-Label(Secret, Label, |
| 185 | * Transcript-Hash(Messages), Hash.length) |
| 186 | * |
| 187 | */ |
| 188 | int quic_hkdf_expand_label(const EVP_MD *md, |
| 189 | unsigned char *buf, size_t buflen, |
| 190 | const unsigned char *key, size_t keylen, |
| 191 | const unsigned char *label, size_t labellen) |
| 192 | { |
| 193 | unsigned char hdkf_label[256], *pos; |
| 194 | const unsigned char hdkf_label_label[] = "tls13 "; |
| 195 | size_t hdkf_label_label_sz = sizeof hdkf_label_label - 1; |
| 196 | |
| 197 | pos = hdkf_label; |
| 198 | *pos++ = buflen >> 8; |
| 199 | *pos++ = buflen & 0xff; |
| 200 | *pos++ = hdkf_label_label_sz + labellen; |
| 201 | memcpy(pos, hdkf_label_label, hdkf_label_label_sz); |
| 202 | pos += hdkf_label_label_sz; |
| 203 | memcpy(pos, label, labellen); |
| 204 | pos += labellen; |
| 205 | *pos++ = '\0'; |
| 206 | |
| 207 | return quic_hkdf_expand(md, buf, buflen, |
| 208 | key, keylen, hdkf_label, pos - hdkf_label); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * This function derives two keys from <secret> is <ctx> as TLS cryptographic context. |
| 213 | * ->key is the TLS key to be derived to encrypt/decrypt data at TLS level. |
| 214 | * ->iv is the initialization vector to be used with ->key. |
| 215 | * ->hp_key is the key to be derived for header protection. |
| 216 | * Obviouly these keys have the same size becaused derived with the same TLS cryptographic context. |
| 217 | */ |
| 218 | 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] | 219 | const EVP_MD *md, const struct quic_version *qv, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 220 | unsigned char *key, size_t keylen, |
| 221 | unsigned char *iv, size_t ivlen, |
| 222 | unsigned char *hp_key, size_t hp_keylen, |
| 223 | const unsigned char *secret, size_t secretlen) |
| 224 | { |
| 225 | size_t aead_keylen = (size_t)EVP_CIPHER_key_length(aead); |
| 226 | 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] | 227 | 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] | 228 | |
| 229 | if (aead_keylen > keylen || aead_ivlen > ivlen || hp_len > hp_keylen) |
| 230 | return 0; |
| 231 | |
| 232 | 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] | 233 | qv->key_label,qv->key_label_len) || |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 234 | !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] | 235 | qv->iv_label, qv->iv_label_len) || |
Frédéric Lécaille | 6e351d6 | 2021-11-30 11:06:41 +0100 | [diff] [blame] | 236 | (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] | 237 | qv->hp_label, qv->hp_label_len))) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 238 | return 0; |
| 239 | |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Derive the initial secret from <secret> and QUIC version dependent salt. |
| 245 | * Returns the size of the derived secret if succeeded, 0 if not. |
| 246 | */ |
| 247 | int quic_derive_initial_secret(const EVP_MD *md, |
Frédéric Lécaille | 2fc76cf | 2021-08-31 19:10:40 +0200 | [diff] [blame] | 248 | 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] | 249 | unsigned char *initial_secret, size_t initial_secret_sz, |
| 250 | const unsigned char *secret, size_t secret_sz) |
| 251 | { |
Frédéric Lécaille | 4ba3b4e | 2022-05-10 18:40:19 +0200 | [diff] [blame] | 252 | 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] | 253 | initial_salt, initial_salt_sz)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 254 | return 0; |
| 255 | |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Derive the client initial secret from the initial secret. |
| 261 | * Returns the size of the derived secret if succeeded, 0 if not. |
| 262 | */ |
| 263 | int quic_tls_derive_initial_secrets(const EVP_MD *md, |
| 264 | unsigned char *rx, size_t rx_sz, |
| 265 | unsigned char *tx, size_t tx_sz, |
| 266 | const unsigned char *secret, size_t secret_sz, |
| 267 | int server) |
| 268 | { |
| 269 | const unsigned char client_label[] = "client in"; |
| 270 | const unsigned char server_label[] = "server in"; |
| 271 | const unsigned char *tx_label, *rx_label; |
| 272 | size_t rx_label_sz, tx_label_sz; |
| 273 | |
| 274 | if (server) { |
| 275 | rx_label = client_label; |
| 276 | rx_label_sz = sizeof client_label; |
| 277 | tx_label = server_label; |
| 278 | tx_label_sz = sizeof server_label; |
| 279 | } |
| 280 | else { |
| 281 | rx_label = server_label; |
| 282 | rx_label_sz = sizeof server_label; |
| 283 | tx_label = client_label; |
| 284 | tx_label_sz = sizeof client_label; |
| 285 | } |
| 286 | |
| 287 | if (!quic_hkdf_expand_label(md, rx, rx_sz, secret, secret_sz, |
| 288 | rx_label, rx_label_sz - 1) || |
| 289 | !quic_hkdf_expand_label(md, tx, tx_sz, secret, secret_sz, |
| 290 | tx_label, tx_label_sz - 1)) |
| 291 | return 0; |
| 292 | |
| 293 | return 1; |
| 294 | } |
| 295 | |
Frédéric Lécaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 296 | /* Update <sec> secret key into <new_sec> according to RFC 9001 6.1. |
| 297 | * Always succeeds. |
| 298 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 299 | 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] | 300 | unsigned char *new_sec, size_t new_seclen, |
| 301 | const unsigned char *sec, size_t seclen) |
| 302 | { |
Frédéric Lécaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 303 | 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] | 304 | qv->ku_label, qv->ku_label_len); |
Frédéric Lécaille | 39484de | 2021-11-30 10:10:24 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 307 | /* |
| 308 | * Build an IV into <iv> buffer with <ivlen> as size from <aead_iv> with |
| 309 | * <aead_ivlen> as size depending on <pn> packet number. |
| 310 | * This is the function which must be called to build an AEAD IV for the AEAD cryptographic algorithm |
| 311 | * used to encrypt/decrypt the QUIC packet payloads depending on the packet number <pn>. |
| 312 | * This function fails and return 0 only if the two buffer lengths are different, 1 if not. |
| 313 | */ |
| 314 | int quic_aead_iv_build(unsigned char *iv, size_t ivlen, |
| 315 | unsigned char *aead_iv, size_t aead_ivlen, uint64_t pn) |
| 316 | { |
| 317 | int i; |
| 318 | unsigned int shift; |
| 319 | unsigned char *pos = iv; |
| 320 | |
| 321 | if (ivlen != aead_ivlen) |
| 322 | return 0; |
| 323 | |
| 324 | for (i = 0; i < ivlen - sizeof pn; i++) |
| 325 | *pos++ = *aead_iv++; |
| 326 | |
| 327 | /* Only the remaining (sizeof pn) bytes are XOR'ed. */ |
| 328 | shift = 56; |
| 329 | for (i = aead_ivlen - sizeof pn; i < aead_ivlen ; i++, shift -= 8) |
| 330 | *pos++ = *aead_iv++ ^ (pn >> shift); |
| 331 | |
| 332 | return 1; |
| 333 | } |
| 334 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 335 | /* Initialize the cipher context for RX part of <tls_ctx> QUIC TLS context. |
| 336 | * Return 1 if succeeded, 0 if not. |
| 337 | */ |
| 338 | int quic_tls_rx_ctx_init(EVP_CIPHER_CTX **rx_ctx, |
| 339 | const EVP_CIPHER *aead, unsigned char *key) |
| 340 | { |
| 341 | EVP_CIPHER_CTX *ctx; |
| 342 | int aead_nid = EVP_CIPHER_nid(aead); |
| 343 | |
| 344 | ctx = EVP_CIPHER_CTX_new(); |
| 345 | if (!ctx) |
| 346 | return 0; |
| 347 | |
| 348 | if (!EVP_DecryptInit_ex(ctx, aead, NULL, NULL, NULL) || |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 349 | !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] | 350 | (aead_nid == NID_aes_128_ccm && |
| 351 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) || |
| 352 | !EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL)) |
| 353 | goto err; |
| 354 | |
| 355 | *rx_ctx = ctx; |
| 356 | |
| 357 | return 1; |
| 358 | |
| 359 | err: |
| 360 | EVP_CIPHER_CTX_free(ctx); |
| 361 | return 0; |
| 362 | } |
| 363 | |
Frédéric Lécaille | 86a53c5 | 2022-08-19 18:18:13 +0200 | [diff] [blame] | 364 | /* Initialize <*aes_ctx> AES cipher context with <key> as key for encryption */ |
| 365 | int quic_tls_enc_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx, |
| 366 | const EVP_CIPHER *aes, unsigned char *key) |
| 367 | { |
| 368 | EVP_CIPHER_CTX *ctx; |
| 369 | |
| 370 | ctx = EVP_CIPHER_CTX_new(); |
| 371 | if (!ctx) |
| 372 | return 0; |
| 373 | |
| 374 | if (!EVP_EncryptInit_ex(ctx, aes, NULL, key, NULL)) |
| 375 | goto err; |
| 376 | |
| 377 | *aes_ctx = ctx; |
| 378 | return 1; |
| 379 | |
| 380 | err: |
| 381 | EVP_CIPHER_CTX_free(ctx); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* Encrypt <inlen> bytes from <in> buffer into <out> with <ctx> as AES |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 386 | * 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] | 387 | * is at least <inlen> bytes of available space in <out> buffer. |
| 388 | * Return 1 if succeeded, 0 if not. |
| 389 | */ |
| 390 | int quic_tls_aes_encrypt(unsigned char *out, |
| 391 | const unsigned char *in, size_t inlen, |
| 392 | EVP_CIPHER_CTX *ctx) |
| 393 | { |
| 394 | int ret = 0; |
| 395 | |
| 396 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, in) || |
| 397 | !EVP_EncryptUpdate(ctx, out, &ret, out, inlen) || |
| 398 | !EVP_EncryptFinal_ex(ctx, out, &ret)) |
| 399 | return 0; |
| 400 | |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | /* Initialize <*aes_ctx> AES cipher context with <key> as key for decryption */ |
| 405 | int quic_tls_dec_aes_ctx_init(EVP_CIPHER_CTX **aes_ctx, |
| 406 | const EVP_CIPHER *aes, unsigned char *key) |
| 407 | { |
| 408 | EVP_CIPHER_CTX *ctx; |
| 409 | |
| 410 | ctx = EVP_CIPHER_CTX_new(); |
| 411 | if (!ctx) |
| 412 | return 0; |
| 413 | |
| 414 | if (!EVP_DecryptInit_ex(ctx, aes, NULL, key, NULL)) |
| 415 | goto err; |
| 416 | |
| 417 | *aes_ctx = ctx; |
| 418 | return 1; |
| 419 | |
| 420 | err: |
| 421 | EVP_CIPHER_CTX_free(ctx); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* Decrypt <in> data into <out> with <ctx> as AES cipher context. |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 426 | * 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] | 427 | * <outlen> bytes into <in> buffer. |
| 428 | * Return 1 if succeeded, 0 if not. |
| 429 | */ |
| 430 | int quic_tls_aes_decrypt(unsigned char *out, |
| 431 | const unsigned char *in, size_t inlen, |
| 432 | EVP_CIPHER_CTX *ctx) |
| 433 | { |
| 434 | int ret = 0; |
| 435 | |
| 436 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, in) || |
| 437 | !EVP_DecryptUpdate(ctx, out, &ret, out, inlen) || |
| 438 | !EVP_DecryptFinal_ex(ctx, out, &ret)) |
| 439 | return 0; |
| 440 | |
| 441 | return 1; |
| 442 | } |
| 443 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 444 | /* Initialize the cipher context for TX part of <tls_ctx> QUIC TLS context. |
| 445 | * Return 1 if succeeded, 0 if not. |
| 446 | */ |
| 447 | int quic_tls_tx_ctx_init(EVP_CIPHER_CTX **tx_ctx, |
| 448 | const EVP_CIPHER *aead, unsigned char *key) |
| 449 | { |
| 450 | EVP_CIPHER_CTX *ctx; |
| 451 | int aead_nid = EVP_CIPHER_nid(aead); |
| 452 | |
| 453 | ctx = EVP_CIPHER_CTX_new(); |
| 454 | if (!ctx) |
| 455 | return 0; |
| 456 | |
| 457 | if (!EVP_EncryptInit_ex(ctx, aead, NULL, NULL, NULL) || |
Frédéric Lécaille | f2f4a4e | 2022-04-05 12:18:46 +0200 | [diff] [blame] | 458 | !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] | 459 | (aead_nid == NID_aes_128_ccm && |
| 460 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, NULL)) || |
| 461 | !EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL)) |
| 462 | goto err; |
| 463 | |
| 464 | *tx_ctx = ctx; |
| 465 | |
| 466 | return 1; |
| 467 | |
| 468 | err: |
| 469 | EVP_CIPHER_CTX_free(ctx); |
| 470 | return 0; |
| 471 | } |
| 472 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 473 | /* |
| 474 | * https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#aead |
| 475 | * |
| 476 | * 5.3. AEAD Usage |
| 477 | * |
| 478 | * Packets are protected prior to applying header protection (Section 5.4). |
| 479 | * The unprotected packet header is part of the associated data (A). When removing |
| 480 | * packet protection, an endpoint first removes the header protection. |
| 481 | * (...) |
| 482 | * These ciphersuites have a 16-byte authentication tag and produce an output 16 |
| 483 | * bytes larger than their input. |
| 484 | * The key and IV for the packet are computed as described in Section 5.1. The nonce, |
| 485 | * N, is formed by combining the packet protection IV with the packet number. The 62 |
| 486 | * bits of the reconstructed QUIC packet number in network byte order are left-padded |
| 487 | * with zeros to the size of the IV. The exclusive OR of the padded packet number and |
| 488 | * the IV forms the AEAD nonce. |
| 489 | * |
| 490 | * The associated data, A, for the AEAD is the contents of the QUIC header, starting |
| 491 | * from the flags byte in either the short or long header, up to and including the |
| 492 | * unprotected packet number. |
| 493 | * |
| 494 | * The input plaintext, P, for the AEAD is the payload of the QUIC packet, as described |
| 495 | * in [QUIC-TRANSPORT]. |
| 496 | * |
| 497 | * The output ciphertext, C, of the AEAD is transmitted in place of P. |
| 498 | * |
| 499 | * Some AEAD functions have limits for how many packets can be encrypted under the same |
| 500 | * key and IV (see for example [AEBounds]). This might be lower than the packet number limit. |
| 501 | * An endpoint MUST initiate a key update (Section 6) prior to exceeding any limit set for |
| 502 | * the AEAD that is in use. |
| 503 | */ |
| 504 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 505 | /* Encrypt in place <buf> plaintext with <len> as length with QUIC_TLS_TAG_LEN |
| 506 | * included tailing bytes for the tag. |
| 507 | * Note that for CCM mode, we must set the the ciphertext length if AAD data |
| 508 | * are provided from <aad> buffer with <aad_len> as length. This is always the |
| 509 | * case here. So the caller of this function must provide <aad>. |
| 510 | * |
| 511 | * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption |
| 512 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 513 | int quic_tls_encrypt(unsigned char *buf, size_t len, |
| 514 | const unsigned char *aad, size_t aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 515 | EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead, |
| 516 | const unsigned char *key, const unsigned char *iv) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 517 | { |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 518 | int outlen; |
| 519 | int aead_nid = EVP_CIPHER_nid(aead); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 520 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 521 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) || |
| 522 | (aead_nid == NID_aes_128_ccm && |
| 523 | !EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, len)) || |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 524 | !EVP_EncryptUpdate(ctx, NULL, &outlen, aad, aad_len) || |
| 525 | !EVP_EncryptUpdate(ctx, buf, &outlen, buf, len) || |
| 526 | !EVP_EncryptFinal_ex(ctx, buf + outlen, &outlen) || |
| 527 | !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] | 528 | return 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 529 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 530 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 533 | /* Decrypt in place <buf> ciphertext with <len> as length with QUIC_TLS_TAG_LEN |
| 534 | * included tailing bytes for the tag. |
| 535 | * Note that for CCM mode, we must set the the ciphertext length if AAD data |
| 536 | * are provided from <aad> buffer with <aad_len> as length. This is always the |
| 537 | * case here. So the caller of this function must provide <aad>. Also not the |
| 538 | * there is no need to call EVP_DecryptFinal_ex for CCM mode. |
| 539 | * |
| 540 | * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption |
| 541 | */ |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 542 | int quic_tls_decrypt(unsigned char *buf, size_t len, |
| 543 | unsigned char *aad, size_t aad_len, |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 544 | EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead, |
| 545 | const unsigned char *key, const unsigned char *iv) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 546 | { |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 547 | int outlen; |
| 548 | int aead_nid = EVP_CIPHER_nid(aead); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 549 | |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 550 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) || |
| 551 | !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] | 552 | buf + len - QUIC_TLS_TAG_LEN) || |
Frédéric Lécaille | f460574 | 2022-04-05 10:28:29 +0200 | [diff] [blame] | 553 | (aead_nid == NID_aes_128_ccm && |
| 554 | !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len - QUIC_TLS_TAG_LEN)) || |
| 555 | !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) || |
| 556 | !EVP_DecryptUpdate(ctx, buf, &outlen, buf, len - QUIC_TLS_TAG_LEN) || |
| 557 | (aead_nid != NID_aes_128_ccm && |
| 558 | !EVP_DecryptFinal_ex(ctx, buf + outlen, &outlen))) |
| 559 | return 0; |
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 | return 1; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 562 | } |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 563 | |
Frédéric Lécaille | 55367c8 | 2022-05-16 10:27:57 +0200 | [diff] [blame] | 564 | /* Similar to quic_tls_decrypt(), except that this function does not decrypt |
| 565 | * in place its ciphertest if <out> output buffer ciphertest with <len> as length |
| 566 | * is different from <in> input buffer. This is the responbality of the caller |
| 567 | * to check that the output buffer has at least the same size as the input buffer. |
| 568 | * Note that for CCM mode, we must set the the ciphertext length if AAD data |
| 569 | * are provided from <aad> buffer with <aad_len> as length. This is always the |
| 570 | * case here. So the caller of this function must provide <aad>. Also note that |
| 571 | * there is no need to call EVP_DecryptFinal_ex for CCM mode. |
| 572 | * |
| 573 | * https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption |
| 574 | * |
| 575 | * Return 1 if succeeded, 0 if not. |
| 576 | */ |
| 577 | int quic_tls_decrypt2(unsigned char *out, |
| 578 | unsigned char *in, size_t len, |
| 579 | unsigned char *aad, size_t aad_len, |
| 580 | EVP_CIPHER_CTX *ctx, const EVP_CIPHER *aead, |
| 581 | const unsigned char *key, const unsigned char *iv) |
| 582 | { |
| 583 | int outlen; |
| 584 | int aead_nid = EVP_CIPHER_nid(aead); |
| 585 | |
| 586 | len -= QUIC_TLS_TAG_LEN; |
| 587 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) || |
| 588 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, QUIC_TLS_TAG_LEN, in + len) || |
| 589 | (aead_nid == NID_aes_128_ccm && |
| 590 | !EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, len)) || |
| 591 | !EVP_DecryptUpdate(ctx, NULL, &outlen, aad, aad_len) || |
| 592 | !EVP_DecryptUpdate(ctx, out, &outlen, in, len) || |
| 593 | (aead_nid != NID_aes_128_ccm && |
| 594 | !EVP_DecryptFinal_ex(ctx, out + outlen, &outlen))) |
| 595 | return 0; |
| 596 | |
| 597 | return 1; |
| 598 | } |
| 599 | |
Frédéric Lécaille | a9c5d8d | 2022-05-12 14:44:51 +0200 | [diff] [blame] | 600 | /* Derive <key> and <iv> key and IV to be used to encrypt a retry token |
| 601 | * with <secret> which is not pseudo-random. |
| 602 | * Return 1 if succeeded, 0 if not. |
| 603 | */ |
| 604 | int quic_tls_derive_retry_token_secret(const EVP_MD *md, |
| 605 | unsigned char *key, size_t keylen, |
| 606 | unsigned char *iv, size_t ivlen, |
| 607 | const unsigned char *salt, size_t saltlen, |
| 608 | const unsigned char *secret, size_t secretlen) |
| 609 | { |
| 610 | unsigned char tmpkey[QUIC_TLS_KEY_LEN]; |
| 611 | const unsigned char tmpkey_label[] = "retry token"; |
| 612 | const unsigned char key_label[] = "retry token key"; |
| 613 | const unsigned char iv_label[] = "retry token iv"; |
| 614 | |
| 615 | if (!quic_hkdf_extract_and_expand(md, tmpkey, sizeof tmpkey, |
| 616 | secret, secretlen, salt, saltlen, |
| 617 | tmpkey_label, sizeof tmpkey_label - 1) || |
| 618 | !quic_hkdf_expand(md, key, keylen, tmpkey, sizeof tmpkey, |
| 619 | key_label, sizeof key_label - 1) || |
| 620 | !quic_hkdf_expand(md, iv, ivlen, secret, secretlen, |
| 621 | iv_label, sizeof iv_label - 1)) |
| 622 | return 0; |
| 623 | |
| 624 | return 1; |
| 625 | } |
| 626 | |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 627 | /* Generate the AEAD tag for the Retry packet <pkt> of <pkt_len> bytes and |
| 628 | * write it to <tag>. The tag is written just after the <pkt> area. It should |
| 629 | * be at least 16 bytes longs. <odcid> is the CID of the Initial packet |
| 630 | * received which triggers the Retry. |
| 631 | * |
| 632 | * Returns non-zero on success else zero. |
| 633 | */ |
Frédéric Lécaille | 3f96a0a | 2022-06-08 08:26:03 +0200 | [diff] [blame] | 634 | int quic_tls_generate_retry_integrity_tag(unsigned char *odcid, unsigned char odcid_len, |
| 635 | unsigned char *pkt, size_t pkt_len, |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 636 | const struct quic_version *qv) |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 637 | { |
| 638 | const EVP_CIPHER *evp = EVP_aes_128_gcm(); |
| 639 | EVP_CIPHER_CTX *ctx; |
Amaury Denoyelle | 6efec29 | 2022-01-11 11:57:00 +0100 | [diff] [blame] | 640 | |
| 641 | /* encryption buffer - not used as only AEAD tag generation is proceed */ |
| 642 | unsigned char *out = NULL; |
| 643 | /* address to store the AEAD tag */ |
| 644 | unsigned char *tag = pkt + pkt_len; |
| 645 | int outlen, ret = 0; |
| 646 | |
| 647 | ctx = EVP_CIPHER_CTX_new(); |
| 648 | if (!ctx) |
| 649 | return 0; |
| 650 | |
| 651 | /* rfc9001 5.8. Retry Packet Integrity |
| 652 | * |
| 653 | * AEAD is proceed over a pseudo-Retry packet used as AAD. It contains |
| 654 | * the ODCID len + data and the Retry packet itself. |
| 655 | */ |
Frédéric Lécaille | 86845c5 | 2022-06-08 19:28:36 +0200 | [diff] [blame] | 656 | 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] | 657 | /* specify pseudo-Retry as AAD */ |
| 658 | !EVP_EncryptUpdate(ctx, NULL, &outlen, &odcid_len, sizeof(odcid_len)) || |
| 659 | !EVP_EncryptUpdate(ctx, NULL, &outlen, odcid, odcid_len) || |
| 660 | !EVP_EncryptUpdate(ctx, NULL, &outlen, pkt, pkt_len) || |
| 661 | /* finalize */ |
| 662 | !EVP_EncryptFinal_ex(ctx, out, &outlen) || |
| 663 | /* store the tag */ |
| 664 | !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, QUIC_TLS_TAG_LEN, tag)) { |
| 665 | goto out; |
| 666 | } |
| 667 | ret = 1; |
| 668 | |
| 669 | out: |
| 670 | EVP_CIPHER_CTX_free(ctx); |
| 671 | return ret; |
| 672 | } |