/src/FreeRDP/channels/rdpear/client/rdpear_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Authentication redirection virtual channel |
4 | | * |
5 | | * Copyright 2023 David Fort <contact@hardening-consulting.com> |
6 | | * |
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 |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
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. |
18 | | */ |
19 | | #include <krb5.h> |
20 | | #include <errno.h> |
21 | | |
22 | | #include <winpr/assert.h> |
23 | | #include <winpr/wtypes.h> |
24 | | |
25 | | #include <winpr/crt.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> |
31 | | #include <winpr/ndr.h> |
32 | | |
33 | | #include <rdpear-common/rdpear_common.h> |
34 | | #include <rdpear-common/rdpear_asn1.h> |
35 | | |
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> |
42 | | |
43 | 0 | #define TAG CHANNELS_TAG("rdpear.client") |
44 | | |
45 | | #ifndef MAX_KEYTAB_NAME_LEN |
46 | | #define MAX_KEYTAB_NAME_LEN 1100 /* Defined in MIT krb5.h */ |
47 | | #endif |
48 | | |
49 | | /* defined in libkrb5 */ |
50 | | krb5_error_code encode_krb5_authenticator(const krb5_authenticator* rep, krb5_data** code_out); |
51 | | krb5_error_code encode_krb5_ap_rep(const krb5_ap_rep* rep, krb5_data** code_out); |
52 | | |
53 | | typedef struct |
54 | | { |
55 | | GENERIC_DYNVC_PLUGIN base; |
56 | | rdpContext* rdp_context; |
57 | | krb5_context krbContext; |
58 | | } RDPEAR_PLUGIN; |
59 | | |
60 | | static const BYTE payloadHeader[16] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
61 | | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
62 | | |
63 | | static krb5_error_code RPC_ENCRYPTION_KEY_to_keyblock(krb5_context ctx, |
64 | | const KERB_RPC_ENCRYPTION_KEY* key, |
65 | | krb5_keyblock** pkeyblock) |
66 | 0 | { |
67 | 0 | WINPR_ASSERT(ctx); |
68 | 0 | WINPR_ASSERT(key); |
69 | 0 | WINPR_ASSERT(pkeyblock); |
70 | |
|
71 | 0 | if (!key->reserved3.length) |
72 | 0 | return KRB5KDC_ERR_NULL_KEY; |
73 | | |
74 | 0 | krb5_error_code rv = |
75 | 0 | krb5_init_keyblock(ctx, (krb5_enctype)key->reserved2, key->reserved3.length, pkeyblock); |
76 | 0 | if (rv) |
77 | 0 | return rv; |
78 | | |
79 | 0 | krb5_keyblock* keyblock = *pkeyblock; |
80 | 0 | memcpy(keyblock->contents, key->reserved3.value, key->reserved3.length); |
81 | 0 | return rv; |
82 | 0 | } |
83 | | |
84 | | /** @brief sets the buffer of a KERB_ASN1_DATA to encode, the data is copied in a NDR referent */ |
85 | | static BOOL rdpear_set_asn1_buffer(KERB_ASN1_DATA* asn1, const void* data, size_t len) |
86 | 0 | { |
87 | 0 | WINPR_ASSERT(asn1); |
88 | |
|
89 | 0 | if (len > UINT32_MAX) |
90 | 0 | return FALSE; |
91 | | |
92 | 0 | const WinPrNdrArrayHints hints = { (UINT32)len }; |
93 | 0 | BYTE* buffer = winpr_ndr_pointer_new(winpr_ndr_uint8Array_descr(), &hints, len); |
94 | 0 | if (!buffer) |
95 | 0 | return FALSE; |
96 | | |
97 | 0 | if (len) |
98 | 0 | memcpy(buffer, data, len); |
99 | 0 | asn1->Asn1Buffer = buffer; |
100 | 0 | asn1->Asn1BufferHints = hints; |
101 | 0 | return TRUE; |
102 | 0 | } |
103 | | |
104 | | static krb5_error_code kerb_do_checksum(krb5_context ctx, const KERB_RPC_ENCRYPTION_KEY* key, |
105 | | krb5_keyusage kusage, krb5_cksumtype cksumtype, |
106 | | const KERB_ASN1_DATA* plain, krb5_checksum* out) |
107 | 0 | { |
108 | 0 | WINPR_ASSERT(ctx); |
109 | 0 | WINPR_ASSERT(key); |
110 | 0 | WINPR_ASSERT(plain); |
111 | 0 | WINPR_ASSERT(out); |
112 | |
|
113 | 0 | krb5_keyblock* keyblock = nullptr; |
114 | 0 | krb5_data data = WINPR_C_ARRAY_INIT; |
115 | |
|
116 | 0 | krb5_error_code rv = RPC_ENCRYPTION_KEY_to_keyblock(ctx, key, &keyblock); |
117 | 0 | if (rv) |
118 | 0 | return rv; |
119 | | |
120 | 0 | data.data = (char*)plain->Asn1Buffer; |
121 | 0 | data.length = plain->Asn1BufferHints.count; |
122 | |
|
123 | 0 | rv = krb5_c_make_checksum(ctx, cksumtype, keyblock, kusage, &data, out); |
124 | |
|
125 | 0 | krb5_free_keyblock(ctx, keyblock); |
126 | 0 | return rv; |
127 | 0 | } |
128 | | |
129 | | static krb5_error_code kerb_do_encrypt(krb5_context ctx, const KERB_RPC_ENCRYPTION_KEY* key, |
130 | | krb5_keyusage kusage, const KERB_ASN1_DATA* plain, |
131 | | krb5_data* out) |
132 | 0 | { |
133 | 0 | WINPR_ASSERT(ctx); |
134 | 0 | WINPR_ASSERT(key); |
135 | 0 | WINPR_ASSERT(plain); |
136 | 0 | WINPR_ASSERT(out); |
137 | |
|
138 | 0 | out->data = nullptr; |
139 | 0 | out->length = 0; |
140 | |
|
141 | 0 | krb5_keyblock* keyblock = nullptr; |
142 | 0 | krb5_data data = WINPR_C_ARRAY_INIT; |
143 | 0 | krb5_enc_data enc = WINPR_C_ARRAY_INIT; |
144 | 0 | size_t elen = 0; |
145 | |
|
146 | 0 | krb5_error_code rv = RPC_ENCRYPTION_KEY_to_keyblock(ctx, key, &keyblock); |
147 | 0 | if (rv) |
148 | 0 | return rv; |
149 | | |
150 | 0 | data.data = (char*)plain->Asn1Buffer; |
151 | 0 | data.length = plain->Asn1BufferHints.count; |
152 | |
|
153 | 0 | rv = krb5_c_encrypt_length(ctx, keyblock->enctype, data.length, &elen); |
154 | 0 | if (rv) |
155 | 0 | goto out; |
156 | 0 | if (!elen || (elen > UINT32_MAX)) |
157 | 0 | { |
158 | 0 | rv = KRB5_PARSE_MALFORMED; |
159 | 0 | goto out; |
160 | 0 | } |
161 | 0 | enc.ciphertext.length = (unsigned int)elen; |
162 | 0 | enc.ciphertext.data = malloc(elen); |
163 | 0 | if (!enc.ciphertext.data) |
164 | 0 | { |
165 | 0 | rv = ENOMEM; |
166 | 0 | goto out; |
167 | 0 | } |
168 | | |
169 | 0 | rv = krb5_c_encrypt(ctx, keyblock, kusage, nullptr, &data, &enc); |
170 | 0 | if (rv) |
171 | 0 | { |
172 | 0 | free(enc.ciphertext.data); |
173 | 0 | goto out; |
174 | 0 | } |
175 | | |
176 | 0 | out->data = enc.ciphertext.data; |
177 | 0 | out->length = enc.ciphertext.length; |
178 | 0 | out: |
179 | 0 | krb5_free_keyblock(ctx, keyblock); |
180 | 0 | return rv; |
181 | 0 | } |
182 | | |
183 | | static krb5_error_code kerb_do_decrypt(krb5_context ctx, const KERB_RPC_ENCRYPTION_KEY* key, |
184 | | krb5_keyusage kusage, const krb5_data* cipher, |
185 | | KERB_ASN1_DATA* plain) |
186 | 0 | { |
187 | 0 | WINPR_ASSERT(ctx); |
188 | 0 | WINPR_ASSERT(key); |
189 | 0 | WINPR_ASSERT(cipher); |
190 | 0 | WINPR_ASSERT(cipher->length); |
191 | 0 | WINPR_ASSERT(plain); |
192 | |
|
193 | 0 | plain->Asn1Buffer = nullptr; |
194 | 0 | plain->Asn1BufferHints.count = 0; |
195 | |
|
196 | 0 | krb5_keyblock* keyblock = nullptr; |
197 | 0 | krb5_data data = WINPR_C_ARRAY_INIT; |
198 | 0 | krb5_enc_data enc = WINPR_C_ARRAY_INIT; |
199 | |
|
200 | 0 | krb5_error_code rv = RPC_ENCRYPTION_KEY_to_keyblock(ctx, key, &keyblock); |
201 | 0 | if (rv) |
202 | 0 | return rv; |
203 | | |
204 | 0 | enc.kvno = KRB5_PVNO; |
205 | 0 | enc.enctype = (krb5_enctype)key->reserved2; |
206 | 0 | enc.ciphertext.length = cipher->length; |
207 | 0 | enc.ciphertext.data = cipher->data; |
208 | |
|
209 | 0 | data.length = cipher->length; |
210 | 0 | data.data = (char*)malloc(cipher->length); |
211 | 0 | if (!data.data) |
212 | 0 | { |
213 | 0 | rv = ENOMEM; |
214 | 0 | goto out; |
215 | 0 | } |
216 | | |
217 | 0 | rv = krb5_c_decrypt(ctx, keyblock, kusage, nullptr, &enc, &data); |
218 | 0 | if (!rv && !rdpear_set_asn1_buffer(plain, data.data, data.length)) |
219 | 0 | rv = ENOMEM; |
220 | 0 | free(data.data); |
221 | 0 | out: |
222 | 0 | krb5_free_keyblock(ctx, keyblock); |
223 | 0 | return rv; |
224 | 0 | } |
225 | | |
226 | | static BOOL rdpear_send_payload(RDPEAR_PLUGIN* rdpear, IWTSVirtualChannelCallback* pChannelCallback, |
227 | | RdpEarPackageType packageType, wStream* payload) |
228 | 0 | { |
229 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
230 | 0 | BOOL ret = FALSE; |
231 | 0 | wStream* finalStream = nullptr; |
232 | 0 | SecBuffer cryptedBuffer = WINPR_C_ARRAY_INIT; |
233 | 0 | wStream* unencodedContent = rdpear_encodePayload(packageType, payload); |
234 | 0 | if (!unencodedContent) |
235 | 0 | goto out; |
236 | | |
237 | 0 | const size_t unencodedLen = Stream_GetPosition(unencodedContent); |
238 | |
|
239 | 0 | #if UINT32_MAX < SIZE_MAX |
240 | 0 | if (unencodedLen > UINT32_MAX) |
241 | 0 | goto out; |
242 | 0 | #endif |
243 | | |
244 | 0 | SecBuffer inBuffer = { (ULONG)unencodedLen, SECBUFFER_DATA, Stream_Buffer(unencodedContent) }; |
245 | |
|
246 | 0 | if (!freerdp_nla_encrypt(rdpear->rdp_context, &inBuffer, &cryptedBuffer)) |
247 | 0 | goto out; |
248 | | |
249 | 0 | finalStream = Stream_New(nullptr, 200); |
250 | 0 | if (!finalStream) |
251 | 0 | goto out; |
252 | 0 | Stream_Write_UINT32(finalStream, 0x4EACC3C8); /* ProtocolMagic (4 bytes) */ |
253 | 0 | Stream_Write_UINT32(finalStream, cryptedBuffer.cbBuffer); /* Length (4 bytes) */ |
254 | 0 | Stream_Write_UINT32(finalStream, 0x00000000); /* Version (4 bytes) */ |
255 | 0 | Stream_Write_UINT32(finalStream, 0x00000000); /* Reserved (4 bytes) */ |
256 | 0 | Stream_Write_UINT64(finalStream, 0); /* TsPkgContext (8 bytes) */ |
257 | | |
258 | | /* payload */ |
259 | 0 | if (!Stream_EnsureRemainingCapacity(finalStream, cryptedBuffer.cbBuffer)) |
260 | 0 | goto out; |
261 | | |
262 | 0 | Stream_Write(finalStream, cryptedBuffer.pvBuffer, cryptedBuffer.cbBuffer); |
263 | |
|
264 | 0 | const size_t pos = Stream_GetPosition(finalStream); |
265 | 0 | #if UINT32_MAX < SIZE_MAX |
266 | 0 | if (pos > UINT32_MAX) |
267 | 0 | goto out; |
268 | 0 | #endif |
269 | | |
270 | 0 | UINT status = callback->channel->Write(callback->channel, (ULONG)pos, |
271 | 0 | Stream_Buffer(finalStream), nullptr); |
272 | 0 | ret = (status == CHANNEL_RC_OK); |
273 | 0 | if (!ret) |
274 | 0 | WLog_DBG(TAG, "rdpear_send_payload=0x%x", status); |
275 | 0 | out: |
276 | 0 | sspi_SecBufferFree(&cryptedBuffer); |
277 | 0 | Stream_Free(unencodedContent, TRUE); |
278 | 0 | Stream_Free(finalStream, TRUE); |
279 | 0 | return ret; |
280 | 0 | } |
281 | | |
282 | | static UINT32 rdpear_kerb_version(UINT32 maxVersion, UINT32* pversion) |
283 | 0 | { |
284 | 0 | WLog_DBG(TAG, "-> KerbNegotiateVersion(v=0x%x)", maxVersion); |
285 | 0 | *pversion = 0; |
286 | 0 | return 0; |
287 | 0 | } |
288 | | |
289 | | static UINT32 rdpear_kerb_ComputeTgsChecksum(RDPEAR_PLUGIN* rdpear, |
290 | | const ComputeTgsChecksumReq* req, KERB_ASN1_DATA* resp) |
291 | 0 | { |
292 | 0 | krb5_checksum checksum = WINPR_C_ARRAY_INIT; |
293 | 0 | wStream* asn1Payload = nullptr; |
294 | |
|
295 | 0 | UINT32 status = ERROR_INVALID_DATA; |
296 | 0 | WLog_DBG(TAG, "-> ComputeTgsChecksum"); |
297 | |
|
298 | 0 | krb5_error_code rv = |
299 | 0 | kerb_do_checksum(rdpear->krbContext, req->Key, KRB5_KEYUSAGE_TGS_REQ_AUTH_CKSUM, |
300 | 0 | (krb5_cksumtype)req->ChecksumType, req->requestBody, &checksum); |
301 | 0 | if (rv) |
302 | 0 | goto out; |
303 | | |
304 | 0 | asn1Payload = rdpear_enc_Checksum(req->ChecksumType, &checksum); |
305 | 0 | if (!asn1Payload) |
306 | 0 | goto out; |
307 | | |
308 | 0 | resp->Pdu = 8; |
309 | 0 | if (!rdpear_set_asn1_buffer(resp, Stream_Buffer(asn1Payload), Stream_GetPosition(asn1Payload))) |
310 | 0 | goto out; |
311 | 0 | status = 0; |
312 | |
|
313 | 0 | out: |
314 | 0 | krb5_free_checksum_contents(rdpear->krbContext, &checksum); |
315 | 0 | Stream_Free(asn1Payload, TRUE); |
316 | 0 | return status; |
317 | 0 | } |
318 | | |
319 | | static UINT32 rdpear_kerb_BuildEncryptedAuthData(RDPEAR_PLUGIN* rdpear, |
320 | | const BuildEncryptedAuthDataReq* req, |
321 | | KERB_ASN1_DATA* asn1) |
322 | 0 | { |
323 | 0 | krb5_data encrypted = WINPR_C_ARRAY_INIT; |
324 | 0 | wStream* asn1Payload = nullptr; |
325 | 0 | krb5_error_code rv = 0; |
326 | |
|
327 | 0 | UINT32 status = ERROR_INVALID_DATA; |
328 | 0 | WLog_DBG(TAG, "-> BuildEncryptedAuthData"); |
329 | |
|
330 | 0 | rv = kerb_do_encrypt(rdpear->krbContext, req->Key, (krb5_keyusage)req->KeyUsage, |
331 | 0 | req->PlainAuthData, &encrypted); |
332 | 0 | if (rv) |
333 | 0 | goto out; |
334 | | |
335 | | /* do the encoding */ |
336 | 0 | asn1Payload = rdpear_enc_EncryptedData(req->Key->reserved2, &encrypted); |
337 | 0 | if (!asn1Payload) |
338 | 0 | goto out; |
339 | | |
340 | | // WLog_DBG(TAG, "rdpear_kerb_BuildEncryptedAuthData resp="); |
341 | | // winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(asn1Payload), Stream_GetPosition(asn1Payload)); |
342 | 0 | asn1->Pdu = 6; |
343 | 0 | if (!rdpear_set_asn1_buffer(asn1, Stream_Buffer(asn1Payload), Stream_GetPosition(asn1Payload))) |
344 | 0 | goto out; |
345 | 0 | status = 0; |
346 | |
|
347 | 0 | out: |
348 | 0 | krb5_free_data_contents(rdpear->krbContext, &encrypted); |
349 | 0 | Stream_Free(asn1Payload, TRUE); |
350 | 0 | return status; |
351 | 0 | } |
352 | | |
353 | | static char* KERB_RPC_UNICODESTR_to_charptr(const RPC_UNICODE_STRING* src) |
354 | 0 | { |
355 | 0 | WINPR_ASSERT(src); |
356 | 0 | return ConvertWCharNToUtf8Alloc(src->Buffer, src->strLength, nullptr); |
357 | 0 | } |
358 | | |
359 | | static BOOL extractAuthData(const KERB_ASN1_DATA* src, krb5_authdata* authData, BOOL* haveData) |
360 | 0 | { |
361 | 0 | WinPrAsn1Decoder dec = WINPR_C_ARRAY_INIT; |
362 | 0 | WinPrAsn1Decoder dec2 = WINPR_C_ARRAY_INIT; |
363 | 0 | WinPrAsn1Decoder dec3 = WINPR_C_ARRAY_INIT; |
364 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, src->Asn1Buffer, src->Asn1BufferHints.count); |
365 | 0 | BOOL error = FALSE; |
366 | 0 | WinPrAsn1_INTEGER adType = 0; |
367 | 0 | WinPrAsn1_OctetString os = WINPR_C_ARRAY_INIT; |
368 | |
|
369 | 0 | *haveData = FALSE; |
370 | 0 | if (!WinPrAsn1DecReadSequence(&dec, &dec2)) |
371 | 0 | return FALSE; |
372 | | |
373 | 0 | wStream subStream = WinPrAsn1DecGetStream(&dec2); |
374 | 0 | if (!Stream_GetRemainingLength(&subStream)) |
375 | 0 | return TRUE; |
376 | | |
377 | 0 | if (!WinPrAsn1DecReadSequence(&dec2, &dec3)) |
378 | 0 | return FALSE; |
379 | | |
380 | 0 | if (!WinPrAsn1DecReadContextualInteger(&dec3, 0, &error, &adType) || |
381 | 0 | !WinPrAsn1DecReadContextualOctetString(&dec3, 1, &error, &os, FALSE)) |
382 | 0 | return FALSE; |
383 | | |
384 | 0 | if (os.len > UINT32_MAX) |
385 | 0 | return FALSE; |
386 | | |
387 | 0 | authData->ad_type = adType; |
388 | 0 | authData->length = (unsigned int)os.len; |
389 | 0 | authData->contents = os.data; |
390 | 0 | *haveData = TRUE; |
391 | 0 | return TRUE; |
392 | 0 | } |
393 | | |
394 | | static BOOL extractChecksum(const KERB_ASN1_DATA* src, krb5_checksum* dst) |
395 | 0 | { |
396 | 0 | WinPrAsn1Decoder dec = WINPR_C_ARRAY_INIT; |
397 | 0 | WinPrAsn1Decoder dec2 = WINPR_C_ARRAY_INIT; |
398 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, src->Asn1Buffer, src->Asn1BufferHints.count); |
399 | 0 | BOOL error = FALSE; |
400 | 0 | WinPrAsn1_OctetString os; |
401 | |
|
402 | 0 | if (!WinPrAsn1DecReadSequence(&dec, &dec2)) |
403 | 0 | return FALSE; |
404 | | |
405 | 0 | WinPrAsn1_INTEGER cksumtype = 0; |
406 | 0 | if (!WinPrAsn1DecReadContextualInteger(&dec2, 0, &error, &cksumtype) || |
407 | 0 | !WinPrAsn1DecReadContextualOctetString(&dec2, 1, &error, &os, FALSE)) |
408 | 0 | return FALSE; |
409 | | |
410 | 0 | if (os.len > UINT32_MAX) |
411 | 0 | return FALSE; |
412 | 0 | dst->checksum_type = cksumtype; |
413 | 0 | dst->length = (unsigned int)os.len; |
414 | 0 | dst->contents = os.data; |
415 | 0 | return TRUE; |
416 | 0 | } |
417 | | |
418 | 0 | #define FILETIME_TO_UNIX_OFFSET_S 11644473600LL |
419 | | |
420 | | static LONGLONG krb5_time_to_FILETIME(const krb5_timestamp* ts, krb5_int32 usec) |
421 | 0 | { |
422 | 0 | WINPR_ASSERT(ts); |
423 | 0 | return (((*ts + FILETIME_TO_UNIX_OFFSET_S) * (1000LL * 1000LL) + usec) * 10LL); |
424 | 0 | } |
425 | | |
426 | | static void krb5_free_principal_contents(krb5_context ctx, krb5_principal principal) |
427 | 0 | { |
428 | 0 | WINPR_ASSERT(principal); |
429 | 0 | krb5_free_data_contents(ctx, &principal->realm); |
430 | 0 | krb5_free_data(ctx, principal->data); |
431 | 0 | } |
432 | | |
433 | | static UINT32 rdpear_kerb_CreateApReqAuthenticator(RDPEAR_PLUGIN* rdpear, |
434 | | const CreateApReqAuthenticatorReq* req, |
435 | | CreateApReqAuthenticatorResp* resp) |
436 | 0 | { |
437 | 0 | krb5_error_code rv = 0; |
438 | 0 | wStream* asn1EncodedAuth = nullptr; |
439 | 0 | krb5_data authenticator = WINPR_C_ARRAY_INIT; |
440 | 0 | krb5_data* der = nullptr; |
441 | 0 | krb5_keyblock* subkey = nullptr; |
442 | 0 | krb5_principal_data client = WINPR_C_ARRAY_INIT; |
443 | |
|
444 | 0 | UINT32 status = ERROR_INVALID_DATA; |
445 | 0 | WLog_DBG(TAG, "-> CreateApReqAuthenticator"); |
446 | |
|
447 | 0 | krb5_authdata authdata = WINPR_C_ARRAY_INIT; |
448 | 0 | krb5_authdata* authDataPtr[2] = { &authdata, nullptr }; |
449 | 0 | BOOL haveData = 0; |
450 | |
|
451 | 0 | if (!extractAuthData(req->AuthData, &authdata, &haveData)) |
452 | 0 | { |
453 | 0 | WLog_ERR(TAG, "error retrieving auth data"); |
454 | 0 | winpr_HexDump(TAG, WLOG_DEBUG, req->AuthData->Asn1Buffer, |
455 | 0 | req->AuthData->Asn1BufferHints.count); |
456 | 0 | goto out; |
457 | 0 | } |
458 | | |
459 | 0 | if (req->SkewTime->QuadPart) |
460 | 0 | { |
461 | 0 | WLog_ERR(TAG, "!!!!! should handle SkewTime !!!!!"); |
462 | 0 | } |
463 | |
|
464 | 0 | if (req->SubKey) |
465 | 0 | { |
466 | 0 | rv = RPC_ENCRYPTION_KEY_to_keyblock(rdpear->krbContext, req->SubKey, &subkey); |
467 | 0 | if (rv) |
468 | 0 | { |
469 | 0 | WLog_ERR(TAG, "error importing subkey"); |
470 | 0 | goto out; |
471 | 0 | } |
472 | 0 | } |
473 | | |
474 | 0 | krb5_authenticator authent = { .checksum = nullptr, |
475 | 0 | .subkey = nullptr, |
476 | 0 | .seq_number = req->SequenceNumber, |
477 | 0 | .authorization_data = haveData ? authDataPtr : nullptr }; |
478 | |
|
479 | 0 | client.type = req->ClientName->NameType; |
480 | 0 | if (req->ClientName->nameHints.count > INT32_MAX) |
481 | 0 | goto out; |
482 | | |
483 | 0 | client.length = (krb5_int32)req->ClientName->nameHints.count; |
484 | 0 | client.data = calloc(req->ClientName->nameHints.count, sizeof(krb5_data)); |
485 | 0 | if (!client.data) |
486 | 0 | goto out; |
487 | | |
488 | 0 | for (int i = 0; i < client.length; i++) |
489 | 0 | { |
490 | 0 | krb5_data* cur = &client.data[i]; |
491 | 0 | cur->data = KERB_RPC_UNICODESTR_to_charptr(&req->ClientName->Names[i]); |
492 | 0 | if (!cur->data) |
493 | 0 | goto out; |
494 | 0 | const size_t len = strnlen(cur->data, MAX_KEYTAB_NAME_LEN + 1); |
495 | 0 | if (len > MAX_KEYTAB_NAME_LEN) |
496 | 0 | { |
497 | 0 | WLog_ERR(TAG, |
498 | 0 | "Invalid ClientName length %d, limited to %" PRIuz |
499 | 0 | " characters. ClientName: (%s)", |
500 | 0 | MAX_KEYTAB_NAME_LEN, len, cur->data); |
501 | 0 | goto out; |
502 | 0 | } |
503 | 0 | cur->length = (unsigned int)len; |
504 | 0 | } |
505 | 0 | client.realm.data = KERB_RPC_UNICODESTR_to_charptr(req->ClientRealm); |
506 | 0 | if (!client.realm.data) |
507 | 0 | goto out; |
508 | | |
509 | 0 | const size_t len = strnlen(client.realm.data, MAX_KEYTAB_NAME_LEN + 1); |
510 | 0 | if (len > MAX_KEYTAB_NAME_LEN) |
511 | 0 | { |
512 | 0 | WLog_ERR(TAG, "Invalid realm length %d, limited to %" PRIuz "characters. Realm: (%s)", |
513 | 0 | MAX_KEYTAB_NAME_LEN, len, client.realm.data); |
514 | 0 | goto out; |
515 | 0 | } |
516 | 0 | client.realm.length = (unsigned int)len; |
517 | 0 | authent.client = &client; |
518 | |
|
519 | 0 | krb5_checksum checksum; |
520 | 0 | krb5_checksum* pchecksum = nullptr; |
521 | 0 | if (req->GssChecksum) |
522 | 0 | { |
523 | 0 | if (!extractChecksum(req->GssChecksum, &checksum)) |
524 | 0 | { |
525 | 0 | WLog_ERR(TAG, "Error getting the checksum"); |
526 | 0 | goto out; |
527 | 0 | } |
528 | 0 | pchecksum = &checksum; |
529 | 0 | } |
530 | 0 | authent.checksum = pchecksum; |
531 | |
|
532 | 0 | krb5_us_timeofday(rdpear->krbContext, &authent.ctime, &authent.cusec); |
533 | |
|
534 | 0 | rv = encode_krb5_authenticator(&authent, &der); |
535 | 0 | if (rv) |
536 | 0 | { |
537 | 0 | WLog_ERR(TAG, "error encoding authenticator"); |
538 | 0 | goto out; |
539 | 0 | } |
540 | | |
541 | 0 | KERB_ASN1_DATA plain_authent = { .Pdu = 0, |
542 | 0 | .Asn1Buffer = (BYTE*)der->data, |
543 | 0 | .Asn1BufferHints = { .count = der->length } }; |
544 | |
|
545 | 0 | rv = kerb_do_encrypt(rdpear->krbContext, req->EncryptionKey, (krb5_keyusage)req->KeyUsage, |
546 | 0 | &plain_authent, &authenticator); |
547 | 0 | if (rv) |
548 | 0 | { |
549 | 0 | WLog_ERR(TAG, "error encrypting authenticator"); |
550 | 0 | goto out; |
551 | 0 | } |
552 | | |
553 | 0 | asn1EncodedAuth = rdpear_enc_EncryptedData(req->EncryptionKey->reserved2, &authenticator); |
554 | 0 | if (!asn1EncodedAuth) |
555 | 0 | { |
556 | 0 | WLog_ERR(TAG, "error encoding to ASN1"); |
557 | 0 | rv = ENOMEM; |
558 | 0 | goto out; |
559 | 0 | } |
560 | | |
561 | | // WLog_DBG(TAG, "authenticator="); |
562 | | // winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(asn1EncodedAuth), |
563 | | // Stream_GetPosition(asn1EncodedAuth)); |
564 | | |
565 | 0 | if (!rdpear_set_asn1_buffer(&resp->Authenticator, Stream_Buffer(asn1EncodedAuth), |
566 | 0 | Stream_GetPosition(asn1EncodedAuth))) |
567 | 0 | goto out; |
568 | 0 | resp->AuthenticatorTime.QuadPart = krb5_time_to_FILETIME(&authent.ctime, authent.cusec); |
569 | 0 | status = 0; |
570 | |
|
571 | 0 | out: |
572 | | /* Pdu is the slot of EncryptedData in the host's compiled Kerberos ASN.1 module: 6 up to |
573 | | * Server 2022, 7 from Windows 11 24H2 / Server 2025, and an updated host rejects a mismatch. |
574 | | * The host labels its GSS checksum with its own Checksum slot: 8 on the older module, blank |
575 | | * on the newer one. */ |
576 | 0 | resp->Authenticator.Pdu = (req->GssChecksum && (req->GssChecksum->Pdu == 8)) ? 6 : 7; |
577 | 0 | WLog_VRB(TAG, "host checksum PDU %" PRIu32 " -> authenticator PDU %" PRIu32, |
578 | 0 | req->GssChecksum ? req->GssChecksum->Pdu : 0, resp->Authenticator.Pdu); |
579 | 0 | resp->KerbProtocolError = rv; |
580 | 0 | krb5_free_principal_contents(rdpear->krbContext, &client); |
581 | 0 | krb5_free_data(rdpear->krbContext, der); |
582 | 0 | krb5_free_data_contents(rdpear->krbContext, &authenticator); |
583 | 0 | if (subkey) |
584 | 0 | krb5_free_keyblock(rdpear->krbContext, subkey); |
585 | 0 | Stream_Free(asn1EncodedAuth, TRUE); |
586 | 0 | return status; |
587 | 0 | } |
588 | | |
589 | | static BOOL rdpear_findEncryptedData(const KERB_ASN1_DATA* src, int* penctype, krb5_data* data) |
590 | 0 | { |
591 | 0 | WinPrAsn1Decoder dec = WINPR_C_ARRAY_INIT; |
592 | 0 | WinPrAsn1Decoder dec2 = WINPR_C_ARRAY_INIT; |
593 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_DER, src->Asn1Buffer, src->Asn1BufferHints.count); |
594 | 0 | BOOL error = FALSE; |
595 | 0 | WinPrAsn1_INTEGER encType = 0; |
596 | 0 | WinPrAsn1_OctetString os = WINPR_C_ARRAY_INIT; |
597 | |
|
598 | 0 | if (!WinPrAsn1DecReadSequence(&dec, &dec2) || |
599 | 0 | !WinPrAsn1DecReadContextualInteger(&dec2, 0, &error, &encType) || |
600 | 0 | !WinPrAsn1DecReadContextualOctetString(&dec2, 2, &error, &os, FALSE)) |
601 | 0 | return FALSE; |
602 | | |
603 | 0 | if (os.len > UINT32_MAX) |
604 | 0 | return FALSE; |
605 | 0 | data->data = (char*)os.data; |
606 | 0 | data->length = (unsigned int)os.len; |
607 | 0 | *penctype = encType; |
608 | 0 | return TRUE; |
609 | 0 | } |
610 | | |
611 | | static UINT32 rdpear_kerb_UnpackKdcReplyBody(RDPEAR_PLUGIN* rdpear, |
612 | | const UnpackKdcReplyBodyReq* req, |
613 | | UnpackKdcReplyBodyResp* resp) |
614 | 0 | { |
615 | 0 | if (req->StrengthenKey) |
616 | 0 | { |
617 | 0 | WLog_ERR(TAG, "StrengthenKey not supported yet"); |
618 | 0 | return ERROR_INVALID_DATA; |
619 | 0 | } |
620 | | |
621 | 0 | WLog_DBG(TAG, "-> UnpackKdcReplyBody: KeyUsage=0x%x PDU=0x%x", req->KeyUsage, req->Pdu); |
622 | | // WLog_DBG(TAG, "encryptedPayload="); |
623 | | // winpr_HexDump(TAG, WLOG_DEBUG, req->EncryptedData->Asn1Buffer, |
624 | | // req->EncryptedData->Asn1BufferHints.count); |
625 | |
|
626 | 0 | krb5_data asn1Data = WINPR_C_ARRAY_INIT; |
627 | 0 | int encType = 0; |
628 | 0 | if (!rdpear_findEncryptedData(req->EncryptedData, &encType, &asn1Data) || !asn1Data.length) |
629 | 0 | return ERROR_INVALID_DATA; |
630 | | |
631 | 0 | resp->KerbProtocolError = kerb_do_decrypt( |
632 | 0 | rdpear->krbContext, req->Key, (krb5_keyusage)req->KeyUsage, &asn1Data, &resp->ReplyBody); |
633 | 0 | resp->ReplyBody.Pdu = req->Pdu; |
634 | |
|
635 | 0 | return 0; |
636 | 0 | } |
637 | | |
638 | | static UINT32 rdpear_kerb_DecryptApReply(RDPEAR_PLUGIN* rdpear, const DecryptApReplyReq* req, |
639 | | KERB_ASN1_DATA* resp) |
640 | 0 | { |
641 | 0 | WLog_DBG(TAG, "-> DecryptApReply"); |
642 | | // winpr_HexDump(TAG, WLOG_DEBUG, req->EncryptedReply->Asn1Buffer, |
643 | | // req->EncryptedReply->Asn1BufferHints.count); |
644 | |
|
645 | 0 | krb5_data asn1Data = WINPR_C_ARRAY_INIT; |
646 | 0 | int encType = 0; |
647 | 0 | if (!rdpear_findEncryptedData(req->EncryptedReply, &encType, &asn1Data) || !asn1Data.length) |
648 | 0 | return ERROR_INVALID_DATA; |
649 | | |
650 | 0 | resp->Pdu = 0x31; |
651 | 0 | krb5_error_code rv = kerb_do_decrypt(rdpear->krbContext, req->Key, KRB5_KEYUSAGE_AP_REP_ENCPART, |
652 | 0 | &asn1Data, resp); |
653 | 0 | if (rv != 0) |
654 | 0 | { |
655 | 0 | WLog_ERR(TAG, "error decrypting"); |
656 | 0 | return ERROR_INVALID_DATA; |
657 | 0 | } |
658 | | |
659 | | // WLog_DBG(TAG, "response="); |
660 | | // winpr_HexDump(TAG, WLOG_DEBUG, resp->Asn1Buffer, resp->Asn1BufferHints.count); |
661 | 0 | return 0; |
662 | 0 | } |
663 | | |
664 | | static UINT32 rdpear_kerb_PackApReply(RDPEAR_PLUGIN* rdpear, const PackApReplyReq* req, |
665 | | PackApReplyResp* resp) |
666 | 0 | { |
667 | 0 | UINT32 status = ERROR_INVALID_DATA; |
668 | 0 | krb5_data asn1Data = WINPR_C_ARRAY_INIT; |
669 | 0 | krb5_data* out = nullptr; |
670 | |
|
671 | 0 | WLog_DBG(TAG, "-> PackApReply"); |
672 | |
|
673 | 0 | krb5_error_code rv = kerb_do_encrypt(rdpear->krbContext, req->SessionKey, |
674 | 0 | KRB5_KEYUSAGE_AP_REP_ENCPART, req->ReplyBody, &asn1Data); |
675 | 0 | if (rv) |
676 | 0 | goto out; |
677 | | |
678 | 0 | krb5_ap_rep reply; |
679 | 0 | reply.enc_part.kvno = KRB5_PVNO; |
680 | 0 | reply.enc_part.enctype = (krb5_enctype)req->SessionKey->reserved2; |
681 | 0 | reply.enc_part.ciphertext.length = asn1Data.length; |
682 | 0 | reply.enc_part.ciphertext.data = asn1Data.data; |
683 | |
|
684 | 0 | rv = encode_krb5_ap_rep(&reply, &out); |
685 | 0 | if (rv) |
686 | 0 | goto out; |
687 | | |
688 | 0 | resp->PackedReplyHints.count = out->length; |
689 | 0 | resp->PackedReply = |
690 | 0 | winpr_ndr_pointer_new(winpr_ndr_uint8Array_descr(), &resp->PackedReplyHints, out->length); |
691 | 0 | if (!resp->PackedReply) |
692 | 0 | goto out; |
693 | 0 | memcpy(resp->PackedReply, out->data, out->length); |
694 | 0 | status = 0; |
695 | 0 | out: |
696 | 0 | krb5_free_data(rdpear->krbContext, out); |
697 | 0 | krb5_free_data_contents(rdpear->krbContext, &asn1Data); |
698 | 0 | return status; |
699 | 0 | } |
700 | | |
701 | | /** @brief handles a Kerberos call |
702 | | * |
703 | | * @return TRUE if the call is handled, FALSE if it's not supported: it's answered without data |
704 | | */ |
705 | | static BOOL rdpear_kerb_call(RDPEAR_PLUGIN* rdpear, const KerbCredIsoRemoteInput* input, |
706 | | KerbCredIsoRemoteOutput* output) |
707 | 0 | { |
708 | 0 | const KerbCredIsoRemoteInputUnion* in = &input->Input; |
709 | 0 | KerbCredIsoRemoteOutputUnion* out = &output->Output; |
710 | |
|
711 | 0 | output->CallId = input->CallId; |
712 | 0 | switch (input->CallId) |
713 | 0 | { |
714 | 0 | case RemoteCallKerbNegotiateVersion: |
715 | 0 | output->Status = rdpear_kerb_version(in->NegotiateVersion, &out->NegotiateVersion); |
716 | 0 | return TRUE; |
717 | 0 | case RemoteCallKerbCreateApReqAuthenticator: |
718 | 0 | output->Status = rdpear_kerb_CreateApReqAuthenticator( |
719 | 0 | rdpear, &in->CreateApReqAuthenticator, &out->CreateApReqAuthenticator); |
720 | 0 | return TRUE; |
721 | 0 | case RemoteCallKerbDecryptApReply: |
722 | 0 | output->Status = |
723 | 0 | rdpear_kerb_DecryptApReply(rdpear, &in->DecryptApReply, &out->DecryptApReply); |
724 | 0 | return TRUE; |
725 | 0 | case RemoteCallKerbComputeTgsChecksum: |
726 | 0 | output->Status = rdpear_kerb_ComputeTgsChecksum(rdpear, &in->ComputeTgsChecksum, |
727 | 0 | &out->ComputeTgsChecksum); |
728 | 0 | return TRUE; |
729 | 0 | case RemoteCallKerbBuildEncryptedAuthData: |
730 | 0 | output->Status = rdpear_kerb_BuildEncryptedAuthData(rdpear, &in->BuildEncryptedAuthData, |
731 | 0 | &out->BuildEncryptedAuthData); |
732 | 0 | return TRUE; |
733 | 0 | case RemoteCallKerbUnpackKdcReplyBody: |
734 | 0 | output->Status = rdpear_kerb_UnpackKdcReplyBody(rdpear, &in->UnpackKdcReplyBody, |
735 | 0 | &out->UnpackKdcReplyBody); |
736 | 0 | return TRUE; |
737 | 0 | case RemoteCallKerbPackApReply: |
738 | 0 | output->Status = rdpear_kerb_PackApReply(rdpear, &in->PackApReply, &out->PackApReply); |
739 | 0 | return TRUE; |
740 | 0 | default: |
741 | 0 | WLog_DBG(TAG, "Unhandled Kerberos callId=0x%x", input->CallId); |
742 | 0 | return FALSE; |
743 | 0 | } |
744 | 0 | } |
745 | | |
746 | | /** @brief handles a NTLM call |
747 | | * |
748 | | * The NTLM supplemental credentials are not sent by the client, the calls using them are answered |
749 | | * with STATUS_NOT_SUPPORTED. |
750 | | * |
751 | | * @return TRUE if the call is handled, FALSE if it's not supported: it's answered without data |
752 | | */ |
753 | | static BOOL rdpear_ntlm_call(const NtlmCredIsoRemoteInput* input, NtlmCredIsoRemoteOutput* output) |
754 | 0 | { |
755 | 0 | output->CallId = input->CallId; |
756 | 0 | switch (input->CallId) |
757 | 0 | { |
758 | 0 | case RemoteCallNtlmNegotiateVersion: |
759 | 0 | WLog_DBG(TAG, "-> NtlmNegotiateVersion(v=0x%x)", input->Input.NegotiateVersion); |
760 | 0 | output->Output.NegotiateVersion = 0; |
761 | 0 | output->Status = STATUS_SUCCESS; |
762 | 0 | return TRUE; |
763 | 0 | case RemoteCallNtlmLm20GetNtlm3ChallengeResponse: |
764 | 0 | case RemoteCallNtlmCalculateNtResponse: |
765 | 0 | case RemoteCallNtlmCalculateUserSessionKeyNt: |
766 | 0 | case RemoteCallNtlmCompareCredentials: |
767 | 0 | WLog_DBG(TAG, "-> NTLM callId=0x%x: no NTLM credentials", input->CallId); |
768 | 0 | output->Status = (UINT32)STATUS_NOT_SUPPORTED; |
769 | 0 | return TRUE; |
770 | 0 | default: |
771 | 0 | WLog_DBG(TAG, "Unhandled NTLM callId=0x%x", input->CallId); |
772 | 0 | return FALSE; |
773 | 0 | } |
774 | 0 | } |
775 | | |
776 | | static UINT rdpear_decode_payload(RDPEAR_PLUGIN* rdpear, const WinPrAsn1_OctetString* packageName, |
777 | | IWTSVirtualChannelCallback* pChannelCallback, wStream* s) |
778 | 0 | { |
779 | 0 | UINT ret = ERROR_INVALID_DATA; |
780 | 0 | WinPrNdrDecoder* decoder = nullptr; |
781 | 0 | WinPrNdrEncoder* encoder = nullptr; |
782 | 0 | KerbCredIsoRemoteInput kerbInput = WINPR_C_ARRAY_INIT; |
783 | 0 | KerbCredIsoRemoteOutput kerbOutput = WINPR_C_ARRAY_INIT; |
784 | 0 | NtlmCredIsoRemoteInput ntlmInput = WINPR_C_ARRAY_INIT; |
785 | 0 | NtlmCredIsoRemoteOutput ntlmOutput = WINPR_C_ARRAY_INIT; |
786 | 0 | BOOL handled = FALSE; |
787 | 0 | BOOL encoded = FALSE; |
788 | |
|
789 | 0 | const RdpEarPackageType packageType = rdpear_packageType_from_name(packageName); |
790 | 0 | if (packageType == RDPEAR_PACKAGE_UNKNOWN) |
791 | 0 | { |
792 | 0 | WLog_ERR(TAG, "unknown package type"); |
793 | 0 | return ERROR_INVALID_DATA; |
794 | 0 | } |
795 | | |
796 | 0 | wStream* respStream = Stream_New(nullptr, 500); |
797 | 0 | if (!respStream) |
798 | 0 | return ERROR_INVALID_DATA; |
799 | | |
800 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 16)) |
801 | 0 | goto out; |
802 | 0 | Stream_Seek(s, 16); /* skip first 16 bytes */ |
803 | |
|
804 | 0 | decoder = winpr_ndr_decoder_new_fromStream(s); |
805 | 0 | if (!decoder) |
806 | 0 | goto out; |
807 | | |
808 | 0 | encoder = winpr_ndr_encoder_from_decoder(decoder); |
809 | 0 | if (!encoder) |
810 | 0 | goto out; |
811 | | |
812 | 0 | Stream_Write(respStream, payloadHeader, sizeof(payloadHeader)); |
813 | |
|
814 | 0 | if (packageType == RDPEAR_PACKAGE_KERBEROS) |
815 | 0 | { |
816 | 0 | if (!rdpear_kerb_input_read(decoder, s, &kerbInput)) |
817 | 0 | goto out; |
818 | | |
819 | 0 | handled = rdpear_kerb_call(rdpear, &kerbInput, &kerbOutput); |
820 | 0 | encoded = rdpear_kerb_output_write(encoder, respStream, &kerbOutput); |
821 | 0 | } |
822 | 0 | else |
823 | 0 | { |
824 | 0 | if (!rdpear_ntlm_input_read(decoder, s, &ntlmInput)) |
825 | 0 | goto out; |
826 | | |
827 | 0 | handled = rdpear_ntlm_call(&ntlmInput, &ntlmOutput); |
828 | 0 | encoded = rdpear_ntlm_output_write(encoder, respStream, &ntlmOutput); |
829 | 0 | } |
830 | | |
831 | 0 | if (!encoded) |
832 | 0 | { |
833 | 0 | WLog_DBG(TAG, "error encoding the reply"); |
834 | 0 | goto out; |
835 | 0 | } |
836 | | |
837 | 0 | if (!rdpear_send_payload(rdpear, pChannelCallback, packageType, respStream)) |
838 | 0 | { |
839 | 0 | WLog_DBG(TAG, "rdpear_send_payload !!!!!!!!"); |
840 | 0 | goto out; |
841 | 0 | } |
842 | | |
843 | 0 | ret = handled ? CHANNEL_RC_OK : CHANNEL_RC_NOT_OPEN; |
844 | 0 | out: |
845 | 0 | rdpear_kerb_input_free(&kerbInput); |
846 | 0 | rdpear_kerb_output_free(&kerbOutput); |
847 | 0 | rdpear_ntlm_input_free(&ntlmInput); |
848 | 0 | rdpear_ntlm_output_free(&ntlmOutput); |
849 | 0 | winpr_ndr_decoder_destroy(&decoder); |
850 | 0 | winpr_ndr_encoder_destroy(&encoder); |
851 | 0 | Stream_Free(respStream, TRUE); |
852 | 0 | return ret; |
853 | 0 | } |
854 | | |
855 | | static UINT rdpear_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s) |
856 | 0 | { |
857 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
858 | 0 | WINPR_ASSERT(callback); |
859 | 0 | UINT ret = ERROR_INVALID_DATA; |
860 | | |
861 | | // winpr_HexDump(TAG, WLOG_DEBUG, Stream_PointerAs(s, BYTE), Stream_GetRemainingLength(s)); |
862 | |
|
863 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 24)) |
864 | 0 | return ERROR_INVALID_DATA; |
865 | | |
866 | 0 | UINT32 protocolMagic = Stream_Get_UINT32(s); |
867 | 0 | if (protocolMagic != 0x4EACC3C8) |
868 | 0 | return ERROR_INVALID_DATA; |
869 | | |
870 | 0 | UINT32 Length = Stream_Get_UINT32(s); |
871 | 0 | UINT32 Version = Stream_Get_UINT32(s); |
872 | 0 | if (Version != 0x00000000) |
873 | 0 | return ERROR_INVALID_DATA; |
874 | | |
875 | 0 | Stream_Seek(s, 4); /* Reserved (4 bytes) */ |
876 | 0 | Stream_Seek(s, 8); /* TsPkgContext (8 bytes) */ |
877 | |
|
878 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, Length)) |
879 | 0 | return ERROR_INVALID_DATA; |
880 | | |
881 | 0 | SecBuffer inBuffer = { Length, SECBUFFER_TOKEN, Stream_PointerAs(s, void) }; |
882 | 0 | SecBuffer decrypted = WINPR_C_ARRAY_INIT; |
883 | |
|
884 | 0 | RDPEAR_PLUGIN* rdpear = (RDPEAR_PLUGIN*)callback->plugin; |
885 | 0 | WINPR_ASSERT(rdpear); |
886 | 0 | if (!freerdp_nla_decrypt(rdpear->rdp_context, &inBuffer, &decrypted)) |
887 | 0 | goto out; |
888 | | |
889 | 0 | WinPrAsn1Decoder dec = WINPR_C_ARRAY_INIT; |
890 | 0 | WinPrAsn1Decoder dec2 = WINPR_C_ARRAY_INIT; |
891 | 0 | wStream decodedStream = WINPR_C_ARRAY_INIT; |
892 | 0 | Stream_StaticInit(&decodedStream, decrypted.pvBuffer, decrypted.cbBuffer); |
893 | 0 | WinPrAsn1Decoder_Init(&dec, WINPR_ASN1_DER, &decodedStream); |
894 | |
|
895 | 0 | if (!WinPrAsn1DecReadSequence(&dec, &dec2)) |
896 | 0 | goto out; |
897 | | |
898 | 0 | WinPrAsn1_OctetString packageName = WINPR_C_ARRAY_INIT; |
899 | 0 | WinPrAsn1_OctetString payload = WINPR_C_ARRAY_INIT; |
900 | 0 | BOOL error = 0; |
901 | 0 | if (!WinPrAsn1DecReadContextualOctetString(&dec2, 1, &error, &packageName, FALSE)) |
902 | 0 | goto out; |
903 | | |
904 | 0 | if (!WinPrAsn1DecReadContextualOctetString(&dec2, 2, &error, &payload, FALSE)) |
905 | 0 | goto out; |
906 | | |
907 | 0 | wStream payloadStream = WINPR_C_ARRAY_INIT; |
908 | 0 | Stream_StaticInit(&payloadStream, payload.data, payload.len); |
909 | |
|
910 | 0 | ret = rdpear_decode_payload(rdpear, &packageName, pChannelCallback, &payloadStream); |
911 | 0 | out: |
912 | 0 | sspi_SecBufferFree(&decrypted); |
913 | 0 | return ret; |
914 | 0 | } |
915 | | |
916 | | /** |
917 | | * Function description |
918 | | * |
919 | | * @return 0 on success, otherwise a Win32 error code |
920 | | */ |
921 | | static UINT rdpear_on_open(IWTSVirtualChannelCallback* pChannelCallback) |
922 | 0 | { |
923 | 0 | WINPR_UNUSED(pChannelCallback); |
924 | 0 | return CHANNEL_RC_OK; |
925 | 0 | } |
926 | | |
927 | | /** |
928 | | * Function description |
929 | | * |
930 | | * @return 0 on success, otherwise a Win32 error code |
931 | | */ |
932 | | static UINT rdpear_on_close(IWTSVirtualChannelCallback* pChannelCallback) |
933 | 0 | { |
934 | 0 | WINPR_UNUSED(pChannelCallback); |
935 | 0 | return CHANNEL_RC_OK; |
936 | 0 | } |
937 | | |
938 | | static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base) |
939 | 0 | { |
940 | 0 | WINPR_ASSERT(base); |
941 | |
|
942 | 0 | RDPEAR_PLUGIN* rdpear = (RDPEAR_PLUGIN*)base; |
943 | 0 | krb5_free_context(rdpear->krbContext); |
944 | 0 | } |
945 | | |
946 | | static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext, rdpSettings* settings) |
947 | 0 | { |
948 | 0 | WINPR_ASSERT(base); |
949 | 0 | WINPR_UNUSED(settings); |
950 | |
|
951 | 0 | RDPEAR_PLUGIN* rdpear = (RDPEAR_PLUGIN*)base; |
952 | 0 | rdpear->rdp_context = rcontext; |
953 | 0 | if (krb5_init_context(&rdpear->krbContext)) |
954 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
955 | 0 | return CHANNEL_RC_OK; |
956 | 0 | } |
957 | | |
958 | | static const IWTSVirtualChannelCallback rdpear_callbacks = { rdpear_on_data_received, |
959 | | rdpear_on_open, rdpear_on_close, |
960 | | nullptr }; |
961 | | |
962 | | /** |
963 | | * Function description |
964 | | * |
965 | | * @return 0 on success, otherwise a Win32 error code |
966 | | */ |
967 | | FREERDP_ENTRY_POINT(UINT rdpear_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints)) |
968 | 0 | { |
969 | 0 | return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, RDPEAR_DVC_CHANNEL_NAME, |
970 | 0 | sizeof(RDPEAR_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK), |
971 | 0 | &rdpear_callbacks, init_plugin_cb, terminate_plugin_cb); |
972 | 0 | } |