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