Skip to content

Commit 7689374

Browse files
committed
Add read cert TPM API
1 parent 2f29196 commit 7689374

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

include/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc,
6363
char* error, int error_sz);
6464
TPM_RC CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out);
6565
int CSME_NSE_API wolfBoot_tpm2_read_pcr(uint8_t pcrIndex, uint8_t* digest, int* digestSz);
66+
int CSME_NSE_API wolfBoot_tpm2_read_cert(uint32_t handle, uint8_t* cert, uint32_t* certSz);
6667

6768
#if defined(WOLFBOOT_TPM_VERIFY) || defined(WOLFBOOT_TPM_SEAL)
6869
int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey,

src/tpm.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,10 @@ int CSME_NSE_API wolfBoot_tpm2_read_pcr(uint8_t pcrIndex, uint8_t* digest, int*
11931193
digest, digestSz);
11941194
}
11951195

1196-
1196+
int CSME_NSE_API wolfBoot_tpm2_read_cert(uint32_t handle, uint8_t* cert, uint32_t* certSz)
1197+
{
1198+
return wolfTPM2_NVReadCert(&wolftpm_dev, handle, cert, certSz);
1199+
}
11971200

11981201

11991202

test-app/app_stm32h5.c

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ static int cmd_update_xmodem(const char *args);
166166
static int cmd_reboot(const char *args);
167167
#ifdef WOLFBOOT_TPM
168168
static int cmd_tpm_info(const char *args);
169+
static int cmd_tpm_idevid(const char *args);
170+
static int cmd_tpm_iak(const char *args);
169171
#endif
170172

171173

@@ -195,6 +197,8 @@ struct console_command COMMANDS[] =
195197
{cmd_reboot, "reboot", "reboot the system"},
196198
#ifdef WOLFBOOT_TPM
197199
{cmd_tpm_info, "tpm", "get TPM capabilities"},
200+
{cmd_tpm_idevid, "idevid", "show Initial Device Identification (IDevID) certificate"},
201+
{cmd_tpm_iak, "iak", "show Initial Attestation Identification (IAK) certificate"},
198202
#endif
199203
{NULL, "", ""}
200204
};
@@ -431,9 +435,47 @@ static const char *part_state_name(uint8_t state)
431435
}
432436
}
433437

438+
#define LINE_LEN 16
439+
void print_hex(const uint8_t* buffer, uint32_t length, int dumpChars)
440+
{
441+
word32 i, sz;
442+
443+
if (!buffer) {
444+
printf("\tNULL\n");
445+
return;
446+
}
447+
448+
while (length > 0) {
449+
sz = length;
450+
if (sz > LINE_LEN)
451+
sz = LINE_LEN;
452+
453+
printf("\t");
454+
for (i = 0; i < LINE_LEN; i++) {
455+
if (i < length)
456+
printf("%02x ", buffer[i]);
457+
else
458+
printf(" ");
459+
}
460+
if (dumpChars) {
461+
printf("| ");
462+
for (i = 0; i < sz; i++) {
463+
if (buffer[i] > 31 && buffer[i] < 127)
464+
printf("%c", buffer[i]);
465+
else
466+
printf(".");
467+
}
468+
}
469+
printf("\r\n");
470+
471+
buffer += sz;
472+
length -= sz;
473+
}
474+
}
475+
434476
static int cmd_info(const char *args)
435477
{
436-
int i, j;
478+
int i;
437479
uint32_t cur_fw_version, update_fw_version;
438480
uint32_t n_keys;
439481
uint16_t hdrSz;
@@ -489,13 +531,7 @@ static int cmd_info(const char *args)
489531
printf(" Public Key #%d: size %lu, type %lx, mask %08lx\r\n", i,
490532
size, type, mask);
491533
printf(" ====================================\r\n ");
492-
for (j = 0; j < size; j++) {
493-
printf("%02X ", keybuf[j]);
494-
if (j % 16 == 15) {
495-
printf("\r\n ");
496-
}
497-
}
498-
printf("\r\n");
534+
print_hex(keybuf, size, 0);
499535
}
500536
return 0;
501537
}
@@ -770,7 +806,37 @@ static int cmd_tpm_info(const char *args)
770806

771807
return rc;
772808
}
773-
#endif
809+
810+
static int cmd_tpm_idevid(const char *args)
811+
{
812+
int rc;
813+
uint8_t cert[1024];
814+
uint32_t certSz = (uint32_t)sizeof(cert);
815+
uint32_t handle = TPM2_IDEVID_CERT_HANDLE;
816+
817+
rc = wolfBoot_tpm2_read_cert(handle, cert, &certSz);
818+
if (rc == 0) {
819+
printf("IDevID Handle 0x%x\r\n", (unsigned int)handle);
820+
print_hex(cert, certSz, 1);
821+
}
822+
return rc;
823+
}
824+
825+
static int cmd_tpm_iak(const char *args)
826+
{
827+
int rc;
828+
uint8_t cert[1024];
829+
uint32_t certSz = (uint32_t)sizeof(cert);
830+
uint32_t handle = TPM2_IAK_CERT_HANDLE;
831+
832+
rc = wolfBoot_tpm2_read_cert(handle, cert, &certSz);
833+
if (rc == 0) {
834+
printf("IAK Handle 0x%x\r\n", (unsigned int)handle);
835+
print_hex(cert, certSz, 1);
836+
}
837+
return rc;
838+
}
839+
#endif /* WOLFBOOT_TPM */
774840

775841

776842
static int parse_cmd(const char *cmd)

0 commit comments

Comments
 (0)