blob: e279b39bc21cd74a1256591a4610ad9ae0eecacc [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;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000066 component_ctx_array[i].is_allowed_to_derive = true;
67 /* export CDI attribute is inherited and once disabled, a derived context
68 * and subsequent derivations cannot export CDI, hence enable by default
69 */
70 component_ctx_array[i].is_export_cdi_allowed = true;
Maulik Patelad2f3db2023-05-17 15:41:36 +010071 component_ctx_array[i].nonce = INVALID_NONCE_VALUE;
72 component_ctx_array[i].parent_idx = INVALID_COMPONENT_IDX;
73 component_ctx_array[i].linked_layer_idx = INVALID_LAYER_IDX;
74 (void)memset(&component_ctx_array[i].data, 0, sizeof(struct component_context_data_t));
75 //TODO: Question: how to initialise MHU Id mapping?
Maulik Patel9fd8bd22023-10-30 10:58:30 +000076 component_ctx_array[i].target_locality = 0;
Maulik Patelad2f3db2023-05-17 15:41:36 +010077 /* Allow component to be derived by default */
78}
79
80static void invalidate_layer(int i)
81{
82 layer_ctx_array[i].state = LAYER_STATE_CLOSED;
83 layer_ctx_array[i].parent_layer_idx = INVALID_LAYER_IDX;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000084 layer_ctx_array[i].is_cdi_to_be_exported = false;
Maulik Patele6adc112023-08-18 14:21:51 +010085 (void)memset(&layer_ctx_array[i].attest_cdi_hash_input, 0,
86 sizeof(layer_ctx_array[i].attest_cdi_hash_input));
Maulik Patel58595d32023-06-22 10:08:53 +010087 (void)psa_destroy_key(layer_ctx_array[i].data.cdi_key_id);
88 (void)psa_destroy_key(layer_ctx_array[i].data.attest_key_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +010089 (void)memset(&layer_ctx_array[i].data, 0, sizeof(struct layer_context_data_t));
90}
91
Maulik Patelad2f3db2023-05-17 15:41:36 +010092static dpe_error_t copy_dice_input(struct component_context_t *dest_ctx,
93 const DiceInputValues *dice_inputs)
94{
95 size_t hash_len;
96 psa_status_t status;
97
98 memcpy(&dest_ctx->data.measurement_value, dice_inputs->code_hash,
99 DICE_HASH_SIZE);
100 memcpy(&dest_ctx->data.measurement_descriptor,
101 dice_inputs->code_descriptor,
102 dice_inputs->code_descriptor_size);
103
104 dest_ctx->data.measurement_descriptor_size =
105 dice_inputs->code_descriptor_size;
106
107 memcpy(&dest_ctx->data.signer_id, dice_inputs->authority_hash, DICE_HASH_SIZE);
108 memcpy(&dest_ctx->data.signer_id_descriptor,
109 dice_inputs->authority_descriptor,
110 dice_inputs->authority_descriptor_size);
111
112 dest_ctx->data.signer_id_descriptor_size =
113 dice_inputs->authority_descriptor_size;
114
115 if (dice_inputs->config_type == kDiceConfigTypeInline) {
116 /* Copy config_value */
117 memcpy(&dest_ctx->data.config_value, dice_inputs->config_value,
118 DICE_INLINE_CONFIG_SIZE);
119
120 } else {
121 /* Copy config descriptor */
122 memcpy(&dest_ctx->data.config_descriptor, dice_inputs->config_descriptor,
123 dice_inputs->config_descriptor_size);
124 dest_ctx->data.config_descriptor_size = dice_inputs->config_descriptor_size;
125
126 /* Calculate config value as hash of input config descriptor */
Maulik Patel2358bbb2023-07-21 10:56:56 +0100127 status = psa_hash_compute(DPE_HASH_ALG,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100128 dice_inputs->config_descriptor,
129 dice_inputs->config_descriptor_size,
130 dest_ctx->data.config_value,
131 sizeof(dest_ctx->data.config_value),
132 &hash_len);
133
134 if (status != PSA_SUCCESS) {
135 return DPE_INTERNAL_ERROR;
136 }
137 }
138
139 dest_ctx->data.mode = dice_inputs->mode;
140 memcpy(&dest_ctx->data.hidden, dice_inputs->hidden, DICE_HIDDEN_SIZE);
141
142 return DPE_NO_ERROR;
143}
144
145static bool is_dice_input_valid(const DiceInputValues *dice_inputs)
146{
147 if ((dice_inputs->code_descriptor_size > DICE_CODE_DESCRIPTOR_MAX_SIZE) ||
148 (dice_inputs->authority_descriptor_size > DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE) ||
149 (dice_inputs->config_descriptor_size > DICE_CONFIG_DESCRIPTOR_MAX_SIZE)) {
150 return false;
151 }
152
153 return true;
154}
155
156static bool is_input_handle_valid(int input_context_handle)
157{
158 uint16_t idx = GET_IDX(input_context_handle);
159 uint16_t nonce = GET_NONCE(input_context_handle);
160
161 /* Validate input handle id and nonce */
162 if ((idx >= MAX_NUM_OF_COMPONENTS) || (nonce == INVALID_NONCE_VALUE)) {
163 return false;
164 }
165
166 if (nonce == component_ctx_array[idx].nonce) {
167 return true;
168 }
169
170 return false;
171}
172
Maulik Patel58595d32023-06-22 10:08:53 +0100173/* Attest_CDI Input requires {measurement_value, config, authority, mode, hidden} in
174 * same order
175 */
176static psa_status_t get_component_data_for_attest_cdi(uint8_t *dest_buf,
177 size_t max_size,
178 size_t *dest_size,
179 const struct component_context_t *comp_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100180{
Maulik Patel58595d32023-06-22 10:08:53 +0100181 size_t out_size = 0;
182
183 if ((DICE_HASH_SIZE + DICE_INLINE_CONFIG_SIZE + DICE_HASH_SIZE +
184 sizeof(comp_ctx->data.mode) + DICE_HIDDEN_SIZE > max_size )) {
185 return PSA_ERROR_BUFFER_TOO_SMALL;
186 }
187
188 memcpy(&dest_buf[out_size], comp_ctx->data.measurement_value, DICE_HASH_SIZE);
189 out_size += DICE_HASH_SIZE;
190
191 memcpy(&dest_buf[out_size], comp_ctx->data.config_value, DICE_INLINE_CONFIG_SIZE);
192 out_size += DICE_INLINE_CONFIG_SIZE;
193
194 memcpy(&dest_buf[out_size], comp_ctx->data.signer_id, DICE_HASH_SIZE);
195 out_size += DICE_HASH_SIZE;
196
197 memcpy(&dest_buf[out_size], &comp_ctx->data.mode, sizeof(comp_ctx->data.mode));
198 out_size += sizeof(comp_ctx->data.mode);
199
200 memcpy(&dest_buf[out_size], comp_ctx->data.hidden, DICE_HIDDEN_SIZE);
201 out_size += DICE_HIDDEN_SIZE;
202
203 *dest_size = out_size;
204
205 return PSA_SUCCESS;
206}
207
208static psa_status_t compute_layer_cdi_attest_input(uint16_t curr_layer_idx)
209{
210 psa_status_t status;
211 uint8_t component_ctx_data[CONTEXT_DATA_MAX_SIZE];
212 size_t ctx_data_size, hash_len;
213 int idx;
214
215 psa_hash_operation_t hash_op = psa_hash_operation_init();
216 status = psa_hash_setup(&hash_op, DPE_HASH_ALG);
217 if (status != PSA_SUCCESS) {
218 return status;
219 }
220
221 //TODO:
222 /* How to combine measurements of multiple SW components into a single hash
223 * is not yet defined by the Open DICE profile. This implementation
224 * concatenates the data of all SW components which belong to the same layer
225 * and hash it.
226 */
227 for (idx = 0; idx < MAX_NUM_OF_COMPONENTS; idx++) {
228 if (component_ctx_array[idx].linked_layer_idx == curr_layer_idx) {
229 /* This component belongs to current layer */
230 /* Concatenate all context data for this component */
231 status = get_component_data_for_attest_cdi(component_ctx_data,
232 sizeof(component_ctx_data),
233 &ctx_data_size,
234 &component_ctx_array[idx]);
235 if (status != PSA_SUCCESS) {
236 return status;
237 }
238
239 status = psa_hash_update(&hash_op,
240 component_ctx_data,
241 ctx_data_size);
242 if (status != PSA_SUCCESS) {
243 return status;
244 }
245 }
246 }
247
Maulik Patele6adc112023-08-18 14:21:51 +0100248 if (layer_ctx_array[curr_layer_idx].data.attest_key_label_len != 0) {
249
250 status = psa_hash_update(&hash_op,
251 &layer_ctx_array[curr_layer_idx].data.attest_key_label[0],
252 layer_ctx_array[curr_layer_idx].data.attest_key_label_len);
253 if (status != PSA_SUCCESS) {
254 return status;
255 }
256 }
257
Maulik Patel58595d32023-06-22 10:08:53 +0100258 status = psa_hash_finish(&hash_op,
259 &layer_ctx_array[curr_layer_idx].attest_cdi_hash_input[0],
260 sizeof(layer_ctx_array[curr_layer_idx].attest_cdi_hash_input),
261 &hash_len);
262
263 assert(hash_len == DPE_HASH_ALG_SIZE);
264
265 return status;
266}
267
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000268static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
269 uint8_t *exported_cdi_buf,
270 size_t exported_cdi_buf_size,
271 size_t *exported_cdi_actual_size)
272{
273 uint8_t cdi_buf[DICE_CDI_SIZE];
274 size_t cdi_size;
275 psa_status_t status;
276 dpe_error_t err;
277
278 /* Get CDI value */
279 status = get_layer_cdi_value(layer_ctx,
280 cdi_buf,
281 sizeof(cdi_buf),
282 &cdi_size);
283 if (status != PSA_SUCCESS) {
284 return DPE_INTERNAL_ERROR;
285 }
286
287 /* Encode CDI value */
288 err = encode_cdi(cdi_buf,
289 cdi_size,
290 exported_cdi_buf,
291 exported_cdi_buf_size,
292 exported_cdi_actual_size);
293 if (err != DPE_NO_ERROR) {
294 return err;
295 }
296 layer_ctx->is_cdi_to_be_exported = true;
297
298 return DPE_NO_ERROR;
299}
300
Maulik Patele6adc112023-08-18 14:21:51 +0100301static dpe_error_t create_layer_certificate(uint16_t layer_idx)
Maulik Patel58595d32023-06-22 10:08:53 +0100302{
Maulik Patel2358bbb2023-07-21 10:56:56 +0100303 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100304 psa_status_t status;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100305 dpe_error_t err;
Maulik Patele6adc112023-08-18 14:21:51 +0100306 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100307
308 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100309 layer_ctx = &layer_ctx_array[layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100310 parent_layer_idx = layer_ctx->parent_layer_idx;
311 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
312 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100313
Maulik Patel2358bbb2023-07-21 10:56:56 +0100314 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patele6adc112023-08-18 14:21:51 +0100315 if ((layer_idx != DPE_ROT_LAYER_IDX) &&
316 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100317
Maulik Patele6adc112023-08-18 14:21:51 +0100318 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100319
320 status = compute_layer_cdi_attest_input(layer_idx);
321 if (status != PSA_SUCCESS) {
322 return DPE_INTERNAL_ERROR;
323 }
324
Maulik Patele6adc112023-08-18 14:21:51 +0100325 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100326 if (status != PSA_SUCCESS) {
327 return DPE_INTERNAL_ERROR;
328 }
329
Maulik Patele6adc112023-08-18 14:21:51 +0100330 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100331 if (status != PSA_SUCCESS) {
332 return DPE_INTERNAL_ERROR;
333 }
334 }
335
Maulik Patele6adc112023-08-18 14:21:51 +0100336 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100337 if (status != PSA_SUCCESS) {
338 return DPE_INTERNAL_ERROR;
339 }
340
Maulik Patele6adc112023-08-18 14:21:51 +0100341 if (!layer_ctx->is_external_pub_key_provided) {
342 status = derive_attestation_key(layer_ctx);
343 if (status != PSA_SUCCESS) {
344 return DPE_INTERNAL_ERROR;
345 }
Maulik Patel58595d32023-06-22 10:08:53 +0100346 }
347
Maulik Patele6adc112023-08-18 14:21:51 +0100348 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100349 if (status != PSA_SUCCESS) {
350 return DPE_INTERNAL_ERROR;
351 }
352
Maulik Patel2358bbb2023-07-21 10:56:56 +0100353 err = encode_layer_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100354 layer_ctx,
355 parent_layer_ctx);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100356 if (err != DPE_NO_ERROR) {
357 return err;
Maulik Patel58595d32023-06-22 10:08:53 +0100358 }
359
Maulik Patel2358bbb2023-07-21 10:56:56 +0100360 log_intermediate_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100361 &layer_ctx->data.cert_buf[0],
362 layer_ctx->data.cert_buf_len);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100363
Maulik Patele6adc112023-08-18 14:21:51 +0100364 return store_layer_certificate(layer_ctx);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100365}
366
367static uint16_t open_new_layer(void)
368{
369 int i;
370
371 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
372 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
373 layer_ctx_array[i].state = LAYER_STATE_OPEN;
374 return i;
375 }
376 }
377
Maulik Patel2358bbb2023-07-21 10:56:56 +0100378 //TODO: There is an open issue of layer creation as described below.
379 /* This is causing extra unintended layers to open. Since each layer
380 * has some context data and certificate buffer of 3k, it is
381 * causing RAM overflow. Hence until resoluton is reached, once all
382 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100383 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
384 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
385 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
386 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100387 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
388 */
389 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100390}
391
Maulik Patela81605b2023-10-24 12:17:03 +0100392static inline void link_layer(uint16_t derived_ctx_layer, uint16_t parent_ctx_layer)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100393{
Maulik Patela81605b2023-10-24 12:17:03 +0100394 layer_ctx_array[derived_ctx_layer].parent_layer_idx = parent_ctx_layer;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100395}
396
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000397static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100398{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000399 // TODO: FIXME
400 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100401 return true;
402}
403
Maulik Patel2358bbb2023-07-21 10:56:56 +0100404static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100405{
406 uint16_t new_layer_idx, parent_layer_idx;
407
408 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
409
410 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
411 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
412
413 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
Maulik Patela81605b2023-10-24 12:17:03 +0100414 /* Parent comp's layer of new derived context is finalised; open a new layer */
Maulik Patelad2f3db2023-05-17 15:41:36 +0100415 new_layer_idx = open_new_layer();
Maulik Patel2358bbb2023-07-21 10:56:56 +0100416 if (new_layer_idx == INVALID_LAYER_IDX) {
417 return DPE_INTERNAL_ERROR;
418 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100419 /* Link this context to the new layer */
420 new_ctx->linked_layer_idx = new_layer_idx;
421 /* New layer's parent is current layer */
422 link_layer(new_layer_idx, parent_layer_idx);
423
424 } else {
425 /* Parent comp's layer is not yet finalised, link
426 * new component to the same layer as parent
427 */
428 new_ctx->linked_layer_idx = parent_layer_idx;
429 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100430
431 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100432}
433
Jamie Fox34681992023-09-04 18:14:06 +0100434/**
435 * \brief Create a root of trust component context.
436 *
437 * \param[out] rot_ctx_handle A new context handle for the RoT context.
438 *
439 * \return Returns error code of type dpe_error_t
440 */
441static dpe_error_t create_rot_context(int *rot_ctx_handle)
442{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100443#ifdef DPE_TEST_MODE
444 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
445#else
Jamie Fox34681992023-09-04 18:14:06 +0100446 int ret;
Jamie Fox34681992023-09-04 18:14:06 +0100447 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100448#endif /* DPE_TEST_MODE */
449 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100450 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
451 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
452
453 /* Open RoT layer */
454 rot_layer_ctx->state = LAYER_STATE_OPEN;
Maulik Patela81605b2023-10-24 12:17:03 +0100455 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100456 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
457
Maulik Pateldbfd5152023-05-30 17:02:42 +0100458#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100459 /* Get the RoT CDI input for the RoT layer */
460 ret = dpe_plat_get_rot_cdi(&rot_cdi_input[0],
461 sizeof(rot_cdi_input));
462 if (ret != 0) {
463 return DPE_INTERNAL_ERROR;
464 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100465#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100466
467 /* Import the CDI key for the RoT layer */
468 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
469 &rot_cdi_input[0],
470 sizeof(rot_cdi_input));
471 if (status != PSA_SUCCESS) {
472 return DPE_INTERNAL_ERROR;
473 }
474
Maulik Patela81605b2023-10-24 12:17:03 +0100475 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100476 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100477 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100478 rot_comp_ctx->parent_idx = 0;
479 /* Link context to RoT Layer */
480 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
481 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100482 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
483
484 return DPE_NO_ERROR;
485}
486
487dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
488{
489 int i;
490
491 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
492 set_context_to_default(i);
493 }
494
495 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
496 invalidate_layer(i);
497 }
498
499 return create_rot_context(rot_ctx_handle);
500}
501
Maulik Patela81605b2023-10-24 12:17:03 +0100502dpe_error_t derive_context_request(int input_ctx_handle,
503 bool retain_parent_context,
504 bool allow_new_context_to_derive,
505 bool create_certificate,
506 const DiceInputValues *dice_inputs,
507 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000508 int32_t target_locality,
509 bool return_certificate,
510 bool allow_new_context_to_export,
511 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100512 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000513 int *new_parent_context_handle,
514 uint8_t *new_certificate_buf,
515 size_t new_certificate_buf_size,
516 size_t *new_certificate_actual_size,
517 uint8_t *exported_cdi_buf,
518 size_t exported_cdi_buf_size,
519 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100520{
Maulik Patel58595d32023-06-22 10:08:53 +0100521 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100522 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000523 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100524 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000525 struct layer_context_t *layer_ctx;
526 psa_status_t status;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100527
Maulik Patela81605b2023-10-24 12:17:03 +0100528 log_derive_context(input_ctx_handle, retain_parent_context,
529 allow_new_context_to_derive, create_certificate, dice_inputs,
530 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100531
Maulik Pateldbfd5152023-05-30 17:02:42 +0100532#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100533 if ((input_ctx_handle == 0) &&
534 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100535 /* Deriving RoT context for tests */
536 err = create_rot_context(&input_ctx_handle);
537 if (err != DPE_NO_ERROR) {
538 return err;
539 }
540 }
541#endif /* DPE_TEST_MODE */
542
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000543 if (export_cdi && !create_certificate) {
544 return DPE_INVALID_ARGUMENT;
545 }
546
Maulik Patelad2f3db2023-05-17 15:41:36 +0100547 /* Validate dice inputs */
548 if (!is_dice_input_valid(dice_inputs)) {
549 return DPE_INVALID_ARGUMENT;
550 }
551
552 /* Validate input handle */
553 if (!is_input_handle_valid(input_ctx_handle)) {
554 return DPE_INVALID_ARGUMENT;
555 }
Maulik Patela81605b2023-10-24 12:17:03 +0100556 /* Get parent component index from the input handle */
557 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100558
559 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100560 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100561 */
Maulik Patela81605b2023-10-24 12:17:03 +0100562 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100563
Maulik Patela81605b2023-10-24 12:17:03 +0100564 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100565
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000566 /* Check if parent context is allowed to derive */
567 if (!parent_ctx->is_allowed_to_derive) {
568 return DPE_INVALID_ARGUMENT;
569 }
570
Maulik Patelad2f3db2023-05-17 15:41:36 +0100571 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000572 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100573 return DPE_INVALID_ARGUMENT;
574 }
575
Maulik Patela81605b2023-10-24 12:17:03 +0100576 /* Get next free component index to add new derived context */
577 free_component_idx = get_free_component_context_index();
578 if (free_component_idx < 0) {
579 return DPE_INSUFFICIENT_MEMORY;
580 }
581
582 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000583 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
584 /* If parent context has export enabled and input allow_new_context_to_export
585 * is true, then allow context CDI to be exported for derived context
586 */
587 derived_ctx->is_export_cdi_allowed = true;
588 } else {
589 /* Export of new context CDI is NOT allowed */
590 derived_ctx->is_export_cdi_allowed = false;
591 if (export_cdi) {
592 return DPE_INVALID_ARGUMENT;
593 }
594 }
595
Maulik Patela81605b2023-10-24 12:17:03 +0100596 /* Copy dice input to the new derived component context */
597 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100598 if (err != DPE_NO_ERROR) {
599 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100600 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000601 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100602
Maulik Patela81605b2023-10-24 12:17:03 +0100603 /* Update parent idx in new derived component context */
604 derived_ctx->parent_idx = parent_ctx_idx;
605 /* Mark new derived component index as in use */
606 derived_ctx->in_use = true;
607 err = assign_layer_to_context(derived_ctx);
608 if (err != DPE_NO_ERROR) {
609 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100610 }
611
612 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100613 /* Retain and return parent handle with renewed nonce */
614 *new_parent_context_handle = input_ctx_handle;
615 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100616 if (err != DPE_NO_ERROR) {
617 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100618 }
Maulik Patela81605b2023-10-24 12:17:03 +0100619 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
620
Maulik Patelad2f3db2023-05-17 15:41:36 +0100621 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100622 /* Return invalid handle */
623 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100624 parent_ctx->nonce = INVALID_NONCE_VALUE;
625 }
626
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000627 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100628 /* Return handle to derived context */
629 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
630 err = renew_nonce(new_context_handle);
631 if (err != DPE_NO_ERROR) {
632 return err;
633 }
634 /* Update nonce in new derived component context */
635 derived_ctx->nonce = GET_NONCE(*new_context_handle);
636
637 } else {
638 /* Return invalid handle */
639 *new_context_handle = INVALID_HANDLE;
640 derived_ctx->nonce = INVALID_NONCE_VALUE;
641 }
642
643 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000644 linked_layer_idx = derived_ctx->linked_layer_idx;
645 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
646 layer_ctx = &layer_ctx_array[linked_layer_idx];
647 layer_ctx->is_cdi_to_be_exported = export_cdi;
648
Maulik Patelcbded682023-12-07 11:50:16 +0000649 /* Finalise the layer */
650 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000651 err = create_layer_certificate(linked_layer_idx);
652 if (err != DPE_NO_ERROR) {
653 return err;
654 }
655
656 if (return_certificate) {
657 if (new_certificate_buf_size < layer_ctx->data.cert_buf_len) {
658 return DPE_INVALID_ARGUMENT;
659 }
660
661 /* Encode and return generated layer certificate */
662 err = add_encoded_layer_certificate(layer_ctx->data.cert_buf,
663 layer_ctx->data.cert_buf_len,
664 new_certificate_buf,
665 new_certificate_buf_size,
666 new_certificate_actual_size);
667 if (err != DPE_NO_ERROR) {
668 return err;
669 }
670 }
671 }
672
673 if (export_cdi) {
674 err = get_encoded_cdi_to_export(layer_ctx,
675 exported_cdi_buf,
676 exported_cdi_buf_size,
677 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100678 if (err != DPE_NO_ERROR) {
679 return err;
680 }
681 }
682
Maulik Patelad2f3db2023-05-17 15:41:36 +0100683 return DPE_NO_ERROR;
684}
Maulik Patel54d65f72023-06-28 13:04:36 +0100685
686dpe_error_t destroy_context_request(int input_ctx_handle,
687 bool destroy_recursively)
688{
689 uint16_t input_ctx_idx, linked_layer_idx;
690 int i;
691 bool is_layer_empty;
692
693 log_destroy_context(input_ctx_handle, destroy_recursively);
694
Maulik Patela81605b2023-10-24 12:17:03 +0100695 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100696 input_ctx_idx = GET_IDX(input_ctx_handle);
697
Maulik Patel54d65f72023-06-28 13:04:36 +0100698 /* Validate input handle */
699 if (!is_input_handle_valid(input_ctx_handle)) {
700 return DPE_INVALID_ARGUMENT;
701 }
702 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
703
Jamie Fox34681992023-09-04 18:14:06 +0100704#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100705 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
706 /* All layers till hypervisor cannot be destroyed dynamically */
707 return DPE_INVALID_ARGUMENT;
708 }
Jamie Fox34681992023-09-04 18:14:06 +0100709#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100710
711
712 if (!destroy_recursively) {
713 set_context_to_default(input_ctx_idx);
714 } else {
715 //TODO: To be implemented
716 }
717
718 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
719
720 /* Close the layer if all of its contexts are destroyed */
721 is_layer_empty = true;
722 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
723 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
724 /* There are active component context in the layer */
725 is_layer_empty = false;
726 break;
727 }
728 }
729
730 if (is_layer_empty) {
731 invalidate_layer(linked_layer_idx);
732 }
733
734 return DPE_NO_ERROR;
735}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100736
737struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
738 uint16_t component_idx)
739{
740 /* Safety case */
741 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
742 return NULL;
743 }
744
745 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
746 return &component_ctx_array[component_idx];
747 } else {
748 return NULL;
749 }
750}
751
Maulik Patele6adc112023-08-18 14:21:51 +0100752struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
753{
754 /* Safety case */
755 if (layer_idx >= MAX_NUM_OF_LAYERS) {
756 return NULL;
757 }
758
759 return &layer_ctx_array[layer_idx];
760}
761
762dpe_error_t certify_key_request(int input_ctx_handle,
763 bool retain_context,
764 const uint8_t *public_key,
765 size_t public_key_size,
766 const uint8_t *label,
767 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000768 uint8_t *certificate_buf,
769 size_t certificate_buf_size,
770 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100771 uint8_t *derived_public_key_buf,
772 size_t derived_public_key_buf_size,
773 size_t *derived_public_key_actual_size,
774 int *new_context_handle)
775{
776 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
777 dpe_error_t err;
778 psa_status_t status;
779 struct layer_context_t *parent_layer_ctx, *layer_ctx;
780
781 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
782 label, label_size);
783
784 /* Validate input handle */
785 if (!is_input_handle_valid(input_ctx_handle)) {
786 return DPE_INVALID_ARGUMENT;
787 }
788
789 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
790 return DPE_INVALID_ARGUMENT;
791 }
792
793 /* Get component index from the input handle */
794 input_ctx_idx = GET_IDX(input_ctx_handle);
795 /* Get current linked layer idx */
796 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
797 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
798
799 layer_ctx = &layer_ctx_array[input_layer_idx];
800 if (public_key_size > sizeof(layer_ctx->data.attest_pub_key)) {
801 return DPE_INVALID_ARGUMENT;
802 }
803
804 if ((public_key_size > 0) && (public_key != NULL)) {
805 layer_ctx->is_external_pub_key_provided = true;
806 /* Copy the public key provided */
807 memcpy(&layer_ctx->data.attest_pub_key[0],
808 public_key,
809 public_key_size);
810 layer_ctx->data.attest_pub_key_len = public_key_size;
811
812 /* If public key is provided, then provided label (if any) is ignored */
813 layer_ctx->data.attest_key_label_len = 0;
814
815 } else {
816 /* No external public key is provided */
817 layer_ctx->is_external_pub_key_provided = false;
818
819 if ((label_size > 0) && (label != NULL)) {
820 /* Copy the label provided */
821 memcpy(&layer_ctx->data.attest_key_label[0],
822 label,
823 label_size);
824 layer_ctx->data.attest_key_label_len = label_size;
825
826 } else {
827 layer_ctx->data.attest_key_label_len = 0;
828 }
829 }
830
831 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100832 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100833 */
Maulik Patelcbded682023-12-07 11:50:16 +0000834 /* Create leaf certificate */
Maulik Patele6adc112023-08-18 14:21:51 +0100835 err = create_layer_certificate(input_layer_idx);
836 if (err != DPE_NO_ERROR) {
837 return err;
838 }
839
840 /* Get parent layer derived public key to verify the certificate signature */
841 parent_layer_idx = layer_ctx_array[input_layer_idx].parent_layer_idx;
842 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
843 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
844
845 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
846 return DPE_INVALID_ARGUMENT;
847 }
848
849 memcpy(derived_public_key_buf,
850 &parent_layer_ctx->data.attest_pub_key[0],
851 parent_layer_ctx->data.attest_pub_key_len);
852 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
853
Maulik Patelcbded682023-12-07 11:50:16 +0000854 /* Get certificate */
855 if (certificate_buf_size < layer_ctx->data.cert_buf_len) {
856 return DPE_INVALID_ARGUMENT;
Maulik Patele6adc112023-08-18 14:21:51 +0100857 }
Maulik Patelcbded682023-12-07 11:50:16 +0000858 memcpy(certificate_buf,
859 &layer_ctx->data.cert_buf[0],
860 layer_ctx->data.cert_buf_len);
861 *certificate_actual_size = layer_ctx->data.cert_buf_len;
Maulik Patele6adc112023-08-18 14:21:51 +0100862
863 /* Renew handle for the same context */
864 *new_context_handle = input_ctx_handle;
865 status = renew_nonce(new_context_handle);
866 if (status != PSA_SUCCESS) {
867 return DPE_INTERNAL_ERROR;
868 }
869 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
870
871 /* Clear the context label and key contents */
872 memset(&layer_ctx->data.attest_key_label[0], 0u, layer_ctx->data.attest_key_label_len);
873 memset(&layer_ctx->data.attest_pub_key[0], 0u, layer_ctx->data.attest_pub_key_len);
874
875 return DPE_NO_ERROR;
876}
Maulik Patel83a6b592023-12-05 15:20:30 +0000877
878dpe_error_t get_certificate_chain_request(int input_ctx_handle,
879 bool retain_context,
880 bool clear_from_context,
881 uint8_t *certificate_chain_buf,
882 size_t certificate_chain_buf_size,
883 size_t *certificate_chain_actual_size,
884 int *new_context_handle)
885{
886 dpe_error_t err;
887 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
888 psa_status_t status;
889 struct layer_context_t *layer_ctx;
890
891 log_get_certificate_chain(input_ctx_handle, retain_context, clear_from_context);
892
893 /* Validate input handle */
894 if (!is_input_handle_valid(input_ctx_handle)) {
895 return DPE_INVALID_ARGUMENT;
896 }
897
898 /* Get component index from the input handle */
899 input_ctx_idx = GET_IDX(input_ctx_handle);
900 /* Get current linked layer idx */
901 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
902 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
903
904 layer_ctx = &layer_ctx_array[input_layer_idx];
905 err = get_certificate_chain(input_layer_idx,
906 certificate_chain_buf,
907 certificate_chain_buf_size,
908 certificate_chain_actual_size);
909 if (err != DPE_NO_ERROR) {
910 return err;
911 }
912
913 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
914
915 /* Renew handle for the same context, if requested */
916 if (retain_context) {
917 *new_context_handle = input_ctx_handle;
918 status = renew_nonce(new_context_handle);
919 if (status != PSA_SUCCESS) {
920 return DPE_INTERNAL_ERROR;
921 }
922 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
923
924 if (clear_from_context) {
925 /* Clear all the accumulated certificate information so far */
926 clear_certificate_chain(input_layer_idx, layer_ctx);
927 }
928
929 } else {
930 *new_context_handle = INVALID_HANDLE;
931 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
932 }
933
934 return DPE_NO_ERROR;
935}