blob: 7fefc311390c9d8ec9e90e9350bce21ea6801164 [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
Tamas Bana5e2f582024-01-25 16:59:26 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Maulik Patelad2f3db2023-05-17 15:41:36 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_context_mngr.h"
9#include <assert.h>
10#include <string.h>
Maulik Patel009450d2024-04-23 12:03:10 +010011#include "array.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010012#include "dice_protection_environment.h"
Maulik Patel2358bbb2023-07-21 10:56:56 +010013#include "dpe_certificate.h"
Maulik Patelcb14cde2024-01-23 12:39:53 +000014#include "dpe_client.h"
Maulik Patel58595d32023-06-22 10:08:53 +010015#include "dpe_crypto_interface.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010016#include "dpe_log.h"
Jamie Fox34681992023-09-04 18:14:06 +010017#include "dpe_plat.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010018#include "psa/crypto.h"
19
Maulik Patel58595d32023-06-22 10:08:53 +010020#define CONTEXT_DATA_MAX_SIZE sizeof(struct component_context_data_t)
21
Maulik Patelad2f3db2023-05-17 15:41:36 +010022static struct component_context_t component_ctx_array[MAX_NUM_OF_COMPONENTS];
23static struct layer_context_t layer_ctx_array[MAX_NUM_OF_LAYERS];
24
Maulik Patel009450d2024-04-23 12:03:10 +010025static dpe_error_t store_linked_component(struct layer_context_t *layer_ctx,
26 int component_idx)
27{
28 if (layer_ctx->linked_components.count >=
29 ARRAY_SIZE(layer_ctx->linked_components.idx)) {
30 /* linked_components.idx[] is full */
31 return DPE_INSUFFICIENT_MEMORY;
32 }
33
34 layer_ctx->linked_components.idx[layer_ctx->linked_components.count] = component_idx;
35 layer_ctx->linked_components.count++;
36
37 return DPE_NO_ERROR;
38}
39
40static void remove_linked_component(struct layer_context_t *layer_ctx,
41 int component_idx)
42{
43 int i, pos;
44
45 /* Find the position of the input component */
46 for (i = 0; i < ARRAY_SIZE(layer_ctx->linked_components.idx); i++) {
47 if (layer_ctx->linked_components.idx[i] == component_idx) {
48 pos = i;
49 break;
50 }
51 }
52
53 assert(i < ARRAY_SIZE(layer_ctx->linked_components.idx));
54
55 /* Left shift remaining elements by 1 from current position */
56 for(i = pos; i < ARRAY_SIZE(layer_ctx->linked_components.idx) - 1; i++) {
57 layer_ctx->linked_components.idx[i] = layer_ctx->linked_components.idx[i + 1];
58 }
59 layer_ctx->linked_components.idx[i] = INVALID_LAYER_IDX;
60 layer_ctx->linked_components.count--;
61}
62
Maulik Patelad2f3db2023-05-17 15:41:36 +010063static int get_free_component_context_index(void)
64{
65 int i;
66
67 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
68 if (!component_ctx_array[i].in_use) {
69 break;
70 }
71 }
72
73 if (i >= MAX_NUM_OF_COMPONENTS) {
74 /* No free index left in the array -- all used up! */
75 return -1;
76 }
77
78 return i;
79}
80
Maulik Patelad2f3db2023-05-17 15:41:36 +010081static dpe_error_t renew_nonce(int *handle)
82{
83 uint16_t nonce;
84
85 psa_status_t status = psa_generate_random((uint8_t *)&nonce, sizeof(nonce));
86 if (status != PSA_SUCCESS) {
87 return DPE_INTERNAL_ERROR;
88 }
89 *handle = SET_NONCE(*handle, nonce);
90
91 return DPE_NO_ERROR;
92}
93
Maulik Patelad2f3db2023-05-17 15:41:36 +010094static void set_context_to_default(int i)
95{
96 component_ctx_array[i].in_use = false;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000097 component_ctx_array[i].is_allowed_to_derive = true;
98 /* export CDI attribute is inherited and once disabled, a derived context
99 * and subsequent derivations cannot export CDI, hence enable by default
100 */
101 component_ctx_array[i].is_export_cdi_allowed = true;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100102 component_ctx_array[i].nonce = INVALID_NONCE_VALUE;
103 component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX;
104 component_ctx_array[i].linked_layer_idx = INVALID_LAYER_IDX;
105 (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t));
Maulik Patelacc3f4a2024-03-25 18:34:05 +0000106 component_ctx_array[i].target_locality = DEFAULT_TARGET_LOCALITY;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100107 /* Allow component to be derived by default */
108}
109
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100110static void initialise_layer(int i)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100111{
Maulik Patel009450d2024-04-23 12:03:10 +0100112 int j;
113
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100114 layer_ctx_array[i].idx = i;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100115 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
116 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000117 layer_ctx_array[i].is_cdi_to_be_exported = false;
Maulik Patel87c47ce2024-04-22 13:30:56 +0100118 layer_ctx_array[i].is_rot_layer = false;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000119 layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID;
Maulik Patele6adc112023-08-18 14:21:51 +0100120 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
121 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patelad2f3db2023-05-17 15:41:36 +0100122 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100123 layer_ctx_array[i].data.cdi_key_id = PSA_KEY_ID_NULL;
124 layer_ctx_array[i].data.attest_key_id = PSA_KEY_ID_NULL;
Maulik Patel009450d2024-04-23 12:03:10 +0100125 layer_ctx_array[i].linked_components.count = 0;
126 for (j = 0; j < ARRAY_SIZE(layer_ctx_array[i].linked_components.idx); j++) {
127 layer_ctx_array[i].linked_components.idx[j] = INVALID_COMPONENT_IDX;
128 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100129}
130
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100131static void close_layer(int i)
132{
133 destroy_layer_keys(&layer_ctx_array[i]);
134 initialise_layer(i);
135}
136
Maulik Patelad2f3db2023-05-17 15:41:36 +0100137static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
138 const DiceInputValues *dice_inputs)
139{
140 size_t hash_len;
141 psa_status_t status;
142
143 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
144 DICE_HASH_SIZE);
145 memcpy(&dest_ctx->data.measurement_descriptor,
146 dice_inputs->code_descriptor,
147 dice_inputs->code_descriptor_size);
148
149 dest_ctx->data.measurement_descriptor_size =
150 dice_inputs->code_descriptor_size;
151
152 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
153 memcpy(&dest_ctx->data.signer_id_descriptor,
154 dice_inputs->authority_descriptor,
155 dice_inputs->authority_descriptor_size);
156
157 dest_ctx->data.signer_id_descriptor_size =
158 dice_inputs->authority_descriptor_size;
159
160 if (dice_inputs->config_type == kDiceConfigTypeInline) {
161 /* Copy config_value */
162 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
163 DICE_INLINE_CONFIG_SIZE);
164
165 } else {
166 /* Copy config descriptor */
167 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
168 dice_inputs->config_descriptor_size);
169 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
170
171 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100172 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100173 dice_inputs->config_descriptor,
174 dice_inputs->config_descriptor_size,
175 dest_ctx->data.config_value,
176 sizeof(dest_ctx->data.config_value),
177 &hash_len);
178
179 if (status != PSA_SUCCESS) {
180 return DPE_INTERNAL_ERROR;
181 }
182 }
183
184 dest_ctx->data.mode = dice_inputs->mode;
185 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
186
187 return DPE_NO_ERROR;
188}
189
190static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
191{
192 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
193 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
194 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
195 return false;
196 }
197
198 return true;
199}
200
201static bool is_input_handle_valid(int input_context_handle)
202{
203 uint16_t idx = GET_IDX(input_context_handle);
204 uint16_t nonce = GET_NONCE(input_context_handle);
205
206 /* Validate input handle id and nonce */
207 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
208 return false;
209 }
210
211 if (nonce == component_ctx_array[idx].nonce) {
212 return true;
213 }
214
215 return false;
216}
217
Maulik Patel58595d32023-06-22 10:08:53 +0100218/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
219 * same order
220 */
221static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
222 size_t max_size,
223 size_t *dest_size,
224 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100225{
Maulik Patel58595d32023-06-22 10:08:53 +0100226 size_t out_size = 0;
227
228 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
229 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
230 return PSA_ERROR_BUFFER_TOO_SMALL;
231 }
232
233 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
234 out_size += DICE_HASH_SIZE;
235
236 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
237 out_size += DICE_INLINE_CONFIG_SIZE;
238
239 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
240 out_size += DICE_HASH_SIZE;
241
242 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
243 out_size += sizeof(comp_ctx->data.mode);
244
245 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
246 out_size += DICE_HIDDEN_SIZE;
247
248 *dest_size = out_size;
249
250 return PSA_SUCCESS;
251}
252
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100253static psa_status_t compute_layer_cdi_attest_input(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100254{
255 psa_status_t status;
256 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
257 size_t ctx_data_size, hash_len;
Maulik Patel009450d2024-04-23 12:03:10 +0100258 int i, idx;
259 uint16_t num_of_linked_components;
260
261 num_of_linked_components = layer_ctx->linked_components.count;
262 if (num_of_linked_components == 0) {
263 /* No components to hash */
264 return PSA_SUCCESS;
265 }
Maulik Patel58595d32023-06-22 10:08:53 +0100266
267 psa_hash_operation_t hash_op = psa_hash_operation_init();
268 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
269 if (status != PSA_SUCCESS) {
270 return status;
271 }
272
273 //TODO:
274 /* How to combine measurements of multiple SW components into a single hash
275 * is not yet defined by the Open DICE profile. This implementation
276 * concatenates the data of all SW components which belong to the same layer
277 * and hash it.
278 */
Maulik Patel009450d2024-04-23 12:03:10 +0100279 for (i = 0; i < num_of_linked_components; i++) {
280 idx = layer_ctx->linked_components.idx[i];
281 status = get_component_data_for_attest_cdi(component_ctx_data,
282 sizeof(component_ctx_data),
283 &ctx_data_size,
284 &component_ctx_array[idx]);
285 if (status != PSA_SUCCESS) {
286 return status;
287 }
Maulik Patel58595d32023-06-22 10:08:53 +0100288
Maulik Patel009450d2024-04-23 12:03:10 +0100289 status = psa_hash_update(&hash_op,
290 component_ctx_data,
291 ctx_data_size);
292 if (status != PSA_SUCCESS) {
293 return status;
Maulik Patel58595d32023-06-22 10:08:53 +0100294 }
295 }
296
297 status = psa_hash_finish(&hash_op,
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100298 &layer_ctx->attest_cdi_hash_input[0],
299 sizeof(layer_ctx->attest_cdi_hash_input),
Maulik Patel58595d32023-06-22 10:08:53 +0100300 &hash_len);
301
302 assert(hash_len == DPE_HASH_ALG_SIZE);
303
304 return status;
305}
306
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000307static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
308 uint8_t *exported_cdi_buf,
309 size_t exported_cdi_buf_size,
310 size_t *exported_cdi_actual_size)
311{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100312 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
313 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000314 psa_status_t status;
315 dpe_error_t err;
316
Tamas Ban5179a4d2024-01-25 17:05:30 +0100317 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000318 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100319 cdi_attest_buf,
320 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000321 if (status != PSA_SUCCESS) {
322 return DPE_INTERNAL_ERROR;
323 }
324
325 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100326 err = encode_cdi(cdi_attest_buf,
327 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000328 exported_cdi_buf,
329 exported_cdi_buf_size,
330 exported_cdi_actual_size);
331 if (err != DPE_NO_ERROR) {
332 return err;
333 }
334 layer_ctx->is_cdi_to_be_exported = true;
335
336 return DPE_NO_ERROR;
337}
338
Maulik Patel009450d2024-04-23 12:03:10 +0100339static dpe_error_t prepare_layer_certificate(struct layer_context_t *layer_ctx,
340 const struct layer_context_t *parent_layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100341{
Maulik Patel58595d32023-06-22 10:08:53 +0100342 psa_status_t status;
Maulik Patel58595d32023-06-22 10:08:53 +0100343
Maulik Patel2358bbb2023-07-21 10:56:56 +0100344 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patel87c47ce2024-04-22 13:30:56 +0100345 if ((!layer_ctx->is_rot_layer) &&
Maulik Patele6adc112023-08-18 14:21:51 +0100346 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100347
Maulik Patele6adc112023-08-18 14:21:51 +0100348 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100349
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100350 status = compute_layer_cdi_attest_input(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100351 if (status != PSA_SUCCESS) {
352 return DPE_INTERNAL_ERROR;
353 }
354
Maulik Patele6adc112023-08-18 14:21:51 +0100355 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100356 if (status != PSA_SUCCESS) {
357 return DPE_INTERNAL_ERROR;
358 }
359
Maulik Patele6adc112023-08-18 14:21:51 +0100360 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100361 if (status != PSA_SUCCESS) {
362 return DPE_INTERNAL_ERROR;
363 }
364 }
365
Maulik Patele6adc112023-08-18 14:21:51 +0100366 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100367 if (status != PSA_SUCCESS) {
368 return DPE_INTERNAL_ERROR;
369 }
370
Maulik Patele6adc112023-08-18 14:21:51 +0100371 if (!layer_ctx->is_external_pub_key_provided) {
372 status = derive_attestation_key(layer_ctx);
373 if (status != PSA_SUCCESS) {
374 return DPE_INTERNAL_ERROR;
375 }
Maulik Patel58595d32023-06-22 10:08:53 +0100376 }
377
Maulik Patele6adc112023-08-18 14:21:51 +0100378 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100379 if (status != PSA_SUCCESS) {
380 return DPE_INTERNAL_ERROR;
381 }
382
Tamas Ban257471b2024-03-25 13:49:53 +0100383 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100384}
385
386static uint16_t open_new_layer(void)
387{
388 int i;
389
390 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
391 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
392 layer_ctx_array[i].state = LAYER_STATE_OPEN;
393 return i;
394 }
395 }
396
Maulik Patel2358bbb2023-07-21 10:56:56 +0100397 //TODO: There is an open issue of layer creation as described below.
398 /* This is causing extra unintended layers to open. Since each layer
399 * has some context data and certificate buffer of 3k, it is
400 * causing RAM overflow. Hence until resoluton is reached, once all
401 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100402 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
403 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
404 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
405 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100406 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
407 */
408 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100409}
410
Maulik Patel9b13e162024-04-24 14:18:17 +0100411static bool is_client_authorised(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100412{
Maulik Patel9b13e162024-04-24 14:18:17 +0100413 int32_t client_locality;
414
415 if (target_locality == LOCALITY_NONE) {
416 /* Context is not bound to any locality */
417 return true;
418 }
419 /* Get the corresponding client locality */
420 client_locality = dpe_plat_get_client_locality(client_id);
421
422 return (client_locality == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100423}
424
Maulik Patelcb14cde2024-01-23 12:39:53 +0000425static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100426{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000427 int i;
428
429 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
430 if (layer_ctx_array[i].cert_id == cert_id) {
431 *layer_idx = i;
432 return true;
433 }
434 }
435
436 /* No certificate ID match found */
437 return false;
438}
439
440static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
441 uint32_t cert_id)
442{
443 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100444
445 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
446
447 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
448 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
449
Maulik Patelcb14cde2024-01-23 12:39:53 +0000450 if (cert_id != DPE_CERT_ID_INVALID) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200451 /* Cert_id was sent by the client */
Maulik Patelcb14cde2024-01-23 12:39:53 +0000452 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
453 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
454 /* Cannot add to the layer which is already finalised */
455 return DPE_INTERNAL_ERROR;
456 }
457 /* Derived context belongs to the same certificate as its parent component */
458 new_ctx->linked_layer_idx = parent_layer_idx;
459
460 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200461 /* Cert_id is already in use but layer must be in open state, because
462 * cert_id is invalidated when layer gets finalized.
463 */
464 assert(layer_ctx_array[layer_idx_to_link].state != LAYER_STATE_FINALISED);
465
Maulik Patelcb14cde2024-01-23 12:39:53 +0000466 /* Use the same layer that is associated with cert_id */
467 new_ctx->linked_layer_idx = layer_idx_to_link;
468 /* Linked layer's parent is already assigned when it was opened */
469
470 } else {
471 /* Open new layer and link derived context to new layer */
472 layer_idx_to_link = open_new_layer();
473 if (layer_idx_to_link == INVALID_LAYER_IDX) {
474 return DPE_INTERNAL_ERROR;
475 }
476 /* Link this context to the new layer */
477 new_ctx->linked_layer_idx = layer_idx_to_link;
478 /* New layer's parent is parent component's layer */
479 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
480 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100481 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100482
483 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000484 /* cert id was not sent by the client */
485 //TODO: To be implemented; return error for now.
486 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100487 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100488
489 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100490}
491
Jamie Fox34681992023-09-04 18:14:06 +0100492/**
493 * \brief Create a root of trust component context.
494 *
495 * \param[out] rot_ctx_handle A new context handle for the RoT context.
496 *
497 * \return Returns error code of type dpe_error_t
498 */
499static dpe_error_t create_rot_context(int *rot_ctx_handle)
500{
Jamie Fox34681992023-09-04 18:14:06 +0100501 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
502 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
503
Maulik Patel87c47ce2024-04-22 13:30:56 +0100504 rot_layer_ctx->is_rot_layer = true;
Maulik Patela81605b2023-10-24 12:17:03 +0100505 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100506 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100507 /* Get the RoT CDI key for the RoT layer */
508 rot_layer_ctx->data.cdi_key_id = dpe_plat_get_rot_cdi_key_id();
Maulik Patela81605b2023-10-24 12:17:03 +0100509 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100510 rot_comp_ctx->nonce = 0;
Maulik Patelacc3f4a2024-03-25 18:34:05 +0000511 /* Set the target locality for RoT context */
512 rot_comp_ctx->target_locality = LOCALITY_RSE_S;
Maulik Patela81605b2023-10-24 12:17:03 +0100513 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100514 rot_comp_ctx->parent_idx = 0;
515 /* Link context to RoT Layer */
516 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
517 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100518 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
519
520 return DPE_NO_ERROR;
521}
522
523dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
524{
525 int i;
526
527 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
528 set_context_to_default(i);
529 }
530
531 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100532 initialise_layer(i);
Jamie Fox34681992023-09-04 18:14:06 +0100533 }
534
535 return create_rot_context(rot_ctx_handle);
536}
537
Maulik Patel9a893122024-04-15 13:48:38 +0100538static void close_layer_if_empty(uint16_t layer_idx)
539{
540 if (layer_ctx_array[layer_idx].linked_components.count == 0) {
541 close_layer(layer_idx);
542 }
543}
544
Maulik Patela81605b2023-10-24 12:17:03 +0100545dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000546 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100547 bool retain_parent_context,
548 bool allow_new_context_to_derive,
549 bool create_certificate,
550 const DiceInputValues *dice_inputs,
551 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000552 int32_t target_locality,
553 bool return_certificate,
554 bool allow_new_context_to_export,
555 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100556 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000557 int *new_parent_context_handle,
558 uint8_t *new_certificate_buf,
559 size_t new_certificate_buf_size,
560 size_t *new_certificate_actual_size,
561 uint8_t *exported_cdi_buf,
562 size_t exported_cdi_buf_size,
563 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100564{
Maulik Patel58595d32023-06-22 10:08:53 +0100565 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100566 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel009450d2024-04-23 12:03:10 +0100567 uint16_t parent_ctx_idx, linked_layer_idx, parent_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100568 int free_component_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100569 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100570
Maulik Patelcb14cde2024-01-23 12:39:53 +0000571 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100572 allow_new_context_to_derive, create_certificate, dice_inputs,
573 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100574
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000575 if (export_cdi && !create_certificate) {
576 return DPE_INVALID_ARGUMENT;
577 }
578
Maulik Patelad2f3db2023-05-17 15:41:36 +0100579 /* Validate dice inputs */
580 if (!is_dice_input_valid(dice_inputs)) {
581 return DPE_INVALID_ARGUMENT;
582 }
583
584 /* Validate input handle */
585 if (!is_input_handle_valid(input_ctx_handle)) {
586 return DPE_INVALID_ARGUMENT;
587 }
Maulik Patela81605b2023-10-24 12:17:03 +0100588 /* Get parent component index from the input handle */
589 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100590
591 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100592 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100593 */
Maulik Patela81605b2023-10-24 12:17:03 +0100594 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100595
Maulik Patela81605b2023-10-24 12:17:03 +0100596 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100597
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000598 /* Check if parent context is allowed to derive */
599 if (!parent_ctx->is_allowed_to_derive) {
600 return DPE_INVALID_ARGUMENT;
601 }
602
Maulik Patel9b13e162024-04-24 14:18:17 +0100603 if (!is_client_authorised(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100604 return DPE_INVALID_ARGUMENT;
605 }
606
Maulik Patela81605b2023-10-24 12:17:03 +0100607 /* Get next free component index to add new derived context */
608 free_component_idx = get_free_component_context_index();
609 if (free_component_idx < 0) {
610 return DPE_INSUFFICIENT_MEMORY;
611 }
612
613 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000614 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
615 /* If parent context has export enabled and input allow_new_context_to_export
616 * is true, then allow context CDI to be exported for derived context
617 */
618 derived_ctx->is_export_cdi_allowed = true;
619 } else {
620 /* Export of new context CDI is NOT allowed */
621 derived_ctx->is_export_cdi_allowed = false;
622 if (export_cdi) {
623 return DPE_INVALID_ARGUMENT;
624 }
625 }
626
Maulik Patela81605b2023-10-24 12:17:03 +0100627 /* Copy dice input to the new derived component context */
628 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100629 if (err != DPE_NO_ERROR) {
630 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100631 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000632 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100633
Maulik Patela81605b2023-10-24 12:17:03 +0100634 /* Update parent idx in new derived component context */
635 derived_ctx->parent_idx = parent_ctx_idx;
636 /* Mark new derived component index as in use */
637 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000638 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000639 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100640 if (err != DPE_NO_ERROR) {
641 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100642 }
643
Maulik Patel9a893122024-04-15 13:48:38 +0100644 linked_layer_idx = derived_ctx->linked_layer_idx;
645 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
646 layer_ctx = &layer_ctx_array[linked_layer_idx];
647 err = store_linked_component(layer_ctx, free_component_idx);
648 if (err != DPE_NO_ERROR) {
649 goto clean_up_and_exit;
650 }
651 parent_layer_idx = layer_ctx->parent_layer_idx;
652 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
653 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
654
655 if (create_certificate) {
656 layer_ctx->is_cdi_to_be_exported = export_cdi;
657
658 /* Finalise the layer */
659 layer_ctx->state = LAYER_STATE_FINALISED;
Tamas Ban33f1aec2024-06-04 12:15:18 +0200660 layer_ctx->cert_id = DPE_CERT_ID_INVALID; /* make same cert_id reusable */
Maulik Patel9a893122024-04-15 13:48:38 +0100661 err = prepare_layer_certificate(layer_ctx, parent_layer_ctx);
662 if (err != DPE_NO_ERROR) {
663 goto clean_up_and_exit;
664 }
665
666 if (return_certificate) {
667 /* Encode and return generated layer certificate */
668 err = encode_layer_certificate(layer_ctx,
669 new_certificate_buf,
670 new_certificate_buf_size,
671 new_certificate_actual_size);
672 if (err != DPE_NO_ERROR) {
673 goto clean_up_and_exit;
674 }
675 }
676 }
677
678 if (export_cdi) {
679 err = get_encoded_cdi_to_export(layer_ctx,
680 exported_cdi_buf,
681 exported_cdi_buf_size,
682 exported_cdi_actual_size);
683 if (err != DPE_NO_ERROR) {
684 goto clean_up_and_exit;
685 }
686 }
687
Maulik Patelad2f3db2023-05-17 15:41:36 +0100688 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100689 /* Retain and return parent handle with renewed nonce */
690 *new_parent_context_handle = input_ctx_handle;
691 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100692 if (err != DPE_NO_ERROR) {
Maulik Patel9a893122024-04-15 13:48:38 +0100693 goto clean_up_and_exit;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100694 }
Maulik Patela81605b2023-10-24 12:17:03 +0100695 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
696
Maulik Patelad2f3db2023-05-17 15:41:36 +0100697 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100698 /* Return invalid handle */
699 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100700 parent_ctx->nonce = INVALID_NONCE_VALUE;
701 }
702
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000703 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100704 /* Return handle to derived context */
705 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
706 err = renew_nonce(new_context_handle);
707 if (err != DPE_NO_ERROR) {
708 return err;
709 }
710 /* Update nonce in new derived component context */
711 derived_ctx->nonce = GET_NONCE(*new_context_handle);
712
713 } else {
714 /* Return invalid handle */
715 *new_context_handle = INVALID_HANDLE;
716 derived_ctx->nonce = INVALID_NONCE_VALUE;
717 }
718
Maulik Patel9a2a5672024-03-14 13:43:58 +0000719 log_derive_context_output_handles(*new_parent_context_handle,
720 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100721
Maulik Patel5ac87802024-03-14 14:22:19 +0000722 /* Log context and layer info and certificate if no error */
723 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
724 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100725 if (return_certificate) {
Maulik Patel5ac87802024-03-14 14:22:19 +0000726 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100727 new_certificate_buf,
728 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000729 }
730
Maulik Patelad2f3db2023-05-17 15:41:36 +0100731 return DPE_NO_ERROR;
Maulik Patel9a893122024-04-15 13:48:38 +0100732
733clean_up_and_exit:
734 set_context_to_default(free_component_idx);
735 close_layer_if_empty(linked_layer_idx);
736
737 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100738}
Maulik Patel54d65f72023-06-28 13:04:36 +0100739
740dpe_error_t destroy_context_request(int input_ctx_handle,
741 bool destroy_recursively)
742{
743 uint16_t input_ctx_idx, linked_layer_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100744 struct layer_context_t *layer_ctx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100745
746 log_destroy_context(input_ctx_handle, destroy_recursively);
747
Maulik Patela81605b2023-10-24 12:17:03 +0100748 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100749 input_ctx_idx = GET_IDX(input_ctx_handle);
750
Maulik Patel54d65f72023-06-28 13:04:36 +0100751 /* Validate input handle */
752 if (!is_input_handle_valid(input_ctx_handle)) {
753 return DPE_INVALID_ARGUMENT;
754 }
755 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
756
Jamie Fox34681992023-09-04 18:14:06 +0100757#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100758 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
759 /* All layers till hypervisor cannot be destroyed dynamically */
760 return DPE_INVALID_ARGUMENT;
761 }
Jamie Fox34681992023-09-04 18:14:06 +0100762#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100763
Maulik Patel009450d2024-04-23 12:03:10 +0100764 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patel54d65f72023-06-28 13:04:36 +0100765
766 if (!destroy_recursively) {
767 set_context_to_default(input_ctx_idx);
Maulik Patel009450d2024-04-23 12:03:10 +0100768 layer_ctx = &layer_ctx_array[linked_layer_idx];
769 remove_linked_component(layer_ctx, input_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100770 } else {
771 //TODO: To be implemented
772 }
773
Maulik Patel54d65f72023-06-28 13:04:36 +0100774 /* Close the layer if all of its contexts are destroyed */
Maulik Patel9a893122024-04-15 13:48:38 +0100775 close_layer_if_empty(linked_layer_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100776
777 return DPE_NO_ERROR;
778}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100779
Maulik Patel009450d2024-04-23 12:03:10 +0100780struct component_context_t* get_component_ctx_ptr(uint16_t component_idx)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100781{
782 /* Safety case */
783 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
784 return NULL;
785 }
786
Maulik Patel009450d2024-04-23 12:03:10 +0100787 return &component_ctx_array[component_idx];
Maulik Patel2358bbb2023-07-21 10:56:56 +0100788}
789
Maulik Patele6adc112023-08-18 14:21:51 +0100790struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
791{
792 /* Safety case */
793 if (layer_idx >= MAX_NUM_OF_LAYERS) {
794 return NULL;
795 }
796
797 return &layer_ctx_array[layer_idx];
798}
799
800dpe_error_t certify_key_request(int input_ctx_handle,
801 bool retain_context,
802 const uint8_t *public_key,
803 size_t public_key_size,
804 const uint8_t *label,
805 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000806 uint8_t *certificate_buf,
807 size_t certificate_buf_size,
808 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100809 uint8_t *derived_public_key_buf,
810 size_t derived_public_key_buf_size,
811 size_t *derived_public_key_actual_size,
812 int *new_context_handle)
813{
814 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
815 dpe_error_t err;
816 psa_status_t status;
Tamas Baneb8d7f12024-04-03 13:55:22 +0200817 struct layer_context_t *parent_layer_ctx, *input_layer_ctx;
818 struct layer_context_t leaf_layer = {0};
Maulik Patele6adc112023-08-18 14:21:51 +0100819
820 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
821 label, label_size);
822
823 /* Validate input handle */
824 if (!is_input_handle_valid(input_ctx_handle)) {
825 return DPE_INVALID_ARGUMENT;
826 }
827
828 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
829 return DPE_INVALID_ARGUMENT;
830 }
831
832 /* Get component index from the input handle */
833 input_ctx_idx = GET_IDX(input_ctx_handle);
834 /* Get current linked layer idx */
835 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
836 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
Tamas Baneb8d7f12024-04-03 13:55:22 +0200837 input_layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100838
Tamas Baneb8d7f12024-04-03 13:55:22 +0200839 if (input_layer_ctx->state == LAYER_STATE_FINALISED) {
840 /* Input layer is finalised, new leaf layer is its child now */
841 leaf_layer.parent_layer_idx = input_layer_idx;
842 /* Linked components count already initialised to 0 */
843
844 } else {
845 /* Input layer is not finalised, new leaf layer share the same
846 * components as in the input layer
847 */
848 memcpy(&leaf_layer.linked_components, &input_layer_ctx->linked_components,
849 sizeof(input_layer_ctx->linked_components));
850 }
Maulik Patel7cc80872024-04-04 12:00:29 +0100851
852 if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100853 return DPE_INVALID_ARGUMENT;
854 }
855
856 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel7cc80872024-04-04 12:00:29 +0100857 leaf_layer.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100858 /* Copy the public key provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100859 memcpy(&leaf_layer.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100860 public_key,
861 public_key_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100862 leaf_layer.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100863
864 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel7cc80872024-04-04 12:00:29 +0100865 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100866
867 } else {
868 /* No external public key is provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100869 leaf_layer.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100870
871 if ((label_size > 0) && (label != NULL)) {
872 /* Copy the label provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100873 memcpy(&leaf_layer.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100874 label,
875 label_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100876 leaf_layer.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100877
878 } else {
Maulik Patel7cc80872024-04-04 12:00:29 +0100879 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100880 }
881 }
882
Maulik Patel009450d2024-04-23 12:03:10 +0100883 /* Get parent layer derived public key to verify the certificate signature */
884 parent_layer_idx = leaf_layer.parent_layer_idx;
885 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
886 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
887
Maulik Patele6adc112023-08-18 14:21:51 +0100888 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100889 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100890 */
Maulik Patelcbded682023-12-07 11:50:16 +0000891 /* Create leaf certificate */
Maulik Patel009450d2024-04-23 12:03:10 +0100892 err = prepare_layer_certificate(&leaf_layer, parent_layer_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100893 if (err != DPE_NO_ERROR) {
894 return err;
895 }
896
Maulik Patel7cc80872024-04-04 12:00:29 +0100897 err = encode_layer_certificate(&leaf_layer,
Tamas Ban257471b2024-03-25 13:49:53 +0100898 certificate_buf,
899 certificate_buf_size,
900 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100901 if (err != DPE_NO_ERROR) {
902 return err;
903 }
904
Maulik Patele6adc112023-08-18 14:21:51 +0100905 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
906 return DPE_INVALID_ARGUMENT;
907 }
908
909 memcpy(derived_public_key_buf,
910 &parent_layer_ctx->data.attest_pub_key[0],
911 parent_layer_ctx->data.attest_pub_key_len);
912 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
913
Maulik Patel91edd632024-02-26 07:44:41 +0000914 /* Renew handle for the same context, if requested */
915 if (retain_context) {
916 *new_context_handle = input_ctx_handle;
917 status = renew_nonce(new_context_handle);
918 if (status != PSA_SUCCESS) {
919 return DPE_INTERNAL_ERROR;
920 }
921 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
922
923 } else {
924 *new_context_handle = INVALID_HANDLE;
925 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100926 }
Maulik Patele6adc112023-08-18 14:21:51 +0100927
Maulik Patel9a2a5672024-03-14 13:43:58 +0000928 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000929 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100930 certificate_buf,
931 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100932
Tamas Baneb8d7f12024-04-03 13:55:22 +0200933 destroy_layer_keys(&leaf_layer);
934
Maulik Patele6adc112023-08-18 14:21:51 +0100935 return DPE_NO_ERROR;
936}
Maulik Patel83a6b592023-12-05 15:20:30 +0000937
938dpe_error_t get_certificate_chain_request(int input_ctx_handle,
939 bool retain_context,
940 bool clear_from_context,
941 uint8_t *certificate_chain_buf,
942 size_t certificate_chain_buf_size,
943 size_t *certificate_chain_actual_size,
944 int *new_context_handle)
945{
946 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000947 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000948 psa_status_t status;
949 struct layer_context_t *layer_ctx;
950
Tamas Bana5e2f582024-01-25 16:59:26 +0100951 log_get_certificate_chain(input_ctx_handle, retain_context,
952 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000953
954 /* Validate input handle */
955 if (!is_input_handle_valid(input_ctx_handle)) {
956 return DPE_INVALID_ARGUMENT;
957 }
958
959 /* Get component index from the input handle */
960 input_ctx_idx = GET_IDX(input_ctx_handle);
961 /* Get current linked layer idx */
962 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
963 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
964
965 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100966 if (layer_ctx->state != LAYER_STATE_FINALISED) {
967 /* If the context has accumulated info and not yet part of a certificate,
968 * return an invalid-argument error
969 */
970 return DPE_INVALID_ARGUMENT;
971 }
972
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100973 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000974 certificate_chain_buf,
975 certificate_chain_buf_size,
976 certificate_chain_actual_size);
977 if (err != DPE_NO_ERROR) {
978 return err;
979 }
980
981 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
982
983 /* Renew handle for the same context, if requested */
984 if (retain_context) {
985 *new_context_handle = input_ctx_handle;
986 status = renew_nonce(new_context_handle);
987 if (status != PSA_SUCCESS) {
988 return DPE_INTERNAL_ERROR;
989 }
990 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
991
992 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +0100993 //TODO: Reimplement the clear_from_context functionality after memory
994 // optimization; Certificates are not ready made and they are not
995 // stored in the layer context anymore. They are created on-the-fly
996 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000997 }
998
999 } else {
1000 *new_context_handle = INVALID_HANDLE;
1001 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
1002 }
Maulik Patel9a2a5672024-03-14 13:43:58 +00001003 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +00001004
1005 return DPE_NO_ERROR;
1006}