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