blob: d750cad3a91c13502581cd32f4646c218c9730fd [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
Tamas Bana5e2f582024-01-25 16:59:26 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Maulik Patelad2f3db2023-05-17 15:41:36 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_context_mngr.h"
9#include <assert.h>
10#include <string.h>
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{
Tamas Ban5179a4d2024-01-25 17:05:30 +0100263 uint8_t cdi_attest_buf[DICE_CDI_SIZE];
264 uint8_t cdi_seal_buf[DICE_CDI_SIZE];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000265 psa_status_t status;
266 dpe_error_t err;
267
Tamas Ban5179a4d2024-01-25 17:05:30 +0100268 /* Get CDIs value */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000269 status = get_layer_cdi_value(layer_ctx,
Tamas Ban5179a4d2024-01-25 17:05:30 +0100270 cdi_attest_buf,
271 cdi_seal_buf);
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000272 if (status != PSA_SUCCESS) {
273 return DPE_INTERNAL_ERROR;
274 }
275
276 /* Encode CDI value */
Tamas Ban5179a4d2024-01-25 17:05:30 +0100277 err = encode_cdi(cdi_attest_buf,
278 cdi_seal_buf,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000279 exported_cdi_buf,
280 exported_cdi_buf_size,
281 exported_cdi_actual_size);
282 if (err != DPE_NO_ERROR) {
283 return err;
284 }
285 layer_ctx->is_cdi_to_be_exported = true;
286
287 return DPE_NO_ERROR;
288}
289
Maulik Patele6adc112023-08-18 14:21:51 +0100290static dpe_error_t create_layer_certificate(uint16_t layer_idx)
Maulik Patel58595d32023-06-22 10:08:53 +0100291{
Maulik Patel2358bbb2023-07-21 10:56:56 +0100292 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +0100293 psa_status_t status;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100294 dpe_error_t err;
Maulik Patele6adc112023-08-18 14:21:51 +0100295 struct layer_context_t *layer_ctx, *parent_layer_ctx;
Maulik Patel58595d32023-06-22 10:08:53 +0100296
297 assert(layer_idx < MAX_NUM_OF_LAYERS);
Maulik Patele6adc112023-08-18 14:21:51 +0100298 layer_ctx = &layer_ctx_array[layer_idx];
Maulik Patele6adc112023-08-18 14:21:51 +0100299 parent_layer_idx = layer_ctx->parent_layer_idx;
300 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
301 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
Maulik Patel58595d32023-06-22 10:08:53 +0100302
Maulik Patel2358bbb2023-07-21 10:56:56 +0100303 /* For RoT Layer, CDI and issuer seed values are calculated by BL1_1 */
Maulik Patele6adc112023-08-18 14:21:51 +0100304 if ((layer_idx != DPE_ROT_LAYER_IDX) &&
305 (!layer_ctx->is_external_pub_key_provided)) {
Maulik Patel58595d32023-06-22 10:08:53 +0100306
Maulik Patele6adc112023-08-18 14:21:51 +0100307 /* Except for RoT Layer with no external public key supplied */
Maulik Patel58595d32023-06-22 10:08:53 +0100308
309 status = compute_layer_cdi_attest_input(layer_idx);
310 if (status != PSA_SUCCESS) {
311 return DPE_INTERNAL_ERROR;
312 }
313
Maulik Patele6adc112023-08-18 14:21:51 +0100314 status = derive_attestation_cdi(layer_ctx, parent_layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100315 if (status != PSA_SUCCESS) {
316 return DPE_INTERNAL_ERROR;
317 }
318
Maulik Patele6adc112023-08-18 14:21:51 +0100319 status = derive_sealing_cdi(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100320 if (status != PSA_SUCCESS) {
321 return DPE_INTERNAL_ERROR;
322 }
323 }
324
Maulik Patele6adc112023-08-18 14:21:51 +0100325 status = derive_wrapping_key(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 if (!layer_ctx->is_external_pub_key_provided) {
331 status = derive_attestation_key(layer_ctx);
332 if (status != PSA_SUCCESS) {
333 return DPE_INTERNAL_ERROR;
334 }
Maulik Patel58595d32023-06-22 10:08:53 +0100335 }
336
Maulik Patele6adc112023-08-18 14:21:51 +0100337 status = derive_id_from_public_key(layer_ctx);
Maulik Patel58595d32023-06-22 10:08:53 +0100338 if (status != PSA_SUCCESS) {
339 return DPE_INTERNAL_ERROR;
340 }
341
Maulik Patel2358bbb2023-07-21 10:56:56 +0100342 err = encode_layer_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100343 layer_ctx,
344 parent_layer_ctx);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100345 if (err != DPE_NO_ERROR) {
346 return err;
Maulik Patel58595d32023-06-22 10:08:53 +0100347 }
348
Maulik Patel2358bbb2023-07-21 10:56:56 +0100349 log_intermediate_certificate(layer_idx,
Maulik Patele6adc112023-08-18 14:21:51 +0100350 &layer_ctx->data.cert_buf[0],
351 layer_ctx->data.cert_buf_len);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100352
Maulik Patele6adc112023-08-18 14:21:51 +0100353 return store_layer_certificate(layer_ctx);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100354}
355
356static uint16_t open_new_layer(void)
357{
358 int i;
359
360 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
361 if (layer_ctx_array[i].state == LAYER_STATE_CLOSED) {
362 layer_ctx_array[i].state = LAYER_STATE_OPEN;
363 return i;
364 }
365 }
366
Maulik Patel2358bbb2023-07-21 10:56:56 +0100367 //TODO: There is an open issue of layer creation as described below.
368 /* This is causing extra unintended layers to open. Since each layer
369 * has some context data and certificate buffer of 3k, it is
370 * causing RAM overflow. Hence until resoluton is reached, once all
371 * layers are opened, link new compenents to the last layer.
Maulik Patela81605b2023-10-24 12:17:03 +0100372 * ISSUE DESCRIPTION: AP BL2 derives AP_BL31 with create_certificate
373 * as true. Hence we finalize Platform layer. Then AP BL2 derives AP_SPM,
374 * but since AP BL2 is finalised, we open new layer (Hypervisor layer).
375 * AP BL2 further derives AP SPx. Again, since AP BL2 is finalised,
Maulik Patel2358bbb2023-07-21 10:56:56 +0100376 * we open new layer! Here AP SPx should belong to same layer as AP SPM.
377 */
378 return MAX_NUM_OF_LAYERS - 1;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100379}
380
Maulik Patela81605b2023-10-24 12:17:03 +0100381static inline void link_layer(uint16_t derived_ctx_layer, uint16_t parent_ctx_layer)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100382{
Maulik Patela81605b2023-10-24 12:17:03 +0100383 layer_ctx_array[derived_ctx_layer].parent_layer_idx = parent_ctx_layer;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100384}
385
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000386static inline bool is_input_client_id_valid(int32_t client_id, int32_t target_locality)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100387{
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000388 // TODO: FIXME
389 // return (client_id == target_locality);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100390 return true;
391}
392
Maulik Patel2358bbb2023-07-21 10:56:56 +0100393static dpe_error_t assign_layer_to_context(struct component_context_t *new_ctx)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100394{
395 uint16_t new_layer_idx, parent_layer_idx;
396
397 assert(new_ctx->parent_idx < MAX_NUM_OF_COMPONENTS);
398
399 parent_layer_idx = component_ctx_array[new_ctx->parent_idx].linked_layer_idx;
400 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
401
402 if (layer_ctx_array[parent_layer_idx].state == LAYER_STATE_FINALISED) {
Maulik Patela81605b2023-10-24 12:17:03 +0100403 /* Parent comp's layer of new derived context is finalised; open a new layer */
Maulik Patelad2f3db2023-05-17 15:41:36 +0100404 new_layer_idx = open_new_layer();
Maulik Patel2358bbb2023-07-21 10:56:56 +0100405 if (new_layer_idx == INVALID_LAYER_IDX) {
406 return DPE_INTERNAL_ERROR;
407 }
Maulik Patelad2f3db2023-05-17 15:41:36 +0100408 /* Link this context to the new layer */
409 new_ctx->linked_layer_idx = new_layer_idx;
410 /* New layer's parent is current layer */
411 link_layer(new_layer_idx, parent_layer_idx);
412
413 } else {
414 /* Parent comp's layer is not yet finalised, link
415 * new component to the same layer as parent
416 */
417 new_ctx->linked_layer_idx = parent_layer_idx;
418 }
Maulik Patel2358bbb2023-07-21 10:56:56 +0100419
420 return DPE_NO_ERROR;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100421}
422
Jamie Fox34681992023-09-04 18:14:06 +0100423/**
424 * \brief Create a root of trust component context.
425 *
426 * \param[out] rot_ctx_handle A new context handle for the RoT context.
427 *
428 * \return Returns error code of type dpe_error_t
429 */
430static dpe_error_t create_rot_context(int *rot_ctx_handle)
431{
Maulik Pateldbfd5152023-05-30 17:02:42 +0100432#ifdef DPE_TEST_MODE
433 uint8_t rot_cdi_input[DICE_CDI_SIZE] = TEST_ROT_CDI_VAL;
434#else
Jamie Fox34681992023-09-04 18:14:06 +0100435 int ret;
Jamie Fox34681992023-09-04 18:14:06 +0100436 uint8_t rot_cdi_input[DICE_CDI_SIZE];
Maulik Pateldbfd5152023-05-30 17:02:42 +0100437#endif /* DPE_TEST_MODE */
438 psa_status_t status;
Jamie Fox34681992023-09-04 18:14:06 +0100439 struct component_context_t *rot_comp_ctx = &component_ctx_array[0];
440 struct layer_context_t *rot_layer_ctx = &layer_ctx_array[DPE_ROT_LAYER_IDX];
441
442 /* Open RoT layer */
443 rot_layer_ctx->state = LAYER_STATE_OPEN;
Maulik Patela81605b2023-10-24 12:17:03 +0100444 /* Parent layer for RoT context's layer is same */
Jamie Fox34681992023-09-04 18:14:06 +0100445 rot_layer_ctx->parent_layer_idx = DPE_ROT_LAYER_IDX;
446
Maulik Pateldbfd5152023-05-30 17:02:42 +0100447#ifndef DPE_TEST_MODE
Jamie Fox34681992023-09-04 18:14:06 +0100448 /* Get the RoT CDI input for the RoT layer */
449 ret = dpe_plat_get_rot_cdi(&rot_cdi_input[0],
450 sizeof(rot_cdi_input));
451 if (ret != 0) {
452 return DPE_INTERNAL_ERROR;
453 }
Maulik Pateldbfd5152023-05-30 17:02:42 +0100454#endif /* DPE_TEST_MODE */
Jamie Fox34681992023-09-04 18:14:06 +0100455
456 /* Import the CDI key for the RoT layer */
457 status = create_layer_cdi_key(&layer_ctx_array[DPE_ROT_LAYER_IDX],
458 &rot_cdi_input[0],
459 sizeof(rot_cdi_input));
460 if (status != PSA_SUCCESS) {
461 return DPE_INTERNAL_ERROR;
462 }
463
Maulik Patela81605b2023-10-24 12:17:03 +0100464 /* Init RoT context, ready to be derived in next call to DeriveContext */
Jamie Fox34681992023-09-04 18:14:06 +0100465 rot_comp_ctx->nonce = 0;
Maulik Patela81605b2023-10-24 12:17:03 +0100466 /* Parent component index for derived RoT context is same */
Jamie Fox34681992023-09-04 18:14:06 +0100467 rot_comp_ctx->parent_idx = 0;
468 /* Link context to RoT Layer */
469 rot_comp_ctx->linked_layer_idx = DPE_ROT_LAYER_IDX;
470 rot_comp_ctx->expected_mhu_id = 0;
Jamie Fox34681992023-09-04 18:14:06 +0100471 *rot_ctx_handle = 0; /* index = 0, nonce = 0 */
472
473 return DPE_NO_ERROR;
474}
475
476dpe_error_t initialise_context_mngr(int *rot_ctx_handle)
477{
478 int i;
479
480 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
481 set_context_to_default(i);
482 }
483
484 for (i = 0; i < MAX_NUM_OF_LAYERS; i++) {
485 invalidate_layer(i);
486 }
487
488 return create_rot_context(rot_ctx_handle);
489}
490
Maulik Patela81605b2023-10-24 12:17:03 +0100491dpe_error_t derive_context_request(int input_ctx_handle,
492 bool retain_parent_context,
493 bool allow_new_context_to_derive,
494 bool create_certificate,
495 const DiceInputValues *dice_inputs,
496 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000497 int32_t target_locality,
498 bool return_certificate,
499 bool allow_new_context_to_export,
500 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100501 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000502 int *new_parent_context_handle,
503 uint8_t *new_certificate_buf,
504 size_t new_certificate_buf_size,
505 size_t *new_certificate_actual_size,
506 uint8_t *exported_cdi_buf,
507 size_t exported_cdi_buf_size,
508 size_t *exported_cdi_actual_size)
Maulik Patelad2f3db2023-05-17 15:41:36 +0100509{
Maulik Patel58595d32023-06-22 10:08:53 +0100510 dpe_error_t err;
Maulik Patela81605b2023-10-24 12:17:03 +0100511 struct component_context_t *parent_ctx, *derived_ctx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000512 uint16_t parent_ctx_idx, linked_layer_idx;
Maulik Patela81605b2023-10-24 12:17:03 +0100513 int free_component_idx;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000514 struct layer_context_t *layer_ctx;
515 psa_status_t status;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100516
Maulik Patela81605b2023-10-24 12:17:03 +0100517 log_derive_context(input_ctx_handle, retain_parent_context,
518 allow_new_context_to_derive, create_certificate, dice_inputs,
519 client_id);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100520
Maulik Pateldbfd5152023-05-30 17:02:42 +0100521#ifdef DPE_TEST_MODE
Maulik Patela81605b2023-10-24 12:17:03 +0100522 if ((input_ctx_handle == 0) &&
523 (layer_ctx_array[DPE_ROT_LAYER_IDX].state != LAYER_STATE_FINALISED)) {
Maulik Pateldbfd5152023-05-30 17:02:42 +0100524 /* Deriving RoT context for tests */
525 err = create_rot_context(&input_ctx_handle);
526 if (err != DPE_NO_ERROR) {
527 return err;
528 }
529 }
530#endif /* DPE_TEST_MODE */
531
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000532 if (export_cdi && !create_certificate) {
533 return DPE_INVALID_ARGUMENT;
534 }
535
Maulik Patelad2f3db2023-05-17 15:41:36 +0100536 /* Validate dice inputs */
537 if (!is_dice_input_valid(dice_inputs)) {
538 return DPE_INVALID_ARGUMENT;
539 }
540
541 /* Validate input handle */
542 if (!is_input_handle_valid(input_ctx_handle)) {
543 return DPE_INVALID_ARGUMENT;
544 }
Maulik Patela81605b2023-10-24 12:17:03 +0100545 /* Get parent component index from the input handle */
546 parent_ctx_idx = GET_IDX(input_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100547
548 /* Below check is for safety only; It should not happen
Maulik Patela81605b2023-10-24 12:17:03 +0100549 * parent_ctx_idx is already checked above in is_input_handle_valid()
Maulik Patelad2f3db2023-05-17 15:41:36 +0100550 */
Maulik Patela81605b2023-10-24 12:17:03 +0100551 assert(parent_ctx_idx < MAX_NUM_OF_COMPONENTS);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100552
Maulik Patela81605b2023-10-24 12:17:03 +0100553 parent_ctx = &component_ctx_array[parent_ctx_idx];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100554
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000555 /* Check if parent context is allowed to derive */
556 if (!parent_ctx->is_allowed_to_derive) {
557 return DPE_INVALID_ARGUMENT;
558 }
559
Maulik Patelad2f3db2023-05-17 15:41:36 +0100560 //TODO: Question: how to get mhu id of incoming request?
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000561 if (!is_input_client_id_valid(client_id, parent_ctx->target_locality)) {
Maulik Patelad2f3db2023-05-17 15:41:36 +0100562 return DPE_INVALID_ARGUMENT;
563 }
564
Maulik Patela81605b2023-10-24 12:17:03 +0100565 /* Get next free component index to add new derived context */
566 free_component_idx = get_free_component_context_index();
567 if (free_component_idx < 0) {
568 return DPE_INSUFFICIENT_MEMORY;
569 }
570
571 derived_ctx = &component_ctx_array[free_component_idx];
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000572 if (parent_ctx->is_export_cdi_allowed && allow_new_context_to_export) {
573 /* If parent context has export enabled and input allow_new_context_to_export
574 * is true, then allow context CDI to be exported for derived context
575 */
576 derived_ctx->is_export_cdi_allowed = true;
577 } else {
578 /* Export of new context CDI is NOT allowed */
579 derived_ctx->is_export_cdi_allowed = false;
580 if (export_cdi) {
581 return DPE_INVALID_ARGUMENT;
582 }
583 }
584
Maulik Patela81605b2023-10-24 12:17:03 +0100585 /* Copy dice input to the new derived component context */
586 err = copy_dice_input(derived_ctx, dice_inputs);
Maulik Patel58595d32023-06-22 10:08:53 +0100587 if (err != DPE_NO_ERROR) {
588 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100589 }
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000590 derived_ctx->target_locality = target_locality;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100591
Maulik Patela81605b2023-10-24 12:17:03 +0100592 /* Update parent idx in new derived component context */
593 derived_ctx->parent_idx = parent_ctx_idx;
594 /* Mark new derived component index as in use */
595 derived_ctx->in_use = true;
596 err = assign_layer_to_context(derived_ctx);
597 if (err != DPE_NO_ERROR) {
598 return err;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100599 }
600
601 if (retain_parent_context) {
Maulik Patela81605b2023-10-24 12:17:03 +0100602 /* Retain and return parent handle with renewed nonce */
603 *new_parent_context_handle = input_ctx_handle;
604 err = renew_nonce(new_parent_context_handle);
Jamie Fox34681992023-09-04 18:14:06 +0100605 if (err != DPE_NO_ERROR) {
606 return err;
Maulik Patel2358bbb2023-07-21 10:56:56 +0100607 }
Maulik Patela81605b2023-10-24 12:17:03 +0100608 parent_ctx->nonce = GET_NONCE(*new_parent_context_handle);
609
Maulik Patelad2f3db2023-05-17 15:41:36 +0100610 } else {
Maulik Patela81605b2023-10-24 12:17:03 +0100611 /* Return invalid handle */
612 *new_parent_context_handle = INVALID_HANDLE;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100613 parent_ctx->nonce = INVALID_NONCE_VALUE;
614 }
615
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000616 if (!export_cdi) {
Maulik Patela81605b2023-10-24 12:17:03 +0100617 /* Return handle to derived context */
618 *new_context_handle = SET_IDX(*new_context_handle, free_component_idx);
619 err = renew_nonce(new_context_handle);
620 if (err != DPE_NO_ERROR) {
621 return err;
622 }
623 /* Update nonce in new derived component context */
624 derived_ctx->nonce = GET_NONCE(*new_context_handle);
625
626 } else {
627 /* Return invalid handle */
628 *new_context_handle = INVALID_HANDLE;
629 derived_ctx->nonce = INVALID_NONCE_VALUE;
630 }
631
632 if (create_certificate) {
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000633 linked_layer_idx = derived_ctx->linked_layer_idx;
634 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
635 layer_ctx = &layer_ctx_array[linked_layer_idx];
636 layer_ctx->is_cdi_to_be_exported = export_cdi;
637
Maulik Patelcbded682023-12-07 11:50:16 +0000638 /* Finalise the layer */
639 layer_ctx->state = LAYER_STATE_FINALISED;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000640 err = create_layer_certificate(linked_layer_idx);
641 if (err != DPE_NO_ERROR) {
642 return err;
643 }
644
645 if (return_certificate) {
646 if (new_certificate_buf_size < layer_ctx->data.cert_buf_len) {
647 return DPE_INVALID_ARGUMENT;
648 }
649
650 /* Encode and return generated layer certificate */
651 err = add_encoded_layer_certificate(layer_ctx->data.cert_buf,
652 layer_ctx->data.cert_buf_len,
653 new_certificate_buf,
654 new_certificate_buf_size,
655 new_certificate_actual_size);
656 if (err != DPE_NO_ERROR) {
657 return err;
658 }
659 }
660 }
661
662 if (export_cdi) {
663 err = get_encoded_cdi_to_export(layer_ctx,
664 exported_cdi_buf,
665 exported_cdi_buf_size,
666 exported_cdi_actual_size);
Maulik Patela81605b2023-10-24 12:17:03 +0100667 if (err != DPE_NO_ERROR) {
668 return err;
669 }
670 }
671
Maulik Patelad2f3db2023-05-17 15:41:36 +0100672 return DPE_NO_ERROR;
673}
Maulik Patel54d65f72023-06-28 13:04:36 +0100674
675dpe_error_t destroy_context_request(int input_ctx_handle,
676 bool destroy_recursively)
677{
678 uint16_t input_ctx_idx, linked_layer_idx;
679 int i;
680 bool is_layer_empty;
681
682 log_destroy_context(input_ctx_handle, destroy_recursively);
683
Maulik Patela81605b2023-10-24 12:17:03 +0100684 /* Get component index and linked layer from the input handle */
Maulik Patel54d65f72023-06-28 13:04:36 +0100685 input_ctx_idx = GET_IDX(input_ctx_handle);
686
Maulik Patel54d65f72023-06-28 13:04:36 +0100687 /* Validate input handle */
688 if (!is_input_handle_valid(input_ctx_handle)) {
689 return DPE_INVALID_ARGUMENT;
690 }
691 linked_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
692
Jamie Fox34681992023-09-04 18:14:06 +0100693#ifndef DPE_TEST_MODE
Maulik Patel54d65f72023-06-28 13:04:36 +0100694 if (linked_layer_idx <= DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX) {
695 /* All layers till hypervisor cannot be destroyed dynamically */
696 return DPE_INVALID_ARGUMENT;
697 }
Jamie Fox34681992023-09-04 18:14:06 +0100698#endif /* !DPE_TEST_MODE */
Maulik Patel54d65f72023-06-28 13:04:36 +0100699
700
701 if (!destroy_recursively) {
702 set_context_to_default(input_ctx_idx);
703 } else {
704 //TODO: To be implemented
705 }
706
707 assert(linked_layer_idx < MAX_NUM_OF_LAYERS);
708
709 /* Close the layer if all of its contexts are destroyed */
710 is_layer_empty = true;
711 for (i = 0; i < MAX_NUM_OF_COMPONENTS; i++) {
712 if (component_ctx_array[i].linked_layer_idx == linked_layer_idx) {
713 /* There are active component context in the layer */
714 is_layer_empty = false;
715 break;
716 }
717 }
718
719 if (is_layer_empty) {
720 invalidate_layer(linked_layer_idx);
721 }
722
723 return DPE_NO_ERROR;
724}
Maulik Patel2358bbb2023-07-21 10:56:56 +0100725
726struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
727 uint16_t component_idx)
728{
729 /* Safety case */
730 if (component_idx >= MAX_NUM_OF_COMPONENTS) {
731 return NULL;
732 }
733
734 if (component_ctx_array[component_idx].linked_layer_idx == layer_idx) {
735 return &component_ctx_array[component_idx];
736 } else {
737 return NULL;
738 }
739}
740
Maulik Patele6adc112023-08-18 14:21:51 +0100741struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx)
742{
743 /* Safety case */
744 if (layer_idx >= MAX_NUM_OF_LAYERS) {
745 return NULL;
746 }
747
748 return &layer_ctx_array[layer_idx];
749}
750
751dpe_error_t certify_key_request(int input_ctx_handle,
752 bool retain_context,
753 const uint8_t *public_key,
754 size_t public_key_size,
755 const uint8_t *label,
756 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000757 uint8_t *certificate_buf,
758 size_t certificate_buf_size,
759 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100760 uint8_t *derived_public_key_buf,
761 size_t derived_public_key_buf_size,
762 size_t *derived_public_key_actual_size,
763 int *new_context_handle)
764{
765 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
766 dpe_error_t err;
767 psa_status_t status;
768 struct layer_context_t *parent_layer_ctx, *layer_ctx;
769
770 log_certify_key(input_ctx_handle, retain_context, public_key, public_key_size,
771 label, label_size);
772
773 /* Validate input handle */
774 if (!is_input_handle_valid(input_ctx_handle)) {
775 return DPE_INVALID_ARGUMENT;
776 }
777
778 if (label_size > DPE_EXTERNAL_LABEL_MAX_SIZE) {
779 return DPE_INVALID_ARGUMENT;
780 }
781
782 /* Get component index from the input handle */
783 input_ctx_idx = GET_IDX(input_ctx_handle);
784 /* Get current linked layer idx */
785 input_layer_idx = component_ctx_array[input_ctx_idx].linked_layer_idx;
786 assert(input_layer_idx < MAX_NUM_OF_LAYERS);
787
788 layer_ctx = &layer_ctx_array[input_layer_idx];
789 if (public_key_size > sizeof(layer_ctx->data.attest_pub_key)) {
790 return DPE_INVALID_ARGUMENT;
791 }
792
793 if ((public_key_size > 0) && (public_key != NULL)) {
794 layer_ctx->is_external_pub_key_provided = true;
795 /* Copy the public key provided */
796 memcpy(&layer_ctx->data.attest_pub_key[0],
797 public_key,
798 public_key_size);
799 layer_ctx->data.attest_pub_key_len = public_key_size;
800
801 /* If public key is provided, then provided label (if any) is ignored */
Maulik Patel4fed7812023-12-08 09:55:22 +0000802 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100803
804 } else {
805 /* No external public key is provided */
806 layer_ctx->is_external_pub_key_provided = false;
807
808 if ((label_size > 0) && (label != NULL)) {
809 /* Copy the label provided */
Maulik Patel4fed7812023-12-08 09:55:22 +0000810 memcpy(&layer_ctx->data.external_key_deriv_label[0],
Maulik Patele6adc112023-08-18 14:21:51 +0100811 label,
812 label_size);
Maulik Patel4fed7812023-12-08 09:55:22 +0000813 layer_ctx->data.external_key_deriv_label_len = label_size;
Maulik Patele6adc112023-08-18 14:21:51 +0100814
815 } else {
Maulik Patel4fed7812023-12-08 09:55:22 +0000816 layer_ctx->data.external_key_deriv_label_len = 0;
Maulik Patele6adc112023-08-18 14:21:51 +0100817 }
818 }
819
820 /* Correct layer should already be assigned in last call of
Maulik Patela81605b2023-10-24 12:17:03 +0100821 * derive context command
Maulik Patele6adc112023-08-18 14:21:51 +0100822 */
Maulik Patelcbded682023-12-07 11:50:16 +0000823 /* Create leaf certificate */
Maulik Patele6adc112023-08-18 14:21:51 +0100824 err = create_layer_certificate(input_layer_idx);
825 if (err != DPE_NO_ERROR) {
826 return err;
827 }
828
829 /* Get parent layer derived public key to verify the certificate signature */
830 parent_layer_idx = layer_ctx_array[input_layer_idx].parent_layer_idx;
831 assert(parent_layer_idx < MAX_NUM_OF_LAYERS);
832 parent_layer_ctx = &layer_ctx_array[parent_layer_idx];
833
834 if (derived_public_key_buf_size < sizeof(parent_layer_ctx->data.attest_pub_key)) {
835 return DPE_INVALID_ARGUMENT;
836 }
837
838 memcpy(derived_public_key_buf,
839 &parent_layer_ctx->data.attest_pub_key[0],
840 parent_layer_ctx->data.attest_pub_key_len);
841 *derived_public_key_actual_size = parent_layer_ctx->data.attest_pub_key_len;
842
Maulik Patelcbded682023-12-07 11:50:16 +0000843 /* Get certificate */
844 if (certificate_buf_size < layer_ctx->data.cert_buf_len) {
845 return DPE_INVALID_ARGUMENT;
Maulik Patele6adc112023-08-18 14:21:51 +0100846 }
Maulik Patelcbded682023-12-07 11:50:16 +0000847 memcpy(certificate_buf,
848 &layer_ctx->data.cert_buf[0],
849 layer_ctx->data.cert_buf_len);
850 *certificate_actual_size = layer_ctx->data.cert_buf_len;
Maulik Patele6adc112023-08-18 14:21:51 +0100851
852 /* Renew handle for the same context */
853 *new_context_handle = input_ctx_handle;
854 status = renew_nonce(new_context_handle);
855 if (status != PSA_SUCCESS) {
856 return DPE_INTERNAL_ERROR;
857 }
858 component_ctx_array[input_ctx_idx].nonce = GET_NONCE(*new_context_handle);
859
860 /* Clear the context label and key contents */
Maulik Patel4fed7812023-12-08 09:55:22 +0000861 memset(&layer_ctx->data.external_key_deriv_label[0], 0u,
862 layer_ctx->data.external_key_deriv_label_len);
863 memset(&layer_ctx->data.attest_pub_key[0], 0u,
864 layer_ctx->data.attest_pub_key_len);
Maulik Patele6adc112023-08-18 14:21:51 +0100865
866 return DPE_NO_ERROR;
867}
Maulik Patel83a6b592023-12-05 15:20:30 +0000868
869dpe_error_t get_certificate_chain_request(int input_ctx_handle,
870 bool retain_context,
871 bool clear_from_context,
872 uint8_t *certificate_chain_buf,
873 size_t certificate_chain_buf_size,
874 size_t *certificate_chain_actual_size,
875 int *new_context_handle)
876{
877 dpe_error_t err;
878 uint16_t input_ctx_idx, input_layer_idx, parent_layer_idx;
879 psa_status_t status;
880 struct layer_context_t *layer_ctx;
881
Tamas Bana5e2f582024-01-25 16:59:26 +0100882 log_get_certificate_chain(input_ctx_handle, retain_context,
883 clear_from_context, certificate_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000884
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}