int nla_client_begin(rdpNla* nla)
{
	int rc = -1;
	SecBuffer outputBuffer = { 0 };
	SecBufferDesc outputBufferDesc = { 0 };

	WINPR_ASSERT(nla);

	if (nla_client_init(nla) < 1)
		goto fail;

	if (nla_get_state(nla) != NLA_STATE_INITIAL)
		goto fail;

	outputBufferDesc.ulVersion = SECBUFFER_VERSION;
	outputBufferDesc.cBuffers = 1;
	outputBufferDesc.pBuffers = &outputBuffer;

	if (!nla_sec_buffer_alloc(&outputBuffer, nla->cbMaxToken))
		goto fail;

	nla->status = nla_initialize_security_context(nla, TRUE, NULL, &outputBufferDesc);
	switch (nla->status)
	{
		case SEC_E_OK: /* Complete, send output token if >0 */
			if (!nla_client_send_token(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_FINAL);
			break;
		case SEC_I_COMPLETE_AND_CONTINUE: /* CompleteAuthToken -> server ->
		                                     InitializeSecurityContext again */
			if (!nla_complete_auth(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_NEGO_TOKEN);
			break;
		case SEC_I_COMPLETE_NEEDED: /* CompleteAuthToken -> finish */
			if (!nla_complete_auth(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_FINAL);
			break;
		case SEC_I_CONTINUE_NEEDED: /* out token -> server -> InitializeSecurityContext again */
			if (!nla_client_send_token(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_NEGO_TOKEN);
			break;
		case SEC_E_NO_CREDENTIALS:
		case SEC_I_INCOMPLETE_CREDENTIALS:
		case SEC_E_INCOMPLETE_MESSAGE:
		default:
			goto fail;
	}

	rc = 1;
fail:
	sspi_SecBufferFree(&outputBuffer);
	return rc;
}
int nla_client_begin(rdpNla* nla)
{
	int rc = -1;
	SecBuffer outputBuffer = { 0 };
	SecBufferDesc outputBufferDesc = { 0 };

	WINPR_ASSERT(nla);

	if (nla_client_init(nla) < 1)
		goto fail;

	if (nla_get_state(nla) != NLA_STATE_INITIAL)
		goto fail;

	outputBufferDesc.ulVersion = SECBUFFER_VERSION;
	outputBufferDesc.cBuffers = 1;
	outputBufferDesc.pBuffers = &outputBuffer;

	if (!nla_sec_buffer_alloc(&outputBuffer, nla->cbMaxToken))
		goto fail;

	nla->status = nla_initialize_security_context(nla, TRUE, NULL, &outputBufferDesc);
	switch (nla->status)
	{
		case SEC_E_OK:  
			if (!nla_client_send_token(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_FINAL);
			break;
		case SEC_I_COMPLETE_AND_CONTINUE:  
			if (!nla_complete_auth(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_NEGO_TOKEN);
			break;
		case SEC_I_COMPLETE_NEEDED:  
			if (!nla_complete_auth(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_FINAL);
			break;
		case SEC_I_CONTINUE_NEEDED:  
			if (!nla_client_send_token(nla, &outputBufferDesc))
				goto fail;
			nla_set_state(nla, NLA_STATE_NEGO_TOKEN);
			break;
		case SEC_E_NO_CREDENTIALS:
		case SEC_I_INCOMPLETE_CREDENTIALS:
		case SEC_E_INCOMPLETE_MESSAGE:
		default:
			goto fail;
	}

	rc = 1;
fail:
	sspi_SecBufferFree(&outputBuffer);
	return rc;
}