blob: ac74c339e65873c021bfafad0e39f9f5d1063ee9 [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{
83 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
84 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000085 layer_ctx_array[i].is_cdi_to_be_exported = false;
Maulik Patelcb14cde2024-01-23 12:39:53 +000086 layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID;
Maulik Patele6adc112023-08-18 14:21:51 +010087 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
88 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patel58595d32023-06-22 10:08:53 +010089 (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id);
90 (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +010091 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
92}
93
Maulik Patelad2f3db2023-05-17 15:41:36 +010094static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
95 const DiceInputValues *dice_inputs)
96{
97 size_t hash_len;
98 psa_status_t status;
99
100 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
101 DICE_HASH_SIZE);
102 memcpy(&dest_ctx->data.measurement_descriptor,
103 dice_inputs->code_descriptor,
104 dice_inputs->code_descriptor_size);
105
106 dest_ctx->data.measurement_descriptor_size =
107 dice_inputs->code_descriptor_size;
108
109 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
110 memcpy(&dest_ctx->data.signer_id_descriptor,
111 dice_inputs->authority_descriptor,
112 dice_inputs->authority_descriptor_size);
113
114 dest_ctx->data.signer_id_descriptor_size =
115 dice_inputs->authority_descriptor_size;
116
117 if (dice_inputs->config_type == kDiceConfigTypeInline) {
118 /* Copy config_value */
119 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
120 DICE_INLINE_CONFIG_SIZE);
121
122 } else {
123 /* Copy config descriptor */
124 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
125 dice_inputs->config_descriptor_size);
126 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
127
128 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100129 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100130 dice_inputs->config_descriptor,
131 dice_inputs->config_descriptor_size,
132 dest_ctx->data.config_value,
133 sizeof(dest_ctx->data.config_value),
134 &hash_len);
135
136 if (status != PSA_SUCCESS) {
137 return DPE_INTERNAL_ERROR;
138 }
139 }
140
141 dest_ctx->data.mode = dice_inputs->mode;
142 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
143
144 return DPE_NO_ERROR;
145}
146
147static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
148{
149 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
150 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
151 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
152 return false;
153 }
154
155 return true;
156}
157
158static bool is_input_handle_valid(int input_context_handle)
159{
160 uint16_t idx = GET_IDX(input_context_handle);
161 uint16_t nonce = GET_NONCE(input_context_handle);
162
163 /* Validate input handle id and nonce */
164 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
165 return false;
166 }
167
168 if (nonce == component_ctx_array[idx].nonce) {
169 return true;
170 }
171
172 return false;
173}
174
Maulik Patel58595d32023-06-22 10:08:53 +0100175/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
176 * same order
177 */
178static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
179 size_t max_size,
180 size_t *dest_size,
181 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100182{
Maulik Patel58595d32023-06-22 10:08:53 +0100183 size_t out_size = 0;
184
185 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
186 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
187 return PSA_ERROR_BUFFER_TOO_SMALL;
188 }
189
190 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
191 out_size += DICE_HASH_SIZE;
192
193 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
194 out_size += DICE_INLINE_CONFIG_SIZE;
195
196 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
197 out_size += DICE_HASH_SIZE;
198
199 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
200 out_size += sizeof(comp_ctx->data.mode);
201
202 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
203 out_size += DICE_HIDDEN_SIZE;
204
205 *dest_size = out_size;
206
207 return PSA_SUCCESS;
208}
209
210static psa_status_t compute_layer_cdi_attest_input(uint16_t curr_layer_idx)
211{
212 psa_status_t status;
213 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
214 size_t ctx_data_size, hash_len;
215 int idx;
216
217 psa_hash_operation_t hash_op = psa_hash_operation_init();
218 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
219 if (status != PSA_SUCCESS) {
220 return status;
221 }
222
223 //TODO:
224 /* How to combine measurements of multiple SW components into a single hash
225 * is not yet defined by the Open DICE profile. This implementation
226 * concatenates the data of all SW components which belong to the same layer
227 * and hash it.
228 */
229 for (idx = 0; idx < MAX_NUM_OF_COMPONENTS; idx++) {
230 if (component_ctx_array[idx].linked_layer_idx == curr_layer_idx) {
231 /* This component belongs to current layer */
232 /* Concatenate all context data for this component */
233 status = get_component_data_for_attest_cdi(component_ctx_data,
234 sizeof(component_ctx_data),
235 &ctx_data_size,
236 &component_ctx_array[idx]);
237 if (status != PSA_SUCCESS) {
238 return status;
239 }
240
241 status = psa_hash_update(&hash_op,
242 component_ctx_data,
243 ctx_data_size);
244 if (status != PSA_SUCCESS) {
245 return status;
246 }
247 }
248 }
249
250 status = psa_hash_finish(&hash_op,
251 &layer_ctx_array[curr_layer_idx].attest_cdi_hash_input[0],
252 sizeof(layer_ctx_array[curr_layer_idx].attest_cdi_hash_input),
253 &hash_len);
254
255 assert(hash_len == DPE_HASH_ALG_SIZE);
256
257 return status;
258}
259
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000260static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
261 uint8_t *exported_cdi_buf,
262 size_t exported_cdi_buf_size,
263 size_t *exported_cdi_actual_size)
264{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100265 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
266 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000267 psa_status_t status;
268 dpe_error_t err;
269
Tamas Ban5179a4d2024-01-25 17:05:30 +0100270 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000271 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100272 cdi_attest_buf,
273 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000274 if (status != PSA_SUCCESS) {
275 return DPE_INTERNAL_ERROR;
276 }
277
278 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100279 err = encode_cdi(cdi_attest_buf,
280 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000281 exported_cdi_buf,
282 exported_cdi_buf_size,
283 exported_cdi_actual_size);
284 if (err != DPE_NO_ERROR) {
285 return err;
286 }
287 layer_ctx->is_cdi_to_be_exported = true;
288
289 return DPE_NO_ERROR;
290}
291
Maulik Patele6adc112023-08-18 14:21:51 +0100292static dpe_error_t create_layer_certificate(uint16_t layer_idx)
Maulik Patel58595d32023-06-22 10:08:53 +0100293{
Maulik Patel2358bbb2023-07-21 10:56:56 +0100294 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100295 psa_status_t status;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100296 dpe_error_t err;
Maulik Patele6adc112023-08-18 14:21:51 +0100297 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100298
299 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100300 layer_ctx = &layer_ctx_array[layer_idx];
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
311 status = compute_layer_cdi_attest_input(layer_idx);
312 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
Maulik Patel2358bbb2023-07-21 10:56:56 +0100344 err = encode_layer_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100345 layer_ctx,
346 parent_layer_ctx);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100347 if (err != DPE_NO_ERROR) {
348 return err;
Maulik Patel58595d32023-06-22 10:08:53 +0100349 }
350
Maulik Patele6adc112023-08-18 14:21:51 +0100351 return store_layer_certificate(layer_ctx);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100352}
353
354static uint16_t open_new_layer(void)
355{
356 int i;
357
358 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
359 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
360 layer_ctx_array[i].state = LAYER_STATE_OPEN;
361 return i;
362 }
363 }
364
Maulik Patel2358bbb2023-07-21 10:56:56 +0100365 //TODO: There is an open issue of layer creation as described below.
366 /* This is causing extra unintended layers to open. Since each layer
367 * has some context data and certificate buffer of 3k, it is
368 * causing RAM overflow. Hence until resoluton is reached, once all
369 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100370 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
371 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
372 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
373 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100374 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
375 */
376 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100377}
378
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000379static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100380{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000381 // TODO: FIXME
382 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100383 return true;
384}
385
Maulik Patelcb14cde2024-01-23 12:39:53 +0000386static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100387{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000388 int i;
389
390 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
391 if (layer_ctx_array[i].cert_id == cert_id) {
392 *layer_idx = i;
393 return true;
394 }
395 }
396
397 /* No certificate ID match found */
398 return false;
399}
400
401static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
402 uint32_t cert_id)
403{
404 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100405
406 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
407
408 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
409 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
410
Maulik Patelcb14cde2024-01-23 12:39:53 +0000411 if (cert_id != DPE_CERT_ID_INVALID) {
412 /* cert id was sent by the client */
413 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
414 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
415 /* Cannot add to the layer which is already finalised */
416 return DPE_INTERNAL_ERROR;
417 }
418 /* Derived context belongs to the same certificate as its parent component */
419 new_ctx->linked_layer_idx = parent_layer_idx;
420
421 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
422 /* Cert ID is already in use */
423 if (layer_ctx_array[layer_idx_to_link].state == LAYER_STATE_FINALISED) {
424 /* Cannot add to the layer which is already finalised */
425 return DPE_INTERNAL_ERROR;
426 }
427 /* Use the same layer that is associated with cert_id */
428 new_ctx->linked_layer_idx = layer_idx_to_link;
429 /* Linked layer's parent is already assigned when it was opened */
430
431 } else {
432 /* Open new layer and link derived context to new layer */
433 layer_idx_to_link = open_new_layer();
434 if (layer_idx_to_link == INVALID_LAYER_IDX) {
435 return DPE_INTERNAL_ERROR;
436 }
437 /* Link this context to the new layer */
438 new_ctx->linked_layer_idx = layer_idx_to_link;
439 /* New layer's parent is parent component's layer */
440 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
441 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100442 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100443
444 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000445 /* cert id was not sent by the client */
446 //TODO: To be implemented; return error for now.
447 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100448 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100449
450 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100451}
452
Jamie Fox34681992023-09-04 18:14:06 +0100453/**
454 * \brief Create a root of trust component context.
455 *
456 * \param[out] rot_ctx_handle A new context handle for the RoT context.
457 *
458 * \return Returns error code of type dpe_error_t
459 */
460static dpe_error_t create_rot_context(int *rot_ctx_handle)
461{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100462#ifdef DPE_TEST_MODE
463 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
464#else
Jamie Fox34681992023-09-04 18:14:06 +0100465 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100466#endif /* DPE_TEST_MODE */
467 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100468 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
469 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
470
Maulik Patela81605b2023-10-24 12:17:03 +0100471 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100472 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
473
Maulik Pateldbfd5152023-05-30 17:02:42 +0100474#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100475 /* Get the RoT CDI input for the RoT layer */
Antonio de Angelis30211a62024-04-04 12:48:18 +0100476 status = get_rot_cdi_input(&rot_cdi_input[0], sizeof(rot_cdi_input));
477 if (status != PSA_SUCCESS) {
Jamie Fox34681992023-09-04 18:14:06 +0100478 return DPE_INTERNAL_ERROR;
479 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100480#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100481
482 /* Import the CDI key for the RoT layer */
483 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
484 &rot_cdi_input[0],
485 sizeof(rot_cdi_input));
486 if (status != PSA_SUCCESS) {
487 return DPE_INTERNAL_ERROR;
488 }
489
Maulik Patela81605b2023-10-24 12:17:03 +0100490 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100491 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100492 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100493 rot_comp_ctx->parent_idx = 0;
494 /* Link context to RoT Layer */
495 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
496 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100497 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
498
499 return DPE_NO_ERROR;
500}
501
502dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
503{
504 int i;
505
506 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
507 set_context_to_default(i);
508 }
509
510 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
511 invalidate_layer(i);
512 }
513
514 return create_rot_context(rot_ctx_handle);
515}
516
Maulik Patela81605b2023-10-24 12:17:03 +0100517dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000518 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100519 bool retain_parent_context,
520 bool allow_new_context_to_derive,
521 bool create_certificate,
522 const DiceInputValues *dice_inputs,
523 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000524 int32_t target_locality,
525 bool return_certificate,
526 bool allow_new_context_to_export,
527 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100528 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000529 int *new_parent_context_handle,
530 uint8_t *new_certificate_buf,
531 size_t new_certificate_buf_size,
532 size_t *new_certificate_actual_size,
533 uint8_t *exported_cdi_buf,
534 size_t exported_cdi_buf_size,
535 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100536{
Maulik Patel58595d32023-06-22 10:08:53 +0100537 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100538 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000539 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100540 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000541 struct layer_context_t *layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100542
Maulik Patelcb14cde2024-01-23 12:39:53 +0000543 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100544 allow_new_context_to_derive, create_certificate, dice_inputs,
545 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100546
Maulik Pateldbfd5152023-05-30 17:02:42 +0100547#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100548 if ((input_ctx_handle == 0) &&
549 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100550 /* Deriving RoT context for tests */
551 err = create_rot_context(&input_ctx_handle);
552 if (err != DPE_NO_ERROR) {
553 return err;
554 }
555 }
556#endif /* DPE_TEST_MODE */
557
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000558 if (export_cdi && !create_certificate) {
559 return DPE_INVALID_ARGUMENT;
560 }
561
Maulik Patelad2f3db2023-05-17 15:41:36 +0100562 /* Validate dice inputs */
563 if (!is_dice_input_valid(dice_inputs)) {
564 return DPE_INVALID_ARGUMENT;
565 }
566
567 /* Validate input handle */
568 if (!is_input_handle_valid(input_ctx_handle)) {
569 return DPE_INVALID_ARGUMENT;
570 }
Maulik Patela81605b2023-10-24 12:17:03 +0100571 /* Get parent component index from the input handle */
572 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100573
574 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100575 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100576 */
Maulik Patela81605b2023-10-24 12:17:03 +0100577 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100578
Maulik Patela81605b2023-10-24 12:17:03 +0100579 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100580
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000581 /* Check if parent context is allowed to derive */
582 if (!parent_ctx->is_allowed_to_derive) {
583 return DPE_INVALID_ARGUMENT;
584 }
585
Maulik Patelad2f3db2023-05-17 15:41:36 +0100586 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000587 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100588 return DPE_INVALID_ARGUMENT;
589 }
590
Maulik Patela81605b2023-10-24 12:17:03 +0100591 /* Get next free component index to add new derived context */
592 free_component_idx = get_free_component_context_index();
593 if (free_component_idx < 0) {
594 return DPE_INSUFFICIENT_MEMORY;
595 }
596
597 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000598 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
599 /* If parent context has export enabled and input allow_new_context_to_export
600 * is true, then allow context CDI to be exported for derived context
601 */
602 derived_ctx->is_export_cdi_allowed = true;
603 } else {
604 /* Export of new context CDI is NOT allowed */
605 derived_ctx->is_export_cdi_allowed = false;
606 if (export_cdi) {
607 return DPE_INVALID_ARGUMENT;
608 }
609 }
610
Maulik Patela81605b2023-10-24 12:17:03 +0100611 /* Copy dice input to the new derived component context */
612 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100613 if (err != DPE_NO_ERROR) {
614 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100615 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000616 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100617
Maulik Patela81605b2023-10-24 12:17:03 +0100618 /* Update parent idx in new derived component context */
619 derived_ctx->parent_idx = parent_ctx_idx;
620 /* Mark new derived component index as in use */
621 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000622 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000623 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100624 if (err != DPE_NO_ERROR) {
625 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100626 }
627
628 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100629 /* Retain and return parent handle with renewed nonce */
630 *new_parent_context_handle = input_ctx_handle;
631 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100632 if (err != DPE_NO_ERROR) {
633 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100634 }
Maulik Patela81605b2023-10-24 12:17:03 +0100635 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
636
Maulik Patelad2f3db2023-05-17 15:41:36 +0100637 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100638 /* Return invalid handle */
639 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100640 parent_ctx->nonce = INVALID_NONCE_VALUE;
641 }
642
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000643 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100644 /* Return handle to derived context */
645 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
646 err = renew_nonce(new_context_handle);
647 if (err != DPE_NO_ERROR) {
648 return err;
649 }
650 /* Update nonce in new derived component context */
651 derived_ctx->nonce = GET_NONCE(*new_context_handle);
652
653 } else {
654 /* Return invalid handle */
655 *new_context_handle = INVALID_HANDLE;
656 derived_ctx->nonce = INVALID_NONCE_VALUE;
657 }
658
Maulik Patel5ac87802024-03-14 14:22:19 +0000659 linked_layer_idx = derived_ctx->linked_layer_idx;
660 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
661 layer_ctx = &layer_ctx_array[linked_layer_idx];
Maulik Patela81605b2023-10-24 12:17:03 +0100662 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000663 layer_ctx->is_cdi_to_be_exported = export_cdi;
664
Maulik Patelcbded682023-12-07 11:50:16 +0000665 /* Finalise the layer */
666 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000667 err = create_layer_certificate(linked_layer_idx);
668 if (err != DPE_NO_ERROR) {
669 return err;
670 }
671
672 if (return_certificate) {
673 if (new_certificate_buf_size < layer_ctx->data.cert_buf_len) {
674 return DPE_INVALID_ARGUMENT;
675 }
676
677 /* Encode and return generated layer certificate */
678 err = add_encoded_layer_certificate(layer_ctx->data.cert_buf,
679 layer_ctx->data.cert_buf_len,
680 new_certificate_buf,
681 new_certificate_buf_size,
682 new_certificate_actual_size);
683 if (err != DPE_NO_ERROR) {
684 return err;
685 }
686 }
687 }
688
689 if (export_cdi) {
690 err = get_encoded_cdi_to_export(layer_ctx,
691 exported_cdi_buf,
692 exported_cdi_buf_size,
693 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100694 if (err != DPE_NO_ERROR) {
695 return err;
696 }
697 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000698 log_derive_context_output_handles(*new_parent_context_handle,
699 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100700
Maulik Patel5ac87802024-03-14 14:22:19 +0000701 /* Log context and layer info and certificate if no error */
702 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
703 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
704 if (create_certificate) {
705 log_intermediate_certificate(linked_layer_idx,
706 &layer_ctx->data.cert_buf[0],
707 layer_ctx->data.cert_buf_len);
708 }
709
Maulik Patelad2f3db2023-05-17 15:41:36 +0100710 return DPE_NO_ERROR;
711}
Maulik Patel54d65f72023-06-28 13:04:36 +0100712
713dpe_error_t destroy_context_request(int input_ctx_handle,
714 bool destroy_recursively)
715{
716 uint16_t input_ctx_idx, linked_layer_idx;
717 int i;
718 bool is_layer_empty;
719
720 log_destroy_context(input_ctx_handle, destroy_recursively);
721
Maulik Patela81605b2023-10-24 12:17:03 +0100722 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100723 input_ctx_idx = GET_IDX(input_ctx_handle);
724
Maulik Patel54d65f72023-06-28 13:04:36 +0100725 /* Validate input handle */
726 if (!is_input_handle_valid(input_ctx_handle)) {
727 return DPE_INVALID_ARGUMENT;
728 }
729 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
730
Jamie Fox34681992023-09-04 18:14:06 +0100731#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100732 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
733 /* All layers till hypervisor cannot be destroyed dynamically */
734 return DPE_INVALID_ARGUMENT;
735 }
Jamie Fox34681992023-09-04 18:14:06 +0100736#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100737
738
739 if (!destroy_recursively) {
740 set_context_to_default(input_ctx_idx);
741 } else {
742 //TODO: To be implemented
743 }
744
745 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
746
747 /* Close the layer if all of its contexts are destroyed */
748 is_layer_empty = true;
749 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
750 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
751 /* There are active component context in the layer */
752 is_layer_empty = false;
753 break;
754 }
755 }
756
757 if (is_layer_empty) {
758 invalidate_layer(linked_layer_idx);
759 }
760
761 return DPE_NO_ERROR;
762}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100763
764struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
765 uint16_t component_idx)
766{
767 /* Safety case */
768 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
769 return NULL;
770 }
771
772 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
773 return &component_ctx_array[component_idx];
774 } else {
775 return NULL;
776 }
777}
778
Maulik Patele6adc112023-08-18 14:21:51 +0100779struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
780{
781 /* Safety case */
782 if (layer_idx >= MAX_NUM_OF_LAYERS) {
783 return NULL;
784 }
785
786 return &layer_ctx_array[layer_idx];
787}
788
789dpe_error_t certify_key_request(int input_ctx_handle,
790 bool retain_context,
791 const uint8_t *public_key,
792 size_t public_key_size,
793 const uint8_t *label,
794 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000795 uint8_t *certificate_buf,
796 size_t certificate_buf_size,
797 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100798 uint8_t *derived_public_key_buf,
799 size_t derived_public_key_buf_size,
800 size_t *derived_public_key_actual_size,
801 int *new_context_handle)
802{
803 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
804 dpe_error_t err;
805 psa_status_t status;
806 struct layer_context_t *parent_layer_ctx, *layer_ctx;
807
808 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
809 label, label_size);
810
811 /* Validate input handle */
812 if (!is_input_handle_valid(input_ctx_handle)) {
813 return DPE_INVALID_ARGUMENT;
814 }
815
816 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
817 return DPE_INVALID_ARGUMENT;
818 }
819
820 /* Get component index from the input handle */
821 input_ctx_idx = GET_IDX(input_ctx_handle);
822 /* Get current linked layer idx */
823 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
824 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
825
826 layer_ctx = &layer_ctx_array[input_layer_idx];
827 if (public_key_size > sizeof(layer_ctx->data.attest_pub_key)) {
828 return DPE_INVALID_ARGUMENT;
829 }
830
831 if ((public_key_size > 0) && (public_key != NULL)) {
832 layer_ctx->is_external_pub_key_provided = true;
833 /* Copy the public key provided */
834 memcpy(&layer_ctx->data.attest_pub_key[0],
835 public_key,
836 public_key_size);
837 layer_ctx->data.attest_pub_key_len = public_key_size;
838
839 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel4fed7812023-12-08 09:55:22 +0000840 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100841
842 } else {
843 /* No external public key is provided */
844 layer_ctx->is_external_pub_key_provided = false;
845
846 if ((label_size > 0) && (label != NULL)) {
847 /* Copy the label provided */
Maulik Patel4fed7812023-12-08 09:55:22 +0000848 memcpy(&layer_ctx->data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100849 label,
850 label_size);
Maulik Patel4fed7812023-12-08 09:55:22 +0000851 layer_ctx->data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100852
853 } else {
Maulik Patel4fed7812023-12-08 09:55:22 +0000854 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100855 }
856 }
857
858 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100859 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100860 */
Maulik Patelcbded682023-12-07 11:50:16 +0000861 /* Create leaf certificate */
Maulik Patele6adc112023-08-18 14:21:51 +0100862 err = create_layer_certificate(input_layer_idx);
863 if (err != DPE_NO_ERROR) {
864 return err;
865 }
866
867 /* Get parent layer derived public key to verify the certificate signature */
868 parent_layer_idx = layer_ctx_array[input_layer_idx].parent_layer_idx;
869 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
870 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
871
872 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
873 return DPE_INVALID_ARGUMENT;
874 }
875
876 memcpy(derived_public_key_buf,
877 &parent_layer_ctx->data.attest_pub_key[0],
878 parent_layer_ctx->data.attest_pub_key_len);
879 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
880
Maulik Patelcbded682023-12-07 11:50:16 +0000881 /* Get certificate */
882 if (certificate_buf_size < layer_ctx->data.cert_buf_len) {
883 return DPE_INVALID_ARGUMENT;
Maulik Patele6adc112023-08-18 14:21:51 +0100884 }
Maulik Patelcbded682023-12-07 11:50:16 +0000885 memcpy(certificate_buf,
886 &layer_ctx->data.cert_buf[0],
887 layer_ctx->data.cert_buf_len);
888 *certificate_actual_size = layer_ctx->data.cert_buf_len;
Maulik Patele6adc112023-08-18 14:21:51 +0100889
Maulik Patel91edd632024-02-26 07:44:41 +0000890 /* Renew handle for the same context, if requested */
891 if (retain_context) {
892 *new_context_handle = input_ctx_handle;
893 status = renew_nonce(new_context_handle);
894 if (status != PSA_SUCCESS) {
895 return DPE_INTERNAL_ERROR;
896 }
897 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
898
899 } else {
900 *new_context_handle = INVALID_HANDLE;
901 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100902 }
Maulik Patele6adc112023-08-18 14:21:51 +0100903
904 /* Clear the context label and key contents */
Maulik Patel4fed7812023-12-08 09:55:22 +0000905 memset(&layer_ctx->data.external_key_deriv_label[0], 0u,
906 layer_ctx->data.external_key_deriv_label_len);
907 memset(&layer_ctx->data.attest_pub_key[0], 0u,
908 layer_ctx->data.attest_pub_key_len);
Maulik Patel9a2a5672024-03-14 13:43:58 +0000909 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000910 log_intermediate_certificate(input_layer_idx,
911 &layer_ctx->data.cert_buf[0],
912 layer_ctx->data.cert_buf_len);
Maulik Patele6adc112023-08-18 14:21:51 +0100913
914 return DPE_NO_ERROR;
915}
Maulik Patel83a6b592023-12-05 15:20:30 +0000916
917dpe_error_t get_certificate_chain_request(int input_ctx_handle,
918 bool retain_context,
919 bool clear_from_context,
920 uint8_t *certificate_chain_buf,
921 size_t certificate_chain_buf_size,
922 size_t *certificate_chain_actual_size,
923 int *new_context_handle)
924{
925 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000926 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000927 psa_status_t status;
928 struct layer_context_t *layer_ctx;
929
Tamas Bana5e2f582024-01-25 16:59:26 +0100930 log_get_certificate_chain(input_ctx_handle, retain_context,
931 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000932
933 /* Validate input handle */
934 if (!is_input_handle_valid(input_ctx_handle)) {
935 return DPE_INVALID_ARGUMENT;
936 }
937
938 /* Get component index from the input handle */
939 input_ctx_idx = GET_IDX(input_ctx_handle);
940 /* Get current linked layer idx */
941 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
942 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
943
944 layer_ctx = &layer_ctx_array[input_layer_idx];
945 err = get_certificate_chain(input_layer_idx,
946 certificate_chain_buf,
947 certificate_chain_buf_size,
948 certificate_chain_actual_size);
949 if (err != DPE_NO_ERROR) {
950 return err;
951 }
952
953 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
954
955 /* Renew handle for the same context, if requested */
956 if (retain_context) {
957 *new_context_handle = input_ctx_handle;
958 status = renew_nonce(new_context_handle);
959 if (status != PSA_SUCCESS) {
960 return DPE_INTERNAL_ERROR;
961 }
962 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
963
964 if (clear_from_context) {
965 /* Clear all the accumulated certificate information so far */
966 clear_certificate_chain(input_layer_idx, layer_ctx);
967 }
968
969 } else {
970 *new_context_handle = INVALID_HANDLE;
971 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
972 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000973 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000974
975 return DPE_NO_ERROR;
976}