blob: 27c7681587e516d6fad3d24f3b26bc1c3794688f [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
Maulik Patelad2f3db2023-05-17 15:41:36 +010026#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patel54d65f72023-06-28 13:04:36 +010027
Maulik Patelf268d902024-02-09 14:25:51 +000028/* Below configuration defines are platform dependent */
Tamas Ban42545792024-02-08 12:10:42 +010029#define MAX_NUM_OF_COMPONENTS 20
Maulik Patel8ee20fc2024-02-28 15:01:51 +000030#ifdef DPE_TEST_MODE
Maulik Patel97a61fe2024-07-01 15:55:04 +010031#define MAX_NUM_OF_CERTIFICATES 6
Maulik Patel8ee20fc2024-02-28 15:01:51 +000032#else
Maulik Patel97a61fe2024-07-01 15:55:04 +010033#define MAX_NUM_OF_CERTIFICATES 4
Maulik Patel8ee20fc2024-02-28 15:01:51 +000034#endif /* DPE_TEST_MODE */
Tamas Ban42545792024-02-08 12:10:42 +010035
Maulik Patelad2f3db2023-05-17 15:41:36 +010036/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
37#define GET_IDX(handle) ((handle) & 0xffff)
38#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
39
40#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
41#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
42
Maulik Patelacc3f4a2024-03-25 18:34:05 +000043/* Current locality by default */
44#define DEFAULT_TARGET_LOCALITY LOCALITY_NONE
45
Maulik Patelad2f3db2023-05-17 15:41:36 +010046struct component_context_data_t {
Maulik Patel97a61fe2024-07-01 15:55:04 +010047 uint8_t measurement_value[DICE_HASH_SIZE];
48 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
49 size_t measurement_descriptor_size;
50 uint8_t signer_id[DICE_HASH_SIZE];
51 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
52 size_t signer_id_descriptor_size;
53 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
54 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
55 size_t config_descriptor_size;
56 DiceMode mode;
57 uint8_t hidden[DICE_HIDDEN_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +010058};
59
60struct component_context_t {
Maulik Patel00d06b62024-07-03 14:51:50 +010061 struct component_context_data_t data; /* Component context data */
62 bool in_use; /* Flag to indicate if element is used */
63 bool is_allowed_to_derive; /* Is the component allowed to derive */
64 bool is_export_cdi_allowed; /* Is CDI allowed to export */
65 uint16_t nonce; /* Context handle nonce for the component */
66 struct component_context_t *parent_comp_ctx; /* Pointer to parent component */
67 struct cert_context_t *linked_cert_ctx; /* Pointer to linked certificate */
68 int32_t target_locality; /* Identifies the locality to which the
69 * derived context will be bound */
70 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
Maulik Patelad2f3db2023-05-17 15:41:36 +010071};
72
Maulik Patel97a61fe2024-07-01 15:55:04 +010073struct cert_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010074 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010075 uint8_t cdi_seal[DICE_CDI_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010076 uint8_t cdi_id[DICE_ID_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010077 psa_key_id_t attest_key_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +010078 uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE];
79 size_t attest_pub_key_len;
Maulik Patel4fed7812023-12-08 09:55:22 +000080 uint8_t external_key_deriv_label[DPE_EXTERNAL_LABEL_MAX_SIZE];
81 size_t external_key_deriv_label_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010082};
83
Maulik Patel97a61fe2024-07-01 15:55:04 +010084enum cert_ctx_state_t {
85 CERT_CTX_UNASSIGNED = 0,
86 CERT_CTX_ASSIGNED,
87 CERT_CTX_FINALISED
Maulik Patelad2f3db2023-05-17 15:41:36 +010088};
89
Maulik Patel009450d2024-04-23 12:03:10 +010090struct linked_components_t {
Maulik Patel00d06b62024-07-03 14:51:50 +010091 struct component_context_t *ptr[MAX_NUM_OF_COMPONENTS]; /* Pointer to the linked components */
92 uint16_t count; /* Count of the linked components */
Maulik Patel009450d2024-04-23 12:03:10 +010093};
94
Maulik Patel97a61fe2024-07-01 15:55:04 +010095struct cert_context_t {
96 struct cert_context_data_t data;
Maulik Patel009450d2024-04-23 12:03:10 +010097 struct linked_components_t linked_components;
Maulik Patel58595d32023-06-22 10:08:53 +010098 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patel97a61fe2024-07-01 15:55:04 +010099 enum cert_ctx_state_t state;
Maulik Patele6adc112023-08-18 14:21:51 +0100100 bool is_external_pub_key_provided;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000101 bool is_cdi_to_be_exported;
Maulik Patel97a61fe2024-07-01 15:55:04 +0100102 bool is_rot_cert_ctx;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000103 uint32_t cert_id;
Maulik Patel00d06b62024-07-03 14:51:50 +0100104 struct cert_context_t *parent_cert_ptr; /* Pointer to parent certificate */
Maulik Patelad2f3db2023-05-17 15:41:36 +0100105};
106
107/**
Jamie Fox34681992023-09-04 18:14:06 +0100108 * \brief Initialise the DPE context manager.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100109 *
Jamie Fox34681992023-09-04 18:14:06 +0100110 * \param[out] rot_ctx_handle A new context handle for the RoT context.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100111 *
112 * \return Returns error code of type dpe_error_t
113 */
Jamie Fox34681992023-09-04 18:14:06 +0100114dpe_error_t initialise_context_mngr(int *rot_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100115
116/**
Maulik Patela81605b2023-10-24 12:17:03 +0100117 * \brief Derives a component context and optionally creates certificate
Maulik Patelad2f3db2023-05-17 15:41:36 +0100118 * chain.
119 *
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000120 * \param[in] input_context_handle Input handle to parent component context.
Maulik Patelcb14cde2024-01-23 12:39:53 +0000121 * \param[in] cert_id Logical certificate id to which derived
122 * context belongs to.
Maulik Patela81605b2023-10-24 12:17:03 +0100123 * \param[in] retain_parent_context Flag to indicate if parent context need
124 * to be retained. TRUE only if a client
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000125 * is calling DPE commands multiple times.
Maulik Patela81605b2023-10-24 12:17:03 +0100126 * \param[in] allow_new_context_to_derive Flag to indicate if derived context can
127 * derive further.
128 * \param[in] create_certificate Flag to indicate if certificate needs
129 * to be created. TRUE only if it is the
Maulik Patel97a61fe2024-07-01 15:55:04 +0100130 * last component in the certificate context.
Maulik Patela81605b2023-10-24 12:17:03 +0100131 * \param[in] dice_inputs Pointer to dice_input buffer.
132 * \param[in] client_id Identifier of the client calling the
133 * service.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000134 * \param[in] target_locality Identifier of the locality to which the
135 * derived context should be bound to.
136 * \param[in] return_certificate Indicates whether to return the generated
137 * certificate when create_certificate is true.
138 * \param[in] allow_new_context_to_export Indicates whether the DPE permits export of
139 * the CDI from the newly derived context.
140 * \param[in] export_cdi Indicates whether to export derived CDI.
Maulik Patela81605b2023-10-24 12:17:03 +0100141 * \param[out] new_context_handle A new handle for derived context.
142 * \param[out] new_parent_context_handle A new handle for parent context.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000143 * \param[out] new_certificate_buf If create_certificate and return_certificate
144 * are both true, this argument holds the new
145 * certificate generated for the new context.
146 * \param[in] new_certificate_buf_size Size of the allocated buffer for
147 * new certificate.
148 * \param[out] new_certificate_actual_size Actual size of the new certificate.
149 * \param[out] exported_cdi_buf If export_cdi is true, this is the
150 * exported CDI value.
151 * \param[in] exported_cdi_buf_size Size of the allocated buffer for
152 * exported CDI.
153 * \param[out] exported_cdi_actual_size Actual size of the exported CDI.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100154 *
155 * \return Returns error code of type dpe_error_t
156 */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000157dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000158 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100159 bool retain_parent_context,
160 bool allow_new_context_to_derive,
161 bool create_certificate,
162 const DiceInputValues *dice_inputs,
163 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000164 int32_t target_locality,
165 bool return_certificate,
166 bool allow_new_context_to_export,
167 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100168 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000169 int *new_parent_context_handle,
170 uint8_t *new_certificate_buf,
171 size_t new_certificate_buf_size,
172 size_t *new_certificate_actual_size,
173 uint8_t *exported_cdi_buf,
174 size_t exported_cdi_buf_size,
175 size_t *exported_cdi_actual_size);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100176
177/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100178 * \brief Destroys a component context and optionally depending on argument
179 * destroy_recursively, destroys all its child context too.
180 *
181 * \param[in] input_context_handle Input handle to child component context
182 * \param[in] destroy_recursively Flag to indicate if all derived contexts
183 * should also be destroyed recursively.
184 *
185 * \return Returns error code of type dpe_error_t
186 */
187dpe_error_t destroy_context_request(int input_ctx_handle,
188 bool destroy_recursively);
189
190/**
Maulik Patelcbded682023-12-07 11:50:16 +0000191 * \brief Certifies the attestation key and generates a leaf certificate.
192 * This command functionality depends on whether:
Maulik Patel97a61fe2024-07-01 15:55:04 +0100193 * - last certificate context is finalised
Maulik Patele6adc112023-08-18 14:21:51 +0100194 * - public key is supplied to the command
195 * - label is supplied to the command
196 *
197 * +---------------+------------+------------+----------------+
198 * | | pub_key | no pub_key | |
199 * +---------------+------------+------------+----------------+
200 * | | | see Note C | label |
201 * | finalized + see Note A +------------+----------------+
202 * | | | see Note D | no label |
203 * +---------------+------------+------------+----------------+
204 * | | | see Note E | label |
205 * | not finalized + see Note B +------------+----------------+
206 * | | | see Note F | no label |
207 * +---------------+------------+------------+----------------+
208 *
Maulik Patel97a61fe2024-07-01 15:55:04 +0100209 * A - Assigns a new certificate context (if not assigned), and creates a leaf
210 * certificate which includes supplied key.
211 * B - Creates certificate for current (existing) context, which includes supplied
Maulik Patelcbded682023-12-07 11:50:16 +0000212 * key.
Maulik Patel97a61fe2024-07-01 15:55:04 +0100213 * C - Assigns a new certificate context (if not assigned), performs derivation
214 * which includes supplied label, and creates leaf certificate (including
215 * supplied label as a claim).
216 * D - Assigns a new certificate context (if not assigned), performs standard
217 * derivation, and creates a leaf certificate.
218 * E - Performs derivation (which includes supplied label) for current/existing
219 * certificate context and creates certificate which includes supplied label
220 * as a claim.
221 * F - Performs standard derivation for current/existing certificate context,
222 * and creates certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100223 *
224 * \param[in] input_ctx_handle Input handle to component context.
225 * \param[in] retain_context Flag to indicate if context needs
226 * to be retained. TRUE only if a client
227 * is calling DPE commands multiple times.
228 * \param[in] public_key The public key to certify. If omitted,
229 * key pair is deterministically derived
230 * from the context and label argument.
231 * \param[in] public_key_size Size of the input public key.
232 * \param[in] label Additional input to the key derivation
233 * from the context. If public key is
234 * already provided, this argument is
235 * ignored.
236 * \param[in] label_size Size of the input label.
Maulik Patelcbded682023-12-07 11:50:16 +0000237 * \param[out] certificate_buf Pointer to the buffer where
238 * the certificate will be stored.
239 * \param[in] certificate_buf_size Size of the allocated buffer for
240 * the certificate.
241 * \param[out] certificate_actual_size Actual size of the certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100242 * \param[out] derived_public_key_buf Pointer to the buffer where
243 * derived public key will be stored.
244 * \param[in] derived_public_key_buf_size Size of the allocated buffer for
245 * derived public key.
246 * \param[out] derived_public_key_actual_size Actual size of the derived public
247 * key.
248 * \param[out] new_context_handle A renewed handle for same context.
249 *
250 * \return Returns error code of type dpe_error_t
251 */
252dpe_error_t certify_key_request(int input_ctx_handle,
253 bool retain_context,
254 const uint8_t *public_key,
255 size_t public_key_size,
256 const uint8_t *label,
257 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000258 uint8_t *certificate_buf,
259 size_t certificate_buf_size,
260 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100261 uint8_t *derived_public_key_buf,
262 size_t derived_public_key_buf_size,
263 size_t *derived_public_key_actual_size,
264 int *new_context_handle);
265
Maulik Patel83a6b592023-12-05 15:20:30 +0000266/**
267 * \brief Returns the certificate chain generated for a given DPE context. The
268 * order, format, and encoding of the certificate chain are specified by
269 * a DPE profile.
270 *
271 * \param[in] input_ctx_handle Input context handle for the DPE
272 * context.
273 * \param[in] retain_context Flag to indicate whether to
274 * retain the context.
275 * \param[in] clear_from_context Flag to indicate whether DPE must
276 * clear the certificate chain from
277 * the context so subsequent calls
278 * on a given context, or contexts
279 * derived from it do not include
280 * the certificates returned by this
281 * command.
282 * retain the context.
283 * \param[out] certificate_chain_buf Buffer to write the certificate
284 * chain output.
285 * \param[in] certificate_chain_buf_size Size of the certificate chain
286 * buffer.
287 * \param[out] certificate_chain_actual_size Size of the certificate chain
288 * output written to the buffer.
289 * \param[out] new_context_handle New handle for the DPE context.
290 *
291 * \return Returns error code of type dpe_error_t
292 */
293dpe_error_t get_certificate_chain_request(int input_ctx_handle,
294 bool retain_context,
295 bool clear_from_context,
296 uint8_t *certificate_chain_buf,
297 size_t certificate_chain_buf_size,
298 size_t *certificate_chain_actual_size,
299 int *new_context_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100300#ifdef __cplusplus
301}
302#endif
303
304#endif /* __DPE_CONTEXT_MNGR_H__ */