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