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