20#include <freerdp/config.h>
22#include "../settings.h"
25#include <winpr/assert.h>
26#include <winpr/cast.h>
28#include <freerdp/log.h>
30#include "rpc_client.h"
37#define TAG FREERDP_TAG("core.gateway.rpc")
39#define AUTH_PKG NTLM_SSP_NAME
54 { 0x3C, 0xDB, 0x6E, 0x7A, 0x27, 0x29 }
63 { 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60 }
72 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
112static int rpc_bind_setup(rdpRpc* rpc)
114 SEC_WINNT_AUTH_IDENTITY identity = WINPR_C_ARRAY_INIT;
118 rdpContext* context = transport_get_context(rpc->transport);
119 WINPR_ASSERT(context);
121 rdpSettings* settings = context->settings;
122 WINPR_ASSERT(settings);
124 freerdp* instance = context->instance;
125 WINPR_ASSERT(instance);
127 credssp_auth_free(rpc->auth);
128 rpc->auth = credssp_auth_new(context);
132 auth_status rc = utils_authenticate_gateway(instance, GW_AUTH_RPC);
139 freerdp_set_last_error_log(instance->context, FREERDP_ERROR_CONNECT_CANCELLED);
141 case AUTH_NO_CREDENTIALS:
142 WLog_INFO(TAG,
"No credentials provided - using nullptr identity");
149 if (!credssp_auth_init(rpc->auth, AUTH_PKG,
nullptr))
152 if (!identity_set_from_settings(&identity, settings, FreeRDP_GatewayUsername,
153 FreeRDP_GatewayDomain, FreeRDP_GatewayPassword))
159 SEC_WINNT_AUTH_IDENTITY* identityArg = (GatewayUsername ? &identity :
nullptr);
160 if (!credssp_auth_setup_client(rpc->auth,
nullptr, GatewayHostname, identityArg,
nullptr))
162 sspi_FreeAuthIdentity(&identity);
165 sspi_FreeAuthIdentity(&identity);
167 credssp_auth_set_flags(rpc->auth, ISC_REQ_USE_DCE_STYLE | ISC_REQ_DELEGATE |
168 ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT);
170 if (credssp_auth_authenticate(rpc->auth) < 0)
176int rpc_send_bind_pdu(rdpRpc* rpc, BOOL initial)
189 connection = rpc->VirtualConnection;
191 WINPR_ASSERT(connection);
193 inChannel = connection->DefaultInChannel;
195 if (initial && rpc_bind_setup(rpc) < 0)
198 WLog_DBG(TAG, initial ?
"Sending Bind PDU" :
"Sending Alter Context PDU");
200 sbuffer = credssp_auth_get_output_buffer(rpc->auth);
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.n_context_elem = 2;
216 bind_pdu.p_context_elem.reserved = 0;
217 bind_pdu.p_context_elem.reserved2 = 0;
218 bind_pdu.p_context_elem.p_cont_elem =
219 calloc(bind_pdu.p_context_elem.n_context_elem,
sizeof(
p_cont_elem_t));
221 if (!bind_pdu.p_context_elem.p_cont_elem)
225 p_cont_elem_t* p_cont_elem = &bind_pdu.p_context_elem.p_cont_elem[0];
226 p_cont_elem->p_cont_id = 0;
227 p_cont_elem->n_transfer_syn = 1;
228 p_cont_elem->reserved = 0;
229 CopyMemory(&(p_cont_elem->abstract_syntax.if_uuid), &TSGU_UUID,
sizeof(
p_uuid_t));
230 p_cont_elem->abstract_syntax.if_version = TSGU_SYNTAX_IF_VERSION;
231 rts_syntax_id_free(p_cont_elem->transfer_syntaxes);
232 p_cont_elem->transfer_syntaxes = rts_syntax_id_new(1);
234 if (!p_cont_elem->transfer_syntaxes)
237 CopyMemory(&(p_cont_elem->transfer_syntaxes[0].if_uuid), &NDR_UUID,
sizeof(
p_uuid_t));
238 p_cont_elem->transfer_syntaxes[0].if_version = NDR_SYNTAX_IF_VERSION;
241 p_cont_elem_t* p_cont_elem = &bind_pdu.p_context_elem.p_cont_elem[1];
242 p_cont_elem->p_cont_id = 1;
243 p_cont_elem->n_transfer_syn = 1;
244 p_cont_elem->reserved = 0;
245 CopyMemory(&(p_cont_elem->abstract_syntax.if_uuid), &TSGU_UUID,
sizeof(
p_uuid_t));
246 p_cont_elem->abstract_syntax.if_version = TSGU_SYNTAX_IF_VERSION;
248 rts_syntax_id_free(p_cont_elem->transfer_syntaxes);
249 p_cont_elem->transfer_syntaxes = rts_syntax_id_new(1);
251 if (!p_cont_elem->transfer_syntaxes)
254 CopyMemory(&(p_cont_elem->transfer_syntaxes[0].if_uuid), &BTFN_UUID,
sizeof(
p_uuid_t));
255 p_cont_elem->transfer_syntaxes[0].if_version = BTFN_SYNTAX_IF_VERSION;
259 bind_pdu.auth_verifier.auth_type =
260 rpc_auth_pkg_to_security_provider(credssp_auth_pkg_name(rpc->auth));
261 bind_pdu.auth_verifier.auth_level = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
262 bind_pdu.auth_verifier.auth_reserved = 0x00;
263 bind_pdu.auth_verifier.auth_context_id = 0x00000000;
264 offset += (8 + bind_pdu.header.auth_length);
266 WINPR_ASSERT(offset <= UINT16_MAX);
267 bind_pdu.header.frag_length = (UINT16)offset;
269 buffer = Stream_New(
nullptr, bind_pdu.header.frag_length);
274 if (!rts_write_pdu_bind(buffer, &bind_pdu))
277 clientCall = rpc_client_call_new(bind_pdu.header.call_id, 0);
282 if (!rpc_client_call_add(rpc->log, rpc->client, clientCall))
284 rpc_client_call_free(clientCall);
288 Stream_SealLength(buffer);
289 status = rpc_in_channel_send_pdu(inChannel, Stream_Buffer(buffer), Stream_Length(buffer));
292 rpc_client_call_discard(rpc->log, rpc->client, bind_pdu.header.call_id);
294 if (bind_pdu.p_context_elem.p_cont_elem)
296 rts_syntax_id_free(bind_pdu.p_context_elem.p_cont_elem[0].transfer_syntaxes);
297 rts_syntax_id_free(bind_pdu.p_context_elem.p_cont_elem[1].transfer_syntaxes);
300 free(bind_pdu.p_context_elem.p_cont_elem);
301 bind_pdu.p_context_elem.p_cont_elem =
nullptr;
303 Stream_Free(buffer, TRUE);
304 return (status > 0) ? 1 : -1;
333BOOL rpc_recv_bind_ack_pdu(rdpRpc* rpc,
wStream* s)
336 const BYTE* auth_data =
nullptr;
343 WINPR_ASSERT(rpc->auth);
346 pos = Stream_GetPosition(s);
347 if (!rts_read_pdu_header(s, &header))
350 WLog_DBG(TAG, header.common.ptype == PTYPE_BIND_ACK ?
"Receiving BindAck PDU"
351 :
"Receiving AlterContextResp PDU");
353 const UINT16 MAX_VALID_FRAG = 0x0FF8;
354 if ((header.bind_ack.max_xmit_frag > MAX_VALID_FRAG) ||
355 (header.bind_ack.max_recv_frag > MAX_VALID_FRAG))
358 "bind_ack: invalid fragment size: max_xmit_frag=%" PRIu16
359 ", max_recv_frag=%" PRIu16
", maximum=%" PRIu16,
360 header.bind_ack.max_xmit_frag, header.bind_ack.max_recv_frag, MAX_VALID_FRAG);
364 rpc->max_recv_frag = header.bind_ack.max_xmit_frag;
365 rpc->max_xmit_frag = header.bind_ack.max_recv_frag;
369 end = Stream_GetPosition(s);
370 if (!Stream_SetPosition(s, pos + header.common.frag_length - header.common.auth_length))
372 auth_data = Stream_ConstPointer(s);
373 if (!Stream_SetPosition(s, end))
376 buffer.cbBuffer = header.common.auth_length;
377 buffer.pvBuffer = malloc(buffer.cbBuffer);
378 if (!buffer.pvBuffer)
380 memcpy(buffer.pvBuffer, auth_data, buffer.cbBuffer);
381 credssp_auth_take_input_buffer(rpc->auth, &buffer);
383 if (credssp_auth_authenticate(rpc->auth) < 0)
388 rts_free_pdu_header(&header, FALSE);
399int rpc_send_rpc_auth_3_pdu(rdpRpc* rpc)
412 connection = rpc->VirtualConnection;
413 WINPR_ASSERT(connection);
415 inChannel = connection->DefaultInChannel;
416 WINPR_ASSERT(inChannel);
418 WLog_DBG(TAG,
"Sending RpcAuth3 PDU");
420 sbuffer = credssp_auth_get_output_buffer(rpc->auth);
425 auth_3_pdu.header = rpc_pdu_header_init(rpc);
426 auth_3_pdu.header.auth_length = (UINT16)sbuffer->cbBuffer;
427 auth_3_pdu.auth_verifier.auth_value = sbuffer->pvBuffer;
428 auth_3_pdu.header.ptype = PTYPE_RPC_AUTH_3;
429 auth_3_pdu.header.pfc_flags = PFC_FIRST_FRAG | PFC_LAST_FRAG | PFC_CONC_MPX;
430 auth_3_pdu.header.call_id = rpc->CallId++;
431 auth_3_pdu.max_xmit_frag = rpc->max_xmit_frag;
432 auth_3_pdu.max_recv_frag = rpc->max_recv_frag;
435 const size_t align = rpc_offset_align(&offset, 4);
436 WINPR_ASSERT(align <= UINT8_MAX);
437 auth_3_pdu.auth_verifier.auth_pad_length = (BYTE)align;
438 auth_3_pdu.auth_verifier.auth_type =
439 rpc_auth_pkg_to_security_provider(credssp_auth_pkg_name(rpc->auth));
440 auth_3_pdu.auth_verifier.auth_level = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
441 auth_3_pdu.auth_verifier.auth_reserved = 0x00;
442 auth_3_pdu.auth_verifier.auth_context_id = 0x00000000;
443 offset += (8 + auth_3_pdu.header.auth_length);
445 WINPR_ASSERT(offset <= UINT16_MAX);
446 auth_3_pdu.header.frag_length = (UINT16)offset;
448 buffer = Stream_New(
nullptr, auth_3_pdu.header.frag_length);
453 if (!rts_write_pdu_auth3(buffer, &auth_3_pdu))
456 clientCall = rpc_client_call_new(auth_3_pdu.header.call_id, 0);
458 if (rpc_client_call_add(rpc->log, rpc->client, clientCall))
460 Stream_SealLength(buffer);
461 status = rpc_in_channel_send_pdu(inChannel, Stream_Buffer(buffer), Stream_Length(buffer));
464 rpc_client_call_free(clientCall);
468 rpc_client_call_discard(rpc->log, rpc->client, auth_3_pdu.header.call_id);
469 Stream_Free(buffer, TRUE);
470 return (status > 0) ? 1 : -1;
473enum RPC_BIND_STATE rpc_bind_state(rdpRpc* rpc)
479 complete = credssp_auth_is_complete(rpc->auth);
480 have_token = credssp_auth_have_output_token(rpc->auth);
482 return complete ? (have_token ? RPC_BIND_STATE_LAST_LEG : RPC_BIND_STATE_COMPLETE)
483 : RPC_BIND_STATE_INCOMPLETE;
486BYTE rpc_auth_pkg_to_security_provider(
const char* name)
488 if (strcmp(name, CREDSSP_AUTH_PKG_SPNEGO) == 0)
489 return RPC_C_AUTHN_GSS_NEGOTIATE;
490 else if (strcmp(name, CREDSSP_AUTH_PKG_NTLM) == 0)
491 return RPC_C_AUTHN_WINNT;
492 else if (strcmp(name, CREDSSP_AUTH_PKG_KERBEROS) == 0)
493 return RPC_C_AUTHN_GSS_KERBEROS;
494 else if (strcmp(name, CREDSSP_AUTH_PKG_SCHANNEL) == 0)
495 return RPC_C_AUTHN_GSS_SCHANNEL;
497 return RPC_C_AUTHN_NONE;
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.