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