1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SECURE_ELEMENT_CA_PROXY_H 16 #define SECURE_ELEMENT_CA_PROXY_H 17 18 #include <dlfcn.h> 19 #include <hdf_log.h> 20 #include <memory> 21 #include <string> 22 23 enum SECURE_ELEMENT_CA_RET { 24 SECURE_ELEMENT_CA_RET_OK = 0, 25 SECURE_ELEMENT_CA_RET_CONTEXT_FAIL = 1, 26 SECURE_ELEMENT_CA_RET_MEMSET_FAIL, 27 SECURE_ELEMENT_CA_RET_TEE_UNINITED, 28 SECURE_ELEMENT_CA_RET_ESE_CONFIG_FAIL, 29 SECURE_ELEMENT_CA_RET_END, 30 SECURE_ELEMENT_CA_RET_LOAD_FAIL, 31 }; 32 33 namespace OHOS { 34 namespace HDI { 35 namespace SecureElement { 36 class SecureElementCaProxy { 37 public: 38 ~SecureElementCaProxy() = default; 39 40 SecureElementCaProxy(const SecureElementCaProxy &) = delete; 41 SecureElementCaProxy &operator=(SecureElementCaProxy &) = delete; 42 GetInstance()43 static SecureElementCaProxy &GetInstance() 44 { 45 static SecureElementCaProxy instance; 46 return instance; 47 } 48 49 int VendorSecureElementCaInit() const; 50 int VendorSecureElementCaUninit() const; 51 int VendorSecureElementCaGetAtr(uint8_t *rsp, uint32_t *rspLen) const; 52 int VendorSecureElementCaOpenLogicalChannel( 53 uint8_t *aid, uint32_t len, uint8_t p2, uint8_t *rsp, uint32_t *rspLen, uint32_t *channelNum) const; 54 int VendorSecureElementCaOpenBasicChannel(uint8_t *aid, uint32_t len, uint8_t *rsp, uint32_t *rspLen) const; 55 int VendorSecureElementCaCloseChannel(uint32_t channelNum) const; 56 int VendorSecureElementCaTransmit(uint8_t *cmd, uint32_t cmdLen, uint8_t *rsp, uint32_t *rspLen) const; 57 58 private: 59 class DynamicLoad { 60 public: 61 explicit DynamicLoad(const std::string &lib); 62 63 ~DynamicLoad(); 64 65 DynamicLoad(const DynamicLoad &) = delete; 66 DynamicLoad &operator=(DynamicLoad &) = delete; 67 68 bool LoadLib(); 69 70 bool CloseLib(); 71 72 template <typename T> FindTheFunc(const std::string & func)73 T FindTheFunc(const std::string &func) 74 { 75 if (!handle_) { 76 HDF_LOGE("fail handle is null"); 77 return nullptr; 78 } 79 T newFunc = reinterpret_cast<T>(dlsym(handle_, func.c_str())); 80 if (!newFunc) { 81 HDF_LOGE("find func:%{public}s in %{public}s fail", func.c_str(), libPath_.c_str()); 82 return nullptr; 83 } 84 HDF_LOGI("find func:%{public}s in %{public}s success", func.c_str(), libPath_.c_str()); 85 return newFunc; 86 } 87 88 private: 89 void *handle_{nullptr}; 90 std::string libPath_; 91 }; 92 93 using VendorSecureElementCaInitT = int (*)(void); 94 using VendorSecureElementCaUninitT = int (*)(void); 95 using VendorSecureElementCaGetAtrT = int (*)(uint8_t *rsp, uint32_t *rspLen); 96 using VendorSecureElementCaOpenLogicalChannelT = int (*)(uint8_t *aid, uint32_t len, uint8_t p2, uint8_t *rsp, 97 uint32_t *rspLen, uint32_t *channelNum); 98 using VendorSecureElementCaOpenBasicChannelT = int (*)(uint8_t *aid, uint32_t len, uint8_t *rsp, uint32_t *rspLen); 99 using VendorSecureElementCaCloseChannelT = int (*)(uint32_t channelNum); 100 using VendorSecureElementCaTransmitT = int (*)(uint8_t *cmd, uint32_t cmdLen, uint8_t *rsp, uint32_t *rspLen); 101 const char *const LIB_NAME = "libsecure_element_ca.z.so"; 102 const char *const CA_INIT_SYMBOL = "VendorSecureElementCaInit"; 103 const char *const CA_UNINIT_SYMBOL = "VendorSecureElementCaUninit"; 104 const char *const CA_GET_ATR_SYMBOL = "VendorSecureElementCaGetAtr"; 105 const char *const CA_OPEN_LOGICAL_SYMBOL = "VendorSecureElementCaOpenLogicalChannel"; 106 const char *const CA_OPEN_BASIC_SYMBOL = "VendorSecureElementCaOpenBasicChannel"; 107 const char *const CA_CLOSE_SYMBOL = "VendorSecureElementCaCloseChannel"; 108 const char *const CA_TRANS_SYMBOL = "VendorSecureElementCaTransmit"; 109 110 SecureElementCaProxy(); 111 112 void InitFunc(); 113 VendorSecureElementCaInitT vendorSecureElementCaInitFunc_{nullptr}; 114 VendorSecureElementCaUninitT vendorSecureElementCaUninitFunc_{nullptr}; 115 VendorSecureElementCaGetAtrT vendorSecureElementCaGetAtrFunc_{nullptr}; 116 VendorSecureElementCaOpenLogicalChannelT vendorSecureElementCaOpenLogicalChannelFunc_{nullptr}; 117 VendorSecureElementCaOpenBasicChannelT vendorSecureElementCaOpenBasicChannelFunc_{nullptr}; 118 VendorSecureElementCaCloseChannelT vendorSecureElementCaCloseChannelFunc_{nullptr}; 119 VendorSecureElementCaTransmitT vendorSecureElementCaTransmitFunc_{nullptr}; 120 static inline std::unique_ptr<DynamicLoad> loader_; 121 }; 122 123 } // SecureElement 124 } // HDI 125 } // OHOS 126 127 #endif