• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "eap_napi_event.h"
17 #include "napi_constant.h"
18 
19 namespace OHOS {
20 namespace NetManagerStandard {
21 
22 namespace {
23 #ifdef NET_EXTENSIBLE_AUTHENTICATION
24 
25 static constexpr const int MAX_EAP_DATA_LENGTH = 4096;
26 
EapNapiReturn(const napi_env & env,bool cond,int32_t errCode)27 static inline napi_value EapNapiReturn(const napi_env &env, bool cond, int32_t errCode)
28 {
29     napi_value res = nullptr;
30     if (!cond) {
31         napi_throw_error(env, std::to_string(errCode).c_str(), std::to_string(errCode).c_str());
32         return res;
33     }
34     napi_get_undefined(env, &res);
35     return res;
36 }
37 
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)38 static bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
39 {
40     if (static_cast<int>(paramsCount) == PARAM_TRIPLE_OPTIONS_AND_CALLBACK) {
41         return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number &&
42                NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_number &&
43                NapiUtils::GetValueType(env, params[ARG_INDEX_2]) == napi_number &&
44                NapiUtils::GetValueType(env, params[ARG_INDEX_3]) == napi_function;
45     }
46     if (static_cast<int>(paramsCount) == PARAM_DOUBLE_OPTIONS) {
47         return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number &&
48                NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_object;
49     }
50     return false;
51 }
52 
CheckStartEthEapParams(napi_env env,napi_value * params,size_t paramsCount)53 static bool CheckStartEthEapParams(napi_env env, napi_value *params, size_t paramsCount)
54 {
55     if (paramsCount == PARAM_DOUBLE_OPTIONS) {
56         return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number &&
57             NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_object;
58     }
59     return false;
60 }
61 
GetU8VectorFromJsOptionItem(const napi_env env,const napi_value config,const std::string & key,std::vector<uint8_t> & value)62 static bool GetU8VectorFromJsOptionItem(const napi_env env, const napi_value config,
63     const std::string &key, std::vector<uint8_t> &value)
64 {
65     bool hasProperty = NapiUtils::HasNamedProperty(env, config, key);
66     if (!hasProperty) {
67         NETMGR_EXT_LOG_E("JsObjectToU8Vector no property: %{public}s", key.c_str());
68         return false;
69     }
70     napi_value array = NapiUtils::GetNamedProperty(env, config, key);
71     bool isTypedArray = false;
72     if (napi_is_typedarray(env, array, &isTypedArray) != napi_ok || !isTypedArray) {
73         NETMGR_EXT_LOG_E("JsObjectToU8Vector not typedarray: %{public}s", key.c_str());
74         return false;
75     }
76     size_t length = 0;
77     size_t offset = 0;
78     napi_typedarray_type type;
79     napi_value buffer = nullptr;
80     NAPI_CALL_BASE(env, napi_get_typedarray_info(env, array, &type, &length, nullptr, &buffer, &offset), {});
81     if (type != napi_uint8_array || buffer == nullptr) {
82         NETMGR_EXT_LOG_E("JsObjectToU8Vector buffer null: %{public}s", key.c_str());
83         return false;
84     }
85     size_t total = 0;
86     uint8_t *data = nullptr;
87     NAPI_CALL_BASE(env, napi_get_arraybuffer_info(env, buffer, reinterpret_cast<void **>(&data), &total), {});
88     length = std::min<size_t>(length, total - offset);
89     value.resize(length);
90     memcpy_s(value.data(), value.size(), &data[offset], length);
91     return true;
92 }
93 #endif
94 } // namespace
95 
RegCustomEapHandler(napi_env env,napi_callback_info info)96 napi_value RegCustomEapHandler(napi_env env, napi_callback_info info)
97 {
98 #ifdef NET_EXTENSIBLE_AUTHENTICATION
99     size_t requireArgc = 4;
100     size_t argc = 4;
101     napi_value argv[4] = {0};
102     napi_value thisVar = 0;
103     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
104     if (requireArgc > argc) {
105         NETMANAGER_EXT_LOGE("requireArgc:%{public}zu, argc:%{public}zu", requireArgc, argc);
106         return NapiUtils::GetUndefined(env);
107     }
108 
109     if (!CheckParamsType(env, argv, argc)) {
110         NETMANAGER_EXT_LOGE("params type error");
111         return NapiUtils::GetUndefined(env);
112     }
113 
114     int32_t netType = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_0]);
115     if (netType < static_cast<int>(NetType::WLAN0) || netType >= static_cast<int>(NetType::INVALID)) {
116         NETMANAGER_EXT_LOGE("invalid netType %{public}d", static_cast<int>(netType));
117         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_NET_TYPE);
118     }
119 
120     uint32_t eapCode = NapiUtils::GetUint32FromValue(env, argv[ARG_INDEX_1]);
121     if (eapCode < EAP_CODE_MIN || eapCode > EAP_CODE_MAX) {
122         NETMANAGER_EXT_LOGE("invalid eapCode %{public}d", eapCode);
123         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_EAP_CODE);
124     }
125 
126     uint32_t eapType = NapiUtils::GetUint32FromValue(env, argv[ARG_INDEX_2]);
127     if (eapType < EAP_TYPE_MIN || eapType > EAP_TYPE_MAX) {
128         NETMANAGER_EXT_LOGE("invalid eapType %{public}d", eapType);
129         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_EAP_TYPE);
130     }
131 
132     int32_t ret = EapEventMgr::GetInstance().RegCustomEapHandler(env, static_cast<NetType>(netType),
133         eapCode, eapType, argv[ARG_INDEX_3]);
134     return EapNapiReturn(env, ret == EAP_ERRCODE_SUCCESS, ret);
135 #endif
136     napi_value result = nullptr;
137     napi_get_undefined(env, &result);
138     return result;
139 }
140 
UnRegCustomEapHandler(napi_env env,napi_callback_info info)141 napi_value UnRegCustomEapHandler(napi_env env, napi_callback_info info)
142 {
143 #ifdef NET_EXTENSIBLE_AUTHENTICATION
144     size_t requireArgc = 4;
145     size_t argc = 4;
146     napi_value argv[4] = {0};
147     napi_value thisVar = 0;
148     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
149     if (requireArgc > argc) {
150         NETMANAGER_EXT_LOGE("requireArgc:%{public}zu, argc:%{public}zu", requireArgc, argc);
151         return NapiUtils::GetUndefined(env);
152     }
153 
154     if (!CheckParamsType(env, argv, argc)) {
155         NETMANAGER_EXT_LOGE("params type error");
156         return NapiUtils::GetUndefined(env);
157     }
158 
159     int32_t netType = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_0]);
160     if (netType < static_cast<int>(NetType::WLAN0) || netType >= static_cast<int>(NetType::INVALID)) {
161         NETMANAGER_EXT_LOGE("invalid netType %{public}d", static_cast<int>(netType));
162         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_NET_TYPE);
163     }
164 
165     int32_t eapCode = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_1]);
166     if (eapCode < EAP_CODE_MIN || eapCode > EAP_CODE_MAX) {
167         NETMANAGER_EXT_LOGE("invalid eapCode %{public}d", eapCode);
168         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_EAP_CODE);
169     }
170 
171     int32_t eapType = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_2]);
172     if (eapType < EAP_TYPE_MIN || eapType > EAP_TYPE_MAX) {
173         NETMANAGER_EXT_LOGE("invalid eapType %{public}d", eapType);
174         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_EAP_TYPE);
175     }
176 
177     int32_t ret = EapEventMgr::GetInstance().UnRegCustomEapHandler(env, static_cast<NetType>(netType),
178         eapCode, eapType, argv[ARG_INDEX_3]);
179     return EapNapiReturn(env, ret == EAP_ERRCODE_SUCCESS, ret);
180 #endif
181     napi_value result = nullptr;
182     napi_get_undefined(env, &result);
183     return result;
184 }
185 
ReplyCustomEapData(napi_env env,napi_callback_info info)186 napi_value ReplyCustomEapData(napi_env env, napi_callback_info info)
187 {
188 #ifdef NET_EXTENSIBLE_AUTHENTICATION
189     size_t requireArgc = 2;
190     size_t argc = 2;
191     napi_value argv[2] = {0};
192     napi_value thisVar = 0;
193     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
194     if (requireArgc > argc) {
195         NETMANAGER_EXT_LOGE("requireArgc:%{public}zu, argc:%{public}zu", requireArgc, argc);
196         return NapiUtils::GetUndefined(env);
197     }
198 
199     if (!CheckParamsType(env, argv, argc)) {
200         NETMANAGER_EXT_LOGE("params type error");
201         return NapiUtils::GetUndefined(env);
202     }
203 
204     int32_t replyResult = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_0]);
205     if (replyResult < static_cast<int32_t>(CustomResult::RESULT_FAIL) ||
206         replyResult > static_cast<int32_t>(CustomResult::RESULT_FINISH)) {
207         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_RESULT);
208     }
209     CustomResult customResult = static_cast<CustomResult>(replyResult);
210     sptr<EapData> eapData = new (std::nothrow) EapData();
211     if (eapData == nullptr) {
212         NETMANAGER_EXT_LOGE("%{public}s, eapData is nullptr", __func__);
213         return EapNapiReturn(env, false, EAP_ERRCODE_INTERNAL_ERROR);
214     }
215     eapData->msgId = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "msgId");
216     eapData->bufferLen = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "bufferLen");
217     NapiUtils::GetVectorUint8Property(env, argv[ARG_INDEX_1], "eapBuffer", eapData->eapBuffer);
218 
219     if (eapData->bufferLen > MAX_EAP_DATA_LENGTH) {
220         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_SIZE_OF_EAPDATA);
221     }
222 
223     NETMANAGER_EXT_LOGI("%{public}s, result:%{public}d, msgId:%{public}d, bufferLen:%{public}d,  buffsize:%{public}zu, "
224         "eapCode:%{public}d, eapType:%{public}d ",
225         __func__, static_cast<int>(customResult), eapData->msgId, eapData->bufferLen,  eapData->eapBuffer.size(),
226         eapData->eapCode, eapData->eapType);
227     int32_t ret = EapEventMgr::GetInstance().ReplyCustomEapData(customResult, eapData);
228     return EapNapiReturn(env, ret == EAP_ERRCODE_SUCCESS, ret);
229 #endif
230     napi_value result = nullptr;
231     napi_get_undefined(env, &result);
232     return result;
233 }
234 
StartEthEap(napi_env env,napi_callback_info info)235 napi_value StartEthEap(napi_env env, napi_callback_info info)
236 {
237 #ifdef NET_EXTENSIBLE_AUTHENTICATION
238     size_t argc = 2;
239     napi_value argv[2] = {0};
240     napi_value thisVar = 0;
241     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
242     if (!CheckStartEthEapParams(env, argv, argc)) {
243         return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_PROFILE);
244     }
245     int32_t netId = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_0]);
246     EthEapProfile profile;
247     int32_t tmpVal = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "eapMethod");
248     if (tmpVal < static_cast<int32_t>(EapMethod::EAP_NONE) ||
249         tmpVal > static_cast<int32_t>(EapMethod::EAP_UNAUTH_TLS)) {
250             return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_PROFILE);
251     }
252     profile.eapMethod = static_cast<EapMethod>(tmpVal);
253     tmpVal = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "phase2Method");
254     if (tmpVal < static_cast<int32_t>(Phase2Method::PHASE2_NONE) ||
255         tmpVal > static_cast<int32_t>(Phase2Method::PHASE2_AKA_PRIME)) {
256             return EapNapiReturn(env, false, EAP_ERRCODE_INVALID_PROFILE);
257     }
258     profile.phase2Method = static_cast<Phase2Method>(tmpVal);
259     profile.identity = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "identity");
260     profile.anonymousIdentity = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "anonymousIdentity");
261     profile.password = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "password");
262     profile.caCertAliases = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "caCertAliases");
263     profile.caPath = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "caPath");
264     profile.clientCertAliases = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "clientCertAliases");
265     GetU8VectorFromJsOptionItem(env, argv[ARG_INDEX_1], "certEntry", profile.certEntry);
266     profile.certPassword = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "certPassword");
267     profile.altSubjectMatch = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "altSubjectMatch");
268     profile.domainSuffixMatch = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "domainSuffixMatch");
269     profile.realm = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "realm");
270     profile.plmn = NapiUtils::GetStringPropertyUtf8(env, argv[ARG_INDEX_1], "plmn");
271     profile.eapSubId = NapiUtils::GetInt32Property(env, argv[ARG_INDEX_1], "eapSubId");
272     int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->StartEthEap(netId, profile);
273     return EapNapiReturn(env, ret == EAP_ERRCODE_SUCCESS, ret);
274 #endif
275     return NapiUtils::GetUndefined(env);
276 }
277 
LogOffEthEap(napi_env env,napi_callback_info info)278 napi_value LogOffEthEap(napi_env env, napi_callback_info info)
279 {
280 #ifdef NET_EXTENSIBLE_AUTHENTICATION
281     size_t argc = 2;
282     napi_value argv[2] = {0};
283     napi_value thisVar = 0;
284     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
285     if ((argc != PARAM_JUST_OPTIONS) || (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) != napi_number)) {
286         return EapNapiReturn(env, false, EAP_ERRCODE_LOGOFF_FAIL);
287     }
288     int32_t netId = NapiUtils::GetInt32FromValue(env, argv[ARG_INDEX_0]);
289     int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->LogOffEthEap(netId);
290     return EapNapiReturn(env, (ret == EAP_ERRCODE_SUCCESS), ret);
291 #endif
292     return NapiUtils::GetUndefined(env);
293 }
294 
295 } // namespace NetManagerStandard
296 } // namespace OHOS