Skip to content

Commit f216caf

Browse files
author
Hernan Gatta
committed
OCALL during session open
1 parent a8ce908 commit f216caf

File tree

3 files changed

+123
-19
lines changed

3 files changed

+123
-19
lines changed

ocall/host/main.c

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ TEEC_Result ocall_handler(TEEC_UUID *taUUID, uint32_t commandId,
5858
printf("\n");
5959

6060
switch (commandId) {
61+
case CA_OCALL_CMD_REPLY_SESSION_OPEN:
62+
expected_pt = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
63+
TEEC_NONE,
64+
TEEC_NONE,
65+
TEEC_MEMREF_TEMP_INPUT);
66+
if (paramTypes != expected_pt) {
67+
fprintf(stderr, "Bad parameter types\n");
68+
return TEEC_ERROR_BAD_PARAMETERS;
69+
}
70+
if (!params[3].tmpref.buffer) {
71+
fprintf(stderr, "No buffer\n");
72+
return TEEC_ERROR_BAD_PARAMETERS;
73+
}
74+
75+
/* Print out the OCALL's INPUT/INOUT parameters */
76+
printf("Input values: 0x%x, 0x%x\n", params[0].value.a,
77+
params[0].value.b);
78+
printf("Input string: %s\n", (char *)params[3].tmpref.buffer);
79+
80+
/* Set the OCALL's INOUT parameters */
81+
params[0].value.a = 0xCDDC1001;
82+
params[0].value.b = 0xFFFFCAFE;
83+
break;
6184
case CA_OCALL_CMD_REPLY_TA:
6285
expected_pt = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
6386
TEEC_VALUE_INOUT,
@@ -94,13 +117,13 @@ TEEC_Result ocall_handler(TEEC_UUID *taUUID, uint32_t commandId,
94117

95118
params[3].tmpref.size = strlen(msg) + 1;
96119
memcpy(params[3].tmpref.buffer, msg, params[3].tmpref.size);
97-
98-
printf("OCALL handled\n");
99120
break;
100121
default:
101122
fprintf(stderr, "Bad function ID\n");
102123
return TEEC_ERROR_BAD_PARAMETERS;
103124
}
125+
126+
printf("OCALL handled\n");
104127
return TEEC_SUCCESS;
105128
}
106129

@@ -109,24 +132,26 @@ int main(int argc, char* argv[])
109132
TEEC_Context ctx;
110133
TEEC_Session sess;
111134
TEEC_UUID uuid = TA_OCALL_UUID;
112-
TEEC_Operation op = { 0 };
135+
TEEC_Operation op;
113136

114137
TEEC_Result res;
115138
uint32_t err_origin;
116139

117140
char buf[128];
141+
char buf2[128];
118142
char *msg1 = "This string was sent by the CA";
119143
const char *msg2 = "The CA thinks this is a fun riddle";
120144

121145
/*
122-
* The TEE context OCALL setting allows setting the callback handler for
123-
* when an OCALL arrives from the TA. This handler is effectively the
124-
* equivalent of TA_InvokeCommandEntryPoint. Additionally, one may set
125-
* an arbitrary pointer that will be passed to the OCALL handler when
126-
* invoked.
146+
* The TEE context OCALL setting allows specifying the callback handler
147+
* for when an OCALL arrives from the TA. This handler is effectively
148+
* the equivalent of TA_InvokeCommandEntryPoint, but on the CA side.
149+
* Additionally, one may set an arbitrary pointer that will be passed
150+
* to the OCALL handler when invoked.
127151
*
128152
* NOTE: You must pass this setting to the TEE context initialization
129-
* routine to receive OCALLs.
153+
* routine to receive OCALLs; otherwise, all OCALLs will return
154+
* a failure code.
130155
*/
131156
TEEC_ContextSettingOcall ocall_setting = {
132157
.handler = ocall_handler,
@@ -145,8 +170,8 @@ int main(int argc, char* argv[])
145170
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
146171

147172
/*
148-
* The session data settings allows attaching an arbitrary pointer to
149-
* the session. This pointer will be passed to the OCALL handler when
173+
* The session data setting allows attaching an arbitrary pointer to the
174+
* session. This pointer will be passed to the OCALL handler when
150175
* invoked.
151176
*
152177
* NOTE: This is optional; you can use TEEC_OpenSession as well even if
@@ -162,13 +187,32 @@ int main(int argc, char* argv[])
162187
.u.data = &data_setting,
163188
};
164189

165-
/* Open a session with settings */
190+
/* Set up the parameters for the TA's session open handler */
191+
memset(&op, 0, sizeof(op));
192+
op.paramTypes = TEEC_PARAM_TYPES(
193+
TEEC_VALUE_INPUT,
194+
TEEC_MEMREF_TEMP_INPUT,
195+
TEEC_NONE,
196+
TEEC_NONE);
197+
198+
op.params[0].value.a = 0x0000CAFE;
199+
op.params[0].value.b = 0xCAFE0000;
200+
201+
op.params[1].tmpref.buffer = (void *)msg2;
202+
op.params[1].tmpref.size = strlen(msg2) + 1;
203+
204+
/* Open a session with settings; the sample TA will issue an OCALL */
166205
res = TEEC_OpenSession2(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
167-
NULL, &err_origin, &session_settings, 1);
206+
&op, &err_origin, &session_settings, 1);
168207
if (res != TEEC_SUCCESS)
169208
errx(1, "TEEC_OpenSessionEx failed with code 0x%x origin 0x%x",
170209
res, err_origin);
171210

211+
/*
212+
* The code below executes after the OCALL has been handled in the
213+
* callback at the top of this file.
214+
*/
215+
172216
/*
173217
* Set up the parameters for the function invocation. These are just to
174218
* show that the CA can pass parameters to the TA and that during the
@@ -177,6 +221,7 @@ int main(int argc, char* argv[])
177221
* parameters passed from the CA to the TA do not interfere with those
178222
* passed from the TA to the CA, and vice-versa.
179223
*/
224+
memset(&op, 0, sizeof(op));
180225
op.paramTypes = TEEC_PARAM_TYPES(
181226
TEEC_VALUE_INPUT,
182227
TEEC_VALUE_INOUT,
@@ -203,8 +248,8 @@ int main(int argc, char* argv[])
203248
res, err_origin);
204249

205250
/*
206-
* The code below executes after the OCALL has been handled in the
207-
* callback at the top of this file.
251+
* The code below once again executes after the OCALL has been handled
252+
* in the callback at the top of this file.
208253
*/
209254

210255
/*

ocall/ta/include/ocall_ta.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#define TA_OCALL_CMD_CALL_CA 0
1717

18-
#define CA_OCALL_CMD_REPLY_TA 100
18+
#define CA_OCALL_CMD_REPLY_SESSION_OPEN 99
19+
#define CA_OCALL_CMD_REPLY_TA 100
1920

2021
#endif /*TA_OCALL_H*/

ocall/ta/ocall_ta.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <string.h>
1010
#include <tee_internal_api.h>
1111
#include <tee_internal_api_extensions.h>
12-
12+
#pragma GCC push_options
13+
#pragma GCC optimize ("-O0")
1314
static TEE_Result call_ca(uint32_t param_types,
1415
TEE_Param params[TEE_NUM_PARAMS])
1516
{
@@ -31,7 +32,7 @@ static TEE_Result call_ca(uint32_t param_types,
3132
TEE_PARAM_TYPE_MEMREF_INPUT,
3233
TEE_PARAM_TYPE_MEMREF_INOUT);
3334

34-
/* Parameter types for the OCALL (could be different from the above) */
35+
/* Parameter types for the OCALL (can be different from the above) */
3536
const uint32_t ocall_param_types =
3637
TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
3738
TEE_PARAM_TYPE_VALUE_INOUT,
@@ -49,6 +50,7 @@ static TEE_Result call_ca(uint32_t param_types,
4950
if (params[3].memref.size < strlen(msg2) + 1)
5051
return TEE_ERROR_BAD_PARAMETERS;
5152

53+
/* Print the invocation's INPUT/INOUT parameters */
5254
DMSG("Input values: %u, %u", params[0].value.a, params[0].value.b);
5355
DMSG("Inout values: %u, %u", params[1].value.a, params[1].value.b);
5456

@@ -94,6 +96,7 @@ static TEE_Result call_ca(uint32_t param_types,
9496
return TEE_ERROR_BAD_PARAMETERS;
9597
}
9698

99+
/* Print the OCALL's INOUT parameters */
97100
DMSG("Output values: %u, %u", ocall_params[1].value.a,
98101
ocall_params[1].value.b);
99102
DMSG("Output string: \"%s\"", (char *)ocall_params[3].memref.buffer);
@@ -112,10 +115,64 @@ void TA_DestroyEntryPoint(void)
112115
/* NOTHING */
113116
}
114117

115-
TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types __unused,
118+
TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
116119
TEE_Param params[4] __unused,
117120
void **sess_ctx __unused)
118121
{
122+
const char *msg = "The TA says hello during session open";
123+
124+
TEE_Param ocall_params[TEE_NUM_PARAMS];
125+
126+
TEE_Result res = TEE_SUCCESS;
127+
uint32_t eorig = TEE_ORIGIN_TRUSTED_APP;
128+
129+
/* Expected parameter types for the function invocation */
130+
const uint32_t expected_pt =
131+
TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
132+
TEE_PARAM_TYPE_MEMREF_INPUT,
133+
TEE_PARAM_TYPE_NONE,
134+
TEE_PARAM_TYPE_NONE);
135+
136+
/* Parameter types for the OCALL (can be different from the above) */
137+
const uint32_t ocall_param_types =
138+
TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
139+
TEE_PARAM_TYPE_NONE,
140+
TEE_PARAM_TYPE_NONE,
141+
TEE_PARAM_TYPE_MEMREF_INPUT);
142+
143+
if (param_types != expected_pt)
144+
return TEE_ERROR_BAD_PARAMETERS;
145+
146+
if (!params[1].memref.buffer) {
147+
EMSG("No buffer");
148+
return TEE_ERROR_BAD_PARAMETERS;
149+
}
150+
151+
/* Print the invocation's INPUT parameters */
152+
DMSG("Input values: 0x%x, 0x%x", params[0].value.a, params[0].value.b);
153+
DMSG("Input string: %s", (char *)params[1].memref.buffer);
154+
DMSG("Input size: %u", params[1].memref.size);
155+
156+
/* Set the OCALL's parameters */
157+
ocall_params[0].value.a = 0xFCFAFFFE;
158+
ocall_params[0].value.b = 0x10CDDC01;
159+
160+
ocall_params[3].memref.buffer = (void *)msg;
161+
ocall_params[3].memref.size = strlen(msg) + 1;
162+
163+
res = TEE_InvokeCACommand(TEE_TIMEOUT_INFINITE,
164+
CA_OCALL_CMD_REPLY_SESSION_OPEN,
165+
ocall_param_types, ocall_params, &eorig);
166+
if (res != TEE_SUCCESS) {
167+
EMSG("TEE_InvokeCACommand failed with code 0x%x origin 0x%x",
168+
res, eorig);
169+
return res;
170+
}
171+
172+
/* Print the OCALL's INOUT parameters */
173+
DMSG("Output values: 0x%x, 0x%x", ocall_params[0].value.a,
174+
ocall_params[0].value.b);
175+
119176
return TEE_SUCCESS;
120177
}
121178

@@ -135,3 +192,4 @@ TEE_Result TA_InvokeCommandEntryPoint(void *sess_ctx __unused, uint32_t cmd_id,
135192
return TEE_ERROR_BAD_PARAMETERS;
136193
}
137194
}
195+
#pragma GCC pop_options

0 commit comments

Comments
 (0)