Skip to content

Commit 9a5fbee

Browse files
committed
Guard TPM keystore auth in check_rot
F/1482
1 parent 1cb6c75 commit 9a5fbee

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

src/tpm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,8 @@ int wolfBoot_check_rot(int key_slot, uint8_t* pubkey_hint)
15471547
nv.handle.hndl = WOLFBOOT_TPM_KEYSTORE_NV_BASE + key_slot;
15481548
#ifdef WOLFBOOT_TPM_KEYSTORE_AUTH
15491549
nv.handle.auth.size = (UINT16)strlen(WOLFBOOT_TPM_KEYSTORE_AUTH);
1550+
if (nv.handle.auth.size > sizeof(nv.handle.auth.buffer))
1551+
return BAD_FUNC_ARG;
15501552
memcpy(nv.handle.auth.buffer, WOLFBOOT_TPM_KEYSTORE_AUTH,
15511553
nv.handle.auth.size);
15521554
#endif

tools/unit-tests/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
5050
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
5151
unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-store-sbrk \
5252
unit-tpm-blob unit-policy-sign unit-rot-auth unit-sdhci-response-bits unit-hal-otp
53+
TESTS+=unit-tpm-check-rot-auth
5354

5455
all: $(TESTS)
5556

@@ -126,6 +127,12 @@ unit-tpm-rsa-exp: ../../include/target.h unit-tpm-rsa-exp.c
126127
-DWOLFBOOT_HASH_SHA256 \
127128
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections
128129

130+
unit-tpm-check-rot-auth: ../../include/target.h unit-tpm-check-rot-auth.c
131+
gcc -o $@ $^ $(CFLAGS) -I$(WOLFBOOT_LIB_WOLFTPM) -DWOLFBOOT_TPM \
132+
-DWOLFTPM_USER_SETTINGS -DWOLFBOOT_TPM_VERIFY -DWOLFBOOT_SIGN_RSA2048 \
133+
-DWOLFBOOT_HASH_SHA256 \
134+
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections
135+
129136
unit-tpm-blob: ../../include/target.h unit-tpm-blob.c
130137
gcc -o $@ $^ $(CFLAGS) -I$(WOLFBOOT_LIB_WOLFTPM) -DWOLFBOOT_TPM \
131138
-DWOLFTPM_USER_SETTINGS -DWOLFBOOT_TPM_SEAL -DWOLFBOOT_SIGN_RSA2048 \
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/* unit-tpm-check-rot-auth.c
2+
*
3+
* Unit tests for TPM root-of-trust auth validation.
4+
*/
5+
6+
#include <check.h>
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
#ifndef SPI_CS_TPM
12+
#define SPI_CS_TPM 1
13+
#endif
14+
#ifndef WOLFBOOT_SHA_DIGEST_SIZE
15+
#define WOLFBOOT_SHA_DIGEST_SIZE 32
16+
#endif
17+
#ifndef WOLFBOOT_TPM_HASH_ALG
18+
#define WOLFBOOT_TPM_HASH_ALG TPM_ALG_SHA256
19+
#endif
20+
#define WOLFBOOT_TPM_KEYSTORE
21+
#define WOLFBOOT_TPM_KEYSTORE_AUTH \
22+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" \
23+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
24+
25+
#include "wolfboot/wolfboot.h"
26+
#include "keystore.h"
27+
#include "tpm.h"
28+
29+
static uint8_t test_hdr[16];
30+
static uint8_t test_modulus[256];
31+
static uint8_t test_exponent_der[] = { 0xAA, 0x01, 0x00, 0x01, 0x7B };
32+
static uint8_t test_nv_digest[WOLFBOOT_SHA_DIGEST_SIZE];
33+
static uint32_t captured_exponent;
34+
35+
int keyslot_id_by_sha(const uint8_t* pubkey_hint)
36+
{
37+
(void)pubkey_hint;
38+
return 0;
39+
}
40+
41+
uint32_t keystore_get_key_type(int id)
42+
{
43+
ck_assert_int_eq(id, 0);
44+
return AUTH_KEY_RSA2048;
45+
}
46+
47+
uint8_t *keystore_get_buffer(int id)
48+
{
49+
ck_assert_int_eq(id, 0);
50+
return test_hdr;
51+
}
52+
53+
int keystore_get_size(int id)
54+
{
55+
ck_assert_int_eq(id, 0);
56+
return (int)sizeof(test_hdr);
57+
}
58+
59+
int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx, word32 inSz,
60+
const byte** n, word32* nSz, const byte** e, word32* eSz)
61+
{
62+
(void)input;
63+
(void)inSz;
64+
65+
*inOutIdx = 0;
66+
*n = test_modulus;
67+
*nSz = sizeof(test_modulus);
68+
*e = &test_exponent_der[1];
69+
*eSz = 3;
70+
return 0;
71+
}
72+
73+
int wolfTPM2_LoadRsaPublicKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
74+
const byte* rsaPub, word32 rsaPubSz, word32 exponent,
75+
TPM_ALG_ID scheme, TPMI_ALG_HASH hashAlg)
76+
{
77+
(void)dev;
78+
(void)key;
79+
(void)rsaPub;
80+
(void)rsaPubSz;
81+
(void)scheme;
82+
(void)hashAlg;
83+
84+
captured_exponent = exponent;
85+
return 0;
86+
}
87+
88+
int wolfTPM2_SetAuthHandle(WOLFTPM2_DEV* dev, int index,
89+
const WOLFTPM2_HANDLE* handle)
90+
{
91+
(void)dev;
92+
(void)index;
93+
(void)handle;
94+
return 0;
95+
}
96+
97+
int wolfTPM2_SetAuthSession(WOLFTPM2_DEV* dev, int index,
98+
WOLFTPM2_SESSION* tpmSession, TPMA_SESSION sessionAttributes)
99+
{
100+
(void)dev;
101+
(void)index;
102+
(void)tpmSession;
103+
(void)sessionAttributes;
104+
return 0;
105+
}
106+
107+
int wolfTPM2_UnsetAuth(WOLFTPM2_DEV* dev, int index)
108+
{
109+
(void)dev;
110+
(void)index;
111+
return 0;
112+
}
113+
114+
int wolfTPM2_UnsetAuthSession(WOLFTPM2_DEV* dev, int index,
115+
WOLFTPM2_SESSION* tpmSession)
116+
{
117+
(void)dev;
118+
(void)index;
119+
(void)tpmSession;
120+
return 0;
121+
}
122+
123+
int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
124+
word32 nvIndex, byte* dataBuf, word32* pDataSz, word32 offset)
125+
{
126+
(void)dev;
127+
(void)nv;
128+
(void)nvIndex;
129+
(void)offset;
130+
ck_assert_uint_eq(*pDataSz, WOLFBOOT_SHA_DIGEST_SIZE);
131+
memcpy(dataBuf, test_nv_digest, WOLFBOOT_SHA_DIGEST_SIZE);
132+
*pDataSz = WOLFBOOT_SHA_DIGEST_SIZE;
133+
return 0;
134+
}
135+
136+
const char* wolfTPM2_GetRCString(int rc)
137+
{
138+
(void)rc;
139+
return "mock";
140+
}
141+
142+
int ConstantCompare(const byte* a, const byte* b, int length)
143+
{
144+
int diff = 0;
145+
int i;
146+
147+
for (i = 0; i < length; i++) {
148+
diff |= a[i] ^ b[i];
149+
}
150+
return diff;
151+
}
152+
153+
#include "../../src/tpm.c"
154+
155+
static void setup(void)
156+
{
157+
memset(test_hdr, 0x42, sizeof(test_hdr));
158+
memset(test_modulus, 0x5A, sizeof(test_modulus));
159+
memset(test_nv_digest, 0x7C, sizeof(test_nv_digest));
160+
captured_exponent = 0;
161+
}
162+
163+
START_TEST(test_wolfBoot_check_rot_rejects_oversized_keystore_auth)
164+
{
165+
uint8_t hint[WOLFBOOT_SHA_DIGEST_SIZE];
166+
int rc;
167+
168+
memcpy(hint, test_nv_digest, sizeof(hint));
169+
170+
rc = wolfBoot_check_rot(0, hint);
171+
172+
ck_assert_int_eq(rc, BAD_FUNC_ARG);
173+
}
174+
END_TEST
175+
176+
static Suite *tpm_suite(void)
177+
{
178+
Suite *s;
179+
TCase *tc;
180+
181+
s = suite_create("TPM RoT auth");
182+
tc = tcase_create("wolfBoot_check_rot");
183+
tcase_add_checked_fixture(tc, setup, NULL);
184+
tcase_add_test(tc, test_wolfBoot_check_rot_rejects_oversized_keystore_auth);
185+
suite_add_tcase(s, tc);
186+
return s;
187+
}
188+
189+
int main(void)
190+
{
191+
Suite *s;
192+
SRunner *sr;
193+
int failed;
194+
195+
s = tpm_suite();
196+
sr = srunner_create(s);
197+
srunner_run_all(sr, CK_NORMAL);
198+
failed = srunner_ntests_failed(sr);
199+
srunner_free(sr);
200+
return failed == 0 ? 0 : 1;
201+
}

0 commit comments

Comments
 (0)