ssl/s3_clnt.c:2481: error: NULL_DEREFERENCE
  pointer `pub_key` last assigned on line 2481 could be null and is dereferenced by call to `EVP_PKEY_CTX_new()` at line 2481, column 13.
Showing all 27 steps of the trace


ssl/s3_clnt.c:1988:1: start of procedure ssl3_send_client_key_exchange()
1986.   
1987.   
1988. > int ssl3_send_client_key_exchange(SSL *s)
1989.   	{
1990.   	unsigned char *p,*d;

ssl/s3_clnt.c:1995:2: 
1993.   #ifndef OPENSSL_NO_RSA
1994.   	unsigned char *q;
1995. > 	EVP_PKEY *pkey=NULL;
1996.   #endif
1997.   #ifndef OPENSSL_NO_KRB5

ssl/s3_clnt.c:2001:2: 
1999.   #endif /* OPENSSL_NO_KRB5 */
2000.   #ifndef OPENSSL_NO_ECDH
2001. > 	EC_KEY *clnt_ecdh = NULL;
2002.   	const EC_POINT *srvr_ecpoint = NULL;
2003.   	EVP_PKEY *srvr_pub_pkey = NULL;

ssl/s3_clnt.c:2002:2: 
2000.   #ifndef OPENSSL_NO_ECDH
2001.   	EC_KEY *clnt_ecdh = NULL;
2002. > 	const EC_POINT *srvr_ecpoint = NULL;
2003.   	EVP_PKEY *srvr_pub_pkey = NULL;
2004.   	unsigned char *encodedPoint = NULL;

ssl/s3_clnt.c:2003:2: 
2001.   	EC_KEY *clnt_ecdh = NULL;
2002.   	const EC_POINT *srvr_ecpoint = NULL;
2003. > 	EVP_PKEY *srvr_pub_pkey = NULL;
2004.   	unsigned char *encodedPoint = NULL;
2005.   	int encoded_pt_len = 0;

ssl/s3_clnt.c:2004:2: 
2002.   	const EC_POINT *srvr_ecpoint = NULL;
2003.   	EVP_PKEY *srvr_pub_pkey = NULL;
2004. > 	unsigned char *encodedPoint = NULL;
2005.   	int encoded_pt_len = 0;
2006.   	BN_CTX * bn_ctx = NULL;

ssl/s3_clnt.c:2005:2: 
2003.   	EVP_PKEY *srvr_pub_pkey = NULL;
2004.   	unsigned char *encodedPoint = NULL;
2005. > 	int encoded_pt_len = 0;
2006.   	BN_CTX * bn_ctx = NULL;
2007.   #endif

ssl/s3_clnt.c:2006:2: 
2004.   	unsigned char *encodedPoint = NULL;
2005.   	int encoded_pt_len = 0;
2006. > 	BN_CTX * bn_ctx = NULL;
2007.   #endif
2008.   

ssl/s3_clnt.c:2009:6: Taking true branch
2007.   #endif
2008.   
2009.   	if (s->state == SSL3_ST_CW_KEY_EXCH_A)
             ^
2010.   		{
2011.   		d=(unsigned char *)s->init_buf->data;

ssl/s3_clnt.c:2011:3: 
2009.   	if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2010.   		{
2011. > 		d=(unsigned char *)s->init_buf->data;
2012.   		p= &(d[4]);
2013.   

ssl/s3_clnt.c:2012:3: 
2010.   		{
2011.   		d=(unsigned char *)s->init_buf->data;
2012. > 		p= &(d[4]);
2013.   
2014.   		alg_k=s->s3->tmp.new_cipher->algorithm_mkey;

ssl/s3_clnt.c:2014:3: 
2012.   		p= &(d[4]);
2013.   
2014. > 		alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2015.   
2016.   		/* Fool emacs indentation */

ssl/s3_clnt.c:2017:7: Taking false branch
2015.   
2016.   		/* Fool emacs indentation */
2017.   		if (0) {}
              ^
2018.   #ifndef OPENSSL_NO_RSA
2019.   		else if (alg_k & SSL_kRSA)

ssl/s3_clnt.c:2019:12: Taking false branch
2017.   		if (0) {}
2018.   #ifndef OPENSSL_NO_RSA
2019.   		else if (alg_k & SSL_kRSA)
                   ^
2020.   			{
2021.   			RSA *rsa;

ssl/s3_clnt.c:2212:12: Taking false branch
2210.   #endif
2211.   #ifndef OPENSSL_NO_DH
2212.   		else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
                   ^
2213.   			{
2214.   			DH *dh_srvr,*dh_clnt;

ssl/s3_clnt.c:2276:12: Taking false branch
2274.   
2275.   #ifndef OPENSSL_NO_ECDH 
2276.   		else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
                   ^
2277.   			{
2278.   			const EC_GROUP *srvr_group = NULL;

ssl/s3_clnt.c:2460:12: Taking true branch
2458.   			}
2459.   #endif /* !OPENSSL_NO_ECDH */
2460.   		else if (alg_k & SSL_kGOST) 
                   ^
2461.   			{
2462.   			/* GOST key exchange message creation */

ssl/s3_clnt.c:2473:4: 
2471.   
2472.   			/* Get server sertificate PKEY and create ctx from it */
2473. > 			peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2474.   			if (!peer_cert) 
2475.   				peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;

ssl/s3_clnt.c:2474:9: Taking true branch
2472.   			/* Get server sertificate PKEY and create ctx from it */
2473.   			peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2474.   			if (!peer_cert) 
                ^
2475.   				peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2476.   			if (!peer_cert)		{

ssl/s3_clnt.c:2475:5: 
2473.   			peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2474.   			if (!peer_cert) 
2475. > 				peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2476.   			if (!peer_cert)		{
2477.   					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);

ssl/s3_clnt.c:2476:9: Taking false branch
2474.   			if (!peer_cert) 
2475.   				peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2476.   			if (!peer_cert)		{
                ^
2477.   					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2478.   					goto err;

ssl/s3_clnt.c:2481:4: 
2479.   				}	
2480.   				
2481. > 			pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
2482.   			/* If we have send a certificate, and certificate key
2483.   

crypto/x509/x509_cmp.c:296:1: start of procedure X509_get_pubkey()
  294.   	}
  295.   
  296. > EVP_PKEY *X509_get_pubkey(X509 *x)
  297.   	{
  298.   	if ((x == NULL) || (x->cert_info == NULL))

crypto/x509/x509_cmp.c:298:7: Taking false branch
  296.   EVP_PKEY *X509_get_pubkey(X509 *x)
  297.   	{
  298.   	if ((x == NULL) || (x->cert_info == NULL))
               ^
  299.   		return(NULL);
  300.   	return(X509_PUBKEY_get(x->cert_info->key));

crypto/x509/x509_cmp.c:298:22: Taking true branch
  296.   EVP_PKEY *X509_get_pubkey(X509 *x)
  297.   	{
  298.   	if ((x == NULL) || (x->cert_info == NULL))
                              ^
  299.   		return(NULL);
  300.   	return(X509_PUBKEY_get(x->cert_info->key));

crypto/x509/x509_cmp.c:299:3: 
  297.   	{
  298.   	if ((x == NULL) || (x->cert_info == NULL))
  299. > 		return(NULL);
  300.   	return(X509_PUBKEY_get(x->cert_info->key));
  301.   	}

crypto/x509/x509_cmp.c:301:2: return from a call to X509_get_pubkey
  299.   		return(NULL);
  300.   	return(X509_PUBKEY_get(x->cert_info->key));
  301.   	}
          ^
  302.   
  303.   ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)

int ssl3_send_client_key_exchange(SSL *s)
	{
	unsigned char *p,*d;
	int n;
	unsigned long alg_k;
#ifndef OPENSSL_NO_RSA
	unsigned char *q;
	EVP_PKEY *pkey=NULL;
#endif
#ifndef OPENSSL_NO_KRB5
	KSSL_ERR kssl_err;
#endif
#ifndef OPENSSL_NO_ECDH
	EC_KEY *clnt_ecdh = NULL;
	const EC_POINT *srvr_ecpoint = NULL;
	EVP_PKEY *srvr_pub_pkey = NULL;
	unsigned char *encodedPoint = NULL;
	int encoded_pt_len = 0;
	BN_CTX * bn_ctx = NULL;
#endif
	if (s->state == SSL3_ST_CW_KEY_EXCH_A)
		{
		d=(unsigned char *)s->init_buf->data;
		p= &(d[4]);
		alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
		if (0) {}
#ifndef OPENSSL_NO_RSA
		else if (alg_k & SSL_kRSA)
			{
			RSA *rsa;
			unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
			if (s->session->sess_cert->peer_rsa_tmp != NULL)
				rsa=s->session->sess_cert->peer_rsa_tmp;
			else
				{
				pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
				if ((pkey == NULL) ||
					(pkey->type != EVP_PKEY_RSA) ||
					(pkey->pkey.rsa == NULL))
					{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
					goto err;
					}
				rsa=pkey->pkey.rsa;
				EVP_PKEY_free(pkey);
				}
			tmp_buf[0]=s->client_version>>8;
			tmp_buf[1]=s->client_version&0xff;
			if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
					goto err;
			s->session->master_key_length=sizeof tmp_buf;
			q=p;
			if (s->version > SSL3_VERSION)
				p+=2;
			n=RSA_public_encrypt(sizeof tmp_buf,
				tmp_buf,p,rsa,RSA_PKCS1_PADDING);
#ifdef PKCS1_CHECK
			if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
			if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
#endif
			if (n <= 0)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
				goto err;
				}
			if (s->version > SSL3_VERSION)
				{
				s2n(n,q);
				n+=2;
				}
			s->session->master_key_length=
				s->method->ssl3_enc->generate_master_secret(s,
					s->session->master_key,
					tmp_buf,sizeof tmp_buf);
			OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
			}
#endif
#ifndef OPENSSL_NO_KRB5
		else if (alg_k & SSL_kKRB5)
			{
			krb5_error_code	krb5rc;
			KSSL_CTX	*kssl_ctx = s->kssl_ctx;
			krb5_data	*enc_ticket;
			krb5_data	authenticator, *authp = NULL;
			EVP_CIPHER_CTX	ciph_ctx;
			const EVP_CIPHER *enc = NULL;
			unsigned char	iv[EVP_MAX_IV_LENGTH];
			unsigned char	tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
			unsigned char	epms[SSL_MAX_MASTER_KEY_LENGTH
						+ EVP_MAX_IV_LENGTH];
			int 		padl, outl = sizeof(epms);
			EVP_CIPHER_CTX_init(&ciph_ctx);
#ifdef KSSL_DEBUG
			printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
				alg_k, SSL_kKRB5);
#endif
			authp = NULL;
#ifdef KRB5SENDAUTH
			if (KRB5SENDAUTH)  authp = &authenticator;
#endif
			krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
				&kssl_err);
			enc = kssl_map_enc(kssl_ctx->enctype);
			if (enc == NULL)
			    goto err;
#ifdef KSSL_DEBUG
			{
			printf("kssl_cget_tkt rtn %d\n", krb5rc);
			if (krb5rc && kssl_err.text)
			  printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
			}
#endif
			if (krb5rc)
				{
				ssl3_send_alert(s,SSL3_AL_FATAL,
						SSL_AD_HANDSHAKE_FAILURE);
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
						kssl_err.reason);
				goto err;
				}
			s2n(enc_ticket->length,p);
			memcpy(p, enc_ticket->data, enc_ticket->length);
			p+= enc_ticket->length;
			n = enc_ticket->length + 2;
			if (authp  &&  authp->length)
				{
				s2n(authp->length,p);
				memcpy(p, authp->data, authp->length);
				p+= authp->length;
				n+= authp->length + 2;
				free(authp->data);
				authp->data = NULL;
				authp->length = 0;
				}
			else
				{
				s2n(0,p);
				n+=2;
				}
			    tmp_buf[0]=s->client_version>>8;
			    tmp_buf[1]=s->client_version&0xff;
			    if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
				goto err;
			memset(iv, 0, sizeof iv);
			EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
				kssl_ctx->key,iv);
			EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
				sizeof tmp_buf);
			EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
			outl += padl;
			if (outl > (int)sizeof epms)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
				goto err;
				}
			EVP_CIPHER_CTX_cleanup(&ciph_ctx);
			s2n(outl,p);
			memcpy(p, epms, outl);
			p+=outl;
			n+=outl + 2;
			s->session->master_key_length=
				s->method->ssl3_enc->generate_master_secret(s,
					s->session->master_key,
					tmp_buf, sizeof tmp_buf);
			OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
			OPENSSL_cleanse(epms, outl);
			}
#endif
#ifndef OPENSSL_NO_DH
		else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
			{
			DH *dh_srvr,*dh_clnt;
			if (s->session->sess_cert == NULL)
				{
				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
				goto err;
				}
			if (s->session->sess_cert->peer_dh_tmp != NULL)
				dh_srvr=s->session->sess_cert->peer_dh_tmp;
			else
				{
				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
				goto err;
				}
			if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
				goto err;
				}
			if (!DH_generate_key(dh_clnt))
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
				goto err;
				}
			n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
			if (n <= 0)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
				goto err;
				}
			s->session->master_key_length=
				s->method->ssl3_enc->generate_master_secret(s,
					s->session->master_key,p,n);
			memset(p,0,n);
			n=BN_num_bytes(dh_clnt->pub_key);
			s2n(n,p);
			BN_bn2bin(dh_clnt->pub_key,p);
			n+=2;
			DH_free(dh_clnt);
			}
#endif
#ifndef OPENSSL_NO_ECDH
		else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
			{
			const EC_GROUP *srvr_group = NULL;
			EC_KEY *tkey;
			int ecdh_clnt_cert = 0;
			int field_size = 0;
			if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
				{
				}
			if (s->session->sess_cert->peer_ecdh_tmp != NULL)
				{
				tkey = s->session->sess_cert->peer_ecdh_tmp;
				}
			else
				{
				srvr_pub_pkey = X509_get_pubkey(s->session-> \
				    sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
				if ((srvr_pub_pkey == NULL) ||
				    (srvr_pub_pkey->type != EVP_PKEY_EC) ||
				    (srvr_pub_pkey->pkey.ec == NULL))
					{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
					    ERR_R_INTERNAL_ERROR);
					goto err;
					}
				tkey = srvr_pub_pkey->pkey.ec;
				}
			srvr_group   = EC_KEY_get0_group(tkey);
			srvr_ecpoint = EC_KEY_get0_public_key(tkey);
			if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
				    ERR_R_INTERNAL_ERROR);
				goto err;
				}
			if ((clnt_ecdh=EC_KEY_new()) == NULL)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
				goto err;
				}
			if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
				goto err;
				}
			if (ecdh_clnt_cert)
				{
				const BIGNUM *priv_key;
				tkey = s->cert->key->privatekey->pkey.ec;
				priv_key = EC_KEY_get0_private_key(tkey);
				if (priv_key == NULL)
					{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
					goto err;
					}
				if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
					{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
					goto err;
					}
				}
			else
				{
				if (!(EC_KEY_generate_key(clnt_ecdh)))
					{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
					goto err;
					}
				}
			field_size = EC_GROUP_get_degree(srvr_group);
			if (field_size <= 0)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
				       ERR_R_ECDH_LIB);
				goto err;
				}
			n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
			if (n <= 0)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
				       ERR_R_ECDH_LIB);
				goto err;
				}
			s->session->master_key_length = s->method->ssl3_enc \
			    -> generate_master_secret(s,
				s->session->master_key,
				p, n);
			memset(p, 0, n);
			if (ecdh_clnt_cert)
				{
				n = 0;
				}
			else
				{
				encoded_pt_len =
				    EC_POINT_point2oct(srvr_group,
					EC_KEY_get0_public_key(clnt_ecdh),
					POINT_CONVERSION_UNCOMPRESSED,
					NULL, 0, NULL);
				encodedPoint = (unsigned char *)
				    OPENSSL_malloc(encoded_pt_len *
					sizeof(unsigned char));
				bn_ctx = BN_CTX_new();
				if ((encodedPoint == NULL) ||
				    (bn_ctx == NULL))
					{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
					goto err;
					}
				n = EC_POINT_point2oct(srvr_group,
				    EC_KEY_get0_public_key(clnt_ecdh),
				    POINT_CONVERSION_UNCOMPRESSED,
				    encodedPoint, encoded_pt_len, bn_ctx);
				*p = n;
				p += 1;
				memcpy((unsigned char *)p, encodedPoint, n);
				n += 1;
				}
			BN_CTX_free(bn_ctx);
			if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
			if (clnt_ecdh != NULL)
				 EC_KEY_free(clnt_ecdh);
			EVP_PKEY_free(srvr_pub_pkey);
			}
#endif
		else if (alg_k & SSL_kGOST)
			{
			EVP_PKEY_CTX *pkey_ctx;
			X509 *peer_cert;
			size_t msglen;
			unsigned int md_len;
			int keytype;
			unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
			EVP_MD_CTX *ukm_hash;
			EVP_PKEY *pub_key;
			peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
			if (!peer_cert)
				peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
			if (!peer_cert)		{
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
					goto err;
				}
			pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
			EVP_PKEY_encrypt_init(pkey_ctx);
		    RAND_bytes(premaster_secret,32);
			if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
				if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
					ERR_clear_error();
				}
			}
			ukm_hash = EVP_MD_CTX_create();
			EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
			EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
			EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
			EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
			EVP_MD_CTX_destroy(ukm_hash);
			if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
				8,shared_ukm)<0) {
					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
						SSL_R_LIBRARY_BUG);
					goto err;
				}
			*(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
			msglen=255;
			if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
			SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
					SSL_R_LIBRARY_BUG);
				goto err;
			}
			if (msglen >= 0x80)
				{
				*(p++)=0x81;
				*(p++)= msglen & 0xff;
				n=msglen+3;
				}
			else
				{
				*(p++)= msglen & 0xff;
				n=msglen+2;
				}
			memcpy(p, tmp, msglen);
			if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
				{
				s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
				}
			EVP_PKEY_CTX_free(pkey_ctx);
			s->session->master_key_length=
				s->method->ssl3_enc->generate_master_secret(s,
					s->session->master_key,premaster_secret,32);
			EVP_PKEY_free(pub_key);
			}
#ifndef OPENSSL_NO_PSK
		else if (alg_k & SSL_kPSK)
			{
			char identity[PSK_MAX_IDENTITY_LEN];
			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;
			n = 0;
			if (s->psk_client_callback == NULL)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
					SSL_R_PSK_NO_CLIENT_CB);
				goto err;
				}
			psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
				identity, PSK_MAX_IDENTITY_LEN,
				psk_or_pre_ms, sizeof(psk_or_pre_ms));
			if (psk_len > PSK_MAX_PSK_LEN)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
					ERR_R_INTERNAL_ERROR);
				goto psk_err;
				}
			else if (psk_len == 0)
				{
				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
					SSL_R_PSK_IDENTITY_NOT_FOUND);
				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_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_SEND_CLIENT_KEY_EXCHANGE,
					ERR_R_MALLOC_FAILURE);
				goto psk_err;
				}
			if (s->session->psk_identity != NULL)
				OPENSSL_free(s->session->psk_identity);
			s->session->psk_identity = BUF_strdup(identity);
			if (s->session->psk_identity == NULL)
				{
				SSLerr(SSL_F_SSL3_SEND_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);
			n = strlen(identity);
			s2n(n, p);
			memcpy(p, identity, n);
			n+=2;
			psk_err = 0;
		psk_err:
			OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
			OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
			if (psk_err != 0)
				{
				ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
				goto err;
				}
			}
#endif
		else
			{
			ssl3_send_alert(s, SSL3_AL_FATAL,
			    SSL_AD_HANDSHAKE_FAILURE);
			SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
			    ERR_R_INTERNAL_ERROR);
			goto err;
			}
		*(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
		l2n3(n,d);
		s->state=SSL3_ST_CW_KEY_EXCH_B;
		s->init_num=n+4;
		s->init_off=0;
		}
	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
err:
#ifndef OPENSSL_NO_ECDH
	BN_CTX_free(bn_ctx);
	if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
	if (clnt_ecdh != NULL)
		EC_KEY_free(clnt_ecdh);
	EVP_PKEY_free(srvr_pub_pkey);
#endif
	return(-1);
	}
['int ssl3_send_client_key_exchange(SSL *s)\n\t{\n\tunsigned char *p,*d;\n\tint n;\n\tunsigned long alg_k;\n#ifndef OPENSSL_NO_RSA\n\tunsigned char *q;\n\tEVP_PKEY *pkey=NULL;\n#endif\n#ifndef OPENSSL_NO_KRB5\n\tKSSL_ERR kssl_err;\n#endif\n#ifndef OPENSSL_NO_ECDH\n\tEC_KEY *clnt_ecdh = NULL;\n\tconst EC_POINT *srvr_ecpoint = NULL;\n\tEVP_PKEY *srvr_pub_pkey = NULL;\n\tunsigned char *encodedPoint = NULL;\n\tint encoded_pt_len = 0;\n\tBN_CTX * bn_ctx = NULL;\n#endif\n\tif (s->state == SSL3_ST_CW_KEY_EXCH_A)\n\t\t{\n\t\td=(unsigned char *)s->init_buf->data;\n\t\tp= &(d[4]);\n\t\talg_k=s->s3->tmp.new_cipher->algorithm_mkey;\n\t\tif (0) {}\n#ifndef OPENSSL_NO_RSA\n\t\telse if (alg_k & SSL_kRSA)\n\t\t\t{\n\t\t\tRSA *rsa;\n\t\t\tunsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];\n\t\t\tif (s->session->sess_cert->peer_rsa_tmp != NULL)\n\t\t\t\trsa=s->session->sess_cert->peer_rsa_tmp;\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n\t\t\t\tif ((pkey == NULL) ||\n\t\t\t\t\t(pkey->type != EVP_PKEY_RSA) ||\n\t\t\t\t\t(pkey->pkey.rsa == NULL))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\trsa=pkey->pkey.rsa;\n\t\t\t\tEVP_PKEY_free(pkey);\n\t\t\t\t}\n\t\t\ttmp_buf[0]=s->client_version>>8;\n\t\t\ttmp_buf[1]=s->client_version&0xff;\n\t\t\tif (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)\n\t\t\t\t\tgoto err;\n\t\t\ts->session->master_key_length=sizeof tmp_buf;\n\t\t\tq=p;\n\t\t\tif (s->version > SSL3_VERSION)\n\t\t\t\tp+=2;\n\t\t\tn=RSA_public_encrypt(sizeof tmp_buf,\n\t\t\t\ttmp_buf,p,rsa,RSA_PKCS1_PADDING);\n#ifdef PKCS1_CHECK\n\t\t\tif (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;\n\t\t\tif (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;\n#endif\n\t\t\tif (n <= 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (s->version > SSL3_VERSION)\n\t\t\t\t{\n\t\t\t\ts2n(n,q);\n\t\t\t\tn+=2;\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,\n\t\t\t\t\ttmp_buf,sizeof tmp_buf);\n\t\t\tOPENSSL_cleanse(tmp_buf,sizeof tmp_buf);\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_KRB5\n\t\telse if (alg_k & SSL_kKRB5)\n\t\t\t{\n\t\t\tkrb5_error_code\tkrb5rc;\n\t\t\tKSSL_CTX\t*kssl_ctx = s->kssl_ctx;\n\t\t\tkrb5_data\t*enc_ticket;\n\t\t\tkrb5_data\tauthenticator, *authp = NULL;\n\t\t\tEVP_CIPHER_CTX\tciph_ctx;\n\t\t\tconst EVP_CIPHER *enc = NULL;\n\t\t\tunsigned char\tiv[EVP_MAX_IV_LENGTH];\n\t\t\tunsigned char\ttmp_buf[SSL_MAX_MASTER_KEY_LENGTH];\n\t\t\tunsigned char\tepms[SSL_MAX_MASTER_KEY_LENGTH\n\t\t\t\t\t\t+ EVP_MAX_IV_LENGTH];\n\t\t\tint \t\tpadl, outl = sizeof(epms);\n\t\t\tEVP_CIPHER_CTX_init(&ciph_ctx);\n#ifdef KSSL_DEBUG\n\t\t\tprintf("ssl3_send_client_key_exchange(%lx & %lx)\\n",\n\t\t\t\talg_k, SSL_kKRB5);\n#endif\n\t\t\tauthp = NULL;\n#ifdef KRB5SENDAUTH\n\t\t\tif (KRB5SENDAUTH)  authp = &authenticator;\n#endif\n\t\t\tkrb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,\n\t\t\t\t&kssl_err);\n\t\t\tenc = kssl_map_enc(kssl_ctx->enctype);\n\t\t\tif (enc == NULL)\n\t\t\t    goto err;\n#ifdef KSSL_DEBUG\n\t\t\t{\n\t\t\tprintf("kssl_cget_tkt rtn %d\\n", krb5rc);\n\t\t\tif (krb5rc && kssl_err.text)\n\t\t\t  printf("kssl_cget_tkt kssl_err=%s\\n", kssl_err.text);\n\t\t\t}\n#endif\n\t\t\tif (krb5rc)\n\t\t\t\t{\n\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,\n\t\t\t\t\t\tSSL_AD_HANDSHAKE_FAILURE);\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\t\tkssl_err.reason);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ts2n(enc_ticket->length,p);\n\t\t\tmemcpy(p, enc_ticket->data, enc_ticket->length);\n\t\t\tp+= enc_ticket->length;\n\t\t\tn = enc_ticket->length + 2;\n\t\t\tif (authp  &&  authp->length)\n\t\t\t\t{\n\t\t\t\ts2n(authp->length,p);\n\t\t\t\tmemcpy(p, authp->data, authp->length);\n\t\t\t\tp+= authp->length;\n\t\t\t\tn+= authp->length + 2;\n\t\t\t\tfree(authp->data);\n\t\t\t\tauthp->data = NULL;\n\t\t\t\tauthp->length = 0;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts2n(0,p);\n\t\t\t\tn+=2;\n\t\t\t\t}\n\t\t\t    tmp_buf[0]=s->client_version>>8;\n\t\t\t    tmp_buf[1]=s->client_version&0xff;\n\t\t\t    if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)\n\t\t\t\tgoto err;\n\t\t\tmemset(iv, 0, sizeof iv);\n\t\t\tEVP_EncryptInit_ex(&ciph_ctx,enc, NULL,\n\t\t\t\tkssl_ctx->key,iv);\n\t\t\tEVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,\n\t\t\t\tsizeof tmp_buf);\n\t\t\tEVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);\n\t\t\toutl += padl;\n\t\t\tif (outl > (int)sizeof epms)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tEVP_CIPHER_CTX_cleanup(&ciph_ctx);\n\t\t\ts2n(outl,p);\n\t\t\tmemcpy(p, epms, outl);\n\t\t\tp+=outl;\n\t\t\tn+=outl + 2;\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,\n\t\t\t\t\ttmp_buf, sizeof tmp_buf);\n\t\t\tOPENSSL_cleanse(tmp_buf, sizeof tmp_buf);\n\t\t\tOPENSSL_cleanse(epms, outl);\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_DH\n\t\telse if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t\t{\n\t\t\tDH *dh_srvr,*dh_clnt;\n\t\t\tif (s->session->sess_cert == NULL)\n\t\t\t\t{\n\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (s->session->sess_cert->peer_dh_tmp != NULL)\n\t\t\t\tdh_srvr=s->session->sess_cert->peer_dh_tmp;\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (!DH_generate_key(dh_clnt))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tn=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);\n\t\t\tif (n <= 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\tgoto 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,p,n);\n\t\t\tmemset(p,0,n);\n\t\t\tn=BN_num_bytes(dh_clnt->pub_key);\n\t\t\ts2n(n,p);\n\t\t\tBN_bn2bin(dh_clnt->pub_key,p);\n\t\t\tn+=2;\n\t\t\tDH_free(dh_clnt);\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\telse if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))\n\t\t\t{\n\t\t\tconst EC_GROUP *srvr_group = NULL;\n\t\t\tEC_KEY *tkey;\n\t\t\tint ecdh_clnt_cert = 0;\n\t\t\tint field_size = 0;\n\t\t\tif ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\tif (s->session->sess_cert->peer_ecdh_tmp != NULL)\n\t\t\t\t{\n\t\t\t\ttkey = s->session->sess_cert->peer_ecdh_tmp;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tsrvr_pub_pkey = X509_get_pubkey(s->session-> \\\n\t\t\t\t    sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);\n\t\t\t\tif ((srvr_pub_pkey == NULL) ||\n\t\t\t\t    (srvr_pub_pkey->type != EVP_PKEY_EC) ||\n\t\t\t\t    (srvr_pub_pkey->pkey.ec == NULL))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\t    ERR_R_INTERNAL_ERROR);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ttkey = srvr_pub_pkey->pkey.ec;\n\t\t\t\t}\n\t\t\tsrvr_group   = EC_KEY_get0_group(tkey);\n\t\t\tsrvr_ecpoint = EC_KEY_get0_public_key(tkey);\n\t\t\tif ((srvr_group == NULL) || (srvr_ecpoint == NULL))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t    ERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((clnt_ecdh=EC_KEY_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (!EC_KEY_set_group(clnt_ecdh, srvr_group))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (ecdh_clnt_cert)\n\t\t\t\t{\n\t\t\t\tconst BIGNUM *priv_key;\n\t\t\t\ttkey = s->cert->key->privatekey->pkey.ec;\n\t\t\t\tpriv_key = EC_KEY_get0_private_key(tkey);\n\t\t\t\tif (priv_key == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tif (!EC_KEY_set_private_key(clnt_ecdh, priv_key))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!(EC_KEY_generate_key(clnt_ecdh)))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tfield_size = EC_GROUP_get_degree(srvr_group);\n\t\t\tif (field_size <= 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t       ERR_R_ECDH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tn=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);\n\t\t\tif (n <= 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t       ERR_R_ECDH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ts->session->master_key_length = s->method->ssl3_enc \\\n\t\t\t    -> generate_master_secret(s,\n\t\t\t\ts->session->master_key,\n\t\t\t\tp, n);\n\t\t\tmemset(p, 0, n);\n\t\t\tif (ecdh_clnt_cert)\n\t\t\t\t{\n\t\t\t\tn = 0;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tencoded_pt_len =\n\t\t\t\t    EC_POINT_point2oct(srvr_group,\n\t\t\t\t\tEC_KEY_get0_public_key(clnt_ecdh),\n\t\t\t\t\tPOINT_CONVERSION_UNCOMPRESSED,\n\t\t\t\t\tNULL, 0, NULL);\n\t\t\t\tencodedPoint = (unsigned char *)\n\t\t\t\t    OPENSSL_malloc(encoded_pt_len *\n\t\t\t\t\tsizeof(unsigned char));\n\t\t\t\tbn_ctx = BN_CTX_new();\n\t\t\t\tif ((encodedPoint == NULL) ||\n\t\t\t\t    (bn_ctx == NULL))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tn = EC_POINT_point2oct(srvr_group,\n\t\t\t\t    EC_KEY_get0_public_key(clnt_ecdh),\n\t\t\t\t    POINT_CONVERSION_UNCOMPRESSED,\n\t\t\t\t    encodedPoint, encoded_pt_len, bn_ctx);\n\t\t\t\t*p = n;\n\t\t\t\tp += 1;\n\t\t\t\tmemcpy((unsigned char *)p, encodedPoint, n);\n\t\t\t\tn += 1;\n\t\t\t\t}\n\t\t\tBN_CTX_free(bn_ctx);\n\t\t\tif (encodedPoint != NULL) OPENSSL_free(encodedPoint);\n\t\t\tif (clnt_ecdh != NULL)\n\t\t\t\t EC_KEY_free(clnt_ecdh);\n\t\t\tEVP_PKEY_free(srvr_pub_pkey);\n\t\t\t}\n#endif\n\t\telse if (alg_k & SSL_kGOST)\n\t\t\t{\n\t\t\tEVP_PKEY_CTX *pkey_ctx;\n\t\t\tX509 *peer_cert;\n\t\t\tsize_t msglen;\n\t\t\tunsigned int md_len;\n\t\t\tint keytype;\n\t\t\tunsigned char premaster_secret[32],shared_ukm[32], tmp[256];\n\t\t\tEVP_MD_CTX *ukm_hash;\n\t\t\tEVP_PKEY *pub_key;\n\t\t\tpeer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;\n\t\t\tif (!peer_cert)\n\t\t\t\tpeer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;\n\t\t\tif (!peer_cert)\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tpkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);\n\t\t\tEVP_PKEY_encrypt_init(pkey_ctx);\n\t\t    RAND_bytes(premaster_secret,32);\n\t\t\tif (s->s3->tmp.cert_req && s->cert->key->privatekey) {\n\t\t\t\tif (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {\n\t\t\t\t\tERR_clear_error();\n\t\t\t\t}\n\t\t\t}\n\t\t\tukm_hash = EVP_MD_CTX_create();\n\t\t\tEVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));\n\t\t\tEVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);\n\t\t\tEVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);\n\t\t\tEVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);\n\t\t\tEVP_MD_CTX_destroy(ukm_hash);\n\t\t\tif (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,\n\t\t\t\t8,shared_ukm)<0) {\n\t\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\t\tSSL_R_LIBRARY_BUG);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t*(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;\n\t\t\tmsglen=255;\n\t\t\tif (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {\n\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tSSL_R_LIBRARY_BUG);\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t\tif (msglen >= 0x80)\n\t\t\t\t{\n\t\t\t\t*(p++)=0x81;\n\t\t\t\t*(p++)= msglen & 0xff;\n\t\t\t\tn=msglen+3;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\t*(p++)= msglen & 0xff;\n\t\t\t\tn=msglen+2;\n\t\t\t\t}\n\t\t\tmemcpy(p, tmp, msglen);\n\t\t\tif (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)\n\t\t\t\t{\n\t\t\t\ts->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;\n\t\t\t\t}\n\t\t\tEVP_PKEY_CTX_free(pkey_ctx);\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\tEVP_PKEY_free(pub_key);\n\t\t\t}\n#ifndef OPENSSL_NO_PSK\n\t\telse if (alg_k & SSL_kPSK)\n\t\t\t{\n\t\t\tchar identity[PSK_MAX_IDENTITY_LEN];\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\tn = 0;\n\t\t\tif (s->psk_client_callback == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tSSL_R_PSK_NO_CLIENT_CB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tpsk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,\n\t\t\t\tidentity, PSK_MAX_IDENTITY_LEN,\n\t\t\t\tpsk_or_pre_ms, sizeof(psk_or_pre_ms));\n\t\t\tif (psk_len > PSK_MAX_PSK_LEN)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_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_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t\t\tSSL_R_PSK_IDENTITY_NOT_FOUND);\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_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_SEND_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 != NULL)\n\t\t\t\tOPENSSL_free(s->session->psk_identity);\n\t\t\ts->session->psk_identity = BUF_strdup(identity);\n\t\t\tif (s->session->psk_identity == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_SEND_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,\n\t\t\t\t\tpsk_or_pre_ms, pre_ms_len);\n\t\t\tn = strlen(identity);\n\t\t\ts2n(n, p);\n\t\t\tmemcpy(p, identity, n);\n\t\t\tn+=2;\n\t\t\tpsk_err = 0;\n\t\tpsk_err:\n\t\t\tOPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);\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\t{\n\t\t\t\tssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n#endif\n\t\telse\n\t\t\t{\n\t\t\tssl3_send_alert(s, SSL3_AL_FATAL,\n\t\t\t    SSL_AD_HANDSHAKE_FAILURE);\n\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n\t\t\t    ERR_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\t*(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;\n\t\tl2n3(n,d);\n\t\ts->state=SSL3_ST_CW_KEY_EXCH_B;\n\t\ts->init_num=n+4;\n\t\ts->init_off=0;\n\t\t}\n\treturn(ssl3_do_write(s,SSL3_RT_HANDSHAKE));\nerr:\n#ifndef OPENSSL_NO_ECDH\n\tBN_CTX_free(bn_ctx);\n\tif (encodedPoint != NULL) OPENSSL_free(encodedPoint);\n\tif (clnt_ecdh != NULL)\n\t\tEC_KEY_free(clnt_ecdh);\n\tEVP_PKEY_free(srvr_pub_pkey);\n#endif\n\treturn(-1);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}']