blob: 579d79e60a95b3d3223dfc3eef0a94ac38d0c7fa [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
Tamas Ban645e5022024-02-07 11:04:44 +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#ifndef __DPE_CONTEXT_MNGR_H__
9#define __DPE_CONTEXT_MNGR_H__
10
11#include <stddef.h>
12#include <stdint.h>
13#include <stdbool.h>
14#include "dice_protection_environment.h"
Maulik Patel58595d32023-06-22 10:08:53 +010015#include "dpe_crypto_config.h"
Maulik Patelacc3f4a2024-03-25 18:34:05 +000016#include "platform_locality.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
Maulik Patel9fd8bd22023-10-30 10:58:30 +000022/* Below encoded CDI size accomodate both Attest and Seal CDI */
23#define DICE_MAX_ENCODED_CDI_SIZE ((2 * DICE_CDI_SIZE) + 16)
Maulik Patelad2f3db2023-05-17 15:41:36 +010024
25#define INVALID_HANDLE 0xFFFFFFFF
26#define INVALID_COMPONENT_IDX 0xFFFF
27#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patel97a61fe2024-07-01 15:55:04 +010028#define INVALID_CERT_CTX_IDX 65535
29#define DPE_ROT_CERT_CTX_IDX 0
Maulik Patel54d65f72023-06-28 13:04:36 +010030
Maulik Patelf268d902024-02-09 14:25:51 +000031/* Below configuration defines are platform dependent */
Tamas Ban42545792024-02-08 12:10:42 +010032#define MAX_NUM_OF_COMPONENTS 20
Maulik Patel97a61fe2024-07-01 15:55:04 +010033#define DPE_PLATFORM_CERT_CTX_IDX 1
34#define DPE_SECURE_WORLD_AND_HYPERVISOR_CERT_CTX_IDX 2
Maulik Patel8ee20fc2024-02-28 15:01:51 +000035#ifdef DPE_TEST_MODE
Maulik Patel97a61fe2024-07-01 15:55:04 +010036#define MAX_NUM_OF_CERTIFICATES 6
Maulik Patel8ee20fc2024-02-28 15:01:51 +000037#else
Maulik Patel97a61fe2024-07-01 15:55:04 +010038#define MAX_NUM_OF_CERTIFICATES 4
Maulik Patel8ee20fc2024-02-28 15:01:51 +000039#endif /* DPE_TEST_MODE */
Tamas Ban42545792024-02-08 12:10:42 +010040
Maulik Patel54d65f72023-06-28 13:04:36 +010041/* Below threshold defines the threshold below which a context cannot be destroyed */
Maulik Patel97a61fe2024-07-01 15:55:04 +010042#define DPE_DESTROY_CONTEXT_THRESHOLD_CERT_CTX_IDX \
43 DPE_SECURE_WORLD_AND_HYPERVISOR_CERT_CTX_IDX
Maulik Patelad2f3db2023-05-17 15:41:36 +010044
45/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
46#define GET_IDX(handle) ((handle) & 0xffff)
47#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
48
49#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
50#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
51
Maulik Patelacc3f4a2024-03-25 18:34:05 +000052/* Current locality by default */
53#define DEFAULT_TARGET_LOCALITY LOCALITY_NONE
54
Maulik Patelad2f3db2023-05-17 15:41:36 +010055struct component_context_data_t {
Maulik Patel97a61fe2024-07-01 15:55:04 +010056 uint8_t measurement_value[DICE_HASH_SIZE];
57 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
58 size_t measurement_descriptor_size;
59 uint8_t signer_id[DICE_HASH_SIZE];
60 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
61 size_t signer_id_descriptor_size;
62 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
63 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
64 size_t config_descriptor_size;
65 DiceMode mode;
66 uint8_t hidden[DICE_HIDDEN_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +010067};
68
69struct component_context_t {
70 struct component_context_data_t data; /* Component context data */
71 bool in_use; /* Flag to indicate if element is used */
Maulik Patel9fd8bd22023-10-30 10:58:30 +000072 bool is_allowed_to_derive; /* Is the component allowed to derive */
73 bool is_export_cdi_allowed; /* Is CDI allowed to export */
Maulik Patelad2f3db2023-05-17 15:41:36 +010074 uint16_t nonce; /* Context handle nonce for the component */
75 uint16_t parent_idx; /* Parent component's index */
Maulik Patel97a61fe2024-07-01 15:55:04 +010076 uint16_t linked_cert_ctx_idx; /* Certificate context component is linked to */
Maulik Patel9fd8bd22023-10-30 10:58:30 +000077 int32_t target_locality; /* Identifies the locality to which the
78 * derived context will be bound */
Maulik Patelad2f3db2023-05-17 15:41:36 +010079 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
80};
81
Maulik Patel97a61fe2024-07-01 15:55:04 +010082struct cert_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010083 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010084 uint8_t cdi_seal[DICE_CDI_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010085 uint8_t cdi_id[DICE_ID_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010086 psa_key_id_t attest_key_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +010087 uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE];
88 size_t attest_pub_key_len;
Maulik Patel4fed7812023-12-08 09:55:22 +000089 uint8_t external_key_deriv_label[DPE_EXTERNAL_LABEL_MAX_SIZE];
90 size_t external_key_deriv_label_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010091};
92
Maulik Patel97a61fe2024-07-01 15:55:04 +010093enum cert_ctx_state_t {
94 CERT_CTX_UNASSIGNED = 0,
95 CERT_CTX_ASSIGNED,
96 CERT_CTX_FINALISED
Maulik Patelad2f3db2023-05-17 15:41:36 +010097};
98
Maulik Patel009450d2024-04-23 12:03:10 +010099struct linked_components_t {
100 uint16_t idx[MAX_NUM_OF_COMPONENTS];
101 uint16_t count;
102};
103
Maulik Patel97a61fe2024-07-01 15:55:04 +0100104struct cert_context_t {
105 struct cert_context_data_t data;
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100106 uint16_t idx;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100107 uint16_t parent_cert_ctx_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100108 struct linked_components_t linked_components;
Maulik Patel58595d32023-06-22 10:08:53 +0100109 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patel97a61fe2024-07-01 15:55:04 +0100110 enum cert_ctx_state_t state;
Maulik Patele6adc112023-08-18 14:21:51 +0100111 bool is_external_pub_key_provided;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000112 bool is_cdi_to_be_exported;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100113 bool is_rot_cert_ctx;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000114 uint32_t cert_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100115};
116
117/**
Jamie Fox34681992023-09-04 18:14:06 +0100118 * \brief Initialise the DPE context manager.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100119 *
Jamie Fox34681992023-09-04 18:14:06 +0100120 * \param[out] rot_ctx_handle A new context handle for the RoT context.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100121 *
122 * \return Returns error code of type dpe_error_t
123 */
Jamie Fox34681992023-09-04 18:14:06 +0100124dpe_error_t initialise_context_mngr(int *rot_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100125
126/**
Maulik Patela81605b2023-10-24 12:17:03 +0100127 * \brief Derives a component context and optionally creates certificate
Maulik Patelad2f3db2023-05-17 15:41:36 +0100128 * chain.
129 *
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000130 * \param[in] input_context_handle Input handle to parent component context.
Maulik Patelcb14cde2024-01-23 12:39:53 +0000131 * \param[in] cert_id Logical certificate id to which derived
132 * context belongs to.
Maulik Patela81605b2023-10-24 12:17:03 +0100133 * \param[in] retain_parent_context Flag to indicate if parent context need
134 * to be retained. TRUE only if a client
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000135 * is calling DPE commands multiple times.
Maulik Patela81605b2023-10-24 12:17:03 +0100136 * \param[in] allow_new_context_to_derive Flag to indicate if derived context can
137 * derive further.
138 * \param[in] create_certificate Flag to indicate if certificate needs
139 * to be created. TRUE only if it is the
Maulik Patel97a61fe2024-07-01 15:55:04 +0100140 * last component in the certificate context.
Maulik Patela81605b2023-10-24 12:17:03 +0100141 * \param[in] dice_inputs Pointer to dice_input buffer.
142 * \param[in] client_id Identifier of the client calling the
143 * service.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000144 * \param[in] target_locality Identifier of the locality to which the
145 * derived context should be bound to.
146 * \param[in] return_certificate Indicates whether to return the generated
147 * certificate when create_certificate is true.
148 * \param[in] allow_new_context_to_export Indicates whether the DPE permits export of
149 * the CDI from the newly derived context.
150 * \param[in] export_cdi Indicates whether to export derived CDI.
Maulik Patela81605b2023-10-24 12:17:03 +0100151 * \param[out] new_context_handle A new handle for derived context.
152 * \param[out] new_parent_context_handle A new handle for parent context.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000153 * \param[out] new_certificate_buf If create_certificate and return_certificate
154 * are both true, this argument holds the new
155 * certificate generated for the new context.
156 * \param[in] new_certificate_buf_size Size of the allocated buffer for
157 * new certificate.
158 * \param[out] new_certificate_actual_size Actual size of the new certificate.
159 * \param[out] exported_cdi_buf If export_cdi is true, this is the
160 * exported CDI value.
161 * \param[in] exported_cdi_buf_size Size of the allocated buffer for
162 * exported CDI.
163 * \param[out] exported_cdi_actual_size Actual size of the exported CDI.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100164 *
165 * \return Returns error code of type dpe_error_t
166 */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000167dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000168 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100169 bool retain_parent_context,
170 bool allow_new_context_to_derive,
171 bool create_certificate,
172 const DiceInputValues *dice_inputs,
173 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000174 int32_t target_locality,
175 bool return_certificate,
176 bool allow_new_context_to_export,
177 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100178 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000179 int *new_parent_context_handle,
180 uint8_t *new_certificate_buf,
181 size_t new_certificate_buf_size,
182 size_t *new_certificate_actual_size,
183 uint8_t *exported_cdi_buf,
184 size_t exported_cdi_buf_size,
185 size_t *exported_cdi_actual_size);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100186
187/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100188 * \brief Destroys a component context and optionally depending on argument
189 * destroy_recursively, destroys all its child context too.
190 *
191 * \param[in] input_context_handle Input handle to child component context
192 * \param[in] destroy_recursively Flag to indicate if all derived contexts
193 * should also be destroyed recursively.
194 *
195 * \return Returns error code of type dpe_error_t
196 */
197dpe_error_t destroy_context_request(int input_ctx_handle,
198 bool destroy_recursively);
199
200/**
Maulik Patel97a61fe2024-07-01 15:55:04 +0100201 * \brief Function to get the pointer to a certificate context
Maulik Patele6adc112023-08-18 14:21:51 +0100202 *
Maulik Patel97a61fe2024-07-01 15:55:04 +0100203 * \param[in] cert_ctx_idx Index of the certificate in the certificate context
204 * array for which pointer is required
Maulik Patele6adc112023-08-18 14:21:51 +0100205 *
Maulik Patel97a61fe2024-07-01 15:55:04 +0100206 * \return Returns pointer to the certificate context if input index is valid
Maulik Patele6adc112023-08-18 14:21:51 +0100207 * else returns NULL
208 */
Maulik Patel97a61fe2024-07-01 15:55:04 +0100209struct cert_context_t* get_cert_ctx_ptr(uint16_t cert_ctx_idx);
Maulik Patele6adc112023-08-18 14:21:51 +0100210
211/**
Maulik Patel009450d2024-04-23 12:03:10 +0100212 * \brief Function to get the pointer to a component context
213 *
214 * \param[in] component_idx Index of the component in the component context array
215 * for which pointer is required
216 *
217 * \return Returns pointer to the component context if input index is valid
218 * else returns NULL
219 */
220struct component_context_t* get_component_ctx_ptr(uint16_t component_idx);
221
222/**
Maulik Patelcbded682023-12-07 11:50:16 +0000223 * \brief Certifies the attestation key and generates a leaf certificate.
224 * This command functionality depends on whether:
Maulik Patel97a61fe2024-07-01 15:55:04 +0100225 * - last certificate context is finalised
Maulik Patele6adc112023-08-18 14:21:51 +0100226 * - public key is supplied to the command
227 * - label is supplied to the command
228 *
229 * +---------------+------------+------------+----------------+
230 * | | pub_key | no pub_key | |
231 * +---------------+------------+------------+----------------+
232 * | | | see Note C | label |
233 * | finalized + see Note A +------------+----------------+
234 * | | | see Note D | no label |
235 * +---------------+------------+------------+----------------+
236 * | | | see Note E | label |
237 * | not finalized + see Note B +------------+----------------+
238 * | | | see Note F | no label |
239 * +---------------+------------+------------+----------------+
240 *
Maulik Patel97a61fe2024-07-01 15:55:04 +0100241 * A - Assigns a new certificate context (if not assigned), and creates a leaf
242 * certificate which includes supplied key.
243 * B - Creates certificate for current (existing) context, which includes supplied
Maulik Patelcbded682023-12-07 11:50:16 +0000244 * key.
Maulik Patel97a61fe2024-07-01 15:55:04 +0100245 * C - Assigns a new certificate context (if not assigned), performs derivation
246 * which includes supplied label, and creates leaf certificate (including
247 * supplied label as a claim).
248 * D - Assigns a new certificate context (if not assigned), performs standard
249 * derivation, and creates a leaf certificate.
250 * E - Performs derivation (which includes supplied label) for current/existing
251 * certificate context and creates certificate which includes supplied label
252 * as a claim.
253 * F - Performs standard derivation for current/existing certificate context,
254 * and creates certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100255 *
256 * \param[in] input_ctx_handle Input handle to component context.
257 * \param[in] retain_context Flag to indicate if context needs
258 * to be retained. TRUE only if a client
259 * is calling DPE commands multiple times.
260 * \param[in] public_key The public key to certify. If omitted,
261 * key pair is deterministically derived
262 * from the context and label argument.
263 * \param[in] public_key_size Size of the input public key.
264 * \param[in] label Additional input to the key derivation
265 * from the context. If public key is
266 * already provided, this argument is
267 * ignored.
268 * \param[in] label_size Size of the input label.
Maulik Patelcbded682023-12-07 11:50:16 +0000269 * \param[out] certificate_buf Pointer to the buffer where
270 * the certificate will be stored.
271 * \param[in] certificate_buf_size Size of the allocated buffer for
272 * the certificate.
273 * \param[out] certificate_actual_size Actual size of the certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100274 * \param[out] derived_public_key_buf Pointer to the buffer where
275 * derived public key will be stored.
276 * \param[in] derived_public_key_buf_size Size of the allocated buffer for
277 * derived public key.
278 * \param[out] derived_public_key_actual_size Actual size of the derived public
279 * key.
280 * \param[out] new_context_handle A renewed handle for same context.
281 *
282 * \return Returns error code of type dpe_error_t
283 */
284dpe_error_t certify_key_request(int input_ctx_handle,
285 bool retain_context,
286 const uint8_t *public_key,
287 size_t public_key_size,
288 const uint8_t *label,
289 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000290 uint8_t *certificate_buf,
291 size_t certificate_buf_size,
292 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100293 uint8_t *derived_public_key_buf,
294 size_t derived_public_key_buf_size,
295 size_t *derived_public_key_actual_size,
296 int *new_context_handle);
297
Maulik Patel83a6b592023-12-05 15:20:30 +0000298/**
299 * \brief Returns the certificate chain generated for a given DPE context. The
300 * order, format, and encoding of the certificate chain are specified by
301 * a DPE profile.
302 *
303 * \param[in] input_ctx_handle Input context handle for the DPE
304 * context.
305 * \param[in] retain_context Flag to indicate whether to
306 * retain the context.
307 * \param[in] clear_from_context Flag to indicate whether DPE must
308 * clear the certificate chain from
309 * the context so subsequent calls
310 * on a given context, or contexts
311 * derived from it do not include
312 * the certificates returned by this
313 * command.
314 * retain the context.
315 * \param[out] certificate_chain_buf Buffer to write the certificate
316 * chain output.
317 * \param[in] certificate_chain_buf_size Size of the certificate chain
318 * buffer.
319 * \param[out] certificate_chain_actual_size Size of the certificate chain
320 * output written to the buffer.
321 * \param[out] new_context_handle New handle for the DPE context.
322 *
323 * \return Returns error code of type dpe_error_t
324 */
325dpe_error_t get_certificate_chain_request(int input_ctx_handle,
326 bool retain_context,
327 bool clear_from_context,
328 uint8_t *certificate_chain_buf,
329 size_t certificate_chain_buf_size,
330 size_t *certificate_chain_actual_size,
331 int *new_context_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100332#ifdef __cplusplus
333}
334#endif
335
336#endif /* __DPE_CONTEXT_MNGR_H__ */