blob: 4e05da3f82ac0708eda017122feae77cfa077c31 [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
2 * Copyright (c) 2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_context_mngr.h"
9#include <assert.h>
10#include <string.h>
11#include "dice_protection_environment.h"
Maulik Patel2358bbb2023-07-21 10:56:56 +010012#include "dpe_certificate.h"
Maulik Patel58595d32023-06-22 10:08:53 +010013#include "dpe_crypto_interface.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010014#include "dpe_log.h"
Jamie Fox34681992023-09-04 18:14:06 +010015#include "dpe_plat.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010016#include "psa/crypto.h"
17
Maulik Pateldbfd5152023-05-30 17:02:42 +010018#ifdef DPE_TEST_MODE
19#define TEST_ROT_CDI_VAL { \
20 0xD2, 0x90, 0x66, 0x07, 0x2A, 0x2D, 0x2A, 0x00, \
21 0x91, 0x9D, 0xD9, 0x15, 0x14, 0xBE, 0x2D, 0xCC, \
22 0xA3, 0x9F, 0xDE, 0xC3, 0x35, 0x75, 0x84, 0x6E, \
23 0x4C, 0xB9, 0x28, 0xAC, 0x7A, 0x4E, 0X00, 0x7F \
24 }
25#endif /* DPE_TEST_MODE */
26
Maulik Patel58595d32023-06-22 10:08:53 +010027#define CONTEXT_DATA_MAX_SIZE sizeof(struct component_context_data_t)
28
Maulik Patelad2f3db2023-05-17 15:41:36 +010029static struct component_context_t component_ctx_array[MAX_NUM_OF_COMPONENTS];
30static struct layer_context_t layer_ctx_array[MAX_NUM_OF_LAYERS];
31
32static int get_free_component_context_index(void)
33{
34 int i;
35
36 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
37 if (!component_ctx_array[i].in_use) {
38 break;
39 }
40 }
41
42 if (i >= MAX_NUM_OF_COMPONENTS) {
43 /* No free index left in the array -- all used up! */
44 return -1;
45 }
46
47 return i;
48}
49
Maulik Patelad2f3db2023-05-17 15:41:36 +010050static dpe_error_t renew_nonce(int *handle)
51{
52 uint16_t nonce;
53
54 psa_status_t status = psa_generate_random((uint8_t *)&nonce, sizeof(nonce));
55 if (status != PSA_SUCCESS) {
56 return DPE_INTERNAL_ERROR;
57 }
58 *handle = SET_NONCE(*handle, nonce);
59
60 return DPE_NO_ERROR;
61}
62
Maulik Patelad2f3db2023-05-17 15:41:36 +010063static void set_context_to_default(int i)
64{
65 component_ctx_array[i].in_use = false;
66 component_ctx_array[i].is_leaf = false;
67 component_ctx_array[i].nonce = INVALID_NONCE_VALUE;
68 component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX;
69 component_ctx_array[i].linked_layer_idx = INVALID_LAYER_IDX;
70 (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t));
71 //TODO: Question: how to initialise MHU Id mapping?
72 /* Allow component to be derived by default */
73}
74
75static void invalidate_layer(int i)
76{
77 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
78 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patele6adc112023-08-18 14:21:51 +010079 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
80 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patel58595d32023-06-22 10:08:53 +010081 (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id);
82 (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +010083 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
84}
85
Maulik Patelad2f3db2023-05-17 15:41:36 +010086static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
87 const DiceInputValues *dice_inputs)
88{
89 size_t hash_len;
90 psa_status_t status;
91
92 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
93 DICE_HASH_SIZE);
94 memcpy(&dest_ctx->data.measurement_descriptor,
95 dice_inputs->code_descriptor,
96 dice_inputs->code_descriptor_size);
97
98 dest_ctx->data.measurement_descriptor_size =
99 dice_inputs->code_descriptor_size;
100
101 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
102 memcpy(&dest_ctx->data.signer_id_descriptor,
103 dice_inputs->authority_descriptor,
104 dice_inputs->authority_descriptor_size);
105
106 dest_ctx->data.signer_id_descriptor_size =
107 dice_inputs->authority_descriptor_size;
108
109 if (dice_inputs->config_type == kDiceConfigTypeInline) {
110 /* Copy config_value */
111 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
112 DICE_INLINE_CONFIG_SIZE);
113
114 } else {
115 /* Copy config descriptor */
116 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
117 dice_inputs->config_descriptor_size);
118 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
119
120 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100121 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100122 dice_inputs->config_descriptor,
123 dice_inputs->config_descriptor_size,
124 dest_ctx->data.config_value,
125 sizeof(dest_ctx->data.config_value),
126 &hash_len);
127
128 if (status != PSA_SUCCESS) {
129 return DPE_INTERNAL_ERROR;
130 }
131 }
132
133 dest_ctx->data.mode = dice_inputs->mode;
134 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
135
136 return DPE_NO_ERROR;
137}
138
139static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
140{
141 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
142 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
143 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
144 return false;
145 }
146
147 return true;
148}
149
150static bool is_input_handle_valid(int input_context_handle)
151{
152 uint16_t idx = GET_IDX(input_context_handle);
153 uint16_t nonce = GET_NONCE(input_context_handle);
154
155 /* Validate input handle id and nonce */
156 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
157 return false;
158 }
159
160 if (nonce == component_ctx_array[idx].nonce) {
161 return true;
162 }
163
164 return false;
165}
166
Maulik Patel58595d32023-06-22 10:08:53 +0100167/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
168 * same order
169 */
170static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
171 size_t max_size,
172 size_t *dest_size,
173 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100174{
Maulik Patel58595d32023-06-22 10:08:53 +0100175 size_t out_size = 0;
176
177 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
178 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
179 return PSA_ERROR_BUFFER_TOO_SMALL;
180 }
181
182 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
183 out_size += DICE_HASH_SIZE;
184
185 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
186 out_size += DICE_INLINE_CONFIG_SIZE;
187
188 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
189 out_size += DICE_HASH_SIZE;
190
191 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
192 out_size += sizeof(comp_ctx->data.mode);
193
194 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
195 out_size += DICE_HIDDEN_SIZE;
196
197 *dest_size = out_size;
198
199 return PSA_SUCCESS;
200}
201
202static psa_status_t compute_layer_cdi_attest_input(uint16_t curr_layer_idx)
203{
204 psa_status_t status;
205 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
206 size_t ctx_data_size, hash_len;
207 int idx;
208
209 psa_hash_operation_t hash_op = psa_hash_operation_init();
210 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
211 if (status != PSA_SUCCESS) {
212 return status;
213 }
214
215 //TODO:
216 /* How to combine measurements of multiple SW components into a single hash
217 * is not yet defined by the Open DICE profile. This implementation
218 * concatenates the data of all SW components which belong to the same layer
219 * and hash it.
220 */
221 for (idx = 0; idx < MAX_NUM_OF_COMPONENTS; idx++) {
222 if (component_ctx_array[idx].linked_layer_idx == curr_layer_idx) {
223 /* This component belongs to current layer */
224 /* Concatenate all context data for this component */
225 status = get_component_data_for_attest_cdi(component_ctx_data,
226 sizeof(component_ctx_data),
227 &ctx_data_size,
228 &component_ctx_array[idx]);
229 if (status != PSA_SUCCESS) {
230 return status;
231 }
232
233 status = psa_hash_update(&hash_op,
234 component_ctx_data,
235 ctx_data_size);
236 if (status != PSA_SUCCESS) {
237 return status;
238 }
239 }
240 }
241
Maulik Patele6adc112023-08-18 14:21:51 +0100242 if (layer_ctx_array[curr_layer_idx].data.attest_key_label_len != 0) {
243
244 status = psa_hash_update(&hash_op,
245 &layer_ctx_array[curr_layer_idx].data.attest_key_label[0],
246 layer_ctx_array[curr_layer_idx].data.attest_key_label_len);
247 if (status != PSA_SUCCESS) {
248 return status;
249 }
250 }
251
Maulik Patel58595d32023-06-22 10:08:53 +0100252 status = psa_hash_finish(&hash_op,
253 &layer_ctx_array[curr_layer_idx].attest_cdi_hash_input[0],
254 sizeof(layer_ctx_array[curr_layer_idx].attest_cdi_hash_input),
255 &hash_len);
256
257 assert(hash_len == DPE_HASH_ALG_SIZE);
258
259 return status;
260}
261
Maulik Patele6adc112023-08-18 14:21:51 +0100262static dpe_error_t create_layer_certificate(uint16_t layer_idx)
Maulik Patel58595d32023-06-22 10:08:53 +0100263{
Maulik Patel2358bbb2023-07-21 10:56:56 +0100264 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100265 psa_status_t status;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100266 dpe_error_t err;
Maulik Patele6adc112023-08-18 14:21:51 +0100267 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100268
269 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100270 layer_ctx = &layer_ctx_array[layer_idx];
271 /* Finalise the layer */
272 layer_ctx->state = LAYER_STATE_FINALISED;
273 parent_layer_idx = layer_ctx->parent_layer_idx;
274 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
275 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100276
Maulik Patel2358bbb2023-07-21 10:56:56 +0100277 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patele6adc112023-08-18 14:21:51 +0100278 if ((layer_idx != DPE_ROT_LAYER_IDX) &&
279 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100280
Maulik Patele6adc112023-08-18 14:21:51 +0100281 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100282
283 status = compute_layer_cdi_attest_input(layer_idx);
284 if (status != PSA_SUCCESS) {
285 return DPE_INTERNAL_ERROR;
286 }
287
Maulik Patele6adc112023-08-18 14:21:51 +0100288 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100289 if (status != PSA_SUCCESS) {
290 return DPE_INTERNAL_ERROR;
291 }
292
Maulik Patele6adc112023-08-18 14:21:51 +0100293 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100294 if (status != PSA_SUCCESS) {
295 return DPE_INTERNAL_ERROR;
296 }
297 }
298
Maulik Patele6adc112023-08-18 14:21:51 +0100299 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100300 if (status != PSA_SUCCESS) {
301 return DPE_INTERNAL_ERROR;
302 }
303
Maulik Patele6adc112023-08-18 14:21:51 +0100304 if (!layer_ctx->is_external_pub_key_provided) {
305 status = derive_attestation_key(layer_ctx);
306 if (status != PSA_SUCCESS) {
307 return DPE_INTERNAL_ERROR;
308 }
Maulik Patel58595d32023-06-22 10:08:53 +0100309 }
310
Maulik Patele6adc112023-08-18 14:21:51 +0100311 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100312 if (status != PSA_SUCCESS) {
313 return DPE_INTERNAL_ERROR;
314 }
315
Maulik Patel2358bbb2023-07-21 10:56:56 +0100316 err = encode_layer_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100317 layer_ctx,
318 parent_layer_ctx);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100319 if (err != DPE_NO_ERROR) {
320 return err;
Maulik Patel58595d32023-06-22 10:08:53 +0100321 }
322
Maulik Patel2358bbb2023-07-21 10:56:56 +0100323 log_intermediate_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100324 &layer_ctx->data.cert_buf[0],
325 layer_ctx->data.cert_buf_len);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100326
Maulik Patele6adc112023-08-18 14:21:51 +0100327 return store_layer_certificate(layer_ctx);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100328}
329
330static uint16_t open_new_layer(void)
331{
332 int i;
333
334 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
335 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
336 layer_ctx_array[i].state = LAYER_STATE_OPEN;
337 return i;
338 }
339 }
340
Maulik Patel2358bbb2023-07-21 10:56:56 +0100341 //TODO: There is an open issue of layer creation as described below.
342 /* This is causing extra unintended layers to open. Since each layer
343 * has some context data and certificate buffer of 3k, it is
344 * causing RAM overflow. Hence until resoluton is reached, once all
345 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100346 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
347 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
348 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
349 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100350 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
351 */
352 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100353}
354
Maulik Patela81605b2023-10-24 12:17:03 +0100355static inline void link_layer(uint16_t derived_ctx_layer, uint16_t parent_ctx_layer)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100356{
Maulik Patela81605b2023-10-24 12:17:03 +0100357 layer_ctx_array[derived_ctx_layer].parent_layer_idx = parent_ctx_layer;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100358}
359
Maulik Patelad2f3db2023-05-17 15:41:36 +0100360static inline bool is_input_client_id_valid(int32_t client_id)
361{
362 //TODO: Waiting for implementation
363 return true;
364}
365
Maulik Patel2358bbb2023-07-21 10:56:56 +0100366static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100367{
368 uint16_t new_layer_idx, parent_layer_idx;
369
370 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
371
372 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
373 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
374
375 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
Maulik Patela81605b2023-10-24 12:17:03 +0100376 /* Parent comp's layer of new derived context is finalised; open a new layer */
Maulik Patelad2f3db2023-05-17 15:41:36 +0100377 new_layer_idx = open_new_layer();
Maulik Patel2358bbb2023-07-21 10:56:56 +0100378 if (new_layer_idx == INVALID_LAYER_IDX) {
379 return DPE_INTERNAL_ERROR;
380 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100381 /* Link this context to the new layer */
382 new_ctx->linked_layer_idx = new_layer_idx;
383 /* New layer's parent is current layer */
384 link_layer(new_layer_idx, parent_layer_idx);
385
386 } else {
387 /* Parent comp's layer is not yet finalised, link
388 * new component to the same layer as parent
389 */
390 new_ctx->linked_layer_idx = parent_layer_idx;
391 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100392
393 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100394}
395
Jamie Fox34681992023-09-04 18:14:06 +0100396/**
397 * \brief Create a root of trust component context.
398 *
399 * \param[out] rot_ctx_handle A new context handle for the RoT context.
400 *
401 * \return Returns error code of type dpe_error_t
402 */
403static dpe_error_t create_rot_context(int *rot_ctx_handle)
404{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100405#ifdef DPE_TEST_MODE
406 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
407#else
Jamie Fox34681992023-09-04 18:14:06 +0100408 int ret;
Jamie Fox34681992023-09-04 18:14:06 +0100409 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100410#endif /* DPE_TEST_MODE */
411 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100412 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
413 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
414
415 /* Open RoT layer */
416 rot_layer_ctx->state = LAYER_STATE_OPEN;
Maulik Patela81605b2023-10-24 12:17:03 +0100417 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100418 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
419
Maulik Pateldbfd5152023-05-30 17:02:42 +0100420#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100421 /* Get the RoT CDI input for the RoT layer */
422 ret = dpe_plat_get_rot_cdi(&rot_cdi_input[0],
423 sizeof(rot_cdi_input));
424 if (ret != 0) {
425 return DPE_INTERNAL_ERROR;
426 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100427#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100428
429 /* Import the CDI key for the RoT layer */
430 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
431 &rot_cdi_input[0],
432 sizeof(rot_cdi_input));
433 if (status != PSA_SUCCESS) {
434 return DPE_INTERNAL_ERROR;
435 }
436
Maulik Patela81605b2023-10-24 12:17:03 +0100437 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100438 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100439 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100440 rot_comp_ctx->parent_idx = 0;
441 /* Link context to RoT Layer */
442 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
443 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100444 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
445
446 return DPE_NO_ERROR;
447}
448
449dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
450{
451 int i;
452
453 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
454 set_context_to_default(i);
455 }
456
457 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
458 invalidate_layer(i);
459 }
460
461 return create_rot_context(rot_ctx_handle);
462}
463
Maulik Patela81605b2023-10-24 12:17:03 +0100464dpe_error_t derive_context_request(int input_ctx_handle,
465 bool retain_parent_context,
466 bool allow_new_context_to_derive,
467 bool create_certificate,
468 const DiceInputValues *dice_inputs,
469 int32_t client_id,
470 int *new_context_handle,
471 int *new_parent_context_handle)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100472{
Maulik Patel58595d32023-06-22 10:08:53 +0100473 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100474 struct component_context_t *parent_ctx, *derived_ctx;
475 uint16_t parent_ctx_idx;
476 int free_component_idx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100477
Maulik Patela81605b2023-10-24 12:17:03 +0100478 log_derive_context(input_ctx_handle, retain_parent_context,
479 allow_new_context_to_derive, create_certificate, dice_inputs,
480 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100481
Maulik Pateldbfd5152023-05-30 17:02:42 +0100482#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100483 if ((input_ctx_handle == 0) &&
484 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100485 /* Deriving RoT context for tests */
486 err = create_rot_context(&input_ctx_handle);
487 if (err != DPE_NO_ERROR) {
488 return err;
489 }
490 }
491#endif /* DPE_TEST_MODE */
492
Maulik Patelad2f3db2023-05-17 15:41:36 +0100493 /* Validate dice inputs */
494 if (!is_dice_input_valid(dice_inputs)) {
495 return DPE_INVALID_ARGUMENT;
496 }
497
498 /* Validate input handle */
499 if (!is_input_handle_valid(input_ctx_handle)) {
500 return DPE_INVALID_ARGUMENT;
501 }
Maulik Patela81605b2023-10-24 12:17:03 +0100502 /* Get parent component index from the input handle */
503 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100504
505 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100506 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100507 */
Maulik Patela81605b2023-10-24 12:17:03 +0100508 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100509
Maulik Patela81605b2023-10-24 12:17:03 +0100510 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100511
512 //TODO: Question: how to get mhu id of incoming request?
513 if (!is_input_client_id_valid(client_id)) {
514 return DPE_INVALID_ARGUMENT;
515 }
516
Maulik Patela81605b2023-10-24 12:17:03 +0100517 /* Get next free component index to add new derived context */
518 free_component_idx = get_free_component_context_index();
519 if (free_component_idx < 0) {
520 return DPE_INSUFFICIENT_MEMORY;
521 }
522
523 derived_ctx = &component_ctx_array[free_component_idx];
524 /* Copy dice input to the new derived component context */
525 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100526 if (err != DPE_NO_ERROR) {
527 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100528 }
529
Maulik Patela81605b2023-10-24 12:17:03 +0100530 /* Update parent idx in new derived component context */
531 derived_ctx->parent_idx = parent_ctx_idx;
532 /* Mark new derived component index as in use */
533 derived_ctx->in_use = true;
534 err = assign_layer_to_context(derived_ctx);
535 if (err != DPE_NO_ERROR) {
536 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100537 }
538
539 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100540 /* Retain and return parent handle with renewed nonce */
541 *new_parent_context_handle = input_ctx_handle;
542 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100543 if (err != DPE_NO_ERROR) {
544 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100545 }
Maulik Patela81605b2023-10-24 12:17:03 +0100546 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
547
Maulik Patelad2f3db2023-05-17 15:41:36 +0100548 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100549 /* Return invalid handle */
550 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100551 parent_ctx->nonce = INVALID_NONCE_VALUE;
552 }
553
Maulik Patela81605b2023-10-24 12:17:03 +0100554 if (allow_new_context_to_derive) {
555 /* Return handle to derived context */
556 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
557 err = renew_nonce(new_context_handle);
558 if (err != DPE_NO_ERROR) {
559 return err;
560 }
561 /* Update nonce in new derived component context */
562 derived_ctx->nonce = GET_NONCE(*new_context_handle);
563
564 } else {
565 /* Return invalid handle */
566 *new_context_handle = INVALID_HANDLE;
567 derived_ctx->nonce = INVALID_NONCE_VALUE;
568 }
569
570 if (create_certificate) {
571 err = create_layer_certificate(derived_ctx->linked_layer_idx);
572 if (err != DPE_NO_ERROR) {
573 return err;
574 }
575 }
576
Maulik Patelad2f3db2023-05-17 15:41:36 +0100577 return DPE_NO_ERROR;
578}
Maulik Patel54d65f72023-06-28 13:04:36 +0100579
580dpe_error_t destroy_context_request(int input_ctx_handle,
581 bool destroy_recursively)
582{
583 uint16_t input_ctx_idx, linked_layer_idx;
584 int i;
585 bool is_layer_empty;
586
587 log_destroy_context(input_ctx_handle, destroy_recursively);
588
Maulik Patela81605b2023-10-24 12:17:03 +0100589 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100590 input_ctx_idx = GET_IDX(input_ctx_handle);
591
Maulik Patel54d65f72023-06-28 13:04:36 +0100592 /* Validate input handle */
593 if (!is_input_handle_valid(input_ctx_handle)) {
594 return DPE_INVALID_ARGUMENT;
595 }
596 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
597
Jamie Fox34681992023-09-04 18:14:06 +0100598#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100599 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
600 /* All layers till hypervisor cannot be destroyed dynamically */
601 return DPE_INVALID_ARGUMENT;
602 }
Jamie Fox34681992023-09-04 18:14:06 +0100603#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100604
605
606 if (!destroy_recursively) {
607 set_context_to_default(input_ctx_idx);
608 } else {
609 //TODO: To be implemented
610 }
611
612 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
613
614 /* Close the layer if all of its contexts are destroyed */
615 is_layer_empty = true;
616 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
617 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
618 /* There are active component context in the layer */
619 is_layer_empty = false;
620 break;
621 }
622 }
623
624 if (is_layer_empty) {
625 invalidate_layer(linked_layer_idx);
626 }
627
628 return DPE_NO_ERROR;
629}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100630
631struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
632 uint16_t component_idx)
633{
634 /* Safety case */
635 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
636 return NULL;
637 }
638
639 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
640 return &component_ctx_array[component_idx];
641 } else {
642 return NULL;
643 }
644}
645
Maulik Patele6adc112023-08-18 14:21:51 +0100646struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
647{
648 /* Safety case */
649 if (layer_idx >= MAX_NUM_OF_LAYERS) {
650 return NULL;
651 }
652
653 return &layer_ctx_array[layer_idx];
654}
655
656dpe_error_t certify_key_request(int input_ctx_handle,
657 bool retain_context,
658 const uint8_t *public_key,
659 size_t public_key_size,
660 const uint8_t *label,
661 size_t label_size,
662 uint8_t *certificate_chain_buf,
663 size_t certificate_chain_buf_size,
664 size_t *certificate_chain_actual_size,
665 uint8_t *derived_public_key_buf,
666 size_t derived_public_key_buf_size,
667 size_t *derived_public_key_actual_size,
668 int *new_context_handle)
669{
670 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
671 dpe_error_t err;
672 psa_status_t status;
673 struct layer_context_t *parent_layer_ctx, *layer_ctx;
674
675 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
676 label, label_size);
677
678 /* Validate input handle */
679 if (!is_input_handle_valid(input_ctx_handle)) {
680 return DPE_INVALID_ARGUMENT;
681 }
682
683 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
684 return DPE_INVALID_ARGUMENT;
685 }
686
687 /* Get component index from the input handle */
688 input_ctx_idx = GET_IDX(input_ctx_handle);
689 /* Get current linked layer idx */
690 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
691 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
692
693 layer_ctx = &layer_ctx_array[input_layer_idx];
694 if (public_key_size > sizeof(layer_ctx->data.attest_pub_key)) {
695 return DPE_INVALID_ARGUMENT;
696 }
697
698 if ((public_key_size > 0) && (public_key != NULL)) {
699 layer_ctx->is_external_pub_key_provided = true;
700 /* Copy the public key provided */
701 memcpy(&layer_ctx->data.attest_pub_key[0],
702 public_key,
703 public_key_size);
704 layer_ctx->data.attest_pub_key_len = public_key_size;
705
706 /* If public key is provided, then provided label (if any) is ignored */
707 layer_ctx->data.attest_key_label_len = 0;
708
709 } else {
710 /* No external public key is provided */
711 layer_ctx->is_external_pub_key_provided = false;
712
713 if ((label_size > 0) && (label != NULL)) {
714 /* Copy the label provided */
715 memcpy(&layer_ctx->data.attest_key_label[0],
716 label,
717 label_size);
718 layer_ctx->data.attest_key_label_len = label_size;
719
720 } else {
721 layer_ctx->data.attest_key_label_len = 0;
722 }
723 }
724
725 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100726 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100727 */
728 /* Finalise the current layer & create leaf certificate */
729 err = create_layer_certificate(input_layer_idx);
730 if (err != DPE_NO_ERROR) {
731 return err;
732 }
733
734 /* Get parent layer derived public key to verify the certificate signature */
735 parent_layer_idx = layer_ctx_array[input_layer_idx].parent_layer_idx;
736 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
737 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
738
739 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
740 return DPE_INVALID_ARGUMENT;
741 }
742
743 memcpy(derived_public_key_buf,
744 &parent_layer_ctx->data.attest_pub_key[0],
745 parent_layer_ctx->data.attest_pub_key_len);
746 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
747
748 /* Get certificate chain */
749 err = get_certificate_chain(input_layer_idx,
750 certificate_chain_buf,
751 certificate_chain_buf_size,
752 certificate_chain_actual_size);
753 if (err != DPE_NO_ERROR) {
754 return err;
755 }
756
757 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
758
759 /* Renew handle for the same context */
760 *new_context_handle = input_ctx_handle;
761 status = renew_nonce(new_context_handle);
762 if (status != PSA_SUCCESS) {
763 return DPE_INTERNAL_ERROR;
764 }
765 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
766
767 /* Clear the context label and key contents */
768 memset(&layer_ctx->data.attest_key_label[0], 0u, layer_ctx->data.attest_key_label_len);
769 memset(&layer_ctx->data.attest_pub_key[0], 0u, layer_ctx->data.attest_pub_key_len);
770
771 return DPE_NO_ERROR;
772}