Skip to content

Commit 20c3b86

Browse files
committed
fix policy_sign digest parse checks
F/1100
1 parent 5b0c400 commit 20c3b86

3 files changed

Lines changed: 239 additions & 3 deletions

File tree

tools/tpm/policy_sign.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ int policy_sign(int argc, char *argv[])
287287
pcrDigestSz = -1;
288288
else
289289
pcrDigestSz = hexToByte(hashHexStr, pcrDigest, hashHexStrlen);
290-
if (pcrDigestSz <= 0) {
290+
if ((int)pcrDigestSz <= 0) {
291291
fprintf(stderr, "Invalid PCR hash length\n");
292292
usage();
293293
return -1;
@@ -300,7 +300,7 @@ int policy_sign(int argc, char *argv[])
300300
digestSz = -1;
301301
else
302302
digestSz = hexToByte(hashHexStr, digest, hashHexStrlen);
303-
if (digestSz <= 0) {
303+
if ((int)digestSz <= 0) {
304304
fprintf(stderr, "Invalid Policy Digest hash length\n");
305305
usage();
306306
return -1;

tools/unit-tests/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
4949
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
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 \
52-
unit-tpm-blob
52+
unit-tpm-blob unit-policy-sign
5353

5454
all: $(TESTS)
5555

@@ -132,6 +132,12 @@ unit-tpm-blob: ../../include/target.h unit-tpm-blob.c
132132
-DWOLFBOOT_HASH_SHA256 \
133133
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections
134134

135+
unit-policy-sign: ../../include/target.h unit-policy-sign.c
136+
gcc -o $@ $^ $(CFLAGS) -I../tpm -I$(WOLFBOOT_LIB_WOLFTPM) -DWOLFBOOT_TPM \
137+
-DWOLFTPM_USER_SETTINGS -DWOLFBOOT_SIGN_ECC256 -DWOLFBOOT_HASH_SHA256 \
138+
-DHAVE_ECC -DHAVE_ECC_KEY_IMPORT \
139+
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections
140+
135141
unit-store-sbrk: unit-store-sbrk.c ../../src/store_sbrk.c
136142
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
137143

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
#include <check.h>
2+
#include <stdarg.h>
3+
#include <stdint.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
#include <wolfssl/wolfcrypt/settings.h>
9+
#include <wolfssl/wolfcrypt/ecc.h>
10+
#include <wolftpm/tpm2_wrap.h>
11+
#include "tpm.h"
12+
13+
#ifdef wc_InitRng
14+
#undef wc_InitRng
15+
#endif
16+
#ifdef wc_FreeRng
17+
#undef wc_FreeRng
18+
#endif
19+
20+
static int policy_pcr_make_calls;
21+
static int policy_ref_make_calls;
22+
static int hash_digest_size_calls;
23+
static int rng_init_calls;
24+
25+
int wolfTPM2_PolicyPCRMake(TPM_ALG_ID pcrAlg, byte* pcrArray, word32 pcrArraySz,
26+
const byte* pcrDigest, word32 pcrDigestSz, byte* digest, word32* digestSz)
27+
{
28+
(void)pcrAlg;
29+
(void)pcrArray;
30+
(void)pcrArraySz;
31+
(void)pcrDigest;
32+
(void)pcrDigestSz;
33+
(void)digest;
34+
(void)digestSz;
35+
policy_pcr_make_calls++;
36+
return -200;
37+
}
38+
39+
int wolfTPM2_PolicyRefMake(TPM_ALG_ID pcrAlg, byte* digest, word32* digestSz,
40+
const byte* policyRef, word32 policyRefSz)
41+
{
42+
(void)pcrAlg;
43+
(void)digest;
44+
(void)digestSz;
45+
(void)policyRef;
46+
(void)policyRefSz;
47+
policy_ref_make_calls++;
48+
return -201;
49+
}
50+
51+
int TPM2_GetHashDigestSize(TPMI_ALG_HASH hashAlg)
52+
{
53+
(void)hashAlg;
54+
hash_digest_size_calls++;
55+
return 32;
56+
}
57+
58+
const char* TPM2_GetAlgName(TPM_ALG_ID alg)
59+
{
60+
(void)alg;
61+
return "SHA256";
62+
}
63+
64+
const char* wolfTPM2_GetRCString(int rc)
65+
{
66+
(void)rc;
67+
return "stub";
68+
}
69+
70+
int wc_InitRng(WC_RNG* rng)
71+
{
72+
(void)rng;
73+
rng_init_calls++;
74+
return -202;
75+
}
76+
77+
int wc_FreeRng(WC_RNG* rng)
78+
{
79+
(void)rng;
80+
return 0;
81+
}
82+
83+
int wc_ecc_init(ecc_key* key)
84+
{
85+
(void)key;
86+
return 0;
87+
}
88+
89+
int wc_ecc_free(ecc_key* key)
90+
{
91+
(void)key;
92+
return 0;
93+
}
94+
95+
int wc_ecc_import_unsigned(ecc_key* key, const byte* qx, const byte* qy,
96+
const byte* d, int curve_id)
97+
{
98+
(void)key;
99+
(void)qx;
100+
(void)qy;
101+
(void)d;
102+
(void)curve_id;
103+
return 0;
104+
}
105+
106+
int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng, ecc_key* key,
107+
mp_int* r, mp_int* s)
108+
{
109+
(void)in;
110+
(void)inlen;
111+
(void)rng;
112+
(void)key;
113+
(void)r;
114+
(void)s;
115+
return 0;
116+
}
117+
118+
int mp_init_multi(mp_int* mp1, mp_int* mp2, mp_int* mp3, mp_int* mp4,
119+
mp_int* mp5, mp_int* mp6)
120+
{
121+
(void)mp1;
122+
(void)mp2;
123+
(void)mp3;
124+
(void)mp4;
125+
(void)mp5;
126+
(void)mp6;
127+
return 0;
128+
}
129+
130+
int sp_unsigned_bin_size(const sp_int* a)
131+
{
132+
(void)a;
133+
return 0;
134+
}
135+
136+
int sp_to_unsigned_bin(const sp_int* a, byte* out)
137+
{
138+
(void)a;
139+
(void)out;
140+
return 0;
141+
}
142+
143+
void mp_clear(mp_int* a)
144+
{
145+
(void)a;
146+
}
147+
148+
#define main policy_sign_tool_main
149+
#include "../tpm/policy_sign.c"
150+
#undef main
151+
152+
static void setup(void)
153+
{
154+
policy_pcr_make_calls = 0;
155+
policy_ref_make_calls = 0;
156+
hash_digest_size_calls = 0;
157+
rng_init_calls = 0;
158+
}
159+
160+
static void make_oversized_hex_arg(char* dst, size_t dst_sz, const char* prefix)
161+
{
162+
size_t prefix_len = strlen(prefix);
163+
size_t hex_len = (WC_MAX_DIGEST_SIZE * 2U) + 2U;
164+
165+
ck_assert_uint_gt(dst_sz, prefix_len + hex_len);
166+
memcpy(dst, prefix, prefix_len);
167+
memset(dst + prefix_len, 'a', hex_len);
168+
dst[prefix_len + hex_len] = '\0';
169+
}
170+
171+
START_TEST(test_policy_sign_rejects_oversized_pcr_digest)
172+
{
173+
char arg[sizeof("-pcrdigest=") + (WC_MAX_DIGEST_SIZE * 2) + 3];
174+
char* argv[] = { (char*)"policy_sign", arg };
175+
int rc;
176+
177+
make_oversized_hex_arg(arg, sizeof(arg), "-pcrdigest=");
178+
rc = policy_sign(2, argv);
179+
180+
ck_assert_int_eq(rc, -1);
181+
ck_assert_int_eq(hash_digest_size_calls, 0);
182+
ck_assert_int_eq(policy_pcr_make_calls, 0);
183+
ck_assert_int_eq(policy_ref_make_calls, 0);
184+
ck_assert_int_eq(rng_init_calls, 0);
185+
}
186+
END_TEST
187+
188+
START_TEST(test_policy_sign_rejects_invalid_policy_digest_hex)
189+
{
190+
char arg[] = "-policydigest=zz";
191+
char* argv[] = { (char*)"policy_sign", arg };
192+
int rc;
193+
194+
rc = policy_sign(2, argv);
195+
196+
ck_assert_int_eq(rc, -1);
197+
ck_assert_int_eq(hash_digest_size_calls, 0);
198+
ck_assert_int_eq(policy_pcr_make_calls, 0);
199+
ck_assert_int_eq(policy_ref_make_calls, 0);
200+
ck_assert_int_eq(rng_init_calls, 0);
201+
}
202+
END_TEST
203+
204+
static Suite* policy_sign_suite(void)
205+
{
206+
Suite* s;
207+
TCase* tc;
208+
209+
s = suite_create("policy_sign");
210+
tc = tcase_create("argument_validation");
211+
tcase_add_checked_fixture(tc, setup, NULL);
212+
tcase_add_test(tc, test_policy_sign_rejects_oversized_pcr_digest);
213+
tcase_add_test(tc, test_policy_sign_rejects_invalid_policy_digest_hex);
214+
suite_add_tcase(s, tc);
215+
return s;
216+
}
217+
218+
int main(void)
219+
{
220+
Suite* s;
221+
SRunner* sr;
222+
int failed;
223+
224+
s = policy_sign_suite();
225+
sr = srunner_create(s);
226+
srunner_run_all(sr, CK_NORMAL);
227+
failed = srunner_ntests_failed(sr);
228+
srunner_free(sr);
229+
return failed == 0 ? 0 : 1;
230+
}

0 commit comments

Comments
 (0)