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