Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 1 | /* |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2023-2024, Arm Limited. All rights reserved. |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 3 | * |
| 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 Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 13 | #include "dpe_context_mngr.h" |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 14 | #include "dpe_crypto_config.h" |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 15 | #include "qcbor/qcbor_encode.h" |
| 16 | #include "qcbor/qcbor_decode.h" |
| 17 | #include "qcbor/qcbor_spiffy_decode.h" |
| 18 | |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 19 | #define COUNT_ARGS(arg) (arg)++ |
| 20 | |
| 21 | #define CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx) \ |
| 22 | qcbor_err = QCBORDecode_GetAndResetError(decode_ctx); \ |
| 23 | if (qcbor_err == QCBOR_SUCCESS) { \ |
| 24 | /* Valid label found - optional argument present */ \ |
| 25 | COUNT_ARGS(num_of_valid_arguments); \ |
| 26 | } else if (qcbor_err != QCBOR_ERR_LABEL_NOT_FOUND) { \ |
| 27 | return DPE_INVALID_COMMAND; \ |
| 28 | } else { \ |
| 29 | /* We have NOT found the optional argument, do not update the count */ \ |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 32 | /* |
| 33 | * The goal to reuse the cmd_buf allocated in dpe_req_mngr.c to create the |
| 34 | * big objects (certificate, certificate_chain) in place rather then allocate |
| 35 | * a separate buffer on the stack and later copy them to cmd_buf. |
| 36 | * |
| 37 | * The temporary buffer is allocated from the end of the cmd_buf. When the |
| 38 | * reply is encoded then the content of the temp buf is moved to its final |
| 39 | * place in the cmd_buf. |
| 40 | * |
| 41 | * Overlapping copy is not an issue because QCBOR relies on memmove under the |
| 42 | * hood which handles this scenario. |
| 43 | * |
| 44 | * Note: |
| 45 | * Make sure that the beginning of the encoded reply does not overwrite the |
| 46 | * data in the temp buf. That is why the temp buff is allocated at the end of |
| 47 | * cmd_buf. |
| 48 | */ |
| 49 | #define REUSE_CMD_BUF(size) (uint8_t *)encode_ctx->OutBuf.UB.ptr + \ |
| 50 | encode_ctx->OutBuf.UB.len - \ |
| 51 | (size) |
| 52 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 53 | static dpe_error_t decode_dice_inputs(QCBORDecodeContext *decode_ctx, |
| 54 | DiceInputValues *input) |
| 55 | { |
| 56 | QCBORError qcbor_err; |
| 57 | UsefulBufC out = { NULL, 0 }; |
| 58 | int64_t out_int; |
| 59 | |
| 60 | /* The DICE inputs are encoded as a map wrapped into a byte string */ |
| 61 | QCBORDecode_EnterBstrWrappedFromMapN(decode_ctx, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 62 | DPE_DERIVE_CONTEXT_INPUT_DATA, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 63 | QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); |
| 64 | QCBORDecode_EnterMap(decode_ctx, NULL); |
| 65 | |
| 66 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CODE_HASH, &out); |
| 67 | if (out.len != sizeof(input->code_hash)) { |
| 68 | return DPE_INVALID_COMMAND; |
| 69 | } |
| 70 | memcpy(input->code_hash, out.ptr, out.len); |
| 71 | |
| 72 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CODE_DESCRIPTOR, &out); |
| 73 | input->code_descriptor = out.ptr; |
| 74 | input->code_descriptor_size = out.len; |
| 75 | |
| 76 | QCBORDecode_GetInt64InMapN(decode_ctx, DICE_CONFIG_TYPE, &out_int); |
| 77 | |
| 78 | /* Check error state before interpreting config type */ |
| 79 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
| 80 | if (qcbor_err != QCBOR_SUCCESS) { |
| 81 | return DPE_INVALID_COMMAND; |
| 82 | } |
| 83 | |
| 84 | if (out_int < kDiceConfigTypeInline || |
| 85 | out_int > kDiceConfigTypeDescriptor) { |
| 86 | return DPE_INVALID_COMMAND; |
| 87 | } |
| 88 | input->config_type = (DiceConfigType)out_int; |
| 89 | |
| 90 | /* Only one of config value or config descriptor needs to be provided */ |
| 91 | if (input->config_type == kDiceConfigTypeInline) { |
| 92 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CONFIG_VALUE, &out); |
| 93 | if (out.len != sizeof(input->config_value)) { |
| 94 | return DPE_INVALID_COMMAND; |
| 95 | } |
| 96 | memcpy(input->config_value, out.ptr, out.len); |
| 97 | |
| 98 | /* Config descriptor is not provided */ |
| 99 | input->config_descriptor = NULL; |
| 100 | input->config_descriptor_size = 0; |
| 101 | } else { |
| 102 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CONFIG_DESCRIPTOR, |
| 103 | &out); |
| 104 | input->config_descriptor = out.ptr; |
| 105 | input->config_descriptor_size = out.len; |
| 106 | |
| 107 | /* Config value is not provided */ |
| 108 | memset(input->config_value, 0, sizeof(input->config_value)); |
| 109 | } |
| 110 | |
| 111 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_AUTHORITY_HASH, &out); |
| 112 | if (out.len != sizeof(input->authority_hash)) { |
| 113 | return DPE_INVALID_COMMAND; |
| 114 | } |
| 115 | memcpy(input->authority_hash, out.ptr, out.len); |
| 116 | |
| 117 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_AUTHORITY_DESCRIPTOR, |
| 118 | &out); |
| 119 | input->authority_descriptor = out.ptr; |
| 120 | input->authority_descriptor_size = out.len; |
| 121 | |
| 122 | QCBORDecode_GetInt64InMapN(decode_ctx, DICE_MODE, &out_int); |
| 123 | if (out_int < kDiceModeNotInitialized || out_int > kDiceModeMaintenance) { |
| 124 | return DPE_INVALID_COMMAND; |
| 125 | } |
| 126 | input->mode = (DiceMode)out_int; |
| 127 | |
| 128 | QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_HIDDEN, &out); |
| 129 | if (out.len != sizeof(input->hidden)) { |
| 130 | return DPE_INVALID_COMMAND; |
| 131 | } |
| 132 | memcpy(input->hidden, out.ptr, out.len); |
| 133 | |
| 134 | QCBORDecode_ExitMap(decode_ctx); |
| 135 | QCBORDecode_ExitBstrWrapped(decode_ctx); |
| 136 | |
Maulik Patel | d936d74 | 2024-01-08 14:16:46 +0000 | [diff] [blame] | 137 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
| 138 | if (qcbor_err != QCBOR_SUCCESS) { |
| 139 | return DPE_INVALID_COMMAND; |
| 140 | } |
| 141 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 142 | return DPE_NO_ERROR; |
| 143 | } |
| 144 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 145 | static dpe_error_t decode_derive_context(QCBORDecodeContext *decode_ctx, |
| 146 | QCBOREncodeContext *encode_ctx, |
| 147 | int32_t client_id) |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 148 | { |
| 149 | dpe_error_t dpe_err; |
| 150 | QCBORError qcbor_err; |
| 151 | UsefulBufC out; |
| 152 | int context_handle; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 153 | int32_t target_locality; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 154 | bool retain_parent_context; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 155 | bool allow_new_context_to_derive; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 156 | bool create_certificate; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 157 | bool return_certificate; |
| 158 | bool allow_new_context_to_export; |
| 159 | bool export_cdi; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 160 | DiceInputValues dice_inputs; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 161 | int new_context_handle; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 162 | int new_parent_context_handle; |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 163 | uint8_t *new_certificate_buf = REUSE_CMD_BUF(DICE_CERT_SIZE); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 164 | uint8_t exported_cdi_buf[DICE_MAX_ENCODED_CDI_SIZE]; |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 165 | uint32_t cert_id; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 166 | size_t new_certificate_actual_size = 0; |
| 167 | size_t exported_cdi_actual_size = 0; |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 168 | QCBORItem item; |
| 169 | uint16_t num_of_input_arguments, num_of_valid_arguments = 0; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 170 | |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 171 | /* Initialise optional parameters with their default value in case |
| 172 | * they are not encoded in the input command |
| 173 | */ |
| 174 | cert_id = DPE_CERT_ID_INVALID; |
| 175 | retain_parent_context = false; |
| 176 | allow_new_context_to_derive = true; |
| 177 | create_certificate = true; |
| 178 | return_certificate = false; |
| 179 | allow_new_context_to_export = false; |
| 180 | export_cdi = false; |
| 181 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 182 | /* Decode DeriveContext command */ |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 183 | QCBORDecode_EnterMap(decode_ctx, &item); |
| 184 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
| 185 | if ((qcbor_err != QCBOR_SUCCESS) || |
| 186 | (item.uDataType != QCBOR_TYPE_MAP)) { |
| 187 | /* We expect a map of Derive Context command arguments here */ |
| 188 | return DPE_INVALID_COMMAND; |
| 189 | } |
| 190 | /* Save the number of items found in the map */ |
| 191 | num_of_input_arguments = item.val.uCount; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 192 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 193 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DERIVE_CONTEXT_CONTEXT_HANDLE, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 194 | &out); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 195 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 196 | if ((qcbor_err != QCBOR_SUCCESS) || (out.len != sizeof(context_handle))) { |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 197 | return DPE_INVALID_COMMAND; |
| 198 | } |
| 199 | memcpy(&context_handle, out.ptr, out.len); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 200 | COUNT_ARGS(num_of_valid_arguments); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 201 | |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 202 | QCBORDecode_GetUInt64InMapN(decode_ctx, DPE_DERIVE_CONTEXT_CERT_ID, &cert_id); |
| 203 | /* Check if cert_id was encoded in the received command buffer */ |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 204 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 205 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 206 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_RETAIN_PARENT_CONTEXT, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 207 | &retain_parent_context); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 208 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 209 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 210 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_ALLOW_NEW_CONTEXT_TO_DERIVE, |
| 211 | &allow_new_context_to_derive); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 212 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 213 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 214 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_CREATE_CERTIFICATE, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 215 | &create_certificate); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 216 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 217 | |
| 218 | dpe_err = decode_dice_inputs(decode_ctx, &dice_inputs); |
| 219 | if (dpe_err != DPE_NO_ERROR) { |
| 220 | return dpe_err; |
| 221 | } |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 222 | COUNT_ARGS(num_of_valid_arguments); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 223 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 224 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DERIVE_CONTEXT_TARGET_LOCALITY, |
| 225 | &out); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 226 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
| 227 | if (qcbor_err == QCBOR_SUCCESS) { |
| 228 | /* Valid argument was found */ |
| 229 | if (out.len != sizeof(target_locality)) { |
| 230 | return DPE_INVALID_ARGUMENT; |
| 231 | } |
| 232 | memcpy(&target_locality, out.ptr, out.len); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 233 | } |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 234 | |
| 235 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_RETURN_CERTIFICATE, |
| 236 | &return_certificate); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 237 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 238 | |
| 239 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_ALLOW_NEW_CONTEXT_TO_EXPORT, |
| 240 | &allow_new_context_to_export); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 241 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 242 | |
| 243 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_EXPORT_CDI, |
| 244 | &export_cdi); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 245 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 246 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 247 | QCBORDecode_ExitMap(decode_ctx); |
| 248 | |
| 249 | /* Exit top level array */ |
| 250 | QCBORDecode_ExitArray(decode_ctx); |
| 251 | |
| 252 | /* Finish and check for errors before using decoded values */ |
| 253 | qcbor_err = QCBORDecode_Finish(decode_ctx); |
| 254 | if (qcbor_err != QCBOR_SUCCESS) { |
| 255 | return DPE_INVALID_COMMAND; |
| 256 | } |
| 257 | |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 258 | if (num_of_input_arguments > num_of_valid_arguments) { |
| 259 | /* Extra unsupported arguments encoded in command map */ |
| 260 | return DPE_INVALID_ARGUMENT; |
| 261 | } |
| 262 | |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 263 | dpe_err = derive_context_request(context_handle, cert_id, retain_parent_context, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 264 | allow_new_context_to_derive, create_certificate, |
| 265 | &dice_inputs, client_id, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 266 | target_locality, |
| 267 | return_certificate, |
| 268 | allow_new_context_to_export, |
| 269 | export_cdi, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 270 | &new_context_handle, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 271 | &new_parent_context_handle, |
| 272 | new_certificate_buf, |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 273 | DICE_CERT_SIZE, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 274 | &new_certificate_actual_size, |
| 275 | exported_cdi_buf, |
| 276 | sizeof(exported_cdi_buf), |
| 277 | &exported_cdi_actual_size); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 278 | if (dpe_err != DPE_NO_ERROR) { |
| 279 | return dpe_err; |
| 280 | } |
| 281 | |
| 282 | /* Encode response */ |
| 283 | QCBOREncode_OpenArray(encode_ctx); |
| 284 | QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR); |
| 285 | |
| 286 | QCBOREncode_OpenMap(encode_ctx); |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 287 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_NEW_CONTEXT_HANDLE, |
| 288 | (UsefulBufC){ &new_context_handle, |
| 289 | sizeof(new_context_handle) }); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 290 | QCBOREncode_AddBytesToMapN(encode_ctx, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 291 | DPE_DERIVE_CONTEXT_PARENT_CONTEXT_HANDLE, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 292 | (UsefulBufC){ &new_parent_context_handle, |
| 293 | sizeof(new_parent_context_handle) }); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 294 | |
| 295 | /* The certificate is already encoded into a CBOR array by the function |
| 296 | * add_encoded_layer_certificate. Add it as a byte string so that its |
| 297 | * decoding can be skipped and the CBOR returned to the caller. |
| 298 | */ |
| 299 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_NEW_CERTIFICATE, |
| 300 | (UsefulBufC){ new_certificate_buf, |
| 301 | new_certificate_actual_size }); |
| 302 | |
| 303 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_EXPORTED_CDI, |
| 304 | (UsefulBufC){ exported_cdi_buf, |
| 305 | exported_cdi_actual_size }); |
| 306 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 307 | QCBOREncode_CloseMap(encode_ctx); |
| 308 | |
| 309 | QCBOREncode_CloseArray(encode_ctx); |
| 310 | |
| 311 | return DPE_NO_ERROR; |
| 312 | } |
| 313 | |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 314 | static dpe_error_t decode_destroy_context(QCBORDecodeContext *decode_ctx, |
| 315 | QCBOREncodeContext *encode_ctx) |
| 316 | { |
| 317 | dpe_error_t dpe_err; |
| 318 | QCBORError qcbor_err; |
| 319 | UsefulBufC out; |
| 320 | int context_handle; |
| 321 | bool destroy_recursively; |
| 322 | |
| 323 | /* Decode Destroy context command */ |
| 324 | QCBORDecode_EnterMap(decode_ctx, NULL); |
| 325 | |
| 326 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DESTROY_CONTEXT_HANDLE, |
| 327 | &out); |
| 328 | if (out.len != sizeof(context_handle)) { |
| 329 | return DPE_INVALID_COMMAND; |
| 330 | } |
| 331 | memcpy(&context_handle, out.ptr, out.len); |
| 332 | |
| 333 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DESTROY_CONTEXT_RECURSIVELY, |
| 334 | &destroy_recursively); |
| 335 | |
| 336 | QCBORDecode_ExitMap(decode_ctx); |
| 337 | |
| 338 | /* Exit top level array */ |
| 339 | QCBORDecode_ExitArray(decode_ctx); |
| 340 | |
| 341 | /* Finish and check for errors before using decoded values */ |
| 342 | qcbor_err = QCBORDecode_Finish(decode_ctx); |
| 343 | if (qcbor_err != QCBOR_SUCCESS) { |
| 344 | return DPE_INVALID_COMMAND; |
| 345 | } |
| 346 | |
| 347 | dpe_err = destroy_context_request(context_handle, destroy_recursively); |
| 348 | if (dpe_err != DPE_NO_ERROR) { |
| 349 | return dpe_err; |
| 350 | } |
| 351 | |
| 352 | /* Encode response */ |
| 353 | QCBOREncode_OpenArray(encode_ctx); |
| 354 | QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR); |
| 355 | QCBOREncode_CloseArray(encode_ctx); |
| 356 | |
| 357 | return DPE_NO_ERROR; |
| 358 | } |
| 359 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 360 | static dpe_error_t decode_certify_key(QCBORDecodeContext *decode_ctx, |
| 361 | QCBOREncodeContext *encode_ctx) |
| 362 | { |
| 363 | QCBORError qcbor_err; |
| 364 | UsefulBufC out; |
| 365 | dpe_error_t dpe_err; |
| 366 | int context_handle; |
| 367 | bool retain_context; |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 368 | const uint8_t *public_key = NULL; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 369 | size_t public_key_size; |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 370 | const uint8_t *label = NULL; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 371 | size_t label_size; |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 372 | uint8_t *certificate_buf = REUSE_CMD_BUF(DICE_CERT_SIZE); |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 373 | size_t certificate_actual_size; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 374 | uint8_t derived_public_key_buf[DPE_ATTEST_PUB_KEY_SIZE]; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 375 | size_t derived_public_key_actual_size; |
| 376 | int new_context_handle; |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 377 | QCBORItem item; |
| 378 | uint16_t num_of_input_arguments, num_of_valid_arguments = 0; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 379 | |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 380 | /* Initialise optional parameters with their default value in case |
| 381 | * they are not encoded in the input command |
| 382 | */ |
| 383 | retain_context = false; |
| 384 | public_key_size = 0; |
| 385 | label_size = 0; |
| 386 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 387 | /* Decode CertifyKey command */ |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 388 | QCBORDecode_EnterMap(decode_ctx, &item); |
| 389 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
| 390 | if ((qcbor_err != QCBOR_SUCCESS) || |
| 391 | (item.uDataType != QCBOR_TYPE_MAP)) { |
| 392 | /* We expect a map of Certify Key command arguments here */ |
| 393 | return DPE_INVALID_COMMAND; |
| 394 | } |
| 395 | /* Save the number of items found in the map */ |
| 396 | num_of_input_arguments = item.val.uCount; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 397 | |
| 398 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_CONTEXT_HANDLE, |
| 399 | &out); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 400 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 401 | if ((qcbor_err != QCBOR_SUCCESS) || (out.len != sizeof(context_handle))) { |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 402 | return DPE_INVALID_COMMAND; |
| 403 | } |
| 404 | memcpy(&context_handle, out.ptr, out.len); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 405 | COUNT_ARGS(num_of_valid_arguments); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 406 | |
| 407 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_CERTIFY_KEY_RETAIN_CONTEXT, |
| 408 | &retain_context); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 409 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 410 | |
| 411 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_PUBLIC_KEY, |
| 412 | &out); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 413 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 414 | if (qcbor_err == QCBOR_SUCCESS) { |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 415 | /* Valid argument was found */ |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 416 | public_key = out.ptr; |
| 417 | public_key_size = out.len; |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 418 | } |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 419 | |
| 420 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_LABEL, &out); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 421 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 422 | if (qcbor_err == QCBOR_SUCCESS) { |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 423 | /* Valid argument was found */ |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 424 | label = out.ptr; |
| 425 | label_size = out.len; |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 426 | } |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 427 | |
| 428 | QCBORDecode_ExitMap(decode_ctx); |
| 429 | |
| 430 | /* Exit top level array */ |
| 431 | QCBORDecode_ExitArray(decode_ctx); |
| 432 | |
| 433 | /* Finish and check for errors before using decoded values */ |
| 434 | qcbor_err = QCBORDecode_Finish(decode_ctx); |
| 435 | if (qcbor_err != QCBOR_SUCCESS) { |
| 436 | return DPE_INVALID_COMMAND; |
| 437 | } |
| 438 | |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 439 | if (num_of_input_arguments > num_of_valid_arguments) { |
| 440 | /* Extra unsupported arguments encoded in command map */ |
| 441 | return DPE_INVALID_ARGUMENT; |
| 442 | } |
| 443 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 444 | dpe_err = certify_key_request(context_handle, retain_context, public_key, |
| 445 | public_key_size, label, label_size, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 446 | certificate_buf, |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 447 | DICE_CERT_SIZE, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 448 | &certificate_actual_size, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 449 | derived_public_key_buf, |
| 450 | sizeof(derived_public_key_buf), |
| 451 | &derived_public_key_actual_size, |
| 452 | &new_context_handle); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 453 | if (dpe_err != DPE_NO_ERROR) { |
| 454 | return dpe_err; |
| 455 | } |
| 456 | |
| 457 | /* Encode response */ |
| 458 | QCBOREncode_OpenArray(encode_ctx); |
| 459 | QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR); |
| 460 | |
| 461 | QCBOREncode_OpenMap(encode_ctx); |
| 462 | |
| 463 | /* The certificate chain is already encoded into a CBOR array by the certify |
| 464 | * key implementation. Add it as a byte string so that its decoding can be |
| 465 | * skipped and the CBOR returned to the caller. |
| 466 | */ |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 467 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_CERTIFICATE, |
| 468 | (UsefulBufC){ certificate_buf, |
| 469 | certificate_actual_size }); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 470 | |
| 471 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_DERIVED_PUBLIC_KEY, |
| 472 | (UsefulBufC){ derived_public_key_buf, |
| 473 | derived_public_key_actual_size }); |
| 474 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_NEW_CONTEXT_HANDLE, |
| 475 | (UsefulBufC){ &new_context_handle, |
| 476 | sizeof(new_context_handle) }); |
| 477 | |
| 478 | QCBOREncode_CloseMap(encode_ctx); |
| 479 | |
| 480 | QCBOREncode_CloseArray(encode_ctx); |
| 481 | |
| 482 | return DPE_NO_ERROR; |
| 483 | } |
| 484 | |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 485 | static dpe_error_t decode_get_certificate_chain(QCBORDecodeContext *decode_ctx, |
| 486 | QCBOREncodeContext *encode_ctx) |
| 487 | { |
| 488 | QCBORError qcbor_err; |
| 489 | UsefulBufC out; |
| 490 | dpe_error_t dpe_err; |
| 491 | int context_handle; |
| 492 | bool retain_context; |
| 493 | bool clear_from_context; |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 494 | uint8_t *certificate_chain_buf = REUSE_CMD_BUF(DICE_CERT_CHAIN_SIZE); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 495 | size_t certificate_chain_actual_size; |
| 496 | int new_context_handle; |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 497 | QCBORItem item; |
| 498 | uint16_t num_of_input_arguments, num_of_valid_arguments = 0; |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 499 | |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 500 | /* Initialise optional parameters with their default value in case |
| 501 | * they are not encoded in the input command |
| 502 | */ |
| 503 | retain_context = false; |
| 504 | clear_from_context = false; |
| 505 | |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 506 | /* Decode GetCertificateChain command */ |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 507 | QCBORDecode_EnterMap(decode_ctx, &item); |
| 508 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
| 509 | if ((qcbor_err != QCBOR_SUCCESS) || |
| 510 | (item.uDataType != QCBOR_TYPE_MAP)) { |
| 511 | /* We expect a map of Get Certificate Chain command arguments here */ |
| 512 | return DPE_INVALID_COMMAND; |
| 513 | } |
| 514 | /* Save the number of items found in the map */ |
| 515 | num_of_input_arguments = item.val.uCount; |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 516 | |
| 517 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_GET_CERTIFICATE_CHAIN_CONTEXT_HANDLE, |
| 518 | &out); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 519 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
Maulik Patel | fbe9bff | 2024-02-09 14:05:25 +0000 | [diff] [blame] | 520 | if ((qcbor_err != QCBOR_SUCCESS) || (out.len != sizeof(context_handle))) { |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 521 | return DPE_INVALID_COMMAND; |
| 522 | } |
| 523 | memcpy(&context_handle, out.ptr, out.len); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 524 | COUNT_ARGS(num_of_valid_arguments); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 525 | |
| 526 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_GET_CERTIFICATE_CHAIN_RETAIN_CONTEXT, |
| 527 | &retain_context); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 528 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 529 | |
| 530 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_GET_CERTIFICATE_CHAIN_CLEAR_FROM_CONTEXT, |
| 531 | &clear_from_context); |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 532 | CHECK_AND_COUNT_OPTIONAL_ARGUMENT(decode_ctx); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 533 | |
| 534 | QCBORDecode_ExitMap(decode_ctx); |
| 535 | |
| 536 | /* Exit top level array */ |
| 537 | QCBORDecode_ExitArray(decode_ctx); |
| 538 | |
| 539 | /* Finish and check for errors before using decoded values */ |
| 540 | qcbor_err = QCBORDecode_Finish(decode_ctx); |
| 541 | if (qcbor_err != QCBOR_SUCCESS) { |
| 542 | return DPE_INVALID_COMMAND; |
| 543 | } |
| 544 | |
Maulik Patel | 8a969fd | 2024-03-01 14:43:03 +0000 | [diff] [blame] | 545 | if (num_of_input_arguments > num_of_valid_arguments) { |
| 546 | /* Extra unsupported arguments encoded in command map */ |
| 547 | return DPE_INVALID_ARGUMENT; |
| 548 | } |
| 549 | |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 550 | dpe_err = get_certificate_chain_request(context_handle, |
| 551 | retain_context, |
| 552 | clear_from_context, |
| 553 | certificate_chain_buf, |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 554 | DICE_CERT_CHAIN_SIZE, |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 555 | &certificate_chain_actual_size, |
| 556 | &new_context_handle); |
| 557 | if (dpe_err != DPE_NO_ERROR) { |
| 558 | return dpe_err; |
| 559 | } |
| 560 | |
| 561 | /* Encode response */ |
| 562 | QCBOREncode_OpenArray(encode_ctx); |
| 563 | QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR); |
| 564 | |
| 565 | QCBOREncode_OpenMap(encode_ctx); |
| 566 | |
| 567 | /* The certificate chain is already encoded into a CBOR array by the get certificate |
| 568 | * chain implementation. Add it as a byte string so that its decoding can be |
| 569 | * skipped and the CBOR returned to the caller. |
| 570 | */ |
| 571 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_GET_CERTIFICATE_CHAIN_CERTIFICATE_CHAIN, |
| 572 | (UsefulBufC){ certificate_chain_buf, |
| 573 | certificate_chain_actual_size }); |
| 574 | |
| 575 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_GET_CERTIFICATE_CHAIN_NEW_CONTEXT_HANDLE, |
| 576 | (UsefulBufC){ &new_context_handle, |
| 577 | sizeof(new_context_handle) }); |
| 578 | |
| 579 | QCBOREncode_CloseMap(encode_ctx); |
| 580 | |
| 581 | QCBOREncode_CloseArray(encode_ctx); |
| 582 | |
| 583 | return DPE_NO_ERROR; |
| 584 | } |
| 585 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 586 | static void encode_error_only(QCBOREncodeContext *encode_ctx, |
| 587 | dpe_error_t dpe_err) |
| 588 | { |
| 589 | QCBOREncode_OpenArray(encode_ctx); |
| 590 | QCBOREncode_AddInt64(encode_ctx, dpe_err); |
| 591 | QCBOREncode_CloseArray(encode_ctx); |
| 592 | } |
| 593 | |
| 594 | int32_t dpe_command_decode(int32_t client_id, |
| 595 | const char *cmd_input, size_t cmd_input_size, |
| 596 | char *cmd_output, size_t *cmd_output_size) |
| 597 | { |
| 598 | dpe_error_t dpe_err; |
| 599 | QCBORError qcbor_err; |
| 600 | QCBORDecodeContext decode_ctx; |
| 601 | QCBOREncodeContext encode_ctx; |
| 602 | UsefulBufC out; |
| 603 | uint64_t command_id; |
| 604 | |
| 605 | QCBORDecode_Init(&decode_ctx, (UsefulBufC){ cmd_input, cmd_input_size }, |
| 606 | QCBOR_DECODE_MODE_NORMAL); |
| 607 | QCBOREncode_Init(&encode_ctx, (UsefulBuf){ cmd_output, *cmd_output_size }); |
| 608 | |
| 609 | /* Enter top level array */ |
| 610 | QCBORDecode_EnterArray(&decode_ctx, NULL); |
| 611 | |
| 612 | /* Get the command ID */ |
| 613 | QCBORDecode_GetUInt64(&decode_ctx, &command_id); |
| 614 | |
| 615 | /* Check for errors before interpreting the decoded command ID */ |
| 616 | qcbor_err = QCBORDecode_GetError(&decode_ctx); |
| 617 | |
| 618 | if (qcbor_err == QCBOR_SUCCESS) { |
| 619 | switch (command_id) { |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 620 | case DPE_DERIVE_CONTEXT: |
| 621 | dpe_err = decode_derive_context(&decode_ctx, &encode_ctx, client_id); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 622 | break; |
| 623 | case DPE_CERTIFY_KEY: |
| 624 | dpe_err = decode_certify_key(&decode_ctx, &encode_ctx); |
| 625 | break; |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 626 | case DPE_GET_CERTIFICATE_CHAIN: |
| 627 | dpe_err = decode_get_certificate_chain(&decode_ctx, &encode_ctx); |
| 628 | break; |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 629 | case DPE_DESTROY_CONTEXT: |
| 630 | dpe_err = decode_destroy_context(&decode_ctx, &encode_ctx); |
| 631 | break; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 632 | default: |
| 633 | dpe_err = DPE_INVALID_COMMAND; |
| 634 | break; |
| 635 | } |
| 636 | } else { |
| 637 | dpe_err = DPE_INVALID_COMMAND; |
| 638 | } |
| 639 | |
| 640 | /* If an unhandled DPE error was returned, then encode it into a response */ |
| 641 | if (dpe_err != DPE_NO_ERROR) { |
| 642 | encode_error_only(&encode_ctx, dpe_err); |
| 643 | } |
| 644 | |
| 645 | qcbor_err = QCBOREncode_Finish(&encode_ctx, &out); |
| 646 | if (qcbor_err != QCBOR_SUCCESS) { |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | *cmd_output_size = out.len; |
| 651 | |
| 652 | return 0; |
| 653 | } |