FreeRDP
Loading...
Searching...
No Matches
arm.c
1
21#include <freerdp/config.h>
22#include <freerdp/version.h>
23
24#include "../settings.h"
25
26#include <winpr/assert.h>
27
28#include <winpr/crt.h>
29#include <winpr/synch.h>
30#include <winpr/print.h>
31#include <winpr/stream.h>
32#include <winpr/winsock.h>
33#include <winpr/cred.h>
34#include <winpr/bcrypt.h>
35
36#include <freerdp/log.h>
37#include <freerdp/error.h>
38#include <freerdp/crypto/certificate.h>
39#include <freerdp/utils/ringbuffer.h>
40#include <freerdp/utils/smartcardlogon.h>
41#include <freerdp/utils/aad.h>
42
43#include "arm.h"
44#include "wst.h"
45#include "websocket.h"
46#include "http.h"
47#include "../credssp_auth.h"
48#include "../proxy.h"
49#include "../rdp.h"
50#include "../../crypto/crypto.h"
51#include "../../crypto/certificate.h"
52#include "../../crypto/opensslcompat.h"
53#include "rpc_fault.h"
54#include "../utils.h"
55#include "../redirection.h"
56
57#include <winpr/json.h>
58
59#include <string.h>
60
61struct rdp_arm
62{
63 rdpContext* context;
64 rdpTls* tls;
65 HttpContext* http;
66
67 UINT32 gateway_retry;
68 wLog* log;
69};
70
71typedef struct rdp_arm rdpArm;
72
73#define TAG FREERDP_TAG("core.gateway.arm")
74
75#ifdef WITH_AAD
76static BOOL arm_tls_connect(rdpArm* arm, rdpTls* tls, UINT32 timeout)
77{
78 WINPR_ASSERT(arm);
79 WINPR_ASSERT(tls);
80 int sockfd = 0;
81 long status = 0;
82 BIO* socketBio = nullptr;
83 BIO* bufferedBio = nullptr;
84 rdpSettings* settings = arm->context->settings;
85 if (!settings)
86 return FALSE;
87
88 const char* peerHostname = freerdp_settings_get_string(settings, FreeRDP_GatewayHostname);
89 if (!peerHostname)
90 return FALSE;
91
92 UINT16 peerPort = (UINT16)freerdp_settings_get_uint32(settings, FreeRDP_GatewayPort);
93 const char* proxyUsername = nullptr;
94 const char* proxyPassword = nullptr;
95 BOOL isProxyConnection =
96 proxy_prepare(settings, &peerHostname, &peerPort, &proxyUsername, &proxyPassword);
97
98 sockfd = freerdp_tcp_connect(arm->context, peerHostname, peerPort, timeout);
99
100 WLog_Print(arm->log, WLOG_DEBUG, "connecting to %s %d", peerHostname, peerPort);
101 if (sockfd < 0)
102 return FALSE;
103
104 socketBio = BIO_new(BIO_s_simple_socket());
105
106 if (!socketBio)
107 {
108 closesocket((SOCKET)sockfd);
109 return FALSE;
110 }
111
112 BIO_set_fd(socketBio, sockfd, BIO_CLOSE);
113 bufferedBio = BIO_new(BIO_s_buffered_socket());
114
115 if (!bufferedBio)
116 {
117 BIO_free_all(socketBio);
118 return FALSE;
119 }
120
121 bufferedBio = BIO_push(bufferedBio, socketBio);
122 if (!bufferedBio)
123 return FALSE;
124
125 status = BIO_set_nonblock(bufferedBio, TRUE);
126
127 if (isProxyConnection)
128 {
129 if (!proxy_connect(arm->context, bufferedBio, proxyUsername, proxyPassword,
130 freerdp_settings_get_string(settings, FreeRDP_GatewayHostname),
131 (UINT16)freerdp_settings_get_uint32(settings, FreeRDP_GatewayPort)))
132 {
133 BIO_free_all(bufferedBio);
134 return FALSE;
135 }
136 }
137
138 if (!status)
139 {
140 BIO_free_all(bufferedBio);
141 return FALSE;
142 }
143
144 tls->hostname = freerdp_settings_get_string(settings, FreeRDP_GatewayHostname);
145 tls->port = MIN(UINT16_MAX, WINPR_ASSERTING_INT_CAST(int32_t, settings->GatewayPort));
146 tls->isGatewayTransport = TRUE;
147 status = freerdp_tls_connect(tls, bufferedBio);
148 if (status < 1)
149 {
150 rdpContext* context = arm->context;
151 if (status < 0)
152 freerdp_set_last_error_if_not(context, FREERDP_ERROR_TLS_CONNECT_FAILED);
153 else
154 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
155
156 return FALSE;
157 }
158 return (status >= 1);
159}
160
161static BOOL arm_fetch_wellknown(rdpArm* arm)
162{
163 WINPR_ASSERT(arm);
164 WINPR_ASSERT(arm->context);
165 WINPR_ASSERT(arm->context->rdp);
166
167 rdpRdp* rdp = arm->context->rdp;
168 if (rdp->wellknown)
169 return TRUE;
170
171 const char* base =
172 freerdp_settings_get_string(arm->context->settings, FreeRDP_GatewayAzureActiveDirectory);
173 const BOOL useTenant =
174 freerdp_settings_get_bool(arm->context->settings, FreeRDP_GatewayAvdUseTenantid);
175 const char* tenantid = "common";
176 if (useTenant)
177 tenantid =
178 freerdp_settings_get_string(arm->context->settings, FreeRDP_GatewayAvdAadtenantid);
179
180 rdp->wellknown = freerdp_utils_aad_get_wellknown(arm->log, base, tenantid);
181 return (rdp->wellknown != nullptr);
182}
183
184static wStream* arm_build_http_request(rdpArm* arm, const char* method,
185 TRANSFER_ENCODING transferEncoding, const char* content_type,
186 size_t content_length)
187{
188 wStream* s = nullptr;
189
190 WINPR_ASSERT(arm);
191 WINPR_ASSERT(method);
192 WINPR_ASSERT(content_type);
193
194 WINPR_ASSERT(arm->context);
195 WINPR_ASSERT(arm->context->rdp);
196
197 const char* uri = http_context_get_uri(arm->http);
198 HttpRequest* request = http_request_new();
199
200 if (!request)
201 return nullptr;
202
203 rdpSettings* settings = arm->context->settings;
204
205 if (!http_request_set_method(request, method) || !http_request_set_uri(request, uri))
206 goto out;
207
208 if (!freerdp_settings_get_string(settings, FreeRDP_GatewayHttpExtAuthBearer))
209 {
210 char* token = nullptr;
211
212 pGetCommonAccessToken GetCommonAccessToken = freerdp_get_common_access_token(arm->context);
213 if (!GetCommonAccessToken)
214 {
215 WLog_Print(arm->log, WLOG_ERROR, "No authorization token provided");
216 goto out;
217 }
218
219 if (!arm_fetch_wellknown(arm))
220 goto out;
221
222 if (!GetCommonAccessToken(arm->context, ACCESS_TOKEN_TYPE_AVD, &token, 0))
223 {
224 WLog_Print(arm->log, WLOG_ERROR, "Unable to obtain access token");
225 goto out;
226 }
227
228 if (!freerdp_settings_set_string(settings, FreeRDP_GatewayHttpExtAuthBearer, token))
229 {
230 free(token);
231 goto out;
232 }
233 free(token);
234 }
235
236 if (!http_request_set_auth_scheme(request, "Bearer") ||
237 !http_request_set_auth_param(
238 request, freerdp_settings_get_string(settings, FreeRDP_GatewayHttpExtAuthBearer)))
239 goto out;
240
241 if (!http_request_set_transfer_encoding(request, transferEncoding) ||
242 !http_request_set_content_length(request, content_length) ||
243 !http_request_set_content_type(request, content_type))
244 goto out;
245
246 s = http_request_write(arm->http, request);
247out:
248 http_request_free(request);
249
250 if (s)
251 Stream_SealLength(s);
252
253 return s;
254}
255
256static BOOL arm_send_http_request(rdpArm* arm, rdpTls* tls, const char* method,
257 const char* content_type, const char* data, size_t content_length)
258{
259 int status = -1;
260 wStream* s =
261 arm_build_http_request(arm, method, TransferEncodingIdentity, content_type, content_length);
262
263 if (!s)
264 return FALSE;
265
266 const size_t sz = Stream_Length(s);
267 WLog_Print(arm->log, WLOG_TRACE, "header [%" PRIuz "]: %s", sz, Stream_Buffer(s));
268 WLog_Print(arm->log, WLOG_TRACE, "body [%" PRIuz "]: %s", content_length, data);
269 status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz);
270
271 Stream_Free(s, TRUE);
272 if (status >= 0 && content_length > 0 && data)
273 status = freerdp_tls_write_all(tls, (const BYTE*)data, content_length);
274
275 return (status >= 0);
276}
277
278static void arm_free(rdpArm* arm)
279{
280 if (!arm)
281 return;
282
283 freerdp_tls_free(arm->tls);
284 http_context_free(arm->http);
285
286 free(arm);
287}
288
289static rdpArm* arm_new(rdpContext* context)
290{
291 WINPR_ASSERT(context);
292
293 rdpArm* arm = (rdpArm*)calloc(1, sizeof(rdpArm));
294 if (!arm)
295 goto fail;
296
297 arm->log = WLog_Get(TAG);
298 arm->context = context;
299 arm->tls = freerdp_tls_new(context);
300 if (!arm->tls)
301 goto fail;
302
303 arm->http = http_context_new();
304
305 if (!arm->http)
306 goto fail;
307
308 return arm;
309
310fail:
311 arm_free(arm);
312 return nullptr;
313}
314
315static char* arm_create_request_json(rdpArm* arm)
316{
317 char* lbi = nullptr;
318 char* message = nullptr;
319
320 WINPR_ASSERT(arm);
321
322 WINPR_JSON* json = WINPR_JSON_CreateObject();
323 if (!json)
324 goto arm_create_cleanup;
326 json, "application",
327 freerdp_settings_get_string(arm->context->settings, FreeRDP_RemoteApplicationProgram)))
328 goto arm_create_cleanup;
329
330 lbi = calloc(
331 freerdp_settings_get_uint32(arm->context->settings, FreeRDP_LoadBalanceInfoLength) + 1,
332 sizeof(char));
333 if (!lbi)
334 goto arm_create_cleanup;
335
336 {
337 const size_t len =
338 freerdp_settings_get_uint32(arm->context->settings, FreeRDP_LoadBalanceInfoLength);
339 memcpy(lbi, freerdp_settings_get_pointer(arm->context->settings, FreeRDP_LoadBalanceInfo),
340 len);
341 }
342
343 if (!WINPR_JSON_AddStringToObject(json, "loadBalanceInfo", lbi))
344 goto arm_create_cleanup;
345 if (!WINPR_JSON_AddNullToObject(json, "LogonToken"))
346 goto arm_create_cleanup;
347 if (!WINPR_JSON_AddNullToObject(json, "gatewayLoadBalancerToken"))
348 goto arm_create_cleanup;
349
350 message = WINPR_JSON_PrintUnformatted(json);
351arm_create_cleanup:
352 if (json)
353 WINPR_JSON_Delete(json);
354 free(lbi);
355 return message;
356}
357
372static WINPR_CIPHER_CTX* treatAuthBlob(wLog* log, const BYTE* pbInput, size_t cbInput,
373 size_t* pBlockSize)
374{
375 WINPR_CIPHER_CTX* ret = nullptr;
376 char algoName[100] = WINPR_C_ARRAY_INIT;
377
378 WINPR_ASSERT(pBlockSize);
379 SSIZE_T algoSz = ConvertWCharNToUtf8((const WCHAR*)pbInput, cbInput / sizeof(WCHAR), algoName,
380 sizeof(algoName) - 1);
381 if (algoSz <= 0)
382 {
383 WLog_Print(log, WLOG_ERROR, "invalid algoName");
384 return nullptr;
385 }
386
387 if (strcmp(algoName, "AES") != 0)
388 {
389 WLog_Print(log, WLOG_ERROR, "only AES is supported for now");
390 return nullptr;
391 }
392
393 *pBlockSize = WINPR_AES_BLOCK_SIZE;
394 const size_t algoLen = WINPR_ASSERTING_INT_CAST(size_t, (algoSz + 1)) * sizeof(WCHAR);
395 if (cbInput < algoLen)
396 {
397 WLog_Print(log, WLOG_ERROR, "invalid AuthBlob size");
398 return nullptr;
399 }
400
401 cbInput -= algoLen;
402
403 /* BCRYPT_KEY_DATA_BLOB_HEADER */
404 wStream staticStream = WINPR_C_ARRAY_INIT;
405 wStream* s = Stream_StaticConstInit(&staticStream, &pbInput[algoLen], cbInput);
406
407 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
408 return nullptr;
409
410 const UINT32 dwMagic = Stream_Get_UINT32(s);
411 if (dwMagic != BCRYPT_KEY_DATA_BLOB_MAGIC)
412 {
413 WLog_Print(log, WLOG_ERROR, "unsupported authBlob type");
414 return nullptr;
415 }
416
417 const UINT32 dwVersion = Stream_Get_UINT32(s);
418 if (dwVersion != BCRYPT_KEY_DATA_BLOB_VERSION1)
419 {
420 WLog_Print(log, WLOG_ERROR, "unsupported authBlob version %" PRIu32 ", expecting %d",
421 dwVersion, BCRYPT_KEY_DATA_BLOB_VERSION1);
422 return nullptr;
423 }
424
425 const UINT32 cbKeyData = Stream_Get_UINT32(s);
426 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, cbKeyData))
427 {
428 WLog_Print(log, WLOG_ERROR, "invalid authBlob size");
429 return nullptr;
430 }
431
432 WINPR_CIPHER_TYPE cipherType = WINPR_CIPHER_NONE;
433 switch (cbKeyData)
434 {
435 case 16:
436 cipherType = WINPR_CIPHER_AES_128_CBC;
437 break;
438 case 24:
439 cipherType = WINPR_CIPHER_AES_192_CBC;
440 break;
441 case 32:
442 cipherType = WINPR_CIPHER_AES_256_CBC;
443 break;
444 default:
445 WLog_Print(log, WLOG_ERROR, "invalid authBlob cipher size");
446 return nullptr;
447 }
448
449 ret = winpr_Cipher_NewEx(cipherType, WINPR_ENCRYPT, Stream_Pointer(s), cbKeyData, nullptr, 0);
450 if (!ret)
451 {
452 WLog_Print(log, WLOG_ERROR, "error creating cipher");
453 return nullptr;
454 }
455
456 if (!winpr_Cipher_SetPadding(ret, TRUE))
457 {
458 WLog_Print(log, WLOG_ERROR, "unable to enable padding on cipher");
459 winpr_Cipher_Free(ret);
460 return nullptr;
461 }
462
463 return ret;
464}
465
466static BOOL arm_stringEncodeW(const BYTE* pin, size_t cbIn, BYTE** ppOut, size_t* pcbOut)
467{
468 *ppOut = nullptr;
469 *pcbOut = 0;
470
471 /* encode to base64 with crlf */
472 char* b64encoded = crypto_base64_encode_ex(pin, cbIn, TRUE);
473 if (!b64encoded)
474 return FALSE;
475
476 /* and then convert to Unicode */
477 size_t outSz = 0;
478 *ppOut = (BYTE*)ConvertUtf8NToWCharAlloc(b64encoded, strlen(b64encoded), &outSz);
479 free(b64encoded);
480
481 if (!*ppOut)
482 return FALSE;
483
484 *pcbOut = (outSz + 1) * sizeof(WCHAR);
485 return TRUE;
486}
487
488static BOOL arm_encodeRedirectPasswd(wLog* log, rdpSettings* settings, const rdpCertificate* cert,
489 WINPR_CIPHER_CTX* cipher, size_t blockSize)
490{
491 BOOL ret = FALSE;
492 BYTE* output = nullptr;
493 BYTE* finalOutput = nullptr;
494
495 /* let's prepare the encrypted password, first we do a
496 * cipheredPass = AES(redirectedAuthBlob, toUtf16(passwd))
497 */
498
499 size_t wpasswdLen = 0;
500 WCHAR* wpasswd = freerdp_settings_get_string_as_utf16(settings, FreeRDP_Password, &wpasswdLen);
501 if (!wpasswd)
502 {
503 WLog_Print(log, WLOG_ERROR, "error when converting password to UTF16");
504 return FALSE;
505 }
506
507 const size_t wpasswdBytes = (wpasswdLen + 1) * sizeof(WCHAR);
508 BYTE* encryptedPass = calloc(1, wpasswdBytes + blockSize); /* 16: block size of AES (padding) */
509
510 if (!encryptedPass)
511 goto out;
512
513 {
514 size_t encryptedPassLen = 0;
515 if (!winpr_Cipher_Update(cipher, wpasswd, wpasswdBytes, encryptedPass, &encryptedPassLen))
516 goto out;
517
518 if (encryptedPassLen > wpasswdBytes)
519 goto out;
520
521 {
522 size_t finalLen = 0;
523 if (!winpr_Cipher_Final(cipher, &encryptedPass[encryptedPassLen], &finalLen))
524 {
525 WLog_Print(log, WLOG_ERROR, "error when ciphering password");
526 goto out;
527 }
528 encryptedPassLen += finalLen;
529 }
530
531 /* then encrypt(cipheredPass, publicKey(redirectedServerCert) */
532 {
533 size_t output_length = 0;
534 if (!freerdp_certificate_publickey_encrypt(cert, encryptedPass, encryptedPassLen,
535 &output, &output_length))
536 {
537 WLog_Print(log, WLOG_ERROR, "unable to encrypt with the server's public key");
538 goto out;
539 }
540
541 {
542 size_t finalOutputLen = 0;
543 if (!arm_stringEncodeW(output, output_length, &finalOutput, &finalOutputLen))
544 {
545 WLog_Print(log, WLOG_ERROR, "unable to base64+utf16 final blob");
546 goto out;
547 }
548
549 if (!freerdp_settings_set_pointer_len(settings, FreeRDP_RedirectionPassword,
550 finalOutput, finalOutputLen))
551 {
552 WLog_Print(log, WLOG_ERROR,
553 "unable to set the redirection password in settings");
554 goto out;
555 }
556 }
557 }
558 }
559
560 settings->RdstlsSecurity = TRUE;
561 settings->AadSecurity = FALSE;
562 settings->NlaSecurity = FALSE;
563 settings->RdpSecurity = FALSE;
564 settings->TlsSecurity = FALSE;
565 settings->RedirectionFlags = LB_PASSWORD_IS_PK_ENCRYPTED;
566 ret = TRUE;
567out:
568 free(finalOutput);
569 free(output);
570 free(encryptedPass);
571 free(wpasswd);
572 return ret;
573}
574
579static BOOL arm_pick_base64Utf16Field(wLog* log, const WINPR_JSON* json, const char* name,
580 BYTE** poutput, size_t* plen)
581{
582 *poutput = nullptr;
583 *plen = 0;
584
585 WINPR_JSON* node = WINPR_JSON_GetObjectItemCaseSensitive(json, name);
586 if (!node || !WINPR_JSON_IsString(node))
587 return TRUE;
588
589 const char* nodeValue = WINPR_JSON_GetStringValue(node);
590 if (!nodeValue)
591 return TRUE;
592
593 BYTE* output1 = nullptr;
594 size_t len1 = 0;
595 crypto_base64_decode(nodeValue, strlen(nodeValue), &output1, &len1);
596 if (!output1 || !len1)
597 {
598 WLog_Print(log, WLOG_ERROR, "error when first unbase64 for %s", name);
599 free(output1);
600 return FALSE;
601 }
602
603 size_t len2 = 0;
604 char* output2 = ConvertWCharNToUtf8Alloc((WCHAR*)output1, len1 / sizeof(WCHAR), &len2);
605 free(output1);
606 if (!output2 || !len2)
607 {
608 WLog_Print(log, WLOG_ERROR, "error when decode('utf-16') for %s", name);
609 free(output2);
610 return FALSE;
611 }
612
613 BYTE* output = nullptr;
614 crypto_base64_decode(output2, len2, &output, plen);
615 free(output2);
616 if (!output || !*plen)
617 {
618 WLog_Print(log, WLOG_ERROR, "error when second unbase64 for %s", name);
619 free(output);
620 return FALSE;
621 }
622
623 *poutput = output;
624 return TRUE;
625}
626
647static size_t arm_parse_ipvx_count(WINPR_JSON* ipvX)
648{
649 WINPR_ASSERT(ipvX);
650 WINPR_JSON* ipAddress = WINPR_JSON_GetObjectItemCaseSensitive(ipvX, "ipAddress");
651 if (!ipAddress || !WINPR_JSON_IsArray(ipAddress))
652 return 0;
653
654 /* Each entry might contain a public and a private address */
655 return WINPR_JSON_GetArraySize(ipAddress) * 2;
656}
657
658static BOOL arm_parse_ipv6(rdpSettings* settings, WINPR_JSON* ipv6, size_t* pAddressIdx)
659{
660 WINPR_ASSERT(settings);
661 WINPR_ASSERT(ipv6);
662 WINPR_ASSERT(pAddressIdx);
663
664 if (!freerdp_settings_get_bool(settings, FreeRDP_IPv6Enabled))
665 return TRUE;
666
667 WINPR_JSON* ipAddress = WINPR_JSON_GetObjectItemCaseSensitive(ipv6, "ipAddress");
668 if (!ipAddress || !WINPR_JSON_IsArray(ipAddress))
669 return TRUE;
670 const size_t naddresses = WINPR_JSON_GetArraySize(ipAddress);
671 const uint32_t count = freerdp_settings_get_uint32(settings, FreeRDP_TargetNetAddressCount);
672 for (size_t j = 0; j < naddresses; j++)
673 {
674 WINPR_JSON* adressN = WINPR_JSON_GetArrayItem(ipAddress, j);
675 if (!adressN || !WINPR_JSON_IsString(adressN))
676 continue;
677
678 const char* addr = WINPR_JSON_GetStringValue(adressN);
679 if (utils_str_is_empty(addr))
680 continue;
681
682 if (*pAddressIdx >= count)
683 {
684 WLog_ERR(TAG, "Exceeded TargetNetAddresses, parsing failed");
685 return FALSE;
686 }
687
688 if (!freerdp_settings_set_pointer_array(settings, FreeRDP_TargetNetAddresses,
689 (*pAddressIdx)++, addr))
690 return FALSE;
691 }
692 return TRUE;
693}
694
695static BOOL arm_parse_ipv4(rdpSettings* settings, WINPR_JSON* ipv4, size_t* pAddressIdx)
696{
697 WINPR_ASSERT(settings);
698 WINPR_ASSERT(ipv4);
699 WINPR_ASSERT(pAddressIdx);
700
701 WINPR_JSON* ipAddress = WINPR_JSON_GetObjectItemCaseSensitive(ipv4, "ipAddress");
702 if (!ipAddress || !WINPR_JSON_IsArray(ipAddress))
703 return TRUE;
704
705 const size_t naddresses = WINPR_JSON_GetArraySize(ipAddress);
706 const uint32_t count = freerdp_settings_get_uint32(settings, FreeRDP_TargetNetAddressCount);
707 for (size_t j = 0; j < naddresses; j++)
708 {
709 WINPR_JSON* adressN = WINPR_JSON_GetArrayItem(ipAddress, j);
710 if (!adressN)
711 continue;
712
713 WINPR_JSON* publicIpNode =
714 WINPR_JSON_GetObjectItemCaseSensitive(adressN, "publicIpAddress");
715 if (publicIpNode && WINPR_JSON_IsString(publicIpNode))
716 {
717 const char* publicIp = WINPR_JSON_GetStringValue(publicIpNode);
718 if (!utils_str_is_empty(publicIp))
719 {
720 if (*pAddressIdx >= count)
721 {
722 WLog_ERR(TAG, "Exceeded TargetNetAddresses, parsing failed");
723 return FALSE;
724 }
725 if (!freerdp_settings_set_pointer_array(settings, FreeRDP_TargetNetAddresses,
726 (*pAddressIdx)++, publicIp))
727 return FALSE;
728 }
729 }
730
731 WINPR_JSON* privateIpNode =
732 WINPR_JSON_GetObjectItemCaseSensitive(adressN, "privateIpAddress");
733 if (privateIpNode && WINPR_JSON_IsString(privateIpNode))
734 {
735 const char* privateIp = WINPR_JSON_GetStringValue(privateIpNode);
736 if (!utils_str_is_empty(privateIp))
737 {
738 if (*pAddressIdx >= count)
739 {
740 WLog_ERR(TAG, "Exceeded TargetNetAddresses, parsing failed");
741 return FALSE;
742 }
743 if (!freerdp_settings_set_pointer_array(settings, FreeRDP_TargetNetAddresses,
744 (*pAddressIdx)++, privateIp))
745 return FALSE;
746 }
747 }
748 }
749 return TRUE;
750}
751
752static BOOL arm_treat_azureInstanceNetworkMetadata(wLog* log, const char* metadata,
753 rdpSettings* settings)
754{
755 BOOL ret = FALSE;
756
757 WINPR_ASSERT(settings);
758
759 if (!freerdp_target_net_adresses_reset(settings, 0))
760 return FALSE;
761
762 WINPR_JSON* json = WINPR_JSON_Parse(metadata);
763 if (!json)
764 {
765 WLog_Print(log, WLOG_ERROR, "invalid azureInstanceNetworkMetadata");
766 return FALSE;
767 }
768
769 WINPR_JSON* iface = WINPR_JSON_GetObjectItemCaseSensitive(json, "interface");
770 if (!iface)
771 {
772 ret = TRUE;
773 goto out;
774 }
775
776 if (!WINPR_JSON_IsArray(iface))
777 {
778 WLog_Print(log, WLOG_ERROR, "expecting interface to be an Array");
779 goto out;
780 }
781
782 {
783 const size_t interfaceSz = WINPR_JSON_GetArraySize(iface);
784 if (interfaceSz == 0)
785 {
786 WLog_WARN(TAG, "no addresses in azure instance metadata");
787 ret = TRUE;
788 goto out;
789 }
790
791 {
792 size_t count = 0;
793 for (size_t i = 0; i < interfaceSz; i++)
794 {
795 WINPR_JSON* interN = WINPR_JSON_GetArrayItem(iface, i);
796 if (!interN)
797 continue;
798
799 WINPR_JSON* ipv6 = WINPR_JSON_GetObjectItemCaseSensitive(interN, "ipv6");
800 if (ipv6)
801 count += arm_parse_ipvx_count(ipv6);
802
803 WINPR_JSON* ipv4 = WINPR_JSON_GetObjectItemCaseSensitive(interN, "ipv4");
804 if (ipv4)
805 count += arm_parse_ipvx_count(ipv4);
806 }
807
808 if (!freerdp_target_net_adresses_reset(settings, count))
809 return FALSE;
810 }
811
812 {
813 size_t addressIdx = 0;
814 for (size_t i = 0; i < interfaceSz; i++)
815 {
816 WINPR_JSON* interN = WINPR_JSON_GetArrayItem(iface, i);
817 if (!interN)
818 continue;
819
820 WINPR_JSON* ipv6 = WINPR_JSON_GetObjectItemCaseSensitive(interN, "ipv6");
821 if (ipv6)
822 {
823 if (!arm_parse_ipv6(settings, ipv6, &addressIdx))
824 goto out;
825 }
826
827 WINPR_JSON* ipv4 = WINPR_JSON_GetObjectItemCaseSensitive(interN, "ipv4");
828 if (ipv4)
829 {
830 if (!arm_parse_ipv4(settings, ipv4, &addressIdx))
831 goto out;
832 }
833 }
834 if (addressIdx > UINT32_MAX)
835 goto out;
836
837 if (!freerdp_settings_set_uint32(settings, FreeRDP_TargetNetAddressCount,
838 (UINT32)addressIdx))
839 goto out;
840
841 ret = addressIdx > 0;
842 }
843 }
844
845out:
846 WINPR_JSON_Delete(json);
847 return ret;
848}
849
850static void zfree(char* str)
851{
852 if (str)
853 {
854 char* cur = str;
855 while (*cur != '\0')
856 *cur++ = '\0';
857 }
858 free(str);
859}
860
861static BOOL arm_fill_rdstls(rdpArm* arm, rdpSettings* settings, const WINPR_JSON* json,
862 const rdpCertificate* redirectedServerCert)
863{
864 WINPR_ASSERT(arm);
865 BOOL ret = FALSE;
866 BYTE* authBlob = nullptr;
867 WCHAR* wGUID = nullptr;
868
869 const char* redirUser = freerdp_settings_get_string(settings, FreeRDP_RedirectionUsername);
870 if (redirUser)
871 {
872 if (!freerdp_settings_set_string(settings, FreeRDP_Username, redirUser))
873 goto end;
874 }
875
876 /* Azure/Entra requires the domain field to be set to 'AzureAD' in most cases.
877 * Some setups have been reported to require a different one, so only supply the suggested
878 * default if there was no other domain provided.
879 */
880 {
881 const char* redirDomain = freerdp_settings_get_string(settings, FreeRDP_Domain);
882 if (!redirDomain)
883 {
884 if (!freerdp_settings_set_string(settings, FreeRDP_Domain, "AzureAD"))
885 goto end;
886 }
887 }
888
889 {
890 const char* duser = freerdp_settings_get_string(settings, FreeRDP_Username);
891 const char* ddomain = freerdp_settings_get_string(settings, FreeRDP_Domain);
892 const char* dpwd = freerdp_settings_get_string(settings, FreeRDP_Password);
893 if (!duser || !dpwd)
894 {
895 WINPR_ASSERT(arm->context);
896 WINPR_ASSERT(arm->context->instance);
897
898 char* username = nullptr;
899 char* password = nullptr;
900 char* domain = nullptr;
901
902 if (ddomain)
903 domain = _strdup(ddomain);
904 if (duser)
905 username = _strdup(duser);
906
907 const BOOL rc =
908 IFCALLRESULT(FALSE, arm->context->instance->AuthenticateEx, arm->context->instance,
909 &username, &password, &domain, AUTH_RDSTLS);
910
911 const BOOL rc1 = freerdp_settings_set_string(settings, FreeRDP_Username, username);
912 const BOOL rc2 = freerdp_settings_set_string(settings, FreeRDP_Password, password);
913 const BOOL rc3 = freerdp_settings_set_string(settings, FreeRDP_Domain, domain);
914 zfree(username);
915 zfree(password);
916 zfree(domain);
917 if (!rc || !rc1 || !rc2 || !rc3)
918 goto end;
919 }
920 }
921
922 /* redirectedAuthGuid */
923 {
924 WINPR_JSON* redirectedAuthGuidNode =
925 WINPR_JSON_GetObjectItemCaseSensitive(json, "redirectedAuthGuid");
926 if (!redirectedAuthGuidNode || !WINPR_JSON_IsString(redirectedAuthGuidNode))
927 goto end;
928
929 {
930 const char* redirectedAuthGuid = WINPR_JSON_GetStringValue(redirectedAuthGuidNode);
931 if (!redirectedAuthGuid)
932 goto end;
933
934 {
935 size_t wGUID_len = 0;
936 wGUID = ConvertUtf8ToWCharAlloc(redirectedAuthGuid, &wGUID_len);
937 if (!wGUID || (wGUID_len == 0))
938 {
939 WLog_Print(arm->log, WLOG_ERROR,
940 "unable to allocate space for redirectedAuthGuid");
941 goto end;
942 }
943
944 {
945 const BOOL status = freerdp_settings_set_pointer_len(
946 settings, FreeRDP_RedirectionGuid, wGUID, (wGUID_len + 1) * sizeof(WCHAR));
947
948 if (!status)
949 {
950 WLog_Print(arm->log, WLOG_ERROR, "unable to set RedirectionGuid");
951 goto end;
952 }
953 }
954 }
955 }
956 }
957
958 /* redirectedAuthBlob */
959 {
960 size_t authBlobLen = 0;
961 if (!arm_pick_base64Utf16Field(arm->log, json, "redirectedAuthBlob", &authBlob,
962 &authBlobLen))
963 goto end;
964
965 {
966 size_t blockSize = 0;
967 WINPR_CIPHER_CTX* cipher = treatAuthBlob(arm->log, authBlob, authBlobLen, &blockSize);
968 if (!cipher)
969 goto end;
970
971 {
972 const BOOL rerp = arm_encodeRedirectPasswd(arm->log, settings, redirectedServerCert,
973 cipher, blockSize);
974 winpr_Cipher_Free(cipher);
975 if (!rerp)
976 goto end;
977 }
978 }
979 }
980
981 ret = TRUE;
982
983end:
984 free(wGUID);
985 free(authBlob);
986 return ret;
987}
988
989static BOOL arm_fill_gateway_parameters(rdpArm* arm, const char* message, size_t len)
990{
991 WINPR_ASSERT(arm);
992 WINPR_ASSERT(arm->context);
993 WINPR_ASSERT(message);
994
995 rdpCertificate* redirectedServerCert = nullptr;
996 WINPR_JSON* json = WINPR_JSON_ParseWithLength(message, len);
997 BOOL status = FALSE;
998 if (!json)
999 {
1000 WLog_Print(arm->log, WLOG_ERROR, "Response data is not valid JSON: %s",
1002 return FALSE;
1003 }
1004
1005 if (WLog_IsLevelActive(arm->log, WLOG_DEBUG))
1006 {
1007 char* str = WINPR_JSON_PrintUnformatted(json);
1008 WLog_Print(arm->log, WLOG_DEBUG, "Got HTTP Response data: %s", str);
1009 free(str);
1010 }
1011
1012 rdpSettings* settings = arm->context->settings;
1013 WINPR_JSON* gwurl = WINPR_JSON_GetObjectItemCaseSensitive(json, "gatewayLocationPreWebSocket");
1014 if (!gwurl)
1015 gwurl = WINPR_JSON_GetObjectItemCaseSensitive(json, "gatewayLocation");
1016 const char* gwurlstr = WINPR_JSON_GetStringValue(gwurl);
1017 if (gwurlstr != nullptr)
1018 {
1019 WLog_Print(arm->log, WLOG_DEBUG, "extracted target url %s", gwurlstr);
1020 if (!freerdp_settings_set_string(settings, FreeRDP_GatewayUrl, gwurlstr))
1021 goto fail;
1022 }
1023
1024 {
1025 WINPR_JSON* serverNameNode =
1026 WINPR_JSON_GetObjectItemCaseSensitive(json, "redirectedServerName");
1027 if (serverNameNode)
1028 {
1029 const char* serverName = WINPR_JSON_GetStringValue(serverNameNode);
1030 if (serverName)
1031 {
1032 if (!freerdp_settings_set_string(settings, FreeRDP_ServerHostname, serverName))
1033 goto fail;
1034 }
1035 }
1036 }
1037
1038 {
1039 const char key[] = "redirectedUsername";
1040 if (WINPR_JSON_HasObjectItem(json, key))
1041 {
1042 const char* userName = nullptr;
1043 WINPR_JSON* userNameNode = WINPR_JSON_GetObjectItemCaseSensitive(json, key);
1044 if (userNameNode)
1045 userName = WINPR_JSON_GetStringValue(userNameNode);
1046 if (!freerdp_settings_set_string(settings, FreeRDP_RedirectionUsername, userName))
1047 goto fail;
1048 }
1049 }
1050
1051 {
1052 WINPR_JSON* azureMeta =
1053 WINPR_JSON_GetObjectItemCaseSensitive(json, "azureInstanceNetworkMetadata");
1054 if (azureMeta && WINPR_JSON_IsString(azureMeta))
1055 {
1056 if (!arm_treat_azureInstanceNetworkMetadata(
1057 arm->log, WINPR_JSON_GetStringValue(azureMeta), settings))
1058 {
1059 WLog_Print(arm->log, WLOG_ERROR,
1060 "error when treating azureInstanceNetworkMetadata");
1061 goto fail;
1062 }
1063 }
1064 }
1065
1066 /* redirectedServerCert */
1067 {
1068 size_t certLen = 0;
1069 BYTE* cert = nullptr;
1070 if (arm_pick_base64Utf16Field(arm->log, json, "redirectedServerCert", &cert, &certLen))
1071 {
1072 const BOOL rc = rdp_redirection_read_target_cert(&redirectedServerCert, cert, certLen);
1073 free(cert);
1074 if (!rc)
1075 goto fail;
1076 else if (!rdp_set_target_certificate(settings, redirectedServerCert))
1077 goto fail;
1078 }
1079 }
1080
1081 if (freerdp_settings_get_bool(settings, FreeRDP_AadSecurity))
1082 status = TRUE;
1083 else
1084 status = arm_fill_rdstls(arm, settings, json, redirectedServerCert);
1085
1086fail:
1087 WINPR_JSON_Delete(json);
1088 freerdp_certificate_free(redirectedServerCert);
1089 return status;
1090}
1091
1092static BOOL arm_handle_request_ok(rdpArm* arm, const HttpResponse* response)
1093{
1094 const size_t len = http_response_get_body_length(response);
1095 const char* msg = http_response_get_body(response);
1096 const size_t alen = strnlen(msg, len + 1);
1097 if (alen > len)
1098 {
1099 WLog_Print(arm->log, WLOG_ERROR, "Got HTTP Response data with invalid termination");
1100 return FALSE;
1101 }
1102
1103 return arm_fill_gateway_parameters(arm, msg, len);
1104}
1105
1106static BOOL arm_handle_bad_request(rdpArm* arm, const HttpResponse* response, BOOL* retry)
1107{
1108 WINPR_ASSERT(response);
1109 WINPR_ASSERT(retry);
1110
1111 *retry = FALSE;
1112
1113 BOOL rc = FALSE;
1114
1115 http_response_log_error_status(WLog_Get(TAG), WLOG_ERROR, response);
1116
1117 const size_t len = http_response_get_body_length(response);
1118 const char* msg = http_response_get_body(response);
1119 if (msg && (strnlen(msg, len + 1) > len))
1120 {
1121 WLog_Print(arm->log, WLOG_ERROR, "Got HTTP Response data, but length is invalid");
1122
1123 return FALSE;
1124 }
1125
1126 WLog_Print(arm->log, WLOG_DEBUG, "Got HTTP Response data: %s", msg);
1127
1128 WINPR_JSON* json = WINPR_JSON_ParseWithLength(msg, len);
1129 if (json == nullptr)
1130 {
1131 const char* error_ptr = WINPR_JSON_GetErrorPtr();
1132 WLog_Print(arm->log, WLOG_ERROR, "WINPR_JSON_ParseWithLength: %s", error_ptr);
1133
1134 return FALSE;
1135 }
1136 else
1137 {
1138 WINPR_JSON* gateway_code_obj = WINPR_JSON_GetObjectItemCaseSensitive(json, "Code");
1139 const char* gw_code_str = WINPR_JSON_GetStringValue(gateway_code_obj);
1140 if (gw_code_str == nullptr)
1141 {
1142 WLog_Print(arm->log, WLOG_ERROR, "Response has no \"Code\" property");
1143 goto fail;
1144 }
1145
1146 if (strcmp(gw_code_str, "E_PROXY_ORCHESTRATION_LB_SESSIONHOST_DEALLOCATED") == 0)
1147 {
1148 *retry = TRUE;
1149 WINPR_JSON* message = WINPR_JSON_GetObjectItemCaseSensitive(json, "Message");
1150 const char* msgstr = WINPR_JSON_GetStringValue(message);
1151 if (!msgstr)
1152 WLog_WARN(TAG, "Starting your VM. It may take up to 5 minutes");
1153 else
1154 WLog_WARN(TAG, "%s", msgstr);
1155 freerdp_set_last_error_if_not(arm->context, FREERDP_ERROR_CONNECT_TARGET_BOOTING);
1156 }
1157 else
1158 {
1159 goto fail;
1160 }
1161 }
1162
1163 rc = TRUE;
1164fail:
1165 WINPR_JSON_Delete(json);
1166 return rc;
1167}
1168
1169static BOOL arm_handle_request(rdpArm* arm, BOOL* retry, DWORD timeout)
1170{
1171 WINPR_ASSERT(retry);
1172
1173 if (!arm_fetch_wellknown(arm))
1174 {
1175 *retry = TRUE;
1176 return FALSE;
1177 }
1178
1179 *retry = FALSE;
1180
1181 char* message = nullptr;
1182 BOOL rc = FALSE;
1183
1184 HttpResponse* response = nullptr;
1185 long StatusCode = 0;
1186
1187 const char* useragent =
1188 freerdp_settings_get_string(arm->context->settings, FreeRDP_GatewayHttpUserAgent);
1189 const char* msuseragent =
1190 freerdp_settings_get_string(arm->context->settings, FreeRDP_GatewayHttpMsUserAgent);
1191 if (!http_context_set_uri(arm->http, "/api/arm/v2/connections") ||
1192 !http_context_set_accept(arm->http, "*/*") ||
1193 !http_context_set_cache_control(arm->http, "no-cache") ||
1194 !http_context_set_pragma(arm->http, "no-cache") ||
1195 !http_context_set_connection(arm->http, "Keep-Alive") ||
1196 !http_context_set_user_agent(arm->http, useragent) ||
1197 !http_context_set_x_ms_user_agent(arm->http, msuseragent) ||
1198 !http_context_set_host(arm->http, freerdp_settings_get_string(arm->context->settings,
1199 FreeRDP_GatewayHostname)))
1200 goto arm_error;
1201
1202 if (!arm_tls_connect(arm, arm->tls, timeout))
1203 goto arm_error;
1204
1205 message = arm_create_request_json(arm);
1206 if (!message)
1207 goto arm_error;
1208
1209 if (!arm_send_http_request(arm, arm->tls, "POST", "application/json", message, strlen(message)))
1210 goto arm_error;
1211
1212 response = http_response_recv(arm->tls, TRUE);
1213 if (!response)
1214 goto arm_error;
1215
1216 StatusCode = http_response_get_status_code(response);
1217 if (StatusCode == HTTP_STATUS_OK)
1218 {
1219 if (!arm_handle_request_ok(arm, response))
1220 goto arm_error;
1221 }
1222 else if (StatusCode == HTTP_STATUS_BAD_REQUEST)
1223 {
1224 if (!arm_handle_bad_request(arm, response, retry))
1225 goto arm_error;
1226 }
1227 else
1228 {
1229 http_response_log_error_status(WLog_Get(TAG), WLOG_ERROR, response);
1230 goto arm_error;
1231 }
1232
1233 rc = TRUE;
1234arm_error:
1235 http_response_free(response);
1236 free(message);
1237 return rc;
1238}
1239
1240#endif
1241
1242BOOL arm_resolve_endpoint(wLog* log, rdpContext* context, DWORD timeout)
1243{
1244#ifndef WITH_AAD
1245 WLog_Print(log, WLOG_ERROR, "arm gateway support not compiled in");
1246 return FALSE;
1247#else
1248
1249 if (!context)
1250 return FALSE;
1251
1252 if (!context->settings)
1253 return FALSE;
1254
1255 if ((freerdp_settings_get_uint32(context->settings, FreeRDP_LoadBalanceInfoLength) == 0) ||
1256 (freerdp_settings_get_string(context->settings, FreeRDP_RemoteApplicationProgram) ==
1257 nullptr))
1258 {
1259 WLog_Print(log, WLOG_ERROR, "loadBalanceInfo and RemoteApplicationProgram needed");
1260 return FALSE;
1261 }
1262
1263 rdpArm* arm = arm_new(context);
1264 if (!arm)
1265 return FALSE;
1266
1267 BOOL retry = FALSE;
1268 BOOL rc = FALSE;
1269 do
1270 {
1271 if (retry && rc)
1272 {
1273 freerdp* instance = context->instance;
1274 WINPR_ASSERT(instance);
1275 SSIZE_T delay = IFCALLRESULT(-1, instance->RetryDialog, instance, "arm-transport",
1276 arm->gateway_retry, arm);
1277 arm->gateway_retry++;
1278 if (delay <= 0)
1279 break; /* error or no retry desired, abort loop */
1280 else
1281 {
1282 WLog_Print(arm->log, WLOG_DEBUG, "Delay for %" PRIdz "ms before next attempt",
1283 delay);
1284 while (delay > 0)
1285 {
1286 DWORD slp = (UINT32)delay;
1287 if (delay > UINT32_MAX)
1288 slp = UINT32_MAX;
1289 Sleep(slp);
1290 delay -= slp;
1291 }
1292 }
1293 }
1294 rc = arm_handle_request(arm, &retry, timeout);
1295
1296 } while (retry && rc);
1297 arm_free(arm);
1298 return rc;
1299#endif
1300}
WINPR_ATTR_NODISCARD WINPR_API char * WINPR_JSON_PrintUnformatted(WINPR_JSON *item)
Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_...
Definition c-json.c:301
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition c-json.c:93
WINPR_API BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition c-json.c:132
WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
WINPR_API WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition c-json.c:232
WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition c-json.c:108
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition c-json.c:127
WINPR_API WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition c-json.c:269
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition c-json.c:98
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142
WINPR_API WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition c-json.c:237
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition c-json.c:114
WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:187
WINPR_API const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition c-json.c:137
WINPR_ATTR_NODISCARD FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
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.
FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API WCHAR * freerdp_settings_get_string_as_utf16(const rdpSettings *settings, FreeRDP_Settings_Keys_String id, size_t *pCharLen)
Return an allocated UTF16 string.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.