blob: 238c715628a9e22c1d06ad9c8fe9e47e99009afa [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
248 status = psa_hash_finish(&hash_op,
249 &layer_ctx_array[curr_layer_idx].attest_cdi_hash_input[0],
250 sizeof(layer_ctx_array[curr_layer_idx].attest_cdi_hash_input),
251 &hash_len);
252
253 assert(hash_len == DPE_HASH_ALG_SIZE);
254
255 return status;
256}
257
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000258static dpe_error_t get_encoded_cdi_to_export(struct layer_context_t *layer_ctx,
259 uint8_t *exported_cdi_buf,
260 size_t exported_cdi_buf_size,
261 size_t *exported_cdi_actual_size)
262{
263 uint8_t cdi_buf[DICE_CDI_SIZE];
264 size_t cdi_size;
265 psa_status_t status;
266 dpe_error_t err;
267
268 /* Get CDI value */
269 status = get_layer_cdi_value(layer_ctx,
270 cdi_buf,
271 sizeof(cdi_buf),
272 &cdi_size);
273 if (status != PSA_SUCCESS) {
274 return DPE_INTERNAL_ERROR;
275 }
276
277 /* Encode CDI value */
278 err = encode_cdi(cdi_buf,
279 cdi_size,
280 exported_cdi_buf,
281 exported_cdi_buf_size,
282 exported_cdi_actual_size);
283 if (err != DPE_NO_ERROR) {
284 return err;
285 }
286 layer_ctx->is_cdi_to_be_exported = true;
287
288 return DPE_NO_ERROR;
289}
290
Maulik Patele6adc112023-08-18 14:21:51 +0100291static dpe_error_t create_layer_certificate(uint16_t layer_idx)
Maulik Patel58595d32023-06-22 10:08:53 +0100292{
Maulik Patel2358bbb2023-07-21 10:56:56 +0100293 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100294 psa_status_t status;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100295 dpe_error_t err;
Maulik Patele6adc112023-08-18 14:21:51 +0100296 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100297
298 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100299 layer_ctx = &layer_ctx_array[layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100300 parent_layer_idx = layer_ctx->parent_layer_idx;
301 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
302 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100303
Maulik Patel2358bbb2023-07-21 10:56:56 +0100304 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patele6adc112023-08-18 14:21:51 +0100305 if ((layer_idx != DPE_ROT_LAYER_IDX) &&
306 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100307
Maulik Patele6adc112023-08-18 14:21:51 +0100308 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100309
310 status = compute_layer_cdi_attest_input(layer_idx);
311 if (status != PSA_SUCCESS) {
312 return DPE_INTERNAL_ERROR;
313 }
314
Maulik Patele6adc112023-08-18 14:21:51 +0100315 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100316 if (status != PSA_SUCCESS) {
317 return DPE_INTERNAL_ERROR;
318 }
319
Maulik Patele6adc112023-08-18 14:21:51 +0100320 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100321 if (status != PSA_SUCCESS) {
322 return DPE_INTERNAL_ERROR;
323 }
324 }
325
Maulik Patele6adc112023-08-18 14:21:51 +0100326 status = derive_wrapping_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100327 if (status != PSA_SUCCESS) {
328 return DPE_INTERNAL_ERROR;
329 }
330
Maulik Patele6adc112023-08-18 14:21:51 +0100331 if (!layer_ctx->is_external_pub_key_provided) {
332 status = derive_attestation_key(layer_ctx);
333 if (status != PSA_SUCCESS) {
334 return DPE_INTERNAL_ERROR;
335 }
Maulik Patel58595d32023-06-22 10:08:53 +0100336 }
337
Maulik Patele6adc112023-08-18 14:21:51 +0100338 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100339 if (status != PSA_SUCCESS) {
340 return DPE_INTERNAL_ERROR;
341 }
342
Maulik Patel2358bbb2023-07-21 10:56:56 +0100343 err = encode_layer_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100344 layer_ctx,
345 parent_layer_ctx);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100346 if (err != DPE_NO_ERROR) {
347 return err;
Maulik Patel58595d32023-06-22 10:08:53 +0100348 }
349
Maulik Patel2358bbb2023-07-21 10:56:56 +0100350 log_intermediate_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100351 &layer_ctx->data.cert_buf[0],
352 layer_ctx->data.cert_buf_len);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100353
Maulik Patele6adc112023-08-18 14:21:51 +0100354 return store_layer_certificate(layer_ctx);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100355}
356
357static uint16_t open_new_layer(void)
358{
359 int i;
360
361 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
362 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
363 layer_ctx_array[i].state = LAYER_STATE_OPEN;
364 return i;
365 }
366 }
367
Maulik Patel2358bbb2023-07-21 10:56:56 +0100368 //TODO: There is an open issue of layer creation as described below.
369 /* This is causing extra unintended layers to open. Since each layer
370 * has some context data and certificate buffer of 3k, it is
371 * causing RAM overflow. Hence until resoluton is reached, once all
372 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100373 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
374 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
375 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
376 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100377 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
378 */
379 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100380}
381
Maulik Patela81605b2023-10-24 12:17:03 +0100382static inline void link_layer(uint16_t derived_ctx_layer, uint16_t parent_ctx_layer)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100383{
Maulik Patela81605b2023-10-24 12:17:03 +0100384 layer_ctx_array[derived_ctx_layer].parent_layer_idx = parent_ctx_layer;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100385}
386
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000387static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100388{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000389 // TODO: FIXME
390 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100391 return true;
392}
393
Maulik Patel2358bbb2023-07-21 10:56:56 +0100394static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100395{
396 uint16_t new_layer_idx, parent_layer_idx;
397
398 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
399
400 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
401 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
402
403 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
Maulik Patela81605b2023-10-24 12:17:03 +0100404 /* Parent comp's layer of new derived context is finalised; open a new layer */
Maulik Patelad2f3db2023-05-17 15:41:36 +0100405 new_layer_idx = open_new_layer();
Maulik Patel2358bbb2023-07-21 10:56:56 +0100406 if (new_layer_idx == INVALID_LAYER_IDX) {
407 return DPE_INTERNAL_ERROR;
408 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100409 /* Link this context to the new layer */
410 new_ctx->linked_layer_idx = new_layer_idx;
411 /* New layer's parent is current layer */
412 link_layer(new_layer_idx, parent_layer_idx);
413
414 } else {
415 /* Parent comp's layer is not yet finalised, link
416 * new component to the same layer as parent
417 */
418 new_ctx->linked_layer_idx = parent_layer_idx;
419 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100420
421 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100422}
423
Jamie Fox34681992023-09-04 18:14:06 +0100424/**
425 * \brief Create a root of trust component context.
426 *
427 * \param[out] rot_ctx_handle A new context handle for the RoT context.
428 *
429 * \return Returns error code of type dpe_error_t
430 */
431static dpe_error_t create_rot_context(int *rot_ctx_handle)
432{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100433#ifdef DPE_TEST_MODE
434 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
435#else
Jamie Fox34681992023-09-04 18:14:06 +0100436 int ret;
Jamie Fox34681992023-09-04 18:14:06 +0100437 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100438#endif /* DPE_TEST_MODE */
439 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100440 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
441 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
442
443 /* Open RoT layer */
444 rot_layer_ctx->state = LAYER_STATE_OPEN;
Maulik Patela81605b2023-10-24 12:17:03 +0100445 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100446 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
447
Maulik Pateldbfd5152023-05-30 17:02:42 +0100448#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100449 /* Get the RoT CDI input for the RoT layer */
450 ret = dpe_plat_get_rot_cdi(&rot_cdi_input[0],
451 sizeof(rot_cdi_input));
452 if (ret != 0) {
453 return DPE_INTERNAL_ERROR;
454 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100455#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100456
457 /* Import the CDI key for the RoT layer */
458 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
459 &rot_cdi_input[0],
460 sizeof(rot_cdi_input));
461 if (status != PSA_SUCCESS) {
462 return DPE_INTERNAL_ERROR;
463 }
464
Maulik Patela81605b2023-10-24 12:17:03 +0100465 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100466 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100467 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100468 rot_comp_ctx->parent_idx = 0;
469 /* Link context to RoT Layer */
470 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
471 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100472 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
473
474 return DPE_NO_ERROR;
475}
476
477dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
478{
479 int i;
480
481 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
482 set_context_to_default(i);
483 }
484
485 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
486 invalidate_layer(i);
487 }
488
489 return create_rot_context(rot_ctx_handle);
490}
491
Maulik Patela81605b2023-10-24 12:17:03 +0100492dpe_error_t derive_context_request(int input_ctx_handle,
493 bool retain_parent_context,
494 bool allow_new_context_to_derive,
495 bool create_certificate,
496 const DiceInputValues *dice_inputs,
497 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000498 int32_t target_locality,
499 bool return_certificate,
500 bool allow_new_context_to_export,
501 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100502 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000503 int *new_parent_context_handle,
504 uint8_t *new_certificate_buf,
505 size_t new_certificate_buf_size,
506 size_t *new_certificate_actual_size,
507 uint8_t *exported_cdi_buf,
508 size_t exported_cdi_buf_size,
509 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100510{
Maulik Patel58595d32023-06-22 10:08:53 +0100511 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100512 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000513 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100514 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000515 struct layer_context_t *layer_ctx;
516 psa_status_t status;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100517
Maulik Patela81605b2023-10-24 12:17:03 +0100518 log_derive_context(input_ctx_handle, retain_parent_context,
519 allow_new_context_to_derive, create_certificate, dice_inputs,
520 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100521
Maulik Pateldbfd5152023-05-30 17:02:42 +0100522#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100523 if ((input_ctx_handle == 0) &&
524 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100525 /* Deriving RoT context for tests */
526 err = create_rot_context(&input_ctx_handle);
527 if (err != DPE_NO_ERROR) {
528 return err;
529 }
530 }
531#endif /* DPE_TEST_MODE */
532
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000533 if (export_cdi && !create_certificate) {
534 return DPE_INVALID_ARGUMENT;
535 }
536
Maulik Patelad2f3db2023-05-17 15:41:36 +0100537 /* Validate dice inputs */
538 if (!is_dice_input_valid(dice_inputs)) {
539 return DPE_INVALID_ARGUMENT;
540 }
541
542 /* Validate input handle */
543 if (!is_input_handle_valid(input_ctx_handle)) {
544 return DPE_INVALID_ARGUMENT;
545 }
Maulik Patela81605b2023-10-24 12:17:03 +0100546 /* Get parent component index from the input handle */
547 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100548
549 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100550 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100551 */
Maulik Patela81605b2023-10-24 12:17:03 +0100552 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100553
Maulik Patela81605b2023-10-24 12:17:03 +0100554 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100555
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000556 /* Check if parent context is allowed to derive */
557 if (!parent_ctx->is_allowed_to_derive) {
558 return DPE_INVALID_ARGUMENT;
559 }
560
Maulik Patelad2f3db2023-05-17 15:41:36 +0100561 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000562 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100563 return DPE_INVALID_ARGUMENT;
564 }
565
Maulik Patela81605b2023-10-24 12:17:03 +0100566 /* Get next free component index to add new derived context */
567 free_component_idx = get_free_component_context_index();
568 if (free_component_idx < 0) {
569 return DPE_INSUFFICIENT_MEMORY;
570 }
571
572 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000573 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
574 /* If parent context has export enabled and input allow_new_context_to_export
575 * is true, then allow context CDI to be exported for derived context
576 */
577 derived_ctx->is_export_cdi_allowed = true;
578 } else {
579 /* Export of new context CDI is NOT allowed */
580 derived_ctx->is_export_cdi_allowed = false;
581 if (export_cdi) {
582 return DPE_INVALID_ARGUMENT;
583 }
584 }
585
Maulik Patela81605b2023-10-24 12:17:03 +0100586 /* Copy dice input to the new derived component context */
587 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100588 if (err != DPE_NO_ERROR) {
589 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100590 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000591 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100592
Maulik Patela81605b2023-10-24 12:17:03 +0100593 /* Update parent idx in new derived component context */
594 derived_ctx->parent_idx = parent_ctx_idx;
595 /* Mark new derived component index as in use */
596 derived_ctx->in_use = true;
597 err = assign_layer_to_context(derived_ctx);
598 if (err != DPE_NO_ERROR) {
599 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100600 }
601
602 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100603 /* Retain and return parent handle with renewed nonce */
604 *new_parent_context_handle = input_ctx_handle;
605 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100606 if (err != DPE_NO_ERROR) {
607 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100608 }
Maulik Patela81605b2023-10-24 12:17:03 +0100609 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
610
Maulik Patelad2f3db2023-05-17 15:41:36 +0100611 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100612 /* Return invalid handle */
613 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100614 parent_ctx->nonce = INVALID_NONCE_VALUE;
615 }
616
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000617 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100618 /* Return handle to derived context */
619 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
620 err = renew_nonce(new_context_handle);
621 if (err != DPE_NO_ERROR) {
622 return err;
623 }
624 /* Update nonce in new derived component context */
625 derived_ctx->nonce = GET_NONCE(*new_context_handle);
626
627 } else {
628 /* Return invalid handle */
629 *new_context_handle = INVALID_HANDLE;
630 derived_ctx->nonce = INVALID_NONCE_VALUE;
631 }
632
633 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000634 linked_layer_idx = derived_ctx->linked_layer_idx;
635 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
636 layer_ctx = &layer_ctx_array[linked_layer_idx];
637 layer_ctx->is_cdi_to_be_exported = export_cdi;
638
Maulik Patelcbded682023-12-07 11:50:16 +0000639 /* Finalise the layer */
640 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000641 err = create_layer_certificate(linked_layer_idx);
642 if (err != DPE_NO_ERROR) {
643 return err;
644 }
645
646 if (return_certificate) {
647 if (new_certificate_buf_size < layer_ctx->data.cert_buf_len) {
648 return DPE_INVALID_ARGUMENT;
649 }
650
651 /* Encode and return generated layer certificate */
652 err = add_encoded_layer_certificate(layer_ctx->data.cert_buf,
653 layer_ctx->data.cert_buf_len,
654 new_certificate_buf,
655 new_certificate_buf_size,
656 new_certificate_actual_size);
657 if (err != DPE_NO_ERROR) {
658 return err;
659 }
660 }
661 }
662
663 if (export_cdi) {
664 err = get_encoded_cdi_to_export(layer_ctx,
665 exported_cdi_buf,
666 exported_cdi_buf_size,
667 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100668 if (err != DPE_NO_ERROR) {
669 return err;
670 }
671 }
672
Maulik Patelad2f3db2023-05-17 15:41:36 +0100673 return DPE_NO_ERROR;
674}
Maulik Patel54d65f72023-06-28 13:04:36 +0100675
676dpe_error_t destroy_context_request(int input_ctx_handle,
677 bool destroy_recursively)
678{
679 uint16_t input_ctx_idx, linked_layer_idx;
680 int i;
681 bool is_layer_empty;
682
683 log_destroy_context(input_ctx_handle, destroy_recursively);
684
Maulik Patela81605b2023-10-24 12:17:03 +0100685 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100686 input_ctx_idx = GET_IDX(input_ctx_handle);
687
Maulik Patel54d65f72023-06-28 13:04:36 +0100688 /* Validate input handle */
689 if (!is_input_handle_valid(input_ctx_handle)) {
690 return DPE_INVALID_ARGUMENT;
691 }
692 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
693
Jamie Fox34681992023-09-04 18:14:06 +0100694#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100695 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
696 /* All layers till hypervisor cannot be destroyed dynamically */
697 return DPE_INVALID_ARGUMENT;
698 }
Jamie Fox34681992023-09-04 18:14:06 +0100699#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100700
701
702 if (!destroy_recursively) {
703 set_context_to_default(input_ctx_idx);
704 } else {
705 //TODO: To be implemented
706 }
707
708 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
709
710 /* Close the layer if all of its contexts are destroyed */
711 is_layer_empty = true;
712 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
713 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
714 /* There are active component context in the layer */
715 is_layer_empty = false;
716 break;
717 }
718 }
719
720 if (is_layer_empty) {
721 invalidate_layer(linked_layer_idx);
722 }
723
724 return DPE_NO_ERROR;
725}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100726
727struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
728 uint16_t component_idx)
729{
730 /* Safety case */
731 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
732 return NULL;
733 }
734
735 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
736 return &component_ctx_array[component_idx];
737 } else {
738 return NULL;
739 }
740}
741
Maulik Patele6adc112023-08-18 14:21:51 +0100742struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
743{
744 /* Safety case */
745 if (layer_idx >= MAX_NUM_OF_LAYERS) {
746 return NULL;
747 }
748
749 return &layer_ctx_array[layer_idx];
750}
751
752dpe_error_t certify_key_request(int input_ctx_handle,
753 bool retain_context,
754 const uint8_t *public_key,
755 size_t public_key_size,
756 const uint8_t *label,
757 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000758 uint8_t *certificate_buf,
759 size_t certificate_buf_size,
760 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100761 uint8_t *derived_public_key_buf,
762 size_t derived_public_key_buf_size,
763 size_t *derived_public_key_actual_size,
764 int *new_context_handle)
765{
766 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
767 dpe_error_t err;
768 psa_status_t status;
769 struct layer_context_t *parent_layer_ctx, *layer_ctx;
770
771 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
772 label, label_size);
773
774 /* Validate input handle */
775 if (!is_input_handle_valid(input_ctx_handle)) {
776 return DPE_INVALID_ARGUMENT;
777 }
778
779 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
780 return DPE_INVALID_ARGUMENT;
781 }
782
783 /* Get component index from the input handle */
784 input_ctx_idx = GET_IDX(input_ctx_handle);
785 /* Get current linked layer idx */
786 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
787 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
788
789 layer_ctx = &layer_ctx_array[input_layer_idx];
790 if (public_key_size > sizeof(layer_ctx->data.attest_pub_key)) {
791 return DPE_INVALID_ARGUMENT;
792 }
793
794 if ((public_key_size > 0) && (public_key != NULL)) {
795 layer_ctx->is_external_pub_key_provided = true;
796 /* Copy the public key provided */
797 memcpy(&layer_ctx->data.attest_pub_key[0],
798 public_key,
799 public_key_size);
800 layer_ctx->data.attest_pub_key_len = public_key_size;
801
802 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel4fed7812023-12-08 09:55:22 +0000803 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100804
805 } else {
806 /* No external public key is provided */
807 layer_ctx->is_external_pub_key_provided = false;
808
809 if ((label_size > 0) && (label != NULL)) {
810 /* Copy the label provided */
Maulik Patel4fed7812023-12-08 09:55:22 +0000811 memcpy(&layer_ctx->data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100812 label,
813 label_size);
Maulik Patel4fed7812023-12-08 09:55:22 +0000814 layer_ctx->data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100815
816 } else {
Maulik Patel4fed7812023-12-08 09:55:22 +0000817 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100818 }
819 }
820
821 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100822 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100823 */
Maulik Patelcbded682023-12-07 11:50:16 +0000824 /* Create leaf certificate */
Maulik Patele6adc112023-08-18 14:21:51 +0100825 err = create_layer_certificate(input_layer_idx);
826 if (err != DPE_NO_ERROR) {
827 return err;
828 }
829
830 /* Get parent layer derived public key to verify the certificate signature */
831 parent_layer_idx = layer_ctx_array[input_layer_idx].parent_layer_idx;
832 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
833 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
834
835 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
836 return DPE_INVALID_ARGUMENT;
837 }
838
839 memcpy(derived_public_key_buf,
840 &parent_layer_ctx->data.attest_pub_key[0],
841 parent_layer_ctx->data.attest_pub_key_len);
842 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
843
Maulik Patelcbded682023-12-07 11:50:16 +0000844 /* Get certificate */
845 if (certificate_buf_size < layer_ctx->data.cert_buf_len) {
846 return DPE_INVALID_ARGUMENT;
Maulik Patele6adc112023-08-18 14:21:51 +0100847 }
Maulik Patelcbded682023-12-07 11:50:16 +0000848 memcpy(certificate_buf,
849 &layer_ctx->data.cert_buf[0],
850 layer_ctx->data.cert_buf_len);
851 *certificate_actual_size = layer_ctx->data.cert_buf_len;
Maulik Patele6adc112023-08-18 14:21:51 +0100852
853 /* Renew handle for the same context */
854 *new_context_handle = input_ctx_handle;
855 status = renew_nonce(new_context_handle);
856 if (status != PSA_SUCCESS) {
857 return DPE_INTERNAL_ERROR;
858 }
859 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
860
861 /* Clear the context label and key contents */
Maulik Patel4fed7812023-12-08 09:55:22 +0000862 memset(&layer_ctx->data.external_key_deriv_label[0], 0u,
863 layer_ctx->data.external_key_deriv_label_len);
864 memset(&layer_ctx->data.attest_pub_key[0], 0u,
865 layer_ctx->data.attest_pub_key_len);
Maulik Patele6adc112023-08-18 14:21:51 +0100866
867 return DPE_NO_ERROR;
868}
Maulik Patel83a6b592023-12-05 15:20:30 +0000869
870dpe_error_t get_certificate_chain_request(int input_ctx_handle,
871 bool retain_context,
872 bool clear_from_context,
873 uint8_t *certificate_chain_buf,
874 size_t certificate_chain_buf_size,
875 size_t *certificate_chain_actual_size,
876 int *new_context_handle)
877{
878 dpe_error_t err;
879 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
880 psa_status_t status;
881 struct layer_context_t *layer_ctx;
882
883 log_get_certificate_chain(input_ctx_handle, retain_context, clear_from_context);
884
885 /* Validate input handle */
886 if (!is_input_handle_valid(input_ctx_handle)) {
887 return DPE_INVALID_ARGUMENT;
888 }
889
890 /* Get component index from the input handle */
891 input_ctx_idx = GET_IDX(input_ctx_handle);
892 /* Get current linked layer idx */
893 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
894 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
895
896 layer_ctx = &layer_ctx_array[input_layer_idx];
897 err = get_certificate_chain(input_layer_idx,
898 certificate_chain_buf,
899 certificate_chain_buf_size,
900 certificate_chain_actual_size);
901 if (err != DPE_NO_ERROR) {
902 return err;
903 }
904
905 log_certificate_chain(certificate_chain_buf, *certificate_chain_actual_size);
906
907 /* Renew handle for the same context, if requested */
908 if (retain_context) {
909 *new_context_handle = input_ctx_handle;
910 status = renew_nonce(new_context_handle);
911 if (status != PSA_SUCCESS) {
912 return DPE_INTERNAL_ERROR;
913 }
914 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
915
916 if (clear_from_context) {
917 /* Clear all the accumulated certificate information so far */
918 clear_certificate_chain(input_layer_idx, layer_ctx);
919 }
920
921 } else {
922 *new_context_handle = INVALID_HANDLE;
923 component_ctx_array[input_ctx_idx].nonce = INVALID_NONCE_VALUE;
924 }
925
926 return DPE_NO_ERROR;
927}