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