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 | |
| 136 | #include <stdio.h> |
| 137 | #include <assert.h> |
| 138 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | #include <openssl/err.h> |
| 140 | #include <openssl/evp.h> |
| 141 | #include <openssl/hmac.h> |
| 142 | #include <openssl/md5.h> |
| 143 | #include <openssl/mem.h> |
| 144 | #include <openssl/obj.h> |
| 145 | #include <openssl/rand.h> |
| 146 | |
| 147 | #include "ssl_locl.h" |
| 148 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 150 | /* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246, |
| 151 | * section 5. It writes |out_len| bytes to |out|, using |md| as the hash and |
| 152 | * |secret| as the secret. |seed1| through |seed3| are concatenated to form the |
| 153 | * seed parameter. It returns one on success and zero on failure. */ |
| 154 | static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, |
| 155 | const uint8_t *secret, size_t secret_len, |
| 156 | const uint8_t *seed1, size_t seed1_len, |
| 157 | const uint8_t *seed2, size_t seed2_len, |
| 158 | const uint8_t *seed3, size_t seed3_len) { |
| 159 | size_t chunk; |
| 160 | HMAC_CTX ctx, ctx_tmp, ctx_init; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 161 | uint8_t A1[EVP_MAX_MD_SIZE]; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 162 | unsigned A1_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 163 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | chunk = EVP_MD_size(md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 167 | HMAC_CTX_init(&ctx); |
| 168 | HMAC_CTX_init(&ctx_tmp); |
| 169 | HMAC_CTX_init(&ctx_init); |
| 170 | if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) || |
| 171 | !HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
| 172 | (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) || |
| 173 | (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) || |
| 174 | (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len)) || |
| 175 | !HMAC_Final(&ctx, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 176 | goto err; |
| 177 | } |
| 178 | |
| 179 | for (;;) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 180 | /* Reinit mac contexts. */ |
| 181 | if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) || |
| 182 | !HMAC_Update(&ctx, A1, A1_len) || |
| 183 | (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) || |
| 184 | (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) || |
| 185 | (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) || |
| 186 | (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 187 | goto err; |
| 188 | } |
| 189 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 190 | if (out_len > chunk) { |
| 191 | unsigned len; |
| 192 | if (!HMAC_Final(&ctx, out, &len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | goto err; |
| 194 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 195 | assert(len == chunk); |
| 196 | out += len; |
| 197 | out_len -= len; |
| 198 | /* Calculate the next A1 value. */ |
| 199 | if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 200 | goto err; |
| 201 | } |
| 202 | } else { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 203 | /* Last chunk. */ |
| 204 | if (!HMAC_Final(&ctx, A1, &A1_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 205 | goto err; |
| 206 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 207 | memcpy(out, A1, out_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | ret = 1; |
| 213 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | err: |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 215 | HMAC_CTX_cleanup(&ctx); |
| 216 | HMAC_CTX_cleanup(&ctx_tmp); |
| 217 | HMAC_CTX_cleanup(&ctx_init); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 218 | OPENSSL_cleanse(A1, sizeof(A1)); |
| 219 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | } |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame] | 221 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 222 | int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret, |
| 223 | size_t secret_len, const char *label, size_t label_len, |
| 224 | const uint8_t *seed1, size_t seed1_len, |
| 225 | const uint8_t *seed2, size_t seed2_len) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 226 | size_t idx, len, count, i; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 227 | const uint8_t *S1; |
| 228 | long m; |
| 229 | const EVP_MD *md; |
| 230 | int ret = 0; |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 231 | uint8_t *tmp; |
| 232 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 233 | if (out_len == 0) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | /* Allocate a temporary buffer. */ |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 238 | tmp = OPENSSL_malloc(out_len); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 239 | if (tmp == NULL) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 240 | OPENSSL_PUT_ERROR(SSL, tls1_prf, ERR_R_MALLOC_FAILURE); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 241 | return 0; |
| 242 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 243 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 244 | /* Count number of digests and partition |secret| evenly. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 245 | count = 0; |
| 246 | for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 247 | if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 248 | count++; |
| 249 | } |
| 250 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 251 | /* TODO(davidben): The only case where count isn't 1 is the old MD5/SHA-1 |
| 252 | * combination. The logic around multiple handshake digests can probably be |
| 253 | * simplified. */ |
| 254 | assert(count == 1 || count == 2); |
| 255 | len = secret_len / count; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 256 | if (count == 1) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 257 | secret_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | } |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 259 | S1 = secret; |
| 260 | memset(out, 0, out_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 261 | for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 262 | if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 263 | /* If |count| is 2 and |secret_len| is odd, |secret| is partitioned into |
| 264 | * two halves with an overlapping byte. */ |
| 265 | if (!tls1_P_hash(tmp, out_len, md, S1, len + (secret_len & 1), |
| 266 | (const uint8_t *)label, label_len, seed1, seed1_len, |
| 267 | seed2, seed2_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | goto err; |
| 269 | } |
| 270 | S1 += len; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 271 | for (i = 0; i < out_len; i++) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 272 | out[i] ^= tmp[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | ret = 1; |
| 277 | |
| 278 | err: |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 279 | OPENSSL_cleanse(tmp, out_len); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 280 | OPENSSL_free(tmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 281 | return ret; |
| 282 | } |
| 283 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 284 | 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] | 285 | return s->enc_method->prf(s, out, out_len, s->session->master_key, |
| 286 | s->session->master_key_length, |
| 287 | TLS_MD_KEY_EXPANSION_CONST, |
| 288 | TLS_MD_KEY_EXPANSION_CONST_SIZE, |
| 289 | s->s3->server_random, SSL3_RANDOM_SIZE, |
| 290 | s->s3->client_random, |
| 291 | SSL3_RANDOM_SIZE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 292 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | /* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It |
| 295 | * returns 0 on malloc error. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 296 | static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx) { |
| 297 | if (*aead_ctx != NULL) { |
| 298 | EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx); |
| 299 | } else { |
| 300 | *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX)); |
| 301 | if (*aead_ctx == NULL) { |
| 302 | OPENSSL_PUT_ERROR(SSL, tls1_aead_ctx_init, ERR_R_MALLOC_FAILURE); |
| 303 | return 0; |
| 304 | } |
| 305 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 306 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | return 1; |
| 308 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | |
| 310 | static int tls1_change_cipher_state_aead(SSL *s, char is_read, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 311 | const uint8_t *key, unsigned key_len, |
| 312 | const uint8_t *iv, unsigned iv_len, |
| 313 | const uint8_t *mac_secret, |
| 314 | unsigned mac_secret_len) { |
| 315 | const EVP_AEAD *aead = s->s3->tmp.new_aead; |
| 316 | SSL_AEAD_CTX *aead_ctx; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 317 | /* merged_key is used to merge the MAC, cipher, and IV keys for an AEAD which |
| 318 | * simulates pre-AEAD cipher suites. */ |
| 319 | uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH]; |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame] | 320 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 321 | if (mac_secret_len > 0) { |
| 322 | /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher |
| 323 | * suites). */ |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 324 | if (mac_secret_len + key_len + iv_len > sizeof(merged_key)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 325 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, |
| 326 | ERR_R_INTERNAL_ERROR); |
| 327 | return 0; |
| 328 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 329 | memcpy(merged_key, mac_secret, mac_secret_len); |
| 330 | memcpy(merged_key + mac_secret_len, key, key_len); |
| 331 | memcpy(merged_key + mac_secret_len + key_len, iv, iv_len); |
| 332 | key = merged_key; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 333 | key_len += mac_secret_len; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 334 | key_len += iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 335 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 337 | if (is_read) { |
| 338 | if (!tls1_aead_ctx_init(&s->aead_read_ctx)) { |
| 339 | return 0; |
| 340 | } |
| 341 | aead_ctx = s->aead_read_ctx; |
| 342 | } else { |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 343 | /* When updating the cipher state for DTLS, we do not wish to overwrite the |
| 344 | * old ones because DTLS stores pointers to them in order to implement |
| 345 | * retransmission. See dtls1_hm_fragment_free. |
| 346 | * |
| 347 | * TODO(davidben): Simplify aead_write_ctx ownership, probably by just |
| 348 | * forbidding DTLS renego. */ |
| 349 | if (SSL_IS_DTLS(s)) { |
| 350 | s->aead_write_ctx = NULL; |
| 351 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 352 | if (!tls1_aead_ctx_init(&s->aead_write_ctx)) { |
| 353 | return 0; |
| 354 | } |
| 355 | aead_ctx = s->aead_write_ctx; |
| 356 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 358 | if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len, |
| 359 | EVP_AEAD_DEFAULT_TAG_LENGTH, NULL /* engine */)) { |
| 360 | OPENSSL_free(aead_ctx); |
| 361 | if (is_read) { |
| 362 | s->aead_read_ctx = NULL; |
| 363 | } else { |
| 364 | s->aead_write_ctx = NULL; |
| 365 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 366 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 367 | return 0; |
| 368 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 370 | if (mac_secret_len == 0) { |
| 371 | /* For a real AEAD, the IV is the fixed part of the nonce. */ |
| 372 | if (iv_len > sizeof(aead_ctx->fixed_nonce)) { |
| 373 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR); |
| 374 | return 0; |
| 375 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 376 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 377 | memcpy(aead_ctx->fixed_nonce, iv, iv_len); |
| 378 | aead_ctx->fixed_nonce_len = iv_len; |
| 379 | aead_ctx->variable_nonce_included_in_record = |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | (s->s3->tmp.new_cipher->algorithm2 & |
| 381 | SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD) != 0; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 382 | aead_ctx->random_variable_nonce = 0; |
| 383 | aead_ctx->omit_length_in_ad = 0; |
| 384 | } else { |
| 385 | aead_ctx->fixed_nonce_len = 0; |
| 386 | aead_ctx->variable_nonce_included_in_record = 1; |
| 387 | aead_ctx->random_variable_nonce = 1; |
| 388 | aead_ctx->omit_length_in_ad = 1; |
| 389 | } |
| 390 | aead_ctx->variable_nonce_len = s->s3->tmp.new_variable_iv_len; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 391 | aead_ctx->omit_version_in_ad = (s->version == SSL3_VERSION); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 392 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 393 | if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len != |
| 394 | EVP_AEAD_nonce_length(aead)) { |
| 395 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR); |
| 396 | return 0; |
| 397 | } |
| 398 | aead_ctx->tag_len = EVP_AEAD_max_overhead(aead); |
| 399 | |
| 400 | return 1; |
| 401 | } |
| 402 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 403 | int tls1_change_cipher_state(SSL *s, int which) { |
| 404 | /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we |
| 405 | * need to update the read cipherspec. Otherwise we have just written one. */ |
| 406 | const char is_read = (which & SSL3_CC_READ) != 0; |
| 407 | /* use_client_keys is true if we wish to use the keys for the "client write" |
| 408 | * direction. This is the case if we're a client sending a ChangeCipherSpec, |
| 409 | * or a server reading a client's ChangeCipherSpec. */ |
| 410 | const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || |
| 411 | which == SSL3_CHANGE_CIPHER_SERVER_READ; |
| 412 | const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret; |
| 413 | const uint8_t *client_write_key, *server_write_key, *key; |
| 414 | const uint8_t *client_write_iv, *server_write_iv, *iv; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 415 | const EVP_AEAD *aead = s->s3->tmp.new_aead; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 416 | size_t key_len, iv_len, mac_secret_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 417 | const uint8_t *key_data; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 419 | /* Reset sequence number to zero. */ |
| 420 | if (!SSL_IS_DTLS(s)) { |
| 421 | memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8); |
| 422 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 424 | mac_secret_len = s->s3->tmp.new_mac_secret_len; |
| 425 | iv_len = s->s3->tmp.new_fixed_iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 427 | if (aead == NULL) { |
| 428 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | key_len = EVP_AEAD_key_length(aead); |
| 433 | if (mac_secret_len > 0) { |
| 434 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher |
| 435 | * suites) the key length reported by |EVP_AEAD_key_length| will |
| 436 | * include the MAC and IV key bytes. */ |
| 437 | if (key_len < mac_secret_len + iv_len) { |
| 438 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 439 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 440 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 441 | key_len -= mac_secret_len + iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 442 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 444 | key_data = s->s3->tmp.key_block; |
| 445 | client_write_mac_secret = key_data; |
| 446 | key_data += mac_secret_len; |
| 447 | server_write_mac_secret = key_data; |
| 448 | key_data += mac_secret_len; |
| 449 | client_write_key = key_data; |
| 450 | key_data += key_len; |
| 451 | server_write_key = key_data; |
| 452 | key_data += key_len; |
| 453 | client_write_iv = key_data; |
| 454 | key_data += iv_len; |
| 455 | server_write_iv = key_data; |
| 456 | key_data += iv_len; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 458 | if (use_client_keys) { |
| 459 | mac_secret = client_write_mac_secret; |
| 460 | key = client_write_key; |
| 461 | iv = client_write_iv; |
| 462 | } else { |
| 463 | mac_secret = server_write_mac_secret; |
| 464 | key = server_write_key; |
| 465 | iv = server_write_iv; |
| 466 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 467 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 468 | if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) { |
| 469 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 470 | return 0; |
| 471 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 473 | return tls1_change_cipher_state_aead(s, is_read, key, key_len, iv, iv_len, |
| 474 | mac_secret, mac_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 477 | int tls1_setup_key_block(SSL *s) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 478 | uint8_t *p; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 479 | const EVP_AEAD *aead = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 480 | int ret = 0; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 481 | 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] | 482 | size_t key_block_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 484 | if (s->s3->tmp.key_block_length != 0) { |
| 485 | return 1; |
| 486 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 487 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 488 | if (s->session->cipher == NULL) { |
| 489 | goto cipher_unavailable_err; |
| 490 | } |
| 491 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 492 | if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, |
| 493 | s->session->cipher, |
| 494 | ssl3_version_from_wire(s, s->version))) { |
| 495 | goto cipher_unavailable_err; |
| 496 | } |
| 497 | key_len = EVP_AEAD_key_length(aead); |
| 498 | variable_iv_len = EVP_AEAD_nonce_length(aead); |
| 499 | if (mac_secret_len > 0) { |
| 500 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the |
| 501 | * key length reported by |EVP_AEAD_key_length| will include the MAC key |
| 502 | * bytes and initial implicit IV. */ |
| 503 | if (key_len < mac_secret_len + fixed_iv_len) { |
| 504 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR); |
| 505 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 506 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 507 | key_len -= mac_secret_len + fixed_iv_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 508 | } else { |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 509 | /* The nonce is split into a fixed portion and a variable portion. */ |
| 510 | if (variable_iv_len < fixed_iv_len) { |
| 511 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR); |
| 512 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 513 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 514 | variable_iv_len -= fixed_iv_len; |
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 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 517 | assert(mac_secret_len < 256); |
| 518 | assert(fixed_iv_len < 256); |
| 519 | assert(variable_iv_len < 256); |
| 520 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 521 | s->s3->tmp.new_aead = aead; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 522 | s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len; |
| 523 | s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len; |
| 524 | s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 526 | key_block_len = key_len + mac_secret_len + fixed_iv_len; |
| 527 | key_block_len *= 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 529 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 531 | p = (uint8_t *)OPENSSL_malloc(key_block_len); |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 532 | if (p == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 533 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE); |
| 534 | goto err; |
| 535 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 537 | s->s3->tmp.key_block_length = key_block_len; |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 538 | s->s3->tmp.key_block = p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 539 | |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 540 | if (!tls1_generate_key_block(s, p, key_block_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 541 | goto err; |
| 542 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 544 | if (!SSL_USE_EXPLICIT_IV(s) && |
| 545 | (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0) { |
| 546 | /* enable vulnerability countermeasure for CBC ciphers with known-IV |
| 547 | * problem (http://www.openssl.org/~bodo/tls-cbc.txt). */ |
| 548 | s->s3->need_record_splitting = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 550 | if (s->session->cipher != NULL && |
| 551 | s->session->cipher->algorithm_enc == SSL_RC4) { |
| 552 | s->s3->need_record_splitting = 0; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | ret = 1; |
| 557 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 559 | return ret; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 560 | |
| 561 | cipher_unavailable_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 562 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, |
| 563 | SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 564 | return 0; |
| 565 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 566 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 567 | /* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, |
| 568 | * respectively. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | * |
| 570 | * Returns: |
| 571 | * 0: (in non-constant time) if the record is publically invalid (i.e. too |
| 572 | * short etc). |
| 573 | * 1: if the record's padding is valid / the encryption was successful. |
| 574 | * -1: if the record's padding/AEAD-authenticator is invalid or, if sending, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 575 | * an internal error occured. */ |
| 576 | int tls1_enc(SSL *s, int send) { |
| 577 | SSL3_RECORD *rec; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 578 | const SSL_AEAD_CTX *aead; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 579 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 580 | if (send) { |
| 581 | rec = &s->s3->wrec; |
| 582 | aead = s->aead_write_ctx; |
| 583 | } else { |
| 584 | rec = &s->s3->rrec; |
| 585 | aead = s->aead_read_ctx; |
| 586 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 587 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 588 | if (s->session == NULL || aead == NULL) { |
| 589 | memmove(rec->data, rec->input, rec->length); |
| 590 | rec->input = rec->data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 591 | return 1; |
| 592 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 594 | uint8_t ad[13], *seq, *in, *out, nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 595 | unsigned nonce_used; |
| 596 | size_t n, ad_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 597 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 598 | seq = send ? s->s3->write_sequence : s->s3->read_sequence; |
| 599 | |
| 600 | if (SSL_IS_DTLS(s)) { |
| 601 | uint8_t dtlsseq[9], *p = dtlsseq; |
| 602 | |
| 603 | s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p); |
| 604 | memcpy(p, &seq[2], 6); |
| 605 | memcpy(ad, dtlsseq, 8); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 606 | } else { |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 607 | int i; |
| 608 | memcpy(ad, seq, 8); |
| 609 | for (i = 7; i >= 0; i--) { |
| 610 | ++seq[i]; |
| 611 | if (seq[i] != 0) { |
| 612 | break; |
| 613 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 617 | ad[8] = rec->type; |
| 618 | ad_len = 9; |
| 619 | if (!aead->omit_version_in_ad) { |
| 620 | ad[ad_len++] = (uint8_t)(s->version >> 8); |
| 621 | ad[ad_len++] = (uint8_t)(s->version); |
| 622 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 624 | if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce)) { |
| 625 | OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR); |
| 626 | return -1; /* internal error - should never happen. */ |
| 627 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 629 | memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len); |
| 630 | nonce_used = aead->fixed_nonce_len; |
| 631 | |
| 632 | if (send) { |
| 633 | size_t len = rec->length; |
| 634 | size_t eivlen = 0; |
| 635 | in = rec->input; |
| 636 | out = rec->data; |
| 637 | |
| 638 | uint8_t *variable_nonce = nonce + nonce_used; |
| 639 | if (aead->random_variable_nonce) { |
| 640 | assert(aead->variable_nonce_included_in_record); |
| 641 | if (!RAND_bytes(nonce + nonce_used, aead->variable_nonce_len)) { |
| 642 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 643 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 644 | } else { |
| 645 | /* When sending we use the sequence number as the variable part of the |
| 646 | * nonce. */ |
| 647 | if (aead->variable_nonce_len != 8) { |
| 648 | OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR); |
| 649 | return -1; |
| 650 | } |
| 651 | memcpy(nonce + nonce_used, ad, aead->variable_nonce_len); |
| 652 | } |
| 653 | nonce_used += aead->variable_nonce_len; |
| 654 | |
| 655 | /* in do_ssl3_write, rec->input is moved forward by variable_nonce_len in |
| 656 | * order to leave space for the variable nonce. Thus we can copy the |
| 657 | * sequence number bytes into place without overwriting any of the |
| 658 | * plaintext. */ |
| 659 | if (aead->variable_nonce_included_in_record) { |
| 660 | memcpy(out, variable_nonce, aead->variable_nonce_len); |
| 661 | len -= aead->variable_nonce_len; |
| 662 | eivlen = aead->variable_nonce_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 663 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 664 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 665 | if (!aead->omit_length_in_ad) { |
| 666 | ad[ad_len++] = len >> 8; |
| 667 | ad[ad_len++] = len & 0xff; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 668 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 669 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 670 | if (!EVP_AEAD_CTX_seal(&aead->ctx, out + eivlen, &n, len + aead->tag_len, |
| 671 | nonce, nonce_used, in + eivlen, len, ad, ad_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 672 | return -1; |
| 673 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 675 | if (aead->variable_nonce_included_in_record) { |
| 676 | n += aead->variable_nonce_len; |
| 677 | } |
| 678 | } else { |
| 679 | /* receive */ |
| 680 | size_t len = rec->length; |
| 681 | |
| 682 | if (rec->data != rec->input) { |
| 683 | OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR); |
| 684 | return -1; /* internal error - should never happen. */ |
| 685 | } |
| 686 | out = in = rec->input; |
| 687 | |
| 688 | if (len < aead->variable_nonce_len) { |
| 689 | return 0; |
| 690 | } |
| 691 | memcpy(nonce + nonce_used, |
| 692 | aead->variable_nonce_included_in_record ? in : ad, |
| 693 | aead->variable_nonce_len); |
| 694 | nonce_used += aead->variable_nonce_len; |
| 695 | |
| 696 | if (aead->variable_nonce_included_in_record) { |
| 697 | in += aead->variable_nonce_len; |
| 698 | len -= aead->variable_nonce_len; |
| 699 | out += aead->variable_nonce_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 700 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 701 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 702 | if (!aead->omit_length_in_ad) { |
| 703 | if (len < aead->tag_len) { |
| 704 | return 0; |
| 705 | } |
| 706 | size_t plaintext_len = len - aead->tag_len; |
| 707 | |
| 708 | ad[ad_len++] = plaintext_len >> 8; |
| 709 | ad[ad_len++] = plaintext_len & 0xff; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 710 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 711 | |
| 712 | if (!EVP_AEAD_CTX_open(&aead->ctx, out, &n, rec->length, nonce, nonce_used, in, |
| 713 | len, ad, ad_len)) { |
| 714 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 715 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 716 | |
| 717 | rec->data = rec->input = out; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 718 | } |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame^] | 719 | |
| 720 | rec->length = n; |
| 721 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 722 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 723 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 724 | int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) { |
| 725 | unsigned int ret; |
| 726 | EVP_MD_CTX ctx, *d = NULL; |
| 727 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 729 | if (s->s3->handshake_buffer && |
| 730 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | for (i = 0; i < SSL_MAX_DIGEST; i++) { |
| 735 | if (s->s3->handshake_dgst[i] && |
| 736 | EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) { |
| 737 | d = s->s3->handshake_dgst[i]; |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if (!d) { |
| 743 | OPENSSL_PUT_ERROR(SSL, tls1_cert_verify_mac, SSL_R_NO_REQUIRED_DIGEST); |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | EVP_MD_CTX_init(&ctx); |
| 748 | EVP_MD_CTX_copy_ex(&ctx, d); |
| 749 | EVP_DigestFinal_ex(&ctx, out, &ret); |
| 750 | EVP_MD_CTX_cleanup(&ctx); |
| 751 | |
| 752 | return ret; |
| 753 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 754 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 755 | /* tls1_handshake_digest calculates the current handshake hash and writes it to |
| 756 | * |out|, which has space for |out_len| bytes. It returns the number of bytes |
| 757 | * written or -1 in the event of an error. This function works on a copy of the |
| 758 | * underlying digests so can be called multiple times and prior to the final |
| 759 | * update etc. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 760 | int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) { |
| 761 | const EVP_MD *md; |
| 762 | EVP_MD_CTX ctx; |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 763 | int err = 0, len = 0; |
| 764 | size_t i; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 765 | long mask; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 766 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 767 | EVP_MD_CTX_init(&ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 768 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 769 | for (i = 0; ssl_get_handshake_digest(i, &mask, &md); i++) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 770 | size_t hash_size; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 771 | unsigned int digest_len; |
| 772 | EVP_MD_CTX *hdgst = s->s3->handshake_dgst[i]; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 773 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 774 | if ((mask & ssl_get_algorithm2(s)) == 0) { |
| 775 | continue; |
| 776 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 777 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 778 | hash_size = EVP_MD_size(md); |
| 779 | if (!hdgst || |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 780 | hash_size > out_len || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 781 | !EVP_MD_CTX_copy_ex(&ctx, hdgst) || |
| 782 | !EVP_DigestFinal_ex(&ctx, out, &digest_len) || |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 783 | digest_len != hash_size /* internal error */) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 784 | err = 1; |
| 785 | break; |
| 786 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 787 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 788 | out += digest_len; |
| 789 | out_len -= digest_len; |
| 790 | len += digest_len; |
| 791 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 792 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 793 | EVP_MD_CTX_cleanup(&ctx); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 795 | if (err != 0) { |
| 796 | return -1; |
| 797 | } |
| 798 | return len; |
| 799 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 800 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 801 | int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) { |
| 802 | uint8_t buf[2 * EVP_MAX_MD_SIZE]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 803 | int err = 0; |
| 804 | int digests_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 805 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 806 | if (s->s3->handshake_buffer && |
| 807 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 808 | return 0; |
| 809 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 810 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 811 | digests_len = tls1_handshake_digest(s, buf, sizeof(buf)); |
| 812 | if (digests_len < 0) { |
| 813 | err = 1; |
| 814 | digests_len = 0; |
| 815 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 816 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 817 | if (!s->enc_method->prf(s, out, 12, s->session->master_key, |
| 818 | s->session->master_key_length, str, slen, buf, |
| 819 | digests_len, NULL, 0)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 820 | err = 1; |
| 821 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 822 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 823 | if (err) { |
| 824 | return 0; |
| 825 | } else { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 826 | return 12; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 827 | } |
| 828 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 829 | |
David Benjamin | 31b1d81 | 2014-12-23 10:01:09 -0500 | [diff] [blame] | 830 | int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster, |
| 831 | size_t premaster_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 832 | if (s->s3->tmp.extended_master_secret) { |
| 833 | uint8_t digests[2 * EVP_MAX_MD_SIZE]; |
| 834 | int digests_len; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 835 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 836 | /* The master secret is based on the handshake hash just after sending the |
| 837 | * ClientKeyExchange. However, we might have a client certificate to send, |
| 838 | * in which case we might need different hashes for the verification and |
| 839 | * thus still need the handshake buffer around. Keeping both a handshake |
| 840 | * buffer *and* running hashes isn't yet supported so, when it comes to |
| 841 | * calculating the Finished hash, we'll have to hash the handshake buffer |
| 842 | * again. */ |
| 843 | if (s->s3->handshake_buffer && |
| 844 | !ssl3_digest_cached_records(s, dont_free_handshake_buffer)) { |
| 845 | return 0; |
| 846 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 847 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 848 | digests_len = tls1_handshake_digest(s, digests, sizeof(digests)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 849 | if (digests_len == -1) { |
| 850 | return 0; |
| 851 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 852 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 853 | if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 854 | premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST, |
| 855 | TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests, |
| 856 | digests_len, NULL, 0)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 857 | return 0; |
| 858 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 859 | } else { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 860 | if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster, |
| 861 | premaster_len, TLS_MD_MASTER_SECRET_CONST, |
| 862 | TLS_MD_MASTER_SECRET_CONST_SIZE, |
| 863 | s->s3->client_random, SSL3_RANDOM_SIZE, |
| 864 | s->s3->server_random, SSL3_RANDOM_SIZE)) { |
David Benjamin | af032d6 | 2014-12-22 10:42:51 -0500 | [diff] [blame] | 865 | return 0; |
| 866 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 867 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 868 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 869 | return SSL3_MASTER_SECRET_SIZE; |
| 870 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 871 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 872 | int tls1_export_keying_material(SSL *s, uint8_t *out, size_t olen, |
| 873 | const char *label, size_t llen, |
| 874 | const uint8_t *context, size_t contextlen, |
| 875 | int use_context) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 876 | uint8_t *val = NULL; |
| 877 | size_t vallen, currentvalpos; |
| 878 | int ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 879 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 880 | /* construct PRF arguments we construct the PRF argument ourself rather than |
| 881 | * passing separate values into the TLS PRF to ensure that the concatenation |
| 882 | * of values does not create a prohibited label. */ |
| 883 | vallen = llen + SSL3_RANDOM_SIZE * 2; |
| 884 | if (use_context) { |
| 885 | vallen += 2 + contextlen; |
| 886 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 887 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 888 | val = OPENSSL_malloc(vallen); |
| 889 | if (val == NULL) { |
| 890 | goto err2; |
| 891 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 892 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 893 | currentvalpos = 0; |
| 894 | memcpy(val + currentvalpos, (uint8_t *)label, llen); |
| 895 | currentvalpos += llen; |
| 896 | memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE); |
| 897 | currentvalpos += SSL3_RANDOM_SIZE; |
| 898 | memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE); |
| 899 | currentvalpos += SSL3_RANDOM_SIZE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 900 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 901 | if (use_context) { |
| 902 | val[currentvalpos] = (contextlen >> 8) & 0xff; |
| 903 | currentvalpos++; |
| 904 | val[currentvalpos] = contextlen & 0xff; |
| 905 | currentvalpos++; |
| 906 | if (contextlen > 0 || context != NULL) { |
| 907 | memcpy(val + currentvalpos, context, contextlen); |
| 908 | } |
| 909 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 911 | /* disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited |
| 912 | * label len) = 15, so size of val > max(prohibited label len) = 15 and the |
| 913 | * comparisons won't have buffer overflow. */ |
| 914 | if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST, |
| 915 | TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0 || |
| 916 | memcmp(val, TLS_MD_SERVER_FINISH_CONST, |
| 917 | TLS_MD_SERVER_FINISH_CONST_SIZE) == 0 || |
| 918 | memcmp(val, TLS_MD_MASTER_SECRET_CONST, |
| 919 | TLS_MD_MASTER_SECRET_CONST_SIZE) == 0 || |
| 920 | memcmp(val, TLS_MD_KEY_EXPANSION_CONST, |
| 921 | TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) { |
| 922 | goto err1; |
| 923 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 924 | |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 925 | /* SSL_export_keying_material is not implemented for SSLv3, so passing |
| 926 | * everything through the label parameter works. */ |
| 927 | assert(s->version != SSL3_VERSION); |
| 928 | ret = s->enc_method->prf(s, out, olen, s->session->master_key, |
| 929 | s->session->master_key_length, (const char *)val, |
| 930 | vallen, NULL, 0, NULL, 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 931 | goto out; |
| 932 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 933 | err1: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 934 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, |
| 935 | SSL_R_TLS_ILLEGAL_EXPORTER_LABEL); |
| 936 | ret = 0; |
| 937 | goto out; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 938 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 939 | err2: |
| 940 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_MALLOC_FAILURE); |
| 941 | ret = 0; |
| 942 | |
| 943 | out: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 944 | if (val != NULL) { |
| 945 | OPENSSL_free(val); |
| 946 | } |
| 947 | |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | int tls1_alert_code(int code) { |
| 952 | switch (code) { |
| 953 | case SSL_AD_CLOSE_NOTIFY: |
| 954 | return SSL3_AD_CLOSE_NOTIFY; |
| 955 | |
| 956 | case SSL_AD_UNEXPECTED_MESSAGE: |
| 957 | return SSL3_AD_UNEXPECTED_MESSAGE; |
| 958 | |
| 959 | case SSL_AD_BAD_RECORD_MAC: |
| 960 | return SSL3_AD_BAD_RECORD_MAC; |
| 961 | |
| 962 | case SSL_AD_DECRYPTION_FAILED: |
| 963 | return TLS1_AD_DECRYPTION_FAILED; |
| 964 | |
| 965 | case SSL_AD_RECORD_OVERFLOW: |
| 966 | return TLS1_AD_RECORD_OVERFLOW; |
| 967 | |
| 968 | case SSL_AD_DECOMPRESSION_FAILURE: |
| 969 | return SSL3_AD_DECOMPRESSION_FAILURE; |
| 970 | |
| 971 | case SSL_AD_HANDSHAKE_FAILURE: |
| 972 | return SSL3_AD_HANDSHAKE_FAILURE; |
| 973 | |
| 974 | case SSL_AD_NO_CERTIFICATE: |
| 975 | return -1; |
| 976 | |
| 977 | case SSL_AD_BAD_CERTIFICATE: |
| 978 | return SSL3_AD_BAD_CERTIFICATE; |
| 979 | |
| 980 | case SSL_AD_UNSUPPORTED_CERTIFICATE: |
| 981 | return SSL3_AD_UNSUPPORTED_CERTIFICATE; |
| 982 | |
| 983 | case SSL_AD_CERTIFICATE_REVOKED: |
| 984 | return SSL3_AD_CERTIFICATE_REVOKED; |
| 985 | |
| 986 | case SSL_AD_CERTIFICATE_EXPIRED: |
| 987 | return SSL3_AD_CERTIFICATE_EXPIRED; |
| 988 | |
| 989 | case SSL_AD_CERTIFICATE_UNKNOWN: |
| 990 | return SSL3_AD_CERTIFICATE_UNKNOWN; |
| 991 | |
| 992 | case SSL_AD_ILLEGAL_PARAMETER: |
| 993 | return SSL3_AD_ILLEGAL_PARAMETER; |
| 994 | |
| 995 | case SSL_AD_UNKNOWN_CA: |
| 996 | return TLS1_AD_UNKNOWN_CA; |
| 997 | |
| 998 | case SSL_AD_ACCESS_DENIED: |
| 999 | return TLS1_AD_ACCESS_DENIED; |
| 1000 | |
| 1001 | case SSL_AD_DECODE_ERROR: |
| 1002 | return TLS1_AD_DECODE_ERROR; |
| 1003 | |
| 1004 | case SSL_AD_DECRYPT_ERROR: |
| 1005 | return TLS1_AD_DECRYPT_ERROR; |
| 1006 | case SSL_AD_EXPORT_RESTRICTION: |
| 1007 | return TLS1_AD_EXPORT_RESTRICTION; |
| 1008 | |
| 1009 | case SSL_AD_PROTOCOL_VERSION: |
| 1010 | return TLS1_AD_PROTOCOL_VERSION; |
| 1011 | |
| 1012 | case SSL_AD_INSUFFICIENT_SECURITY: |
| 1013 | return TLS1_AD_INSUFFICIENT_SECURITY; |
| 1014 | |
| 1015 | case SSL_AD_INTERNAL_ERROR: |
| 1016 | return TLS1_AD_INTERNAL_ERROR; |
| 1017 | |
| 1018 | case SSL_AD_USER_CANCELLED: |
| 1019 | return TLS1_AD_USER_CANCELLED; |
| 1020 | |
| 1021 | case SSL_AD_NO_RENEGOTIATION: |
| 1022 | return TLS1_AD_NO_RENEGOTIATION; |
| 1023 | |
| 1024 | case SSL_AD_UNSUPPORTED_EXTENSION: |
| 1025 | return TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1026 | |
| 1027 | case SSL_AD_CERTIFICATE_UNOBTAINABLE: |
| 1028 | return TLS1_AD_CERTIFICATE_UNOBTAINABLE; |
| 1029 | |
| 1030 | case SSL_AD_UNRECOGNIZED_NAME: |
| 1031 | return TLS1_AD_UNRECOGNIZED_NAME; |
| 1032 | |
| 1033 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: |
| 1034 | return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 1035 | |
| 1036 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: |
| 1037 | return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE; |
| 1038 | |
| 1039 | case SSL_AD_UNKNOWN_PSK_IDENTITY: |
| 1040 | return TLS1_AD_UNKNOWN_PSK_IDENTITY; |
| 1041 | |
| 1042 | case SSL_AD_INAPPROPRIATE_FALLBACK: |
| 1043 | return SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 1044 | |
| 1045 | default: |
| 1046 | return -1; |
| 1047 | } |
| 1048 | } |