/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2018-2019. All rights reserved. * Description: head file of hidl for tee * Author: NameMagic * Create: 2018-03-10 */ #ifndef VENDOR_HARDWARE_LIBTEEC_V3_0_LIBTEEC_H #define VENDOR_HARDWARE_LIBTEEC_V3_0_LIBTEEC_H #include #include #include #include #include #include #include #include #include #include #include #include "tee_client_type.h" #include "tee_client_inner.h" namespace vendor { namespace huanglong { namespace hardware { namespace libteec { namespace V3_0 { namespace implementation { using::android::hardware::Return; using::android::hardware::hidl_vec; using::android::hardware::hidl_string; using::vendor::huanglong::hardware::libteec::V3_0::ILibteecGlobal; using::vendor::huanglong::hardware::libteec::V3_0::ILibteecGlobalNotify; using::android::sp; using::android::hardware::hidl_death_recipient; using::android::Mutex; const static int MAX_CXTCNT_ONECA = 5; const static int BUF_MAX_SIZE = 4096; const static int PARAM_NUM = 4; #define SYS_HASH_FILE_MAX_SIZE (16 * 1024) typedef enum { SYSTEM_CA = 1, VENDOR_CA, APP_CA, MAX_CA, } CaType; typedef struct { int callingPid; uint32_t opsCnt; int32_t cxtFd[MAX_CXTCNT_ONECA]; struct listnode procdataHead; } DaemonProcdata; typedef struct { int callingPid; int tid; struct listnode tidHead; } TidData; typedef struct { uint8_t certs[BUF_MAX_SIZE]; CaType type; uid_t uid; pid_t pid; int fromHidlSide; } CaAuthInfo; typedef struct { uint8_t certs[BUF_MAX_SIZE]; pid_t pid; } CaCertInfo; typedef struct { CaCertInfo certInfo; struct listnode authNode; } CaLoginNode; typedef struct { int32_t cmdId; int32_t pid; int32_t ret; int32_t originRet; TEEC_Operation *operationPtr; TEEC_ContextHidl *outContextPtr; TEEC_Session *outSessionPtr; } InOutPara; typedef struct { uint32_t offset; uint32_t memSize; uint32_t totalSize; uint32_t paraType; } InputPara; typedef enum { TEE_HIDL_FAILURE = -1, TEE_HIDL_SUCCESS = 0, } LibteecErrCode; typedef struct { uint32_t fileSize; /* xml file size */ uint8_t fileBuf[SYS_HASH_FILE_MAX_SIZE]; /* read xml file data to this buffer */ } TEEC_XmlParameter; /* * GP standard Interfaces */ using InitializeContextFunc = int32_t (*)(const char *name, TEEC_ContextHidl *context, const int32_t type, bool fromHidl, CaAuthInfo *authInfo); using OpenSessionFunc = int32_t (*)(int pid, const TaFileInfo *taFile, TEEC_ContextHidl *context, TEEC_Session *session, const TEEC_UUID *destination, uint32_t connectionMethod, const void *connectionData, TEEC_Operation *operation, uint32_t *returnOrigin); using InvokeCommandFunc = int32_t (*)(TEEC_ContextHidl *context, TEEC_Session *session, uint32_t commandID, TEEC_Operation *operation, uint32_t *returnOrigin); using CloseSessionFunc = void (*)(TEEC_Session *session, TEEC_ContextHidl *context); using RegisterSharedMemoryFunc = int32_t (*)(TEEC_ContextHidl *context, TEEC_SharedMemoryHidl *sharedMem); using AllocateSharedMemoryFunc = int32_t (*)(TEEC_ContextHidl *context, TEEC_SharedMemoryHidl *sharedMem); using ReleaseSharedMemoryFunc = void (*)(TEEC_SharedMemoryHidl *sharedMem); using requestCancellation_f = void (*)(TEEC_Operation *operation); using ExtTuiSendEventFunc = int32_t (*)(const TEEC_TUI_Parameter *tuiParam, CaAuthInfo *authInfo); using GetTEEVersionFunc = uint32_t (*)(); using ExtSendSysHashXmlFunc = int32_t (*)(const TEEC_XmlParameter *halXmlPtr, CaAuthInfo *authInfo); using GetBnContextFunc = TEEC_ContextHidl *(*)(const TEEC_Context *context); using PutBnContextFunc = void (*)(TEEC_ContextHidl *context); using FindAndRemoveBnContextFunc = TEEC_ContextHidl *(*)(const TEEC_Context *context); using GetBnSessionFunc = TEEC_Session *(*)(const TEEC_Session *session, TEEC_ContextHidl *context); using PutBnSessionFunc = void (*)(TEEC_Session *session); using FindAndRemoveBnSessionFunc = TEEC_Session *(*)(const TEEC_Session *session, TEEC_ContextHidl *context); using GetBnShmByOffsetFunc = TEEC_SharedMemoryHidl *(*)(uint32_t shmOffset, TEEC_ContextHidl *context); using PutBnShmFunc = void (*)(TEEC_SharedMemoryHidl *shrMem); using SendSecfileFunc = int32_t (*)(const char *path, int tzFd, const FILE *fp); struct LibteecGlobal : public ILibteecGlobal, public hidl_death_recipient { LibteecGlobal() : mHandle(nullptr), initializeContextProxy(nullptr), openSessionProxy(nullptr), invokeCommandProxy(nullptr), closeSessionProxy(nullptr), registerSharedMemoryProxy(nullptr), allocateSharedMemoryProxy(nullptr), releaseSharedMemoryProxy(nullptr), extTuiSendEventProxy(nullptr), getTEEVersionProxy(nullptr), extSendSysHashXmlProxy(nullptr), getBnContextProxy(nullptr), putBnContextProxy(nullptr), findAndRemoveBnContextProxy(nullptr), getBnSessionProxy(nullptr), putBnSessionProxy(nullptr), findAndRemoveBnSessionProxy(nullptr), getBnShmByOffsetProxy(nullptr), putBnShmProxy(nullptr), sendSecfileProxy(nullptr) { ALOGD("Init LibteecGlobal!\n"); } virtual ~LibteecGlobal(); virtual Return initializeContext(const hidl_string &name, const hidl_vec &caInfo, initializeContext_cb hidlCallBackPtr) override; virtual Return finalizeContext(int32_t pid, const hidl_vec &halCxtPtr) override; virtual Return openSession(int32_t pid, const hidl_vec &halCxtPtr, const android::hardware::hidl_handle &handle, const hidl_string &taPath, const hidl_vec &halUuidPtr, uint32_t halConnMth, const hidl_vec &haConnData, const hidl_vec &halOptPtr, const android::hardware::hidl_memory &opMem, openSession_cb hidlCallBackPtr) override; virtual Return closeSession(int32_t pid, const hidl_vec &halCxtPtr, const hidl_vec &halSessPtr) override; virtual Return invokeCommandHidl(int32_t pid, const hidl_vec &halCxtPtr, const hidl_vec &halSessPtr, uint32_t cmdId, const hidl_vec &halOptPtr, const android::hardware::hidl_memory &opMem, invokeCommandHidl_cb hidlCallBackPtr) override; virtual Return registerSharedMemory(int32_t pid, const hidl_vec &halCxtPtr, const hidl_vec &halMemPtr, registerSharedMemory_cb hidlCallBackPtr) override; virtual Return allocateSharedMemory(int32_t pid, const hidl_vec &halCxtPtr, const hidl_vec &halMemPtr, allocateSharedMemory_cb hidlCallBackPtr) override; virtual Return releaseSharedMemory(int32_t pid, const hidl_vec &halCxtPtr, const hidl_vec &halMemPtr, uint32_t shmOffset) override; virtual Return requestCancellation(const hidl_vec &halOptPtr) override; virtual Return extTuiSendEvent(int32_t pid, int32_t uid, const hidl_vec &halTuiPtr) override; virtual Return handleMultiUserMsg(const hidl_vec &multiUserRecvMsg) override; void serviceDied(uint64_t cookie, const::android::wp<::android::hidl::base::V1_0::IBase> &who) override; virtual Return processCaDied(int32_t pid) override; virtual Return setCallBack(const ::android::sp ¬ify) override; virtual Return iGetTEEVersionHidl() override; virtual Return extSendSystemHashXml(const hidl_vec &halXmlPtr, const hidl_vec &authInfo) override; virtual Return secfileSendEvent(int32_t pid, const android::hardware::hidl_handle &handle, const hidl_string &libPath, const hidl_vec &halCxtPtr, const hidl_vec &halSessPtr) override; private: virtual Return CallGetProcDataPtr(int pid); virtual Return CallInitializeContextProxy(const hidl_string &name, CaAuthInfo *authInfo, TEEC_ContextHidl **outHidlContext); virtual Return CallFinalizeContextProxy(const TEEC_Context *contextPtr); virtual Return CallGetBnContextProxy(const hidl_vec &halCxtPtr, int pid, TEEC_Session **outHidlSession, TEEC_ContextHidl **outHidlContext); virtual Return SubOpenSession(const TaFileInfo *taFile, const hidl_vec &halOptPtr, const android::hardware::hidl_memory &opMem, const hidl_vec &halUuidPtr, const hidl_vec &haConnData, InOutPara *paraInOut); virtual Return CallOpenSessionProxy(const hidl_vec &halUuidPtr, const TaFileInfo *taFile, TEEC_ContextHidl *outContext, TEEC_Session *outSession, InOutPara *paraInOut, const hidl_vec &haConnData); virtual Return CallCloseSessionProxy(TEEC_Session *outSession, TEEC_ContextHidl *outContext, int pid); virtual Return CallGetBnProxy(int pid, const hidl_vec &halCxtPtr, const hidl_vec &halSessPtr, TEEC_ContextHidl **outHidlContext, TEEC_Session **outHidlSession); virtual Return SubInvokeCommand(const hidl_vec &halOptPtr, const android::hardware::hidl_memory &opMem, InOutPara *paraInOut); virtual Return CallInvokeCommandProxy(TEEC_ContextHidl *outContext, TEEC_Session *outSession, TEEC_Operation *operation, InOutPara *paraInOut); virtual Return CallRegisterSharedMemoryProxy(const hidl_vec &halMemPtr, TEEC_ContextHidl *outContext, TEEC_SharedMemoryHidl *outRegShm); virtual Return GetSharedMemoryContextAndFd(const hidl_vec &halCxtPtr, int32_t pid, TEEC_ContextHidl **outContext, int *fd); virtual Return CallAllocateSharedMemoryProxy(const hidl_vec &halMemPtr, TEEC_ContextHidl *outContext, TEEC_SharedMemoryHidl *outAllocShm); virtual Return CallReleaseSharedMemoryProxy(const hidl_vec &halCxtPtr, TEEC_SharedMemoryHidl *outShm); virtual Return CheckInputPidUid(int pid, uid_t uid); virtual Return DecodeHidlMemory(TEEC_ContextHidl *outContext, TEEC_Operation *operation, TEEC_SharedMemory *shm, TEEC_SharedMemoryHidl *shmHidl[], uint32_t shmSize, uint8_t *data, size_t memSize); virtual Return FillShareMemoryBuffer(TEEC_SharedMemory *shareMemBuf, TEEC_SharedMemoryHidl **shmHidl, uint8_t *data, InputPara *inputPara, TEEC_ContextHidl *outContext, TEEC_Parameter *params); virtual void PutAllocShrMem(TEEC_SharedMemoryHidl *shmHidl[], uint32_t shmNum); virtual Return GetFpFromHandle(const android::hardware::hidl_handle &handle, native_handle_t *&tempHandle) const; virtual Return CleanProcDataForOneCa(DaemonProcdata *procData); virtual Return CheckAndOpenHandle(); bool IsValidContext(const TEEC_Context *context, int pid); bool IsValidContextWithoutLock(const TEEC_Context *context, int pid); virtual Return GetOperationFromHidlVec(const hidl_vec &halOptPtr, TEEC_Operation *operation); void *mHandle; Mutex mHandleLock; Mutex mProcDataLock; InitializeContextFunc initializeContextProxy; OpenSessionFunc openSessionProxy; InvokeCommandFunc invokeCommandProxy; CloseSessionFunc closeSessionProxy; RegisterSharedMemoryFunc registerSharedMemoryProxy; AllocateSharedMemoryFunc allocateSharedMemoryProxy; ReleaseSharedMemoryFunc releaseSharedMemoryProxy; ExtTuiSendEventFunc extTuiSendEventProxy; GetTEEVersionFunc getTEEVersionProxy; ExtSendSysHashXmlFunc extSendSysHashXmlProxy; GetBnContextFunc getBnContextProxy; PutBnContextFunc putBnContextProxy; FindAndRemoveBnContextFunc findAndRemoveBnContextProxy; GetBnSessionFunc getBnSessionProxy; PutBnSessionFunc putBnSessionProxy; FindAndRemoveBnSessionFunc findAndRemoveBnSessionProxy; GetBnShmByOffsetFunc getBnShmByOffsetProxy; PutBnShmFunc putBnShmProxy; SendSecfileFunc sendSecfileProxy; }; extern "C" ILibteecGlobal *HIDL_FETCH_ILibteecGlobal(const char *name); } // namespace implementation } // namespace V3_0 } // namespace libteec } // namespace hardware } // namespace huanglong } // namespace vendor #endif // VENDOR_HARDWARE_LIBTEEC_V3_0_LIBTEEC_H