blob: e907b0cdc88ddc5030e59d3f5e396585b32d8e21 [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
Tamas Bana5e2f582024-01-25 16:59:26 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Maulik Patelad2f3db2023-05-17 15:41:36 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_context_mngr.h"
9#include <assert.h>
10#include <string.h>
Maulik Patel009450d2024-04-23 12:03:10 +010011#include "array.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010012#include "dice_protection_environment.h"
Maulik Patel2358bbb2023-07-21 10:56:56 +010013#include "dpe_certificate.h"
Maulik Patelcb14cde2024-01-23 12:39:53 +000014#include "dpe_client.h"
Maulik Patel58595d32023-06-22 10:08:53 +010015#include "dpe_crypto_interface.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010016#include "dpe_log.h"
Jamie Fox34681992023-09-04 18:14:06 +010017#include "dpe_plat.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010018#include "psa/crypto.h"
19
Maulik Pateldbfd5152023-05-30 17:02:42 +010020#ifdef DPE_TEST_MODE
21#define TEST_ROT_CDI_VAL { \
22 0xD2, 0x90, 0x66, 0x07, 0x2A, 0x2D, 0x2A, 0x00, \
23 0x91, 0x9D, 0xD9, 0x15, 0x14, 0xBE, 0x2D, 0xCC, \
24 0xA3, 0x9F, 0xDE, 0xC3, 0x35, 0x75, 0x84, 0x6E, \
25 0x4C, 0xB9, 0x28, 0xAC, 0x7A, 0x4E, 0X00, 0x7F \
26 }
27#endif /* DPE_TEST_MODE */
28
Maulik Patel58595d32023-06-22 10:08:53 +010029#define CONTEXT_DATA_MAX_SIZE sizeof(struct component_context_data_t)
30
Maulik Patelad2f3db2023-05-17 15:41:36 +010031static struct component_context_t component_ctx_array[MAX_NUM_OF_COMPONENTS];
32static struct layer_context_t layer_ctx_array[MAX_NUM_OF_LAYERS];
33
Maulik Patel009450d2024-04-23 12:03:10 +010034static dpe_error_t store_linked_component(struct layer_context_t *layer_ctx,
35 int component_idx)
36{
37 if (layer_ctx->linked_components.count >=
38 ARRAY_SIZE(layer_ctx->linked_components.idx)) {
39 /* linked_components.idx[] is full */
40 return DPE_INSUFFICIENT_MEMORY;
41 }
42
43 layer_ctx->linked_components.idx[layer_ctx->linked_components.count] = component_idx;
44 layer_ctx->linked_components.count++;
45
46 return DPE_NO_ERROR;
47}
48
49static void remove_linked_component(struct layer_context_t *layer_ctx,
50 int component_idx)
51{
52 int i, pos;
53
54 /* Find the position of the input component */
55 for (i = 0; i < ARRAY_SIZE(layer_ctx->linked_components.idx); i++) {
56 if (layer_ctx->linked_components.idx[i] == component_idx) {
57 pos = i;
58 break;
59 }
60 }
61
62 assert(i < ARRAY_SIZE(layer_ctx->linked_components.idx));
63
64 /* Left shift remaining elements by 1 from current position */
65 for(i = pos; i < ARRAY_SIZE(layer_ctx->linked_components.idx) - 1; i++) {
66 layer_ctx->linked_components.idx[i] = layer_ctx->linked_components.idx[i + 1];
67 }
68 layer_ctx->linked_components.idx[i] = INVALID_LAYER_IDX;
69 layer_ctx->linked_components.count--;
70}
71
Maulik Patelad2f3db2023-05-17 15:41:36 +010072static int get_free_component_context_index(void)
73{
74 int i;
75
76 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
77 if (!component_ctx_array[i].in_use) {
78 break;
79 }
80 }
81
82 if (i >= MAX_NUM_OF_COMPONENTS) {
83 /* No free index left in the array -- all used up! */
84 return -1;
85 }
86
87 return i;
88}
89
Maulik Patelad2f3db2023-05-17 15:41:36 +010090static dpe_error_t renew_nonce(int *handle)
91{
92 uint16_t nonce;
93
94 psa_status_t status = psa_generate_random((uint8_t *)&nonce, sizeof(nonce));
95 if (status != PSA_SUCCESS) {
96 return DPE_INTERNAL_ERROR;
97 }
98 *handle = SET_NONCE(*handle, nonce);
99
100 return DPE_NO_ERROR;
101}
102
Maulik Patelad2f3db2023-05-17 15:41:36 +0100103static void set_context_to_default(int i)
104{
105 component_ctx_array[i].in_use = false;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000106 component_ctx_array[i].is_allowed_to_derive = true;
107 /* export CDI attribute is inherited and once disabled, a derived context
108 * and subsequent derivations cannot export CDI, hence enable by default
109 */
110 component_ctx_array[i].is_export_cdi_allowed = true;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100111 component_ctx_array[i].nonce = INVALID_NONCE_VALUE;
112 component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX;
113 component_ctx_array[i].linked_layer_idx = INVALID_LAYER_IDX;
114 (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t));
115 //TODO: Question: how to initialise MHU Id mapping?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000116 component_ctx_array[i].target_locality = 0;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100117 /* Allow component to be derived by default */
118}
119
120static void invalidate_layer(int i)
121{
Maulik Patel009450d2024-04-23 12:03:10 +0100122 int j;
123
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100124 layer_ctx_array[i].idx = i;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100125 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
126 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000127 layer_ctx_array[i].is_cdi_to_be_exported = false;
Maulik Patel87c47ce2024-04-22 13:30:56 +0100128 layer_ctx_array[i].is_rot_layer = false;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000129 layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID;
Maulik Patele6adc112023-08-18 14:21:51 +0100130 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
131 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patel58595d32023-06-22 10:08:53 +0100132 (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id);
133 (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100134 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
Maulik Patel009450d2024-04-23 12:03:10 +0100135 layer_ctx_array[i].linked_components.count = 0;
136 for (j = 0; j < ARRAY_SIZE(layer_ctx_array[i].linked_components.idx); j++) {
137 layer_ctx_array[i].linked_components.idx[j] = INVALID_COMPONENT_IDX;
138 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100139}
140
Maulik Patelad2f3db2023-05-17 15:41:36 +0100141static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
142 const DiceInputValues *dice_inputs)
143{
144 size_t hash_len;
145 psa_status_t status;
146
147 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
148 DICE_HASH_SIZE);
149 memcpy(&dest_ctx->data.measurement_descriptor,
150 dice_inputs->code_descriptor,
151 dice_inputs->code_descriptor_size);
152
153 dest_ctx->data.measurement_descriptor_size =
154 dice_inputs->code_descriptor_size;
155
156 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
157 memcpy(&dest_ctx->data.signer_id_descriptor,
158 dice_inputs->authority_descriptor,
159 dice_inputs->authority_descriptor_size);
160
161 dest_ctx->data.signer_id_descriptor_size =
162 dice_inputs->authority_descriptor_size;
163
164 if (dice_inputs->config_type == kDiceConfigTypeInline) {
165 /* Copy config_value */
166 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
167 DICE_INLINE_CONFIG_SIZE);
168
169 } else {
170 /* Copy config descriptor */
171 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
172 dice_inputs->config_descriptor_size);
173 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
174
175 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100176 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100177 dice_inputs->config_descriptor,
178 dice_inputs->config_descriptor_size,
179 dest_ctx->data.config_value,
180 sizeof(dest_ctx->data.config_value),
181 &hash_len);
182
183 if (status != PSA_SUCCESS) {
184 return DPE_INTERNAL_ERROR;
185 }
186 }
187
188 dest_ctx->data.mode = dice_inputs->mode;
189 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
190
191 return DPE_NO_ERROR;
192}
193
194static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
195{
196 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
197 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
198 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
199 return false;
200 }
201
202 return true;
203}
204
205static bool is_input_handle_valid(int input_context_handle)
206{
207 uint16_t idx = GET_IDX(input_context_handle);
208 uint16_t nonce = GET_NONCE(input_context_handle);
209
210 /* Validate input handle id and nonce */
211 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
212 return false;
213 }
214
215 if (nonce == component_ctx_array[idx].nonce) {
216 return true;
217 }
218
219 return false;
220}
221
Maulik Patel58595d32023-06-22 10:08:53 +0100222/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
223 * same order
224 */
225static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
226 size_t max_size,
227 size_t *dest_size,
228 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100229{
Maulik Patel58595d32023-06-22 10:08:53 +0100230 size_t out_size = 0;
231
232 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
233 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
234 return PSA_ERROR_BUFFER_TOO_SMALL;
235 }
236
237 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
238 out_size += DICE_HASH_SIZE;
239
240 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
241 out_size += DICE_INLINE_CONFIG_SIZE;
242
243 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
244 out_size += DICE_HASH_SIZE;
245
246 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
247 out_size += sizeof(comp_ctx->data.mode);
248
249 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
250 out_size += DICE_HIDDEN_SIZE;
251
252 *dest_size = out_size;
253
254 return PSA_SUCCESS;
255}
256
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100257static psa_status_t compute_layer_cdi_attest_input(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100258{
259 psa_status_t status;
260 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
261 size_t ctx_data_size, hash_len;
Maulik Patel009450d2024-04-23 12:03:10 +0100262 int i, idx;
263 uint16_t num_of_linked_components;
264
265 num_of_linked_components = layer_ctx->linked_components.count;
266 if (num_of_linked_components == 0) {
267 /* No components to hash */
268 return PSA_SUCCESS;
269 }
Maulik Patel58595d32023-06-22 10:08:53 +0100270
271 psa_hash_operation_t hash_op = psa_hash_operation_init();
272 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
273 if (status != PSA_SUCCESS) {
274 return status;
275 }
276
277 //TODO:
278 /* How to combine measurements of multiple SW components into a single hash
279 * is not yet defined by the Open DICE profile. This implementation
280 * concatenates the data of all SW components which belong to the same layer
281 * and hash it.
282 */
Maulik Patel009450d2024-04-23 12:03:10 +0100283 for (i = 0; i < num_of_linked_components; i++) {
284 idx = layer_ctx->linked_components.idx[i];
285 status = get_component_data_for_attest_cdi(component_ctx_data,
286 sizeof(component_ctx_data),
287 &ctx_data_size,
288 &component_ctx_array[idx]);
289 if (status != PSA_SUCCESS) {
290 return status;
291 }
Maulik Patel58595d32023-06-22 10:08:53 +0100292
Maulik Patel009450d2024-04-23 12:03:10 +0100293 status = psa_hash_update(&hash_op,
294 component_ctx_data,
295 ctx_data_size);
296 if (status != PSA_SUCCESS) {
297 return status;
Maulik Patel58595d32023-06-22 10:08:53 +0100298 }
299 }
300
301 status = psa_hash_finish(&hash_op,
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100302 &layer_ctx->attest_cdi_hash_input[0],
303 sizeof(layer_ctx->attest_cdi_hash_input),
Maulik Patel58595d32023-06-22 10:08:53 +0100304 &hash_len);
305
306 assert(hash_len == DPE_HASH_ALG_SIZE);
307
308 return status;
309}
310
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000311static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
312 uint8_t *exported_cdi_buf,
313 size_t exported_cdi_buf_size,
314 size_t *exported_cdi_actual_size)
315{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100316 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
317 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000318 psa_status_t status;
319 dpe_error_t err;
320
Tamas Ban5179a4d2024-01-25 17:05:30 +0100321 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000322 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100323 cdi_attest_buf,
324 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000325 if (status != PSA_SUCCESS) {
326 return DPE_INTERNAL_ERROR;
327 }
328
329 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100330 err = encode_cdi(cdi_attest_buf,
331 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000332 exported_cdi_buf,
333 exported_cdi_buf_size,
334 exported_cdi_actual_size);
335 if (err != DPE_NO_ERROR) {
336 return err;
337 }
338 layer_ctx->is_cdi_to_be_exported = true;
339
340 return DPE_NO_ERROR;
341}
342
Maulik Patel009450d2024-04-23 12:03:10 +0100343static dpe_error_t prepare_layer_certificate(struct layer_context_t *layer_ctx,
344 const struct layer_context_t *parent_layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100345{
Maulik Patel58595d32023-06-22 10:08:53 +0100346 psa_status_t status;
Maulik Patel58595d32023-06-22 10:08:53 +0100347
Maulik Patel2358bbb2023-07-21 10:56:56 +0100348 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patel87c47ce2024-04-22 13:30:56 +0100349 if ((!layer_ctx->is_rot_layer) &&
Maulik Patele6adc112023-08-18 14:21:51 +0100350 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100351
Maulik Patele6adc112023-08-18 14:21:51 +0100352 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100353
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100354 status = compute_layer_cdi_attest_input(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100355 if (status != PSA_SUCCESS) {
356 return DPE_INTERNAL_ERROR;
357 }
358
Maulik Patele6adc112023-08-18 14:21:51 +0100359 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100360 if (status != PSA_SUCCESS) {
361 return DPE_INTERNAL_ERROR;
362 }
363
Maulik Patele6adc112023-08-18 14:21:51 +0100364 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100365 if (status != PSA_SUCCESS) {
366 return DPE_INTERNAL_ERROR;
367 }
368 }
369
Maulik Patele6adc112023-08-18 14:21:51 +0100370 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100371 if (status != PSA_SUCCESS) {
372 return DPE_INTERNAL_ERROR;
373 }
374
Maulik Patele6adc112023-08-18 14:21:51 +0100375 if (!layer_ctx->is_external_pub_key_provided) {
376 status = derive_attestation_key(layer_ctx);
377 if (status != PSA_SUCCESS) {
378 return DPE_INTERNAL_ERROR;
379 }
Maulik Patel58595d32023-06-22 10:08:53 +0100380 }
381
Maulik Patele6adc112023-08-18 14:21:51 +0100382 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100383 if (status != PSA_SUCCESS) {
384 return DPE_INTERNAL_ERROR;
385 }
386
Tamas Ban257471b2024-03-25 13:49:53 +0100387 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100388}
389
390static uint16_t open_new_layer(void)
391{
392 int i;
393
394 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
395 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
396 layer_ctx_array[i].state = LAYER_STATE_OPEN;
397 return i;
398 }
399 }
400
Maulik Patel2358bbb2023-07-21 10:56:56 +0100401 //TODO: There is an open issue of layer creation as described below.
402 /* This is causing extra unintended layers to open. Since each layer
403 * has some context data and certificate buffer of 3k, it is
404 * causing RAM overflow. Hence until resoluton is reached, once all
405 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100406 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
407 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
408 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
409 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100410 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
411 */
412 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100413}
414
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000415static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100416{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000417 // TODO: FIXME
418 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100419 return true;
420}
421
Maulik Patelcb14cde2024-01-23 12:39:53 +0000422static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100423{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000424 int i;
425
426 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
427 if (layer_ctx_array[i].cert_id == cert_id) {
428 *layer_idx = i;
429 return true;
430 }
431 }
432
433 /* No certificate ID match found */
434 return false;
435}
436
437static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
438 uint32_t cert_id)
439{
440 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100441
442 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
443
444 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
445 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
446
Maulik Patelcb14cde2024-01-23 12:39:53 +0000447 if (cert_id != DPE_CERT_ID_INVALID) {
448 /* cert id was sent by the client */
449 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
450 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
451 /* Cannot add to the layer which is already finalised */
452 return DPE_INTERNAL_ERROR;
453 }
454 /* Derived context belongs to the same certificate as its parent component */
455 new_ctx->linked_layer_idx = parent_layer_idx;
456
457 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
458 /* Cert ID is already in use */
459 if (layer_ctx_array[layer_idx_to_link].state == LAYER_STATE_FINALISED) {
460 /* Cannot add to the layer which is already finalised */
461 return DPE_INTERNAL_ERROR;
462 }
463 /* Use the same layer that is associated with cert_id */
464 new_ctx->linked_layer_idx = layer_idx_to_link;
465 /* Linked layer's parent is already assigned when it was opened */
466
467 } else {
468 /* Open new layer and link derived context to new layer */
469 layer_idx_to_link = open_new_layer();
470 if (layer_idx_to_link == INVALID_LAYER_IDX) {
471 return DPE_INTERNAL_ERROR;
472 }
473 /* Link this context to the new layer */
474 new_ctx->linked_layer_idx = layer_idx_to_link;
475 /* New layer's parent is parent component's layer */
476 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
477 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100478 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100479
480 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000481 /* cert id was not sent by the client */
482 //TODO: To be implemented; return error for now.
483 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100484 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100485
486 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100487}
488
Jamie Fox34681992023-09-04 18:14:06 +0100489/**
490 * \brief Create a root of trust component context.
491 *
492 * \param[out] rot_ctx_handle A new context handle for the RoT context.
493 *
494 * \return Returns error code of type dpe_error_t
495 */
496static dpe_error_t create_rot_context(int *rot_ctx_handle)
497{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100498#ifdef DPE_TEST_MODE
499 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
500#else
Jamie Fox34681992023-09-04 18:14:06 +0100501 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100502#endif /* DPE_TEST_MODE */
503 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100504 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
505 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
506
Maulik Patel87c47ce2024-04-22 13:30:56 +0100507 rot_layer_ctx->is_rot_layer = true;
Maulik Patela81605b2023-10-24 12:17:03 +0100508 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100509 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
510
Maulik Pateldbfd5152023-05-30 17:02:42 +0100511#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100512 /* Get the RoT CDI input for the RoT layer */
Antonio de Angelis30211a62024-04-04 12:48:18 +0100513 status = get_rot_cdi_input(&rot_cdi_input[0], sizeof(rot_cdi_input));
514 if (status != PSA_SUCCESS) {
Jamie Fox34681992023-09-04 18:14:06 +0100515 return DPE_INTERNAL_ERROR;
516 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100517#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100518
519 /* Import the CDI key for the RoT layer */
520 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
521 &rot_cdi_input[0],
522 sizeof(rot_cdi_input));
523 if (status != PSA_SUCCESS) {
524 return DPE_INTERNAL_ERROR;
525 }
526
Maulik Patela81605b2023-10-24 12:17:03 +0100527 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100528 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100529 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100530 rot_comp_ctx->parent_idx = 0;
531 /* Link context to RoT Layer */
532 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
533 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100534 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
535
536 return DPE_NO_ERROR;
537}
538
539dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
540{
541 int i;
542
543 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
544 set_context_to_default(i);
545 }
546
547 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
548 invalidate_layer(i);
549 }
550
551 return create_rot_context(rot_ctx_handle);
552}
553
Maulik Patela81605b2023-10-24 12:17:03 +0100554dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000555 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100556 bool retain_parent_context,
557 bool allow_new_context_to_derive,
558 bool create_certificate,
559 const DiceInputValues *dice_inputs,
560 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000561 int32_t target_locality,
562 bool return_certificate,
563 bool allow_new_context_to_export,
564 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100565 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000566 int *new_parent_context_handle,
567 uint8_t *new_certificate_buf,
568 size_t new_certificate_buf_size,
569 size_t *new_certificate_actual_size,
570 uint8_t *exported_cdi_buf,
571 size_t exported_cdi_buf_size,
572 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100573{
Maulik Patel58595d32023-06-22 10:08:53 +0100574 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100575 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel009450d2024-04-23 12:03:10 +0100576 uint16_t parent_ctx_idx, linked_layer_idx, parent_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100577 int free_component_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100578 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100579
Maulik Patelcb14cde2024-01-23 12:39:53 +0000580 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100581 allow_new_context_to_derive, create_certificate, dice_inputs,
582 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100583
Maulik Pateldbfd5152023-05-30 17:02:42 +0100584#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100585 if ((input_ctx_handle == 0) &&
586 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100587 /* Deriving RoT context for tests */
588 err = create_rot_context(&input_ctx_handle);
589 if (err != DPE_NO_ERROR) {
590 return err;
591 }
592 }
593#endif /* DPE_TEST_MODE */
594
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000595 if (export_cdi && !create_certificate) {
596 return DPE_INVALID_ARGUMENT;
597 }
598
Maulik Patelad2f3db2023-05-17 15:41:36 +0100599 /* Validate dice inputs */
600 if (!is_dice_input_valid(dice_inputs)) {
601 return DPE_INVALID_ARGUMENT;
602 }
603
604 /* Validate input handle */
605 if (!is_input_handle_valid(input_ctx_handle)) {
606 return DPE_INVALID_ARGUMENT;
607 }
Maulik Patela81605b2023-10-24 12:17:03 +0100608 /* Get parent component index from the input handle */
609 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100610
611 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100612 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100613 */
Maulik Patela81605b2023-10-24 12:17:03 +0100614 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100615
Maulik Patela81605b2023-10-24 12:17:03 +0100616 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100617
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000618 /* Check if parent context is allowed to derive */
619 if (!parent_ctx->is_allowed_to_derive) {
620 return DPE_INVALID_ARGUMENT;
621 }
622
Maulik Patelad2f3db2023-05-17 15:41:36 +0100623 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000624 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100625 return DPE_INVALID_ARGUMENT;
626 }
627
Maulik Patela81605b2023-10-24 12:17:03 +0100628 /* Get next free component index to add new derived context */
629 free_component_idx = get_free_component_context_index();
630 if (free_component_idx < 0) {
631 return DPE_INSUFFICIENT_MEMORY;
632 }
633
634 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000635 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
636 /* If parent context has export enabled and input allow_new_context_to_export
637 * is true, then allow context CDI to be exported for derived context
638 */
639 derived_ctx->is_export_cdi_allowed = true;
640 } else {
641 /* Export of new context CDI is NOT allowed */
642 derived_ctx->is_export_cdi_allowed = false;
643 if (export_cdi) {
644 return DPE_INVALID_ARGUMENT;
645 }
646 }
647
Maulik Patela81605b2023-10-24 12:17:03 +0100648 /* Copy dice input to the new derived component context */
649 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100650 if (err != DPE_NO_ERROR) {
651 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100652 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000653 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100654
Maulik Patela81605b2023-10-24 12:17:03 +0100655 /* Update parent idx in new derived component context */
656 derived_ctx->parent_idx = parent_ctx_idx;
657 /* Mark new derived component index as in use */
658 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000659 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000660 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100661 if (err != DPE_NO_ERROR) {
662 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100663 }
664
665 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100666 /* Retain and return parent handle with renewed nonce */
667 *new_parent_context_handle = input_ctx_handle;
668 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100669 if (err != DPE_NO_ERROR) {
670 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100671 }
Maulik Patela81605b2023-10-24 12:17:03 +0100672 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
673
Maulik Patelad2f3db2023-05-17 15:41:36 +0100674 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100675 /* Return invalid handle */
676 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100677 parent_ctx->nonce = INVALID_NONCE_VALUE;
678 }
679
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000680 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100681 /* Return handle to derived context */
682 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
683 err = renew_nonce(new_context_handle);
684 if (err != DPE_NO_ERROR) {
685 return err;
686 }
687 /* Update nonce in new derived component context */
688 derived_ctx->nonce = GET_NONCE(*new_context_handle);
689
690 } else {
691 /* Return invalid handle */
692 *new_context_handle = INVALID_HANDLE;
693 derived_ctx->nonce = INVALID_NONCE_VALUE;
694 }
695
Maulik Patel5ac87802024-03-14 14:22:19 +0000696 linked_layer_idx = derived_ctx->linked_layer_idx;
697 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
698 layer_ctx = &layer_ctx_array[linked_layer_idx];
Maulik Patel009450d2024-04-23 12:03:10 +0100699 err = store_linked_component(layer_ctx, free_component_idx);
700 if (err != DPE_NO_ERROR) {
701 return err;
702 }
703 parent_layer_idx = layer_ctx->parent_layer_idx;
704 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
705 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
706
Maulik Patela81605b2023-10-24 12:17:03 +0100707 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000708 layer_ctx->is_cdi_to_be_exported = export_cdi;
709
Maulik Patelcbded682023-12-07 11:50:16 +0000710 /* Finalise the layer */
711 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patel009450d2024-04-23 12:03:10 +0100712 err = prepare_layer_certificate(layer_ctx, parent_layer_ctx);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000713 if (err != DPE_NO_ERROR) {
714 return err;
715 }
716
717 if (return_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000718 /* Encode and return generated layer certificate */
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100719 err = encode_layer_certificate(layer_ctx,
Tamas Ban257471b2024-03-25 13:49:53 +0100720 new_certificate_buf,
721 new_certificate_buf_size,
722 new_certificate_actual_size);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000723 if (err != DPE_NO_ERROR) {
724 return err;
725 }
726 }
727 }
728
729 if (export_cdi) {
730 err = get_encoded_cdi_to_export(layer_ctx,
731 exported_cdi_buf,
732 exported_cdi_buf_size,
733 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100734 if (err != DPE_NO_ERROR) {
735 return err;
736 }
737 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000738 log_derive_context_output_handles(*new_parent_context_handle,
739 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100740
Maulik Patel5ac87802024-03-14 14:22:19 +0000741 /* Log context and layer info and certificate if no error */
742 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
743 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100744 if (return_certificate) {
Maulik Patel5ac87802024-03-14 14:22:19 +0000745 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100746 new_certificate_buf,
747 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000748 }
749
Maulik Patelad2f3db2023-05-17 15:41:36 +0100750 return DPE_NO_ERROR;
751}
Maulik Patel54d65f72023-06-28 13:04:36 +0100752
753dpe_error_t destroy_context_request(int input_ctx_handle,
754 bool destroy_recursively)
755{
756 uint16_t input_ctx_idx, linked_layer_idx;
757 int i;
758 bool is_layer_empty;
Maulik Patel009450d2024-04-23 12:03:10 +0100759 struct layer_context_t *layer_ctx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100760
761 log_destroy_context(input_ctx_handle, destroy_recursively);
762
Maulik Patela81605b2023-10-24 12:17:03 +0100763 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100764 input_ctx_idx = GET_IDX(input_ctx_handle);
765
Maulik Patel54d65f72023-06-28 13:04:36 +0100766 /* Validate input handle */
767 if (!is_input_handle_valid(input_ctx_handle)) {
768 return DPE_INVALID_ARGUMENT;
769 }
770 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
771
Jamie Fox34681992023-09-04 18:14:06 +0100772#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100773 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
774 /* All layers till hypervisor cannot be destroyed dynamically */
775 return DPE_INVALID_ARGUMENT;
776 }
Jamie Fox34681992023-09-04 18:14:06 +0100777#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100778
Maulik Patel009450d2024-04-23 12:03:10 +0100779 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patel54d65f72023-06-28 13:04:36 +0100780
781 if (!destroy_recursively) {
782 set_context_to_default(input_ctx_idx);
Maulik Patel009450d2024-04-23 12:03:10 +0100783 layer_ctx = &layer_ctx_array[linked_layer_idx];
784 remove_linked_component(layer_ctx, input_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100785 } else {
786 //TODO: To be implemented
787 }
788
Maulik Patel54d65f72023-06-28 13:04:36 +0100789 /* Close the layer if all of its contexts are destroyed */
790 is_layer_empty = true;
791 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
792 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
793 /* There are active component context in the layer */
794 is_layer_empty = false;
795 break;
796 }
797 }
798
799 if (is_layer_empty) {
800 invalidate_layer(linked_layer_idx);
801 }
802
803 return DPE_NO_ERROR;
804}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100805
Maulik Patel009450d2024-04-23 12:03:10 +0100806struct component_context_t* get_component_ctx_ptr(uint16_t component_idx)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100807{
808 /* Safety case */
809 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
810 return NULL;
811 }
812
Maulik Patel009450d2024-04-23 12:03:10 +0100813 return &component_ctx_array[component_idx];
Maulik Patel2358bbb2023-07-21 10:56:56 +0100814}
815
Maulik Patele6adc112023-08-18 14:21:51 +0100816struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
817{
818 /* Safety case */
819 if (layer_idx >= MAX_NUM_OF_LAYERS) {
820 return NULL;
821 }
822
823 return &layer_ctx_array[layer_idx];
824}
825
826dpe_error_t certify_key_request(int input_ctx_handle,
827 bool retain_context,
828 const uint8_t *public_key,
829 size_t public_key_size,
830 const uint8_t *label,
831 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000832 uint8_t *certificate_buf,
833 size_t certificate_buf_size,
834 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100835 uint8_t *derived_public_key_buf,
836 size_t derived_public_key_buf_size,
837 size_t *derived_public_key_actual_size,
838 int *new_context_handle)
839{
840 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
841 dpe_error_t err;
842 psa_status_t status;
843 struct layer_context_t *parent_layer_ctx, *layer_ctx;
Maulik Patel7cc80872024-04-04 12:00:29 +0100844 struct layer_context_t leaf_layer;
Maulik Patele6adc112023-08-18 14:21:51 +0100845
846 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
847 label, label_size);
848
849 /* Validate input handle */
850 if (!is_input_handle_valid(input_ctx_handle)) {
851 return DPE_INVALID_ARGUMENT;
852 }
853
854 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
855 return DPE_INVALID_ARGUMENT;
856 }
857
858 /* Get component index from the input handle */
859 input_ctx_idx = GET_IDX(input_ctx_handle);
860 /* Get current linked layer idx */
861 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
862 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
863
864 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patel7cc80872024-04-04 12:00:29 +0100865 /* Create leaf layer as copy of input context linked layer */
866 memcpy(&leaf_layer, layer_ctx, sizeof(leaf_layer));
867
868 if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100869 return DPE_INVALID_ARGUMENT;
870 }
871
872 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel7cc80872024-04-04 12:00:29 +0100873 leaf_layer.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100874 /* Copy the public key provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100875 memcpy(&leaf_layer.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100876 public_key,
877 public_key_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100878 leaf_layer.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100879
880 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel7cc80872024-04-04 12:00:29 +0100881 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100882
883 } else {
884 /* No external public key is provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100885 leaf_layer.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100886
887 if ((label_size > 0) && (label != NULL)) {
888 /* Copy the label provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100889 memcpy(&leaf_layer.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100890 label,
891 label_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100892 leaf_layer.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100893
894 } else {
Maulik Patel7cc80872024-04-04 12:00:29 +0100895 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100896 }
897 }
898
Maulik Patel009450d2024-04-23 12:03:10 +0100899 /* Get parent layer derived public key to verify the certificate signature */
900 parent_layer_idx = leaf_layer.parent_layer_idx;
901 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
902 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
903
Maulik Patele6adc112023-08-18 14:21:51 +0100904 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100905 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100906 */
Maulik Patelcbded682023-12-07 11:50:16 +0000907 /* Create leaf certificate */
Maulik Patel009450d2024-04-23 12:03:10 +0100908 err = prepare_layer_certificate(&leaf_layer, parent_layer_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100909 if (err != DPE_NO_ERROR) {
910 return err;
911 }
912
Maulik Patel7cc80872024-04-04 12:00:29 +0100913 err = encode_layer_certificate(&leaf_layer,
Tamas Ban257471b2024-03-25 13:49:53 +0100914 certificate_buf,
915 certificate_buf_size,
916 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100917 if (err != DPE_NO_ERROR) {
918 return err;
919 }
920
Maulik Patele6adc112023-08-18 14:21:51 +0100921 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
922 return DPE_INVALID_ARGUMENT;
923 }
924
925 memcpy(derived_public_key_buf,
926 &parent_layer_ctx->data.attest_pub_key[0],
927 parent_layer_ctx->data.attest_pub_key_len);
928 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
929
Maulik Patel91edd632024-02-26 07:44:41 +0000930 /* Renew handle for the same context, if requested */
931 if (retain_context) {
932 *new_context_handle = input_ctx_handle;
933 status = renew_nonce(new_context_handle);
934 if (status != PSA_SUCCESS) {
935 return DPE_INTERNAL_ERROR;
936 }
937 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
938
939 } else {
940 *new_context_handle = INVALID_HANDLE;
941 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100942 }
Maulik Patele6adc112023-08-18 14:21:51 +0100943
Maulik Patel9a2a5672024-03-14 13:43:58 +0000944 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000945 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100946 certificate_buf,
947 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100948
949 return DPE_NO_ERROR;
950}
Maulik Patel83a6b592023-12-05 15:20:30 +0000951
952dpe_error_t get_certificate_chain_request(int input_ctx_handle,
953 bool retain_context,
954 bool clear_from_context,
955 uint8_t *certificate_chain_buf,
956 size_t certificate_chain_buf_size,
957 size_t *certificate_chain_actual_size,
958 int *new_context_handle)
959{
960 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000961 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000962 psa_status_t status;
963 struct layer_context_t *layer_ctx;
964
Tamas Bana5e2f582024-01-25 16:59:26 +0100965 log_get_certificate_chain(input_ctx_handle, retain_context,
966 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000967
968 /* Validate input handle */
969 if (!is_input_handle_valid(input_ctx_handle)) {
970 return DPE_INVALID_ARGUMENT;
971 }
972
973 /* Get component index from the input handle */
974 input_ctx_idx = GET_IDX(input_ctx_handle);
975 /* Get current linked layer idx */
976 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
977 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
978
979 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100980 if (layer_ctx->state != LAYER_STATE_FINALISED) {
981 /* If the context has accumulated info and not yet part of a certificate,
982 * return an invalid-argument error
983 */
984 return DPE_INVALID_ARGUMENT;
985 }
986
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100987 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000988 certificate_chain_buf,
989 certificate_chain_buf_size,
990 certificate_chain_actual_size);
991 if (err != DPE_NO_ERROR) {
992 return err;
993 }
994
995 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
996
997 /* Renew handle for the same context, if requested */
998 if (retain_context) {
999 *new_context_handle = input_ctx_handle;
1000 status = renew_nonce(new_context_handle);
1001 if (status != PSA_SUCCESS) {
1002 return DPE_INTERNAL_ERROR;
1003 }
1004 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
1005
1006 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +01001007 //TODO: Reimplement the clear_from_context functionality after memory
1008 // optimization; Certificates are not ready made and they are not
1009 // stored in the layer context anymore. They are created on-the-fly
1010 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +00001011 }
1012
1013 } else {
1014 *new_context_handle = INVALID_HANDLE;
1015 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
1016 }
Maulik Patel9a2a5672024-03-14 13:43:58 +00001017 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +00001018
1019 return DPE_NO_ERROR;
1020}