1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) cryptsoft.com). This product includes software written by Tim 107 * Hudson (tjh (at) 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 <openssl/ssl.h> 137 138 #include <assert.h> 139 #include <stdio.h> 140 #include <string.h> 141 142 #include <openssl/err.h> 143 #include <openssl/evp.h> 144 #include <openssl/hmac.h> 145 #include <openssl/md5.h> 146 #include <openssl/mem.h> 147 #include <openssl/obj.h> 148 #include <openssl/rand.h> 149 150 #include "internal.h" 151 152 153 /* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246, 154 * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and 155 * |secret| as the secret. |seed1| through |seed3| are concatenated to form the 156 * seed parameter. It returns one on success and zero on failure. */ 157 static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md, 158 const uint8_t *secret, size_t secret_len, 159 const uint8_t *seed1, size_t seed1_len, 160 const uint8_t *seed2, size_t seed2_len, 161 const uint8_t *seed3, size_t seed3_len) { 162 HMAC_CTX ctx, ctx_tmp, ctx_init; 163 uint8_t A1[EVP_MAX_MD_SIZE]; 164 unsigned A1_len; 165 int ret = 0; 166 167 size_t chunk = EVP_MD_size(md); 168 169 HMAC_CTX_init(&ctx); 170 HMAC_CTX_init(&ctx_tmp); 171 HMAC_CTX_init(&ctx_init); 172 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) || 173 !HMAC_CTX_copy_ex(&ctx, &ctx_init) || 174 !HMAC_Update(&ctx, seed1, seed1_len) || 175 !HMAC_Update(&ctx, seed2, seed2_len) || 176 !HMAC_Update(&ctx, seed3, seed3_len) || 177 !HMAC_Final(&ctx, A1, &A1_len)) { 178 goto err; 179 } 180 181 for (;;) { 182 unsigned len; 183 uint8_t hmac[EVP_MAX_MD_SIZE]; 184 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) || 185 !HMAC_Update(&ctx, A1, A1_len) || 186 /* Save a copy of |ctx| to compute the next A1 value below. */ 187 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) || 188 !HMAC_Update(&ctx, seed1, seed1_len) || 189 !HMAC_Update(&ctx, seed2, seed2_len) || 190 !HMAC_Update(&ctx, seed3, seed3_len) || 191 !HMAC_Final(&ctx, hmac, &len)) { 192 goto err; 193 } 194 assert(len == chunk); 195 196 /* XOR the result into |out|. */ 197 if (len > out_len) { 198 len = out_len; 199 } 200 unsigned i; 201 for (i = 0; i < len; i++) { 202 out[i] ^= hmac[i]; 203 } 204 out += len; 205 out_len -= len; 206 207 if (out_len == 0) { 208 break; 209 } 210 211 /* Calculate the next A1 value. */ 212 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) { 213 goto err; 214 } 215 } 216 217 ret = 1; 218 219 err: 220 HMAC_CTX_cleanup(&ctx); 221 HMAC_CTX_cleanup(&ctx_tmp); 222 HMAC_CTX_cleanup(&ctx_init); 223 OPENSSL_cleanse(A1, sizeof(A1)); 224 return ret; 225 } 226 227 int tls1_prf(SSL *ssl, uint8_t *out, size_t out_len, const uint8_t *secret, 228 size_t secret_len, const char *label, size_t label_len, 229 const uint8_t *seed1, size_t seed1_len, 230 const uint8_t *seed2, size_t seed2_len) { 231 232 if (out_len == 0) { 233 return 1; 234 } 235 236 memset(out, 0, out_len); 237 238 uint32_t algorithm_prf = ssl_get_algorithm_prf(ssl); 239 if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) { 240 /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and 241 * MD5, MD5 first. */ 242 size_t secret_half = secret_len - (secret_len / 2); 243 if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, 244 (const uint8_t *)label, label_len, seed1, seed1_len, seed2, 245 seed2_len)) { 246 return 0; 247 } 248 249 /* Note that, if |secret_len| is odd, the two halves share a byte. */ 250 secret = secret + (secret_len - secret_half); 251 secret_len = secret_half; 252 } 253 254 if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf), 255 secret, secret_len, (const uint8_t *)label, label_len, 256 seed1, seed1_len, seed2, seed2_len)) { 257 return 0; 258 } 259 260 return 1; 261 } 262 263 static int tls1_generate_key_block(SSL *ssl, uint8_t *out, size_t out_len) { 264 return ssl->enc_method->prf( 265 ssl, out, out_len, ssl->session->master_key, 266 ssl->session->master_key_length, TLS_MD_KEY_EXPANSION_CONST, 267 TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE, 268 ssl->s3->client_random, SSL3_RANDOM_SIZE); 269 } 270 271 int tls1_change_cipher_state(SSL *ssl, int which) { 272 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we 273 * need to update the read cipherspec. Otherwise we have just written one. */ 274 const char is_read = (which & SSL3_CC_READ) != 0; 275 /* use_client_keys is true if we wish to use the keys for the "client write" 276 * direction. This is the case if we're a client sending a ChangeCipherSpec, 277 * or a server reading a client's ChangeCipherSpec. */ 278 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE || 279 which == SSL3_CHANGE_CIPHER_SERVER_READ; 280 const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret; 281 const uint8_t *client_write_key, *server_write_key, *key; 282 const uint8_t *client_write_iv, *server_write_iv, *iv; 283 const EVP_AEAD *aead = ssl->s3->tmp.new_aead; 284 size_t key_len, iv_len, mac_secret_len; 285 const uint8_t *key_data; 286 287 /* Reset sequence number to zero. */ 288 if (is_read) { 289 if (SSL_IS_DTLS(ssl)) { 290 ssl->d1->r_epoch++; 291 memset(&ssl->d1->bitmap, 0, sizeof(ssl->d1->bitmap)); 292 } 293 memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence)); 294 } else { 295 if (SSL_IS_DTLS(ssl)) { 296 ssl->d1->w_epoch++; 297 memcpy(ssl->d1->last_write_sequence, ssl->s3->write_sequence, 298 sizeof(ssl->s3->write_sequence)); 299 } 300 memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence)); 301 } 302 303 mac_secret_len = ssl->s3->tmp.new_mac_secret_len; 304 iv_len = ssl->s3->tmp.new_fixed_iv_len; 305 306 if (aead == NULL) { 307 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 308 return 0; 309 } 310 311 key_len = EVP_AEAD_key_length(aead); 312 if (mac_secret_len > 0) { 313 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher 314 * suites) the key length reported by |EVP_AEAD_key_length| will 315 * include the MAC and IV key bytes. */ 316 if (key_len < mac_secret_len + iv_len) { 317 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 318 return 0; 319 } 320 key_len -= mac_secret_len + iv_len; 321 } 322 323 key_data = ssl->s3->tmp.key_block; 324 client_write_mac_secret = key_data; 325 key_data += mac_secret_len; 326 server_write_mac_secret = key_data; 327 key_data += mac_secret_len; 328 client_write_key = key_data; 329 key_data += key_len; 330 server_write_key = key_data; 331 key_data += key_len; 332 client_write_iv = key_data; 333 key_data += iv_len; 334 server_write_iv = key_data; 335 key_data += iv_len; 336 337 if (use_client_keys) { 338 mac_secret = client_write_mac_secret; 339 key = client_write_key; 340 iv = client_write_iv; 341 } else { 342 mac_secret = server_write_mac_secret; 343 key = server_write_key; 344 iv = server_write_iv; 345 } 346 347 if (key_data - ssl->s3->tmp.key_block != ssl->s3->tmp.key_block_length) { 348 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 349 return 0; 350 } 351 352 if (is_read) { 353 SSL_AEAD_CTX_free(ssl->aead_read_ctx); 354 ssl->aead_read_ctx = SSL_AEAD_CTX_new( 355 evp_aead_open, ssl3_version_from_wire(ssl, ssl->version), 356 ssl->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv, 357 iv_len); 358 return ssl->aead_read_ctx != NULL; 359 } 360 361 SSL_AEAD_CTX_free(ssl->aead_write_ctx); 362 ssl->aead_write_ctx = SSL_AEAD_CTX_new( 363 evp_aead_seal, ssl3_version_from_wire(ssl, ssl->version), 364 ssl->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv, 365 iv_len); 366 return ssl->aead_write_ctx != NULL; 367 } 368 369 int tls1_setup_key_block(SSL *ssl) { 370 uint8_t *p; 371 const EVP_AEAD *aead = NULL; 372 int ret = 0; 373 size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len; 374 size_t key_block_len; 375 376 if (ssl->s3->tmp.key_block_length != 0) { 377 return 1; 378 } 379 380 if (ssl->session->cipher == NULL) { 381 goto cipher_unavailable_err; 382 } 383 384 if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len, 385 ssl->session->cipher, 386 ssl3_version_from_wire(ssl, ssl->version))) { 387 goto cipher_unavailable_err; 388 } 389 key_len = EVP_AEAD_key_length(aead); 390 variable_iv_len = EVP_AEAD_nonce_length(aead); 391 if (mac_secret_len > 0) { 392 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the 393 * key length reported by |EVP_AEAD_key_length| will include the MAC key 394 * bytes and initial implicit IV. */ 395 if (key_len < mac_secret_len + fixed_iv_len) { 396 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 397 return 0; 398 } 399 key_len -= mac_secret_len + fixed_iv_len; 400 } else { 401 /* The nonce is split into a fixed portion and a variable portion. */ 402 if (variable_iv_len < fixed_iv_len) { 403 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 404 return 0; 405 } 406 variable_iv_len -= fixed_iv_len; 407 } 408 409 assert(mac_secret_len < 256); 410 assert(fixed_iv_len < 256); 411 assert(variable_iv_len < 256); 412 413 ssl->s3->tmp.new_aead = aead; 414 ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len; 415 ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len; 416 ssl->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len; 417 418 key_block_len = key_len + mac_secret_len + fixed_iv_len; 419 key_block_len *= 2; 420 421 ssl3_cleanup_key_block(ssl); 422 423 p = (uint8_t *)OPENSSL_malloc(key_block_len); 424 if (p == NULL) { 425 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); 426 goto err; 427 } 428 429 ssl->s3->tmp.key_block_length = key_block_len; 430 ssl->s3->tmp.key_block = p; 431 432 if (!tls1_generate_key_block(ssl, p, key_block_len)) { 433 goto err; 434 } 435 436 ret = 1; 437 438 err: 439 return ret; 440 441 cipher_unavailable_err: 442 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 443 return 0; 444 } 445 446 int tls1_cert_verify_mac(SSL *ssl, int md_nid, uint8_t *out) { 447 const EVP_MD_CTX *ctx_template; 448 if (md_nid == NID_md5) { 449 ctx_template = &ssl->s3->handshake_md5; 450 } else if (md_nid == EVP_MD_CTX_type(&ssl->s3->handshake_hash)) { 451 ctx_template = &ssl->s3->handshake_hash; 452 } else { 453 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST); 454 return 0; 455 } 456 457 EVP_MD_CTX ctx; 458 EVP_MD_CTX_init(&ctx); 459 if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) { 460 EVP_MD_CTX_cleanup(&ctx); 461 return 0; 462 } 463 unsigned ret; 464 EVP_DigestFinal_ex(&ctx, out, &ret); 465 EVP_MD_CTX_cleanup(&ctx); 466 return ret; 467 } 468 469 static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len, 470 size_t max_out) { 471 int ret = 0; 472 EVP_MD_CTX ctx_copy; 473 EVP_MD_CTX_init(&ctx_copy); 474 475 if (EVP_MD_CTX_size(ctx) > max_out) { 476 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); 477 goto err; 478 } 479 unsigned len; 480 if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) || 481 !EVP_DigestFinal_ex(&ctx_copy, out, &len)) { 482 goto err; 483 } 484 assert(len == EVP_MD_CTX_size(ctx)); 485 486 *out_len = len; 487 ret = 1; 488 489 err: 490 EVP_MD_CTX_cleanup(&ctx_copy); 491 return ret; 492 } 493 494 /* tls1_handshake_digest calculates the current handshake hash and writes it to 495 * |out|, which has space for |out_len| bytes. It returns the number of bytes 496 * written or -1 in the event of an error. This function works on a copy of the 497 * underlying digests so can be called multiple times and prior to the final 498 * update etc. */ 499 int tls1_handshake_digest(SSL *ssl, uint8_t *out, size_t out_len) { 500 size_t md5_len = 0; 501 if (EVP_MD_CTX_md(&ssl->s3->handshake_md5) != NULL && 502 !append_digest(&ssl->s3->handshake_md5, out, &md5_len, out_len)) { 503 return -1; 504 } 505 506 size_t len; 507 if (!append_digest(&ssl->s3->handshake_hash, out + md5_len, &len, 508 out_len - md5_len)) { 509 return -1; 510 } 511 512 return (int)(md5_len + len); 513 } 514 515 int tls1_final_finish_mac(SSL *ssl, const char *str, int slen, uint8_t *out) { 516 uint8_t buf[2 * EVP_MAX_MD_SIZE]; 517 int err = 0; 518 int digests_len; 519 520 /* At this point, the handshake should have released the handshake buffer on 521 * its own. */ 522 assert(ssl->s3->handshake_buffer == NULL); 523 524 digests_len = tls1_handshake_digest(ssl, buf, sizeof(buf)); 525 if (digests_len < 0) { 526 err = 1; 527 digests_len = 0; 528 } 529 530 if (!ssl->enc_method->prf(ssl, out, 12, ssl->session->master_key, 531 ssl->session->master_key_length, str, slen, buf, 532 digests_len, NULL, 0)) { 533 err = 1; 534 } 535 536 if (err) { 537 return 0; 538 } else { 539 return 12; 540 } 541 } 542 543 int tls1_generate_master_secret(SSL *ssl, uint8_t *out, 544 const uint8_t *premaster, 545 size_t premaster_len) { 546 if (ssl->s3->tmp.extended_master_secret) { 547 uint8_t digests[2 * EVP_MAX_MD_SIZE]; 548 int digests_len = tls1_handshake_digest(ssl, digests, sizeof(digests)); 549 if (digests_len == -1) { 550 return 0; 551 } 552 553 if (!ssl->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster, 554 premaster_len, 555 TLS_MD_EXTENDED_MASTER_SECRET_CONST, 556 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests, 557 digests_len, NULL, 0)) { 558 return 0; 559 } 560 } else { 561 if (!ssl->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster, 562 premaster_len, TLS_MD_MASTER_SECRET_CONST, 563 TLS_MD_MASTER_SECRET_CONST_SIZE, 564 ssl->s3->client_random, SSL3_RANDOM_SIZE, 565 ssl->s3->server_random, SSL3_RANDOM_SIZE)) { 566 return 0; 567 } 568 } 569 570 return SSL3_MASTER_SECRET_SIZE; 571 } 572 573 int tls1_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len, 574 const char *label, size_t label_len, 575 const uint8_t *context, size_t context_len, 576 int use_context) { 577 if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) { 578 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 579 return 0; 580 } 581 582 size_t seed_len = 2 * SSL3_RANDOM_SIZE; 583 if (use_context) { 584 if (context_len >= 1u << 16) { 585 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); 586 return 0; 587 } 588 seed_len += 2 + context_len; 589 } 590 uint8_t *seed = OPENSSL_malloc(seed_len); 591 if (seed == NULL) { 592 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); 593 return 0; 594 } 595 596 memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE); 597 memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE); 598 if (use_context) { 599 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8); 600 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len; 601 memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len); 602 } 603 604 int ret = ssl->enc_method->prf(ssl, out, out_len, ssl->session->master_key, 605 ssl->session->master_key_length, label, 606 label_len, seed, seed_len, NULL, 0); 607 OPENSSL_free(seed); 608 return ret; 609 } 610 611 int tls1_alert_code(int code) { 612 switch (code) { 613 case SSL_AD_CLOSE_NOTIFY: 614 return SSL3_AD_CLOSE_NOTIFY; 615 616 case SSL_AD_UNEXPECTED_MESSAGE: 617 return SSL3_AD_UNEXPECTED_MESSAGE; 618 619 case SSL_AD_BAD_RECORD_MAC: 620 return SSL3_AD_BAD_RECORD_MAC; 621 622 case SSL_AD_DECRYPTION_FAILED: 623 return TLS1_AD_DECRYPTION_FAILED; 624 625 case SSL_AD_RECORD_OVERFLOW: 626 return TLS1_AD_RECORD_OVERFLOW; 627 628 case SSL_AD_DECOMPRESSION_FAILURE: 629 return SSL3_AD_DECOMPRESSION_FAILURE; 630 631 case SSL_AD_HANDSHAKE_FAILURE: 632 return SSL3_AD_HANDSHAKE_FAILURE; 633 634 case SSL_AD_NO_CERTIFICATE: 635 return -1; 636 637 case SSL_AD_BAD_CERTIFICATE: 638 return SSL3_AD_BAD_CERTIFICATE; 639 640 case SSL_AD_UNSUPPORTED_CERTIFICATE: 641 return SSL3_AD_UNSUPPORTED_CERTIFICATE; 642 643 case SSL_AD_CERTIFICATE_REVOKED: 644 return SSL3_AD_CERTIFICATE_REVOKED; 645 646 case SSL_AD_CERTIFICATE_EXPIRED: 647 return SSL3_AD_CERTIFICATE_EXPIRED; 648 649 case SSL_AD_CERTIFICATE_UNKNOWN: 650 return SSL3_AD_CERTIFICATE_UNKNOWN; 651 652 case SSL_AD_ILLEGAL_PARAMETER: 653 return SSL3_AD_ILLEGAL_PARAMETER; 654 655 case SSL_AD_UNKNOWN_CA: 656 return TLS1_AD_UNKNOWN_CA; 657 658 case SSL_AD_ACCESS_DENIED: 659 return TLS1_AD_ACCESS_DENIED; 660 661 case SSL_AD_DECODE_ERROR: 662 return TLS1_AD_DECODE_ERROR; 663 664 case SSL_AD_DECRYPT_ERROR: 665 return TLS1_AD_DECRYPT_ERROR; 666 case SSL_AD_EXPORT_RESTRICTION: 667 return TLS1_AD_EXPORT_RESTRICTION; 668 669 case SSL_AD_PROTOCOL_VERSION: 670 return TLS1_AD_PROTOCOL_VERSION; 671 672 case SSL_AD_INSUFFICIENT_SECURITY: 673 return TLS1_AD_INSUFFICIENT_SECURITY; 674 675 case SSL_AD_INTERNAL_ERROR: 676 return TLS1_AD_INTERNAL_ERROR; 677 678 case SSL_AD_USER_CANCELLED: 679 return TLS1_AD_USER_CANCELLED; 680 681 case SSL_AD_NO_RENEGOTIATION: 682 return TLS1_AD_NO_RENEGOTIATION; 683 684 case SSL_AD_UNSUPPORTED_EXTENSION: 685 return TLS1_AD_UNSUPPORTED_EXTENSION; 686 687 case SSL_AD_CERTIFICATE_UNOBTAINABLE: 688 return TLS1_AD_CERTIFICATE_UNOBTAINABLE; 689 690 case SSL_AD_UNRECOGNIZED_NAME: 691 return TLS1_AD_UNRECOGNIZED_NAME; 692 693 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: 694 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 695 696 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: 697 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE; 698 699 case SSL_AD_UNKNOWN_PSK_IDENTITY: 700 return TLS1_AD_UNKNOWN_PSK_IDENTITY; 701 702 case SSL_AD_INAPPROPRIATE_FALLBACK: 703 return SSL3_AD_INAPPROPRIATE_FALLBACK; 704 705 default: 706 return -1; 707 } 708 } 709