blob: fbbb2034c0f8cc0618dd3b846ab9386769704383 [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 Patel9fd8bd22023-10-30 10:58:30 +0000411static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100412{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000413 // TODO: FIXME
414 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100415 return true;
416}
417
Maulik Patelcb14cde2024-01-23 12:39:53 +0000418static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100419{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000420 int i;
421
422 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
423 if (layer_ctx_array[i].cert_id == cert_id) {
424 *layer_idx = i;
425 return true;
426 }
427 }
428
429 /* No certificate ID match found */
430 return false;
431}
432
433static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
434 uint32_t cert_id)
435{
436 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100437
438 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
439
440 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
441 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
442
Maulik Patelcb14cde2024-01-23 12:39:53 +0000443 if (cert_id != DPE_CERT_ID_INVALID) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200444 /* Cert_id was sent by the client */
Maulik Patelcb14cde2024-01-23 12:39:53 +0000445 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
446 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
447 /* Cannot add to the layer which is already finalised */
448 return DPE_INTERNAL_ERROR;
449 }
450 /* Derived context belongs to the same certificate as its parent component */
451 new_ctx->linked_layer_idx = parent_layer_idx;
452
453 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200454 /* Cert_id is already in use but layer must be in open state, because
455 * cert_id is invalidated when layer gets finalized.
456 */
457 assert(layer_ctx_array[layer_idx_to_link].state != LAYER_STATE_FINALISED);
458
Maulik Patelcb14cde2024-01-23 12:39:53 +0000459 /* Use the same layer that is associated with cert_id */
460 new_ctx->linked_layer_idx = layer_idx_to_link;
461 /* Linked layer's parent is already assigned when it was opened */
462
463 } else {
464 /* Open new layer and link derived context to new layer */
465 layer_idx_to_link = open_new_layer();
466 if (layer_idx_to_link == INVALID_LAYER_IDX) {
467 return DPE_INTERNAL_ERROR;
468 }
469 /* Link this context to the new layer */
470 new_ctx->linked_layer_idx = layer_idx_to_link;
471 /* New layer's parent is parent component's layer */
472 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
473 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100474 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100475
476 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000477 /* cert id was not sent by the client */
478 //TODO: To be implemented; return error for now.
479 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100480 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100481
482 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100483}
484
Jamie Fox34681992023-09-04 18:14:06 +0100485/**
486 * \brief Create a root of trust component context.
487 *
488 * \param[out] rot_ctx_handle A new context handle for the RoT context.
489 *
490 * \return Returns error code of type dpe_error_t
491 */
492static dpe_error_t create_rot_context(int *rot_ctx_handle)
493{
Jamie Fox34681992023-09-04 18:14:06 +0100494 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
495 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
496
Maulik Patel87c47ce2024-04-22 13:30:56 +0100497 rot_layer_ctx->is_rot_layer = true;
Maulik Patela81605b2023-10-24 12:17:03 +0100498 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100499 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100500 /* Get the RoT CDI key for the RoT layer */
501 rot_layer_ctx->data.cdi_key_id = dpe_plat_get_rot_cdi_key_id();
Maulik Patela81605b2023-10-24 12:17:03 +0100502 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100503 rot_comp_ctx->nonce = 0;
Maulik Patelacc3f4a2024-03-25 18:34:05 +0000504 /* Set the target locality for RoT context */
505 rot_comp_ctx->target_locality = LOCALITY_RSE_S;
Maulik Patela81605b2023-10-24 12:17:03 +0100506 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100507 rot_comp_ctx->parent_idx = 0;
508 /* Link context to RoT Layer */
509 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
510 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100511 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
512
513 return DPE_NO_ERROR;
514}
515
516dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
517{
518 int i;
519
520 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
521 set_context_to_default(i);
522 }
523
524 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100525 initialise_layer(i);
Jamie Fox34681992023-09-04 18:14:06 +0100526 }
527
528 return create_rot_context(rot_ctx_handle);
529}
530
Maulik Patel9a893122024-04-15 13:48:38 +0100531static void close_layer_if_empty(uint16_t layer_idx)
532{
533 if (layer_ctx_array[layer_idx].linked_components.count == 0) {
534 close_layer(layer_idx);
535 }
536}
537
Maulik Patela81605b2023-10-24 12:17:03 +0100538dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000539 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100540 bool retain_parent_context,
541 bool allow_new_context_to_derive,
542 bool create_certificate,
543 const DiceInputValues *dice_inputs,
544 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000545 int32_t target_locality,
546 bool return_certificate,
547 bool allow_new_context_to_export,
548 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100549 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000550 int *new_parent_context_handle,
551 uint8_t *new_certificate_buf,
552 size_t new_certificate_buf_size,
553 size_t *new_certificate_actual_size,
554 uint8_t *exported_cdi_buf,
555 size_t exported_cdi_buf_size,
556 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100557{
Maulik Patel58595d32023-06-22 10:08:53 +0100558 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100559 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel009450d2024-04-23 12:03:10 +0100560 uint16_t parent_ctx_idx, linked_layer_idx, parent_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100561 int free_component_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100562 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100563
Maulik Patelcb14cde2024-01-23 12:39:53 +0000564 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100565 allow_new_context_to_derive, create_certificate, dice_inputs,
566 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100567
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000568 if (export_cdi && !create_certificate) {
569 return DPE_INVALID_ARGUMENT;
570 }
571
Maulik Patelad2f3db2023-05-17 15:41:36 +0100572 /* Validate dice inputs */
573 if (!is_dice_input_valid(dice_inputs)) {
574 return DPE_INVALID_ARGUMENT;
575 }
576
577 /* Validate input handle */
578 if (!is_input_handle_valid(input_ctx_handle)) {
579 return DPE_INVALID_ARGUMENT;
580 }
Maulik Patela81605b2023-10-24 12:17:03 +0100581 /* Get parent component index from the input handle */
582 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100583
584 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100585 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100586 */
Maulik Patela81605b2023-10-24 12:17:03 +0100587 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100588
Maulik Patela81605b2023-10-24 12:17:03 +0100589 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100590
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000591 /* Check if parent context is allowed to derive */
592 if (!parent_ctx->is_allowed_to_derive) {
593 return DPE_INVALID_ARGUMENT;
594 }
595
Maulik Patelad2f3db2023-05-17 15:41:36 +0100596 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000597 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100598 return DPE_INVALID_ARGUMENT;
599 }
600
Maulik Patela81605b2023-10-24 12:17:03 +0100601 /* Get next free component index to add new derived context */
602 free_component_idx = get_free_component_context_index();
603 if (free_component_idx < 0) {
604 return DPE_INSUFFICIENT_MEMORY;
605 }
606
607 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000608 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
609 /* If parent context has export enabled and input allow_new_context_to_export
610 * is true, then allow context CDI to be exported for derived context
611 */
612 derived_ctx->is_export_cdi_allowed = true;
613 } else {
614 /* Export of new context CDI is NOT allowed */
615 derived_ctx->is_export_cdi_allowed = false;
616 if (export_cdi) {
617 return DPE_INVALID_ARGUMENT;
618 }
619 }
620
Maulik Patela81605b2023-10-24 12:17:03 +0100621 /* Copy dice input to the new derived component context */
622 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100623 if (err != DPE_NO_ERROR) {
624 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100625 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000626 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100627
Maulik Patela81605b2023-10-24 12:17:03 +0100628 /* Update parent idx in new derived component context */
629 derived_ctx->parent_idx = parent_ctx_idx;
630 /* Mark new derived component index as in use */
631 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000632 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000633 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100634 if (err != DPE_NO_ERROR) {
635 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100636 }
637
Maulik Patel9a893122024-04-15 13:48:38 +0100638 linked_layer_idx = derived_ctx->linked_layer_idx;
639 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
640 layer_ctx = &layer_ctx_array[linked_layer_idx];
641 err = store_linked_component(layer_ctx, free_component_idx);
642 if (err != DPE_NO_ERROR) {
643 goto clean_up_and_exit;
644 }
645 parent_layer_idx = layer_ctx->parent_layer_idx;
646 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
647 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
648
649 if (create_certificate) {
650 layer_ctx->is_cdi_to_be_exported = export_cdi;
651
652 /* Finalise the layer */
653 layer_ctx->state = LAYER_STATE_FINALISED;
Tamas Ban33f1aec2024-06-04 12:15:18 +0200654 layer_ctx->cert_id = DPE_CERT_ID_INVALID; /* make same cert_id reusable */
Maulik Patel9a893122024-04-15 13:48:38 +0100655 err = prepare_layer_certificate(layer_ctx, parent_layer_ctx);
656 if (err != DPE_NO_ERROR) {
657 goto clean_up_and_exit;
658 }
659
660 if (return_certificate) {
661 /* Encode and return generated layer certificate */
662 err = encode_layer_certificate(layer_ctx,
663 new_certificate_buf,
664 new_certificate_buf_size,
665 new_certificate_actual_size);
666 if (err != DPE_NO_ERROR) {
667 goto clean_up_and_exit;
668 }
669 }
670 }
671
672 if (export_cdi) {
673 err = get_encoded_cdi_to_export(layer_ctx,
674 exported_cdi_buf,
675 exported_cdi_buf_size,
676 exported_cdi_actual_size);
677 if (err != DPE_NO_ERROR) {
678 goto clean_up_and_exit;
679 }
680 }
681
Maulik Patelad2f3db2023-05-17 15:41:36 +0100682 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100683 /* Retain and return parent handle with renewed nonce */
684 *new_parent_context_handle = input_ctx_handle;
685 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100686 if (err != DPE_NO_ERROR) {
Maulik Patel9a893122024-04-15 13:48:38 +0100687 goto clean_up_and_exit;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100688 }
Maulik Patela81605b2023-10-24 12:17:03 +0100689 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
690
Maulik Patelad2f3db2023-05-17 15:41:36 +0100691 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100692 /* Return invalid handle */
693 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100694 parent_ctx->nonce = INVALID_NONCE_VALUE;
695 }
696
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000697 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100698 /* Return handle to derived context */
699 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
700 err = renew_nonce(new_context_handle);
701 if (err != DPE_NO_ERROR) {
702 return err;
703 }
704 /* Update nonce in new derived component context */
705 derived_ctx->nonce = GET_NONCE(*new_context_handle);
706
707 } else {
708 /* Return invalid handle */
709 *new_context_handle = INVALID_HANDLE;
710 derived_ctx->nonce = INVALID_NONCE_VALUE;
711 }
712
Maulik Patel9a2a5672024-03-14 13:43:58 +0000713 log_derive_context_output_handles(*new_parent_context_handle,
714 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100715
Maulik Patel5ac87802024-03-14 14:22:19 +0000716 /* Log context and layer info and certificate if no error */
717 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
718 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100719 if (return_certificate) {
Maulik Patel5ac87802024-03-14 14:22:19 +0000720 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100721 new_certificate_buf,
722 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000723 }
724
Maulik Patelad2f3db2023-05-17 15:41:36 +0100725 return DPE_NO_ERROR;
Maulik Patel9a893122024-04-15 13:48:38 +0100726
727clean_up_and_exit:
728 set_context_to_default(free_component_idx);
729 close_layer_if_empty(linked_layer_idx);
730
731 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100732}
Maulik Patel54d65f72023-06-28 13:04:36 +0100733
734dpe_error_t destroy_context_request(int input_ctx_handle,
735 bool destroy_recursively)
736{
737 uint16_t input_ctx_idx, linked_layer_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100738 struct layer_context_t *layer_ctx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100739
740 log_destroy_context(input_ctx_handle, destroy_recursively);
741
Maulik Patela81605b2023-10-24 12:17:03 +0100742 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100743 input_ctx_idx = GET_IDX(input_ctx_handle);
744
Maulik Patel54d65f72023-06-28 13:04:36 +0100745 /* Validate input handle */
746 if (!is_input_handle_valid(input_ctx_handle)) {
747 return DPE_INVALID_ARGUMENT;
748 }
749 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
750
Jamie Fox34681992023-09-04 18:14:06 +0100751#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100752 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
753 /* All layers till hypervisor cannot be destroyed dynamically */
754 return DPE_INVALID_ARGUMENT;
755 }
Jamie Fox34681992023-09-04 18:14:06 +0100756#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100757
Maulik Patel009450d2024-04-23 12:03:10 +0100758 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patel54d65f72023-06-28 13:04:36 +0100759
760 if (!destroy_recursively) {
761 set_context_to_default(input_ctx_idx);
Maulik Patel009450d2024-04-23 12:03:10 +0100762 layer_ctx = &layer_ctx_array[linked_layer_idx];
763 remove_linked_component(layer_ctx, input_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100764 } else {
765 //TODO: To be implemented
766 }
767
Maulik Patel54d65f72023-06-28 13:04:36 +0100768 /* Close the layer if all of its contexts are destroyed */
Maulik Patel9a893122024-04-15 13:48:38 +0100769 close_layer_if_empty(linked_layer_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100770
771 return DPE_NO_ERROR;
772}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100773
Maulik Patel009450d2024-04-23 12:03:10 +0100774struct component_context_t* get_component_ctx_ptr(uint16_t component_idx)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100775{
776 /* Safety case */
777 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
778 return NULL;
779 }
780
Maulik Patel009450d2024-04-23 12:03:10 +0100781 return &component_ctx_array[component_idx];
Maulik Patel2358bbb2023-07-21 10:56:56 +0100782}
783
Maulik Patele6adc112023-08-18 14:21:51 +0100784struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
785{
786 /* Safety case */
787 if (layer_idx >= MAX_NUM_OF_LAYERS) {
788 return NULL;
789 }
790
791 return &layer_ctx_array[layer_idx];
792}
793
794dpe_error_t certify_key_request(int input_ctx_handle,
795 bool retain_context,
796 const uint8_t *public_key,
797 size_t public_key_size,
798 const uint8_t *label,
799 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000800 uint8_t *certificate_buf,
801 size_t certificate_buf_size,
802 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100803 uint8_t *derived_public_key_buf,
804 size_t derived_public_key_buf_size,
805 size_t *derived_public_key_actual_size,
806 int *new_context_handle)
807{
808 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
809 dpe_error_t err;
810 psa_status_t status;
Tamas Baneb8d7f12024-04-03 13:55:22 +0200811 struct layer_context_t *parent_layer_ctx, *input_layer_ctx;
812 struct layer_context_t leaf_layer = {0};
Maulik Patele6adc112023-08-18 14:21:51 +0100813
814 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
815 label, label_size);
816
817 /* Validate input handle */
818 if (!is_input_handle_valid(input_ctx_handle)) {
819 return DPE_INVALID_ARGUMENT;
820 }
821
822 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
823 return DPE_INVALID_ARGUMENT;
824 }
825
826 /* Get component index from the input handle */
827 input_ctx_idx = GET_IDX(input_ctx_handle);
828 /* Get current linked layer idx */
829 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
830 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
Tamas Baneb8d7f12024-04-03 13:55:22 +0200831 input_layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100832
Tamas Baneb8d7f12024-04-03 13:55:22 +0200833 if (input_layer_ctx->state == LAYER_STATE_FINALISED) {
834 /* Input layer is finalised, new leaf layer is its child now */
835 leaf_layer.parent_layer_idx = input_layer_idx;
836 /* Linked components count already initialised to 0 */
837
838 } else {
839 /* Input layer is not finalised, new leaf layer share the same
840 * components as in the input layer
841 */
842 memcpy(&leaf_layer.linked_components, &input_layer_ctx->linked_components,
843 sizeof(input_layer_ctx->linked_components));
844 }
Maulik Patel7cc80872024-04-04 12:00:29 +0100845
846 if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100847 return DPE_INVALID_ARGUMENT;
848 }
849
850 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel7cc80872024-04-04 12:00:29 +0100851 leaf_layer.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100852 /* Copy the public key provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100853 memcpy(&leaf_layer.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100854 public_key,
855 public_key_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100856 leaf_layer.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100857
858 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel7cc80872024-04-04 12:00:29 +0100859 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100860
861 } else {
862 /* No external public key is provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100863 leaf_layer.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100864
865 if ((label_size > 0) && (label != NULL)) {
866 /* Copy the label provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100867 memcpy(&leaf_layer.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100868 label,
869 label_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100870 leaf_layer.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100871
872 } else {
Maulik Patel7cc80872024-04-04 12:00:29 +0100873 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100874 }
875 }
876
Maulik Patel009450d2024-04-23 12:03:10 +0100877 /* Get parent layer derived public key to verify the certificate signature */
878 parent_layer_idx = leaf_layer.parent_layer_idx;
879 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
880 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
881
Maulik Patele6adc112023-08-18 14:21:51 +0100882 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100883 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100884 */
Maulik Patelcbded682023-12-07 11:50:16 +0000885 /* Create leaf certificate */
Maulik Patel009450d2024-04-23 12:03:10 +0100886 err = prepare_layer_certificate(&leaf_layer, parent_layer_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100887 if (err != DPE_NO_ERROR) {
888 return err;
889 }
890
Maulik Patel7cc80872024-04-04 12:00:29 +0100891 err = encode_layer_certificate(&leaf_layer,
Tamas Ban257471b2024-03-25 13:49:53 +0100892 certificate_buf,
893 certificate_buf_size,
894 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100895 if (err != DPE_NO_ERROR) {
896 return err;
897 }
898
Maulik Patele6adc112023-08-18 14:21:51 +0100899 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
900 return DPE_INVALID_ARGUMENT;
901 }
902
903 memcpy(derived_public_key_buf,
904 &parent_layer_ctx->data.attest_pub_key[0],
905 parent_layer_ctx->data.attest_pub_key_len);
906 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
907
Maulik Patel91edd632024-02-26 07:44:41 +0000908 /* Renew handle for the same context, if requested */
909 if (retain_context) {
910 *new_context_handle = input_ctx_handle;
911 status = renew_nonce(new_context_handle);
912 if (status != PSA_SUCCESS) {
913 return DPE_INTERNAL_ERROR;
914 }
915 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
916
917 } else {
918 *new_context_handle = INVALID_HANDLE;
919 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100920 }
Maulik Patele6adc112023-08-18 14:21:51 +0100921
Maulik Patel9a2a5672024-03-14 13:43:58 +0000922 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000923 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100924 certificate_buf,
925 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100926
Tamas Baneb8d7f12024-04-03 13:55:22 +0200927 destroy_layer_keys(&leaf_layer);
928
Maulik Patele6adc112023-08-18 14:21:51 +0100929 return DPE_NO_ERROR;
930}
Maulik Patel83a6b592023-12-05 15:20:30 +0000931
932dpe_error_t get_certificate_chain_request(int input_ctx_handle,
933 bool retain_context,
934 bool clear_from_context,
935 uint8_t *certificate_chain_buf,
936 size_t certificate_chain_buf_size,
937 size_t *certificate_chain_actual_size,
938 int *new_context_handle)
939{
940 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000941 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000942 psa_status_t status;
943 struct layer_context_t *layer_ctx;
944
Tamas Bana5e2f582024-01-25 16:59:26 +0100945 log_get_certificate_chain(input_ctx_handle, retain_context,
946 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000947
948 /* Validate input handle */
949 if (!is_input_handle_valid(input_ctx_handle)) {
950 return DPE_INVALID_ARGUMENT;
951 }
952
953 /* Get component index from the input handle */
954 input_ctx_idx = GET_IDX(input_ctx_handle);
955 /* Get current linked layer idx */
956 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
957 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
958
959 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100960 if (layer_ctx->state != LAYER_STATE_FINALISED) {
961 /* If the context has accumulated info and not yet part of a certificate,
962 * return an invalid-argument error
963 */
964 return DPE_INVALID_ARGUMENT;
965 }
966
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100967 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000968 certificate_chain_buf,
969 certificate_chain_buf_size,
970 certificate_chain_actual_size);
971 if (err != DPE_NO_ERROR) {
972 return err;
973 }
974
975 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
976
977 /* Renew handle for the same context, if requested */
978 if (retain_context) {
979 *new_context_handle = input_ctx_handle;
980 status = renew_nonce(new_context_handle);
981 if (status != PSA_SUCCESS) {
982 return DPE_INTERNAL_ERROR;
983 }
984 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
985
986 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +0100987 //TODO: Reimplement the clear_from_context functionality after memory
988 // optimization; Certificates are not ready made and they are not
989 // stored in the layer context anymore. They are created on-the-fly
990 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000991 }
992
993 } else {
994 *new_context_handle = INVALID_HANDLE;
995 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
996 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000997 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000998
999 return DPE_NO_ERROR;
1000}