|
| 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 | +#define PTR_ADD(ptr, offs) ((void *)((uintptr_t)(ptr) + (uintptr_t)(offs))) |
| 18 | +#define GET_BUF(p) (PTR_ADD((p).memref.parent->buffer, (p).memref.offset)) |
| 19 | + |
| 20 | +static void print_uuid(TEEC_UUID *uuid) |
| 21 | +{ |
| 22 | + printf("%x-%x-%x-%x%x-%x%x%x%x%x%x", |
| 23 | + uuid->timeLow, |
| 24 | + uuid->timeMid, |
| 25 | + uuid->timeHiAndVersion, |
| 26 | + uuid->clockSeqAndNode[0], |
| 27 | + uuid->clockSeqAndNode[1], |
| 28 | + uuid->clockSeqAndNode[2], |
| 29 | + uuid->clockSeqAndNode[3], |
| 30 | + uuid->clockSeqAndNode[4], |
| 31 | + uuid->clockSeqAndNode[5], |
| 32 | + uuid->clockSeqAndNode[6], |
| 33 | + uuid->clockSeqAndNode[7]); |
| 34 | +} |
| 35 | + |
| 36 | +/* |
| 37 | + * This function is called by the TEE Client API whenever an OCALL arrives from |
| 38 | + * the TA. |
| 39 | + * |
| 40 | + * The 'taUUID' parameter carries the UUID of the TA that sent the OCALL. Since |
| 41 | + * a TA can open a session to another TA, it is possible to receive OCALLs from |
| 42 | + * other TAs that your TA calls into, if any. |
| 43 | + * |
| 44 | + * The 'commandId' indicates which function the TA wishes the CA to run. |
| 45 | + * |
| 46 | + * 'ctxData' is the arbitrary pointer that was set via the TEE context OCALL |
| 47 | + * setting, if any. Similarly, 'sessionData' is the arbitrary pointer set via |
| 48 | + * the session data setting, if it was supplied, or NULL. |
| 49 | + * |
| 50 | + * NOTE: Notice that the OCALL carries memory references. Currently, OP-TEE |
| 51 | + * takes any memref parameter that originates in the TA and marshals it to |
| 52 | + * the CA in a single shared memory object. Hence, the two buffers passed |
| 53 | + * from the TA wind up at different offsets within the same memory |
| 54 | + * reference. Therefore, it is necessary to take the offset into |
| 55 | + * consideration while manipulating these buffers. |
| 56 | + */ |
| 57 | +TEEC_Result ocall_handler(TEEC_UUID *taUUID, uint32_t commandId, |
| 58 | + uint32_t paramTypes, |
| 59 | + TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT], |
| 60 | + void *ctxData, void *sessionData) |
| 61 | +{ |
| 62 | + printf("Received an OCALL for Command Id: %u\n", commandId); |
| 63 | + |
| 64 | + printf("The TA that sent it is: "); |
| 65 | + print_uuid(taUUID); |
| 66 | + printf("\n"); |
| 67 | + |
| 68 | + uint32_t expected_pt; |
| 69 | + |
| 70 | + char *msg = "This string was sent by the CA"; |
| 71 | + |
| 72 | + switch (commandId) { |
| 73 | + case CA_OCALL_CMD_REPLY_TA: |
| 74 | + expected_pt = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 75 | + TEEC_VALUE_INOUT, |
| 76 | + TEEC_MEMREF_TEMP_INPUT, |
| 77 | + TEEC_MEMREF_TEMP_INOUT); |
| 78 | + if (paramTypes != expected_pt) { |
| 79 | + printf("Bad param types\n"); |
| 80 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 81 | + } |
| 82 | + |
| 83 | + if (!params[2].memref.parent || !params[3].memref.parent) { |
| 84 | + printf("No buffer(s)\n"); |
| 85 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 86 | + } |
| 87 | + |
| 88 | + printf("Input values: %u, %u\n", params[0].value.a, |
| 89 | + params[0].value.b); |
| 90 | + printf("Inout values: %u, %u\n", params[1].value.a, |
| 91 | + params[1].value.b); |
| 92 | + |
| 93 | + printf("Input string: %s\n", (char *)GET_BUF(params[2])); |
| 94 | + printf("Input size: %zu\n", params[2].memref.size); |
| 95 | + |
| 96 | + printf("Inout string: %s\n", (char *)GET_BUF(params[3])); |
| 97 | + printf("Inout size: %zu\n", params[3].memref.size); |
| 98 | + |
| 99 | + /* Set the OCALL's INOUT parameters */ |
| 100 | + params[1].value.a = 0x3; |
| 101 | + params[1].value.b = 0x4; |
| 102 | + |
| 103 | + params[3].memref.size = strlen(msg) + 1; |
| 104 | + memcpy(GET_BUF(params[3]), msg, params[3].memref.size); |
| 105 | + |
| 106 | + printf("OCALL handled\n"); |
| 107 | + break; |
| 108 | + default: |
| 109 | + printf("Bad function ID\n"); |
| 110 | + return TEEC_ERROR_BAD_PARAMETERS; |
| 111 | + } |
| 112 | + return TEEC_SUCCESS; |
| 113 | +} |
| 114 | + |
| 115 | +int main(int argc, char* argv[]) |
| 116 | +{ |
| 117 | + TEEC_Context ctx; |
| 118 | + TEEC_Session sess; |
| 119 | + TEEC_UUID uuid = TA_OCALL_UUID; |
| 120 | + TEEC_Operation op = { 0 }; |
| 121 | + |
| 122 | + TEEC_Result res; |
| 123 | + uint32_t err_origin; |
| 124 | + |
| 125 | + char buf[128]; |
| 126 | + char *msg1 = "This string was sent by the CA"; |
| 127 | + const char *msg2 = "The CA thinks this is a fun riddle"; |
| 128 | + |
| 129 | + /* |
| 130 | + * The TEE context OCALL setting allows setting the callback handler for |
| 131 | + * when an OCALL arrives from the TA. This handler is effectively the |
| 132 | + * equivalent of TA_InvokeCommandEntryPoint. Additionally, one may set |
| 133 | + * an arbitrary pointer that will be passed to the OCALL handler when |
| 134 | + * invoked. |
| 135 | + * |
| 136 | + * NOTE: You must pass this setting to the TEE context initialization |
| 137 | + * routine to receive OCALLs. |
| 138 | + */ |
| 139 | + TEEC_ContextSettingOcall ocall_setting = { |
| 140 | + .handler = ocall_handler, |
| 141 | + .data = &ctx, |
| 142 | + }; |
| 143 | + |
| 144 | + /* Array of TEE context settings */ |
| 145 | + TEEC_ContextSetting ctx_settings = { |
| 146 | + .type = TEEC_CONTEXT_SETTING_OCALL, |
| 147 | + .u.ocall = &ocall_setting, |
| 148 | + }; |
| 149 | + |
| 150 | + /* Initialize a TEE context with settings */ |
| 151 | + res = TEEC_InitializeContext2(NULL, &ctx, &ctx_settings, 1); |
| 152 | + if (res != TEEC_SUCCESS) |
| 153 | + errx(1, "TEEC_InitializeContext failed with code 0x%x", res); |
| 154 | + |
| 155 | + /* |
| 156 | + * The session data settings allows attaching an arbitrary pointer to |
| 157 | + * the session. This pointer will be passed to the OCALL handler when |
| 158 | + * invoked. |
| 159 | + * |
| 160 | + * NOTE: This is optional; you can use TEEC_OpenSession as well even if |
| 161 | + * you expect OCALLs. |
| 162 | + */ |
| 163 | + TEEC_SessionSettingData data_setting = { |
| 164 | + .data = &sess |
| 165 | + }; |
| 166 | + |
| 167 | + /* Array of session settings */ |
| 168 | + TEEC_SessionSetting session_settings = { |
| 169 | + .type = TEEC_SESSION_SETTING_DATA, |
| 170 | + .u.data = &data_setting, |
| 171 | + }; |
| 172 | + |
| 173 | + /* Open a session with settings */ |
| 174 | + res = TEEC_OpenSession2(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 175 | + NULL, &err_origin, &session_settings, 1); |
| 176 | + if (res != TEEC_SUCCESS) |
| 177 | + errx(1, "TEEC_OpenSessionEx failed with code 0x%x origin 0x%x", |
| 178 | + res, err_origin); |
| 179 | + |
| 180 | + /* |
| 181 | + * Set up the parameters for the function invocation. These are just to |
| 182 | + * show that the CA can pass parameters to the TA and that during the |
| 183 | + * function invocation that carries those parameters to the TA, the TA |
| 184 | + * can make an OCALL with parameters of its own choosing. That is, the |
| 185 | + * parameters passed from the CA to the TA do not interfere with those |
| 186 | + * passed from the TA to the CA, and vice-versa. |
| 187 | + */ |
| 188 | + op.paramTypes = TEEC_PARAM_TYPES( |
| 189 | + TEEC_VALUE_INPUT, |
| 190 | + TEEC_VALUE_INOUT, |
| 191 | + TEEC_MEMREF_TEMP_INPUT, |
| 192 | + TEEC_MEMREF_TEMP_INOUT); |
| 193 | + |
| 194 | + op.params[0].value.a = 0x3; |
| 195 | + op.params[0].value.b = 0x4; |
| 196 | + |
| 197 | + op.params[1].value.a = 0x5; |
| 198 | + op.params[1].value.b = 0x6; |
| 199 | + |
| 200 | + op.params[2].tmpref.buffer = msg1; |
| 201 | + op.params[2].tmpref.size = strlen(msg1) + 1; |
| 202 | + |
| 203 | + op.params[3].tmpref.buffer = buf; |
| 204 | + op.params[3].tmpref.size = sizeof(buf); |
| 205 | + memcpy(buf, msg2, strlen(msg2) + 1); |
| 206 | + |
| 207 | + /* Ask the TA to call us back */ |
| 208 | + res = TEEC_InvokeCommand(&sess, TA_OCALL_CMD_CALL_CA, &op, &err_origin); |
| 209 | + if (res != TEEC_SUCCESS) |
| 210 | + errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x", |
| 211 | + res, err_origin); |
| 212 | + |
| 213 | + /* |
| 214 | + * The code below executes after the OCALL has been handled in the |
| 215 | + * callback at the top of this file. |
| 216 | + */ |
| 217 | + |
| 218 | + /* |
| 219 | + * Print out the values of the INOUT parameters of the original function |
| 220 | + * invocation that we got from the TA.. |
| 221 | + */ |
| 222 | + printf("INOUT parameters from the original function invocation:\n"); |
| 223 | + printf("Inout values: %u, %u\n", op.params[1].value.a, |
| 224 | + op.params[1].value.b); |
| 225 | + |
| 226 | + printf("Inout string: %s\n", (char *)op.params[3].tmpref.buffer); |
| 227 | + printf("Inout size: %zu\n", op.params[3].tmpref.size); |
| 228 | + |
| 229 | + /* All done */ |
| 230 | + TEEC_CloseSession(&sess); |
| 231 | + |
| 232 | + TEEC_FinalizeContext(&ctx); |
| 233 | + |
| 234 | + return 0; |
| 235 | +} |
0 commit comments