22#include <winpr/assert.h>
23#include <winpr/wtypes.h>
26#include <winpr/wlog.h>
27#include <winpr/print.h>
28#include <winpr/asn1.h>
29#include <winpr/sspi.h>
30#include <winpr/collections.h>
33#include <rdpear-common/rdpear_common.h>
34#include <rdpear-common/rdpear_asn1.h>
36#include <freerdp/config.h>
37#include <freerdp/freerdp.h>
38#include <freerdp/addin.h>
39#include <freerdp/client/channels.h>
40#include <freerdp/channels/log.h>
41#include <freerdp/channels/rdpear.h>
43#define TAG CHANNELS_TAG("rdpear.client")
45#ifndef MAX_KEYTAB_NAME_LEN
46#define MAX_KEYTAB_NAME_LEN 1100
50krb5_error_code encode_krb5_authenticator(
const krb5_authenticator* rep, krb5_data** code_out);
51krb5_error_code encode_krb5_ap_rep(
const krb5_ap_rep* rep, krb5_data** code_out);
57 krb5_context krbContext;
60static const BYTE payloadHeader[16] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
63static krb5_error_code RPC_ENCRYPTION_KEY_to_keyblock(krb5_context ctx,
65 krb5_keyblock** pkeyblock)
69 WINPR_ASSERT(pkeyblock);
71 if (!key->reserved3.length)
72 return KRB5KDC_ERR_NULL_KEY;
75 krb5_init_keyblock(ctx, (krb5_enctype)key->reserved2, key->reserved3.length, pkeyblock);
79 krb5_keyblock* keyblock = *pkeyblock;
80 memcpy(keyblock->contents, key->reserved3.value, key->reserved3.length);
85static BOOL rdpear_set_asn1_buffer(
KERB_ASN1_DATA* asn1,
const void* data,
size_t len)
98 memcpy(buffer, data, len);
99 asn1->Asn1Buffer = buffer;
100 asn1->Asn1BufferHints = hints;
105 krb5_keyusage kusage, krb5_cksumtype cksumtype,
113 krb5_keyblock* keyblock =
nullptr;
114 krb5_data data = WINPR_C_ARRAY_INIT;
116 krb5_error_code rv = RPC_ENCRYPTION_KEY_to_keyblock(ctx, key, &keyblock);
120 data.data = (
char*)plain->Asn1Buffer;
121 data.length = plain->Asn1BufferHints.
count;
123 rv = krb5_c_make_checksum(ctx, cksumtype, keyblock, kusage, &data, out);
125 krb5_free_keyblock(ctx, keyblock);
141 krb5_keyblock* keyblock =
nullptr;
142 krb5_data data = WINPR_C_ARRAY_INIT;
143 krb5_enc_data enc = WINPR_C_ARRAY_INIT;
146 krb5_error_code rv = RPC_ENCRYPTION_KEY_to_keyblock(ctx, key, &keyblock);
150 data.data = (
char*)plain->Asn1Buffer;
151 data.length = plain->Asn1BufferHints.
count;
153 rv = krb5_c_encrypt_length(ctx, keyblock->enctype, data.length, &elen);
156 if (!elen || (elen > UINT32_MAX))
158 rv = KRB5_PARSE_MALFORMED;
161 enc.ciphertext.length = (
unsigned int)elen;
162 enc.ciphertext.data = malloc(elen);
163 if (!enc.ciphertext.data)
169 rv = krb5_c_encrypt(ctx, keyblock, kusage,
nullptr, &data, &enc);
172 free(enc.ciphertext.data);
176 out->data = enc.ciphertext.data;
177 out->length = enc.ciphertext.length;
179 krb5_free_keyblock(ctx, keyblock);
184 krb5_keyusage kusage,
const krb5_data* cipher,
189 WINPR_ASSERT(cipher);
190 WINPR_ASSERT(cipher->length);
193 plain->Asn1Buffer =
nullptr;
194 plain->Asn1BufferHints.
count = 0;
196 krb5_keyblock* keyblock =
nullptr;
197 krb5_data data = WINPR_C_ARRAY_INIT;
198 krb5_enc_data enc = WINPR_C_ARRAY_INIT;
200 krb5_error_code rv = RPC_ENCRYPTION_KEY_to_keyblock(ctx, key, &keyblock);
204 enc.kvno = KRB5_PVNO;
205 enc.enctype = (krb5_enctype)key->reserved2;
206 enc.ciphertext.length = cipher->length;
207 enc.ciphertext.data = cipher->data;
209 data.length = cipher->length;
210 data.data = (
char*)malloc(cipher->length);
217 rv = krb5_c_decrypt(ctx, keyblock, kusage,
nullptr, &enc, &data);
218 if (!rv && !rdpear_set_asn1_buffer(plain, data.data, data.length))
222 krb5_free_keyblock(ctx, keyblock);
226static BOOL rdpear_send_payload(RDPEAR_PLUGIN* rdpear, IWTSVirtualChannelCallback* pChannelCallback,
227 RdpEarPackageType packageType,
wStream* payload)
231 wStream* finalStream =
nullptr;
232 SecBuffer cryptedBuffer = WINPR_C_ARRAY_INIT;
233 wStream* unencodedContent = rdpear_encodePayload(packageType, payload);
234 if (!unencodedContent)
237 const size_t unencodedLen = Stream_GetPosition(unencodedContent);
239#if UINT32_MAX < SIZE_MAX
240 if (unencodedLen > UINT32_MAX)
244 SecBuffer inBuffer = { (ULONG)unencodedLen, SECBUFFER_DATA, Stream_Buffer(unencodedContent) };
246 if (!freerdp_nla_encrypt(rdpear->rdp_context, &inBuffer, &cryptedBuffer))
249 finalStream = Stream_New(
nullptr, 200);
252 Stream_Write_UINT32(finalStream, 0x4EACC3C8);
253 Stream_Write_UINT32(finalStream, cryptedBuffer.cbBuffer);
254 Stream_Write_UINT32(finalStream, 0x00000000);
255 Stream_Write_UINT32(finalStream, 0x00000000);
256 Stream_Write_UINT64(finalStream, 0);
259 if (!Stream_EnsureRemainingCapacity(finalStream, cryptedBuffer.cbBuffer))
262 Stream_Write(finalStream, cryptedBuffer.pvBuffer, cryptedBuffer.cbBuffer);
264 const size_t pos = Stream_GetPosition(finalStream);
265#if UINT32_MAX < SIZE_MAX
266 if (pos > UINT32_MAX)
270 UINT status = callback->channel->Write(callback->channel, (ULONG)pos,
271 Stream_Buffer(finalStream),
nullptr);
272 ret = (status == CHANNEL_RC_OK);
274 WLog_DBG(TAG,
"rdpear_send_payload=0x%x", status);
276 sspi_SecBufferFree(&cryptedBuffer);
277 Stream_Free(unencodedContent, TRUE);
278 Stream_Free(finalStream, TRUE);
282static UINT32 rdpear_kerb_version(UINT32 maxVersion, UINT32* pversion)
284 WLog_DBG(TAG,
"-> KerbNegotiateVersion(v=0x%x)", maxVersion);
289static UINT32 rdpear_kerb_ComputeTgsChecksum(RDPEAR_PLUGIN* rdpear,
292 krb5_checksum checksum = WINPR_C_ARRAY_INIT;
293 wStream* asn1Payload =
nullptr;
295 UINT32 status = ERROR_INVALID_DATA;
296 WLog_DBG(TAG,
"-> ComputeTgsChecksum");
299 kerb_do_checksum(rdpear->krbContext, req->Key, KRB5_KEYUSAGE_TGS_REQ_AUTH_CKSUM,
300 (krb5_cksumtype)req->ChecksumType, req->requestBody, &checksum);
304 asn1Payload = rdpear_enc_Checksum(req->ChecksumType, &checksum);
309 if (!rdpear_set_asn1_buffer(resp, Stream_Buffer(asn1Payload), Stream_GetPosition(asn1Payload)))
314 krb5_free_checksum_contents(rdpear->krbContext, &checksum);
315 Stream_Free(asn1Payload, TRUE);
319static UINT32 rdpear_kerb_BuildEncryptedAuthData(RDPEAR_PLUGIN* rdpear,
323 krb5_data encrypted = WINPR_C_ARRAY_INIT;
324 wStream* asn1Payload =
nullptr;
325 krb5_error_code rv = 0;
327 UINT32 status = ERROR_INVALID_DATA;
328 WLog_DBG(TAG,
"-> BuildEncryptedAuthData");
330 rv = kerb_do_encrypt(rdpear->krbContext, req->Key, (krb5_keyusage)req->KeyUsage,
331 req->PlainAuthData, &encrypted);
336 asn1Payload = rdpear_enc_EncryptedData(req->Key->reserved2, &encrypted);
343 if (!rdpear_set_asn1_buffer(asn1, Stream_Buffer(asn1Payload), Stream_GetPosition(asn1Payload)))
348 krb5_free_data_contents(rdpear->krbContext, &encrypted);
349 Stream_Free(asn1Payload, TRUE);
356 return ConvertWCharNToUtf8Alloc(src->Buffer, src->strLength,
nullptr);
359static BOOL extractAuthData(
const KERB_ASN1_DATA* src, krb5_authdata* authData, BOOL* haveData)
364 WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, src->Asn1Buffer, src->Asn1BufferHints.count);
366 WinPrAsn1_INTEGER adType = 0;
367 WinPrAsn1_OctetString os = WINPR_C_ARRAY_INIT;
370 if (!WinPrAsn1DecReadSequence(&dec, &dec2))
373 wStream subStream = WinPrAsn1DecGetStream(&dec2);
374 if (!Stream_GetRemainingLength(&subStream))
377 if (!WinPrAsn1DecReadSequence(&dec2, &dec3))
380 if (!WinPrAsn1DecReadContextualInteger(&dec3, 0, &error, &adType) ||
381 !WinPrAsn1DecReadContextualOctetString(&dec3, 1, &error, &os, FALSE))
384 if (os.len > UINT32_MAX)
387 authData->ad_type = adType;
388 authData->length = (
unsigned int)os.len;
389 authData->contents = os.data;
394static BOOL extractChecksum(
const KERB_ASN1_DATA* src, krb5_checksum* dst)
398 WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, src->Asn1Buffer, src->Asn1BufferHints.count);
400 WinPrAsn1_OctetString os;
402 if (!WinPrAsn1DecReadSequence(&dec, &dec2))
405 WinPrAsn1_INTEGER cksumtype = 0;
406 if (!WinPrAsn1DecReadContextualInteger(&dec2, 0, &error, &cksumtype) ||
407 !WinPrAsn1DecReadContextualOctetString(&dec2, 1, &error, &os, FALSE))
410 if (os.len > UINT32_MAX)
412 dst->checksum_type = cksumtype;
413 dst->length = (
unsigned int)os.len;
414 dst->contents = os.data;
418#define FILETIME_TO_UNIX_OFFSET_S 11644473600LL
420static LONGLONG krb5_time_to_FILETIME(
const krb5_timestamp* ts, krb5_int32 usec)
423 return (((*ts + FILETIME_TO_UNIX_OFFSET_S) * (1000LL * 1000LL) + usec) * 10LL);
426static void krb5_free_principal_contents(krb5_context ctx, krb5_principal principal)
428 WINPR_ASSERT(principal);
429 krb5_free_data_contents(ctx, &principal->realm);
430 krb5_free_data(ctx, principal->data);
433static UINT32 rdpear_kerb_CreateApReqAuthenticator(RDPEAR_PLUGIN* rdpear,
437 krb5_error_code rv = 0;
438 wStream* asn1EncodedAuth =
nullptr;
439 krb5_data authenticator = WINPR_C_ARRAY_INIT;
440 krb5_data* der =
nullptr;
441 krb5_keyblock* subkey =
nullptr;
442 krb5_principal_data client = WINPR_C_ARRAY_INIT;
444 UINT32 status = ERROR_INVALID_DATA;
445 WLog_DBG(TAG,
"-> CreateApReqAuthenticator");
447 krb5_authdata authdata = WINPR_C_ARRAY_INIT;
448 krb5_authdata* authDataPtr[2] = { &authdata,
nullptr };
451 if (!extractAuthData(req->AuthData, &authdata, &haveData))
453 WLog_ERR(TAG,
"error retrieving auth data");
454 winpr_HexDump(TAG, WLOG_DEBUG, req->AuthData->Asn1Buffer,
455 req->AuthData->Asn1BufferHints.
count);
459 if (req->SkewTime->QuadPart)
461 WLog_ERR(TAG,
"!!!!! should handle SkewTime !!!!!");
466 rv = RPC_ENCRYPTION_KEY_to_keyblock(rdpear->krbContext, req->SubKey, &subkey);
469 WLog_ERR(TAG,
"error importing subkey");
474 krb5_authenticator authent = { .checksum =
nullptr,
476 .seq_number = req->SequenceNumber,
477 .authorization_data = haveData ? authDataPtr :
nullptr };
479 client.type = req->ClientName->NameType;
480 if (req->ClientName->nameHints.
count > INT32_MAX)
483 client.length = (krb5_int32)req->ClientName->nameHints.
count;
484 client.data = calloc(req->ClientName->nameHints.
count,
sizeof(krb5_data));
488 for (
int i = 0; i < client.length; i++)
490 krb5_data* cur = &client.data[i];
491 cur->data = KERB_RPC_UNICODESTR_to_charptr(&req->ClientName->Names[i]);
494 const size_t len = strnlen(cur->data, MAX_KEYTAB_NAME_LEN + 1);
495 if (len > MAX_KEYTAB_NAME_LEN)
498 "Invalid ClientName length %d, limited to %" PRIuz
499 " characters. ClientName: (%s)",
500 MAX_KEYTAB_NAME_LEN, len, cur->data);
503 cur->length = (
unsigned int)len;
505 client.realm.data = KERB_RPC_UNICODESTR_to_charptr(req->ClientRealm);
506 if (!client.realm.data)
509 const size_t len = strnlen(client.realm.data, MAX_KEYTAB_NAME_LEN + 1);
510 if (len > MAX_KEYTAB_NAME_LEN)
512 WLog_ERR(TAG,
"Invalid realm length %d, limited to %" PRIuz
"characters. Realm: (%s)",
513 MAX_KEYTAB_NAME_LEN, len, client.realm.data);
516 client.realm.length = (
unsigned int)len;
517 authent.client = &client;
519 krb5_checksum checksum;
520 krb5_checksum* pchecksum =
nullptr;
521 if (req->GssChecksum)
523 if (!extractChecksum(req->GssChecksum, &checksum))
525 WLog_ERR(TAG,
"Error getting the checksum");
528 pchecksum = &checksum;
530 authent.checksum = pchecksum;
532 krb5_us_timeofday(rdpear->krbContext, &authent.ctime, &authent.cusec);
534 rv = encode_krb5_authenticator(&authent, &der);
537 WLog_ERR(TAG,
"error encoding authenticator");
542 .Asn1Buffer = (BYTE*)der->data,
543 .Asn1BufferHints = { .count = der->length } };
545 rv = kerb_do_encrypt(rdpear->krbContext, req->EncryptionKey, (krb5_keyusage)req->KeyUsage,
546 &plain_authent, &authenticator);
549 WLog_ERR(TAG,
"error encrypting authenticator");
553 asn1EncodedAuth = rdpear_enc_EncryptedData(req->EncryptionKey->reserved2, &authenticator);
554 if (!asn1EncodedAuth)
556 WLog_ERR(TAG,
"error encoding to ASN1");
565 if (!rdpear_set_asn1_buffer(&resp->Authenticator, Stream_Buffer(asn1EncodedAuth),
566 Stream_GetPosition(asn1EncodedAuth)))
568 resp->AuthenticatorTime.QuadPart = krb5_time_to_FILETIME(&authent.ctime, authent.cusec);
576 resp->Authenticator.Pdu = (req->GssChecksum && (req->GssChecksum->Pdu == 8)) ? 6 : 7;
577 WLog_VRB(TAG,
"host checksum PDU %" PRIu32
" -> authenticator PDU %" PRIu32,
578 req->GssChecksum ? req->GssChecksum->Pdu : 0, resp->Authenticator.Pdu);
579 resp->KerbProtocolError = rv;
580 krb5_free_principal_contents(rdpear->krbContext, &client);
581 krb5_free_data(rdpear->krbContext, der);
582 krb5_free_data_contents(rdpear->krbContext, &authenticator);
584 krb5_free_keyblock(rdpear->krbContext, subkey);
585 Stream_Free(asn1EncodedAuth, TRUE);
589static BOOL rdpear_findEncryptedData(
const KERB_ASN1_DATA* src,
int* penctype, krb5_data* data)
593 WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, src->Asn1Buffer, src->Asn1BufferHints.count);
595 WinPrAsn1_INTEGER encType = 0;
596 WinPrAsn1_OctetString os = WINPR_C_ARRAY_INIT;
598 if (!WinPrAsn1DecReadSequence(&dec, &dec2) ||
599 !WinPrAsn1DecReadContextualInteger(&dec2, 0, &error, &encType) ||
600 !WinPrAsn1DecReadContextualOctetString(&dec2, 2, &error, &os, FALSE))
603 if (os.len > UINT32_MAX)
605 data->data = (
char*)os.data;
606 data->length = (
unsigned int)os.len;
611static UINT32 rdpear_kerb_UnpackKdcReplyBody(RDPEAR_PLUGIN* rdpear,
615 if (req->StrengthenKey)
617 WLog_ERR(TAG,
"StrengthenKey not supported yet");
618 return ERROR_INVALID_DATA;
621 WLog_DBG(TAG,
"-> UnpackKdcReplyBody: KeyUsage=0x%x PDU=0x%x", req->KeyUsage, req->Pdu);
626 krb5_data asn1Data = WINPR_C_ARRAY_INIT;
628 if (!rdpear_findEncryptedData(req->EncryptedData, &encType, &asn1Data) || !asn1Data.length)
629 return ERROR_INVALID_DATA;
631 resp->KerbProtocolError = kerb_do_decrypt(
632 rdpear->krbContext, req->Key, (krb5_keyusage)req->KeyUsage, &asn1Data, &resp->ReplyBody);
633 resp->ReplyBody.Pdu = req->Pdu;
638static UINT32 rdpear_kerb_DecryptApReply(RDPEAR_PLUGIN* rdpear,
const DecryptApReplyReq* req,
641 WLog_DBG(TAG,
"-> DecryptApReply");
645 krb5_data asn1Data = WINPR_C_ARRAY_INIT;
647 if (!rdpear_findEncryptedData(req->EncryptedReply, &encType, &asn1Data) || !asn1Data.length)
648 return ERROR_INVALID_DATA;
651 krb5_error_code rv = kerb_do_decrypt(rdpear->krbContext, req->Key, KRB5_KEYUSAGE_AP_REP_ENCPART,
655 WLog_ERR(TAG,
"error decrypting");
656 return ERROR_INVALID_DATA;
664static UINT32 rdpear_kerb_PackApReply(RDPEAR_PLUGIN* rdpear,
const PackApReplyReq* req,
667 UINT32 status = ERROR_INVALID_DATA;
668 krb5_data asn1Data = WINPR_C_ARRAY_INIT;
669 krb5_data* out =
nullptr;
671 WLog_DBG(TAG,
"-> PackApReply");
673 krb5_error_code rv = kerb_do_encrypt(rdpear->krbContext, req->SessionKey,
674 KRB5_KEYUSAGE_AP_REP_ENCPART, req->ReplyBody, &asn1Data);
679 reply.enc_part.kvno = KRB5_PVNO;
680 reply.enc_part.enctype = (krb5_enctype)req->SessionKey->reserved2;
681 reply.enc_part.ciphertext.length = asn1Data.length;
682 reply.enc_part.ciphertext.data = asn1Data.data;
684 rv = encode_krb5_ap_rep(&reply, &out);
688 resp->PackedReplyHints.
count = out->length;
691 if (!resp->PackedReply)
693 memcpy(resp->PackedReply, out->data, out->length);
696 krb5_free_data(rdpear->krbContext, out);
697 krb5_free_data_contents(rdpear->krbContext, &asn1Data);
711 output->CallId = input->CallId;
712 switch (input->CallId)
714 case RemoteCallKerbNegotiateVersion:
715 output->
Status = rdpear_kerb_version(in->NegotiateVersion, &out->NegotiateVersion);
717 case RemoteCallKerbCreateApReqAuthenticator:
718 output->
Status = rdpear_kerb_CreateApReqAuthenticator(
721 case RemoteCallKerbDecryptApReply:
723 rdpear_kerb_DecryptApReply(rdpear, &in->DecryptApReply, &out->DecryptApReply);
725 case RemoteCallKerbComputeTgsChecksum:
726 output->
Status = rdpear_kerb_ComputeTgsChecksum(rdpear, &in->ComputeTgsChecksum,
727 &out->ComputeTgsChecksum);
729 case RemoteCallKerbBuildEncryptedAuthData:
730 output->
Status = rdpear_kerb_BuildEncryptedAuthData(rdpear, &in->BuildEncryptedAuthData,
733 case RemoteCallKerbUnpackKdcReplyBody:
734 output->
Status = rdpear_kerb_UnpackKdcReplyBody(rdpear, &in->UnpackKdcReplyBody,
737 case RemoteCallKerbPackApReply:
738 output->
Status = rdpear_kerb_PackApReply(rdpear, &in->PackApReply, &out->
PackApReply);
741 WLog_DBG(TAG,
"Unhandled Kerberos callId=0x%x", input->CallId);
755 output->CallId = input->CallId;
756 switch (input->CallId)
758 case RemoteCallNtlmNegotiateVersion:
759 WLog_DBG(TAG,
"-> NtlmNegotiateVersion(v=0x%x)", input->
Input.NegotiateVersion);
760 output->
Output.NegotiateVersion = 0;
761 output->
Status = STATUS_SUCCESS;
763 case RemoteCallNtlmLm20GetNtlm3ChallengeResponse:
764 case RemoteCallNtlmCalculateNtResponse:
765 case RemoteCallNtlmCalculateUserSessionKeyNt:
766 case RemoteCallNtlmCompareCredentials:
767 WLog_DBG(TAG,
"-> NTLM callId=0x%x: no NTLM credentials", input->CallId);
768 output->
Status = (UINT32)STATUS_NOT_SUPPORTED;
771 WLog_DBG(TAG,
"Unhandled NTLM callId=0x%x", input->CallId);
776static UINT rdpear_decode_payload(RDPEAR_PLUGIN* rdpear,
const WinPrAsn1_OctetString* packageName,
777 IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
779 UINT ret = ERROR_INVALID_DATA;
780 WinPrNdrDecoder* decoder =
nullptr;
781 WinPrNdrEncoder* encoder =
nullptr;
786 BOOL handled = FALSE;
787 BOOL encoded = FALSE;
789 const RdpEarPackageType packageType = rdpear_packageType_from_name(packageName);
790 if (packageType == RDPEAR_PACKAGE_UNKNOWN)
792 WLog_ERR(TAG,
"unknown package type");
793 return ERROR_INVALID_DATA;
796 wStream* respStream = Stream_New(
nullptr, 500);
798 return ERROR_INVALID_DATA;
800 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
812 Stream_Write(respStream, payloadHeader,
sizeof(payloadHeader));
814 if (packageType == RDPEAR_PACKAGE_KERBEROS)
816 if (!rdpear_kerb_input_read(decoder, s, &kerbInput))
819 handled = rdpear_kerb_call(rdpear, &kerbInput, &kerbOutput);
820 encoded = rdpear_kerb_output_write(encoder, respStream, &kerbOutput);
824 if (!rdpear_ntlm_input_read(decoder, s, &ntlmInput))
827 handled = rdpear_ntlm_call(&ntlmInput, &ntlmOutput);
828 encoded = rdpear_ntlm_output_write(encoder, respStream, &ntlmOutput);
833 WLog_DBG(TAG,
"error encoding the reply");
837 if (!rdpear_send_payload(rdpear, pChannelCallback, packageType, respStream))
839 WLog_DBG(TAG,
"rdpear_send_payload !!!!!!!!");
843 ret = handled ? CHANNEL_RC_OK : CHANNEL_RC_NOT_OPEN;
845 rdpear_kerb_input_free(&kerbInput);
846 rdpear_kerb_output_free(&kerbOutput);
847 rdpear_ntlm_input_free(&ntlmInput);
848 rdpear_ntlm_output_free(&ntlmOutput);
851 Stream_Free(respStream, TRUE);
855static UINT rdpear_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
858 WINPR_ASSERT(callback);
859 UINT ret = ERROR_INVALID_DATA;
863 if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
864 return ERROR_INVALID_DATA;
866 UINT32 protocolMagic = Stream_Get_UINT32(s);
867 if (protocolMagic != 0x4EACC3C8)
868 return ERROR_INVALID_DATA;
870 UINT32 Length = Stream_Get_UINT32(s);
871 UINT32 Version = Stream_Get_UINT32(s);
872 if (Version != 0x00000000)
873 return ERROR_INVALID_DATA;
878 if (!Stream_CheckAndLogRequiredLength(TAG, s, Length))
879 return ERROR_INVALID_DATA;
881 SecBuffer inBuffer = { Length, SECBUFFER_TOKEN, Stream_PointerAs(s,
void) };
882 SecBuffer decrypted = WINPR_C_ARRAY_INIT;
884 RDPEAR_PLUGIN* rdpear = (RDPEAR_PLUGIN*)callback->plugin;
885 WINPR_ASSERT(rdpear);
886 if (!freerdp_nla_decrypt(rdpear->rdp_context, &inBuffer, &decrypted))
891 wStream decodedStream = WINPR_C_ARRAY_INIT;
892 Stream_StaticInit(&decodedStream, decrypted.pvBuffer, decrypted.cbBuffer);
893 WinPrAsn1Decoder_Init(&dec, WINPR_ASN1_DER, &decodedStream);
895 if (!WinPrAsn1DecReadSequence(&dec, &dec2))
898 WinPrAsn1_OctetString packageName = WINPR_C_ARRAY_INIT;
899 WinPrAsn1_OctetString payload = WINPR_C_ARRAY_INIT;
901 if (!WinPrAsn1DecReadContextualOctetString(&dec2, 1, &error, &packageName, FALSE))
904 if (!WinPrAsn1DecReadContextualOctetString(&dec2, 2, &error, &payload, FALSE))
907 wStream payloadStream = WINPR_C_ARRAY_INIT;
908 Stream_StaticInit(&payloadStream, payload.data, payload.len);
910 ret = rdpear_decode_payload(rdpear, &packageName, pChannelCallback, &payloadStream);
912 sspi_SecBufferFree(&decrypted);
921static UINT rdpear_on_open(IWTSVirtualChannelCallback* pChannelCallback)
923 WINPR_UNUSED(pChannelCallback);
924 return CHANNEL_RC_OK;
932static UINT rdpear_on_close(IWTSVirtualChannelCallback* pChannelCallback)
934 WINPR_UNUSED(pChannelCallback);
935 return CHANNEL_RC_OK;
942 RDPEAR_PLUGIN* rdpear = (RDPEAR_PLUGIN*)base;
943 krb5_free_context(rdpear->krbContext);
946static UINT init_plugin_cb(
GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings)
949 WINPR_UNUSED(settings);
951 RDPEAR_PLUGIN* rdpear = (RDPEAR_PLUGIN*)base;
952 rdpear->rdp_context = rcontext;
953 if (krb5_init_context(&rdpear->krbContext))
954 return CHANNEL_RC_INITIALIZATION_ERROR;
955 return CHANNEL_RC_OK;
958static const IWTSVirtualChannelCallback rdpear_callbacks = { rdpear_on_data_received,
959 rdpear_on_open, rdpear_on_close,
967FREERDP_ENTRY_POINT(UINT rdpear_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
969 return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, RDPEAR_DVC_CHANNEL_NAME,
971 &rdpear_callbacks, init_plugin_cb, terminate_plugin_cb);
NDR encoder and decoder ([C706] chapter 14, [MS-RPCE] 2.2.5 and 2.2.6).
static INLINE void winpr_ndr_decoder_destroy(WinPrNdrDecoder **pcontext)
frees a decoder and resets the pointer
static INLINE void winpr_ndr_encoder_destroy(WinPrNdrEncoder **pcontext)
frees an encoder and resets the pointer
WINPR_API WinPrNdrEncoder * winpr_ndr_encoder_from_decoder(const WinPrNdrDecoder *src)
creates an encoder with the same endianness and version as a decoder, to answer the message it decode...
WINPR_API void * winpr_ndr_pointer_new(WinPrNdrMessageType descr, const void *hints, size_t size)
allocates a referent
WINPR_API WinPrNdrDecoder * winpr_ndr_decoder_new_fromStream(wStream *s)
reads the common type header ([MS-RPCE] 2.2.6.1) and creates a decoder for its version and endianness
hints for a conformant array
2.2.2.1.8 BuildEncryptedAuthData
2.2.2.1.7 ComputeTgsChecksum
2.2.2.1.4 CreateApReqAuthenticator
2.2.2.1.4 CreateApReqAuthenticator
2.2.1.2.8 KERB_RPC_ENCRYPTION_KEY
KerbCredIsoRemoteOutputUnion Output
NtlmCredIsoRemoteOutputUnion Output
2.3.10 RPC_UNICODE_STRING (MS-DTYP)
2.2.2.1.6 UnpackKdcReplyBody
2.2.2.1.6 UnpackKdcReplyBody
2.2.1.2.10 KerbCredIsoRemoteOutput
CreateApReqAuthenticatorResp CreateApReqAuthenticator
UnpackKdcReplyBodyResp UnpackKdcReplyBody
KERB_ASN1_DATA BuildEncryptedAuthData
PackApReplyResp PackApReply