xref: /aosp_15_r20/external/tpm2-tss/test/integration/esys-policy-template-opt.int.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3  * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4  * All rights reserved.
5  *******************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include <stdlib.h>
12 
13 #include "tss2_esys.h"
14 #include "tss2_mu.h"
15 
16 #include "esys_iutil.h"
17 #include "test-esapi.h"
18 #define LOGMODULE test
19 #include "util/log.h"
20 #include "util/aux_util.h"
21 
22 #define FLUSH true
23 #define NOT_FLUSH false
24 
25 /*
26  * Function to compare policy digest with expected digest.
27  * The digest is computed with Esys_PolicyGetDigest.
28  */
29 bool
cmp_policy_digest(ESYS_CONTEXT * esys_context,ESYS_TR * session,TPM2B_DIGEST * expected_digest,char * comment,bool flush_session)30 cmp_policy_digest(ESYS_CONTEXT * esys_context,
31                   ESYS_TR * session,
32                   TPM2B_DIGEST * expected_digest,
33                   char *comment, bool flush_session)
34 {
35 
36     TSS2_RC r;
37     TPM2B_DIGEST *policyDigest;
38 
39     r = Esys_PolicyGetDigest(esys_context,
40                              *session,
41                              ESYS_TR_NONE,
42                              ESYS_TR_NONE, ESYS_TR_NONE, &policyDigest);
43     goto_if_error(r, "Error: PolicyGetDigest", error);
44 
45     LOGBLOB_DEBUG(&policyDigest->buffer[0], policyDigest->size,
46                   "POLICY DIGEST");
47 
48     if (policyDigest->size != 20
49         || memcmp(&policyDigest->buffer[0], &expected_digest->buffer[0],
50                   policyDigest->size)) {
51         free(policyDigest);
52         LOG_ERROR("Error: Policy%s digest did not match expected policy.",
53                   comment);
54         return false;
55     }
56     free(policyDigest);
57     if (flush_session) {
58         r = Esys_FlushContext(esys_context, *session);
59         goto_if_error(r, "Error: PolicyGetDigest", error);
60         *session = ESYS_TR_NONE;
61     }
62 
63     return true;
64 
65  error:
66     return false;
67 }
68 
69 /** This test is intended to test the ESAPI policy commands, not tested
70  *  in other test cases.
71  *  When possoble the commands are tested with a
72  * trial session and the policy digest is compared with the expected digest.
73  *
74  * Tested ESAPI commands:
75  *  - Esys_FlushContext() (M)
76  *  - Esys_PolicyTemplate() (F)
77  *
78  * @param[in,out] esys_context The ESYS_CONTEXT.
79  * @retval EXIT_FAILURE
80  * @retval EXIT_SKIP
81  * @retval EXIT_SUCCESS
82  */
83 int
test_esys_policy_template_opt(ESYS_CONTEXT * esys_context)84 test_esys_policy_template_opt(ESYS_CONTEXT * esys_context)
85 {
86     TSS2_RC r;
87     int failure_return = EXIT_FAILURE;
88 
89     /* Dummy parameters for trial sessoin  */
90     ESYS_TR sessionTrial = ESYS_TR_NONE;
91     TPMT_SYM_DEF symmetricTrial = {.algorithm = TPM2_ALG_AES,
92         .keyBits = {.aes = 128},
93         .mode = {.aes = TPM2_ALG_CFB}
94     };
95     TPM2B_NONCE nonceCallerTrial = {
96         .size = 20,
97         .buffer = {11, 12, 13, 14, 15, 16, 17, 18, 19, 11,
98                    21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
99     };
100 
101     /*
102      * Test PolicyTemplate
103      */
104     r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
105                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
106                               &nonceCallerTrial,
107                               TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
108                               &sessionTrial);
109     goto_if_error(r, "Error: During initialization of policy trial session",
110                   error);
111 
112     TPM2B_DIGEST templateHash = {
113         .size = 20,
114         .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
115                    11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
116     };
117 
118     r = Esys_PolicyTemplate(esys_context,
119                             sessionTrial,
120                             ESYS_TR_NONE,
121                             ESYS_TR_NONE, ESYS_TR_NONE, &templateHash);
122     if ((r == TPM2_RC_COMMAND_CODE) ||
123         (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
124         (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
125         LOG_WARNING("Command TPM2_PolicyTemplate  not supported by TPM.");
126         failure_return = EXIT_SKIP;
127         goto error;
128     } else {
129         goto_if_error(r, "Error: PolicyTemplate", error);
130 
131         TPM2B_DIGEST expectedPolicyTemplate = {
132             .size = 20,
133             .buffer =
134                 {0xf6, 0x6d, 0x2a, 0x9c, 0x6e, 0xa8, 0xdf, 0x1a, 0x49, 0x3c,
135                  0x42, 0xcc, 0xac, 0x6e, 0x3d, 0x08, 0xc0, 0x84, 0xcf, 0x73}
136         };
137 
138         if (!cmp_policy_digest
139             (esys_context, &sessionTrial, &expectedPolicyTemplate, "Template",
140              FLUSH))
141             goto error;
142     }
143 
144     return EXIT_SUCCESS;
145 
146  error:
147 
148     if (sessionTrial != ESYS_TR_NONE) {
149         if (Esys_FlushContext(esys_context, sessionTrial) != TSS2_RC_SUCCESS) {
150             LOG_ERROR("Cleanup sessionTrial failed.");
151         }
152     }
153     return failure_return;
154 }
155 
156 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)157 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
158     return test_esys_policy_template_opt(esys_context);
159 }
160