blob: bebe77959b23791b1e5a7069c60ce28119080d04 [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>
11#include "dice_protection_environment.h"
Maulik Patel2358bbb2023-07-21 10:56:56 +010012#include "dpe_certificate.h"
Maulik Patelcb14cde2024-01-23 12:39:53 +000013#include "dpe_client.h"
Maulik Patel58595d32023-06-22 10:08:53 +010014#include "dpe_crypto_interface.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010015#include "dpe_log.h"
Jamie Fox34681992023-09-04 18:14:06 +010016#include "dpe_plat.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010017#include "psa/crypto.h"
18
Maulik Pateldbfd5152023-05-30 17:02:42 +010019#ifdef DPE_TEST_MODE
20#define TEST_ROT_CDI_VAL { \
21 0xD2, 0x90, 0x66, 0x07, 0x2A, 0x2D, 0x2A, 0x00, \
22 0x91, 0x9D, 0xD9, 0x15, 0x14, 0xBE, 0x2D, 0xCC, \
23 0xA3, 0x9F, 0xDE, 0xC3, 0x35, 0x75, 0x84, 0x6E, \
24 0x4C, 0xB9, 0x28, 0xAC, 0x7A, 0x4E, 0X00, 0x7F \
25 }
26#endif /* DPE_TEST_MODE */
27
Maulik Patel58595d32023-06-22 10:08:53 +010028#define CONTEXT_DATA_MAX_SIZE sizeof(struct component_context_data_t)
29
Maulik Patelad2f3db2023-05-17 15:41:36 +010030static struct component_context_t component_ctx_array[MAX_NUM_OF_COMPONENTS];
31static struct layer_context_t layer_ctx_array[MAX_NUM_OF_LAYERS];
32
33static int get_free_component_context_index(void)
34{
35 int i;
36
37 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
38 if (!component_ctx_array[i].in_use) {
39 break;
40 }
41 }
42
43 if (i >= MAX_NUM_OF_COMPONENTS) {
44 /* No free index left in the array -- all used up! */
45 return -1;
46 }
47
48 return i;
49}
50
Maulik Patelad2f3db2023-05-17 15:41:36 +010051static dpe_error_t renew_nonce(int *handle)
52{
53 uint16_t nonce;
54
55 psa_status_t status = psa_generate_random((uint8_t *)&nonce, sizeof(nonce));
56 if (status != PSA_SUCCESS) {
57 return DPE_INTERNAL_ERROR;
58 }
59 *handle = SET_NONCE(*handle, nonce);
60
61 return DPE_NO_ERROR;
62}
63
Maulik Patelad2f3db2023-05-17 15:41:36 +010064static void set_context_to_default(int i)
65{
66 component_ctx_array[i].in_use = false;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000067 component_ctx_array[i].is_allowed_to_derive = true;
68 /* export CDI attribute is inherited and once disabled, a derived context
69 * and subsequent derivations cannot export CDI, hence enable by default
70 */
71 component_ctx_array[i].is_export_cdi_allowed = true;
Maulik Patelad2f3db2023-05-17 15:41:36 +010072 component_ctx_array[i].nonce = INVALID_NONCE_VALUE;
73 component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX;
74 component_ctx_array[i].linked_layer_idx = INVALID_LAYER_IDX;
75 (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t));
76 //TODO: Question: how to initialise MHU Id mapping?
Maulik Patel9fd8bd22023-10-30 10:58:30 +000077 component_ctx_array[i].target_locality = 0;
Maulik Patelad2f3db2023-05-17 15:41:36 +010078 /* Allow component to be derived by default */
79}
80
81static void invalidate_layer(int i)
82{
Maulik Patelaa6b24f2024-04-05 15:13:08 +010083 layer_ctx_array[i].idx = i;
Maulik Patelad2f3db2023-05-17 15:41:36 +010084 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
85 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000086 layer_ctx_array[i].is_cdi_to_be_exported = false;
Maulik Patelcb14cde2024-01-23 12:39:53 +000087 layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID;
Maulik Patele6adc112023-08-18 14:21:51 +010088 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
89 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patel58595d32023-06-22 10:08:53 +010090 (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id);
91 (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +010092 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
93}
94
Maulik Patelad2f3db2023-05-17 15:41:36 +010095static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
96 const DiceInputValues *dice_inputs)
97{
98 size_t hash_len;
99 psa_status_t status;
100
101 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
102 DICE_HASH_SIZE);
103 memcpy(&dest_ctx->data.measurement_descriptor,
104 dice_inputs->code_descriptor,
105 dice_inputs->code_descriptor_size);
106
107 dest_ctx->data.measurement_descriptor_size =
108 dice_inputs->code_descriptor_size;
109
110 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
111 memcpy(&dest_ctx->data.signer_id_descriptor,
112 dice_inputs->authority_descriptor,
113 dice_inputs->authority_descriptor_size);
114
115 dest_ctx->data.signer_id_descriptor_size =
116 dice_inputs->authority_descriptor_size;
117
118 if (dice_inputs->config_type == kDiceConfigTypeInline) {
119 /* Copy config_value */
120 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
121 DICE_INLINE_CONFIG_SIZE);
122
123 } else {
124 /* Copy config descriptor */
125 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
126 dice_inputs->config_descriptor_size);
127 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
128
129 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100130 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100131 dice_inputs->config_descriptor,
132 dice_inputs->config_descriptor_size,
133 dest_ctx->data.config_value,
134 sizeof(dest_ctx->data.config_value),
135 &hash_len);
136
137 if (status != PSA_SUCCESS) {
138 return DPE_INTERNAL_ERROR;
139 }
140 }
141
142 dest_ctx->data.mode = dice_inputs->mode;
143 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
144
145 return DPE_NO_ERROR;
146}
147
148static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
149{
150 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
151 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
152 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
153 return false;
154 }
155
156 return true;
157}
158
159static bool is_input_handle_valid(int input_context_handle)
160{
161 uint16_t idx = GET_IDX(input_context_handle);
162 uint16_t nonce = GET_NONCE(input_context_handle);
163
164 /* Validate input handle id and nonce */
165 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
166 return false;
167 }
168
169 if (nonce == component_ctx_array[idx].nonce) {
170 return true;
171 }
172
173 return false;
174}
175
Maulik Patel58595d32023-06-22 10:08:53 +0100176/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
177 * same order
178 */
179static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
180 size_t max_size,
181 size_t *dest_size,
182 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100183{
Maulik Patel58595d32023-06-22 10:08:53 +0100184 size_t out_size = 0;
185
186 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
187 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
188 return PSA_ERROR_BUFFER_TOO_SMALL;
189 }
190
191 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
192 out_size += DICE_HASH_SIZE;
193
194 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
195 out_size += DICE_INLINE_CONFIG_SIZE;
196
197 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
198 out_size += DICE_HASH_SIZE;
199
200 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
201 out_size += sizeof(comp_ctx->data.mode);
202
203 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
204 out_size += DICE_HIDDEN_SIZE;
205
206 *dest_size = out_size;
207
208 return PSA_SUCCESS;
209}
210
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100211static psa_status_t compute_layer_cdi_attest_input(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100212{
213 psa_status_t status;
214 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
215 size_t ctx_data_size, hash_len;
216 int idx;
217
218 psa_hash_operation_t hash_op = psa_hash_operation_init();
219 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
220 if (status != PSA_SUCCESS) {
221 return status;
222 }
223
224 //TODO:
225 /* How to combine measurements of multiple SW components into a single hash
226 * is not yet defined by the Open DICE profile. This implementation
227 * concatenates the data of all SW components which belong to the same layer
228 * and hash it.
229 */
230 for (idx = 0; idx < MAX_NUM_OF_COMPONENTS; idx++) {
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100231 if (component_ctx_array[idx].linked_layer_idx == layer_ctx->idx) {
Maulik Patel58595d32023-06-22 10:08:53 +0100232 /* This component belongs to current layer */
233 /* Concatenate all context data for this component */
234 status = get_component_data_for_attest_cdi(component_ctx_data,
235 sizeof(component_ctx_data),
236 &ctx_data_size,
237 &component_ctx_array[idx]);
238 if (status != PSA_SUCCESS) {
239 return status;
240 }
241
242 status = psa_hash_update(&hash_op,
243 component_ctx_data,
244 ctx_data_size);
245 if (status != PSA_SUCCESS) {
246 return status;
247 }
248 }
249 }
250
251 status = psa_hash_finish(&hash_op,
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100252 &layer_ctx->attest_cdi_hash_input[0],
253 sizeof(layer_ctx->attest_cdi_hash_input),
Maulik Patel58595d32023-06-22 10:08:53 +0100254 &hash_len);
255
256 assert(hash_len == DPE_HASH_ALG_SIZE);
257
258 return status;
259}
260
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000261static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
262 uint8_t *exported_cdi_buf,
263 size_t exported_cdi_buf_size,
264 size_t *exported_cdi_actual_size)
265{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100266 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
267 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000268 psa_status_t status;
269 dpe_error_t err;
270
Tamas Ban5179a4d2024-01-25 17:05:30 +0100271 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000272 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100273 cdi_attest_buf,
274 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000275 if (status != PSA_SUCCESS) {
276 return DPE_INTERNAL_ERROR;
277 }
278
279 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100280 err = encode_cdi(cdi_attest_buf,
281 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000282 exported_cdi_buf,
283 exported_cdi_buf_size,
284 exported_cdi_actual_size);
285 if (err != DPE_NO_ERROR) {
286 return err;
287 }
288 layer_ctx->is_cdi_to_be_exported = true;
289
290 return DPE_NO_ERROR;
291}
292
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100293static dpe_error_t prepare_layer_certificate(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100294{
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100295 uint16_t layer_idx, parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100296 psa_status_t status;
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100297 struct layer_context_t *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100298
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100299 layer_idx = layer_ctx->idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100300 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100301 parent_layer_idx = layer_ctx->parent_layer_idx;
302 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
303 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100304
Maulik Patel2358bbb2023-07-21 10:56:56 +0100305 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patele6adc112023-08-18 14:21:51 +0100306 if ((layer_idx != DPE_ROT_LAYER_IDX) &&
307 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100308
Maulik Patele6adc112023-08-18 14:21:51 +0100309 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100310
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100311 status = compute_layer_cdi_attest_input(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100312 if (status != PSA_SUCCESS) {
313 return DPE_INTERNAL_ERROR;
314 }
315
Maulik Patele6adc112023-08-18 14:21:51 +0100316 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100317 if (status != PSA_SUCCESS) {
318 return DPE_INTERNAL_ERROR;
319 }
320
Maulik Patele6adc112023-08-18 14:21:51 +0100321 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100322 if (status != PSA_SUCCESS) {
323 return DPE_INTERNAL_ERROR;
324 }
325 }
326
Maulik Patele6adc112023-08-18 14:21:51 +0100327 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100328 if (status != PSA_SUCCESS) {
329 return DPE_INTERNAL_ERROR;
330 }
331
Maulik Patele6adc112023-08-18 14:21:51 +0100332 if (!layer_ctx->is_external_pub_key_provided) {
333 status = derive_attestation_key(layer_ctx);
334 if (status != PSA_SUCCESS) {
335 return DPE_INTERNAL_ERROR;
336 }
Maulik Patel58595d32023-06-22 10:08:53 +0100337 }
338
Maulik Patele6adc112023-08-18 14:21:51 +0100339 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100340 if (status != PSA_SUCCESS) {
341 return DPE_INTERNAL_ERROR;
342 }
343
Tamas Ban257471b2024-03-25 13:49:53 +0100344 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100345}
346
347static uint16_t open_new_layer(void)
348{
349 int i;
350
351 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
352 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
353 layer_ctx_array[i].state = LAYER_STATE_OPEN;
354 return i;
355 }
356 }
357
Maulik Patel2358bbb2023-07-21 10:56:56 +0100358 //TODO: There is an open issue of layer creation as described below.
359 /* This is causing extra unintended layers to open. Since each layer
360 * has some context data and certificate buffer of 3k, it is
361 * causing RAM overflow. Hence until resoluton is reached, once all
362 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100363 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
364 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
365 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
366 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100367 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
368 */
369 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100370}
371
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000372static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100373{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000374 // TODO: FIXME
375 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100376 return true;
377}
378
Maulik Patelcb14cde2024-01-23 12:39:53 +0000379static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100380{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000381 int i;
382
383 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
384 if (layer_ctx_array[i].cert_id == cert_id) {
385 *layer_idx = i;
386 return true;
387 }
388 }
389
390 /* No certificate ID match found */
391 return false;
392}
393
394static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
395 uint32_t cert_id)
396{
397 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100398
399 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
400
401 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
402 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
403
Maulik Patelcb14cde2024-01-23 12:39:53 +0000404 if (cert_id != DPE_CERT_ID_INVALID) {
405 /* cert id was sent by the client */
406 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
407 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
408 /* Cannot add to the layer which is already finalised */
409 return DPE_INTERNAL_ERROR;
410 }
411 /* Derived context belongs to the same certificate as its parent component */
412 new_ctx->linked_layer_idx = parent_layer_idx;
413
414 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
415 /* Cert ID is already in use */
416 if (layer_ctx_array[layer_idx_to_link].state == LAYER_STATE_FINALISED) {
417 /* Cannot add to the layer which is already finalised */
418 return DPE_INTERNAL_ERROR;
419 }
420 /* Use the same layer that is associated with cert_id */
421 new_ctx->linked_layer_idx = layer_idx_to_link;
422 /* Linked layer's parent is already assigned when it was opened */
423
424 } else {
425 /* Open new layer and link derived context to new layer */
426 layer_idx_to_link = open_new_layer();
427 if (layer_idx_to_link == INVALID_LAYER_IDX) {
428 return DPE_INTERNAL_ERROR;
429 }
430 /* Link this context to the new layer */
431 new_ctx->linked_layer_idx = layer_idx_to_link;
432 /* New layer's parent is parent component's layer */
433 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
434 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100435 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100436
437 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000438 /* cert id was not sent by the client */
439 //TODO: To be implemented; return error for now.
440 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100441 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100442
443 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100444}
445
Jamie Fox34681992023-09-04 18:14:06 +0100446/**
447 * \brief Create a root of trust component context.
448 *
449 * \param[out] rot_ctx_handle A new context handle for the RoT context.
450 *
451 * \return Returns error code of type dpe_error_t
452 */
453static dpe_error_t create_rot_context(int *rot_ctx_handle)
454{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100455#ifdef DPE_TEST_MODE
456 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
457#else
Jamie Fox34681992023-09-04 18:14:06 +0100458 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100459#endif /* DPE_TEST_MODE */
460 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100461 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
462 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
463
Maulik Patela81605b2023-10-24 12:17:03 +0100464 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100465 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
466
Maulik Pateldbfd5152023-05-30 17:02:42 +0100467#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100468 /* Get the RoT CDI input for the RoT layer */
Antonio de Angelis30211a62024-04-04 12:48:18 +0100469 status = get_rot_cdi_input(&rot_cdi_input[0], sizeof(rot_cdi_input));
470 if (status != PSA_SUCCESS) {
Jamie Fox34681992023-09-04 18:14:06 +0100471 return DPE_INTERNAL_ERROR;
472 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100473#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100474
475 /* Import the CDI key for the RoT layer */
476 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
477 &rot_cdi_input[0],
478 sizeof(rot_cdi_input));
479 if (status != PSA_SUCCESS) {
480 return DPE_INTERNAL_ERROR;
481 }
482
Maulik Patela81605b2023-10-24 12:17:03 +0100483 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100484 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100485 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100486 rot_comp_ctx->parent_idx = 0;
487 /* Link context to RoT Layer */
488 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
489 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100490 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
491
492 return DPE_NO_ERROR;
493}
494
495dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
496{
497 int i;
498
499 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
500 set_context_to_default(i);
501 }
502
503 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
504 invalidate_layer(i);
505 }
506
507 return create_rot_context(rot_ctx_handle);
508}
509
Maulik Patela81605b2023-10-24 12:17:03 +0100510dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000511 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100512 bool retain_parent_context,
513 bool allow_new_context_to_derive,
514 bool create_certificate,
515 const DiceInputValues *dice_inputs,
516 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000517 int32_t target_locality,
518 bool return_certificate,
519 bool allow_new_context_to_export,
520 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100521 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000522 int *new_parent_context_handle,
523 uint8_t *new_certificate_buf,
524 size_t new_certificate_buf_size,
525 size_t *new_certificate_actual_size,
526 uint8_t *exported_cdi_buf,
527 size_t exported_cdi_buf_size,
528 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100529{
Maulik Patel58595d32023-06-22 10:08:53 +0100530 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100531 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000532 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100533 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000534 struct layer_context_t *layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100535
Maulik Patelcb14cde2024-01-23 12:39:53 +0000536 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100537 allow_new_context_to_derive, create_certificate, dice_inputs,
538 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100539
Maulik Pateldbfd5152023-05-30 17:02:42 +0100540#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100541 if ((input_ctx_handle == 0) &&
542 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100543 /* Deriving RoT context for tests */
544 err = create_rot_context(&input_ctx_handle);
545 if (err != DPE_NO_ERROR) {
546 return err;
547 }
548 }
549#endif /* DPE_TEST_MODE */
550
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000551 if (export_cdi && !create_certificate) {
552 return DPE_INVALID_ARGUMENT;
553 }
554
Maulik Patelad2f3db2023-05-17 15:41:36 +0100555 /* Validate dice inputs */
556 if (!is_dice_input_valid(dice_inputs)) {
557 return DPE_INVALID_ARGUMENT;
558 }
559
560 /* Validate input handle */
561 if (!is_input_handle_valid(input_ctx_handle)) {
562 return DPE_INVALID_ARGUMENT;
563 }
Maulik Patela81605b2023-10-24 12:17:03 +0100564 /* Get parent component index from the input handle */
565 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100566
567 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100568 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100569 */
Maulik Patela81605b2023-10-24 12:17:03 +0100570 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100571
Maulik Patela81605b2023-10-24 12:17:03 +0100572 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100573
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000574 /* Check if parent context is allowed to derive */
575 if (!parent_ctx->is_allowed_to_derive) {
576 return DPE_INVALID_ARGUMENT;
577 }
578
Maulik Patelad2f3db2023-05-17 15:41:36 +0100579 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000580 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100581 return DPE_INVALID_ARGUMENT;
582 }
583
Maulik Patela81605b2023-10-24 12:17:03 +0100584 /* Get next free component index to add new derived context */
585 free_component_idx = get_free_component_context_index();
586 if (free_component_idx < 0) {
587 return DPE_INSUFFICIENT_MEMORY;
588 }
589
590 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000591 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
592 /* If parent context has export enabled and input allow_new_context_to_export
593 * is true, then allow context CDI to be exported for derived context
594 */
595 derived_ctx->is_export_cdi_allowed = true;
596 } else {
597 /* Export of new context CDI is NOT allowed */
598 derived_ctx->is_export_cdi_allowed = false;
599 if (export_cdi) {
600 return DPE_INVALID_ARGUMENT;
601 }
602 }
603
Maulik Patela81605b2023-10-24 12:17:03 +0100604 /* Copy dice input to the new derived component context */
605 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100606 if (err != DPE_NO_ERROR) {
607 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100608 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000609 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100610
Maulik Patela81605b2023-10-24 12:17:03 +0100611 /* Update parent idx in new derived component context */
612 derived_ctx->parent_idx = parent_ctx_idx;
613 /* Mark new derived component index as in use */
614 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000615 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000616 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100617 if (err != DPE_NO_ERROR) {
618 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100619 }
620
621 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100622 /* Retain and return parent handle with renewed nonce */
623 *new_parent_context_handle = input_ctx_handle;
624 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100625 if (err != DPE_NO_ERROR) {
626 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100627 }
Maulik Patela81605b2023-10-24 12:17:03 +0100628 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
629
Maulik Patelad2f3db2023-05-17 15:41:36 +0100630 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100631 /* Return invalid handle */
632 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100633 parent_ctx->nonce = INVALID_NONCE_VALUE;
634 }
635
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000636 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100637 /* Return handle to derived context */
638 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
639 err = renew_nonce(new_context_handle);
640 if (err != DPE_NO_ERROR) {
641 return err;
642 }
643 /* Update nonce in new derived component context */
644 derived_ctx->nonce = GET_NONCE(*new_context_handle);
645
646 } else {
647 /* Return invalid handle */
648 *new_context_handle = INVALID_HANDLE;
649 derived_ctx->nonce = INVALID_NONCE_VALUE;
650 }
651
Maulik Patel5ac87802024-03-14 14:22:19 +0000652 linked_layer_idx = derived_ctx->linked_layer_idx;
653 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
654 layer_ctx = &layer_ctx_array[linked_layer_idx];
Maulik Patela81605b2023-10-24 12:17:03 +0100655 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000656 layer_ctx->is_cdi_to_be_exported = export_cdi;
657
Maulik Patelcbded682023-12-07 11:50:16 +0000658 /* Finalise the layer */
659 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100660 err = prepare_layer_certificate(layer_ctx);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000661 if (err != DPE_NO_ERROR) {
662 return err;
663 }
664
665 if (return_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000666 /* Encode and return generated layer certificate */
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100667 err = encode_layer_certificate(layer_ctx,
Tamas Ban257471b2024-03-25 13:49:53 +0100668 new_certificate_buf,
669 new_certificate_buf_size,
670 new_certificate_actual_size);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000671 if (err != DPE_NO_ERROR) {
672 return err;
673 }
674 }
675 }
676
677 if (export_cdi) {
678 err = get_encoded_cdi_to_export(layer_ctx,
679 exported_cdi_buf,
680 exported_cdi_buf_size,
681 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100682 if (err != DPE_NO_ERROR) {
683 return err;
684 }
685 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000686 log_derive_context_output_handles(*new_parent_context_handle,
687 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100688
Maulik Patel5ac87802024-03-14 14:22:19 +0000689 /* Log context and layer info and certificate if no error */
690 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
691 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
692 if (create_certificate) {
693 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100694 new_certificate_buf,
695 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000696 }
697
Maulik Patelad2f3db2023-05-17 15:41:36 +0100698 return DPE_NO_ERROR;
699}
Maulik Patel54d65f72023-06-28 13:04:36 +0100700
701dpe_error_t destroy_context_request(int input_ctx_handle,
702 bool destroy_recursively)
703{
704 uint16_t input_ctx_idx, linked_layer_idx;
705 int i;
706 bool is_layer_empty;
707
708 log_destroy_context(input_ctx_handle, destroy_recursively);
709
Maulik Patela81605b2023-10-24 12:17:03 +0100710 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100711 input_ctx_idx = GET_IDX(input_ctx_handle);
712
Maulik Patel54d65f72023-06-28 13:04:36 +0100713 /* Validate input handle */
714 if (!is_input_handle_valid(input_ctx_handle)) {
715 return DPE_INVALID_ARGUMENT;
716 }
717 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
718
Jamie Fox34681992023-09-04 18:14:06 +0100719#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100720 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
721 /* All layers till hypervisor cannot be destroyed dynamically */
722 return DPE_INVALID_ARGUMENT;
723 }
Jamie Fox34681992023-09-04 18:14:06 +0100724#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100725
726
727 if (!destroy_recursively) {
728 set_context_to_default(input_ctx_idx);
729 } else {
730 //TODO: To be implemented
731 }
732
733 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
734
735 /* Close the layer if all of its contexts are destroyed */
736 is_layer_empty = true;
737 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
738 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
739 /* There are active component context in the layer */
740 is_layer_empty = false;
741 break;
742 }
743 }
744
745 if (is_layer_empty) {
746 invalidate_layer(linked_layer_idx);
747 }
748
749 return DPE_NO_ERROR;
750}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100751
752struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
753 uint16_t component_idx)
754{
755 /* Safety case */
756 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
757 return NULL;
758 }
759
760 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
761 return &component_ctx_array[component_idx];
762 } else {
763 return NULL;
764 }
765}
766
Maulik Patele6adc112023-08-18 14:21:51 +0100767struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
768{
769 /* Safety case */
770 if (layer_idx >= MAX_NUM_OF_LAYERS) {
771 return NULL;
772 }
773
774 return &layer_ctx_array[layer_idx];
775}
776
777dpe_error_t certify_key_request(int input_ctx_handle,
778 bool retain_context,
779 const uint8_t *public_key,
780 size_t public_key_size,
781 const uint8_t *label,
782 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000783 uint8_t *certificate_buf,
784 size_t certificate_buf_size,
785 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100786 uint8_t *derived_public_key_buf,
787 size_t derived_public_key_buf_size,
788 size_t *derived_public_key_actual_size,
789 int *new_context_handle)
790{
791 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
792 dpe_error_t err;
793 psa_status_t status;
794 struct layer_context_t *parent_layer_ctx, *layer_ctx;
795
796 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
797 label, label_size);
798
799 /* Validate input handle */
800 if (!is_input_handle_valid(input_ctx_handle)) {
801 return DPE_INVALID_ARGUMENT;
802 }
803
804 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
805 return DPE_INVALID_ARGUMENT;
806 }
807
808 /* Get component index from the input handle */
809 input_ctx_idx = GET_IDX(input_ctx_handle);
810 /* Get current linked layer idx */
811 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
812 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
813
814 layer_ctx = &layer_ctx_array[input_layer_idx];
815 if (public_key_size > sizeof(layer_ctx->data.attest_pub_key)) {
816 return DPE_INVALID_ARGUMENT;
817 }
818
819 if ((public_key_size > 0) && (public_key != NULL)) {
820 layer_ctx->is_external_pub_key_provided = true;
821 /* Copy the public key provided */
822 memcpy(&layer_ctx->data.attest_pub_key[0],
823 public_key,
824 public_key_size);
825 layer_ctx->data.attest_pub_key_len = public_key_size;
826
827 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel4fed7812023-12-08 09:55:22 +0000828 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100829
830 } else {
831 /* No external public key is provided */
832 layer_ctx->is_external_pub_key_provided = false;
833
834 if ((label_size > 0) && (label != NULL)) {
835 /* Copy the label provided */
Maulik Patel4fed7812023-12-08 09:55:22 +0000836 memcpy(&layer_ctx->data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100837 label,
838 label_size);
Maulik Patel4fed7812023-12-08 09:55:22 +0000839 layer_ctx->data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100840
841 } else {
Maulik Patel4fed7812023-12-08 09:55:22 +0000842 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100843 }
844 }
845
846 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100847 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100848 */
Maulik Patelcbded682023-12-07 11:50:16 +0000849 /* Create leaf certificate */
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100850 err = prepare_layer_certificate(layer_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100851 if (err != DPE_NO_ERROR) {
852 return err;
853 }
854
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100855 err = encode_layer_certificate(layer_ctx,
Tamas Ban257471b2024-03-25 13:49:53 +0100856 certificate_buf,
857 certificate_buf_size,
858 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100859 if (err != DPE_NO_ERROR) {
860 return err;
861 }
862
863 /* Get parent layer derived public key to verify the certificate signature */
864 parent_layer_idx = layer_ctx_array[input_layer_idx].parent_layer_idx;
865 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
866 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
867
868 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
869 return DPE_INVALID_ARGUMENT;
870 }
871
872 memcpy(derived_public_key_buf,
873 &parent_layer_ctx->data.attest_pub_key[0],
874 parent_layer_ctx->data.attest_pub_key_len);
875 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
876
Maulik Patel91edd632024-02-26 07:44:41 +0000877 /* Renew handle for the same context, if requested */
878 if (retain_context) {
879 *new_context_handle = input_ctx_handle;
880 status = renew_nonce(new_context_handle);
881 if (status != PSA_SUCCESS) {
882 return DPE_INTERNAL_ERROR;
883 }
884 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
885
886 } else {
887 *new_context_handle = INVALID_HANDLE;
888 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100889 }
Maulik Patele6adc112023-08-18 14:21:51 +0100890
891 /* Clear the context label and key contents */
Maulik Patel4fed7812023-12-08 09:55:22 +0000892 memset(&layer_ctx->data.external_key_deriv_label[0], 0u,
893 layer_ctx->data.external_key_deriv_label_len);
894 memset(&layer_ctx->data.attest_pub_key[0], 0u,
895 layer_ctx->data.attest_pub_key_len);
Maulik Patel9a2a5672024-03-14 13:43:58 +0000896 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000897 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100898 certificate_buf,
899 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100900
901 return DPE_NO_ERROR;
902}
Maulik Patel83a6b592023-12-05 15:20:30 +0000903
904dpe_error_t get_certificate_chain_request(int input_ctx_handle,
905 bool retain_context,
906 bool clear_from_context,
907 uint8_t *certificate_chain_buf,
908 size_t certificate_chain_buf_size,
909 size_t *certificate_chain_actual_size,
910 int *new_context_handle)
911{
912 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000913 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000914 psa_status_t status;
915 struct layer_context_t *layer_ctx;
916
Tamas Bana5e2f582024-01-25 16:59:26 +0100917 log_get_certificate_chain(input_ctx_handle, retain_context,
918 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000919
920 /* Validate input handle */
921 if (!is_input_handle_valid(input_ctx_handle)) {
922 return DPE_INVALID_ARGUMENT;
923 }
924
925 /* Get component index from the input handle */
926 input_ctx_idx = GET_IDX(input_ctx_handle);
927 /* Get current linked layer idx */
928 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
929 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
930
931 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100932 if (layer_ctx->state != LAYER_STATE_FINALISED) {
933 /* If the context has accumulated info and not yet part of a certificate,
934 * return an invalid-argument error
935 */
936 return DPE_INVALID_ARGUMENT;
937 }
938
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100939 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000940 certificate_chain_buf,
941 certificate_chain_buf_size,
942 certificate_chain_actual_size);
943 if (err != DPE_NO_ERROR) {
944 return err;
945 }
946
947 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
948
949 /* Renew handle for the same context, if requested */
950 if (retain_context) {
951 *new_context_handle = input_ctx_handle;
952 status = renew_nonce(new_context_handle);
953 if (status != PSA_SUCCESS) {
954 return DPE_INTERNAL_ERROR;
955 }
956 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
957
958 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +0100959 //TODO: Reimplement the clear_from_context functionality after memory
960 // optimization; Certificates are not ready made and they are not
961 // stored in the layer context anymore. They are created on-the-fly
962 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000963 }
964
965 } else {
966 *new_context_handle = INVALID_HANDLE;
967 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
968 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000969 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000970
971 return DPE_NO_ERROR;
972}