blob: 0eea2e7a6b35ac1d0753572d8944e3b3a72137e4 [file] [log] [blame]
Jamie Foxe7f8b4e2023-05-30 18:03:20 +01001/*
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +00002 * SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
Jamie Foxe7f8b4e2023-05-30 18:03:20 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_log.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +01009#include "dpe_context_mngr.h"
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010010
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000011#if (LOG_LEVEL_UNPRIV >= LOG_LEVEL_VERBOSE)
Maulik Patel780943f2024-03-14 11:40:14 +000012#define LOG_BOOL_VAL(arg) ((arg) ? "true" : "false")
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010013
14static void print_byte_array(const uint8_t *array, size_t len)
15{
16 size_t i;
17
18 if (array != NULL) {
19 for (i = 0; i < len; ++i) {
20 if ((i & 0xF) == 0) {
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000021 VERBOSE_UNPRIV_RAW("\n ");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010022 }
23 if (array[i] < 0x10) {
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000024 VERBOSE_UNPRIV_RAW(" 0%x", array[i]);
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010025 } else {
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000026 VERBOSE_UNPRIV_RAW(" %x", array[i]);
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010027 }
28 }
29 }
30
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000031 VERBOSE_UNPRIV_RAW("\n");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010032}
33
34static void log_dice_inputs(const DiceInputValues *input)
35{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000036 VERBOSE_UNPRIV_RAW(" - DICE code_hash =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010037 print_byte_array(input->code_hash, sizeof(input->code_hash));
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000038 VERBOSE_UNPRIV_RAW(" - DICE code_descriptor =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010039 print_byte_array(input->code_descriptor, input->code_descriptor_size);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000040 VERBOSE_UNPRIV_RAW(" - DICE config_type = %d\n", input->config_type);
41 VERBOSE_UNPRIV_RAW(" - DICE config_value =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010042 print_byte_array(input->config_value, sizeof(input->config_value));
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000043 VERBOSE_UNPRIV_RAW(" - DICE config_descriptor =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010044 print_byte_array(input->config_descriptor, input->config_descriptor_size);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000045 VERBOSE_UNPRIV_RAW(" - DICE authority_hash =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010046 print_byte_array(input->authority_hash, sizeof(input->authority_hash));
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000047 VERBOSE_UNPRIV_RAW(" - DICE authority_descriptor =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010048 print_byte_array(input->authority_descriptor,
49 input->authority_descriptor_size);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000050 VERBOSE_UNPRIV_RAW(" - DICE mode = %d\n", input->mode);
51 VERBOSE_UNPRIV_RAW(" - DICE hidden =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010052 print_byte_array(input->hidden, sizeof(input->hidden));
53}
54
Maulik Patelad2f3db2023-05-17 15:41:36 +010055void log_derive_rot_context(const DiceInputValues *dice_inputs)
56{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000057 VERBOSE_UNPRIV_RAW("DPE DeriveRoTContext:\n");
Maulik Patelad2f3db2023-05-17 15:41:36 +010058 log_dice_inputs(dice_inputs);
59}
60
Maulik Patel9a2a5672024-03-14 13:43:58 +000061static void log_handle(int context_handle)
62{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000063 VERBOSE_UNPRIV_RAW(" index - %d,", GET_IDX(context_handle));
64 VERBOSE_UNPRIV_RAW(" nonce - 0x%x\n", GET_NONCE(context_handle));
Maulik Patel9a2a5672024-03-14 13:43:58 +000065}
66
Maulik Patela81605b2023-10-24 12:17:03 +010067void log_derive_context(int context_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +000068 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +010069 bool retain_parent_context,
70 bool allow_new_context_to_derive,
71 bool create_certificate,
72 const DiceInputValues *dice_inputs,
73 int32_t client_id)
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010074{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000075 VERBOSE_UNPRIV_RAW("DPE DeriveContext:\n");
76 VERBOSE_UNPRIV_RAW(" - input context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +000077 log_handle(context_handle);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000078 VERBOSE_UNPRIV_RAW(" - cert_id = 0x%x\n", cert_id);
79 VERBOSE_UNPRIV_RAW(" - retain_parent_context = %s\n", LOG_BOOL_VAL(retain_parent_context));
80 VERBOSE_UNPRIV_RAW(" - allow_new_context_to_derive = %s\n", LOG_BOOL_VAL(allow_new_context_to_derive));
81 VERBOSE_UNPRIV_RAW(" - create_certificate = %s\n", LOG_BOOL_VAL(create_certificate));
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010082 log_dice_inputs(dice_inputs);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000083 VERBOSE_UNPRIV_RAW(" - client_id = %d\n", client_id);
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010084}
85
Maulik Patel54d65f72023-06-28 13:04:36 +010086void log_destroy_context(int context_handle, bool destroy_recursively)
87{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000088 VERBOSE_UNPRIV_RAW("DPE DestroyContext:\n");
89 VERBOSE_UNPRIV_RAW(" - input context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +000090 log_handle(context_handle);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000091 VERBOSE_UNPRIV_RAW(" - destroy_recursively = %s\n", LOG_BOOL_VAL(destroy_recursively));
Maulik Patel54d65f72023-06-28 13:04:36 +010092}
93
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010094void log_certify_key(int context_handle,
95 bool retain_context,
96 const uint8_t *public_key,
97 size_t public_key_size,
98 const uint8_t *label,
99 size_t label_size)
100{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000101 VERBOSE_UNPRIV_RAW("DPE CertifyKey:\n");
102 VERBOSE_UNPRIV_RAW(" - input context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000103 log_handle(context_handle);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000104 VERBOSE_UNPRIV_RAW(" - retain_context = %s\n", LOG_BOOL_VAL(retain_context));
105 VERBOSE_UNPRIV_RAW(" - public_key =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +0100106 print_byte_array(public_key, public_key_size);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000107 VERBOSE_UNPRIV_RAW(" - label =");
Jamie Foxe7f8b4e2023-05-30 18:03:20 +0100108 print_byte_array(label, label_size);
109}
110
Maulik Patel83a6b592023-12-05 15:20:30 +0000111void log_get_certificate_chain(int context_handle,
112 bool retain_context,
Tamas Bana5e2f582024-01-25 16:59:26 +0100113 bool clear_from_context,
114 size_t cert_chain_buf_size)
Maulik Patel83a6b592023-12-05 15:20:30 +0000115{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000116 VERBOSE_UNPRIV_RAW("DPE GetCertificateChain:\n");
117 VERBOSE_UNPRIV_RAW(" - input context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000118 log_handle(context_handle);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000119 VERBOSE_UNPRIV_RAW(" - retain_context = %s\n", LOG_BOOL_VAL(retain_context));
120 VERBOSE_UNPRIV_RAW(" - clear_from_context = %s\n", LOG_BOOL_VAL(clear_from_context));
121 VERBOSE_UNPRIV_RAW(" - cert_chain_buf_size = %d\n", cert_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000122}
123
Maulik Patel00d06b62024-07-03 14:51:50 +0100124void log_intermediate_certificate(const uint8_t *cert_buf,
Tamas Ban7daae9e2024-04-03 13:54:34 +0200125 size_t cert_size)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100126{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000127 VERBOSE_UNPRIV_RAW("DPE Intermediate Certificate:\n");
128 VERBOSE_UNPRIV_RAW(" - size = %d\n", cert_size);
129 VERBOSE_UNPRIV_RAW(" - certificate =");
Tamas Ban7daae9e2024-04-03 13:54:34 +0200130 print_byte_array(cert_buf, cert_size);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100131}
132
Maulik Patele6adc112023-08-18 14:21:51 +0100133void log_certificate_chain(const uint8_t *certificate_chain_buf,
134 size_t certificate_chain_size)
135{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000136 VERBOSE_UNPRIV_RAW("DPE Certificate Chain:\n");
137 VERBOSE_UNPRIV_RAW(" - size = %d\n", certificate_chain_size);
Maulik Patele6adc112023-08-18 14:21:51 +0100138 print_byte_array(certificate_chain_buf, certificate_chain_size);
139}
140
Maulik Patel9a2a5672024-03-14 13:43:58 +0000141void log_derive_context_output_handles(int parent_context_handle,
142 int new_context_handle)
143{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000144 VERBOSE_UNPRIV_RAW("DPE DeriveContext output handles:\n");
145 VERBOSE_UNPRIV_RAW(" - parent context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000146 log_handle(parent_context_handle);
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000147 VERBOSE_UNPRIV_RAW(" - new context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000148 log_handle(new_context_handle);
149}
150
151void log_certify_key_output_handle(int new_context_handle)
152{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000153 VERBOSE_UNPRIV_RAW("DPE CertifyKey output handle:\n");
154 VERBOSE_UNPRIV_RAW(" - new context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000155 log_handle(new_context_handle);
156}
157
158void log_get_certificate_chain_output_handle(int new_context_handle)
159{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000160 VERBOSE_UNPRIV_RAW("DPE GetCertificateChain output handle:\n");
161 VERBOSE_UNPRIV_RAW(" - new context handle:");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000162 log_handle(new_context_handle);
163}
164
Maulik Patel5ac87802024-03-14 14:22:19 +0000165void log_dpe_component_ctx_metadata(const struct component_context_t *ctx_ptr,
166 int component_index)
167{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000168 VERBOSE_UNPRIV_RAW(" DPE component_ctx_array[%d]: \n", component_index);
169 VERBOSE_UNPRIV_RAW(" - in_use = %s\n", LOG_BOOL_VAL(ctx_ptr->in_use));
170 VERBOSE_UNPRIV_RAW(" - is_allowed_to_derive = %s\n",
Maulik Patel5ac87802024-03-14 14:22:19 +0000171 LOG_BOOL_VAL(ctx_ptr->is_allowed_to_derive));
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000172 VERBOSE_UNPRIV_RAW(" - is_export_cdi_allowed = %s\n",
Maulik Patel5ac87802024-03-14 14:22:19 +0000173 LOG_BOOL_VAL(ctx_ptr->is_export_cdi_allowed));
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000174 VERBOSE_UNPRIV_RAW(" - nonce = 0x%x\n", ctx_ptr->nonce);
175 VERBOSE_UNPRIV_RAW(" - target_locality = %d\n", ctx_ptr->target_locality);
176 VERBOSE_UNPRIV_RAW(" - expected_mhu_id = %u\n", ctx_ptr->expected_mhu_id);
177 VERBOSE_UNPRIV_RAW(" - parent_comp_ctx->nonce = %d\n", ctx_ptr->parent_comp_ctx->nonce);
Maulik Patel00d06b62024-07-03 14:51:50 +0100178 if (ctx_ptr->linked_cert_ctx != NULL) {
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000179 VERBOSE_UNPRIV_RAW(" - linked_cert_ctx->cert_id = %d\n",
Maulik Patel00d06b62024-07-03 14:51:50 +0100180 ctx_ptr->linked_cert_ctx->cert_id);
181 }
Maulik Patel5ac87802024-03-14 14:22:19 +0000182}
183
Maulik Patel00d06b62024-07-03 14:51:50 +0100184void log_dpe_cert_ctx_metadata(const struct cert_context_t *ctx_ptr)
Maulik Patel5ac87802024-03-14 14:22:19 +0000185{
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000186 VERBOSE_UNPRIV_RAW(" DPE cert_ctx_array[]: \n");
187 VERBOSE_UNPRIV_RAW(" - cert_id = 0x%x\n", ctx_ptr->cert_id);
188 VERBOSE_UNPRIV_RAW(" - state = %d\n", ctx_ptr->state);
189 VERBOSE_UNPRIV_RAW(" - is_external_pub_key_provided = %s\n",
Maulik Patel5ac87802024-03-14 14:22:19 +0000190 LOG_BOOL_VAL(ctx_ptr->is_external_pub_key_provided));
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000191 VERBOSE_UNPRIV_RAW(" - is_cdi_to_be_exported = %s\n",
Maulik Patel5ac87802024-03-14 14:22:19 +0000192 LOG_BOOL_VAL(ctx_ptr->is_cdi_to_be_exported));
193}
194
Maulik Patelb3c82a02024-07-24 13:05:42 +0100195void log_derive_context_output(int *new_context_handle,
196 int *new_parent_context_handle,
197 struct component_context_t *derived_ctx,
198 int free_component_idx,
199 struct cert_context_t *cert_ctx,
200 uint8_t *new_certificate_buf,
Jamie Foxc4c3d792024-08-27 15:24:04 +0100201 size_t *new_certificate_actual_size)
Maulik Patelb3c82a02024-07-24 13:05:42 +0100202{
203 log_derive_context_output_handles(*new_parent_context_handle,
204 *new_context_handle);
205
206 /* Log component context, certificate context & certificate if no error */
207 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
208 if (cert_ctx != NULL) {
209 log_dpe_cert_ctx_metadata(cert_ctx);
210 }
Jamie Foxc4c3d792024-08-27 15:24:04 +0100211 if (new_certificate_actual_size != NULL && *new_certificate_actual_size > 0) {
Maulik Patelb3c82a02024-07-24 13:05:42 +0100212 log_intermediate_certificate(new_certificate_buf,
Jamie Foxc4c3d792024-08-27 15:24:04 +0100213 *new_certificate_actual_size);
Maulik Patelb3c82a02024-07-24 13:05:42 +0100214 }
215}
216
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000217#endif /* LOG_LEVEL_UNPRIV */