• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "se_vendor_adaptions.h"
17 
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <vector>
21 
22 #include "secure_element_ca.h"
23 
24 #define HDF_LOG_TAG hdf_se
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace SecureElement {
29 static sptr<ISecureElementCallback> g_callbackV1_0 = nullptr;
30 static const int RES_BUFFER_MAX_LENGTH = 512;
31 
SeVendorAdaptions()32 SeVendorAdaptions::SeVendorAdaptions() {}
33 
~SeVendorAdaptions()34 SeVendorAdaptions::~SeVendorAdaptions() {}
35 
init(const sptr<ISecureElementCallback> & clientCallback,SecureElementStatus & status)36 int32_t SeVendorAdaptions::init(const sptr<ISecureElementCallback>& clientCallback, SecureElementStatus& status)
37 {
38     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
39     if (clientCallback == nullptr) {
40         HDF_LOGE("init failed, clientCallback is null");
41         status = SecureElementStatus::SE_NULL_POINTER_ERROR;
42         return HDF_ERR_INVALID_PARAM;
43     }
44 
45     int ret = HuaweiSecureElementCaInit();
46     if (ret != SECURE_ELEMENT_CA_RET_OK) {
47         HDF_LOGE("getAtr failed ret %u", ret);
48         status = SecureElementStatus::SE_GENERAL_ERROR;
49         return HDF_ERR_INVALID_PARAM;
50     }
51     g_callbackV1_0 = clientCallback;
52     g_callbackV1_0->OnSeStateChanged(true);
53     status = SecureElementStatus::SE_SUCCESS;
54     return HDF_SUCCESS;
55 }
56 
getAtr(std::vector<uint8_t> & response)57 int32_t SeVendorAdaptions::getAtr(std::vector<uint8_t>& response)
58 {
59     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
60     uint8_t res[RES_BUFFER_MAX_LENGTH] = {0};
61     uint32_t resLen = RES_BUFFER_MAX_LENGTH;
62     int ret = HuaweiSecureElementCaGetAtr(res, &resLen);
63     for (uint32_t i = 0; i < resLen; i++) {
64         response.push_back(res[i]);
65     }
66     if (ret != SECURE_ELEMENT_CA_RET_OK) {
67         HDF_LOGE("getAtr failed ret %u", ret);
68     }
69     return HDF_SUCCESS;
70 }
71 
isSecureElementPresent(bool & present)72 int32_t SeVendorAdaptions::isSecureElementPresent(bool& present)
73 {
74     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
75     if (g_callbackV1_0 == nullptr) {
76         present = false;
77     } else {
78         present = true;
79     }
80     return HDF_SUCCESS;
81 }
82 
openLogicalChannel(const std::vector<uint8_t> & aid,uint8_t p2,std::vector<uint8_t> & response,uint8_t & channelNumber,SecureElementStatus & status)83 int32_t SeVendorAdaptions::openLogicalChannel(const std::vector<uint8_t>& aid, uint8_t p2,
84     std::vector<uint8_t>& response, uint8_t& channelNumber, SecureElementStatus& status)
85 {
86     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
87     if (aid.empty()) {
88         HDF_LOGE("aid is null");
89         status = SecureElementStatus::SE_ILLEGAL_PARAMETER_ERROR;
90         return HDF_ERR_INVALID_PARAM;
91     }
92     uint8_t res[RES_BUFFER_MAX_LENGTH] = {0};
93     uint32_t resLen = RES_BUFFER_MAX_LENGTH;
94     int ret = HuaweiSecureElementCaOpenLogicalChannel((uint8_t *)&aid[0], aid.size(), p2, res, &resLen,
95         (uint32_t *)&channelNumber);
96     for (uint32_t i = 0; i < resLen; i++) {
97         response.push_back(res[i]);
98     }
99     if (ret != SECURE_ELEMENT_CA_RET_OK) {
100         status = SecureElementStatus::SE_GENERAL_ERROR;
101         HDF_LOGE("openLogicalChannel failed ret %u", ret);
102     }
103     status = SecureElementStatus::SE_SUCCESS;
104     return HDF_SUCCESS;
105 }
106 
openBasicChannel(const std::vector<uint8_t> & aid,uint8_t p2,std::vector<uint8_t> & response,SecureElementStatus & status)107 int32_t SeVendorAdaptions::openBasicChannel(const std::vector<uint8_t>& aid, uint8_t p2, std::vector<uint8_t>& response,
108     SecureElementStatus& status)
109 {
110     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
111     if (aid.empty()) {
112         HDF_LOGE("aid is null");
113         status = SecureElementStatus::SE_ILLEGAL_PARAMETER_ERROR;
114         return HDF_ERR_INVALID_PARAM;
115     }
116     uint8_t res[RES_BUFFER_MAX_LENGTH] = {0};
117     uint32_t resLen = RES_BUFFER_MAX_LENGTH;
118     int ret = HuaweiSecureElementCaOpenBasicChannel((uint8_t *)&aid[0], aid.size(), res, &resLen);
119     for (uint32_t i = 0; i < resLen; i++) {
120         response.push_back(res[i]);
121     }
122     if (ret != SECURE_ELEMENT_CA_RET_OK) {
123         status = SecureElementStatus::SE_GENERAL_ERROR;
124         HDF_LOGE("openBasicChannel failed ret %u", ret);
125     }
126     status = SecureElementStatus::SE_SUCCESS;
127     return HDF_SUCCESS;
128 }
129 
closeChannel(uint8_t channelNumber,SecureElementStatus & status)130 int32_t SeVendorAdaptions::closeChannel(uint8_t channelNumber, SecureElementStatus& status)
131 {
132     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
133     int ret = HuaweiSecureElementCaCloseChannel(channelNumber);
134     if (ret != SECURE_ELEMENT_CA_RET_OK) {
135         status = SecureElementStatus::SE_GENERAL_ERROR;
136         HDF_LOGE("closeChannel failed ret %u", ret);
137     }
138     status = SecureElementStatus::SE_SUCCESS;
139     return HDF_SUCCESS;
140 }
141 
transmit(const std::vector<uint8_t> & command,std::vector<uint8_t> & response,SecureElementStatus & status)142 int32_t SeVendorAdaptions::transmit(const std::vector<uint8_t>& command, std::vector<uint8_t>& response,
143     SecureElementStatus& status)
144 {
145     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
146     uint8_t res[RES_BUFFER_MAX_LENGTH] = {0};
147     uint32_t resLen = RES_BUFFER_MAX_LENGTH;
148     int ret = HuaweiSecureElementCaTransmit((uint8_t *)&command[0], command.size(), res, &resLen);
149     for (uint32_t i = 0; i < resLen; i++) {
150         response.push_back(res[i]);
151     }
152     if (ret != SECURE_ELEMENT_CA_RET_OK) {
153         status = SecureElementStatus::SE_GENERAL_ERROR;
154         HDF_LOGE("transmit failed ret %u", ret);
155     }
156     status = SecureElementStatus::SE_SUCCESS;
157     return HDF_SUCCESS;
158 }
159 
reset(SecureElementStatus & status)160 int32_t SeVendorAdaptions::reset(SecureElementStatus& status)
161 {
162     HDF_LOGI("SeVendorAdaptions:%{public}s!", __func__);
163     HDF_LOGE("reset is not support");
164     status = SecureElementStatus::SE_SUCCESS;
165     return HDF_SUCCESS;
166 }
167 } // SecureElement
168 } // HDI
169 } // OHOS