1 /*
2 * Copyright (c) 2023-2024 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 <string>
17
18 #include "cert_context.h"
19 #include "net_ssl_async_work.h"
20 #include "net_ssl_exec.h"
21 #include "net_ssl_module.h"
22
23 #include "js_native_api.h"
24 #include "module_template.h"
25 #include "netstack_log.h"
26
27 namespace OHOS::NetStack::Ssl {
28 static constexpr const char *NET_SSL_MODULE_NAME = "net.networkSecurity";
29
30 #ifdef MAC_PLATFORM
31 static constexpr const char *VERIFY_ASYNC_WORK_NAME = "ExecVerify";
32 #endif
33
34 struct AsyncCallbackInfo {
35 napi_env env;
36 napi_async_work asyncWork;
37 napi_deferred deferred;
38 };
39
InitNetSslModule(napi_env env,napi_value exports)40 napi_value NetSslModuleExports::InitNetSslModule(napi_env env, napi_value exports)
41 {
42 InitSslProperties(env, exports);
43 NapiUtils::SetEnvValid(env);
44 napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env);
45 return exports;
46 }
47
InitSslProperties(napi_env env,napi_value exports)48 void NetSslModuleExports::InitSslProperties(napi_env env, napi_value exports)
49 {
50 std::initializer_list<napi_property_descriptor> properties = {
51 DECLARE_NAPI_FUNCTION("certVerification", VerifyCertification),
52 DECLARE_NAPI_FUNCTION("certVerificationSync", VerifyCertificationSync)};
53 NapiUtils::DefineProperties(env, exports, properties);
54
55 InitCertType(env, exports);
56 }
57
InitCertType(napi_env env,napi_value exports)58 void NetSslModuleExports::InitCertType(napi_env env, napi_value exports)
59 {
60 std::initializer_list<napi_property_descriptor> properties = {
61 DECLARE_NAPI_STATIC_PROPERTY("CERT_TYPE_PEM", NapiUtils::CreateUint32(env, CERT_TYPE_PEM)),
62 DECLARE_NAPI_STATIC_PROPERTY("CERT_TYPE_DER", NapiUtils::CreateUint32(env, CERT_TYPE_DER))};
63
64 napi_value certType = NapiUtils::CreateObject(env);
65 NapiUtils::DefineProperties(env, certType, properties);
66 NapiUtils::SetNamedProperty(env, exports, "CertType", certType);
67 }
68
VerifyCertification(napi_env env,napi_callback_info info)69 napi_value NetSslModuleExports::VerifyCertification(napi_env env, napi_callback_info info)
70 {
71 return ModuleTemplate::InterfaceWithOutAsyncWork<CertContext>(
72 env, info,
73 [](napi_env, napi_value, CertContext *context) -> bool {
74 SslExec::AsyncRunVerify(context);
75 return context->IsExecOK();
76 },
77 "VerifyCertification", NetSslAsyncWork::ExecVerify, NetSslAsyncWork::VerifyCallback);
78 }
79
VerifyCertificationSync(napi_env env,napi_callback_info info)80 napi_value NetSslModuleExports::VerifyCertificationSync(napi_env env, napi_callback_info info)
81 {
82 napi_value thisVal = nullptr;
83 size_t paramsCount = MAX_PARAM_NUM;
84 napi_value params[MAX_PARAM_NUM] = {nullptr};
85 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
86 EventManager *manager = nullptr;
87 auto context = std::make_unique<CertContext>(env, manager);
88 context->ParseParams(params, paramsCount);
89 if (context->GetErrorCode() != PARSE_ERROR_CODE) {
90 if (context->GetCertBlobClient() == nullptr) {
91 context->SetErrorCode(NetStackVerifyCertification(context->GetCertBlob()));
92 NETSTACK_LOGD("verifyResult is %{public}d\n", context->GetErrorCode());
93 } else {
94 context->SetErrorCode(NetStackVerifyCertification(context->GetCertBlob(), context->GetCertBlobClient()));
95 NETSTACK_LOGD("verifyResult is %{public}d\n", context->GetErrorCode());
96 }
97 }
98
99 napi_value verifyResult;
100 napi_status status = napi_create_int32(env, context->GetErrorCode(), &verifyResult);
101 if (status != napi_ok) {
102 return nullptr;
103 }
104 return verifyResult;
105 }
106
107 static napi_module g_sslModule = {
108 .nm_version = 1,
109 .nm_flags = 0,
110 .nm_filename = nullptr,
111 .nm_register_func = NetSslModuleExports::InitNetSslModule,
112 .nm_modname = NET_SSL_MODULE_NAME,
113 .nm_priv = nullptr,
114 .reserved = {nullptr},
115 };
116
RegisterSslModule(void)117 extern "C" __attribute__((constructor)) void RegisterSslModule(void)
118 {
119 napi_module_register(&g_sslModule);
120 }
121 } // namespace OHOS::NetStack::Ssl
122