1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2018-2020, Linaro Limited 4 */ 5 6 #include <compiler.h> 7 #include <pkcs11_ta.h> 8 #include <tee_internal_api.h> 9 #include <tee_internal_api_extensions.h> 10 11 #include "pkcs11_helpers.h" 12 13 TEE_Result TA_CreateEntryPoint(void) 14 { 15 return TEE_SUCCESS; 16 } 17 18 void TA_DestroyEntryPoint(void) 19 { 20 } 21 22 TEE_Result TA_OpenSessionEntryPoint(uint32_t __unused param_types, 23 TEE_Param __unused params[4], 24 void **session) 25 { 26 *session = NULL; 27 28 return TEE_SUCCESS; 29 } 30 31 void TA_CloseSessionEntryPoint(void *session __unused) 32 { 33 } 34 35 /* 36 * Entry point for invocation command PKCS11_CMD_PING 37 * 38 * @ctrl - param memref[0] or NULL: expected NULL 39 * @in - param memref[1] or NULL: expected NULL 40 * @out - param memref[2] or NULL 41 * 42 * Return a TEE_Result value 43 */ 44 static TEE_Result entry_ping(TEE_Param *ctrl, TEE_Param *in, TEE_Param *out) 45 { 46 const uint32_t ver[] = { 47 PKCS11_TA_VERSION_MAJOR, 48 PKCS11_TA_VERSION_MINOR, 49 PKCS11_TA_VERSION_PATCH, 50 }; 51 size_t size = 0; 52 53 if (ctrl || in) 54 return TEE_ERROR_BAD_PARAMETERS; 55 56 if (!out) 57 return TEE_SUCCESS; 58 59 size = out->memref.size; 60 out->memref.size = sizeof(ver); 61 62 if (size < sizeof(ver)) 63 return TEE_ERROR_SHORT_BUFFER; 64 65 TEE_MemMove(out->memref.buffer, ver, sizeof(ver)); 66 67 return TEE_SUCCESS; 68 } 69 70 /* 71 * Entry point for PKCS11 TA commands 72 */ 73 TEE_Result TA_InvokeCommandEntryPoint(void *tee_session __unused, uint32_t cmd, 74 uint32_t ptypes, 75 TEE_Param params[TEE_NUM_PARAMS]) 76 { 77 TEE_Param *ctrl = NULL; 78 TEE_Param *p1_in = NULL; 79 TEE_Param __maybe_unused *p2_in = NULL; 80 TEE_Param *p2_out = NULL; 81 TEE_Result res = TEE_ERROR_GENERIC; 82 83 /* Param#0: none or in-out buffer with serialized arguments */ 84 switch (TEE_PARAM_TYPE_GET(ptypes, 0)) { 85 case TEE_PARAM_TYPE_NONE: 86 break; 87 case TEE_PARAM_TYPE_MEMREF_INOUT: 88 ctrl = ¶ms[0]; 89 break; 90 default: 91 return TEE_ERROR_BAD_PARAMETERS; 92 } 93 94 /* Param#1: none or input data buffer */ 95 switch (TEE_PARAM_TYPE_GET(ptypes, 1)) { 96 case TEE_PARAM_TYPE_NONE: 97 break; 98 case TEE_PARAM_TYPE_MEMREF_INPUT: 99 p1_in = ¶ms[1]; 100 break; 101 default: 102 return TEE_ERROR_BAD_PARAMETERS; 103 } 104 105 /* Param#2: none or input data buffer */ 106 switch (TEE_PARAM_TYPE_GET(ptypes, 2)) { 107 case TEE_PARAM_TYPE_NONE: 108 break; 109 case TEE_PARAM_TYPE_MEMREF_INPUT: 110 p2_in = ¶ms[2]; 111 break; 112 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 113 p2_out = ¶ms[2]; 114 break; 115 default: 116 return TEE_ERROR_BAD_PARAMETERS; 117 } 118 119 /* Param#3: currently unused */ 120 switch (TEE_PARAM_TYPE_GET(ptypes, 3)) { 121 case TEE_PARAM_TYPE_NONE: 122 break; 123 default: 124 return TEE_ERROR_BAD_PARAMETERS; 125 } 126 127 DMSG("%s ctrl %"PRIu32"@%p, %s %"PRIu32"@%p, %s %"PRIu32"@%p", 128 id2str_ta_cmd(cmd), 129 ctrl ? ctrl->memref.size : 0, ctrl ? ctrl->memref.buffer : 0, 130 p1_in ? "in" : "---", p1_in ? p1_in->memref.size : 0, 131 p1_in ? p1_in->memref.buffer : NULL, 132 p2_out ? "out" : (p2_in ? "in" : "---"), 133 p2_out ? p2_out->memref.size : (p2_in ? p2_in->memref.size : 0), 134 p2_out ? p2_out->memref.buffer : 135 (p2_in ? p2_in->memref.buffer : NULL)); 136 137 switch (cmd) { 138 case PKCS11_CMD_PING: 139 res = entry_ping(ctrl, p1_in, p2_out); 140 break; 141 142 default: 143 EMSG("Command 0x%"PRIx32" is not supported", cmd); 144 return TEE_ERROR_NOT_SUPPORTED; 145 } 146 147 /* Currently no output data stored in output param#0 */ 148 if (TEE_PARAM_TYPE_GET(ptypes, 0) == TEE_PARAM_TYPE_MEMREF_INOUT) 149 ctrl->memref.size = 0; 150 151 return res; 152 } 153