• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tss2_mu.h"
12 #include "tss2_sys.h"
13 #include "tss2_esys.h"
14 
15 #include "esys_types.h"
16 #include "esys_iutil.h"
17 #include "esys_mu.h"
18 #define LOGMODULE esys
19 #include "util/log.h"
20 #include "util/aux_util.h"
21 
22 /** One-Call function for TPM2_SetPrimaryPolicy
23  *
24  * This function invokes the TPM2_SetPrimaryPolicy command in a one-call
25  * variant. This means the function will block until the TPM response is
26  * available. All input parameters are const. The memory for non-simple output
27  * parameters is allocated by the function implementation.
28  *
29  * @param[in,out] esysContext The ESYS_CONTEXT.
30  * @param[in]  authHandle TPM2_RH_LOCKOUT, TPM2_RH_ENDORSEMENT, TPM2_RH_OWNER or
31  *             TPM2_RH_PLATFORM+{PP}.
32  * @param[in]  shandle1 Session handle for authorization of authHandle
33  * @param[in]  shandle2 Second session handle.
34  * @param[in]  shandle3 Third session handle.
35  * @param[in]  authPolicy An authorization policy digest; may be the Empty
36  *             Buffer.
37  * @param[in]  hashAlg The hash algorithm to use for the policy.
38  * @retval TSS2_RC_SUCCESS if the function call was a success.
39  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
40  *         pointers or required output handle references are NULL.
41  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
42  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
43  *         internal operations or return parameters.
44  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
45  *         operation already pending.
46  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
47  *          at least contain the tag, response length, and response code.
48  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
49  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
50            did not verify.
51  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
52  *         the 'decrypt' attribute bit set.
53  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
54  *         the 'encrypt' attribute bit set.
55  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
56  *         to the ESYS_CONTEXT or are of the wrong type or if required
57  *         ESYS_TR objects are ESYS_TR_NONE.
58  * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
59  *         'encrypt' attribute set and the command does not support encryption
60  *          of the first response parameter.
61  * @retval TSS2_RCs produced by lower layers of the software stack may be
62  *         returned to the caller unaltered unless handled internally.
63  */
64 TSS2_RC
Esys_SetPrimaryPolicy(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_DIGEST * authPolicy,TPMI_ALG_HASH hashAlg)65 Esys_SetPrimaryPolicy(
66     ESYS_CONTEXT *esysContext,
67     ESYS_TR authHandle,
68     ESYS_TR shandle1,
69     ESYS_TR shandle2,
70     ESYS_TR shandle3,
71     const TPM2B_DIGEST *authPolicy,
72     TPMI_ALG_HASH hashAlg)
73 {
74     TSS2_RC r;
75 
76     r = Esys_SetPrimaryPolicy_Async(esysContext, authHandle, shandle1, shandle2,
77                                     shandle3, authPolicy, hashAlg);
78     return_if_error(r, "Error in async function");
79 
80     /* Set the timeout to indefinite for now, since we want _Finish to block */
81     int32_t timeouttmp = esysContext->timeout;
82     esysContext->timeout = -1;
83     /*
84      * Now we call the finish function, until return code is not equal to
85      * from TSS2_BASE_RC_TRY_AGAIN.
86      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
87      * have set the timeout to -1. This occurs for example if the TPM requests
88      * a retransmission of the command via TPM2_RC_YIELDED.
89      */
90     do {
91         r = Esys_SetPrimaryPolicy_Finish(esysContext);
92         /* This is just debug information about the reattempt to finish the
93            command */
94         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
95             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
96                       " => resubmitting command", r);
97     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
98 
99     /* Restore the timeout value to the original value */
100     esysContext->timeout = timeouttmp;
101     return_if_error(r, "Esys Finish");
102 
103     return TSS2_RC_SUCCESS;
104 }
105 
106 /** Asynchronous function for TPM2_SetPrimaryPolicy
107  *
108  * This function invokes the TPM2_SetPrimaryPolicy command in a asynchronous
109  * variant. This means the function will return as soon as the command has been
110  * sent downwards the stack to the TPM. All input parameters are const.
111  * In order to retrieve the TPM's response call Esys_SetPrimaryPolicy_Finish.
112  *
113  * @param[in,out] esysContext The ESYS_CONTEXT.
114  * @param[in]  authHandle TPM2_RH_LOCKOUT, TPM2_RH_ENDORSEMENT, TPM2_RH_OWNER or
115  *             TPM2_RH_PLATFORM+{PP}.
116  * @param[in]  shandle1 Session handle for authorization of authHandle
117  * @param[in]  shandle2 Second session handle.
118  * @param[in]  shandle3 Third session handle.
119  * @param[in]  authPolicy An authorization policy digest; may be the Empty
120  *             Buffer.
121  * @param[in]  hashAlg The hash algorithm to use for the policy.
122  * @retval ESYS_RC_SUCCESS if the function call was a success.
123  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
124  *         pointers or required output handle references are NULL.
125  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
126  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
127  *         internal operations or return parameters.
128  * @retval TSS2_RCs produced by lower layers of the software stack may be
129            returned to the caller unaltered unless handled internally.
130  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
131  *         the 'decrypt' attribute bit set.
132  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
133  *         the 'encrypt' attribute bit set.
134  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
135  *         to the ESYS_CONTEXT or are of the wrong type or if required
136  *         ESYS_TR objects are ESYS_TR_NONE.
137  * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
138  *         'encrypt' attribute set and the command does not support encryption
139  *          of the first response parameter.
140  */
141 TSS2_RC
Esys_SetPrimaryPolicy_Async(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_DIGEST * authPolicy,TPMI_ALG_HASH hashAlg)142 Esys_SetPrimaryPolicy_Async(
143     ESYS_CONTEXT *esysContext,
144     ESYS_TR authHandle,
145     ESYS_TR shandle1,
146     ESYS_TR shandle2,
147     ESYS_TR shandle3,
148     const TPM2B_DIGEST *authPolicy,
149     TPMI_ALG_HASH hashAlg)
150 {
151     TSS2_RC r;
152     LOG_TRACE("context=%p, authHandle=%"PRIx32 ", authPolicy=%p,"
153               "hashAlg=%04"PRIx16"",
154               esysContext, authHandle, authPolicy, hashAlg);
155     TSS2L_SYS_AUTH_COMMAND auths;
156     RSRC_NODE_T *authHandleNode;
157 
158     /* Check context, sequence correctness and set state to error for now */
159     if (esysContext == NULL) {
160         LOG_ERROR("esyscontext is NULL.");
161         return TSS2_ESYS_RC_BAD_REFERENCE;
162     }
163     r = iesys_check_sequence_async(esysContext);
164     if (r != TSS2_RC_SUCCESS)
165         return r;
166     esysContext->state = _ESYS_STATE_INTERNALERROR;
167 
168     /* Check input parameters */
169     r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
170     return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
171 
172     /* Retrieve the metadata objects for provided handles */
173     r = esys_GetResourceObject(esysContext, authHandle, &authHandleNode);
174     return_state_if_error(r, _ESYS_STATE_INIT, "authHandle unknown.");
175 
176     /* Initial invocation of SAPI to prepare the command buffer with parameters */
177     r = Tss2_Sys_SetPrimaryPolicy_Prepare(esysContext->sys,
178                                           (authHandleNode == NULL) ? TPM2_RH_NULL
179                                            : authHandleNode->rsrc.handle,
180                                           authPolicy, hashAlg);
181     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
182 
183     /* Calculate the cpHash Values */
184     r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
185     return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
186     if (authHandleNode != NULL)
187         iesys_compute_session_value(esysContext->session_tab[0],
188                 &authHandleNode->rsrc.name, &authHandleNode->auth);
189     else
190         iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
191 
192     iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
193     iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
194 
195     /* Generate the auth values and set them in the SAPI command buffer */
196     r = iesys_gen_auths(esysContext, authHandleNode, NULL, NULL, &auths);
197     return_state_if_error(r, _ESYS_STATE_INIT,
198                           "Error in computation of auth values");
199 
200     esysContext->authsCount = auths.count;
201     if (auths.count > 0) {
202         r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
203         return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
204     }
205 
206     /* Trigger execution and finish the async invocation */
207     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
208     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
209                           "Finish (Execute Async)");
210 
211     esysContext->state = _ESYS_STATE_SENT;
212 
213     return r;
214 }
215 
216 /** Asynchronous finish function for TPM2_SetPrimaryPolicy
217  *
218  * This function returns the results of a TPM2_SetPrimaryPolicy command
219  * invoked via Esys_SetPrimaryPolicy_Finish. All non-simple output parameters
220  * are allocated by the function's implementation. NULL can be passed for every
221  * output parameter if the value is not required.
222  *
223  * @param[in,out] esysContext The ESYS_CONTEXT.
224  * @retval TSS2_RC_SUCCESS on success
225  * @retval ESYS_RC_SUCCESS if the function call was a success.
226  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
227  *         pointers or required output handle references are NULL.
228  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
229  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
230  *         internal operations or return parameters.
231  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
232  *         operation already pending.
233  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
234  *         TPM response is received.
235  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
236  *         at least contain the tag, response length, and response code.
237  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
238  *         not verify.
239  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
240  * @retval TSS2_RCs produced by lower layers of the software stack may be
241  *         returned to the caller unaltered unless handled internally.
242  */
243 TSS2_RC
Esys_SetPrimaryPolicy_Finish(ESYS_CONTEXT * esysContext)244 Esys_SetPrimaryPolicy_Finish(
245     ESYS_CONTEXT *esysContext)
246 {
247     TSS2_RC r;
248     LOG_TRACE("context=%p",
249               esysContext);
250 
251     if (esysContext == NULL) {
252         LOG_ERROR("esyscontext is NULL.");
253         return TSS2_ESYS_RC_BAD_REFERENCE;
254     }
255 
256     /* Check for correct sequence and set sequence to irregular for now */
257     if (esysContext->state != _ESYS_STATE_SENT &&
258         esysContext->state != _ESYS_STATE_RESUBMISSION) {
259         LOG_ERROR("Esys called in bad sequence.");
260         return TSS2_ESYS_RC_BAD_SEQUENCE;
261     }
262     esysContext->state = _ESYS_STATE_INTERNALERROR;
263 
264     /*Receive the TPM response and handle resubmissions if necessary. */
265     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
266     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
267         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
268         esysContext->state = _ESYS_STATE_SENT;
269         return r;
270     }
271     /* This block handle the resubmission of TPM commands given a certain set of
272      * TPM response codes. */
273     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
274         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
275             "resubmission: %" PRIx32, r);
276         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
277             LOG_WARNING("Maximum number of (re)submissions has been reached.");
278             esysContext->state = _ESYS_STATE_INIT;
279             return r;
280         }
281         esysContext->state = _ESYS_STATE_RESUBMISSION;
282         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
283         if (r != TSS2_RC_SUCCESS) {
284             LOG_WARNING("Error attempting to resubmit");
285             /* We do not set esysContext->state here but inherit the most recent
286              * state of the _async function. */
287             return r;
288         }
289         r = TSS2_ESYS_RC_TRY_AGAIN;
290         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
291         return r;
292     }
293     /* The following is the "regular error" handling. */
294     if (iesys_tpm_error(r)) {
295         LOG_WARNING("Received TPM Error");
296         esysContext->state = _ESYS_STATE_INIT;
297         return r;
298     } else if (r != TSS2_RC_SUCCESS) {
299         LOG_ERROR("Received a non-TPM Error");
300         esysContext->state = _ESYS_STATE_INTERNALERROR;
301         return r;
302     }
303 
304     /*
305      * Now the verification of the response (hmac check) and if necessary the
306      * parameter decryption have to be done.
307      */
308     r = iesys_check_response(esysContext);
309     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
310                           "Error: check response");
311 
312     /*
313      * After the verification of the response we call the complete function
314      * to deliver the result.
315      */
316     r = Tss2_Sys_SetPrimaryPolicy_Complete(esysContext->sys);
317     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
318                           "Received error from SAPI unmarshaling" );
319 
320     esysContext->state = _ESYS_STATE_INIT;
321 
322     return TSS2_RC_SUCCESS;
323 }
324