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 "net_ssl_exec.h"
17 
18 #include "napi_utils.h"
19 #include "net_ssl_async_work.h"
20 #include "netstack_common_utils.h"
21 #include "netstack_log.h"
22 
23 namespace OHOS::NetStack::Ssl {
CallbackTemplate(uv_work_t * work,int status)24 template <napi_value (*MakeJsValue)(napi_env, void *)> static void CallbackTemplate(uv_work_t *work, int status)
25 {
26     (void)status;
27 
28     auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
29     napi_env env = workWrapper->env;
30     auto closeScope = [env](napi_handle_scope scope) { NapiUtils::CloseScope(env, scope); };
31     std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(NapiUtils::OpenScope(env), closeScope);
32 
33     napi_value obj = MakeJsValue(env, workWrapper->data);
34 
35     std::pair<napi_value, napi_value> arg = {NapiUtils::GetUndefined(workWrapper->env), obj};
36     workWrapper->manager->Emit(workWrapper->type, arg);
37 
38     delete workWrapper;
39     delete work;
40 }
41 
ExecVerify(CertContext * context)42 bool SslExec::ExecVerify(CertContext *context)
43 {
44     context->SetPermissionDenied(true);
45 
46     if (context->GetManager()->IsEventDestroy()) {
47         return false;
48     }
49 
50     if (context->GetErrorCode() == PARSE_ERROR_CODE) {
51         return false;
52     }
53 
54     if (context->GetCertBlob() == nullptr) {
55         return false;
56     }
57 
58     if (context->GetCertBlobClient() == nullptr) {
59         context->SetErrorCode(NetStackVerifyCertification(context->GetCertBlob()));
60         NETSTACK_LOGD("verifyResult is %{public}d\n", context->GetErrorCode());
61 
62         if (context->GetErrorCode() != 0) {
63             if (EventManager::IsManagerValid(context->GetManager())) {
64                 NapiUtils::CreateUvQueueWorkEnhanced(context->GetEnv(), context, NetSslAsyncWork::VerifyCallback);
65             }
66             return false;
67         }
68     } else {
69         context->SetErrorCode(NetStackVerifyCertification(context->GetCertBlob(), context->GetCertBlobClient()));
70         NETSTACK_LOGD("verifyResult is %{public}d\n", context->GetErrorCode());
71         if (context->GetErrorCode() != 0) {
72             if (EventManager::IsManagerValid(context->GetManager())) {
73                 NapiUtils::CreateUvQueueWorkEnhanced(context->GetEnv(), context, NetSslAsyncWork::VerifyCallback);
74             }
75             return false;
76         }
77     }
78 
79     return true;
80 }
81 
VerifyCallback(CertContext * context)82 napi_value SslExec::VerifyCallback(CertContext *context)
83 {
84     napi_value result;
85     napi_create_int32(context->GetEnv(), static_cast<int32_t>(context->GetErrorCode()), &result);
86     return result;
87 }
88 
89 #ifndef MAC_PLATFORM
AsyncRunVerify(CertContext * context)90 void SslExec::AsyncRunVerify(CertContext *context)
91 {
92     NetSslAsyncWork::ExecVerify(context->GetEnv(), context);
93 }
94 #endif
95 } // namespace OHOS::NetStack::Ssl
96