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_get_app_cert_list.h"
17 #include "cm_napi_get_app_cert_list_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_GET_APP_CERT_LIST_MIN_ARGS = 0;
30 constexpr int CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS = 1;
31 } // namespace
32
CreateGetAppCertListAsyncContext()33 GetAppCertListAsyncContext CreateGetAppCertListAsyncContext()
34 {
35 GetAppCertListAsyncContext context =
36 static_cast<GetAppCertListAsyncContext>(CmMalloc(sizeof(GetAppCertListAsyncContextT)));
37 if (context != nullptr) {
38 (void)memset_s(context, sizeof(GetAppCertListAsyncContextT), 0, sizeof(GetAppCertListAsyncContextT));
39 }
40 return context;
41 }
42
DeleteGetAppCertListAsyncContext(napi_env env,GetAppCertListAsyncContext & context)43 void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context)
44 {
45 if (context == nullptr) {
46 return;
47 }
48
49 DeleteNapiContext(env, context->asyncWork, context->callback);
50
51 if (context->credentialList != nullptr) {
52 FreeCredentialList(context->credentialList);
53 }
54
55 CmFree(context);
56 context = nullptr;
57 }
58
GetAppCertListParseParams(napi_env env,napi_callback_info info,GetAppCertListAsyncContext context)59 napi_value GetAppCertListParseParams(
60 napi_env env, napi_callback_info info, GetAppCertListAsyncContext context)
61 {
62 size_t argc = CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS;
63 napi_value argv[CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS] = { nullptr };
64 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
65
66 if ((argc != CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS) && (argc != CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS)) {
67 ThrowError(env, PARAM_ERROR, "Missing parameter, arguments count need between 0 and 1.");
68 CM_LOG_E("Missing parameter");
69 return nullptr;
70 }
71
72 size_t index = 0;
73 if (index < argc) {
74 int32_t ret = GetCallback(env, argv[index], context->callback);
75 if (ret != CM_SUCCESS) {
76 ThrowError(env, PARAM_ERROR, "Get callback failed, callback must be a function.");
77 CM_LOG_E("get callback function faild when getting application certificate list");
78 return nullptr;
79 }
80 }
81
82 return GetInt32(env, 0);
83 }
84
GetAppCertListWriteResult(napi_env env,GetAppCertListAsyncContext context)85 napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context)
86 {
87 napi_value result = nullptr;
88 NAPI_CALL(env, napi_create_object(env, &result));
89 napi_value credentail = GenerateCredentialAbstractArray(env,
90 context->credentialList->credentialAbstract, context->credentialList->credentialCount);
91 if (credentail != nullptr) {
92 napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL_LIST.c_str(), credentail);
93 } else {
94 NAPI_CALL(env, napi_get_undefined(env, &result));
95 }
96 return result;
97 }
98
InitAppCertList(struct CredentialList * credentialList)99 void InitAppCertList(struct CredentialList *credentialList)
100 {
101 uint32_t buffSize = (MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract));
102 credentialList->credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
103 if (credentialList->credentialAbstract == nullptr) {
104 CM_LOG_E("malloc file buffer failed");
105 return;
106 }
107 (void)memset_s(credentialList->credentialAbstract, buffSize, 0, buffSize);
108 credentialList->credentialCount = MAX_COUNT_CERTIFICATE;
109 }
110
GetAppCertListAsyncWork(napi_env env,GetAppCertListAsyncContext asyncContext)111 napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext asyncContext)
112 {
113 napi_value promise = nullptr;
114 GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
115
116 napi_value resourceName = nullptr;
117 NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
118
119 NAPI_CALL(env, napi_create_async_work(
120 env,
121 nullptr,
122 resourceName,
123 [](napi_env env, void *data) {
124 GetAppCertListAsyncContext context = static_cast<GetAppCertListAsyncContext>(data);
125
126 context->credentialList = static_cast<struct CredentialList *>(CmMalloc(sizeof(struct CredentialList)));
127 if (context->credentialList != nullptr) {
128 InitAppCertList(context->credentialList);
129 }
130 context->result = CmGetAppCertList(context->store, context->credentialList);
131 },
132 [](napi_env env, napi_status status, void *data) {
133 GetAppCertListAsyncContext context = static_cast<GetAppCertListAsyncContext>(data);
134 napi_value result[RESULT_NUMBER] = { nullptr };
135 if (context->result == CM_SUCCESS) {
136 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
137 result[1] = GetAppCertListWriteResult(env, context);
138 } else {
139 result[0] = GenerateBusinessError(env, context->result);
140 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
141 }
142 if (context->deferred != nullptr) {
143 GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
144 } else {
145 GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
146 }
147 DeleteGetAppCertListAsyncContext(env, context);
148 CM_LOG_D("get app cert list end");
149 },
150 static_cast<void *>(asyncContext),
151 &asyncContext->asyncWork));
152
153 napi_status napiStatus = napi_queue_async_work(env, asyncContext->asyncWork);
154 if (napiStatus != napi_ok) {
155 GET_AND_THROW_LAST_ERROR((env));
156 DeleteGetAppCertListAsyncContext(env, asyncContext);
157 CM_LOG_E("get app cert list could not queue async work");
158 return nullptr;
159 }
160 return promise;
161 }
162
GetCallingAppCertListAsyncWork(napi_env env,GetAppCertListAsyncContext asyncContext)163 napi_value GetCallingAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext asyncContext)
164 {
165 napi_value promise = nullptr;
166 GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
167
168 napi_value resourceName = nullptr;
169 NAPI_CALL(env, napi_create_string_latin1(env, "GetCallingAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
170
171 NAPI_CALL(env, napi_create_async_work(
172 env,
173 nullptr,
174 resourceName,
175 [](napi_env env, void *information) {
176 GetAppCertListAsyncContext mcontext = static_cast<GetAppCertListAsyncContext>(information);
177
178 mcontext->credentialList = static_cast<struct CredentialList *>(CmMalloc(sizeof(struct CredentialList)));
179 if (mcontext->credentialList != nullptr) {
180 InitAppCertList(mcontext->credentialList);
181 }
182 mcontext->result = CmCallingGetAppCertList(mcontext->store, mcontext->credentialList);
183 },
184 [](napi_env env, napi_status status, void *information) {
185 GetAppCertListAsyncContext mcontext = static_cast<GetAppCertListAsyncContext>(information);
186 napi_value res[RESULT_NUMBER] = { nullptr };
187 if (mcontext->result == CM_SUCCESS) {
188 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &res[0]));
189 res[1] = GetAppCertListWriteResult(env, mcontext);
190 } else {
191 res[0] = GenerateBusinessError(env, mcontext->result);
192 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &res[1]));
193 }
194 if (mcontext->deferred != nullptr) {
195 GeneratePromise(env, mcontext->deferred, mcontext->result, res, CM_ARRAY_SIZE(res));
196 } else {
197 GenerateCallback(env, mcontext->callback, res, CM_ARRAY_SIZE(res), mcontext->result);
198 }
199 DeleteGetAppCertListAsyncContext(env, mcontext);
200 CM_LOG_D("get calling app cert list end");
201 },
202 static_cast<void *>(asyncContext),
203 &asyncContext->asyncWork));
204
205 napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
206 if (status != napi_ok) {
207 GET_AND_THROW_LAST_ERROR((env));
208 DeleteGetAppCertListAsyncContext(env, asyncContext);
209 CM_LOG_E("get calling app cert list could not queue async work");
210 return nullptr;
211 }
212 return promise;
213 }
214
CMNapiGetAppCertListCommon(napi_env env,napi_callback_info info,uint32_t store)215 napi_value CMNapiGetAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store)
216 {
217 GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext();
218 if (context == nullptr) {
219 CM_LOG_E("could not create context");
220 return nullptr;
221 }
222
223 context->store = store;
224
225 napi_value result = GetAppCertListParseParams(env, info, context);
226 if (result == nullptr) {
227 CM_LOG_E("could not parse params");
228 DeleteGetAppCertListAsyncContext(env, context);
229 return nullptr;
230 }
231 result = GetAppCertListAsyncWork(env, context);
232 if (result == nullptr) {
233 CM_LOG_E("could not start async work");
234 DeleteGetAppCertListAsyncContext(env, context);
235 return nullptr;
236 }
237 return result;
238 }
239
CMNapiGetCallingAppCertListCommon(napi_env env,napi_callback_info info,uint32_t store)240 napi_value CMNapiGetCallingAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store)
241 {
242 GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext();
243 if (context == nullptr) {
244 CM_LOG_E("could not create context");
245 return nullptr;
246 }
247
248 context->store = store;
249
250 napi_value result = GetAppCertListParseParams(env, info, context);
251 if (result == nullptr) {
252 CM_LOG_E("could not parse params");
253 DeleteGetAppCertListAsyncContext(env, context);
254 return nullptr;
255 }
256 result = GetCallingAppCertListAsyncWork(env, context);
257 if (result == nullptr) {
258 CM_LOG_E("could not start async work");
259 DeleteGetAppCertListAsyncContext(env, context);
260 return nullptr;
261 }
262 return result;
263 }
264 } // namespace CertManagerNapi
265