2011-07-01 02:48:48 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* RDP Protocol Security Negotiation
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <freerdp/constants.h>
|
2011-07-01 02:48:48 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
#include "tpkt.h"
|
|
|
|
|
2011-07-01 02:48:48 +04:00
|
|
|
#include "nego.h"
|
|
|
|
|
2011-07-10 09:48:10 +04:00
|
|
|
char NEGO_STATE_STRINGS[6][25] =
|
|
|
|
{
|
|
|
|
"NEGO_STATE_INITIAL",
|
|
|
|
"NEGO_STATE_NLA",
|
|
|
|
"NEGO_STATE_TLS",
|
|
|
|
"NEGO_STATE_RDP",
|
|
|
|
"NEGO_STATE_FAIL",
|
|
|
|
"NEGO_STATE_FINAL"
|
|
|
|
};
|
|
|
|
|
|
|
|
char PROTOCOL_SECURITY_STRINGS[3][4] =
|
|
|
|
{
|
|
|
|
"RDP",
|
|
|
|
"TLS",
|
|
|
|
"NLA"
|
|
|
|
};
|
|
|
|
|
2011-07-01 02:48:48 +04:00
|
|
|
/**
|
|
|
|
* Negotiate protocol security and connect.
|
|
|
|
* @param nego
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2011-07-18 07:16:31 +04:00
|
|
|
boolean nego_connect(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
if (nego->state == NEGO_STATE_INITIAL)
|
|
|
|
{
|
|
|
|
if (nego->enabled_protocols[PROTOCOL_NLA] > 0)
|
|
|
|
nego->state = NEGO_STATE_NLA;
|
|
|
|
else if (nego->enabled_protocols[PROTOCOL_TLS] > 0)
|
|
|
|
nego->state = NEGO_STATE_TLS;
|
|
|
|
else if (nego->enabled_protocols[PROTOCOL_RDP] > 0)
|
|
|
|
nego->state = NEGO_STATE_RDP;
|
|
|
|
else
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
do
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("state: %s", NEGO_STATE_STRINGS[nego->state]);
|
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
nego_send(nego);
|
|
|
|
|
2011-07-01 02:48:48 +04:00
|
|
|
if (nego->state == NEGO_STATE_FAIL)
|
|
|
|
{
|
2011-07-07 21:37:48 +04:00
|
|
|
DEBUG_NEGO("Protocol Security Negotiation Failure");
|
2011-07-01 02:48:48 +04:00
|
|
|
nego->state = NEGO_STATE_FINAL;
|
2011-07-18 07:16:31 +04:00
|
|
|
return False;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
}
|
2011-07-07 21:37:48 +04:00
|
|
|
while (nego->state != NEGO_STATE_FINAL);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
DEBUG_NEGO("Negotiated %s security", PROTOCOL_SECURITY_STRINGS[nego->selected_protocol]);
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
/* update settings with negotiated protocol security */
|
2011-08-19 19:56:47 +04:00
|
|
|
nego->transport->settings->requested_protocols = nego->requested_protocols;
|
2011-07-07 21:37:48 +04:00
|
|
|
nego->transport->settings->selected_protocol = nego->selected_protocol;
|
|
|
|
|
2011-07-18 07:16:31 +04:00
|
|
|
return True;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect TCP layer.
|
|
|
|
* @param nego
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2011-08-13 08:40:14 +04:00
|
|
|
boolean nego_tcp_connect(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
if (nego->tcp_connected == 0)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
if (transport_connect(nego->transport, nego->hostname, nego->port) == False)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
nego->tcp_connected = 0;
|
2011-08-13 08:40:14 +04:00
|
|
|
return False;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nego->tcp_connected = 1;
|
2011-08-13 08:40:14 +04:00
|
|
|
return True;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 08:40:14 +04:00
|
|
|
return True;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect TCP layer.
|
|
|
|
* @param nego
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
int nego_tcp_disconnect(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
if (nego->tcp_connected)
|
2011-07-03 20:42:35 +04:00
|
|
|
transport_disconnect(nego->transport);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
|
|
|
nego->tcp_connected = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt negotiating NLA + TLS security.
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_attempt_nla(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
nego->requested_protocols = PROTOCOL_NLA | PROTOCOL_TLS;
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Attempting NLA security");
|
|
|
|
|
2011-08-13 08:40:14 +04:00
|
|
|
if (!nego_tcp_connect(nego))
|
|
|
|
{
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
nego_send_negotiation_request(nego);
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
nego_recv_response(nego);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
|
|
|
if (nego->state != NEGO_STATE_FINAL)
|
|
|
|
{
|
|
|
|
nego_tcp_disconnect(nego);
|
|
|
|
|
|
|
|
if (nego->enabled_protocols[PROTOCOL_TLS] > 0)
|
|
|
|
nego->state = NEGO_STATE_TLS;
|
|
|
|
else if (nego->enabled_protocols[PROTOCOL_RDP] > 0)
|
|
|
|
nego->state = NEGO_STATE_RDP;
|
|
|
|
else
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt negotiating TLS security.
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_attempt_tls(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
nego->requested_protocols = PROTOCOL_TLS;
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Attempting TLS security");
|
|
|
|
|
2011-08-13 08:40:14 +04:00
|
|
|
if (!nego_tcp_connect(nego))
|
|
|
|
{
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
nego_send_negotiation_request(nego);
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
nego_recv_response(nego);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
|
|
|
if (nego->state != NEGO_STATE_FINAL)
|
|
|
|
{
|
|
|
|
nego_tcp_disconnect(nego);
|
|
|
|
|
|
|
|
if (nego->enabled_protocols[PROTOCOL_RDP] > 0)
|
|
|
|
nego->state = NEGO_STATE_RDP;
|
|
|
|
else
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt negotiating standard RDP security.
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_attempt_rdp(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
nego->requested_protocols = PROTOCOL_RDP;
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Attempting RDP security");
|
|
|
|
|
2011-08-13 08:40:14 +04:00
|
|
|
if (!nego_tcp_connect(nego))
|
|
|
|
{
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
nego_send_negotiation_request(nego);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
nego_recv_response(nego);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait to receive a negotiation response
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
|
|
|
void nego_recv_response(rdpNego* nego)
|
|
|
|
{
|
2011-07-10 23:34:43 +04:00
|
|
|
STREAM* s = transport_recv_stream_init(nego->transport, 1024);
|
|
|
|
transport_read(nego->transport, s);
|
|
|
|
nego_recv(nego->transport, s, nego->transport->recv_extra);
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-05 05:35:32 +04:00
|
|
|
* Receive protocol security negotiation message.\n
|
|
|
|
* @msdn{cc240501}
|
|
|
|
* @param transport transport
|
|
|
|
* @param s stream
|
|
|
|
* @param extra nego pointer
|
2011-07-01 02:48:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
int nego_recv(rdpTransport* transport, STREAM* s, void* extra)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
2011-07-03 21:49:06 +04:00
|
|
|
uint8 li;
|
2011-07-01 02:48:48 +04:00
|
|
|
uint8 type;
|
2011-07-07 21:37:48 +04:00
|
|
|
rdpNego* nego = (rdpNego*) extra;
|
2011-07-01 02:48:48 +04:00
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
tpkt_read_header(s);
|
|
|
|
li = tpdu_read_connection_confirm(s);
|
|
|
|
|
|
|
|
if (li > 6)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
2011-07-03 21:49:06 +04:00
|
|
|
/* rdpNegData (optional) */
|
|
|
|
|
|
|
|
stream_read_uint8(s, type); /* Type */
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TYPE_RDP_NEG_RSP:
|
|
|
|
nego_process_negotiation_response(nego, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_RDP_NEG_FAILURE:
|
|
|
|
nego_process_negotiation_failure(nego, s);
|
|
|
|
break;
|
|
|
|
}
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
2011-07-07 21:37:48 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nego->state = NEGO_STATE_FINAL;
|
|
|
|
}
|
2011-07-03 21:49:06 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
/**
|
2011-08-22 11:03:58 +04:00
|
|
|
* Read protocol security negotiation request message.\n
|
2011-08-18 19:15:28 +04:00
|
|
|
* @param nego
|
|
|
|
* @param s stream
|
|
|
|
*/
|
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
boolean nego_read_request(rdpNego* nego, STREAM* s)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
|
|
|
uint8 li;
|
|
|
|
uint8 c;
|
|
|
|
uint8 type;
|
|
|
|
|
|
|
|
tpkt_read_header(s);
|
|
|
|
li = tpdu_read_connection_request(s);
|
|
|
|
if (li != stream_get_left(s) + 6)
|
|
|
|
{
|
|
|
|
printf("Incorrect TPDU length indicator.\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_get_left(s) > 8)
|
|
|
|
{
|
|
|
|
/* Optional routingToken or cookie, ending with CR+LF */
|
|
|
|
while (stream_get_left(s) > 0)
|
|
|
|
{
|
|
|
|
stream_read_uint8(s, c);
|
|
|
|
if (c != '\x0D')
|
|
|
|
continue;
|
|
|
|
stream_peek_uint8(s, c);
|
|
|
|
if (c != '\x0A')
|
|
|
|
continue;
|
|
|
|
|
2011-08-20 14:29:57 +04:00
|
|
|
stream_seek_uint8(s);
|
2011-08-18 19:15:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_get_left(s) >= 8)
|
|
|
|
{
|
|
|
|
/* rdpNegData (optional) */
|
|
|
|
|
|
|
|
stream_read_uint8(s, type); /* Type */
|
|
|
|
if (type != TYPE_RDP_NEG_REQ)
|
|
|
|
{
|
|
|
|
printf("Incorrect negotiation request type %d\n", type);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
nego_process_negotiation_request(nego, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2011-07-01 02:48:48 +04:00
|
|
|
/**
|
|
|
|
* Send protocol security negotiation message.
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_send(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
if (nego->state == NEGO_STATE_NLA)
|
|
|
|
nego_attempt_nla(nego);
|
|
|
|
else if (nego->state == NEGO_STATE_TLS)
|
|
|
|
nego_attempt_tls(nego);
|
|
|
|
else if (nego->state == NEGO_STATE_RDP)
|
|
|
|
nego_attempt_rdp(nego);
|
2011-07-07 21:37:48 +04:00
|
|
|
else
|
|
|
|
DEBUG_NEGO("invalid negotiation state for sending");
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Send RDP Negotiation Request (RDP_NEG_REQ).\n
|
|
|
|
* @msdn{cc240500}\n
|
|
|
|
* @msdn{cc240470}
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_send_negotiation_request(rdpNego* nego)
|
2011-07-03 20:42:35 +04:00
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
int length;
|
|
|
|
uint8 *bm, *em;
|
|
|
|
|
2011-08-25 09:45:43 +04:00
|
|
|
s = transport_send_stream_init(nego->transport, 256);
|
2011-07-12 02:46:36 +04:00
|
|
|
length = TPDU_CONNECTION_REQUEST_LENGTH;
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_get_mark(s, bm);
|
|
|
|
stream_seek(s, length);
|
|
|
|
|
2011-09-04 02:21:21 +04:00
|
|
|
if (nego->routing_token != NULL)
|
|
|
|
{
|
|
|
|
stream_write(s, nego->routing_token->data, nego->routing_token->length);
|
|
|
|
length += nego->routing_token->length;
|
|
|
|
}
|
|
|
|
else
|
2011-07-03 20:42:35 +04:00
|
|
|
{
|
|
|
|
int cookie_length = strlen(nego->cookie);
|
2011-07-06 02:26:12 +04:00
|
|
|
stream_write(s, "Cookie: mstshash=", 17);
|
2011-07-27 03:14:11 +04:00
|
|
|
stream_write(s, (uint8*)nego->cookie, cookie_length);
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_write_uint8(s, 0x0D); /* CR */
|
|
|
|
stream_write_uint8(s, 0x0A); /* LF */
|
|
|
|
length += cookie_length + 19;
|
|
|
|
}
|
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
if (nego->requested_protocols > PROTOCOL_RDP)
|
|
|
|
{
|
|
|
|
/* RDP_NEG_DATA must be present for TLS and NLA */
|
|
|
|
stream_write_uint8(s, TYPE_RDP_NEG_REQ);
|
|
|
|
stream_write_uint8(s, 0); /* flags, must be set to zero */
|
|
|
|
stream_write_uint16(s, 8); /* RDP_NEG_DATA length (8) */
|
|
|
|
stream_write_uint32(s, nego->requested_protocols); /* requestedProtocols */
|
|
|
|
length += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_get_mark(s, em);
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_set_mark(s, bm);
|
|
|
|
tpkt_write_header(s, length);
|
|
|
|
tpdu_write_connection_request(s, length - 5);
|
|
|
|
stream_set_mark(s, em);
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
transport_write(nego->transport, s);
|
2011-07-03 20:42:35 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
/**
|
|
|
|
* Process Negotiation Request from Connection Request message.
|
|
|
|
* @param nego
|
|
|
|
* @param s
|
|
|
|
*/
|
|
|
|
|
|
|
|
void nego_process_negotiation_request(rdpNego* nego, STREAM* s)
|
|
|
|
{
|
|
|
|
uint8 flags;
|
|
|
|
uint16 length;
|
|
|
|
|
|
|
|
DEBUG_NEGO("RDP_NEG_REQ");
|
|
|
|
|
|
|
|
stream_read_uint8(s, flags);
|
|
|
|
stream_read_uint16(s, length);
|
|
|
|
stream_read_uint32(s, nego->requested_protocols);
|
|
|
|
|
|
|
|
nego->state = NEGO_STATE_FINAL;
|
|
|
|
}
|
|
|
|
|
2011-07-01 02:48:48 +04:00
|
|
|
/**
|
|
|
|
* Process Negotiation Response from Connection Confirm message.
|
|
|
|
* @param nego
|
|
|
|
* @param s
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_process_negotiation_response(rdpNego* nego, STREAM* s)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
uint8 flags;
|
|
|
|
uint16 length;
|
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
DEBUG_NEGO("RDP_NEG_RSP");
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_read_uint8(s, flags);
|
|
|
|
stream_read_uint16(s, length);
|
2011-07-07 21:37:48 +04:00
|
|
|
stream_read_uint32(s, nego->selected_protocol);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
|
|
|
nego->state = NEGO_STATE_FINAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process Negotiation Failure from Connection Confirm message.
|
|
|
|
* @param nego
|
|
|
|
* @param s
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_process_negotiation_failure(rdpNego* nego, STREAM* s)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
|
|
|
uint8 flags;
|
|
|
|
uint16 length;
|
|
|
|
uint32 failureCode;
|
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
DEBUG_NEGO("RDP_NEG_FAILURE");
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_read_uint8(s, flags);
|
|
|
|
stream_read_uint16(s, length);
|
|
|
|
stream_read_uint32(s, failureCode);
|
2011-07-01 02:48:48 +04:00
|
|
|
|
|
|
|
switch (failureCode)
|
|
|
|
{
|
|
|
|
case SSL_REQUIRED_BY_SERVER:
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Error: SSL_REQUIRED_BY_SERVER");
|
2011-07-01 02:48:48 +04:00
|
|
|
break;
|
|
|
|
case SSL_NOT_ALLOWED_BY_SERVER:
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Error: SSL_NOT_ALLOWED_BY_SERVER");
|
2011-07-01 02:48:48 +04:00
|
|
|
break;
|
|
|
|
case SSL_CERT_NOT_ON_SERVER:
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Error: SSL_CERT_NOT_ON_SERVER");
|
2011-07-01 02:48:48 +04:00
|
|
|
break;
|
|
|
|
case INCONSISTENT_FLAGS:
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Error: INCONSISTENT_FLAGS");
|
2011-07-01 02:48:48 +04:00
|
|
|
break;
|
|
|
|
case HYBRID_REQUIRED_BY_SERVER:
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Error: HYBRID_REQUIRED_BY_SERVER");
|
2011-07-01 02:48:48 +04:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-03 20:42:35 +04:00
|
|
|
DEBUG_NEGO("Error: Unknown protocol security error %d", failureCode);
|
2011-07-01 02:48:48 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-07-03 21:49:06 +04:00
|
|
|
|
|
|
|
nego->state = NEGO_STATE_FAIL;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
/**
|
|
|
|
* Send RDP Negotiation Response (RDP_NEG_RSP).\n
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
|
|
|
void nego_send_negotiation_response(rdpNego* nego)
|
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
int length;
|
|
|
|
uint8 *bm, *em;
|
|
|
|
|
2011-08-25 09:45:43 +04:00
|
|
|
s = transport_send_stream_init(nego->transport, 256);
|
2011-08-18 19:15:28 +04:00
|
|
|
length = TPDU_CONNECTION_CONFIRM_LENGTH;
|
|
|
|
stream_get_mark(s, bm);
|
|
|
|
stream_seek(s, length);
|
|
|
|
|
|
|
|
if (nego->selected_protocol > PROTOCOL_RDP)
|
|
|
|
{
|
|
|
|
/* RDP_NEG_DATA must be present for TLS and NLA */
|
|
|
|
stream_write_uint8(s, TYPE_RDP_NEG_RSP);
|
|
|
|
stream_write_uint8(s, EXTENDED_CLIENT_DATA_SUPPORTED); /* flags */
|
|
|
|
stream_write_uint16(s, 8); /* RDP_NEG_DATA length (8) */
|
|
|
|
stream_write_uint32(s, nego->selected_protocol); /* selectedProtocol */
|
|
|
|
length += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_get_mark(s, em);
|
|
|
|
stream_set_mark(s, bm);
|
|
|
|
tpkt_write_header(s, length);
|
|
|
|
tpdu_write_connection_confirm(s, length - 5);
|
|
|
|
stream_set_mark(s, em);
|
|
|
|
|
|
|
|
transport_write(nego->transport, s);
|
2011-08-19 13:39:37 +04:00
|
|
|
|
|
|
|
/* update settings with negotiated protocol security */
|
2011-08-19 19:56:47 +04:00
|
|
|
nego->transport->settings->requested_protocols = nego->requested_protocols;
|
2011-08-19 13:39:37 +04:00
|
|
|
nego->transport->settings->selected_protocol = nego->selected_protocol;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
/**
|
|
|
|
* Initialize NEGO state machine.
|
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_init(rdpNego* nego)
|
2011-07-03 20:42:35 +04:00
|
|
|
{
|
|
|
|
nego->state = NEGO_STATE_INITIAL;
|
|
|
|
nego->requested_protocols = PROTOCOL_RDP;
|
2011-07-03 21:49:06 +04:00
|
|
|
nego->transport->recv_callback = nego_recv;
|
|
|
|
nego->transport->recv_extra = (void*) nego;
|
2011-07-03 20:42:35 +04:00
|
|
|
}
|
|
|
|
|
2011-07-01 02:48:48 +04:00
|
|
|
/**
|
|
|
|
* Create a new NEGO state machine instance.
|
2011-07-03 20:42:35 +04:00
|
|
|
* @param transport
|
2011-07-01 02:48:48 +04:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
rdpNego* nego_new(struct rdp_transport * transport)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
2011-07-07 21:37:48 +04:00
|
|
|
rdpNego* nego = (rdpNego*) xzalloc(sizeof(rdpNego));
|
2011-07-01 02:48:48 +04:00
|
|
|
|
|
|
|
if (nego != NULL)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
nego->transport = transport;
|
2011-07-01 02:48:48 +04:00
|
|
|
nego_init(nego);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nego;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-03 20:42:35 +04:00
|
|
|
* Free NEGO state machine.
|
2011-07-01 02:48:48 +04:00
|
|
|
* @param nego
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_free(rdpNego* nego)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
xfree(nego);
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-03 20:42:35 +04:00
|
|
|
* Set target hostname and port.
|
2011-07-01 02:48:48 +04:00
|
|
|
* @param nego
|
2011-07-03 20:42:35 +04:00
|
|
|
* @param hostname
|
|
|
|
* @param port
|
2011-07-01 02:48:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_set_target(rdpNego* nego, char* hostname, int port)
|
2011-07-01 02:48:48 +04:00
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
nego->hostname = hostname;
|
|
|
|
nego->port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-31 07:51:26 +04:00
|
|
|
* Enable RDP security protocol.
|
|
|
|
* @param nego pointer to the negotiation structure
|
|
|
|
* @param enable_rdp whether to enable normal RDP protocol (True for enabled, False for disabled)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void nego_enable_rdp(rdpNego* nego, boolean enable_rdp)
|
|
|
|
{
|
|
|
|
DEBUG_NEGO("Enabling RDP security: %s", enable_rdp ? "True" : "False");
|
|
|
|
nego->enabled_protocols[PROTOCOL_RDP] = enable_rdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable TLS security protocol.
|
|
|
|
* @param nego pointer to the negotiation structure
|
|
|
|
* @param enable_tls whether to enable TLS + RDP protocol (True for enabled, False for disabled)
|
|
|
|
*/
|
|
|
|
void nego_enable_tls(rdpNego* nego, boolean enable_tls)
|
|
|
|
{
|
|
|
|
DEBUG_NEGO("Enabling TLS security: %s", enable_tls ? "True" : "False");
|
|
|
|
nego->enabled_protocols[PROTOCOL_TLS] = enable_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable NLA security protocol.
|
|
|
|
* @param nego pointer to the negotiation structure
|
|
|
|
* @param enable_nla whether to enable network level authentication protocol (True for enabled, False for disabled)
|
2011-07-03 20:42:35 +04:00
|
|
|
*/
|
|
|
|
|
2011-07-31 07:51:26 +04:00
|
|
|
void nego_enable_nla(rdpNego* nego, boolean enable_nla)
|
2011-07-03 20:42:35 +04:00
|
|
|
{
|
2011-07-31 07:51:26 +04:00
|
|
|
DEBUG_NEGO("Enabling NLA security: %s", enable_nla ? "True" : "False");
|
|
|
|
nego->enabled_protocols[PROTOCOL_NLA] = enable_nla;
|
2011-07-03 20:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set routing token.
|
|
|
|
* @param nego
|
|
|
|
* @param routing_token
|
|
|
|
*/
|
|
|
|
|
2011-09-04 02:21:21 +04:00
|
|
|
void nego_set_routing_token(rdpNego* nego, rdpBlob* routing_token)
|
2011-07-03 20:42:35 +04:00
|
|
|
{
|
|
|
|
nego->routing_token = routing_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set cookie.
|
|
|
|
* @param nego
|
|
|
|
* @param cookie
|
|
|
|
*/
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void nego_set_cookie(rdpNego* nego, char* cookie)
|
2011-07-03 20:42:35 +04:00
|
|
|
{
|
|
|
|
nego->cookie = cookie;
|
2011-07-01 02:48:48 +04:00
|
|
|
}
|