blob: 6c4aff38d1e47857dc08029088496ab1d7b370f6 [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) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200445 /* Cert_id was sent by the client */
Maulik Patelcb14cde2024-01-23 12:39:53 +0000446 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)) {
Tamas Ban33f1aec2024-06-04 12:15:18 +0200455 /* Cert_id is already in use but layer must be in open state, because
456 * cert_id is invalidated when layer gets finalized.
457 */
458 assert(layer_ctx_array[layer_idx_to_link].state != LAYER_STATE_FINALISED);
459
Maulik Patelcb14cde2024-01-23 12:39:53 +0000460 /* 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 Patel9a893122024-04-15 13:48:38 +0100530static void close_layer_if_empty(uint16_t layer_idx)
531{
532 if (layer_ctx_array[layer_idx].linked_components.count == 0) {
533 close_layer(layer_idx);
534 }
535}
536
Maulik Patela81605b2023-10-24 12:17:03 +0100537dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000538 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100539 bool retain_parent_context,
540 bool allow_new_context_to_derive,
541 bool create_certificate,
542 const DiceInputValues *dice_inputs,
543 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000544 int32_t target_locality,
545 bool return_certificate,
546 bool allow_new_context_to_export,
547 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100548 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000549 int *new_parent_context_handle,
550 uint8_t *new_certificate_buf,
551 size_t new_certificate_buf_size,
552 size_t *new_certificate_actual_size,
553 uint8_t *exported_cdi_buf,
554 size_t exported_cdi_buf_size,
555 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100556{
Maulik Patel58595d32023-06-22 10:08:53 +0100557 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100558 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel009450d2024-04-23 12:03:10 +0100559 uint16_t parent_ctx_idx, linked_layer_idx, parent_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100560 int free_component_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100561 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100562
Maulik Patelcb14cde2024-01-23 12:39:53 +0000563 log_derive_context(input_ctx_handle, cert_id, retain_parent_context,
Maulik Patela81605b2023-10-24 12:17:03 +0100564 allow_new_context_to_derive, create_certificate, dice_inputs,
565 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100566
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000567 if (export_cdi && !create_certificate) {
568 return DPE_INVALID_ARGUMENT;
569 }
570
Maulik Patelad2f3db2023-05-17 15:41:36 +0100571 /* Validate dice inputs */
572 if (!is_dice_input_valid(dice_inputs)) {
573 return DPE_INVALID_ARGUMENT;
574 }
575
576 /* Validate input handle */
577 if (!is_input_handle_valid(input_ctx_handle)) {
578 return DPE_INVALID_ARGUMENT;
579 }
Maulik Patela81605b2023-10-24 12:17:03 +0100580 /* Get parent component index from the input handle */
581 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100582
583 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100584 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100585 */
Maulik Patela81605b2023-10-24 12:17:03 +0100586 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100587
Maulik Patela81605b2023-10-24 12:17:03 +0100588 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100589
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000590 /* Check if parent context is allowed to derive */
591 if (!parent_ctx->is_allowed_to_derive) {
592 return DPE_INVALID_ARGUMENT;
593 }
594
Maulik Patelad2f3db2023-05-17 15:41:36 +0100595 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000596 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100597 return DPE_INVALID_ARGUMENT;
598 }
599
Maulik Patela81605b2023-10-24 12:17:03 +0100600 /* Get next free component index to add new derived context */
601 free_component_idx = get_free_component_context_index();
602 if (free_component_idx < 0) {
603 return DPE_INSUFFICIENT_MEMORY;
604 }
605
606 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000607 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
608 /* If parent context has export enabled and input allow_new_context_to_export
609 * is true, then allow context CDI to be exported for derived context
610 */
611 derived_ctx->is_export_cdi_allowed = true;
612 } else {
613 /* Export of new context CDI is NOT allowed */
614 derived_ctx->is_export_cdi_allowed = false;
615 if (export_cdi) {
616 return DPE_INVALID_ARGUMENT;
617 }
618 }
619
Maulik Patela81605b2023-10-24 12:17:03 +0100620 /* Copy dice input to the new derived component context */
621 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100622 if (err != DPE_NO_ERROR) {
623 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100624 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000625 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100626
Maulik Patela81605b2023-10-24 12:17:03 +0100627 /* Update parent idx in new derived component context */
628 derived_ctx->parent_idx = parent_ctx_idx;
629 /* Mark new derived component index as in use */
630 derived_ctx->in_use = true;
Maulik Pateld2806072024-02-02 10:30:08 +0000631 derived_ctx->is_allowed_to_derive = allow_new_context_to_derive;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000632 err = assign_layer_to_context(derived_ctx, cert_id);
Maulik Patela81605b2023-10-24 12:17:03 +0100633 if (err != DPE_NO_ERROR) {
634 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100635 }
636
Maulik Patel9a893122024-04-15 13:48:38 +0100637 linked_layer_idx = derived_ctx->linked_layer_idx;
638 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
639 layer_ctx = &layer_ctx_array[linked_layer_idx];
640 err = store_linked_component(layer_ctx, free_component_idx);
641 if (err != DPE_NO_ERROR) {
642 goto clean_up_and_exit;
643 }
644 parent_layer_idx = layer_ctx->parent_layer_idx;
645 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
646 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
647
648 if (create_certificate) {
649 layer_ctx->is_cdi_to_be_exported = export_cdi;
650
651 /* Finalise the layer */
652 layer_ctx->state = LAYER_STATE_FINALISED;
Tamas Ban33f1aec2024-06-04 12:15:18 +0200653 layer_ctx->cert_id = DPE_CERT_ID_INVALID; /* make same cert_id reusable */
Maulik Patel9a893122024-04-15 13:48:38 +0100654 err = prepare_layer_certificate(layer_ctx, parent_layer_ctx);
655 if (err != DPE_NO_ERROR) {
656 goto clean_up_and_exit;
657 }
658
659 if (return_certificate) {
660 /* Encode and return generated layer certificate */
661 err = encode_layer_certificate(layer_ctx,
662 new_certificate_buf,
663 new_certificate_buf_size,
664 new_certificate_actual_size);
665 if (err != DPE_NO_ERROR) {
666 goto clean_up_and_exit;
667 }
668 }
669 }
670
671 if (export_cdi) {
672 err = get_encoded_cdi_to_export(layer_ctx,
673 exported_cdi_buf,
674 exported_cdi_buf_size,
675 exported_cdi_actual_size);
676 if (err != DPE_NO_ERROR) {
677 goto clean_up_and_exit;
678 }
679 }
680
Maulik Patelad2f3db2023-05-17 15:41:36 +0100681 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100682 /* Retain and return parent handle with renewed nonce */
683 *new_parent_context_handle = input_ctx_handle;
684 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100685 if (err != DPE_NO_ERROR) {
Maulik Patel9a893122024-04-15 13:48:38 +0100686 goto clean_up_and_exit;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100687 }
Maulik Patela81605b2023-10-24 12:17:03 +0100688 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
689
Maulik Patelad2f3db2023-05-17 15:41:36 +0100690 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100691 /* Return invalid handle */
692 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100693 parent_ctx->nonce = INVALID_NONCE_VALUE;
694 }
695
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000696 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100697 /* Return handle to derived context */
698 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
699 err = renew_nonce(new_context_handle);
700 if (err != DPE_NO_ERROR) {
701 return err;
702 }
703 /* Update nonce in new derived component context */
704 derived_ctx->nonce = GET_NONCE(*new_context_handle);
705
706 } else {
707 /* Return invalid handle */
708 *new_context_handle = INVALID_HANDLE;
709 derived_ctx->nonce = INVALID_NONCE_VALUE;
710 }
711
Maulik Patel9a2a5672024-03-14 13:43:58 +0000712 log_derive_context_output_handles(*new_parent_context_handle,
713 *new_context_handle);
Maulik Patela81605b2023-10-24 12:17:03 +0100714
Maulik Patel5ac87802024-03-14 14:22:19 +0000715 /* Log context and layer info and certificate if no error */
716 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
717 log_dpe_layer_metadata(layer_ctx, linked_layer_idx);
Jamie Fox4b8a6d62024-04-11 15:19:08 +0100718 if (return_certificate) {
Maulik Patel5ac87802024-03-14 14:22:19 +0000719 log_intermediate_certificate(linked_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100720 new_certificate_buf,
721 *new_certificate_actual_size);
Maulik Patel5ac87802024-03-14 14:22:19 +0000722 }
723
Maulik Patelad2f3db2023-05-17 15:41:36 +0100724 return DPE_NO_ERROR;
Maulik Patel9a893122024-04-15 13:48:38 +0100725
726clean_up_and_exit:
727 set_context_to_default(free_component_idx);
728 close_layer_if_empty(linked_layer_idx);
729
730 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100731}
Maulik Patel54d65f72023-06-28 13:04:36 +0100732
733dpe_error_t destroy_context_request(int input_ctx_handle,
734 bool destroy_recursively)
735{
736 uint16_t input_ctx_idx, linked_layer_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100737 struct layer_context_t *layer_ctx;
Maulik Patel54d65f72023-06-28 13:04:36 +0100738
739 log_destroy_context(input_ctx_handle, destroy_recursively);
740
Maulik Patela81605b2023-10-24 12:17:03 +0100741 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100742 input_ctx_idx = GET_IDX(input_ctx_handle);
743
Maulik Patel54d65f72023-06-28 13:04:36 +0100744 /* Validate input handle */
745 if (!is_input_handle_valid(input_ctx_handle)) {
746 return DPE_INVALID_ARGUMENT;
747 }
748 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
749
Jamie Fox34681992023-09-04 18:14:06 +0100750#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100751 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
752 /* All layers till hypervisor cannot be destroyed dynamically */
753 return DPE_INVALID_ARGUMENT;
754 }
Jamie Fox34681992023-09-04 18:14:06 +0100755#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100756
Maulik Patel009450d2024-04-23 12:03:10 +0100757 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patel54d65f72023-06-28 13:04:36 +0100758
759 if (!destroy_recursively) {
760 set_context_to_default(input_ctx_idx);
Maulik Patel009450d2024-04-23 12:03:10 +0100761 layer_ctx = &layer_ctx_array[linked_layer_idx];
762 remove_linked_component(layer_ctx, input_ctx_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100763 } else {
764 //TODO: To be implemented
765 }
766
Maulik Patel54d65f72023-06-28 13:04:36 +0100767 /* Close the layer if all of its contexts are destroyed */
Maulik Patel9a893122024-04-15 13:48:38 +0100768 close_layer_if_empty(linked_layer_idx);
Maulik Patel54d65f72023-06-28 13:04:36 +0100769
770 return DPE_NO_ERROR;
771}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100772
Maulik Patel009450d2024-04-23 12:03:10 +0100773struct component_context_t* get_component_ctx_ptr(uint16_t component_idx)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100774{
775 /* Safety case */
776 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
777 return NULL;
778 }
779
Maulik Patel009450d2024-04-23 12:03:10 +0100780 return &component_ctx_array[component_idx];
Maulik Patel2358bbb2023-07-21 10:56:56 +0100781}
782
Maulik Patele6adc112023-08-18 14:21:51 +0100783struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
784{
785 /* Safety case */
786 if (layer_idx >= MAX_NUM_OF_LAYERS) {
787 return NULL;
788 }
789
790 return &layer_ctx_array[layer_idx];
791}
792
793dpe_error_t certify_key_request(int input_ctx_handle,
794 bool retain_context,
795 const uint8_t *public_key,
796 size_t public_key_size,
797 const uint8_t *label,
798 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000799 uint8_t *certificate_buf,
800 size_t certificate_buf_size,
801 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100802 uint8_t *derived_public_key_buf,
803 size_t derived_public_key_buf_size,
804 size_t *derived_public_key_actual_size,
805 int *new_context_handle)
806{
807 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
808 dpe_error_t err;
809 psa_status_t status;
Tamas Baneb8d7f12024-04-03 13:55:22 +0200810 struct layer_context_t *parent_layer_ctx, *input_layer_ctx;
811 struct layer_context_t leaf_layer = {0};
Maulik Patele6adc112023-08-18 14:21:51 +0100812
813 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
814 label, label_size);
815
816 /* Validate input handle */
817 if (!is_input_handle_valid(input_ctx_handle)) {
818 return DPE_INVALID_ARGUMENT;
819 }
820
821 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
822 return DPE_INVALID_ARGUMENT;
823 }
824
825 /* Get component index from the input handle */
826 input_ctx_idx = GET_IDX(input_ctx_handle);
827 /* Get current linked layer idx */
828 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
829 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
Tamas Baneb8d7f12024-04-03 13:55:22 +0200830 input_layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100831
Tamas Baneb8d7f12024-04-03 13:55:22 +0200832 if (input_layer_ctx->state == LAYER_STATE_FINALISED) {
833 /* Input layer is finalised, new leaf layer is its child now */
834 leaf_layer.parent_layer_idx = input_layer_idx;
835 /* Linked components count already initialised to 0 */
836
837 } else {
838 /* Input layer is not finalised, new leaf layer share the same
839 * components as in the input layer
840 */
841 memcpy(&leaf_layer.linked_components, &input_layer_ctx->linked_components,
842 sizeof(input_layer_ctx->linked_components));
843 }
Maulik Patel7cc80872024-04-04 12:00:29 +0100844
845 if (public_key_size > sizeof(leaf_layer.data.attest_pub_key)) {
Maulik Patele6adc112023-08-18 14:21:51 +0100846 return DPE_INVALID_ARGUMENT;
847 }
848
849 if ((public_key_size > 0) && (public_key != NULL)) {
Maulik Patel7cc80872024-04-04 12:00:29 +0100850 leaf_layer.is_external_pub_key_provided = true;
Maulik Patele6adc112023-08-18 14:21:51 +0100851 /* Copy the public key provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100852 memcpy(&leaf_layer.data.attest_pub_key[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100853 public_key,
854 public_key_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100855 leaf_layer.data.attest_pub_key_len = public_key_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100856
857 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel7cc80872024-04-04 12:00:29 +0100858 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100859
860 } else {
861 /* No external public key is provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100862 leaf_layer.is_external_pub_key_provided = false;
Maulik Patele6adc112023-08-18 14:21:51 +0100863
864 if ((label_size > 0) && (label != NULL)) {
865 /* Copy the label provided */
Maulik Patel7cc80872024-04-04 12:00:29 +0100866 memcpy(&leaf_layer.data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100867 label,
868 label_size);
Maulik Patel7cc80872024-04-04 12:00:29 +0100869 leaf_layer.data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100870
871 } else {
Maulik Patel7cc80872024-04-04 12:00:29 +0100872 leaf_layer.data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100873 }
874 }
875
Maulik Patel009450d2024-04-23 12:03:10 +0100876 /* Get parent layer derived public key to verify the certificate signature */
877 parent_layer_idx = leaf_layer.parent_layer_idx;
878 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
879 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
880
Maulik Patele6adc112023-08-18 14:21:51 +0100881 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100882 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100883 */
Maulik Patelcbded682023-12-07 11:50:16 +0000884 /* Create leaf certificate */
Maulik Patel009450d2024-04-23 12:03:10 +0100885 err = prepare_layer_certificate(&leaf_layer, parent_layer_ctx);
Tamas Ban257471b2024-03-25 13:49:53 +0100886 if (err != DPE_NO_ERROR) {
887 return err;
888 }
889
Maulik Patel7cc80872024-04-04 12:00:29 +0100890 err = encode_layer_certificate(&leaf_layer,
Tamas Ban257471b2024-03-25 13:49:53 +0100891 certificate_buf,
892 certificate_buf_size,
893 certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100894 if (err != DPE_NO_ERROR) {
895 return err;
896 }
897
Maulik Patele6adc112023-08-18 14:21:51 +0100898 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
899 return DPE_INVALID_ARGUMENT;
900 }
901
902 memcpy(derived_public_key_buf,
903 &parent_layer_ctx->data.attest_pub_key[0],
904 parent_layer_ctx->data.attest_pub_key_len);
905 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
906
Maulik Patel91edd632024-02-26 07:44:41 +0000907 /* Renew handle for the same context, if requested */
908 if (retain_context) {
909 *new_context_handle = input_ctx_handle;
910 status = renew_nonce(new_context_handle);
911 if (status != PSA_SUCCESS) {
912 return DPE_INTERNAL_ERROR;
913 }
914 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
915
916 } else {
917 *new_context_handle = INVALID_HANDLE;
918 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
Maulik Patele6adc112023-08-18 14:21:51 +0100919 }
Maulik Patele6adc112023-08-18 14:21:51 +0100920
Maulik Patel9a2a5672024-03-14 13:43:58 +0000921 log_certify_key_output_handle(*new_context_handle);
Maulik Patel5ac87802024-03-14 14:22:19 +0000922 log_intermediate_certificate(input_layer_idx,
Tamas Ban257471b2024-03-25 13:49:53 +0100923 certificate_buf,
924 *certificate_actual_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100925
Tamas Baneb8d7f12024-04-03 13:55:22 +0200926 destroy_layer_keys(&leaf_layer);
927
Maulik Patele6adc112023-08-18 14:21:51 +0100928 return DPE_NO_ERROR;
929}
Maulik Patel83a6b592023-12-05 15:20:30 +0000930
931dpe_error_t get_certificate_chain_request(int input_ctx_handle,
932 bool retain_context,
933 bool clear_from_context,
934 uint8_t *certificate_chain_buf,
935 size_t certificate_chain_buf_size,
936 size_t *certificate_chain_actual_size,
937 int *new_context_handle)
938{
939 dpe_error_t err;
Maulik Pateld2806072024-02-02 10:30:08 +0000940 uint16_t input_ctx_idx, input_layer_idx;
Maulik Patel83a6b592023-12-05 15:20:30 +0000941 psa_status_t status;
942 struct layer_context_t *layer_ctx;
943
Tamas Bana5e2f582024-01-25 16:59:26 +0100944 log_get_certificate_chain(input_ctx_handle, retain_context,
945 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000946
947 /* Validate input handle */
948 if (!is_input_handle_valid(input_ctx_handle)) {
949 return DPE_INVALID_ARGUMENT;
950 }
951
952 /* Get component index from the input handle */
953 input_ctx_idx = GET_IDX(input_ctx_handle);
954 /* Get current linked layer idx */
955 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
956 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
957
958 layer_ctx = &layer_ctx_array[input_layer_idx];
Maulik Patelf2820972024-04-03 10:24:45 +0100959 if (layer_ctx->state != LAYER_STATE_FINALISED) {
960 /* If the context has accumulated info and not yet part of a certificate,
961 * return an invalid-argument error
962 */
963 return DPE_INVALID_ARGUMENT;
964 }
965
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100966 err = get_certificate_chain(layer_ctx,
Maulik Patel83a6b592023-12-05 15:20:30 +0000967 certificate_chain_buf,
968 certificate_chain_buf_size,
969 certificate_chain_actual_size);
970 if (err != DPE_NO_ERROR) {
971 return err;
972 }
973
974 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
975
976 /* Renew handle for the same context, if requested */
977 if (retain_context) {
978 *new_context_handle = input_ctx_handle;
979 status = renew_nonce(new_context_handle);
980 if (status != PSA_SUCCESS) {
981 return DPE_INTERNAL_ERROR;
982 }
983 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
984
985 if (clear_from_context) {
Tamas Ban257471b2024-03-25 13:49:53 +0100986 //TODO: Reimplement the clear_from_context functionality after memory
987 // optimization; Certificates are not ready made and they are not
988 // stored in the layer context anymore. They are created on-the-fly
989 // when requested. Add a test as well.
Maulik Patel83a6b592023-12-05 15:20:30 +0000990 }
991
992 } else {
993 *new_context_handle = INVALID_HANDLE;
994 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
995 }
Maulik Patel9a2a5672024-03-14 13:43:58 +0000996 log_get_certificate_chain_output_handle(*new_context_handle);
Maulik Patel83a6b592023-12-05 15:20:30 +0000997
998 return DPE_NO_ERROR;
999}