2 * FreeRDP: A Remote Desktop Protocol Client
5 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
24 #include "connection.h"
30 * |-----------------------X.224 Connection Request PDU--------------------->|
31 * |<----------------------X.224 Connection Confirm PDU----------------------|
32 * |-------MCS Connect-Initial PDU with GCC Conference Create Request------->|
33 * |<-----MCS Connect-Response PDU with GCC Conference Create Response-------|
34 * |------------------------MCS Erect Domain Request PDU-------------------->|
35 * |------------------------MCS Attach User Request PDU--------------------->|
36 * |<-----------------------MCS Attach User Confirm PDU----------------------|
37 * |------------------------MCS Channel Join Request PDU-------------------->|
38 * |<-----------------------MCS Channel Join Confirm PDU---------------------|
39 * |----------------------------Security Exchange PDU----------------------->|
40 * |-------------------------------Client Info PDU-------------------------->|
41 * |<---------------------License Error PDU - Valid Client-------------------|
42 * |<-----------------------------Demand Active PDU--------------------------|
43 * |------------------------------Confirm Active PDU------------------------>|
44 * |-------------------------------Synchronize PDU-------------------------->|
45 * |---------------------------Control PDU - Cooperate---------------------->|
46 * |------------------------Control PDU - Request Control------------------->|
47 * |--------------------------Persistent Key List PDU(s)-------------------->|
48 * |--------------------------------Font List PDU--------------------------->|
49 * |<------------------------------Synchronize PDU---------------------------|
50 * |<--------------------------Control PDU - Cooperate-----------------------|
51 * |<-----------------------Control PDU - Granted Control--------------------|
52 * |<-------------------------------Font Map PDU-----------------------------|
57 * Establish RDP Connection.\n
59 * @param rdp RDP module
62 boolean rdp_client_connect(rdpRdp* rdp)
65 uint32 selectedProtocol;
66 rdpSettings* settings = rdp->settings;
69 nego_set_target(rdp->nego, settings->hostname, settings->port);
70 nego_set_cookie(rdp->nego, settings->username);
71 nego_enable_rdp(rdp->nego, settings->rdp_security);
72 nego_enable_nla(rdp->nego, settings->nla_security);
73 nego_enable_tls(rdp->nego, settings->tls_security);
75 if (nego_connect(rdp->nego) != true)
77 printf("Error: protocol security negotiation failure\n");
81 selectedProtocol = rdp->nego->selected_protocol;
83 if ((selectedProtocol & PROTOCOL_TLS) || (selectedProtocol == PROTOCOL_RDP))
85 if ((settings->username != NULL) && ((settings->password != NULL) || (settings->password_cookie != NULL && settings->password_cookie->length > 0)))
86 settings->autologon = true;
90 if (selectedProtocol & PROTOCOL_NLA)
91 status = transport_connect_nla(rdp->transport);
92 else if (selectedProtocol & PROTOCOL_TLS)
93 status = transport_connect_tls(rdp->transport);
94 else if (selectedProtocol == PROTOCOL_RDP) /* 0 */
95 status = transport_connect_rdp(rdp->transport);
100 rdp_set_blocking_mode(rdp, false);
101 rdp->state = CONNECTION_STATE_NEGO;
102 rdp->finalize_sc_pdus = 0;
104 if (mcs_send_connect_initial(rdp->mcs) != true)
106 printf("Error: unable to send MCS Connect Initial\n");
110 while (rdp->state != CONNECTION_STATE_ACTIVE)
112 if (rdp_check_fds(rdp) < 0)
119 boolean rdp_client_disconnect(rdpRdp* rdp)
121 return transport_disconnect(rdp->transport);
124 boolean rdp_client_redirect(rdpRdp* rdp)
126 rdpSettings* settings = rdp->settings;
127 rdpRedirection* redirection = rdp->redirection;
129 rdp_client_disconnect(rdp);
132 nego_free(rdp->nego);
133 license_free(rdp->license);
134 transport_free(rdp->transport);
135 rdp->transport = transport_new(settings);
136 rdp->license = license_new(rdp);
137 rdp->nego = nego_new(rdp->transport);
138 rdp->mcs = mcs_new(rdp->transport);
140 rdp->transport->layer = TRANSPORT_LAYER_TCP;
141 settings->redirected_session_id = redirection->sessionID;
143 if (redirection->flags & LB_LOAD_BALANCE_INFO)
145 nego_set_routing_token(rdp->nego, &redirection->loadBalanceInfo);
149 if (redirection->flags & LB_TARGET_NET_ADDRESS)
151 xfree(settings->hostname);
152 settings->hostname = xstrdup(redirection->targetNetAddress.ascii);
154 else if (redirection->flags & LB_TARGET_FQDN)
156 xfree(settings->hostname);
157 settings->hostname = xstrdup(redirection->targetFQDN.ascii);
159 else if (redirection->flags & LB_TARGET_NETBIOS_NAME)
161 xfree(settings->hostname);
162 settings->hostname = xstrdup(redirection->targetNetBiosName.ascii);
166 if (redirection->flags & LB_USERNAME)
168 xfree(settings->username);
169 settings->username = xstrdup(redirection->username.ascii);
172 if (redirection->flags & LB_DOMAIN)
174 xfree(settings->domain);
175 settings->domain = xstrdup(redirection->domain.ascii);
178 if (redirection->flags & LB_PASSWORD)
180 settings->password_cookie = &redirection->password_cookie;
183 return rdp_client_connect(rdp);
186 static boolean rdp_client_establish_keys(rdpRdp* rdp)
188 uint8 client_random[CLIENT_RANDOM_LENGTH];
189 uint8 crypt_client_random[256 + 8];
196 if (rdp->settings->encryption == false)
198 /* no RDP encryption */
202 /* encrypt client random */
203 memset(crypt_client_random, 0, sizeof(crypt_client_random));
204 crypto_nonce(client_random, sizeof(client_random));
205 key_len = rdp->settings->server_cert->cert_info.modulus.length;
206 mod = rdp->settings->server_cert->cert_info.modulus.data;
207 exp = rdp->settings->server_cert->cert_info.exponent;
208 crypto_rsa_public_encrypt(client_random, sizeof(client_random), key_len, mod, exp, crypt_client_random);
210 /* send crypt client random to server */
211 length = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SECURITY_HEADER_LENGTH + 4 + key_len + 8;
212 s = transport_send_stream_init(rdp->mcs->transport, length);
213 rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID);
214 rdp_write_security_header(s, SEC_EXCHANGE_PKT);
215 length = key_len + 8;
216 stream_write_uint32(s, length);
217 stream_write(s, crypt_client_random, length);
218 if (transport_write(rdp->mcs->transport, s) < 0)
223 /* now calculate encrypt / decrypt and update keys */
224 if (!security_establish_keys(client_random, rdp))
229 rdp->do_crypt = true;
230 if (rdp->settings->secure_checksum)
231 rdp->do_secure_checksum = true;
233 if (rdp->settings->encryption_method == ENCRYPTION_METHOD_FIPS)
235 uint8 fips_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
236 rdp->fips_encrypt = crypto_des3_encrypt_init(rdp->fips_encrypt_key, fips_ivec);
237 rdp->fips_decrypt = crypto_des3_decrypt_init(rdp->fips_decrypt_key, fips_ivec);
239 rdp->fips_hmac = crypto_hmac_new();
243 rdp->rc4_decrypt_key = crypto_rc4_init(rdp->decrypt_key, rdp->rc4_key_len);
244 rdp->rc4_encrypt_key = crypto_rc4_init(rdp->encrypt_key, rdp->rc4_key_len);
249 static boolean rdp_server_establish_keys(rdpRdp* rdp, STREAM* s)
251 uint8 client_random[64]; /* Should be only 32 after successfull decryption, but on failure might take up to 64 bytes. */
252 uint8 crypt_client_random[256 + 8];
253 uint32 rand_len, key_len;
254 uint16 channel_id, length, sec_flags;
258 if (rdp->settings->encryption == false)
260 /* No RDP Security. */
264 if (!rdp_read_header(rdp, s, &length, &channel_id))
266 printf("rdp_server_establish_keys: invalid RDP header\n");
269 rdp_read_security_header(s, &sec_flags);
270 if ((sec_flags & SEC_EXCHANGE_PKT) == 0)
272 printf("rdp_server_establish_keys: missing SEC_EXCHANGE_PKT in security header\n");
275 stream_read_uint32(s, rand_len);
276 key_len = rdp->settings->server_key->modulus.length;
277 if (rand_len != key_len + 8)
279 printf("rdp_server_establish_keys: invalid encrypted client random length\n");
282 memset(crypt_client_random, 0, sizeof(crypt_client_random));
283 stream_read(s, crypt_client_random, rand_len);
284 /* 8 zero bytes of padding */
286 mod = rdp->settings->server_key->modulus.data;
287 priv_exp = rdp->settings->server_key->private_exponent.data;
288 crypto_rsa_private_decrypt(crypt_client_random, rand_len - 8, key_len, mod, priv_exp, client_random);
290 /* now calculate encrypt / decrypt and update keys */
291 if (!security_establish_keys(client_random, rdp))
296 rdp->do_crypt = true;
297 if (rdp->settings->secure_checksum)
298 rdp->do_secure_checksum = true;
300 if (rdp->settings->encryption_method == ENCRYPTION_METHOD_FIPS)
302 uint8 fips_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
303 rdp->fips_encrypt = crypto_des3_encrypt_init(rdp->fips_encrypt_key, fips_ivec);
304 rdp->fips_decrypt = crypto_des3_decrypt_init(rdp->fips_decrypt_key, fips_ivec);
306 rdp->fips_hmac = crypto_hmac_new();
310 rdp->rc4_decrypt_key = crypto_rc4_init(rdp->decrypt_key, rdp->rc4_key_len);
311 rdp->rc4_encrypt_key = crypto_rc4_init(rdp->encrypt_key, rdp->rc4_key_len);
316 boolean rdp_client_connect_mcs_connect_response(rdpRdp* rdp, STREAM* s)
318 if (!mcs_recv_connect_response(rdp->mcs, s))
320 printf("rdp_client_connect_mcs_connect_response: mcs_recv_connect_response failed\n");
323 if (!mcs_send_erect_domain_request(rdp->mcs))
325 if (!mcs_send_attach_user_request(rdp->mcs))
328 rdp->state = CONNECTION_STATE_MCS_ATTACH_USER;
333 boolean rdp_client_connect_mcs_attach_user_confirm(rdpRdp* rdp, STREAM* s)
335 if (!mcs_recv_attach_user_confirm(rdp->mcs, s))
338 if (!mcs_send_channel_join_request(rdp->mcs, rdp->mcs->user_id))
341 rdp->state = CONNECTION_STATE_MCS_CHANNEL_JOIN;
346 boolean rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp, STREAM* s)
350 boolean all_joined = true;
352 if (!mcs_recv_channel_join_confirm(rdp->mcs, s, &channel_id))
355 if (!rdp->mcs->user_channel_joined)
357 if (channel_id != rdp->mcs->user_id)
359 rdp->mcs->user_channel_joined = true;
361 if (!mcs_send_channel_join_request(rdp->mcs, MCS_GLOBAL_CHANNEL_ID))
364 else if (!rdp->mcs->global_channel_joined)
366 if (channel_id != MCS_GLOBAL_CHANNEL_ID)
368 rdp->mcs->global_channel_joined = true;
370 if (rdp->settings->num_channels > 0)
372 if (!mcs_send_channel_join_request(rdp->mcs, rdp->settings->channels[0].channel_id))
380 for (i = 0; i < rdp->settings->num_channels; i++)
382 if (rdp->settings->channels[i].joined)
385 if (rdp->settings->channels[i].channel_id != channel_id)
388 rdp->settings->channels[i].joined = true;
391 if (i + 1 < rdp->settings->num_channels)
393 if (!mcs_send_channel_join_request(rdp->mcs, rdp->settings->channels[i + 1].channel_id))
400 if (rdp->mcs->user_channel_joined && rdp->mcs->global_channel_joined && all_joined)
402 if (!rdp_client_establish_keys(rdp))
404 if (!rdp_send_client_info(rdp))
406 rdp->state = CONNECTION_STATE_LICENSE;
412 boolean rdp_client_connect_license(rdpRdp* rdp, STREAM* s)
414 if (!license_recv(rdp->license, s))
417 if (rdp->license->state == LICENSE_STATE_ABORTED)
419 printf("license connection sequence aborted.\n");
423 if (rdp->license->state == LICENSE_STATE_COMPLETED)
425 rdp->state = CONNECTION_STATE_CAPABILITY;
431 boolean rdp_client_connect_demand_active(rdpRdp* rdp, STREAM* s)
437 width = rdp->settings->width;
438 height = rdp->settings->height;
440 stream_get_mark(s, mark);
442 if (!rdp_recv_demand_active(rdp, s))
444 stream_set_mark(s, mark);
445 stream_seek(s, RDP_PACKET_HEADER_MAX_LENGTH);
447 if (rdp_recv_out_of_sequence_pdu(rdp, s) != true)
456 if (!rdp_send_confirm_active(rdp))
459 input_register_client_callbacks(rdp->input);
462 * The server may request a different desktop size during Deactivation-Reactivation sequence.
463 * In this case, the UI should be informed and do actual window resizing at this point.
465 if (width != rdp->settings->width || height != rdp->settings->height)
467 IFCALL(rdp->update->DesktopResize, rdp->update->context);
470 rdp->state = CONNECTION_STATE_FINALIZATION;
471 update_reset_state(rdp->update);
473 rdp_client_connect_finalize(rdp);
478 boolean rdp_client_connect_finalize(rdpRdp* rdp)
481 * [MS-RDPBCGR] 1.3.1.1 - 8.
482 * The client-to-server PDUs sent during this phase have no dependencies on any of the server-to-
483 * client PDUs; they may be sent as a single batch, provided that sequencing is maintained.
486 if (!rdp_send_client_synchronize_pdu(rdp))
488 if (!rdp_send_client_control_pdu(rdp, CTRLACTION_COOPERATE))
490 if (!rdp_send_client_control_pdu(rdp, CTRLACTION_REQUEST_CONTROL))
493 rdp->input->SynchronizeEvent(rdp->input, 0);
495 if (!rdp_send_client_persistent_key_list_pdu(rdp))
497 if (!rdp_send_client_font_list_pdu(rdp, FONTLIST_FIRST | FONTLIST_LAST))
503 boolean rdp_server_accept_nego(rdpRdp* rdp, STREAM* s)
507 transport_set_blocking_mode(rdp->transport, true);
509 if (!nego_read_request(rdp->nego, s))
512 rdp->nego->selected_protocol = 0;
514 printf("Requested protocols:");
515 if ((rdp->nego->requested_protocols & PROTOCOL_TLS))
518 if (rdp->settings->tls_security)
521 rdp->nego->selected_protocol |= PROTOCOL_TLS;
526 if ((rdp->nego->requested_protocols & PROTOCOL_NLA))
529 if (rdp->settings->nla_security)
532 rdp->nego->selected_protocol |= PROTOCOL_NLA;
538 if (rdp->settings->rdp_security && rdp->nego->selected_protocol == 0)
541 rdp->nego->selected_protocol = PROTOCOL_RDP;
547 if (!nego_send_negotiation_response(rdp->nego))
551 if (rdp->nego->selected_protocol & PROTOCOL_NLA)
552 ret = transport_accept_nla(rdp->transport);
553 else if (rdp->nego->selected_protocol & PROTOCOL_TLS)
554 ret = transport_accept_tls(rdp->transport);
555 else if (rdp->nego->selected_protocol == PROTOCOL_RDP) /* 0 */
556 ret = transport_accept_rdp(rdp->transport);
561 transport_set_blocking_mode(rdp->transport, false);
563 rdp->state = CONNECTION_STATE_NEGO;
568 boolean rdp_server_accept_mcs_connect_initial(rdpRdp* rdp, STREAM* s)
572 if (!mcs_recv_connect_initial(rdp->mcs, s))
575 printf("Accepted client: %s\n", rdp->settings->client_hostname);
576 printf("Accepted channels:");
577 for (i = 0; i < rdp->settings->num_channels; i++)
579 printf(" %s", rdp->settings->channels[i].name);
583 if (!mcs_send_connect_response(rdp->mcs))
586 rdp->state = CONNECTION_STATE_MCS_CONNECT;
591 boolean rdp_server_accept_mcs_erect_domain_request(rdpRdp* rdp, STREAM* s)
593 if (!mcs_recv_erect_domain_request(rdp->mcs, s))
596 rdp->state = CONNECTION_STATE_MCS_ERECT_DOMAIN;
601 boolean rdp_server_accept_mcs_attach_user_request(rdpRdp* rdp, STREAM* s)
603 if (!mcs_recv_attach_user_request(rdp->mcs, s))
606 if (!mcs_send_attach_user_confirm(rdp->mcs))
609 rdp->state = CONNECTION_STATE_MCS_ATTACH_USER;
614 boolean rdp_server_accept_mcs_channel_join_request(rdpRdp* rdp, STREAM* s)
618 boolean all_joined = true;
620 if (!mcs_recv_channel_join_request(rdp->mcs, s, &channel_id))
623 if (!mcs_send_channel_join_confirm(rdp->mcs, channel_id))
626 if (channel_id == rdp->mcs->user_id)
627 rdp->mcs->user_channel_joined = true;
628 else if (channel_id == MCS_GLOBAL_CHANNEL_ID)
629 rdp->mcs->global_channel_joined = true;
631 for (i = 0; i < rdp->settings->num_channels; i++)
633 if (rdp->settings->channels[i].channel_id == channel_id)
634 rdp->settings->channels[i].joined = true;
636 if (!rdp->settings->channels[i].joined)
640 if (rdp->mcs->user_channel_joined && rdp->mcs->global_channel_joined && all_joined)
641 rdp->state = CONNECTION_STATE_MCS_CHANNEL_JOIN;
646 boolean rdp_server_accept_client_keys(rdpRdp* rdp, STREAM* s)
649 if (!rdp_server_establish_keys(rdp, s))
652 rdp->state = CONNECTION_STATE_ESTABLISH_KEYS;
657 boolean rdp_server_accept_client_info(rdpRdp* rdp, STREAM* s)
660 if (!rdp_recv_client_info(rdp, s))
663 if (!license_send_valid_client_error_packet(rdp->license))
666 rdp->state = CONNECTION_STATE_LICENSE;
671 boolean rdp_server_accept_confirm_active(rdpRdp* rdp, STREAM* s)
673 if (!rdp_recv_confirm_active(rdp, s))
676 rdp->state = CONNECTION_STATE_ACTIVE;
677 update_reset_state(rdp->update);
679 if (!rdp_send_server_synchronize_pdu(rdp))
682 if (!rdp_send_server_control_cooperate_pdu(rdp))
688 boolean rdp_server_reactivate(rdpRdp* rdp)
690 if (!rdp_send_deactivate_all(rdp))
693 rdp->state = CONNECTION_STATE_LICENSE;
695 if (!rdp_send_demand_active(rdp))