1 /*
2 * Copyright (c) 2022-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 "cm_napi_get_system_cert_list.h"
17
18 #include "securec.h"
19
20 #include "cert_manager_api.h"
21 #include "cm_log.h"
22 #include "cm_mem.h"
23 #include "cm_type.h"
24 #include "cm_napi_common.h"
25
26 namespace CMNapi {
27 namespace {
28 constexpr int CM_NAPI_GET_CERT_LIST_MIN_ARGS = 0;
29 constexpr int CM_NAPI_GET_CERT_LIST_MAX_ARGS = 1;
30 } // namespace
31
32 struct GetCertListAsyncContextT {
33 napi_async_work asyncWork = nullptr;
34 napi_deferred deferred = nullptr;
35 napi_ref callback = nullptr;
36
37 int32_t result = 0;
38 uint32_t store = 0;
39 struct CertList *certificateList = nullptr;
40 };
41 using GetCertListAsyncContext = GetCertListAsyncContextT *;
42
CreateGetCertListAsyncContext()43 static GetCertListAsyncContext CreateGetCertListAsyncContext()
44 {
45 GetCertListAsyncContext context =
46 static_cast<GetCertListAsyncContext>(CmMalloc(sizeof(GetCertListAsyncContextT)));
47 if (context != nullptr) {
48 (void)memset_s(
49 context, sizeof(GetCertListAsyncContextT), 0, sizeof(GetCertListAsyncContextT));
50 }
51 return context;
52 }
53
DeleteGetCertListAsyncContext(napi_env env,GetCertListAsyncContext & context)54 static void DeleteGetCertListAsyncContext(napi_env env, GetCertListAsyncContext &context)
55 {
56 if (context == nullptr) {
57 return;
58 }
59
60 DeleteNapiContext(env, context->asyncWork, context->callback);
61
62 if (context->certificateList != nullptr) {
63 FreeCertList(context->certificateList);
64 }
65
66 CmFree(context);
67 context = nullptr;
68 }
69
GetCertListParseParams(napi_env env,napi_callback_info info,GetCertListAsyncContext context,uint32_t store)70 static napi_value GetCertListParseParams( napi_env env, napi_callback_info info,
71 GetCertListAsyncContext context, uint32_t store)
72 {
73 size_t argc = CM_NAPI_GET_CERT_LIST_MAX_ARGS;
74 napi_value argv[CM_NAPI_GET_CERT_LIST_MAX_ARGS] = { nullptr };
75 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
76
77 if ((argc != CM_NAPI_GET_CERT_LIST_MIN_ARGS) &&
78 (argc != CM_NAPI_GET_CERT_LIST_MAX_ARGS)) {
79 ThrowError(env, PARAM_ERROR, "arguments count invalid when getting trusted certificate list");
80 CM_LOG_E("arguments count is not expected when getting trusted certificate list");
81 return nullptr;
82 }
83
84 size_t index = 0;
85 if (index < argc) {
86 int32_t ret = GetCallback(env, argv[index], context->callback);
87 if (ret != CM_SUCCESS) {
88 ThrowError(env, PARAM_ERROR, "Get callback type failed.");
89 CM_LOG_E("get callback function failed when get certlist function");
90 return nullptr;
91 }
92 }
93
94 context->store = store;
95 return GetInt32(env, 0);
96 }
97
GetCertListWriteResult(napi_env env,GetCertListAsyncContext context)98 static napi_value GetCertListWriteResult(napi_env env, GetCertListAsyncContext context)
99 {
100 napi_value result = nullptr;
101 NAPI_CALL(env, napi_create_object(env, &result));
102 napi_value certChains = GenerateCertAbstractArray(env,
103 context->certificateList->certAbstract, context->certificateList->certsCount);
104 if (certChains != nullptr) {
105 napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CERTLIST.c_str(), certChains);
106 } else {
107 NAPI_CALL(env, napi_get_undefined(env, &result));
108 }
109 return result;
110 }
111
GetCertListExecute(napi_env env,void * data)112 static void GetCertListExecute(napi_env env, void *data)
113 {
114 GetCertListAsyncContext context = static_cast<GetCertListAsyncContext>(data);
115
116 context->certificateList = static_cast<struct CertList *>(CmMalloc(sizeof(struct CertList)));
117 if (context->certificateList == nullptr) {
118 CM_LOG_E("malloc certificateList fail");
119 context->result = CMR_ERROR_MALLOC_FAIL;
120 return;
121 }
122 context->certificateList->certAbstract = nullptr;
123 context->certificateList->certsCount = 0;
124
125 uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract);
126 context->certificateList->certAbstract = static_cast<struct CertAbstract *>(CmMalloc(buffSize));
127 if (context->certificateList->certAbstract == nullptr) {
128 CM_LOG_E("malloc certificateList certAbstract fail");
129 context->result = CMR_ERROR_MALLOC_FAIL;
130 return;
131 }
132 (void)memset_s(context->certificateList->certAbstract, buffSize, 0, buffSize);
133 context->certificateList->certsCount = MAX_COUNT_CERTIFICATE;
134
135 if (context->store == CM_SYSTEM_TRUSTED_STORE) {
136 context->result = CmGetCertList(context->store, context->certificateList);
137 } else {
138 context->result = CmGetUserCertList(context->store, context->certificateList);
139 }
140 }
141
GetCertListComplete(napi_env env,napi_status status,void * data)142 static void GetCertListComplete(napi_env env, napi_status status, void *data)
143 {
144 GetCertListAsyncContext context = static_cast<GetCertListAsyncContext>(data);
145 napi_value result[RESULT_NUMBER] = { nullptr };
146 if (context->result == CM_SUCCESS) {
147 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
148 result[1] = GetCertListWriteResult(env, context);
149 } else {
150 result[0] = GenerateBusinessError(env, context->result);
151 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
152 }
153 if (context->deferred != nullptr) {
154 GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
155 } else {
156 GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
157 }
158 DeleteGetCertListAsyncContext(env, context);
159 }
160
GetCertListAsyncWork(napi_env env,GetCertListAsyncContext context)161 static napi_value GetCertListAsyncWork(napi_env env, GetCertListAsyncContext context)
162 {
163 napi_value promise = nullptr;
164 GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
165
166 napi_value resourceName = nullptr;
167 NAPI_CALL(env, napi_create_string_latin1(env, "GetCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
168
169 NAPI_CALL(env, napi_create_async_work(
170 env,
171 nullptr,
172 resourceName,
173 GetCertListExecute,
174 GetCertListComplete,
175 static_cast<void *>(context),
176 &context->asyncWork));
177
178 napi_status status = napi_queue_async_work(env, context->asyncWork);
179 if (status != napi_ok) {
180 GET_AND_THROW_LAST_ERROR((env));
181 DeleteGetCertListAsyncContext(env, context);
182 CM_LOG_E("could not queue async work");
183 return nullptr;
184 }
185 return promise;
186 }
187
CMNapiGetSystemCertList(napi_env env,napi_callback_info info)188 napi_value CMNapiGetSystemCertList(napi_env env, napi_callback_info info)
189 {
190 GetCertListAsyncContext context = CreateGetCertListAsyncContext();
191 if (context == nullptr) {
192 CM_LOG_E("could not create context");
193 return nullptr;
194 }
195 napi_value result = GetCertListParseParams(env, info, context, CM_SYSTEM_TRUSTED_STORE);
196 if (result == nullptr) {
197 CM_LOG_E("could not parse params");
198 DeleteGetCertListAsyncContext(env, context);
199 return nullptr;
200 }
201 result = GetCertListAsyncWork(env, context);
202 if (result == nullptr) {
203 CM_LOG_E("could not start async work");
204 DeleteGetCertListAsyncContext(env, context);
205 return nullptr;
206 }
207 return result;
208 }
209
CMNapiGetAllUserTrustedCertList(napi_env env,napi_callback_info info)210 napi_value CMNapiGetAllUserTrustedCertList(napi_env env, napi_callback_info info)
211 {
212 GetCertListAsyncContext context = CreateGetCertListAsyncContext();
213 if (context == nullptr) {
214 CM_LOG_E("create context failed");
215 return nullptr;
216 }
217
218 napi_value result = GetCertListParseParams(env, info, context, CM_USER_TRUSTED_STORE);
219 if (result == nullptr) {
220 CM_LOG_E("could not parse user trusted cert list params");
221 DeleteGetCertListAsyncContext(env, context);
222 return nullptr;
223 }
224
225 result = GetCertListAsyncWork(env, context);
226 if (result == nullptr) {
227 CM_LOG_E("get user trusted cert list async work failed");
228 DeleteGetCertListAsyncContext(env, context);
229 return nullptr;
230 }
231 return result;
232 }
233 } // namespace CertManagerNapi
234