Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2005 Nokia. All rights reserved. |
| 112 | * |
| 113 | * The portions of the attached software ("Contribution") is developed by |
| 114 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 115 | * license. |
| 116 | * |
| 117 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 118 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 119 | * support (see RFC 4279) to OpenSSL. |
| 120 | * |
| 121 | * No patent licenses or other rights except those expressly stated in |
| 122 | * the OpenSSL open source license shall be deemed granted or received |
| 123 | * expressly, by implication, estoppel, or otherwise. |
| 124 | * |
| 125 | * No assurances are provided by Nokia that the Contribution does not |
| 126 | * infringe the patent or other intellectual property rights of any third |
| 127 | * party or that the license provides you with all the necessary rights |
| 128 | * to make use of the Contribution. |
| 129 | * |
| 130 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 131 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 132 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 133 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 134 | * OTHERWISE. */ |
| 135 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 136 | #include <openssl/ssl.h> |
| 137 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 139 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 141 | #include <utility> |
| 142 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | #include <openssl/err.h> |
| 144 | #include <openssl/evp.h> |
| 145 | #include <openssl/hmac.h> |
| 146 | #include <openssl/md5.h> |
| 147 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 148 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | #include <openssl/rand.h> |
| 150 | |
Adam Langley | 37c6eb4 | 2018-01-05 13:59:09 -0800 | [diff] [blame] | 151 | #include "../crypto/fipsmodule/tls/internal.h" |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 152 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 153 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 156 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 157 | |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 158 | bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out, |
| 159 | Span<const uint8_t> secret, Span<const char> label, |
| 160 | Span<const uint8_t> seed1, Span<const uint8_t> seed2) { |
Adam Langley | 37c6eb4 | 2018-01-05 13:59:09 -0800 | [diff] [blame] | 161 | return 1 == CRYPTO_tls1_prf(digest, out.data(), out.size(), secret.data(), |
| 162 | secret.size(), label.data(), label.size(), |
| 163 | seed1.data(), seed1.size(), seed2.data(), |
| 164 | seed2.size()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | } |
| 166 | |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 167 | static bool get_key_block_lengths(const SSL *ssl, size_t *out_mac_secret_len, |
| 168 | size_t *out_key_len, size_t *out_iv_len, |
| 169 | const SSL_CIPHER *cipher) { |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 170 | const EVP_AEAD *aead = NULL; |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 171 | if (!ssl_cipher_get_evp_aead(&aead, out_mac_secret_len, out_iv_len, cipher, |
| 172 | ssl_protocol_version(ssl), SSL_is_dtls(ssl))) { |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 173 | OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 174 | return false; |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 175 | } |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 176 | |
| 177 | *out_key_len = EVP_AEAD_key_length(aead); |
| 178 | if (*out_mac_secret_len > 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 179 | // For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 180 | // key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 181 | // bytes and initial implicit IV. |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 182 | if (*out_key_len < *out_mac_secret_len + *out_iv_len) { |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 183 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 184 | return false; |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 185 | } |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 186 | *out_key_len -= *out_mac_secret_len + *out_iv_len; |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 187 | } |
| 188 | |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 189 | return true; |
| 190 | } |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 191 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 192 | static bool generate_key_block(const SSL *ssl, Span<uint8_t> out, |
| 193 | const SSL_SESSION *session) { |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 194 | auto secret = MakeConstSpan(session->secret, session->secret_length); |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 195 | static const char kLabel[] = "key expansion"; |
| 196 | auto label = MakeConstSpan(kLabel, sizeof(kLabel) - 1); |
| 197 | |
| 198 | const EVP_MD *digest = ssl_session_get_digest(session); |
| 199 | // Note this function assumes that |session|'s key material corresponds to |
| 200 | // |ssl->s3->client_random| and |ssl->s3->server_random|. |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 201 | return tls1_prf(digest, out, secret, label, ssl->s3->server_random, |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 202 | ssl->s3->client_random); |
| 203 | } |
| 204 | |
| 205 | bool tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction, |
| 206 | Array<uint8_t> *key_block_cache, |
| 207 | const SSL_SESSION *session, |
| 208 | Span<const uint8_t> iv_override) { |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 209 | size_t mac_secret_len, key_len, iv_len; |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 210 | if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &iv_len, |
| 211 | session->cipher)) { |
| 212 | return false; |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 215 | // Ensure that |key_block_cache| is set up. |
| 216 | const size_t key_block_size = 2 * (mac_secret_len + key_len + iv_len); |
| 217 | if (key_block_cache->empty()) { |
| 218 | if (!key_block_cache->Init(key_block_size) || |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 219 | !generate_key_block(ssl, MakeSpan(*key_block_cache), session)) { |
| 220 | return false; |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 221 | } |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 222 | } |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 223 | assert(key_block_cache->size() == key_block_size); |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 225 | Span<const uint8_t> key_block = *key_block_cache; |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 226 | Span<const uint8_t> mac_secret, key, iv; |
| 227 | if (direction == (ssl->server ? evp_aead_open : evp_aead_seal)) { |
| 228 | // Use the client write (server read) keys. |
| 229 | mac_secret = key_block.subspan(0, mac_secret_len); |
| 230 | key = key_block.subspan(2 * mac_secret_len, key_len); |
| 231 | iv = key_block.subspan(2 * mac_secret_len + 2 * key_len, iv_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 232 | } else { |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 233 | // Use the server write (client read) keys. |
| 234 | mac_secret = key_block.subspan(mac_secret_len, mac_secret_len); |
| 235 | key = key_block.subspan(2 * mac_secret_len + key_len, key_len); |
| 236 | iv = key_block.subspan(2 * mac_secret_len + 2 * key_len + iv_len, iv_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 237 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 239 | if (!iv_override.empty()) { |
| 240 | if (iv_override.size() != iv_len) { |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 241 | return false; |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 242 | } |
| 243 | iv = iv_override; |
| 244 | } |
| 245 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 246 | UniquePtr<SSLAEADContext> aead_ctx = |
| 247 | SSLAEADContext::Create(direction, ssl->version, SSL_is_dtls(ssl), |
| 248 | session->cipher, key, mac_secret, iv); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 249 | if (!aead_ctx) { |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 250 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 251 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
David Benjamin | e58f8a6 | 2017-09-21 19:07:15 -0400 | [diff] [blame] | 253 | if (direction == evp_aead_open) { |
David Benjamin | b092192 | 2020-02-20 12:33:28 -0500 | [diff] [blame] | 254 | return ssl->method->set_read_state(ssl, ssl_encryption_application, |
David Benjamin | 5298ef9 | 2020-03-13 12:17:30 -0400 | [diff] [blame] | 255 | std::move(aead_ctx), |
| 256 | /*secret_for_quic=*/{}); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 257 | } |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 258 | |
David Benjamin | b092192 | 2020-02-20 12:33:28 -0500 | [diff] [blame] | 259 | return ssl->method->set_write_state(ssl, ssl_encryption_application, |
David Benjamin | 5298ef9 | 2020-03-13 12:17:30 -0400 | [diff] [blame] | 260 | std::move(aead_ctx), |
| 261 | /*secret_for_quic=*/{}); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 262 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 264 | bool tls1_change_cipher_state(SSL_HANDSHAKE *hs, |
| 265 | evp_aead_direction_t direction) { |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 266 | return tls1_configure_aead(hs->ssl, direction, &hs->key_block, |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 267 | ssl_handshake_session(hs), {}); |
Adam Langley | ab5a947 | 2018-01-25 15:38:20 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 270 | int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out, |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 271 | Span<const uint8_t> premaster) { |
| 272 | static const char kMasterSecretLabel[] = "master secret"; |
| 273 | static const char kExtendedMasterSecretLabel[] = "extended master secret"; |
| 274 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 275 | const SSL *ssl = hs->ssl; |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 276 | auto out_span = MakeSpan(out, SSL3_MASTER_SECRET_SIZE); |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 277 | if (hs->extended_master_secret) { |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 278 | auto label = MakeConstSpan(kExtendedMasterSecretLabel, |
| 279 | sizeof(kExtendedMasterSecretLabel) - 1); |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 280 | uint8_t digests[EVP_MAX_MD_SIZE]; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 281 | size_t digests_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 282 | if (!hs->transcript.GetHash(digests, &digests_len) || |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 283 | !tls1_prf(hs->transcript.Digest(), out_span, premaster, label, |
| 284 | MakeConstSpan(digests, digests_len), {})) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 285 | return 0; |
| 286 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 287 | } else { |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 288 | auto label = |
| 289 | MakeConstSpan(kMasterSecretLabel, sizeof(kMasterSecretLabel) - 1); |
David Benjamin | 9bb15f5 | 2018-06-26 00:07:40 -0400 | [diff] [blame] | 290 | if (!tls1_prf(hs->transcript.Digest(), out_span, premaster, label, |
| 291 | ssl->s3->client_random, ssl->s3->server_random)) { |
| 292 | return 0; |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 293 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 294 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 296 | return SSL3_MASTER_SECRET_SIZE; |
| 297 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 298 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 299 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 300 | |
| 301 | using namespace bssl; |
| 302 | |
| 303 | size_t SSL_get_key_block_len(const SSL *ssl) { |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 304 | // See |SSL_generate_key_block|. |
David Benjamin | 19d6ec9 | 2022-10-07 17:58:46 -0400 | [diff] [blame] | 305 | if (SSL_in_init(ssl) || ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
David Benjamin | 71ea6b1 | 2017-10-15 22:08:47 -0400 | [diff] [blame] | 309 | size_t mac_secret_len, key_len, fixed_iv_len; |
| 310 | if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &fixed_iv_len, |
| 311 | SSL_get_current_cipher(ssl))) { |
| 312 | ERR_clear_error(); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | return 2 * (mac_secret_len + key_len + fixed_iv_len); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) { |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 320 | // Which cipher state to use is ambiguous during a handshake. In particular, |
| 321 | // there are points where read and write states are from different epochs. |
| 322 | // During a handshake, before ChangeCipherSpec, the encryption states may not |
| 323 | // match |ssl->s3->client_random| and |ssl->s3->server_random|. |
David Benjamin | 19d6ec9 | 2022-10-07 17:58:46 -0400 | [diff] [blame] | 324 | if (SSL_in_init(ssl) || ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 325 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 326 | return 0; |
| 327 | } |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 328 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 329 | return generate_key_block(ssl, MakeSpan(out, out_len), SSL_get_session(ssl)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 330 | } |
| 331 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 332 | int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 333 | const char *label, size_t label_len, |
| 334 | const uint8_t *context, size_t context_len, |
| 335 | int use_context) { |
Nick Harper | 1045897 | 2022-10-26 15:28:08 -0400 | [diff] [blame] | 336 | // In TLS 1.3, the exporter may be used whenever the secret has been derived. |
| 337 | if (ssl->s3->have_version && ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 338 | if (ssl->s3->exporter_secret_len == 0) { |
| 339 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 340 | return 0; |
| 341 | } |
David Benjamin | 650d8c3 | 2017-12-08 17:05:06 -0500 | [diff] [blame] | 342 | if (!use_context) { |
| 343 | context = nullptr; |
| 344 | context_len = 0; |
| 345 | } |
| 346 | return tls13_export_keying_material( |
| 347 | ssl, MakeSpan(out, out_len), |
| 348 | MakeConstSpan(ssl->s3->exporter_secret, ssl->s3->exporter_secret_len), |
| 349 | MakeConstSpan(label, label_len), MakeConstSpan(context, context_len)); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Nick Harper | 1045897 | 2022-10-26 15:28:08 -0400 | [diff] [blame] | 352 | // Exporters may be used in False Start, where the handshake has progressed |
| 353 | // enough. Otherwise, they may not be used during a handshake. |
| 354 | if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) { |
| 355 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 356 | return 0; |
| 357 | } |
| 358 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 359 | size_t seed_len = 2 * SSL3_RANDOM_SIZE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 360 | if (use_context) { |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 361 | if (context_len >= 1u << 16) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 362 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 363 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 364 | } |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 365 | seed_len += 2 + context_len; |
| 366 | } |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 367 | Array<uint8_t> seed; |
| 368 | if (!seed.Init(seed_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 369 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 370 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 373 | OPENSSL_memcpy(seed.data(), ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 374 | OPENSSL_memcpy(seed.data() + SSL3_RANDOM_SIZE, ssl->s3->server_random, |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 375 | SSL3_RANDOM_SIZE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 376 | if (use_context) { |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 377 | seed[2 * SSL3_RANDOM_SIZE] = static_cast<uint8_t>(context_len >> 8); |
| 378 | seed[2 * SSL3_RANDOM_SIZE + 1] = static_cast<uint8_t>(context_len); |
| 379 | OPENSSL_memcpy(seed.data() + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 382 | const SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 383 | const EVP_MD *digest = ssl_session_get_digest(session); |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 384 | return tls1_prf(digest, MakeSpan(out, out_len), |
| 385 | MakeConstSpan(session->secret, session->secret_length), |
| 386 | MakeConstSpan(label, label_len), seed, {}); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 387 | } |