|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Microsoft Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: BSD-2-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +#include <err.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include <tee_client_api.h> |
| 13 | +#include <tee_client_api_extensions.h> |
| 14 | + |
| 15 | +#include <ocall_ta.h> |
| 16 | + |
| 17 | +static void print_uuid(TEEC_UUID *uuid) |
| 18 | +{ |
| 19 | + printf("%x-%x-%x-%x%x-%x%x%x%x%x%x", |
| 20 | + uuid->timeLow, |
| 21 | + uuid->timeMid, |
| 22 | + uuid->timeHiAndVersion, |
| 23 | + uuid->clockSeqAndNode[0], |
| 24 | + uuid->clockSeqAndNode[1], |
| 25 | + uuid->clockSeqAndNode[2], |
| 26 | + uuid->clockSeqAndNode[3], |
| 27 | + uuid->clockSeqAndNode[4], |
| 28 | + uuid->clockSeqAndNode[5], |
| 29 | + uuid->clockSeqAndNode[6], |
| 30 | + uuid->clockSeqAndNode[7]); |
| 31 | +} |
| 32 | + |
| 33 | +/* |
| 34 | + * This function is called by the TEE Client API whenever an OCALL arrives from |
| 35 | + * the TA. |
| 36 | + * |
| 37 | + * The 'taUUID' parameter carries the UUID of the TA that sent the OCALL. Since |
| 38 | + * a TA can open a session to another TA, it is possible to receive OCALLs from |
| 39 | + * other TAs that your TA calls into, if any. |
| 40 | + * |
| 41 | + * The 'commandId' indicates which function the TA wishes the CA to run. |
| 42 | + * |
| 43 | + * 'ctxData' is the arbitrary pointer that was set via the TEE context OCALL |
| 44 | + * setting, if any. Similarly, 'sessionData' is the arbitrary pointer set via |
| 45 | + * the session data setting, if it was supplied, or NULL. |
| 46 | + */ |
| 47 | +TEEC_Result ocall_handler(TEEC_UUID *taUUID, uint32_t commandId, |
| 48 | + uint32_t paramTypes, |
| 49 | + TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT], |
| 50 | + void *ctxData, void *sessionData) |
| 51 | +{ |
| 52 | + const char *msg = "This string was sent by the CA"; |
| 53 | + uint32_t expected_pt; |
| 54 | + |
| 55 | + printf("Received an OCALL for Command Id: %u\n", commandId); |
| 56 | + printf("The TA that sent it is: "); |
| 57 | + print_uuid(taUUID); |
| 58 | + printf("\n"); |
| 59 | + |
| 60 | + 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; |
| 84 | + case CA_OCALL_CMD_REPLY_TA: |
| 85 | + expected_pt = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 86 | + TEEC_VALUE_INOUT, |
| 87 | + TEEC_MEMREF_TEMP_INPUT, |
| 88 | + TEEC_MEMREF_TEMP_INOUT); |
| 89 | + if (paramTypes != expected_pt) { |
| 90 | + fprintf(stderr, "Bad parameter types\n"); |
| 91 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 92 | + } |
| 93 | + if (!params[2].tmpref.buffer || !params[3].tmpref.buffer) { |
| 94 | + fprintf(stderr, "No buffer(s)\n"); |
| 95 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 96 | + } |
| 97 | + if (params[3].tmpref.size < strlen(msg) + 1) { |
| 98 | + fprintf(stderr, "Bad parameters\n"); |
| 99 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 100 | + } |
| 101 | + |
| 102 | + /* Print out the OCALL's INPUT/INOUT parameters */ |
| 103 | + printf("Input values: %u, %u\n", params[0].value.a, |
| 104 | + params[0].value.b); |
| 105 | + printf("Inout values: %u, %u\n", params[1].value.a, |
| 106 | + params[1].value.b); |
| 107 | + |
| 108 | + printf("Input string: %s\n", (char *)params[2].tmpref.buffer); |
| 109 | + printf("Input size: %zu\n", params[2].tmpref.size); |
| 110 | + |
| 111 | + printf("Inout string: %s\n", (char *)params[3].tmpref.buffer); |
| 112 | + printf("Inout size: %zu\n", params[3].tmpref.size); |
| 113 | + |
| 114 | + /* Set the OCALL's INOUT parameters */ |
| 115 | + params[1].value.a = 0x3; |
| 116 | + params[1].value.b = 0x4; |
| 117 | + |
| 118 | + params[3].tmpref.size = strlen(msg) + 1; |
| 119 | + memcpy(params[3].tmpref.buffer, msg, params[3].tmpref.size); |
| 120 | + break; |
| 121 | + default: |
| 122 | + fprintf(stderr, "Bad function ID\n"); |
| 123 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 124 | + } |
| 125 | + |
| 126 | + printf("OCALL handled\n"); |
| 127 | + return TEEC_SUCCESS; |
| 128 | +} |
| 129 | + |
| 130 | +int main(int argc, char* argv[]) |
| 131 | +{ |
| 132 | + TEEC_Context ctx; |
| 133 | + TEEC_Session sess; |
| 134 | + TEEC_UUID uuid = TA_OCALL_UUID; |
| 135 | + TEEC_Operation op; |
| 136 | + |
| 137 | + TEEC_Result res; |
| 138 | + uint32_t err_origin; |
| 139 | + |
| 140 | + char buf[128]; |
| 141 | + char buf2[128]; |
| 142 | + char *msg1 = "This string was sent by the CA"; |
| 143 | + const char *msg2 = "The CA thinks this is a fun riddle"; |
| 144 | + |
| 145 | + /* |
| 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. |
| 151 | + * |
| 152 | + * NOTE: You must pass this setting to the TEE context initialization |
| 153 | + * routine to receive OCALLs; otherwise, all OCALLs will return |
| 154 | + * a failure code. |
| 155 | + */ |
| 156 | + TEEC_ContextSettingOCall ocall_setting = { |
| 157 | + .handler = ocall_handler, |
| 158 | + .data = &ctx, |
| 159 | + }; |
| 160 | + |
| 161 | + /* Array of TEE context settings */ |
| 162 | + TEEC_ContextSetting ctx_settings = { |
| 163 | + .type = TEEC_CONTEXT_SETTING_OCALL, |
| 164 | + .u.ocall = &ocall_setting, |
| 165 | + }; |
| 166 | + |
| 167 | + /* Initialize a TEE context with settings */ |
| 168 | + res = TEEC_InitializeContext2(NULL, &ctx, &ctx_settings, 1); |
| 169 | + if (res != TEEC_SUCCESS) |
| 170 | + errx(1, "TEEC_InitializeContext failed with code 0x%x", res); |
| 171 | + |
| 172 | + /* |
| 173 | + * The session data setting allows attaching an arbitrary pointer to the |
| 174 | + * session. This pointer will be passed to the OCALL handler when |
| 175 | + * invoked. |
| 176 | + * |
| 177 | + * NOTE: This is optional; you can use TEEC_OpenSession as well even if |
| 178 | + * you expect OCALLs. |
| 179 | + */ |
| 180 | + TEEC_SessionSettingData data_setting = { |
| 181 | + .data = &sess |
| 182 | + }; |
| 183 | + |
| 184 | + /* Array of session settings */ |
| 185 | + TEEC_SessionSetting session_settings = { |
| 186 | + .type = TEEC_SESSION_SETTING_DATA, |
| 187 | + .u.data = &data_setting, |
| 188 | + }; |
| 189 | + |
| 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 */ |
| 205 | + res = TEEC_OpenSession2(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 206 | + &op, &err_origin, &session_settings, 1); |
| 207 | + if (res != TEEC_SUCCESS) |
| 208 | + errx(1, "TEEC_OpenSessionEx failed with code 0x%x origin 0x%x", |
| 209 | + res, err_origin); |
| 210 | + |
| 211 | + /* |
| 212 | + * The code below executes after the OCALL has been handled in the |
| 213 | + * callback at the top of this file. |
| 214 | + */ |
| 215 | + |
| 216 | + /* |
| 217 | + * Set up the parameters for the function invocation. These are just to |
| 218 | + * show that the CA can pass parameters to the TA and that during the |
| 219 | + * function invocation that carries those parameters to the TA, the TA |
| 220 | + * can make an OCALL with parameters of its own choosing. That is, the |
| 221 | + * parameters passed from the CA to the TA do not interfere with those |
| 222 | + * passed from the TA to the CA, and vice-versa. |
| 223 | + */ |
| 224 | + memset(&op, 0, sizeof(op)); |
| 225 | + op.paramTypes = TEEC_PARAM_TYPES( |
| 226 | + TEEC_VALUE_INPUT, |
| 227 | + TEEC_VALUE_INOUT, |
| 228 | + TEEC_MEMREF_TEMP_INPUT, |
| 229 | + TEEC_MEMREF_TEMP_INOUT); |
| 230 | + |
| 231 | + op.params[0].value.a = 0x3; |
| 232 | + op.params[0].value.b = 0x4; |
| 233 | + |
| 234 | + op.params[1].value.a = 0x5; |
| 235 | + op.params[1].value.b = 0x6; |
| 236 | + |
| 237 | + op.params[2].tmpref.buffer = msg1; |
| 238 | + op.params[2].tmpref.size = strlen(msg1) + 1; |
| 239 | + |
| 240 | + op.params[3].tmpref.buffer = buf; |
| 241 | + op.params[3].tmpref.size = sizeof(buf); |
| 242 | + memcpy(buf, msg2, strlen(msg2) + 1); |
| 243 | + |
| 244 | + /* Ask the TA to call us back */ |
| 245 | + res = TEEC_InvokeCommand(&sess, TA_OCALL_CMD_CALL_CA, &op, &err_origin); |
| 246 | + if (res != TEEC_SUCCESS) |
| 247 | + errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x", |
| 248 | + res, err_origin); |
| 249 | + |
| 250 | + /* |
| 251 | + * The code below once again executes after the OCALL has been handled |
| 252 | + * in the callback at the top of this file. |
| 253 | + */ |
| 254 | + |
| 255 | + /* |
| 256 | + * Print out the values of the INOUT parameters of the original function |
| 257 | + * invocation that we got from the TA.. |
| 258 | + */ |
| 259 | + printf("INOUT parameters from the original function invocation:\n"); |
| 260 | + printf("Inout values: %u, %u\n", op.params[1].value.a, |
| 261 | + op.params[1].value.b); |
| 262 | + |
| 263 | + printf("Inout string: %s\n", (char *)op.params[3].tmpref.buffer); |
| 264 | + printf("Inout size: %zu\n", op.params[3].tmpref.size); |
| 265 | + |
| 266 | + /* All done */ |
| 267 | + TEEC_CloseSession(&sess); |
| 268 | + |
| 269 | + TEEC_FinalizeContext(&ctx); |
| 270 | + |
| 271 | + return 0; |
| 272 | +} |
0 commit comments