Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 1 | /* |
Tamas Ban | a5e2f58 | 2024-01-25 16:59:26 +0100 | [diff] [blame] | 2 | * Copyright (c) 2023-2024, Arm Limited. All rights reserved. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "dpe_context_mngr.h" |
| 9 | #include <assert.h> |
| 10 | #include <string.h> |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 11 | #include "array.h" |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 12 | #include "dice_protection_environment.h" |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 13 | #include "dpe_certificate.h" |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 14 | #include "dpe_client.h" |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 15 | #include "dpe_crypto_interface.h" |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 16 | #include "dpe_log.h" |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 17 | #include "dpe_plat.h" |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 18 | #include "psa/crypto.h" |
| 19 | |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 20 | #ifdef DPE_TEST_MODE |
| 21 | #define TEST_ROT_CDI_VAL { \ |
| 22 | 0xD2, 0x90, 0x66, 0x07, 0x2A, 0x2D, 0x2A, 0x00, \ |
| 23 | 0x91, 0x9D, 0xD9, 0x15, 0x14, 0xBE, 0x2D, 0xCC, \ |
| 24 | 0xA3, 0x9F, 0xDE, 0xC3, 0x35, 0x75, 0x84, 0x6E, \ |
| 25 | 0x4C, 0xB9, 0x28, 0xAC, 0x7A, 0x4E, 0X00, 0x7F \ |
| 26 | } |
| 27 | #endif /* DPE_TEST_MODE */ |
| 28 | |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 29 | #define CONTEXT_DATA_MAX_SIZE sizeof(struct component_context_data_t) |
| 30 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 31 | static struct component_context_t component_ctx_array[MAX_NUM_OF_COMPONENTS]; |
| 32 | static struct layer_context_t layer_ctx_array[MAX_NUM_OF_LAYERS]; |
| 33 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 34 | static dpe_error_t store_linked_component(struct layer_context_t *layer_ctx, |
| 35 | int component_idx) |
| 36 | { |
| 37 | if (layer_ctx->linked_components.count >= |
| 38 | ARRAY_SIZE(layer_ctx->linked_components.idx)) { |
| 39 | /* linked_components.idx[] is full */ |
| 40 | return DPE_INSUFFICIENT_MEMORY; |
| 41 | } |
| 42 | |
| 43 | layer_ctx->linked_components.idx[layer_ctx->linked_components.count] = component_idx; |
| 44 | layer_ctx->linked_components.count++; |
| 45 | |
| 46 | return DPE_NO_ERROR; |
| 47 | } |
| 48 | |
| 49 | static void remove_linked_component(struct layer_context_t *layer_ctx, |
| 50 | int component_idx) |
| 51 | { |
| 52 | int i, pos; |
| 53 | |
| 54 | /* Find the position of the input component */ |
| 55 | for (i = 0; i < ARRAY_SIZE(layer_ctx->linked_components.idx); i++) { |
| 56 | if (layer_ctx->linked_components.idx[i] == component_idx) { |
| 57 | pos = i; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | assert(i < ARRAY_SIZE(layer_ctx->linked_components.idx)); |
| 63 | |
| 64 | /* Left shift remaining elements by 1 from current position */ |
| 65 | for(i = pos; i < ARRAY_SIZE(layer_ctx->linked_components.idx) - 1; i++) { |
| 66 | layer_ctx->linked_components.idx[i] = layer_ctx->linked_components.idx[i + 1]; |
| 67 | } |
| 68 | layer_ctx->linked_components.idx[i] = INVALID_LAYER_IDX; |
| 69 | layer_ctx->linked_components.count--; |
| 70 | } |
| 71 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 72 | static int get_free_component_context_index(void) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) { |
| 77 | if (!component_ctx_array[i].in_use) { |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (i >= MAX_NUM_OF_COMPONENTS) { |
| 83 | /* No free index left in the array -- all used up! */ |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | return i; |
| 88 | } |
| 89 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 90 | static dpe_error_t renew_nonce(int *handle) |
| 91 | { |
| 92 | uint16_t nonce; |
| 93 | |
| 94 | psa_status_t status = psa_generate_random((uint8_t *)&nonce, sizeof(nonce)); |
| 95 | if (status != PSA_SUCCESS) { |
| 96 | return DPE_INTERNAL_ERROR; |
| 97 | } |
| 98 | *handle = SET_NONCE(*handle, nonce); |
| 99 | |
| 100 | return DPE_NO_ERROR; |
| 101 | } |
| 102 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 103 | static void set_context_to_default(int i) |
| 104 | { |
| 105 | component_ctx_array[i].in_use = false; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 106 | component_ctx_array[i].is_allowed_to_derive = true; |
| 107 | /* export CDI attribute is inherited and once disabled, a derived context |
| 108 | * and subsequent derivations cannot export CDI, hence enable by default |
| 109 | */ |
| 110 | component_ctx_array[i].is_export_cdi_allowed = true; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 111 | component_ctx_array[i].nonce = INVALID_NONCE_VALUE; |
| 112 | component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX; |
| 113 | component_ctx_array[i].linked_layer_idx = INVALID_LAYER_IDX; |
| 114 | (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t)); |
| 115 | //TODO: Question: how to initialise MHU Id mapping? |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 116 | component_ctx_array[i].target_locality = 0; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 117 | /* Allow component to be derived by default */ |
| 118 | } |
| 119 | |
| 120 | static void invalidate_layer(int i) |
| 121 | { |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 122 | int j; |
| 123 | |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 124 | layer_ctx_array[i].idx = i; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 125 | layer_ctx_array[i].state = LAYER_STATE_CLOSED; |
| 126 | layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 127 | layer_ctx_array[i].is_cdi_to_be_exported = false; |
Maulik Patel | 87c47ce | 2024-04-22 13:30:56 +0100 | [diff] [blame] | 128 | layer_ctx_array[i].is_rot_layer = false; |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 129 | layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 130 | (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0, |
| 131 | sizeof(layer_ctx_array[i].attest_cdi_hash_input)); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 132 | (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id); |
| 133 | (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 134 | (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t)); |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 135 | layer_ctx_array[i].linked_components.count = 0; |
| 136 | for (j = 0; j < ARRAY_SIZE(layer_ctx_array[i].linked_components.idx); j++) { |
| 137 | layer_ctx_array[i].linked_components.idx[j] = INVALID_COMPONENT_IDX; |
| 138 | } |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 141 | static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx, |
| 142 | const DiceInputValues *dice_inputs) |
| 143 | { |
| 144 | size_t hash_len; |
| 145 | psa_status_t status; |
| 146 | |
| 147 | memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash, |
| 148 | DICE_HASH_SIZE); |
| 149 | memcpy(&dest_ctx->data.measurement_descriptor, |
| 150 | dice_inputs->code_descriptor, |
| 151 | dice_inputs->code_descriptor_size); |
| 152 | |
| 153 | dest_ctx->data.measurement_descriptor_size = |
| 154 | dice_inputs->code_descriptor_size; |
| 155 | |
| 156 | memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE); |
| 157 | memcpy(&dest_ctx->data.signer_id_descriptor, |
| 158 | dice_inputs->authority_descriptor, |
| 159 | dice_inputs->authority_descriptor_size); |
| 160 | |
| 161 | dest_ctx->data.signer_id_descriptor_size = |
| 162 | dice_inputs->authority_descriptor_size; |
| 163 | |
| 164 | if (dice_inputs->config_type == kDiceConfigTypeInline) { |
| 165 | /* Copy config_value */ |
| 166 | memcpy(&dest_ctx->data.config_value, dice_inputs->config_value, |
| 167 | DICE_INLINE_CONFIG_SIZE); |
| 168 | |
| 169 | } else { |
| 170 | /* Copy config descriptor */ |
| 171 | memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor, |
| 172 | dice_inputs->config_descriptor_size); |
| 173 | dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size; |
| 174 | |
| 175 | /* Calculate config value as hash of input config descriptor */ |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 176 | status = psa_hash_compute(DPE_HASH_ALG, |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 177 | dice_inputs->config_descriptor, |
| 178 | dice_inputs->config_descriptor_size, |
| 179 | dest_ctx->data.config_value, |
| 180 | sizeof(dest_ctx->data.config_value), |
| 181 | &hash_len); |
| 182 | |
| 183 | if (status != PSA_SUCCESS) { |
| 184 | return DPE_INTERNAL_ERROR; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | dest_ctx->data.mode = dice_inputs->mode; |
| 189 | memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE); |
| 190 | |
| 191 | return DPE_NO_ERROR; |
| 192 | } |
| 193 | |
| 194 | static bool is_dice_input_valid(const DiceInputValues *dice_inputs) |
| 195 | { |
| 196 | if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) || |
| 197 | (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) || |
| 198 | (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | static bool is_input_handle_valid(int input_context_handle) |
| 206 | { |
| 207 | uint16_t idx = GET_IDX(input_context_handle); |
| 208 | uint16_t nonce = GET_NONCE(input_context_handle); |
| 209 | |
| 210 | /* Validate input handle id and nonce */ |
| 211 | if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if (nonce == component_ctx_array[idx].nonce) { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 222 | /* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in |
| 223 | * same order |
| 224 | */ |
| 225 | static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf, |
| 226 | size_t max_size, |
| 227 | size_t *dest_size, |
| 228 | const struct component_context_t *comp_ctx) |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 229 | { |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 230 | size_t out_size = 0; |
| 231 | |
| 232 | if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE + |
| 233 | sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) { |
| 234 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 235 | } |
| 236 | |
| 237 | memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE); |
| 238 | out_size += DICE_HASH_SIZE; |
| 239 | |
| 240 | memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE); |
| 241 | out_size += DICE_INLINE_CONFIG_SIZE; |
| 242 | |
| 243 | memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE); |
| 244 | out_size += DICE_HASH_SIZE; |
| 245 | |
| 246 | memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode)); |
| 247 | out_size += sizeof(comp_ctx->data.mode); |
| 248 | |
| 249 | memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE); |
| 250 | out_size += DICE_HIDDEN_SIZE; |
| 251 | |
| 252 | *dest_size = out_size; |
| 253 | |
| 254 | return PSA_SUCCESS; |
| 255 | } |
| 256 | |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 257 | static psa_status_t compute_layer_cdi_attest_input(struct layer_context_t *layer_ctx) |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 258 | { |
| 259 | psa_status_t status; |
| 260 | uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE]; |
| 261 | size_t ctx_data_size, hash_len; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 262 | int i, idx; |
| 263 | uint16_t num_of_linked_components; |
| 264 | |
| 265 | num_of_linked_components = layer_ctx->linked_components.count; |
| 266 | if (num_of_linked_components == 0) { |
| 267 | /* No components to hash */ |
| 268 | return PSA_SUCCESS; |
| 269 | } |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 270 | |
| 271 | psa_hash_operation_t hash_op = psa_hash_operation_init(); |
| 272 | status = psa_hash_setup(&hash_op, DPE_HASH_ALG); |
| 273 | if (status != PSA_SUCCESS) { |
| 274 | return status; |
| 275 | } |
| 276 | |
| 277 | //TODO: |
| 278 | /* How to combine measurements of multiple SW components into a single hash |
| 279 | * is not yet defined by the Open DICE profile. This implementation |
| 280 | * concatenates the data of all SW components which belong to the same layer |
| 281 | * and hash it. |
| 282 | */ |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 283 | for (i = 0; i < num_of_linked_components; i++) { |
| 284 | idx = layer_ctx->linked_components.idx[i]; |
| 285 | status = get_component_data_for_attest_cdi(component_ctx_data, |
| 286 | sizeof(component_ctx_data), |
| 287 | &ctx_data_size, |
| 288 | &component_ctx_array[idx]); |
| 289 | if (status != PSA_SUCCESS) { |
| 290 | return status; |
| 291 | } |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 292 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 293 | status = psa_hash_update(&hash_op, |
| 294 | component_ctx_data, |
| 295 | ctx_data_size); |
| 296 | if (status != PSA_SUCCESS) { |
| 297 | return status; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | status = psa_hash_finish(&hash_op, |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 302 | &layer_ctx->attest_cdi_hash_input[0], |
| 303 | sizeof(layer_ctx->attest_cdi_hash_input), |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 304 | &hash_len); |
| 305 | |
| 306 | assert(hash_len == DPE_HASH_ALG_SIZE); |
| 307 | |
| 308 | return status; |
| 309 | } |
| 310 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 311 | static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx, |
| 312 | uint8_t *exported_cdi_buf, |
| 313 | size_t exported_cdi_buf_size, |
| 314 | size_t *exported_cdi_actual_size) |
| 315 | { |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 316 | uint8_t cdi_attest_buf[DICE_CDI_SIZE]; |
| 317 | uint8_t cdi_seal_buf[DICE_CDI_SIZE]; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 318 | psa_status_t status; |
| 319 | dpe_error_t err; |
| 320 | |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 321 | /* Get CDIs value */ |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 322 | status = get_layer_cdi_value(layer_ctx, |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 323 | cdi_attest_buf, |
| 324 | cdi_seal_buf); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 325 | if (status != PSA_SUCCESS) { |
| 326 | return DPE_INTERNAL_ERROR; |
| 327 | } |
| 328 | |
| 329 | /* Encode CDI value */ |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 330 | err = encode_cdi(cdi_attest_buf, |
| 331 | cdi_seal_buf, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 332 | exported_cdi_buf, |
| 333 | exported_cdi_buf_size, |
| 334 | exported_cdi_actual_size); |
| 335 | if (err != DPE_NO_ERROR) { |
| 336 | return err; |
| 337 | } |
| 338 | layer_ctx->is_cdi_to_be_exported = true; |
| 339 | |
| 340 | return DPE_NO_ERROR; |
| 341 | } |
| 342 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 343 | static dpe_error_t prepare_layer_certificate(struct layer_context_t *layer_ctx, |
| 344 | const struct layer_context_t *parent_layer_ctx) |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 345 | { |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 346 | psa_status_t status; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 347 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 348 | /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */ |
Maulik Patel | 87c47ce | 2024-04-22 13:30:56 +0100 | [diff] [blame] | 349 | if ((!layer_ctx->is_rot_layer) && |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 350 | (!layer_ctx->is_external_pub_key_provided)) { |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 351 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 352 | /* Except for RoT Layer with no external public key supplied */ |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 353 | |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 354 | status = compute_layer_cdi_attest_input(layer_ctx); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 355 | if (status != PSA_SUCCESS) { |
| 356 | return DPE_INTERNAL_ERROR; |
| 357 | } |
| 358 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 359 | status = derive_attestation_cdi(layer_ctx, parent_layer_ctx); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 360 | if (status != PSA_SUCCESS) { |
| 361 | return DPE_INTERNAL_ERROR; |
| 362 | } |
| 363 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 364 | status = derive_sealing_cdi(layer_ctx); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 365 | if (status != PSA_SUCCESS) { |
| 366 | return DPE_INTERNAL_ERROR; |
| 367 | } |
| 368 | } |
| 369 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 370 | status = derive_wrapping_key(layer_ctx); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 371 | if (status != PSA_SUCCESS) { |
| 372 | return DPE_INTERNAL_ERROR; |
| 373 | } |
| 374 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 375 | if (!layer_ctx->is_external_pub_key_provided) { |
| 376 | status = derive_attestation_key(layer_ctx); |
| 377 | if (status != PSA_SUCCESS) { |
| 378 | return DPE_INTERNAL_ERROR; |
| 379 | } |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 382 | status = derive_id_from_public_key(layer_ctx); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 383 | if (status != PSA_SUCCESS) { |
| 384 | return DPE_INTERNAL_ERROR; |
| 385 | } |
| 386 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 387 | return DPE_NO_ERROR; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static uint16_t open_new_layer(void) |
| 391 | { |
| 392 | int i; |
| 393 | |
| 394 | for (i = 0; i < MAX_NUM_OF_LAYERS; i++) { |
| 395 | if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) { |
| 396 | layer_ctx_array[i].state = LAYER_STATE_OPEN; |
| 397 | return i; |
| 398 | } |
| 399 | } |
| 400 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 401 | //TODO: There is an open issue of layer creation as described below. |
| 402 | /* This is causing extra unintended layers to open. Since each layer |
| 403 | * has some context data and certificate buffer of 3k, it is |
| 404 | * causing RAM overflow. Hence until resoluton is reached, once all |
| 405 | * layers are opened, link new compenents to the last layer. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 406 | * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate |
| 407 | * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM, |
| 408 | * but since AP BL2 is finalised, we open new layer (Hypervisor layer). |
| 409 | * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 410 | * we open new layer! Here AP SPx should belong to same layer as AP SPM. |
| 411 | */ |
| 412 | return MAX_NUM_OF_LAYERS - 1; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 415 | static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality) |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 416 | { |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 417 | // TODO: FIXME |
| 418 | // return (client_id == target_locality); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 419 | return true; |
| 420 | } |
| 421 | |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 422 | static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx) |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 423 | { |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 424 | int i; |
| 425 | |
| 426 | for (i = 0; i < MAX_NUM_OF_LAYERS; i++) { |
| 427 | if (layer_ctx_array[i].cert_id == cert_id) { |
| 428 | *layer_idx = i; |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* No certificate ID match found */ |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx, |
| 438 | uint32_t cert_id) |
| 439 | { |
| 440 | uint16_t parent_layer_idx, layer_idx_to_link; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 441 | |
| 442 | assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS); |
| 443 | |
| 444 | parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx; |
| 445 | assert(parent_layer_idx < MAX_NUM_OF_LAYERS); |
| 446 | |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 447 | if (cert_id != DPE_CERT_ID_INVALID) { |
| 448 | /* cert id was sent by the client */ |
| 449 | if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) { |
| 450 | if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) { |
| 451 | /* Cannot add to the layer which is already finalised */ |
| 452 | return DPE_INTERNAL_ERROR; |
| 453 | } |
| 454 | /* Derived context belongs to the same certificate as its parent component */ |
| 455 | new_ctx->linked_layer_idx = parent_layer_idx; |
| 456 | |
| 457 | } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) { |
| 458 | /* Cert ID is already in use */ |
| 459 | if (layer_ctx_array[layer_idx_to_link].state == LAYER_STATE_FINALISED) { |
| 460 | /* Cannot add to the layer which is already finalised */ |
| 461 | return DPE_INTERNAL_ERROR; |
| 462 | } |
| 463 | /* Use the same layer that is associated with cert_id */ |
| 464 | new_ctx->linked_layer_idx = layer_idx_to_link; |
| 465 | /* Linked layer's parent is already assigned when it was opened */ |
| 466 | |
| 467 | } else { |
| 468 | /* Open new layer and link derived context to new layer */ |
| 469 | layer_idx_to_link = open_new_layer(); |
| 470 | if (layer_idx_to_link == INVALID_LAYER_IDX) { |
| 471 | return DPE_INTERNAL_ERROR; |
| 472 | } |
| 473 | /* Link this context to the new layer */ |
| 474 | new_ctx->linked_layer_idx = layer_idx_to_link; |
| 475 | /* New layer's parent is parent component's layer */ |
| 476 | layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx; |
| 477 | layer_ctx_array[layer_idx_to_link].cert_id = cert_id; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 478 | } |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 479 | |
| 480 | } else { |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 481 | /* cert id was not sent by the client */ |
| 482 | //TODO: To be implemented; return error for now. |
| 483 | return DPE_INVALID_ARGUMENT; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 484 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 485 | |
| 486 | return DPE_NO_ERROR; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 489 | /** |
| 490 | * \brief Create a root of trust component context. |
| 491 | * |
| 492 | * \param[out] rot_ctx_handle A new context handle for the RoT context. |
| 493 | * |
| 494 | * \return Returns error code of type dpe_error_t |
| 495 | */ |
| 496 | static dpe_error_t create_rot_context(int *rot_ctx_handle) |
| 497 | { |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 498 | #ifdef DPE_TEST_MODE |
| 499 | uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL; |
| 500 | #else |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 501 | uint8_t rot_cdi_input[DICE_CDI_SIZE]; |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 502 | #endif /* DPE_TEST_MODE */ |
| 503 | psa_status_t status; |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 504 | struct component_context_t *rot_comp_ctx = &component_ctx_array[0]; |
| 505 | struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX]; |
| 506 | |
Maulik Patel | 87c47ce | 2024-04-22 13:30:56 +0100 | [diff] [blame] | 507 | rot_layer_ctx->is_rot_layer = true; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 508 | /* Parent layer for RoT context's layer is same */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 509 | rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX; |
| 510 | |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 511 | #ifndef DPE_TEST_MODE |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 512 | /* Get the RoT CDI input for the RoT layer */ |
Antonio de Angelis | 30211a6 | 2024-04-04 12:48:18 +0100 | [diff] [blame] | 513 | status = get_rot_cdi_input(&rot_cdi_input[0], sizeof(rot_cdi_input)); |
| 514 | if (status != PSA_SUCCESS) { |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 515 | return DPE_INTERNAL_ERROR; |
| 516 | } |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 517 | #endif /* DPE_TEST_MODE */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 518 | |
| 519 | /* Import the CDI key for the RoT layer */ |
| 520 | status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX], |
| 521 | &rot_cdi_input[0], |
| 522 | sizeof(rot_cdi_input)); |
| 523 | if (status != PSA_SUCCESS) { |
| 524 | return DPE_INTERNAL_ERROR; |
| 525 | } |
| 526 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 527 | /* Init RoT context, ready to be derived in next call to DeriveContext */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 528 | rot_comp_ctx->nonce = 0; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 529 | /* Parent component index for derived RoT context is same */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 530 | rot_comp_ctx->parent_idx = 0; |
| 531 | /* Link context to RoT Layer */ |
| 532 | rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX; |
| 533 | rot_comp_ctx->expected_mhu_id = 0; |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 534 | *rot_ctx_handle = 0; /* index = 0, nonce = 0 */ |
| 535 | |
| 536 | return DPE_NO_ERROR; |
| 537 | } |
| 538 | |
| 539 | dpe_error_t initialise_context_mngr(int *rot_ctx_handle) |
| 540 | { |
| 541 | int i; |
| 542 | |
| 543 | for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) { |
| 544 | set_context_to_default(i); |
| 545 | } |
| 546 | |
| 547 | for (i = 0; i < MAX_NUM_OF_LAYERS; i++) { |
| 548 | invalidate_layer(i); |
| 549 | } |
| 550 | |
| 551 | return create_rot_context(rot_ctx_handle); |
| 552 | } |
| 553 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 554 | dpe_error_t derive_context_request(int input_ctx_handle, |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 555 | uint32_t cert_id, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 556 | bool retain_parent_context, |
| 557 | bool allow_new_context_to_derive, |
| 558 | bool create_certificate, |
| 559 | const DiceInputValues *dice_inputs, |
| 560 | int32_t client_id, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 561 | int32_t target_locality, |
| 562 | bool return_certificate, |
| 563 | bool allow_new_context_to_export, |
| 564 | bool export_cdi, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 565 | int *new_context_handle, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 566 | int *new_parent_context_handle, |
| 567 | uint8_t *new_certificate_buf, |
| 568 | size_t new_certificate_buf_size, |
| 569 | size_t *new_certificate_actual_size, |
| 570 | uint8_t *exported_cdi_buf, |
| 571 | size_t exported_cdi_buf_size, |
| 572 | size_t *exported_cdi_actual_size) |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 573 | { |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 574 | dpe_error_t err; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 575 | struct component_context_t *parent_ctx, *derived_ctx; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 576 | uint16_t parent_ctx_idx, linked_layer_idx, parent_layer_idx; |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 577 | int free_component_idx; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 578 | struct layer_context_t *layer_ctx, *parent_layer_ctx; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 579 | |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 580 | log_derive_context(input_ctx_handle, cert_id, retain_parent_context, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 581 | allow_new_context_to_derive, create_certificate, dice_inputs, |
| 582 | client_id); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 583 | |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 584 | #ifdef DPE_TEST_MODE |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 585 | if ((input_ctx_handle == 0) && |
| 586 | (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) { |
Maulik Patel | dbfd515 | 2023-05-30 17:02:42 +0100 | [diff] [blame] | 587 | /* Deriving RoT context for tests */ |
| 588 | err = create_rot_context(&input_ctx_handle); |
| 589 | if (err != DPE_NO_ERROR) { |
| 590 | return err; |
| 591 | } |
| 592 | } |
| 593 | #endif /* DPE_TEST_MODE */ |
| 594 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 595 | if (export_cdi && !create_certificate) { |
| 596 | return DPE_INVALID_ARGUMENT; |
| 597 | } |
| 598 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 599 | /* Validate dice inputs */ |
| 600 | if (!is_dice_input_valid(dice_inputs)) { |
| 601 | return DPE_INVALID_ARGUMENT; |
| 602 | } |
| 603 | |
| 604 | /* Validate input handle */ |
| 605 | if (!is_input_handle_valid(input_ctx_handle)) { |
| 606 | return DPE_INVALID_ARGUMENT; |
| 607 | } |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 608 | /* Get parent component index from the input handle */ |
| 609 | parent_ctx_idx = GET_IDX(input_ctx_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 610 | |
| 611 | /* Below check is for safety only; It should not happen |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 612 | * parent_ctx_idx is already checked above in is_input_handle_valid() |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 613 | */ |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 614 | assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 615 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 616 | parent_ctx = &component_ctx_array[parent_ctx_idx]; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 617 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 618 | /* Check if parent context is allowed to derive */ |
| 619 | if (!parent_ctx->is_allowed_to_derive) { |
| 620 | return DPE_INVALID_ARGUMENT; |
| 621 | } |
| 622 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 623 | //TODO: Question: how to get mhu id of incoming request? |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 624 | if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) { |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 625 | return DPE_INVALID_ARGUMENT; |
| 626 | } |
| 627 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 628 | /* Get next free component index to add new derived context */ |
| 629 | free_component_idx = get_free_component_context_index(); |
| 630 | if (free_component_idx < 0) { |
| 631 | return DPE_INSUFFICIENT_MEMORY; |
| 632 | } |
| 633 | |
| 634 | derived_ctx = &component_ctx_array[free_component_idx]; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 635 | if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) { |
| 636 | /* If parent context has export enabled and input allow_new_context_to_export |
| 637 | * is true, then allow context CDI to be exported for derived context |
| 638 | */ |
| 639 | derived_ctx->is_export_cdi_allowed = true; |
| 640 | } else { |
| 641 | /* Export of new context CDI is NOT allowed */ |
| 642 | derived_ctx->is_export_cdi_allowed = false; |
| 643 | if (export_cdi) { |
| 644 | return DPE_INVALID_ARGUMENT; |
| 645 | } |
| 646 | } |
| 647 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 648 | /* Copy dice input to the new derived component context */ |
| 649 | err = copy_dice_input(derived_ctx, dice_inputs); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 650 | if (err != DPE_NO_ERROR) { |
| 651 | return err; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 652 | } |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 653 | derived_ctx->target_locality = target_locality; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 654 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 655 | /* Update parent idx in new derived component context */ |
| 656 | derived_ctx->parent_idx = parent_ctx_idx; |
| 657 | /* Mark new derived component index as in use */ |
| 658 | derived_ctx->in_use = true; |
Maulik Patel | d280607 | 2024-02-02 10:30:08 +0000 | [diff] [blame] | 659 | derived_ctx->is_allowed_to_derive = allow_new_context_to_derive; |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 660 | err = assign_layer_to_context(derived_ctx, cert_id); |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 661 | if (err != DPE_NO_ERROR) { |
| 662 | return err; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (retain_parent_context) { |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 666 | /* Retain and return parent handle with renewed nonce */ |
| 667 | *new_parent_context_handle = input_ctx_handle; |
| 668 | err = renew_nonce(new_parent_context_handle); |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 669 | if (err != DPE_NO_ERROR) { |
| 670 | return err; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 671 | } |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 672 | parent_ctx->nonce = GET_NONCE(*new_parent_context_handle); |
| 673 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 674 | } else { |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 675 | /* Return invalid handle */ |
| 676 | *new_parent_context_handle = INVALID_HANDLE; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 677 | parent_ctx->nonce = INVALID_NONCE_VALUE; |
| 678 | } |
| 679 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 680 | if (!export_cdi) { |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 681 | /* Return handle to derived context */ |
| 682 | *new_context_handle = SET_IDX(*new_context_handle, free_component_idx); |
| 683 | err = renew_nonce(new_context_handle); |
| 684 | if (err != DPE_NO_ERROR) { |
| 685 | return err; |
| 686 | } |
| 687 | /* Update nonce in new derived component context */ |
| 688 | derived_ctx->nonce = GET_NONCE(*new_context_handle); |
| 689 | |
| 690 | } else { |
| 691 | /* Return invalid handle */ |
| 692 | *new_context_handle = INVALID_HANDLE; |
| 693 | derived_ctx->nonce = INVALID_NONCE_VALUE; |
| 694 | } |
| 695 | |
Maulik Patel | 5ac8780 | 2024-03-14 14:22:19 +0000 | [diff] [blame] | 696 | linked_layer_idx = derived_ctx->linked_layer_idx; |
| 697 | assert(linked_layer_idx < MAX_NUM_OF_LAYERS); |
| 698 | layer_ctx = &layer_ctx_array[linked_layer_idx]; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 699 | err = store_linked_component(layer_ctx, free_component_idx); |
| 700 | if (err != DPE_NO_ERROR) { |
| 701 | return err; |
| 702 | } |
| 703 | parent_layer_idx = layer_ctx->parent_layer_idx; |
| 704 | assert(parent_layer_idx < MAX_NUM_OF_LAYERS); |
| 705 | parent_layer_ctx = &layer_ctx_array[parent_layer_idx]; |
| 706 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 707 | if (create_certificate) { |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 708 | layer_ctx->is_cdi_to_be_exported = export_cdi; |
| 709 | |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 710 | /* Finalise the layer */ |
| 711 | layer_ctx->state = LAYER_STATE_FINALISED; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 712 | err = prepare_layer_certificate(layer_ctx, parent_layer_ctx); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 713 | if (err != DPE_NO_ERROR) { |
| 714 | return err; |
| 715 | } |
| 716 | |
| 717 | if (return_certificate) { |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 718 | /* Encode and return generated layer certificate */ |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 719 | err = encode_layer_certificate(layer_ctx, |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 720 | new_certificate_buf, |
| 721 | new_certificate_buf_size, |
| 722 | new_certificate_actual_size); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 723 | if (err != DPE_NO_ERROR) { |
| 724 | return err; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if (export_cdi) { |
| 730 | err = get_encoded_cdi_to_export(layer_ctx, |
| 731 | exported_cdi_buf, |
| 732 | exported_cdi_buf_size, |
| 733 | exported_cdi_actual_size); |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 734 | if (err != DPE_NO_ERROR) { |
| 735 | return err; |
| 736 | } |
| 737 | } |
Maulik Patel | 9a2a567 | 2024-03-14 13:43:58 +0000 | [diff] [blame] | 738 | log_derive_context_output_handles(*new_parent_context_handle, |
| 739 | *new_context_handle); |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 740 | |
Maulik Patel | 5ac8780 | 2024-03-14 14:22:19 +0000 | [diff] [blame] | 741 | /* Log context and layer info and certificate if no error */ |
| 742 | log_dpe_component_ctx_metadata(derived_ctx, free_component_idx); |
| 743 | log_dpe_layer_metadata(layer_ctx, linked_layer_idx); |
Jamie Fox | 4b8a6d6 | 2024-04-11 15:19:08 +0100 | [diff] [blame] | 744 | if (return_certificate) { |
Maulik Patel | 5ac8780 | 2024-03-14 14:22:19 +0000 | [diff] [blame] | 745 | log_intermediate_certificate(linked_layer_idx, |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 746 | new_certificate_buf, |
| 747 | *new_certificate_actual_size); |
Maulik Patel | 5ac8780 | 2024-03-14 14:22:19 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 750 | return DPE_NO_ERROR; |
| 751 | } |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 752 | |
| 753 | dpe_error_t destroy_context_request(int input_ctx_handle, |
| 754 | bool destroy_recursively) |
| 755 | { |
| 756 | uint16_t input_ctx_idx, linked_layer_idx; |
| 757 | int i; |
| 758 | bool is_layer_empty; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 759 | struct layer_context_t *layer_ctx; |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 760 | |
| 761 | log_destroy_context(input_ctx_handle, destroy_recursively); |
| 762 | |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 763 | /* Get component index and linked layer from the input handle */ |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 764 | input_ctx_idx = GET_IDX(input_ctx_handle); |
| 765 | |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 766 | /* Validate input handle */ |
| 767 | if (!is_input_handle_valid(input_ctx_handle)) { |
| 768 | return DPE_INVALID_ARGUMENT; |
| 769 | } |
| 770 | linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx; |
| 771 | |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 772 | #ifndef DPE_TEST_MODE |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 773 | if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) { |
| 774 | /* All layers till hypervisor cannot be destroyed dynamically */ |
| 775 | return DPE_INVALID_ARGUMENT; |
| 776 | } |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 777 | #endif /* !DPE_TEST_MODE */ |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 778 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 779 | assert(linked_layer_idx < MAX_NUM_OF_LAYERS); |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 780 | |
| 781 | if (!destroy_recursively) { |
| 782 | set_context_to_default(input_ctx_idx); |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 783 | layer_ctx = &layer_ctx_array[linked_layer_idx]; |
| 784 | remove_linked_component(layer_ctx, input_ctx_idx); |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 785 | } else { |
| 786 | //TODO: To be implemented |
| 787 | } |
| 788 | |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 789 | /* Close the layer if all of its contexts are destroyed */ |
| 790 | is_layer_empty = true; |
| 791 | for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) { |
| 792 | if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) { |
| 793 | /* There are active component context in the layer */ |
| 794 | is_layer_empty = false; |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | if (is_layer_empty) { |
| 800 | invalidate_layer(linked_layer_idx); |
| 801 | } |
| 802 | |
| 803 | return DPE_NO_ERROR; |
| 804 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 805 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 806 | struct component_context_t* get_component_ctx_ptr(uint16_t component_idx) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 807 | { |
| 808 | /* Safety case */ |
| 809 | if (component_idx >= MAX_NUM_OF_COMPONENTS) { |
| 810 | return NULL; |
| 811 | } |
| 812 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 813 | return &component_ctx_array[component_idx]; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 816 | struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx) |
| 817 | { |
| 818 | /* Safety case */ |
| 819 | if (layer_idx >= MAX_NUM_OF_LAYERS) { |
| 820 | return NULL; |
| 821 | } |
| 822 | |
| 823 | return &layer_ctx_array[layer_idx]; |
| 824 | } |
| 825 | |
| 826 | dpe_error_t certify_key_request(int input_ctx_handle, |
| 827 | bool retain_context, |
| 828 | const uint8_t *public_key, |
| 829 | size_t public_key_size, |
| 830 | const uint8_t *label, |
| 831 | size_t label_size, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 832 | uint8_t *certificate_buf, |
| 833 | size_t certificate_buf_size, |
| 834 | size_t *certificate_actual_size, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 835 | uint8_t *derived_public_key_buf, |
| 836 | size_t derived_public_key_buf_size, |
| 837 | size_t *derived_public_key_actual_size, |
| 838 | int *new_context_handle) |
| 839 | { |
| 840 | uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx; |
| 841 | dpe_error_t err; |
| 842 | psa_status_t status; |
| 843 | struct layer_context_t *parent_layer_ctx, *layer_ctx; |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 844 | struct layer_context_t leaf_layer; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 845 | |
| 846 | log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size, |
| 847 | label, label_size); |
| 848 | |
| 849 | /* Validate input handle */ |
| 850 | if (!is_input_handle_valid(input_ctx_handle)) { |
| 851 | return DPE_INVALID_ARGUMENT; |
| 852 | } |
| 853 | |
| 854 | if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) { |
| 855 | return DPE_INVALID_ARGUMENT; |
| 856 | } |
| 857 | |
| 858 | /* Get component index from the input handle */ |
| 859 | input_ctx_idx = GET_IDX(input_ctx_handle); |
| 860 | /* Get current linked layer idx */ |
| 861 | input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx; |
| 862 | assert(input_layer_idx < MAX_NUM_OF_LAYERS); |
| 863 | |
| 864 | layer_ctx = &layer_ctx_array[input_layer_idx]; |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 865 | /* Create leaf layer as copy of input context linked layer */ |
| 866 | memcpy(&leaf_layer, layer_ctx, sizeof(leaf_layer)); |
| 867 | |
| 868 | if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) { |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 869 | return DPE_INVALID_ARGUMENT; |
| 870 | } |
| 871 | |
| 872 | if ((public_key_size > 0) && (public_key != NULL)) { |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 873 | leaf_layer.is_external_pub_key_provided = true; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 874 | /* Copy the public key provided */ |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 875 | memcpy(&leaf_layer.data.attest_pub_key[0], |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 876 | public_key, |
| 877 | public_key_size); |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 878 | leaf_layer.data.attest_pub_key_len = public_key_size; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 879 | |
| 880 | /* If public key is provided, then provided label (if any) is ignored */ |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 881 | leaf_layer.data.external_key_deriv_label_len = 0; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 882 | |
| 883 | } else { |
| 884 | /* No external public key is provided */ |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 885 | leaf_layer.is_external_pub_key_provided = false; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 886 | |
| 887 | if ((label_size > 0) && (label != NULL)) { |
| 888 | /* Copy the label provided */ |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 889 | memcpy(&leaf_layer.data.external_key_deriv_label[0], |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 890 | label, |
| 891 | label_size); |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 892 | leaf_layer.data.external_key_deriv_label_len = label_size; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 893 | |
| 894 | } else { |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 895 | leaf_layer.data.external_key_deriv_label_len = 0; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 899 | /* Get parent layer derived public key to verify the certificate signature */ |
| 900 | parent_layer_idx = leaf_layer.parent_layer_idx; |
| 901 | assert(parent_layer_idx < MAX_NUM_OF_LAYERS); |
| 902 | parent_layer_ctx = &layer_ctx_array[parent_layer_idx]; |
| 903 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 904 | /* Correct layer should already be assigned in last call of |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 905 | * derive context command |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 906 | */ |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 907 | /* Create leaf certificate */ |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame^] | 908 | err = prepare_layer_certificate(&leaf_layer, parent_layer_ctx); |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 909 | if (err != DPE_NO_ERROR) { |
| 910 | return err; |
| 911 | } |
| 912 | |
Maulik Patel | 7cc8087 | 2024-04-04 12:00:29 +0100 | [diff] [blame] | 913 | err = encode_layer_certificate(&leaf_layer, |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 914 | certificate_buf, |
| 915 | certificate_buf_size, |
| 916 | certificate_actual_size); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 917 | if (err != DPE_NO_ERROR) { |
| 918 | return err; |
| 919 | } |
| 920 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 921 | if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) { |
| 922 | return DPE_INVALID_ARGUMENT; |
| 923 | } |
| 924 | |
| 925 | memcpy(derived_public_key_buf, |
| 926 | &parent_layer_ctx->data.attest_pub_key[0], |
| 927 | parent_layer_ctx->data.attest_pub_key_len); |
| 928 | *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len; |
| 929 | |
Maulik Patel | 91edd63 | 2024-02-26 07:44:41 +0000 | [diff] [blame] | 930 | /* Renew handle for the same context, if requested */ |
| 931 | if (retain_context) { |
| 932 | *new_context_handle = input_ctx_handle; |
| 933 | status = renew_nonce(new_context_handle); |
| 934 | if (status != PSA_SUCCESS) { |
| 935 | return DPE_INTERNAL_ERROR; |
| 936 | } |
| 937 | component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle); |
| 938 | |
| 939 | } else { |
| 940 | *new_context_handle = INVALID_HANDLE; |
| 941 | component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 942 | } |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 943 | |
Maulik Patel | 9a2a567 | 2024-03-14 13:43:58 +0000 | [diff] [blame] | 944 | log_certify_key_output_handle(*new_context_handle); |
Maulik Patel | 5ac8780 | 2024-03-14 14:22:19 +0000 | [diff] [blame] | 945 | log_intermediate_certificate(input_layer_idx, |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 946 | certificate_buf, |
| 947 | *certificate_actual_size); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 948 | |
| 949 | return DPE_NO_ERROR; |
| 950 | } |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 951 | |
| 952 | dpe_error_t get_certificate_chain_request(int input_ctx_handle, |
| 953 | bool retain_context, |
| 954 | bool clear_from_context, |
| 955 | uint8_t *certificate_chain_buf, |
| 956 | size_t certificate_chain_buf_size, |
| 957 | size_t *certificate_chain_actual_size, |
| 958 | int *new_context_handle) |
| 959 | { |
| 960 | dpe_error_t err; |
Maulik Patel | d280607 | 2024-02-02 10:30:08 +0000 | [diff] [blame] | 961 | uint16_t input_ctx_idx, input_layer_idx; |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 962 | psa_status_t status; |
| 963 | struct layer_context_t *layer_ctx; |
| 964 | |
Tamas Ban | a5e2f58 | 2024-01-25 16:59:26 +0100 | [diff] [blame] | 965 | log_get_certificate_chain(input_ctx_handle, retain_context, |
| 966 | clear_from_context, certificate_chain_buf_size); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 967 | |
| 968 | /* Validate input handle */ |
| 969 | if (!is_input_handle_valid(input_ctx_handle)) { |
| 970 | return DPE_INVALID_ARGUMENT; |
| 971 | } |
| 972 | |
| 973 | /* Get component index from the input handle */ |
| 974 | input_ctx_idx = GET_IDX(input_ctx_handle); |
| 975 | /* Get current linked layer idx */ |
| 976 | input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx; |
| 977 | assert(input_layer_idx < MAX_NUM_OF_LAYERS); |
| 978 | |
| 979 | layer_ctx = &layer_ctx_array[input_layer_idx]; |
Maulik Patel | f282097 | 2024-04-03 10:24:45 +0100 | [diff] [blame] | 980 | if (layer_ctx->state != LAYER_STATE_FINALISED) { |
| 981 | /* If the context has accumulated info and not yet part of a certificate, |
| 982 | * return an invalid-argument error |
| 983 | */ |
| 984 | return DPE_INVALID_ARGUMENT; |
| 985 | } |
| 986 | |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 987 | err = get_certificate_chain(layer_ctx, |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 988 | certificate_chain_buf, |
| 989 | certificate_chain_buf_size, |
| 990 | certificate_chain_actual_size); |
| 991 | if (err != DPE_NO_ERROR) { |
| 992 | return err; |
| 993 | } |
| 994 | |
| 995 | log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size); |
| 996 | |
| 997 | /* Renew handle for the same context, if requested */ |
| 998 | if (retain_context) { |
| 999 | *new_context_handle = input_ctx_handle; |
| 1000 | status = renew_nonce(new_context_handle); |
| 1001 | if (status != PSA_SUCCESS) { |
| 1002 | return DPE_INTERNAL_ERROR; |
| 1003 | } |
| 1004 | component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle); |
| 1005 | |
| 1006 | if (clear_from_context) { |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 1007 | //TODO: Reimplement the clear_from_context functionality after memory |
| 1008 | // optimization; Certificates are not ready made and they are not |
| 1009 | // stored in the layer context anymore. They are created on-the-fly |
| 1010 | // when requested. Add a test as well. |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | } else { |
| 1014 | *new_context_handle = INVALID_HANDLE; |
| 1015 | component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE; |
| 1016 | } |
Maulik Patel | 9a2a567 | 2024-03-14 13:43:58 +0000 | [diff] [blame] | 1017 | log_get_certificate_chain_output_handle(*new_context_handle); |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 1018 | |
| 1019 | return DPE_NO_ERROR; |
| 1020 | } |