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