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 context or callback 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 check_not_null(callback);
56
57 /* Store the callback and userdata pointer. */
58 context->callbacks.branch = callback;
59 context->callbacks.branchData = userData;
60
61 LOG_TRACE("finished");
62 return TSS2_RC_SUCCESS;
63 }
64
65 /**
66 * This function registers an application-defined function as a callback to
67 * allow the TSS to get authorization values from the application.
68 *
69 * @param[in,out] context The FAPI_CONTEXT
70 * @param[in] callback The callback function for auth value retrieval
71 * @param[in] userData A pointer that is provided to all callback invocations
72 *
73 * @retval TSS2_RC_SUCCESS: if the function call was a success.
74 * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
75 * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
76 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
77 * internal operations or return parameters.
78 * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the synchronous or Async functions are
79 * called while the context has another asynchronous operation
80 * outstanding, or the Finish function is called while the context does
81 * not have an appropriate asynchronous operation outstanding.
82 * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
83 */
84 TSS2_RC
Fapi_SetAuthCB(FAPI_CONTEXT * context,Fapi_CB_Auth callback,void * userData)85 Fapi_SetAuthCB(
86 FAPI_CONTEXT *context,
87 Fapi_CB_Auth callback,
88 void *userData)
89 {
90 LOG_TRACE("called for context:%p", context);
91 LOG_TRACE("Callback %p Userdata %p", callback, userData);
92
93 /* Check for NULL parameters */
94 check_not_null(context);
95 check_not_null(callback);
96
97 /* Store the callback and userdata pointer. */
98 context->callbacks.auth = callback;
99 context->callbacks.authData = userData;
100
101 LOG_TRACE("finished");
102 return TSS2_RC_SUCCESS;
103 }
104
105 /**
106 * Fapi_SetSignCB() registers an application-defined function as a callback to
107 * allow the FAPI to get signatures authorizing use of TPM objects.
108 *
109 * @param[in,out] context The FAPI_CONTEXT
110 * @param[in] callback The callback function for signing selection
111 * @param[in] userData A pointer that is provided to all callback invocations
112 *
113 * @retval TSS2_RC_SUCCESS: if the function call was a success.
114 * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
115 * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
116 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
117 * internal operations or return parameters.
118 * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the synchronous or Async functions are
119 * called while the context has another asynchronous operation
120 * outstanding, or the Finish function is called while the context does
121 * not have an appropriate asynchronous operation outstanding.
122 * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
123 */
124 TSS2_RC
Fapi_SetSignCB(FAPI_CONTEXT * context,Fapi_CB_Sign callback,void * userData)125 Fapi_SetSignCB(
126 FAPI_CONTEXT *context,
127 Fapi_CB_Sign callback,
128 void *userData)
129 {
130 LOG_TRACE("called for context:%p", context);
131 LOG_TRACE("Callback %p Userdata %p", callback, userData);
132
133 /* Check for NULL parameters */
134 check_not_null(context);
135 check_not_null(callback);
136
137 /* Store the callback and userdata pointer. */
138 context->callbacks.sign = callback;
139 context->callbacks.signData = userData;
140
141 LOG_TRACE("finished");
142 return TSS2_RC_SUCCESS;
143 }
144
145
146 /**
147 * Fapi_SetActionCB() registers an application-defined function as a callback
148 * that shall be called back upon encountering a policy action element.
149 *
150 * @param[in,out] context The FAPI_CONTEXT
151 * @param[in] callback The callback function for branch selection
152 * @param[in] userData A pointer that is provided to all callback invocations
153 *
154 * @retval TSS2_RC_SUCCESS: if the function call was a success.
155 * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or callback is NULL.
156 * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
157 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
158 * internal operations or return parameters.
159 * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the synchronous or Async functions are
160 * called while the context has another asynchronous operation
161 * outstanding, or the Finish function is called while the context does
162 * not have an appropriate asynchronous operation outstanding.
163 * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
164 */
165 TSS2_RC
Fapi_SetPolicyActionCB(FAPI_CONTEXT * context,Fapi_CB_PolicyAction callback,void * userData)166 Fapi_SetPolicyActionCB(
167 FAPI_CONTEXT *context,
168 Fapi_CB_PolicyAction callback,
169 void *userData)
170 {
171 LOG_TRACE("called for context:%p", context);
172 LOG_TRACE("Callback %p Userdata %p", callback, userData);
173
174 /* Check for NULL parameters */
175 check_not_null(context);
176 check_not_null(callback);
177
178 /* Store the callback and userdata pointer. */
179 context->callbacks.action = callback;
180 context->callbacks.actionData = userData;
181
182 LOG_TRACE("finished");
183 return TSS2_RC_SUCCESS;
184 }
185