ssl/s3_srvr.c:2274: error: UNINITIALIZED_VALUE
  The value read from i was never initialized.
Showing all 1 steps of the trace


ssl/s3_srvr.c:2274:12: 
2272.   		if (n)
2273.   			n2s(p,i);
2274. > 		if (n && n != i+2)
2275.   			{
2276.   			if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))

int ssl3_get_client_key_exchange(SSL *s)
	{
	int i,al,ok;
	long n;
	unsigned long alg_k;
	unsigned char *p;
#ifndef OPENSSL_NO_RSA
	RSA *rsa=NULL;
	EVP_PKEY *pkey=NULL;
#endif
#ifndef OPENSSL_NO_DH
	BIGNUM *pub=NULL;
	DH *dh_srvr, *dh_clnt = NULL;
#endif
#ifndef OPENSSL_NO_KRB5
	KSSL_ERR kssl_err;
#endif
#ifndef OPENSSL_NO_ECDH
	EC_KEY *srvr_ecdh = NULL;
	EVP_PKEY *clnt_pub_pkey = NULL;
	EC_POINT *clnt_ecpoint = NULL;
	BN_CTX *bn_ctx = NULL;
#endif
	n=s->method->ssl_get_message(s,
		SSL3_ST_SR_KEY_EXCH_A,
		SSL3_ST_SR_KEY_EXCH_B,
		SSL3_MT_CLIENT_KEY_EXCHANGE,
		2048,
		&ok);
	if (!ok) return((int)n);
	p=(unsigned char *)s->init_msg;
	alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
#ifndef OPENSSL_NO_RSA
	if (alg_k & SSL_kRSA)
		{
		if (s->s3->tmp.use_rsa_tmp)
			{
			if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
				rsa=s->cert->rsa_tmp;
			if (rsa == NULL)
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
				goto f_err;
				}
			}
		else
			{
			pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
			if (	(pkey == NULL) ||
				(pkey->type != EVP_PKEY_RSA) ||
				(pkey->pkey.rsa == NULL))
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
				goto f_err;
				}
			rsa=pkey->pkey.rsa;
			}
		if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)
			{
			n2s(p,i);
			if (n != i+2)
				{
				if (!(s->options & SSL_OP_TLS_D5_BUG))
					{
					SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
					goto err;
					}
				else
					p-=2;
				}
			else
				n=i;
			}
		i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
		al = -1;
		if (i != SSL_MAX_MASTER_KEY_LENGTH)
			{
			al=SSL_AD_DECODE_ERROR;
			}
		if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
			{
			if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
				(p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
				{
				al=SSL_AD_DECODE_ERROR;
				}
			}
		if (al != -1)
			{
			ERR_clear_error();
			i = SSL_MAX_MASTER_KEY_LENGTH;
			p[0] = s->client_version >> 8;
			p[1] = s->client_version & 0xff;
			if (RAND_pseudo_bytes(p+2, i-2) <= 0)
				goto err;
			}
		s->session->master_key_length=
			s->method->ssl3_enc->generate_master_secret(s,
				s->session->master_key,
				p,i);
		OPENSSL_cleanse(p,i);
		}
	else
#endif
#ifndef OPENSSL_NO_DH
		if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
		{
		int idx = -1;
		EVP_PKEY *skey = NULL;
		if (n)
			n2s(p,i);
		if (n && n != i+2)
			{
			if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
				goto err;
				}
			else
				{
				p-=2;
				i=(int)n;
				}
			}
		if (alg_k & SSL_kDHr)
			idx = SSL_PKEY_DH_RSA;
		else if (alg_k & SSL_kDHd)
			idx = SSL_PKEY_DH_DSA;
		if (idx >= 0)
			{
			skey = s->cert->pkeys[idx].privatekey;
			if ((skey == NULL) ||
				(skey->type != EVP_PKEY_DH) ||
				(skey->pkey.dh == NULL))
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
				goto f_err;
				}
			dh_srvr = skey->pkey.dh;
			}
		else if (s->s3->tmp.dh == NULL)
			{
			al=SSL_AD_HANDSHAKE_FAILURE;
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
			goto f_err;
			}
		else
			dh_srvr=s->s3->tmp.dh;
		if (n == 0L)
			{
			EVP_PKEY *clkey=X509_get_pubkey(s->session->peer);
			if (clkey)
				{
				if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
					dh_clnt = EVP_PKEY_get1_DH(clkey);
				}
			if (dh_clnt == NULL)
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
				goto f_err;
				}
			EVP_PKEY_free(clkey);
			pub = dh_clnt->pub_key;
			}
		else
			pub=BN_bin2bn(p,i,NULL);
		if (pub == NULL)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
			goto err;
			}
		i=DH_compute_key(p,pub,dh_srvr);
		if (i <= 0)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
			BN_clear_free(pub);
			goto err;
			}
		DH_free(s->s3->tmp.dh);
		s->s3->tmp.dh=NULL;
		if (dh_clnt)
			DH_free(dh_clnt);
		else
			BN_clear_free(pub);
		pub=NULL;
		s->session->master_key_length=
			s->method->ssl3_enc->generate_master_secret(s,
				s->session->master_key,p,i);
		OPENSSL_cleanse(p,i);
		if (dh_clnt)
			return 2;
		}
	else
#endif
#ifndef OPENSSL_NO_KRB5
	if (alg_k & SSL_kKRB5)
		{
		krb5_error_code		krb5rc;
		krb5_data		enc_ticket;
		krb5_data		authenticator;
		krb5_data		enc_pms;
		KSSL_CTX		*kssl_ctx = s->kssl_ctx;
		EVP_CIPHER_CTX		ciph_ctx;
		const EVP_CIPHER	*enc = NULL;
		unsigned char		iv[EVP_MAX_IV_LENGTH];
		unsigned char		pms[SSL_MAX_MASTER_KEY_LENGTH
					       + EVP_MAX_BLOCK_LENGTH];
		int		     padl, outl;
		krb5_timestamp		authtime = 0;
		krb5_ticket_times	ttimes;
		EVP_CIPHER_CTX_init(&ciph_ctx);
		if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
		n2s(p,i);
		enc_ticket.length = i;
		if (n < (long)(enc_ticket.length + 6))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DATA_LENGTH_TOO_LONG);
			goto err;
			}
		enc_ticket.data = (char *)p;
		p+=enc_ticket.length;
		n2s(p,i);
		authenticator.length = i;
		if (n < (long)(enc_ticket.length + authenticator.length + 6))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DATA_LENGTH_TOO_LONG);
			goto err;
			}
		authenticator.data = (char *)p;
		p+=authenticator.length;
		n2s(p,i);
		enc_pms.length = i;
		enc_pms.data = (char *)p;
		p+=enc_pms.length;
		if(enc_pms.length > sizeof pms)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			       SSL_R_DATA_LENGTH_TOO_LONG);
			goto err;
			}
		if (n != (long)(enc_ticket.length + authenticator.length +
						enc_pms.length + 6))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DATA_LENGTH_TOO_LONG);
			goto err;
			}
		if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
					&kssl_err)) != 0)
			{
#ifdef KSSL_DEBUG
			printf("kssl_sget_tkt rtn %d [%d]\n",
				krb5rc, kssl_err.reason);
			if (kssl_err.text)
				printf("kssl_err text= %s\n", kssl_err.text);
#endif
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				kssl_err.reason);
			goto err;
			}
		if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
					&authtime, &kssl_err)) != 0)
			{
#ifdef KSSL_DEBUG
			printf("kssl_check_authent rtn %d [%d]\n",
				krb5rc, kssl_err.reason);
			if (kssl_err.text)
				printf("kssl_err text= %s\n", kssl_err.text);
#endif
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				kssl_err.reason);
			goto err;
			}
		if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
			goto err;
			}
#ifdef KSSL_DEBUG
		kssl_ctx_show(kssl_ctx);
#endif
		enc = kssl_map_enc(kssl_ctx->enctype);
		if (enc == NULL)
		    goto err;
		memset(iv, 0, sizeof iv);
		if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DECRYPTION_FAILED);
			goto err;
			}
		if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
					(unsigned char *)enc_pms.data, enc_pms.length))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DECRYPTION_FAILED);
			goto err;
			}
		if (outl > SSL_MAX_MASTER_KEY_LENGTH)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DATA_LENGTH_TOO_LONG);
			goto err;
			}
		if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DECRYPTION_FAILED);
			goto err;
			}
		outl += padl;
		if (outl > SSL_MAX_MASTER_KEY_LENGTH)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_DATA_LENGTH_TOO_LONG);
			goto err;
			}
		if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff))))
		    {
		    if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			       SSL_AD_DECODE_ERROR);
			goto err;
			}
		    }
		EVP_CIPHER_CTX_cleanup(&ciph_ctx);
		s->session->master_key_length=
			s->method->ssl3_enc->generate_master_secret(s,
				s->session->master_key, pms, outl);
		if (kssl_ctx->client_princ)
			{
			size_t len = strlen(kssl_ctx->client_princ);
			if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
				{
				s->session->krb5_client_princ_len = len;
				memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
				}
			}
		}
	else
#endif
#ifndef OPENSSL_NO_ECDH
		if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
		{
		int ret = 1;
		int field_size = 0;
		const EC_KEY   *tkey;
		const EC_GROUP *group;
		const BIGNUM *priv_key;
		if ((srvr_ecdh = EC_KEY_new()) == NULL)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			    ERR_R_MALLOC_FAILURE);
			goto err;
			}
		if (alg_k & (SSL_kECDHr|SSL_kECDHe))
			{
			tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
			}
		else
			{
			tkey = s->s3->tmp.ecdh;
			}
		group    = EC_KEY_get0_group(tkey);
		priv_key = EC_KEY_get0_private_key(tkey);
		if (!EC_KEY_set_group(srvr_ecdh, group) ||
		    !EC_KEY_set_private_key(srvr_ecdh, priv_key))
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			       ERR_R_EC_LIB);
			goto err;
			}
		if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			    ERR_R_MALLOC_FAILURE);
			goto err;
			}
		if (n == 0L)
			{
			 if (alg_k & SSL_kEECDH)
				 {
				 al=SSL_AD_HANDSHAKE_FAILURE;
				 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
				 goto f_err;
				 }
			if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
			    == NULL) ||
			    (clnt_pub_pkey->type != EVP_PKEY_EC))
				{
			   	al=SSL_AD_HANDSHAKE_FAILURE;
			   	SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				    SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
			   	goto f_err;
			   	}
			if (EC_POINT_copy(clnt_ecpoint,
			    EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					ERR_R_EC_LIB);
				goto err;
				}
			ret = 2;
			}
		else
			{
			if ((bn_ctx = BN_CTX_new()) == NULL)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				    ERR_R_MALLOC_FAILURE);
				goto err;
				}
			i = *p;
			p += 1;
			if (n != 1 + i)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				    ERR_R_EC_LIB);
				goto err;
				}
			if (EC_POINT_oct2point(group,
			    clnt_ecpoint, p, i, bn_ctx) == 0)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				    ERR_R_EC_LIB);
				goto err;
				}
			p=(unsigned char *)s->init_buf->data;
			}
		field_size = EC_GROUP_get_degree(group);
		if (field_size <= 0)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			       ERR_R_ECDH_LIB);
			goto err;
			}
		i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
		if (i <= 0)
			{
			SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
			    ERR_R_ECDH_LIB);
			goto err;
			}
		EVP_PKEY_free(clnt_pub_pkey);
		EC_POINT_free(clnt_ecpoint);
		EC_KEY_free(srvr_ecdh);
		BN_CTX_free(bn_ctx);
		EC_KEY_free(s->s3->tmp.ecdh);
		s->s3->tmp.ecdh = NULL;
		s->session->master_key_length = s->method->ssl3_enc-> \
		    generate_master_secret(s, s->session->master_key, p, i);
		OPENSSL_cleanse(p, i);
		return (ret);
		}
	else
#endif
#ifndef OPENSSL_NO_PSK
		if (alg_k & SSL_kPSK)
			{
			unsigned char *t = NULL;
			unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
			unsigned int pre_ms_len = 0, psk_len = 0;
			int psk_err = 1;
			char tmp_id[PSK_MAX_IDENTITY_LEN+1];
			al=SSL_AD_HANDSHAKE_FAILURE;
			n2s(p,i);
			if (n != i+2)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					SSL_R_LENGTH_MISMATCH);
				goto psk_err;
				}
			if (i > PSK_MAX_IDENTITY_LEN)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					SSL_R_DATA_LENGTH_TOO_LONG);
				goto psk_err;
				}
			if (s->psk_server_callback == NULL)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				       SSL_R_PSK_NO_SERVER_CB);
				goto psk_err;
				}
			memcpy(tmp_id, p, i);
			memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
			psk_len = s->psk_server_callback(s, tmp_id,
				psk_or_pre_ms, sizeof(psk_or_pre_ms));
			OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
			if (psk_len > PSK_MAX_PSK_LEN)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					ERR_R_INTERNAL_ERROR);
				goto psk_err;
				}
			else if (psk_len == 0)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				       SSL_R_PSK_IDENTITY_NOT_FOUND);
				al=SSL_AD_UNKNOWN_PSK_IDENTITY;
				goto psk_err;
				}
			pre_ms_len=2+psk_len+2+psk_len;
			t = psk_or_pre_ms;
			memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
			s2n(psk_len, t);
			memset(t, 0, psk_len);
			t+=psk_len;
			s2n(psk_len, t);
			if (s->session->psk_identity != NULL)
				OPENSSL_free(s->session->psk_identity);
			s->session->psk_identity = BUF_strdup((char *)p);
			if (s->session->psk_identity == NULL)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					ERR_R_MALLOC_FAILURE);
				goto psk_err;
				}
			if (s->session->psk_identity_hint != NULL)
				OPENSSL_free(s->session->psk_identity_hint);
			s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
			if (s->ctx->psk_identity_hint != NULL &&
				s->session->psk_identity_hint == NULL)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					ERR_R_MALLOC_FAILURE);
				goto psk_err;
				}
			s->session->master_key_length=
				s->method->ssl3_enc->generate_master_secret(s,
					s->session->master_key, psk_or_pre_ms, pre_ms_len);
			psk_err = 0;
		psk_err:
			OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
			if (psk_err != 0)
				goto f_err;
			}
		else
#endif
#ifndef OPENSSL_NO_SRP
		if (alg_k & SSL_kSRP)
			{
			int param_len;
			n2s(p,i);
			param_len=i+2;
			if (param_len > n)
				{
				al=SSL_AD_DECODE_ERROR;
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_SRP_A_LENGTH);
				goto f_err;
				}
			if (!(s->srp_ctx.A=BN_bin2bn(p,i,NULL)))
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_BN_LIB);
				goto err;
				}
			if (s->session->srp_username != NULL)
				OPENSSL_free(s->session->srp_username);
			s->session->srp_username = BUF_strdup(s->srp_ctx.login);
			if (s->session->srp_username == NULL)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
					ERR_R_MALLOC_FAILURE);
				goto err;
				}
			if ((s->session->master_key_length = SRP_generate_server_master_secret(s,s->session->master_key))<0)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
				goto err;
				}
			p+=i;
			}
		else
#endif
		if (alg_k & SSL_kGOST)
			{
			int ret = 0;
			EVP_PKEY_CTX *pkey_ctx;
			EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
			unsigned char premaster_secret[32], *start;
			size_t outlen=32, inlen;
			unsigned long alg_a;
			alg_a = s->s3->tmp.new_cipher->algorithm_auth;
			if (alg_a & SSL_aGOST94)
				pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
			else if (alg_a & SSL_aGOST01)
				pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
			pkey_ctx = EVP_PKEY_CTX_new(pk,NULL);
			EVP_PKEY_decrypt_init(pkey_ctx);
			client_pub_pkey = X509_get_pubkey(s->session->peer);
			if (client_pub_pkey)
				{
				if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
					ERR_clear_error();
				}
			if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED)))
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
				goto gerr;
				}
			if (p[1] == 0x81)
				{
				start = p+3;
				inlen = p[2];
				}
			else if (p[1] < 0x80)
				{
				start = p+2;
				inlen = p[1];
				}
			else
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
				goto gerr;
				}
			if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0)
				{
				SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
				goto gerr;
				}
			s->session->master_key_length=
				s->method->ssl3_enc->generate_master_secret(s,
					s->session->master_key,premaster_secret,32);
			if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
				ret = 2;
			else
				ret = 1;
		gerr:
			EVP_PKEY_free(client_pub_pkey);
			EVP_PKEY_CTX_free(pkey_ctx);
			if (ret)
				return ret;
			else
				goto err;
			}
		else
		{
		al=SSL_AD_HANDSHAKE_FAILURE;
		SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
				SSL_R_UNKNOWN_CIPHER_TYPE);
		goto f_err;
		}
	return(1);
f_err:
	ssl3_send_alert(s,SSL3_AL_FATAL,al);
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
err:
#endif
#ifndef OPENSSL_NO_ECDH
	EVP_PKEY_free(clnt_pub_pkey);
	EC_POINT_free(clnt_ecpoint);
	if (srvr_ecdh != NULL)
		EC_KEY_free(srvr_ecdh);
	BN_CTX_free(bn_ctx);
#endif
	return(-1);
	}
['int ssl3_get_client_key_exchange(SSL *s)\n\t{\n\tint i,al,ok;\n\tlong n;\n\tunsigned long alg_k;\n\tunsigned char *p;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa=NULL;\n\tEVP_PKEY *pkey=NULL;\n#endif\n#ifndef OPENSSL_NO_DH\n\tBIGNUM *pub=NULL;\n\tDH *dh_srvr, *dh_clnt = NULL;\n#endif\n#ifndef OPENSSL_NO_KRB5\n\tKSSL_ERR kssl_err;\n#endif\n#ifndef OPENSSL_NO_ECDH\n\tEC_KEY *srvr_ecdh = NULL;\n\tEVP_PKEY *clnt_pub_pkey = NULL;\n\tEC_POINT *clnt_ecpoint = NULL;\n\tBN_CTX *bn_ctx = NULL;\n#endif\n\tn=s->method->ssl_get_message(s,\n\t\tSSL3_ST_SR_KEY_EXCH_A,\n\t\tSSL3_ST_SR_KEY_EXCH_B,\n\t\tSSL3_MT_CLIENT_KEY_EXCHANGE,\n\t\t2048,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tp=(unsigned char *)s->init_msg;\n\talg_k=s->s3->tmp.new_cipher->algorithm_mkey;\n#ifndef OPENSSL_NO_RSA\n\tif (alg_k & SSL_kRSA)\n\t\t{\n\t\tif (s->s3->tmp.use_rsa_tmp)\n\t\t\t{\n\t\t\tif ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))\n\t\t\t\trsa=s->cert->rsa_tmp;\n\t\t\tif (rsa == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;\n\t\t\tif (\t(pkey == NULL) ||\n\t\t\t\t(pkey->type != EVP_PKEY_RSA) ||\n\t\t\t\t(pkey->pkey.rsa == NULL))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\trsa=pkey->pkey.rsa;\n\t\t\t}\n\t\tif (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)\n\t\t\t{\n\t\t\tn2s(p,i);\n\t\t\tif (n != i+2)\n\t\t\t\t{\n\t\t\t\tif (!(s->options & SSL_OP_TLS_D5_BUG))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tp-=2;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tn=i;\n\t\t\t}\n\t\ti=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);\n\t\tal = -1;\n\t\tif (i != SSL_MAX_MASTER_KEY_LENGTH)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t}\n\t\tif ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))\n\t\t\t{\n\t\t\tif (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&\n\t\t\t\t(p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\t}\n\t\t\t}\n\t\tif (al != -1)\n\t\t\t{\n\t\t\tERR_clear_error();\n\t\t\ti = SSL_MAX_MASTER_KEY_LENGTH;\n\t\t\tp[0] = s->client_version >> 8;\n\t\t\tp[1] = s->client_version & 0xff;\n\t\t\tif (RAND_pseudo_bytes(p+2, i-2) <= 0)\n\t\t\t\tgoto err;\n\t\t\t}\n\t\ts->session->master_key_length=\n\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\ts->session->master_key,\n\t\t\t\tp,i);\n\t\tOPENSSL_cleanse(p,i);\n\t\t}\n\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\tif (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t{\n\t\tint idx = -1;\n\t\tEVP_PKEY *skey = NULL;\n\t\tif (n)\n\t\t\tn2s(p,i);\n\t\tif (n && n != i+2)\n\t\t\t{\n\t\t\tif (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tp-=2;\n\t\t\t\ti=(int)n;\n\t\t\t\t}\n\t\t\t}\n\t\tif (alg_k & SSL_kDHr)\n\t\t\tidx = SSL_PKEY_DH_RSA;\n\t\telse if (alg_k & SSL_kDHd)\n\t\t\tidx = SSL_PKEY_DH_DSA;\n\t\tif (idx >= 0)\n\t\t\t{\n\t\t\tskey = s->cert->pkeys[idx].privatekey;\n\t\t\tif ((skey == NULL) ||\n\t\t\t\t(skey->type != EVP_PKEY_DH) ||\n\t\t\t\t(skey->pkey.dh == NULL))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tdh_srvr = skey->pkey.dh;\n\t\t\t}\n\t\telse if (s->s3->tmp.dh == NULL)\n\t\t\t{\n\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\telse\n\t\t\tdh_srvr=s->s3->tmp.dh;\n\t\tif (n == 0L)\n\t\t\t{\n\t\t\tEVP_PKEY *clkey=X509_get_pubkey(s->session->peer);\n\t\t\tif (clkey)\n\t\t\t\t{\n\t\t\t\tif (EVP_PKEY_cmp_parameters(clkey, skey) == 1)\n\t\t\t\t\tdh_clnt = EVP_PKEY_get1_DH(clkey);\n\t\t\t\t}\n\t\t\tif (dh_clnt == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tEVP_PKEY_free(clkey);\n\t\t\tpub = dh_clnt->pub_key;\n\t\t\t}\n\t\telse\n\t\t\tpub=BN_bin2bn(p,i,NULL);\n\t\tif (pub == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\ti=DH_compute_key(p,pub,dh_srvr);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\tBN_clear_free(pub);\n\t\t\tgoto err;\n\t\t\t}\n\t\tDH_free(s->s3->tmp.dh);\n\t\ts->s3->tmp.dh=NULL;\n\t\tif (dh_clnt)\n\t\t\tDH_free(dh_clnt);\n\t\telse\n\t\t\tBN_clear_free(pub);\n\t\tpub=NULL;\n\t\ts->session->master_key_length=\n\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\ts->session->master_key,p,i);\n\t\tOPENSSL_cleanse(p,i);\n\t\tif (dh_clnt)\n\t\t\treturn 2;\n\t\t}\n\telse\n#endif\n#ifndef OPENSSL_NO_KRB5\n\tif (alg_k & SSL_kKRB5)\n\t\t{\n\t\tkrb5_error_code\t\tkrb5rc;\n\t\tkrb5_data\t\tenc_ticket;\n\t\tkrb5_data\t\tauthenticator;\n\t\tkrb5_data\t\tenc_pms;\n\t\tKSSL_CTX\t\t*kssl_ctx = s->kssl_ctx;\n\t\tEVP_CIPHER_CTX\t\tciph_ctx;\n\t\tconst EVP_CIPHER\t*enc = NULL;\n\t\tunsigned char\t\tiv[EVP_MAX_IV_LENGTH];\n\t\tunsigned char\t\tpms[SSL_MAX_MASTER_KEY_LENGTH\n\t\t\t\t\t       + EVP_MAX_BLOCK_LENGTH];\n\t\tint\t\t     padl, outl;\n\t\tkrb5_timestamp\t\tauthtime = 0;\n\t\tkrb5_ticket_times\tttimes;\n\t\tEVP_CIPHER_CTX_init(&ciph_ctx);\n\t\tif (!kssl_ctx)  kssl_ctx = kssl_ctx_new();\n\t\tn2s(p,i);\n\t\tenc_ticket.length = i;\n\t\tif (n < (long)(enc_ticket.length + 6))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tenc_ticket.data = (char *)p;\n\t\tp+=enc_ticket.length;\n\t\tn2s(p,i);\n\t\tauthenticator.length = i;\n\t\tif (n < (long)(enc_ticket.length + authenticator.length + 6))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tauthenticator.data = (char *)p;\n\t\tp+=authenticator.length;\n\t\tn2s(p,i);\n\t\tenc_pms.length = i;\n\t\tenc_pms.data = (char *)p;\n\t\tp+=enc_pms.length;\n\t\tif(enc_pms.length > sizeof pms)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t       SSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (n != (long)(enc_ticket.length + authenticator.length +\n\t\t\t\t\t\tenc_pms.length + 6))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,\n\t\t\t\t\t&kssl_err)) != 0)\n\t\t\t{\n#ifdef KSSL_DEBUG\n\t\t\tprintf("kssl_sget_tkt rtn %d [%d]\\n",\n\t\t\t\tkrb5rc, kssl_err.reason);\n\t\t\tif (kssl_err.text)\n\t\t\t\tprintf("kssl_err text= %s\\n", kssl_err.text);\n#endif\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tkssl_err.reason);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,\n\t\t\t\t\t&authtime, &kssl_err)) != 0)\n\t\t\t{\n#ifdef KSSL_DEBUG\n\t\t\tprintf("kssl_check_authent rtn %d [%d]\\n",\n\t\t\t\tkrb5rc, kssl_err.reason);\n\t\t\tif (kssl_err.text)\n\t\t\t\tprintf("kssl_err text= %s\\n", kssl_err.text);\n#endif\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tkssl_err.reason);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);\n\t\t\tgoto err;\n\t\t\t}\n#ifdef KSSL_DEBUG\n\t\tkssl_ctx_show(kssl_ctx);\n#endif\n\t\tenc = kssl_map_enc(kssl_ctx->enctype);\n\t\tif (enc == NULL)\n\t\t    goto err;\n\t\tmemset(iv, 0, sizeof iv);\n\t\tif (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DECRYPTION_FAILED);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,\n\t\t\t\t\t(unsigned char *)enc_pms.data, enc_pms.length))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DECRYPTION_FAILED);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (outl > SSL_MAX_MASTER_KEY_LENGTH)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DECRYPTION_FAILED);\n\t\t\tgoto err;\n\t\t\t}\n\t\toutl += padl;\n\t\tif (outl > SSL_MAX_MASTER_KEY_LENGTH)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff))))\n\t\t    {\n\t\t    if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t       SSL_AD_DECODE_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\t    }\n\t\tEVP_CIPHER_CTX_cleanup(&ciph_ctx);\n\t\ts->session->master_key_length=\n\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\ts->session->master_key, pms, outl);\n\t\tif (kssl_ctx->client_princ)\n\t\t\t{\n\t\t\tsize_t len = strlen(kssl_ctx->client_princ);\n\t\t\tif ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )\n\t\t\t\t{\n\t\t\t\ts->session->krb5_client_princ_len = len;\n\t\t\t\tmemcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\tif (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))\n\t\t{\n\t\tint ret = 1;\n\t\tint field_size = 0;\n\t\tconst EC_KEY   *tkey;\n\t\tconst EC_GROUP *group;\n\t\tconst BIGNUM *priv_key;\n\t\tif ((srvr_ecdh = EC_KEY_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t    ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (alg_k & (SSL_kECDHr|SSL_kECDHe))\n\t\t\t{\n\t\t\ttkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\ttkey = s->s3->tmp.ecdh;\n\t\t\t}\n\t\tgroup    = EC_KEY_get0_group(tkey);\n\t\tpriv_key = EC_KEY_get0_private_key(tkey);\n\t\tif (!EC_KEY_set_group(srvr_ecdh, group) ||\n\t\t    !EC_KEY_set_private_key(srvr_ecdh, priv_key))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t       ERR_R_EC_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif ((clnt_ecpoint = EC_POINT_new(group)) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t    ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (n == 0L)\n\t\t\t{\n\t\t\t if (alg_k & SSL_kEECDH)\n\t\t\t\t {\n\t\t\t\t al=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\t SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);\n\t\t\t\t goto f_err;\n\t\t\t\t }\n\t\t\tif (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))\n\t\t\t    == NULL) ||\n\t\t\t    (clnt_pub_pkey->type != EVP_PKEY_EC))\n\t\t\t\t{\n\t\t\t   \tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t   \tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t    SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);\n\t\t\t   \tgoto f_err;\n\t\t\t   \t}\n\t\t\tif (EC_POINT_copy(clnt_ecpoint,\n\t\t\t    EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tret = 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif ((bn_ctx = BN_CTX_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t    ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ti = *p;\n\t\t\tp += 1;\n\t\t\tif (n != 1 + i)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t    ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (EC_POINT_oct2point(group,\n\t\t\t    clnt_ecpoint, p, i, bn_ctx) == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t    ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tp=(unsigned char *)s->init_buf->data;\n\t\t\t}\n\t\tfield_size = EC_GROUP_get_degree(group);\n\t\tif (field_size <= 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t       ERR_R_ECDH_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\ti = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t    ERR_R_ECDH_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tEVP_PKEY_free(clnt_pub_pkey);\n\t\tEC_POINT_free(clnt_ecpoint);\n\t\tEC_KEY_free(srvr_ecdh);\n\t\tBN_CTX_free(bn_ctx);\n\t\tEC_KEY_free(s->s3->tmp.ecdh);\n\t\ts->s3->tmp.ecdh = NULL;\n\t\ts->session->master_key_length = s->method->ssl3_enc-> \\\n\t\t    generate_master_secret(s, s->session->master_key, p, i);\n\t\tOPENSSL_cleanse(p, i);\n\t\treturn (ret);\n\t\t}\n\telse\n#endif\n#ifndef OPENSSL_NO_PSK\n\t\tif (alg_k & SSL_kPSK)\n\t\t\t{\n\t\t\tunsigned char *t = NULL;\n\t\t\tunsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];\n\t\t\tunsigned int pre_ms_len = 0, psk_len = 0;\n\t\t\tint psk_err = 1;\n\t\t\tchar tmp_id[PSK_MAX_IDENTITY_LEN+1];\n\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\tn2s(p,i);\n\t\t\tif (n != i+2)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tSSL_R_LENGTH_MISMATCH);\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\tif (i > PSK_MAX_IDENTITY_LEN)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\tif (s->psk_server_callback == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t       SSL_R_PSK_NO_SERVER_CB);\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\tmemcpy(tmp_id, p, i);\n\t\t\tmemset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);\n\t\t\tpsk_len = s->psk_server_callback(s, tmp_id,\n\t\t\t\tpsk_or_pre_ms, sizeof(psk_or_pre_ms));\n\t\t\tOPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);\n\t\t\tif (psk_len > PSK_MAX_PSK_LEN)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\telse if (psk_len == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t       SSL_R_PSK_IDENTITY_NOT_FOUND);\n\t\t\t\tal=SSL_AD_UNKNOWN_PSK_IDENTITY;\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\tpre_ms_len=2+psk_len+2+psk_len;\n\t\t\tt = psk_or_pre_ms;\n\t\t\tmemmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);\n\t\t\ts2n(psk_len, t);\n\t\t\tmemset(t, 0, psk_len);\n\t\t\tt+=psk_len;\n\t\t\ts2n(psk_len, t);\n\t\t\tif (s->session->psk_identity != NULL)\n\t\t\t\tOPENSSL_free(s->session->psk_identity);\n\t\t\ts->session->psk_identity = BUF_strdup((char *)p);\n\t\t\tif (s->session->psk_identity == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\tif (s->session->psk_identity_hint != NULL)\n\t\t\t\tOPENSSL_free(s->session->psk_identity_hint);\n\t\t\ts->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);\n\t\t\tif (s->ctx->psk_identity_hint != NULL &&\n\t\t\t\ts->session->psk_identity_hint == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto psk_err;\n\t\t\t\t}\n\t\t\ts->session->master_key_length=\n\t\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\t\ts->session->master_key, psk_or_pre_ms, pre_ms_len);\n\t\t\tpsk_err = 0;\n\t\tpsk_err:\n\t\t\tOPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));\n\t\t\tif (psk_err != 0)\n\t\t\t\tgoto f_err;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_SRP\n\t\tif (alg_k & SSL_kSRP)\n\t\t\t{\n\t\t\tint param_len;\n\t\t\tn2s(p,i);\n\t\t\tparam_len=i+2;\n\t\t\tif (param_len > n)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_SRP_A_LENGTH);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif (!(s->srp_ctx.A=BN_bin2bn(p,i,NULL)))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (s->session->srp_username != NULL)\n\t\t\t\tOPENSSL_free(s->session->srp_username);\n\t\t\ts->session->srp_username = BUF_strdup(s->srp_ctx.login);\n\t\t\tif (s->session->srp_username == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((s->session->master_key_length = SRP_generate_server_master_secret(s,s->session->master_key))<0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tp+=i;\n\t\t\t}\n\t\telse\n#endif\n\t\tif (alg_k & SSL_kGOST)\n\t\t\t{\n\t\t\tint ret = 0;\n\t\t\tEVP_PKEY_CTX *pkey_ctx;\n\t\t\tEVP_PKEY *client_pub_pkey = NULL, *pk = NULL;\n\t\t\tunsigned char premaster_secret[32], *start;\n\t\t\tsize_t outlen=32, inlen;\n\t\t\tunsigned long alg_a;\n\t\t\talg_a = s->s3->tmp.new_cipher->algorithm_auth;\n\t\t\tif (alg_a & SSL_aGOST94)\n\t\t\t\tpk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;\n\t\t\telse if (alg_a & SSL_aGOST01)\n\t\t\t\tpk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;\n\t\t\tpkey_ctx = EVP_PKEY_CTX_new(pk,NULL);\n\t\t\tEVP_PKEY_decrypt_init(pkey_ctx);\n\t\t\tclient_pub_pkey = X509_get_pubkey(s->session->peer);\n\t\t\tif (client_pub_pkey)\n\t\t\t\t{\n\t\t\t\tif (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)\n\t\t\t\t\tERR_clear_error();\n\t\t\t\t}\n\t\t\tif ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED)))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);\n\t\t\t\tgoto gerr;\n\t\t\t\t}\n\t\t\tif (p[1] == 0x81)\n\t\t\t\t{\n\t\t\t\tstart = p+3;\n\t\t\t\tinlen = p[2];\n\t\t\t\t}\n\t\t\telse if (p[1] < 0x80)\n\t\t\t\t{\n\t\t\t\tstart = p+2;\n\t\t\t\tinlen = p[1];\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);\n\t\t\t\tgoto gerr;\n\t\t\t\t}\n\t\t\tif (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);\n\t\t\t\tgoto gerr;\n\t\t\t\t}\n\t\t\ts->session->master_key_length=\n\t\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\t\ts->session->master_key,premaster_secret,32);\n\t\t\tif (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)\n\t\t\t\tret = 2;\n\t\t\telse\n\t\t\t\tret = 1;\n\t\tgerr:\n\t\t\tEVP_PKEY_free(client_pub_pkey);\n\t\t\tEVP_PKEY_CTX_free(pkey_ctx);\n\t\t\tif (ret)\n\t\t\t\treturn ret;\n\t\t\telse\n\t\t\t\tgoto err;\n\t\t\t}\n\t\telse\n\t\t{\n\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_UNKNOWN_CIPHER_TYPE);\n\t\tgoto f_err;\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)\nerr:\n#endif\n#ifndef OPENSSL_NO_ECDH\n\tEVP_PKEY_free(clnt_pub_pkey);\n\tEC_POINT_free(clnt_ecpoint);\n\tif (srvr_ecdh != NULL)\n\t\tEC_KEY_free(srvr_ecdh);\n\tBN_CTX_free(bn_ctx);\n#endif\n\treturn(-1);\n\t}']