t1_enc.c revision 291721
1/* ssl/t1_enc.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58/* ==================================================================== 59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111/* ==================================================================== 112 * Copyright 2005 Nokia. All rights reserved. 113 * 114 * The portions of the attached software ("Contribution") is developed by 115 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 116 * license. 117 * 118 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 119 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 120 * support (see RFC 4279) to OpenSSL. 121 * 122 * No patent licenses or other rights except those expressly stated in 123 * the OpenSSL open source license shall be deemed granted or received 124 * expressly, by implication, estoppel, or otherwise. 125 * 126 * No assurances are provided by Nokia that the Contribution does not 127 * infringe the patent or other intellectual property rights of any third 128 * party or that the license provides you with all the necessary rights 129 * to make use of the Contribution. 130 * 131 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 132 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 133 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 134 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 135 * OTHERWISE. 136 */ 137 138#include <stdio.h> 139#include "ssl_locl.h" 140#ifndef OPENSSL_NO_COMP 141# include <openssl/comp.h> 142#endif 143#include <openssl/evp.h> 144#include <openssl/hmac.h> 145#include <openssl/md5.h> 146#include <openssl/rand.h> 147#ifdef KSSL_DEBUG 148# include <openssl/des.h> 149#endif 150 151/* seed1 through seed5 are virtually concatenated */ 152static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec, 153 int sec_len, 154 const void *seed1, int seed1_len, 155 const void *seed2, int seed2_len, 156 const void *seed3, int seed3_len, 157 const void *seed4, int seed4_len, 158 const void *seed5, int seed5_len, 159 unsigned char *out, int olen) 160{ 161 int chunk; 162 size_t j; 163 EVP_MD_CTX ctx, ctx_tmp; 164 EVP_PKEY *mac_key; 165 unsigned char A1[EVP_MAX_MD_SIZE]; 166 size_t A1_len; 167 int ret = 0; 168 169 chunk = EVP_MD_size(md); 170 OPENSSL_assert(chunk >= 0); 171 172 EVP_MD_CTX_init(&ctx); 173 EVP_MD_CTX_init(&ctx_tmp); 174 EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 175 EVP_MD_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 176 mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len); 177 if (!mac_key) 178 goto err; 179 if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key)) 180 goto err; 181 if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key)) 182 goto err; 183 if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len)) 184 goto err; 185 if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len)) 186 goto err; 187 if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len)) 188 goto err; 189 if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len)) 190 goto err; 191 if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len)) 192 goto err; 193 if (!EVP_DigestSignFinal(&ctx, A1, &A1_len)) 194 goto err; 195 196 for (;;) { 197 /* Reinit mac contexts */ 198 if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key)) 199 goto err; 200 if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key)) 201 goto err; 202 if (!EVP_DigestSignUpdate(&ctx, A1, A1_len)) 203 goto err; 204 if (!EVP_DigestSignUpdate(&ctx_tmp, A1, A1_len)) 205 goto err; 206 if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len)) 207 goto err; 208 if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len)) 209 goto err; 210 if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len)) 211 goto err; 212 if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len)) 213 goto err; 214 if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len)) 215 goto err; 216 217 if (olen > chunk) { 218 if (!EVP_DigestSignFinal(&ctx, out, &j)) 219 goto err; 220 out += j; 221 olen -= j; 222 /* calc the next A1 value */ 223 if (!EVP_DigestSignFinal(&ctx_tmp, A1, &A1_len)) 224 goto err; 225 } else { /* last one */ 226 227 if (!EVP_DigestSignFinal(&ctx, A1, &A1_len)) 228 goto err; 229 memcpy(out, A1, olen); 230 break; 231 } 232 } 233 ret = 1; 234 err: 235 EVP_PKEY_free(mac_key); 236 EVP_MD_CTX_cleanup(&ctx); 237 EVP_MD_CTX_cleanup(&ctx_tmp); 238 OPENSSL_cleanse(A1, sizeof(A1)); 239 return ret; 240} 241 242/* seed1 through seed5 are virtually concatenated */ 243static int tls1_PRF(long digest_mask, 244 const void *seed1, int seed1_len, 245 const void *seed2, int seed2_len, 246 const void *seed3, int seed3_len, 247 const void *seed4, int seed4_len, 248 const void *seed5, int seed5_len, 249 const unsigned char *sec, int slen, 250 unsigned char *out1, 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) 262 count++; 263 } 264 if (!count) { 265 /* Should never happen */ 266 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR); 267 goto err; 268 } 269 len = slen / count; 270 if (count == 1) 271 slen = 0; 272 S1 = sec; 273 memset(out1, 0, olen); 274 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) { 275 if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) { 276 if (!md) { 277 SSLerr(SSL_F_TLS1_PRF, SSL_R_UNSUPPORTED_DIGEST_TYPE); 278 goto err; 279 } 280 if (!tls1_P_hash(md, S1, len + (slen & 1), 281 seed1, seed1_len, seed2, seed2_len, seed3, 282 seed3_len, seed4, seed4_len, seed5, seed5_len, 283 out2, olen)) 284 goto err; 285 S1 += len; 286 for (i = 0; i < olen; i++) { 287 out1[i] ^= out2[i]; 288 } 289 } 290 } 291 ret = 1; 292 err: 293 return ret; 294} 295 296static int tls1_generate_key_block(SSL *s, unsigned char *km, 297 unsigned char *tmp, int num) 298{ 299 int ret; 300 ret = tls1_PRF(ssl_get_algorithm2(s), 301 TLS_MD_KEY_EXPANSION_CONST, 302 TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random, 303 SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE, 304 NULL, 0, NULL, 0, s->session->master_key, 305 s->session->master_key_length, km, tmp, num); 306#ifdef KSSL_DEBUG 307 fprintf(stderr, "tls1_generate_key_block() ==> %d byte master_key =\n\t", 308 s->session->master_key_length); 309 { 310 int i; 311 for (i = 0; i < s->session->master_key_length; i++) { 312 fprintf(stderr, "%02X", s->session->master_key[i]); 313 } 314 fprintf(stderr, "\n"); 315 } 316#endif /* KSSL_DEBUG */ 317 return ret; 318} 319 320int tls1_change_cipher_state(SSL *s, int which) 321{ 322 static const unsigned char empty[] = ""; 323 unsigned char *p, *mac_secret; 324 unsigned char *exp_label; 325 unsigned char tmp1[EVP_MAX_KEY_LENGTH]; 326 unsigned char tmp2[EVP_MAX_KEY_LENGTH]; 327 unsigned char iv1[EVP_MAX_IV_LENGTH * 2]; 328 unsigned char iv2[EVP_MAX_IV_LENGTH * 2]; 329 unsigned char *ms, *key, *iv; 330 int client_write; 331 EVP_CIPHER_CTX *dd; 332 const EVP_CIPHER *c; 333#ifndef OPENSSL_NO_COMP 334 const SSL_COMP *comp; 335#endif 336 const EVP_MD *m; 337 int mac_type; 338 int *mac_secret_size; 339 EVP_MD_CTX *mac_ctx; 340 EVP_PKEY *mac_key; 341 int is_export, n, i, j, k, exp_label_len, cl; 342 int reuse_dd = 0; 343 344 is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); 345 c = s->s3->tmp.new_sym_enc; 346 m = s->s3->tmp.new_hash; 347 mac_type = s->s3->tmp.new_mac_pkey_type; 348#ifndef OPENSSL_NO_COMP 349 comp = s->s3->tmp.new_compression; 350#endif 351 352#ifdef KSSL_DEBUG 353 fprintf(stderr, "tls1_change_cipher_state(which= %d) w/\n", which); 354 fprintf(stderr, "\talg= %ld/%ld, comp= %p\n", 355 s->s3->tmp.new_cipher->algorithm_mkey, 356 s->s3->tmp.new_cipher->algorithm_auth, comp); 357 fprintf(stderr, "\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c); 358 fprintf(stderr, "\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n", 359 c->nid, c->block_size, c->key_len, c->iv_len); 360 fprintf(stderr, "\tkey_block: len= %d, data= ", 361 s->s3->tmp.key_block_length); 362 { 363 int i; 364 for (i = 0; i < s->s3->tmp.key_block_length; i++) 365 fprintf(stderr, "%02x", s->s3->tmp.key_block[i]); 366 fprintf(stderr, "\n"); 367 } 368#endif /* KSSL_DEBUG */ 369 370 if (which & SSL3_CC_READ) { 371 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) 372 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM; 373 else 374 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM; 375 376 if (s->enc_read_ctx != NULL) 377 reuse_dd = 1; 378 else if ((s->enc_read_ctx = 379 OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) 380 goto err; 381 else 382 /* 383 * make sure it's intialized in case we exit later with an error 384 */ 385 EVP_CIPHER_CTX_init(s->enc_read_ctx); 386 dd = s->enc_read_ctx; 387 mac_ctx = ssl_replace_hash(&s->read_hash, NULL); 388 if (mac_ctx == NULL) 389 goto err; 390#ifndef OPENSSL_NO_COMP 391 if (s->expand != NULL) { 392 COMP_CTX_free(s->expand); 393 s->expand = NULL; 394 } 395 if (comp != NULL) { 396 s->expand = COMP_CTX_new(comp->method); 397 if (s->expand == NULL) { 398 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, 399 SSL_R_COMPRESSION_LIBRARY_ERROR); 400 goto err2; 401 } 402 if (s->s3->rrec.comp == NULL) 403 s->s3->rrec.comp = (unsigned char *) 404 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH); 405 if (s->s3->rrec.comp == NULL) 406 goto err; 407 } 408#endif 409 /* 410 * this is done by dtls1_reset_seq_numbers for DTLS1_VERSION 411 */ 412 if (s->version != DTLS1_VERSION) 413 memset(&(s->s3->read_sequence[0]), 0, 8); 414 mac_secret = &(s->s3->read_mac_secret[0]); 415 mac_secret_size = &(s->s3->read_mac_secret_size); 416 } else { 417 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) 418 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM; 419 else 420 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM; 421 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) 422 reuse_dd = 1; 423 else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) 424 goto err; 425 dd = s->enc_write_ctx; 426 if (SSL_IS_DTLS(s)) { 427 mac_ctx = EVP_MD_CTX_create(); 428 if (mac_ctx == NULL) 429 goto err; 430 s->write_hash = mac_ctx; 431 } else { 432 mac_ctx = ssl_replace_hash(&s->write_hash, NULL); 433 if (mac_ctx == NULL) 434 goto err; 435 } 436#ifndef OPENSSL_NO_COMP 437 if (s->compress != NULL) { 438 COMP_CTX_free(s->compress); 439 s->compress = NULL; 440 } 441 if (comp != NULL) { 442 s->compress = COMP_CTX_new(comp->method); 443 if (s->compress == NULL) { 444 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, 445 SSL_R_COMPRESSION_LIBRARY_ERROR); 446 goto err2; 447 } 448 } 449#endif 450 /* 451 * this is done by dtls1_reset_seq_numbers for DTLS1_VERSION 452 */ 453 if (s->version != DTLS1_VERSION) 454 memset(&(s->s3->write_sequence[0]), 0, 8); 455 mac_secret = &(s->s3->write_mac_secret[0]); 456 mac_secret_size = &(s->s3->write_mac_secret_size); 457 } 458 459 if (reuse_dd) 460 EVP_CIPHER_CTX_cleanup(dd); 461 462 p = s->s3->tmp.key_block; 463 i = *mac_secret_size = s->s3->tmp.new_mac_secret_size; 464 465 cl = EVP_CIPHER_key_length(c); 466 j = is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? 467 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl; 468 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */ 469 /* If GCM mode only part of IV comes from PRF */ 470 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) 471 k = EVP_GCM_TLS_FIXED_IV_LEN; 472 else 473 k = EVP_CIPHER_iv_length(c); 474 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) || 475 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) { 476 ms = &(p[0]); 477 n = i + i; 478 key = &(p[n]); 479 n += j + j; 480 iv = &(p[n]); 481 n += k + k; 482 exp_label = (unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST; 483 exp_label_len = TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE; 484 client_write = 1; 485 } else { 486 n = i; 487 ms = &(p[n]); 488 n += i + j; 489 key = &(p[n]); 490 n += j + k; 491 iv = &(p[n]); 492 n += k; 493 exp_label = (unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST; 494 exp_label_len = TLS_MD_SERVER_WRITE_KEY_CONST_SIZE; 495 client_write = 0; 496 } 497 498 if (n > s->s3->tmp.key_block_length) { 499 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 500 goto err2; 501 } 502 503 memcpy(mac_secret, ms, i); 504 505 if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) { 506 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, 507 mac_secret, *mac_secret_size); 508 if (mac_key == NULL 509 || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) { 510 EVP_PKEY_free(mac_key); 511 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 512 goto err2; 513 } 514 EVP_PKEY_free(mac_key); 515 } 516#ifdef TLS_DEBUG 517 printf("which = %04X\nmac key=", which); 518 { 519 int z; 520 for (z = 0; z < i; z++) 521 printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n'); 522 } 523#endif 524 if (is_export) { 525 /* 526 * In here I set both the read and write key/iv to the same value 527 * since only the correct one will be used :-). 528 */ 529 if (!tls1_PRF(ssl_get_algorithm2(s), 530 exp_label, exp_label_len, 531 s->s3->client_random, SSL3_RANDOM_SIZE, 532 s->s3->server_random, SSL3_RANDOM_SIZE, 533 NULL, 0, NULL, 0, 534 key, j, tmp1, tmp2, EVP_CIPHER_key_length(c))) 535 goto err2; 536 key = tmp1; 537 538 if (k > 0) { 539 if (!tls1_PRF(ssl_get_algorithm2(s), 540 TLS_MD_IV_BLOCK_CONST, TLS_MD_IV_BLOCK_CONST_SIZE, 541 s->s3->client_random, SSL3_RANDOM_SIZE, 542 s->s3->server_random, SSL3_RANDOM_SIZE, 543 NULL, 0, NULL, 0, empty, 0, iv1, iv2, k * 2)) 544 goto err2; 545 if (client_write) 546 iv = iv1; 547 else 548 iv = &(iv1[k]); 549 } 550 } 551 552 s->session->key_arg_length = 0; 553#ifdef KSSL_DEBUG 554 { 555 int i; 556 fprintf(stderr, "EVP_CipherInit_ex(dd,c,key=,iv=,which)\n"); 557 fprintf(stderr, "\tkey= "); 558 for (i = 0; i < c->key_len; i++) 559 fprintf(stderr, "%02x", key[i]); 560 fprintf(stderr, "\n"); 561 fprintf(stderr, "\t iv= "); 562 for (i = 0; i < c->iv_len; i++) 563 fprintf(stderr, "%02x", iv[i]); 564 fprintf(stderr, "\n"); 565 } 566#endif /* KSSL_DEBUG */ 567 568 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) { 569 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE)) 570 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, k, iv)) { 571 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 572 goto err2; 573 } 574 } else { 575 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) { 576 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 577 goto err2; 578 } 579 } 580 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */ 581 if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size 582 && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY, 583 *mac_secret_size, mac_secret)) { 584 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 585 goto err2; 586 } 587 588#ifdef TLS_DEBUG 589 printf("which = %04X\nkey=", which); 590 { 591 int z; 592 for (z = 0; z < EVP_CIPHER_key_length(c); z++) 593 printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n'); 594 } 595 printf("\niv="); 596 { 597 int z; 598 for (z = 0; z < k; z++) 599 printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n'); 600 } 601 printf("\n"); 602#endif 603 604 OPENSSL_cleanse(tmp1, sizeof(tmp1)); 605 OPENSSL_cleanse(tmp2, sizeof(tmp1)); 606 OPENSSL_cleanse(iv1, sizeof(iv1)); 607 OPENSSL_cleanse(iv2, sizeof(iv2)); 608 return (1); 609 err: 610 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); 611 err2: 612 return (0); 613} 614 615int tls1_setup_key_block(SSL *s) 616{ 617 unsigned char *p1, *p2 = NULL; 618 const EVP_CIPHER *c; 619 const EVP_MD *hash; 620 int num; 621 SSL_COMP *comp; 622 int mac_type = NID_undef, mac_secret_size = 0; 623 int ret = 0; 624 625#ifdef KSSL_DEBUG 626 fprintf(stderr, "tls1_setup_key_block()\n"); 627#endif /* KSSL_DEBUG */ 628 629 if (s->s3->tmp.key_block_length != 0) 630 return (1); 631 632 if (!ssl_cipher_get_evp 633 (s->session, &c, &hash, &mac_type, &mac_secret_size, &comp)) { 634 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 635 return (0); 636 } 637 638 s->s3->tmp.new_sym_enc = c; 639 s->s3->tmp.new_hash = hash; 640 s->s3->tmp.new_mac_pkey_type = mac_type; 641 s->s3->tmp.new_mac_secret_size = mac_secret_size; 642 num = 643 EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c); 644 num *= 2; 645 646 ssl3_cleanup_key_block(s); 647 648 if ((p1 = (unsigned char *)OPENSSL_malloc(num)) == NULL) { 649 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE); 650 goto err; 651 } 652 653 s->s3->tmp.key_block_length = num; 654 s->s3->tmp.key_block = p1; 655 656 if ((p2 = (unsigned char *)OPENSSL_malloc(num)) == NULL) { 657 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE); 658 OPENSSL_free(p1); 659 goto err; 660 } 661#ifdef TLS_DEBUG 662 printf("client random\n"); 663 { 664 int z; 665 for (z = 0; z < SSL3_RANDOM_SIZE; z++) 666 printf("%02X%c", s->s3->client_random[z], 667 ((z + 1) % 16) ? ' ' : '\n'); 668 } 669 printf("server random\n"); 670 { 671 int z; 672 for (z = 0; z < SSL3_RANDOM_SIZE; z++) 673 printf("%02X%c", s->s3->server_random[z], 674 ((z + 1) % 16) ? ' ' : '\n'); 675 } 676 printf("pre-master\n"); 677 { 678 int z; 679 for (z = 0; z < s->session->master_key_length; z++) 680 printf("%02X%c", s->session->master_key[z], 681 ((z + 1) % 16) ? ' ' : '\n'); 682 } 683#endif 684 if (!tls1_generate_key_block(s, p1, p2, num)) 685 goto err; 686#ifdef TLS_DEBUG 687 printf("\nkey block\n"); 688 { 689 int z; 690 for (z = 0; z < num; z++) 691 printf("%02X%c", p1[z], ((z + 1) % 16) ? ' ' : '\n'); 692 } 693#endif 694 695 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) 696 && s->method->version <= TLS1_VERSION) { 697 /* 698 * enable vulnerability countermeasure for CBC ciphers with known-IV 699 * problem (http://www.openssl.org/~bodo/tls-cbc.txt) 700 */ 701 s->s3->need_empty_fragments = 1; 702 703 if (s->session->cipher != NULL) { 704 if (s->session->cipher->algorithm_enc == SSL_eNULL) 705 s->s3->need_empty_fragments = 0; 706 707#ifndef OPENSSL_NO_RC4 708 if (s->session->cipher->algorithm_enc == SSL_RC4) 709 s->s3->need_empty_fragments = 0; 710#endif 711 } 712 } 713 714 ret = 1; 715 err: 716 if (p2) { 717 OPENSSL_cleanse(p2, num); 718 OPENSSL_free(p2); 719 } 720 return (ret); 721} 722 723/*- 724 * tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively. 725 * 726 * Returns: 727 * 0: (in non-constant time) if the record is publically invalid (i.e. too 728 * short etc). 729 * 1: if the record's padding is valid / the encryption was successful. 730 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending, 731 * an internal error occured. 732 */ 733int tls1_enc(SSL *s, int send) 734{ 735 SSL3_RECORD *rec; 736 EVP_CIPHER_CTX *ds; 737 unsigned long l; 738 int bs, i, j, k, pad = 0, ret, mac_size = 0; 739 const EVP_CIPHER *enc; 740 741 if (send) { 742 if (EVP_MD_CTX_md(s->write_hash)) { 743 int n = EVP_MD_CTX_size(s->write_hash); 744 OPENSSL_assert(n >= 0); 745 } 746 ds = s->enc_write_ctx; 747 rec = &(s->s3->wrec); 748 if (s->enc_write_ctx == NULL) 749 enc = NULL; 750 else { 751 int ivlen; 752 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 753 /* For TLSv1.1 and later explicit IV */ 754 if (s->version >= TLS1_1_VERSION 755 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE) 756 ivlen = EVP_CIPHER_iv_length(enc); 757 else 758 ivlen = 0; 759 if (ivlen > 1) { 760 if (rec->data != rec->input) 761 /* 762 * we can't write into the input stream: Can this ever 763 * happen?? (steve) 764 */ 765 fprintf(stderr, 766 "%s:%d: rec->data != rec->input\n", 767 __FILE__, __LINE__); 768 else if (RAND_bytes(rec->input, ivlen) <= 0) 769 return -1; 770 } 771 } 772 } else { 773 if (EVP_MD_CTX_md(s->read_hash)) { 774 int n = EVP_MD_CTX_size(s->read_hash); 775 OPENSSL_assert(n >= 0); 776 } 777 ds = s->enc_read_ctx; 778 rec = &(s->s3->rrec); 779 if (s->enc_read_ctx == NULL) 780 enc = NULL; 781 else 782 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx); 783 } 784 785#ifdef KSSL_DEBUG 786 fprintf(stderr, "tls1_enc(%d)\n", send); 787#endif /* KSSL_DEBUG */ 788 789 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) { 790 memmove(rec->data, rec->input, rec->length); 791 rec->input = rec->data; 792 ret = 1; 793 } else { 794 l = rec->length; 795 bs = EVP_CIPHER_block_size(ds->cipher); 796 797 if (EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) { 798 unsigned char buf[EVP_AEAD_TLS1_AAD_LEN], *seq; 799 800 seq = send ? s->s3->write_sequence : s->s3->read_sequence; 801 802 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) { 803 unsigned char dtlsseq[9], *p = dtlsseq; 804 805 s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p); 806 memcpy(p, &seq[2], 6); 807 memcpy(buf, dtlsseq, 8); 808 } else { 809 memcpy(buf, seq, 8); 810 for (i = 7; i >= 0; i--) { /* increment */ 811 ++seq[i]; 812 if (seq[i] != 0) 813 break; 814 } 815 } 816 817 buf[8] = rec->type; 818 buf[9] = (unsigned char)(s->version >> 8); 819 buf[10] = (unsigned char)(s->version); 820 buf[11] = rec->length >> 8; 821 buf[12] = rec->length & 0xff; 822 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD, 823 EVP_AEAD_TLS1_AAD_LEN, buf); 824 if (pad <= 0) 825 return -1; 826 if (send) { 827 l += pad; 828 rec->length += pad; 829 } 830 } else if ((bs != 1) && send) { 831 i = bs - ((int)l % bs); 832 833 /* Add weird padding of upto 256 bytes */ 834 835 /* we need to add 'i' padding bytes of value j */ 836 j = i - 1; 837 if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) { 838 if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) 839 j++; 840 } 841 for (k = (int)l; k < (int)(l + i); k++) 842 rec->input[k] = j; 843 l += i; 844 rec->length += i; 845 } 846#ifdef KSSL_DEBUG 847 { 848 unsigned long ui; 849 fprintf(stderr, 850 "EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", 851 ds, rec->data, rec->input, l); 852 fprintf(stderr, 853 "\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%lu %lu], %d iv_len\n", 854 ds->buf_len, ds->cipher->key_len, DES_KEY_SZ, 855 DES_SCHEDULE_SZ, ds->cipher->iv_len); 856 fprintf(stderr, "\t\tIV: "); 857 for (i = 0; i < ds->cipher->iv_len; i++) 858 fprintf(stderr, "%02X", ds->iv[i]); 859 fprintf(stderr, "\n"); 860 fprintf(stderr, "\trec->input="); 861 for (ui = 0; ui < l; ui++) 862 fprintf(stderr, " %02x", rec->input[ui]); 863 fprintf(stderr, "\n"); 864 } 865#endif /* KSSL_DEBUG */ 866 867 if (!send) { 868 if (l == 0 || l % bs != 0) 869 return 0; 870 } 871 872 i = EVP_Cipher(ds, rec->data, rec->input, l); 873 if ((EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_CUSTOM_CIPHER) 874 ? (i < 0) 875 : (i == 0)) 876 return -1; /* AEAD can fail to verify MAC */ 877 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE && !send) { 878 rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN; 879 rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN; 880 rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN; 881 } 882#ifdef KSSL_DEBUG 883 { 884 unsigned long i; 885 fprintf(stderr, "\trec->data="); 886 for (i = 0; i < l; i++) 887 fprintf(stderr, " %02x", rec->data[i]); 888 fprintf(stderr, "\n"); 889 } 890#endif /* KSSL_DEBUG */ 891 892 ret = 1; 893 if (EVP_MD_CTX_md(s->read_hash) != NULL) 894 mac_size = EVP_MD_CTX_size(s->read_hash); 895 if ((bs != 1) && !send) 896 ret = tls1_cbc_remove_padding(s, rec, bs, mac_size); 897 if (pad && !send) 898 rec->length -= pad; 899 } 900 return ret; 901} 902 903int tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out) 904{ 905 unsigned int ret; 906 EVP_MD_CTX ctx, *d = NULL; 907 int i; 908 909 if (s->s3->handshake_buffer) 910 if (!ssl3_digest_cached_records(s)) 911 return 0; 912 913 for (i = 0; i < SSL_MAX_DIGEST; i++) { 914 if (s->s3->handshake_dgst[i] 915 && EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) { 916 d = s->s3->handshake_dgst[i]; 917 break; 918 } 919 } 920 if (!d) { 921 SSLerr(SSL_F_TLS1_CERT_VERIFY_MAC, SSL_R_NO_REQUIRED_DIGEST); 922 return 0; 923 } 924 925 EVP_MD_CTX_init(&ctx); 926 if (EVP_MD_CTX_copy_ex(&ctx, d) <=0 927 || EVP_DigestFinal_ex(&ctx, out, &ret) <= 0) 928 ret = 0; 929 EVP_MD_CTX_cleanup(&ctx); 930 return ((int)ret); 931} 932 933int tls1_final_finish_mac(SSL *s, 934 const char *str, int slen, unsigned char *out) 935{ 936 unsigned int i; 937 EVP_MD_CTX ctx; 938 unsigned char buf[2 * EVP_MAX_MD_SIZE]; 939 unsigned char *q, buf2[12]; 940 int idx; 941 long mask; 942 int err = 0; 943 const EVP_MD *md; 944 945 q = buf; 946 947 if (s->s3->handshake_buffer) 948 if (!ssl3_digest_cached_records(s)) 949 return 0; 950 951 EVP_MD_CTX_init(&ctx); 952 953 for (idx = 0; ssl_get_handshake_digest(idx, &mask, &md); idx++) { 954 if (mask & ssl_get_algorithm2(s)) { 955 int hashsize = EVP_MD_size(md); 956 EVP_MD_CTX *hdgst = s->s3->handshake_dgst[idx]; 957 if (!hdgst || hashsize < 0 958 || hashsize > (int)(sizeof buf - (size_t)(q - buf))) { 959 /* 960 * internal error: 'buf' is too small for this cipersuite! 961 */ 962 err = 1; 963 } else { 964 if (!EVP_MD_CTX_copy_ex(&ctx, hdgst) || 965 !EVP_DigestFinal_ex(&ctx, q, &i) || 966 (i != (unsigned int)hashsize)) 967 err = 1; 968 q += hashsize; 969 } 970 } 971 } 972 973 if (!tls1_PRF(ssl_get_algorithm2(s), 974 str, slen, buf, (int)(q - buf), NULL, 0, NULL, 0, NULL, 0, 975 s->session->master_key, s->session->master_key_length, 976 out, buf2, sizeof buf2)) 977 err = 1; 978 EVP_MD_CTX_cleanup(&ctx); 979 980 OPENSSL_cleanse(buf, (int)(q - buf)); 981 OPENSSL_cleanse(buf2, sizeof(buf2)); 982 if (err) 983 return 0; 984 else 985 return sizeof buf2; 986} 987 988int tls1_mac(SSL *ssl, unsigned char *md, int send) 989{ 990 SSL3_RECORD *rec; 991 unsigned char *seq; 992 EVP_MD_CTX *hash; 993 size_t md_size, orig_len; 994 int i; 995 EVP_MD_CTX hmac, *mac_ctx; 996 unsigned char header[13]; 997 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) 998 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM)); 999 int t; 1000 1001 if (send) { 1002 rec = &(ssl->s3->wrec); 1003 seq = &(ssl->s3->write_sequence[0]); 1004 hash = ssl->write_hash; 1005 } else { 1006 rec = &(ssl->s3->rrec); 1007 seq = &(ssl->s3->read_sequence[0]); 1008 hash = ssl->read_hash; 1009 } 1010 1011 t = EVP_MD_CTX_size(hash); 1012 OPENSSL_assert(t >= 0); 1013 md_size = t; 1014 1015 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */ 1016 if (stream_mac) { 1017 mac_ctx = hash; 1018 } else { 1019 if (!EVP_MD_CTX_copy(&hmac, hash)) 1020 return -1; 1021 mac_ctx = &hmac; 1022 } 1023 1024 if (ssl->version == DTLS1_VERSION || ssl->version == DTLS1_BAD_VER) { 1025 unsigned char dtlsseq[8], *p = dtlsseq; 1026 1027 s2n(send ? ssl->d1->w_epoch : ssl->d1->r_epoch, p); 1028 memcpy(p, &seq[2], 6); 1029 1030 memcpy(header, dtlsseq, 8); 1031 } else 1032 memcpy(header, seq, 8); 1033 1034 /* 1035 * kludge: tls1_cbc_remove_padding passes padding length in rec->type 1036 */ 1037 orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8); 1038 rec->type &= 0xff; 1039 1040 header[8] = rec->type; 1041 header[9] = (unsigned char)(ssl->version >> 8); 1042 header[10] = (unsigned char)(ssl->version); 1043 header[11] = (rec->length) >> 8; 1044 header[12] = (rec->length) & 0xff; 1045 1046 if (!send && 1047 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE && 1048 ssl3_cbc_record_digest_supported(mac_ctx)) { 1049 /* 1050 * This is a CBC-encrypted record. We must avoid leaking any 1051 * timing-side channel information about how many blocks of data we 1052 * are hashing because that gives an attacker a timing-oracle. 1053 */ 1054 /* Final param == not SSLv3 */ 1055 if (ssl3_cbc_digest_record(mac_ctx, 1056 md, &md_size, 1057 header, rec->input, 1058 rec->length + md_size, orig_len, 1059 ssl->s3->read_mac_secret, 1060 ssl->s3->read_mac_secret_size, 0) <= 0) { 1061 if (!stream_mac) 1062 EVP_MD_CTX_cleanup(&hmac); 1063 return -1; 1064 } 1065 } else { 1066 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0 1067 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0 1068 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) { 1069 if (!stream_mac) 1070 EVP_MD_CTX_cleanup(&hmac); 1071 return -1; 1072 } 1073#ifdef OPENSSL_FIPS 1074 if (!send && FIPS_mode()) 1075 tls_fips_digest_extra(ssl->enc_read_ctx, 1076 mac_ctx, rec->input, rec->length, orig_len); 1077#endif 1078 } 1079 1080 if (!stream_mac) 1081 EVP_MD_CTX_cleanup(&hmac); 1082#ifdef TLS_DEBUG 1083 fprintf(stderr, "seq="); 1084 { 1085 int z; 1086 for (z = 0; z < 8; z++) 1087 fprintf(stderr, "%02X ", seq[z]); 1088 fprintf(stderr, "\n"); 1089 } 1090 fprintf(stderr, "rec="); 1091 { 1092 unsigned int z; 1093 for (z = 0; z < rec->length; z++) 1094 fprintf(stderr, "%02X ", rec->data[z]); 1095 fprintf(stderr, "\n"); 1096 } 1097#endif 1098 1099 if (ssl->version != DTLS1_VERSION && ssl->version != DTLS1_BAD_VER) { 1100 for (i = 7; i >= 0; i--) { 1101 ++seq[i]; 1102 if (seq[i] != 0) 1103 break; 1104 } 1105 } 1106#ifdef TLS_DEBUG 1107 { 1108 unsigned int z; 1109 for (z = 0; z < md_size; z++) 1110 fprintf(stderr, "%02X ", md[z]); 1111 fprintf(stderr, "\n"); 1112 } 1113#endif 1114 return (md_size); 1115} 1116 1117int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, 1118 int len) 1119{ 1120 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; 1121 const void *co = NULL, *so = NULL; 1122 int col = 0, sol = 0; 1123 1124#ifdef KSSL_DEBUG 1125 fprintf(stderr, "tls1_generate_master_secret(%p,%p, %p, %d)\n", s, out, p, 1126 len); 1127#endif /* KSSL_DEBUG */ 1128 1129#ifdef TLSEXT_TYPE_opaque_prf_input 1130 if (s->s3->client_opaque_prf_input != NULL 1131 && s->s3->server_opaque_prf_input != NULL 1132 && s->s3->client_opaque_prf_input_len > 0 1133 && s->s3->client_opaque_prf_input_len == 1134 s->s3->server_opaque_prf_input_len) { 1135 co = s->s3->client_opaque_prf_input; 1136 col = s->s3->server_opaque_prf_input_len; 1137 so = s->s3->server_opaque_prf_input; 1138 /* 1139 * must be same as col (see 1140 * draft-resc-00.txts-opaque-prf-input-00.txt, section 3.1) 1141 */ 1142 sol = s->s3->client_opaque_prf_input_len; 1143 } 1144#endif 1145 1146 tls1_PRF(ssl_get_algorithm2(s), 1147 TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE, 1148 s->s3->client_random, SSL3_RANDOM_SIZE, 1149 co, col, 1150 s->s3->server_random, SSL3_RANDOM_SIZE, 1151 so, sol, p, len, s->session->master_key, buff, sizeof buff); 1152 OPENSSL_cleanse(buff, sizeof buff); 1153#ifdef SSL_DEBUG 1154 fprintf(stderr, "Premaster Secret:\n"); 1155 BIO_dump_fp(stderr, (char *)p, len); 1156 fprintf(stderr, "Client Random:\n"); 1157 BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE); 1158 fprintf(stderr, "Server Random:\n"); 1159 BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE); 1160 fprintf(stderr, "Master Secret:\n"); 1161 BIO_dump_fp(stderr, (char *)s->session->master_key, 1162 SSL3_MASTER_SECRET_SIZE); 1163#endif 1164 1165#ifdef KSSL_DEBUG 1166 fprintf(stderr, "tls1_generate_master_secret() complete\n"); 1167#endif /* KSSL_DEBUG */ 1168 return (SSL3_MASTER_SECRET_SIZE); 1169} 1170 1171int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1172 const char *label, size_t llen, 1173 const unsigned char *context, 1174 size_t contextlen, int use_context) 1175{ 1176 unsigned char *buff; 1177 unsigned char *val = NULL; 1178 size_t vallen, currentvalpos; 1179 int rv; 1180 1181#ifdef KSSL_DEBUG 1182 fprintf(stderr, "tls1_export_keying_material(%p,%p,%lu,%s,%lu,%p,%lu)\n", 1183 s, out, olen, label, llen, context, contextlen); 1184#endif /* KSSL_DEBUG */ 1185 1186 buff = OPENSSL_malloc(olen); 1187 if (buff == NULL) 1188 goto err2; 1189 1190 /* 1191 * construct PRF arguments we construct the PRF argument ourself rather 1192 * than passing separate values into the TLS PRF to ensure that the 1193 * concatenation of values does not create a prohibited label. 1194 */ 1195 vallen = llen + SSL3_RANDOM_SIZE * 2; 1196 if (use_context) { 1197 vallen += 2 + contextlen; 1198 } 1199 1200 val = OPENSSL_malloc(vallen); 1201 if (val == NULL) 1202 goto err2; 1203 currentvalpos = 0; 1204 memcpy(val + currentvalpos, (unsigned char *)label, llen); 1205 currentvalpos += llen; 1206 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE); 1207 currentvalpos += SSL3_RANDOM_SIZE; 1208 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE); 1209 currentvalpos += SSL3_RANDOM_SIZE; 1210 1211 if (use_context) { 1212 val[currentvalpos] = (contextlen >> 8) & 0xff; 1213 currentvalpos++; 1214 val[currentvalpos] = contextlen & 0xff; 1215 currentvalpos++; 1216 if ((contextlen > 0) || (context != NULL)) { 1217 memcpy(val + currentvalpos, context, contextlen); 1218 } 1219 } 1220 1221 /* 1222 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited 1223 * label len) = 15, so size of val > max(prohibited label len) = 15 and 1224 * the comparisons won't have buffer overflow 1225 */ 1226 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST, 1227 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0) 1228 goto err1; 1229 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST, 1230 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0) 1231 goto err1; 1232 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST, 1233 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) 1234 goto err1; 1235 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST, 1236 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) 1237 goto err1; 1238 1239 rv = tls1_PRF(ssl_get_algorithm2(s), 1240 val, vallen, 1241 NULL, 0, 1242 NULL, 0, 1243 NULL, 0, 1244 NULL, 0, 1245 s->session->master_key, s->session->master_key_length, 1246 out, buff, olen); 1247 OPENSSL_cleanse(val, vallen); 1248 OPENSSL_cleanse(buff, olen); 1249 1250#ifdef KSSL_DEBUG 1251 fprintf(stderr, "tls1_export_keying_material() complete\n"); 1252#endif /* KSSL_DEBUG */ 1253 goto ret; 1254 err1: 1255 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, 1256 SSL_R_TLS_ILLEGAL_EXPORTER_LABEL); 1257 rv = 0; 1258 goto ret; 1259 err2: 1260 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE); 1261 rv = 0; 1262 ret: 1263 if (buff != NULL) 1264 OPENSSL_free(buff); 1265 if (val != NULL) 1266 OPENSSL_free(val); 1267 return (rv); 1268} 1269 1270int tls1_alert_code(int code) 1271{ 1272 switch (code) { 1273 case SSL_AD_CLOSE_NOTIFY: 1274 return (SSL3_AD_CLOSE_NOTIFY); 1275 case SSL_AD_UNEXPECTED_MESSAGE: 1276 return (SSL3_AD_UNEXPECTED_MESSAGE); 1277 case SSL_AD_BAD_RECORD_MAC: 1278 return (SSL3_AD_BAD_RECORD_MAC); 1279 case SSL_AD_DECRYPTION_FAILED: 1280 return (TLS1_AD_DECRYPTION_FAILED); 1281 case SSL_AD_RECORD_OVERFLOW: 1282 return (TLS1_AD_RECORD_OVERFLOW); 1283 case SSL_AD_DECOMPRESSION_FAILURE: 1284 return (SSL3_AD_DECOMPRESSION_FAILURE); 1285 case SSL_AD_HANDSHAKE_FAILURE: 1286 return (SSL3_AD_HANDSHAKE_FAILURE); 1287 case SSL_AD_NO_CERTIFICATE: 1288 return (-1); 1289 case SSL_AD_BAD_CERTIFICATE: 1290 return (SSL3_AD_BAD_CERTIFICATE); 1291 case SSL_AD_UNSUPPORTED_CERTIFICATE: 1292 return (SSL3_AD_UNSUPPORTED_CERTIFICATE); 1293 case SSL_AD_CERTIFICATE_REVOKED: 1294 return (SSL3_AD_CERTIFICATE_REVOKED); 1295 case SSL_AD_CERTIFICATE_EXPIRED: 1296 return (SSL3_AD_CERTIFICATE_EXPIRED); 1297 case SSL_AD_CERTIFICATE_UNKNOWN: 1298 return (SSL3_AD_CERTIFICATE_UNKNOWN); 1299 case SSL_AD_ILLEGAL_PARAMETER: 1300 return (SSL3_AD_ILLEGAL_PARAMETER); 1301 case SSL_AD_UNKNOWN_CA: 1302 return (TLS1_AD_UNKNOWN_CA); 1303 case SSL_AD_ACCESS_DENIED: 1304 return (TLS1_AD_ACCESS_DENIED); 1305 case SSL_AD_DECODE_ERROR: 1306 return (TLS1_AD_DECODE_ERROR); 1307 case SSL_AD_DECRYPT_ERROR: 1308 return (TLS1_AD_DECRYPT_ERROR); 1309 case SSL_AD_EXPORT_RESTRICTION: 1310 return (TLS1_AD_EXPORT_RESTRICTION); 1311 case SSL_AD_PROTOCOL_VERSION: 1312 return (TLS1_AD_PROTOCOL_VERSION); 1313 case SSL_AD_INSUFFICIENT_SECURITY: 1314 return (TLS1_AD_INSUFFICIENT_SECURITY); 1315 case SSL_AD_INTERNAL_ERROR: 1316 return (TLS1_AD_INTERNAL_ERROR); 1317 case SSL_AD_USER_CANCELLED: 1318 return (TLS1_AD_USER_CANCELLED); 1319 case SSL_AD_NO_RENEGOTIATION: 1320 return (TLS1_AD_NO_RENEGOTIATION); 1321 case SSL_AD_UNSUPPORTED_EXTENSION: 1322 return (TLS1_AD_UNSUPPORTED_EXTENSION); 1323 case SSL_AD_CERTIFICATE_UNOBTAINABLE: 1324 return (TLS1_AD_CERTIFICATE_UNOBTAINABLE); 1325 case SSL_AD_UNRECOGNIZED_NAME: 1326 return (TLS1_AD_UNRECOGNIZED_NAME); 1327 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: 1328 return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE); 1329 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: 1330 return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE); 1331 case SSL_AD_UNKNOWN_PSK_IDENTITY: 1332 return (TLS1_AD_UNKNOWN_PSK_IDENTITY); 1333 case SSL_AD_INAPPROPRIATE_FALLBACK: 1334 return (TLS1_AD_INAPPROPRIATE_FALLBACK); 1335#if 0 1336 /* not appropriate for TLS, not used for DTLS */ 1337 case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: 1338 return (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); 1339#endif 1340 default: 1341 return (-1); 1342 } 1343} 1344