1 /*
2 * Copyright (c) 2022 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 "cm_napi_uninstall_app_cert.h"
17 #include "cm_napi_uninstall_app_cert_common.h"
18
19 #include "securec.h"
20
21 #include "cert_manager_api.h"
22 #include "cm_log.h"
23 #include "cm_mem.h"
24 #include "cm_type.h"
25 #include "cm_napi_common.h"
26
27 namespace CMNapi {
28 namespace {
29 constexpr int CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS = 1;
30 constexpr int CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS = 2;
31 } // namespace
32
CreateUninstallAppCertAsyncContext()33 UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext()
34 {
35 UninstallAppCertAsyncContext context =
36 static_cast<UninstallAppCertAsyncContext>(CmMalloc(sizeof(UninstallAppCertAsyncContextT)));
37 if (context != nullptr) {
38 (void)memset_s(
39 context, sizeof(UninstallAppCertAsyncContextT), 0, sizeof(UninstallAppCertAsyncContextT));
40 }
41 return context;
42 }
43
DeleteUninstallAppCertAsyncContext(napi_env env,UninstallAppCertAsyncContext & context)44 void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context)
45 {
46 if (context == nullptr) {
47 return;
48 }
49
50 DeleteNapiContext(env, context->asyncWork, context->callback);
51
52 if (context->keyUri != nullptr) {
53 FreeCmBlob(context->keyUri);
54 }
55
56 CmFree(context);
57 context = nullptr;
58 }
59
UninstallAppCertParseParams(napi_env env,napi_callback_info info,UninstallAppCertAsyncContext context)60 napi_value UninstallAppCertParseParams(
61 napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context)
62 {
63 size_t argc = CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS;
64 napi_value argv[CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS] = { nullptr };
65 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
66
67 if (argc < CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS) {
68 ThrowParamsError(env, PARAM_ERROR, "Missing parameter");
69 CM_LOG_E("Missing parameter");
70 return nullptr;
71 }
72
73 size_t index = 0;
74 napi_value result = ParseString(env, argv[index], context->keyUri);
75 if (result == nullptr) {
76 ThrowParamsError(env, PARAM_ERROR, "get keyUri type error");
77 CM_LOG_E("could not get cert uri");
78 return nullptr;
79 }
80
81 index++;
82 if (index < argc) {
83 context->callback = GetCallback(env, argv[index]);
84 if (context->callback == nullptr) {
85 ThrowParamsError(env, PARAM_ERROR, "get callback type error");
86 CM_LOG_E("get callback function faild when uninstall applicaiton cert");
87 return nullptr;
88 }
89 }
90
91 return GetInt32(env, 0);
92 }
93
UninstallAppCertAsyncWork(napi_env env,UninstallAppCertAsyncContext asyncContext)94 napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext asyncContext)
95 {
96 napi_value promise = nullptr;
97 GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
98
99 napi_value resourceName = nullptr;
100 NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
101
102 NAPI_CALL(env, napi_create_async_work(
103 env,
104 nullptr,
105 resourceName,
106 [](napi_env env, void *data) {
107 UninstallAppCertAsyncContext context = static_cast<UninstallAppCertAsyncContext>(data);
108 context->result = CmUninstallAppCert(context->keyUri, context->store);
109 },
110 [](napi_env env, napi_status status, void *data) {
111 UninstallAppCertAsyncContext context = static_cast<UninstallAppCertAsyncContext>(data);
112 napi_value result[RESULT_NUMBER] = { nullptr };
113 if (context->result == CM_SUCCESS) {
114 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
115 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1]));
116 } else {
117 const char *errorMsg = "uninstall app cert error";
118 result[0] = GenerateBusinessError(env, context->result, errorMsg);
119 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
120 }
121 if (context->deferred != nullptr) {
122 GeneratePromise(env, context->deferred, context->result, result, sizeof(result));
123 } else {
124 GenerateCallback(env, context->callback, result, sizeof(result));
125 }
126 DeleteUninstallAppCertAsyncContext(env, context);
127 },
128 static_cast<void *>(asyncContext),
129 &asyncContext->asyncWork));
130
131 napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
132 if (status != napi_ok) {
133 GET_AND_THROW_LAST_ERROR((env));
134 DeleteUninstallAppCertAsyncContext(env, asyncContext);
135 CM_LOG_E("could not queue async work");
136 return nullptr;
137 }
138
139 return promise;
140 }
141
CMNapiUninstallAppCertCommon(napi_env env,napi_callback_info info,uint32_t store)142 napi_value CMNapiUninstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store)
143 {
144 UninstallAppCertAsyncContext context = CreateUninstallAppCertAsyncContext();
145 if (context == nullptr) {
146 CM_LOG_E("could not create context");
147 return nullptr;
148 }
149
150 context->store = store;
151
152 napi_value result = UninstallAppCertParseParams(env, info, context);
153 if (result == nullptr) {
154 CM_LOG_E("could not parse params");
155 DeleteUninstallAppCertAsyncContext(env, context);
156 return nullptr;
157 }
158 result = UninstallAppCertAsyncWork(env, context);
159 if (result == nullptr) {
160 CM_LOG_E("could not start async work");
161 DeleteUninstallAppCertAsyncContext(env, context);
162 return nullptr;
163 }
164 return result;
165 }
166 } // namespace CertManagerNapi
167