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