VirtualBox

source: vbox/trunk/src/libs/libtpms-0.9.0/man/man3/TPMLIB_RegisterCallbacks.pod@ 91612

Last change on this file since 91612 was 91612, checked in by vboxsync, 4 years ago

src/libs: Export libtpms-0.9.0, bugref:10078

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette