David Benjamin | c895d6b | 2016-08-11 13:26:41 -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 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 22 | #include <openssl/aead.h> |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/digest.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 25 | #include <openssl/hkdf.h> |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 26 | #include <openssl/hmac.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 27 | #include <openssl/mem.h> |
| 28 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 29 | #include "../crypto/internal.h" |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
| 32 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 33 | namespace bssl { |
| 34 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 35 | static int init_key_schedule(SSL_HANDSHAKE *hs, uint16_t version, |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 36 | const SSL_CIPHER *cipher) { |
| 37 | if (!hs->transcript.InitHash(version, cipher)) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 38 | return 0; |
| 39 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 40 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 41 | hs->hash_len = hs->transcript.DigestLen(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 42 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 43 | /* Initialize the secret to the zero key. */ |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 44 | OPENSSL_memset(hs->secret, 0, hs->hash_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 45 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | int tls13_init_key_schedule(SSL_HANDSHAKE *hs) { |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 50 | if (!init_key_schedule(hs, ssl3_protocol_version(hs->ssl), hs->new_cipher)) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 54 | hs->transcript.FreeBuffer(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 55 | return 1; |
| 56 | } |
| 57 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 58 | int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs) { |
| 59 | SSL *const ssl = hs->ssl; |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 60 | return init_key_schedule(hs, SSL_SESSION_protocol_version(ssl->session), |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 61 | ssl->session->cipher); |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 62 | } |
| 63 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 64 | int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in, |
| 65 | size_t len) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 66 | return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), in, |
| 67 | len, hs->secret, hs->hash_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int hkdf_expand_label(uint8_t *out, const EVP_MD *digest, |
| 71 | const uint8_t *secret, size_t secret_len, |
| 72 | const uint8_t *label, size_t label_len, |
| 73 | const uint8_t *hash, size_t hash_len, size_t len) { |
| 74 | static const char kTLS13LabelVersion[] = "TLS 1.3, "; |
| 75 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 76 | ScopedCBB cbb; |
| 77 | CBB child; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 78 | uint8_t *hkdf_label; |
| 79 | size_t hkdf_label_len; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 80 | if (!CBB_init(cbb.get(), 2 + 1 + strlen(kTLS13LabelVersion) + label_len + 1 + |
| 81 | hash_len) || |
| 82 | !CBB_add_u16(cbb.get(), len) || |
| 83 | !CBB_add_u8_length_prefixed(cbb.get(), &child) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 84 | !CBB_add_bytes(&child, (const uint8_t *)kTLS13LabelVersion, |
| 85 | strlen(kTLS13LabelVersion)) || |
| 86 | !CBB_add_bytes(&child, label, label_len) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 87 | !CBB_add_u8_length_prefixed(cbb.get(), &child) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 88 | !CBB_add_bytes(&child, hash, hash_len) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 89 | !CBB_finish(cbb.get(), &hkdf_label, &hkdf_label_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int ret = HKDF_expand(out, len, digest, secret, secret_len, hkdf_label, |
| 94 | hkdf_label_len); |
| 95 | OPENSSL_free(hkdf_label); |
| 96 | return ret; |
| 97 | } |
| 98 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 99 | /* derive_secret derives a secret of length |len| and writes the result in |out| |
| 100 | * with the given label and the current base secret and most recently-saved |
| 101 | * handshake context. It returns one on success and zero on error. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 102 | static int derive_secret(SSL_HANDSHAKE *hs, uint8_t *out, size_t len, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 103 | const uint8_t *label, size_t label_len) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 104 | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
| 105 | size_t context_hash_len; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 106 | if (!hs->transcript.GetHash(context_hash, &context_hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 110 | return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret, |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 111 | hs->hash_len, label, label_len, context_hash, |
| 112 | context_hash_len, len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 115 | int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 116 | const uint8_t *traffic_secret, |
| 117 | size_t traffic_secret_len) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 118 | const SSL_SESSION *session = SSL_get_session(ssl); |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 119 | uint16_t version = SSL_SESSION_protocol_version(session); |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 120 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 121 | if (traffic_secret_len > 0xff) { |
| 122 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 123 | return 0; |
| 124 | } |
| 125 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 126 | /* Look up cipher suite properties. */ |
| 127 | const EVP_AEAD *aead; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 128 | size_t discard; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 129 | if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher, |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 130 | version, SSL_is_dtls(ssl))) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 134 | const EVP_MD *digest = SSL_SESSION_get_digest(session); |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 135 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 136 | /* Derive the key. */ |
| 137 | size_t key_len = EVP_AEAD_key_length(aead); |
| 138 | uint8_t key[EVP_AEAD_MAX_KEY_LENGTH]; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 139 | if (!hkdf_expand_label(key, digest, traffic_secret, traffic_secret_len, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 140 | (const uint8_t *)"key", 3, NULL, 0, key_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 144 | /* Derive the IV. */ |
| 145 | size_t iv_len = EVP_AEAD_nonce_length(aead); |
| 146 | uint8_t iv[EVP_AEAD_MAX_NONCE_LENGTH]; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 147 | if (!hkdf_expand_label(iv, digest, traffic_secret, traffic_secret_len, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 148 | (const uint8_t *)"iv", 2, NULL, 0, iv_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 152 | UniquePtr<SSLAEADContext> traffic_aead = SSLAEADContext::Create( |
| 153 | direction, version, SSL_is_dtls(ssl), session->cipher, key, key_len, NULL, |
| 154 | 0, iv, iv_len); |
| 155 | if (!traffic_aead) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | if (direction == evp_aead_open) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 160 | if (!ssl->method->set_read_state(ssl, std::move(traffic_aead))) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | } else { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 164 | if (!ssl->method->set_write_state(ssl, std::move(traffic_aead))) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* Save the traffic secret. */ |
| 170 | if (direction == evp_aead_open) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 171 | OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret, |
| 172 | traffic_secret_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 173 | ssl->s3->read_traffic_secret_len = traffic_secret_len; |
| 174 | } else { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 175 | OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret, |
| 176 | traffic_secret_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 177 | ssl->s3->write_traffic_secret_len = traffic_secret_len; |
| 178 | } |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 183 | static const char kTLS13LabelExporter[] = "exporter master secret"; |
| 184 | static const char kTLS13LabelEarlyExporter[] = "early exporter master secret"; |
| 185 | |
| 186 | static const char kTLS13LabelClientEarlyTraffic[] = |
| 187 | "client early traffic secret"; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 188 | static const char kTLS13LabelClientHandshakeTraffic[] = |
| 189 | "client handshake traffic secret"; |
| 190 | static const char kTLS13LabelServerHandshakeTraffic[] = |
| 191 | "server handshake traffic secret"; |
| 192 | static const char kTLS13LabelClientApplicationTraffic[] = |
| 193 | "client application traffic secret"; |
| 194 | static const char kTLS13LabelServerApplicationTraffic[] = |
| 195 | "server application traffic secret"; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 196 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 197 | int tls13_derive_early_secrets(SSL_HANDSHAKE *hs) { |
| 198 | SSL *const ssl = hs->ssl; |
| 199 | return derive_secret(hs, hs->early_traffic_secret, hs->hash_len, |
| 200 | (const uint8_t *)kTLS13LabelClientEarlyTraffic, |
| 201 | strlen(kTLS13LabelClientEarlyTraffic)) && |
| 202 | ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET", |
| 203 | hs->early_traffic_secret, hs->hash_len) && |
| 204 | derive_secret(hs, ssl->s3->early_exporter_secret, hs->hash_len, |
| 205 | (const uint8_t *)kTLS13LabelEarlyExporter, |
| 206 | strlen(kTLS13LabelEarlyExporter)); |
| 207 | } |
| 208 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 209 | int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 210 | SSL *const ssl = hs->ssl; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 211 | return derive_secret(hs, hs->client_handshake_secret, hs->hash_len, |
| 212 | (const uint8_t *)kTLS13LabelClientHandshakeTraffic, |
| 213 | strlen(kTLS13LabelClientHandshakeTraffic)) && |
| 214 | ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET", |
| 215 | hs->client_handshake_secret, hs->hash_len) && |
| 216 | derive_secret(hs, hs->server_handshake_secret, hs->hash_len, |
| 217 | (const uint8_t *)kTLS13LabelServerHandshakeTraffic, |
| 218 | strlen(kTLS13LabelServerHandshakeTraffic)) && |
| 219 | ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET", |
| 220 | hs->server_handshake_secret, hs->hash_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 221 | } |
| 222 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 223 | int tls13_derive_application_secrets(SSL_HANDSHAKE *hs) { |
| 224 | SSL *const ssl = hs->ssl; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 225 | ssl->s3->exporter_secret_len = hs->hash_len; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 226 | return derive_secret(hs, hs->client_traffic_secret_0, hs->hash_len, |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 227 | (const uint8_t *)kTLS13LabelClientApplicationTraffic, |
| 228 | strlen(kTLS13LabelClientApplicationTraffic)) && |
| 229 | ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0", |
| 230 | hs->client_traffic_secret_0, hs->hash_len) && |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 231 | derive_secret(hs, hs->server_traffic_secret_0, hs->hash_len, |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 232 | (const uint8_t *)kTLS13LabelServerApplicationTraffic, |
| 233 | strlen(kTLS13LabelServerApplicationTraffic)) && |
| 234 | ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0", |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 235 | hs->server_traffic_secret_0, hs->hash_len) && |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 236 | derive_secret(hs, ssl->s3->exporter_secret, hs->hash_len, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 237 | (const uint8_t *)kTLS13LabelExporter, |
| 238 | strlen(kTLS13LabelExporter)); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 239 | } |
| 240 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 241 | static const char kTLS13LabelApplicationTraffic[] = |
| 242 | "application traffic secret"; |
| 243 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 244 | int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 245 | uint8_t *secret; |
| 246 | size_t secret_len; |
| 247 | if (direction == evp_aead_open) { |
| 248 | secret = ssl->s3->read_traffic_secret; |
| 249 | secret_len = ssl->s3->read_traffic_secret_len; |
| 250 | } else { |
| 251 | secret = ssl->s3->write_traffic_secret; |
| 252 | secret_len = ssl->s3->write_traffic_secret_len; |
| 253 | } |
| 254 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 255 | const EVP_MD *digest = SSL_SESSION_get_digest(SSL_get_session(ssl)); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 256 | if (!hkdf_expand_label(secret, digest, secret, secret_len, |
| 257 | (const uint8_t *)kTLS13LabelApplicationTraffic, |
| 258 | strlen(kTLS13LabelApplicationTraffic), NULL, 0, |
| 259 | secret_len)) { |
| 260 | return 0; |
| 261 | } |
| 262 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 263 | return tls13_set_traffic_key(ssl, direction, secret, secret_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 264 | } |
| 265 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 266 | static const char kTLS13LabelResumption[] = "resumption master secret"; |
| 267 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 268 | int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) { |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 269 | if (hs->hash_len > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 270 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 274 | hs->new_session->master_key_length = hs->hash_len; |
| 275 | return derive_secret( |
| 276 | hs, hs->new_session->master_key, hs->new_session->master_key_length, |
| 277 | (const uint8_t *)kTLS13LabelResumption, strlen(kTLS13LabelResumption)); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 278 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 279 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 280 | static const char kTLS13LabelFinished[] = "finished"; |
| 281 | |
| 282 | /* tls13_verify_data sets |out| to be the HMAC of |context| using a derived |
| 283 | * Finished key for both Finished messages and the PSK binder. */ |
| 284 | static int tls13_verify_data(const EVP_MD *digest, uint8_t *out, |
| 285 | size_t *out_len, const uint8_t *secret, |
| 286 | size_t hash_len, uint8_t *context, |
| 287 | size_t context_len) { |
| 288 | uint8_t key[EVP_MAX_MD_SIZE]; |
| 289 | unsigned len; |
| 290 | if (!hkdf_expand_label(key, digest, secret, hash_len, |
| 291 | (const uint8_t *)kTLS13LabelFinished, |
| 292 | strlen(kTLS13LabelFinished), NULL, 0, hash_len) || |
| 293 | HMAC(digest, key, hash_len, context, context_len, out, &len) == NULL) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 294 | return 0; |
| 295 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 296 | *out_len = len; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 297 | return 1; |
| 298 | } |
| 299 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 300 | int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, |
| 301 | int is_server) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 302 | const uint8_t *traffic_secret; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 303 | if (is_server) { |
| 304 | traffic_secret = hs->server_handshake_secret; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 305 | } else { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 306 | traffic_secret = hs->client_handshake_secret; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 309 | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
| 310 | size_t context_hash_len; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 311 | if (!hs->transcript.GetHash(context_hash, &context_hash_len) || |
| 312 | !tls13_verify_data(hs->transcript.Digest(), out, out_len, traffic_secret, |
| 313 | hs->hash_len, context_hash, context_hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 314 | return 0; |
| 315 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 316 | return 1; |
| 317 | } |
| 318 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 319 | int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 320 | const char *label, size_t label_len, |
| 321 | const uint8_t *context, size_t context_len, |
| 322 | int use_context) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 323 | const uint8_t *hash = NULL; |
| 324 | size_t hash_len = 0; |
| 325 | if (use_context) { |
| 326 | hash = context; |
| 327 | hash_len = context_len; |
| 328 | } |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 329 | |
| 330 | const EVP_MD *digest = SSL_SESSION_get_digest(SSL_get_session(ssl)); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 331 | return hkdf_expand_label(out, digest, ssl->s3->exporter_secret, |
| 332 | ssl->s3->exporter_secret_len, (const uint8_t *)label, |
| 333 | label_len, hash, hash_len, out_len); |
| 334 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 335 | |
| 336 | static const char kTLS13LabelPSKBinder[] = "resumption psk binder key"; |
| 337 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 338 | static int tls13_psk_binder(uint8_t *out, const EVP_MD *digest, uint8_t *psk, |
| 339 | size_t psk_len, uint8_t *context, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 340 | size_t context_len, size_t hash_len) { |
| 341 | uint8_t binder_context[EVP_MAX_MD_SIZE]; |
| 342 | unsigned binder_context_len; |
| 343 | if (!EVP_Digest(NULL, 0, binder_context, &binder_context_len, digest, NULL)) { |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | uint8_t early_secret[EVP_MAX_MD_SIZE] = {0}; |
| 348 | size_t early_secret_len; |
| 349 | if (!HKDF_extract(early_secret, &early_secret_len, digest, psk, hash_len, |
| 350 | NULL, 0)) { |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | uint8_t binder_key[EVP_MAX_MD_SIZE] = {0}; |
| 355 | size_t len; |
| 356 | if (!hkdf_expand_label(binder_key, digest, early_secret, hash_len, |
| 357 | (const uint8_t *)kTLS13LabelPSKBinder, |
| 358 | strlen(kTLS13LabelPSKBinder), binder_context, |
| 359 | binder_context_len, hash_len) || |
| 360 | !tls13_verify_data(digest, out, &len, binder_key, hash_len, context, |
| 361 | context_len)) { |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | return 1; |
| 366 | } |
| 367 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 368 | int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len) { |
| 369 | SSL *const ssl = hs->ssl; |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 370 | const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 371 | size_t hash_len = EVP_MD_size(digest); |
| 372 | |
| 373 | if (len < hash_len + 3) { |
| 374 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 378 | ScopedEVP_MD_CTX ctx; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 379 | uint8_t context[EVP_MAX_MD_SIZE]; |
| 380 | unsigned context_len; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 381 | if (!EVP_DigestInit_ex(ctx.get(), digest, NULL) || |
| 382 | !EVP_DigestUpdate(ctx.get(), hs->transcript.buffer_data(), |
| 383 | hs->transcript.buffer_len()) || |
| 384 | !EVP_DigestUpdate(ctx.get(), msg, len - hash_len - 3) || |
| 385 | !EVP_DigestFinal_ex(ctx.get(), context, &context_len)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 389 | uint8_t verify_data[EVP_MAX_MD_SIZE] = {0}; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 390 | if (!tls13_psk_binder(verify_data, digest, ssl->session->master_key, |
| 391 | ssl->session->master_key_length, context, context_len, |
| 392 | hash_len)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 396 | OPENSSL_memcpy(msg + len - hash_len, verify_data, hash_len); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 400 | int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session, |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 401 | const SSLMessage &msg, CBS *binders) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 402 | size_t hash_len = hs->transcript.DigestLen(); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 403 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 404 | /* The message must be large enough to exclude the binders. */ |
| 405 | if (CBS_len(&msg.raw) < CBS_len(binders) + 2) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 406 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 407 | return 0; |
| 408 | } |
| 409 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 410 | /* Hash a ClientHello prefix up to the binders. This includes the header. For |
| 411 | * now, this assumes we only ever verify PSK binders on initial |
| 412 | * ClientHellos. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 413 | uint8_t context[EVP_MAX_MD_SIZE]; |
| 414 | unsigned context_len; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 415 | if (!EVP_Digest(CBS_data(&msg.raw), CBS_len(&msg.raw) - CBS_len(binders) - 2, |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 416 | context, &context_len, hs->transcript.Digest(), NULL)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | uint8_t verify_data[EVP_MAX_MD_SIZE] = {0}; |
| 421 | CBS binder; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 422 | if (!tls13_psk_binder(verify_data, hs->transcript.Digest(), |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 423 | session->master_key, session->master_key_length, |
| 424 | context, context_len, hash_len) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 425 | /* We only consider the first PSK, so compare against the first binder. */ |
| 426 | !CBS_get_u8_length_prefixed(binders, &binder)) { |
| 427 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 428 | return 0; |
| 429 | } |
| 430 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 431 | int binder_ok = |
| 432 | CBS_len(&binder) == hash_len && |
| 433 | CRYPTO_memcmp(CBS_data(&binder), verify_data, hash_len) == 0; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 434 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 435 | binder_ok = 1; |
| 436 | #endif |
| 437 | if (!binder_ok) { |
| 438 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | return 1; |
| 443 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 444 | |
| 445 | } // namespace bssl |