19#include <freerdp/config.h>
26#include <winpr/assert.h>
27#include <winpr/endian.h>
29#include <freerdp/channels/log.h>
31#include "rdpewa_cbor.h"
33#define TAG CHANNELS_TAG("rdpewa.client")
36static BOOL check_duplicate(
size_t offset,
const char* key, BOOL* array,
size_t arraylen)
38 if (offset >= arraylen)
40 WLog_ERR(TAG,
"Invalid offset %" PRIuz
" for key '%s', maximum is %" PRIuz, offset, key,
46 WLog_ERR(TAG,
"Duplicate key '%s' at offset %" PRIuz, key, offset);
53BOOL rdpewa_cbor_decode_request(
const BYTE* data,
size_t length,
RDPEWA_REQUEST* out)
55 struct cbor_load_result result = WINPR_C_ARRAY_INIT;
63 cbor_item_t* root = cbor_load(data, length, &result);
64 if (!root || result.error.code != CBOR_ERR_NONE)
66 WLog_ERR(TAG,
"Failed to decode CBOR request, error %u at position %" PRIuz,
67 result.error.code, result.error.position);
71 if (!cbor_isa_map(root))
73 WLog_ERR(TAG,
"CBOR request is not a map");
77 const size_t mapSize = cbor_map_size(root);
78 struct cbor_pair* pairs = cbor_map_handle(root);
80 WLog_DBG(TAG,
"Request CBOR map has %" PRIuz
" entries", mapSize);
82 BOOL keyAlreadyUsed[14] = WINPR_C_ARRAY_INIT;
83 for (
size_t i = 0; i < mapSize; i++)
85 cbor_item_t* key = pairs[i].key;
86 cbor_item_t* value = pairs[i].value;
88 if (!cbor_isa_string(key))
91 const char* keyStr = (
const char*)cbor_string_handle(key);
92 const size_t keyLen = cbor_string_length(key);
94 WLog_DBG(TAG,
" key[%" PRIuz
"]: \"%.*s\" type=%u", i,
95 WINPR_ASSERTING_INT_CAST(
int, keyLen), keyStr, cbor_typeof(value));
100 if (memcmp(keyStr,
"rpId", 4) == 0)
102 if (!check_duplicate(0, keyStr, keyAlreadyUsed,
sizeof(keyAlreadyUsed)))
105 if (cbor_isa_string(value))
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';
116 if (memcmp(keyStr,
"flags", 5) == 0)
118 if (!check_duplicate(1, keyStr, keyAlreadyUsed,
sizeof(keyAlreadyUsed)))
121 if (!cbor_isa_uint(value))
123 WLog_ERR(TAG,
"\"flags\"is not an unsigned int");
126 out->flags = WINPR_ASSERTING_INT_CAST(UINT32, cbor_get_int(value));
130 if (memcmp(keyStr,
"command", 7) == 0)
132 if (!check_duplicate(2, keyStr, keyAlreadyUsed,
sizeof(keyAlreadyUsed)))
135 if (!cbor_isa_uint(value))
137 WLog_ERR(TAG,
"\"command\"is not an unsigned int");
140 out->command = WINPR_ASSERTING_INT_CAST(UINT32, cbor_get_int(value));
142 else if (memcmp(keyStr,
"request", 7) == 0)
144 if (!check_duplicate(3, keyStr, keyAlreadyUsed,
sizeof(keyAlreadyUsed)))
147 if (!cbor_isa_bytestring(value))
149 WLog_ERR(TAG,
"\"request\"is not a byte string");
152 out->requestLen = cbor_bytestring_length(value);
154 out->request = malloc(out->requestLen);
160 memcpy(out->request, cbor_bytestring_handle(value), out->requestLen);
162 else if (memcmp(keyStr,
"timeout", 7) == 0)
164 if (!check_duplicate(4, keyStr, keyAlreadyUsed,
sizeof(keyAlreadyUsed)))
167 if (!cbor_isa_uint(value))
169 WLog_ERR(TAG,
"\"timeout\"is not an unsigned int");
172 out->timeout = WINPR_ASSERTING_INT_CAST(UINT32, cbor_get_int(value));
176 if (memcmp(keyStr,
"transactionId", 13) == 0)
178 if (!check_duplicate(5, keyStr, keyAlreadyUsed,
sizeof(keyAlreadyUsed)))
181 if (!cbor_isa_bytestring(value) || cbor_bytestring_length(value) != 16)
183 WLog_ERR(TAG,
"\"transactionId\"is not a 16-byte byte string");
186 memcpy(out->transactionId, cbor_bytestring_handle(value), 16);
187 out->hasTransactionId = TRUE;
195 if (out->command == 0)
197 WLog_ERR(TAG,
"Missing or invalid \"command\" in request");
206 out->request =
nullptr;
214static BOOL wrap_cbor_map_add(cbor_item_t* root, cbor_item_t* diKey, cbor_item_t* diMap)
216 const bool rc = cbor_map_add(root, (
struct cbor_pair){ .key = diKey, .value = diMap });
222wStream* rdpewa_cbor_encode_webauthn_response(HRESULT hresult, BYTE ctapStatus,
223 const BYTE* ctapData,
size_t ctapLen,
227 cbor_item_t* root =
nullptr;
230 WINPR_ASSERT(devInfo);
233 size_t responseLen = 1 + ctapLen;
234 BYTE* responseData = malloc(responseLen);
238 responseData[0] = ctapStatus;
239 if (ctapData && ctapLen > 0)
240 memcpy(responseData + 1, ctapData, ctapLen);
242 root = cbor_new_definite_map(3);
247 cbor_item_t* diKey = cbor_build_string(
"deviceInfo");
248 cbor_item_t* diMap = cbor_new_definite_map(11);
249 if (!diKey || !diMap)
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"),
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)) }
277 for (
size_t i = 0; i < 11; i++)
279 if (!wrap_cbor_map_add(diMap, pairs[i][0], pairs[i][1]))
283 if (!wrap_cbor_map_add(root, diKey, diMap))
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))
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))
298 cborLen = cbor_serialized_size(root);
303 s = Stream_New(
nullptr, 4 + cborLen);
307 Stream_Write_UINT32(s, (UINT32)hresult);
308 if (cbor_serialize(root, Stream_Pointer(s), cborLen) == 0)
310 Stream_Free(s, TRUE);
314 Stream_Seek(s, cborLen);
323wStream* rdpewa_cbor_encode_simple_response(HRESULT hresult, UINT32 value)
325 wStream* s = Stream_New(
nullptr, 8);
329 Stream_Write_UINT32(s, (UINT32)hresult);
330 Stream_Write_UINT32(s, value);
334wStream* rdpewa_cbor_encode_hresult_response(HRESULT hresult)
336 wStream* s = Stream_New(
nullptr, 4);
340 Stream_Write_UINT32(s, (UINT32)hresult);
Device info for the response map.
Decoded MS-RDPEWA request message.