int ssl_get_new_session(SSL *s, int session)
	{
	unsigned int tmp;
	SSL_SESSION *ss=NULL;
	GEN_SESSION_CB cb = def_generate_session_id;
	if ((ss=SSL_SESSION_new()) == NULL) return(0);
	if (s->ctx->session_timeout == 0)
		ss->timeout=SSL_get_default_timeout(s);
	else
		ss->timeout=s->ctx->session_timeout;
	if (s->session != NULL)
		{
		SSL_SESSION_free(s->session);
		s->session=NULL;
		}
	if (session)
		{
		if (s->version == SSL2_VERSION)
			{
			ss->ssl_version=SSL2_VERSION;
			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
			}
		else if (s->version == SSL3_VERSION)
			{
			ss->ssl_version=SSL3_VERSION;
			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
			}
		else if (s->version == TLS1_VERSION)
			{
			ss->ssl_version=TLS1_VERSION;
			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
			}
		else
			{
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
			SSL_SESSION_free(ss);
			return(0);
			}
		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
		if(s->generate_session_id)
			cb = s->generate_session_id;
		else if(s->ctx->generate_session_id)
			cb = s->ctx->generate_session_id;
		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
		tmp = ss->session_id_length;
		if(!cb(s, ss->session_id, &tmp))
			{
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
			SSL_SESSION_free(ss);
			return(0);
			}
		if(!tmp || (tmp > ss->session_id_length))
			{
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
			SSL_SESSION_free(ss);
			return(0);
			}
		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
		else
			ss->session_id_length = tmp;
		if(SSL_CTX_has_matching_session_id(s->ctx, ss->session_id,
						ss->session_id_length))
			{
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
				SSL_R_SSL_SESSION_ID_CONFLICT);
			SSL_SESSION_free(ss);
			return(0);
			}
		}
	else
		{
		ss->session_id_length=0;
		}
	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
	ss->sid_ctx_length=s->sid_ctx_length;
	s->session=ss;
	ss->ssl_version=s->version;
	ss->verify_result = X509_V_OK;
	return(1);
	}
['int ssl_get_new_session(SSL *s, int session)\n\t{\n\tunsigned int tmp;\n\tSSL_SESSION *ss=NULL;\n\tGEN_SESSION_CB cb = def_generate_session_id;\n\tif ((ss=SSL_SESSION_new()) == NULL) return(0);\n\tif (s->ctx->session_timeout == 0)\n\t\tss->timeout=SSL_get_default_timeout(s);\n\telse\n\t\tss->timeout=s->ctx->session_timeout;\n\tif (s->session != NULL)\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\tif (session)\n\t\t{\n\t\tif (s->version == SSL2_VERSION)\n\t\t\t{\n\t\t\tss->ssl_version=SSL2_VERSION;\n\t\t\tss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;\n\t\t\t}\n\t\telse if (s->version == SSL3_VERSION)\n\t\t\t{\n\t\t\tss->ssl_version=SSL3_VERSION;\n\t\t\tss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;\n\t\t\t}\n\t\telse if (s->version == TLS1_VERSION)\n\t\t\t{\n\t\t\tss->ssl_version=TLS1_VERSION;\n\t\t\tss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);\n\t\t\tSSL_SESSION_free(ss);\n\t\t\treturn(0);\n\t\t\t}\n\t\tCRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tif(s->generate_session_id)\n\t\t\tcb = s->generate_session_id;\n\t\telse if(s->ctx->generate_session_id)\n\t\t\tcb = s->ctx->generate_session_id;\n\t\tCRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\ttmp = ss->session_id_length;\n\t\tif(!cb(s, ss->session_id, &tmp))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_GET_NEW_SESSION,\n\t\t\t\tSSL_R_SSL_SESSION_ID_CALLBACK_FAILED);\n\t\t\tSSL_SESSION_free(ss);\n\t\t\treturn(0);\n\t\t\t}\n\t\tif(!tmp || (tmp > ss->session_id_length))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_GET_NEW_SESSION,\n\t\t\t\tSSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);\n\t\t\tSSL_SESSION_free(ss);\n\t\t\treturn(0);\n\t\t\t}\n\t\tif((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))\n\t\t\tmemset(ss->session_id + tmp, 0, ss->session_id_length - tmp);\n\t\telse\n\t\t\tss->session_id_length = tmp;\n\t\tif(SSL_CTX_has_matching_session_id(s->ctx, ss->session_id,\n\t\t\t\t\t\tss->session_id_length))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_GET_NEW_SESSION,\n\t\t\t\tSSL_R_SSL_SESSION_ID_CONFLICT);\n\t\t\tSSL_SESSION_free(ss);\n\t\t\treturn(0);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tss->session_id_length=0;\n\t\t}\n\tmemcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);\n\tss->sid_ctx_length=s->sid_ctx_length;\n\ts->session=ss;\n\tss->ssl_version=s->version;\n\tss->verify_result = X509_V_OK;\n\treturn(1);\n\t}']