blob: 63e23ead24e1268fe7aa948f49a7868f1c3436cd [file] [log] [blame]
Jamie Foxab30e712023-03-30 17:48:36 +01001/*
Tamas Band0983e92024-01-25 16:32:51 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Jamie Foxab30e712023-03-30 17:48:36 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_cmd_decode.h"
9
10#include <string.h>
11
12#include "dpe_client.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010013#include "dpe_context_mngr.h"
Maulik Patele6adc112023-08-18 14:21:51 +010014#include "dpe_crypto_config.h"
Jamie Foxab30e712023-03-30 17:48:36 +010015#include "qcbor/qcbor_encode.h"
16#include "qcbor/qcbor_decode.h"
17#include "qcbor/qcbor_spiffy_decode.h"
18
Tamas Band0983e92024-01-25 16:32:51 +010019/*
20 * The goal to reuse the cmd_buf allocated in dpe_req_mngr.c to create the
21 * big objects (certificate, certificate_chain) in place rather then allocate
22 * a separate buffer on the stack and later copy them to cmd_buf.
23 *
24 * The temporary buffer is allocated from the end of the cmd_buf. When the
25 * reply is encoded then the content of the temp buf is moved to its final
26 * place in the cmd_buf.
27 *
28 * Overlapping copy is not an issue because QCBOR relies on memmove under the
29 * hood which handles this scenario.
30 *
31 * Note:
32 * Make sure that the beginning of the encoded reply does not overwrite the
33 * data in the temp buf. That is why the temp buff is allocated at the end of
34 * cmd_buf.
35 */
36#define REUSE_CMD_BUF(size) (uint8_t *)encode_ctx->OutBuf.UB.ptr + \
37 encode_ctx->OutBuf.UB.len - \
38 (size)
39
Jamie Foxab30e712023-03-30 17:48:36 +010040static dpe_error_t decode_dice_inputs(QCBORDecodeContext *decode_ctx,
41 DiceInputValues *input)
42{
43 QCBORError qcbor_err;
44 UsefulBufC out = { NULL, 0 };
45 int64_t out_int;
46
47 /* The DICE inputs are encoded as a map wrapped into a byte string */
48 QCBORDecode_EnterBstrWrappedFromMapN(decode_ctx,
Maulik Patela81605b2023-10-24 12:17:03 +010049 DPE_DERIVE_CONTEXT_INPUT_DATA,
Jamie Foxab30e712023-03-30 17:48:36 +010050 QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
51 QCBORDecode_EnterMap(decode_ctx, NULL);
52
53 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CODE_HASH, &out);
54 if (out.len != sizeof(input->code_hash)) {
55 return DPE_INVALID_COMMAND;
56 }
57 memcpy(input->code_hash, out.ptr, out.len);
58
59 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CODE_DESCRIPTOR, &out);
60 input->code_descriptor = out.ptr;
61 input->code_descriptor_size = out.len;
62
63 QCBORDecode_GetInt64InMapN(decode_ctx, DICE_CONFIG_TYPE, &out_int);
64
65 /* Check error state before interpreting config type */
66 qcbor_err = QCBORDecode_GetError(decode_ctx);
67 if (qcbor_err != QCBOR_SUCCESS) {
68 return DPE_INVALID_COMMAND;
69 }
70
71 if (out_int < kDiceConfigTypeInline ||
72 out_int > kDiceConfigTypeDescriptor) {
73 return DPE_INVALID_COMMAND;
74 }
75 input->config_type = (DiceConfigType)out_int;
76
77 /* Only one of config value or config descriptor needs to be provided */
78 if (input->config_type == kDiceConfigTypeInline) {
79 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CONFIG_VALUE, &out);
80 if (out.len != sizeof(input->config_value)) {
81 return DPE_INVALID_COMMAND;
82 }
83 memcpy(input->config_value, out.ptr, out.len);
84
85 /* Config descriptor is not provided */
86 input->config_descriptor = NULL;
87 input->config_descriptor_size = 0;
88 } else {
89 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CONFIG_DESCRIPTOR,
90 &out);
91 input->config_descriptor = out.ptr;
92 input->config_descriptor_size = out.len;
93
94 /* Config value is not provided */
95 memset(input->config_value, 0, sizeof(input->config_value));
96 }
97
98 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_AUTHORITY_HASH, &out);
99 if (out.len != sizeof(input->authority_hash)) {
100 return DPE_INVALID_COMMAND;
101 }
102 memcpy(input->authority_hash, out.ptr, out.len);
103
104 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_AUTHORITY_DESCRIPTOR,
105 &out);
106 input->authority_descriptor = out.ptr;
107 input->authority_descriptor_size = out.len;
108
109 QCBORDecode_GetInt64InMapN(decode_ctx, DICE_MODE, &out_int);
110 if (out_int < kDiceModeNotInitialized || out_int > kDiceModeMaintenance) {
111 return DPE_INVALID_COMMAND;
112 }
113 input->mode = (DiceMode)out_int;
114
115 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_HIDDEN, &out);
116 if (out.len != sizeof(input->hidden)) {
117 return DPE_INVALID_COMMAND;
118 }
119 memcpy(input->hidden, out.ptr, out.len);
120
121 QCBORDecode_ExitMap(decode_ctx);
122 QCBORDecode_ExitBstrWrapped(decode_ctx);
123
Maulik Pateld936d742024-01-08 14:16:46 +0000124 qcbor_err = QCBORDecode_GetError(decode_ctx);
125 if (qcbor_err != QCBOR_SUCCESS) {
126 return DPE_INVALID_COMMAND;
127 }
128
Jamie Foxab30e712023-03-30 17:48:36 +0100129 return DPE_NO_ERROR;
130}
131
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000132//TODO: Handle the omission of parameters from DPE commands.
Maulik Patela81605b2023-10-24 12:17:03 +0100133static dpe_error_t decode_derive_context(QCBORDecodeContext *decode_ctx,
134 QCBOREncodeContext *encode_ctx,
135 int32_t client_id)
Jamie Foxab30e712023-03-30 17:48:36 +0100136{
137 dpe_error_t dpe_err;
138 QCBORError qcbor_err;
139 UsefulBufC out;
140 int context_handle;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000141 int32_t target_locality;
Jamie Foxab30e712023-03-30 17:48:36 +0100142 bool retain_parent_context;
Maulik Patela81605b2023-10-24 12:17:03 +0100143 bool allow_new_context_to_derive;
Jamie Foxab30e712023-03-30 17:48:36 +0100144 bool create_certificate;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000145 bool return_certificate;
146 bool allow_new_context_to_export;
147 bool export_cdi;
Jamie Foxab30e712023-03-30 17:48:36 +0100148 DiceInputValues dice_inputs;
Maulik Patela81605b2023-10-24 12:17:03 +0100149 int new_context_handle;
Jamie Foxab30e712023-03-30 17:48:36 +0100150 int new_parent_context_handle;
Tamas Band0983e92024-01-25 16:32:51 +0100151 uint8_t *new_certificate_buf = REUSE_CMD_BUF(DICE_CERT_SIZE);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000152 uint8_t exported_cdi_buf[DICE_MAX_ENCODED_CDI_SIZE];
153 size_t new_certificate_actual_size = 0;
154 size_t exported_cdi_actual_size = 0;
Jamie Foxab30e712023-03-30 17:48:36 +0100155
Maulik Patela81605b2023-10-24 12:17:03 +0100156 /* Decode DeriveContext command */
Jamie Foxab30e712023-03-30 17:48:36 +0100157 QCBORDecode_EnterMap(decode_ctx, NULL);
158
Maulik Patela81605b2023-10-24 12:17:03 +0100159 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DERIVE_CONTEXT_CONTEXT_HANDLE,
Jamie Foxab30e712023-03-30 17:48:36 +0100160 &out);
161 if (out.len != sizeof(context_handle)) {
162 return DPE_INVALID_COMMAND;
163 }
164 memcpy(&context_handle, out.ptr, out.len);
165
Maulik Patela81605b2023-10-24 12:17:03 +0100166 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_RETAIN_PARENT_CONTEXT,
Jamie Foxab30e712023-03-30 17:48:36 +0100167 &retain_parent_context);
168
Maulik Patela81605b2023-10-24 12:17:03 +0100169 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_ALLOW_NEW_CONTEXT_TO_DERIVE,
170 &allow_new_context_to_derive);
Jamie Foxab30e712023-03-30 17:48:36 +0100171
Maulik Patela81605b2023-10-24 12:17:03 +0100172 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_CREATE_CERTIFICATE,
Jamie Foxab30e712023-03-30 17:48:36 +0100173 &create_certificate);
174
175 dpe_err = decode_dice_inputs(decode_ctx, &dice_inputs);
176 if (dpe_err != DPE_NO_ERROR) {
177 return dpe_err;
178 }
179
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000180 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DERIVE_CONTEXT_TARGET_LOCALITY,
181 &out);
182 if (out.len != sizeof(target_locality)) {
183 return DPE_INVALID_COMMAND;
184 }
185 memcpy(&target_locality, out.ptr, out.len);
186
187 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_RETURN_CERTIFICATE,
188 &return_certificate);
189
190 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_ALLOW_NEW_CONTEXT_TO_EXPORT,
191 &allow_new_context_to_export);
192
193 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_EXPORT_CDI,
194 &export_cdi);
195
Jamie Foxab30e712023-03-30 17:48:36 +0100196 QCBORDecode_ExitMap(decode_ctx);
197
198 /* Exit top level array */
199 QCBORDecode_ExitArray(decode_ctx);
200
201 /* Finish and check for errors before using decoded values */
202 qcbor_err = QCBORDecode_Finish(decode_ctx);
203 if (qcbor_err != QCBOR_SUCCESS) {
204 return DPE_INVALID_COMMAND;
205 }
206
Maulik Patela81605b2023-10-24 12:17:03 +0100207 dpe_err = derive_context_request(context_handle, retain_parent_context,
208 allow_new_context_to_derive, create_certificate,
209 &dice_inputs, client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000210 target_locality,
211 return_certificate,
212 allow_new_context_to_export,
213 export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100214 &new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000215 &new_parent_context_handle,
216 new_certificate_buf,
Tamas Band0983e92024-01-25 16:32:51 +0100217 DICE_CERT_SIZE,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000218 &new_certificate_actual_size,
219 exported_cdi_buf,
220 sizeof(exported_cdi_buf),
221 &exported_cdi_actual_size);
Jamie Foxab30e712023-03-30 17:48:36 +0100222 if (dpe_err != DPE_NO_ERROR) {
223 return dpe_err;
224 }
225
226 /* Encode response */
227 QCBOREncode_OpenArray(encode_ctx);
228 QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR);
229
230 QCBOREncode_OpenMap(encode_ctx);
Maulik Patela81605b2023-10-24 12:17:03 +0100231 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_NEW_CONTEXT_HANDLE,
232 (UsefulBufC){ &new_context_handle,
233 sizeof(new_context_handle) });
Jamie Foxab30e712023-03-30 17:48:36 +0100234 QCBOREncode_AddBytesToMapN(encode_ctx,
Maulik Patela81605b2023-10-24 12:17:03 +0100235 DPE_DERIVE_CONTEXT_PARENT_CONTEXT_HANDLE,
Jamie Foxab30e712023-03-30 17:48:36 +0100236 (UsefulBufC){ &new_parent_context_handle,
237 sizeof(new_parent_context_handle) });
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000238
239 /* The certificate is already encoded into a CBOR array by the function
240 * add_encoded_layer_certificate. Add it as a byte string so that its
241 * decoding can be skipped and the CBOR returned to the caller.
242 */
243 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_NEW_CERTIFICATE,
244 (UsefulBufC){ new_certificate_buf,
245 new_certificate_actual_size });
246
247 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_EXPORTED_CDI,
248 (UsefulBufC){ exported_cdi_buf,
249 exported_cdi_actual_size });
250
Jamie Foxab30e712023-03-30 17:48:36 +0100251 QCBOREncode_CloseMap(encode_ctx);
252
253 QCBOREncode_CloseArray(encode_ctx);
254
255 return DPE_NO_ERROR;
256}
257
Maulik Patel54d65f72023-06-28 13:04:36 +0100258static dpe_error_t decode_destroy_context(QCBORDecodeContext *decode_ctx,
259 QCBOREncodeContext *encode_ctx)
260{
261 dpe_error_t dpe_err;
262 QCBORError qcbor_err;
263 UsefulBufC out;
264 int context_handle;
265 bool destroy_recursively;
266
267 /* Decode Destroy context command */
268 QCBORDecode_EnterMap(decode_ctx, NULL);
269
270 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DESTROY_CONTEXT_HANDLE,
271 &out);
272 if (out.len != sizeof(context_handle)) {
273 return DPE_INVALID_COMMAND;
274 }
275 memcpy(&context_handle, out.ptr, out.len);
276
277 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DESTROY_CONTEXT_RECURSIVELY,
278 &destroy_recursively);
279
280 QCBORDecode_ExitMap(decode_ctx);
281
282 /* Exit top level array */
283 QCBORDecode_ExitArray(decode_ctx);
284
285 /* Finish and check for errors before using decoded values */
286 qcbor_err = QCBORDecode_Finish(decode_ctx);
287 if (qcbor_err != QCBOR_SUCCESS) {
288 return DPE_INVALID_COMMAND;
289 }
290
291 dpe_err = destroy_context_request(context_handle, destroy_recursively);
292 if (dpe_err != DPE_NO_ERROR) {
293 return dpe_err;
294 }
295
296 /* Encode response */
297 QCBOREncode_OpenArray(encode_ctx);
298 QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR);
299 QCBOREncode_CloseArray(encode_ctx);
300
301 return DPE_NO_ERROR;
302}
303
Jamie Foxab30e712023-03-30 17:48:36 +0100304static dpe_error_t decode_certify_key(QCBORDecodeContext *decode_ctx,
305 QCBOREncodeContext *encode_ctx)
306{
307 QCBORError qcbor_err;
308 UsefulBufC out;
309 dpe_error_t dpe_err;
310 int context_handle;
311 bool retain_context;
312 const uint8_t *public_key;
313 size_t public_key_size;
314 const uint8_t *label;
315 size_t label_size;
Tamas Band0983e92024-01-25 16:32:51 +0100316 uint8_t *certificate_buf = REUSE_CMD_BUF(DICE_CERT_SIZE);
Maulik Patelcbded682023-12-07 11:50:16 +0000317 size_t certificate_actual_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100318 uint8_t derived_public_key_buf[DPE_ATTEST_PUB_KEY_SIZE];
Jamie Foxab30e712023-03-30 17:48:36 +0100319 size_t derived_public_key_actual_size;
320 int new_context_handle;
321
322 /* Decode CertifyKey command */
323 QCBORDecode_EnterMap(decode_ctx, NULL);
324
325 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_CONTEXT_HANDLE,
326 &out);
327 if (out.len != sizeof(context_handle)) {
328 return DPE_INVALID_COMMAND;
329 }
330 memcpy(&context_handle, out.ptr, out.len);
331
332 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_CERTIFY_KEY_RETAIN_CONTEXT,
333 &retain_context);
334
335 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_PUBLIC_KEY,
336 &out);
337 public_key = out.ptr;
338 public_key_size = out.len;
339
340 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_LABEL, &out);
341 label = out.ptr;
342 label_size = out.len;
343
344 QCBORDecode_ExitMap(decode_ctx);
345
346 /* Exit top level array */
347 QCBORDecode_ExitArray(decode_ctx);
348
349 /* Finish and check for errors before using decoded values */
350 qcbor_err = QCBORDecode_Finish(decode_ctx);
351 if (qcbor_err != QCBOR_SUCCESS) {
352 return DPE_INVALID_COMMAND;
353 }
354
Maulik Patele6adc112023-08-18 14:21:51 +0100355 dpe_err = certify_key_request(context_handle, retain_context, public_key,
356 public_key_size, label, label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000357 certificate_buf,
Tamas Band0983e92024-01-25 16:32:51 +0100358 DICE_CERT_SIZE,
Maulik Patelcbded682023-12-07 11:50:16 +0000359 &certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100360 derived_public_key_buf,
361 sizeof(derived_public_key_buf),
362 &derived_public_key_actual_size,
363 &new_context_handle);
Jamie Foxab30e712023-03-30 17:48:36 +0100364 if (dpe_err != DPE_NO_ERROR) {
365 return dpe_err;
366 }
367
368 /* Encode response */
369 QCBOREncode_OpenArray(encode_ctx);
370 QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR);
371
372 QCBOREncode_OpenMap(encode_ctx);
373
374 /* The certificate chain is already encoded into a CBOR array by the certify
375 * key implementation. Add it as a byte string so that its decoding can be
376 * skipped and the CBOR returned to the caller.
377 */
Maulik Patelcbded682023-12-07 11:50:16 +0000378 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_CERTIFICATE,
379 (UsefulBufC){ certificate_buf,
380 certificate_actual_size });
Jamie Foxab30e712023-03-30 17:48:36 +0100381
382 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_DERIVED_PUBLIC_KEY,
383 (UsefulBufC){ derived_public_key_buf,
384 derived_public_key_actual_size });
385 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_NEW_CONTEXT_HANDLE,
386 (UsefulBufC){ &new_context_handle,
387 sizeof(new_context_handle) });
388
389 QCBOREncode_CloseMap(encode_ctx);
390
391 QCBOREncode_CloseArray(encode_ctx);
392
393 return DPE_NO_ERROR;
394}
395
Maulik Patel83a6b592023-12-05 15:20:30 +0000396static dpe_error_t decode_get_certificate_chain(QCBORDecodeContext *decode_ctx,
397 QCBOREncodeContext *encode_ctx)
398{
399 QCBORError qcbor_err;
400 UsefulBufC out;
401 dpe_error_t dpe_err;
402 int context_handle;
403 bool retain_context;
404 bool clear_from_context;
Tamas Band0983e92024-01-25 16:32:51 +0100405 uint8_t *certificate_chain_buf = REUSE_CMD_BUF(DICE_CERT_CHAIN_SIZE);
Maulik Patel83a6b592023-12-05 15:20:30 +0000406 size_t certificate_chain_actual_size;
407 int new_context_handle;
408
409 /* Decode GetCertificateChain command */
410 QCBORDecode_EnterMap(decode_ctx, NULL);
411
412 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_GET_CERTIFICATE_CHAIN_CONTEXT_HANDLE,
413 &out);
414 if (out.len != sizeof(context_handle)) {
415 return DPE_INVALID_COMMAND;
416 }
417 memcpy(&context_handle, out.ptr, out.len);
418
419 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_GET_CERTIFICATE_CHAIN_RETAIN_CONTEXT,
420 &retain_context);
421
422 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_GET_CERTIFICATE_CHAIN_CLEAR_FROM_CONTEXT,
423 &clear_from_context);
424
425 QCBORDecode_ExitMap(decode_ctx);
426
427 /* Exit top level array */
428 QCBORDecode_ExitArray(decode_ctx);
429
430 /* Finish and check for errors before using decoded values */
431 qcbor_err = QCBORDecode_Finish(decode_ctx);
432 if (qcbor_err != QCBOR_SUCCESS) {
433 return DPE_INVALID_COMMAND;
434 }
435
436 dpe_err = get_certificate_chain_request(context_handle,
437 retain_context,
438 clear_from_context,
439 certificate_chain_buf,
Tamas Band0983e92024-01-25 16:32:51 +0100440 DICE_CERT_CHAIN_SIZE,
Maulik Patel83a6b592023-12-05 15:20:30 +0000441 &certificate_chain_actual_size,
442 &new_context_handle);
443 if (dpe_err != DPE_NO_ERROR) {
444 return dpe_err;
445 }
446
447 /* Encode response */
448 QCBOREncode_OpenArray(encode_ctx);
449 QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR);
450
451 QCBOREncode_OpenMap(encode_ctx);
452
453 /* The certificate chain is already encoded into a CBOR array by the get certificate
454 * chain implementation. Add it as a byte string so that its decoding can be
455 * skipped and the CBOR returned to the caller.
456 */
457 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_GET_CERTIFICATE_CHAIN_CERTIFICATE_CHAIN,
458 (UsefulBufC){ certificate_chain_buf,
459 certificate_chain_actual_size });
460
461 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_GET_CERTIFICATE_CHAIN_NEW_CONTEXT_HANDLE,
462 (UsefulBufC){ &new_context_handle,
463 sizeof(new_context_handle) });
464
465 QCBOREncode_CloseMap(encode_ctx);
466
467 QCBOREncode_CloseArray(encode_ctx);
468
469 return DPE_NO_ERROR;
470}
471
Jamie Foxab30e712023-03-30 17:48:36 +0100472static void encode_error_only(QCBOREncodeContext *encode_ctx,
473 dpe_error_t dpe_err)
474{
475 QCBOREncode_OpenArray(encode_ctx);
476 QCBOREncode_AddInt64(encode_ctx, dpe_err);
477 QCBOREncode_CloseArray(encode_ctx);
478}
479
480int32_t dpe_command_decode(int32_t client_id,
481 const char *cmd_input, size_t cmd_input_size,
482 char *cmd_output, size_t *cmd_output_size)
483{
484 dpe_error_t dpe_err;
485 QCBORError qcbor_err;
486 QCBORDecodeContext decode_ctx;
487 QCBOREncodeContext encode_ctx;
488 UsefulBufC out;
489 uint64_t command_id;
490
491 QCBORDecode_Init(&decode_ctx, (UsefulBufC){ cmd_input, cmd_input_size },
492 QCBOR_DECODE_MODE_NORMAL);
493 QCBOREncode_Init(&encode_ctx, (UsefulBuf){ cmd_output, *cmd_output_size });
494
495 /* Enter top level array */
496 QCBORDecode_EnterArray(&decode_ctx, NULL);
497
498 /* Get the command ID */
499 QCBORDecode_GetUInt64(&decode_ctx, &command_id);
500
501 /* Check for errors before interpreting the decoded command ID */
502 qcbor_err = QCBORDecode_GetError(&decode_ctx);
503
504 if (qcbor_err == QCBOR_SUCCESS) {
505 switch (command_id) {
Maulik Patela81605b2023-10-24 12:17:03 +0100506 case DPE_DERIVE_CONTEXT:
507 dpe_err = decode_derive_context(&decode_ctx, &encode_ctx, client_id);
Jamie Foxab30e712023-03-30 17:48:36 +0100508 break;
509 case DPE_CERTIFY_KEY:
510 dpe_err = decode_certify_key(&decode_ctx, &encode_ctx);
511 break;
Maulik Patel83a6b592023-12-05 15:20:30 +0000512 case DPE_GET_CERTIFICATE_CHAIN:
513 dpe_err = decode_get_certificate_chain(&decode_ctx, &encode_ctx);
514 break;
Maulik Patel54d65f72023-06-28 13:04:36 +0100515 case DPE_DESTROY_CONTEXT:
516 dpe_err = decode_destroy_context(&decode_ctx, &encode_ctx);
517 break;
Jamie Foxab30e712023-03-30 17:48:36 +0100518 default:
519 dpe_err = DPE_INVALID_COMMAND;
520 break;
521 }
522 } else {
523 dpe_err = DPE_INVALID_COMMAND;
524 }
525
526 /* If an unhandled DPE error was returned, then encode it into a response */
527 if (dpe_err != DPE_NO_ERROR) {
528 encode_error_only(&encode_ctx, dpe_err);
529 }
530
531 qcbor_err = QCBOREncode_Finish(&encode_ctx, &out);
532 if (qcbor_err != QCBOR_SUCCESS) {
533 return -1;
534 }
535
536 *cmd_output_size = out.len;
537
538 return 0;
539}