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 VendorSecureElementCaOnStart() const;
50     int VendorSecureElementCaInit() const;
51     int VendorSecureElementCaUninit() const;
52     int VendorSecureElementCaGetAtr(uint8_t *rsp, uint32_t *rspLen) const;
53     int VendorSecureElementCaOpenLogicalChannel(
54         uint8_t *aid, uint32_t len, uint8_t p2, uint8_t *rsp, uint32_t *rspLen, uint32_t *channelNum) const;
55     int VendorSecureElementCaOpenBasicChannel(uint8_t *aid, uint32_t len, uint8_t *rsp, uint32_t *rspLen) const;
56     int VendorSecureElementCaCloseChannel(uint32_t channelNum) const;
57     int VendorSecureElementCaTransmit(uint8_t *cmd, uint32_t cmdLen, uint8_t *rsp, uint32_t *rspLen) const;
58 
59 private:
60     class DynamicLoad {
61     public:
62         explicit DynamicLoad(const std::string &lib);
63 
64         ~DynamicLoad();
65 
66         DynamicLoad(const DynamicLoad &) = delete;
67         DynamicLoad &operator=(DynamicLoad &) = delete;
68 
69         bool LoadLib();
70 
71         bool CloseLib();
72 
73         template <typename T>
FindTheFunc(const std::string & func)74         T FindTheFunc(const std::string &func)
75         {
76             if (!handle_) {
77                 HDF_LOGE("fail handle is null");
78                 return nullptr;
79             }
80             T newFunc = reinterpret_cast<T>(dlsym(handle_, func.c_str()));
81             if (!newFunc) {
82                 HDF_LOGE("find func:%{public}s in %{public}s fail", func.c_str(), libPath_.c_str());
83                 return nullptr;
84             }
85             HDF_LOGI("find func:%{public}s in %{public}s success", func.c_str(), libPath_.c_str());
86             return newFunc;
87         }
88 
89     private:
90         void *handle_{nullptr};
91         std::string libPath_;
92     };
93 
94     using VendorSecureElementCaInitT = int (*)(void);
95     using VendorSecureElementCaUninitT = int (*)(void);
96     using VendorSecureElementCaGetAtrT = int (*)(uint8_t *rsp, uint32_t *rspLen);
97     using VendorSecureElementCaOpenLogicalChannelT = int (*)(uint8_t *aid, uint32_t len, uint8_t p2, uint8_t *rsp,
98                                                             uint32_t *rspLen, uint32_t *channelNum);
99     using VendorSecureElementCaOpenBasicChannelT = int (*)(uint8_t *aid, uint32_t len, uint8_t *rsp, uint32_t *rspLen);
100     using VendorSecureElementCaCloseChannelT = int (*)(uint32_t channelNum);
101     using VendorSecureElementCaTransmitT = int (*)(uint8_t *cmd, uint32_t cmdLen, uint8_t *rsp, uint32_t *rspLen);
102     using VendorSecureElementCaOnStartT = int (*)(void);
103     const char *const LIB_NAME = "libsecure_element_ca.z.so";
104     const char *const CA_INIT_SYMBOL = "VendorSecureElementCaInit";
105     const char *const CA_UNINIT_SYMBOL = "VendorSecureElementCaUninit";
106     const char *const CA_GET_ATR_SYMBOL = "VendorSecureElementCaGetAtr";
107     const char *const CA_OPEN_LOGICAL_SYMBOL = "VendorSecureElementCaOpenLogicalChannel";
108     const char *const CA_OPEN_BASIC_SYMBOL = "VendorSecureElementCaOpenBasicChannel";
109     const char *const CA_CLOSE_SYMBOL = "VendorSecureElementCaCloseChannel";
110     const char *const CA_TRANS_SYMBOL = "VendorSecureElementCaTransmit";
111     const char *const CA_ON_START_SYMBOL = "VendorSecureElementOnStart";
112 
113     SecureElementCaProxy();
114 
115     void InitFunc();
116     VendorSecureElementCaOnStartT vendorSecureElementCaOnStartFunc_{nullptr};
117     VendorSecureElementCaInitT vendorSecureElementCaInitFunc_{nullptr};
118     VendorSecureElementCaUninitT vendorSecureElementCaUninitFunc_{nullptr};
119     VendorSecureElementCaGetAtrT vendorSecureElementCaGetAtrFunc_{nullptr};
120     VendorSecureElementCaOpenLogicalChannelT vendorSecureElementCaOpenLogicalChannelFunc_{nullptr};
121     VendorSecureElementCaOpenBasicChannelT vendorSecureElementCaOpenBasicChannelFunc_{nullptr};
122     VendorSecureElementCaCloseChannelT vendorSecureElementCaCloseChannelFunc_{nullptr};
123     VendorSecureElementCaTransmitT vendorSecureElementCaTransmitFunc_{nullptr};
124     static inline std::unique_ptr<DynamicLoad> loader_;
125 };
126 
127 }  // SecureElement
128 }  // HDI
129 }  // OHOS
130 
131 #endif