@@ -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 */
14451498static 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