blob: c0168bfa6251ab1f1c3667691a6cb68bcaeffe53 [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));
106 //TODO: Question: how to initialise MHU Id mapping?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000107 component_ctx_array[i].target_locality = 0;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100108 /* Allow component to be derived by default */
109}
110
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100111static void initialise_layer(int i)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100112{
Maulik Patel009450d2024-04-23 12:03:10 +0100113 int j;
114
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100115 layer_ctx_array[i].idx = i;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100116 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
117 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000118 layer_ctx_array[i].is_cdi_to_be_exported = false;
Maulik Patel87c47ce2024-04-22 13:30:56 +0100119 layer_ctx_array[i].is_rot_layer = false;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000120 layer_ctx_array[i].cert_id = DPE_CERT_ID_INVALID;
Maulik Patele6adc112023-08-18 14:21:51 +0100121 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
122 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patelad2f3db2023-05-17 15:41:36 +0100123 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100124 layer_ctx_array[i].data.cdi_key_id = PSA_KEY_ID_NULL;
125 layer_ctx_array[i].data.attest_key_id = PSA_KEY_ID_NULL;
Maulik Patel009450d2024-04-23 12:03:10 +0100126 layer_ctx_array[i].linked_components.count = 0;
127 for (j = 0; j < ARRAY_SIZE(layer_ctx_array[i].linked_components.idx); j++) {
128 layer_ctx_array[i].linked_components.idx[j] = INVALID_COMPONENT_IDX;
129 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100130}
131
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100132static void close_layer(int i)
133{
134 destroy_layer_keys(&layer_ctx_array[i]);
135 initialise_layer(i);
136}
137
Maulik Patelad2f3db2023-05-17 15:41:36 +0100138static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
139 const DiceInputValues *dice_inputs)
140{
141 size_t hash_len;
142 psa_status_t status;
143
144 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
145 DICE_HASH_SIZE);
146 memcpy(&dest_ctx->data.measurement_descriptor,
147 dice_inputs->code_descriptor,
148 dice_inputs->code_descriptor_size);
149
150 dest_ctx->data.measurement_descriptor_size =
151 dice_inputs->code_descriptor_size;
152
153 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
154 memcpy(&dest_ctx->data.signer_id_descriptor,
155 dice_inputs->authority_descriptor,
156 dice_inputs->authority_descriptor_size);
157
158 dest_ctx->data.signer_id_descriptor_size =
159 dice_inputs->authority_descriptor_size;
160
161 if (dice_inputs->config_type == kDiceConfigTypeInline) {
162 /* Copy config_value */
163 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
164 DICE_INLINE_CONFIG_SIZE);
165
166 } else {
167 /* Copy config descriptor */
168 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
169 dice_inputs->config_descriptor_size);
170 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
171
172 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100173 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100174 dice_inputs->config_descriptor,
175 dice_inputs->config_descriptor_size,
176 dest_ctx->data.config_value,
177 sizeof(dest_ctx->data.config_value),
178 &hash_len);
179
180 if (status != PSA_SUCCESS) {
181 return DPE_INTERNAL_ERROR;
182 }
183 }
184
185 dest_ctx->data.mode = dice_inputs->mode;
186 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
187
188 return DPE_NO_ERROR;
189}
190
191static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
192{
193 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
194 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
195 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
196 return false;
197 }
198
199 return true;
200}
201
202static bool is_input_handle_valid(int input_context_handle)
203{
204 uint16_t idx = GET_IDX(input_context_handle);
205 uint16_t nonce = GET_NONCE(input_context_handle);
206
207 /* Validate input handle id and nonce */
208 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
209 return false;
210 }
211
212 if (nonce == component_ctx_array[idx].nonce) {
213 return true;
214 }
215
216 return false;
217}
218
Maulik Patel58595d32023-06-22 10:08:53 +0100219/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
220 * same order
221 */
222static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
223 size_t max_size,
224 size_t *dest_size,
225 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100226{
Maulik Patel58595d32023-06-22 10:08:53 +0100227 size_t out_size = 0;
228
229 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
230 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
231 return PSA_ERROR_BUFFER_TOO_SMALL;
232 }
233
234 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
235 out_size += DICE_HASH_SIZE;
236
237 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
238 out_size += DICE_INLINE_CONFIG_SIZE;
239
240 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
241 out_size += DICE_HASH_SIZE;
242
243 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
244 out_size += sizeof(comp_ctx->data.mode);
245
246 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
247 out_size += DICE_HIDDEN_SIZE;
248
249 *dest_size = out_size;
250
251 return PSA_SUCCESS;
252}
253
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100254static psa_status_t compute_layer_cdi_attest_input(struct layer_context_t *layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100255{
256 psa_status_t status;
257 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
258 size_t ctx_data_size, hash_len;
Maulik Patel009450d2024-04-23 12:03:10 +0100259 int i, idx;
260 uint16_t num_of_linked_components;
261
262 num_of_linked_components = layer_ctx->linked_components.count;
263 if (num_of_linked_components == 0) {
264 /* No components to hash */
265 return PSA_SUCCESS;
266 }
Maulik Patel58595d32023-06-22 10:08:53 +0100267
268 psa_hash_operation_t hash_op = psa_hash_operation_init();
269 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
270 if (status != PSA_SUCCESS) {
271 return status;
272 }
273
274 //TODO:
275 /* How to combine measurements of multiple SW components into a single hash
276 * is not yet defined by the Open DICE profile. This implementation
277 * concatenates the data of all SW components which belong to the same layer
278 * and hash it.
279 */
Maulik Patel009450d2024-04-23 12:03:10 +0100280 for (i = 0; i < num_of_linked_components; i++) {
281 idx = layer_ctx->linked_components.idx[i];
282 status = get_component_data_for_attest_cdi(component_ctx_data,
283 sizeof(component_ctx_data),
284 &ctx_data_size,
285 &component_ctx_array[idx]);
286 if (status != PSA_SUCCESS) {
287 return status;
288 }
Maulik Patel58595d32023-06-22 10:08:53 +0100289
Maulik Patel009450d2024-04-23 12:03:10 +0100290 status = psa_hash_update(&hash_op,
291 component_ctx_data,
292 ctx_data_size);
293 if (status != PSA_SUCCESS) {
294 return status;
Maulik Patel58595d32023-06-22 10:08:53 +0100295 }
296 }
297
298 status = psa_hash_finish(&hash_op,
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100299 &layer_ctx->attest_cdi_hash_input[0],
300 sizeof(layer_ctx->attest_cdi_hash_input),
Maulik Patel58595d32023-06-22 10:08:53 +0100301 &hash_len);
302
303 assert(hash_len == DPE_HASH_ALG_SIZE);
304
305 return status;
306}
307
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000308static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
309 uint8_t *exported_cdi_buf,
310 size_t exported_cdi_buf_size,
311 size_t *exported_cdi_actual_size)
312{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100313 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
314 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000315 psa_status_t status;
316 dpe_error_t err;
317
Tamas Ban5179a4d2024-01-25 17:05:30 +0100318 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000319 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100320 cdi_attest_buf,
321 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000322 if (status != PSA_SUCCESS) {
323 return DPE_INTERNAL_ERROR;
324 }
325
326 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100327 err = encode_cdi(cdi_attest_buf,
328 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000329 exported_cdi_buf,
330 exported_cdi_buf_size,
331 exported_cdi_actual_size);
332 if (err != DPE_NO_ERROR) {
333 return err;
334 }
335 layer_ctx->is_cdi_to_be_exported = true;
336
337 return DPE_NO_ERROR;
338}
339
Maulik Patel009450d2024-04-23 12:03:10 +0100340static dpe_error_t prepare_layer_certificate(struct layer_context_t *layer_ctx,
341 const struct layer_context_t *parent_layer_ctx)
Maulik Patel58595d32023-06-22 10:08:53 +0100342{
Maulik Patel58595d32023-06-22 10:08:53 +0100343 psa_status_t status;
Maulik Patel58595d32023-06-22 10:08:53 +0100344
Maulik Patel2358bbb2023-07-21 10:56:56 +0100345 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patel87c47ce2024-04-22 13:30:56 +0100346 if ((!layer_ctx->is_rot_layer) &&
Maulik Patele6adc112023-08-18 14:21:51 +0100347 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100348
Maulik Patele6adc112023-08-18 14:21:51 +0100349 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100350
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100351 status = compute_layer_cdi_attest_input(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100352 if (status != PSA_SUCCESS) {
353 return DPE_INTERNAL_ERROR;
354 }
355
Maulik Patele6adc112023-08-18 14:21:51 +0100356 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100357 if (status != PSA_SUCCESS) {
358 return DPE_INTERNAL_ERROR;
359 }
360
Maulik Patele6adc112023-08-18 14:21:51 +0100361 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100362 if (status != PSA_SUCCESS) {
363 return DPE_INTERNAL_ERROR;
364 }
365 }
366
Maulik Patele6adc112023-08-18 14:21:51 +0100367 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100368 if (status != PSA_SUCCESS) {
369 return DPE_INTERNAL_ERROR;
370 }
371
Maulik Patele6adc112023-08-18 14:21:51 +0100372 if (!layer_ctx->is_external_pub_key_provided) {
373 status = derive_attestation_key(layer_ctx);
374 if (status != PSA_SUCCESS) {
375 return DPE_INTERNAL_ERROR;
376 }
Maulik Patel58595d32023-06-22 10:08:53 +0100377 }
378
Maulik Patele6adc112023-08-18 14:21:51 +0100379 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100380 if (status != PSA_SUCCESS) {
381 return DPE_INTERNAL_ERROR;
382 }
383
Tamas Ban257471b2024-03-25 13:49:53 +0100384 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100385}
386
387static uint16_t open_new_layer(void)
388{
389 int i;
390
391 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
392 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
393 layer_ctx_array[i].state = LAYER_STATE_OPEN;
394 return i;
395 }
396 }
397
Maulik Patel2358bbb2023-07-21 10:56:56 +0100398 //TODO: There is an open issue of layer creation as described below.
399 /* This is causing extra unintended layers to open. Since each layer
400 * has some context data and certificate buffer of 3k, it is
401 * causing RAM overflow. Hence until resoluton is reached, once all
402 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100403 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
404 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
405 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
406 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100407 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
408 */
409 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100410}
411
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000412static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100413{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000414 // TODO: FIXME
415 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100416 return true;
417}
418
Maulik Patelcb14cde2024-01-23 12:39:53 +0000419static bool is_cert_id_used(uint32_t cert_id, uint16_t *layer_idx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100420{
Maulik Patelcb14cde2024-01-23 12:39:53 +0000421 int i;
422
423 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
424 if (layer_ctx_array[i].cert_id == cert_id) {
425 *layer_idx = i;
426 return true;
427 }
428 }
429
430 /* No certificate ID match found */
431 return false;
432}
433
434static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx,
435 uint32_t cert_id)
436{
437 uint16_t parent_layer_idx, layer_idx_to_link;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100438
439 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
440
441 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
442 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
443
Maulik Patelcb14cde2024-01-23 12:39:53 +0000444 if (cert_id != DPE_CERT_ID_INVALID) {
445 /* cert id was sent by the client */
446 if (cert_id == DPE_CERT_ID_SAME_AS_PARENT) {
447 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
448 /* Cannot add to the layer which is already finalised */
449 return DPE_INTERNAL_ERROR;
450 }
451 /* Derived context belongs to the same certificate as its parent component */
452 new_ctx->linked_layer_idx = parent_layer_idx;
453
454 } else if (is_cert_id_used(cert_id, &layer_idx_to_link)) {
455 /* Cert ID is already in use */
456 if (layer_ctx_array[layer_idx_to_link].state == LAYER_STATE_FINALISED) {
457 /* Cannot add to the layer which is already finalised */
458 return DPE_INTERNAL_ERROR;
459 }
460 /* Use the same layer that is associated with cert_id */
461 new_ctx->linked_layer_idx = layer_idx_to_link;
462 /* Linked layer's parent is already assigned when it was opened */
463
464 } else {
465 /* Open new layer and link derived context to new layer */
466 layer_idx_to_link = open_new_layer();
467 if (layer_idx_to_link == INVALID_LAYER_IDX) {
468 return DPE_INTERNAL_ERROR;
469 }
470 /* Link this context to the new layer */
471 new_ctx->linked_layer_idx = layer_idx_to_link;
472 /* New layer's parent is parent component's layer */
473 layer_ctx_array[layer_idx_to_link].parent_layer_idx = parent_layer_idx;
474 layer_ctx_array[layer_idx_to_link].cert_id = cert_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100475 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100476
477 } else {
Maulik Patelcb14cde2024-01-23 12:39:53 +0000478 /* cert id was not sent by the client */
479 //TODO: To be implemented; return error for now.
480 return DPE_INVALID_ARGUMENT;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100481 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100482
483 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100484}
485
Jamie Fox34681992023-09-04 18:14:06 +0100486/**
487 * \brief Create a root of trust component context.
488 *
489 * \param[out] rot_ctx_handle A new context handle for the RoT context.
490 *
491 * \return Returns error code of type dpe_error_t
492 */
493static dpe_error_t create_rot_context(int *rot_ctx_handle)
494{
Jamie Fox34681992023-09-04 18:14:06 +0100495 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
496 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
497
Maulik Patel87c47ce2024-04-22 13:30:56 +0100498 rot_layer_ctx->is_rot_layer = true;
Maulik Patela81605b2023-10-24 12:17:03 +0100499 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100500 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100501 /* Get the RoT CDI key for the RoT layer */
502 rot_layer_ctx->data.cdi_key_id = dpe_plat_get_rot_cdi_key_id();
Maulik Patela81605b2023-10-24 12:17:03 +0100503 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100504 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100505 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100506 rot_comp_ctx->parent_idx = 0;
507 /* Link context to RoT Layer */
508 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
509 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100510 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
511
512 return DPE_NO_ERROR;
513}
514
515dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
516{
517 int i;
518
519 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
520 set_context_to_default(i);
521 }
522
523 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100524 initialise_layer(i);
Jamie Fox34681992023-09-04 18:14:06 +0100525 }
526
527 return create_rot_context(rot_ctx_handle);
528}
529
Maulik Patela81605b2023-10-24 12:17:03 +0100530dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000531 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100532 bool retain_parent_context,
533 bool allow_new_context_to_derive,
534 bool create_certificate,
535 const DiceInputValues *dice_inputs,
536 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000537 int32_t target_locality,
538 bool return_certificate,
539 bool allow_new_context_to_export,
540 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100541 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000542 int *new_parent_context_handle,
543 uint8_t *new_certificate_buf,
544 size_t new_certificate_buf_size,
545 size_t *new_certificate_actual_size,
546 uint8_t *exported_cdi_buf,
547 size_t exported_cdi_buf_size,
548 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100549{
Maulik Patel58595d32023-06-22 10:08:53 +0100550 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100551 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel009450d2024-04-23 12:03:10 +0100552 uint16_t parent_ctx_idx, linked_layer_idx, parent_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100553 int free_component_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100554 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100555
Maulik Patelcb14cde2024-01-23 12:39:53 +0000556 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100557 allow_new_context_to_derive, create_certificate, dice_inputs,
558 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100559
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000560 if (export_cdi && !create_certificate) {
561 return DPE_INVALID_ARGUMENT;
562 }
563
Maulik Patelad2f3db2023-05-17 15:41:36 +0100564 /* Validate dice inputs */
565 if (!is_dice_input_valid(dice_inputs)) {
566 return DPE_INVALID_ARGUMENT;
567 }
568
569 /* Validate input handle */
570 if (!is_input_handle_valid(input_ctx_handle)) {
571 return DPE_INVALID_ARGUMENT;
572 }
Maulik Patela81605b2023-10-24 12:17:03 +0100573 /* Get parent component index from the input handle */
574 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100575
576 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100577 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100578 */
Maulik Patela81605b2023-10-24 12:17:03 +0100579 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100580
Maulik Patela81605b2023-10-24 12:17:03 +0100581 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100582
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000583 /* Check if parent context is allowed to derive */
584 if (!parent_ctx->is_allowed_to_derive) {
585 return DPE_INVALID_ARGUMENT;
586 }
587
Maulik Patelad2f3db2023-05-17 15:41:36 +0100588 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000589 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100590 return DPE_INVALID_ARGUMENT;
591 }
592
Maulik Patela81605b2023-10-24 12:17:03 +0100593 /* Get next free component index to add new derived context */
594 free_component_idx = get_free_component_context_index();
595 if (free_component_idx < 0) {
596 return DPE_INSUFFICIENT_MEMORY;
597 }
598
599 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000600 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
601 /* If parent context has export enabled and input allow_new_context_to_export
602 * is true, then allow context CDI to be exported for derived context
603 */
604 derived_ctx->is_export_cdi_allowed = true;
605 } else {
606 /* Export of new context CDI is NOT allowed */
607 derived_ctx->is_export_cdi_allowed = false;
608 if (export_cdi) {
609 return DPE_INVALID_ARGUMENT;
610 }
611 }
612
Maulik Patela81605b2023-10-24 12:17:03 +0100613 /* Copy dice input to the new derived component context */
614 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100615 if (err != DPE_NO_ERROR) {
616 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100617 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000618 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100619
Maulik Patela81605b2023-10-24 12:17:03 +0100620 /* Update parent idx in new derived component context */
621 derived_ctx->parent_idx = parent_ctx_idx;
622 /* Mark new derived component index as in use */
623 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000624 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000625 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100626 if (err != DPE_NO_ERROR) {
627 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100628 }
629
630 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100631 /* Retain and return parent handle with renewed nonce */
632 *new_parent_context_handle = input_ctx_handle;
633 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100634 if (err != DPE_NO_ERROR) {
635 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100636 }
Maulik Patela81605b2023-10-24 12:17:03 +0100637 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
638
Maulik Patelad2f3db2023-05-17 15:41:36 +0100639 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100640 /* Return invalid handle */
641 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100642 parent_ctx->nonce = INVALID_NONCE_VALUE;
643 }
644
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000645 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100646 /* Return handle to derived context */
647 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
648 err = renew_nonce(new_context_handle);
649 if (err != DPE_NO_ERROR) {
650 return err;
651 }
652 /* Update nonce in new derived component context */
653 derived_ctx->nonce = GET_NONCE(*new_context_handle);
654
655 } else {
656 /* Return invalid handle */
657 *new_context_handle = INVALID_HANDLE;
658 derived_ctx->nonce = INVALID_NONCE_VALUE;
659 }
660
Maulik Patel5ac87802024-03-14 14:22:19 +0000661 linked_layer_idx = derived_ctx->linked_layer_idx;
662 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
663 layer_ctx = &layer_ctx_array[linked_layer_idx];
Maulik Patel009450d2024-04-23 12:03:10 +0100664 err = store_linked_component(layer_ctx, free_component_idx);
665 if (err != DPE_NO_ERROR) {
666 return err;
667 }
668 parent_layer_idx = layer_ctx->parent_layer_idx;
669 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
670 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
671
Maulik Patela81605b2023-10-24 12:17:03 +0100672 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000673 layer_ctx->is_cdi_to_be_exported = export_cdi;
674
Maulik Patelcbded682023-12-07 11:50:16 +0000675 /* Finalise the layer */
676 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patel009450d2024-04-23 12:03:10 +0100677 err = prepare_layer_certificate(layer_ctx, parent_layer_ctx);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000678 if (err != DPE_NO_ERROR) {
679 return err;
680 }
681
682 if (return_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000683 /* Encode and return generated layer certificate */
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100684 err = encode_layer_certificate(layer_ctx,
Tamas Ban257471b2024-03-25 13:49:53 +0100685 new_certificate_buf,
686 new_certificate_buf_size,
687 new_certificate_actual_size);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000688 if (err != DPE_NO_ERROR) {
689 return err;
690 }
691 }
692 }
693
694 if (export_cdi) {
695 err = get_encoded_cdi_to_export(layer_ctx,
696 exported_cdi_buf,
697 exported_cdi_buf_size,
698 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100699 if (err != DPE_NO_ERROR) {
700 return err;
701 }
702 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000703 log_derive_context_output_handles(*new_parent_context_handle,
704 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100705
Maulik Patel5ac87802024-03-14 14:22:19 +0000706 /* Log context and layer info and certificate if no error */
707 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
708 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100709 if (return_certificate) {
Maulik Patel5ac87802024-03-14 14:22:19 +0000710 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100711 new_certificate_buf,
712 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000713 }
714
Maulik Patelad2f3db2023-05-17 15:41:36 +0100715 return DPE_NO_ERROR;
716}
Maulik Patel54d65f72023-06-28 13:04:36 +0100717
718dpe_error_t destroy_context_request(int input_ctx_handle,
719 bool destroy_recursively)
720{
721 uint16_t input_ctx_idx, linked_layer_idx;
722 int i;
723 bool is_layer_empty;
Maulik Patel009450d2024-04-23 12:03:10 +0100724 struct layer_context_t *layer_ctx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100725
726 log_destroy_context(input_ctx_handle, destroy_recursively);
727
Maulik Patela81605b2023-10-24 12:17:03 +0100728 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100729 input_ctx_idx = GET_IDX(input_ctx_handle);
730
Maulik Patel54d65f72023-06-28 13:04:36 +0100731 /* Validate input handle */
732 if (!is_input_handle_valid(input_ctx_handle)) {
733 return DPE_INVALID_ARGUMENT;
734 }
735 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
736
Jamie Fox34681992023-09-04 18:14:06 +0100737#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100738 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
739 /* All layers till hypervisor cannot be destroyed dynamically */
740 return DPE_INVALID_ARGUMENT;
741 }
Jamie Fox34681992023-09-04 18:14:06 +0100742#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100743
Maulik Patel009450d2024-04-23 12:03:10 +0100744 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patel54d65f72023-06-28 13:04:36 +0100745
746 if (!destroy_recursively) {
747 set_context_to_default(input_ctx_idx);
Maulik Patel009450d2024-04-23 12:03:10 +0100748 layer_ctx = &layer_ctx_array[linked_layer_idx];
749 remove_linked_component(layer_ctx, input_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100750 } else {
751 //TODO: To be implemented
752 }
753
Maulik Patel54d65f72023-06-28 13:04:36 +0100754 /* Close the layer if all of its contexts are destroyed */
755 is_layer_empty = true;
756 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
757 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
758 /* There are active component context in the layer */
759 is_layer_empty = false;
760 break;
761 }
762 }
763
764 if (is_layer_empty) {
Maulik Patelfb2db1c2024-04-12 11:11:21 +0100765 close_layer(linked_layer_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100766 }
767
768 return DPE_NO_ERROR;
769}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100770
Maulik Patel009450d2024-04-23 12:03:10 +0100771struct component_context_t* get_component_ctx_ptr(uint16_t component_idx)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100772{
773 /* Safety case */
774 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
775 return NULL;
776 }
777
Maulik Patel009450d2024-04-23 12:03:10 +0100778 return &component_ctx_array[component_idx];
Maulik Patel2358bbb2023-07-21 10:56:56 +0100779}
780
Maulik Patele6adc112023-08-18 14:21:51 +0100781struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
782{
783 /* Safety case */
784 if (layer_idx >= MAX_NUM_OF_LAYERS) {
785 return NULL;
786 }
787
788 return &layer_ctx_array[layer_idx];
789}
790
791dpe_error_t certify_key_request(int input_ctx_handle,
792 bool retain_context,
793 const uint8_t *public_key,
794 size_t public_key_size,
795 const uint8_t *label,
796 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000797 uint8_t *certificate_buf,
798 size_t certificate_buf_size,
799 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100800 uint8_t *derived_public_key_buf,
801 size_t derived_public_key_buf_size,
802 size_t *derived_public_key_actual_size,
803 int *new_context_handle)
804{
805 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
806 dpe_error_t err;
807 psa_status_t status;
Tamas Baneb8d7f12024-04-03 13:55:22 +0200808 struct layer_context_t *parent_layer_ctx, *input_layer_ctx;
809 struct layer_context_t leaf_layer = {0};
Maulik Patele6adc112023-08-18 14:21:51 +0100810
811 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
812 label, label_size);
813
814 /* Validate input handle */
815 if (!is_input_handle_valid(input_ctx_handle)) {
816 return DPE_INVALID_ARGUMENT;
817 }
818
819 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
820 return DPE_INVALID_ARGUMENT;
821 }
822
823 /* Get component index from the input handle */
824 input_ctx_idx = GET_IDX(input_ctx_handle);
825 /* Get current linked layer idx */
826 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
827 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
Tamas Baneb8d7f12024-04-03 13:55:22 +0200828 input_layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100829
Tamas Baneb8d7f12024-04-03 13:55:22 +0200830 if (input_layer_ctx->state == LAYER_STATE_FINALISED) {
831 /* Input layer is finalised, new leaf layer is its child now */
832 leaf_layer.parent_layer_idx = input_layer_idx;
833 /* Linked components count already initialised to 0 */
834
835 } else {
836 /* Input layer is not finalised, new leaf layer share the same
837 * components as in the input layer
838 */
839 memcpy(&leaf_layer.linked_components, &input_layer_ctx->linked_components,
840 sizeof(input_layer_ctx->linked_components));
841 }
Maulik Patel7cc80872024-04-04 12:00:29 +0100842
843 if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100844 return DPE_INVALID_ARGUMENT;
845 }
846
847 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel7cc80872024-04-04 12:00:29 +0100848 leaf_layer.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100849 /* Copy the public key provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100850 memcpy(&leaf_layer.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100851 public_key,
852 public_key_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100853 leaf_layer.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100854
855 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel7cc80872024-04-04 12:00:29 +0100856 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100857
858 } else {
859 /* No external public key is provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100860 leaf_layer.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100861
862 if ((label_size > 0) && (label != NULL)) {
863 /* Copy the label provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100864 memcpy(&leaf_layer.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100865 label,
866 label_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100867 leaf_layer.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100868
869 } else {
Maulik Patel7cc80872024-04-04 12:00:29 +0100870 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100871 }
872 }
873
Maulik Patel009450d2024-04-23 12:03:10 +0100874 /* Get parent layer derived public key to verify the certificate signature */
875 parent_layer_idx = leaf_layer.parent_layer_idx;
876 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
877 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
878
Maulik Patele6adc112023-08-18 14:21:51 +0100879 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100880 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100881 */
Maulik Patelcbded682023-12-07 11:50:16 +0000882 /* Create leaf certificate */
Maulik Patel009450d2024-04-23 12:03:10 +0100883 err = prepare_layer_certificate(&leaf_layer, parent_layer_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100884 if (err != DPE_NO_ERROR) {
885 return err;
886 }
887
Maulik Patel7cc80872024-04-04 12:00:29 +0100888 err = encode_layer_certificate(&leaf_layer,
Tamas Ban257471b2024-03-25 13:49:53 +0100889 certificate_buf,
890 certificate_buf_size,
891 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100892 if (err != DPE_NO_ERROR) {
893 return err;
894 }
895
Maulik Patele6adc112023-08-18 14:21:51 +0100896 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
897 return DPE_INVALID_ARGUMENT;
898 }
899
900 memcpy(derived_public_key_buf,
901 &parent_layer_ctx->data.attest_pub_key[0],
902 parent_layer_ctx->data.attest_pub_key_len);
903 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
904
Maulik Patel91edd632024-02-26 07:44:41 +0000905 /* Renew handle for the same context, if requested */
906 if (retain_context) {
907 *new_context_handle = input_ctx_handle;
908 status = renew_nonce(new_context_handle);
909 if (status != PSA_SUCCESS) {
910 return DPE_INTERNAL_ERROR;
911 }
912 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
913
914 } else {
915 *new_context_handle = INVALID_HANDLE;
916 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100917 }
Maulik Patele6adc112023-08-18 14:21:51 +0100918
Maulik Patel9a2a5672024-03-14 13:43:58 +0000919 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000920 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100921 certificate_buf,
922 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100923
Tamas Baneb8d7f12024-04-03 13:55:22 +0200924 destroy_layer_keys(&leaf_layer);
925
Maulik Patele6adc112023-08-18 14:21:51 +0100926 return DPE_NO_ERROR;
927}
Maulik Patel83a6b592023-12-05 15:20:30 +0000928
929dpe_error_t get_certificate_chain_request(int input_ctx_handle,
930 bool retain_context,
931 bool clear_from_context,
932 uint8_t *certificate_chain_buf,
933 size_t certificate_chain_buf_size,
934 size_t *certificate_chain_actual_size,
935 int *new_context_handle)
936{
937 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000938 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000939 psa_status_t status;
940 struct layer_context_t *layer_ctx;
941
Tamas Bana5e2f582024-01-25 16:59:26 +0100942 log_get_certificate_chain(input_ctx_handle, retain_context,
943 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000944
945 /* Validate input handle */
946 if (!is_input_handle_valid(input_ctx_handle)) {
947 return DPE_INVALID_ARGUMENT;
948 }
949
950 /* Get component index from the input handle */
951 input_ctx_idx = GET_IDX(input_ctx_handle);
952 /* Get current linked layer idx */
953 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
954 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
955
956 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100957 if (layer_ctx->state != LAYER_STATE_FINALISED) {
958 /* If the context has accumulated info and not yet part of a certificate,
959 * return an invalid-argument error
960 */
961 return DPE_INVALID_ARGUMENT;
962 }
963
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100964 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000965 certificate_chain_buf,
966 certificate_chain_buf_size,
967 certificate_chain_actual_size);
968 if (err != DPE_NO_ERROR) {
969 return err;
970 }
971
972 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
973
974 /* Renew handle for the same context, if requested */
975 if (retain_context) {
976 *new_context_handle = input_ctx_handle;
977 status = renew_nonce(new_context_handle);
978 if (status != PSA_SUCCESS) {
979 return DPE_INTERNAL_ERROR;
980 }
981 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
982
983 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +0100984 //TODO: Reimplement the clear_from_context functionality after memory
985 // optimization; Certificates are not ready made and they are not
986 // stored in the layer context anymore. They are created on-the-fly
987 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000988 }
989
990 } else {
991 *new_context_handle = INVALID_HANDLE;
992 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
993 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000994 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000995
996 return DPE_NO_ERROR;
997}