blob: fc542a08dfb8e671910c76222cac066e9a4dae82 [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];
310 /* Finalise the layer */
311 layer_ctx->state = LAYER_STATE_FINALISED;
312 parent_layer_idx = layer_ctx->parent_layer_idx;
313 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
314 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100315
Maulik Patel2358bbb2023-07-21 10:56:56 +0100316 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patele6adc112023-08-18 14:21:51 +0100317 if ((layer_idx != DPE_ROT_LAYER_IDX) &&
318 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100319
Maulik Patele6adc112023-08-18 14:21:51 +0100320 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100321
322 status = compute_layer_cdi_attest_input(layer_idx);
323 if (status != PSA_SUCCESS) {
324 return DPE_INTERNAL_ERROR;
325 }
326
Maulik Patele6adc112023-08-18 14:21:51 +0100327 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100328 if (status != PSA_SUCCESS) {
329 return DPE_INTERNAL_ERROR;
330 }
331
Maulik Patele6adc112023-08-18 14:21:51 +0100332 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100333 if (status != PSA_SUCCESS) {
334 return DPE_INTERNAL_ERROR;
335 }
336 }
337
Maulik Patele6adc112023-08-18 14:21:51 +0100338 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100339 if (status != PSA_SUCCESS) {
340 return DPE_INTERNAL_ERROR;
341 }
342
Maulik Patele6adc112023-08-18 14:21:51 +0100343 if (!layer_ctx->is_external_pub_key_provided) {
344 status = derive_attestation_key(layer_ctx);
345 if (status != PSA_SUCCESS) {
346 return DPE_INTERNAL_ERROR;
347 }
Maulik Patel58595d32023-06-22 10:08:53 +0100348 }
349
Maulik Patele6adc112023-08-18 14:21:51 +0100350 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100351 if (status != PSA_SUCCESS) {
352 return DPE_INTERNAL_ERROR;
353 }
354
Maulik Patel2358bbb2023-07-21 10:56:56 +0100355 err = encode_layer_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100356 layer_ctx,
357 parent_layer_ctx);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100358 if (err != DPE_NO_ERROR) {
359 return err;
Maulik Patel58595d32023-06-22 10:08:53 +0100360 }
361
Maulik Patel2358bbb2023-07-21 10:56:56 +0100362 log_intermediate_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100363 &layer_ctx->data.cert_buf[0],
364 layer_ctx->data.cert_buf_len);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100365
Maulik Patele6adc112023-08-18 14:21:51 +0100366 return store_layer_certificate(layer_ctx);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100367}
368
369static uint16_t open_new_layer(void)
370{
371 int i;
372
373 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
374 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
375 layer_ctx_array[i].state = LAYER_STATE_OPEN;
376 return i;
377 }
378 }
379
Maulik Patel2358bbb2023-07-21 10:56:56 +0100380 //TODO: There is an open issue of layer creation as described below.
381 /* This is causing extra unintended layers to open. Since each layer
382 * has some context data and certificate buffer of 3k, it is
383 * causing RAM overflow. Hence until resoluton is reached, once all
384 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100385 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
386 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
387 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
388 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100389 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
390 */
391 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100392}
393
Maulik Patela81605b2023-10-24 12:17:03 +0100394static inline void link_layer(uint16_t derived_ctx_layer, uint16_t parent_ctx_layer)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100395{
Maulik Patela81605b2023-10-24 12:17:03 +0100396 layer_ctx_array[derived_ctx_layer].parent_layer_idx = parent_ctx_layer;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100397}
398
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000399static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100400{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000401 // TODO: FIXME
402 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100403 return true;
404}
405
Maulik Patel2358bbb2023-07-21 10:56:56 +0100406static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100407{
408 uint16_t new_layer_idx, parent_layer_idx;
409
410 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
411
412 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
413 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
414
415 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
Maulik Patela81605b2023-10-24 12:17:03 +0100416 /* Parent comp's layer of new derived context is finalised; open a new layer */
Maulik Patelad2f3db2023-05-17 15:41:36 +0100417 new_layer_idx = open_new_layer();
Maulik Patel2358bbb2023-07-21 10:56:56 +0100418 if (new_layer_idx == INVALID_LAYER_IDX) {
419 return DPE_INTERNAL_ERROR;
420 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100421 /* Link this context to the new layer */
422 new_ctx->linked_layer_idx = new_layer_idx;
423 /* New layer's parent is current layer */
424 link_layer(new_layer_idx, parent_layer_idx);
425
426 } else {
427 /* Parent comp's layer is not yet finalised, link
428 * new component to the same layer as parent
429 */
430 new_ctx->linked_layer_idx = parent_layer_idx;
431 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100432
433 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100434}
435
Jamie Fox34681992023-09-04 18:14:06 +0100436/**
437 * \brief Create a root of trust component context.
438 *
439 * \param[out] rot_ctx_handle A new context handle for the RoT context.
440 *
441 * \return Returns error code of type dpe_error_t
442 */
443static dpe_error_t create_rot_context(int *rot_ctx_handle)
444{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100445#ifdef DPE_TEST_MODE
446 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
447#else
Jamie Fox34681992023-09-04 18:14:06 +0100448 int ret;
Jamie Fox34681992023-09-04 18:14:06 +0100449 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100450#endif /* DPE_TEST_MODE */
451 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100452 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
453 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
454
455 /* Open RoT layer */
456 rot_layer_ctx->state = LAYER_STATE_OPEN;
Maulik Patela81605b2023-10-24 12:17:03 +0100457 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100458 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
459
Maulik Pateldbfd5152023-05-30 17:02:42 +0100460#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100461 /* Get the RoT CDI input for the RoT layer */
462 ret = dpe_plat_get_rot_cdi(&rot_cdi_input[0],
463 sizeof(rot_cdi_input));
464 if (ret != 0) {
465 return DPE_INTERNAL_ERROR;
466 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100467#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100468
469 /* Import the CDI key for the RoT layer */
470 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
471 &rot_cdi_input[0],
472 sizeof(rot_cdi_input));
473 if (status != PSA_SUCCESS) {
474 return DPE_INTERNAL_ERROR;
475 }
476
Maulik Patela81605b2023-10-24 12:17:03 +0100477 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100478 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100479 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100480 rot_comp_ctx->parent_idx = 0;
481 /* Link context to RoT Layer */
482 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
483 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100484 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
485
486 return DPE_NO_ERROR;
487}
488
489dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
490{
491 int i;
492
493 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
494 set_context_to_default(i);
495 }
496
497 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
498 invalidate_layer(i);
499 }
500
501 return create_rot_context(rot_ctx_handle);
502}
503
Maulik Patela81605b2023-10-24 12:17:03 +0100504dpe_error_t derive_context_request(int input_ctx_handle,
505 bool retain_parent_context,
506 bool allow_new_context_to_derive,
507 bool create_certificate,
508 const DiceInputValues *dice_inputs,
509 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000510 int32_t target_locality,
511 bool return_certificate,
512 bool allow_new_context_to_export,
513 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100514 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000515 int *new_parent_context_handle,
516 uint8_t *new_certificate_buf,
517 size_t new_certificate_buf_size,
518 size_t *new_certificate_actual_size,
519 uint8_t *exported_cdi_buf,
520 size_t exported_cdi_buf_size,
521 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100522{
Maulik Patel58595d32023-06-22 10:08:53 +0100523 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100524 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000525 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100526 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000527 struct layer_context_t *layer_ctx;
528 psa_status_t status;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100529
Maulik Patela81605b2023-10-24 12:17:03 +0100530 log_derive_context(input_ctx_handle, retain_parent_context,
531 allow_new_context_to_derive, create_certificate, dice_inputs,
532 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100533
Maulik Pateldbfd5152023-05-30 17:02:42 +0100534#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100535 if ((input_ctx_handle == 0) &&
536 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100537 /* Deriving RoT context for tests */
538 err = create_rot_context(&input_ctx_handle);
539 if (err != DPE_NO_ERROR) {
540 return err;
541 }
542 }
543#endif /* DPE_TEST_MODE */
544
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000545 if (export_cdi && !create_certificate) {
546 return DPE_INVALID_ARGUMENT;
547 }
548
Maulik Patelad2f3db2023-05-17 15:41:36 +0100549 /* Validate dice inputs */
550 if (!is_dice_input_valid(dice_inputs)) {
551 return DPE_INVALID_ARGUMENT;
552 }
553
554 /* Validate input handle */
555 if (!is_input_handle_valid(input_ctx_handle)) {
556 return DPE_INVALID_ARGUMENT;
557 }
Maulik Patela81605b2023-10-24 12:17:03 +0100558 /* Get parent component index from the input handle */
559 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100560
561 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100562 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100563 */
Maulik Patela81605b2023-10-24 12:17:03 +0100564 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100565
Maulik Patela81605b2023-10-24 12:17:03 +0100566 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100567
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000568 /* Check if parent context is allowed to derive */
569 if (!parent_ctx->is_allowed_to_derive) {
570 return DPE_INVALID_ARGUMENT;
571 }
572
Maulik Patelad2f3db2023-05-17 15:41:36 +0100573 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000574 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100575 return DPE_INVALID_ARGUMENT;
576 }
577
Maulik Patela81605b2023-10-24 12:17:03 +0100578 /* Get next free component index to add new derived context */
579 free_component_idx = get_free_component_context_index();
580 if (free_component_idx < 0) {
581 return DPE_INSUFFICIENT_MEMORY;
582 }
583
584 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000585 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
586 /* If parent context has export enabled and input allow_new_context_to_export
587 * is true, then allow context CDI to be exported for derived context
588 */
589 derived_ctx->is_export_cdi_allowed = true;
590 } else {
591 /* Export of new context CDI is NOT allowed */
592 derived_ctx->is_export_cdi_allowed = false;
593 if (export_cdi) {
594 return DPE_INVALID_ARGUMENT;
595 }
596 }
597
Maulik Patela81605b2023-10-24 12:17:03 +0100598 /* Copy dice input to the new derived component context */
599 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100600 if (err != DPE_NO_ERROR) {
601 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100602 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000603 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100604
Maulik Patela81605b2023-10-24 12:17:03 +0100605 /* Update parent idx in new derived component context */
606 derived_ctx->parent_idx = parent_ctx_idx;
607 /* Mark new derived component index as in use */
608 derived_ctx->in_use = true;
609 err = assign_layer_to_context(derived_ctx);
610 if (err != DPE_NO_ERROR) {
611 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100612 }
613
614 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100615 /* Retain and return parent handle with renewed nonce */
616 *new_parent_context_handle = input_ctx_handle;
617 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100618 if (err != DPE_NO_ERROR) {
619 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100620 }
Maulik Patela81605b2023-10-24 12:17:03 +0100621 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
622
Maulik Patelad2f3db2023-05-17 15:41:36 +0100623 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100624 /* Return invalid handle */
625 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100626 parent_ctx->nonce = INVALID_NONCE_VALUE;
627 }
628
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000629 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100630 /* Return handle to derived context */
631 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
632 err = renew_nonce(new_context_handle);
633 if (err != DPE_NO_ERROR) {
634 return err;
635 }
636 /* Update nonce in new derived component context */
637 derived_ctx->nonce = GET_NONCE(*new_context_handle);
638
639 } else {
640 /* Return invalid handle */
641 *new_context_handle = INVALID_HANDLE;
642 derived_ctx->nonce = INVALID_NONCE_VALUE;
643 }
644
645 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000646 linked_layer_idx = derived_ctx->linked_layer_idx;
647 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
648 layer_ctx = &layer_ctx_array[linked_layer_idx];
649 layer_ctx->is_cdi_to_be_exported = export_cdi;
650
651 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,
768 uint8_t *certificate_chain_buf,
769 size_t certificate_chain_buf_size,
770 size_t *certificate_chain_actual_size,
771 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 */
834 /* Finalise the current layer & create leaf certificate */
835 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
854 /* Get certificate chain */
855 err = get_certificate_chain(input_layer_idx,
856 certificate_chain_buf,
857 certificate_chain_buf_size,
858 certificate_chain_actual_size);
859 if (err != DPE_NO_ERROR) {
860 return err;
861 }
862
863 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
864
865 /* Renew handle for the same context */
866 *new_context_handle = input_ctx_handle;
867 status = renew_nonce(new_context_handle);
868 if (status != PSA_SUCCESS) {
869 return DPE_INTERNAL_ERROR;
870 }
871 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
872
873 /* Clear the context label and key contents */
874 memset(&layer_ctx->data.attest_key_label[0], 0u, layer_ctx->data.attest_key_label_len);
875 memset(&layer_ctx->data.attest_pub_key[0], 0u, layer_ctx->data.attest_pub_key_len);
876
877 return DPE_NO_ERROR;
878}
Maulik Patel83a6b592023-12-05 15:20:30 +0000879
880dpe_error_t get_certificate_chain_request(int input_ctx_handle,
881 bool retain_context,
882 bool clear_from_context,
883 uint8_t *certificate_chain_buf,
884 size_t certificate_chain_buf_size,
885 size_t *certificate_chain_actual_size,
886 int *new_context_handle)
887{
888 dpe_error_t err;
889 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
890 psa_status_t status;
891 struct layer_context_t *layer_ctx;
892
893 log_get_certificate_chain(input_ctx_handle, retain_context, clear_from_context);
894
895 /* Validate input handle */
896 if (!is_input_handle_valid(input_ctx_handle)) {
897 return DPE_INVALID_ARGUMENT;
898 }
899
900 /* Get component index from the input handle */
901 input_ctx_idx = GET_IDX(input_ctx_handle);
902 /* Get current linked layer idx */
903 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
904 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
905
906 layer_ctx = &layer_ctx_array[input_layer_idx];
907 err = get_certificate_chain(input_layer_idx,
908 certificate_chain_buf,
909 certificate_chain_buf_size,
910 certificate_chain_actual_size);
911 if (err != DPE_NO_ERROR) {
912 return err;
913 }
914
915 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
916
917 /* Renew handle for the same context, if requested */
918 if (retain_context) {
919 *new_context_handle = input_ctx_handle;
920 status = renew_nonce(new_context_handle);
921 if (status != PSA_SUCCESS) {
922 return DPE_INTERNAL_ERROR;
923 }
924 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
925
926 if (clear_from_context) {
927 /* Clear all the accumulated certificate information so far */
928 clear_certificate_chain(input_layer_idx, layer_ctx);
929 }
930
931 } else {
932 *new_context_handle = INVALID_HANDLE;
933 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
934 }
935
936 return DPE_NO_ERROR;
937}