Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2005 Nokia. All rights reserved. |
| 112 | * |
| 113 | * The portions of the attached software ("Contribution") is developed by |
| 114 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 115 | * license. |
| 116 | * |
| 117 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 118 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 119 | * support (see RFC 4279) to OpenSSL. |
| 120 | * |
| 121 | * No patent licenses or other rights except those expressly stated in |
| 122 | * the OpenSSL open source license shall be deemed granted or received |
| 123 | * expressly, by implication, estoppel, or otherwise. |
| 124 | * |
| 125 | * No assurances are provided by Nokia that the Contribution does not |
| 126 | * infringe the patent or other intellectual property rights of any third |
| 127 | * party or that the license provides you with all the necessary rights |
| 128 | * to make use of the Contribution. |
| 129 | * |
| 130 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 131 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 132 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 133 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 134 | * OTHERWISE. */ |
| 135 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 136 | #include <openssl/ssl.h> |
| 137 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 139 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 141 | #include <utility> |
| 142 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | #include <openssl/err.h> |
| 144 | #include <openssl/evp.h> |
| 145 | #include <openssl/hmac.h> |
| 146 | #include <openssl/md5.h> |
| 147 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 148 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | #include <openssl/rand.h> |
| 150 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 151 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 152 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 155 | namespace bssl { |
| 156 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 157 | /* 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] | 158 | * 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] | 159 | * |secret| as the secret. |seed1| through |seed3| are concatenated to form the |
| 160 | * seed parameter. It returns one on success and zero on failure. */ |
| 161 | static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, |
| 162 | const uint8_t *secret, size_t secret_len, |
| 163 | const uint8_t *seed1, size_t seed1_len, |
| 164 | const uint8_t *seed2, size_t seed2_len, |
| 165 | const uint8_t *seed3, size_t seed3_len) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 166 | ScopedHMAC_CTX ctx, ctx_tmp, ctx_init; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 167 | uint8_t A1[EVP_MAX_MD_SIZE]; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 168 | unsigned A1_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 169 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 171 | size_t chunk = EVP_MD_size(md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 173 | if (!HMAC_Init_ex(ctx_init.get(), secret, secret_len, md, NULL) || |
| 174 | !HMAC_CTX_copy_ex(ctx.get(), ctx_init.get()) || |
| 175 | !HMAC_Update(ctx.get(), seed1, seed1_len) || |
| 176 | !HMAC_Update(ctx.get(), seed2, seed2_len) || |
| 177 | !HMAC_Update(ctx.get(), seed3, seed3_len) || |
| 178 | !HMAC_Final(ctx.get(), A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 179 | goto err; |
| 180 | } |
| 181 | |
| 182 | for (;;) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 183 | unsigned len; |
| 184 | uint8_t hmac[EVP_MAX_MD_SIZE]; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 185 | if (!HMAC_CTX_copy_ex(ctx.get(), ctx_init.get()) || |
| 186 | !HMAC_Update(ctx.get(), A1, A1_len) || |
David Benjamin | 871fff0 | 2015-12-06 21:04:36 -0500 | [diff] [blame] | 187 | /* Save a copy of |ctx| to compute the next A1 value below. */ |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 188 | (out_len > chunk && !HMAC_CTX_copy_ex(ctx_tmp.get(), ctx.get())) || |
| 189 | !HMAC_Update(ctx.get(), seed1, seed1_len) || |
| 190 | !HMAC_Update(ctx.get(), seed2, seed2_len) || |
| 191 | !HMAC_Update(ctx.get(), seed3, seed3_len) || |
| 192 | !HMAC_Final(ctx.get(), hmac, &len)) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 193 | goto err; |
| 194 | } |
| 195 | assert(len == chunk); |
| 196 | |
| 197 | /* XOR the result into |out|. */ |
| 198 | if (len > out_len) { |
| 199 | len = out_len; |
| 200 | } |
| 201 | unsigned i; |
| 202 | for (i = 0; i < len; i++) { |
| 203 | out[i] ^= hmac[i]; |
| 204 | } |
| 205 | out += len; |
| 206 | out_len -= len; |
| 207 | |
| 208 | if (out_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | break; |
| 210 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 211 | |
| 212 | /* Calculate the next A1 value. */ |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 213 | if (!HMAC_Final(ctx_tmp.get(), A1, &A1_len)) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 214 | goto err; |
| 215 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | ret = 1; |
| 219 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 221 | OPENSSL_cleanse(A1, sizeof(A1)); |
| 222 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | } |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame] | 224 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 225 | int tls1_prf(const EVP_MD *digest, uint8_t *out, size_t out_len, |
| 226 | const uint8_t *secret, size_t secret_len, const char *label, |
| 227 | size_t label_len, const uint8_t *seed1, size_t seed1_len, |
| 228 | const uint8_t *seed2, size_t seed2_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 229 | if (out_len == 0) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 230 | return 1; |
| 231 | } |
| 232 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 233 | OPENSSL_memset(out, 0, out_len); |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 234 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 235 | if (digest == EVP_md5_sha1()) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 236 | /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and |
| 237 | * MD5, MD5 first. */ |
| 238 | size_t secret_half = secret_len - (secret_len / 2); |
| 239 | if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, |
| 240 | (const uint8_t *)label, label_len, seed1, seed1_len, seed2, |
| 241 | seed2_len)) { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* Note that, if |secret_len| is odd, the two halves share a byte. */ |
| 246 | secret = secret + (secret_len - secret_half); |
| 247 | secret_len = secret_half; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 248 | |
| 249 | digest = EVP_sha1(); |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 252 | if (!tls1_P_hash(out, out_len, digest, secret, secret_len, |
| 253 | (const uint8_t *)label, label_len, seed1, seed1_len, seed2, |
| 254 | seed2_len)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 255 | return 0; |
| 256 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 258 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 261 | static int ssl3_prf(uint8_t *out, size_t out_len, const uint8_t *secret, |
| 262 | size_t secret_len, const char *label, size_t label_len, |
| 263 | const uint8_t *seed1, size_t seed1_len, |
| 264 | const uint8_t *seed2, size_t seed2_len) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 265 | ScopedEVP_MD_CTX md5; |
| 266 | ScopedEVP_MD_CTX sha1; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 267 | uint8_t buf[16], smd[SHA_DIGEST_LENGTH]; |
| 268 | uint8_t c = 'A'; |
| 269 | size_t i, j, k; |
| 270 | |
| 271 | k = 0; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 272 | for (i = 0; i < out_len; i += MD5_DIGEST_LENGTH) { |
| 273 | k++; |
| 274 | if (k > sizeof(buf)) { |
| 275 | /* bug: 'buf' is too small for this ciphersuite */ |
| 276 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | for (j = 0; j < k; j++) { |
| 281 | buf[j] = c; |
| 282 | } |
| 283 | c++; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 284 | if (!EVP_DigestInit_ex(sha1.get(), EVP_sha1(), NULL)) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 285 | OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP); |
| 286 | return 0; |
| 287 | } |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 288 | EVP_DigestUpdate(sha1.get(), buf, k); |
| 289 | EVP_DigestUpdate(sha1.get(), secret, secret_len); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 290 | /* |label| is ignored for SSLv3. */ |
| 291 | if (seed1_len) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 292 | EVP_DigestUpdate(sha1.get(), seed1, seed1_len); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 293 | } |
| 294 | if (seed2_len) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 295 | EVP_DigestUpdate(sha1.get(), seed2, seed2_len); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 296 | } |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 297 | EVP_DigestFinal_ex(sha1.get(), smd, NULL); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 298 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 299 | if (!EVP_DigestInit_ex(md5.get(), EVP_md5(), NULL)) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 300 | OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP); |
| 301 | return 0; |
| 302 | } |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 303 | EVP_DigestUpdate(md5.get(), secret, secret_len); |
| 304 | EVP_DigestUpdate(md5.get(), smd, SHA_DIGEST_LENGTH); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 305 | if (i + MD5_DIGEST_LENGTH > out_len) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 306 | EVP_DigestFinal_ex(md5.get(), smd, NULL); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 307 | OPENSSL_memcpy(out, smd, out_len - i); |
| 308 | } else { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 309 | EVP_DigestFinal_ex(md5.get(), out, NULL); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | out += MD5_DIGEST_LENGTH; |
| 313 | } |
| 314 | |
| 315 | OPENSSL_cleanse(smd, SHA_DIGEST_LENGTH); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 316 | return 1; |
| 317 | } |
| 318 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 319 | static int tls1_setup_key_block(SSL_HANDSHAKE *hs) { |
| 320 | SSL *const ssl = hs->ssl; |
| 321 | if (hs->key_block_len != 0) { |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | SSL_SESSION *session = ssl->session; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 326 | if (hs->new_session) { |
| 327 | session = hs->new_session.get(); |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | const EVP_AEAD *aead = NULL; |
| 331 | size_t mac_secret_len, fixed_iv_len; |
| 332 | if (session->cipher == NULL || |
| 333 | !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, |
Steven Valdez | 2f3404b | 2017-05-24 16:54:35 -0400 | [diff] [blame] | 334 | session->cipher, ssl3_protocol_version(ssl), |
| 335 | SSL_is_dtls(ssl))) { |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 336 | OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 337 | return 0; |
| 338 | } |
| 339 | size_t key_len = EVP_AEAD_key_length(aead); |
| 340 | if (mac_secret_len > 0) { |
| 341 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 342 | * key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 343 | * bytes and initial implicit IV. */ |
| 344 | if (key_len < mac_secret_len + fixed_iv_len) { |
| 345 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 346 | return 0; |
| 347 | } |
| 348 | key_len -= mac_secret_len + fixed_iv_len; |
| 349 | } |
| 350 | |
| 351 | assert(mac_secret_len < 256); |
| 352 | assert(key_len < 256); |
| 353 | assert(fixed_iv_len < 256); |
| 354 | |
| 355 | ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len; |
| 356 | ssl->s3->tmp.new_key_len = (uint8_t)key_len; |
| 357 | ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len; |
| 358 | |
| 359 | size_t key_block_len = SSL_get_key_block_len(ssl); |
| 360 | |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 361 | uint8_t *keyblock = (uint8_t *)OPENSSL_malloc(key_block_len); |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 362 | if (keyblock == NULL) { |
| 363 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | if (!SSL_generate_key_block(ssl, keyblock, key_block_len)) { |
| 368 | OPENSSL_free(keyblock); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | assert(key_block_len < 256); |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 373 | hs->key_block_len = (uint8_t)key_block_len; |
| 374 | hs->key_block = keyblock; |
David Benjamin | b5172a7 | 2016-11-17 17:01:01 +0900 | [diff] [blame] | 375 | return 1; |
| 376 | } |
| 377 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 378 | int tls1_change_cipher_state(SSL_HANDSHAKE *hs, int which) { |
| 379 | SSL *const ssl = hs->ssl; |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 380 | /* Ensure the key block is set up. */ |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 381 | if (!tls1_setup_key_block(hs)) { |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we |
| 386 | * need to update the read cipherspec. Otherwise we have just written one. */ |
| 387 | const char is_read = (which & SSL3_CC_READ) != 0; |
| 388 | /* use_client_keys is true if we wish to use the keys for the "client write" |
| 389 | * direction. This is the case if we're a client sending a ChangeCipherSpec, |
| 390 | * or a server reading a client's ChangeCipherSpec. */ |
| 391 | const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || |
| 392 | which == SSL3_CHANGE_CIPHER_SERVER_READ; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 394 | size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len; |
| 395 | size_t key_len = ssl->s3->tmp.new_key_len; |
| 396 | size_t iv_len = ssl->s3->tmp.new_fixed_iv_len; |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 397 | assert((mac_secret_len + key_len + iv_len) * 2 == hs->key_block_len); |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 399 | const uint8_t *key_data = hs->key_block; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 400 | const uint8_t *client_write_mac_secret = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 401 | key_data += mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 402 | const uint8_t *server_write_mac_secret = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 403 | key_data += mac_secret_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 404 | const uint8_t *client_write_key = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 405 | key_data += key_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 406 | const uint8_t *server_write_key = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 407 | key_data += key_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 408 | const uint8_t *client_write_iv = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 409 | key_data += iv_len; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 410 | const uint8_t *server_write_iv = key_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 411 | key_data += iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 412 | |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 413 | const uint8_t *mac_secret, *key, *iv; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 414 | if (use_client_keys) { |
| 415 | mac_secret = client_write_mac_secret; |
| 416 | key = client_write_key; |
| 417 | iv = client_write_iv; |
| 418 | } else { |
| 419 | mac_secret = server_write_mac_secret; |
| 420 | key = server_write_key; |
| 421 | iv = server_write_iv; |
| 422 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 424 | UniquePtr<SSLAEADContext> aead_ctx = SSLAEADContext::Create( |
| 425 | is_read ? evp_aead_open : evp_aead_seal, ssl3_protocol_version(ssl), |
| 426 | SSL_is_dtls(ssl), hs->new_cipher, key, key_len, mac_secret, |
| 427 | mac_secret_len, iv, iv_len); |
| 428 | if (!aead_ctx) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 429 | return 0; |
| 430 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 431 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 432 | if (is_read) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 433 | return ssl->method->set_read_state(ssl, std::move(aead_ctx)); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 434 | } |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 435 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 436 | return ssl->method->set_write_state(ssl, std::move(aead_ctx)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 439 | int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 440 | const uint8_t *premaster, |
David Benjamin | 31b1d81 | 2014-12-23 10:01:09 -0500 | [diff] [blame] | 441 | size_t premaster_len) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 442 | const SSL *ssl = hs->ssl; |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 443 | if (hs->extended_master_secret) { |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 444 | uint8_t digests[EVP_MAX_MD_SIZE]; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 445 | size_t digests_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 446 | if (!hs->transcript.GetHash(digests, &digests_len) || |
| 447 | !tls1_prf(hs->transcript.Digest(), out, SSL3_MASTER_SECRET_SIZE, |
| 448 | premaster, premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST, |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 449 | TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests, |
| 450 | digests_len, NULL, 0)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 451 | return 0; |
| 452 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 453 | } else { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 454 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 455 | if (!ssl3_prf(out, SSL3_MASTER_SECRET_SIZE, premaster, premaster_len, |
| 456 | TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE, |
| 457 | ssl->s3->client_random, SSL3_RANDOM_SIZE, |
| 458 | ssl->s3->server_random, SSL3_RANDOM_SIZE)) { |
| 459 | return 0; |
| 460 | } |
| 461 | } else { |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 462 | if (!tls1_prf(hs->transcript.Digest(), out, SSL3_MASTER_SECRET_SIZE, |
| 463 | premaster, premaster_len, TLS_MD_MASTER_SECRET_CONST, |
| 464 | TLS_MD_MASTER_SECRET_CONST_SIZE, ssl->s3->client_random, |
| 465 | SSL3_RANDOM_SIZE, ssl->s3->server_random, |
| 466 | SSL3_RANDOM_SIZE)) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 467 | return 0; |
| 468 | } |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 469 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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 | return SSL3_MASTER_SECRET_SIZE; |
| 473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 475 | } // namespace bssl |
| 476 | |
| 477 | using namespace bssl; |
| 478 | |
| 479 | size_t SSL_get_key_block_len(const SSL *ssl) { |
| 480 | return 2 * ((size_t)ssl->s3->tmp.new_mac_secret_len + |
| 481 | (size_t)ssl->s3->tmp.new_key_len + |
| 482 | (size_t)ssl->s3->tmp.new_fixed_iv_len); |
| 483 | } |
| 484 | |
| 485 | int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) { |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 486 | const SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 487 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 488 | return ssl3_prf(out, out_len, session->master_key, |
| 489 | session->master_key_length, TLS_MD_KEY_EXPANSION_CONST, |
| 490 | TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, |
| 491 | SSL3_RANDOM_SIZE, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 492 | } |
| 493 | |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 494 | const EVP_MD *digest = SSL_SESSION_get_digest(session); |
| 495 | return tls1_prf(digest, out, out_len, session->master_key, |
| 496 | session->master_key_length, TLS_MD_KEY_EXPANSION_CONST, |
| 497 | TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, |
| 498 | SSL3_RANDOM_SIZE, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 499 | } |
| 500 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 501 | int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, |
| 502 | const char *label, size_t label_len, |
| 503 | const uint8_t *context, size_t context_len, |
| 504 | int use_context) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 505 | if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) { |
David Benjamin | d6e95ee | 2015-05-03 15:34:35 -0400 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
David Benjamin | 7bb1d29 | 2016-11-01 19:45:06 -0400 | [diff] [blame] | 509 | /* Exporters may not be used in the middle of a renegotiation. */ |
| 510 | if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) { |
| 511 | return 0; |
| 512 | } |
| 513 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 514 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 515 | return tls13_export_keying_material(ssl, out, out_len, label, label_len, |
| 516 | context, context_len, use_context); |
| 517 | } |
| 518 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 519 | size_t seed_len = 2 * SSL3_RANDOM_SIZE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 520 | if (use_context) { |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 521 | if (context_len >= 1u << 16) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 522 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 523 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 524 | } |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 525 | seed_len += 2 + context_len; |
| 526 | } |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 527 | uint8_t *seed = (uint8_t *)OPENSSL_malloc(seed_len); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 528 | if (seed == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 529 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 530 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 531 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 532 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 533 | OPENSSL_memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 534 | OPENSSL_memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random, |
| 535 | SSL3_RANDOM_SIZE); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 536 | if (use_context) { |
| 537 | seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8); |
| 538 | seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 539 | OPENSSL_memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); |
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 | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 542 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 543 | const EVP_MD *digest = SSL_SESSION_get_digest(session); |
| 544 | int ret = tls1_prf(digest, out, out_len, session->master_key, |
| 545 | session->master_key_length, label, label_len, seed, |
| 546 | seed_len, NULL, 0); |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 547 | OPENSSL_free(seed); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 548 | return ret; |
| 549 | } |