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 | |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 19 | /* |
| 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 Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 40 | static 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 Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 49 | DPE_DERIVE_CONTEXT_INPUT_DATA, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 50 | 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 Patel | d936d74 | 2024-01-08 14:16:46 +0000 | [diff] [blame] | 124 | qcbor_err = QCBORDecode_GetError(decode_ctx); |
| 125 | if (qcbor_err != QCBOR_SUCCESS) { |
| 126 | return DPE_INVALID_COMMAND; |
| 127 | } |
| 128 | |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 129 | return DPE_NO_ERROR; |
| 130 | } |
| 131 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 132 | //TODO: Handle the omission of parameters from DPE commands. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 133 | static dpe_error_t decode_derive_context(QCBORDecodeContext *decode_ctx, |
| 134 | QCBOREncodeContext *encode_ctx, |
| 135 | int32_t client_id) |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 136 | { |
| 137 | dpe_error_t dpe_err; |
| 138 | QCBORError qcbor_err; |
| 139 | UsefulBufC out; |
| 140 | int context_handle; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 141 | int32_t target_locality; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 142 | bool retain_parent_context; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 143 | bool allow_new_context_to_derive; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 144 | bool create_certificate; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 145 | bool return_certificate; |
| 146 | bool allow_new_context_to_export; |
| 147 | bool export_cdi; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 148 | DiceInputValues dice_inputs; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 149 | int new_context_handle; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 150 | int new_parent_context_handle; |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 151 | uint8_t *new_certificate_buf = REUSE_CMD_BUF(DICE_CERT_SIZE); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 152 | 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 Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 155 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 156 | /* Decode DeriveContext command */ |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 157 | QCBORDecode_EnterMap(decode_ctx, NULL); |
| 158 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 159 | QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DERIVE_CONTEXT_CONTEXT_HANDLE, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 160 | &out); |
| 161 | if (out.len != sizeof(context_handle)) { |
| 162 | return DPE_INVALID_COMMAND; |
| 163 | } |
| 164 | memcpy(&context_handle, out.ptr, out.len); |
| 165 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 166 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_RETAIN_PARENT_CONTEXT, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 167 | &retain_parent_context); |
| 168 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 169 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_ALLOW_NEW_CONTEXT_TO_DERIVE, |
| 170 | &allow_new_context_to_derive); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 171 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 172 | QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CONTEXT_CREATE_CERTIFICATE, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 173 | &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 Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 180 | 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 Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 196 | 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 Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 207 | dpe_err = derive_context_request(context_handle, retain_parent_context, |
| 208 | allow_new_context_to_derive, create_certificate, |
| 209 | &dice_inputs, client_id, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 210 | target_locality, |
| 211 | return_certificate, |
| 212 | allow_new_context_to_export, |
| 213 | export_cdi, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 214 | &new_context_handle, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 215 | &new_parent_context_handle, |
| 216 | new_certificate_buf, |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 217 | DICE_CERT_SIZE, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 218 | &new_certificate_actual_size, |
| 219 | exported_cdi_buf, |
| 220 | sizeof(exported_cdi_buf), |
| 221 | &exported_cdi_actual_size); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 222 | 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 Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 231 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CONTEXT_NEW_CONTEXT_HANDLE, |
| 232 | (UsefulBufC){ &new_context_handle, |
| 233 | sizeof(new_context_handle) }); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 234 | QCBOREncode_AddBytesToMapN(encode_ctx, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 235 | DPE_DERIVE_CONTEXT_PARENT_CONTEXT_HANDLE, |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 236 | (UsefulBufC){ &new_parent_context_handle, |
| 237 | sizeof(new_parent_context_handle) }); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 238 | |
| 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 Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 251 | QCBOREncode_CloseMap(encode_ctx); |
| 252 | |
| 253 | QCBOREncode_CloseArray(encode_ctx); |
| 254 | |
| 255 | return DPE_NO_ERROR; |
| 256 | } |
| 257 | |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 258 | static 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 Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 304 | static 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 Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 316 | uint8_t *certificate_buf = REUSE_CMD_BUF(DICE_CERT_SIZE); |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 317 | size_t certificate_actual_size; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 318 | uint8_t derived_public_key_buf[DPE_ATTEST_PUB_KEY_SIZE]; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 319 | 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 Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 355 | dpe_err = certify_key_request(context_handle, retain_context, public_key, |
| 356 | public_key_size, label, label_size, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 357 | certificate_buf, |
Tamas Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 358 | DICE_CERT_SIZE, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 359 | &certificate_actual_size, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 360 | derived_public_key_buf, |
| 361 | sizeof(derived_public_key_buf), |
| 362 | &derived_public_key_actual_size, |
| 363 | &new_context_handle); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 364 | 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 Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 378 | QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_CERTIFICATE, |
| 379 | (UsefulBufC){ certificate_buf, |
| 380 | certificate_actual_size }); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 381 | |
| 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 Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 396 | static 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 Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 405 | uint8_t *certificate_chain_buf = REUSE_CMD_BUF(DICE_CERT_CHAIN_SIZE); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 406 | 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 Ban | d0983e9 | 2024-01-25 16:32:51 +0100 | [diff] [blame] | 440 | DICE_CERT_CHAIN_SIZE, |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 441 | &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 Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 472 | static 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 | |
| 480 | int32_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 Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 506 | case DPE_DERIVE_CONTEXT: |
| 507 | dpe_err = decode_derive_context(&decode_ctx, &encode_ctx, client_id); |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 508 | break; |
| 509 | case DPE_CERTIFY_KEY: |
| 510 | dpe_err = decode_certify_key(&decode_ctx, &encode_ctx); |
| 511 | break; |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 512 | case DPE_GET_CERTIFICATE_CHAIN: |
| 513 | dpe_err = decode_get_certificate_chain(&decode_ctx, &encode_ctx); |
| 514 | break; |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 515 | case DPE_DESTROY_CONTEXT: |
| 516 | dpe_err = decode_destroy_context(&decode_ctx, &encode_ctx); |
| 517 | break; |
Jamie Fox | ab30e71 | 2023-03-30 17:48:36 +0100 | [diff] [blame] | 518 | 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 | } |