static int client_master_key(SSL *s)
	{
	unsigned char *buf;
	unsigned char *p,*d;
	int clear,enc,karg,i;
	SSL_SESSION *sess;
	const EVP_CIPHER *c;
	const EVP_MD *md;
	buf=(unsigned char *)s->init_buf->data;
	if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
		{
		if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
			{
			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
			return(-1);
			}
		sess=s->session;
		p=buf;
		d=p+10;
		*(p++)=SSL2_MT_CLIENT_MASTER_KEY;
		i=ssl_put_cipher_by_char(s,sess->cipher,p);
		p+=i;
		i=EVP_CIPHER_iv_length(c);
		sess->key_arg_length=i;
		if (i > SSL_MAX_KEY_ARG_LENGTH)
			{
			ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
			SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
			return -1;
			}
		if (i > 0)
			if (RAND_pseudo_bytes(sess->key_arg,i) <= 0)
				return -1;
		i=EVP_CIPHER_key_length(c);
		sess->master_key_length=i;
		if (i > 0)
			{
			if (i > (int)sizeof(sess->master_key))
				{
				ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
				SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
				return -1;
				}
			if (RAND_bytes(sess->master_key,i) <= 0)
				{
				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
				return(-1);
				}
			}
		if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
			enc=8;
		else if (SSL_C_IS_EXPORT(sess->cipher))
			enc=5;
		else
			enc=i;
		if ((int)i < enc)
			{
			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);
			return(-1);
			}
		clear=i-enc;
		s2n(clear,p);
		memcpy(d,sess->master_key,(unsigned int)clear);
		d+=clear;
		enc=ssl_rsa_public_encrypt(sess->sess_cert,enc,
			&(sess->master_key[clear]),d,
			(s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
		if (enc <= 0)
			{
			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
			return(-1);
			}
#ifdef PKCS1_CHECK
		if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;
		if (s->options & SSL_OP_PKCS1_CHECK_2)
			sess->master_key[clear]++;
#endif
		s2n(enc,p);
		d+=enc;
		karg=sess->key_arg_length;
		s2n(karg,p);
		if (karg > (int)sizeof(sess->key_arg))
			{
			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
			SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
			return -1;
			}
		memcpy(d,sess->key_arg,(unsigned int)karg);
		d+=karg;
		s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
		s->init_num=d-buf;
		s->init_off=0;
		}
	return(ssl2_do_write(s));
	}
['static int client_master_key(SSL *s)\n\t{\n\tunsigned char *buf;\n\tunsigned char *p,*d;\n\tint clear,enc,karg,i;\n\tSSL_SESSION *sess;\n\tconst EVP_CIPHER *c;\n\tconst EVP_MD *md;\n\tbuf=(unsigned char *)s->init_buf->data;\n\tif (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)\n\t\t{\n\t\tif (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_NO_CIPHER);\n\t\t\tSSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);\n\t\t\treturn(-1);\n\t\t\t}\n\t\tsess=s->session;\n\t\tp=buf;\n\t\td=p+10;\n\t\t*(p++)=SSL2_MT_CLIENT_MASTER_KEY;\n\t\ti=ssl_put_cipher_by_char(s,sess->cipher,p);\n\t\tp+=i;\n\t\ti=EVP_CIPHER_iv_length(c);\n\t\tsess->key_arg_length=i;\n\t\tif (i > SSL_MAX_KEY_ARG_LENGTH)\n\t\t\t{\n\t\t\tssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);\n\t\t\treturn -1;\n\t\t\t}\n\t\tif (i > 0)\n\t\t\tif (RAND_pseudo_bytes(sess->key_arg,i) <= 0)\n\t\t\t\treturn -1;\n\t\ti=EVP_CIPHER_key_length(c);\n\t\tsess->master_key_length=i;\n\t\tif (i > 0)\n\t\t\t{\n\t\t\tif (i > (int)sizeof(sess->master_key))\n\t\t\t\t{\n\t\t\t\tssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);\n\t\t\t\tSSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);\n\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\tif (RAND_bytes(sess->master_key,i) <= 0)\n\t\t\t\t{\n\t\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\t}\n\t\tif (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)\n\t\t\tenc=8;\n\t\telse if (SSL_C_IS_EXPORT(sess->cipher))\n\t\t\tenc=5;\n\t\telse\n\t\t\tenc=i;\n\t\tif ((int)i < enc)\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);\n\t\t\treturn(-1);\n\t\t\t}\n\t\tclear=i-enc;\n\t\ts2n(clear,p);\n\t\tmemcpy(d,sess->master_key,(unsigned int)clear);\n\t\td+=clear;\n\t\tenc=ssl_rsa_public_encrypt(sess->sess_cert,enc,\n\t\t\t&(sess->master_key[clear]),d,\n\t\t\t(s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);\n\t\tif (enc <= 0)\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);\n\t\t\treturn(-1);\n\t\t\t}\n#ifdef PKCS1_CHECK\n\t\tif (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;\n\t\tif (s->options & SSL_OP_PKCS1_CHECK_2)\n\t\t\tsess->master_key[clear]++;\n#endif\n\t\ts2n(enc,p);\n\t\td+=enc;\n\t\tkarg=sess->key_arg_length;\n\t\ts2n(karg,p);\n\t\tif (karg > (int)sizeof(sess->key_arg))\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);\n\t\t\treturn -1;\n\t\t\t}\n\t\tmemcpy(d,sess->key_arg,(unsigned int)karg);\n\t\td+=karg;\n\t\ts->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;\n\t\ts->init_num=d-buf;\n\t\ts->init_off=0;\n\t\t}\n\treturn(ssl2_do_write(s));\n\t}']