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 <stdlib.h>
12*758e9fbaSOystein Eftevaag #include <string.h>
13*758e9fbaSOystein Eftevaag #include <unistd.h>
14*758e9fbaSOystein Eftevaag #include <errno.h>
15*758e9fbaSOystein Eftevaag
16*758e9fbaSOystein Eftevaag #include "tss2_mu.h"
17*758e9fbaSOystein Eftevaag #include "tss2_fapi.h"
18*758e9fbaSOystein Eftevaag #include "fapi_int.h"
19*758e9fbaSOystein Eftevaag #include "fapi_util.h"
20*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
21*758e9fbaSOystein Eftevaag #include "ifapi_policy_json_serialize.h"
22*758e9fbaSOystein Eftevaag #define LOGMODULE fapi
23*758e9fbaSOystein Eftevaag #include "util/log.h"
24*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
25*758e9fbaSOystein Eftevaag
26*758e9fbaSOystein Eftevaag
27*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_GetTpmBlobs
28*758e9fbaSOystein Eftevaag *
29*758e9fbaSOystein Eftevaag * Get the public and private blobs of a TPM object. They can be loaded with a
30*758e9fbaSOystein Eftevaag * lower-level API such as the SAPI or the ESAPI.
31*758e9fbaSOystein Eftevaag *
32*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
33*758e9fbaSOystein Eftevaag * @param[in] path The path to the key for which the blobs will be returned
34*758e9fbaSOystein Eftevaag * @param[out] tpm2bPublic The returned public area of the object. May be NULL
35*758e9fbaSOystein Eftevaag * @param[out] tpm2bPublicSize The size of tpm2bPublic in bytes. May be NULL
36*758e9fbaSOystein Eftevaag * @param[out] tpm2bPrivate The returned private area of the object. May be
37*758e9fbaSOystein Eftevaag * NULL
38*758e9fbaSOystein Eftevaag * @param[out] tpm2bPrivateSize The size of tpm2bPrivate in bytes. May be NULL
39*758e9fbaSOystein Eftevaag * @param[out] policy The policy that is associated with the object encoded in
40*758e9fbaSOystein Eftevaag * JSON. May be NULL
41*758e9fbaSOystein Eftevaag *
42*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
43*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
44*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
45*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
46*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
47*758e9fbaSOystein Eftevaag * operation already pending.
48*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
49*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
50*758e9fbaSOystein Eftevaag * internal operations or return parameters.
51*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
52*758e9fbaSOystein Eftevaag * during authorization.
53*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
54*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
55*758e9fbaSOystein Eftevaag * the function.
56*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
57*758e9fbaSOystein Eftevaag * this function needs to be called again.
58*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
59*758e9fbaSOystein Eftevaag */
60*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_GetTpmBlobs(FAPI_CONTEXT * context,char const * path,uint8_t ** tpm2bPublic,size_t * tpm2bPublicSize,uint8_t ** tpm2bPrivate,size_t * tpm2bPrivateSize,char ** policy)61*758e9fbaSOystein Eftevaag Fapi_GetTpmBlobs(
62*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
63*758e9fbaSOystein Eftevaag char const *path,
64*758e9fbaSOystein Eftevaag uint8_t **tpm2bPublic,
65*758e9fbaSOystein Eftevaag size_t *tpm2bPublicSize,
66*758e9fbaSOystein Eftevaag uint8_t **tpm2bPrivate,
67*758e9fbaSOystein Eftevaag size_t *tpm2bPrivateSize,
68*758e9fbaSOystein Eftevaag char **policy)
69*758e9fbaSOystein Eftevaag {
70*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
71*758e9fbaSOystein Eftevaag
72*758e9fbaSOystein Eftevaag TSS2_RC r;
73*758e9fbaSOystein Eftevaag
74*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
75*758e9fbaSOystein Eftevaag check_not_null(context);
76*758e9fbaSOystein Eftevaag check_not_null(path);
77*758e9fbaSOystein Eftevaag
78*758e9fbaSOystein Eftevaag r = Fapi_GetTpmBlobs_Async(context, path);
79*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Entity_GetTPMBlobs");
80*758e9fbaSOystein Eftevaag
81*758e9fbaSOystein Eftevaag do {
82*758e9fbaSOystein Eftevaag /* We wait for file I/O to be ready if the FAPI state automata
83*758e9fbaSOystein Eftevaag are in a file I/O state. */
84*758e9fbaSOystein Eftevaag r = ifapi_io_poll(&context->io);
85*758e9fbaSOystein Eftevaag return_if_error(r, "Something went wrong with IO polling");
86*758e9fbaSOystein Eftevaag
87*758e9fbaSOystein Eftevaag /* Repeatedly call the finish function, until FAPI has transitioned
88*758e9fbaSOystein Eftevaag through all execution stages / states of this invocation. */
89*758e9fbaSOystein Eftevaag r = Fapi_GetTpmBlobs_Finish(context, tpm2bPublic, tpm2bPublicSize, tpm2bPrivate,
90*758e9fbaSOystein Eftevaag tpm2bPrivateSize, policy);
91*758e9fbaSOystein Eftevaag } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
92*758e9fbaSOystein Eftevaag
93*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Entity_GetTPMBlobs");
94*758e9fbaSOystein Eftevaag
95*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
96*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
97*758e9fbaSOystein Eftevaag }
98*758e9fbaSOystein Eftevaag
99*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_GetTpmBlobs
100*758e9fbaSOystein Eftevaag *
101*758e9fbaSOystein Eftevaag * Get the public and private blobs of a TPM object. They can be loaded with a
102*758e9fbaSOystein Eftevaag * lower-level API such as the SAPI or the ESAPI.
103*758e9fbaSOystein Eftevaag *
104*758e9fbaSOystein Eftevaag * Call Fapi_GetTpmBlobs_Finish to finish the execution of this command.
105*758e9fbaSOystein Eftevaag *
106*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
107*758e9fbaSOystein Eftevaag * @param[in] path The path to the key for which the blobs will be returned
108*758e9fbaSOystein Eftevaag *
109*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
110*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
111*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
112*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
113*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
114*758e9fbaSOystein Eftevaag * operation already pending.
115*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
116*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
117*758e9fbaSOystein Eftevaag * internal operations or return parameters.
118*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
119*758e9fbaSOystein Eftevaag * during authorization.
120*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
121*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
122*758e9fbaSOystein Eftevaag * the function.
123*758e9fbaSOystein Eftevaag */
124*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_GetTpmBlobs_Async(FAPI_CONTEXT * context,char const * path)125*758e9fbaSOystein Eftevaag Fapi_GetTpmBlobs_Async(
126*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
127*758e9fbaSOystein Eftevaag char const *path)
128*758e9fbaSOystein Eftevaag {
129*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
130*758e9fbaSOystein Eftevaag LOG_TRACE("path: %s", path);
131*758e9fbaSOystein Eftevaag
132*758e9fbaSOystein Eftevaag TSS2_RC r;
133*758e9fbaSOystein Eftevaag
134*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
135*758e9fbaSOystein Eftevaag check_not_null(context);
136*758e9fbaSOystein Eftevaag check_not_null(path);
137*758e9fbaSOystein Eftevaag
138*758e9fbaSOystein Eftevaag /* Load the object from the key store. */
139*758e9fbaSOystein Eftevaag r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
140*758e9fbaSOystein Eftevaag return_if_error2(r, "Could not open: %s", path);
141*758e9fbaSOystein Eftevaag
142*758e9fbaSOystein Eftevaag /* Initialize the context state for this operation. */
143*758e9fbaSOystein Eftevaag context->state = ENTITY_GET_TPM_BLOBS_READ;
144*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
145*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
146*758e9fbaSOystein Eftevaag }
147*758e9fbaSOystein Eftevaag
148*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_GetTpmBlobs
149*758e9fbaSOystein Eftevaag *
150*758e9fbaSOystein Eftevaag * This function should be called after a previous Fapi_GetTpmBlobs_Async.
151*758e9fbaSOystein Eftevaag *
152*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
153*758e9fbaSOystein Eftevaag * @param[out] tpm2bPublic The returned public area of the object. May be NULL
154*758e9fbaSOystein Eftevaag * @param[out] tpm2bPublicSize The size of tpm2bPublic in bytes. May be NULL
155*758e9fbaSOystein Eftevaag * @param[out] tpm2bPrivate The returned private area of the object. May be
156*758e9fbaSOystein Eftevaag * NULL
157*758e9fbaSOystein Eftevaag * @param[out] tpm2bPrivateSize The size of tpm2bPrivate in bytes. May be NULL
158*758e9fbaSOystein Eftevaag * @param[out] policy The policy that is associated with the object encoded in
159*758e9fbaSOystein Eftevaag * JSON. May be NULL
160*758e9fbaSOystein Eftevaag *
161*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
162*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context is NULL.
163*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
164*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
165*758e9fbaSOystein Eftevaag * operation already pending.
166*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
167*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
168*758e9fbaSOystein Eftevaag * internal operations or return parameters.
169*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
170*758e9fbaSOystein Eftevaag * complete. Call this function again later.
171*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_PATH if the used path in inappropriate-
172*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
173*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
174*758e9fbaSOystein Eftevaag * the function.
175*758e9fbaSOystein Eftevaag */
176*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_GetTpmBlobs_Finish(FAPI_CONTEXT * context,uint8_t ** tpm2bPublic,size_t * tpm2bPublicSize,uint8_t ** tpm2bPrivate,size_t * tpm2bPrivateSize,char ** policy)177*758e9fbaSOystein Eftevaag Fapi_GetTpmBlobs_Finish(
178*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
179*758e9fbaSOystein Eftevaag uint8_t **tpm2bPublic,
180*758e9fbaSOystein Eftevaag size_t *tpm2bPublicSize,
181*758e9fbaSOystein Eftevaag uint8_t **tpm2bPrivate,
182*758e9fbaSOystein Eftevaag size_t *tpm2bPrivateSize,
183*758e9fbaSOystein Eftevaag char **policy)
184*758e9fbaSOystein Eftevaag {
185*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
186*758e9fbaSOystein Eftevaag
187*758e9fbaSOystein Eftevaag TSS2_RC r;
188*758e9fbaSOystein Eftevaag IFAPI_OBJECT object;
189*758e9fbaSOystein Eftevaag UINT16 private_size;
190*758e9fbaSOystein Eftevaag size_t offset;
191*758e9fbaSOystein Eftevaag json_object *jso = NULL;
192*758e9fbaSOystein Eftevaag
193*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
194*758e9fbaSOystein Eftevaag check_not_null(context);
195*758e9fbaSOystein Eftevaag
196*758e9fbaSOystein Eftevaag switch (context->state) {
197*758e9fbaSOystein Eftevaag statecase(context->state, ENTITY_GET_TPM_BLOBS_READ);
198*758e9fbaSOystein Eftevaag /* Finish readon the metadata from key store. */
199*758e9fbaSOystein Eftevaag r = ifapi_keystore_load_finish(&context->keystore, &context->io, &object);
200*758e9fbaSOystein Eftevaag return_try_again(r);
201*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "read_finish failed");
202*758e9fbaSOystein Eftevaag
203*758e9fbaSOystein Eftevaag if (object.objectType != IFAPI_KEY_OBJ) {
204*758e9fbaSOystein Eftevaag goto_error(r, TSS2_FAPI_RC_BAD_PATH, "No key object.", error_cleanup);
205*758e9fbaSOystein Eftevaag }
206*758e9fbaSOystein Eftevaag
207*758e9fbaSOystein Eftevaag /* Marshal the public data to the output parameter. */
208*758e9fbaSOystein Eftevaag if (tpm2bPublic && tpm2bPublicSize) {
209*758e9fbaSOystein Eftevaag *tpm2bPublic = malloc(sizeof(uint8_t) * sizeof(TPM2B_PUBLIC));
210*758e9fbaSOystein Eftevaag goto_if_null(*tpm2bPublic, "Out of memory.",
211*758e9fbaSOystein Eftevaag TSS2_FAPI_RC_MEMORY, error_cleanup);
212*758e9fbaSOystein Eftevaag offset = 0;
213*758e9fbaSOystein Eftevaag r = Tss2_MU_TPM2B_PUBLIC_Marshal(&object.misc.key.public,
214*758e9fbaSOystein Eftevaag *tpm2bPublic, sizeof(TPM2B_PUBLIC), &offset);
215*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "FAPI marshal TPM2B_PUBLIC",
216*758e9fbaSOystein Eftevaag error_cleanup);
217*758e9fbaSOystein Eftevaag
218*758e9fbaSOystein Eftevaag *tpm2bPublicSize = offset;
219*758e9fbaSOystein Eftevaag goto_if_error(r, "Marshaling TPM2B_PUBLIC", error_cleanup);
220*758e9fbaSOystein Eftevaag }
221*758e9fbaSOystein Eftevaag
222*758e9fbaSOystein Eftevaag /* Marshal the private data to the output parameter. */
223*758e9fbaSOystein Eftevaag if (tpm2bPrivate && tpm2bPrivateSize) {
224*758e9fbaSOystein Eftevaag private_size = object.misc.key.private.size;
225*758e9fbaSOystein Eftevaag *tpm2bPrivateSize = private_size + sizeof(UINT16);
226*758e9fbaSOystein Eftevaag *tpm2bPrivate = malloc(*tpm2bPrivateSize);
227*758e9fbaSOystein Eftevaag goto_if_null(*tpm2bPrivate, "Out of memory.",
228*758e9fbaSOystein Eftevaag TSS2_FAPI_RC_MEMORY, error_cleanup);
229*758e9fbaSOystein Eftevaag offset = 0;
230*758e9fbaSOystein Eftevaag r = Tss2_MU_UINT16_Marshal(private_size,
231*758e9fbaSOystein Eftevaag *tpm2bPrivate, sizeof(TPM2B_PRIVATE), &offset);
232*758e9fbaSOystein Eftevaag goto_if_error_reset_state(r, "FAPI marshal UINT16", error_cleanup);
233*758e9fbaSOystein Eftevaag
234*758e9fbaSOystein Eftevaag memcpy(*tpm2bPrivate + offset, &object.misc.key.private.buffer[0], private_size);
235*758e9fbaSOystein Eftevaag }
236*758e9fbaSOystein Eftevaag
237*758e9fbaSOystein Eftevaag /* Duplicate the policy to the output parameter. */
238*758e9fbaSOystein Eftevaag if (object.policy && policy) {
239*758e9fbaSOystein Eftevaag r = ifapi_json_TPMS_POLICY_serialize(
240*758e9fbaSOystein Eftevaag object.policy, &jso);
241*758e9fbaSOystein Eftevaag goto_if_error(r, "Serialize policy", error_cleanup);
242*758e9fbaSOystein Eftevaag
243*758e9fbaSOystein Eftevaag strdup_check(*policy,
244*758e9fbaSOystein Eftevaag json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY),
245*758e9fbaSOystein Eftevaag r, error_cleanup);
246*758e9fbaSOystein Eftevaag json_object_put(jso);
247*758e9fbaSOystein Eftevaag }
248*758e9fbaSOystein Eftevaag
249*758e9fbaSOystein Eftevaag /* Cleanup any intermediate results and state stored in the context. */
250*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&object);
251*758e9fbaSOystein Eftevaag context->state = _FAPI_STATE_INIT;
252*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
253*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
254*758e9fbaSOystein Eftevaag
255*758e9fbaSOystein Eftevaag statecasedefault(context->state);
256*758e9fbaSOystein Eftevaag }
257*758e9fbaSOystein Eftevaag error_cleanup:
258*758e9fbaSOystein Eftevaag /* Cleanup any intermediate results and state stored in the context. */
259*758e9fbaSOystein Eftevaag if (jso)
260*758e9fbaSOystein Eftevaag json_object_put(jso);
261*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&object);
262*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
263*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(context->loadKey.key_object);
264*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
265*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
266*758e9fbaSOystein Eftevaag return r;
267*758e9fbaSOystein Eftevaag }
268