-
Notifications
You must be signed in to change notification settings - Fork 240
/
invoke_ta.c
360 lines (299 loc) · 8.1 KB
/
invoke_ta.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2017-2020, Linaro Limited
*/
/* BINARY_PREFIX is expected by teec_trace.h */
#ifndef BINARY_PREFIX
#define BINARY_PREFIX "ckteec"
#endif
#include <errno.h>
#include <inttypes.h>
#include <pkcs11.h>
#include <pkcs11_ta.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <tee_client_api.h>
#include <teec_trace.h>
#include <unistd.h>
#include "ck_helpers.h"
#include "invoke_ta.h"
#include "local_utils.h"
struct ta_context {
pthread_mutex_t init_mutex;
bool initiated;
TEEC_Context context;
TEEC_Session session;
};
static struct ta_context ta_ctx = {
.init_mutex = PTHREAD_MUTEX_INITIALIZER,
};
bool ckteec_invoke_initiated(void)
{
return ta_ctx.initiated;
}
TEEC_SharedMemory *ckteec_alloc_shm(size_t size, enum ckteec_shm_dir dir)
{
TEEC_SharedMemory *shm = NULL;
switch (dir) {
case CKTEEC_SHM_IN:
case CKTEEC_SHM_OUT:
case CKTEEC_SHM_INOUT:
break;
default:
return NULL;
}
shm = calloc(1, sizeof(TEEC_SharedMemory));
if (!shm)
return NULL;
shm->size = size;
if (dir == CKTEEC_SHM_IN || dir == CKTEEC_SHM_INOUT)
shm->flags |= TEEC_MEM_INPUT;
if (dir == CKTEEC_SHM_OUT || dir == CKTEEC_SHM_INOUT)
shm->flags |= TEEC_MEM_OUTPUT;
if (TEEC_AllocateSharedMemory(&ta_ctx.context, shm)) {
free(shm);
return NULL;
}
return shm;
}
TEEC_SharedMemory *ckteec_register_shm(void *buffer, size_t size,
enum ckteec_shm_dir dir)
{
TEEC_SharedMemory *shm = NULL;
switch (dir) {
case CKTEEC_SHM_IN:
case CKTEEC_SHM_OUT:
case CKTEEC_SHM_INOUT:
break;
default:
return NULL;
}
shm = calloc(1, sizeof(TEEC_SharedMemory));
if (!shm)
return NULL;
shm->buffer = buffer;
shm->size = size;
if (dir == CKTEEC_SHM_IN || dir == CKTEEC_SHM_INOUT)
shm->flags |= TEEC_MEM_INPUT;
if (dir == CKTEEC_SHM_OUT || dir == CKTEEC_SHM_INOUT)
shm->flags |= TEEC_MEM_OUTPUT;
if (TEEC_RegisterSharedMemory(&ta_ctx.context, shm)) {
free(shm);
return NULL;
}
return shm;
}
void ckteec_free_shm(TEEC_SharedMemory *shm)
{
TEEC_ReleaseSharedMemory(shm);
free(shm);
}
static bool is_output_shm(TEEC_SharedMemory *shm)
{
return shm && (shm->flags & TEEC_MEM_OUTPUT);
}
CK_RV ckteec_invoke_ta(unsigned long cmd, TEEC_SharedMemory *ctrl,
TEEC_SharedMemory *io1,
TEEC_SharedMemory *io2, size_t *out2_size,
TEEC_SharedMemory *io3, size_t *out3_size)
{
uint32_t command = (uint32_t)cmd;
TEEC_Operation op;
uint32_t origin = 0;
TEEC_Result res = TEEC_ERROR_GENERIC;
uint32_t ta_rc = PKCS11_CKR_GENERAL_ERROR;
if ((is_output_shm(io2) && !out2_size) ||
(is_output_shm(io3) && !out3_size))
return CKR_ARGUMENTS_BAD;
memset(&op, 0, sizeof(op));
if (ctrl && !(ctrl->flags & TEEC_MEM_INPUT &&
ctrl->flags & TEEC_MEM_OUTPUT))
return CKR_ARGUMENTS_BAD;
if (ctrl) {
op.paramTypes |= TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, 0, 0, 0);
op.params[0].memref.parent = ctrl;
} else {
/* TA mandates param#0 as in/out memref for output status */
op.paramTypes |= TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
0, 0, 0);
op.params[0].tmpref.buffer = &ta_rc;
op.params[0].tmpref.size = sizeof(ta_rc);
}
if (io1) {
op.paramTypes |= TEEC_PARAM_TYPES(0, TEEC_MEMREF_WHOLE, 0, 0);
op.params[1].memref.parent = io1;
}
if (io2) {
op.paramTypes |= TEEC_PARAM_TYPES(0, 0, TEEC_MEMREF_WHOLE, 0);
op.params[2].memref.parent = io2;
}
if (io3) {
op.paramTypes |= TEEC_PARAM_TYPES(0, 0, 0, TEEC_MEMREF_WHOLE);
op.params[3].memref.parent = io3;
}
res = TEEC_InvokeCommand(&ta_ctx.session, command, &op, &origin);
switch (res) {
case TEEC_SUCCESS:
/* Get PKCS11 TA return value from ctrl buffer */
if (ctrl) {
if (op.params[0].memref.size == sizeof(ta_rc))
memcpy(&ta_rc, ctrl->buffer, sizeof(ta_rc));
} else {
if (op.params[0].tmpref.size != sizeof(ta_rc))
ta_rc = PKCS11_CKR_GENERAL_ERROR;
}
break;
case TEEC_ERROR_SHORT_BUFFER:
ta_rc = CKR_BUFFER_TOO_SMALL;
break;
case TEEC_ERROR_OUT_OF_MEMORY:
return CKR_DEVICE_MEMORY;
default:
return CKR_GENERAL_ERROR;
}
if (ta_rc == CKR_OK || ta_rc == CKR_BUFFER_TOO_SMALL) {
if (is_output_shm(io2))
*out2_size = op.params[2].memref.size;
if (is_output_shm(io3))
*out3_size = op.params[3].memref.size;
}
return ta_rc;
}
static CK_RV ping_ta(void)
{
TEEC_Operation op = { 0 };
uint32_t origin = 0;
TEEC_Result res = TEEC_SUCCESS;
uint32_t ta_version[3] = { 0 };
uint32_t status = 0;
memset(&op, 0, sizeof(op));
op.params[0].tmpref.buffer = &status;
op.params[0].tmpref.size = sizeof(status);
op.params[2].tmpref.buffer = ta_version;
op.params[2].tmpref.size = sizeof(ta_version);
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE);
res = TEEC_InvokeCommand(&ta_ctx.session, PKCS11_CMD_PING, &op,
&origin);
if (res != TEEC_SUCCESS ||
origin != TEEC_ORIGIN_TRUSTED_APP ||
op.params[0].tmpref.size != sizeof(status) ||
status != PKCS11_CKR_OK)
return CKR_DEVICE_ERROR;
if (ta_version[0] != PKCS11_TA_VERSION_MAJOR &&
ta_version[1] > PKCS11_TA_VERSION_MINOR) {
EMSG("PKCS11 TA version mismatch: %"PRIu32".%"PRIu32".%"PRIu32,
ta_version[0], ta_version[1], ta_version[2]);
return CKR_DEVICE_ERROR;
}
DMSG("PKCS11 TA version %"PRIu32".%"PRIu32".%"PRIu32,
ta_version[0], ta_version[1], ta_version[2]);
return CKR_OK;
}
CK_RV ckteec_invoke_init(void)
{
TEEC_UUID uuid = PKCS11_TA_UUID;
uint32_t origin = 0;
TEEC_Result res = TEEC_SUCCESS;
CK_RV rv = CKR_CRYPTOKI_ALREADY_INITIALIZED;
const char *login_type_env = NULL;
const char *login_gid_env = NULL;
uint32_t login_method = TEEC_LOGIN_PUBLIC;
void *login_data = NULL;
gid_t login_gid = 0;
unsigned long tmpconv = 0;
char *endp = NULL;
int e = 0;
login_type_env = getenv("CKTEEC_LOGIN_TYPE");
if (login_type_env) {
if (strcmp(login_type_env, "public") == 0) {
login_method = TEEC_LOGIN_PUBLIC;
} else if (strcmp(login_type_env, "user") == 0) {
login_method = TEEC_LOGIN_USER;
} else if (strcmp(login_type_env, "group") == 0) {
login_gid_env = getenv("CKTEEC_LOGIN_GID");
if (!login_gid_env || !strlen(login_gid_env)) {
EMSG("missing CKTEEC_LOGIN_GID");
rv = CKR_ARGUMENTS_BAD;
goto out;
}
login_method = TEEC_LOGIN_GROUP;
tmpconv = strtoul(login_gid_env, &endp, 10);
if (errno == ERANGE || tmpconv > (gid_t)-1 ||
(login_gid_env + strlen(login_gid_env) != endp)) {
EMSG("failed to convert CKTEEC_LOGIN_GID");
rv = CKR_ARGUMENTS_BAD;
goto out;
}
login_gid = (gid_t)tmpconv;
login_data = &login_gid;
} else {
EMSG("invalid value for CKTEEC_LOGIN_TYPE");
rv = CKR_ARGUMENTS_BAD;
goto out;
}
}
e = pthread_mutex_lock(&ta_ctx.init_mutex);
if (e)
return CKR_CANT_LOCK;
if (ta_ctx.initiated) {
rv = CKR_CRYPTOKI_ALREADY_INITIALIZED;
goto out;
}
res = TEEC_InitializeContext(NULL, &ta_ctx.context);
if (res != TEEC_SUCCESS) {
EMSG("TEEC init context failed\n");
rv = CKR_DEVICE_ERROR;
goto out;
}
res = TEEC_OpenSession(&ta_ctx.context, &ta_ctx.session, &uuid,
login_method, login_data, NULL, &origin);
if (res != TEEC_SUCCESS) {
EMSG("TEEC open session failed %x from %d\n", res, origin);
TEEC_FinalizeContext(&ta_ctx.context);
rv = CKR_DEVICE_ERROR;
goto out;
}
rv = ping_ta();
if (rv == CKR_OK) {
ta_ctx.initiated = true;
} else {
TEEC_CloseSession(&ta_ctx.session);
TEEC_FinalizeContext(&ta_ctx.context);
}
out:
e = pthread_mutex_unlock(&ta_ctx.init_mutex);
if (e) {
EMSG("pthread_mutex_unlock: %s", strerror(e));
EMSG("terminating...");
exit(EXIT_FAILURE);
}
return rv;
}
CK_RV ckteec_invoke_terminate(void)
{
CK_RV rv = CKR_CRYPTOKI_NOT_INITIALIZED;
int e = 0;
e = pthread_mutex_lock(&ta_ctx.init_mutex);
if (e) {
EMSG("pthread_mutex_lock: %s", strerror(e));
EMSG("terminating...");
exit(EXIT_FAILURE);
}
if (!ta_ctx.initiated)
goto out;
ta_ctx.initiated = false;
TEEC_CloseSession(&ta_ctx.session);
TEEC_FinalizeContext(&ta_ctx.context);
rv = CKR_OK;
out:
e = pthread_mutex_unlock(&ta_ctx.init_mutex);
if (e) {
EMSG("pthread_mutex_unlock: %s", strerror(e));
EMSG("terminating...");
exit(EXIT_FAILURE);
}
return rv;
}