Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <openssl/aead.h> |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/digest.h> |
| 23 | #include <openssl/hmac.h> |
| 24 | #include <openssl/hkdf.h> |
| 25 | #include <openssl/mem.h> |
| 26 | |
| 27 | #include "internal.h" |
| 28 | |
| 29 | |
| 30 | int tls13_init_key_schedule(SSL *ssl, const uint8_t *resumption_ctx, |
| 31 | size_t resumption_ctx_len) { |
| 32 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 33 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 34 | |
| 35 | hs->hash_len = EVP_MD_size(digest); |
| 36 | |
| 37 | /* Save the hash of the resumption context. */ |
| 38 | unsigned resumption_hash_len; |
| 39 | if (!EVP_Digest(resumption_ctx, resumption_ctx_len, hs->resumption_hash, |
| 40 | &resumption_hash_len, digest, NULL)) { |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | /* Initialize the secret to the zero key. */ |
| 45 | memset(hs->secret, 0, hs->hash_len); |
| 46 | |
| 47 | /* Initialize the rolling hashes and release the handshake buffer. */ |
| 48 | if (!ssl3_init_handshake_hash(ssl)) { |
| 49 | return 0; |
| 50 | } |
| 51 | ssl3_free_handshake_buffer(ssl); |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | int tls13_advance_key_schedule(SSL *ssl, const uint8_t *in, size_t len) { |
| 56 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 57 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 58 | |
| 59 | return HKDF_extract(hs->secret, &hs->hash_len, digest, in, len, hs->secret, |
| 60 | hs->hash_len); |
| 61 | } |
| 62 | |
| 63 | static int hkdf_expand_label(uint8_t *out, const EVP_MD *digest, |
| 64 | const uint8_t *secret, size_t secret_len, |
| 65 | const uint8_t *label, size_t label_len, |
| 66 | const uint8_t *hash, size_t hash_len, size_t len) { |
| 67 | static const char kTLS13LabelVersion[] = "TLS 1.3, "; |
| 68 | |
| 69 | CBB cbb, child; |
| 70 | uint8_t *hkdf_label; |
| 71 | size_t hkdf_label_len; |
| 72 | if (!CBB_init(&cbb, 2 + 1 + strlen(kTLS13LabelVersion) + label_len + 1 + |
| 73 | hash_len) || |
| 74 | !CBB_add_u16(&cbb, len) || |
| 75 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 76 | !CBB_add_bytes(&child, (const uint8_t *)kTLS13LabelVersion, |
| 77 | strlen(kTLS13LabelVersion)) || |
| 78 | !CBB_add_bytes(&child, label, label_len) || |
| 79 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 80 | !CBB_add_bytes(&child, hash, hash_len) || |
| 81 | !CBB_finish(&cbb, &hkdf_label, &hkdf_label_len)) { |
| 82 | CBB_cleanup(&cbb); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int ret = HKDF_expand(out, len, digest, secret, secret_len, hkdf_label, |
| 87 | hkdf_label_len); |
| 88 | OPENSSL_free(hkdf_label); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | int tls13_get_context_hashes(SSL *ssl, uint8_t *out, size_t *out_len) { |
| 93 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 94 | |
| 95 | EVP_MD_CTX ctx; |
| 96 | EVP_MD_CTX_init(&ctx); |
| 97 | unsigned handshake_len = 0; |
| 98 | int ok = EVP_MD_CTX_copy_ex(&ctx, &ssl->s3->handshake_hash) && |
| 99 | EVP_DigestFinal_ex(&ctx, out, &handshake_len); |
| 100 | EVP_MD_CTX_cleanup(&ctx); |
| 101 | if (!ok) { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | memcpy(out + handshake_len, hs->resumption_hash, hs->hash_len); |
| 106 | *out_len = handshake_len + hs->hash_len; |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | /* derive_secret derives a secret of length |len| and writes the result in |out| |
| 111 | * with the given label and the current base secret and most recently-saved |
| 112 | * handshake context. It returns one on success and zero on error. */ |
| 113 | static int derive_secret(SSL *ssl, uint8_t *out, size_t len, |
| 114 | const uint8_t *label, size_t label_len) { |
| 115 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 116 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 117 | |
| 118 | uint8_t context_hashes[2 * EVP_MAX_MD_SIZE]; |
| 119 | size_t context_hashes_len; |
| 120 | if (!tls13_get_context_hashes(ssl, context_hashes, &context_hashes_len)) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | return hkdf_expand_label(out, digest, hs->secret, hs->hash_len, label, |
| 125 | label_len, context_hashes, context_hashes_len, len); |
| 126 | } |
| 127 | |
| 128 | int tls13_set_traffic_key(SSL *ssl, enum tls_record_type_t type, |
| 129 | enum evp_aead_direction_t direction, |
| 130 | const uint8_t *traffic_secret, |
| 131 | size_t traffic_secret_len) { |
| 132 | if (traffic_secret_len > 0xff) { |
| 133 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | const char *phase; |
| 138 | switch (type) { |
| 139 | case type_early_handshake: |
| 140 | phase = "early handshake key expansion, "; |
| 141 | break; |
| 142 | case type_early_data: |
| 143 | phase = "early application data key expansion, "; |
| 144 | break; |
| 145 | case type_handshake: |
| 146 | phase = "handshake key expansion, "; |
| 147 | break; |
| 148 | case type_data: |
| 149 | phase = "application data key expansion, "; |
| 150 | break; |
| 151 | default: |
| 152 | return 0; |
| 153 | } |
| 154 | size_t phase_len = strlen(phase); |
| 155 | |
| 156 | const char *purpose = "client write key"; |
| 157 | if ((ssl->server && direction == evp_aead_seal) || |
| 158 | (!ssl->server && direction == evp_aead_open)) { |
| 159 | purpose = "server write key"; |
| 160 | } |
| 161 | size_t purpose_len = strlen(purpose); |
| 162 | |
| 163 | /* The longest label has length 38 (type_early_data) + 16 (either purpose |
| 164 | * value). */ |
| 165 | uint8_t label[38 + 16]; |
| 166 | size_t label_len = phase_len + purpose_len; |
| 167 | if (label_len > sizeof(label)) { |
| 168 | assert(0); |
| 169 | return 0; |
| 170 | } |
| 171 | memcpy(label, phase, phase_len); |
| 172 | memcpy(label + phase_len, purpose, purpose_len); |
| 173 | |
| 174 | /* Look up cipher suite properties. */ |
| 175 | const EVP_AEAD *aead; |
| 176 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 177 | size_t mac_secret_len, fixed_iv_len; |
| 178 | if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 179 | SSL_get_session(ssl)->cipher, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 180 | ssl3_protocol_version(ssl))) { |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* Derive the key. */ |
| 185 | size_t key_len = EVP_AEAD_key_length(aead); |
| 186 | uint8_t key[EVP_AEAD_MAX_KEY_LENGTH]; |
| 187 | if (!hkdf_expand_label(key, digest, traffic_secret, traffic_secret_len, label, |
| 188 | label_len, NULL, 0, key_len)) { |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /* The IV's label ends in "iv" instead of "key". */ |
| 193 | if (label_len < 3) { |
| 194 | assert(0); |
| 195 | return 0; |
| 196 | } |
| 197 | label_len--; |
| 198 | label[label_len - 2] = 'i'; |
| 199 | label[label_len - 1] = 'v'; |
| 200 | |
| 201 | /* Derive the IV. */ |
| 202 | size_t iv_len = EVP_AEAD_nonce_length(aead); |
| 203 | uint8_t iv[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 204 | if (!hkdf_expand_label(iv, digest, traffic_secret, traffic_secret_len, label, |
| 205 | label_len, NULL, 0, iv_len)) { |
| 206 | return 0; |
| 207 | } |
| 208 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 209 | SSL_AEAD_CTX *traffic_aead = SSL_AEAD_CTX_new( |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 210 | direction, ssl3_protocol_version(ssl), SSL_get_session(ssl)->cipher, key, |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 211 | key_len, NULL, 0, iv, iv_len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 212 | if (traffic_aead == NULL) { |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | if (direction == evp_aead_open) { |
| 217 | if (!ssl->method->set_read_state(ssl, traffic_aead)) { |
| 218 | return 0; |
| 219 | } |
| 220 | } else { |
| 221 | if (!ssl->method->set_write_state(ssl, traffic_aead)) { |
| 222 | return 0; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* Save the traffic secret. */ |
| 227 | if (direction == evp_aead_open) { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 228 | memmove(ssl->s3->read_traffic_secret, traffic_secret, traffic_secret_len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 229 | ssl->s3->read_traffic_secret_len = traffic_secret_len; |
| 230 | } else { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 231 | memmove(ssl->s3->write_traffic_secret, traffic_secret, traffic_secret_len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 232 | ssl->s3->write_traffic_secret_len = traffic_secret_len; |
| 233 | } |
| 234 | |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | static const char kTLS13LabelHandshakeTraffic[] = "handshake traffic secret"; |
| 239 | static const char kTLS13LabelApplicationTraffic[] = |
| 240 | "application traffic secret"; |
| 241 | |
| 242 | int tls13_set_handshake_traffic(SSL *ssl) { |
| 243 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 244 | |
| 245 | uint8_t traffic_secret[EVP_MAX_MD_SIZE]; |
| 246 | if (!derive_secret(ssl, traffic_secret, hs->hash_len, |
| 247 | (const uint8_t *)kTLS13LabelHandshakeTraffic, |
| 248 | strlen(kTLS13LabelHandshakeTraffic)) || |
David Benjamin | e776cc2 | 2016-07-19 07:26:49 +0200 | [diff] [blame] | 249 | !ssl_log_secret(ssl, "HANDSHAKE_TRAFFIC_SECRET", traffic_secret, |
| 250 | hs->hash_len) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 251 | !tls13_set_traffic_key(ssl, type_handshake, evp_aead_open, traffic_secret, |
| 252 | hs->hash_len) || |
| 253 | !tls13_set_traffic_key(ssl, type_handshake, evp_aead_seal, traffic_secret, |
| 254 | hs->hash_len)) { |
| 255 | return 0; |
| 256 | } |
| 257 | return 1; |
| 258 | } |
| 259 | |
| 260 | int tls13_derive_traffic_secret_0(SSL *ssl) { |
| 261 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 262 | |
| 263 | return derive_secret(ssl, hs->traffic_secret_0, hs->hash_len, |
| 264 | (const uint8_t *)kTLS13LabelApplicationTraffic, |
David Benjamin | e776cc2 | 2016-07-19 07:26:49 +0200 | [diff] [blame] | 265 | strlen(kTLS13LabelApplicationTraffic)) && |
| 266 | ssl_log_secret(ssl, "TRAFFIC_SECRET_0", hs->traffic_secret_0, |
| 267 | hs->hash_len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 270 | int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) { |
| 271 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 272 | |
| 273 | uint8_t *secret; |
| 274 | size_t secret_len; |
| 275 | if (direction == evp_aead_open) { |
| 276 | secret = ssl->s3->read_traffic_secret; |
| 277 | secret_len = ssl->s3->read_traffic_secret_len; |
| 278 | } else { |
| 279 | secret = ssl->s3->write_traffic_secret; |
| 280 | secret_len = ssl->s3->write_traffic_secret_len; |
| 281 | } |
| 282 | |
| 283 | if (!hkdf_expand_label(secret, digest, secret, secret_len, |
| 284 | (const uint8_t *)kTLS13LabelApplicationTraffic, |
| 285 | strlen(kTLS13LabelApplicationTraffic), NULL, 0, |
| 286 | secret_len)) { |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | return tls13_set_traffic_key(ssl, type_data, direction, secret, secret_len); |
| 291 | } |
| 292 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 293 | static const char kTLS13LabelExporter[] = "exporter master secret"; |
| 294 | static const char kTLS13LabelResumption[] = "resumption master secret"; |
| 295 | |
| 296 | int tls13_finalize_keys(SSL *ssl) { |
| 297 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 298 | |
| 299 | ssl->s3->exporter_secret_len = hs->hash_len; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 300 | ssl->s3->new_session->master_key_length = hs->hash_len; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 301 | if (!derive_secret( |
| 302 | ssl, ssl->s3->exporter_secret, ssl->s3->exporter_secret_len, |
| 303 | (const uint8_t *)kTLS13LabelExporter, strlen(kTLS13LabelExporter)) || |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 304 | !derive_secret(ssl, ssl->s3->new_session->master_key, |
| 305 | ssl->s3->new_session->master_key_length, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 306 | (const uint8_t *)kTLS13LabelResumption, |
| 307 | strlen(kTLS13LabelResumption))) { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | int tls13_finished_mac(SSL *ssl, uint8_t *out, size_t *out_len, int is_server) { |
| 315 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 316 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 317 | |
| 318 | uint8_t key[EVP_MAX_MD_SIZE]; |
| 319 | size_t key_len = EVP_MD_size(digest); |
| 320 | |
David Benjamin | 0fb46d4 | 2016-08-31 16:58:36 -0400 | [diff] [blame^] | 321 | const uint8_t *traffic_secret; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 322 | const char *label; |
| 323 | if (is_server) { |
| 324 | label = "server finished"; |
| 325 | if (ssl->server) { |
| 326 | traffic_secret = ssl->s3->write_traffic_secret; |
| 327 | } else { |
| 328 | traffic_secret = ssl->s3->read_traffic_secret; |
| 329 | } |
| 330 | } else { |
| 331 | label = "client finished"; |
| 332 | if (!ssl->server) { |
| 333 | traffic_secret = ssl->s3->write_traffic_secret; |
| 334 | } else { |
| 335 | traffic_secret = ssl->s3->read_traffic_secret; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | uint8_t context_hashes[2 * EVP_MAX_MD_SIZE]; |
| 340 | size_t context_hashes_len; |
| 341 | unsigned len; |
| 342 | if (!hkdf_expand_label(key, digest, traffic_secret, hs->hash_len, |
| 343 | (const uint8_t *)label, strlen(label), NULL, 0, |
| 344 | hs->hash_len) || |
| 345 | !tls13_get_context_hashes(ssl, context_hashes, &context_hashes_len) || |
| 346 | HMAC(digest, key, key_len, context_hashes, context_hashes_len, out, |
| 347 | &len) == NULL) { |
| 348 | return 0; |
| 349 | } |
| 350 | *out_len = len; |
| 351 | return 1; |
| 352 | } |
| 353 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 354 | static const char kTLS13LabelResumptionPSK[] = "resumption psk"; |
| 355 | static const char kTLS13LabelResumptionContext[] = "resumption context"; |
| 356 | |
| 357 | int tls13_resumption_psk(SSL *ssl, uint8_t *out, size_t out_len, |
| 358 | const SSL_SESSION *session) { |
| 359 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 360 | return hkdf_expand_label(out, digest, session->master_key, |
| 361 | session->master_key_length, |
| 362 | (const uint8_t *)kTLS13LabelResumptionPSK, |
| 363 | strlen(kTLS13LabelResumptionPSK), NULL, 0, out_len); |
| 364 | } |
| 365 | |
| 366 | int tls13_resumption_context(SSL *ssl, uint8_t *out, size_t out_len, |
| 367 | const SSL_SESSION *session) { |
| 368 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 369 | return hkdf_expand_label(out, digest, session->master_key, |
| 370 | session->master_key_length, |
| 371 | (const uint8_t *)kTLS13LabelResumptionContext, |
| 372 | strlen(kTLS13LabelResumptionContext), NULL, 0, |
| 373 | out_len); |
| 374 | } |
| 375 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 376 | int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 377 | const char *label, size_t label_len, |
| 378 | const uint8_t *context, size_t context_len, |
| 379 | int use_context) { |
| 380 | const EVP_MD *digest = ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)); |
| 381 | |
| 382 | const uint8_t *hash = NULL; |
| 383 | size_t hash_len = 0; |
| 384 | if (use_context) { |
| 385 | hash = context; |
| 386 | hash_len = context_len; |
| 387 | } |
| 388 | return hkdf_expand_label(out, digest, ssl->s3->exporter_secret, |
| 389 | ssl->s3->exporter_secret_len, (const uint8_t *)label, |
| 390 | label_len, hash, hash_len, out_len); |
| 391 | } |