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 | |
| 149 | /* seed1 through seed5 are virtually concatenated */ |
| 150 | static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec, |
| 151 | int sec_len, |
| 152 | const void *seed1, int seed1_len, |
| 153 | const void *seed2, int seed2_len, |
| 154 | const void *seed3, int seed3_len, |
| 155 | const void *seed4, int seed4_len, |
| 156 | const void *seed5, int seed5_len, |
| 157 | unsigned char *out, int olen) |
| 158 | { |
| 159 | int chunk; |
| 160 | size_t j; |
| 161 | EVP_MD_CTX ctx, ctx_tmp, ctx_init; |
| 162 | EVP_PKEY *mac_key; |
| 163 | unsigned char A1[EVP_MAX_MD_SIZE]; |
| 164 | size_t A1_len; |
| 165 | int ret = 0; |
| 166 | |
| 167 | chunk=EVP_MD_size(md); |
| 168 | |
| 169 | EVP_MD_CTX_init(&ctx); |
| 170 | EVP_MD_CTX_init(&ctx_tmp); |
| 171 | EVP_MD_CTX_init(&ctx_init); |
| 172 | mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len); |
| 173 | if (!mac_key) |
| 174 | goto err; |
| 175 | if (!EVP_DigestSignInit(&ctx_init,NULL,md, NULL, mac_key)) |
| 176 | goto err; |
| 177 | if (!EVP_MD_CTX_copy_ex(&ctx,&ctx_init)) |
| 178 | goto err; |
| 179 | if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len)) |
| 180 | goto err; |
| 181 | if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len)) |
| 182 | goto err; |
| 183 | if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len)) |
| 184 | goto err; |
| 185 | if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len)) |
| 186 | goto err; |
| 187 | if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len)) |
| 188 | goto err; |
| 189 | if (!EVP_DigestSignFinal(&ctx,A1,&A1_len)) |
| 190 | goto err; |
| 191 | |
| 192 | for (;;) |
| 193 | { |
| 194 | /* Reinit mac contexts */ |
| 195 | if (!EVP_MD_CTX_copy_ex(&ctx,&ctx_init)) |
| 196 | goto err; |
| 197 | if (!EVP_DigestSignUpdate(&ctx,A1,A1_len)) |
| 198 | goto err; |
| 199 | if (olen>chunk && !EVP_MD_CTX_copy_ex(&ctx_tmp,&ctx)) |
| 200 | goto err; |
| 201 | if (seed1 && !EVP_DigestSignUpdate(&ctx,seed1,seed1_len)) |
| 202 | goto err; |
| 203 | if (seed2 && !EVP_DigestSignUpdate(&ctx,seed2,seed2_len)) |
| 204 | goto err; |
| 205 | if (seed3 && !EVP_DigestSignUpdate(&ctx,seed3,seed3_len)) |
| 206 | goto err; |
| 207 | if (seed4 && !EVP_DigestSignUpdate(&ctx,seed4,seed4_len)) |
| 208 | goto err; |
| 209 | if (seed5 && !EVP_DigestSignUpdate(&ctx,seed5,seed5_len)) |
| 210 | goto err; |
| 211 | |
| 212 | if (olen > chunk) |
| 213 | { |
| 214 | if (!EVP_DigestSignFinal(&ctx,out,&j)) |
| 215 | goto err; |
| 216 | out+=j; |
| 217 | olen-=j; |
| 218 | /* calc the next A1 value */ |
| 219 | if (!EVP_DigestSignFinal(&ctx_tmp,A1,&A1_len)) |
| 220 | goto err; |
| 221 | } |
| 222 | else /* last one */ |
| 223 | { |
| 224 | if (!EVP_DigestSignFinal(&ctx,A1,&A1_len)) |
| 225 | goto err; |
| 226 | memcpy(out,A1,olen); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | ret = 1; |
| 231 | err: |
| 232 | EVP_PKEY_free(mac_key); |
| 233 | EVP_MD_CTX_cleanup(&ctx); |
| 234 | EVP_MD_CTX_cleanup(&ctx_tmp); |
| 235 | EVP_MD_CTX_cleanup(&ctx_init); |
| 236 | OPENSSL_cleanse(A1,sizeof(A1)); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | /* seed1 through seed5 are virtually concatenated */ |
| 241 | static int tls1_PRF(long digest_mask, |
| 242 | const void *seed1, int seed1_len, |
| 243 | const void *seed2, int seed2_len, |
| 244 | const void *seed3, int seed3_len, |
| 245 | const void *seed4, int seed4_len, |
| 246 | const void *seed5, int seed5_len, |
| 247 | const unsigned char *sec, int slen, |
| 248 | unsigned char *out1, |
| 249 | unsigned char *out2, int olen) |
| 250 | { |
| 251 | int len,i,idx,count; |
| 252 | const unsigned char *S1; |
| 253 | long m; |
| 254 | const EVP_MD *md; |
| 255 | int ret = 0; |
| 256 | |
| 257 | /* Count number of digests and partition sec evenly */ |
| 258 | count=0; |
| 259 | for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) { |
| 260 | if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++; |
| 261 | } |
| 262 | len=slen/count; |
| 263 | if (count == 1) |
| 264 | slen = 0; |
| 265 | S1=sec; |
| 266 | memset(out1,0,olen); |
| 267 | for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) { |
| 268 | if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) { |
| 269 | if (!md) { |
| 270 | OPENSSL_PUT_ERROR(SSL, tls1_PRF, SSL_R_UNSUPPORTED_DIGEST_TYPE); |
| 271 | goto err; |
| 272 | } |
| 273 | if (!tls1_P_hash(md ,S1,len+(slen&1), |
| 274 | seed1,seed1_len,seed2,seed2_len,seed3,seed3_len,seed4,seed4_len,seed5,seed5_len, |
| 275 | out2,olen)) |
| 276 | goto err; |
| 277 | S1+=len; |
| 278 | for (i=0; i<olen; i++) |
| 279 | { |
| 280 | out1[i]^=out2[i]; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | ret = 1; |
| 285 | err: |
| 286 | return ret; |
| 287 | } |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 288 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | static int tls1_generate_key_block(SSL *s, unsigned char *km, |
| 290 | unsigned char *tmp, int num) |
| 291 | { |
| 292 | int ret; |
| 293 | ret = tls1_PRF(ssl_get_algorithm2(s), |
| 294 | TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE, |
| 295 | s->s3->server_random,SSL3_RANDOM_SIZE, |
| 296 | s->s3->client_random,SSL3_RANDOM_SIZE, |
| 297 | NULL,0,NULL,0, |
| 298 | s->session->master_key,s->session->master_key_length, |
| 299 | km,tmp,num); |
| 300 | #ifdef KSSL_DEBUG |
| 301 | printf("tls1_generate_key_block() ==> %d byte master_key =\n\t", |
| 302 | s->session->master_key_length); |
| 303 | { |
| 304 | int i; |
| 305 | for (i=0; i < s->session->master_key_length; i++) |
| 306 | { |
| 307 | printf("%02X", s->session->master_key[i]); |
| 308 | } |
| 309 | printf("\n"); } |
| 310 | #endif /* KSSL_DEBUG */ |
| 311 | return ret; |
| 312 | } |
| 313 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | /* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It |
| 315 | * returns 0 on malloc error. */ |
| 316 | static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx) |
| 317 | { |
| 318 | if (*aead_ctx != NULL) |
| 319 | EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx); |
| 320 | else |
| 321 | { |
| 322 | *aead_ctx = (SSL_AEAD_CTX*) OPENSSL_malloc(sizeof(SSL_AEAD_CTX)); |
| 323 | if (*aead_ctx == NULL) |
| 324 | { |
| 325 | OPENSSL_PUT_ERROR(SSL, tls1_aead_ctx_init, ERR_R_MALLOC_FAILURE); |
| 326 | return 0; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | static int tls1_change_cipher_state_aead(SSL *s, char is_read, |
| 334 | const unsigned char *key, unsigned key_len, |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 335 | const unsigned char *iv, unsigned iv_len, |
| 336 | const unsigned char *mac_secret, unsigned mac_secret_len) |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | { |
| 338 | const EVP_AEAD *aead = s->s3->tmp.new_aead; |
| 339 | SSL_AEAD_CTX *aead_ctx; |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 340 | /* mac_key_and_key is used to merge the MAC and cipher keys for an AEAD |
| 341 | * which simulates pre-AEAD cipher suites. It needs to be large enough |
| 342 | * to cope with the largest pair of keys. */ |
| 343 | uint8_t mac_key_and_key[32 /* HMAC(SHA256) */ + 32 /* AES-256 */]; |
| 344 | |
| 345 | if (mac_secret_len > 0) |
| 346 | { |
| 347 | /* This is a "stateful" AEAD (for compatibility with pre-AEAD |
| 348 | * cipher suites). */ |
| 349 | if (mac_secret_len + key_len > sizeof(mac_key_and_key)) |
| 350 | { |
| 351 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR); |
| 352 | return 0; |
| 353 | } |
| 354 | memcpy(mac_key_and_key, mac_secret, mac_secret_len); |
| 355 | memcpy(mac_key_and_key + mac_secret_len, key, key_len); |
| 356 | key = mac_key_and_key; |
| 357 | key_len += mac_secret_len; |
| 358 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 359 | |
| 360 | if (is_read) |
| 361 | { |
| 362 | if (!tls1_aead_ctx_init(&s->aead_read_ctx)) |
| 363 | return 0; |
| 364 | aead_ctx = s->aead_read_ctx; |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | if (!tls1_aead_ctx_init(&s->aead_write_ctx)) |
| 369 | return 0; |
| 370 | aead_ctx = s->aead_write_ctx; |
| 371 | } |
| 372 | |
| 373 | if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len, |
| 374 | EVP_AEAD_DEFAULT_TAG_LENGTH, NULL /* engine */)) |
| 375 | return 0; |
| 376 | if (iv_len > sizeof(aead_ctx->fixed_nonce)) |
| 377 | { |
| 378 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR); |
| 379 | return 0; |
| 380 | } |
| 381 | memcpy(aead_ctx->fixed_nonce, iv, iv_len); |
| 382 | aead_ctx->fixed_nonce_len = iv_len; |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 383 | aead_ctx->variable_nonce_len = 8; /* correct for all true AEADs so far. */ |
| 384 | if (s->s3->tmp.new_cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) |
| 385 | aead_ctx->variable_nonce_len = 0; |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | aead_ctx->variable_nonce_included_in_record = |
| 387 | (s->s3->tmp.new_cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD) != 0; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len != EVP_AEAD_nonce_length(aead)) |
| 389 | { |
| 390 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR); |
| 391 | return 0; |
| 392 | } |
| 393 | aead_ctx->tag_len = EVP_AEAD_max_overhead(aead); |
| 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | /* tls1_change_cipher_state_cipher performs the work needed to switch cipher |
| 399 | * states when using EVP_CIPHER. The argument |is_read| is true iff this |
| 400 | * function is being called due to reading, as opposed to writing, a |
| 401 | * ChangeCipherSpec message. In order to support export ciphersuites, |
| 402 | * use_client_keys indicates whether the key material provided is in the |
| 403 | * "client write" direction. */ |
| 404 | static int tls1_change_cipher_state_cipher( |
| 405 | SSL *s, char is_read, char use_client_keys, |
| 406 | const unsigned char *mac_secret, unsigned mac_secret_len, |
| 407 | const unsigned char *key, unsigned key_len, |
| 408 | const unsigned char *iv, unsigned iv_len) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | { |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | const EVP_CIPHER *cipher = s->s3->tmp.new_sym_enc; |
| 411 | const char is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) != 0; |
| 412 | EVP_CIPHER_CTX *cipher_ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 413 | EVP_MD_CTX *mac_ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | unsigned char export_tmp1[EVP_MAX_KEY_LENGTH]; |
| 416 | unsigned char export_tmp2[EVP_MAX_KEY_LENGTH]; |
| 417 | unsigned char export_iv1[EVP_MAX_IV_LENGTH * 2]; |
| 418 | unsigned char export_iv2[EVP_MAX_IV_LENGTH * 2]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 419 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 420 | if (is_read) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | { |
| 422 | if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) |
| 423 | s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM; |
| 424 | else |
| 425 | s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM; |
| 426 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 427 | if (s->enc_read_ctx != NULL && !SSL_IS_DTLS(s)) |
| 428 | EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); |
| 429 | else if ((s->enc_read_ctx=EVP_CIPHER_CTX_new()) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | goto err; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 431 | |
| 432 | cipher_ctx = s->enc_read_ctx; |
| 433 | mac_ctx = ssl_replace_hash(&s->read_hash, NULL); |
| 434 | |
| 435 | memcpy(s->s3->read_mac_secret, mac_secret, mac_secret_len); |
| 436 | s->s3->read_mac_secret_size = mac_secret_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 437 | } |
| 438 | else |
| 439 | { |
| 440 | if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) |
| 441 | s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
| 445 | /* When updating the write contexts for DTLS, we do not wish to |
| 446 | * free the old ones because DTLS stores pointers to them in |
| 447 | * order to implement retransmission. */ |
| 448 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); |
| 451 | else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 452 | goto err; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | else |
| 454 | /* make sure it's intialized in case we exit later with an error */ |
| 455 | EVP_CIPHER_CTX_init(s->enc_write_ctx); |
| 456 | |
| 457 | cipher_ctx = s->enc_write_ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 458 | if (SSL_IS_DTLS(s)) |
| 459 | { |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 460 | /* This is the same as ssl_replace_hash, but doesn't |
| 461 | * free the old |s->write_hash|. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | mac_ctx = EVP_MD_CTX_create(); |
| 463 | if (!mac_ctx) |
| 464 | goto err; |
| 465 | s->write_hash = mac_ctx; |
| 466 | } |
| 467 | else |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 468 | mac_ctx = ssl_replace_hash(&s->write_hash, NULL); |
| 469 | |
| 470 | memcpy(s->s3->write_mac_secret, mac_secret, mac_secret_len); |
| 471 | s->s3->write_mac_secret_size = mac_secret_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | if (is_export) |
| 475 | { |
| 476 | /* In here I set both the read and write key/iv to the |
| 477 | * same value since only the correct one will be used :-). |
| 478 | */ |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | const unsigned char *label; |
| 480 | unsigned label_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | if (use_client_keys) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | { |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | label = (const unsigned char*) TLS_MD_CLIENT_WRITE_KEY_CONST; |
| 485 | label_len = TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE; |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | label = (const unsigned char*) TLS_MD_SERVER_WRITE_KEY_CONST; |
| 490 | label_len = TLS_MD_SERVER_WRITE_KEY_CONST_SIZE; |
| 491 | } |
| 492 | |
| 493 | if (!tls1_PRF(ssl_get_algorithm2(s), |
| 494 | label, label_len, |
| 495 | s->s3->client_random, SSL3_RANDOM_SIZE, |
| 496 | s->s3->server_random, SSL3_RANDOM_SIZE, |
| 497 | NULL, 0, NULL, 0, |
| 498 | key /* secret */, key_len /* secret length */, |
| 499 | export_tmp1 /* output */, |
| 500 | export_tmp2 /* scratch space */, |
| 501 | EVP_CIPHER_key_length(s->s3->tmp.new_sym_enc) /* output length */)) |
| 502 | return 0; |
| 503 | key = export_tmp1; |
| 504 | |
| 505 | if (iv_len > 0) |
| 506 | { |
| 507 | static const unsigned char empty[] = ""; |
| 508 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | if (!tls1_PRF(ssl_get_algorithm2(s), |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | TLS_MD_IV_BLOCK_CONST, TLS_MD_IV_BLOCK_CONST_SIZE, |
| 511 | s->s3->client_random, SSL3_RANDOM_SIZE, |
| 512 | s->s3->server_random, SSL3_RANDOM_SIZE, |
| 513 | NULL, 0, NULL, 0, |
| 514 | empty /* secret */ ,0 /* secret length */, |
| 515 | export_iv1 /* output */, |
| 516 | export_iv2 /* scratch space */, |
| 517 | iv_len * 2 /* output length */)) |
| 518 | return 0; |
| 519 | |
| 520 | if (use_client_keys) |
| 521 | iv = export_iv1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | else |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | iv = &export_iv1[iv_len]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Victor Vasiliev | 508c29f | 2014-06-25 11:19:26 -0400 | [diff] [blame] | 527 | EVP_PKEY *mac_key = |
| 528 | EVP_PKEY_new_mac_key(s->s3->tmp.new_mac_pkey_type, |
| 529 | NULL, mac_secret, mac_secret_len); |
| 530 | if (!mac_key) |
| 531 | return 0; |
| 532 | EVP_DigestSignInit(mac_ctx, NULL, s->s3->tmp.new_hash, NULL, mac_key); |
| 533 | EVP_PKEY_free(mac_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 534 | |
Victor Vasiliev | 508c29f | 2014-06-25 11:19:26 -0400 | [diff] [blame] | 535 | EVP_CipherInit_ex(cipher_ctx, cipher, NULL /* engine */, key, iv, !is_read); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | if (is_export) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | { |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 539 | OPENSSL_cleanse(export_tmp1, sizeof(export_tmp1)); |
| 540 | OPENSSL_cleanse(export_tmp2, sizeof(export_tmp1)); |
| 541 | OPENSSL_cleanse(export_iv1, sizeof(export_iv1)); |
| 542 | OPENSSL_cleanse(export_iv2, sizeof(export_iv2)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 546 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 547 | err: |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 548 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_cipher, ERR_R_MALLOC_FAILURE); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | int tls1_change_cipher_state(SSL *s, int which) |
| 553 | { |
| 554 | /* is_read is true if we have just read a ChangeCipherSpec message - |
| 555 | * i.e. we need to update the read cipherspec. Otherwise we have just |
| 556 | * written one. */ |
| 557 | const char is_read = (which & SSL3_CC_READ) != 0; |
| 558 | /* use_client_keys is true if we wish to use the keys for the "client |
| 559 | * write" direction. This is the case if we're a client sending a |
| 560 | * ChangeCipherSpec, or a server reading a client's ChangeCipherSpec. */ |
| 561 | const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || |
| 562 | which == SSL3_CHANGE_CIPHER_SERVER_READ; |
| 563 | const unsigned char *client_write_mac_secret, *server_write_mac_secret, *mac_secret; |
| 564 | const unsigned char *client_write_key, *server_write_key, *key; |
| 565 | const unsigned char *client_write_iv, *server_write_iv, *iv; |
| 566 | const EVP_CIPHER *cipher = s->s3->tmp.new_sym_enc; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 567 | const EVP_AEAD *aead = s->s3->tmp.new_aead; |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | unsigned key_len, iv_len, mac_secret_len; |
| 569 | const unsigned char *key_data; |
| 570 | const char is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) != 0; |
| 571 | |
| 572 | /* Reset sequence number to zero. */ |
| 573 | if (s->version != DTLS1_VERSION) |
| 574 | memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8); |
| 575 | |
| 576 | /* key_arg is used for SSLv2. We don't need it for TLS. */ |
| 577 | s->session->key_arg_length = 0; |
| 578 | |
| 579 | mac_secret_len = s->s3->tmp.new_mac_secret_size; |
| 580 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 581 | if (aead != NULL) |
| 582 | { |
| 583 | key_len = EVP_AEAD_key_length(aead); |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 584 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD |
| 585 | * cipher suites) the key length reported by |
| 586 | * |EVP_AEAD_key_length| will include the MAC key bytes. */ |
| 587 | if (key_len < mac_secret_len) |
| 588 | { |
| 589 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 590 | return 0; |
| 591 | } |
| 592 | key_len -= mac_secret_len; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->s3->tmp.new_cipher); |
| 594 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 595 | else |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 596 | { |
| 597 | key_len = EVP_CIPHER_key_length(cipher); |
| 598 | if (is_export && key_len > SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) |
| 599 | key_len = SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher); |
| 600 | |
| 601 | if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) |
| 602 | iv_len = EVP_GCM_TLS_FIXED_IV_LEN; |
| 603 | else |
| 604 | iv_len = EVP_CIPHER_iv_length(cipher); |
| 605 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 606 | |
| 607 | key_data = s->s3->tmp.key_block; |
| 608 | client_write_mac_secret = key_data; key_data += mac_secret_len; |
| 609 | server_write_mac_secret = key_data; key_data += mac_secret_len; |
| 610 | client_write_key = key_data; key_data += key_len; |
| 611 | server_write_key = key_data; key_data += key_len; |
| 612 | client_write_iv = key_data; key_data += iv_len; |
| 613 | server_write_iv = key_data; key_data += iv_len; |
| 614 | |
| 615 | if (use_client_keys) |
| 616 | { |
| 617 | mac_secret = client_write_mac_secret; |
| 618 | key = client_write_key; |
| 619 | iv = client_write_iv; |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | mac_secret = server_write_mac_secret; |
| 624 | key = server_write_key; |
| 625 | iv = server_write_iv; |
| 626 | } |
| 627 | |
| 628 | if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) |
| 629 | { |
| 630 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 631 | return 0; |
| 632 | } |
| 633 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 634 | if (aead != NULL) |
| 635 | { |
| 636 | if (!tls1_change_cipher_state_aead(s, is_read, |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 637 | key, key_len, iv, iv_len, |
| 638 | mac_secret, mac_secret_len)) |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | if (!tls1_change_cipher_state_cipher(s, is_read, use_client_keys, |
| 644 | mac_secret, mac_secret_len, |
| 645 | key, key_len, |
| 646 | iv, iv_len)) |
| 647 | return 0; |
| 648 | } |
Adam Langley | a5fa5b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | |
| 650 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | int tls1_setup_key_block(SSL *s) |
| 654 | { |
| 655 | unsigned char *p1,*p2=NULL; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 656 | const EVP_CIPHER *c = NULL; |
| 657 | const EVP_MD *hash = NULL; |
| 658 | const EVP_AEAD *aead = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 659 | int num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 660 | int mac_type= NID_undef,mac_secret_size=0; |
| 661 | int ret=0; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 662 | unsigned key_len, iv_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 663 | |
| 664 | #ifdef KSSL_DEBUG |
| 665 | printf ("tls1_setup_key_block()\n"); |
| 666 | #endif /* KSSL_DEBUG */ |
| 667 | |
| 668 | if (s->s3->tmp.key_block_length != 0) |
| 669 | return(1); |
| 670 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 671 | if (s->session->cipher && |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 672 | ((s->session->cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) || |
| 673 | (s->session->cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | { |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 675 | if (!ssl_cipher_get_evp_aead(s->session, &aead)) |
| 676 | goto cipher_unavailable_err; |
| 677 | key_len = EVP_AEAD_key_length(aead); |
| 678 | iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->session->cipher); |
Adam Langley | 9447dff | 2014-06-24 17:29:06 -0700 | [diff] [blame^] | 679 | if (!ssl_cipher_get_mac(s->session, &hash, &mac_type, &mac_secret_size)) |
| 680 | goto cipher_unavailable_err; |
| 681 | /* For "stateful" AEADs (i.e. compatibility with pre-AEAD |
| 682 | * cipher suites) the key length reported by |
| 683 | * |EVP_AEAD_key_length| will include the MAC key bytes. */ |
| 684 | if (key_len < mac_secret_size) |
| 685 | { |
| 686 | OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR); |
| 687 | return 0; |
| 688 | } |
| 689 | key_len -= mac_secret_size; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
| 693 | if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secret_size)) |
| 694 | goto cipher_unavailable_err; |
| 695 | key_len = EVP_CIPHER_key_length(c); |
| 696 | |
| 697 | if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) |
| 698 | iv_len = EVP_GCM_TLS_FIXED_IV_LEN; |
| 699 | else |
| 700 | iv_len = EVP_CIPHER_iv_length(c); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 703 | s->s3->tmp.new_aead=aead; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 704 | s->s3->tmp.new_sym_enc=c; |
| 705 | s->s3->tmp.new_hash=hash; |
| 706 | s->s3->tmp.new_mac_pkey_type = mac_type; |
| 707 | s->s3->tmp.new_mac_secret_size = mac_secret_size; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 708 | |
| 709 | num=key_len+mac_secret_size+iv_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | num*=2; |
| 711 | |
| 712 | ssl3_cleanup_key_block(s); |
| 713 | |
| 714 | if ((p1=(unsigned char *)OPENSSL_malloc(num)) == NULL) |
| 715 | { |
| 716 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE); |
| 717 | goto err; |
| 718 | } |
| 719 | |
| 720 | s->s3->tmp.key_block_length=num; |
| 721 | s->s3->tmp.key_block=p1; |
| 722 | |
| 723 | if ((p2=(unsigned char *)OPENSSL_malloc(num)) == NULL) |
| 724 | { |
| 725 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE); |
| 726 | goto err; |
| 727 | } |
| 728 | |
| 729 | #ifdef TLS_DEBUG |
| 730 | printf("client random\n"); |
| 731 | { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->client_random[z],((z+1)%16)?' ':'\n'); } |
| 732 | printf("server random\n"); |
| 733 | { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->server_random[z],((z+1)%16)?' ':'\n'); } |
| 734 | printf("pre-master\n"); |
| 735 | { int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); } |
| 736 | #endif |
| 737 | if (!tls1_generate_key_block(s,p1,p2,num)) |
| 738 | goto err; |
| 739 | #ifdef TLS_DEBUG |
| 740 | printf("\nkey block\n"); |
| 741 | { int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); } |
| 742 | #endif |
| 743 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 744 | if (s->method->version <= TLS1_VERSION && |
| 745 | (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 746 | { |
| 747 | /* enable vulnerability countermeasure for CBC ciphers with |
| 748 | * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt) |
| 749 | */ |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 750 | s->s3->need_record_splitting = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 751 | |
| 752 | if (s->session->cipher != NULL) |
| 753 | { |
| 754 | if (s->session->cipher->algorithm_enc == SSL_eNULL) |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 755 | s->s3->need_record_splitting = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 756 | |
| 757 | #ifndef OPENSSL_NO_RC4 |
| 758 | if (s->session->cipher->algorithm_enc == SSL_RC4) |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | s->s3->need_record_splitting = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 760 | #endif |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | ret = 1; |
| 765 | err: |
| 766 | if (p2) |
| 767 | { |
| 768 | OPENSSL_cleanse(p2,num); |
| 769 | OPENSSL_free(p2); |
| 770 | } |
| 771 | return(ret); |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 772 | |
| 773 | cipher_unavailable_err: |
| 774 | OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); |
| 775 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively. |
| 779 | * |
| 780 | * Returns: |
| 781 | * 0: (in non-constant time) if the record is publically invalid (i.e. too |
| 782 | * short etc). |
| 783 | * 1: if the record's padding is valid / the encryption was successful. |
| 784 | * -1: if the record's padding/AEAD-authenticator is invalid or, if sending, |
| 785 | * an internal error occured. |
| 786 | */ |
| 787 | int tls1_enc(SSL *s, int send) |
| 788 | { |
| 789 | SSL3_RECORD *rec; |
| 790 | EVP_CIPHER_CTX *ds; |
| 791 | unsigned long l; |
| 792 | int bs,i,j,k,pad=0,ret,mac_size=0; |
| 793 | const EVP_CIPHER *enc; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | const SSL_AEAD_CTX *aead; |
| 795 | |
| 796 | if (send) |
| 797 | rec = &s->s3->wrec; |
| 798 | else |
| 799 | rec = &s->s3->rrec; |
| 800 | |
| 801 | if (send) |
| 802 | aead = s->aead_write_ctx; |
| 803 | else |
| 804 | aead = s->aead_read_ctx; |
| 805 | |
| 806 | if (aead) |
| 807 | { |
| 808 | unsigned char ad[13], *seq, *in, *out, nonce[16]; |
| 809 | unsigned nonce_used; |
| 810 | size_t n; |
| 811 | |
| 812 | seq = send ? s->s3->write_sequence : s->s3->read_sequence; |
| 813 | |
| 814 | if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) |
| 815 | { |
| 816 | unsigned char dtlsseq[9], *p = dtlsseq; |
| 817 | |
| 818 | s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p); |
| 819 | memcpy(p, &seq[2], 6); |
| 820 | memcpy(ad, dtlsseq, 8); |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | memcpy(ad, seq, 8); |
| 825 | for (i=7; i>=0; i--) /* increment */ |
| 826 | { |
| 827 | ++seq[i]; |
| 828 | if (seq[i] != 0) |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | ad[8] = rec->type; |
| 834 | ad[9] = (unsigned char)(s->version>>8); |
| 835 | ad[10] = (unsigned char)(s->version); |
| 836 | |
| 837 | if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce) || |
| 838 | aead->variable_nonce_len > 8) |
| 839 | return -1; /* internal error - should never happen. */ |
| 840 | |
| 841 | memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len); |
| 842 | nonce_used = aead->fixed_nonce_len; |
| 843 | |
| 844 | if (send) |
| 845 | { |
| 846 | size_t len = rec->length; |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 847 | size_t eivlen = 0; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 848 | in = rec->input; |
| 849 | out = rec->data; |
| 850 | |
| 851 | /* When sending we use the sequence number as the |
| 852 | * variable part of the nonce. */ |
| 853 | if (aead->variable_nonce_len > 8) |
| 854 | return -1; |
| 855 | memcpy(nonce + nonce_used, ad, aead->variable_nonce_len); |
| 856 | nonce_used += aead->variable_nonce_len; |
| 857 | |
| 858 | /* in do_ssl3_write, rec->input is moved forward by |
| 859 | * variable_nonce_len in order to leave space for the |
| 860 | * variable nonce. Thus we can copy the sequence number |
| 861 | * bytes into place without overwriting any of the |
| 862 | * plaintext. */ |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 863 | if (aead->variable_nonce_included_in_record) |
| 864 | { |
| 865 | memcpy(out, ad, aead->variable_nonce_len); |
| 866 | len -= aead->variable_nonce_len; |
| 867 | eivlen = aead->variable_nonce_len; |
| 868 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 869 | |
| 870 | ad[11] = len >> 8; |
| 871 | ad[12] = len & 0xff; |
| 872 | |
| 873 | if (!EVP_AEAD_CTX_seal( |
| 874 | &aead->ctx, |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 875 | out + eivlen, &n, len + aead->tag_len, |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 876 | nonce, nonce_used, |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | in + eivlen, len, |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 878 | ad, sizeof(ad))) |
| 879 | { |
| 880 | return -1; |
| 881 | } |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 882 | if (aead->variable_nonce_included_in_record) |
| 883 | n += aead->variable_nonce_len; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | } |
| 885 | else |
| 886 | { |
| 887 | /* receive */ |
| 888 | size_t len = rec->length; |
| 889 | |
| 890 | if (rec->data != rec->input) |
| 891 | return -1; /* internal error - should never happen. */ |
| 892 | out = in = rec->input; |
| 893 | |
| 894 | if (len < aead->variable_nonce_len) |
| 895 | return 0; |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | memcpy(nonce + nonce_used, |
| 897 | aead->variable_nonce_included_in_record ? in : ad, |
| 898 | aead->variable_nonce_len); |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 899 | nonce_used += aead->variable_nonce_len; |
| 900 | |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 901 | if (aead->variable_nonce_included_in_record) |
| 902 | { |
| 903 | in += aead->variable_nonce_len; |
| 904 | len -= aead->variable_nonce_len; |
| 905 | out += aead->variable_nonce_len; |
| 906 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 907 | |
| 908 | if (len < aead->tag_len) |
| 909 | return 0; |
| 910 | len -= aead->tag_len; |
| 911 | |
| 912 | ad[11] = len >> 8; |
| 913 | ad[12] = len & 0xff; |
| 914 | |
| 915 | if (!EVP_AEAD_CTX_open( |
| 916 | &aead->ctx, |
| 917 | out, &n, len, |
| 918 | nonce, nonce_used, |
| 919 | in, len + aead->tag_len, |
| 920 | ad, sizeof(ad))) |
| 921 | { |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | rec->data = rec->input = out; |
| 926 | } |
| 927 | |
| 928 | rec->length = n; |
| 929 | return 1; |
| 930 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 931 | |
| 932 | if (send) |
| 933 | { |
| 934 | ds=s->enc_write_ctx; |
| 935 | rec= &(s->s3->wrec); |
| 936 | if (s->enc_write_ctx == NULL) |
| 937 | enc=NULL; |
| 938 | else |
| 939 | { |
| 940 | int ivlen; |
| 941 | enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx); |
| 942 | /* For TLSv1.1 and later explicit IV */ |
| 943 | if (SSL_USE_EXPLICIT_IV(s) |
| 944 | && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE) |
| 945 | ivlen = EVP_CIPHER_iv_length(enc); |
| 946 | else |
| 947 | ivlen = 0; |
| 948 | if (ivlen > 1) |
| 949 | { |
| 950 | if ( rec->data != rec->input) |
| 951 | /* we can't write into the input stream: |
| 952 | * Can this ever happen?? (steve) |
| 953 | */ |
| 954 | fprintf(stderr, |
| 955 | "%s:%d: rec->data != rec->input\n", |
| 956 | __FILE__, __LINE__); |
| 957 | else if (RAND_bytes(rec->input, ivlen) <= 0) |
| 958 | return -1; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | else |
| 963 | { |
| 964 | ds=s->enc_read_ctx; |
| 965 | rec= &(s->s3->rrec); |
| 966 | if (s->enc_read_ctx == NULL) |
| 967 | enc=NULL; |
| 968 | else |
| 969 | enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx); |
| 970 | } |
| 971 | |
| 972 | #ifdef KSSL_DEBUG |
| 973 | printf("tls1_enc(%d)\n", send); |
| 974 | #endif /* KSSL_DEBUG */ |
| 975 | |
| 976 | if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) |
| 977 | { |
| 978 | memmove(rec->data,rec->input,rec->length); |
| 979 | rec->input=rec->data; |
| 980 | ret = 1; |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | l=rec->length; |
| 985 | bs=EVP_CIPHER_block_size(ds->cipher); |
| 986 | |
Victor Vasiliev | 508c29f | 2014-06-25 11:19:26 -0400 | [diff] [blame] | 987 | if ((bs != 1) && send) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 988 | { |
| 989 | i=bs-((int)l%bs); |
| 990 | |
| 991 | /* Add weird padding of upto 256 bytes */ |
| 992 | |
| 993 | /* we need to add 'i' padding bytes of value j */ |
| 994 | j=i-1; |
| 995 | if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) |
| 996 | { |
| 997 | if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) |
| 998 | j++; |
| 999 | } |
| 1000 | for (k=(int)l; k<(int)(l+i); k++) |
| 1001 | rec->input[k]=j; |
| 1002 | l+=i; |
| 1003 | rec->length+=i; |
| 1004 | } |
| 1005 | |
| 1006 | #ifdef KSSL_DEBUG |
| 1007 | { |
| 1008 | unsigned long ui; |
| 1009 | printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", |
| 1010 | ds,rec->data,rec->input,l); |
| 1011 | printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n", |
| 1012 | ds->buf_len, ds->cipher->key_len, |
| 1013 | DES_KEY_SZ, DES_SCHEDULE_SZ, |
| 1014 | ds->cipher->iv_len); |
| 1015 | printf("\t\tIV: "); |
| 1016 | for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]); |
| 1017 | printf("\n"); |
| 1018 | printf("\trec->input="); |
| 1019 | for (ui=0; ui<l; ui++) printf(" %02x", rec->input[ui]); |
| 1020 | printf("\n"); |
| 1021 | } |
| 1022 | #endif /* KSSL_DEBUG */ |
| 1023 | |
| 1024 | if (!send) |
| 1025 | { |
| 1026 | if (l == 0 || l%bs != 0) |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | i = EVP_Cipher(ds,rec->data,rec->input,l); |
| 1031 | if ((EVP_CIPHER_flags(ds->cipher)&EVP_CIPH_FLAG_CUSTOM_CIPHER) |
| 1032 | ?(i<0) |
| 1033 | :(i==0)) |
| 1034 | return -1; /* AEAD can fail to verify MAC */ |
| 1035 | if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE && !send) |
| 1036 | { |
| 1037 | rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 1038 | rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 1039 | rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 1040 | } |
| 1041 | |
| 1042 | #ifdef KSSL_DEBUG |
| 1043 | { |
| 1044 | unsigned long i; |
| 1045 | printf("\trec->data="); |
| 1046 | for (i=0; i<l; i++) |
| 1047 | printf(" %02x", rec->data[i]); printf("\n"); |
| 1048 | } |
| 1049 | #endif /* KSSL_DEBUG */ |
| 1050 | |
| 1051 | ret = 1; |
| 1052 | if (EVP_MD_CTX_md(s->read_hash) != NULL) |
| 1053 | mac_size = EVP_MD_CTX_size(s->read_hash); |
| 1054 | if ((bs != 1) && !send) |
| 1055 | ret = tls1_cbc_remove_padding(s, rec, bs, mac_size); |
| 1056 | if (pad && !send) |
| 1057 | rec->length -= pad; |
| 1058 | } |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | int tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out) |
| 1063 | { |
| 1064 | unsigned int ret; |
| 1065 | EVP_MD_CTX ctx, *d=NULL; |
| 1066 | int i; |
| 1067 | |
| 1068 | if (s->s3->handshake_buffer) |
| 1069 | if (!ssl3_digest_cached_records(s)) |
| 1070 | return 0; |
| 1071 | |
| 1072 | for (i=0;i<SSL_MAX_DIGEST;i++) |
| 1073 | { |
| 1074 | if (s->s3->handshake_dgst[i]&&EVP_MD_CTX_type(s->s3->handshake_dgst[i])==md_nid) |
| 1075 | { |
| 1076 | d=s->s3->handshake_dgst[i]; |
| 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | if (!d) { |
| 1081 | OPENSSL_PUT_ERROR(SSL, tls1_cert_verify_mac, SSL_R_NO_REQUIRED_DIGEST); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | EVP_MD_CTX_init(&ctx); |
| 1086 | EVP_MD_CTX_copy_ex(&ctx,d); |
| 1087 | EVP_DigestFinal_ex(&ctx,out,&ret); |
| 1088 | EVP_MD_CTX_cleanup(&ctx); |
| 1089 | return((int)ret); |
| 1090 | } |
| 1091 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1092 | /* tls1_handshake_digest calculates the current handshake hash and writes it to |
| 1093 | * |out|, which has space for |out_len| bytes. It returns the number of bytes |
| 1094 | * written or -1 in the event of an error. This function works on a copy of the |
| 1095 | * underlying digests so can be called multiple times and prior to the final |
| 1096 | * update etc. */ |
| 1097 | int tls1_handshake_digest(SSL *s, unsigned char *out, size_t out_len) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1098 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1099 | const EVP_MD *md; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1100 | EVP_MD_CTX ctx; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1101 | int i, err = 0, len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1102 | long mask; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
| 1104 | EVP_MD_CTX_init(&ctx); |
| 1105 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1106 | for (i = 0; ssl_get_handshake_digest(i, &mask, &md); i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1107 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1108 | int hash_size; |
| 1109 | unsigned int digest_len; |
| 1110 | EVP_MD_CTX *hdgst = s->s3->handshake_dgst[i]; |
| 1111 | |
| 1112 | if ((mask & ssl_get_algorithm2(s)) == 0) |
| 1113 | continue; |
| 1114 | |
| 1115 | hash_size = EVP_MD_size(md); |
| 1116 | if (!hdgst || hash_size < 0 || (size_t)hash_size > out_len) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1117 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1118 | err = 1; |
| 1119 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1120 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1121 | |
| 1122 | if (!EVP_MD_CTX_copy_ex(&ctx, hdgst) || |
| 1123 | !EVP_DigestFinal_ex(&ctx, out, &digest_len) || |
| 1124 | digest_len != (unsigned int)hash_size) /* internal error */ |
| 1125 | { |
| 1126 | err = 1; |
| 1127 | break; |
| 1128 | } |
| 1129 | out += digest_len; |
| 1130 | out_len -= digest_len; |
| 1131 | len += digest_len; |
| 1132 | } |
| 1133 | |
| 1134 | EVP_MD_CTX_cleanup(&ctx); |
| 1135 | |
| 1136 | if (err != 0) |
| 1137 | return -1; |
| 1138 | return len; |
| 1139 | } |
| 1140 | |
| 1141 | int tls1_final_finish_mac(SSL *s, |
| 1142 | const char *str, int slen, unsigned char *out) |
| 1143 | { |
| 1144 | unsigned char buf[2*EVP_MAX_MD_SIZE]; |
| 1145 | unsigned char buf2[12]; |
| 1146 | int err=0; |
| 1147 | int digests_len; |
| 1148 | |
| 1149 | if (s->s3->handshake_buffer) |
| 1150 | if (!ssl3_digest_cached_records(s)) |
| 1151 | return 0; |
| 1152 | |
| 1153 | digests_len = tls1_handshake_digest(s, buf, sizeof(buf)); |
| 1154 | if (digests_len < 0) |
| 1155 | { |
| 1156 | err = 1; |
| 1157 | digests_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | if (!tls1_PRF(ssl_get_algorithm2(s), |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1161 | str,slen, buf, digests_len, NULL,0, NULL,0, NULL,0, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1162 | s->session->master_key,s->session->master_key_length, |
| 1163 | out,buf2,sizeof buf2)) |
| 1164 | err = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1165 | |
| 1166 | if (err) |
| 1167 | return 0; |
| 1168 | else |
| 1169 | return sizeof buf2; |
| 1170 | } |
| 1171 | |
| 1172 | int tls1_mac(SSL *ssl, unsigned char *md, int send) |
| 1173 | { |
| 1174 | SSL3_RECORD *rec; |
| 1175 | unsigned char *seq; |
| 1176 | EVP_MD_CTX *hash; |
| 1177 | size_t md_size, orig_len; |
| 1178 | int i; |
| 1179 | EVP_MD_CTX hmac, *mac_ctx; |
| 1180 | unsigned char header[13]; |
| 1181 | int stream_mac = (send?(ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM):(ssl->mac_flags&SSL_MAC_FLAG_READ_MAC_STREAM)); |
| 1182 | int t; |
| 1183 | |
| 1184 | if (send) |
| 1185 | { |
| 1186 | rec= &(ssl->s3->wrec); |
| 1187 | seq= &(ssl->s3->write_sequence[0]); |
| 1188 | hash=ssl->write_hash; |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | rec= &(ssl->s3->rrec); |
| 1193 | seq= &(ssl->s3->read_sequence[0]); |
| 1194 | hash=ssl->read_hash; |
| 1195 | } |
| 1196 | |
| 1197 | t=EVP_MD_CTX_size(hash); |
| 1198 | assert(t >= 0); |
| 1199 | md_size=t; |
| 1200 | |
| 1201 | /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */ |
| 1202 | if (stream_mac) |
| 1203 | { |
| 1204 | mac_ctx = hash; |
| 1205 | } |
| 1206 | else |
| 1207 | { |
Adam Langley | 9f61d7f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1208 | if (!EVP_MD_CTX_copy(&hmac,hash)) |
| 1209 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1210 | mac_ctx = &hmac; |
| 1211 | } |
| 1212 | |
| 1213 | if (SSL_IS_DTLS(ssl)) |
| 1214 | { |
| 1215 | unsigned char dtlsseq[8],*p=dtlsseq; |
| 1216 | |
| 1217 | s2n(send?ssl->d1->w_epoch:ssl->d1->r_epoch, p); |
| 1218 | memcpy (p,&seq[2],6); |
| 1219 | |
| 1220 | memcpy(header, dtlsseq, 8); |
| 1221 | } |
| 1222 | else |
| 1223 | memcpy(header, seq, 8); |
| 1224 | |
| 1225 | /* kludge: tls1_cbc_remove_padding passes padding length in rec->type */ |
| 1226 | orig_len = rec->length+md_size+((unsigned int)rec->type>>8); |
| 1227 | rec->type &= 0xff; |
| 1228 | |
| 1229 | header[8]=rec->type; |
| 1230 | header[9]=(unsigned char)(ssl->version>>8); |
| 1231 | header[10]=(unsigned char)(ssl->version); |
| 1232 | header[11]=(rec->length)>>8; |
| 1233 | header[12]=(rec->length)&0xff; |
| 1234 | |
| 1235 | if (!send && |
| 1236 | EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE && |
| 1237 | ssl3_cbc_record_digest_supported(mac_ctx)) |
| 1238 | { |
| 1239 | /* This is a CBC-encrypted record. We must avoid leaking any |
| 1240 | * timing-side channel information about how many blocks of |
| 1241 | * data we are hashing because that gives an attacker a |
| 1242 | * timing-oracle. */ |
| 1243 | ssl3_cbc_digest_record( |
| 1244 | mac_ctx, |
| 1245 | md, &md_size, |
| 1246 | header, rec->input, |
| 1247 | rec->length + md_size, orig_len, |
| 1248 | ssl->s3->read_mac_secret, |
| 1249 | ssl->s3->read_mac_secret_size, |
| 1250 | 0 /* not SSLv3 */); |
| 1251 | } |
| 1252 | else |
| 1253 | { |
| 1254 | EVP_DigestSignUpdate(mac_ctx,header,sizeof(header)); |
| 1255 | EVP_DigestSignUpdate(mac_ctx,rec->input,rec->length); |
| 1256 | t=EVP_DigestSignFinal(mac_ctx,md,&md_size); |
| 1257 | assert(t > 0); |
| 1258 | #ifdef OPENSSL_FIPS |
| 1259 | if (!send && FIPS_mode()) |
| 1260 | tls_fips_digest_extra( |
| 1261 | ssl->enc_read_ctx, |
| 1262 | mac_ctx, rec->input, |
| 1263 | rec->length, orig_len); |
| 1264 | #endif |
| 1265 | } |
| 1266 | |
| 1267 | if (!stream_mac) |
| 1268 | EVP_MD_CTX_cleanup(&hmac); |
| 1269 | #ifdef TLS_DEBUG |
| 1270 | printf("sec="); |
| 1271 | {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); } |
| 1272 | printf("seq="); |
| 1273 | {int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); } |
| 1274 | printf("buf="); |
| 1275 | {int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); } |
| 1276 | printf("rec="); |
| 1277 | {unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); } |
| 1278 | #endif |
| 1279 | |
| 1280 | if (!SSL_IS_DTLS(ssl)) |
| 1281 | { |
| 1282 | for (i=7; i>=0; i--) |
| 1283 | { |
| 1284 | ++seq[i]; |
| 1285 | if (seq[i] != 0) break; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | #ifdef TLS_DEBUG |
| 1290 | {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); } |
| 1291 | #endif |
| 1292 | return(md_size); |
| 1293 | } |
| 1294 | |
| 1295 | int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, |
| 1296 | int len) |
| 1297 | { |
| 1298 | unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; |
| 1299 | const void *co = NULL, *so = NULL; |
| 1300 | int col = 0, sol = 0; |
| 1301 | |
| 1302 | |
| 1303 | #ifdef KSSL_DEBUG |
| 1304 | printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len); |
| 1305 | #endif /* KSSL_DEBUG */ |
| 1306 | |
| 1307 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 1308 | if (s->s3->client_opaque_prf_input != NULL && s->s3->server_opaque_prf_input != NULL && |
| 1309 | s->s3->client_opaque_prf_input_len > 0 && |
| 1310 | s->s3->client_opaque_prf_input_len == s->s3->server_opaque_prf_input_len) |
| 1311 | { |
| 1312 | co = s->s3->client_opaque_prf_input; |
| 1313 | col = s->s3->server_opaque_prf_input_len; |
| 1314 | so = s->s3->server_opaque_prf_input; |
| 1315 | sol = s->s3->client_opaque_prf_input_len; /* must be same as col (see draft-rescorla-tls-opaque-prf-input-00.txt, section 3.1) */ |
| 1316 | } |
| 1317 | #endif |
| 1318 | |
| 1319 | tls1_PRF(ssl_get_algorithm2(s), |
| 1320 | TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE, |
| 1321 | s->s3->client_random,SSL3_RANDOM_SIZE, |
| 1322 | co, col, |
| 1323 | s->s3->server_random,SSL3_RANDOM_SIZE, |
| 1324 | so, sol, |
| 1325 | p,len, |
| 1326 | s->session->master_key,buff,sizeof buff); |
| 1327 | #ifdef SSL_DEBUG |
| 1328 | fprintf(stderr, "Premaster Secret:\n"); |
| 1329 | BIO_dump_fp(stderr, (char *)p, len); |
| 1330 | fprintf(stderr, "Client Random:\n"); |
| 1331 | BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE); |
| 1332 | fprintf(stderr, "Server Random:\n"); |
| 1333 | BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE); |
| 1334 | fprintf(stderr, "Master Secret:\n"); |
| 1335 | BIO_dump_fp(stderr, (char *)s->session->master_key, SSL3_MASTER_SECRET_SIZE); |
| 1336 | #endif |
| 1337 | |
| 1338 | #ifdef OPENSSL_SSL_TRACE_CRYPTO |
| 1339 | if (s->msg_callback) |
| 1340 | { |
| 1341 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER, |
| 1342 | p, len, s, s->msg_callback_arg); |
| 1343 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM, |
| 1344 | s->s3->client_random, SSL3_RANDOM_SIZE, |
| 1345 | s, s->msg_callback_arg); |
| 1346 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM, |
| 1347 | s->s3->server_random, SSL3_RANDOM_SIZE, |
| 1348 | s, s->msg_callback_arg); |
| 1349 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER, |
| 1350 | s->session->master_key, |
| 1351 | SSL3_MASTER_SECRET_SIZE, |
| 1352 | s, s->msg_callback_arg); |
| 1353 | } |
| 1354 | #endif |
| 1355 | |
| 1356 | #ifdef KSSL_DEBUG |
| 1357 | printf ("tls1_generate_master_secret() complete\n"); |
| 1358 | #endif /* KSSL_DEBUG */ |
| 1359 | return(SSL3_MASTER_SECRET_SIZE); |
| 1360 | } |
| 1361 | |
| 1362 | int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen, |
| 1363 | const char *label, size_t llen, const unsigned char *context, |
| 1364 | size_t contextlen, int use_context) |
| 1365 | { |
| 1366 | unsigned char *buff; |
| 1367 | unsigned char *val = NULL; |
| 1368 | size_t vallen, currentvalpos; |
| 1369 | int rv; |
| 1370 | |
| 1371 | #ifdef KSSL_DEBUG |
| 1372 | printf ("tls1_export_keying_material(%p,%p,%d,%s,%d,%p,%d)\n", s, out, olen, label, llen, p, plen); |
| 1373 | #endif /* KSSL_DEBUG */ |
| 1374 | |
| 1375 | buff = OPENSSL_malloc(olen); |
| 1376 | if (buff == NULL) goto err2; |
| 1377 | |
| 1378 | /* construct PRF arguments |
| 1379 | * we construct the PRF argument ourself rather than passing separate |
| 1380 | * values into the TLS PRF to ensure that the concatenation of values |
| 1381 | * does not create a prohibited label. |
| 1382 | */ |
| 1383 | vallen = llen + SSL3_RANDOM_SIZE * 2; |
| 1384 | if (use_context) |
| 1385 | { |
| 1386 | vallen += 2 + contextlen; |
| 1387 | } |
| 1388 | |
| 1389 | val = OPENSSL_malloc(vallen); |
| 1390 | if (val == NULL) goto err2; |
| 1391 | currentvalpos = 0; |
| 1392 | memcpy(val + currentvalpos, (unsigned char *) label, llen); |
| 1393 | currentvalpos += llen; |
| 1394 | memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE); |
| 1395 | currentvalpos += SSL3_RANDOM_SIZE; |
| 1396 | memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE); |
| 1397 | currentvalpos += SSL3_RANDOM_SIZE; |
| 1398 | |
| 1399 | if (use_context) |
| 1400 | { |
| 1401 | val[currentvalpos] = (contextlen >> 8) & 0xff; |
| 1402 | currentvalpos++; |
| 1403 | val[currentvalpos] = contextlen & 0xff; |
| 1404 | currentvalpos++; |
| 1405 | if ((contextlen > 0) || (context != NULL)) |
| 1406 | { |
| 1407 | memcpy(val + currentvalpos, context, contextlen); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | /* disallow prohibited labels |
| 1412 | * note that SSL3_RANDOM_SIZE > max(prohibited label len) = |
| 1413 | * 15, so size of val > max(prohibited label len) = 15 and the |
| 1414 | * comparisons won't have buffer overflow |
| 1415 | */ |
| 1416 | if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST, |
| 1417 | TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0) goto err1; |
| 1418 | if (memcmp(val, TLS_MD_SERVER_FINISH_CONST, |
| 1419 | TLS_MD_SERVER_FINISH_CONST_SIZE) == 0) goto err1; |
| 1420 | if (memcmp(val, TLS_MD_MASTER_SECRET_CONST, |
| 1421 | TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) goto err1; |
| 1422 | if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST, |
| 1423 | TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) goto err1; |
| 1424 | |
Adam Langley | caddf1c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1425 | rv = tls1_PRF(ssl_get_algorithm2(s), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1426 | val, vallen, |
| 1427 | NULL, 0, |
| 1428 | NULL, 0, |
| 1429 | NULL, 0, |
| 1430 | NULL, 0, |
| 1431 | s->session->master_key,s->session->master_key_length, |
| 1432 | out,buff,olen); |
| 1433 | |
| 1434 | #ifdef KSSL_DEBUG |
| 1435 | printf ("tls1_export_keying_material() complete\n"); |
| 1436 | #endif /* KSSL_DEBUG */ |
| 1437 | goto ret; |
| 1438 | err1: |
| 1439 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL); |
| 1440 | rv = 0; |
| 1441 | goto ret; |
| 1442 | err2: |
| 1443 | OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_MALLOC_FAILURE); |
| 1444 | rv = 0; |
| 1445 | ret: |
| 1446 | if (buff != NULL) OPENSSL_free(buff); |
| 1447 | if (val != NULL) OPENSSL_free(val); |
| 1448 | return(rv); |
| 1449 | } |
| 1450 | |
| 1451 | int tls1_alert_code(int code) |
| 1452 | { |
| 1453 | switch (code) |
| 1454 | { |
| 1455 | case SSL_AD_CLOSE_NOTIFY: return(SSL3_AD_CLOSE_NOTIFY); |
| 1456 | case SSL_AD_UNEXPECTED_MESSAGE: return(SSL3_AD_UNEXPECTED_MESSAGE); |
| 1457 | case SSL_AD_BAD_RECORD_MAC: return(SSL3_AD_BAD_RECORD_MAC); |
| 1458 | case SSL_AD_DECRYPTION_FAILED: return(TLS1_AD_DECRYPTION_FAILED); |
| 1459 | case SSL_AD_RECORD_OVERFLOW: return(TLS1_AD_RECORD_OVERFLOW); |
| 1460 | case SSL_AD_DECOMPRESSION_FAILURE:return(SSL3_AD_DECOMPRESSION_FAILURE); |
| 1461 | case SSL_AD_HANDSHAKE_FAILURE: return(SSL3_AD_HANDSHAKE_FAILURE); |
| 1462 | case SSL_AD_NO_CERTIFICATE: return(-1); |
| 1463 | case SSL_AD_BAD_CERTIFICATE: return(SSL3_AD_BAD_CERTIFICATE); |
| 1464 | case SSL_AD_UNSUPPORTED_CERTIFICATE:return(SSL3_AD_UNSUPPORTED_CERTIFICATE); |
| 1465 | case SSL_AD_CERTIFICATE_REVOKED:return(SSL3_AD_CERTIFICATE_REVOKED); |
| 1466 | case SSL_AD_CERTIFICATE_EXPIRED:return(SSL3_AD_CERTIFICATE_EXPIRED); |
| 1467 | case SSL_AD_CERTIFICATE_UNKNOWN:return(SSL3_AD_CERTIFICATE_UNKNOWN); |
| 1468 | case SSL_AD_ILLEGAL_PARAMETER: return(SSL3_AD_ILLEGAL_PARAMETER); |
| 1469 | case SSL_AD_UNKNOWN_CA: return(TLS1_AD_UNKNOWN_CA); |
| 1470 | case SSL_AD_ACCESS_DENIED: return(TLS1_AD_ACCESS_DENIED); |
| 1471 | case SSL_AD_DECODE_ERROR: return(TLS1_AD_DECODE_ERROR); |
| 1472 | case SSL_AD_DECRYPT_ERROR: return(TLS1_AD_DECRYPT_ERROR); |
| 1473 | case SSL_AD_EXPORT_RESTRICTION: return(TLS1_AD_EXPORT_RESTRICTION); |
| 1474 | case SSL_AD_PROTOCOL_VERSION: return(TLS1_AD_PROTOCOL_VERSION); |
| 1475 | case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY); |
| 1476 | case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR); |
| 1477 | case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED); |
| 1478 | case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION); |
| 1479 | case SSL_AD_UNSUPPORTED_EXTENSION: return(TLS1_AD_UNSUPPORTED_EXTENSION); |
| 1480 | case SSL_AD_CERTIFICATE_UNOBTAINABLE: return(TLS1_AD_CERTIFICATE_UNOBTAINABLE); |
| 1481 | case SSL_AD_UNRECOGNIZED_NAME: return(TLS1_AD_UNRECOGNIZED_NAME); |
| 1482 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: return(TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE); |
| 1483 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: return(TLS1_AD_BAD_CERTIFICATE_HASH_VALUE); |
| 1484 | case SSL_AD_UNKNOWN_PSK_IDENTITY:return(TLS1_AD_UNKNOWN_PSK_IDENTITY); |
Adam Langley | 2970779 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1485 | case SSL_AD_INAPPROPRIATE_FALLBACK:return(SSL3_AD_INAPPROPRIATE_FALLBACK); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1486 | #if 0 /* not appropriate for TLS, not used for DTLS */ |
| 1487 | case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return |
| 1488 | (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); |
| 1489 | #endif |
| 1490 | default: return(-1); |
| 1491 | } |
| 1492 | } |