Harrison Mutai | 762d494 | 2025-05-06 13:06:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020-2025, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <stdbool.h> |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "event_log.h" |
| 13 | |
| 14 | /** |
| 15 | * Print a TCG_EfiSpecIDEventStruct entry from the event log. |
| 16 | * |
| 17 | * This function extracts and prints a TCG_EfiSpecIDEventStruct |
| 18 | * entry from the event log for debugging or auditing purposes. |
| 19 | * |
| 20 | * @param[in,out] log_addr Pointer to the current position in the Event Log. |
| 21 | * Updated to the next entry after processing. |
| 22 | * @param[in,out] log_size Pointer to the remaining Event Log size. |
| 23 | * Updated to reflect the remaining bytes. |
| 24 | * |
| 25 | * @return 0 on success, or a negative error code on failure. |
| 26 | */ |
| 27 | static int event_log_print_id_event(uint8_t **log_addr, size_t *log_size) |
| 28 | { |
| 29 | unsigned int i; |
| 30 | uint8_t info_size, *info_size_ptr; |
| 31 | void *ptr = *log_addr; |
| 32 | id_event_headers_t *event = (id_event_headers_t *)ptr; |
| 33 | id_event_algorithm_size_t *alg_ptr; |
| 34 | uint32_t event_size, number_of_algorithms; |
| 35 | size_t digest_len; |
| 36 | const uint8_t *end_ptr = (uint8_t *)((uintptr_t)*log_addr + *log_size); |
| 37 | |
| 38 | if (*log_size < sizeof(id_event_headers_t)) { |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | |
| 42 | /* The fields of the event log header are defined to be PCRIndex of 0, |
| 43 | * EventType of EV_NO_ACTION, Digest of 20 bytes of 0, and |
| 44 | * Event content defined as TCG_EfiSpecIDEventStruct. |
| 45 | */ |
| 46 | printf("TCG_EfiSpecIDEvent:\n"); |
| 47 | printf(" PCRIndex : %u\n", event->header.pcr_index); |
| 48 | if (event->header.pcr_index != (uint32_t)PCR_0) { |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | printf(" EventType : %u\n", event->header.event_type); |
| 53 | if (event->header.event_type != EV_NO_ACTION) { |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | |
| 57 | printf(" Digest :"); |
| 58 | for (i = 0U; i < sizeof(event->header.digest); ++i) { |
| 59 | uint8_t val = event->header.digest[i]; |
| 60 | |
| 61 | (void)printf(" %02x", val); |
| 62 | if ((i & 0xFU) == 0U) { |
| 63 | (void)printf("\n"); |
| 64 | printf("\t\t :"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ((i & 0xFU) != 0U) { |
| 69 | (void)printf("\n"); |
| 70 | } |
| 71 | |
| 72 | /* EventSize */ |
| 73 | event_size = event->header.event_size; |
| 74 | printf(" EventSize : %u\n", event_size); |
| 75 | |
| 76 | printf(" Signature : %s\n", event->struct_header.signature); |
| 77 | printf(" PlatformClass : %u\n", |
| 78 | event->struct_header.platform_class); |
| 79 | printf(" SpecVersion : %u.%u.%u\n", |
| 80 | event->struct_header.spec_version_major, |
| 81 | event->struct_header.spec_version_minor, |
| 82 | event->struct_header.spec_errata); |
| 83 | printf(" UintnSize : %u\n", event->struct_header.uintn_size); |
| 84 | |
| 85 | /* NumberOfAlgorithms */ |
| 86 | number_of_algorithms = event->struct_header.number_of_algorithms; |
| 87 | printf(" NumberOfAlgorithms : %u\n", number_of_algorithms); |
| 88 | |
| 89 | /* Address of DigestSizes[] */ |
| 90 | alg_ptr = event->struct_header.digest_size; |
| 91 | |
| 92 | /* Size of DigestSizes[] */ |
| 93 | digest_len = number_of_algorithms * sizeof(id_event_algorithm_size_t); |
| 94 | if (digest_len > (uintptr_t)end_ptr - (uintptr_t)alg_ptr) { |
| 95 | return -EFAULT; |
| 96 | } |
| 97 | |
| 98 | printf(" DigestSizes :\n"); |
| 99 | for (i = 0U; i < number_of_algorithms; ++i) { |
| 100 | printf(" #%u AlgorithmId : SHA", i); |
| 101 | uint16_t algorithm_id = alg_ptr[i].algorithm_id; |
| 102 | |
| 103 | switch (algorithm_id) { |
| 104 | case TPM_ALG_SHA256: |
| 105 | (void)printf("256\n"); |
| 106 | break; |
| 107 | case TPM_ALG_SHA384: |
| 108 | (void)printf("384\n"); |
| 109 | break; |
| 110 | case TPM_ALG_SHA512: |
| 111 | (void)printf("512\n"); |
| 112 | break; |
| 113 | default: |
| 114 | (void)printf("?\n"); |
| 115 | // ERROR("Algorithm 0x%x not found\n", algorithm_id); |
| 116 | printf("Algorithm 0x%x not found\n", algorithm_id); |
| 117 | return -ENOENT; |
| 118 | } |
| 119 | |
| 120 | printf(" DigestSize : %u\n", alg_ptr[i].digest_size); |
| 121 | } |
| 122 | |
| 123 | /* Address of VendorInfoSize */ |
| 124 | info_size_ptr = (uint8_t *)((uintptr_t)alg_ptr + digest_len); |
| 125 | if ((uintptr_t)info_size_ptr > (uintptr_t)end_ptr) { |
| 126 | return -EFAULT; |
| 127 | } |
| 128 | |
| 129 | info_size = *info_size_ptr++; |
| 130 | printf(" VendorInfoSize : %u\n", info_size); |
| 131 | |
| 132 | /* Check VendorInfo end address */ |
| 133 | if (((uintptr_t)info_size_ptr + info_size) > (uintptr_t)end_ptr) { |
| 134 | return -EFAULT; |
| 135 | } |
| 136 | |
| 137 | /* Check EventSize */ |
| 138 | if (event_size != |
| 139 | (sizeof(id_event_struct_t) + digest_len + info_size)) { |
| 140 | return -EFAULT; |
| 141 | } |
| 142 | |
| 143 | if (info_size != 0U) { |
| 144 | printf(" VendorInfo :"); |
| 145 | for (i = 0U; i < info_size; ++i) { |
| 146 | (void)printf(" %02x", *info_size_ptr++); |
| 147 | } |
| 148 | (void)printf("\n"); |
| 149 | } |
| 150 | |
| 151 | *log_size -= (uintptr_t)info_size_ptr - (uintptr_t)*log_addr; |
| 152 | *log_addr = info_size_ptr; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Print a TCG_PCR_EVENT2 entry from the event log. |
| 159 | * |
| 160 | * This function extracts and prints a TCG_PCR_EVENT2 structure |
| 161 | * from the event log for debugging or auditing purposes. |
| 162 | * |
| 163 | * @param[in,out] log_addr Pointer to the current position in the Event Log. |
| 164 | * Updated to the next entry after processing. |
| 165 | * @param[in,out] log_size Pointer to the remaining Event Log size. |
| 166 | * Updated to reflect the remaining bytes. |
| 167 | * |
| 168 | * @return 0 on success, or a negative error code on failure. |
| 169 | */ |
| 170 | static int event_log_print_pcr_event2(uint8_t **log_addr, size_t *log_size) |
| 171 | { |
| 172 | uint32_t event_size, count; |
| 173 | size_t sha_size, digests_size = 0U; |
| 174 | void *ptr = *log_addr; |
| 175 | const uint8_t *end_ptr = (uint8_t *)((uintptr_t)*log_addr + *log_size); |
| 176 | |
| 177 | if (*log_size < sizeof(event2_header_t)) { |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | printf("PCR_Event2:\n"); |
| 182 | printf(" PCRIndex : %u\n", |
| 183 | ((event2_header_t *)ptr)->pcr_index); |
| 184 | printf(" EventType : %u\n", |
| 185 | ((event2_header_t *)ptr)->event_type); |
| 186 | |
| 187 | count = ((event2_header_t *)ptr)->digests.count; |
| 188 | if (count < 1U) { |
| 189 | printf("Invalid Digests Count : %u\n", count); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | printf(" Digests Count : %u\n", count); |
| 194 | |
| 195 | /* Address of TCG_PCR_EVENT2.Digests[] */ |
| 196 | ptr = (uint8_t *)ptr + sizeof(event2_header_t); |
| 197 | if ((uintptr_t)ptr > (uintptr_t)end_ptr) { |
| 198 | return -EFAULT; |
| 199 | } |
| 200 | |
| 201 | for (unsigned int i = 0U; i < count; ++i) { |
| 202 | /* Check AlgorithmId address */ |
| 203 | if (((uintptr_t)ptr + offsetof(tpmt_ha, digest)) > |
| 204 | (uintptr_t)end_ptr) { |
| 205 | return -EFAULT; |
| 206 | } |
| 207 | |
| 208 | printf(" #%u AlgorithmId : SHA", i); |
| 209 | switch (((tpmt_ha *)ptr)->algorithm_id) { |
| 210 | case TPM_ALG_SHA256: |
| 211 | sha_size = SHA256_DIGEST_SIZE; |
| 212 | (void)printf("256\n"); |
| 213 | break; |
| 214 | case TPM_ALG_SHA384: |
| 215 | sha_size = SHA384_DIGEST_SIZE; |
| 216 | (void)printf("384\n"); |
| 217 | break; |
| 218 | case TPM_ALG_SHA512: |
| 219 | sha_size = SHA512_DIGEST_SIZE; |
| 220 | (void)printf("512\n"); |
| 221 | break; |
| 222 | default: |
| 223 | (void)printf("?\n"); |
| 224 | printf("Algorithm 0x%x not found\n", |
| 225 | ((tpmt_ha *)ptr)->algorithm_id); |
| 226 | return -ENOENT; |
| 227 | } |
| 228 | |
| 229 | /* End of Digest[] */ |
| 230 | ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest)); |
| 231 | if (((uintptr_t)ptr + sha_size) > (uintptr_t)end_ptr) { |
| 232 | return -EFAULT; |
| 233 | } |
| 234 | |
| 235 | /* Total size of all digests */ |
| 236 | digests_size += sha_size; |
| 237 | |
| 238 | printf(" Digest :"); |
| 239 | for (unsigned int j = 0U; j < sha_size; ++j) { |
| 240 | (void)printf(" %02x", *(uint8_t *)ptr++); |
| 241 | if ((j & 0xFU) == 0xFU) { |
| 242 | (void)printf("\n"); |
| 243 | if (j < (sha_size - 1U)) { |
| 244 | printf("\t\t :"); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* TCG_PCR_EVENT2.EventSize */ |
| 251 | if (((uintptr_t)ptr + offsetof(event2_data_t, event)) > |
| 252 | (uintptr_t)end_ptr) { |
| 253 | return -EFAULT; |
| 254 | } |
| 255 | |
| 256 | event_size = ((event2_data_t *)ptr)->event_size; |
| 257 | printf(" EventSize : %u\n", event_size); |
| 258 | |
| 259 | /* Address of TCG_PCR_EVENT2.Event[EventSize] */ |
| 260 | ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event)); |
| 261 | |
| 262 | /* End of TCG_PCR_EVENT2.Event[EventSize] */ |
| 263 | if (((uintptr_t)ptr + event_size) > (uintptr_t)end_ptr) { |
| 264 | return -EFAULT; |
| 265 | } |
| 266 | |
| 267 | if ((event_size == sizeof(startup_locality_event_t)) && |
| 268 | (strcmp((const char *)ptr, TCG_STARTUP_LOCALITY_SIGNATURE) == 0)) { |
| 269 | printf(" Signature : %s\n", |
| 270 | ((startup_locality_event_t *)ptr)->signature); |
| 271 | printf(" StartupLocality : %u\n", |
| 272 | ((startup_locality_event_t *)ptr)->startup_locality); |
| 273 | } else { |
| 274 | printf(" Event : %s\n", (uint8_t *)ptr); |
| 275 | } |
| 276 | |
| 277 | *log_size -= (uintptr_t)ptr + event_size - (uintptr_t)*log_addr; |
| 278 | *log_addr = (uint8_t *)ptr + event_size; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int event_log_dump(uint8_t *log_addr, size_t log_size) |
| 284 | { |
| 285 | int rc; |
| 286 | |
| 287 | if (log_addr == NULL) { |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | |
| 291 | /* Print TCG_EfiSpecIDEvent */ |
| 292 | rc = event_log_print_id_event(&log_addr, &log_size); |
| 293 | |
| 294 | if (rc < 0) { |
| 295 | return rc; |
| 296 | } |
| 297 | |
| 298 | while (log_size != 0U) { |
| 299 | rc = event_log_print_pcr_event2(&log_addr, &log_size); |
| 300 | if (rc < 0) { |
| 301 | return rc; |
| 302 | } |
| 303 | } |
| 304 | return 0; |
| 305 | } |