blob: ebed2843195301b09c4b7da7f5f3d6cc4ff8a4c2 [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 Patel87c47ce2024-04-22 13:30:56 +010087 layer_ctx_array[i].is_rot_layer = false;
Maulik Patelcb14cde2024-01-23 12:39:53 +000088 layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID;
Maulik Patele6adc112023-08-18 14:21:51 +010089 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
90 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patel58595d32023-06-22 10:08:53 +010091 (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id);
92 (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +010093 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
94}
95
Maulik Patelad2f3db2023-05-17 15:41:36 +010096static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
97 const DiceInputValues *dice_inputs)
98{
99 size_t hash_len;
100 psa_status_t status;
101
102 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
103 DICE_HASH_SIZE);
104 memcpy(&dest_ctx->data.measurement_descriptor,
105 dice_inputs->code_descriptor,
106 dice_inputs->code_descriptor_size);
107
108 dest_ctx->data.measurement_descriptor_size =
109 dice_inputs->code_descriptor_size;
110
111 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
112 memcpy(&dest_ctx->data.signer_id_descriptor,
113 dice_inputs->authority_descriptor,
114 dice_inputs->authority_descriptor_size);
115
116 dest_ctx->data.signer_id_descriptor_size =
117 dice_inputs->authority_descriptor_size;
118
119 if (dice_inputs->config_type == kDiceConfigTypeInline) {
120 /* Copy config_value */
121 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
122 DICE_INLINE_CONFIG_SIZE);
123
124 } else {
125 /* Copy config descriptor */
126 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
127 dice_inputs->config_descriptor_size);
128 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
129
130 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100131 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100132 dice_inputs->config_descriptor,
133 dice_inputs->config_descriptor_size,
134 dest_ctx->data.config_value,
135 sizeof(dest_ctx->data.config_value),
136 &hash_len);
137
138 if (status != PSA_SUCCESS) {
139 return DPE_INTERNAL_ERROR;
140 }
141 }
142
143 dest_ctx->data.mode = dice_inputs->mode;
144 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
145
146 return DPE_NO_ERROR;
147}
148
149static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
150{
151 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
152 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
153 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
154 return false;
155 }
156
157 return true;
158}
159
160static bool is_input_handle_valid(int input_context_handle)
161{
162 uint16_t idx = GET_IDX(input_context_handle);
163 uint16_t nonce = GET_NONCE(input_context_handle);
164
165 /* Validate input handle id and nonce */
166 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
167 return false;
168 }
169
170 if (nonce == component_ctx_array[idx].nonce) {
171 return true;
172 }
173
174 return false;
175}
176
Maulik Patel58595d32023-06-22 10:08:53 +0100177/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
178 * same order
179 */
180static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
181 size_t max_size,
182 size_t *dest_size,
183 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100184{
Maulik Patel58595d32023-06-22 10:08:53 +0100185 size_t out_size = 0;
186
187 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
188 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
189 return PSA_ERROR_BUFFER_TOO_SMALL;
190 }
191
192 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
193 out_size += DICE_HASH_SIZE;
194
195 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
196 out_size += DICE_INLINE_CONFIG_SIZE;
197
198 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
199 out_size += DICE_HASH_SIZE;
200
201 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
202 out_size += sizeof(comp_ctx->data.mode);
203
204 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
205 out_size += DICE_HIDDEN_SIZE;
206
207 *dest_size = out_size;
208
209 return PSA_SUCCESS;
210}
211
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100212static psa_status_t compute_layer_cdi_attest_input(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100213{
214 psa_status_t status;
215 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
216 size_t ctx_data_size, hash_len;
217 int idx;
218
219 psa_hash_operation_t hash_op = psa_hash_operation_init();
220 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
221 if (status != PSA_SUCCESS) {
222 return status;
223 }
224
225 //TODO:
226 /* How to combine measurements of multiple SW components into a single hash
227 * is not yet defined by the Open DICE profile. This implementation
228 * concatenates the data of all SW components which belong to the same layer
229 * and hash it.
230 */
231 for (idx = 0; idx < MAX_NUM_OF_COMPONENTS; idx++) {
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100232 if (component_ctx_array[idx].linked_layer_idx == layer_ctx->idx) {
Maulik Patel58595d32023-06-22 10:08:53 +0100233 /* This component belongs to current layer */
234 /* Concatenate all context data for this component */
235 status = get_component_data_for_attest_cdi(component_ctx_data,
236 sizeof(component_ctx_data),
237 &ctx_data_size,
238 &component_ctx_array[idx]);
239 if (status != PSA_SUCCESS) {
240 return status;
241 }
242
243 status = psa_hash_update(&hash_op,
244 component_ctx_data,
245 ctx_data_size);
246 if (status != PSA_SUCCESS) {
247 return status;
248 }
249 }
250 }
251
252 status = psa_hash_finish(&hash_op,
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100253 &layer_ctx->attest_cdi_hash_input[0],
254 sizeof(layer_ctx->attest_cdi_hash_input),
Maulik Patel58595d32023-06-22 10:08:53 +0100255 &hash_len);
256
257 assert(hash_len == DPE_HASH_ALG_SIZE);
258
259 return status;
260}
261
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000262static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
263 uint8_t *exported_cdi_buf,
264 size_t exported_cdi_buf_size,
265 size_t *exported_cdi_actual_size)
266{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100267 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
268 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000269 psa_status_t status;
270 dpe_error_t err;
271
Tamas Ban5179a4d2024-01-25 17:05:30 +0100272 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000273 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100274 cdi_attest_buf,
275 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000276 if (status != PSA_SUCCESS) {
277 return DPE_INTERNAL_ERROR;
278 }
279
280 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100281 err = encode_cdi(cdi_attest_buf,
282 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000283 exported_cdi_buf,
284 exported_cdi_buf_size,
285 exported_cdi_actual_size);
286 if (err != DPE_NO_ERROR) {
287 return err;
288 }
289 layer_ctx->is_cdi_to_be_exported = true;
290
291 return DPE_NO_ERROR;
292}
293
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100294static dpe_error_t prepare_layer_certificate(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100295{
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100296 uint16_t layer_idx, parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100297 psa_status_t status;
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100298 struct layer_context_t *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100299
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100300 layer_idx = layer_ctx->idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100301 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100302 parent_layer_idx = layer_ctx->parent_layer_idx;
303 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
304 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100305
Maulik Patel2358bbb2023-07-21 10:56:56 +0100306 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patel87c47ce2024-04-22 13:30:56 +0100307 if ((!layer_ctx->is_rot_layer) &&
Maulik Patele6adc112023-08-18 14:21:51 +0100308 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100309
Maulik Patele6adc112023-08-18 14:21:51 +0100310 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100311
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100312 status = compute_layer_cdi_attest_input(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100313 if (status != PSA_SUCCESS) {
314 return DPE_INTERNAL_ERROR;
315 }
316
Maulik Patele6adc112023-08-18 14:21:51 +0100317 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100318 if (status != PSA_SUCCESS) {
319 return DPE_INTERNAL_ERROR;
320 }
321
Maulik Patele6adc112023-08-18 14:21:51 +0100322 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100323 if (status != PSA_SUCCESS) {
324 return DPE_INTERNAL_ERROR;
325 }
326 }
327
Maulik Patele6adc112023-08-18 14:21:51 +0100328 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100329 if (status != PSA_SUCCESS) {
330 return DPE_INTERNAL_ERROR;
331 }
332
Maulik Patele6adc112023-08-18 14:21:51 +0100333 if (!layer_ctx->is_external_pub_key_provided) {
334 status = derive_attestation_key(layer_ctx);
335 if (status != PSA_SUCCESS) {
336 return DPE_INTERNAL_ERROR;
337 }
Maulik Patel58595d32023-06-22 10:08:53 +0100338 }
339
Maulik Patele6adc112023-08-18 14:21:51 +0100340 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100341 if (status != PSA_SUCCESS) {
342 return DPE_INTERNAL_ERROR;
343 }
344
Tamas Ban257471b2024-03-25 13:49:53 +0100345 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100346}
347
348static uint16_t open_new_layer(void)
349{
350 int i;
351
352 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
353 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
354 layer_ctx_array[i].state = LAYER_STATE_OPEN;
355 return i;
356 }
357 }
358
Maulik Patel2358bbb2023-07-21 10:56:56 +0100359 //TODO: There is an open issue of layer creation as described below.
360 /* This is causing extra unintended layers to open. Since each layer
361 * has some context data and certificate buffer of 3k, it is
362 * causing RAM overflow. Hence until resoluton is reached, once all
363 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100364 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
365 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
366 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
367 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100368 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
369 */
370 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100371}
372
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000373static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100374{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000375 // TODO: FIXME
376 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100377 return true;
378}
379
Maulik Patelcb14cde2024-01-23 12:39:53 +0000380static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100381{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000382 int i;
383
384 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
385 if (layer_ctx_array[i].cert_id == cert_id) {
386 *layer_idx = i;
387 return true;
388 }
389 }
390
391 /* No certificate ID match found */
392 return false;
393}
394
395static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
396 uint32_t cert_id)
397{
398 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100399
400 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
401
402 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
403 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
404
Maulik Patelcb14cde2024-01-23 12:39:53 +0000405 if (cert_id != DPE_CERT_ID_INVALID) {
406 /* cert id was sent by the client */
407 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
408 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
409 /* Cannot add to the layer which is already finalised */
410 return DPE_INTERNAL_ERROR;
411 }
412 /* Derived context belongs to the same certificate as its parent component */
413 new_ctx->linked_layer_idx = parent_layer_idx;
414
415 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
416 /* Cert ID is already in use */
417 if (layer_ctx_array[layer_idx_to_link].state == LAYER_STATE_FINALISED) {
418 /* Cannot add to the layer which is already finalised */
419 return DPE_INTERNAL_ERROR;
420 }
421 /* Use the same layer that is associated with cert_id */
422 new_ctx->linked_layer_idx = layer_idx_to_link;
423 /* Linked layer's parent is already assigned when it was opened */
424
425 } else {
426 /* Open new layer and link derived context to new layer */
427 layer_idx_to_link = open_new_layer();
428 if (layer_idx_to_link == INVALID_LAYER_IDX) {
429 return DPE_INTERNAL_ERROR;
430 }
431 /* Link this context to the new layer */
432 new_ctx->linked_layer_idx = layer_idx_to_link;
433 /* New layer's parent is parent component's layer */
434 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
435 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100436 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100437
438 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000439 /* cert id was not sent by the client */
440 //TODO: To be implemented; return error for now.
441 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100442 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100443
444 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100445}
446
Jamie Fox34681992023-09-04 18:14:06 +0100447/**
448 * \brief Create a root of trust component context.
449 *
450 * \param[out] rot_ctx_handle A new context handle for the RoT context.
451 *
452 * \return Returns error code of type dpe_error_t
453 */
454static dpe_error_t create_rot_context(int *rot_ctx_handle)
455{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100456#ifdef DPE_TEST_MODE
457 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
458#else
Jamie Fox34681992023-09-04 18:14:06 +0100459 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100460#endif /* DPE_TEST_MODE */
461 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100462 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
463 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
464
Maulik Patel87c47ce2024-04-22 13:30:56 +0100465 rot_layer_ctx->is_rot_layer = true;
Maulik Patela81605b2023-10-24 12:17:03 +0100466 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100467 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
468
Maulik Pateldbfd5152023-05-30 17:02:42 +0100469#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100470 /* Get the RoT CDI input for the RoT layer */
Antonio de Angelis30211a62024-04-04 12:48:18 +0100471 status = get_rot_cdi_input(&rot_cdi_input[0], sizeof(rot_cdi_input));
472 if (status != PSA_SUCCESS) {
Jamie Fox34681992023-09-04 18:14:06 +0100473 return DPE_INTERNAL_ERROR;
474 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100475#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100476
477 /* Import the CDI key for the RoT layer */
478 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
479 &rot_cdi_input[0],
480 sizeof(rot_cdi_input));
481 if (status != PSA_SUCCESS) {
482 return DPE_INTERNAL_ERROR;
483 }
484
Maulik Patela81605b2023-10-24 12:17:03 +0100485 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100486 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100487 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100488 rot_comp_ctx->parent_idx = 0;
489 /* Link context to RoT Layer */
490 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
491 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100492 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
493
494 return DPE_NO_ERROR;
495}
496
497dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
498{
499 int i;
500
501 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
502 set_context_to_default(i);
503 }
504
505 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
506 invalidate_layer(i);
507 }
508
509 return create_rot_context(rot_ctx_handle);
510}
511
Maulik Patela81605b2023-10-24 12:17:03 +0100512dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000513 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100514 bool retain_parent_context,
515 bool allow_new_context_to_derive,
516 bool create_certificate,
517 const DiceInputValues *dice_inputs,
518 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000519 int32_t target_locality,
520 bool return_certificate,
521 bool allow_new_context_to_export,
522 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100523 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000524 int *new_parent_context_handle,
525 uint8_t *new_certificate_buf,
526 size_t new_certificate_buf_size,
527 size_t *new_certificate_actual_size,
528 uint8_t *exported_cdi_buf,
529 size_t exported_cdi_buf_size,
530 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100531{
Maulik Patel58595d32023-06-22 10:08:53 +0100532 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100533 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000534 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100535 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000536 struct layer_context_t *layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100537
Maulik Patelcb14cde2024-01-23 12:39:53 +0000538 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100539 allow_new_context_to_derive, create_certificate, dice_inputs,
540 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100541
Maulik Pateldbfd5152023-05-30 17:02:42 +0100542#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100543 if ((input_ctx_handle == 0) &&
544 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100545 /* Deriving RoT context for tests */
546 err = create_rot_context(&input_ctx_handle);
547 if (err != DPE_NO_ERROR) {
548 return err;
549 }
550 }
551#endif /* DPE_TEST_MODE */
552
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000553 if (export_cdi && !create_certificate) {
554 return DPE_INVALID_ARGUMENT;
555 }
556
Maulik Patelad2f3db2023-05-17 15:41:36 +0100557 /* Validate dice inputs */
558 if (!is_dice_input_valid(dice_inputs)) {
559 return DPE_INVALID_ARGUMENT;
560 }
561
562 /* Validate input handle */
563 if (!is_input_handle_valid(input_ctx_handle)) {
564 return DPE_INVALID_ARGUMENT;
565 }
Maulik Patela81605b2023-10-24 12:17:03 +0100566 /* Get parent component index from the input handle */
567 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100568
569 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100570 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100571 */
Maulik Patela81605b2023-10-24 12:17:03 +0100572 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100573
Maulik Patela81605b2023-10-24 12:17:03 +0100574 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100575
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000576 /* Check if parent context is allowed to derive */
577 if (!parent_ctx->is_allowed_to_derive) {
578 return DPE_INVALID_ARGUMENT;
579 }
580
Maulik Patelad2f3db2023-05-17 15:41:36 +0100581 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000582 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100583 return DPE_INVALID_ARGUMENT;
584 }
585
Maulik Patela81605b2023-10-24 12:17:03 +0100586 /* Get next free component index to add new derived context */
587 free_component_idx = get_free_component_context_index();
588 if (free_component_idx < 0) {
589 return DPE_INSUFFICIENT_MEMORY;
590 }
591
592 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000593 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
594 /* If parent context has export enabled and input allow_new_context_to_export
595 * is true, then allow context CDI to be exported for derived context
596 */
597 derived_ctx->is_export_cdi_allowed = true;
598 } else {
599 /* Export of new context CDI is NOT allowed */
600 derived_ctx->is_export_cdi_allowed = false;
601 if (export_cdi) {
602 return DPE_INVALID_ARGUMENT;
603 }
604 }
605
Maulik Patela81605b2023-10-24 12:17:03 +0100606 /* Copy dice input to the new derived component context */
607 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100608 if (err != DPE_NO_ERROR) {
609 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100610 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000611 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100612
Maulik Patela81605b2023-10-24 12:17:03 +0100613 /* Update parent idx in new derived component context */
614 derived_ctx->parent_idx = parent_ctx_idx;
615 /* Mark new derived component index as in use */
616 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000617 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000618 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100619 if (err != DPE_NO_ERROR) {
620 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100621 }
622
623 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100624 /* Retain and return parent handle with renewed nonce */
625 *new_parent_context_handle = input_ctx_handle;
626 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100627 if (err != DPE_NO_ERROR) {
628 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100629 }
Maulik Patela81605b2023-10-24 12:17:03 +0100630 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
631
Maulik Patelad2f3db2023-05-17 15:41:36 +0100632 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100633 /* Return invalid handle */
634 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100635 parent_ctx->nonce = INVALID_NONCE_VALUE;
636 }
637
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000638 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100639 /* Return handle to derived context */
640 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
641 err = renew_nonce(new_context_handle);
642 if (err != DPE_NO_ERROR) {
643 return err;
644 }
645 /* Update nonce in new derived component context */
646 derived_ctx->nonce = GET_NONCE(*new_context_handle);
647
648 } else {
649 /* Return invalid handle */
650 *new_context_handle = INVALID_HANDLE;
651 derived_ctx->nonce = INVALID_NONCE_VALUE;
652 }
653
Maulik Patel5ac87802024-03-14 14:22:19 +0000654 linked_layer_idx = derived_ctx->linked_layer_idx;
655 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
656 layer_ctx = &layer_ctx_array[linked_layer_idx];
Maulik Patela81605b2023-10-24 12:17:03 +0100657 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000658 layer_ctx->is_cdi_to_be_exported = export_cdi;
659
Maulik Patelcbded682023-12-07 11:50:16 +0000660 /* Finalise the layer */
661 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100662 err = prepare_layer_certificate(layer_ctx);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000663 if (err != DPE_NO_ERROR) {
664 return err;
665 }
666
667 if (return_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000668 /* Encode and return generated layer certificate */
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100669 err = encode_layer_certificate(layer_ctx,
Tamas Ban257471b2024-03-25 13:49:53 +0100670 new_certificate_buf,
671 new_certificate_buf_size,
672 new_certificate_actual_size);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000673 if (err != DPE_NO_ERROR) {
674 return err;
675 }
676 }
677 }
678
679 if (export_cdi) {
680 err = get_encoded_cdi_to_export(layer_ctx,
681 exported_cdi_buf,
682 exported_cdi_buf_size,
683 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100684 if (err != DPE_NO_ERROR) {
685 return err;
686 }
687 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000688 log_derive_context_output_handles(*new_parent_context_handle,
689 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100690
Maulik Patel5ac87802024-03-14 14:22:19 +0000691 /* Log context and layer info and certificate if no error */
692 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
693 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100694 if (return_certificate) {
Maulik Patel5ac87802024-03-14 14:22:19 +0000695 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100696 new_certificate_buf,
697 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000698 }
699
Maulik Patelad2f3db2023-05-17 15:41:36 +0100700 return DPE_NO_ERROR;
701}
Maulik Patel54d65f72023-06-28 13:04:36 +0100702
703dpe_error_t destroy_context_request(int input_ctx_handle,
704 bool destroy_recursively)
705{
706 uint16_t input_ctx_idx, linked_layer_idx;
707 int i;
708 bool is_layer_empty;
709
710 log_destroy_context(input_ctx_handle, destroy_recursively);
711
Maulik Patela81605b2023-10-24 12:17:03 +0100712 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100713 input_ctx_idx = GET_IDX(input_ctx_handle);
714
Maulik Patel54d65f72023-06-28 13:04:36 +0100715 /* Validate input handle */
716 if (!is_input_handle_valid(input_ctx_handle)) {
717 return DPE_INVALID_ARGUMENT;
718 }
719 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
720
Jamie Fox34681992023-09-04 18:14:06 +0100721#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100722 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
723 /* All layers till hypervisor cannot be destroyed dynamically */
724 return DPE_INVALID_ARGUMENT;
725 }
Jamie Fox34681992023-09-04 18:14:06 +0100726#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100727
728
729 if (!destroy_recursively) {
730 set_context_to_default(input_ctx_idx);
731 } else {
732 //TODO: To be implemented
733 }
734
735 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
736
737 /* Close the layer if all of its contexts are destroyed */
738 is_layer_empty = true;
739 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
740 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
741 /* There are active component context in the layer */
742 is_layer_empty = false;
743 break;
744 }
745 }
746
747 if (is_layer_empty) {
748 invalidate_layer(linked_layer_idx);
749 }
750
751 return DPE_NO_ERROR;
752}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100753
754struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
755 uint16_t component_idx)
756{
757 /* Safety case */
758 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
759 return NULL;
760 }
761
762 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
763 return &component_ctx_array[component_idx];
764 } else {
765 return NULL;
766 }
767}
768
Maulik Patele6adc112023-08-18 14:21:51 +0100769struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
770{
771 /* Safety case */
772 if (layer_idx >= MAX_NUM_OF_LAYERS) {
773 return NULL;
774 }
775
776 return &layer_ctx_array[layer_idx];
777}
778
779dpe_error_t certify_key_request(int input_ctx_handle,
780 bool retain_context,
781 const uint8_t *public_key,
782 size_t public_key_size,
783 const uint8_t *label,
784 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000785 uint8_t *certificate_buf,
786 size_t certificate_buf_size,
787 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100788 uint8_t *derived_public_key_buf,
789 size_t derived_public_key_buf_size,
790 size_t *derived_public_key_actual_size,
791 int *new_context_handle)
792{
793 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
794 dpe_error_t err;
795 psa_status_t status;
796 struct layer_context_t *parent_layer_ctx, *layer_ctx;
Maulik Patel7cc80872024-04-04 12:00:29 +0100797 struct layer_context_t leaf_layer;
Maulik Patele6adc112023-08-18 14:21:51 +0100798
799 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
800 label, label_size);
801
802 /* Validate input handle */
803 if (!is_input_handle_valid(input_ctx_handle)) {
804 return DPE_INVALID_ARGUMENT;
805 }
806
807 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
808 return DPE_INVALID_ARGUMENT;
809 }
810
811 /* Get component index from the input handle */
812 input_ctx_idx = GET_IDX(input_ctx_handle);
813 /* Get current linked layer idx */
814 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
815 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
816
817 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patel7cc80872024-04-04 12:00:29 +0100818 /* Create leaf layer as copy of input context linked layer */
819 memcpy(&leaf_layer, layer_ctx, sizeof(leaf_layer));
820
821 if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100822 return DPE_INVALID_ARGUMENT;
823 }
824
825 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel7cc80872024-04-04 12:00:29 +0100826 leaf_layer.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100827 /* Copy the public key provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100828 memcpy(&leaf_layer.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100829 public_key,
830 public_key_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100831 leaf_layer.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100832
833 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel7cc80872024-04-04 12:00:29 +0100834 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100835
836 } else {
837 /* No external public key is provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100838 leaf_layer.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100839
840 if ((label_size > 0) && (label != NULL)) {
841 /* Copy the label provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100842 memcpy(&leaf_layer.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100843 label,
844 label_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100845 leaf_layer.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100846
847 } else {
Maulik Patel7cc80872024-04-04 12:00:29 +0100848 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100849 }
850 }
851
852 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100853 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100854 */
Maulik Patelcbded682023-12-07 11:50:16 +0000855 /* Create leaf certificate */
Maulik Patel7cc80872024-04-04 12:00:29 +0100856 err = prepare_layer_certificate(&leaf_layer);
Tamas Ban257471b2024-03-25 13:49:53 +0100857 if (err != DPE_NO_ERROR) {
858 return err;
859 }
860
Maulik Patel7cc80872024-04-04 12:00:29 +0100861 err = encode_layer_certificate(&leaf_layer,
Tamas Ban257471b2024-03-25 13:49:53 +0100862 certificate_buf,
863 certificate_buf_size,
864 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100865 if (err != DPE_NO_ERROR) {
866 return err;
867 }
868
869 /* Get parent layer derived public key to verify the certificate signature */
Maulik Patel7cc80872024-04-04 12:00:29 +0100870 parent_layer_idx = leaf_layer.parent_layer_idx;
Maulik Patele6adc112023-08-18 14:21:51 +0100871 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
872 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
873
874 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
875 return DPE_INVALID_ARGUMENT;
876 }
877
878 memcpy(derived_public_key_buf,
879 &parent_layer_ctx->data.attest_pub_key[0],
880 parent_layer_ctx->data.attest_pub_key_len);
881 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
882
Maulik Patel91edd632024-02-26 07:44:41 +0000883 /* Renew handle for the same context, if requested */
884 if (retain_context) {
885 *new_context_handle = input_ctx_handle;
886 status = renew_nonce(new_context_handle);
887 if (status != PSA_SUCCESS) {
888 return DPE_INTERNAL_ERROR;
889 }
890 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
891
892 } else {
893 *new_context_handle = INVALID_HANDLE;
894 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100895 }
Maulik Patele6adc112023-08-18 14:21:51 +0100896
Maulik Patel9a2a5672024-03-14 13:43:58 +0000897 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000898 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100899 certificate_buf,
900 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100901
902 return DPE_NO_ERROR;
903}
Maulik Patel83a6b592023-12-05 15:20:30 +0000904
905dpe_error_t get_certificate_chain_request(int input_ctx_handle,
906 bool retain_context,
907 bool clear_from_context,
908 uint8_t *certificate_chain_buf,
909 size_t certificate_chain_buf_size,
910 size_t *certificate_chain_actual_size,
911 int *new_context_handle)
912{
913 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000914 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000915 psa_status_t status;
916 struct layer_context_t *layer_ctx;
917
Tamas Bana5e2f582024-01-25 16:59:26 +0100918 log_get_certificate_chain(input_ctx_handle, retain_context,
919 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000920
921 /* Validate input handle */
922 if (!is_input_handle_valid(input_ctx_handle)) {
923 return DPE_INVALID_ARGUMENT;
924 }
925
926 /* Get component index from the input handle */
927 input_ctx_idx = GET_IDX(input_ctx_handle);
928 /* Get current linked layer idx */
929 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
930 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
931
932 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100933 if (layer_ctx->state != LAYER_STATE_FINALISED) {
934 /* If the context has accumulated info and not yet part of a certificate,
935 * return an invalid-argument error
936 */
937 return DPE_INVALID_ARGUMENT;
938 }
939
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100940 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000941 certificate_chain_buf,
942 certificate_chain_buf_size,
943 certificate_chain_actual_size);
944 if (err != DPE_NO_ERROR) {
945 return err;
946 }
947
948 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
949
950 /* Renew handle for the same context, if requested */
951 if (retain_context) {
952 *new_context_handle = input_ctx_handle;
953 status = renew_nonce(new_context_handle);
954 if (status != PSA_SUCCESS) {
955 return DPE_INTERNAL_ERROR;
956 }
957 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
958
959 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +0100960 //TODO: Reimplement the clear_from_context functionality after memory
961 // optimization; Certificates are not ready made and they are not
962 // stored in the layer context anymore. They are created on-the-fly
963 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000964 }
965
966 } else {
967 *new_context_handle = INVALID_HANDLE;
968 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
969 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000970 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000971
972 return DPE_NO_ERROR;
973}