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