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