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 <stdio.h> |
| 140 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | #include <openssl/err.h> |
| 143 | #include <openssl/evp.h> |
| 144 | #include <openssl/hmac.h> |
| 145 | #include <openssl/md5.h> |
| 146 | #include <openssl/mem.h> |
| 147 | #include <openssl/obj.h> |
| 148 | #include <openssl/rand.h> |
| 149 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 150 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 153 | /* 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] | 154 | * 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] | 155 | * |secret| as the secret. |seed1| through |seed3| are concatenated to form the |
| 156 | * seed parameter. It returns one on success and zero on failure. */ |
| 157 | static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, |
| 158 | const uint8_t *secret, size_t secret_len, |
| 159 | const uint8_t *seed1, size_t seed1_len, |
| 160 | const uint8_t *seed2, size_t seed2_len, |
| 161 | const uint8_t *seed3, size_t seed3_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 162 | HMAC_CTX ctx, ctx_tmp, ctx_init; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 163 | uint8_t A1[EVP_MAX_MD_SIZE]; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 164 | unsigned A1_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 167 | size_t chunk = EVP_MD_size(md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 169 | HMAC_CTX_init(&ctx); |
| 170 | HMAC_CTX_init(&ctx_tmp); |
| 171 | HMAC_CTX_init(&ctx_init); |
| 172 | if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) || |
| 173 | !HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 174 | !HMAC_Update(&ctx, seed1, seed1_len) || |
| 175 | !HMAC_Update(&ctx, seed2, seed2_len) || |
| 176 | !HMAC_Update(&ctx, seed3, seed3_len) || |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 177 | !HMAC_Final(&ctx, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 178 | goto err; |
| 179 | } |
| 180 | |
| 181 | for (;;) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 182 | unsigned len; |
| 183 | uint8_t hmac[EVP_MAX_MD_SIZE]; |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 184 | if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
| 185 | !HMAC_Update(&ctx, A1, A1_len) || |
| 186 | /* Save a copy of |ctx| to compute the next A1 value below. */ |
| 187 | (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) || |
| 188 | !HMAC_Update(&ctx, seed1, seed1_len) || |
| 189 | !HMAC_Update(&ctx, seed2, seed2_len) || |
| 190 | !HMAC_Update(&ctx, seed3, seed3_len) || |
| 191 | !HMAC_Final(&ctx, hmac, &len)) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 192 | goto err; |
| 193 | } |
| 194 | assert(len == chunk); |
| 195 | |
| 196 | /* XOR the result into |out|. */ |
| 197 | if (len > out_len) { |
| 198 | len = out_len; |
| 199 | } |
| 200 | unsigned i; |
| 201 | for (i = 0; i < len; i++) { |
| 202 | out[i] ^= hmac[i]; |
| 203 | } |
| 204 | out += len; |
| 205 | out_len -= len; |
| 206 | |
| 207 | if (out_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 208 | break; |
| 209 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 210 | |
| 211 | /* Calculate the next A1 value. */ |
| 212 | if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) { |
| 213 | goto err; |
| 214 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | ret = 1; |
| 218 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | err: |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 220 | HMAC_CTX_cleanup(&ctx); |
| 221 | HMAC_CTX_cleanup(&ctx_tmp); |
| 222 | HMAC_CTX_cleanup(&ctx_init); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 223 | OPENSSL_cleanse(A1, sizeof(A1)); |
| 224 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 225 | } |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame] | 226 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame^] | 227 | static int tls1_prf(const SSL *ssl, uint8_t *out, size_t out_len, |
| 228 | const uint8_t *secret, size_t secret_len, const char *label, |
| 229 | size_t label_len, const uint8_t *seed1, size_t seed1_len, |
| 230 | const uint8_t *seed2, size_t seed2_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 231 | if (out_len == 0) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 232 | return 1; |
| 233 | } |
| 234 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 235 | memset(out, 0, out_len); |
| 236 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 237 | uint32_t algorithm_prf = ssl_get_algorithm_prf(ssl); |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 238 | if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) { |
| 239 | /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and |
| 240 | * MD5, MD5 first. */ |
| 241 | size_t secret_half = secret_len - (secret_len / 2); |
| 242 | if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, |
| 243 | (const uint8_t *)label, label_len, seed1, seed1_len, seed2, |
| 244 | seed2_len)) { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* Note that, if |secret_len| is odd, the two halves share a byte. */ |
| 249 | secret = secret + (secret_len - secret_half); |
| 250 | secret_len = secret_half; |
| 251 | } |
| 252 | |
| 253 | if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf), |
| 254 | secret, secret_len, (const uint8_t *)label, label_len, |
| 255 | seed1, seed1_len, seed2, seed2_len)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 256 | return 0; |
| 257 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 259 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 260 | } |
| 261 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 262 | int tls1_change_cipher_state(SSL *ssl, int which) { |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 263 | /* Ensure the key block is set up. */ |
| 264 | if (!tls1_setup_key_block(ssl)) { |
| 265 | return 0; |
| 266 | } |
| 267 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we |
| 269 | * need to update the read cipherspec. Otherwise we have just written one. */ |
| 270 | const char is_read = (which & SSL3_CC_READ) != 0; |
| 271 | /* use_client_keys is true if we wish to use the keys for the "client write" |
| 272 | * direction. This is the case if we're a client sending a ChangeCipherSpec, |
| 273 | * or a server reading a client's ChangeCipherSpec. */ |
| 274 | const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || |
| 275 | which == SSL3_CHANGE_CIPHER_SERVER_READ; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 277 | size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len; |
| 278 | size_t key_len = ssl->s3->tmp.new_key_len; |
| 279 | size_t iv_len = ssl->s3->tmp.new_fixed_iv_len; |
| 280 | assert((mac_secret_len + key_len + iv_len) * 2 == |
| 281 | ssl->s3->tmp.key_block_length); |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 283 | const uint8_t *key_data = ssl->s3->tmp.key_block; |
| 284 | const uint8_t *client_write_mac_secret = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 285 | key_data += mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 286 | const uint8_t *server_write_mac_secret = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 287 | key_data += mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 288 | const uint8_t *client_write_key = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 289 | key_data += key_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 290 | const uint8_t *server_write_key = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 291 | key_data += key_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 292 | const uint8_t *client_write_iv = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 293 | key_data += iv_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 294 | const uint8_t *server_write_iv = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 295 | key_data += iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 297 | const uint8_t *mac_secret, *key, *iv; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 298 | if (use_client_keys) { |
| 299 | mac_secret = client_write_mac_secret; |
| 300 | key = client_write_key; |
| 301 | iv = client_write_iv; |
| 302 | } else { |
| 303 | mac_secret = server_write_mac_secret; |
| 304 | key = server_write_key; |
| 305 | iv = server_write_iv; |
| 306 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 308 | SSL_AEAD_CTX *aead_ctx = SSL_AEAD_CTX_new( |
| 309 | is_read ? evp_aead_open : evp_aead_seal, |
| 310 | ssl3_version_from_wire(ssl, ssl->version), ssl->s3->tmp.new_cipher, key, |
| 311 | key_len, mac_secret, mac_secret_len, iv, iv_len); |
| 312 | if (aead_ctx == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 313 | return 0; |
| 314 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 316 | if (is_read) { |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 317 | ssl_set_read_state(ssl, aead_ctx); |
| 318 | } else { |
| 319 | ssl_set_write_state(ssl, aead_ctx); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 320 | } |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 321 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 322 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | |
David Benjamin | 928f32a | 2015-12-29 23:32:29 -0500 | [diff] [blame] | 324 | size_t SSL_get_key_block_len(const SSL *ssl) { |
| 325 | return 2 * ((size_t)ssl->s3->tmp.new_mac_secret_len + |
| 326 | (size_t)ssl->s3->tmp.new_key_len + |
| 327 | (size_t)ssl->s3->tmp.new_fixed_iv_len); |
| 328 | } |
| 329 | |
| 330 | int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) { |
| 331 | return ssl->enc_method->prf( |
| 332 | ssl, out, out_len, ssl->session->master_key, |
| 333 | ssl->session->master_key_length, TLS_MD_KEY_EXPANSION_CONST, |
| 334 | TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE, |
| 335 | ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 336 | } |
| 337 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 338 | int tls1_setup_key_block(SSL *ssl) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 339 | if (ssl->s3->tmp.key_block_length != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 340 | return 1; |
| 341 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 342 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 343 | const EVP_AEAD *aead = NULL; |
| 344 | size_t mac_secret_len, fixed_iv_len; |
| 345 | if (ssl->session->cipher == NULL || |
| 346 | !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 347 | ssl->session->cipher, |
| 348 | ssl3_version_from_wire(ssl, ssl->version))) { |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 349 | OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 350 | return 0; |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 351 | } |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 352 | size_t key_len = EVP_AEAD_key_length(aead); |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 353 | if (mac_secret_len > 0) { |
| 354 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 355 | * key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 356 | * bytes and initial implicit IV. */ |
| 357 | if (key_len < mac_secret_len + fixed_iv_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 358 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 359 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 360 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 361 | key_len -= mac_secret_len + fixed_iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 362 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 364 | assert(mac_secret_len < 256); |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 365 | assert(key_len < 256); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 366 | assert(fixed_iv_len < 256); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 367 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 368 | ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 369 | ssl->s3->tmp.new_key_len = (uint8_t)key_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 370 | ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | |
David Benjamin | 928f32a | 2015-12-29 23:32:29 -0500 | [diff] [blame] | 372 | size_t key_block_len = SSL_get_key_block_len(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 374 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 376 | uint8_t *keyblock = (uint8_t *)OPENSSL_malloc(key_block_len); |
| 377 | if (keyblock == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 378 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 379 | return 0; |
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 | 928f32a | 2015-12-29 23:32:29 -0500 | [diff] [blame] | 382 | if (!SSL_generate_key_block(ssl, keyblock, key_block_len)) { |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 383 | OPENSSL_free(keyblock); |
| 384 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 387 | assert(key_block_len < 256); |
| 388 | ssl->s3->tmp.key_block_length = (uint8_t)key_block_len; |
| 389 | ssl->s3->tmp.key_block = keyblock; |
| 390 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 391 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame^] | 393 | static int tls1_cert_verify_mac(SSL *ssl, int md_nid, uint8_t *out) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 394 | const EVP_MD_CTX *ctx_template; |
| 395 | if (md_nid == NID_md5) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 396 | ctx_template = &ssl->s3->handshake_md5; |
| 397 | } else if (md_nid == EVP_MD_CTX_type(&ssl->s3->handshake_hash)) { |
| 398 | ctx_template = &ssl->s3->handshake_hash; |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 399 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 400 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 404 | EVP_MD_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 405 | EVP_MD_CTX_init(&ctx); |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 406 | if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) { |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 407 | EVP_MD_CTX_cleanup(&ctx); |
| 408 | return 0; |
| 409 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 410 | unsigned ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 411 | EVP_DigestFinal_ex(&ctx, out, &ret); |
| 412 | EVP_MD_CTX_cleanup(&ctx); |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 413 | return ret; |
| 414 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 415 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 416 | static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 417 | size_t max_out) { |
| 418 | int ret = 0; |
| 419 | EVP_MD_CTX ctx_copy; |
| 420 | EVP_MD_CTX_init(&ctx_copy); |
| 421 | |
| 422 | if (EVP_MD_CTX_size(ctx) > max_out) { |
| 423 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 424 | goto err; |
| 425 | } |
| 426 | unsigned len; |
| 427 | if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) || |
| 428 | !EVP_DigestFinal_ex(&ctx_copy, out, &len)) { |
| 429 | goto err; |
| 430 | } |
| 431 | assert(len == EVP_MD_CTX_size(ctx)); |
| 432 | |
| 433 | *out_len = len; |
| 434 | ret = 1; |
| 435 | |
| 436 | err: |
| 437 | EVP_MD_CTX_cleanup(&ctx_copy); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 438 | return ret; |
| 439 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 440 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | /* tls1_handshake_digest calculates the current handshake hash and writes it to |
| 442 | * |out|, which has space for |out_len| bytes. It returns the number of bytes |
| 443 | * written or -1 in the event of an error. This function works on a copy of the |
| 444 | * underlying digests so can be called multiple times and prior to the final |
| 445 | * update etc. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 446 | 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] | 447 | size_t md5_len = 0; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 448 | if (EVP_MD_CTX_md(&ssl->s3->handshake_md5) != NULL && |
| 449 | !append_digest(&ssl->s3->handshake_md5, out, &md5_len, out_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 450 | return -1; |
| 451 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 452 | |
| 453 | size_t len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 454 | if (!append_digest(&ssl->s3->handshake_hash, out + md5_len, &len, |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 455 | out_len - md5_len)) { |
| 456 | return -1; |
| 457 | } |
| 458 | |
| 459 | return (int)(md5_len + len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame^] | 462 | 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] | 463 | /* At this point, the handshake should have released the handshake buffer on |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 464 | * its own. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 465 | assert(ssl->s3->handshake_buffer == NULL); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 467 | const char *label = TLS_MD_CLIENT_FINISH_CONST; |
| 468 | size_t label_len = TLS_MD_SERVER_FINISH_CONST_SIZE; |
| 469 | if (from_server) { |
| 470 | label = TLS_MD_SERVER_FINISH_CONST; |
| 471 | label_len = TLS_MD_SERVER_FINISH_CONST_SIZE; |
| 472 | } |
| 473 | |
| 474 | uint8_t buf[EVP_MAX_MD_SIZE]; |
| 475 | int digests_len = tls1_handshake_digest(ssl, buf, sizeof(buf)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 476 | if (digests_len < 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 477 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 478 | } |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 479 | |
| 480 | static const size_t kFinishedLen = 12; |
| 481 | if (!ssl->enc_method->prf(ssl, out, kFinishedLen, ssl->session->master_key, |
| 482 | ssl->session->master_key_length, label, label_len, |
| 483 | buf, digests_len, NULL, 0)) { |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | return (int)kFinishedLen; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 490 | int tls1_generate_master_secret(SSL *ssl, uint8_t *out, |
| 491 | const uint8_t *premaster, |
David Benjamin | 31b1d81 | 2014-12-23 10:01:09 -0500 | [diff] [blame] | 492 | size_t premaster_len) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 493 | if (ssl->s3->tmp.extended_master_secret) { |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 494 | uint8_t digests[EVP_MAX_MD_SIZE]; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 495 | int digests_len = tls1_handshake_digest(ssl, digests, sizeof(digests)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | if (digests_len == -1) { |
| 497 | return 0; |
| 498 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 499 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 500 | if (!ssl->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 501 | premaster_len, |
| 502 | TLS_MD_EXTENDED_MASTER_SECRET_CONST, |
| 503 | TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests, |
| 504 | digests_len, NULL, 0)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 505 | return 0; |
| 506 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 507 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 508 | if (!ssl->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 509 | premaster_len, TLS_MD_MASTER_SECRET_CONST, |
| 510 | TLS_MD_MASTER_SECRET_CONST_SIZE, |
| 511 | ssl->s3->client_random, SSL3_RANDOM_SIZE, |
| 512 | ssl->s3->server_random, SSL3_RANDOM_SIZE)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 513 | return 0; |
| 514 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 515 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 517 | return SSL3_MASTER_SECRET_SIZE; |
| 518 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 520 | int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 521 | const char *label, size_t label_len, |
| 522 | const uint8_t *context, size_t context_len, |
| 523 | int use_context) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 524 | if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) { |
David Benjamin | d6e95ee | 2015-05-03 15:34:35 -0400 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 528 | size_t seed_len = 2 * SSL3_RANDOM_SIZE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 529 | if (use_context) { |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 530 | if (context_len >= 1u << 16) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 531 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 532 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 533 | } |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 534 | seed_len += 2 + context_len; |
| 535 | } |
| 536 | uint8_t *seed = OPENSSL_malloc(seed_len); |
| 537 | if (seed == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 538 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 539 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 540 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 541 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 542 | memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 543 | memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 544 | if (use_context) { |
| 545 | seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8); |
| 546 | seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len; |
| 547 | memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 548 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 550 | int ret = ssl->enc_method->prf(ssl, out, out_len, ssl->session->master_key, |
| 551 | ssl->session->master_key_length, label, |
| 552 | label_len, seed, seed_len, NULL, 0); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 553 | OPENSSL_free(seed); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame^] | 557 | static int tls1_alert_code(int code) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 558 | switch (code) { |
| 559 | case SSL_AD_CLOSE_NOTIFY: |
| 560 | return SSL3_AD_CLOSE_NOTIFY; |
| 561 | |
| 562 | case SSL_AD_UNEXPECTED_MESSAGE: |
| 563 | return SSL3_AD_UNEXPECTED_MESSAGE; |
| 564 | |
| 565 | case SSL_AD_BAD_RECORD_MAC: |
| 566 | return SSL3_AD_BAD_RECORD_MAC; |
| 567 | |
| 568 | case SSL_AD_DECRYPTION_FAILED: |
| 569 | return TLS1_AD_DECRYPTION_FAILED; |
| 570 | |
| 571 | case SSL_AD_RECORD_OVERFLOW: |
| 572 | return TLS1_AD_RECORD_OVERFLOW; |
| 573 | |
| 574 | case SSL_AD_DECOMPRESSION_FAILURE: |
| 575 | return SSL3_AD_DECOMPRESSION_FAILURE; |
| 576 | |
| 577 | case SSL_AD_HANDSHAKE_FAILURE: |
| 578 | return SSL3_AD_HANDSHAKE_FAILURE; |
| 579 | |
| 580 | case SSL_AD_NO_CERTIFICATE: |
| 581 | return -1; |
| 582 | |
| 583 | case SSL_AD_BAD_CERTIFICATE: |
| 584 | return SSL3_AD_BAD_CERTIFICATE; |
| 585 | |
| 586 | case SSL_AD_UNSUPPORTED_CERTIFICATE: |
| 587 | return SSL3_AD_UNSUPPORTED_CERTIFICATE; |
| 588 | |
| 589 | case SSL_AD_CERTIFICATE_REVOKED: |
| 590 | return SSL3_AD_CERTIFICATE_REVOKED; |
| 591 | |
| 592 | case SSL_AD_CERTIFICATE_EXPIRED: |
| 593 | return SSL3_AD_CERTIFICATE_EXPIRED; |
| 594 | |
| 595 | case SSL_AD_CERTIFICATE_UNKNOWN: |
| 596 | return SSL3_AD_CERTIFICATE_UNKNOWN; |
| 597 | |
| 598 | case SSL_AD_ILLEGAL_PARAMETER: |
| 599 | return SSL3_AD_ILLEGAL_PARAMETER; |
| 600 | |
| 601 | case SSL_AD_UNKNOWN_CA: |
| 602 | return TLS1_AD_UNKNOWN_CA; |
| 603 | |
| 604 | case SSL_AD_ACCESS_DENIED: |
| 605 | return TLS1_AD_ACCESS_DENIED; |
| 606 | |
| 607 | case SSL_AD_DECODE_ERROR: |
| 608 | return TLS1_AD_DECODE_ERROR; |
| 609 | |
| 610 | case SSL_AD_DECRYPT_ERROR: |
| 611 | return TLS1_AD_DECRYPT_ERROR; |
| 612 | case SSL_AD_EXPORT_RESTRICTION: |
| 613 | return TLS1_AD_EXPORT_RESTRICTION; |
| 614 | |
| 615 | case SSL_AD_PROTOCOL_VERSION: |
| 616 | return TLS1_AD_PROTOCOL_VERSION; |
| 617 | |
| 618 | case SSL_AD_INSUFFICIENT_SECURITY: |
| 619 | return TLS1_AD_INSUFFICIENT_SECURITY; |
| 620 | |
| 621 | case SSL_AD_INTERNAL_ERROR: |
| 622 | return TLS1_AD_INTERNAL_ERROR; |
| 623 | |
| 624 | case SSL_AD_USER_CANCELLED: |
| 625 | return TLS1_AD_USER_CANCELLED; |
| 626 | |
| 627 | case SSL_AD_NO_RENEGOTIATION: |
| 628 | return TLS1_AD_NO_RENEGOTIATION; |
| 629 | |
| 630 | case SSL_AD_UNSUPPORTED_EXTENSION: |
| 631 | return TLS1_AD_UNSUPPORTED_EXTENSION; |
| 632 | |
| 633 | case SSL_AD_CERTIFICATE_UNOBTAINABLE: |
| 634 | return TLS1_AD_CERTIFICATE_UNOBTAINABLE; |
| 635 | |
| 636 | case SSL_AD_UNRECOGNIZED_NAME: |
| 637 | return TLS1_AD_UNRECOGNIZED_NAME; |
| 638 | |
| 639 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: |
| 640 | return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 641 | |
| 642 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: |
| 643 | return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE; |
| 644 | |
| 645 | case SSL_AD_UNKNOWN_PSK_IDENTITY: |
| 646 | return TLS1_AD_UNKNOWN_PSK_IDENTITY; |
| 647 | |
| 648 | case SSL_AD_INAPPROPRIATE_FALLBACK: |
| 649 | return SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 650 | |
| 651 | default: |
| 652 | return -1; |
| 653 | } |
| 654 | } |
David Benjamin | 23b0a65 | 2015-12-29 23:41:34 -0500 | [diff] [blame^] | 655 | |
| 656 | const SSL3_ENC_METHOD TLSv1_enc_data = { |
| 657 | tls1_prf, |
| 658 | tls1_final_finish_mac, |
| 659 | tls1_cert_verify_mac, |
| 660 | tls1_alert_code, |
| 661 | 0, |
| 662 | }; |
| 663 | |
| 664 | const SSL3_ENC_METHOD TLSv1_1_enc_data = { |
| 665 | tls1_prf, |
| 666 | tls1_final_finish_mac, |
| 667 | tls1_cert_verify_mac, |
| 668 | tls1_alert_code, |
| 669 | SSL_ENC_FLAG_EXPLICIT_IV, |
| 670 | }; |
| 671 | |
| 672 | const SSL3_ENC_METHOD TLSv1_2_enc_data = { |
| 673 | tls1_prf, |
| 674 | tls1_final_finish_mac, |
| 675 | tls1_cert_verify_mac, |
| 676 | tls1_alert_code, |
| 677 | SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF, |
| 678 | }; |