1*758e9fbaSOystein Eftevaag /* SPDX-License-Identifier: BSD-2-Clause */
2*758e9fbaSOystein Eftevaag /*******************************************************************************
3*758e9fbaSOystein Eftevaag * Copyright 2018-2019, Fraunhofer SIT sponsored by Infineon Technologies AG
4*758e9fbaSOystein Eftevaag * All rights reserved.
5*758e9fbaSOystein Eftevaag ******************************************************************************/
6*758e9fbaSOystein Eftevaag
7*758e9fbaSOystein Eftevaag #ifdef HAVE_CONFIG_H
8*758e9fbaSOystein Eftevaag #include <config.h>
9*758e9fbaSOystein Eftevaag #endif
10*758e9fbaSOystein Eftevaag
11*758e9fbaSOystein Eftevaag #include <stdio.h>
12*758e9fbaSOystein Eftevaag #include <string.h>
13*758e9fbaSOystein Eftevaag
14*758e9fbaSOystein Eftevaag #include "tss2_fapi.h"
15*758e9fbaSOystein Eftevaag #include "fapi_int.h"
16*758e9fbaSOystein Eftevaag #include "fapi_util.h"
17*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
18*758e9fbaSOystein Eftevaag #define LOGMODULE fapi
19*758e9fbaSOystein Eftevaag #include "util/log.h"
20*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
21*758e9fbaSOystein Eftevaag
22*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_Quote
23*758e9fbaSOystein Eftevaag *
24*758e9fbaSOystein Eftevaag * Given a set of PCRs and a restricted signing key, it will sign those PCRs and
25*758e9fbaSOystein Eftevaag * return the quote.
26*758e9fbaSOystein Eftevaag *
27*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
28*758e9fbaSOystein Eftevaag * @param[in] pcrList The list of PCRs that are to be quoted
29*758e9fbaSOystein Eftevaag * @param[in] pcrListSize The size of pcrList in bytes
30*758e9fbaSOystein Eftevaag * @param[in] keyPath The path to the signing key
31*758e9fbaSOystein Eftevaag * @param[in] quoteType The type of quote. May be NULL
32*758e9fbaSOystein Eftevaag * @param[in] qualifyingData A nonce provided by the caller. May be NULL
33*758e9fbaSOystein Eftevaag * @param[in] qualifyingDataSize The size of qualifyingData in bytes. Must be 0
34*758e9fbaSOystein Eftevaag * if qualifyingData is NULL
35*758e9fbaSOystein Eftevaag * @param[out] quoteInfo A JSON-encoded structure holding the inputs to the
36*758e9fbaSOystein Eftevaag * quote operation
37*758e9fbaSOystein Eftevaag * @param[out] signature The signature of the PCRs
38*758e9fbaSOystein Eftevaag * @param[out] signatureSize The size of the signature in bytes. May be NULL
39*758e9fbaSOystein Eftevaag * @param[out] pcrLog The log of the PCR. May be NULL
40*758e9fbaSOystein Eftevaag * @param[out] certificate The certificate associated with the signing key. May
41*758e9fbaSOystein Eftevaag * be NULL
42*758e9fbaSOystein Eftevaag *
43*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
44*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, pcrList, keyPath,
45*758e9fbaSOystein Eftevaag * quoteInfo or signature is NULL.
46*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
47*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if path does not map to a FAPI entity.
48*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_KEY: if the entity at path is not a key, or is a key
49*758e9fbaSOystein Eftevaag * that is unsuitable for the requested operation.
50*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE: if qualifyingData is invalid or if
51*758e9fbaSOystein Eftevaag * qualifyingDataSize is zero.
52*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
53*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
54*758e9fbaSOystein Eftevaag * operation already pending.
55*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
56*758e9fbaSOystein Eftevaag * internal operations or return parameters.
57*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
58*758e9fbaSOystein Eftevaag * config file.
59*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
60*758e9fbaSOystein Eftevaag * this function needs to be called again.
61*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
62*758e9fbaSOystein Eftevaag * during authorization.
63*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
64*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
65*758e9fbaSOystein Eftevaag * is not set.
66*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
67*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
68*758e9fbaSOystein Eftevaag * was not successful.
69*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
70*758e9fbaSOystein Eftevaag */
71*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Quote(FAPI_CONTEXT * context,uint32_t * pcrList,size_t pcrListSize,char const * keyPath,char const * quoteType,uint8_t const * qualifyingData,size_t qualifyingDataSize,char ** quoteInfo,uint8_t ** signature,size_t * signatureSize,char ** pcrLog,char ** certificate)72*758e9fbaSOystein Eftevaag Fapi_Quote(
73*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
74*758e9fbaSOystein Eftevaag uint32_t *pcrList,
75*758e9fbaSOystein Eftevaag size_t pcrListSize,
76*758e9fbaSOystein Eftevaag char const *keyPath,
77*758e9fbaSOystein Eftevaag char const *quoteType,
78*758e9fbaSOystein Eftevaag uint8_t const *qualifyingData,
79*758e9fbaSOystein Eftevaag size_t qualifyingDataSize,
80*758e9fbaSOystein Eftevaag char **quoteInfo,
81*758e9fbaSOystein Eftevaag uint8_t **signature,
82*758e9fbaSOystein Eftevaag size_t *signatureSize,
83*758e9fbaSOystein Eftevaag char **pcrLog,
84*758e9fbaSOystein Eftevaag char **certificate)
85*758e9fbaSOystein Eftevaag {
86*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
87*758e9fbaSOystein Eftevaag
88*758e9fbaSOystein Eftevaag TSS2_RC r, r2;
89*758e9fbaSOystein Eftevaag
90*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
91*758e9fbaSOystein Eftevaag check_not_null(context);
92*758e9fbaSOystein Eftevaag check_not_null(pcrList);
93*758e9fbaSOystein Eftevaag check_not_null(keyPath);
94*758e9fbaSOystein Eftevaag check_not_null(quoteInfo);
95*758e9fbaSOystein Eftevaag check_not_null(signature);
96*758e9fbaSOystein Eftevaag
97*758e9fbaSOystein Eftevaag /* Check whether TCTI and ESYS are initialized */
98*758e9fbaSOystein Eftevaag return_if_null(context->esys, "Command can't be executed in none TPM mode.",
99*758e9fbaSOystein Eftevaag TSS2_FAPI_RC_NO_TPM);
100*758e9fbaSOystein Eftevaag
101*758e9fbaSOystein Eftevaag /* If the async state automata of FAPI shall be tested, then we must not set
102*758e9fbaSOystein Eftevaag the timeouts of ESYS to blocking mode.
103*758e9fbaSOystein Eftevaag During testing, the mssim tcti will ensure multiple re-invocations.
104*758e9fbaSOystein Eftevaag Usually however the synchronous invocations of FAPI shall instruct ESYS
105*758e9fbaSOystein Eftevaag to block until a result is available. */
106*758e9fbaSOystein Eftevaag #ifndef TEST_FAPI_ASYNC
107*758e9fbaSOystein Eftevaag r = Esys_SetTimeout(context->esys, TSS2_TCTI_TIMEOUT_BLOCK);
108*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Set Timeout to blocking");
109*758e9fbaSOystein Eftevaag #endif /* TEST_FAPI_ASYNC */
110*758e9fbaSOystein Eftevaag
111*758e9fbaSOystein Eftevaag r = Fapi_Quote_Async(context, pcrList, pcrListSize, keyPath, quoteType,
112*758e9fbaSOystein Eftevaag qualifyingData, qualifyingDataSize);
113*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "PCR_Quote");
114*758e9fbaSOystein Eftevaag
115*758e9fbaSOystein Eftevaag do {
116*758e9fbaSOystein Eftevaag /* We wait for file I/O to be ready if the FAPI state automata
117*758e9fbaSOystein Eftevaag are in a file I/O state. */
118*758e9fbaSOystein Eftevaag r = ifapi_io_poll(&context->io);
119*758e9fbaSOystein Eftevaag return_if_error(r, "Something went wrong with IO polling");
120*758e9fbaSOystein Eftevaag
121*758e9fbaSOystein Eftevaag /* Repeatedly call the finish function, until FAPI has transitioned
122*758e9fbaSOystein Eftevaag through all execution stages / states of this invocation. */
123*758e9fbaSOystein Eftevaag r = Fapi_Quote_Finish(context, quoteInfo, signature, signatureSize,
124*758e9fbaSOystein Eftevaag pcrLog, certificate);
125*758e9fbaSOystein Eftevaag } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
126*758e9fbaSOystein Eftevaag
127*758e9fbaSOystein Eftevaag /* Reset the ESYS timeout to non-blocking, immediate response. */
128*758e9fbaSOystein Eftevaag r2 = Esys_SetTimeout(context->esys, 0);
129*758e9fbaSOystein Eftevaag return_if_error(r2, "Set Timeout to non-blocking");
130*758e9fbaSOystein Eftevaag
131*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "PCR_Quote");
132*758e9fbaSOystein Eftevaag
133*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
134*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
135*758e9fbaSOystein Eftevaag }
136*758e9fbaSOystein Eftevaag
137*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_Quote
138*758e9fbaSOystein Eftevaag *
139*758e9fbaSOystein Eftevaag * Given a set of PCRs and a restricted signing key, it will sign those PCRs and
140*758e9fbaSOystein Eftevaag * return the quote.
141*758e9fbaSOystein Eftevaag *
142*758e9fbaSOystein Eftevaag * Call Fapi_Quote_Finish to finish the execution of this command.
143*758e9fbaSOystein Eftevaag *
144*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
145*758e9fbaSOystein Eftevaag * @param[in] pcrList The list of PCRs that are to be quoted
146*758e9fbaSOystein Eftevaag * @param[in] pcrListSize The size of pcrList in bytes
147*758e9fbaSOystein Eftevaag * @param[in] keyPath The path to the signing key
148*758e9fbaSOystein Eftevaag * @param[in] quoteType The type of quote. May be NULL
149*758e9fbaSOystein Eftevaag * @param[in] qualifyingData A nonce provided by the caller. May be NULL
150*758e9fbaSOystein Eftevaag * @param[in] qualifyingDataSize The size of qualifyingData in bytes. Must be 0
151*758e9fbaSOystein Eftevaag * if qualifyingData is NULL
152*758e9fbaSOystein Eftevaag *
153*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
154*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, pcrList or keyPath
155*758e9fbaSOystein Eftevaag * is NULL.
156*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
157*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if path does not map to a FAPI entity.
158*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_KEY: if the entity at path is not a key, or is a key
159*758e9fbaSOystein Eftevaag * that is unsuitable for the requested operation.
160*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE: if pcrListSize is 0, qualifyingData is
161*758e9fbaSOystein Eftevaag * invalid or if qualifyingDataSize is zero.
162*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
163*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
164*758e9fbaSOystein Eftevaag * operation already pending.
165*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
166*758e9fbaSOystein Eftevaag * internal operations or return parameters.
167*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
168*758e9fbaSOystein Eftevaag * config file.
169*758e9fbaSOystein Eftevaag */
170*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Quote_Async(FAPI_CONTEXT * context,uint32_t * pcrList,size_t pcrListSize,char const * keyPath,char const * quoteType,uint8_t const * qualifyingData,size_t qualifyingDataSize)171*758e9fbaSOystein Eftevaag Fapi_Quote_Async(
172*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
173*758e9fbaSOystein Eftevaag uint32_t *pcrList,
174*758e9fbaSOystein Eftevaag size_t pcrListSize,
175*758e9fbaSOystein Eftevaag char const *keyPath,
176*758e9fbaSOystein Eftevaag char const *quoteType,
177*758e9fbaSOystein Eftevaag uint8_t const *qualifyingData,
178*758e9fbaSOystein Eftevaag size_t qualifyingDataSize)
179*758e9fbaSOystein Eftevaag {
180*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
181*758e9fbaSOystein Eftevaag LOG_TRACE("pcrListSize: %zi", pcrListSize);
182*758e9fbaSOystein Eftevaag for (size_t i = 0; i < pcrListSize; i++) {
183*758e9fbaSOystein Eftevaag LOG_TRACE("PCR list entry %zu: %ul", i, pcrList[i]);
184*758e9fbaSOystein Eftevaag }
185*758e9fbaSOystein Eftevaag LOG_TRACE("keyPath: %s", keyPath);
186*758e9fbaSOystein Eftevaag LOG_TRACE("quoteType: %s", quoteType);
187*758e9fbaSOystein Eftevaag if (qualifyingData) {
188*758e9fbaSOystein Eftevaag LOGBLOB_TRACE(qualifyingData, qualifyingDataSize, "qualifyingData");
189*758e9fbaSOystein Eftevaag } else {
190*758e9fbaSOystein Eftevaag LOG_TRACE("qualifyingData: (null) qualifyingDataSize: %zi", qualifyingDataSize);
191*758e9fbaSOystein Eftevaag }
192*758e9fbaSOystein Eftevaag
193*758e9fbaSOystein Eftevaag TSS2_RC r;
194*758e9fbaSOystein Eftevaag
195*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
196*758e9fbaSOystein Eftevaag check_not_null(context);
197*758e9fbaSOystein Eftevaag check_not_null(pcrList);
198*758e9fbaSOystein Eftevaag check_not_null(keyPath);
199*758e9fbaSOystein Eftevaag
200*758e9fbaSOystein Eftevaag /* Check for invalid parameters */
201*758e9fbaSOystein Eftevaag if (pcrListSize == 0) {
202*758e9fbaSOystein Eftevaag LOG_ERROR("pcrListSize must not be NULL");
203*758e9fbaSOystein Eftevaag return TSS2_FAPI_RC_BAD_VALUE;
204*758e9fbaSOystein Eftevaag }
205*758e9fbaSOystein Eftevaag if (qualifyingData == NULL && qualifyingDataSize) {
206*758e9fbaSOystein Eftevaag LOG_ERROR("QualifyingData is NULL but qualifyingDataSize is not 0");
207*758e9fbaSOystein Eftevaag return TSS2_FAPI_RC_BAD_VALUE;
208*758e9fbaSOystein Eftevaag }
209*758e9fbaSOystein Eftevaag
210*758e9fbaSOystein Eftevaag /* Helpful alias pointers */
211*758e9fbaSOystein Eftevaag IFAPI_PCR * command = &context->cmd.pcr;
212*758e9fbaSOystein Eftevaag
213*758e9fbaSOystein Eftevaag /* Reset all context-internal session state information. */
214*758e9fbaSOystein Eftevaag r = ifapi_session_init(context);
215*758e9fbaSOystein Eftevaag return_if_error(r, "Initialize Quote");
216*758e9fbaSOystein Eftevaag
217*758e9fbaSOystein Eftevaag if (quoteType && strcmp(quoteType, "TPM-Quote") != 0) {
218*758e9fbaSOystein Eftevaag return_error(TSS2_FAPI_RC_BAD_VALUE,
219*758e9fbaSOystein Eftevaag "Only quote type TPM-Quote is allowed");
220*758e9fbaSOystein Eftevaag }
221*758e9fbaSOystein Eftevaag
222*758e9fbaSOystein Eftevaag /* Store parameters in context */
223*758e9fbaSOystein Eftevaag strdup_check(command->keyPath, keyPath, r, error_cleanup);
224*758e9fbaSOystein Eftevaag
225*758e9fbaSOystein Eftevaag command->pcrList = malloc(pcrListSize * sizeof(TPM2_HANDLE));
226*758e9fbaSOystein Eftevaag goto_if_null2(command->pcrList, "Out of memory", r, TSS2_FAPI_RC_MEMORY,
227*758e9fbaSOystein Eftevaag error_cleanup);
228*758e9fbaSOystein Eftevaag memcpy(command->pcrList, pcrList, pcrListSize);
229*758e9fbaSOystein Eftevaag
230*758e9fbaSOystein Eftevaag command->pcrListSize = pcrListSize;
231*758e9fbaSOystein Eftevaag command->tpm_quoted = NULL;
232*758e9fbaSOystein Eftevaag if (qualifyingData != NULL) {
233*758e9fbaSOystein Eftevaag FAPI_COPY_DIGEST(&command->qualifyingData.buffer[0],
234*758e9fbaSOystein Eftevaag command->qualifyingData.size, qualifyingData, qualifyingDataSize);
235*758e9fbaSOystein Eftevaag } else {
236*758e9fbaSOystein Eftevaag command->qualifyingData.size = 0;
237*758e9fbaSOystein Eftevaag }
238*758e9fbaSOystein Eftevaag
239*758e9fbaSOystein Eftevaag /* Initialize the context state for this operation. */
240*758e9fbaSOystein Eftevaag context->state = PCR_QUOTE_WAIT_FOR_GET_CAP;
241*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
242*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
243*758e9fbaSOystein Eftevaag
244*758e9fbaSOystein Eftevaag error_cleanup:
245*758e9fbaSOystein Eftevaag /* Cleanup duplicated input parameters that were copied before. */
246*758e9fbaSOystein Eftevaag SAFE_FREE(command->keyPath);
247*758e9fbaSOystein Eftevaag SAFE_FREE(command->pcrList);
248*758e9fbaSOystein Eftevaag return r;
249*758e9fbaSOystein Eftevaag }
250*758e9fbaSOystein Eftevaag
251*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_Quote
252*758e9fbaSOystein Eftevaag *
253*758e9fbaSOystein Eftevaag * This function should be called after a previous Fapi_Quote_Async.
254*758e9fbaSOystein Eftevaag *
255*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
256*758e9fbaSOystein Eftevaag * @param[out] quoteInfo A JSON-encoded structure holding the inputs to the
257*758e9fbaSOystein Eftevaag * quote operation
258*758e9fbaSOystein Eftevaag * @param[out] signature The signature of the PCRs
259*758e9fbaSOystein Eftevaag * @param[out] signatureSize The size of the signature in bytes. May be NULL
260*758e9fbaSOystein Eftevaag * @param[out] pcrLog The log of the PCR. May be NULL
261*758e9fbaSOystein Eftevaag * @param[out] certificate The certificate associated with the signing key. May
262*758e9fbaSOystein Eftevaag * be NULL
263*758e9fbaSOystein Eftevaag *
264*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
265*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, quoteInfor or signature
266*758e9fbaSOystein Eftevaag * is NULL.
267*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
268*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
269*758e9fbaSOystein Eftevaag * operation already pending.
270*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
271*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
272*758e9fbaSOystein Eftevaag * internal operations or return parameters.
273*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
274*758e9fbaSOystein Eftevaag * complete. Call this function again later.
275*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
276*758e9fbaSOystein Eftevaag * the function.
277*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
278*758e9fbaSOystein Eftevaag * during authorization.
279*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
280*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
281*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
282*758e9fbaSOystein Eftevaag * is not set.
283*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
284*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
285*758e9fbaSOystein Eftevaag * was not successful.
286*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
287*758e9fbaSOystein Eftevaag */
288*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Quote_Finish(FAPI_CONTEXT * context,char ** quoteInfo,uint8_t ** signature,size_t * signatureSize,char ** pcrLog,char ** certificate)289*758e9fbaSOystein Eftevaag Fapi_Quote_Finish(
290*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
291*758e9fbaSOystein Eftevaag char **quoteInfo,
292*758e9fbaSOystein Eftevaag uint8_t **signature,
293*758e9fbaSOystein Eftevaag size_t *signatureSize,
294*758e9fbaSOystein Eftevaag char **pcrLog,
295*758e9fbaSOystein Eftevaag char **certificate)
296*758e9fbaSOystein Eftevaag {
297*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
298*758e9fbaSOystein Eftevaag
299*758e9fbaSOystein Eftevaag TSS2_RC r;
300*758e9fbaSOystein Eftevaag IFAPI_OBJECT *sig_key_object;
301*758e9fbaSOystein Eftevaag const IFAPI_PROFILE *profile;
302*758e9fbaSOystein Eftevaag ESYS_TR auth_session;
303*758e9fbaSOystein Eftevaag
304*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
305*758e9fbaSOystein Eftevaag check_not_null(context);
306*758e9fbaSOystein Eftevaag check_not_null(quoteInfo);
307*758e9fbaSOystein Eftevaag check_not_null(signature);
308*758e9fbaSOystein Eftevaag
309*758e9fbaSOystein Eftevaag /* Helpful alias pointers */
310*758e9fbaSOystein Eftevaag IFAPI_PCR * command = &context->cmd.pcr;
311*758e9fbaSOystein Eftevaag
312*758e9fbaSOystein Eftevaag switch (context->state) {
313*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_WAIT_FOR_GET_CAP);
314*758e9fbaSOystein Eftevaag command->pcr_selection = context->profiles.default_profile.pcr_selection;
315*758e9fbaSOystein Eftevaag
316*758e9fbaSOystein Eftevaag r = ifapi_filter_pcr_selection_by_index(&command->pcr_selection,
317*758e9fbaSOystein Eftevaag command->pcrList,
318*758e9fbaSOystein Eftevaag command->pcrListSize);
319*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "Filtering banks for PCR list.", error_cleanup);
320*758e9fbaSOystein Eftevaag
321*758e9fbaSOystein Eftevaag /* Get a session for authorization of the quote operation. */
322*758e9fbaSOystein Eftevaag r = ifapi_get_sessions_async(context,
323*758e9fbaSOystein Eftevaag IFAPI_SESSION_GENEK | IFAPI_SESSION1,
324*758e9fbaSOystein Eftevaag TPMA_SESSION_DECRYPT, 0);
325*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "Create sessions", error_cleanup);
326*758e9fbaSOystein Eftevaag
327*758e9fbaSOystein Eftevaag fallthrough;
328*758e9fbaSOystein Eftevaag
329*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_WAIT_FOR_SESSION);
330*758e9fbaSOystein Eftevaag /* Retrieve the profile information. */
331*758e9fbaSOystein Eftevaag r = ifapi_profiles_get(&context->profiles, command->keyPath, &profile);
332*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, " FAPI create session", error_cleanup);
333*758e9fbaSOystein Eftevaag
334*758e9fbaSOystein Eftevaag r = ifapi_get_sessions_finish(context, profile, profile->nameAlg);
335*758e9fbaSOystein Eftevaag return_try_again(r);
336*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, " FAPI create session", error_cleanup);
337*758e9fbaSOystein Eftevaag
338*758e9fbaSOystein Eftevaag /* Load the key into the TPM. */
339*758e9fbaSOystein Eftevaag r = ifapi_load_keys_async(context, command->keyPath);
340*758e9fbaSOystein Eftevaag goto_if_error(r, "Load keys.", error_cleanup);
341*758e9fbaSOystein Eftevaag
342*758e9fbaSOystein Eftevaag fallthrough;
343*758e9fbaSOystein Eftevaag
344*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_WAIT_FOR_KEY);
345*758e9fbaSOystein Eftevaag r = ifapi_load_keys_finish(context, IFAPI_FLUSH_PARENT,
346*758e9fbaSOystein Eftevaag &command->handle,
347*758e9fbaSOystein Eftevaag &command->key_object);
348*758e9fbaSOystein Eftevaag return_try_again(r);
349*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, " Load key.", error_cleanup);
350*758e9fbaSOystein Eftevaag
351*758e9fbaSOystein Eftevaag fallthrough;
352*758e9fbaSOystein Eftevaag
353*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_AUTHORIZE);
354*758e9fbaSOystein Eftevaag /* Authorize the session for use with the key. */
355*758e9fbaSOystein Eftevaag r = ifapi_authorize_object(context, command->key_object, &auth_session);
356*758e9fbaSOystein Eftevaag return_try_again(r);
357*758e9fbaSOystein Eftevaag goto_if_error(r, "Authorize key.", error_cleanup);
358*758e9fbaSOystein Eftevaag
359*758e9fbaSOystein Eftevaag /* Perform the Quote operation. */
360*758e9fbaSOystein Eftevaag r = Esys_Quote_Async(context->esys, command->handle,
361*758e9fbaSOystein Eftevaag auth_session, ESYS_TR_NONE, ESYS_TR_NONE,
362*758e9fbaSOystein Eftevaag &command->qualifyingData,
363*758e9fbaSOystein Eftevaag &command->key_object->misc.key.signing_scheme,
364*758e9fbaSOystein Eftevaag &command->pcr_selection);
365*758e9fbaSOystein Eftevaag goto_if_error(r, "Error: PCR_Quote", error_cleanup);
366*758e9fbaSOystein Eftevaag
367*758e9fbaSOystein Eftevaag fallthrough;
368*758e9fbaSOystein Eftevaag
369*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_AUTH_SENT);
370*758e9fbaSOystein Eftevaag command->tpm_signature = NULL;
371*758e9fbaSOystein Eftevaag SAFE_FREE(command->tpm_signature);
372*758e9fbaSOystein Eftevaag r = Esys_Quote_Finish(context->esys, &command->tpm_quoted,
373*758e9fbaSOystein Eftevaag &command->tpm_signature);
374*758e9fbaSOystein Eftevaag return_try_again(r);
375*758e9fbaSOystein Eftevaag goto_if_error(r, "Error: PCR_Quote", error_cleanup);
376*758e9fbaSOystein Eftevaag
377*758e9fbaSOystein Eftevaag /* Flush the key used for the quote. */
378*758e9fbaSOystein Eftevaag r = Esys_FlushContext_Async(context->esys, command->handle);
379*758e9fbaSOystein Eftevaag goto_if_error(r, "Error: FlushContext", error_cleanup);
380*758e9fbaSOystein Eftevaag
381*758e9fbaSOystein Eftevaag fallthrough;
382*758e9fbaSOystein Eftevaag
383*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_WAIT_FOR_FLUSH);
384*758e9fbaSOystein Eftevaag r = Esys_FlushContext_Finish(context->esys);
385*758e9fbaSOystein Eftevaag return_try_again(r);
386*758e9fbaSOystein Eftevaag goto_if_error(r, "Error: Sign", error_cleanup);
387*758e9fbaSOystein Eftevaag
388*758e9fbaSOystein Eftevaag sig_key_object = command->key_object;
389*758e9fbaSOystein Eftevaag /* Convert the TPM-encoded signature into something useful for the caller. */
390*758e9fbaSOystein Eftevaag r = ifapi_tpm_to_fapi_signature(sig_key_object,
391*758e9fbaSOystein Eftevaag command->tpm_signature,
392*758e9fbaSOystein Eftevaag signature, signatureSize);
393*758e9fbaSOystein Eftevaag SAFE_FREE(command->tpm_signature);
394*758e9fbaSOystein Eftevaag goto_if_error(r, "Create FAPI signature.", error_cleanup);
395*758e9fbaSOystein Eftevaag
396*758e9fbaSOystein Eftevaag /* Compute the quote info; i.e. the data that was actually
397*758e9fbaSOystein Eftevaag signed by the TPM. */
398*758e9fbaSOystein Eftevaag r = ifapi_compute_quote_info(sig_key_object,
399*758e9fbaSOystein Eftevaag command->tpm_quoted,
400*758e9fbaSOystein Eftevaag quoteInfo);
401*758e9fbaSOystein Eftevaag goto_if_error(r, "Create compute quote info.", error_cleanup);
402*758e9fbaSOystein Eftevaag
403*758e9fbaSOystein Eftevaag /* Return the key's certificate if requested. */
404*758e9fbaSOystein Eftevaag if (certificate) {
405*758e9fbaSOystein Eftevaag strdup_check(*certificate, sig_key_object->misc.key.certificate, r, error_cleanup);
406*758e9fbaSOystein Eftevaag }
407*758e9fbaSOystein Eftevaag
408*758e9fbaSOystein Eftevaag /* If the pcrLog was not requested, the operation is done. */
409*758e9fbaSOystein Eftevaag if (!pcrLog) {
410*758e9fbaSOystein Eftevaag context->state = PCR_QUOTE_CLEANUP;
411*758e9fbaSOystein Eftevaag return TSS2_FAPI_RC_TRY_AGAIN;
412*758e9fbaSOystein Eftevaag }
413*758e9fbaSOystein Eftevaag
414*758e9fbaSOystein Eftevaag /* Retrieve the eventlog for the PCRs for the quote. */
415*758e9fbaSOystein Eftevaag r = ifapi_eventlog_get_async(&context->eventlog, &context->io,
416*758e9fbaSOystein Eftevaag command->pcrList,
417*758e9fbaSOystein Eftevaag command->pcrListSize);
418*758e9fbaSOystein Eftevaag goto_if_error(r, "Error getting event log", error_cleanup);
419*758e9fbaSOystein Eftevaag
420*758e9fbaSOystein Eftevaag fallthrough;
421*758e9fbaSOystein Eftevaag
422*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_READ_EVENT_LIST);
423*758e9fbaSOystein Eftevaag r = ifapi_eventlog_get_finish(&context->eventlog, &context->io, pcrLog);
424*758e9fbaSOystein Eftevaag return_try_again(r);
425*758e9fbaSOystein Eftevaag goto_if_error(r, "Error getting event log", error_cleanup);
426*758e9fbaSOystein Eftevaag fallthrough;
427*758e9fbaSOystein Eftevaag
428*758e9fbaSOystein Eftevaag statecase(context->state, PCR_QUOTE_CLEANUP)
429*758e9fbaSOystein Eftevaag /* Cleanup the session used for authorization. */
430*758e9fbaSOystein Eftevaag r = ifapi_cleanup_session(context);
431*758e9fbaSOystein Eftevaag try_again_or_error_goto(r, "Cleanup", error_cleanup);
432*758e9fbaSOystein Eftevaag
433*758e9fbaSOystein Eftevaag context->state = _FAPI_STATE_INIT;
434*758e9fbaSOystein Eftevaag break;
435*758e9fbaSOystein Eftevaag
436*758e9fbaSOystein Eftevaag statecasedefault(context->state);
437*758e9fbaSOystein Eftevaag }
438*758e9fbaSOystein Eftevaag
439*758e9fbaSOystein Eftevaag error_cleanup:
440*758e9fbaSOystein Eftevaag /* Cleanup any intermediate results and state stored in the context. */
441*758e9fbaSOystein Eftevaag SAFE_FREE(command->tpm_signature);
442*758e9fbaSOystein Eftevaag SAFE_FREE(command->tpm_quoted);
443*758e9fbaSOystein Eftevaag SAFE_FREE(command->keyPath);
444*758e9fbaSOystein Eftevaag SAFE_FREE(command->pcrList);
445*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
446*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(context->loadKey.key_object);
447*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
448*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(command->key_object);
449*758e9fbaSOystein Eftevaag ifapi_session_clean(context);
450*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
451*758e9fbaSOystein Eftevaag return r;
452*758e9fbaSOystein Eftevaag }
453