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