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 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 137 | #include <stdio.h> |
| 138 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | #include <openssl/err.h> |
| 141 | #include <openssl/evp.h> |
| 142 | #include <openssl/hmac.h> |
| 143 | #include <openssl/md5.h> |
| 144 | #include <openssl/mem.h> |
| 145 | #include <openssl/obj.h> |
| 146 | #include <openssl/rand.h> |
| 147 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 148 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 151 | /* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246, |
| 152 | * section 5. It writes |out_len| bytes to |out|, using |md| as the hash and |
| 153 | * |secret| as the secret. |seed1| through |seed3| are concatenated to form the |
| 154 | * seed parameter. It returns one on success and zero on failure. */ |
| 155 | static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, |
| 156 | const uint8_t *secret, size_t secret_len, |
| 157 | const uint8_t *seed1, size_t seed1_len, |
| 158 | const uint8_t *seed2, size_t seed2_len, |
| 159 | const uint8_t *seed3, size_t seed3_len) { |
| 160 | size_t chunk; |
| 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 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 166 | 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) || |
| 173 | (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) || |
| 174 | (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) || |
| 175 | (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len)) || |
| 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 | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 181 | /* Reinit mac contexts. */ |
| 182 | if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
| 183 | !HMAC_Update(&ctx, A1, A1_len) || |
| 184 | (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) || |
| 185 | (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) || |
| 186 | (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) || |
| 187 | (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | goto err; |
| 189 | } |
| 190 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 191 | if (out_len > chunk) { |
| 192 | unsigned len; |
| 193 | if (!HMAC_Final(&ctx, out, &len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 194 | goto err; |
| 195 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 196 | assert(len == chunk); |
| 197 | out += len; |
| 198 | out_len -= len; |
| 199 | /* Calculate the next A1 value. */ |
| 200 | if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 201 | goto err; |
| 202 | } |
| 203 | } else { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 204 | /* Last chunk. */ |
| 205 | if (!HMAC_Final(&ctx, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 206 | goto err; |
| 207 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 208 | memcpy(out, A1, out_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ret = 1; |
| 214 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | err: |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 216 | HMAC_CTX_cleanup(&ctx); |
| 217 | HMAC_CTX_cleanup(&ctx_tmp); |
| 218 | HMAC_CTX_cleanup(&ctx_init); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 219 | OPENSSL_cleanse(A1, sizeof(A1)); |
| 220 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | } |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame] | 222 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 223 | int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret, |
| 224 | size_t secret_len, const char *label, size_t label_len, |
| 225 | const uint8_t *seed1, size_t seed1_len, |
| 226 | const uint8_t *seed2, size_t seed2_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 227 | size_t idx, len, count, i; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | const uint8_t *S1; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 229 | uint32_t m; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 230 | const EVP_MD *md; |
| 231 | int ret = 0; |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 232 | uint8_t *tmp; |
| 233 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 234 | if (out_len == 0) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* Allocate a temporary buffer. */ |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 239 | tmp = OPENSSL_malloc(out_len); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 240 | if (tmp == NULL) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 241 | OPENSSL_PUT_ERROR(SSL, tls1_prf, ERR_R_MALLOC_FAILURE); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 242 | return 0; |
| 243 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 244 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 245 | /* Count number of digests and partition |secret| evenly. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 246 | count = 0; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 247 | for (idx = 0; ssl_get_handshake_digest(&m, &md, idx); idx++) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 248 | if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 249 | count++; |
| 250 | } |
| 251 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 252 | /* TODO(davidben): The only case where count isn't 1 is the old MD5/SHA-1 |
| 253 | * combination. The logic around multiple handshake digests can probably be |
| 254 | * simplified. */ |
| 255 | assert(count == 1 || count == 2); |
| 256 | len = secret_len / count; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | if (count == 1) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 258 | secret_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 259 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 260 | S1 = secret; |
| 261 | memset(out, 0, out_len); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 262 | for (idx = 0; ssl_get_handshake_digest(&m, &md, idx); idx++) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 263 | if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 264 | /* If |count| is 2 and |secret_len| is odd, |secret| is partitioned into |
| 265 | * two halves with an overlapping byte. */ |
| 266 | if (!tls1_P_hash(tmp, out_len, md, S1, len + (secret_len & 1), |
| 267 | (const uint8_t *)label, label_len, seed1, seed1_len, |
| 268 | seed2, seed2_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | goto err; |
| 270 | } |
| 271 | S1 += len; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 272 | for (i = 0; i < out_len; i++) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 273 | out[i] ^= tmp[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | ret = 1; |
| 278 | |
| 279 | err: |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 280 | OPENSSL_cleanse(tmp, out_len); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 281 | OPENSSL_free(tmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 282 | return ret; |
| 283 | } |
| 284 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 285 | static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 286 | return s->enc_method->prf(s, out, out_len, s->session->master_key, |
| 287 | s->session->master_key_length, |
| 288 | TLS_MD_KEY_EXPANSION_CONST, |
| 289 | TLS_MD_KEY_EXPANSION_CONST_SIZE, |
| 290 | s->s3->server_random, SSL3_RANDOM_SIZE, |
| 291 | s->s3->client_random, |
| 292 | SSL3_RANDOM_SIZE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 293 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 295 | int tls1_change_cipher_state(SSL *s, int which) { |
| 296 | /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we |
| 297 | * need to update the read cipherspec. Otherwise we have just written one. */ |
| 298 | const char is_read = (which & SSL3_CC_READ) != 0; |
| 299 | /* use_client_keys is true if we wish to use the keys for the "client write" |
| 300 | * direction. This is the case if we're a client sending a ChangeCipherSpec, |
| 301 | * or a server reading a client's ChangeCipherSpec. */ |
| 302 | const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || |
| 303 | which == SSL3_CHANGE_CIPHER_SERVER_READ; |
| 304 | const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret; |
| 305 | const uint8_t *client_write_key, *server_write_key, *key; |
| 306 | const uint8_t *client_write_iv, *server_write_iv, *iv; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | const EVP_AEAD *aead = s->s3->tmp.new_aead; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 308 | size_t key_len, iv_len, mac_secret_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 309 | const uint8_t *key_data; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 311 | /* Reset sequence number to zero. */ |
| 312 | if (!SSL_IS_DTLS(s)) { |
| 313 | memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8); |
| 314 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 316 | mac_secret_len = s->s3->tmp.new_mac_secret_len; |
| 317 | iv_len = s->s3->tmp.new_fixed_iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 319 | if (aead == NULL) { |
| 320 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | key_len = EVP_AEAD_key_length(aead); |
| 325 | if (mac_secret_len > 0) { |
| 326 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher |
| 327 | * suites) the key length reported by |EVP_AEAD_key_length| will |
| 328 | * include the MAC and IV key bytes. */ |
| 329 | if (key_len < mac_secret_len + iv_len) { |
| 330 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 331 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 332 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 333 | key_len -= mac_secret_len + iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 334 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 336 | key_data = s->s3->tmp.key_block; |
| 337 | client_write_mac_secret = key_data; |
| 338 | key_data += mac_secret_len; |
| 339 | server_write_mac_secret = key_data; |
| 340 | key_data += mac_secret_len; |
| 341 | client_write_key = key_data; |
| 342 | key_data += key_len; |
| 343 | server_write_key = key_data; |
| 344 | key_data += key_len; |
| 345 | client_write_iv = key_data; |
| 346 | key_data += iv_len; |
| 347 | server_write_iv = key_data; |
| 348 | key_data += iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 350 | if (use_client_keys) { |
| 351 | mac_secret = client_write_mac_secret; |
| 352 | key = client_write_key; |
| 353 | iv = client_write_iv; |
| 354 | } else { |
| 355 | mac_secret = server_write_mac_secret; |
| 356 | key = server_write_key; |
| 357 | iv = server_write_iv; |
| 358 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 359 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 360 | if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) { |
| 361 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 362 | return 0; |
| 363 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 365 | if (is_read) { |
| 366 | SSL_AEAD_CTX_free(s->aead_read_ctx); |
| 367 | s->aead_read_ctx = SSL_AEAD_CTX_new( |
| 368 | evp_aead_open, ssl3_version_from_wire(s, s->version), |
| 369 | s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv, |
| 370 | iv_len); |
| 371 | return s->aead_read_ctx != NULL; |
| 372 | } else { |
| 373 | SSL_AEAD_CTX_free(s->aead_write_ctx); |
| 374 | s->aead_write_ctx = SSL_AEAD_CTX_new( |
| 375 | evp_aead_seal, ssl3_version_from_wire(s, s->version), |
| 376 | s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv, |
| 377 | iv_len); |
| 378 | return s->aead_write_ctx != NULL; |
| 379 | } |
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 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 382 | int tls1_setup_key_block(SSL *s) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 383 | uint8_t *p; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 384 | const EVP_AEAD *aead = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | int ret = 0; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 386 | size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 387 | size_t key_block_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | if (s->s3->tmp.key_block_length != 0) { |
| 390 | return 1; |
| 391 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 393 | if (s->session->cipher == NULL) { |
| 394 | goto cipher_unavailable_err; |
| 395 | } |
| 396 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 397 | if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, |
| 398 | s->session->cipher, |
| 399 | ssl3_version_from_wire(s, s->version))) { |
| 400 | goto cipher_unavailable_err; |
| 401 | } |
| 402 | key_len = EVP_AEAD_key_length(aead); |
| 403 | variable_iv_len = EVP_AEAD_nonce_length(aead); |
| 404 | if (mac_secret_len > 0) { |
| 405 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 406 | * key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 407 | * bytes and initial implicit IV. */ |
| 408 | if (key_len < mac_secret_len + fixed_iv_len) { |
| 409 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR); |
| 410 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 411 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 412 | key_len -= mac_secret_len + fixed_iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 413 | } else { |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 414 | /* The nonce is split into a fixed portion and a variable portion. */ |
| 415 | if (variable_iv_len < fixed_iv_len) { |
| 416 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR); |
| 417 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 418 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 419 | variable_iv_len -= fixed_iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 422 | assert(mac_secret_len < 256); |
| 423 | assert(fixed_iv_len < 256); |
| 424 | assert(variable_iv_len < 256); |
| 425 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 426 | s->s3->tmp.new_aead = aead; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 427 | s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len; |
| 428 | s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len; |
| 429 | s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 431 | key_block_len = key_len + mac_secret_len + fixed_iv_len; |
| 432 | key_block_len *= 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 434 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 436 | p = (uint8_t *)OPENSSL_malloc(key_block_len); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 437 | if (p == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 438 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE); |
| 439 | goto err; |
| 440 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 442 | s->s3->tmp.key_block_length = key_block_len; |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 443 | s->s3->tmp.key_block = p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 445 | if (!tls1_generate_key_block(s, p, key_block_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 446 | goto err; |
| 447 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 449 | if (!SSL_USE_EXPLICIT_IV(s) && |
| 450 | (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0) { |
| 451 | /* enable vulnerability countermeasure for CBC ciphers with known-IV |
| 452 | * problem (http://www.openssl.org/~bodo/tls-cbc.txt). */ |
| 453 | s->s3->need_record_splitting = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 454 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 455 | if (s->session->cipher != NULL && |
| 456 | s->session->cipher->algorithm_enc == SSL_RC4) { |
| 457 | s->s3->need_record_splitting = 0; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | ret = 1; |
| 462 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 464 | return ret; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
| 466 | cipher_unavailable_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, |
| 468 | SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 469 | return 0; |
| 470 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 472 | int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) { |
| 473 | unsigned int ret; |
| 474 | EVP_MD_CTX ctx, *d = NULL; |
| 475 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 477 | if (s->s3->handshake_buffer && |
| 478 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | for (i = 0; i < SSL_MAX_DIGEST; i++) { |
| 483 | if (s->s3->handshake_dgst[i] && |
| 484 | EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) { |
| 485 | d = s->s3->handshake_dgst[i]; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (!d) { |
| 491 | OPENSSL_PUT_ERROR(SSL, tls1_cert_verify_mac, SSL_R_NO_REQUIRED_DIGEST); |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | EVP_MD_CTX_init(&ctx); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 496 | if (!EVP_MD_CTX_copy_ex(&ctx, d)) { |
| 497 | EVP_MD_CTX_cleanup(&ctx); |
| 498 | return 0; |
| 499 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 500 | EVP_DigestFinal_ex(&ctx, out, &ret); |
| 501 | EVP_MD_CTX_cleanup(&ctx); |
| 502 | |
| 503 | return ret; |
| 504 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | /* tls1_handshake_digest calculates the current handshake hash and writes it to |
| 507 | * |out|, which has space for |out_len| bytes. It returns the number of bytes |
| 508 | * written or -1 in the event of an error. This function works on a copy of the |
| 509 | * underlying digests so can be called multiple times and prior to the final |
| 510 | * update etc. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 511 | int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) { |
| 512 | const EVP_MD *md; |
| 513 | EVP_MD_CTX ctx; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 514 | int err = 0, len = 0; |
| 515 | size_t i; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 516 | uint32_t mask; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 518 | EVP_MD_CTX_init(&ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 520 | for (i = 0; ssl_get_handshake_digest(&mask, &md, i); i++) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 521 | size_t hash_size; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 522 | unsigned int digest_len; |
| 523 | EVP_MD_CTX *hdgst = s->s3->handshake_dgst[i]; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 525 | if ((mask & ssl_get_algorithm2(s)) == 0) { |
| 526 | continue; |
| 527 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 529 | hash_size = EVP_MD_size(md); |
| 530 | if (!hdgst || |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 531 | hash_size > out_len || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 532 | !EVP_MD_CTX_copy_ex(&ctx, hdgst) || |
| 533 | !EVP_DigestFinal_ex(&ctx, out, &digest_len) || |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 534 | digest_len != hash_size /* internal error */) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 535 | err = 1; |
| 536 | break; |
| 537 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 539 | out += digest_len; |
| 540 | out_len -= digest_len; |
| 541 | len += digest_len; |
| 542 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 544 | EVP_MD_CTX_cleanup(&ctx); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 546 | if (err != 0) { |
| 547 | return -1; |
| 548 | } |
| 549 | return len; |
| 550 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 552 | int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) { |
| 553 | uint8_t buf[2 * EVP_MAX_MD_SIZE]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 554 | int err = 0; |
| 555 | int digests_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | |
David Benjamin | e76ccae | 2015-05-26 17:41:50 -0400 | [diff] [blame^] | 557 | /* At this point, the handshake should have released the handshake buffer on |
| 558 | * its own. |
| 559 | * TODO(davidben): Apart from initialization, the handshake buffer should be |
| 560 | * orthogonal to the handshake digest. https://crbug.com/492371 */ |
| 561 | assert(s->s3->handshake_buffer == NULL); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 562 | if (s->s3->handshake_buffer && |
| 563 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 564 | return 0; |
| 565 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 566 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 567 | digests_len = tls1_handshake_digest(s, buf, sizeof(buf)); |
| 568 | if (digests_len < 0) { |
| 569 | err = 1; |
| 570 | digests_len = 0; |
| 571 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 572 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 573 | if (!s->enc_method->prf(s, out, 12, s->session->master_key, |
| 574 | s->session->master_key_length, str, slen, buf, |
| 575 | digests_len, NULL, 0)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 576 | err = 1; |
| 577 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 578 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 579 | if (err) { |
| 580 | return 0; |
| 581 | } else { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 582 | return 12; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 583 | } |
| 584 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 585 | |
David Benjamin | 31b1d81 | 2014-12-23 10:01:09 -0500 | [diff] [blame] | 586 | int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster, |
| 587 | size_t premaster_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 588 | if (s->s3->tmp.extended_master_secret) { |
| 589 | uint8_t digests[2 * EVP_MAX_MD_SIZE]; |
| 590 | int digests_len; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 591 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 592 | /* The master secret is based on the handshake hash just after sending the |
| 593 | * ClientKeyExchange. However, we might have a client certificate to send, |
| 594 | * in which case we might need different hashes for the verification and |
| 595 | * thus still need the handshake buffer around. Keeping both a handshake |
| 596 | * buffer *and* running hashes isn't yet supported so, when it comes to |
| 597 | * calculating the Finished hash, we'll have to hash the handshake buffer |
| 598 | * again. */ |
| 599 | if (s->s3->handshake_buffer && |
| 600 | !ssl3_digest_cached_records(s, dont_free_handshake_buffer)) { |
| 601 | return 0; |
| 602 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 603 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 604 | digests_len = tls1_handshake_digest(s, digests, sizeof(digests)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 605 | if (digests_len == -1) { |
| 606 | return 0; |
| 607 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 608 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 609 | if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 610 | premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST, |
| 611 | TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests, |
| 612 | digests_len, NULL, 0)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 613 | return 0; |
| 614 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | } else { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 616 | if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 617 | premaster_len, TLS_MD_MASTER_SECRET_CONST, |
| 618 | TLS_MD_MASTER_SECRET_CONST_SIZE, |
| 619 | s->s3->client_random, SSL3_RANDOM_SIZE, |
| 620 | s->s3->server_random, SSL3_RANDOM_SIZE)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 621 | return 0; |
| 622 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 623 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 625 | return SSL3_MASTER_SECRET_SIZE; |
| 626 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 627 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 628 | int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len, |
| 629 | const char *label, size_t label_len, |
| 630 | const uint8_t *context, size_t context_len, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 631 | int use_context) { |
David Benjamin | d6e95ee | 2015-05-03 15:34:35 -0400 | [diff] [blame] | 632 | if (!s->s3->have_version || s->version == SSL3_VERSION) { |
| 633 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, |
| 634 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 635 | return 0; |
| 636 | } |
| 637 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 638 | size_t seed_len = 2 * SSL3_RANDOM_SIZE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 639 | if (use_context) { |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 640 | if (context_len >= 1u << 16) { |
| 641 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_OVERFLOW); |
| 642 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 643 | } |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 644 | seed_len += 2 + context_len; |
| 645 | } |
| 646 | uint8_t *seed = OPENSSL_malloc(seed_len); |
| 647 | if (seed == NULL) { |
| 648 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_MALLOC_FAILURE); |
| 649 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 650 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 652 | memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE); |
| 653 | memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE); |
| 654 | if (use_context) { |
| 655 | seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8); |
| 656 | seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len; |
| 657 | memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 658 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 659 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 660 | int ret = s->enc_method->prf(s, out, out_len, s->session->master_key, |
| 661 | s->session->master_key_length, label, label_len, |
| 662 | seed, seed_len, NULL, 0); |
| 663 | OPENSSL_free(seed); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | int tls1_alert_code(int code) { |
| 668 | switch (code) { |
| 669 | case SSL_AD_CLOSE_NOTIFY: |
| 670 | return SSL3_AD_CLOSE_NOTIFY; |
| 671 | |
| 672 | case SSL_AD_UNEXPECTED_MESSAGE: |
| 673 | return SSL3_AD_UNEXPECTED_MESSAGE; |
| 674 | |
| 675 | case SSL_AD_BAD_RECORD_MAC: |
| 676 | return SSL3_AD_BAD_RECORD_MAC; |
| 677 | |
| 678 | case SSL_AD_DECRYPTION_FAILED: |
| 679 | return TLS1_AD_DECRYPTION_FAILED; |
| 680 | |
| 681 | case SSL_AD_RECORD_OVERFLOW: |
| 682 | return TLS1_AD_RECORD_OVERFLOW; |
| 683 | |
| 684 | case SSL_AD_DECOMPRESSION_FAILURE: |
| 685 | return SSL3_AD_DECOMPRESSION_FAILURE; |
| 686 | |
| 687 | case SSL_AD_HANDSHAKE_FAILURE: |
| 688 | return SSL3_AD_HANDSHAKE_FAILURE; |
| 689 | |
| 690 | case SSL_AD_NO_CERTIFICATE: |
| 691 | return -1; |
| 692 | |
| 693 | case SSL_AD_BAD_CERTIFICATE: |
| 694 | return SSL3_AD_BAD_CERTIFICATE; |
| 695 | |
| 696 | case SSL_AD_UNSUPPORTED_CERTIFICATE: |
| 697 | return SSL3_AD_UNSUPPORTED_CERTIFICATE; |
| 698 | |
| 699 | case SSL_AD_CERTIFICATE_REVOKED: |
| 700 | return SSL3_AD_CERTIFICATE_REVOKED; |
| 701 | |
| 702 | case SSL_AD_CERTIFICATE_EXPIRED: |
| 703 | return SSL3_AD_CERTIFICATE_EXPIRED; |
| 704 | |
| 705 | case SSL_AD_CERTIFICATE_UNKNOWN: |
| 706 | return SSL3_AD_CERTIFICATE_UNKNOWN; |
| 707 | |
| 708 | case SSL_AD_ILLEGAL_PARAMETER: |
| 709 | return SSL3_AD_ILLEGAL_PARAMETER; |
| 710 | |
| 711 | case SSL_AD_UNKNOWN_CA: |
| 712 | return TLS1_AD_UNKNOWN_CA; |
| 713 | |
| 714 | case SSL_AD_ACCESS_DENIED: |
| 715 | return TLS1_AD_ACCESS_DENIED; |
| 716 | |
| 717 | case SSL_AD_DECODE_ERROR: |
| 718 | return TLS1_AD_DECODE_ERROR; |
| 719 | |
| 720 | case SSL_AD_DECRYPT_ERROR: |
| 721 | return TLS1_AD_DECRYPT_ERROR; |
| 722 | case SSL_AD_EXPORT_RESTRICTION: |
| 723 | return TLS1_AD_EXPORT_RESTRICTION; |
| 724 | |
| 725 | case SSL_AD_PROTOCOL_VERSION: |
| 726 | return TLS1_AD_PROTOCOL_VERSION; |
| 727 | |
| 728 | case SSL_AD_INSUFFICIENT_SECURITY: |
| 729 | return TLS1_AD_INSUFFICIENT_SECURITY; |
| 730 | |
| 731 | case SSL_AD_INTERNAL_ERROR: |
| 732 | return TLS1_AD_INTERNAL_ERROR; |
| 733 | |
| 734 | case SSL_AD_USER_CANCELLED: |
| 735 | return TLS1_AD_USER_CANCELLED; |
| 736 | |
| 737 | case SSL_AD_NO_RENEGOTIATION: |
| 738 | return TLS1_AD_NO_RENEGOTIATION; |
| 739 | |
| 740 | case SSL_AD_UNSUPPORTED_EXTENSION: |
| 741 | return TLS1_AD_UNSUPPORTED_EXTENSION; |
| 742 | |
| 743 | case SSL_AD_CERTIFICATE_UNOBTAINABLE: |
| 744 | return TLS1_AD_CERTIFICATE_UNOBTAINABLE; |
| 745 | |
| 746 | case SSL_AD_UNRECOGNIZED_NAME: |
| 747 | return TLS1_AD_UNRECOGNIZED_NAME; |
| 748 | |
| 749 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: |
| 750 | return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 751 | |
| 752 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: |
| 753 | return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE; |
| 754 | |
| 755 | case SSL_AD_UNKNOWN_PSK_IDENTITY: |
| 756 | return TLS1_AD_UNKNOWN_PSK_IDENTITY; |
| 757 | |
| 758 | case SSL_AD_INAPPROPRIATE_FALLBACK: |
| 759 | return SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 760 | |
| 761 | default: |
| 762 | return -1; |
| 763 | } |
| 764 | } |