ssl/d1_srvr.c:1109: error: NULL_DEREFERENCE
  pointer `dh` last assigned on line 1032 could be null and is dereferenced by call to `DH_free()` at line 1109, column 5.
Showing all 27 steps of the trace


ssl/d1_srvr.c:1022:1: start of procedure dtls1_send_server_key_exchange()
1020.   	}
1021.   
1022. > int dtls1_send_server_key_exchange(SSL *s)
1023.   	{
1024.   #ifndef OPENSSL_NO_RSA

ssl/d1_srvr.c:1032:2: 
1030.   #endif
1031.   #ifndef OPENSSL_NO_DH
1032. > 	DH *dh=NULL,*dhp;
1033.   #endif
1034.   #ifndef OPENSSL_NO_ECDH

ssl/d1_srvr.c:1035:2: 
1033.   #endif
1034.   #ifndef OPENSSL_NO_ECDH
1035. > 	EC_KEY *ecdh=NULL, *ecdhp;
1036.   	unsigned char *encodedPoint = NULL;
1037.   	int encodedlen = 0;

ssl/d1_srvr.c:1036:2: 
1034.   #ifndef OPENSSL_NO_ECDH
1035.   	EC_KEY *ecdh=NULL, *ecdhp;
1036. > 	unsigned char *encodedPoint = NULL;
1037.   	int encodedlen = 0;
1038.   	int curve_id = 0;

ssl/d1_srvr.c:1037:2: 
1035.   	EC_KEY *ecdh=NULL, *ecdhp;
1036.   	unsigned char *encodedPoint = NULL;
1037. > 	int encodedlen = 0;
1038.   	int curve_id = 0;
1039.   	BN_CTX *bn_ctx = NULL; 

ssl/d1_srvr.c:1038:2: 
1036.   	unsigned char *encodedPoint = NULL;
1037.   	int encodedlen = 0;
1038. > 	int curve_id = 0;
1039.   	BN_CTX *bn_ctx = NULL; 
1040.   #endif

ssl/d1_srvr.c:1039:2: 
1037.   	int encodedlen = 0;
1038.   	int curve_id = 0;
1039. > 	BN_CTX *bn_ctx = NULL; 
1040.   #endif
1041.   	EVP_PKEY *pkey;

ssl/d1_srvr.c:1052:2: 
1050.   	EVP_MD_CTX md_ctx;
1051.   
1052. > 	EVP_MD_CTX_init(&md_ctx);
1053.   	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1054.   		{

crypto/evp/digest.c:120:1: start of procedure EVP_MD_CTX_init()
  118.   #endif
  119.   
  120. > void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
  121.   	{
  122.   	memset(ctx,'\0',sizeof *ctx);

crypto/evp/digest.c:122:2: 
  120.   void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
  121.   	{
  122. > 	memset(ctx,'\0',sizeof *ctx);
  123.   	}
  124.   

crypto/evp/digest.c:123:2: return from a call to EVP_MD_CTX_init
  121.   	{
  122.   	memset(ctx,'\0',sizeof *ctx);
  123.   	}
          ^
  124.   
  125.   EVP_MD_CTX *EVP_MD_CTX_create(void)

ssl/d1_srvr.c:1053:6: Taking true branch
1051.   
1052.   	EVP_MD_CTX_init(&md_ctx);
1053.   	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
             ^
1054.   		{
1055.   		type=s->s3->tmp.new_cipher->algorithm_mkey;

ssl/d1_srvr.c:1055:3: 
1053.   	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1054.   		{
1055. > 		type=s->s3->tmp.new_cipher->algorithm_mkey;
1056.   		cert=s->cert;
1057.   

ssl/d1_srvr.c:1056:3: 
1054.   		{
1055.   		type=s->s3->tmp.new_cipher->algorithm_mkey;
1056. > 		cert=s->cert;
1057.   
1058.   		buf=s->init_buf;

ssl/d1_srvr.c:1058:3: 
1056.   		cert=s->cert;
1057.   
1058. > 		buf=s->init_buf;
1059.   
1060.   		r[0]=r[1]=r[2]=r[3]=NULL;

ssl/d1_srvr.c:1060:3: 
1058.   		buf=s->init_buf;
1059.   
1060. > 		r[0]=r[1]=r[2]=r[3]=NULL;
1061.   		n=0;
1062.   #ifndef OPENSSL_NO_RSA

ssl/d1_srvr.c:1061:3: 
1059.   
1060.   		r[0]=r[1]=r[2]=r[3]=NULL;
1061. > 		n=0;
1062.   #ifndef OPENSSL_NO_RSA
1063.   		if (type & SSL_kRSA)

ssl/d1_srvr.c:1063:7: Taking false branch
1061.   		n=0;
1062.   #ifndef OPENSSL_NO_RSA
1063.   		if (type & SSL_kRSA)
              ^
1064.   			{
1065.   			rsa=cert->rsa_tmp;

ssl/d1_srvr.c:1093:8: Taking true branch
1091.   #endif
1092.   #ifndef OPENSSL_NO_DH
1093.   			if (type & SSL_kEDH)
               ^
1094.   			{
1095.   			dhp=cert->dh_tmp;

ssl/d1_srvr.c:1095:4: 
1093.   			if (type & SSL_kEDH)
1094.   			{
1095. > 			dhp=cert->dh_tmp;
1096.   			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1097.   				dhp=s->cert->dh_tmp_cb(s,

ssl/d1_srvr.c:1096:9: Taking true branch
1094.   			{
1095.   			dhp=cert->dh_tmp;
1096.   			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
                ^
1097.   				dhp=s->cert->dh_tmp_cb(s,
1098.   				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),

ssl/d1_srvr.c:1096:26: Taking true branch
1094.   			{
1095.   			dhp=cert->dh_tmp;
1096.   			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
                                 ^
1097.   				dhp=s->cert->dh_tmp_cb(s,
1098.   				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),

ssl/d1_srvr.c:1099:11: Condition is false
1097.   				dhp=s->cert->dh_tmp_cb(s,
1098.   				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1099.   				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
                  ^
1100.   			if (dhp == NULL)
1101.   				{

ssl/d1_srvr.c:1097:5: Skipping __function_pointer__(): unresolved function pointer
1095.   			dhp=cert->dh_tmp;
1096.   			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1097.   				dhp=s->cert->dh_tmp_cb(s,
            ^
1098.   				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1099.   				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));

ssl/d1_srvr.c:1100:8: Taking false branch
1098.   				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1099.   				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1100.   			if (dhp == NULL)
               ^
1101.   				{
1102.   				al=SSL_AD_HANDSHAKE_FAILURE;

ssl/d1_srvr.c:1107:8: Taking true branch
1105.   				}
1106.   
1107.   			if (s->s3->tmp.dh != NULL)
               ^
1108.   				{
1109.   				DH_free(dh);

ssl/d1_srvr.c:1109:5: 
1107.   			if (s->s3->tmp.dh != NULL)
1108.   				{
1109. > 				DH_free(dh);
1110.   				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1111.   				goto err;

int dtls1_send_server_key_exchange(SSL *s)
	{
#ifndef OPENSSL_NO_RSA
	unsigned char *q;
	int j,num;
	RSA *rsa;
	unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
	unsigned int u;
#endif
#ifndef OPENSSL_NO_DH
	DH *dh=NULL,*dhp;
#endif
#ifndef OPENSSL_NO_ECDH
	EC_KEY *ecdh=NULL, *ecdhp;
	unsigned char *encodedPoint = NULL;
	int encodedlen = 0;
	int curve_id = 0;
	BN_CTX *bn_ctx = NULL;
#endif
	EVP_PKEY *pkey;
	unsigned char *p,*d;
	int al,i;
	unsigned long type;
	int n;
	CERT *cert;
	BIGNUM *r[4];
	int nr[4],kn;
	BUF_MEM *buf;
	EVP_MD_CTX md_ctx;
	EVP_MD_CTX_init(&md_ctx);
	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
		{
		type=s->s3->tmp.new_cipher->algorithm_mkey;
		cert=s->cert;
		buf=s->init_buf;
		r[0]=r[1]=r[2]=r[3]=NULL;
		n=0;
#ifndef OPENSSL_NO_RSA
		if (type & SSL_kRSA)
			{
			rsa=cert->rsa_tmp;
			if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
				{
				rsa=s->cert->rsa_tmp_cb(s,
				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
				if(rsa == NULL)
				{
					al=SSL_AD_HANDSHAKE_FAILURE;
					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
					goto f_err;
				}
				RSA_up_ref(rsa);
				cert->rsa_tmp=rsa;
				}
			if (rsa == NULL)
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
				goto f_err;
				}
			r[0]=rsa->n;
			r[1]=rsa->e;
			s->s3->tmp.use_rsa_tmp=1;
			}
		else
#endif
#ifndef OPENSSL_NO_DH
			if (type & SSL_kEDH)
			{
			dhp=cert->dh_tmp;
			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
				dhp=s->cert->dh_tmp_cb(s,
				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
			if (dhp == NULL)
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
				goto f_err;
				}
			if (s->s3->tmp.dh != NULL)
				{
				DH_free(dh);
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
				goto err;
				}
			if ((dh=DHparams_dup(dhp)) == NULL)
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
				goto err;
				}
			s->s3->tmp.dh=dh;
			if ((dhp->pub_key == NULL ||
			     dhp->priv_key == NULL ||
			     (s->options & SSL_OP_SINGLE_DH_USE)))
				{
				if(!DH_generate_key(dh))
				    {
				    SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
					   ERR_R_DH_LIB);
				    goto err;
				    }
				}
			else
				{
				dh->pub_key=BN_dup(dhp->pub_key);
				dh->priv_key=BN_dup(dhp->priv_key);
				if ((dh->pub_key == NULL) ||
					(dh->priv_key == NULL))
					{
					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
					goto err;
					}
				}
			r[0]=dh->p;
			r[1]=dh->g;
			r[2]=dh->pub_key;
			}
		else
#endif
#ifndef OPENSSL_NO_ECDH
			if (type & SSL_kEECDH)
			{
			const EC_GROUP *group;
			ecdhp=cert->ecdh_tmp;
			if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
				{
				ecdhp=s->cert->ecdh_tmp_cb(s,
				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
				}
			if (ecdhp == NULL)
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
				goto f_err;
				}
			if (s->s3->tmp.ecdh != NULL)
				{
				EC_KEY_free(s->s3->tmp.ecdh);
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
				goto err;
				}
			if (ecdhp == NULL)
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
				goto err;
				}
			if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
				goto err;
				}
			s->s3->tmp.ecdh=ecdh;
			if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
			    (EC_KEY_get0_private_key(ecdh) == NULL) ||
			    (s->options & SSL_OP_SINGLE_ECDH_USE))
				{
				if(!EC_KEY_generate_key(ecdh))
				    {
				    SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
				    goto err;
				    }
				}
			if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
			    (EC_KEY_get0_public_key(ecdh)  == NULL) ||
			    (EC_KEY_get0_private_key(ecdh) == NULL))
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
				goto err;
				}
			if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
			    (EC_GROUP_get_degree(group) > 163))
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
				goto err;
				}
			if ((curve_id =
			    tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
			    == 0)
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
				goto err;
				}
			encodedlen = EC_POINT_point2oct(group,
			    EC_KEY_get0_public_key(ecdh),
			    POINT_CONVERSION_UNCOMPRESSED,
			    NULL, 0, NULL);
			encodedPoint = (unsigned char *)
			    OPENSSL_malloc(encodedlen*sizeof(unsigned char));
			bn_ctx = BN_CTX_new();
			if ((encodedPoint == NULL) || (bn_ctx == NULL))
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
				goto err;
				}
			encodedlen = EC_POINT_point2oct(group,
			    EC_KEY_get0_public_key(ecdh),
			    POINT_CONVERSION_UNCOMPRESSED,
			    encodedPoint, encodedlen, bn_ctx);
			if (encodedlen == 0)
				{
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
				goto err;
				}
			BN_CTX_free(bn_ctx);  bn_ctx=NULL;
			n = 4 + encodedlen;
			r[0]=NULL;
			r[1]=NULL;
			r[2]=NULL;
			r[3]=NULL;
			}
		else
#endif
#ifndef OPENSSL_NO_PSK
			if (type & SSL_kPSK)
				{
				n+=2+strlen(s->ctx->psk_identity_hint);
				}
			else
#endif
			{
			al=SSL_AD_HANDSHAKE_FAILURE;
			SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
			goto f_err;
			}
		for (i=0; r[i] != NULL; i++)
			{
			nr[i]=BN_num_bytes(r[i]);
			n+=2+nr[i];
			}
		if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
			&& !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
			{
			if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher, NULL))
				== NULL)
				{
				al=SSL_AD_DECODE_ERROR;
				goto f_err;
				}
			kn=EVP_PKEY_size(pkey);
			}
		else
			{
			pkey=NULL;
			kn=0;
			}
		if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))
			{
			SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
			goto err;
			}
		d=(unsigned char *)s->init_buf->data;
		p= &(d[DTLS1_HM_HEADER_LENGTH]);
		for (i=0; r[i] != NULL; i++)
			{
			s2n(nr[i],p);
			BN_bn2bin(r[i],p);
			p+=nr[i];
			}
#ifndef OPENSSL_NO_ECDH
		if (type & SSL_kEECDH)
			{
			*p = NAMED_CURVE_TYPE;
			p += 1;
			*p = 0;
			p += 1;
			*p = curve_id;
			p += 1;
			*p = encodedlen;
			p += 1;
			memcpy((unsigned char*)p,
			    (unsigned char *)encodedPoint,
			    encodedlen);
			OPENSSL_free(encodedPoint);
			p += encodedlen;
			}
#endif
#ifndef OPENSSL_NO_PSK
		if (type & SSL_kPSK)
			{
			s2n(strlen(s->ctx->psk_identity_hint), p);
			strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
			p+=strlen(s->ctx->psk_identity_hint);
			}
#endif
		if (pkey != NULL)
			{
#ifndef OPENSSL_NO_RSA
			if (pkey->type == EVP_PKEY_RSA)
				{
				q=md_buf;
				j=0;
				for (num=2; num > 0; num--)
					{
					EVP_DigestInit_ex(&md_ctx,(num == 2)
						?s->ctx->md5:s->ctx->sha1, NULL);
					EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
					EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
					EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
					EVP_DigestFinal_ex(&md_ctx,q,
						(unsigned int *)&i);
					q+=i;
					j+=i;
					}
				if (RSA_sign(NID_md5_sha1, md_buf, j,
					&(p[2]), &u, pkey->pkey.rsa) <= 0)
					{
					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
					goto err;
					}
				s2n(u,p);
				n+=u+2;
				}
			else
#endif
#if !defined(OPENSSL_NO_DSA)
				if (pkey->type == EVP_PKEY_DSA)
				{
				EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
				EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
				if (!EVP_SignFinal(&md_ctx,&(p[2]),
					(unsigned int *)&i,pkey))
					{
					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
					goto err;
					}
				s2n(i,p);
				n+=i+2;
				}
			else
#endif
#if !defined(OPENSSL_NO_ECDSA)
				if (pkey->type == EVP_PKEY_EC)
				{
				EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
				EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
				if (!EVP_SignFinal(&md_ctx,&(p[2]),
					(unsigned int *)&i,pkey))
					{
					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
					goto err;
					}
				s2n(i,p);
				n+=i+2;
				}
			else
#endif
				{
				al=SSL_AD_HANDSHAKE_FAILURE;
				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
				goto f_err;
				}
			}
		d = dtls1_set_message_header(s, d,
			SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
		s->init_num=n+DTLS1_HM_HEADER_LENGTH;
		s->init_off=0;
		dtls1_buffer_message(s, 0);
		}
	s->state = SSL3_ST_SW_KEY_EXCH_B;
	EVP_MD_CTX_cleanup(&md_ctx);
	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
f_err:
	ssl3_send_alert(s,SSL3_AL_FATAL,al);
err:
#ifndef OPENSSL_NO_ECDH
	if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
	BN_CTX_free(bn_ctx);
#endif
	EVP_MD_CTX_cleanup(&md_ctx);
	return(-1);
	}
['int dtls1_send_server_key_exchange(SSL *s)\n\t{\n#ifndef OPENSSL_NO_RSA\n\tunsigned char *q;\n\tint j,num;\n\tRSA *rsa;\n\tunsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];\n\tunsigned int u;\n#endif\n#ifndef OPENSSL_NO_DH\n\tDH *dh=NULL,*dhp;\n#endif\n#ifndef OPENSSL_NO_ECDH\n\tEC_KEY *ecdh=NULL, *ecdhp;\n\tunsigned char *encodedPoint = NULL;\n\tint encodedlen = 0;\n\tint curve_id = 0;\n\tBN_CTX *bn_ctx = NULL;\n#endif\n\tEVP_PKEY *pkey;\n\tunsigned char *p,*d;\n\tint al,i;\n\tunsigned long type;\n\tint n;\n\tCERT *cert;\n\tBIGNUM *r[4];\n\tint nr[4],kn;\n\tBUF_MEM *buf;\n\tEVP_MD_CTX md_ctx;\n\tEVP_MD_CTX_init(&md_ctx);\n\tif (s->state == SSL3_ST_SW_KEY_EXCH_A)\n\t\t{\n\t\ttype=s->s3->tmp.new_cipher->algorithm_mkey;\n\t\tcert=s->cert;\n\t\tbuf=s->init_buf;\n\t\tr[0]=r[1]=r[2]=r[3]=NULL;\n\t\tn=0;\n#ifndef OPENSSL_NO_RSA\n\t\tif (type & SSL_kRSA)\n\t\t\t{\n\t\t\trsa=cert->rsa_tmp;\n\t\t\tif ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))\n\t\t\t\t{\n\t\t\t\trsa=s->cert->rsa_tmp_cb(s,\n\t\t\t\t      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),\n\t\t\t\t      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));\n\t\t\t\tif(rsa == NULL)\n\t\t\t\t{\n\t\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);\n\t\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t\tRSA_up_ref(rsa);\n\t\t\t\tcert->rsa_tmp=rsa;\n\t\t\t\t}\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_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tr[0]=rsa->n;\n\t\t\tr[1]=rsa->e;\n\t\t\ts->s3->tmp.use_rsa_tmp=1;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\t\tif (type & SSL_kEDH)\n\t\t\t{\n\t\t\tdhp=cert->dh_tmp;\n\t\t\tif ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))\n\t\t\t\tdhp=s->cert->dh_tmp_cb(s,\n\t\t\t\t      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),\n\t\t\t\t      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));\n\t\t\tif (dhp == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif (s->s3->tmp.dh != NULL)\n\t\t\t\t{\n\t\t\t\tDH_free(dh);\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((dh=DHparams_dup(dhp)) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ts->s3->tmp.dh=dh;\n\t\t\tif ((dhp->pub_key == NULL ||\n\t\t\t     dhp->priv_key == NULL ||\n\t\t\t     (s->options & SSL_OP_SINGLE_DH_USE)))\n\t\t\t\t{\n\t\t\t\tif(!DH_generate_key(dh))\n\t\t\t\t    {\n\t\t\t\t    SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,\n\t\t\t\t\t   ERR_R_DH_LIB);\n\t\t\t\t    goto err;\n\t\t\t\t    }\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tdh->pub_key=BN_dup(dhp->pub_key);\n\t\t\t\tdh->priv_key=BN_dup(dhp->priv_key);\n\t\t\t\tif ((dh->pub_key == NULL) ||\n\t\t\t\t\t(dh->priv_key == NULL))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tr[0]=dh->p;\n\t\t\tr[1]=dh->g;\n\t\t\tr[2]=dh->pub_key;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\tif (type & SSL_kEECDH)\n\t\t\t{\n\t\t\tconst EC_GROUP *group;\n\t\t\tecdhp=cert->ecdh_tmp;\n\t\t\tif ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))\n\t\t\t\t{\n\t\t\t\tecdhp=s->cert->ecdh_tmp_cb(s,\n\t\t\t\t      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),\n\t\t\t\t      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));\n\t\t\t\t}\n\t\t\tif (ecdhp == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif (s->s3->tmp.ecdh != NULL)\n\t\t\t\t{\n\t\t\t\tEC_KEY_free(s->s3->tmp.ecdh);\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (ecdhp == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((ecdh = EC_KEY_dup(ecdhp)) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ts->s3->tmp.ecdh=ecdh;\n\t\t\tif ((EC_KEY_get0_public_key(ecdh) == NULL) ||\n\t\t\t    (EC_KEY_get0_private_key(ecdh) == NULL) ||\n\t\t\t    (s->options & SSL_OP_SINGLE_ECDH_USE))\n\t\t\t\t{\n\t\t\t\tif(!EC_KEY_generate_key(ecdh))\n\t\t\t\t    {\n\t\t\t\t    SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);\n\t\t\t\t    goto err;\n\t\t\t\t    }\n\t\t\t\t}\n\t\t\tif (((group = EC_KEY_get0_group(ecdh)) == NULL) ||\n\t\t\t    (EC_KEY_get0_public_key(ecdh)  == NULL) ||\n\t\t\t    (EC_KEY_get0_private_key(ecdh) == NULL))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&\n\t\t\t    (EC_GROUP_get_degree(group) > 163))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((curve_id =\n\t\t\t    tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))\n\t\t\t    == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tencodedlen = EC_POINT_point2oct(group,\n\t\t\t    EC_KEY_get0_public_key(ecdh),\n\t\t\t    POINT_CONVERSION_UNCOMPRESSED,\n\t\t\t    NULL, 0, NULL);\n\t\t\tencodedPoint = (unsigned char *)\n\t\t\t    OPENSSL_malloc(encodedlen*sizeof(unsigned char));\n\t\t\tbn_ctx = BN_CTX_new();\n\t\t\tif ((encodedPoint == NULL) || (bn_ctx == NULL))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tencodedlen = EC_POINT_point2oct(group,\n\t\t\t    EC_KEY_get0_public_key(ecdh),\n\t\t\t    POINT_CONVERSION_UNCOMPRESSED,\n\t\t\t    encodedPoint, encodedlen, bn_ctx);\n\t\t\tif (encodedlen == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tBN_CTX_free(bn_ctx);  bn_ctx=NULL;\n\t\t\tn = 4 + encodedlen;\n\t\t\tr[0]=NULL;\n\t\t\tr[1]=NULL;\n\t\t\tr[2]=NULL;\n\t\t\tr[3]=NULL;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_PSK\n\t\t\tif (type & SSL_kPSK)\n\t\t\t\t{\n\t\t\t\tn+=2+strlen(s->ctx->psk_identity_hint);\n\t\t\t\t}\n\t\t\telse\n#endif\n\t\t\t{\n\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tfor (i=0; r[i] != NULL; i++)\n\t\t\t{\n\t\t\tnr[i]=BN_num_bytes(r[i]);\n\t\t\tn+=2+nr[i];\n\t\t\t}\n\t\tif (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)\n\t\t\t&& !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))\n\t\t\t{\n\t\t\tif ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher, NULL))\n\t\t\t\t== NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tkn=EVP_PKEY_size(pkey);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey=NULL;\n\t\t\tkn=0;\n\t\t\t}\n\t\tif (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);\n\t\t\tgoto err;\n\t\t\t}\n\t\td=(unsigned char *)s->init_buf->data;\n\t\tp= &(d[DTLS1_HM_HEADER_LENGTH]);\n\t\tfor (i=0; r[i] != NULL; i++)\n\t\t\t{\n\t\t\ts2n(nr[i],p);\n\t\t\tBN_bn2bin(r[i],p);\n\t\t\tp+=nr[i];\n\t\t\t}\n#ifndef OPENSSL_NO_ECDH\n\t\tif (type & SSL_kEECDH)\n\t\t\t{\n\t\t\t*p = NAMED_CURVE_TYPE;\n\t\t\tp += 1;\n\t\t\t*p = 0;\n\t\t\tp += 1;\n\t\t\t*p = curve_id;\n\t\t\tp += 1;\n\t\t\t*p = encodedlen;\n\t\t\tp += 1;\n\t\t\tmemcpy((unsigned char*)p,\n\t\t\t    (unsigned char *)encodedPoint,\n\t\t\t    encodedlen);\n\t\t\tOPENSSL_free(encodedPoint);\n\t\t\tp += encodedlen;\n\t\t\t}\n#endif\n#ifndef OPENSSL_NO_PSK\n\t\tif (type & SSL_kPSK)\n\t\t\t{\n\t\t\ts2n(strlen(s->ctx->psk_identity_hint), p);\n\t\t\tstrncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));\n\t\t\tp+=strlen(s->ctx->psk_identity_hint);\n\t\t\t}\n#endif\n\t\tif (pkey != NULL)\n\t\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t\t{\n\t\t\t\tq=md_buf;\n\t\t\t\tj=0;\n\t\t\t\tfor (num=2; num > 0; num--)\n\t\t\t\t\t{\n\t\t\t\t\tEVP_DigestInit_ex(&md_ctx,(num == 2)\n\t\t\t\t\t\t?s->ctx->md5:s->ctx->sha1, NULL);\n\t\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\t\tEVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);\n\t\t\t\t\tEVP_DigestFinal_ex(&md_ctx,q,\n\t\t\t\t\t\t(unsigned int *)&i);\n\t\t\t\t\tq+=i;\n\t\t\t\t\tj+=i;\n\t\t\t\t\t}\n\t\t\t\tif (RSA_sign(NID_md5_sha1, md_buf, j,\n\t\t\t\t\t&(p[2]), &u, pkey->pkey.rsa) <= 0)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ts2n(u,p);\n\t\t\t\tn+=u+2;\n\t\t\t\t}\n\t\t\telse\n#endif\n#if !defined(OPENSSL_NO_DSA)\n\t\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t\t{\n\t\t\t\tEVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);\n\t\t\t\tif (!EVP_SignFinal(&md_ctx,&(p[2]),\n\t\t\t\t\t(unsigned int *)&i,pkey))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ts2n(i,p);\n\t\t\t\tn+=i+2;\n\t\t\t\t}\n\t\t\telse\n#endif\n#if !defined(OPENSSL_NO_ECDSA)\n\t\t\t\tif (pkey->type == EVP_PKEY_EC)\n\t\t\t\t{\n\t\t\t\tEVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);\n\t\t\t\tif (!EVP_SignFinal(&md_ctx,&(p[2]),\n\t\t\t\t\t(unsigned int *)&i,pkey))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ts2n(i,p);\n\t\t\t\tn+=i+2;\n\t\t\t\t}\n\t\t\telse\n#endif\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\td = dtls1_set_message_header(s, d,\n\t\t\tSSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);\n\t\ts->init_num=n+DTLS1_HM_HEADER_LENGTH;\n\t\ts->init_off=0;\n\t\tdtls1_buffer_message(s, 0);\n\t\t}\n\ts->state = SSL3_ST_SW_KEY_EXCH_B;\n\tEVP_MD_CTX_cleanup(&md_ctx);\n\treturn(dtls1_do_write(s,SSL3_RT_HANDSHAKE));\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n#ifndef OPENSSL_NO_ECDH\n\tif (encodedPoint != NULL) OPENSSL_free(encodedPoint);\n\tBN_CTX_free(bn_ctx);\n#endif\n\tEVP_MD_CTX_cleanup(&md_ctx);\n\treturn(-1);\n\t}', "void EVP_MD_CTX_init(EVP_MD_CTX *ctx)\n\t{\n\tmemset(ctx,'\\0',sizeof *ctx);\n\t}"]