155714Skris/* ssl/t1_enc.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 58100928Snectar/* ==================================================================== 59238405Sjkim * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 60100928Snectar * 61100928Snectar * Redistribution and use in source and binary forms, with or without 62100928Snectar * modification, are permitted provided that the following conditions 63100928Snectar * are met: 64100928Snectar * 65100928Snectar * 1. Redistributions of source code must retain the above copyright 66296341Sdelphij * notice, this list of conditions and the following disclaimer. 67100928Snectar * 68100928Snectar * 2. Redistributions in binary form must reproduce the above copyright 69100928Snectar * notice, this list of conditions and the following disclaimer in 70100928Snectar * the documentation and/or other materials provided with the 71100928Snectar * distribution. 72100928Snectar * 73100928Snectar * 3. All advertising materials mentioning features or use of this 74100928Snectar * software must display the following acknowledgment: 75100928Snectar * "This product includes software developed by the OpenSSL Project 76100928Snectar * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77100928Snectar * 78100928Snectar * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79100928Snectar * endorse or promote products derived from this software without 80100928Snectar * prior written permission. For written permission, please contact 81100928Snectar * openssl-core@openssl.org. 82100928Snectar * 83100928Snectar * 5. Products derived from this software may not be called "OpenSSL" 84100928Snectar * nor may "OpenSSL" appear in their names without prior written 85100928Snectar * permission of the OpenSSL Project. 86100928Snectar * 87100928Snectar * 6. Redistributions of any form whatsoever must retain the following 88100928Snectar * acknowledgment: 89100928Snectar * "This product includes software developed by the OpenSSL Project 90100928Snectar * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91100928Snectar * 92100928Snectar * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93100928Snectar * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94100928Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95100928Snectar * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96100928Snectar * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97100928Snectar * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98100928Snectar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99100928Snectar * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100100928Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101100928Snectar * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102100928Snectar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103100928Snectar * OF THE POSSIBILITY OF SUCH DAMAGE. 104100928Snectar * ==================================================================== 105100928Snectar * 106100928Snectar * This product includes cryptographic software written by Eric Young 107100928Snectar * (eay@cryptsoft.com). This product includes software written by Tim 108100928Snectar * Hudson (tjh@cryptsoft.com). 109100928Snectar * 110100928Snectar */ 111238405Sjkim/* ==================================================================== 112238405Sjkim * Copyright 2005 Nokia. All rights reserved. 113238405Sjkim * 114238405Sjkim * The portions of the attached software ("Contribution") is developed by 115238405Sjkim * Nokia Corporation and is licensed pursuant to the OpenSSL open source 116238405Sjkim * license. 117238405Sjkim * 118238405Sjkim * The Contribution, originally written by Mika Kousa and Pasi Eronen of 119238405Sjkim * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 120238405Sjkim * support (see RFC 4279) to OpenSSL. 121238405Sjkim * 122238405Sjkim * No patent licenses or other rights except those expressly stated in 123238405Sjkim * the OpenSSL open source license shall be deemed granted or received 124238405Sjkim * expressly, by implication, estoppel, or otherwise. 125238405Sjkim * 126238405Sjkim * No assurances are provided by Nokia that the Contribution does not 127238405Sjkim * infringe the patent or other intellectual property rights of any third 128238405Sjkim * party or that the license provides you with all the necessary rights 129238405Sjkim * to make use of the Contribution. 130238405Sjkim * 131238405Sjkim * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 132238405Sjkim * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 133238405Sjkim * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 134238405Sjkim * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 135238405Sjkim * OTHERWISE. 136238405Sjkim */ 13755714Skris 13855714Skris#include <stdio.h> 139109998Smarkm#include "ssl_locl.h" 140194206Ssimon#ifndef OPENSSL_NO_COMP 141296341Sdelphij# include <openssl/comp.h> 142194206Ssimon#endif 14355714Skris#include <openssl/evp.h> 14455714Skris#include <openssl/hmac.h> 145109998Smarkm#include <openssl/md5.h> 146238405Sjkim#include <openssl/rand.h> 147194206Ssimon#ifdef KSSL_DEBUG 148296341Sdelphij# include <openssl/des.h> 149194206Ssimon#endif 15055714Skris 151238405Sjkim/* seed1 through seed5 are virtually concatenated */ 152238405Sjkimstatic int tls1_P_hash(const EVP_MD *md, const unsigned char *sec, 153296341Sdelphij int sec_len, 154296341Sdelphij const void *seed1, int seed1_len, 155296341Sdelphij const void *seed2, int seed2_len, 156296341Sdelphij const void *seed3, int seed3_len, 157296341Sdelphij const void *seed4, int seed4_len, 158296341Sdelphij const void *seed5, int seed5_len, 159296341Sdelphij unsigned char *out, int olen) 160296341Sdelphij{ 161296341Sdelphij int chunk; 162296341Sdelphij size_t j; 163296341Sdelphij EVP_MD_CTX ctx, ctx_tmp; 164296341Sdelphij EVP_PKEY *mac_key; 165296341Sdelphij unsigned char A1[EVP_MAX_MD_SIZE]; 166296341Sdelphij size_t A1_len; 167296341Sdelphij int ret = 0; 16855714Skris 169296341Sdelphij chunk = EVP_MD_size(md); 170296341Sdelphij OPENSSL_assert(chunk >= 0); 17155714Skris 172296341Sdelphij EVP_MD_CTX_init(&ctx); 173296341Sdelphij EVP_MD_CTX_init(&ctx_tmp); 174296341Sdelphij EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 175296341Sdelphij EVP_MD_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 176296341Sdelphij mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len); 177296341Sdelphij if (!mac_key) 178296341Sdelphij goto err; 179296341Sdelphij if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key)) 180296341Sdelphij goto err; 181296341Sdelphij if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key)) 182296341Sdelphij goto err; 183296341Sdelphij if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len)) 184296341Sdelphij goto err; 185296341Sdelphij if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len)) 186296341Sdelphij goto err; 187296341Sdelphij if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len)) 188296341Sdelphij goto err; 189296341Sdelphij if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len)) 190296341Sdelphij goto err; 191296341Sdelphij if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len)) 192296341Sdelphij goto err; 193296341Sdelphij if (!EVP_DigestSignFinal(&ctx, A1, &A1_len)) 194296341Sdelphij goto err; 19555714Skris 196296341Sdelphij for (;;) { 197296341Sdelphij /* Reinit mac contexts */ 198296341Sdelphij if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key)) 199296341Sdelphij goto err; 200296341Sdelphij if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key)) 201296341Sdelphij goto err; 202296341Sdelphij if (!EVP_DigestSignUpdate(&ctx, A1, A1_len)) 203296341Sdelphij goto err; 204296341Sdelphij if (!EVP_DigestSignUpdate(&ctx_tmp, A1, A1_len)) 205296341Sdelphij goto err; 206296341Sdelphij if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len)) 207296341Sdelphij goto err; 208296341Sdelphij if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len)) 209296341Sdelphij goto err; 210296341Sdelphij if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len)) 211296341Sdelphij goto err; 212296341Sdelphij if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len)) 213296341Sdelphij goto err; 214296341Sdelphij if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len)) 215296341Sdelphij goto err; 21655714Skris 217296341Sdelphij if (olen > chunk) { 218296341Sdelphij if (!EVP_DigestSignFinal(&ctx, out, &j)) 219296341Sdelphij goto err; 220296341Sdelphij out += j; 221296341Sdelphij olen -= j; 222296341Sdelphij /* calc the next A1 value */ 223296341Sdelphij if (!EVP_DigestSignFinal(&ctx_tmp, A1, &A1_len)) 224296341Sdelphij goto err; 225296341Sdelphij } else { /* last one */ 226296341Sdelphij 227296341Sdelphij if (!EVP_DigestSignFinal(&ctx, A1, &A1_len)) 228296341Sdelphij goto err; 229296341Sdelphij memcpy(out, A1, olen); 230296341Sdelphij break; 231296341Sdelphij } 232296341Sdelphij } 233296341Sdelphij ret = 1; 234296341Sdelphij err: 235296341Sdelphij EVP_PKEY_free(mac_key); 236296341Sdelphij EVP_MD_CTX_cleanup(&ctx); 237296341Sdelphij EVP_MD_CTX_cleanup(&ctx_tmp); 238296341Sdelphij OPENSSL_cleanse(A1, sizeof(A1)); 239296341Sdelphij return ret; 240296341Sdelphij} 241296341Sdelphij 242238405Sjkim/* seed1 through seed5 are virtually concatenated */ 243238405Sjkimstatic int tls1_PRF(long digest_mask, 244296341Sdelphij const void *seed1, int seed1_len, 245296341Sdelphij const void *seed2, int seed2_len, 246296341Sdelphij const void *seed3, int seed3_len, 247296341Sdelphij const void *seed4, int seed4_len, 248296341Sdelphij const void *seed5, int seed5_len, 249296341Sdelphij const unsigned char *sec, int slen, 250296341Sdelphij unsigned char *out1, unsigned char *out2, int olen) 251296341Sdelphij{ 252296341Sdelphij int len, i, idx, count; 253296341Sdelphij const unsigned char *S1; 254296341Sdelphij long m; 255296341Sdelphij const EVP_MD *md; 256296341Sdelphij int ret = 0; 25755714Skris 258296341Sdelphij /* Count number of digests and partition sec evenly */ 259296341Sdelphij count = 0; 260296341Sdelphij for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) { 261296341Sdelphij if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) 262296341Sdelphij count++; 263296341Sdelphij } 264296341Sdelphij if (!count) { 265296341Sdelphij /* Should never happen */ 266296341Sdelphij SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR); 267296341Sdelphij goto err; 268296341Sdelphij } 269296341Sdelphij len = slen / count; 270296341Sdelphij if (count == 1) 271296341Sdelphij slen = 0; 272296341Sdelphij S1 = sec; 273296341Sdelphij memset(out1, 0, olen); 274296341Sdelphij for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) { 275296341Sdelphij if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) { 276296341Sdelphij if (!md) { 277296341Sdelphij SSLerr(SSL_F_TLS1_PRF, SSL_R_UNSUPPORTED_DIGEST_TYPE); 278296341Sdelphij goto err; 279296341Sdelphij } 280296341Sdelphij if (!tls1_P_hash(md, S1, len + (slen & 1), 281296341Sdelphij seed1, seed1_len, seed2, seed2_len, seed3, 282296341Sdelphij seed3_len, seed4, seed4_len, seed5, seed5_len, 283296341Sdelphij out2, olen)) 284296341Sdelphij goto err; 285296341Sdelphij S1 += len; 286296341Sdelphij for (i = 0; i < olen; i++) { 287296341Sdelphij out1[i] ^= out2[i]; 288296341Sdelphij } 289296341Sdelphij } 290296341Sdelphij } 291296341Sdelphij ret = 1; 292296341Sdelphij err: 293296341Sdelphij return ret; 294238405Sjkim} 295296341Sdelphij 296238405Sjkimstatic int tls1_generate_key_block(SSL *s, unsigned char *km, 297296341Sdelphij unsigned char *tmp, int num) 298296341Sdelphij{ 299296341Sdelphij int ret; 300296341Sdelphij ret = tls1_PRF(ssl_get_algorithm2(s), 301296341Sdelphij TLS_MD_KEY_EXPANSION_CONST, 302296341Sdelphij TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random, 303296341Sdelphij SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE, 304296341Sdelphij NULL, 0, NULL, 0, s->session->master_key, 305296341Sdelphij s->session->master_key_length, km, tmp, num); 306109998Smarkm#ifdef KSSL_DEBUG 307296341Sdelphij fprintf(stderr, "tls1_generate_key_block() ==> %d byte master_key =\n\t", 308296341Sdelphij s->session->master_key_length); 309296341Sdelphij { 310109998Smarkm int i; 311296341Sdelphij for (i = 0; i < s->session->master_key_length; i++) { 312296341Sdelphij fprintf(stderr, "%02X", s->session->master_key[i]); 313296341Sdelphij } 314296341Sdelphij fprintf(stderr, "\n"); 315296341Sdelphij } 316296341Sdelphij#endif /* KSSL_DEBUG */ 317296341Sdelphij return ret; 318296341Sdelphij} 31955714Skris 32055714Skrisint tls1_change_cipher_state(SSL *s, int which) 321296341Sdelphij{ 322296341Sdelphij static const unsigned char empty[] = ""; 323296341Sdelphij unsigned char *p, *mac_secret; 324296341Sdelphij unsigned char *exp_label; 325296341Sdelphij unsigned char tmp1[EVP_MAX_KEY_LENGTH]; 326296341Sdelphij unsigned char tmp2[EVP_MAX_KEY_LENGTH]; 327296341Sdelphij unsigned char iv1[EVP_MAX_IV_LENGTH * 2]; 328296341Sdelphij unsigned char iv2[EVP_MAX_IV_LENGTH * 2]; 329296341Sdelphij unsigned char *ms, *key, *iv; 330296341Sdelphij int client_write; 331296341Sdelphij EVP_CIPHER_CTX *dd; 332296341Sdelphij const EVP_CIPHER *c; 333160814Ssimon#ifndef OPENSSL_NO_COMP 334296341Sdelphij const SSL_COMP *comp; 335160814Ssimon#endif 336296341Sdelphij const EVP_MD *m; 337296341Sdelphij int mac_type; 338296341Sdelphij int *mac_secret_size; 339296341Sdelphij EVP_MD_CTX *mac_ctx; 340296341Sdelphij EVP_PKEY *mac_key; 341296341Sdelphij int is_export, n, i, j, k, exp_label_len, cl; 342296341Sdelphij int reuse_dd = 0; 34355714Skris 344296341Sdelphij is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); 345296341Sdelphij c = s->s3->tmp.new_sym_enc; 346296341Sdelphij m = s->s3->tmp.new_hash; 347296341Sdelphij mac_type = s->s3->tmp.new_mac_pkey_type; 348160814Ssimon#ifndef OPENSSL_NO_COMP 349296341Sdelphij comp = s->s3->tmp.new_compression; 350160814Ssimon#endif 351215697Ssimon 352215697Ssimon#ifdef KSSL_DEBUG 353296341Sdelphij fprintf(stderr, "tls1_change_cipher_state(which= %d) w/\n", which); 354296341Sdelphij fprintf(stderr, "\talg= %ld/%ld, comp= %p\n", 355296341Sdelphij s->s3->tmp.new_cipher->algorithm_mkey, 356296341Sdelphij s->s3->tmp.new_cipher->algorithm_auth, comp); 357296341Sdelphij fprintf(stderr, "\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c); 358296341Sdelphij fprintf(stderr, "\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n", 359296341Sdelphij c->nid, c->block_size, c->key_len, c->iv_len); 360296341Sdelphij fprintf(stderr, "\tkey_block: len= %d, data= ", 361296341Sdelphij s->s3->tmp.key_block_length); 362296341Sdelphij { 363238405Sjkim int i; 364296341Sdelphij for (i = 0; i < s->s3->tmp.key_block_length; i++) 365296341Sdelphij fprintf(stderr, "%02x", s->s3->tmp.key_block[i]); 366296341Sdelphij fprintf(stderr, "\n"); 367296341Sdelphij } 368296341Sdelphij#endif /* KSSL_DEBUG */ 369109998Smarkm 370296341Sdelphij if (which & SSL3_CC_READ) { 371296341Sdelphij if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) 372296341Sdelphij s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM; 373296341Sdelphij else 374296341Sdelphij s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM; 375238405Sjkim 376296341Sdelphij if (s->enc_read_ctx != NULL) 377296341Sdelphij reuse_dd = 1; 378296341Sdelphij else if ((s->enc_read_ctx = 379296341Sdelphij OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL) 380296341Sdelphij goto err; 381296341Sdelphij else 382296341Sdelphij /* 383296341Sdelphij * make sure it's intialized in case we exit later with an error 384296341Sdelphij */ 385296341Sdelphij EVP_CIPHER_CTX_init(s->enc_read_ctx); 386296341Sdelphij dd = s->enc_read_ctx; 387296341Sdelphij mac_ctx = ssl_replace_hash(&s->read_hash, NULL); 388160814Ssimon#ifndef OPENSSL_NO_COMP 389296341Sdelphij if (s->expand != NULL) { 390296341Sdelphij COMP_CTX_free(s->expand); 391296341Sdelphij s->expand = NULL; 392296341Sdelphij } 393296341Sdelphij if (comp != NULL) { 394296341Sdelphij s->expand = COMP_CTX_new(comp->method); 395296341Sdelphij if (s->expand == NULL) { 396296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, 397296341Sdelphij SSL_R_COMPRESSION_LIBRARY_ERROR); 398296341Sdelphij goto err2; 399296341Sdelphij } 400296341Sdelphij if (s->s3->rrec.comp == NULL) 401296341Sdelphij s->s3->rrec.comp = (unsigned char *) 402296341Sdelphij OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH); 403296341Sdelphij if (s->s3->rrec.comp == NULL) 404296341Sdelphij goto err; 405296341Sdelphij } 406160814Ssimon#endif 407296341Sdelphij /* 408296341Sdelphij * this is done by dtls1_reset_seq_numbers for DTLS1_VERSION 409296341Sdelphij */ 410296341Sdelphij if (s->version != DTLS1_VERSION) 411296341Sdelphij memset(&(s->s3->read_sequence[0]), 0, 8); 412296341Sdelphij mac_secret = &(s->s3->read_mac_secret[0]); 413296341Sdelphij mac_secret_size = &(s->s3->read_mac_secret_size); 414296341Sdelphij } else { 415296341Sdelphij if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC) 416296341Sdelphij s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM; 417296341Sdelphij else 418296341Sdelphij s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM; 419296341Sdelphij if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) 420296341Sdelphij reuse_dd = 1; 421296341Sdelphij else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) 422296341Sdelphij goto err; 423296341Sdelphij dd = s->enc_write_ctx; 424296341Sdelphij if (SSL_IS_DTLS(s)) { 425296341Sdelphij mac_ctx = EVP_MD_CTX_create(); 426296341Sdelphij if (!mac_ctx) 427296341Sdelphij goto err; 428296341Sdelphij s->write_hash = mac_ctx; 429296341Sdelphij } else 430296341Sdelphij mac_ctx = ssl_replace_hash(&s->write_hash, NULL); 431160814Ssimon#ifndef OPENSSL_NO_COMP 432296341Sdelphij if (s->compress != NULL) { 433296341Sdelphij COMP_CTX_free(s->compress); 434296341Sdelphij s->compress = NULL; 435296341Sdelphij } 436296341Sdelphij if (comp != NULL) { 437296341Sdelphij s->compress = COMP_CTX_new(comp->method); 438296341Sdelphij if (s->compress == NULL) { 439296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, 440296341Sdelphij SSL_R_COMPRESSION_LIBRARY_ERROR); 441296341Sdelphij goto err2; 442296341Sdelphij } 443296341Sdelphij } 444160814Ssimon#endif 445296341Sdelphij /* 446296341Sdelphij * this is done by dtls1_reset_seq_numbers for DTLS1_VERSION 447296341Sdelphij */ 448296341Sdelphij if (s->version != DTLS1_VERSION) 449296341Sdelphij memset(&(s->s3->write_sequence[0]), 0, 8); 450296341Sdelphij mac_secret = &(s->s3->write_mac_secret[0]); 451296341Sdelphij mac_secret_size = &(s->s3->write_mac_secret_size); 452296341Sdelphij } 45355714Skris 454296341Sdelphij if (reuse_dd) 455296341Sdelphij EVP_CIPHER_CTX_cleanup(dd); 45655714Skris 457296341Sdelphij p = s->s3->tmp.key_block; 458296341Sdelphij i = *mac_secret_size = s->s3->tmp.new_mac_secret_size; 459238405Sjkim 460296341Sdelphij cl = EVP_CIPHER_key_length(c); 461296341Sdelphij j = is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ? 462296341Sdelphij cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl; 463296341Sdelphij /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */ 464296341Sdelphij /* If GCM mode only part of IV comes from PRF */ 465296341Sdelphij if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) 466296341Sdelphij k = EVP_GCM_TLS_FIXED_IV_LEN; 467296341Sdelphij else 468296341Sdelphij k = EVP_CIPHER_iv_length(c); 469296341Sdelphij if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) || 470296341Sdelphij (which == SSL3_CHANGE_CIPHER_SERVER_READ)) { 471296341Sdelphij ms = &(p[0]); 472296341Sdelphij n = i + i; 473296341Sdelphij key = &(p[n]); 474296341Sdelphij n += j + j; 475296341Sdelphij iv = &(p[n]); 476296341Sdelphij n += k + k; 477296341Sdelphij exp_label = (unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST; 478296341Sdelphij exp_label_len = TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE; 479296341Sdelphij client_write = 1; 480296341Sdelphij } else { 481296341Sdelphij n = i; 482296341Sdelphij ms = &(p[n]); 483296341Sdelphij n += i + j; 484296341Sdelphij key = &(p[n]); 485296341Sdelphij n += j + k; 486296341Sdelphij iv = &(p[n]); 487296341Sdelphij n += k; 488296341Sdelphij exp_label = (unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST; 489296341Sdelphij exp_label_len = TLS_MD_SERVER_WRITE_KEY_CONST_SIZE; 490296341Sdelphij client_write = 0; 491296341Sdelphij } 49255714Skris 493296341Sdelphij if (n > s->s3->tmp.key_block_length) { 494296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 495296341Sdelphij goto err2; 496296341Sdelphij } 49755714Skris 498296341Sdelphij memcpy(mac_secret, ms, i); 499238405Sjkim 500296341Sdelphij if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) { 501296341Sdelphij mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, 502296341Sdelphij mac_secret, *mac_secret_size); 503296341Sdelphij EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key); 504296341Sdelphij EVP_PKEY_free(mac_key); 505296341Sdelphij } 50655714Skris#ifdef TLS_DEBUG 507296341Sdelphij printf("which = %04X\nmac key=", which); 508296341Sdelphij { 509296341Sdelphij int z; 510296341Sdelphij for (z = 0; z < i; z++) 511296341Sdelphij printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n'); 512296341Sdelphij } 51355714Skris#endif 514296341Sdelphij if (is_export) { 515296341Sdelphij /* 516296341Sdelphij * In here I set both the read and write key/iv to the same value 517296341Sdelphij * since only the correct one will be used :-). 518296341Sdelphij */ 519296341Sdelphij if (!tls1_PRF(ssl_get_algorithm2(s), 520296341Sdelphij exp_label, exp_label_len, 521296341Sdelphij s->s3->client_random, SSL3_RANDOM_SIZE, 522296341Sdelphij s->s3->server_random, SSL3_RANDOM_SIZE, 523296341Sdelphij NULL, 0, NULL, 0, 524296341Sdelphij key, j, tmp1, tmp2, EVP_CIPHER_key_length(c))) 525296341Sdelphij goto err2; 526296341Sdelphij key = tmp1; 52755714Skris 528296341Sdelphij if (k > 0) { 529296341Sdelphij if (!tls1_PRF(ssl_get_algorithm2(s), 530296341Sdelphij TLS_MD_IV_BLOCK_CONST, TLS_MD_IV_BLOCK_CONST_SIZE, 531296341Sdelphij s->s3->client_random, SSL3_RANDOM_SIZE, 532296341Sdelphij s->s3->server_random, SSL3_RANDOM_SIZE, 533296341Sdelphij NULL, 0, NULL, 0, empty, 0, iv1, iv2, k * 2)) 534296341Sdelphij goto err2; 535296341Sdelphij if (client_write) 536296341Sdelphij iv = iv1; 537296341Sdelphij else 538296341Sdelphij iv = &(iv1[k]); 539296341Sdelphij } 540296341Sdelphij } 54155714Skris 542296341Sdelphij s->session->key_arg_length = 0; 543109998Smarkm#ifdef KSSL_DEBUG 544296341Sdelphij { 545238405Sjkim int i; 546296341Sdelphij fprintf(stderr, "EVP_CipherInit_ex(dd,c,key=,iv=,which)\n"); 547296341Sdelphij fprintf(stderr, "\tkey= "); 548296341Sdelphij for (i = 0; i < c->key_len; i++) 549296341Sdelphij fprintf(stderr, "%02x", key[i]); 550296341Sdelphij fprintf(stderr, "\n"); 551296341Sdelphij fprintf(stderr, "\t iv= "); 552296341Sdelphij for (i = 0; i < c->iv_len; i++) 553296341Sdelphij fprintf(stderr, "%02x", iv[i]); 554296341Sdelphij fprintf(stderr, "\n"); 555296341Sdelphij } 556296341Sdelphij#endif /* KSSL_DEBUG */ 55755714Skris 558296341Sdelphij if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) { 559296341Sdelphij if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE)) 560296341Sdelphij || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, k, iv)) { 561296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 562296341Sdelphij goto err2; 563296341Sdelphij } 564296341Sdelphij } else { 565296341Sdelphij if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) { 566296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 567296341Sdelphij goto err2; 568296341Sdelphij } 569296341Sdelphij } 570296341Sdelphij /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */ 571296341Sdelphij if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size 572296341Sdelphij && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY, 573296341Sdelphij *mac_secret_size, mac_secret)) { 574296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); 575296341Sdelphij goto err2; 576296341Sdelphij } 577238405Sjkim 57855714Skris#ifdef TLS_DEBUG 579296341Sdelphij printf("which = %04X\nkey=", which); 580296341Sdelphij { 581296341Sdelphij int z; 582296341Sdelphij for (z = 0; z < EVP_CIPHER_key_length(c); z++) 583296341Sdelphij printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n'); 584296341Sdelphij } 585296341Sdelphij printf("\niv="); 586296341Sdelphij { 587296341Sdelphij int z; 588296341Sdelphij for (z = 0; z < k; z++) 589296341Sdelphij printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n'); 590296341Sdelphij } 591296341Sdelphij printf("\n"); 59255714Skris#endif 59355714Skris 594296341Sdelphij OPENSSL_cleanse(tmp1, sizeof(tmp1)); 595296341Sdelphij OPENSSL_cleanse(tmp2, sizeof(tmp1)); 596296341Sdelphij OPENSSL_cleanse(iv1, sizeof(iv1)); 597296341Sdelphij OPENSSL_cleanse(iv2, sizeof(iv2)); 598296341Sdelphij return (1); 599296341Sdelphij err: 600296341Sdelphij SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); 601296341Sdelphij err2: 602296341Sdelphij return (0); 603296341Sdelphij} 60455714Skris 60555714Skrisint tls1_setup_key_block(SSL *s) 606296341Sdelphij{ 607296341Sdelphij unsigned char *p1, *p2 = NULL; 608296341Sdelphij const EVP_CIPHER *c; 609296341Sdelphij const EVP_MD *hash; 610296341Sdelphij int num; 611296341Sdelphij SSL_COMP *comp; 612296341Sdelphij int mac_type = NID_undef, mac_secret_size = 0; 613296341Sdelphij int ret = 0; 61455714Skris 615109998Smarkm#ifdef KSSL_DEBUG 616296341Sdelphij fprintf(stderr, "tls1_setup_key_block()\n"); 617296341Sdelphij#endif /* KSSL_DEBUG */ 618109998Smarkm 619296341Sdelphij if (s->s3->tmp.key_block_length != 0) 620296341Sdelphij return (1); 62155714Skris 622296341Sdelphij if (!ssl_cipher_get_evp 623296341Sdelphij (s->session, &c, &hash, &mac_type, &mac_secret_size, &comp)) { 624296341Sdelphij SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 625296341Sdelphij return (0); 626296341Sdelphij } 62755714Skris 628296341Sdelphij s->s3->tmp.new_sym_enc = c; 629296341Sdelphij s->s3->tmp.new_hash = hash; 630296341Sdelphij s->s3->tmp.new_mac_pkey_type = mac_type; 631296341Sdelphij s->s3->tmp.new_mac_secret_size = mac_secret_size; 632296341Sdelphij num = 633296341Sdelphij EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c); 634296341Sdelphij num *= 2; 63555714Skris 636296341Sdelphij ssl3_cleanup_key_block(s); 63755714Skris 638296341Sdelphij if ((p1 = (unsigned char *)OPENSSL_malloc(num)) == NULL) { 639296341Sdelphij SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE); 640296341Sdelphij goto err; 641296341Sdelphij } 64255714Skris 643296341Sdelphij s->s3->tmp.key_block_length = num; 644296341Sdelphij s->s3->tmp.key_block = p1; 64555714Skris 646296341Sdelphij if ((p2 = (unsigned char *)OPENSSL_malloc(num)) == NULL) { 647296341Sdelphij SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE); 648296341Sdelphij OPENSSL_free(p1); 649296341Sdelphij goto err; 650296341Sdelphij } 65155714Skris#ifdef TLS_DEBUG 652296341Sdelphij printf("client random\n"); 653296341Sdelphij { 654296341Sdelphij int z; 655296341Sdelphij for (z = 0; z < SSL3_RANDOM_SIZE; z++) 656296341Sdelphij printf("%02X%c", s->s3->client_random[z], 657296341Sdelphij ((z + 1) % 16) ? ' ' : '\n'); 658296341Sdelphij } 659296341Sdelphij printf("server random\n"); 660296341Sdelphij { 661296341Sdelphij int z; 662296341Sdelphij for (z = 0; z < SSL3_RANDOM_SIZE; z++) 663296341Sdelphij printf("%02X%c", s->s3->server_random[z], 664296341Sdelphij ((z + 1) % 16) ? ' ' : '\n'); 665296341Sdelphij } 666296341Sdelphij printf("pre-master\n"); 667296341Sdelphij { 668296341Sdelphij int z; 669296341Sdelphij for (z = 0; z < s->session->master_key_length; z++) 670296341Sdelphij printf("%02X%c", s->session->master_key[z], 671296341Sdelphij ((z + 1) % 16) ? ' ' : '\n'); 672296341Sdelphij } 67355714Skris#endif 674296341Sdelphij if (!tls1_generate_key_block(s, p1, p2, num)) 675296341Sdelphij goto err; 67655714Skris#ifdef TLS_DEBUG 677296341Sdelphij printf("\nkey block\n"); 678296341Sdelphij { 679296341Sdelphij int z; 680296341Sdelphij for (z = 0; z < num; z++) 681296341Sdelphij printf("%02X%c", p1[z], ((z + 1) % 16) ? ' ' : '\n'); 682296341Sdelphij } 68355714Skris#endif 68455714Skris 685296341Sdelphij if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) 686296341Sdelphij && s->method->version <= TLS1_VERSION) { 687296341Sdelphij /* 688296341Sdelphij * enable vulnerability countermeasure for CBC ciphers with known-IV 689296341Sdelphij * problem (http://www.openssl.org/~bodo/tls-cbc.txt) 690296341Sdelphij */ 691296341Sdelphij s->s3->need_empty_fragments = 1; 692100936Snectar 693296341Sdelphij if (s->session->cipher != NULL) { 694296341Sdelphij if (s->session->cipher->algorithm_enc == SSL_eNULL) 695296341Sdelphij s->s3->need_empty_fragments = 0; 696296341Sdelphij 697109998Smarkm#ifndef OPENSSL_NO_RC4 698296341Sdelphij if (s->session->cipher->algorithm_enc == SSL_RC4) 699296341Sdelphij s->s3->need_empty_fragments = 0; 700100928Snectar#endif 701296341Sdelphij } 702296341Sdelphij } 70355714Skris 704296341Sdelphij ret = 1; 705296341Sdelphij err: 706296341Sdelphij if (p2) { 707296341Sdelphij OPENSSL_cleanse(p2, num); 708296341Sdelphij OPENSSL_free(p2); 709296341Sdelphij } 710296341Sdelphij return (ret); 711296341Sdelphij} 712296341Sdelphij 713296341Sdelphij/*- 714296341Sdelphij * tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively. 715246772Sjkim * 716246772Sjkim * Returns: 717246772Sjkim * 0: (in non-constant time) if the record is publically invalid (i.e. too 718246772Sjkim * short etc). 719246772Sjkim * 1: if the record's padding is valid / the encryption was successful. 720246772Sjkim * -1: if the record's padding/AEAD-authenticator is invalid or, if sending, 721246772Sjkim * an internal error occured. 722246772Sjkim */ 72355714Skrisint tls1_enc(SSL *s, int send) 724296341Sdelphij{ 725296341Sdelphij SSL3_RECORD *rec; 726296341Sdelphij EVP_CIPHER_CTX *ds; 727296341Sdelphij unsigned long l; 728296341Sdelphij int bs, i, j, k, pad = 0, ret, mac_size = 0; 729296341Sdelphij const EVP_CIPHER *enc; 73055714Skris 731296341Sdelphij if (send) { 732296341Sdelphij if (EVP_MD_CTX_md(s->write_hash)) { 733296341Sdelphij int n = EVP_MD_CTX_size(s->write_hash); 734296341Sdelphij OPENSSL_assert(n >= 0); 735296341Sdelphij } 736296341Sdelphij ds = s->enc_write_ctx; 737296341Sdelphij rec = &(s->s3->wrec); 738296341Sdelphij if (s->enc_write_ctx == NULL) 739296341Sdelphij enc = NULL; 740296341Sdelphij else { 741296341Sdelphij int ivlen; 742296341Sdelphij enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 743296341Sdelphij /* For TLSv1.1 and later explicit IV */ 744296341Sdelphij if (s->version >= TLS1_1_VERSION 745296341Sdelphij && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE) 746296341Sdelphij ivlen = EVP_CIPHER_iv_length(enc); 747296341Sdelphij else 748296341Sdelphij ivlen = 0; 749296341Sdelphij if (ivlen > 1) { 750296341Sdelphij if (rec->data != rec->input) 751296341Sdelphij /* 752296341Sdelphij * we can't write into the input stream: Can this ever 753296341Sdelphij * happen?? (steve) 754296341Sdelphij */ 755296341Sdelphij fprintf(stderr, 756296341Sdelphij "%s:%d: rec->data != rec->input\n", 757296341Sdelphij __FILE__, __LINE__); 758296341Sdelphij else if (RAND_bytes(rec->input, ivlen) <= 0) 759296341Sdelphij return -1; 760296341Sdelphij } 761296341Sdelphij } 762296341Sdelphij } else { 763296341Sdelphij if (EVP_MD_CTX_md(s->read_hash)) { 764296341Sdelphij int n = EVP_MD_CTX_size(s->read_hash); 765296341Sdelphij OPENSSL_assert(n >= 0); 766296341Sdelphij } 767296341Sdelphij ds = s->enc_read_ctx; 768296341Sdelphij rec = &(s->s3->rrec); 769296341Sdelphij if (s->enc_read_ctx == NULL) 770296341Sdelphij enc = NULL; 771296341Sdelphij else 772296341Sdelphij enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx); 773296341Sdelphij } 77455714Skris 775109998Smarkm#ifdef KSSL_DEBUG 776296341Sdelphij fprintf(stderr, "tls1_enc(%d)\n", send); 777296341Sdelphij#endif /* KSSL_DEBUG */ 778109998Smarkm 779296341Sdelphij if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) { 780296341Sdelphij memmove(rec->data, rec->input, rec->length); 781296341Sdelphij rec->input = rec->data; 782296341Sdelphij ret = 1; 783296341Sdelphij } else { 784296341Sdelphij l = rec->length; 785296341Sdelphij bs = EVP_CIPHER_block_size(ds->cipher); 78655714Skris 787296341Sdelphij if (EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) { 788296341Sdelphij unsigned char buf[EVP_AEAD_TLS1_AAD_LEN], *seq; 789238405Sjkim 790296341Sdelphij seq = send ? s->s3->write_sequence : s->s3->read_sequence; 791238405Sjkim 792296341Sdelphij if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) { 793296341Sdelphij unsigned char dtlsseq[9], *p = dtlsseq; 794238405Sjkim 795296341Sdelphij s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p); 796296341Sdelphij memcpy(p, &seq[2], 6); 797296341Sdelphij memcpy(buf, dtlsseq, 8); 798296341Sdelphij } else { 799296341Sdelphij memcpy(buf, seq, 8); 800296341Sdelphij for (i = 7; i >= 0; i--) { /* increment */ 801296341Sdelphij ++seq[i]; 802296341Sdelphij if (seq[i] != 0) 803296341Sdelphij break; 804296341Sdelphij } 805296341Sdelphij } 806238405Sjkim 807296341Sdelphij buf[8] = rec->type; 808296341Sdelphij buf[9] = (unsigned char)(s->version >> 8); 809296341Sdelphij buf[10] = (unsigned char)(s->version); 810296341Sdelphij buf[11] = rec->length >> 8; 811296341Sdelphij buf[12] = rec->length & 0xff; 812296341Sdelphij pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD, 813296341Sdelphij EVP_AEAD_TLS1_AAD_LEN, buf); 814296341Sdelphij if (pad <= 0) 815296341Sdelphij return -1; 816296341Sdelphij if (send) { 817296341Sdelphij l += pad; 818296341Sdelphij rec->length += pad; 819296341Sdelphij } 820296341Sdelphij } else if ((bs != 1) && send) { 821296341Sdelphij i = bs - ((int)l % bs); 82255714Skris 823296341Sdelphij /* Add weird padding of upto 256 bytes */ 82455714Skris 825296341Sdelphij /* we need to add 'i' padding bytes of value j */ 826296341Sdelphij j = i - 1; 827296341Sdelphij if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) { 828296341Sdelphij if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) 829296341Sdelphij j++; 830296341Sdelphij } 831296341Sdelphij for (k = (int)l; k < (int)(l + i); k++) 832296341Sdelphij rec->input[k] = j; 833296341Sdelphij l += i; 834296341Sdelphij rec->length += i; 835296341Sdelphij } 836109998Smarkm#ifdef KSSL_DEBUG 837296341Sdelphij { 838296341Sdelphij unsigned long ui; 839296341Sdelphij fprintf(stderr, 840296341Sdelphij "EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", 841296341Sdelphij ds, rec->data, rec->input, l); 842296341Sdelphij fprintf(stderr, 843296341Sdelphij "\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%lu %lu], %d iv_len\n", 844296341Sdelphij ds->buf_len, ds->cipher->key_len, DES_KEY_SZ, 845296341Sdelphij DES_SCHEDULE_SZ, ds->cipher->iv_len); 846296341Sdelphij fprintf(stderr, "\t\tIV: "); 847296341Sdelphij for (i = 0; i < ds->cipher->iv_len; i++) 848296341Sdelphij fprintf(stderr, "%02X", ds->iv[i]); 849296341Sdelphij fprintf(stderr, "\n"); 850296341Sdelphij fprintf(stderr, "\trec->input="); 851296341Sdelphij for (ui = 0; ui < l; ui++) 852296341Sdelphij fprintf(stderr, " %02x", rec->input[ui]); 853296341Sdelphij fprintf(stderr, "\n"); 854296341Sdelphij } 855296341Sdelphij#endif /* KSSL_DEBUG */ 856109998Smarkm 857296341Sdelphij if (!send) { 858296341Sdelphij if (l == 0 || l % bs != 0) 859296341Sdelphij return 0; 860296341Sdelphij } 86155714Skris 862296341Sdelphij i = EVP_Cipher(ds, rec->data, rec->input, l); 863296341Sdelphij if ((EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_CUSTOM_CIPHER) 864296341Sdelphij ? (i < 0) 865296341Sdelphij : (i == 0)) 866296341Sdelphij return -1; /* AEAD can fail to verify MAC */ 867296341Sdelphij if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE && !send) { 868296341Sdelphij rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN; 869296341Sdelphij rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN; 870296341Sdelphij rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN; 871296341Sdelphij } 872109998Smarkm#ifdef KSSL_DEBUG 873296341Sdelphij { 874296341Sdelphij unsigned long i; 875296341Sdelphij fprintf(stderr, "\trec->data="); 876296341Sdelphij for (i = 0; i < l; i++) 877296341Sdelphij fprintf(stderr, " %02x", rec->data[i]); 878296341Sdelphij fprintf(stderr, "\n"); 879296341Sdelphij } 880296341Sdelphij#endif /* KSSL_DEBUG */ 881109998Smarkm 882296341Sdelphij ret = 1; 883296341Sdelphij if (EVP_MD_CTX_md(s->read_hash) != NULL) 884296341Sdelphij mac_size = EVP_MD_CTX_size(s->read_hash); 885296341Sdelphij if ((bs != 1) && !send) 886296341Sdelphij ret = tls1_cbc_remove_padding(s, rec, bs, mac_size); 887296341Sdelphij if (pad && !send) 888296341Sdelphij rec->length -= pad; 889296341Sdelphij } 890296341Sdelphij return ret; 891296341Sdelphij} 892246772Sjkim 893238405Sjkimint tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out) 894296341Sdelphij{ 895296341Sdelphij unsigned int ret; 896296341Sdelphij EVP_MD_CTX ctx, *d = NULL; 897296341Sdelphij int i; 89855714Skris 899296341Sdelphij if (s->s3->handshake_buffer) 900296341Sdelphij if (!ssl3_digest_cached_records(s)) 901296341Sdelphij return 0; 902238405Sjkim 903296341Sdelphij for (i = 0; i < SSL_MAX_DIGEST; i++) { 904296341Sdelphij if (s->s3->handshake_dgst[i] 905296341Sdelphij && EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) { 906296341Sdelphij d = s->s3->handshake_dgst[i]; 907296341Sdelphij break; 908296341Sdelphij } 909296341Sdelphij } 910296341Sdelphij if (!d) { 911296341Sdelphij SSLerr(SSL_F_TLS1_CERT_VERIFY_MAC, SSL_R_NO_REQUIRED_DIGEST); 912296341Sdelphij return 0; 913296341Sdelphij } 914238405Sjkim 915296341Sdelphij EVP_MD_CTX_init(&ctx); 916296341Sdelphij EVP_MD_CTX_copy_ex(&ctx, d); 917296341Sdelphij EVP_DigestFinal_ex(&ctx, out, &ret); 918296341Sdelphij EVP_MD_CTX_cleanup(&ctx); 919296341Sdelphij return ((int)ret); 920296341Sdelphij} 92155714Skris 922238405Sjkimint tls1_final_finish_mac(SSL *s, 923296341Sdelphij const char *str, int slen, unsigned char *out) 924296341Sdelphij{ 925296341Sdelphij unsigned int i; 926296341Sdelphij EVP_MD_CTX ctx; 927296341Sdelphij unsigned char buf[2 * EVP_MAX_MD_SIZE]; 928296341Sdelphij unsigned char *q, buf2[12]; 929296341Sdelphij int idx; 930296341Sdelphij long mask; 931296341Sdelphij int err = 0; 932296341Sdelphij const EVP_MD *md; 93355714Skris 934296341Sdelphij q = buf; 93555714Skris 936296341Sdelphij if (s->s3->handshake_buffer) 937296341Sdelphij if (!ssl3_digest_cached_records(s)) 938296341Sdelphij return 0; 939238405Sjkim 940296341Sdelphij EVP_MD_CTX_init(&ctx); 94155714Skris 942296341Sdelphij for (idx = 0; ssl_get_handshake_digest(idx, &mask, &md); idx++) { 943296341Sdelphij if (mask & ssl_get_algorithm2(s)) { 944296341Sdelphij int hashsize = EVP_MD_size(md); 945296341Sdelphij EVP_MD_CTX *hdgst = s->s3->handshake_dgst[idx]; 946296341Sdelphij if (!hdgst || hashsize < 0 947296341Sdelphij || hashsize > (int)(sizeof buf - (size_t)(q - buf))) { 948296341Sdelphij /* 949296341Sdelphij * internal error: 'buf' is too small for this cipersuite! 950296341Sdelphij */ 951296341Sdelphij err = 1; 952296341Sdelphij } else { 953296341Sdelphij if (!EVP_MD_CTX_copy_ex(&ctx, hdgst) || 954296341Sdelphij !EVP_DigestFinal_ex(&ctx, q, &i) || 955296341Sdelphij (i != (unsigned int)hashsize)) 956296341Sdelphij err = 1; 957296341Sdelphij q += hashsize; 958296341Sdelphij } 959296341Sdelphij } 960296341Sdelphij } 96155714Skris 962296341Sdelphij if (!tls1_PRF(ssl_get_algorithm2(s), 963296341Sdelphij str, slen, buf, (int)(q - buf), NULL, 0, NULL, 0, NULL, 0, 964296341Sdelphij s->session->master_key, s->session->master_key_length, 965296341Sdelphij out, buf2, sizeof buf2)) 966296341Sdelphij err = 1; 967296341Sdelphij EVP_MD_CTX_cleanup(&ctx); 96855714Skris 969296341Sdelphij OPENSSL_cleanse(buf, (int)(q - buf)); 970296341Sdelphij OPENSSL_cleanse(buf2, sizeof(buf2)); 971296341Sdelphij if (err) 972296341Sdelphij return 0; 973296341Sdelphij else 974296341Sdelphij return sizeof buf2; 975296341Sdelphij} 976296341Sdelphij 97755714Skrisint tls1_mac(SSL *ssl, unsigned char *md, int send) 978296341Sdelphij{ 979296341Sdelphij SSL3_RECORD *rec; 980296341Sdelphij unsigned char *seq; 981296341Sdelphij EVP_MD_CTX *hash; 982296341Sdelphij size_t md_size, orig_len; 983296341Sdelphij int i; 984296341Sdelphij EVP_MD_CTX hmac, *mac_ctx; 985296341Sdelphij unsigned char header[13]; 986296341Sdelphij int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) 987296341Sdelphij : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM)); 988296341Sdelphij int t; 98955714Skris 990296341Sdelphij if (send) { 991296341Sdelphij rec = &(ssl->s3->wrec); 992296341Sdelphij seq = &(ssl->s3->write_sequence[0]); 993296341Sdelphij hash = ssl->write_hash; 994296341Sdelphij } else { 995296341Sdelphij rec = &(ssl->s3->rrec); 996296341Sdelphij seq = &(ssl->s3->read_sequence[0]); 997296341Sdelphij hash = ssl->read_hash; 998296341Sdelphij } 99955714Skris 1000296341Sdelphij t = EVP_MD_CTX_size(hash); 1001296341Sdelphij OPENSSL_assert(t >= 0); 1002296341Sdelphij md_size = t; 100355714Skris 1004296341Sdelphij /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */ 1005296341Sdelphij if (stream_mac) { 1006296341Sdelphij mac_ctx = hash; 1007296341Sdelphij } else { 1008296341Sdelphij if (!EVP_MD_CTX_copy(&hmac, hash)) 1009296341Sdelphij return -1; 1010296341Sdelphij mac_ctx = &hmac; 1011296341Sdelphij } 1012194206Ssimon 1013296341Sdelphij if (ssl->version == DTLS1_VERSION || ssl->version == DTLS1_BAD_VER) { 1014296341Sdelphij unsigned char dtlsseq[8], *p = dtlsseq; 1015238405Sjkim 1016296341Sdelphij s2n(send ? ssl->d1->w_epoch : ssl->d1->r_epoch, p); 1017296341Sdelphij memcpy(p, &seq[2], 6); 1018194206Ssimon 1019296341Sdelphij memcpy(header, dtlsseq, 8); 1020296341Sdelphij } else 1021296341Sdelphij memcpy(header, seq, 8); 1022194206Ssimon 1023296341Sdelphij /* 1024296341Sdelphij * kludge: tls1_cbc_remove_padding passes padding length in rec->type 1025296341Sdelphij */ 1026296341Sdelphij orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8); 1027296341Sdelphij rec->type &= 0xff; 1028246772Sjkim 1029296341Sdelphij header[8] = rec->type; 1030296341Sdelphij header[9] = (unsigned char)(ssl->version >> 8); 1031296341Sdelphij header[10] = (unsigned char)(ssl->version); 1032296341Sdelphij header[11] = (rec->length) >> 8; 1033296341Sdelphij header[12] = (rec->length) & 0xff; 1034246772Sjkim 1035296341Sdelphij if (!send && 1036296341Sdelphij EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE && 1037296341Sdelphij ssl3_cbc_record_digest_supported(mac_ctx)) { 1038296341Sdelphij /* 1039296341Sdelphij * This is a CBC-encrypted record. We must avoid leaking any 1040296341Sdelphij * timing-side channel information about how many blocks of data we 1041296341Sdelphij * are hashing because that gives an attacker a timing-oracle. 1042296341Sdelphij */ 1043296341Sdelphij /* Final param == not SSLv3 */ 1044296341Sdelphij ssl3_cbc_digest_record(mac_ctx, 1045296341Sdelphij md, &md_size, 1046296341Sdelphij header, rec->input, 1047296341Sdelphij rec->length + md_size, orig_len, 1048296341Sdelphij ssl->s3->read_mac_secret, 1049296341Sdelphij ssl->s3->read_mac_secret_size, 0); 1050296341Sdelphij } else { 1051296341Sdelphij EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)); 1052296341Sdelphij EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length); 1053296341Sdelphij t = EVP_DigestSignFinal(mac_ctx, md, &md_size); 1054296341Sdelphij OPENSSL_assert(t > 0); 1055246772Sjkim#ifdef OPENSSL_FIPS 1056296341Sdelphij if (!send && FIPS_mode()) 1057296341Sdelphij tls_fips_digest_extra(ssl->enc_read_ctx, 1058296341Sdelphij mac_ctx, rec->input, rec->length, orig_len); 1059246772Sjkim#endif 1060296341Sdelphij } 1061296341Sdelphij 1062296341Sdelphij if (!stream_mac) 1063296341Sdelphij EVP_MD_CTX_cleanup(&hmac); 106455714Skris#ifdef TLS_DEBUG 1065296341Sdelphij fprintf(stderr, "seq="); 1066296341Sdelphij { 1067296341Sdelphij int z; 1068296341Sdelphij for (z = 0; z < 8; z++) 1069296341Sdelphij fprintf(stderr, "%02X ", seq[z]); 1070296341Sdelphij fprintf(stderr, "\n"); 1071296341Sdelphij } 1072296341Sdelphij fprintf(stderr, "rec="); 1073296341Sdelphij { 1074296341Sdelphij unsigned int z; 1075296341Sdelphij for (z = 0; z < rec->length; z++) 1076296341Sdelphij fprintf(stderr, "%02X ", rec->data[z]); 1077296341Sdelphij fprintf(stderr, "\n"); 1078296341Sdelphij } 107955714Skris#endif 108055714Skris 1081296341Sdelphij if (ssl->version != DTLS1_VERSION && ssl->version != DTLS1_BAD_VER) { 1082296341Sdelphij for (i = 7; i >= 0; i--) { 1083296341Sdelphij ++seq[i]; 1084296341Sdelphij if (seq[i] != 0) 1085296341Sdelphij break; 1086296341Sdelphij } 1087296341Sdelphij } 108855714Skris#ifdef TLS_DEBUG 1089296341Sdelphij { 1090296341Sdelphij unsigned int z; 1091296341Sdelphij for (z = 0; z < md_size; z++) 1092296341Sdelphij fprintf(stderr, "%02X ", md[z]); 1093296341Sdelphij fprintf(stderr, "\n"); 1094296341Sdelphij } 109555714Skris#endif 1096296341Sdelphij return (md_size); 1097296341Sdelphij} 109855714Skris 109955714Skrisint tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, 1100296341Sdelphij int len) 1101296341Sdelphij{ 1102296341Sdelphij unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; 1103296341Sdelphij const void *co = NULL, *so = NULL; 1104296341Sdelphij int col = 0, sol = 0; 110555714Skris 1106109998Smarkm#ifdef KSSL_DEBUG 1107296341Sdelphij fprintf(stderr, "tls1_generate_master_secret(%p,%p, %p, %d)\n", s, out, p, 1108296341Sdelphij len); 1109296341Sdelphij#endif /* KSSL_DEBUG */ 1110109998Smarkm 1111238405Sjkim#ifdef TLSEXT_TYPE_opaque_prf_input 1112296341Sdelphij if (s->s3->client_opaque_prf_input != NULL 1113296341Sdelphij && s->s3->server_opaque_prf_input != NULL 1114296341Sdelphij && s->s3->client_opaque_prf_input_len > 0 1115296341Sdelphij && s->s3->client_opaque_prf_input_len == 1116296341Sdelphij s->s3->server_opaque_prf_input_len) { 1117296341Sdelphij co = s->s3->client_opaque_prf_input; 1118296341Sdelphij col = s->s3->server_opaque_prf_input_len; 1119296341Sdelphij so = s->s3->server_opaque_prf_input; 1120296341Sdelphij /* 1121296341Sdelphij * must be same as col (see 1122296341Sdelphij * draft-resc-00.txts-opaque-prf-input-00.txt, section 3.1) 1123296341Sdelphij */ 1124296341Sdelphij sol = s->s3->client_opaque_prf_input_len; 1125296341Sdelphij } 1126238405Sjkim#endif 1127238405Sjkim 1128296341Sdelphij tls1_PRF(ssl_get_algorithm2(s), 1129296341Sdelphij TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE, 1130296341Sdelphij s->s3->client_random, SSL3_RANDOM_SIZE, 1131296341Sdelphij co, col, 1132296341Sdelphij s->s3->server_random, SSL3_RANDOM_SIZE, 1133296341Sdelphij so, sol, p, len, s->session->master_key, buff, sizeof buff); 1134296341Sdelphij OPENSSL_cleanse(buff, sizeof buff); 1135238405Sjkim#ifdef SSL_DEBUG 1136296341Sdelphij fprintf(stderr, "Premaster Secret:\n"); 1137296341Sdelphij BIO_dump_fp(stderr, (char *)p, len); 1138296341Sdelphij fprintf(stderr, "Client Random:\n"); 1139296341Sdelphij BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE); 1140296341Sdelphij fprintf(stderr, "Server Random:\n"); 1141296341Sdelphij BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE); 1142296341Sdelphij fprintf(stderr, "Master Secret:\n"); 1143296341Sdelphij BIO_dump_fp(stderr, (char *)s->session->master_key, 1144296341Sdelphij SSL3_MASTER_SECRET_SIZE); 1145238405Sjkim#endif 1146238405Sjkim 1147109998Smarkm#ifdef KSSL_DEBUG 1148296341Sdelphij fprintf(stderr, "tls1_generate_master_secret() complete\n"); 1149296341Sdelphij#endif /* KSSL_DEBUG */ 1150296341Sdelphij return (SSL3_MASTER_SECRET_SIZE); 1151296341Sdelphij} 115255714Skris 1153238405Sjkimint tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1154296341Sdelphij const char *label, size_t llen, 1155296341Sdelphij const unsigned char *context, 1156296341Sdelphij size_t contextlen, int use_context) 1157296341Sdelphij{ 1158296341Sdelphij unsigned char *buff; 1159296341Sdelphij unsigned char *val = NULL; 1160296341Sdelphij size_t vallen, currentvalpos; 1161296341Sdelphij int rv; 1162238405Sjkim 1163238405Sjkim#ifdef KSSL_DEBUG 1164296341Sdelphij fprintf(stderr, "tls1_export_keying_material(%p,%p,%lu,%s,%lu,%p,%lu)\n", 1165296341Sdelphij s, out, olen, label, llen, context, contextlen); 1166296341Sdelphij#endif /* KSSL_DEBUG */ 1167238405Sjkim 1168296341Sdelphij buff = OPENSSL_malloc(olen); 1169296341Sdelphij if (buff == NULL) 1170296341Sdelphij goto err2; 1171238405Sjkim 1172296341Sdelphij /* 1173296341Sdelphij * construct PRF arguments we construct the PRF argument ourself rather 1174296341Sdelphij * than passing separate values into the TLS PRF to ensure that the 1175296341Sdelphij * concatenation of values does not create a prohibited label. 1176296341Sdelphij */ 1177296341Sdelphij vallen = llen + SSL3_RANDOM_SIZE * 2; 1178296341Sdelphij if (use_context) { 1179296341Sdelphij vallen += 2 + contextlen; 1180296341Sdelphij } 1181238405Sjkim 1182296341Sdelphij val = OPENSSL_malloc(vallen); 1183296341Sdelphij if (val == NULL) 1184296341Sdelphij goto err2; 1185296341Sdelphij currentvalpos = 0; 1186296341Sdelphij memcpy(val + currentvalpos, (unsigned char *)label, llen); 1187296341Sdelphij currentvalpos += llen; 1188296341Sdelphij memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE); 1189296341Sdelphij currentvalpos += SSL3_RANDOM_SIZE; 1190296341Sdelphij memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE); 1191296341Sdelphij currentvalpos += SSL3_RANDOM_SIZE; 1192238405Sjkim 1193296341Sdelphij if (use_context) { 1194296341Sdelphij val[currentvalpos] = (contextlen >> 8) & 0xff; 1195296341Sdelphij currentvalpos++; 1196296341Sdelphij val[currentvalpos] = contextlen & 0xff; 1197296341Sdelphij currentvalpos++; 1198296341Sdelphij if ((contextlen > 0) || (context != NULL)) { 1199296341Sdelphij memcpy(val + currentvalpos, context, contextlen); 1200296341Sdelphij } 1201296341Sdelphij } 1202238405Sjkim 1203296341Sdelphij /* 1204296341Sdelphij * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited 1205296341Sdelphij * label len) = 15, so size of val > max(prohibited label len) = 15 and 1206296341Sdelphij * the comparisons won't have buffer overflow 1207296341Sdelphij */ 1208296341Sdelphij if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST, 1209296341Sdelphij TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0) 1210296341Sdelphij goto err1; 1211296341Sdelphij if (memcmp(val, TLS_MD_SERVER_FINISH_CONST, 1212296341Sdelphij TLS_MD_SERVER_FINISH_CONST_SIZE) == 0) 1213296341Sdelphij goto err1; 1214296341Sdelphij if (memcmp(val, TLS_MD_MASTER_SECRET_CONST, 1215296341Sdelphij TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) 1216296341Sdelphij goto err1; 1217296341Sdelphij if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST, 1218296341Sdelphij TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) 1219296341Sdelphij goto err1; 1220238405Sjkim 1221296341Sdelphij rv = tls1_PRF(ssl_get_algorithm2(s), 1222296341Sdelphij val, vallen, 1223296341Sdelphij NULL, 0, 1224296341Sdelphij NULL, 0, 1225296341Sdelphij NULL, 0, 1226296341Sdelphij NULL, 0, 1227296341Sdelphij s->session->master_key, s->session->master_key_length, 1228296341Sdelphij out, buff, olen); 1229296341Sdelphij OPENSSL_cleanse(val, vallen); 1230296341Sdelphij OPENSSL_cleanse(buff, olen); 1231238405Sjkim 1232238405Sjkim#ifdef KSSL_DEBUG 1233296341Sdelphij fprintf(stderr, "tls1_export_keying_material() complete\n"); 1234296341Sdelphij#endif /* KSSL_DEBUG */ 1235296341Sdelphij goto ret; 1236296341Sdelphij err1: 1237296341Sdelphij SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, 1238296341Sdelphij SSL_R_TLS_ILLEGAL_EXPORTER_LABEL); 1239296341Sdelphij rv = 0; 1240296341Sdelphij goto ret; 1241296341Sdelphij err2: 1242296341Sdelphij SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE); 1243296341Sdelphij rv = 0; 1244296341Sdelphij ret: 1245296341Sdelphij if (buff != NULL) 1246296341Sdelphij OPENSSL_free(buff); 1247296341Sdelphij if (val != NULL) 1248296341Sdelphij OPENSSL_free(val); 1249296341Sdelphij return (rv); 1250296341Sdelphij} 1251238405Sjkim 125255714Skrisint tls1_alert_code(int code) 1253296341Sdelphij{ 1254296341Sdelphij switch (code) { 1255296341Sdelphij case SSL_AD_CLOSE_NOTIFY: 1256296341Sdelphij return (SSL3_AD_CLOSE_NOTIFY); 1257296341Sdelphij case SSL_AD_UNEXPECTED_MESSAGE: 1258296341Sdelphij return (SSL3_AD_UNEXPECTED_MESSAGE); 1259296341Sdelphij case SSL_AD_BAD_RECORD_MAC: 1260296341Sdelphij return (SSL3_AD_BAD_RECORD_MAC); 1261296341Sdelphij case SSL_AD_DECRYPTION_FAILED: 1262296341Sdelphij return (TLS1_AD_DECRYPTION_FAILED); 1263296341Sdelphij case SSL_AD_RECORD_OVERFLOW: 1264296341Sdelphij return (TLS1_AD_RECORD_OVERFLOW); 1265296341Sdelphij case SSL_AD_DECOMPRESSION_FAILURE: 1266296341Sdelphij return (SSL3_AD_DECOMPRESSION_FAILURE); 1267296341Sdelphij case SSL_AD_HANDSHAKE_FAILURE: 1268296341Sdelphij return (SSL3_AD_HANDSHAKE_FAILURE); 1269296341Sdelphij case SSL_AD_NO_CERTIFICATE: 1270296341Sdelphij return (-1); 1271296341Sdelphij case SSL_AD_BAD_CERTIFICATE: 1272296341Sdelphij return (SSL3_AD_BAD_CERTIFICATE); 1273296341Sdelphij case SSL_AD_UNSUPPORTED_CERTIFICATE: 1274296341Sdelphij return (SSL3_AD_UNSUPPORTED_CERTIFICATE); 1275296341Sdelphij case SSL_AD_CERTIFICATE_REVOKED: 1276296341Sdelphij return (SSL3_AD_CERTIFICATE_REVOKED); 1277296341Sdelphij case SSL_AD_CERTIFICATE_EXPIRED: 1278296341Sdelphij return (SSL3_AD_CERTIFICATE_EXPIRED); 1279296341Sdelphij case SSL_AD_CERTIFICATE_UNKNOWN: 1280296341Sdelphij return (SSL3_AD_CERTIFICATE_UNKNOWN); 1281296341Sdelphij case SSL_AD_ILLEGAL_PARAMETER: 1282296341Sdelphij return (SSL3_AD_ILLEGAL_PARAMETER); 1283296341Sdelphij case SSL_AD_UNKNOWN_CA: 1284296341Sdelphij return (TLS1_AD_UNKNOWN_CA); 1285296341Sdelphij case SSL_AD_ACCESS_DENIED: 1286296341Sdelphij return (TLS1_AD_ACCESS_DENIED); 1287296341Sdelphij case SSL_AD_DECODE_ERROR: 1288296341Sdelphij return (TLS1_AD_DECODE_ERROR); 1289296341Sdelphij case SSL_AD_DECRYPT_ERROR: 1290296341Sdelphij return (TLS1_AD_DECRYPT_ERROR); 1291296341Sdelphij case SSL_AD_EXPORT_RESTRICTION: 1292296341Sdelphij return (TLS1_AD_EXPORT_RESTRICTION); 1293296341Sdelphij case SSL_AD_PROTOCOL_VERSION: 1294296341Sdelphij return (TLS1_AD_PROTOCOL_VERSION); 1295296341Sdelphij case SSL_AD_INSUFFICIENT_SECURITY: 1296296341Sdelphij return (TLS1_AD_INSUFFICIENT_SECURITY); 1297296341Sdelphij case SSL_AD_INTERNAL_ERROR: 1298296341Sdelphij return (TLS1_AD_INTERNAL_ERROR); 1299296341Sdelphij case SSL_AD_USER_CANCELLED: 1300296341Sdelphij return (TLS1_AD_USER_CANCELLED); 1301296341Sdelphij case SSL_AD_NO_RENEGOTIATION: 1302296341Sdelphij return (TLS1_AD_NO_RENEGOTIATION); 1303296341Sdelphij case SSL_AD_UNSUPPORTED_EXTENSION: 1304296341Sdelphij return (TLS1_AD_UNSUPPORTED_EXTENSION); 1305296341Sdelphij case SSL_AD_CERTIFICATE_UNOBTAINABLE: 1306296341Sdelphij return (TLS1_AD_CERTIFICATE_UNOBTAINABLE); 1307296341Sdelphij case SSL_AD_UNRECOGNIZED_NAME: 1308296341Sdelphij return (TLS1_AD_UNRECOGNIZED_NAME); 1309296341Sdelphij case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: 1310296341Sdelphij return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE); 1311296341Sdelphij case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: 1312296341Sdelphij return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE); 1313296341Sdelphij case SSL_AD_UNKNOWN_PSK_IDENTITY: 1314296341Sdelphij return (TLS1_AD_UNKNOWN_PSK_IDENTITY); 1315296341Sdelphij case SSL_AD_INAPPROPRIATE_FALLBACK: 1316296341Sdelphij return (TLS1_AD_INAPPROPRIATE_FALLBACK); 1317296341Sdelphij#if 0 1318296341Sdelphij /* not appropriate for TLS, not used for DTLS */ 1319296341Sdelphij case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: 1320296341Sdelphij return (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); 1321194206Ssimon#endif 1322296341Sdelphij default: 1323296341Sdelphij return (-1); 1324296341Sdelphij } 1325296341Sdelphij} 1326