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 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | #include <openssl/err.h> |
| 142 | #include <openssl/evp.h> |
| 143 | #include <openssl/hmac.h> |
| 144 | #include <openssl/md5.h> |
| 145 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 146 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | #include <openssl/rand.h> |
| 148 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 149 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 152 | /* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246, |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 153 | * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 154 | * |secret| as the secret. |seed1| through |seed3| are concatenated to form the |
| 155 | * seed parameter. It returns one on success and zero on failure. */ |
| 156 | static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, |
| 157 | const uint8_t *secret, size_t secret_len, |
| 158 | const uint8_t *seed1, size_t seed1_len, |
| 159 | const uint8_t *seed2, size_t seed2_len, |
| 160 | const uint8_t *seed3, size_t seed3_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 161 | HMAC_CTX ctx, ctx_tmp, ctx_init; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 162 | uint8_t A1[EVP_MAX_MD_SIZE]; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 163 | unsigned A1_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 164 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 166 | size_t chunk = EVP_MD_size(md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 168 | HMAC_CTX_init(&ctx); |
| 169 | HMAC_CTX_init(&ctx_tmp); |
| 170 | HMAC_CTX_init(&ctx_init); |
| 171 | if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) || |
| 172 | !HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 173 | !HMAC_Update(&ctx, seed1, seed1_len) || |
| 174 | !HMAC_Update(&ctx, seed2, seed2_len) || |
| 175 | !HMAC_Update(&ctx, seed3, seed3_len) || |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 176 | !HMAC_Final(&ctx, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 177 | goto err; |
| 178 | } |
| 179 | |
| 180 | for (;;) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 181 | unsigned len; |
| 182 | uint8_t hmac[EVP_MAX_MD_SIZE]; |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 183 | if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
| 184 | !HMAC_Update(&ctx, A1, A1_len) || |
| 185 | /* Save a copy of |ctx| to compute the next A1 value below. */ |
| 186 | (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) || |
| 187 | !HMAC_Update(&ctx, seed1, seed1_len) || |
| 188 | !HMAC_Update(&ctx, seed2, seed2_len) || |
| 189 | !HMAC_Update(&ctx, seed3, seed3_len) || |
| 190 | !HMAC_Final(&ctx, hmac, &len)) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 191 | goto err; |
| 192 | } |
| 193 | assert(len == chunk); |
| 194 | |
| 195 | /* XOR the result into |out|. */ |
| 196 | if (len > out_len) { |
| 197 | len = out_len; |
| 198 | } |
| 199 | unsigned i; |
| 200 | for (i = 0; i < len; i++) { |
| 201 | out[i] ^= hmac[i]; |
| 202 | } |
| 203 | out += len; |
| 204 | out_len -= len; |
| 205 | |
| 206 | if (out_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 207 | break; |
| 208 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 209 | |
| 210 | /* Calculate the next A1 value. */ |
| 211 | if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) { |
| 212 | goto err; |
| 213 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | ret = 1; |
| 217 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | err: |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 219 | HMAC_CTX_cleanup(&ctx); |
| 220 | HMAC_CTX_cleanup(&ctx_tmp); |
| 221 | HMAC_CTX_cleanup(&ctx_init); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 222 | OPENSSL_cleanse(A1, sizeof(A1)); |
| 223 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | } |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame] | 225 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame] | 226 | static int tls1_prf(const SSL *ssl, uint8_t *out, size_t out_len, |
| 227 | const uint8_t *secret, size_t secret_len, const char *label, |
| 228 | size_t label_len, const uint8_t *seed1, size_t seed1_len, |
| 229 | const uint8_t *seed2, size_t seed2_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 230 | if (out_len == 0) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 231 | return 1; |
| 232 | } |
| 233 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 234 | memset(out, 0, out_len); |
| 235 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 236 | uint32_t algorithm_prf = ssl_get_algorithm_prf(ssl); |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 237 | if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) { |
| 238 | /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and |
| 239 | * MD5, MD5 first. */ |
| 240 | size_t secret_half = secret_len - (secret_len / 2); |
| 241 | if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, |
| 242 | (const uint8_t *)label, label_len, seed1, seed1_len, seed2, |
| 243 | seed2_len)) { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /* Note that, if |secret_len| is odd, the two halves share a byte. */ |
| 248 | secret = secret + (secret_len - secret_half); |
| 249 | secret_len = secret_half; |
| 250 | } |
| 251 | |
| 252 | if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf), |
| 253 | secret, secret_len, (const uint8_t *)label, label_len, |
| 254 | seed1, seed1_len, seed2, seed2_len)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 255 | return 0; |
| 256 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 258 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 259 | } |
| 260 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame^] | 261 | static int tls1_setup_key_block(SSL_HANDSHAKE *hs) { |
| 262 | SSL *const ssl = hs->ssl; |
| 263 | if (hs->key_block_len != 0) { |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | SSL_SESSION *session = ssl->session; |
| 268 | if (ssl->s3->new_session != NULL) { |
| 269 | session = ssl->s3->new_session; |
| 270 | } |
| 271 | |
| 272 | const EVP_AEAD *aead = NULL; |
| 273 | size_t mac_secret_len, fixed_iv_len; |
| 274 | if (session->cipher == NULL || |
| 275 | !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, |
| 276 | session->cipher, ssl3_protocol_version(ssl))) { |
| 277 | OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 278 | return 0; |
| 279 | } |
| 280 | size_t key_len = EVP_AEAD_key_length(aead); |
| 281 | if (mac_secret_len > 0) { |
| 282 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 283 | * key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 284 | * bytes and initial implicit IV. */ |
| 285 | if (key_len < mac_secret_len + fixed_iv_len) { |
| 286 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 287 | return 0; |
| 288 | } |
| 289 | key_len -= mac_secret_len + fixed_iv_len; |
| 290 | } |
| 291 | |
| 292 | assert(mac_secret_len < 256); |
| 293 | assert(key_len < 256); |
| 294 | assert(fixed_iv_len < 256); |
| 295 | |
| 296 | ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len; |
| 297 | ssl->s3->tmp.new_key_len = (uint8_t)key_len; |
| 298 | ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len; |
| 299 | |
| 300 | size_t key_block_len = SSL_get_key_block_len(ssl); |
| 301 | |
| 302 | uint8_t *keyblock = OPENSSL_malloc(key_block_len); |
| 303 | if (keyblock == NULL) { |
| 304 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | if (!SSL_generate_key_block(ssl, keyblock, key_block_len)) { |
| 309 | OPENSSL_free(keyblock); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | assert(key_block_len < 256); |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame^] | 314 | hs->key_block_len = (uint8_t)key_block_len; |
| 315 | hs->key_block = keyblock; |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 316 | return 1; |
| 317 | } |
| 318 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame^] | 319 | int tls1_change_cipher_state(SSL_HANDSHAKE *hs, int which) { |
| 320 | SSL *const ssl = hs->ssl; |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 321 | /* Ensure the key block is set up. */ |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame^] | 322 | if (!tls1_setup_key_block(hs)) { |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 326 | /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we |
| 327 | * need to update the read cipherspec. Otherwise we have just written one. */ |
| 328 | const char is_read = (which & SSL3_CC_READ) != 0; |
| 329 | /* use_client_keys is true if we wish to use the keys for the "client write" |
| 330 | * direction. This is the case if we're a client sending a ChangeCipherSpec, |
| 331 | * or a server reading a client's ChangeCipherSpec. */ |
| 332 | const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || |
| 333 | which == SSL3_CHANGE_CIPHER_SERVER_READ; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 335 | size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len; |
| 336 | size_t key_len = ssl->s3->tmp.new_key_len; |
| 337 | size_t iv_len = ssl->s3->tmp.new_fixed_iv_len; |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame^] | 338 | assert((mac_secret_len + key_len + iv_len) * 2 == hs->key_block_len); |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame^] | 340 | const uint8_t *key_data = hs->key_block; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 341 | const uint8_t *client_write_mac_secret = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 342 | key_data += mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 343 | const uint8_t *server_write_mac_secret = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 344 | key_data += mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 345 | const uint8_t *client_write_key = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 346 | key_data += key_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 347 | const uint8_t *server_write_key = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 348 | key_data += key_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 349 | const uint8_t *client_write_iv = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 350 | key_data += iv_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 351 | const uint8_t *server_write_iv = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 352 | key_data += iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 354 | const uint8_t *mac_secret, *key, *iv; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 355 | if (use_client_keys) { |
| 356 | mac_secret = client_write_mac_secret; |
| 357 | key = client_write_key; |
| 358 | iv = client_write_iv; |
| 359 | } else { |
| 360 | mac_secret = server_write_mac_secret; |
| 361 | key = server_write_key; |
| 362 | iv = server_write_iv; |
| 363 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | |
David Benjamin | b9e4fa5 | 2015-12-29 23:58:17 -0500 | [diff] [blame] | 365 | SSL_AEAD_CTX *aead_ctx = |
| 366 | SSL_AEAD_CTX_new(is_read ? evp_aead_open : evp_aead_seal, |
| 367 | ssl3_protocol_version(ssl), ssl->s3->tmp.new_cipher, key, |
| 368 | key_len, mac_secret, mac_secret_len, iv, iv_len); |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 369 | if (aead_ctx == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 370 | return 0; |
| 371 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 373 | if (is_read) { |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 374 | return ssl->method->set_read_state(ssl, aead_ctx); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 375 | } |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 376 | |
| 377 | return ssl->method->set_write_state(ssl, aead_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 378 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
David Benjamin | 928f32a | 2015-12-29 23:32:29 -0500 | [diff] [blame] | 380 | size_t SSL_get_key_block_len(const SSL *ssl) { |
| 381 | return 2 * ((size_t)ssl->s3->tmp.new_mac_secret_len + |
| 382 | (size_t)ssl->s3->tmp.new_key_len + |
| 383 | (size_t)ssl->s3->tmp.new_fixed_iv_len); |
| 384 | } |
| 385 | |
| 386 | int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) { |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 387 | return ssl->s3->enc_method->prf( |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 388 | ssl, out, out_len, SSL_get_session(ssl)->master_key, |
| 389 | SSL_get_session(ssl)->master_key_length, TLS_MD_KEY_EXPANSION_CONST, |
David Benjamin | 928f32a | 2015-12-29 23:32:29 -0500 | [diff] [blame] | 390 | TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE, |
| 391 | ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 392 | } |
| 393 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 394 | static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 395 | size_t max_out) { |
| 396 | int ret = 0; |
| 397 | EVP_MD_CTX ctx_copy; |
| 398 | EVP_MD_CTX_init(&ctx_copy); |
| 399 | |
| 400 | if (EVP_MD_CTX_size(ctx) > max_out) { |
| 401 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 402 | goto err; |
| 403 | } |
| 404 | unsigned len; |
| 405 | if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) || |
| 406 | !EVP_DigestFinal_ex(&ctx_copy, out, &len)) { |
| 407 | goto err; |
| 408 | } |
| 409 | assert(len == EVP_MD_CTX_size(ctx)); |
| 410 | |
| 411 | *out_len = len; |
| 412 | ret = 1; |
| 413 | |
| 414 | err: |
| 415 | EVP_MD_CTX_cleanup(&ctx_copy); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 416 | return ret; |
| 417 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 419 | /* tls1_handshake_digest calculates the current handshake hash and writes it to |
| 420 | * |out|, which has space for |out_len| bytes. It returns the number of bytes |
| 421 | * written or -1 in the event of an error. This function works on a copy of the |
| 422 | * underlying digests so can be called multiple times and prior to the final |
| 423 | * update etc. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 424 | int tls1_handshake_digest(SSL *ssl, uint8_t *out, size_t out_len) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 425 | size_t md5_len = 0; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 426 | if (EVP_MD_CTX_md(&ssl->s3->handshake_md5) != NULL && |
| 427 | !append_digest(&ssl->s3->handshake_md5, out, &md5_len, out_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 428 | return -1; |
| 429 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 430 | |
| 431 | size_t len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 432 | if (!append_digest(&ssl->s3->handshake_hash, out + md5_len, &len, |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 433 | out_len - md5_len)) { |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | return (int)(md5_len + len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 438 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame] | 440 | static int tls1_final_finish_mac(SSL *ssl, int from_server, uint8_t *out) { |
David Benjamin | e76ccae | 2015-05-26 17:41:50 -0400 | [diff] [blame] | 441 | /* At this point, the handshake should have released the handshake buffer on |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 442 | * its own. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 443 | assert(ssl->s3->handshake_buffer == NULL); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 445 | const char *label = TLS_MD_CLIENT_FINISH_CONST; |
| 446 | size_t label_len = TLS_MD_SERVER_FINISH_CONST_SIZE; |
| 447 | if (from_server) { |
| 448 | label = TLS_MD_SERVER_FINISH_CONST; |
| 449 | label_len = TLS_MD_SERVER_FINISH_CONST_SIZE; |
| 450 | } |
| 451 | |
| 452 | uint8_t buf[EVP_MAX_MD_SIZE]; |
| 453 | int digests_len = tls1_handshake_digest(ssl, buf, sizeof(buf)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | if (digests_len < 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 455 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 456 | } |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 457 | |
| 458 | static const size_t kFinishedLen = 12; |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 459 | if (!ssl->s3->enc_method->prf(ssl, out, kFinishedLen, |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 460 | SSL_get_session(ssl)->master_key, |
| 461 | SSL_get_session(ssl)->master_key_length, label, |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 462 | label_len, buf, digests_len, NULL, 0)) { |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | return (int)kFinishedLen; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 468 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 469 | int tls1_generate_master_secret(SSL *ssl, uint8_t *out, |
| 470 | const uint8_t *premaster, |
David Benjamin | 31b1d81 | 2014-12-23 10:01:09 -0500 | [diff] [blame] | 471 | size_t premaster_len) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 472 | if (ssl->s3->tmp.extended_master_secret) { |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 473 | uint8_t digests[EVP_MAX_MD_SIZE]; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 474 | int digests_len = tls1_handshake_digest(ssl, digests, sizeof(digests)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | if (digests_len == -1) { |
| 476 | return 0; |
| 477 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 478 | |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 479 | if (!ssl->s3->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 480 | premaster_len, |
| 481 | TLS_MD_EXTENDED_MASTER_SECRET_CONST, |
| 482 | TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, |
| 483 | digests, digests_len, NULL, 0)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 484 | return 0; |
| 485 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 486 | } else { |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 487 | if (!ssl->s3->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 488 | premaster_len, TLS_MD_MASTER_SECRET_CONST, |
| 489 | TLS_MD_MASTER_SECRET_CONST_SIZE, |
| 490 | ssl->s3->client_random, SSL3_RANDOM_SIZE, |
| 491 | ssl->s3->server_random, SSL3_RANDOM_SIZE)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 492 | return 0; |
| 493 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 494 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | return SSL3_MASTER_SECRET_SIZE; |
| 497 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 499 | int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 500 | const char *label, size_t label_len, |
| 501 | const uint8_t *context, size_t context_len, |
| 502 | int use_context) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 503 | if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) { |
David Benjamin | d6e95ee | 2015-05-03 15:34:35 -0400 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
David Benjamin | 7bb1d29 | 2016-11-01 19:45:06 -0400 | [diff] [blame] | 507 | /* Exporters may not be used in the middle of a renegotiation. */ |
| 508 | if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) { |
| 509 | return 0; |
| 510 | } |
| 511 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 512 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 513 | return tls13_export_keying_material(ssl, out, out_len, label, label_len, |
| 514 | context, context_len, use_context); |
| 515 | } |
| 516 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 517 | size_t seed_len = 2 * SSL3_RANDOM_SIZE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 518 | if (use_context) { |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 519 | if (context_len >= 1u << 16) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 520 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 521 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 522 | } |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 523 | seed_len += 2 + context_len; |
| 524 | } |
| 525 | uint8_t *seed = OPENSSL_malloc(seed_len); |
| 526 | if (seed == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 527 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 528 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 529 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 531 | memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 532 | memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 533 | if (use_context) { |
| 534 | seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8); |
| 535 | seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len; |
| 536 | memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 537 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 539 | int ret = |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 540 | ssl->s3->enc_method->prf(ssl, out, out_len, |
| 541 | SSL_get_session(ssl)->master_key, |
| 542 | SSL_get_session(ssl)->master_key_length, label, |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 543 | label_len, seed, seed_len, NULL, 0); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 544 | OPENSSL_free(seed); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 545 | return ret; |
| 546 | } |
| 547 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame] | 548 | const SSL3_ENC_METHOD TLSv1_enc_data = { |
| 549 | tls1_prf, |
| 550 | tls1_final_finish_mac, |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame] | 551 | }; |