/
tctildr-dl.c
328 lines (295 loc) · 8.65 KB
/
tctildr-dl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* SPDX-License-Identifier: BSD-2-Clause */
/*******************************************************************************
* Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
* Copyright 2019, Intel Corporation
* All rights reserved.
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <dlfcn.h>
#include <limits.h>
#include <stdio.h>
#include "tss2_tcti.h"
#include "tctildr-interface.h"
#include "tctildr.h"
#define LOGMODULE tcti
#include "util/log.h"
#define ARRAY_SIZE(X) (sizeof(X)/sizeof(X[0]))
struct {
char *file;
char *conf;
char *description;
} tctis[] = {
{
.file = "libtss2-tcti-default.so",
.description = "Access libtss2-tcti-default.so",
},
{
.file = "libtss2-tcti-tabrmd.so.0",
.description = "Access libtss2-tcti-tabrmd.so",
},
{
.file = "libtss2-tcti-device.so.0",
.conf = "/dev/tpmrm0",
.description = "Access libtss2-tcti-device.so.0 with /dev/tpmrm0",
},
{
.file = "libtss2-tcti-device.so.0",
.conf = "/dev/tpm0",
.description = "Access libtss2-tcti-device.so.0 with /dev/tpm0",
},
{
.file = "libtss2-tcti-device.so.0",
.conf = "/dev/tcm0",
.description = "Access libtss2-tcti-device.so.0 with /dev/tcm0",
},
{
.file = "libtss2-tcti-swtpm.so.0",
.description = "Access to libtss2-tcti-swtpm.so",
},
{
.file = "libtss2-tcti-mssim.so.0",
.description = "Access to libtss2-tcti-mssim.so",
},
};
const TSS2_TCTI_INFO*
info_from_handle (void *dlhandle)
{
TSS2_TCTI_INFO_FUNC info_func;
if (dlhandle == NULL)
return NULL;
info_func = dlsym (dlhandle, TSS2_TCTI_INFO_SYMBOL);
if (info_func == NULL) {
LOG_ERROR ("Failed to get reference to TSS2_TCTI_INFO_SYMBOL: %s",
dlerror());
return NULL;
}
return info_func ();
}
TSS2_RC
handle_from_name(const char *file,
void **handle)
{
size_t size;
char file_xfrm[PATH_MAX];
const char *formats[] = {
/* <name> */
"%s",
/* libtss2-tcti-<name>.so.0 */
FMT_TCTI_PREFIX "%s" FMT_LIB_SUFFIX_0,
/* libtss2-tcti-<name>.so */
FMT_TCTI_PREFIX "%s" FMT_LIB_SUFFIX,
/* libtss2-<name>.so.0 */
FMT_TSS_PREFIX "%s" FMT_LIB_SUFFIX_0,
/* libtss2-<name>.so */
FMT_TSS_PREFIX "%s" FMT_LIB_SUFFIX,
};
if (handle == NULL) {
return TSS2_TCTI_RC_BAD_REFERENCE;
}
for (size_t i = 0; i < ARRAY_SIZE(formats); i++) {
size = snprintf(file_xfrm, sizeof(file_xfrm), formats[i], file);
if (size >= sizeof(file_xfrm)) {
LOG_ERROR("TCTI name truncated in transform.");
return TSS2_TCTI_RC_BAD_VALUE;
}
*handle = dlopen(file_xfrm, RTLD_NOW);
if (*handle != NULL) {
return TSS2_RC_SUCCESS;
} else {
LOG_DEBUG("Could not load TCTI file \"%s\": %s", file, dlerror());
}
}
return TSS2_TCTI_RC_NOT_SUPPORTED;
}
TSS2_RC
tcti_from_file(const char *file,
const char* conf,
TSS2_TCTI_CONTEXT **tcti,
void **dlhandle)
{
TSS2_RC r;
void *handle;
TSS2_TCTI_INFO_FUNC infof;
LOG_TRACE("Attempting to load TCTI file: %s", file);
if (tcti == NULL) {
return TSS2_TCTI_RC_BAD_REFERENCE;
}
r = handle_from_name(file, &handle);
if (r != TSS2_RC_SUCCESS) {
return r;
}
infof = (TSS2_TCTI_INFO_FUNC) dlsym(handle, TSS2_TCTI_INFO_SYMBOL);
if (infof == NULL) {
LOG_ERROR("Info not found in TCTI file: %s", file);
dlclose(handle);
return TSS2_ESYS_RC_BAD_REFERENCE;
}
r = tcti_from_info(infof, conf, tcti);
if (r != TSS2_RC_SUCCESS) {
LOG_ERROR("Could not initialize TCTI file: %s", file);
dlclose(handle);
return r;
}
if (dlhandle)
*dlhandle = handle;
LOG_DEBUG("Initialized TCTI file: %s", file);
return TSS2_RC_SUCCESS;
}
TSS2_RC
get_info_default(const TSS2_TCTI_INFO **info,
void **dlhandle)
{
void *handle = NULL;
const TSS2_TCTI_INFO *info_src;
char *name = NULL;
TSS2_RC rc = TSS2_TCTI_RC_GENERAL_FAILURE;
LOG_DEBUG("%s", __func__);
if (info == NULL || dlhandle == NULL) {
LOG_ERROR("parameters cannot be NULL");
return TSS2_TCTI_RC_BAD_REFERENCE;
}
#ifdef ESYS_TCTI_DEFAULT_MODULE
name = ESYS_TCTI_DEFAULT_MODULE;
LOG_DEBUG("name: %s", name);
rc = handle_from_name (name, &handle);
if (rc != TSS2_RC_SUCCESS)
return rc;
else if (handle == NULL)
return TSS2_TCTI_RC_IO_ERROR;
#else
size_t i;
if (ARRAY_SIZE(tctis) == 0) {
LOG_ERROR("No default TCTIs configured during compilation");
return TSS2_TCTI_RC_IO_ERROR;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
for (i = 0; i < ARRAY_SIZE(tctis); i++) {
#pragma GCC diagnostic pop
name = tctis[i].file;
LOG_DEBUG("name: %s", name);
if (name == NULL) {
continue;
}
rc = handle_from_name (name, &handle);
if (rc != TSS2_RC_SUCCESS || handle == NULL) {
LOG_DEBUG("Failed to get handle for TCTI with name: %s", name);
continue;
}
break;
}
#endif /* ESYS_TCTI_DEFAULT_MODULE */
info_src = info_from_handle (handle);
if (info_src != NULL) {
*info = info_src;
} else {
tctildr_finalize_data (&handle);
rc = TSS2_TCTI_RC_GENERAL_FAILURE;
}
*dlhandle = handle;
return rc;
}
TSS2_RC
tctildr_get_default(TSS2_TCTI_CONTEXT ** tcticontext, void **dlhandle)
{
if (tcticontext == NULL) {
LOG_ERROR("tcticontext must not be NULL");
return TSS2_TCTI_RC_BAD_REFERENCE;
}
*tcticontext = NULL;
#ifdef ESYS_TCTI_DEFAULT_MODULE
#ifdef ESYS_TCTI_DEFAULT_CONFIG
const char *config = ESYS_TCTI_DEFAULT_CONFIG;
#else /* ESYS_TCTI_DEFAULT_CONFIG */
const char *config = NULL;
#endif /* ESYS_TCTI_DEFAULT_CONFIG */
LOG_DEBUG("Attempting to initialize TCTI defined during compilation: %s:%s",
ESYS_TCTI_DEFAULT_MODULE, config);
return tcti_from_file(ESYS_TCTI_DEFAULT_MODULE, config, tcticontext,
dlhandle);
#else /* ESYS_TCTI_DEFAULT_MODULE */
TSS2_RC r;
size_t i;
if (ARRAY_SIZE(tctis) == 0) {
LOG_ERROR("No default TCTIs configured during compilation");
return TSS2_TCTI_RC_IO_ERROR;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
for (i = 0; i < ARRAY_SIZE(tctis); i++) {
#pragma GCC diagnostic pop
LOG_DEBUG("Attempting to connect using standard TCTI: %s",
tctis[i].description);
r = tcti_from_file(tctis[i].file, tctis[i].conf, tcticontext,
dlhandle);
if (r == TSS2_RC_SUCCESS)
return TSS2_RC_SUCCESS;
LOG_DEBUG("Failed to load standard TCTI number %zu", i);
}
LOG_ERROR("No standard TCTI could be loaded");
return TSS2_TCTI_RC_IO_ERROR;
#endif /* ESYS_TCTI_DEFAULT_MODULE */
}
TSS2_RC
info_from_name (const char *name,
const TSS2_TCTI_INFO **info,
void **data)
{
TSS2_RC rc;
if (data == NULL || info == NULL)
return TSS2_TCTI_RC_BAD_REFERENCE;
rc = handle_from_name (name, data);
if (rc != TSS2_RC_SUCCESS)
return rc;
*info = info_from_handle (*data);
if (*info == NULL) {
tctildr_finalize_data (data);
return TSS2_TCTI_RC_IO_ERROR;
}
return rc;
}
TSS2_RC
tctildr_get_info(const char *name,
const TSS2_TCTI_INFO **info,
void **data)
{
if (info == NULL) {
LOG_ERROR("info must not be NULL");
return TSS2_TCTI_RC_BAD_REFERENCE;
}
if (name != NULL) {
return info_from_name (name, info, data);
} else {
return get_info_default (info, data);
}
}
TSS2_RC
tctildr_get_tcti(const char *name,
const char* conf,
TSS2_TCTI_CONTEXT **tcti,
void **data)
{
LOG_DEBUG("name: \"%s\", conf: \"%s\"", name, conf);
if (tcti == NULL) {
LOG_ERROR("tcticontext must not be NULL");
return TSS2_TCTI_RC_BAD_REFERENCE;
}
*tcti = NULL;
if (name == NULL) {
return tctildr_get_default (tcti, data);
}
return tcti_from_file (name, conf, tcti, data);
}
void
tctildr_finalize_data (void **data)
{
if (data != NULL && *data != NULL) {
dlclose(*data);
*data = NULL;
}
}