• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3  * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4  * All rights reserved.
5  * Copyright 2019, Intel Corporation
6  * Copyright (c) 2019, Wind River Systems.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  *******************************************************************************/
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <inttypes.h>
40 
41 #include "tctildr.h"
42 #include "tss2_tcti_mssim.h"
43 #ifdef _WIN32
44 #include "tss2_tcti_tbs.h"
45 #else /* _WIN32 */
46 #include "tss2_tcti_device.h"
47 #endif
48 #define LOGMODULE tcti
49 #include "util/log.h"
50 
51 #define ARRAY_SIZE(X) (sizeof(X)/sizeof(X[0]))
52 #define NAME_ARRAY_SIZE 3
53 
54 struct {
55     const char *names [NAME_ARRAY_SIZE];
56     TSS2_TCTI_INIT_FUNC init;
57     char *conf;
58     char *description;
59 } tctis [] = {
60 #if defined(_WIN32) && !defined(__MINGW32__)
61     {
62         .names = {
63             "libtss2-tcti-tbs.so.0",
64             "libtss2-tcti-tbs.so",
65             "tbs",
66         },
67         .init = Tss2_Tcti_Tbs_Init,
68         .description = "Access to TBS",
69     },
70 #elif defined (__VXWORKS__)
71     {
72         .names = {
73             "libtss2-tcti-device.so.0",
74             "libtss2-tcti-device.so",
75             "device",
76         },
77         .init = Tss2_Tcti_Device_Init,
78         .conf = "/tpm0",
79         .description = "Access to /tpm0",
80     },
81 #else /* _WIN32 */
82 #ifdef TCTI_DEVICE
83     {
84         .names = {
85             "libtss2-tcti-device.so.0",
86             "libtss2-tcti-device.so",
87             "device",
88         },
89         .init = Tss2_Tcti_Device_Init,
90         .conf = "/dev/tpmrm0",
91         .description = "Access to /dev/tpmrm0",
92     },
93     {
94         .names = {
95             "libtss2-tcti-device.so.0",
96             "libtss2-tcti-device.so",
97             "device",
98         },
99         .init = Tss2_Tcti_Device_Init,
100         .conf = "/dev/tpm0",
101         .description = "Access to /dev/tpm0",
102     },
103 #endif /* TCTI_DEVICE */
104 #endif /* _WIN32 */
105 #ifdef TCTI_MSSIM
106     {
107         .names = {
108             "libtss2-tcti-mssim.so.0",
109             "libtss2-tcti-mssim.so",
110             "mssim",
111         },
112         .init = Tss2_Tcti_Mssim_Init,
113         .description = "Access to simulator using MS protocol, default conf",
114     },
115 #endif /* TCTI_MSSIM */
116 };
117 TSS2_RC
tctildr_get_default(TSS2_TCTI_CONTEXT ** tcticontext,void ** dlhandle)118 tctildr_get_default(TSS2_TCTI_CONTEXT ** tcticontext, void **dlhandle)
119 {
120     TSS2_RC rc;
121 
122     (void)dlhandle;
123     if (tcticontext == NULL) {
124         LOG_ERROR("tcticontext must not be NULL");
125         return TSS2_TCTI_RC_BAD_REFERENCE;
126     }
127     *tcticontext = NULL;
128 
129     for (size_t i = 0; i < ARRAY_SIZE(tctis); i++) {
130         LOG_DEBUG("Attempting to connect using standard TCTI: %s",
131                   tctis[i].description);
132         rc = tcti_from_init (tctis[i].init,
133                              tctis[i].conf,
134                              tcticontext);
135         if (rc == TSS2_RC_SUCCESS)
136             return TSS2_RC_SUCCESS;
137         LOG_DEBUG("Failed to load standard TCTI number %zu", i);
138     }
139 
140     LOG_ERROR("No standard TCTI could be loaded");
141     return TSS2_TCTI_RC_IO_ERROR;
142 }
143 TSS2_RC
tctildr_get_tcti(const char * name,const char * conf,TSS2_TCTI_CONTEXT ** tcti,void ** data)144 tctildr_get_tcti (const char *name,
145                   const char* conf,
146                   TSS2_TCTI_CONTEXT **tcti,
147                   void **data)
148 {
149     TSS2_RC rc;
150 
151     if (tcti == NULL) {
152         LOG_ERROR("tcticontext must not be NULL");
153         return TSS2_TCTI_RC_BAD_REFERENCE;
154     }
155     *tcti = NULL;
156     if (name == NULL) {
157         return tctildr_get_default (tcti, data);
158     }
159 
160     for (size_t i = 0; i < ARRAY_SIZE(tctis); ++i) {
161         for (size_t j = 0; j < NAME_ARRAY_SIZE; ++j) {
162             if (strcmp (name, tctis[i].names[j]))
163                 continue;
164             LOG_DEBUG("initializing TCTI with name \"%s\"",
165                       tctis[i].names[j]);
166             rc = tcti_from_init (tctis[i].init, conf, tcti);
167             if (rc == TSS2_RC_SUCCESS)
168                 return TSS2_RC_SUCCESS;
169         }
170     }
171     LOG_ERROR("Unable to initialize TCTI with name: \"%s\"", name);
172     return TSS2_TCTI_RC_IO_ERROR;
173 }
174 
175 void
tctildr_finalize_data(void ** data)176 tctildr_finalize_data(void **data)
177 {
178     (void)data;
179     return;
180 }
181 
182 TSS2_RC
tctildr_get_info(const char * name,const TSS2_TCTI_INFO ** info,void ** data)183 tctildr_get_info (const char *name,
184                   const TSS2_TCTI_INFO **info,
185                   void **data)
186 {
187     (void)name;
188     (void)info;
189     (void)data;
190     return TSS2_TCTI_RC_NOT_SUPPORTED;
191 }
192