Skip to content

Commit c539cb2

Browse files
shmsetJuergenReppSIT
authored andcommitted
tpm2_makecredential: fix wrong tcg ek templates
The current implementation assumes low-range RSA and ECC keys. Therefore if another key type is used, the default values are used and either the tpm2_makecredential call or the tpm2_activatecredential call fails. This commit reduces the required template values to a minimum and supports additional key types. Fixes: #3526 Signed-off-by: Silas Meier <silas.meier@gapfruit.com>
1 parent 3858c0f commit c539cb2

2 files changed

Lines changed: 93 additions & 24 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
3+
source helpers.sh
4+
5+
cleanup() {
6+
rm -f secret.data ek.pub ak.pub ak.name mkcred.out actcred.out ak.out \
7+
ak.ctx session.ctx policyA.sha384 policyC.sha384
8+
9+
# Evict persistent handles, we want them to always succeed and never trip
10+
# the onerror trap.
11+
tpm2 evictcontrol -Q -C o -c 0x81010009 2>/dev/null || true
12+
13+
if [ "$1" != "no-shut-down" ]; then
14+
shut_down
15+
fi
16+
}
17+
trap cleanup EXIT
18+
19+
start_up
20+
21+
cleanup "no-shut-down"
22+
23+
echo 12345678 > secret.data
24+
25+
# Policies for high range EKs
26+
policy_a_hex="8bbf2266537c171cb56e403c4dc1d4b64f432611dc386e6f532050c3278c930e143e8bb1133824ccb431053871c6db53"
27+
echo -n "$policy_a_hex" | xxd -r -p > policyA.sha384
28+
policy_c_hex="d6032ce61f2fb3c240eb3cf6a33237ef2b6a16f4293c22b455e261cffd217ad5b4947c2d73e63005eed2dc2b3593d165"
29+
echo -n "$policy_c_hex" | xxd -r -p > policyC.sha384
30+
31+
tpm2 createek -Q -c 0x81010009 -G ecc384 -u ek.pub
32+
33+
tpm2 createak -C 0x81010009 -c ak.ctx -G rsa -g sha384 -s rsassa -u ak.pub \
34+
-n ak.name -p akpass> ak.out
35+
36+
file_size=`ls -l ak.name | awk {'print $5'}`
37+
loaded_key_name=`cat ak.name | xxd -p -c $file_size` # Use -c in xxd so there is no line wrapping
38+
39+
tpm2 readpublic -c 0x81010009 -o ek.pem -f pem -Q
40+
41+
tpm2 makecredential -Q -u ek.pem -s secret.data -n $loaded_key_name \
42+
-o mkcred.out -G ecc --tcti=none
43+
44+
# Test the secret data matches after credential activation process
45+
tpm2 startauthsession --policy-session -S session.ctx -g sha384
46+
tpm2 policysecret -S session.ctx -c e
47+
tpm2 policyor -S session.ctx sha384:policyA.sha384,policyC.sha384
48+
tpm2 activatecredential -Q -c ak.ctx -C 0x81010009 -i mkcred.out \
49+
-o actcred.out -p akpass -P"session:session.ctx"
50+
tpm2 flushcontext session.ctx
51+
52+
diff actcred.out secret.data
53+
54+
# Capture the yaml output and verify that its the same as the name output
55+
loaded_key_name_yaml=`python << pyscript
56+
from __future__ import print_function
57+
58+
import yaml
59+
60+
with open('ak.out', 'r') as f:
61+
doc = yaml.safe_load(f)
62+
print(doc['loaded-key']['name'])
63+
pyscript`
64+
65+
test "$loaded_key_name_yaml" == "$loaded_key_name"
66+
67+
exit 0

tools/tpm2_makecredential.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -268,50 +268,52 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
268268
}
269269

270270
static void set_default_TCG_EK_template(TPMI_ALG_PUBLIC alg) {
271+
/* Values for RSA-2048 and ECC-256 keys. */
272+
TPMI_AES_KEY_BITS bits = 128;
273+
TPMI_ALG_HASH name_alg = TPM2_ALG_SHA256;
274+
275+
ctx.public.publicArea.objectAttributes = 0;
271276

272277
switch (alg) {
273278
case TPM2_ALG_RSA:
279+
if (ctx.public.publicArea.parameters.rsaDetail.keyBits > 2048) {
280+
/* Values for RSA-3072 and RSA-4096 keys. */
281+
bits = 256;
282+
name_alg = TPM2_ALG_SHA384;
283+
ctx.public.publicArea.objectAttributes = TPMA_OBJECT_USERWITHAUTH;
284+
}
274285
ctx.public.publicArea.parameters.rsaDetail.symmetric.algorithm =
275286
TPM2_ALG_AES;
276-
ctx.public.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
287+
ctx.public.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = bits;
277288
ctx.public.publicArea.parameters.rsaDetail.symmetric.mode.aes =
278289
TPM2_ALG_CFB;
279-
ctx.public.publicArea.parameters.rsaDetail.scheme.scheme = TPM2_ALG_NULL;
280-
ctx.public.publicArea.parameters.rsaDetail.keyBits = 2048;
281-
ctx.public.publicArea.parameters.rsaDetail.exponent = 0;
282-
ctx.public.publicArea.unique.rsa.size = 256;
283290
break;
284291
case TPM2_ALG_ECC:
292+
if (ctx.public.publicArea.unique.ecc.x.size > 32) {
293+
/* Values for ECC-384 keys. */
294+
bits = 256;
295+
name_alg = TPM2_ALG_SHA384;
296+
ctx.public.publicArea.objectAttributes = TPMA_OBJECT_USERWITHAUTH;
297+
}
298+
if (ctx.public.publicArea.unique.ecc.x.size > 48) {
299+
/* Values for ECC-512 keys. */
300+
name_alg = TPM2_ALG_SHA512;
301+
}
302+
285303
ctx.public.publicArea.parameters.eccDetail.symmetric.algorithm =
286304
TPM2_ALG_AES;
287-
ctx.public.publicArea.parameters.eccDetail.symmetric.keyBits.aes = 128;
305+
ctx.public.publicArea.parameters.eccDetail.symmetric.keyBits.aes = bits;
288306
ctx.public.publicArea.parameters.eccDetail.symmetric.mode.sym =
289307
TPM2_ALG_CFB;
290-
ctx.public.publicArea.parameters.eccDetail.scheme.scheme = TPM2_ALG_NULL;
291-
ctx.public.publicArea.parameters.eccDetail.curveID = TPM2_ECC_NIST_P256;
292-
ctx.public.publicArea.parameters.eccDetail.kdf.scheme = TPM2_ALG_NULL;
293-
ctx.public.publicArea.unique.ecc.x.size = 32;
294-
ctx.public.publicArea.unique.ecc.y.size = 32;
295308
break;
296309
}
297310

298-
ctx.public.publicArea.objectAttributes =
311+
ctx.public.publicArea.objectAttributes |=
299312
TPMA_OBJECT_RESTRICTED | TPMA_OBJECT_ADMINWITHPOLICY
300313
| TPMA_OBJECT_DECRYPT | TPMA_OBJECT_FIXEDTPM
301314
| TPMA_OBJECT_FIXEDPARENT | TPMA_OBJECT_SENSITIVEDATAORIGIN;
302315

303-
static const TPM2B_DIGEST auth_policy = {
304-
.size = 32,
305-
.buffer = {
306-
0x83, 0x71, 0x97, 0x67, 0x44, 0x84, 0xB3, 0xF8, 0x1A, 0x90, 0xCC,
307-
0x8D, 0x46, 0xA5, 0xD7, 0x24, 0xFD, 0x52, 0xD7, 0x6E, 0x06, 0x52,
308-
0x0B, 0x64, 0xF2, 0xA1, 0xDA, 0x1B, 0x33, 0x14, 0x69, 0xAA
309-
}
310-
};
311-
TPM2B_DIGEST *authp = &ctx.public.publicArea.authPolicy;
312-
*authp = auth_policy;
313-
314-
ctx.public.publicArea.nameAlg = TPM2_ALG_SHA256;
316+
ctx.public.publicArea.nameAlg = name_alg;
315317
}
316318

317319
static tool_rc process_input(tpm2_option_flags flags) {

0 commit comments

Comments
 (0)