From 47f6e752a5c280bdd1ff112dc6d32d8731cf13b6 Mon Sep 17 00:00:00 2001 From: Krzysztof Dynowski Date: Thu, 27 Jul 2017 16:04:33 +0200 Subject: [PATCH] Native TEF API (GlobalPlatform/OpenSource) Change-Id: I51d198d5227643369f790c6b3f740bef937c1d69 --- CMakeLists.txt | 100 ++++++++ LICENSE | 203 +++++++++++++++ api/tef/tee_client_api.h | 563 +++++++++++++++++++++++++++++++++++++++++ packaging/tef-libteec.manifest | 5 + packaging/tef-libteec.spec | 54 ++++ src/optee/creators.c | 33 +++ src/optee/creators.h | 27 ++ src/optee/tee_client_api.h | 536 +++++++++++++++++++++++++++++++++++++++ src/scripts/tef-update.sh | 10 + src/simulator/creators.c | 33 +++ src/simulator/creators.h | 27 ++ src/tef-libteec.pc.in | 12 + src/tef.conf | 6 + src/tef_libteec.c | 401 +++++++++++++++++++++++++++++ 14 files changed, 2010 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 api/tef/tee_client_api.h create mode 100644 packaging/tef-libteec.manifest create mode 100644 packaging/tef-libteec.spec create mode 100644 src/optee/creators.c create mode 100644 src/optee/creators.h create mode 100644 src/optee/tee_client_api.h create mode 100755 src/scripts/tef-update.sh create mode 100644 src/simulator/creators.c create mode 100644 src/simulator/creators.h create mode 100644 src/tef-libteec.pc.in create mode 100644 src/tef.conf create mode 100644 src/tef_libteec.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..862b80f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,100 @@ +# +# Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved +# +# Contact: Krzysztof Dynowski +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License +# +# +# @file CMakeLists.txt +# @author Krzysztof Dynowski +# + +CMAKE_MINIMUM_REQUIRED (VERSION 2.6) + +PROJECT(tef-libteec) +SET(VERSION "0.0.1") + +INCLUDE(FindPkgConfig) +INCLUDE(GNUInstallDirs) # CMAKE_INSTALL_xxx + +## Compiler flags, depending on the build type ################################# +IF(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE "DEBUG") +ENDIF(NOT CMAKE_BUILD_TYPE) + +## Print build information ##################################################### +MESSAGE(STATUS "-------------------------------------------------") +MESSAGE(STATUS "Compiler: " ${CMAKE_C_COMPILER_ID}) +MESSAGE(STATUS "Compiler version: " ${CMAKE_C_COMPILER_VERSION}) +MESSAGE(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) +MESSAGE(STATUS "-------------------------------------------------") + +SET(CMAKE_C_FLAGS_DEBUG "-std=c11 -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") +SET(CMAKE_C_FLAGS_RELEASE "-std=c11 -O2 -DNDEBUG") + +ADD_DEFINITIONS("-fPIC") # Position Independent Code +ADD_DEFINITIONS("-Werror") # Make all warnings into errors +ADD_DEFINITIONS("-Wall") # Generate all warnings +ADD_DEFINITIONS("-Wextra") # Generate even more extra warnings +ADD_DEFINITIONS("-pedantic") # Be pedantic +ADD_DEFINITIONS("-pedantic-errors") # Make pedantic warnings into errors + + +SET(TEF_API_ROOT_PATH ${PROJECT_SOURCE_DIR}) +SET(TEF_API_PATH $(TEF_API_ROOT_PATH)/api) +SET(TEF_TARGET "teec") +SET(TEF_PC tef-libteec.pc) + +# force 32-bit mode +IF(DEFINED FORCE_32BIT) + SET(CMAKE_C_FLAGS -m32) +ENDIF() + +INCLUDE_DIRECTORIES( + ${TEF_API_PATH} +) + +FILE(GLOB TEF_API_HEADERS api/tef/*.h) +FILE(GLOB TEF_API_SOURCES src/*.c src/simulator/*.c src/optee/*.c) + +ADD_LIBRARY(${TEF_TARGET} SHARED ${TEF_API_SOURCES}) + +SET(_LIB_VERSION_ "${VERSION}") +SET(_LIB_SOVERSION_ "0") +SET_TARGET_PROPERTIES(${TEF_TARGET} PROPERTIES + SOVERSION ${_LIB_SOVERSION_} + VERSION ${_LIB_VERSION_}) + +IF(NOT DEFINED LIB_INSTALL_DIR) + SET(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") +ENDIF(NOT DEFINED LIB_INSTALL_DIR) + +IF(NOT DEFINED INCLUDE_INSTALL_DIR) + SET(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}") +ENDIF(NOT DEFINED INCLUDE_INSTALL_DIR) + +ADD_DEFINITIONS(-DLIB_INSTALL_DIR="${LIB_INSTALL_DIR}") + +CONFIGURE_FILE(src/${TEF_PC}.in ${CMAKE_BINARY_DIR}/${TEF_PC} @ONLY) + +#install library +INSTALL(FILES src/tef.conf DESTINATION /etc/tef/) +INSTALL(PROGRAMS src/scripts/tef-update.sh DESTINATION /usr/sbin/) +INSTALL(TARGETS ${TEF_TARGET} DESTINATION ${LIB_INSTALL_DIR}) +INSTALL(FILES ${CMAKE_BINARY_DIR}/${TEF_PC} + DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) + + +#install headers +INSTALL(FILES ${TEF_API_HEADERS} DESTINATION "${INCLUDE_INSTALL_DIR}/tef") diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..181359e --- /dev/null +++ b/LICENSE @@ -0,0 +1,203 @@ +Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/api/tef/tee_client_api.h b/api/tef/tee_client_api.h new file mode 100644 index 0000000..c3c637a --- /dev/null +++ b/api/tef/tee_client_api.h @@ -0,0 +1,563 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Dynowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file tee_client_api.h + * @brief Client GlobalPlatform API. + */ + +#ifndef TEE_CLIENT_API_H +#define TEE_CLIENT_API_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup CAPI_TEF_MODULE + * @{ + */ + +/* + * @brief Defines the number of available memory references in an open session or + * invoke command operation payload. + * + * @since_tizen 4.0 + */ +#define TEEC_CONFIG_PAYLOAD_REF_COUNT 4 + +/** + * @brief Defines the maximum size of a single shared memory block, in bytes, of both + * API allocated and API registered memory. The size is currently set to + * 512 * kB (512 * 1024). + * + * @since_tizen 4.0 + */ +#define TEEC_CONFIG_SHAREDMEM_MAX_SIZE 0x80000 + +/** + * @brief Flag constants indicating the type of parameters encoded inside the + * operation payload (TEEC_Operation), Type is uint32_t. + * + * @since_tizen 4.0 + * + * TEEC_NONE The Parameter is not used + * + * TEEC_VALUE_INPUT The Parameter is a TEEC_Value tagged as input. + * + * TEEC_VALUE_OUTPUT The Parameter is a TEEC_Value tagged as output. + * + * TEEC_VALUE_INOUT The Parameter is a TEEC_Value tagged as both as + * input and output, i.e., for which both the + * behaviors of TEEC_VALUE_INPUT and + * TEEC_VALUE_OUTPUT apply. + * + * TEEC_MEMREF_TEMP_INPUT The Parameter is a TEEC_TempMemoryReference + * describing a region of memory which needs to be + * temporarily registered for the duration of the + * Operation and is tagged as input. + * + * TEEC_MEMREF_TEMP_OUTPUT Same as TEEC_MEMREF_TEMP_INPUT, but the Memory + * Reference is tagged as output. The + * Implementation may update the size field to + * reflect the required output size in some use + * cases. + * + * TEEC_MEMREF_TEMP_INOUT A Temporary Memory Reference tagged as both + * input and output, i.e., for which both the + * behaviors of TEEC_MEMREF_TEMP_INPUT and + * TEEC_MEMREF_TEMP_OUTPUT apply. + * + * TEEC_MEMREF_WHOLE The Parameter is a Registered Memory Reference + * that refers to the entirety of its parent Shared + * Memory block. The parameter structure is a + * TEEC_MemoryReference. In this structure, the + * Implementation MUST read only the parent field + * and MAY update the size field when the operation + * completes. + * + * TEEC_MEMREF_PARTIAL_INPUT A Registered Memory Reference structure that + * refers to a partial region of its parent Shared + * Memory block and is tagged as input. + * + * TEEC_MEMREF_PARTIAL_OUTPUT Registered Memory Reference structure that + * refers to a partial region of its parent Shared + * Memory block and is tagged as output. + * + * TEEC_MEMREF_PARTIAL_INOUT The Registered Memory Reference structure that + * refers to a partial region of its parent Shared + * Memory block and is tagged as both input and + * output, i.e., for which both the behaviors of + * TEEC_MEMREF_PARTIAL_INPUT and + * TEEC_MEMREF_PARTIAL_OUTPUT apply. + */ +#define TEEC_NONE 0x00000000 +#define TEEC_VALUE_INPUT 0x00000001 +#define TEEC_VALUE_OUTPUT 0x00000002 +#define TEEC_VALUE_INOUT 0x00000003 +#define TEEC_MEMREF_TEMP_INPUT 0x00000005 +#define TEEC_MEMREF_TEMP_OUTPUT 0x00000006 +#define TEEC_MEMREF_TEMP_INOUT 0x00000007 +#define TEEC_MEMREF_WHOLE 0x0000000C +#define TEEC_MEMREF_PARTIAL_INPUT 0x0000000D +#define TEEC_MEMREF_PARTIAL_OUTPUT 0x0000000E +#define TEEC_MEMREF_PARTIAL_INOUT 0x0000000F + +/** + * @brief Flag constants indicating the data transfer direction of memory in + * TEEC_Parameter. TEEC_MEM_INPUT signifies data transfer direction from the + * client application to the TEE. TEEC_MEM_OUTPUT signifies data transfer + * direction from the TEE to the client application. Type is uint32_t. + * + * @since_tizen 4.0 + * + * + * TEEC_MEM_INPUT The Shared Memory can carry data from the client + * application to the Trusted Application. + * TEEC_MEM_OUTPUT The Shared Memory can carry data from the Trusted + * Application to the client application. + */ +#define TEEC_MEM_INPUT 0x00000001 +#define TEEC_MEM_OUTPUT 0x00000002 + +/** + * @brief Return values. Type is TEEC_Result + * + * @since_tizen 4.0 + * + * TEEC_SUCCESS The operation was successful. + * TEEC_ERROR_GENERIC Non-specific cause. + * TEEC_ERROR_ACCESS_DENIED Access privileges are not sufficient. + * TEEC_ERROR_CANCEL The operation was canceled. + * TEEC_ERROR_ACCESS_CONFLICT Concurrent accesses caused conflict. + * TEEC_ERROR_EXCESS_DATA Too much data for the requested operation was + * passed. + * TEEC_ERROR_BAD_FORMAT Input data was of invalid format. + * TEEC_ERROR_BAD_PARAMETERS Input parameters were invalid. + * TEEC_ERROR_BAD_STATE Operation is not valid in the current state. + * TEEC_ERROR_ITEM_NOT_FOUND The requested data item is not found. + * TEEC_ERROR_NOT_IMPLEMENTED The requested operation should exist but is not + * yet implemented. + * TEEC_ERROR_NOT_SUPPORTED The requested operation is valid but is not + * supported in this implementation. + * TEEC_ERROR_NO_DATA Expected data was missing. + * TEEC_ERROR_OUT_OF_MEMORY System ran out of resources. + * TEEC_ERROR_BUSY The system is busy working on something else. + * TEEC_ERROR_COMMUNICATION Communication with a remote party failed. + * TEEC_ERROR_SECURITY A security fault was detected. + * TEEC_ERROR_SHORT_BUFFER The supplied buffer is too short for the + * generated output. + * TEEC_ERROR_TARGET_DEAD Trusted Application has panicked + * during the operation. + */ + +/** + * Standard defined error codes. + */ +#define TEEC_SUCCESS 0x00000000 +#define TEEC_ERROR_GENERIC 0xFFFF0000 +#define TEEC_ERROR_ACCESS_DENIED 0xFFFF0001 +#define TEEC_ERROR_CANCEL 0xFFFF0002 +#define TEEC_ERROR_ACCESS_CONFLICT 0xFFFF0003 +#define TEEC_ERROR_EXCESS_DATA 0xFFFF0004 +#define TEEC_ERROR_BAD_FORMAT 0xFFFF0005 +#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 +#define TEEC_ERROR_BAD_STATE 0xFFFF0007 +#define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008 +#define TEEC_ERROR_NOT_IMPLEMENTED 0xFFFF0009 +#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A +#define TEEC_ERROR_NO_DATA 0xFFFF000B +#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C +#define TEEC_ERROR_BUSY 0xFFFF000D +#define TEEC_ERROR_COMMUNICATION 0xFFFF000E +#define TEEC_ERROR_SECURITY 0xFFFF000F +#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010 +#define TEEC_ERROR_EXTERNAL_CANCEL 0xFFFF0011 +#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024 + +/** + * @brief Function error origins, of type TEEC_ErrorOrigin. These indicate where in + * the software stack a particular return value originates from. + * + * @since_tizen 4.0 + * + * TEEC_ORIGIN_API The error originated within the TEE Client API + * implementation. + * TEEC_ORIGIN_COMMS The error originated within the underlying + * communications stack linking the rich OS with + * the TEE. + * TEEC_ORIGIN_TEE The error originated within the common TEE code. + * TEEC_ORIGIN_TRUSTED_APP The error originated within the Trusted Application + * code. + */ +#define TEEC_ORIGIN_API 0x00000001 +#define TEEC_ORIGIN_COMMS 0x00000002 +#define TEEC_ORIGIN_TEE 0x00000003 +#define TEEC_ORIGIN_TRUSTED_APP 0x00000004 + +/** + * @brief Session login methods, for use in TEEC_OpenSession() as parameter + * connectionMethod. Type is uint32_t. + * + * @since_tizen 4.0 + * + * TEEC_LOGIN_PUBLIC No login data is provided. + * TEEC_LOGIN_USER Login data about the user running the Client + * Application process is provided. + * TEEC_LOGIN_GROUP Login data about the group running the Client + * Application process is provided. + * TEEC_LOGIN_APPLICATION Login data about the running Client Application + * itself is provided. + * TEEC_LOGIN_USER_APPLICATION Login data about the user and the running + * Client Application itself is provided. + * TEEC_LOGIN_GROUP_APPLICATION Login data about the group and the running + * Client Application itself is provided. + */ +#define TEEC_LOGIN_PUBLIC 0x00000000 +#define TEEC_LOGIN_USER 0x00000001 +#define TEEC_LOGIN_GROUP 0x00000002 +#define TEEC_LOGIN_APPLICATION 0x00000004 +#define TEEC_LOGIN_USER_APPLICATION 0x00000005 +#define TEEC_LOGIN_GROUP_APPLICATION 0x00000006 + +/** + * @brief Encode the paramTypes according to the supplied types. + * + * @since_tizen 4.0 + * + * @param[in] p0 The first param type. + * @param[in] p1 The second param type. + * @param[in] p2 The third param type. + * @param[in] p3 The fourth param type. + */ +#define TEEC_PARAM_TYPES(p0, p1, p2, p3) \ + ((p0) | ((p1) << 4) | ((p2) << 8) | ((p3) << 12)) + +/** + * @brief Get the i_th param type from the paramType. + * + * @since_tizen 4.0 + * + * @param p[in] The paramType. + * @param i[in] The i-th parameter to get the type for. + */ +#define TEEC_PARAM_TYPE_GET(p, i) (((p) >> (i * 4)) & 0xF) + +/** + * @brief This type is used to contain return codes which are the results of invoking TEE Client API functions. + */ +typedef uint32_t TEEC_Result; + +/** + * @brief This type contains a Universally Unique Resource Identifier (UUID) type as + * defined in RFC4122. These UUID values are used to identify Trusted Applications. + * + * @since_tizen 4.0 + */ +typedef struct { + uint32_t timeLow; + uint16_t timeMid; + uint16_t timeHiAndVersion; + uint8_t clockSeqAndNode[8]; +} TEEC_UUID; + +/** + * @brief Memory to transfer data between a client application and trusted code. + * + * @since_tizen 4.0 + * + * A shared memory block is a region of memory allocated in the context of the + * client application memory space that can be used to transfer data between + * that client application and a trusted application. The user of this struct + * is responsible to populate the buffer pointer. + */ +typedef struct { + /** The memory buffer which is to be, or has been, shared with the TEE. */ + void *buffer; + /** The size, in bytes, of the memory buffer. */ + size_t size; + /** Bit-vector which holds properties of buffer. + * The bit-vector can contain either or both of the + * TEEC_MEM_INPUT and TEEC_MEM_OUTPUT flags. + */ + uint32_t flags; + /* Implementation defined */ + void *imp; +} TEEC_SharedMemory; + +/** + * @brief Temporary memory to transfer data between + * a client application and trusted code, only used for the duration of the + * operation. + * + * @since_tizen 4.0 + * + * A memory buffer that is registered temporarily for the duration of the + * operation to be called. + */ +typedef struct { + /** The memory buffer which is to be, or has been shared with the TEE. */ + void *buffer; + /** The size, in bytes, of the memory buffer. */ + size_t size; +} TEEC_TempMemoryReference; + +/** + * @brief Uses pre-registered or pre-allocated shared memory block of memory to transfer + * data between a client application and trusted code. + * + * @since_tizen 4.0 + * + */ +typedef struct { + /** Points to a shared memory structure. The memory reference + * may utilize the whole shared memory or only a part of it. Must not be NULL + */ + TEEC_SharedMemory *parent; + /** The size, in bytes, of the memory buffer. */ + size_t size; + /** The offset, in bytes, of the referenced memory region from + * the start of the shared memory block. + */ + size_t offset; +} TEEC_RegisteredMemoryReference; + +/** + * @brief Small raw data container + * + * @since_tizen 4.0 + * + * Instead of allocating a shared memory buffer this structure can be used + * to pass small raw data between a client application and trusted code. + */ +typedef struct { + /** The first integer value. */ + uint32_t a; + /** The second integer value. */ + uint32_t b; +} TEEC_Value; + +/** + * @brief Memory container to be used when passing data between + * client application and trusted code. + * + * @since_tizen 4.0 + * + * Either the client uses a shared memory reference, parts of it or a small raw + * data container. + */ +typedef union { + /** A temporary memory reference only valid for the duration of the operation. */ + TEEC_TempMemoryReference tmpref; + /** The entire shared memory or parts of it. */ + TEEC_RegisteredMemoryReference memref; + /** The small raw data container to use */ + TEEC_Value value; +} TEEC_Parameter; + +/** + * @brief Represents a connection between a client application and a TEE. + * + * @since_tizen 4.0 + */ +typedef struct { + /* Implementation defined */ + void *imp; +} TEEC_Context; + +/** + * @brief Represents a connection between a client application + * and a trusted application. + * + * @since_tizen 4.0 + */ +typedef struct { + /* Implementation defined */ + void *imp; +} TEEC_Session; + +/** + * @brief Holds information and memory references used in TEEC_InvokeCommand(). + * + * @since_tizen 4.0 + * + */ +typedef struct { + /** Client must initialize to zero if it needs to cancel an operation about to be performed. */ + uint32_t started; + /** Type of data passed. Use TEEC_PARAMS_TYPE macro to create the correct flags. */ + uint32_t paramTypes; + /** Array of parameters of type TEEC_Parameter. */ + TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT]; + /* Implementation-Defined */ + void *imp; +} TEEC_Operation; + +/** + * @brief Initializes a context holding connection + * information on the specific TEE, designated by the name string. + * + * @since_tizen 4.0 + * + * @param[in] name A zero-terminated string identifying the TEE to connect to. + * If name is set to NULL, the default TEE is connected to. NULL + * is the only supported value in this version of the API + * implementation. + * @param[out] context The context structure which is to be initialized. + * + * @return TEEC_SUCCESS on success, or another Return Code on failure + */ +TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context); + +/** + * @brief Destroys a context holding connection information + * on the specific TEE. + * + * @since_tizen 4.0 + * + * This function destroys an initialized TEE context, closing the connection + * between the client application and the TEE. This function must only be + * called when all sessions related to this TEE context have been closed and + * all shared memory blocks have been released. + * + * @param[in] context The context to be destroyed. + */ +void TEEC_FinalizeContext(TEEC_Context *context); + +/** + * @brief Opens a new session with the specified trusted application. + * + * @since_tizen 4.0 + * + * @param[in] context The initialized TEE context structure in which + * scope to open the session. + * @param[in] session The session to initialize. + * @param[in] destination A structure identifying the trusted application + * with which to open a session. + * + * @param[in] connectionMethod The connection method to use. + * @param[in] connectionData Any data necessary to connect with the chosen + * connection method. Not supported, should be set to NULL. + * @param[in,out] operation An operation structure to use in the session. May + * be set to NULL to signify no operation structure needed. + * + * @param[out] returnOrigin A parameter which will hold the error origin if + * this function returns any value other than TEEC_SUCCESS. + * + * @return TEEC_SUCCESS on success, or another Return Code on failure + * + */ +TEEC_Result TEEC_OpenSession(TEEC_Context *context, + TEEC_Session *session, + const TEEC_UUID *destination, + uint32_t connectionMethod, + const void *connectionData, + TEEC_Operation *operation, + uint32_t *returnOrigin); + +/** + * @brief Closes the session which has been opened with the + * specific trusted application. + * + * @since_tizen 4.0 + * + * @param[in] session The opened session to close. + */ +void TEEC_CloseSession(TEEC_Session *session); + +/** + * @brief Executes a command in the specified trusted application. + * + * @param[in] session A handle to an open connection to the trusted application. + * @param[in] commandID Identifier of the command in the trusted application to invoke. + * @param[in,out] operation An operation structure to use in the invoke command. + * May be set to NULL to signify no operation structure + * needed. + * @param[out] returnOrigin A parameter which will hold the error origin if this + * function returns any value other than TEEC_SUCCESS. + * + * @return TEEC_SUCCESS on success, or another Return Code on failure + */ +TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, + uint32_t commandID, + TEEC_Operation *operation, + uint32_t *returnOrigin); + +/** + * @brief Registers a block of existing memory as a + * shared block within the scope of the specified context. + * + * @since_tizen 4.0 + * + * @param[in] context The initialized TEE context structure in which scope to + * open the session. + * @param[in] sharedMem pointer to the shared memory structure to register. + * + * @return TEEC_SUCCESS on success, or another Return Code on failure + * @retval TEEC_ERROR_OUT_OF_MEMORY Memory exhaustion. + */ +TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, + TEEC_SharedMemory *sharedMem); + +/** + * @brief Allocates shared memory for TEE. + * + * @since_tizen 4.0 + * + * @param[in] context The initialized TEE context structure in which scope to + * open the session. + * @param[in] sharedMem Pointer to the allocated shared memory. + * + * @return TEEC_SUCCESS on success, or another Return Code on failure + * @retval TEEC_ERROR_OUT_OF_MEMORY Memory exhaustion. + */ +TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, + TEEC_SharedMemory *sharedMem); + +/** + * @brief Frees or deregisters the shared memory. + * + * @since_tizen 4.0 + * + * @param[in] sharedMem Pointer to the shared memory to be freed. + */ +void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMemory); + +/** + * @brief Requests the cancellation of a pending open + * session or command invocation. + * + * @since_tizen 4.0 + * + * @param[in] operation Pointer to an operation previously passed to open session + * or invoke. + */ +void TEEC_RequestCancellation(TEEC_Operation *operation); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/packaging/tef-libteec.manifest b/packaging/tef-libteec.manifest new file mode 100644 index 0000000..86dbb26 --- /dev/null +++ b/packaging/tef-libteec.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/tef-libteec.spec b/packaging/tef-libteec.spec new file mode 100644 index 0000000..c6e65e5 --- /dev/null +++ b/packaging/tef-libteec.spec @@ -0,0 +1,54 @@ +Name: tef-libteec +Version: 0.0.1 +Release: 0 +License: Apache-2.0 +Group: Security/Other +Summary: Global Platform client API +BuildRequires: cmake +Requires(post): /sbin/ldconfig +Requires(postun): /sbin/ldconfig +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +%description +The package provides Global Platform client API. + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + +%files +%manifest packaging/%{name}.manifest +%license LICENSE +%{_libdir}/libteec.so* +/etc/tef/tef.conf +/usr/sbin/tef-update.sh + + +%prep +%setup -q + +%build +%{!?build_type:%define build_type "RELEASE"} + +%cmake . -DCMAKE_BUILD_TYPE=%{build_type} +make -k %{?jobs:-j%jobs} + +%install +%make_install + +%clean + +## Devel Package ############################################################### +%package devel +Summary: Global Platform client API development files +Group: Security/Other +Requires: tef-libteec = %{version}-%{release} + +%description devel +The package provides Global Platform client API development files. + +%files devel +%{_includedir}/tef +%{_libdir}/pkgconfig/tef-libteec.pc +%{_libdir}/libteec.so* diff --git a/src/optee/creators.c b/src/optee/creators.c new file mode 100644 index 0000000..8c580bb --- /dev/null +++ b/src/optee/creators.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Dynowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include "tee_client_api.h" +#include + +void *optee_TEEC_Context() { + return malloc(sizeof(TEEC_Context)); +} +void *optee_TEEC_Session() { + return malloc(sizeof(TEEC_Session)); +} +void *optee_TEEC_Operation() { + return malloc(sizeof(TEEC_Operation)); +} +void *optee_TEEC_SharedMemory() { + return malloc(sizeof(TEEC_SharedMemory)); +} diff --git a/src/optee/creators.h b/src/optee/creators.h new file mode 100644 index 0000000..12725cf --- /dev/null +++ b/src/optee/creators.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Dynowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#ifndef __TEF_OPTEE_CREATORS_H__ +#define __TEF_OPTEE_CREATORS_H__ + +void *optee_TEEC_Context(); +void *optee_TEEC_Session(); +void *optee_TEEC_Operation(); +void *optee_TEEC_SharedMemory(); + +#endif diff --git a/src/optee/tee_client_api.h b/src/optee/tee_client_api.h new file mode 100644 index 0000000..369df1a --- /dev/null +++ b/src/optee/tee_client_api.h @@ -0,0 +1,536 @@ +/* + * Copyright (c) 2014, STMicroelectronics International N.V. + * All rights reserved. + * Copyright (c) 2015, Linaro Limited + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef TEE_CLIENT_API_H +#define TEE_CLIENT_API_H + +#include +#include + +/* + * Defines the number of available memory references in an open session or + * invoke command operation payload. + */ +#define TEEC_CONFIG_PAYLOAD_REF_COUNT 4 + +/** + * Defines the maximum size of a single shared memory block, in bytes, of both + * API allocated and API registered memory. The size is currently set to + * 512 * kB (512 * 1024). + */ +#define TEEC_CONFIG_SHAREDMEM_MAX_SIZE 0x8000 + +/** + * Flag constants indicating the type of parameters encoded inside the + * operation payload (TEEC_Operation), Type is uint32_t. + * + * TEEC_NONE The Parameter is not used + * + * TEEC_VALUE_INPUT The Parameter is a TEEC_Value tagged as input. + * + * TEEC_VALUE_OUTPUT The Parameter is a TEEC_Value tagged as output. + * + * TEEC_VALUE_INOUT The Parameter is a TEEC_Value tagged as both as + * input and output, i.e., for which both the + * behaviors of TEEC_VALUE_INPUT and + * TEEC_VALUE_OUTPUT apply. + * + * TEEC_MEMREF_TEMP_INPUT The Parameter is a TEEC_TempMemoryReference + * describing a region of memory which needs to be + * temporarily registered for the duration of the + * Operation and is tagged as input. + * + * TEEC_MEMREF_TEMP_OUTPUT Same as TEEC_MEMREF_TEMP_INPUT, but the Memory + * Reference is tagged as output. The + * Implementation may update the size field to + * reflect the required output size in some use + * cases. + * + * TEEC_MEMREF_TEMP_INOUT A Temporary Memory Reference tagged as both + * input and output, i.e., for which both the + * behaviors of TEEC_MEMREF_TEMP_INPUT and + * TEEC_MEMREF_TEMP_OUTPUT apply. + * + * TEEC_MEMREF_WHOLE The Parameter is a Registered Memory Reference + * that refers to the entirety of its parent Shared + * Memory block. The parameter structure is a + * TEEC_MemoryReference. In this structure, the + * Implementation MUST read only the parent field + * and MAY update the size field when the operation + * completes. + * + * TEEC_MEMREF_PARTIAL_INPUT A Registered Memory Reference structure that + * refers to a partial region of its parent Shared + * Memory block and is tagged as input. + * + * TEEC_MEMREF_PARTIAL_OUTPUT Registered Memory Reference structure that + * refers to a partial region of its parent Shared + * Memory block and is tagged as output. + * + * TEEC_MEMREF_PARTIAL_INOUT The Registered Memory Reference structure that + * refers to a partial region of its parent Shared + * Memory block and is tagged as both input and + * output, i.e., for which both the behaviors of + * TEEC_MEMREF_PARTIAL_INPUT and + * TEEC_MEMREF_PARTIAL_OUTPUT apply. + */ +#define TEEC_NONE 0x00000000 +#define TEEC_VALUE_INPUT 0x00000001 +#define TEEC_VALUE_OUTPUT 0x00000002 +#define TEEC_VALUE_INOUT 0x00000003 +#define TEEC_MEMREF_TEMP_INPUT 0x00000005 +#define TEEC_MEMREF_TEMP_OUTPUT 0x00000006 +#define TEEC_MEMREF_TEMP_INOUT 0x00000007 +#define TEEC_MEMREF_WHOLE 0x0000000C +#define TEEC_MEMREF_PARTIAL_INPUT 0x0000000D +#define TEEC_MEMREF_PARTIAL_OUTPUT 0x0000000E +#define TEEC_MEMREF_PARTIAL_INOUT 0x0000000F + +/** + * Flag constants indicating the data transfer direction of memory in + * TEEC_Parameter. TEEC_MEM_INPUT signifies data transfer direction from the + * client application to the TEE. TEEC_MEM_OUTPUT signifies data transfer + * direction from the TEE to the client application. Type is uint32_t. + * + * TEEC_MEM_INPUT The Shared Memory can carry data from the client + * application to the Trusted Application. + * TEEC_MEM_OUTPUT The Shared Memory can carry data from the Trusted + * Application to the client application. + */ +#define TEEC_MEM_INPUT 0x00000001 +#define TEEC_MEM_OUTPUT 0x00000002 + +/** + * Return values. Type is TEEC_Result + * + * TEEC_SUCCESS The operation was successful. + * TEEC_ERROR_GENERIC Non-specific cause. + * TEEC_ERROR_ACCESS_DENIED Access privileges are not sufficient. + * TEEC_ERROR_CANCEL The operation was canceled. + * TEEC_ERROR_ACCESS_CONFLICT Concurrent accesses caused conflict. + * TEEC_ERROR_EXCESS_DATA Too much data for the requested operation was + * passed. + * TEEC_ERROR_BAD_FORMAT Input data was of invalid format. + * TEEC_ERROR_BAD_PARAMETERS Input parameters were invalid. + * TEEC_ERROR_BAD_STATE Operation is not valid in the current state. + * TEEC_ERROR_ITEM_NOT_FOUND The requested data item is not found. + * TEEC_ERROR_NOT_IMPLEMENTED The requested operation should exist but is not + * yet implemented. + * TEEC_ERROR_NOT_SUPPORTED The requested operation is valid but is not + * supported in this implementation. + * TEEC_ERROR_NO_DATA Expected data was missing. + * TEEC_ERROR_OUT_OF_MEMORY System ran out of resources. + * TEEC_ERROR_BUSY The system is busy working on something else. + * TEEC_ERROR_COMMUNICATION Communication with a remote party failed. + * TEEC_ERROR_SECURITY A security fault was detected. + * TEEC_ERROR_SHORT_BUFFER The supplied buffer is too short for the + * generated output. + * TEEC_ERROR_TARGET_DEAD Trusted Application has panicked + * during the operation. + */ + +/** + * Standard defined error codes. + */ +#define TEEC_SUCCESS 0x00000000 +#define TEEC_ERROR_GENERIC 0xFFFF0000 +#define TEEC_ERROR_ACCESS_DENIED 0xFFFF0001 +#define TEEC_ERROR_CANCEL 0xFFFF0002 +#define TEEC_ERROR_ACCESS_CONFLICT 0xFFFF0003 +#define TEEC_ERROR_EXCESS_DATA 0xFFFF0004 +#define TEEC_ERROR_BAD_FORMAT 0xFFFF0005 +#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 +#define TEEC_ERROR_BAD_STATE 0xFFFF0007 +#define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008 +#define TEEC_ERROR_NOT_IMPLEMENTED 0xFFFF0009 +#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A +#define TEEC_ERROR_NO_DATA 0xFFFF000B +#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C +#define TEEC_ERROR_BUSY 0xFFFF000D +#define TEEC_ERROR_COMMUNICATION 0xFFFF000E +#define TEEC_ERROR_SECURITY 0xFFFF000F +#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010 +#define TEEC_ERROR_EXTERNAL_CANCEL 0xFFFF0011 +#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024 + +/** + * Function error origins, of type TEEC_ErrorOrigin. These indicate where in + * the software stack a particular return value originates from. + * + * TEEC_ORIGIN_API The error originated within the TEE Client API + * implementation. + * TEEC_ORIGIN_COMMS The error originated within the underlying + * communications stack linking the rich OS with + * the TEE. + * TEEC_ORIGIN_TEE The error originated within the common TEE code. + * TEEC_ORIGIN_TRUSTED_APP The error originated within the Trusted Application + * code. + */ +#define TEEC_ORIGIN_API 0x00000001 +#define TEEC_ORIGIN_COMMS 0x00000002 +#define TEEC_ORIGIN_TEE 0x00000003 +#define TEEC_ORIGIN_TRUSTED_APP 0x00000004 + +/** + * Session login methods, for use in TEEC_OpenSession() as parameter + * connectionMethod. Type is uint32_t. + * + * TEEC_LOGIN_PUBLIC No login data is provided. + * TEEC_LOGIN_USER Login data about the user running the Client + * Application process is provided. + * TEEC_LOGIN_GROUP Login data about the group running the Client + * Application process is provided. + * TEEC_LOGIN_APPLICATION Login data about the running Client Application + * itself is provided. + * TEEC_LOGIN_USER_APPLICATION Login data about the user and the running + * Client Application itself is provided. + * TEEC_LOGIN_GROUP_APPLICATION Login data about the group and the running + * Client Application itself is provided. + */ +#define TEEC_LOGIN_PUBLIC 0x00000000 +#define TEEC_LOGIN_USER 0x00000001 +#define TEEC_LOGIN_GROUP 0x00000002 +#define TEEC_LOGIN_APPLICATION 0x00000004 +#define TEEC_LOGIN_USER_APPLICATION 0x00000005 +#define TEEC_LOGIN_GROUP_APPLICATION 0x00000006 + +/** + * Encode the paramTypes according to the supplied types. + * + * @param p0 The first param type. + * @param p1 The second param type. + * @param p2 The third param type. + * @param p3 The fourth param type. + */ +#define TEEC_PARAM_TYPES(p0, p1, p2, p3) \ + ((p0) | ((p1) << 4) | ((p2) << 8) | ((p3) << 12)) + +/** + * Get the i_th param type from the paramType. + * + * @param p The paramType. + * @param i The i-th parameter to get the type for. + */ +#define TEEC_PARAM_TYPE_GET(p, i) (((p) >> (i * 4)) & 0xF) + +typedef uint32_t TEEC_Result; + +/** + * struct TEEC_Context - Represents a connection between a client application + * and a TEE. + */ +typedef struct { + /* Implementation defined */ + int fd; +} TEEC_Context; + +/** + * This type contains a Universally Unique Resource Identifier (UUID) type as + * defined in RFC4122. These UUID values are used to identify Trusted + * Applications. + */ +typedef struct { + uint32_t timeLow; + uint16_t timeMid; + uint16_t timeHiAndVersion; + uint8_t clockSeqAndNode[8]; +} TEEC_UUID; + +/** + * struct TEEC_SharedMemory - Memory to transfer data between a client + * application and trusted code. + * + * @param buffer The memory buffer which is to be, or has been, shared + * with the TEE. + * @param size The size, in bytes, of the memory buffer. + * @param flags Bit-vector which holds properties of buffer. + * The bit-vector can contain either or both of the + * TEEC_MEM_INPUT and TEEC_MEM_OUTPUT flags. + * + * A shared memory block is a region of memory allocated in the context of the + * client application memory space that can be used to transfer data between + * that client application and a trusted application. The user of this struct + * is responsible to populate the buffer pointer. + */ +typedef struct { + void *buffer; + size_t size; + uint32_t flags; + /* + * Implementation-Defined + */ + int id; + size_t alloced_size; + void *shadow_buffer; + int registered_fd; +} TEEC_SharedMemory; + +/** + * struct TEEC_TempMemoryReference - Temporary memory to transfer data between + * a client application and trusted code, only used for the duration of the + * operation. + * + * @param buffer The memory buffer which is to be, or has been shared with + * the TEE. + * @param size The size, in bytes, of the memory buffer. + * + * A memory buffer that is registered temporarily for the duration of the + * operation to be called. + */ +typedef struct { + void *buffer; + size_t size; +} TEEC_TempMemoryReference; + +/** + * struct TEEC_RegisteredMemoryReference - use a pre-registered or + * pre-allocated shared memory block of memory to transfer data between + * a client application and trusted code. + * + * @param parent Points to a shared memory structure. The memory reference + * may utilize the whole shared memory or only a part of it. + * Must not be NULL + * + * @param size The size, in bytes, of the memory buffer. + * + * @param offset The offset, in bytes, of the referenced memory region from + * the start of the shared memory block. + * + */ +typedef struct { + TEEC_SharedMemory *parent; + size_t size; + size_t offset; +} TEEC_RegisteredMemoryReference; + +/** + * struct TEEC_Value - Small raw data container + * + * Instead of allocating a shared memory buffer this structure can be used + * to pass small raw data between a client application and trusted code. + * + * @param a The first integer value. + * + * @param b The second second value. + */ +typedef struct { + uint32_t a; + uint32_t b; +} TEEC_Value; + +/** + * union TEEC_Parameter - Memory container to be used when passing data between + * client application and trusted code. + * + * Either the client uses a shared memory reference, parts of it or a small raw + * data container. + * + * @param tmpref A temporary memory reference only valid for the duration + * of the operation. + * + * @param memref The entire shared memory or parts of it. + * + * @param value The small raw data container to use + */ +typedef union { + TEEC_TempMemoryReference tmpref; + TEEC_RegisteredMemoryReference memref; + TEEC_Value value; +} TEEC_Parameter; + +/** + * struct TEEC_Session - Represents a connection between a client application + * and a trusted application. + */ +typedef struct { + /* Implementation defined */ + TEEC_Context *ctx; + uint32_t session_id; +} TEEC_Session; + +/** + * struct TEEC_Operation - Holds information and memory references used in + * TEEC_InvokeCommand(). + * + * @param started Client must initialize to zero if it needs to cancel + * an operation about to be performed. + * @param paramTypes Type of data passed. Use TEEC_PARAMS_TYPE macro to + * create the correct flags. + * 0 means TEEC_NONE is passed for all params. + * @param params Array of parameters of type TEEC_Parameter. + * @param session Internal pointer to the last session used by + * TEEC_InvokeCommand with this operation. + * + */ +typedef struct { + uint32_t started; + uint32_t paramTypes; + TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT]; + /* Implementation-Defined */ + TEEC_Session *session; +} TEEC_Operation; + +/** + * TEEC_InitializeContext() - Initializes a context holding connection + * information on the specific TEE, designated by the name string. + + * @param name A zero-terminated string identifying the TEE to connect to. + * If name is set to NULL, the default TEE is connected to. NULL + * is the only supported value in this version of the API + * implementation. + * + * @param context The context structure which is to be initialized. + * + * @return TEEC_SUCCESS The initialization was successful. + * @return TEEC_Result Something failed. + */ +TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context); + +/** + * TEEC_FinalizeContext() - Destroys a context holding connection information + * on the specific TEE. + * + * This function destroys an initialized TEE context, closing the connection + * between the client application and the TEE. This function must only be + * called when all sessions related to this TEE context have been closed and + * all shared memory blocks have been released. + * + * @param context The context to be destroyed. + */ +void TEEC_FinalizeContext(TEEC_Context *context); + +/** + * TEEC_OpenSession() - Opens a new session with the specified trusted + * application. + * + * @param context The initialized TEE context structure in which + * scope to open the session. + * @param session The session to initialize. + * @param destination A structure identifying the trusted application + * with which to open a session. + * + * @param connectionMethod The connection method to use. + * @param connectionData Any data necessary to connect with the chosen + * connection method. Not supported, should be set to + * NULL. + * @param operation An operation structure to use in the session. May + * be set to NULL to signify no operation structure + * needed. + * + * @param returnOrigin A parameter which will hold the error origin if + * this function returns any value other than + * TEEC_SUCCESS. + * + * @return TEEC_SUCCESS OpenSession successfully opened a new session. + * @return TEEC_Result Something failed. + * + */ +TEEC_Result TEEC_OpenSession(TEEC_Context *context, + TEEC_Session *session, + const TEEC_UUID *destination, + uint32_t connectionMethod, + const void *connectionData, + TEEC_Operation *operation, + uint32_t *returnOrigin); + +/** + * TEEC_CloseSession() - Closes the session which has been opened with the + * specific trusted application. + * + * @param session The opened session to close. + */ +void TEEC_CloseSession(TEEC_Session *session); + +/** + * TEEC_InvokeCommand() - Executes a command in the specified trusted + * application. + * + * @param session A handle to an open connection to the trusted + * application. + * @param commandID Identifier of the command in the trusted application + * to invoke. + * @param operation An operation structure to use in the invoke command. + * May be set to NULL to signify no operation structure + * needed. + * @param returnOrigin A parameter which will hold the error origin if this + * function returns any value other than TEEC_SUCCESS. + * + * @return TEEC_SUCCESS OpenSession successfully opened a new session. + * @return TEEC_Result Something failed. + */ +TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, + uint32_t commandID, + TEEC_Operation *operation, + uint32_t *returnOrigin); + +/** + * TEEC_RegisterSharedMemory() - Register a block of existing memory as a + * shared block within the scope of the specified context. + * + * @param context The initialized TEE context structure in which scope to + * open the session. + * @param sharedMem pointer to the shared memory structure to register. + * + * @return TEEC_SUCCESS The registration was successful. + * @return TEEC_ERROR_OUT_OF_MEMORY Memory exhaustion. + * @return TEEC_Result Something failed. + */ +TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, + TEEC_SharedMemory *sharedMem); + +/** + * TEEC_AllocateSharedMemory() - Allocate shared memory for TEE. + * + * @param context The initialized TEE context structure in which scope to + * open the session. + * @param sharedMem Pointer to the allocated shared memory. + * + * @return TEEC_SUCCESS The registration was successful. + * @return TEEC_ERROR_OUT_OF_MEMORY Memory exhaustion. + * @return TEEC_Result Something failed. + */ +TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, + TEEC_SharedMemory *sharedMem); + +/** + * TEEC_ReleaseSharedMemory() - Free or deregister the shared memory. + * + * @param sharedMem Pointer to the shared memory to be freed. + */ +void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMemory); + +/** + * TEEC_RequestCancellation() - Request the cancellation of a pending open + * session or command invocation. + * + * @param operation Pointer to an operation previously passed to open session + * or invoke. + */ +void TEEC_RequestCancellation(TEEC_Operation *operation); + +#endif diff --git a/src/scripts/tef-update.sh b/src/scripts/tef-update.sh new file mode 100755 index 0000000..6e5758c --- /dev/null +++ b/src/scripts/tef-update.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +TEF_CONF="/etc/tef/tef.conf" +TEF_IMPLEMENTATION=$1 + +if [ ! -f "$TEF_CONF" ]; then + exit 1 +fi + +sed -i -e 's/^lib=.*$/lib='"$TEF_IMPLEMENTATION"'/' "$TEF_CONF" diff --git a/src/simulator/creators.c b/src/simulator/creators.c new file mode 100644 index 0000000..e747d2a --- /dev/null +++ b/src/simulator/creators.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Dynowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include +#include + +void *simulator_TEEC_Context() { + return malloc(sizeof(TEEC_Context)); +} +void *simulator_TEEC_Session() { + return malloc(sizeof(TEEC_Session)); +} +void *simulator_TEEC_Operation() { + return malloc(sizeof(TEEC_Operation)); +} +void *simulator_TEEC_SharedMemory() { + return malloc(sizeof(TEEC_SharedMemory)); +} diff --git a/src/simulator/creators.h b/src/simulator/creators.h new file mode 100644 index 0000000..c7abebd --- /dev/null +++ b/src/simulator/creators.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Dynowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#ifndef __TEF_SIMULATOR_CREATORS_H__ +#define __TEF_SIMULATOR_CREATORS_H__ + +void *simulator_TEEC_Context(); +void *simulator_TEEC_Session(); +void *simulator_TEEC_Operation(); +void *simulator_TEEC_SharedMemory(); + +#endif diff --git a/src/tef-libteec.pc.in b/src/tef-libteec.pc.in new file mode 100644 index 0000000..798037b --- /dev/null +++ b/src/tef-libteec.pc.in @@ -0,0 +1,12 @@ +# Package Information for pkg-config + +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=@LIB_INSTALL_DIR@ +includedir=${prefix}/include + +Name: tef-libteec +Description: TEF - Global Platform API +Version: @_LIB_VERSION_@ +Libs: -L${libdir} -lteec -ldl +Cflags: -I${includedir}/tef diff --git a/src/tef.conf b/src/tef.conf new file mode 100644 index 0000000..817056c --- /dev/null +++ b/src/tef.conf @@ -0,0 +1,6 @@ +# Configuration file for tef-libteec package +# supported libraries: optee, simulator +# if not given or empty, will work as dummy +#lib=optee +#lib=simulator +lib= diff --git a/src/tef_libteec.c b/src/tef_libteec.c new file mode 100644 index 0000000..5796d0d --- /dev/null +++ b/src/tef_libteec.c @@ -0,0 +1,401 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Dynowski + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "simulator/creators.h" +#include "optee/creators.h" + +#define TEF_CONFIG_FILE "/etc/tef/tef.conf" +#define TEF_CONFIG_MAXLINE 1024 +#define TEF_LIB_PATH LIB_INSTALL_DIR "/tef" +#define TEF_IMPLEMENTATION_SONAME "libteec.so" +#define TEEC_SYMBOL(lib, name) *((void**)&lib.w##name) = dlsym(lib.handle, "TEEC_" #name) + +typedef struct { + const char *name; + void *handle; + + void *(*createContext)(); + void *(*createSession)(); + void *(*createOperation)(); + void *(*createSharedMemory)(); + + TEEC_Result (*wInitializeContext)(const char *name, TEEC_Context *context); + void (*wFinalizeContext)(TEEC_Context *context); + TEEC_Result (*wOpenSession)(TEEC_Context *context, + TEEC_Session *session, + const TEEC_UUID *destination, + uint32_t connectionMethod, + const void *connectionData, + TEEC_Operation *operation, + uint32_t *returnOrigin); + void (*wCloseSession)(TEEC_Session *session); + TEEC_Result (*wInvokeCommand)(TEEC_Session *session, + uint32_t commandID, + TEEC_Operation *operation, + uint32_t *returnOrigin); + TEEC_Result (*wRegisterSharedMemory)(TEEC_Context *context, TEEC_SharedMemory *sharedMemory); + TEEC_Result (*wAllocateSharedMemory)(TEEC_Context *context, TEEC_SharedMemory *sharedMemory); + void (*wReleaseSharedMemory)(TEEC_SharedMemory *sharedMemory); + void (*wRequestCancellation)(TEEC_Operation *operation); +} TEF_LibraryImpl; + +static TEF_LibraryImpl lib; + +typedef struct { + void *implementation; + void *lastOperation; +} TEF_SessionImpl; + +TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) +{ + if (lib.handle == NULL) { + return TEEC_ERROR_NOT_IMPLEMENTED; + } + if (context == NULL) { + return TEEC_ERROR_BAD_PARAMETERS; + } + context->imp = lib.createContext(); + TEEC_Result result = lib.wInitializeContext(name, (TEEC_Context *)context->imp); + if (result != TEEC_SUCCESS) { + free(context->imp); + context->imp = NULL; + } + return result; +} + + +void TEEC_FinalizeContext(TEEC_Context *context) +{ + if (lib.handle == NULL || context == NULL) { + return; + } + lib.wFinalizeContext((TEEC_Context *)context->imp); + free(context->imp); + memset(context, 0, sizeof(*context)); +} + +static void freeSession(TEF_SessionImpl *tefSession); +static TEF_SessionImpl *allocSession(TEEC_Session *session) +{ + TEF_SessionImpl *tefSession = (TEF_SessionImpl *)malloc(sizeof(TEF_SessionImpl)); + if (tefSession) { + tefSession->implementation = lib.createSession(); + tefSession->lastOperation = lib.createOperation(); + if (tefSession->implementation == NULL || tefSession->lastOperation == NULL) { + freeSession(tefSession); + return NULL; + } + session->imp = tefSession; + } + return tefSession; +} + +static void freeSession(TEF_SessionImpl *tefSession) { + if (tefSession == NULL) { + return; + } + free(tefSession->lastOperation); + tefSession->lastOperation = NULL; + free(tefSession->implementation); + tefSession->implementation = NULL; + free(tefSession); +} + +static TEEC_Operation *libOperation(TEF_SessionImpl *tefSession, TEEC_Operation *operation) +{ + if (operation == NULL) { + return NULL; + } + + operation->imp = tefSession->lastOperation; + TEEC_Operation *op = (TEEC_Operation *)operation->imp; + op->started = operation->started; + op->paramTypes = operation->paramTypes; + memcpy(op->params, operation->params, sizeof(op->params)); + return op; +} + +TEEC_Result TEEC_OpenSession(TEEC_Context *context, + TEEC_Session *session, + const TEEC_UUID *destination, + uint32_t connectionMethod, + const void *connectionData, + TEEC_Operation *operation, + uint32_t *returnOrigin) +{ + if (lib.handle == NULL) { + *returnOrigin = TEEC_ORIGIN_API; + return TEEC_ERROR_NOT_IMPLEMENTED; + } + if (context == NULL || session == NULL || context->imp == NULL) { + return TEEC_ERROR_BAD_PARAMETERS; + } + TEF_SessionImpl *tefSession = allocSession(session); + if (tefSession == NULL) { + return TEEC_ERROR_OUT_OF_MEMORY; + } + TEEC_Operation *op = libOperation(tefSession, operation); + TEEC_Result result = lib.wOpenSession((TEEC_Context *)context->imp, + (TEEC_Session *)tefSession->implementation, + destination, connectionMethod, connectionData, op, returnOrigin); + if (result != TEEC_SUCCESS) { + freeSession(tefSession); + session->imp = NULL; + } + return result; +} + + +void TEEC_CloseSession(TEEC_Session *session) +{ + if (lib.handle == NULL || session == NULL) { + return; + } + TEF_SessionImpl *tefSession = (TEF_SessionImpl *)session->imp; + if (tefSession == NULL) { + return; + } + lib.wCloseSession((TEEC_Session *)tefSession->implementation); + freeSession(tefSession); + memset(session, 0, sizeof(*session)); +} + + +TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, + uint32_t commandID, + TEEC_Operation *operation, + uint32_t *returnOrigin) +{ + if (lib.handle == NULL) { + *returnOrigin = TEEC_ORIGIN_API; + return TEEC_ERROR_NOT_IMPLEMENTED; + } + TEF_SessionImpl *tefSession = (TEF_SessionImpl *)session->imp; + if (tefSession == NULL) { + return TEEC_ERROR_BAD_PARAMETERS; + } + TEEC_Operation *op = libOperation(tefSession, operation); + TEEC_Result result = lib.wInvokeCommand((TEEC_Session *)tefSession->implementation, + commandID, op, returnOrigin); + if (op != NULL) { + operation->started = op->started; + operation->paramTypes = op->paramTypes; + memcpy(operation->params, op->params, sizeof(operation->params)); + } + return result; +} + + +TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, + TEEC_SharedMemory *sharedMemory) +{ + if (lib.handle == NULL) { + return TEEC_ERROR_NOT_IMPLEMENTED; + } + if (sharedMemory == NULL) { + return TEEC_ERROR_BAD_PARAMETERS; + } + sharedMemory->imp = lib.createSharedMemory(); + if (sharedMemory->imp == NULL) { + return TEEC_ERROR_OUT_OF_MEMORY; + } + TEEC_Result result = lib.wRegisterSharedMemory((TEEC_Context *)context->imp, sharedMemory->imp); + if (result != TEEC_SUCCESS) { + free(sharedMemory->imp); + sharedMemory->imp = NULL; + } + return result; +} + + +TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, + TEEC_SharedMemory *sharedMemory) +{ + if (lib.handle == NULL) { + return TEEC_ERROR_NOT_IMPLEMENTED; + } + if (sharedMemory == NULL) { + return TEEC_ERROR_BAD_PARAMETERS; + } + sharedMemory->imp = lib.createSharedMemory(); + if (sharedMemory->imp == NULL) { + return TEEC_ERROR_OUT_OF_MEMORY; + } + TEEC_Result result = lib.wAllocateSharedMemory((TEEC_Context *)context->imp, sharedMemory->imp); + if (result != TEEC_SUCCESS) { + free(sharedMemory->imp); + sharedMemory->imp = NULL; + } + return result; +} + + +void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMemory) +{ + if (lib.handle == NULL || sharedMemory == NULL || sharedMemory->imp == NULL) { + return; + } + lib.wReleaseSharedMemory(sharedMemory->imp); + free(sharedMemory->imp); + sharedMemory->imp = NULL; +} + + +void TEEC_RequestCancellation(TEEC_Operation *operation) +{ + if (lib.handle == NULL || operation == NULL || operation->imp == NULL) { + return; + } + lib.wRequestCancellation(operation->imp); +} + +static struct { + char libname[50]; +} config; + +static void configParam(const char *p) +{ + if (*p == '#') { + return; + } + if (strncmp(p, "lib=", 4) == 0) { + const char *libname = p + 4; + if (strlen(libname) > sizeof(config.libname) - 1) { + fprintf(stderr, "tef-libteec: name to long '%s'\n", libname); + return; + } + strcpy(config.libname, libname); + } +} + +static void readConfig() +{ + int fd; + memset(&config, 0, sizeof(config)); + if ((fd = open(TEF_CONFIG_FILE, O_RDONLY)) < 0) { + return; + } + + char buf[TEF_CONFIG_MAXLINE]; + ssize_t r; + size_t l = 0; + while ((r = read(fd, buf + l, sizeof(buf) - l)) > 0) { + buf[l + r] = 0; + char *p = strchr(buf, '\n'); + if (p == NULL) { + break; + } + l = 0; + do { + *p = 0; + configParam(buf + l); + l = p - buf + 1; + p = strchr(buf + l, '\n'); + } while (p != NULL); + memcpy(buf, buf + l, r - l); + l = r - l; + } + close(fd); +} + +static int findLibrary(const char *name, char *libname) +{ + snprintf(libname, PATH_MAX, TEF_LIB_PATH "/%s/" TEF_IMPLEMENTATION_SONAME, name); + return access(libname, R_OK) == 0; +} + +static void loadLibrary(const char *name) +{ + char libfile[PATH_MAX]; + lib.name = name; + if (findLibrary(name, libfile)) { + lib.handle = dlopen(libfile, RTLD_NOW); + if (lib.handle == NULL) { + fprintf(stderr, "tef-libteec: %s\n", dlerror()); + return; + } + } else { + fprintf(stderr, "tef-libteec: implementation '%s' not found in " TEF_LIB_PATH "\n", config.libname); + lib.handle = NULL; + return; + } + + if (strcmp(name, "simulator") == 0) { + lib.createContext = simulator_TEEC_Context; + lib.createSession = simulator_TEEC_Session; + lib.createOperation = simulator_TEEC_Operation; + lib.createSharedMemory = simulator_TEEC_SharedMemory; + } else if (strcmp(name, "optee") == 0) { + lib.createContext = optee_TEEC_Context; + lib.createSession = optee_TEEC_Session; + lib.createOperation = optee_TEEC_Operation; + lib.createSharedMemory = optee_TEEC_SharedMemory; + } else { + fprintf(stderr, "tef-libteec: implementation '%s' is not supported\n", name); + dlclose(lib.handle); + lib.handle = NULL; + return; + } + + TEEC_SYMBOL(lib, InitializeContext); + TEEC_SYMBOL(lib, FinalizeContext); + TEEC_SYMBOL(lib, OpenSession); + TEEC_SYMBOL(lib, CloseSession); + TEEC_SYMBOL(lib, InvokeCommand); + TEEC_SYMBOL(lib, RegisterSharedMemory); + TEEC_SYMBOL(lib, AllocateSharedMemory); + TEEC_SYMBOL(lib, ReleaseSharedMemory); + TEEC_SYMBOL(lib, RequestCancellation); +} + +// Note: _init() and _fini() are obsolete +void __attribute__ ((constructor)) tef_load(void) +{ + //read configuration file + readConfig(); + + memset(&lib, 0, sizeof(lib)); + if (config.libname[0]) { + loadLibrary(config.libname); + } + + if (lib.handle == NULL) { // fallback to dummy + lib.name = "dummy"; + } + + fprintf(stderr, "tef-libteec: linked to %s\n", lib.name); +} +void __attribute__ ((destructor)) tef_unload(void) +{ + if (lib.handle != NULL) { + dlclose(lib.handle); // decrement refcount to wrapped library + } + memset(&lib, 0, sizeof(lib)); +} -- 2.7.4