Skip to content

Commit 6f8a67f

Browse files
committed
F-2977 - https://fenrir.wolfssl.com/finding/2977 - Fix DecodeRsaDer/DecodeEccDer default attributes for private key imports
1 parent 75867cc commit 6f8a67f

2 files changed

Lines changed: 84 additions & 12 deletions

File tree

src/tpm2_wrap.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,13 +3761,21 @@ int wolfTPM2_DecodeRsaDer(const byte* der, word32 derSz,
37613761
XMEMSET(q, 0, sizeof(q));
37623762

37633763
if (attributes == 0) {
3764-
attributes = (TPMA_OBJECT_restricted |
3765-
TPMA_OBJECT_sensitiveDataOrigin |
3766-
TPMA_OBJECT_sign |
3767-
TPMA_OBJECT_userWithAuth |
3768-
TPMA_OBJECT_noDA);
37693764
if (sens != NULL) {
3770-
attributes |= TPMA_OBJECT_decrypt;
3765+
/* Imported private keys: restricted must not be set when both
3766+
* sign and decrypt are set (TPM 2.0 Part 2 Table 31), and
3767+
* sensitiveDataOrigin must not be set for imported keys */
3768+
attributes = (TPMA_OBJECT_sign |
3769+
TPMA_OBJECT_decrypt |
3770+
TPMA_OBJECT_userWithAuth |
3771+
TPMA_OBJECT_noDA);
3772+
}
3773+
else {
3774+
attributes = (TPMA_OBJECT_restricted |
3775+
TPMA_OBJECT_sensitiveDataOrigin |
3776+
TPMA_OBJECT_sign |
3777+
TPMA_OBJECT_userWithAuth |
3778+
TPMA_OBJECT_noDA);
37713779
}
37723780
}
37733781

@@ -3866,13 +3874,21 @@ int wolfTPM2_DecodeEccDer(const byte* der, word32 derSz, TPM2B_PUBLIC* pub,
38663874
XMEMSET(qy, 0, sizeof(qy));
38673875

38683876
if (attributes == 0) {
3869-
attributes = (TPMA_OBJECT_restricted |
3870-
TPMA_OBJECT_sensitiveDataOrigin |
3871-
TPMA_OBJECT_sign |
3872-
TPMA_OBJECT_userWithAuth |
3873-
TPMA_OBJECT_noDA);
38743877
if (sens != NULL) {
3875-
attributes |= TPMA_OBJECT_decrypt;
3878+
/* Imported private keys: restricted must not be set when both
3879+
* sign and decrypt are set (TPM 2.0 Part 2 Table 31), and
3880+
* sensitiveDataOrigin must not be set for imported keys */
3881+
attributes = (TPMA_OBJECT_sign |
3882+
TPMA_OBJECT_decrypt |
3883+
TPMA_OBJECT_userWithAuth |
3884+
TPMA_OBJECT_noDA);
3885+
}
3886+
else {
3887+
attributes = (TPMA_OBJECT_restricted |
3888+
TPMA_OBJECT_sensitiveDataOrigin |
3889+
TPMA_OBJECT_sign |
3890+
TPMA_OBJECT_userWithAuth |
3891+
TPMA_OBJECT_noDA);
38763892
}
38773893
}
38783894

tests/unit_tests.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,59 @@ static void test_wolfTPM2_KeyBlob(TPM_ALG_ID alg)
14411441
TPM2_GetAlgName(alg), rc == 0 ? "Passed" : "Failed");
14421442
}
14431443

1444+
/* Test DecodeRsaDer/DecodeEccDer default attributes for private key imports */
1445+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_ASN)
1446+
static void test_wolfTPM2_DecodeDer_DefaultAttribs(void)
1447+
{
1448+
#ifdef HAVE_ECC
1449+
int rc;
1450+
TPM2B_PUBLIC pub;
1451+
TPM2B_SENSITIVE sens;
1452+
TPMA_OBJECT attrs;
1453+
/* ECC P-256 private key DER (from certs/example-ecc256-key.der) */
1454+
static const byte eccKeyDer[] = {
1455+
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x45, 0xb6, 0x69, 0x02,
1456+
0x73, 0x9c, 0x6c, 0x85, 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7,
1457+
0xac, 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c, 0x28, 0xdc,
1458+
0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c, 0xa0, 0x0a, 0x06, 0x08, 0x2a,
1459+
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42,
1460+
0x00, 0x04, 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6, 0x4a,
1461+
0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36, 0xdb, 0x72, 0x2d, 0xce,
1462+
0x94, 0xea, 0x2b, 0xfa, 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8,
1463+
0x61, 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a, 0x31, 0x5b,
1464+
0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf, 0x18, 0xda, 0x91, 0x11, 0x02,
1465+
0x34, 0x86, 0xe8, 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
1466+
};
1467+
1468+
XMEMSET(&pub, 0, sizeof(pub));
1469+
XMEMSET(&sens, 0, sizeof(sens));
1470+
1471+
/* Call with attributes=0 and sens!=NULL (private key import) */
1472+
rc = wolfTPM2_DecodeEccDer(eccKeyDer, (word32)sizeof(eccKeyDer),
1473+
&pub, &sens, 0);
1474+
AssertIntEQ(rc, 0);
1475+
1476+
attrs = pub.publicArea.objectAttributes;
1477+
1478+
/* For imported private keys, restricted must NOT be set when both
1479+
* sign and decrypt are set (TPM 2.0 Part 2 Table 31) */
1480+
AssertIntEQ(attrs & TPMA_OBJECT_restricted, 0);
1481+
1482+
/* sensitiveDataOrigin must NOT be set for imported keys */
1483+
AssertIntEQ(attrs & TPMA_OBJECT_sensitiveDataOrigin, 0);
1484+
1485+
/* sign and decrypt should both be set for general-purpose imported keys */
1486+
AssertTrue(attrs & TPMA_OBJECT_sign);
1487+
AssertTrue(attrs & TPMA_OBJECT_decrypt);
1488+
1489+
/* userWithAuth should be set */
1490+
AssertTrue(attrs & TPMA_OBJECT_userWithAuth);
1491+
#endif
1492+
1493+
printf("Test TPM Wrapper:\tDecodeDer DefaultAttribs:\tPassed\n");
1494+
}
1495+
#endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_ASN */
1496+
14441497
/* Test NULL parentKey handling in LoadRsaPrivateKey_ex and LoadEccPrivateKey */
14451498
static void test_wolfTPM2_LoadPrivateKey_NullParent(void)
14461499
{
@@ -1521,6 +1574,9 @@ int unit_tests(int argc, char *argv[])
15211574
test_wolfTPM2_PCRPolicy();
15221575
#endif
15231576
test_wolfTPM2_EncryptSecret();
1577+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_ASN)
1578+
test_wolfTPM2_DecodeDer_DefaultAttribs();
1579+
#endif
15241580
test_wolfTPM2_LoadPrivateKey_NullParent();
15251581
test_wolfTPM2_KeyBlob(TPM_ALG_RSA);
15261582
test_wolfTPM2_KeyBlob(TPM_ALG_ECC);

0 commit comments

Comments
 (0)