• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************
3  * Copyright (c) 2017-2018, Intel Corporation
4  *
5  * All rights reserved.
6  ***********************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include <errno.h>
12 #include <inttypes.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "tss2_tcti_device.h"
18 #include "tss2_tcti_mssim.h"
19 #ifdef TCTI_FUZZING
20 #include "tss2_tcti_fuzzing.h"
21 #endif /* TCTI_FUZZING */
22 
23 #include "context-util.h"
24 #include "tss2-tcti/tcti-mssim.h"
25 
26 #ifdef TCTI_DEVICE
27 /*
28  * Initialize a TSS2_TCTI_CONTEXT for the device TCTI.
29  */
30 TSS2_TCTI_CONTEXT *
tcti_device_init(char const * device_path)31 tcti_device_init(char const *device_path)
32 {
33     size_t size;
34     TSS2_RC rc;
35     TSS2_TCTI_CONTEXT *tcti_ctx;
36 
37     rc = Tss2_Tcti_Device_Init(NULL, &size, 0);
38     if (rc != TSS2_RC_SUCCESS) {
39         fprintf(stderr,
40                 "Failed to get allocation size for device tcti context: "
41                 "0x%x\n", rc);
42         return NULL;
43     }
44     tcti_ctx = (TSS2_TCTI_CONTEXT *) calloc(1, size);
45     if (tcti_ctx == NULL) {
46         fprintf(stderr,
47                 "Allocation for device TCTI context failed: %s\n",
48                 strerror(errno));
49         return NULL;
50     }
51     rc = Tss2_Tcti_Device_Init(tcti_ctx, &size, device_path);
52     if (rc != TSS2_RC_SUCCESS) {
53         fprintf(stderr, "Failed to initialize device TCTI context: 0x%x\n", rc);
54         free(tcti_ctx);
55         return NULL;
56     }
57     return tcti_ctx;
58 }
59 #endif /* TCTI_DEVICE */
60 
61 #ifdef TCTI_MSSIM
62 /*
63  * Initialize a socket TCTI instance using the provided options structure.
64  * The hostname and port are the only configuration options used.
65  * The caller is returned a TCTI context structure that is allocated by this
66  * function. This structure must be freed by the caller.
67  */
68 TSS2_TCTI_CONTEXT *
tcti_socket_init(char const * host,uint16_t port)69 tcti_socket_init(char const *host, uint16_t port)
70 {
71     size_t size;
72     TSS2_RC rc;
73     TSS2_TCTI_CONTEXT *tcti_ctx;
74     char conf_str[TCTI_MSSIM_CONF_MAX] = { 0 };
75 
76     snprintf(conf_str, TCTI_MSSIM_CONF_MAX, "host=%s,port=%" PRIu16, host, port);
77     rc = Tss2_Tcti_Mssim_Init(NULL, &size, conf_str);
78     if (rc != TSS2_RC_SUCCESS) {
79         fprintf(stderr, "Faled to get allocation size for tcti context: "
80                 "0x%x\n", rc);
81         return NULL;
82     }
83     tcti_ctx = (TSS2_TCTI_CONTEXT *) calloc(1, size);
84     if (tcti_ctx == NULL) {
85         fprintf(stderr, "Allocation for tcti context failed: %s\n",
86                 strerror(errno));
87         return NULL;
88     }
89     rc = Tss2_Tcti_Mssim_Init(tcti_ctx, &size, conf_str);
90     if (rc != TSS2_RC_SUCCESS) {
91         fprintf(stderr, "Failed to initialize tcti context: 0x%x\n", rc);
92         free(tcti_ctx);
93         return NULL;
94     }
95     return tcti_ctx;
96 }
97 #endif /* TCTI_MSSIM */
98 
99 #ifdef TCTI_FUZZING
100 /*
101  * Initialize a fuzzing TCTI instance using the provided options structure.
102  * The fuzzing_lengths.log file is the only configuration option used.
103  * The caller is returned a TCTI context structure that is allocated by this
104  * function. This structure must be freed by the caller.
105  */
106 TSS2_TCTI_CONTEXT *
tcti_fuzzing_init()107 tcti_fuzzing_init()
108 {
109     size_t size;
110     TSS2_RC rc;
111     TSS2_TCTI_CONTEXT *tcti_ctx;
112 
113     rc = Tss2_Tcti_Fuzzing_Init(NULL, &size, NULL);
114     if (rc != TSS2_RC_SUCCESS) {
115         fprintf(stderr, "Faled to get allocation size for tcti context: "
116                 "0x%x\n", rc);
117         return NULL;
118     }
119     tcti_ctx = (TSS2_TCTI_CONTEXT *) calloc(1, size);
120     if (tcti_ctx == NULL) {
121         fprintf(stderr, "Allocation for tcti context failed: %s\n",
122                 strerror(errno));
123         return NULL;
124     }
125     rc = Tss2_Tcti_Fuzzing_Init(tcti_ctx, &size, NULL);
126     if (rc != TSS2_RC_SUCCESS) {
127         fprintf(stderr, "Failed to initialize tcti context: 0x%x\n", rc);
128         free(tcti_ctx);
129         return NULL;
130     }
131     return tcti_ctx;
132 }
133 #endif /* TCTI_FUZZING */
134 
135 /*
136  * Initialize a SAPI context using the TCTI context provided by the caller.
137  * This function allocates memory for the SAPI context and returns it to the
138  * caller. This memory must be freed by the caller.
139  */
140 TSS2_SYS_CONTEXT *
sapi_init_from_tcti_ctx(TSS2_TCTI_CONTEXT * tcti_ctx)141 sapi_init_from_tcti_ctx(TSS2_TCTI_CONTEXT * tcti_ctx)
142 {
143     TSS2_SYS_CONTEXT *sapi_ctx;
144     TSS2_RC rc;
145     size_t size;
146     TSS2_ABI_VERSION abi_version = {
147         .tssCreator = 1,
148         .tssFamily = 2,
149         .tssLevel = 1,
150         .tssVersion = 108,
151     };
152 
153     size = Tss2_Sys_GetContextSize(0);
154     sapi_ctx = (TSS2_SYS_CONTEXT *) calloc(1, size);
155     if (sapi_ctx == NULL) {
156         fprintf(stderr,
157                 "Failed to allocate 0x%zx bytes for the SAPI context\n", size);
158         return NULL;
159     }
160     rc = Tss2_Sys_Initialize(sapi_ctx, size, tcti_ctx, &abi_version);
161     if (rc != TSS2_RC_SUCCESS) {
162         fprintf(stderr, "Failed to initialize SAPI context: 0x%x\n", rc);
163         free(sapi_ctx);
164         return NULL;
165     }
166     return sapi_ctx;
167 }
168 
169 /*
170  * Initialize a SAPI context to use a socket TCTI. Get configuration data from
171  * the provided structure.
172  */
173 TSS2_SYS_CONTEXT *
sapi_init_from_opts(test_opts_t * options)174 sapi_init_from_opts(test_opts_t * options)
175 {
176     TSS2_TCTI_CONTEXT *tcti_ctx;
177     TSS2_SYS_CONTEXT *sapi_ctx;
178 
179     tcti_ctx = tcti_init_from_opts(options);
180     if (tcti_ctx == NULL)
181         return NULL;
182     sapi_ctx = sapi_init_from_tcti_ctx(tcti_ctx);
183     if (sapi_ctx == NULL)
184         return NULL;
185     return sapi_ctx;
186 }
187 
188 /*
189  * Initialize a TSS2_TCTI_CONTEXT using whatever TCTI data is in the options
190  * structure. This is a mechanism that allows the calling application to be
191  * mostly ignorant of which TCTI they're creating / initializing.
192  */
193 TSS2_TCTI_CONTEXT *
tcti_init_from_opts(test_opts_t * options)194 tcti_init_from_opts(test_opts_t * options)
195 {
196     switch (options->tcti_type) {
197 #ifdef TCTI_DEVICE
198     case DEVICE_TCTI:
199         return tcti_device_init(options->device_file);
200 #endif /* TCTI_DEVICE */
201 #ifdef TCTI_MSSIM
202     case SOCKET_TCTI:
203         return tcti_socket_init(options->socket_address, options->socket_port);
204 #endif /* TCTI_MSSIM */
205 #ifdef TCTI_FUZZING
206     case FUZZING_TCTI:
207         return tcti_fuzzing_init();
208 #endif /* TCTI_FUZZING */
209     default:
210         return NULL;
211     }
212 }
213 
214 /*
215  * Teardown / Finalize TCTI context and free memory.
216  */
217 void
tcti_teardown(TSS2_TCTI_CONTEXT * tcti_context)218 tcti_teardown(TSS2_TCTI_CONTEXT * tcti_context)
219 {
220     if (tcti_context) {
221         Tss2_Tcti_Finalize(tcti_context);
222         free(tcti_context);
223     }
224 }
225 
226 /*
227  * Teardown and free the resources associated with a SAPI context structure.
228  */
229 void
sapi_teardown(TSS2_SYS_CONTEXT * sapi_context)230 sapi_teardown(TSS2_SYS_CONTEXT * sapi_context)
231 {
232     Tss2_Sys_Finalize(sapi_context);
233     free(sapi_context);
234 }
235 
236 /*
237  * Teardown and free the resources associated with a SAPI context structure.
238  * This includes tearing down the TCTI as well.
239  */
240 void
sapi_teardown_full(TSS2_SYS_CONTEXT * sapi_context)241 sapi_teardown_full(TSS2_SYS_CONTEXT * sapi_context)
242 {
243     TSS2_TCTI_CONTEXT *tcti_context = NULL;
244     TSS2_RC rc;
245 
246     rc = Tss2_Sys_GetTctiContext(sapi_context, &tcti_context);
247     if (rc != TSS2_RC_SUCCESS)
248         return;
249 
250     sapi_teardown(sapi_context);
251     tcti_teardown(tcti_context);
252 }
253