1 |
|
---|
2 |
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | TPMLIB_RegisterCallbacks - Register callbacks for implementing customized
|
---|
6 | behavior of certain functions
|
---|
7 |
|
---|
8 | =head1 LIBRARY
|
---|
9 |
|
---|
10 | TPM library (libtpms, -ltpms)
|
---|
11 |
|
---|
12 | =head1 SYNOPSIS
|
---|
13 |
|
---|
14 | B<#include <libtpms/tpm_types.h>>
|
---|
15 |
|
---|
16 | B<#include <libtpms/tpm_library.h>>
|
---|
17 |
|
---|
18 | B<#include <libtpms/tpm_error.h>>
|
---|
19 |
|
---|
20 | B<TPM_RESULT TPMLIB_RegisterCallbacks(struct tpmlibrary_callbacks *);>
|
---|
21 |
|
---|
22 | =head1 DESCRIPTION
|
---|
23 |
|
---|
24 | The B<TPMLIB_RegisterCallbacks()> functions allows to register several
|
---|
25 | callback functions with libtpms that enable a user to implement customized
|
---|
26 | behavior of several library-internal functions. This feature will typically
|
---|
27 | be used if the behavior of the provided internal functions is not as needed.
|
---|
28 | An example would be that libtpms writes all data into files with certain names.
|
---|
29 | If, however, the data needs to be written into a special type of storage
|
---|
30 | the user will register callbacks with the library that are invoked when
|
---|
31 | the TPM needs to write, read or delete data from storage and the user may
|
---|
32 | then implement custom behavior in these functions.
|
---|
33 |
|
---|
34 | The 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 |
|
---|
57 | Currently 7 callbacks are supported. If a callback pointer in the above
|
---|
58 | structure is set to NULL the default library-internal implementation
|
---|
59 | of that function will be used.
|
---|
60 |
|
---|
61 | If one of the callbacks in either the I<tpm_nvram> or I<tpm_io> group is
|
---|
62 | set, then all of the callbacks in the respective group should
|
---|
63 | be implemented.
|
---|
64 |
|
---|
65 | =over 4
|
---|
66 |
|
---|
67 | =item B<tpm_nvram_init>
|
---|
68 |
|
---|
69 | This function is called before any access to persitent storage is done. It
|
---|
70 | allows the user to perform initialization of access to persitent storage.
|
---|
71 |
|
---|
72 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
73 | otherwise.
|
---|
74 |
|
---|
75 | The default implementation requires that the environment variable
|
---|
76 | I<TPM_PATH> is set and points to a directory where the TPM's state
|
---|
77 | can be written to. If the variable is not set, it will return B<TPM_FAIL>
|
---|
78 | and the initialization of the TPM in B<TPMLIB_MainInit()> will fail.
|
---|
79 |
|
---|
80 | =item B<tpm_nvram_loaddata>
|
---|
81 |
|
---|
82 | This function is called when the TPM wants to load state from persistent
|
---|
83 | storage. The implementing function must allocate a buffer (I<data>)
|
---|
84 | and return it to the TPM along with the length of the buffer (I<length>).
|
---|
85 | The I<tpm_number> is always 0 and can be ignored.
|
---|
86 | The I<name> parameter is either one of B<TPM_SAVESTATE_NAME>,
|
---|
87 | B<TPM_VOLATILESTATE_NAME>, or B<TPM_PERMANENT_ALL_NAME> and indicates
|
---|
88 | which one of the 3 types of state is supposed to be loaded.
|
---|
89 |
|
---|
90 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
91 | otherwise.
|
---|
92 |
|
---|
93 | The default implementation writes the TPM's state into files in a directory
|
---|
94 | where the I<TPM_PATH> environment variable pointed to when
|
---|
95 | B<TPMLIB_MainInit()> was executed. Failure to write the TPM's state into
|
---|
96 | files will put the TPM into failure mode.
|
---|
97 |
|
---|
98 | If this function is not set (NULL), then the original NVChip file
|
---|
99 | will be read when using a TPM 2. This file contains the memory dump of
|
---|
100 | internal data structures and is neither portable between endianesses or
|
---|
101 | architectures of different sizes (32 bit, 64 bit), nor will it allow
|
---|
102 | handling extensions of those internal data structures it carries
|
---|
103 | through additions in the TPM 2 code. In the worst case this may result
|
---|
104 | in memory access errors by internal functions and result in crashes.
|
---|
105 | Therefore, it is recommended to set this function and handle the writing
|
---|
106 | of the TPM state.
|
---|
107 |
|
---|
108 | =item B<tpm_nvram_storedata>
|
---|
109 |
|
---|
110 | This function is called when the TPM wants to store state to persistent
|
---|
111 | storage. The I<data> and I<length> parameters provide the data to be
|
---|
112 | stored and the number of bytes. The implementing function must not
|
---|
113 | free the I<data> buffer.
|
---|
114 | The I<tpm_number> is always 0 and can be ignored.
|
---|
115 | The I<name> parameter is either one of B<TPM_SAVESTATE_NAME>,
|
---|
116 | B<TPM_VOLATILESTATE_NAME>, or B<TPM_PERMANENT_ALL_NAME> and indicates
|
---|
117 | which one of the 3 types of state is supposed to be stored.
|
---|
118 |
|
---|
119 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
120 | otherwise.
|
---|
121 |
|
---|
122 | The default implementation reads the TPM's state from files in a directory
|
---|
123 | where the I<TPM_PATH> environment variable pointed to when
|
---|
124 | B<TPMLIB_MainInit()> was executed. Failure to read the TPM's state from
|
---|
125 | files may put the TPM into failure mode.
|
---|
126 |
|
---|
127 | If this function is not set (NULL), the memory dump will be written
|
---|
128 | to the NVChip file (TPM 2) and the same comments apply as when the
|
---|
129 | I<tpm_nvram_loaddata> interface function is not set.
|
---|
130 |
|
---|
131 | =item B<tpm_nvram_deletename>
|
---|
132 |
|
---|
133 | This function is called when the TPM wants to delete state on persistent
|
---|
134 | storage.
|
---|
135 | The I<tpm_number> is always 0 and can be ignored.
|
---|
136 | The I<name> parameter is either one of B<TPM_SAVESTATE_NAME>,
|
---|
137 | B<TPM_VOLATILESTATE_NAME>, or B<TPM_PERMANENT_ALL_NAME> and indicates
|
---|
138 | which one of the 3 types of state is supposed to be deleted.
|
---|
139 | The I<mustExist> parameter indicates whether the given data must exist
|
---|
140 | and the implementing function should return B<TPM_FAIL> if the data did
|
---|
141 | not exist.
|
---|
142 |
|
---|
143 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
144 | otherwise.
|
---|
145 |
|
---|
146 | The default implementation deletes the TPM's state files in a directory
|
---|
147 | where the I<TPM_PATH> environment variable pointed to when
|
---|
148 | B<TPMLIB_MainInit()> was executed. Failure to delete the TPM's state
|
---|
149 | files may put the TPM into failure mode.
|
---|
150 |
|
---|
151 | =item B<tpm_io_init>
|
---|
152 |
|
---|
153 | This function is called to initialize the IO subsystem of the TPM.
|
---|
154 |
|
---|
155 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
156 | otherwise.
|
---|
157 |
|
---|
158 | The default implementation simply returns B<TPM_SUCCESS>.
|
---|
159 |
|
---|
160 | =item B<tpm_io_getlocality>
|
---|
161 |
|
---|
162 | This function is called when the TPM needs to determine the locality
|
---|
163 | under which a command is supposed to be executed. The implementing function
|
---|
164 | should return the number of the locality by writing it into the
|
---|
165 | B<localityModifier> pointer.
|
---|
166 |
|
---|
167 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
168 | otherwise.
|
---|
169 |
|
---|
170 | The default implementation returns 0 as the locality.
|
---|
171 |
|
---|
172 | =item B<tpm_io_getphysicalpresence>
|
---|
173 |
|
---|
174 | This function is called when the TPM needs to determine whether physical
|
---|
175 | presence has been asserted. The implementing function should write either
|
---|
176 | B<TRUE> or B<FALSE> into the physicalPresence pointer.
|
---|
177 |
|
---|
178 | Upon success this function should return B<TPM_SUCCESS>, a failure code
|
---|
179 | otherwise.
|
---|
180 |
|
---|
181 | The default implementation returns B<FALSE> for physical presence.
|
---|
182 |
|
---|
183 | =back
|
---|
184 |
|
---|
185 | =head1 RETURN VALUE
|
---|
186 |
|
---|
187 | Upon successful completion, B<TPMLIB_MainInit()> returns B<TPM_SUCCESS>,
|
---|
188 | an error value otherwise.
|
---|
189 |
|
---|
190 | =head1 ERRORS
|
---|
191 |
|
---|
192 | =over 4
|
---|
193 |
|
---|
194 | =item B<TPM_SUCCESS>
|
---|
195 |
|
---|
196 | The function completed successfully.
|
---|
197 |
|
---|
198 | =item B<TPM_FAIL>
|
---|
199 |
|
---|
200 | General failure.
|
---|
201 |
|
---|
202 | =back
|
---|
203 |
|
---|
204 | For a complete list of TPM error codes please consult the include file
|
---|
205 | B<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 |
|
---|
284 | B<TPMLIB_Process>(3), B<TPMLIB_MainInit>(3), B<TPMLIB_Terminate>(3),
|
---|
285 | B<TPMLIB_DecodeBlobs>(3)
|
---|
286 |
|
---|
287 | =cut
|
---|