xref: /aosp_15_r20/external/tpm2-tss/src/tss2-fapi/api/fapi_callback.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
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 #ifndef NO_DL
12*758e9fbaSOystein Eftevaag #include <dlfcn.h>
13*758e9fbaSOystein Eftevaag #endif /* NO_DL */
14*758e9fbaSOystein Eftevaag #include <stdlib.h>
15*758e9fbaSOystein Eftevaag 
16*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
17*758e9fbaSOystein Eftevaag #include "tss2_fapi.h"
18*758e9fbaSOystein Eftevaag #include "fapi_int.h"
19*758e9fbaSOystein Eftevaag 
20*758e9fbaSOystein Eftevaag #define LOGMODULE fapi
21*758e9fbaSOystein Eftevaag #include "util/log.h"
22*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
23*758e9fbaSOystein Eftevaag 
24*758e9fbaSOystein Eftevaag /**
25*758e9fbaSOystein Eftevaag  * This function registers a callback that will be invoked whenever the FAPI has
26*758e9fbaSOystein Eftevaag  * to decide which branch of a Policy-OR policy to use to authorize a particular
27*758e9fbaSOystein Eftevaag  * FAPI operation.
28*758e9fbaSOystein Eftevaag  *
29*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
30*758e9fbaSOystein Eftevaag  * @param[in] callback The callback function for branch selection
31*758e9fbaSOystein Eftevaag  * @param[in] userData A pointer that is provided to all callback invocations
32*758e9fbaSOystein Eftevaag  *
33*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
34*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
35*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
36*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
37*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
38*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the synchronous or Async functions are
39*758e9fbaSOystein Eftevaag  *         called while the context has another asynchronous operation
40*758e9fbaSOystein Eftevaag  *         outstanding, or the Finish function is called while the context does
41*758e9fbaSOystein Eftevaag  *         not have an appropriate asynchronous operation outstanding.
42*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
43*758e9fbaSOystein Eftevaag  */
44*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetBranchCB(FAPI_CONTEXT * context,Fapi_CB_Branch callback,void * userData)45*758e9fbaSOystein Eftevaag Fapi_SetBranchCB(
46*758e9fbaSOystein Eftevaag     FAPI_CONTEXT                      *context,
47*758e9fbaSOystein Eftevaag     Fapi_CB_Branch                     callback,
48*758e9fbaSOystein Eftevaag     void                              *userData)
49*758e9fbaSOystein Eftevaag {
50*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
51*758e9fbaSOystein Eftevaag     LOG_TRACE("Callback %p Userdata %p", callback, userData);
52*758e9fbaSOystein Eftevaag 
53*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
54*758e9fbaSOystein Eftevaag     check_not_null(context);
55*758e9fbaSOystein Eftevaag     check_not_null(callback);
56*758e9fbaSOystein Eftevaag 
57*758e9fbaSOystein Eftevaag     /* Store the callback and userdata pointer. */
58*758e9fbaSOystein Eftevaag     context->callbacks.branch = callback;
59*758e9fbaSOystein Eftevaag     context->callbacks.branchData = userData;
60*758e9fbaSOystein Eftevaag 
61*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
62*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
63*758e9fbaSOystein Eftevaag }
64*758e9fbaSOystein Eftevaag 
65*758e9fbaSOystein Eftevaag /**
66*758e9fbaSOystein Eftevaag  * This function registers an application-defined function as a callback to
67*758e9fbaSOystein Eftevaag  * allow the TSS to get authorization values from the application.
68*758e9fbaSOystein Eftevaag  *
69*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
70*758e9fbaSOystein Eftevaag  * @param[in] callback The callback function for auth value retrieval
71*758e9fbaSOystein Eftevaag  * @param[in] userData A pointer that is provided to all callback invocations
72*758e9fbaSOystein Eftevaag  *
73*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
74*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
75*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
76*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
77*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
78*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the synchronous or Async functions are
79*758e9fbaSOystein Eftevaag  *         called while the context has another asynchronous operation
80*758e9fbaSOystein Eftevaag  *         outstanding, or the Finish function is called while the context does
81*758e9fbaSOystein Eftevaag  *         not have an appropriate asynchronous operation outstanding.
82*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
83*758e9fbaSOystein Eftevaag  */
84*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetAuthCB(FAPI_CONTEXT * context,Fapi_CB_Auth callback,void * userData)85*758e9fbaSOystein Eftevaag Fapi_SetAuthCB(
86*758e9fbaSOystein Eftevaag     FAPI_CONTEXT           *context,
87*758e9fbaSOystein Eftevaag     Fapi_CB_Auth           callback,
88*758e9fbaSOystein Eftevaag     void                   *userData)
89*758e9fbaSOystein Eftevaag {
90*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
91*758e9fbaSOystein Eftevaag     LOG_TRACE("Callback %p Userdata %p", callback, userData);
92*758e9fbaSOystein Eftevaag 
93*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
94*758e9fbaSOystein Eftevaag     check_not_null(context);
95*758e9fbaSOystein Eftevaag     check_not_null(callback);
96*758e9fbaSOystein Eftevaag 
97*758e9fbaSOystein Eftevaag     /* Store the callback and userdata pointer. */
98*758e9fbaSOystein Eftevaag     context->callbacks.auth = callback;
99*758e9fbaSOystein Eftevaag     context->callbacks.authData = userData;
100*758e9fbaSOystein Eftevaag 
101*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
102*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
103*758e9fbaSOystein Eftevaag }
104*758e9fbaSOystein Eftevaag 
105*758e9fbaSOystein Eftevaag /**
106*758e9fbaSOystein Eftevaag  * Fapi_SetSignCB() registers an application-defined function as a callback to
107*758e9fbaSOystein Eftevaag  * allow the FAPI to get signatures authorizing use of TPM objects.
108*758e9fbaSOystein Eftevaag  *
109*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
110*758e9fbaSOystein Eftevaag  * @param[in] callback The callback function for signing selection
111*758e9fbaSOystein Eftevaag  * @param[in] userData A pointer that is provided to all callback invocations
112*758e9fbaSOystein Eftevaag  *
113*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
114*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
115*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
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_BAD_SEQUENCE: if the synchronous or Async functions are
119*758e9fbaSOystein Eftevaag  *         called while the context has another asynchronous operation
120*758e9fbaSOystein Eftevaag  *         outstanding, or the Finish function is called while the context does
121*758e9fbaSOystein Eftevaag  *         not have an appropriate asynchronous operation outstanding.
122*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
123*758e9fbaSOystein Eftevaag  */
124*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetSignCB(FAPI_CONTEXT * context,Fapi_CB_Sign callback,void * userData)125*758e9fbaSOystein Eftevaag Fapi_SetSignCB(
126*758e9fbaSOystein Eftevaag     FAPI_CONTEXT                *context,
127*758e9fbaSOystein Eftevaag     Fapi_CB_Sign                callback,
128*758e9fbaSOystein Eftevaag     void                        *userData)
129*758e9fbaSOystein Eftevaag {
130*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
131*758e9fbaSOystein Eftevaag     LOG_TRACE("Callback %p Userdata %p", callback, userData);
132*758e9fbaSOystein Eftevaag 
133*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
134*758e9fbaSOystein Eftevaag     check_not_null(context);
135*758e9fbaSOystein Eftevaag     check_not_null(callback);
136*758e9fbaSOystein Eftevaag 
137*758e9fbaSOystein Eftevaag     /* Store the callback and userdata pointer. */
138*758e9fbaSOystein Eftevaag     context->callbacks.sign = callback;
139*758e9fbaSOystein Eftevaag     context->callbacks.signData = userData;
140*758e9fbaSOystein Eftevaag 
141*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
142*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
143*758e9fbaSOystein Eftevaag }
144*758e9fbaSOystein Eftevaag 
145*758e9fbaSOystein Eftevaag 
146*758e9fbaSOystein Eftevaag /**
147*758e9fbaSOystein Eftevaag  * Fapi_SetActionCB() registers an application-defined function as a callback
148*758e9fbaSOystein Eftevaag  * that shall be called back upon encountering a policy action element.
149*758e9fbaSOystein Eftevaag  *
150*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
151*758e9fbaSOystein Eftevaag  * @param[in] callback The callback function for branch selection
152*758e9fbaSOystein Eftevaag  * @param[in] userData A pointer that is provided to all callback invocations
153*758e9fbaSOystein Eftevaag  *
154*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
155*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
156*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
157*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
158*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
159*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the synchronous or Async functions are
160*758e9fbaSOystein Eftevaag  *         called while the context has another asynchronous operation
161*758e9fbaSOystein Eftevaag  *         outstanding, or the Finish function is called while the context does
162*758e9fbaSOystein Eftevaag  *         not have an appropriate asynchronous operation outstanding.
163*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
164*758e9fbaSOystein Eftevaag  */
165*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetPolicyActionCB(FAPI_CONTEXT * context,Fapi_CB_PolicyAction callback,void * userData)166*758e9fbaSOystein Eftevaag Fapi_SetPolicyActionCB(
167*758e9fbaSOystein Eftevaag     FAPI_CONTEXT                *context,
168*758e9fbaSOystein Eftevaag     Fapi_CB_PolicyAction         callback,
169*758e9fbaSOystein Eftevaag     void                        *userData)
170*758e9fbaSOystein Eftevaag {
171*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
172*758e9fbaSOystein Eftevaag     LOG_TRACE("Callback %p Userdata %p", callback, userData);
173*758e9fbaSOystein Eftevaag 
174*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
175*758e9fbaSOystein Eftevaag     check_not_null(context);
176*758e9fbaSOystein Eftevaag     check_not_null(callback);
177*758e9fbaSOystein Eftevaag 
178*758e9fbaSOystein Eftevaag     /* Store the callback and userdata pointer. */
179*758e9fbaSOystein Eftevaag     context->callbacks.action = callback;
180*758e9fbaSOystein Eftevaag     context->callbacks.actionData = userData;
181*758e9fbaSOystein Eftevaag 
182*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
183*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
184*758e9fbaSOystein Eftevaag }
185