1TPMLIB_RegisterCallbacks(3) TPMLIB_RegisterCallbacks(3)
2
3
4
6 TPMLIB_RegisterCallbacks - Register callbacks for implementing
7 customized behavior of certain functions
8
10 TPM library (libtpms, -ltpms)
11
13 #include <libtpms/tpm_types.h>
14
15 #include <libtpms/tpm_library.h>
16
17 #include <libtpms/tpm_error.h>
18
19 TPM_RESULT TPMLIB_RegisterCallbacks(struct tpmlibrary_callbacks *);
20
22 The TPMLIB_RegisterCallbacks() functions allows to register several
23 callback functions with libtpms that enable a user to implement
24 customized behavior of several library-internal functions. This feature
25 will typically be used if the behavior of the provided internal
26 functions is not as needed. An example would be that libtpms writes
27 all data into files with certain names. If, however, the data needs to
28 be written into a special type of storage the user will register
29 callbacks with the library that are invoked when the TPM needs to
30 write, read or delete data from storage and the user may then implement
31 custom behavior in these functions.
32
33 The following shows the data structure used for registering the
34 callbacks.
35
36 struct libtpms_callbacks {
37 int sizeOfStruct;
38 TPM_RESULT (*tpm_nvram_init)(void);
39 TPM_RESULT (*tpm_nvram_loaddata)(unsigned char **data,
40 uint32_t *length,
41 uint32_t tpm_number,
42 const char *name);
43 TPM_RESULT (*tpm_nvram_storedata)(const unsigned char *data,
44 uint32_t length,
45 uint32_t tpm_number,
46 const char *name);
47 TPM_RESULT (*tpm_nvram_deletename)(uint32_t tpm_number,
48 const char *name,
49 TPM_BOOL mustExist);
50 TPM_RESULT (*tpm_io_init)(void);
51 TPM_RESULT (*tpm_io_getlocality)(TPM_MODIFIER_INDICATOR *localityModifer,
52 uint32_t tpm_number);
53 TPM_RESULT (*tpm_io_getphysicalpresence)(TPM_BOOL *physicalPresence,
54 uint32_t tpm_number);
55 };
56
57 Currently 7 callbacks are supported. If a callback pointer in the above
58 structure is set to NULL the default library-internal implementation of
59 that function will be used.
60
61 If one of the callbacks in either the tpm_nvram or tpm_io group is set,
62 then all of the callbacks in the respective group should be
63 implemented.
64
65 tpm_nvram_init
66 This function is called before any access to persitent storage is
67 done. It allows the user to perform initialization of access to
68 persitent storage.
69
70 Upon success this function should return TPM_SUCCESS, a failure
71 code otherwise.
72
73 The default implementation requires that the environment variable
74 TPM_PATH is set and points to a directory where the TPM's state can
75 be written to. If the variable is not set, it will return TPM_FAIL
76 and the initialization of the TPM in TPMLIB_MainInit() will fail.
77
78 tpm_nvram_loaddata
79 This function is called when the TPM wants to load state from
80 persistent storage. The implementing function must allocate a
81 buffer (data) and return it to the TPM along with the length of the
82 buffer (length). The tpm_number is always 0 and can be ignored.
83 The name parameter is either one of TPM_SAVESTATE_NAME,
84 TPM_VOLATILESTATE_NAME, or TPM_PERMANENT_ALL_NAME and indicates
85 which one of the 3 types of state is supposed to be loaded.
86
87 Upon success this function should return TPM_SUCCESS, a failure
88 code otherwise.
89
90 The default implementation writes the TPM's state into files in a
91 directory where the TPM_PATH environment variable pointed to when
92 TPMLIB_MainInit() was executed. Failure to write the TPM's state
93 into files will put the TPM into failure mode.
94
95 If this function is not set (NULL), then the original NVChip file
96 will be read when using a TPM 2. This file contains the memory dump
97 of internal data structures and is neither portable between
98 endianesses or architectures of different sizes (32 bit, 64 bit),
99 nor will it allow handling extensions of those internal data
100 structures it carries through additions in the TPM 2 code. In the
101 worst case this may result in memory access errors by internal
102 functions and result in crashes. Therefore, it is recommended to
103 set this function and handle the writing of the TPM state.
104
105 tpm_nvram_storedata
106 This function is called when the TPM wants to store state to
107 persistent storage. The data and length parameters provide the data
108 to be stored and the number of bytes. The implementing function
109 must not free the data buffer. The tpm_number is always 0 and can
110 be ignored. The name parameter is either one of
111 TPM_SAVESTATE_NAME, TPM_VOLATILESTATE_NAME, or
112 TPM_PERMANENT_ALL_NAME and indicates which one of the 3 types of
113 state is supposed to be stored.
114
115 Upon success this function should return TPM_SUCCESS, a failure
116 code otherwise.
117
118 The default implementation reads the TPM's state from files in a
119 directory where the TPM_PATH environment variable pointed to when
120 TPMLIB_MainInit() was executed. Failure to read the TPM's state
121 from files may put the TPM into failure mode.
122
123 If this function is not set (NULL), the memory dump will be written
124 to the NVChip file (TPM 2) and the same comments apply as when the
125 tpm_nvram_loaddata interface function is not set.
126
127 tpm_nvram_deletename
128 This function is called when the TPM wants to delete state on
129 persistent storage. The tpm_number is always 0 and can be ignored.
130 The name parameter is either one of TPM_SAVESTATE_NAME,
131 TPM_VOLATILESTATE_NAME, or TPM_PERMANENT_ALL_NAME and indicates
132 which one of the 3 types of state is supposed to be deleted. The
133 mustExist parameter indicates whether the given data must exist and
134 the implementing function should return TPM_FAIL if the data did
135 not exist.
136
137 Upon success this function should return TPM_SUCCESS, a failure
138 code otherwise.
139
140 The default implementation deletes the TPM's state files in a
141 directory where the TPM_PATH environment variable pointed to when
142 TPMLIB_MainInit() was executed. Failure to delete the TPM's state
143 files may put the TPM into failure mode.
144
145 tpm_io_init
146 This function is called to initialize the IO subsystem of the TPM.
147
148 Upon success this function should return TPM_SUCCESS, a failure
149 code otherwise.
150
151 The default implementation simply returns TPM_SUCCESS.
152
153 tpm_io_getlocality
154 This function is called when the TPM needs to determine the
155 locality under which a command is supposed to be executed. The
156 implementing function should return the number of the locality by
157 writing it into the localityModifier pointer.
158
159 Upon success this function should return TPM_SUCCESS, a failure
160 code otherwise.
161
162 The default implementation returns 0 as the locality.
163
164 tpm_io_getphysicalpresence
165 This function is called when the TPM needs to determine whether
166 physical presence has been asserted. The implementing function
167 should write either TRUE or FALSE into the physicalPresence
168 pointer.
169
170 Upon success this function should return TPM_SUCCESS, a failure
171 code otherwise.
172
173 The default implementation returns FALSE for physical presence.
174
176 Upon successful completion, TPMLIB_MainInit() returns TPM_SUCCESS, an
177 error value otherwise.
178
180 TPM_SUCCESS
181 The function completed successfully.
182
183 TPM_FAIL
184 General failure.
185
186 For a complete list of TPM error codes please consult the include file
187 libtpms/tpm_error.h
188
190 #include <libtpms/tpm_types.h>
191 #include <libtpms/tpm_library.h>
192 #include <libtpms/tpm_error.h>
193
194 static TPM_MODIFIER_INDICATOR locality;
195
196 static TPM_RESULT mytpm_io_init(void)
197 {
198 return TPM_SUCCESS;
199 }
200
201 static TPM_RESULT mytpm_io_getlocality(TPM_MODIFIER_INDICATOR *locModif,
202 uint32_t tpm_number)
203 {
204 *locModif = locality;
205
206 return TPM_SUCCESS:
207 }
208
209 static TPM_RESULT mytpm_io_getphysicalpresence(TPM_BOOL *physicalPresence,
210 uint32_t tpm_number)
211 {
212 *physicalPresence = FALSE;
213
214 return TPM_SUCCESS;
215 }
216
217 int main(void) {
218 TPM_RESULT res;
219 unsigned char *respbuffer;
220 uint32_t resp_size;
221 uint32_t respbufsize;
222 unsigned char *command;
223 uint32_t command_size;
224
225 struct libtpms_callbacks cbs = {
226 .sizeOfStruct = sizeof(struct libtpms_callbacks),
227 .tpm_nvram_init = NULL,
228 .tpm_nvram_loaddata = NULL,
229 .tpm_nvram_storedata = NULL,
230 .tpm_nvram_deletename = NULL,
231 .tpm_io_init = mytpm_io_init,
232 .tpm_io_getlocality = mytpm_io_getlocality,
233 .tpm_io_getphysicalpresence = mytpm_io_getphysicalpresence,
234 };
235
236
237 [...]
238
239 if (TPMLIB_RegisterCallbacks(&cbs) != TPM_SUCCESS) {
240 fprintf(stderr, "Could not register the callbacks.\n");
241 return 1;
242 }
243
244 if (TPMLIB_MainInit()) != TPM_SUCCESS) {
245 fprintf(stderr, "Could not start the TPM.\n");
246 return 1;
247 }
248
249 [...]
250 /* build TPM command */
251 [...]
252
253 res = TPMLIB_Process(&respbuffer, &resp_size,
254 &respbufsize,
255 command, command_size);
256 [...]
257
258 TPMLIB_Terminate();
259
260 return 0;
261 }
262
264 TPMLIB_Process(3), TPMLIB_MainInit(3), TPMLIB_Terminate(3),
265 TPMLIB_DecodeBlobs(3)
266
267
268
269libtpms 2022-07-01 TPMLIB_RegisterCallbacks(3)