FreeRDP
Loading...
Searching...
No Matches
rpc_bind.c
1
19
20#include <freerdp/config.h>
21
22#include "../settings.h"
23
24#include <winpr/crt.h>
25#include <winpr/assert.h>
26#include <winpr/cast.h>
27
28#include <freerdp/log.h>
29
30#include "rpc_client.h"
31
32#include "rts.h"
33
34#include "rpc_bind.h"
35#include "../utils.h"
36
37#define TAG FREERDP_TAG("core.gateway.rpc")
38
39#define AUTH_PKG NTLM_SSP_NAME
40
45
46/* Syntax UUIDs */
47
48const p_uuid_t TSGU_UUID = {
49 0x44E265DD, /* time_low */
50 0x7DAF, /* time_mid */
51 0x42CD, /* time_hi_and_version */
52 0x85, /* clock_seq_hi_and_reserved */
53 0x60, /* clock_seq_low */
54 { 0x3C, 0xDB, 0x6E, 0x7A, 0x27, 0x29 } /* node[6] */
55};
56
57const p_uuid_t NDR_UUID = {
58 0x8A885D04, /* time_low */
59 0x1CEB, /* time_mid */
60 0x11C9, /* time_hi_and_version */
61 0x9F, /* clock_seq_hi_and_reserved */
62 0xE8, /* clock_seq_low */
63 { 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60 } /* node[6] */
64};
65
66const p_uuid_t BTFN_UUID = {
67 0x6CB71C2C, /* time_low */
68 0x9812, /* time_mid */
69 0x4540, /* time_hi_and_version */
70 0x03, /* clock_seq_hi_and_reserved */
71 0x00, /* clock_seq_low */
72 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } /* node[6] */
73};
74
97
111
112static int rpc_bind_setup(rdpRpc* rpc)
113{
114 SEC_WINNT_AUTH_IDENTITY identity = WINPR_C_ARRAY_INIT;
115
116 WINPR_ASSERT(rpc);
117
118 rdpContext* context = transport_get_context(rpc->transport);
119 WINPR_ASSERT(context);
120
121 rdpSettings* settings = context->settings;
122 WINPR_ASSERT(settings);
123
124 freerdp* instance = context->instance;
125 WINPR_ASSERT(instance);
126
127 credssp_auth_free(rpc->auth);
128 rpc->auth = credssp_auth_new(context);
129 if (!rpc->auth)
130 return -1;
131
132 auth_status rc = utils_authenticate_gateway(instance, GW_AUTH_RPC);
133 switch (rc)
134 {
135 case AUTH_SUCCESS:
136 case AUTH_SKIP:
137 break;
138 case AUTH_CANCELLED:
139 freerdp_set_last_error_log(instance->context, FREERDP_ERROR_CONNECT_CANCELLED);
140 return -1;
141 case AUTH_NO_CREDENTIALS:
142 WLog_INFO(TAG, "No credentials provided - using nullptr identity");
143 break;
144 case AUTH_FAILED:
145 default:
146 return -1;
147 }
148
149 if (!credssp_auth_init(rpc->auth, AUTH_PKG, nullptr))
150 return -1;
151
152 if (!identity_set_from_settings(&identity, settings, FreeRDP_GatewayUsername,
153 FreeRDP_GatewayDomain, FreeRDP_GatewayPassword))
154 return -1;
155
156 const char* GatewayHostname = freerdp_settings_get_string(settings, FreeRDP_GatewayHostname);
157 const char* GatewayUsername = freerdp_settings_get_string(settings, FreeRDP_GatewayUsername);
158
159 SEC_WINNT_AUTH_IDENTITY* identityArg = (GatewayUsername ? &identity : nullptr);
160 if (!credssp_auth_setup_client(rpc->auth, nullptr, GatewayHostname, identityArg, nullptr))
161 {
162 sspi_FreeAuthIdentity(&identity);
163 return -1;
164 }
165 sspi_FreeAuthIdentity(&identity);
166
167 credssp_auth_set_flags(rpc->auth, ISC_REQ_USE_DCE_STYLE | ISC_REQ_DELEGATE |
168 ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT);
169
170 if (credssp_auth_authenticate(rpc->auth) < 0)
171 return -1;
172
173 return 1;
174}
175
176int rpc_send_bind_pdu(rdpRpc* rpc, BOOL initial)
177{
178 int status = -1;
179 wStream* buffer = nullptr;
180 UINT32 offset = 0;
181 RpcClientCall* clientCall = nullptr;
182 rpcconn_bind_hdr_t bind_pdu = WINPR_C_ARRAY_INIT;
183 RpcVirtualConnection* connection = nullptr;
184 RpcInChannel* inChannel = nullptr;
185 const SecBuffer* sbuffer = nullptr;
186
187 WINPR_ASSERT(rpc);
188
189 connection = rpc->VirtualConnection;
190
191 WINPR_ASSERT(connection);
192
193 inChannel = connection->DefaultInChannel;
194
195 if (initial && rpc_bind_setup(rpc) < 0)
196 return -1;
197
198 WLog_DBG(TAG, initial ? "Sending Bind PDU" : "Sending Alter Context PDU");
199
200 sbuffer = credssp_auth_get_output_buffer(rpc->auth);
201
202 if (!sbuffer)
203 goto fail;
204
205 bind_pdu.header = rpc_pdu_header_init(rpc);
206 bind_pdu.header.auth_length = (UINT16)sbuffer->cbBuffer;
207 bind_pdu.auth_verifier.auth_value = sbuffer->pvBuffer;
208 bind_pdu.header.ptype = initial ? PTYPE_BIND : PTYPE_ALTER_CONTEXT;
209 bind_pdu.header.pfc_flags =
210 PFC_FIRST_FRAG | PFC_LAST_FRAG | PFC_SUPPORT_HEADER_SIGN | PFC_CONC_MPX;
211 bind_pdu.header.call_id = rpc->CallId++;
212 bind_pdu.max_xmit_frag = rpc->max_xmit_frag;
213 bind_pdu.max_recv_frag = rpc->max_recv_frag;
214 bind_pdu.assoc_group_id = 0;
215 bind_pdu.p_context_elem.reserved = 0;
216 bind_pdu.p_context_elem.reserved2 = 0;
217 bind_pdu.p_context_elem.p_cont_elem = rts_context_elem_new(2);
218
219 if (!bind_pdu.p_context_elem.p_cont_elem)
220 goto fail;
221 bind_pdu.p_context_elem.n_context_elem = 2;
222
223 {
224 p_cont_elem_t* p_cont_elem = &bind_pdu.p_context_elem.p_cont_elem[0];
225 p_cont_elem->p_cont_id = 0;
226 p_cont_elem->n_transfer_syn = 1;
227 p_cont_elem->reserved = 0;
228 CopyMemory(&(p_cont_elem->abstract_syntax.if_uuid), &TSGU_UUID, sizeof(p_uuid_t));
229 p_cont_elem->abstract_syntax.if_version = TSGU_SYNTAX_IF_VERSION;
230 rts_syntax_id_free(p_cont_elem->transfer_syntaxes);
231 p_cont_elem->transfer_syntaxes = rts_syntax_id_new(1);
232
233 if (!p_cont_elem->transfer_syntaxes)
234 goto fail;
235
236 CopyMemory(&(p_cont_elem->transfer_syntaxes[0].if_uuid), &NDR_UUID, sizeof(p_uuid_t));
237 p_cont_elem->transfer_syntaxes[0].if_version = NDR_SYNTAX_IF_VERSION;
238 }
239 {
240 p_cont_elem_t* p_cont_elem = &bind_pdu.p_context_elem.p_cont_elem[1];
241 p_cont_elem->p_cont_id = 1;
242 p_cont_elem->n_transfer_syn = 1;
243 p_cont_elem->reserved = 0;
244 CopyMemory(&(p_cont_elem->abstract_syntax.if_uuid), &TSGU_UUID, sizeof(p_uuid_t));
245 p_cont_elem->abstract_syntax.if_version = TSGU_SYNTAX_IF_VERSION;
246
247 rts_syntax_id_free(p_cont_elem->transfer_syntaxes);
248 p_cont_elem->transfer_syntaxes = rts_syntax_id_new(1);
249
250 if (!p_cont_elem->transfer_syntaxes)
251 goto fail;
252
253 CopyMemory(&(p_cont_elem->transfer_syntaxes[0].if_uuid), &BTFN_UUID, sizeof(p_uuid_t));
254 p_cont_elem->transfer_syntaxes[0].if_version = BTFN_SYNTAX_IF_VERSION;
255 offset = 116;
256 }
257
258 bind_pdu.auth_verifier.auth_type =
259 rpc_auth_pkg_to_security_provider(credssp_auth_pkg_name(rpc->auth));
260 bind_pdu.auth_verifier.auth_level = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
261 bind_pdu.auth_verifier.auth_reserved = 0x00;
262 bind_pdu.auth_verifier.auth_context_id = 0x00000000;
263 offset += (8 + bind_pdu.header.auth_length);
264
265 WINPR_ASSERT(offset <= UINT16_MAX);
266 bind_pdu.header.frag_length = (UINT16)offset;
267
268 buffer = Stream_New(nullptr, bind_pdu.header.frag_length);
269
270 if (!buffer)
271 goto fail;
272
273 if (!rts_write_pdu_bind(buffer, &bind_pdu))
274 goto fail;
275
276 clientCall = rpc_client_call_new(bind_pdu.header.call_id, 0);
277
278 if (!clientCall)
279 goto fail;
280
281 if (!rpc_client_call_add(rpc->log, rpc->client, clientCall))
282 {
283 rpc_client_call_free(clientCall);
284 goto fail;
285 }
286
287 Stream_SealLength(buffer);
288 status = rpc_in_channel_send_pdu(inChannel, Stream_Buffer(buffer), Stream_Length(buffer));
289fail:
290 if (status <= 0)
291 rpc_client_call_discard(rpc->log, rpc->client, bind_pdu.header.call_id);
292
293 rts_context_elem_free(bind_pdu.p_context_elem.p_cont_elem,
294 bind_pdu.p_context_elem.n_context_elem);
295 bind_pdu.p_context_elem.n_context_elem = 0;
296 bind_pdu.p_context_elem.p_cont_elem = nullptr;
297
298 Stream_Free(buffer, TRUE);
299 return (status > 0) ? 1 : -1;
300}
301
318
327
328BOOL rpc_recv_bind_ack_pdu(rdpRpc* rpc, wStream* s)
329{
330 BOOL rc = FALSE;
331 const BYTE* auth_data = nullptr;
332 size_t pos = 0;
333 size_t end = 0;
334 rpcconn_hdr_t header = WINPR_C_ARRAY_INIT;
335 SecBuffer buffer = WINPR_C_ARRAY_INIT;
336
337 WINPR_ASSERT(rpc);
338 WINPR_ASSERT(rpc->auth);
339 WINPR_ASSERT(s);
340
341 pos = Stream_GetPosition(s);
342 if (!rts_read_pdu_header(s, &header))
343 goto fail;
344
345 WLog_DBG(TAG, header.common.ptype == PTYPE_BIND_ACK ? "Receiving BindAck PDU"
346 : "Receiving AlterContextResp PDU");
347
348 const UINT16 MAX_VALID_FRAG = 0x0FF8;
349 if ((header.bind_ack.max_xmit_frag > MAX_VALID_FRAG) ||
350 (header.bind_ack.max_recv_frag > MAX_VALID_FRAG))
351 {
352 WLog_ERR(TAG,
353 "bind_ack: invalid fragment size: max_xmit_frag=%" PRIu16
354 ", max_recv_frag=%" PRIu16 ", maximum=%" PRIu16,
355 header.bind_ack.max_xmit_frag, header.bind_ack.max_recv_frag, MAX_VALID_FRAG);
356 goto fail;
357 }
358
359 rpc->max_recv_frag = header.bind_ack.max_xmit_frag;
360 rpc->max_xmit_frag = header.bind_ack.max_recv_frag;
361
362 /* Get the correct offset in the input data and pass that on as input buffer.
363 * rts_read_pdu_header did already do consistency checks */
364 end = Stream_GetPosition(s);
365 if (!Stream_SetPosition(s, pos + header.common.frag_length - header.common.auth_length))
366 goto fail;
367 auth_data = Stream_ConstPointer(s);
368 if (!Stream_SetPosition(s, end))
369 goto fail;
370
371 buffer.cbBuffer = header.common.auth_length;
372 buffer.pvBuffer = malloc(buffer.cbBuffer);
373 if (!buffer.pvBuffer)
374 goto fail;
375 memcpy(buffer.pvBuffer, auth_data, buffer.cbBuffer);
376 credssp_auth_take_input_buffer(rpc->auth, &buffer);
377
378 if (credssp_auth_authenticate(rpc->auth) < 0)
379 goto fail;
380
381 rc = TRUE;
382fail:
383 rts_free_pdu_header(&header, FALSE);
384 return rc;
385}
386
393
394int rpc_send_rpc_auth_3_pdu(rdpRpc* rpc)
395{
396 int status = -1;
397 wStream* buffer = nullptr;
398 size_t offset = 0;
399 const SecBuffer* sbuffer = nullptr;
400 RpcClientCall* clientCall = nullptr;
401 rpcconn_rpc_auth_3_hdr_t auth_3_pdu = WINPR_C_ARRAY_INIT;
402 RpcVirtualConnection* connection = nullptr;
403 RpcInChannel* inChannel = nullptr;
404
405 WINPR_ASSERT(rpc);
406
407 connection = rpc->VirtualConnection;
408 WINPR_ASSERT(connection);
409
410 inChannel = connection->DefaultInChannel;
411 WINPR_ASSERT(inChannel);
412
413 WLog_DBG(TAG, "Sending RpcAuth3 PDU");
414
415 sbuffer = credssp_auth_get_output_buffer(rpc->auth);
416
417 if (!sbuffer)
418 goto fail;
419
420 auth_3_pdu.header = rpc_pdu_header_init(rpc);
421 auth_3_pdu.header.auth_length = (UINT16)sbuffer->cbBuffer;
422 auth_3_pdu.auth_verifier.auth_value = sbuffer->pvBuffer;
423 auth_3_pdu.header.ptype = PTYPE_RPC_AUTH_3;
424 auth_3_pdu.header.pfc_flags = PFC_FIRST_FRAG | PFC_LAST_FRAG | PFC_CONC_MPX;
425 auth_3_pdu.header.call_id = rpc->CallId++;
426 auth_3_pdu.max_xmit_frag = rpc->max_xmit_frag;
427 auth_3_pdu.max_recv_frag = rpc->max_recv_frag;
428 offset = 20;
429
430 const size_t align = rpc_offset_align(&offset, 4);
431 WINPR_ASSERT(align <= UINT8_MAX);
432 auth_3_pdu.auth_verifier.auth_pad_length = (BYTE)align;
433 auth_3_pdu.auth_verifier.auth_type =
434 rpc_auth_pkg_to_security_provider(credssp_auth_pkg_name(rpc->auth));
435 auth_3_pdu.auth_verifier.auth_level = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
436 auth_3_pdu.auth_verifier.auth_reserved = 0x00;
437 auth_3_pdu.auth_verifier.auth_context_id = 0x00000000;
438 offset += (8 + auth_3_pdu.header.auth_length);
439
440 WINPR_ASSERT(offset <= UINT16_MAX);
441 auth_3_pdu.header.frag_length = (UINT16)offset;
442
443 buffer = Stream_New(nullptr, auth_3_pdu.header.frag_length);
444
445 if (!buffer)
446 goto fail;
447
448 if (!rts_write_pdu_auth3(buffer, &auth_3_pdu))
449 goto fail;
450
451 clientCall = rpc_client_call_new(auth_3_pdu.header.call_id, 0);
452
453 if (rpc_client_call_add(rpc->log, rpc->client, clientCall))
454 {
455 Stream_SealLength(buffer);
456 status = rpc_in_channel_send_pdu(inChannel, Stream_Buffer(buffer), Stream_Length(buffer));
457 }
458 else
459 rpc_client_call_free(clientCall);
460
461fail:
462 if (status <= 0)
463 rpc_client_call_discard(rpc->log, rpc->client, auth_3_pdu.header.call_id);
464 Stream_Free(buffer, TRUE);
465 return (status > 0) ? 1 : -1;
466}
467
468enum RPC_BIND_STATE rpc_bind_state(rdpRpc* rpc)
469{
470 BOOL complete = 0;
471 BOOL have_token = 0;
472 WINPR_ASSERT(rpc);
473
474 complete = credssp_auth_is_complete(rpc->auth);
475 have_token = credssp_auth_have_output_token(rpc->auth);
476
477 return complete ? (have_token ? RPC_BIND_STATE_LAST_LEG : RPC_BIND_STATE_COMPLETE)
478 : RPC_BIND_STATE_INCOMPLETE;
479}
480
481BYTE rpc_auth_pkg_to_security_provider(const char* name)
482{
483 if (strcmp(name, CREDSSP_AUTH_PKG_SPNEGO) == 0)
484 return RPC_C_AUTHN_GSS_NEGOTIATE;
485 else if (strcmp(name, CREDSSP_AUTH_PKG_NTLM) == 0)
486 return RPC_C_AUTHN_WINNT;
487 else if (strcmp(name, CREDSSP_AUTH_PKG_KERBEROS) == 0)
488 return RPC_C_AUTHN_GSS_KERBEROS;
489 else if (strcmp(name, CREDSSP_AUTH_PKG_SCHANNEL) == 0)
490 return RPC_C_AUTHN_GSS_SCHANNEL;
491 else
492 return RPC_C_AUTHN_NONE;
493}
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.