FreeRDP
Loading...
Searching...
No Matches
rdpewa_cbor.c
1
18
19#include <freerdp/config.h>
20
21#include <string.h>
22#include <stdlib.h>
23
24#include <cbor.h>
25
26#include <winpr/assert.h>
27#include <winpr/endian.h>
28
29#include <freerdp/channels/log.h>
30
31#include "rdpewa_cbor.h"
32
33#define TAG CHANNELS_TAG("rdpewa.client")
34
35WINPR_ATTR_NODISCARD
36static BOOL check_duplicate(size_t offset, const char* key, BOOL* array, size_t arraylen)
37{
38 if (offset >= arraylen)
39 {
40 WLog_ERR(TAG, "Invalid offset %" PRIuz " for key '%s', maximum is %" PRIuz, offset, key,
41 arraylen);
42 return FALSE;
43 }
44 if (array[offset])
45 {
46 WLog_ERR(TAG, "Duplicate key '%s' at offset %" PRIuz, key, offset);
47 return FALSE;
48 }
49 array[offset] = TRUE;
50 return TRUE;
51}
52
53BOOL rdpewa_cbor_decode_request(const BYTE* data, size_t length, RDPEWA_REQUEST* out)
54{
55 struct cbor_load_result result = WINPR_C_ARRAY_INIT;
56 BOOL ret = FALSE;
57
58 WINPR_ASSERT(data);
59 WINPR_ASSERT(out);
60
61 memset(out, 0, sizeof(RDPEWA_REQUEST));
62
63 cbor_item_t* root = cbor_load(data, length, &result);
64 if (!root || result.error.code != CBOR_ERR_NONE)
65 {
66 WLog_ERR(TAG, "Failed to decode CBOR request, error %u at position %" PRIuz,
67 result.error.code, result.error.position);
68 goto out;
69 }
70
71 if (!cbor_isa_map(root))
72 {
73 WLog_ERR(TAG, "CBOR request is not a map");
74 goto out;
75 }
76
77 const size_t mapSize = cbor_map_size(root);
78 struct cbor_pair* pairs = cbor_map_handle(root);
79
80 WLog_DBG(TAG, "Request CBOR map has %" PRIuz " entries", mapSize);
81
82 BOOL keyAlreadyUsed[14] = WINPR_C_ARRAY_INIT;
83 for (size_t i = 0; i < mapSize; i++)
84 {
85 cbor_item_t* key = pairs[i].key;
86 cbor_item_t* value = pairs[i].value;
87
88 if (!cbor_isa_string(key))
89 continue;
90
91 const char* keyStr = (const char*)cbor_string_handle(key);
92 const size_t keyLen = cbor_string_length(key);
93
94 WLog_DBG(TAG, " key[%" PRIuz "]: \"%.*s\" type=%u", i,
95 WINPR_ASSERTING_INT_CAST(int, keyLen), keyStr, cbor_typeof(value));
96
97 switch (keyLen)
98 {
99 case 4:
100 if (memcmp(keyStr, "rpId", 4) == 0)
101 {
102 if (!check_duplicate(0, keyStr, keyAlreadyUsed, sizeof(keyAlreadyUsed)))
103 goto out;
104
105 if (cbor_isa_string(value))
106 {
107 size_t slen = cbor_string_length(value);
108 if (slen >= sizeof(out->rpId))
109 slen = sizeof(out->rpId) - 1;
110 memcpy(out->rpId, cbor_string_handle(value), slen);
111 out->rpId[slen] = '\0';
112 }
113 }
114 break;
115 case 5:
116 if (memcmp(keyStr, "flags", 5) == 0)
117 {
118 if (!check_duplicate(1, keyStr, keyAlreadyUsed, sizeof(keyAlreadyUsed)))
119 goto out;
120
121 if (!cbor_isa_uint(value))
122 {
123 WLog_ERR(TAG, "\"flags\"is not an unsigned int");
124 goto out;
125 }
126 out->flags = WINPR_ASSERTING_INT_CAST(UINT32, cbor_get_int(value));
127 }
128 break;
129 case 7:
130 if (memcmp(keyStr, "command", 7) == 0)
131 {
132 if (!check_duplicate(2, keyStr, keyAlreadyUsed, sizeof(keyAlreadyUsed)))
133 goto out;
134
135 if (!cbor_isa_uint(value))
136 {
137 WLog_ERR(TAG, "\"command\"is not an unsigned int");
138 goto out;
139 }
140 out->command = WINPR_ASSERTING_INT_CAST(UINT32, cbor_get_int(value));
141 }
142 else if (memcmp(keyStr, "request", 7) == 0)
143 {
144 if (!check_duplicate(3, keyStr, keyAlreadyUsed, sizeof(keyAlreadyUsed)))
145 goto out;
146
147 if (!cbor_isa_bytestring(value))
148 {
149 WLog_ERR(TAG, "\"request\"is not a byte string");
150 goto out;
151 }
152 out->requestLen = cbor_bytestring_length(value);
153 free(out->request);
154 out->request = malloc(out->requestLen);
155 if (!out->request)
156 {
157 out->requestLen = 0;
158 goto out;
159 }
160 memcpy(out->request, cbor_bytestring_handle(value), out->requestLen);
161 }
162 else if (memcmp(keyStr, "timeout", 7) == 0)
163 {
164 if (!check_duplicate(4, keyStr, keyAlreadyUsed, sizeof(keyAlreadyUsed)))
165 goto out;
166
167 if (!cbor_isa_uint(value))
168 {
169 WLog_ERR(TAG, "\"timeout\"is not an unsigned int");
170 goto out;
171 }
172 out->timeout = WINPR_ASSERTING_INT_CAST(UINT32, cbor_get_int(value));
173 }
174 break;
175 case 13:
176 if (memcmp(keyStr, "transactionId", 13) == 0)
177 {
178 if (!check_duplicate(5, keyStr, keyAlreadyUsed, sizeof(keyAlreadyUsed)))
179 goto out;
180
181 if (!cbor_isa_bytestring(value) || cbor_bytestring_length(value) != 16)
182 {
183 WLog_ERR(TAG, "\"transactionId\"is not a 16-byte byte string");
184 goto out;
185 }
186 memcpy(out->transactionId, cbor_bytestring_handle(value), 16);
187 out->hasTransactionId = TRUE;
188 }
189 break;
190 default:
191 break;
192 }
193 }
194
195 if (out->command == 0)
196 {
197 WLog_ERR(TAG, "Missing or invalid \"command\" in request");
198 goto out;
199 }
200
201 ret = TRUE;
202out:
203 if (!ret)
204 {
205 free(out->request);
206 out->request = nullptr;
207 out->requestLen = 0;
208 }
209 if (root)
210 cbor_decref(&root);
211 return ret;
212}
213
214static BOOL wrap_cbor_map_add(cbor_item_t* root, cbor_item_t* diKey, cbor_item_t* diMap)
215{
216 const bool rc = cbor_map_add(root, (struct cbor_pair){ .key = diKey, .value = diMap });
217 cbor_decref(&diKey);
218 cbor_decref(&diMap);
219 return rc;
220}
221
222wStream* rdpewa_cbor_encode_webauthn_response(HRESULT hresult, BYTE ctapStatus,
223 const BYTE* ctapData, size_t ctapLen,
224 const RDPEWA_DEVICE_INFO* devInfo)
225{
226 wStream* s = nullptr;
227 cbor_item_t* root = nullptr;
228 size_t cborLen = 0;
229
230 WINPR_ASSERT(devInfo);
231
232 /* Build the inner "Response" byte string: status byte + CTAP CBOR data */
233 size_t responseLen = 1 + ctapLen;
234 BYTE* responseData = malloc(responseLen);
235 if (!responseData)
236 return nullptr;
237
238 responseData[0] = ctapStatus;
239 if (ctapData && ctapLen > 0)
240 memcpy(responseData + 1, ctapData, ctapLen);
241
242 root = cbor_new_definite_map(3);
243 if (!root)
244 goto out;
245
246 /* "deviceInfo" key */
247 cbor_item_t* diKey = cbor_build_string("deviceInfo");
248 cbor_item_t* diMap = cbor_new_definite_map(11);
249 if (!diKey || !diMap)
250 {
251 if (diKey)
252 cbor_decref(&diKey);
253 if (diMap)
254 cbor_decref(&diMap);
255 goto out;
256 }
257 {
258 /* The casing is all over the place, but that's how it's defined in the spec */
259 cbor_item_t* pairs[][2] = {
260 { cbor_build_string("maxMsgSize"),
261 cbor_build_uint16(WINPR_ASSERTING_INT_CAST(uint16_t, devInfo->maxMsgSize)) },
262 { cbor_build_string("maxSerializedLargeBlobArray"),
263 cbor_build_uint16(
264 WINPR_ASSERTING_INT_CAST(uint16_t, devInfo->maxSerializedLargeBlobArray)) },
265 { cbor_build_string("providerType"), cbor_build_string(devInfo->providerType) },
266 { cbor_build_string("providerName"), cbor_build_string(devInfo->providerName) },
267 { cbor_build_string("devicePath"), cbor_build_string(devInfo->devicePath) },
268 { cbor_build_string("Manufacturer"), cbor_build_string(devInfo->manufacturer) },
269 { cbor_build_string("Product"), cbor_build_string(devInfo->product) },
270 { cbor_build_string("aaGuid"),
271 cbor_build_bytestring(devInfo->aaGuid, sizeof(devInfo->aaGuid)) },
272 { cbor_build_string("uvStatus"), cbor_build_uint8(devInfo->uvStatus) },
273 { cbor_build_string("uvRetries"), cbor_build_uint8(devInfo->uvRetries) },
274 { cbor_build_string("transports"),
275 cbor_build_uint8(WINPR_ASSERTING_INT_CAST(uint8_t, devInfo->transports)) }
276 };
277 for (size_t i = 0; i < 11; i++)
278 {
279 if (!wrap_cbor_map_add(diMap, pairs[i][0], pairs[i][1]))
280 goto out;
281 }
282 }
283 if (!wrap_cbor_map_add(root, diKey, diMap))
284 goto out;
285
286 /* "status" key (lowercase per spec example hex) */
287 cbor_item_t* statusKey = cbor_build_string("status");
288 cbor_item_t* statusVal = cbor_build_uint8(0);
289 if (!wrap_cbor_map_add(root, statusKey, statusVal))
290 goto out;
291
292 /* "response" key (lowercase per spec example hex) */
293 cbor_item_t* respKey = cbor_build_string("response");
294 cbor_item_t* respVal = cbor_build_bytestring(responseData, responseLen);
295 if (!wrap_cbor_map_add(root, respKey, respVal))
296 goto out;
297
298 cborLen = cbor_serialized_size(root);
299 if (cborLen == 0)
300 goto out;
301
302 /* Response: 4-byte LE HRESULT + CBOR map */
303 s = Stream_New(nullptr, 4 + cborLen);
304 if (!s)
305 goto out;
306
307 Stream_Write_UINT32(s, (UINT32)hresult);
308 if (cbor_serialize(root, Stream_Pointer(s), cborLen) == 0)
309 {
310 Stream_Free(s, TRUE);
311 s = nullptr;
312 goto out;
313 }
314 Stream_Seek(s, cborLen);
315
316out:
317 free(responseData);
318 if (root)
319 cbor_decref(&root);
320 return s;
321}
322
323wStream* rdpewa_cbor_encode_simple_response(HRESULT hresult, UINT32 value)
324{
325 wStream* s = Stream_New(nullptr, 8);
326 if (!s)
327 return nullptr;
328
329 Stream_Write_UINT32(s, (UINT32)hresult);
330 Stream_Write_UINT32(s, value);
331 return s;
332}
333
334wStream* rdpewa_cbor_encode_hresult_response(HRESULT hresult)
335{
336 wStream* s = Stream_New(nullptr, 4);
337 if (!s)
338 return nullptr;
339
340 Stream_Write_UINT32(s, (UINT32)hresult);
341 return s;
342}
Device info for the response map.
Definition rdpewa_cbor.h:42
Decoded MS-RDPEWA request message.
Definition rdpewa_cbor.h:29