1TPMLIB_REGISTERCALLBACKS(1)  libtpms documentation TPMLIB_REGISTERCALLBACKS(1)
2
3
4

NAME

6       TPMLIB_RegisterCallbacks    - Register callbacks for implementing
7       customized behavior of certain functions
8

LIBRARY

10       TPM library (libtpms, -ltpms)
11

SYNOPSIS

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

DESCRIPTION

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       tpm_nvram_storedata
96           This function is called when the TPM wants to store state to
97           persistent storage. The data and length parameters provide the data
98           to be stored and the number of bytes. The implementing function
99           must not free the data buffer.  The tpm_number is always 0 and can
100           be ignored.  The name parameter is either one of
101           TPM_SAVESTATE_NAME, TPM_VOLATILESTATE_NAME, or
102           TPM_PERMANENT_ALL_NAME and indicates which one of the 3 types of
103           state is supposed to be stored.
104
105           Upon success this function should return TPM_SUCCESS, a failure
106           code otherwise.
107
108           The default implementation reads the TPM's state from files in a
109           directory where the TPM_PATH environment variable pointed to when
110           TPMLIB_MainInit() was executed. Failure to read the TPM's state
111           from files may put the TPM into failure mode.
112
113       tpm_nvram_deletename
114           This function is called when the TPM wants to delete state on
115           persistent storage.  The tpm_number is always 0 and can be ignored.
116           The name parameter is either one of TPM_SAVESTATE_NAME,
117           TPM_VOLATILESTATE_NAME, or TPM_PERMANENT_ALL_NAME and indicates
118           which one of the 3 types of state is supposed to be deleted.  The
119           mustExist parameter indicates wheteher the given data must exist
120           and the implementing function should return TPM_FAIL if the data
121           did not exist.
122
123           Upon success this function should return TPM_SUCCESS, a failure
124           code otherwise.
125
126           The default implementation deletes the TPM's state files in a
127           directory where the TPM_PATH environment variable pointed to when
128           TPMLIB_MainInit() was executed. Failure to delete the TPM's state
129           files may put the TPM into failure mode.
130
131       tpm_io_init
132           This function is called to initialize the IO subsystem of the TPM.
133
134           Upon success this function should return TPM_SUCCESS, a failure
135           code otherwise.
136
137           The default implementation simply returns TPM_SUCCESS.
138
139       tpm_io_getlocality
140           This function is called when the TPM needs to determine the
141           locality under which a command is supposed to be executed. The
142           implementing function should return the number of the locality by
143           writing it into the localityModifier pointer.
144
145           Upon success this function should return TPM_SUCCESS, a failure
146           code otherwise.
147
148           The default implementation returns 0 as the locality.
149
150       tpm_io_getphysicalpresence
151           This function is called when the TPM needs to determine whether
152           physical presence has been asserted. The implementing function
153           should write either TRUE or FALSE into the physicalPresence
154           pointer.
155
156           Upon success this function should return TPM_SUCCESS, a failure
157           code otherwise.
158
159           The default implementation returns FALSE for physical presence.
160

RETURN VALUE

162       Upon successful completion, TPMLIB_MainInit() returns TPM_SUCCESS, an
163       error value otherwise.
164

ERRORS

166       TPM_SUCCESS
167           The function completed sucessfully.
168
169       TPM_FAIL
170           General failure.
171
172       For a complete list of TPM error codes please consult the include file
173       libtpms/tpm_error.h
174

EXAMPLE

176        #include <libtpms/tpm_types.h>
177        #include <libtpms/tpm_library.h>
178        #include <libtpms/tpm_error.h>
179
180        static TPM_MODIFIER_INDICATOR locality;
181
182        static TPM_RESULT mytpm_io_init(void)
183        {
184               return TPM_SUCCESS;
185        }
186
187        static TPM_RESULT tpm_io_getlocality(TPM_MODIFIER_INDICATOR *locModif)
188        {
189               *locModif = locality;
190
191               return TPM_SUCCESS:
192        }
193
194        static TPM_RESULT mytpm_io_getphysicalpresence(TPM_BOOL *phyPres)
195        {
196               *physicalPresence = FALSE;
197
198               return TPM_SUCCESS;
199        }
200
201        int main(void) {
202            TPM_RESULT res;
203            unsigned char *respbuffer;
204            uint32_t resp_size;
205            uint32_t respbufsize;
206            unsigned char *command;
207            uint32_t command_size;
208
209            struct libtpms_callbacks cbs = {
210                .sizeOfStruct               = sizeof(struct libtpms_callbacks),
211                .tpm_nvram_init             = NULL,
212                .tpm_nvram_loaddata         = NULL,
213                .tpm_nvram_storedata        = NULL,
214                .tpm_nvram_deletename       = NULL,
215                .tpm_io_init                = mytpm_io_init,
216                .tpm_io_getlocality         = mytpm_io_getlocality,
217                .tpm_io_getphysicalpresence = mytpm_io_getphysicalpresence,
218            };
219
220
221            [...]
222
223            if (TPMLIB_RegisterCallbacks(cbs) != TPM_SUCCESS) {
224                fprintf(stderr, "Could not register the callbacks.\n");
225                return 1;
226            }
227
228            if (TPMLIB_MainInit()) != TPM_SUCCESS) {
229                fprintf(stderr, "Could not start the TPM.\n");
230                return 1;
231            }
232
233            [...]
234            /* build TPM command */
235            [...]
236
237            res = TPMLIB_Process(&respbuffer, &resp_size,
238                                 &respbufsize,
239                                 command, command_size);
240            [...]
241
242            TPMLIB_Terminate();
243
244            return 0;
245        }
246

SEE ALSO

248       TPMLIB_Process(3), TPMLIB_MainInit(3), TPMLIB_Terminate(3),
249       TPMLIB_DecodeBlobs(3)
250
251
252
253libtpms-0.5.1                     2011-10-12       TPMLIB_REGISTERCALLBACKS(1)
Impressum