int tls1_export_keying_material(SSL *s, unsigned char *out, unsigned int olen,
         const char *label, unsigned int llen, const unsigned char *context,
         unsigned int contextlen, int use_context)
	{
	unsigned char *buff;
	unsigned char *val;
	unsigned int vallen, currentvalpos, rv;
#ifdef KSSL_DEBUG
	printf ("tls1_export_keying_material(%p, %p,%d, %s,%d, %p,%d)\n", s, out,olen, label,llen, p,plen);
#endif
	buff = OPENSSL_malloc(olen);
	if (buff == NULL) goto err2;
	vallen = llen + SSL3_RANDOM_SIZE * 2;
        if (use_context)
                {
                vallen +=  2 + contextlen;
                }
	val = OPENSSL_malloc(vallen);
	if (val == NULL) goto err2;
	currentvalpos = 0;
	memcpy(val + currentvalpos, (unsigned char *) label, llen);
	currentvalpos += llen;
	memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
	currentvalpos += SSL3_RANDOM_SIZE;
	memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
	currentvalpos += SSL3_RANDOM_SIZE;
        if (use_context)
                {
                val[currentvalpos] = (contextlen << 8) & 0xff;
                currentvalpos++;
                val[currentvalpos] = contextlen & 0xff;
                currentvalpos++;
                if ((contextlen > 0) || (context != NULL))
                        {
                        memcpy(val + currentvalpos, context, contextlen);
                        }
                }
	if (bcmp(val, TLS_MD_CLIENT_FINISH_CONST,
		 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0) goto err1;
	if (bcmp(val, TLS_MD_SERVER_FINISH_CONST,
		 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0) goto err1;
	if (bcmp(val, TLS_MD_MASTER_SECRET_CONST,
		 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) goto err1;
	if (bcmp(val, TLS_MD_KEY_EXPANSION_CONST,
		 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) goto err1;
	tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
		val, vallen,
		NULL, 0,
		NULL, 0,
		NULL, 0,
		NULL, 0,
		s->session->master_key,s->session->master_key_length,
		out,buff,olen);
#ifdef KSSL_DEBUG
	printf ("tls1_export_keying_material() complete\n");
#endif
	rv = olen;
	goto ret;
err1:
	SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
	rv = 0;
	goto ret;
err2:
	SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
	rv = 0;
ret:
	if (buff != NULL) OPENSSL_free(buff);
	if (val != NULL) OPENSSL_free(val);
	return(rv);
	}
['int tls1_export_keying_material(SSL *s, unsigned char *out, unsigned int olen,\n         const char *label, unsigned int llen, const unsigned char *context,\n         unsigned int contextlen, int use_context)\n\t{\n\tunsigned char *buff;\n\tunsigned char *val;\n\tunsigned int vallen, currentvalpos, rv;\n#ifdef KSSL_DEBUG\n\tprintf ("tls1_export_keying_material(%p, %p,%d, %s,%d, %p,%d)\\n", s, out,olen, label,llen, p,plen);\n#endif\n\tbuff = OPENSSL_malloc(olen);\n\tif (buff == NULL) goto err2;\n\tvallen = llen + SSL3_RANDOM_SIZE * 2;\n        if (use_context)\n                {\n                vallen +=  2 + contextlen;\n                }\n\tval = OPENSSL_malloc(vallen);\n\tif (val == NULL) goto err2;\n\tcurrentvalpos = 0;\n\tmemcpy(val + currentvalpos, (unsigned char *) label, llen);\n\tcurrentvalpos += llen;\n\tmemcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);\n\tcurrentvalpos += SSL3_RANDOM_SIZE;\n\tmemcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);\n\tcurrentvalpos += SSL3_RANDOM_SIZE;\n        if (use_context)\n                {\n                val[currentvalpos] = (contextlen << 8) & 0xff;\n                currentvalpos++;\n                val[currentvalpos] = contextlen & 0xff;\n                currentvalpos++;\n                if ((contextlen > 0) || (context != NULL))\n                        {\n                        memcpy(val + currentvalpos, context, contextlen);\n                        }\n                }\n\tif (bcmp(val, TLS_MD_CLIENT_FINISH_CONST,\n\t\t TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0) goto err1;\n\tif (bcmp(val, TLS_MD_SERVER_FINISH_CONST,\n\t\t TLS_MD_SERVER_FINISH_CONST_SIZE) == 0) goto err1;\n\tif (bcmp(val, TLS_MD_MASTER_SECRET_CONST,\n\t\t TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) goto err1;\n\tif (bcmp(val, TLS_MD_KEY_EXPANSION_CONST,\n\t\t TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) goto err1;\n\ttls1_PRF(s->s3->tmp.new_cipher->algorithm2,\n\t\tval, vallen,\n\t\tNULL, 0,\n\t\tNULL, 0,\n\t\tNULL, 0,\n\t\tNULL, 0,\n\t\ts->session->master_key,s->session->master_key_length,\n\t\tout,buff,olen);\n#ifdef KSSL_DEBUG\n\tprintf ("tls1_export_keying_material() complete\\n");\n#endif\n\trv = olen;\n\tgoto ret;\nerr1:\n\tSSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);\n\trv = 0;\n\tgoto ret;\nerr2:\n\tSSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);\n\trv = 0;\nret:\n\tif (buff != NULL) OPENSSL_free(buff);\n\tif (val != NULL) OPENSSL_free(val);\n\treturn(rv);\n\t}']