blob: f6f328db983f516e180171863e70d8674eb1f6eb [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
Tamas Bana5e2f582024-01-25 16:59:26 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Maulik Patelad2f3db2023-05-17 15:41:36 +01003 *
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 Patel009450d2024-04-23 12:03:10 +010011#include "array.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010012#include "dice_protection_environment.h"
Maulik Patel2358bbb2023-07-21 10:56:56 +010013#include "dpe_certificate.h"
Maulik Patelcb14cde2024-01-23 12:39:53 +000014#include "dpe_client.h"
Maulik Patel58595d32023-06-22 10:08:53 +010015#include "dpe_crypto_interface.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010016#include "dpe_log.h"
Jamie Fox34681992023-09-04 18:14:06 +010017#include "dpe_plat.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010018#include "psa/crypto.h"
19
Maulik Patel58595d32023-06-22 10:08:53 +010020#define CONTEXT_DATA_MAX_SIZE sizeof(struct component_context_data_t)
21
Maulik Patelad2f3db2023-05-17 15:41:36 +010022static struct component_context_t component_ctx_array[MAX_NUM_OF_COMPONENTS];
Maulik Patel97a61fe2024-07-01 15:55:04 +010023static struct cert_context_t cert_ctx_array[MAX_NUM_OF_CERTIFICATES];
Maulik Patelad2f3db2023-05-17 15:41:36 +010024
Maulik Patel97a61fe2024-07-01 15:55:04 +010025static dpe_error_t store_linked_component(struct cert_context_t *cert_ctx,
Maulik Patel009450d2024-04-23 12:03:10 +010026 int component_idx)
27{
Maulik Patel97a61fe2024-07-01 15:55:04 +010028 if (cert_ctx->linked_components.count >=
29 ARRAY_SIZE(cert_ctx->linked_components.idx)) {
Maulik Patel009450d2024-04-23 12:03:10 +010030 /* linked_components.idx[] is full */
31 return DPE_INSUFFICIENT_MEMORY;
32 }
33
Maulik Patel97a61fe2024-07-01 15:55:04 +010034 cert_ctx->linked_components.idx[cert_ctx->linked_components.count] = component_idx;
35 cert_ctx->linked_components.count++;
Maulik Patel009450d2024-04-23 12:03:10 +010036
37 return DPE_NO_ERROR;
38}
39
Maulik Patel97a61fe2024-07-01 15:55:04 +010040static void remove_linked_component(struct cert_context_t *cert_ctx,
Maulik Patel009450d2024-04-23 12:03:10 +010041 int component_idx)
42{
43 int i, pos;
44
45 /* Find the position of the input component */
Maulik Patel97a61fe2024-07-01 15:55:04 +010046 for (i = 0; i < ARRAY_SIZE(cert_ctx->linked_components.idx); i++) {
47 if (cert_ctx->linked_components.idx[i] == component_idx) {
Maulik Patel009450d2024-04-23 12:03:10 +010048 pos = i;
49 break;
50 }
51 }
52
Maulik Patel97a61fe2024-07-01 15:55:04 +010053 assert(i < ARRAY_SIZE(cert_ctx->linked_components.idx));
Maulik Patel009450d2024-04-23 12:03:10 +010054
55 /* Left shift remaining elements by 1 from current position */
Maulik Patel97a61fe2024-07-01 15:55:04 +010056 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 Patel009450d2024-04-23 12:03:10 +010058 }
Maulik Patel97a61fe2024-07-01 15:55:04 +010059 cert_ctx->linked_components.idx[i] = INVALID_CERT_CTX_IDX;
60 cert_ctx->linked_components.count--;
Maulik Patel009450d2024-04-23 12:03:10 +010061}
62
Maulik Patelad2f3db2023-05-17 15:41:36 +010063static 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 Patelad2f3db2023-05-17 15:41:36 +010081static 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 Patelad2f3db2023-05-17 15:41:36 +010094static void set_context_to_default(int i)
95{
96 component_ctx_array[i].in_use = false;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000097 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 Patelad2f3db2023-05-17 15:41:36 +0100102 component_ctx_array[i].nonce = INVALID_NONCE_VALUE;
103 component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100104 component_ctx_array[i].linked_cert_ctx_idx = INVALID_CERT_CTX_IDX;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100105 (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t));
Maulik Patelacc3f4a2024-03-25 18:34:05 +0000106 component_ctx_array[i].target_locality = DEFAULT_TARGET_LOCALITY;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100107 /* Allow component to be derived by default */
108}
109
Maulik Patel97a61fe2024-07-01 15:55:04 +0100110static void initialise_certificate_context(int i)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100111{
Maulik Patel009450d2024-04-23 12:03:10 +0100112 int j;
113
Maulik Patel97a61fe2024-07-01 15:55:04 +0100114 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 Patel009450d2024-04-23 12:03:10 +0100128 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100129}
130
Maulik Patel97a61fe2024-07-01 15:55:04 +0100131static void free_certificate_context(int i)
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100132{
Maulik Patel97a61fe2024-07-01 15:55:04 +0100133 destroy_certificate_context_keys(&cert_ctx_array[i]);
134 initialise_certificate_context(i);
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100135}
136
Maulik Patelad2f3db2023-05-17 15:41:36 +0100137static 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 Patel2358bbb2023-07-21 10:56:56 +0100172 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100173 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
190static 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
201static 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 Patel58595d32023-06-22 10:08:53 +0100218/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
219 * same order
220 */
221static 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 Patelad2f3db2023-05-17 15:41:36 +0100225{
Maulik Patel58595d32023-06-22 10:08:53 +0100226 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 Patel97a61fe2024-07-01 15:55:04 +0100253static psa_status_t compute_attestation_cdi_input(struct cert_context_t *cert_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100254{
255 psa_status_t status;
256 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
257 size_t ctx_data_size, hash_len;
Maulik Patel009450d2024-04-23 12:03:10 +0100258 int i, idx;
259 uint16_t num_of_linked_components;
260
Maulik Patel97a61fe2024-07-01 15:55:04 +0100261 num_of_linked_components = cert_ctx->linked_components.count;
Maulik Patel009450d2024-04-23 12:03:10 +0100262 if (num_of_linked_components == 0) {
263 /* No components to hash */
264 return PSA_SUCCESS;
265 }
Maulik Patel58595d32023-06-22 10:08:53 +0100266
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 Patel97a61fe2024-07-01 15:55:04 +0100276 * concatenates the data of all SW components which belong to the same
277 * certificate and hash it.
Maulik Patel58595d32023-06-22 10:08:53 +0100278 */
Maulik Patel009450d2024-04-23 12:03:10 +0100279 for (i = 0; i < num_of_linked_components; i++) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100280 idx = cert_ctx->linked_components.idx[i];
Maulik Patel009450d2024-04-23 12:03:10 +0100281 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 Patel58595d32023-06-22 10:08:53 +0100288
Maulik Patel009450d2024-04-23 12:03:10 +0100289 status = psa_hash_update(&hash_op,
290 component_ctx_data,
291 ctx_data_size);
292 if (status != PSA_SUCCESS) {
293 return status;
Maulik Patel58595d32023-06-22 10:08:53 +0100294 }
295 }
296
297 status = psa_hash_finish(&hash_op,
Maulik Patel97a61fe2024-07-01 15:55:04 +0100298 &cert_ctx->attest_cdi_hash_input[0],
299 sizeof(cert_ctx->attest_cdi_hash_input),
Maulik Patel58595d32023-06-22 10:08:53 +0100300 &hash_len);
301
302 assert(hash_len == DPE_HASH_ALG_SIZE);
303
304 return status;
305}
306
Maulik Patel97a61fe2024-07-01 15:55:04 +0100307static dpe_error_t get_encoded_cdi_to_export(struct cert_context_t *cert_ctx,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000308 uint8_t *exported_cdi_buf,
309 size_t exported_cdi_buf_size,
310 size_t *exported_cdi_actual_size)
311{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100312 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
313 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000314 psa_status_t status;
315 dpe_error_t err;
316
Tamas Ban5179a4d2024-01-25 17:05:30 +0100317 /* Get CDIs value */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100318 status = get_certificate_cdi_value(cert_ctx,
319 cdi_attest_buf,
320 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000321 if (status != PSA_SUCCESS) {
322 return DPE_INTERNAL_ERROR;
323 }
324
325 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100326 err = encode_cdi(cdi_attest_buf,
327 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000328 exported_cdi_buf,
329 exported_cdi_buf_size,
330 exported_cdi_actual_size);
331 if (err != DPE_NO_ERROR) {
332 return err;
333 }
Maulik Patel97a61fe2024-07-01 15:55:04 +0100334 cert_ctx->is_cdi_to_be_exported = true;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000335
336 return DPE_NO_ERROR;
337}
338
Maulik Patel97a61fe2024-07-01 15:55:04 +0100339static dpe_error_t prepare_certificate(struct cert_context_t *cert_ctx,
340 const struct cert_context_t *parent_cert_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100341{
Maulik Patel58595d32023-06-22 10:08:53 +0100342 psa_status_t status;
Maulik Patel58595d32023-06-22 10:08:53 +0100343
Maulik Patel97a61fe2024-07-01 15:55:04 +0100344 /* 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 Patel58595d32023-06-22 10:08:53 +0100347
Maulik Patel97a61fe2024-07-01 15:55:04 +0100348 /* Except for RoT certificate with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100349
Maulik Patel97a61fe2024-07-01 15:55:04 +0100350 status = compute_attestation_cdi_input(cert_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100351 if (status != PSA_SUCCESS) {
352 return DPE_INTERNAL_ERROR;
353 }
354
Maulik Patel97a61fe2024-07-01 15:55:04 +0100355 status = derive_attestation_cdi(cert_ctx, parent_cert_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100356 if (status != PSA_SUCCESS) {
357 return DPE_INTERNAL_ERROR;
358 }
359
Maulik Patel97a61fe2024-07-01 15:55:04 +0100360 status = derive_sealing_cdi(cert_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100361 if (status != PSA_SUCCESS) {
362 return DPE_INTERNAL_ERROR;
363 }
364 }
365
Maulik Patel97a61fe2024-07-01 15:55:04 +0100366 status = derive_wrapping_key(cert_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100367 if (status != PSA_SUCCESS) {
368 return DPE_INTERNAL_ERROR;
369 }
370
Maulik Patel97a61fe2024-07-01 15:55:04 +0100371 if (!cert_ctx->is_external_pub_key_provided) {
372 status = derive_attestation_key(cert_ctx);
Maulik Patele6adc112023-08-18 14:21:51 +0100373 if (status != PSA_SUCCESS) {
374 return DPE_INTERNAL_ERROR;
375 }
Maulik Patel58595d32023-06-22 10:08:53 +0100376 }
377
Maulik Patel97a61fe2024-07-01 15:55:04 +0100378 status = derive_id_from_public_key(cert_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100379 if (status != PSA_SUCCESS) {
380 return DPE_INTERNAL_ERROR;
381 }
382
Tamas Ban257471b2024-03-25 13:49:53 +0100383 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100384}
385
Maulik Patel97a61fe2024-07-01 15:55:04 +0100386static uint16_t assign_new_certificate_context(void)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100387{
388 int i;
389
Maulik Patel97a61fe2024-07-01 15:55:04 +0100390 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 Patelad2f3db2023-05-17 15:41:36 +0100393 return i;
394 }
395 }
396
Maulik Patel97a61fe2024-07-01 15:55:04 +0100397 return MAX_NUM_OF_CERTIFICATES - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100398}
399
Maulik Patel9b13e162024-04-24 14:18:17 +0100400static bool is_client_authorised(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100401{
Maulik Patel9b13e162024-04-24 14:18:17 +0100402 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 Patelad2f3db2023-05-17 15:41:36 +0100412}
413
Maulik Patel97a61fe2024-07-01 15:55:04 +0100414static bool is_cert_id_used(uint32_t cert_id, uint16_t *cert_ctx_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100415{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000416 int i;
417
Maulik Patel97a61fe2024-07-01 15:55:04 +0100418 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 Patelcb14cde2024-01-23 12:39:53 +0000421 return true;
422 }
423 }
424
425 /* No certificate ID match found */
426 return false;
427}
428
Maulik Patel97a61fe2024-07-01 15:55:04 +0100429static dpe_error_t assign_certificate_to_component(struct component_context_t *new_ctx,
430 uint32_t cert_id)
Maulik Patelcb14cde2024-01-23 12:39:53 +0000431{
Maulik Patel97a61fe2024-07-01 15:55:04 +0100432 uint16_t parent_cert_ctx_idx, cert_ctx_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100433
434 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
435
Maulik Patel97a61fe2024-07-01 15:55:04 +0100436 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 Patelad2f3db2023-05-17 15:41:36 +0100438
Maulik Patelcb14cde2024-01-23 12:39:53 +0000439 if (cert_id != DPE_CERT_ID_INVALID) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200440 /* Cert_id was sent by the client */
Maulik Patelcb14cde2024-01-23 12:39:53 +0000441 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100442 if (cert_ctx_array[parent_cert_ctx_idx].state == CERT_CTX_FINALISED) {
443 /* Cannot add to the certificate context which is already finalised */
Maulik Patelcb14cde2024-01-23 12:39:53 +0000444 return DPE_INTERNAL_ERROR;
445 }
446 /* Derived context belongs to the same certificate as its parent component */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100447 new_ctx->linked_cert_ctx_idx = parent_cert_ctx_idx;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000448
Maulik Patel97a61fe2024-07-01 15:55:04 +0100449 } 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 Ban33f1aec2024-06-04 12:15:18 +0200452 */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100453 assert(cert_ctx_array[cert_ctx_idx_to_link].state != CERT_CTX_FINALISED);
Tamas Ban33f1aec2024-06-04 12:15:18 +0200454
Maulik Patel97a61fe2024-07-01 15:55:04 +0100455 /* 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 Patelcb14cde2024-01-23 12:39:53 +0000458
459 } else {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100460 /* 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 Patelcb14cde2024-01-23 12:39:53 +0000463 return DPE_INTERNAL_ERROR;
464 }
Maulik Patel97a61fe2024-07-01 15:55:04 +0100465 /* 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 Patel2358bbb2023-07-21 10:56:56 +0100470 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100471
472 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000473 /* cert id was not sent by the client */
474 //TODO: To be implemented; return error for now.
475 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100476 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100477
478 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100479}
480
Jamie Fox34681992023-09-04 18:14:06 +0100481/**
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 */
488static dpe_error_t create_rot_context(int *rot_ctx_handle)
489{
Jamie Fox34681992023-09-04 18:14:06 +0100490 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
Maulik Patel97a61fe2024-07-01 15:55:04 +0100491 struct cert_context_t *rot_cert_ctx = &cert_ctx_array[DPE_ROT_CERT_CTX_IDX];
Jamie Fox34681992023-09-04 18:14:06 +0100492
Maulik Patel97a61fe2024-07-01 15:55:04 +0100493 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 Patela81605b2023-10-24 12:17:03 +0100498 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100499 rot_comp_ctx->nonce = 0;
Maulik Patelacc3f4a2024-03-25 18:34:05 +0000500 /* Set the target locality for RoT context */
501 rot_comp_ctx->target_locality = LOCALITY_RSE_S;
Maulik Patela81605b2023-10-24 12:17:03 +0100502 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100503 rot_comp_ctx->parent_idx = 0;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100504 /* Link context to RoT certificate */
505 rot_comp_ctx->linked_cert_ctx_idx = DPE_ROT_CERT_CTX_IDX;
Jamie Fox34681992023-09-04 18:14:06 +0100506 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100507 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
508
509 return DPE_NO_ERROR;
510}
511
512dpe_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 Patel97a61fe2024-07-01 15:55:04 +0100520 for (i = 0; i < MAX_NUM_OF_CERTIFICATES; i++) {
521 initialise_certificate_context(i);
Jamie Fox34681992023-09-04 18:14:06 +0100522 }
523
524 return create_rot_context(rot_ctx_handle);
525}
526
Maulik Patel97a61fe2024-07-01 15:55:04 +0100527static void free_certificate_context_if_empty(uint16_t cert_ctx_idx)
Maulik Patel9a893122024-04-15 13:48:38 +0100528{
Maulik Patel97a61fe2024-07-01 15:55:04 +0100529 if (cert_ctx_array[cert_ctx_idx].linked_components.count == 0) {
530 free_certificate_context(cert_ctx_idx);
Maulik Patel9a893122024-04-15 13:48:38 +0100531 }
532}
533
Maulik Patela81605b2023-10-24 12:17:03 +0100534dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000535 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100536 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 Patel9fd8bd22023-10-30 10:58:30 +0000541 int32_t target_locality,
542 bool return_certificate,
543 bool allow_new_context_to_export,
544 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100545 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000546 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 Patelad2f3db2023-05-17 15:41:36 +0100553{
Maulik Patel58595d32023-06-22 10:08:53 +0100554 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100555 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100556 uint16_t parent_ctx_idx, linked_cert_ctx_idx, parent_cert_ctx_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100557 int free_component_idx;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100558 struct cert_context_t *cert_ctx, *parent_cert_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100559
Maulik Patelcb14cde2024-01-23 12:39:53 +0000560 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100561 allow_new_context_to_derive, create_certificate, dice_inputs,
562 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100563
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000564 if (export_cdi && !create_certificate) {
565 return DPE_INVALID_ARGUMENT;
566 }
567
Maulik Patelad2f3db2023-05-17 15:41:36 +0100568 /* 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 Patela81605b2023-10-24 12:17:03 +0100577 /* Get parent component index from the input handle */
578 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100579
580 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100581 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100582 */
Maulik Patela81605b2023-10-24 12:17:03 +0100583 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100584
Maulik Patela81605b2023-10-24 12:17:03 +0100585 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100586
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000587 /* Check if parent context is allowed to derive */
588 if (!parent_ctx->is_allowed_to_derive) {
589 return DPE_INVALID_ARGUMENT;
590 }
591
Maulik Patel9b13e162024-04-24 14:18:17 +0100592 if (!is_client_authorised(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100593 return DPE_INVALID_ARGUMENT;
594 }
595
Maulik Patela81605b2023-10-24 12:17:03 +0100596 /* 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 Patel9fd8bd22023-10-30 10:58:30 +0000603 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 Patela81605b2023-10-24 12:17:03 +0100616 /* Copy dice input to the new derived component context */
617 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100618 if (err != DPE_NO_ERROR) {
619 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100620 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000621 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100622
Maulik Patela81605b2023-10-24 12:17:03 +0100623 /* 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 Pateld2806072024-02-02 10:30:08 +0000627 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100628 /* Assign certificate to the component */
629 err = assign_certificate_to_component(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100630 if (err != DPE_NO_ERROR) {
631 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100632 }
633
Maulik Patel97a61fe2024-07-01 15:55:04 +0100634 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 Patel9a893122024-04-15 13:48:38 +0100638 if (err != DPE_NO_ERROR) {
639 goto clean_up_and_exit;
640 }
Maulik Patel97a61fe2024-07-01 15:55:04 +0100641 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 Patel9a893122024-04-15 13:48:38 +0100644
645 if (create_certificate) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100646 cert_ctx->is_cdi_to_be_exported = export_cdi;
Maulik Patel9a893122024-04-15 13:48:38 +0100647
Maulik Patel97a61fe2024-07-01 15:55:04 +0100648 /* 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 Patel9a893122024-04-15 13:48:38 +0100652 if (err != DPE_NO_ERROR) {
653 goto clean_up_and_exit;
654 }
655
656 if (return_certificate) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100657 /* 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 Patel9a893122024-04-15 13:48:38 +0100662 if (err != DPE_NO_ERROR) {
663 goto clean_up_and_exit;
664 }
665 }
666 }
667
668 if (export_cdi) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100669 err = get_encoded_cdi_to_export(cert_ctx,
Maulik Patel9a893122024-04-15 13:48:38 +0100670 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 Patelad2f3db2023-05-17 15:41:36 +0100678 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100679 /* 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 Fox34681992023-09-04 18:14:06 +0100682 if (err != DPE_NO_ERROR) {
Maulik Patel9a893122024-04-15 13:48:38 +0100683 goto clean_up_and_exit;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100684 }
Maulik Patela81605b2023-10-24 12:17:03 +0100685 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
686
Maulik Patelad2f3db2023-05-17 15:41:36 +0100687 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100688 /* Return invalid handle */
689 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100690 parent_ctx->nonce = INVALID_NONCE_VALUE;
691 }
692
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000693 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100694 /* 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 Patel9a2a5672024-03-14 13:43:58 +0000709 log_derive_context_output_handles(*new_parent_context_handle,
710 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100711
Maulik Patel97a61fe2024-07-01 15:55:04 +0100712 /* Log component context, certificate context & certificate if no error */
Maulik Patel5ac87802024-03-14 14:22:19 +0000713 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
Maulik Patel97a61fe2024-07-01 15:55:04 +0100714 log_dpe_cert_ctx_metadata(cert_ctx, linked_cert_ctx_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100715 if (return_certificate) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100716 log_intermediate_certificate(linked_cert_ctx_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100717 new_certificate_buf,
718 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000719 }
720
Maulik Patelad2f3db2023-05-17 15:41:36 +0100721 return DPE_NO_ERROR;
Maulik Patel9a893122024-04-15 13:48:38 +0100722
723clean_up_and_exit:
724 set_context_to_default(free_component_idx);
Maulik Patel97a61fe2024-07-01 15:55:04 +0100725 free_certificate_context_if_empty(linked_cert_ctx_idx);
Maulik Patel9a893122024-04-15 13:48:38 +0100726
727 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100728}
Maulik Patel54d65f72023-06-28 13:04:36 +0100729
730dpe_error_t destroy_context_request(int input_ctx_handle,
731 bool destroy_recursively)
732{
Maulik Patel97a61fe2024-07-01 15:55:04 +0100733 uint16_t input_ctx_idx, linked_cert_ctx_idx;
734 struct cert_context_t *cert_ctx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100735
736 log_destroy_context(input_ctx_handle, destroy_recursively);
737
Maulik Patel97a61fe2024-07-01 15:55:04 +0100738 /* Get component index and linked certificate context from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100739 input_ctx_idx = GET_IDX(input_ctx_handle);
740
Maulik Patel54d65f72023-06-28 13:04:36 +0100741 /* Validate input handle */
742 if (!is_input_handle_valid(input_ctx_handle)) {
743 return DPE_INVALID_ARGUMENT;
744 }
Maulik Patel97a61fe2024-07-01 15:55:04 +0100745 linked_cert_ctx_idx = component_ctx_array[input_ctx_idx].linked_cert_ctx_idx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100746
Jamie Fox34681992023-09-04 18:14:06 +0100747#ifndef DPE_TEST_MODE
Maulik Patel97a61fe2024-07-01 15:55:04 +0100748 if (linked_cert_ctx_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_CERT_CTX_IDX) {
749 /* All certificate contexts till hypervisor cannot be destroyed dynamically */
Maulik Patel54d65f72023-06-28 13:04:36 +0100750 return DPE_INVALID_ARGUMENT;
751 }
Jamie Fox34681992023-09-04 18:14:06 +0100752#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100753
Maulik Patel97a61fe2024-07-01 15:55:04 +0100754 assert(linked_cert_ctx_idx < MAX_NUM_OF_CERTIFICATES);
Maulik Patel54d65f72023-06-28 13:04:36 +0100755
756 if (!destroy_recursively) {
757 set_context_to_default(input_ctx_idx);
Maulik Patel97a61fe2024-07-01 15:55:04 +0100758 cert_ctx = &cert_ctx_array[linked_cert_ctx_idx];
759 remove_linked_component(cert_ctx, input_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100760 } else {
761 //TODO: To be implemented
762 }
763
Maulik Patel97a61fe2024-07-01 15:55:04 +0100764 /* Free the certificate context if all of its components are destroyed */
765 free_certificate_context_if_empty(linked_cert_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100766
767 return DPE_NO_ERROR;
768}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100769
Maulik Patel009450d2024-04-23 12:03:10 +0100770struct component_context_t* get_component_ctx_ptr(uint16_t component_idx)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100771{
772 /* Safety case */
773 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
774 return NULL;
775 }
776
Maulik Patel009450d2024-04-23 12:03:10 +0100777 return &component_ctx_array[component_idx];
Maulik Patel2358bbb2023-07-21 10:56:56 +0100778}
779
Maulik Patel97a61fe2024-07-01 15:55:04 +0100780struct cert_context_t* get_cert_ctx_ptr(uint16_t cert_ctx_idx)
Maulik Patele6adc112023-08-18 14:21:51 +0100781{
782 /* Safety case */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100783 if (cert_ctx_idx >= MAX_NUM_OF_CERTIFICATES) {
Maulik Patele6adc112023-08-18 14:21:51 +0100784 return NULL;
785 }
786
Maulik Patel97a61fe2024-07-01 15:55:04 +0100787 return &cert_ctx_array[cert_ctx_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100788}
789
790dpe_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 Patelcbded682023-12-07 11:50:16 +0000796 uint8_t *certificate_buf,
797 size_t certificate_buf_size,
798 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100799 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 Patel97a61fe2024-07-01 15:55:04 +0100804 uint16_t input_ctx_idx, input_cert_ctx_idx, parent_cert_ctx_idx;
Maulik Patele6adc112023-08-18 14:21:51 +0100805 dpe_error_t err;
806 psa_status_t status;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100807 struct cert_context_t *parent_cert_ctx, *input_cert_ctx;
808 struct cert_context_t leaf_cert_ctx = {0};
Maulik Patele6adc112023-08-18 14:21:51 +0100809
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 Patel97a61fe2024-07-01 15:55:04 +0100824 /* 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 Patele6adc112023-08-18 14:21:51 +0100828
Maulik Patel97a61fe2024-07-01 15:55:04 +0100829 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 Baneb8d7f12024-04-03 13:55:22 +0200834 /* Linked components count already initialised to 0 */
835
836 } else {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100837 /* Input certificate context is not finalised,
838 * new leaf certificate context share the same components as in the
839 * input certificate context
Tamas Baneb8d7f12024-04-03 13:55:22 +0200840 */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100841 memcpy(&leaf_cert_ctx.linked_components, &input_cert_ctx->linked_components,
842 sizeof(input_cert_ctx->linked_components));
Tamas Baneb8d7f12024-04-03 13:55:22 +0200843 }
Maulik Patel7cc80872024-04-04 12:00:29 +0100844
Maulik Patel97a61fe2024-07-01 15:55:04 +0100845 if (public_key_size > sizeof(leaf_cert_ctx.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100846 return DPE_INVALID_ARGUMENT;
847 }
848
849 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100850 leaf_cert_ctx.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100851 /* Copy the public key provided */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100852 memcpy(&leaf_cert_ctx.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100853 public_key,
854 public_key_size);
Maulik Patel97a61fe2024-07-01 15:55:04 +0100855 leaf_cert_ctx.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100856
857 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100858 leaf_cert_ctx.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100859
860 } else {
861 /* No external public key is provided */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100862 leaf_cert_ctx.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100863
864 if ((label_size > 0) && (label != NULL)) {
865 /* Copy the label provided */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100866 memcpy(&leaf_cert_ctx.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100867 label,
868 label_size);
Maulik Patel97a61fe2024-07-01 15:55:04 +0100869 leaf_cert_ctx.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100870
871 } else {
Maulik Patel97a61fe2024-07-01 15:55:04 +0100872 leaf_cert_ctx.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100873 }
874 }
875
Maulik Patel97a61fe2024-07-01 15:55:04 +0100876 /* 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 Patel009450d2024-04-23 12:03:10 +0100880
Maulik Patel97a61fe2024-07-01 15:55:04 +0100881 /* Correct certificate context should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100882 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100883 */
Maulik Patelcbded682023-12-07 11:50:16 +0000884 /* Create leaf certificate */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100885 err = prepare_certificate(&leaf_cert_ctx, parent_cert_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100886 if (err != DPE_NO_ERROR) {
887 return err;
888 }
889
Maulik Patel97a61fe2024-07-01 15:55:04 +0100890 err = encode_certificate(&leaf_cert_ctx,
Tamas Ban257471b2024-03-25 13:49:53 +0100891 certificate_buf,
892 certificate_buf_size,
893 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100894 if (err != DPE_NO_ERROR) {
895 return err;
896 }
897
Maulik Patel97a61fe2024-07-01 15:55:04 +0100898 if (derived_public_key_buf_size < sizeof(parent_cert_ctx->data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100899 return DPE_INVALID_ARGUMENT;
900 }
901
902 memcpy(derived_public_key_buf,
Maulik Patel97a61fe2024-07-01 15:55:04 +0100903 &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 Patele6adc112023-08-18 14:21:51 +0100906
Maulik Patel91edd632024-02-26 07:44:41 +0000907 /* 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 Patele6adc112023-08-18 14:21:51 +0100919 }
Maulik Patele6adc112023-08-18 14:21:51 +0100920
Maulik Patel9a2a5672024-03-14 13:43:58 +0000921 log_certify_key_output_handle(*new_context_handle);
Maulik Patel97a61fe2024-07-01 15:55:04 +0100922 log_intermediate_certificate(input_cert_ctx_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100923 certificate_buf,
924 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100925
Maulik Patel97a61fe2024-07-01 15:55:04 +0100926 destroy_certificate_context_keys(&leaf_cert_ctx);
Tamas Baneb8d7f12024-04-03 13:55:22 +0200927
Maulik Patele6adc112023-08-18 14:21:51 +0100928 return DPE_NO_ERROR;
929}
Maulik Patel83a6b592023-12-05 15:20:30 +0000930
931dpe_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 Patel97a61fe2024-07-01 15:55:04 +0100940 uint16_t input_ctx_idx, input_cert_ctx_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000941 psa_status_t status;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100942 struct cert_context_t *cert_ctx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000943
Tamas Bana5e2f582024-01-25 16:59:26 +0100944 log_get_certificate_chain(input_ctx_handle, retain_context,
945 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000946
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 Patel97a61fe2024-07-01 15:55:04 +0100954 /* 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 Patel83a6b592023-12-05 15:20:30 +0000957
Maulik Patel97a61fe2024-07-01 15:55:04 +0100958 cert_ctx = &cert_ctx_array[input_cert_ctx_idx];
959 if (cert_ctx->state != CERT_CTX_FINALISED) {
Maulik Patelf2820972024-04-03 10:24:45 +0100960 /* 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 Patel97a61fe2024-07-01 15:55:04 +0100966 err = get_certificate_chain(cert_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000967 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 Ban257471b2024-03-25 13:49:53 +0100986 //TODO: Reimplement the clear_from_context functionality after memory
987 // optimization; Certificates are not ready made and they are not
Maulik Patel97a61fe2024-07-01 15:55:04 +0100988 // stored in the certificate context anymore. They are created on-the-fly
Tamas Ban257471b2024-03-25 13:49:53 +0100989 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000990 }
991
992 } else {
993 *new_context_handle = INVALID_HANDLE;
994 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
995 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000996 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000997
998 return DPE_NO_ERROR;
999}