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_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_SYSTEM_CERT_LIST_MIN_ARGS = 1;
29 constexpr int CM_NAPI_GET_SYSTEM_CERT_LIST_MAX_ARGS = 2;
30 constexpr int CM_NAPI_GET_USER_CERT_LIST_MIN_ARGS = 0;
31 constexpr int CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS = 1;
32 } // namespace
33
34 struct GetCertListAsyncContextT {
35 napi_async_work asyncWork = nullptr;
36 napi_deferred deferred = nullptr;
37 napi_ref callback = nullptr;
38
39 int32_t result = 0;
40 struct CmContext *cmContext = nullptr;
41 uint32_t store = 0;
42 struct CertList *certificateList = nullptr;
43 };
44 using GetCertListAsyncContext = GetCertListAsyncContextT *;
45
CreateGetCertListAsyncContext()46 static GetCertListAsyncContext CreateGetCertListAsyncContext()
47 {
48 GetCertListAsyncContext context =
49 static_cast<GetCertListAsyncContext>(CmMalloc(sizeof(GetCertListAsyncContextT)));
50 if (context != nullptr) {
51 (void)memset_s(
52 context, sizeof(GetCertListAsyncContextT), 0, sizeof(GetCertListAsyncContextT));
53 }
54 return context;
55 }
56
DeleteGetCertListAsyncContext(napi_env env,GetCertListAsyncContext & context)57 static void DeleteGetCertListAsyncContext(napi_env env, GetCertListAsyncContext &context)
58 {
59 if (context == nullptr) {
60 return;
61 }
62
63 DeleteNapiContext(env, context->asyncWork, context->callback);
64
65 if (context->cmContext != nullptr) {
66 FreeCmContext(context->cmContext);
67 }
68
69 if (context->certificateList != nullptr) {
70 FreeCertList(context->certificateList);
71 }
72
73 CmFree(context);
74 context = nullptr;
75 }
76
GetCertListParseParams(napi_env env,napi_callback_info info,GetCertListAsyncContext context)77 static napi_value GetCertListParseParams(
78 napi_env env, napi_callback_info info, GetCertListAsyncContext context)
79 {
80 size_t argc = CM_NAPI_GET_SYSTEM_CERT_LIST_MAX_ARGS;
81 napi_value argv[CM_NAPI_GET_SYSTEM_CERT_LIST_MAX_ARGS] = { nullptr };
82 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
83
84 if (argc < CM_NAPI_GET_SYSTEM_CERT_LIST_MIN_ARGS) {
85 ThrowParamsError(env, PARAM_ERROR, "Missing parameter");
86 CM_LOG_E("Missing parameter");
87 return nullptr;
88 }
89
90 size_t index = 0;
91 napi_value result = ParseCmContext(env, argv[index], context->cmContext);
92 if (result == nullptr) {
93 ThrowParamsError(env, PARAM_ERROR, "get context type error");
94 CM_LOG_E("could not get cert manager context");
95 return nullptr;
96 }
97
98 index++;
99 if (index < argc) {
100 context->callback = GetCallback(env, argv[index]);
101 }
102
103 context->store = CM_SYSTEM_TRUSTED_STORE;
104 return GetInt32(env, 0);
105 }
106
GetUserTrustedCertListParseParams(napi_env env,napi_callback_info info,GetCertListAsyncContext context)107 static napi_value GetUserTrustedCertListParseParams(
108 napi_env env, napi_callback_info info, GetCertListAsyncContext context)
109 {
110 size_t argc = CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS;
111 napi_value argv[CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS] = { nullptr };
112 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
113
114 if ((argc != CM_NAPI_GET_USER_CERT_LIST_MIN_ARGS) &&
115 (argc != CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS)) {
116 ThrowParamsError(env, PARAM_ERROR, "arguments count invalid when getting user trusted certificate list");
117 CM_LOG_E("arguments count is not expected when getting user trusted certificate list");
118 return nullptr;
119 }
120
121 size_t index = 0;
122 if (index < argc) {
123 context->callback = GetCallback(env, argv[index]);
124 if (context->callback == nullptr) {
125 ThrowParamsError(env, PARAM_ERROR, "Get callback type error");
126 CM_LOG_E("get callback function failed when getting user trusted certificate list");
127 return nullptr;
128 }
129 }
130
131 context->store = CM_USER_TRUSTED_STORE;
132 return GetInt32(env, 0);
133 }
134
GetCertListWriteResult(napi_env env,GetCertListAsyncContext context)135 static napi_value GetCertListWriteResult(napi_env env, GetCertListAsyncContext context)
136 {
137 napi_value result = nullptr;
138 NAPI_CALL(env, napi_create_object(env, &result));
139 napi_value certChains = GenerateCertAbstractArray(env,
140 context->certificateList->certAbstract, context->certificateList->certsCount);
141 if (certChains != nullptr) {
142 napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CERTLIST.c_str(), certChains);
143 } else {
144 NAPI_CALL(env, napi_get_undefined(env, &result));
145 }
146 return result;
147 }
148
GetCertListExecute(napi_env env,void * data)149 static void GetCertListExecute(napi_env env, void *data)
150 {
151 GetCertListAsyncContext context = static_cast<GetCertListAsyncContext>(data);
152
153 context->certificateList = static_cast<struct CertList *>(CmMalloc(sizeof(struct CertList)));
154 if (context->certificateList == nullptr) {
155 CM_LOG_E("malloc certificateList fail");
156 context->result = CMR_ERROR_MALLOC_FAIL;
157 return;
158 }
159 context->certificateList->certAbstract = nullptr;
160 context->certificateList->certsCount = 0;
161
162 uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract);
163 context->certificateList->certAbstract = static_cast<struct CertAbstract *>(CmMalloc(buffSize));
164 if (context->certificateList->certAbstract == nullptr) {
165 CM_LOG_E("malloc certificateList certAbstract fail");
166 context->result = CMR_ERROR_MALLOC_FAIL;
167 return;
168 }
169 (void)memset_s(context->certificateList->certAbstract, buffSize, 0, buffSize);
170 context->certificateList->certsCount = MAX_COUNT_CERTIFICATE;
171
172 if (context->store == CM_SYSTEM_TRUSTED_STORE) {
173 context->result = CmGetCertList(context->store, context->certificateList);
174 } else {
175 context->result = CmGetUserCertList(context->store, context->certificateList);
176 }
177 }
178
GetCertListComplete(napi_env env,napi_status status,void * data)179 static void GetCertListComplete(napi_env env, napi_status status, void *data)
180 {
181 GetCertListAsyncContext context = static_cast<GetCertListAsyncContext>(data);
182 napi_value result[RESULT_NUMBER] = { nullptr };
183 if (context->result == CM_SUCCESS) {
184 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
185 result[1] = GetCertListWriteResult(env, context);
186 } else {
187 const char *errorMsg = "get system cert list error";
188 result[0] = GenerateBusinessError(env, context->result, errorMsg);
189 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
190 }
191 if (context->deferred != nullptr) {
192 GeneratePromise(env, context->deferred, context->result, result, sizeof(result));
193 } else {
194 GenerateCallback(env, context->callback, result, sizeof(result));
195 }
196 DeleteGetCertListAsyncContext(env, context);
197 }
198
GetCertListAsyncWork(napi_env env,GetCertListAsyncContext context)199 static napi_value GetCertListAsyncWork(napi_env env, GetCertListAsyncContext context)
200 {
201 napi_value promise = nullptr;
202 GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
203
204 napi_value resourceName = nullptr;
205 NAPI_CALL(env, napi_create_string_latin1(env, "GetCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
206
207 NAPI_CALL(env, napi_create_async_work(
208 env,
209 nullptr,
210 resourceName,
211 GetCertListExecute,
212 GetCertListComplete,
213 static_cast<void *>(context),
214 &context->asyncWork));
215
216 napi_status status = napi_queue_async_work(env, context->asyncWork);
217 if (status != napi_ok) {
218 GET_AND_THROW_LAST_ERROR((env));
219 DeleteGetCertListAsyncContext(env, context);
220 CM_LOG_E("could not queue async work");
221 return nullptr;
222 }
223 return promise;
224 }
225
CMNapiGetSystemCertList(napi_env env,napi_callback_info info)226 napi_value CMNapiGetSystemCertList(napi_env env, napi_callback_info info)
227 {
228 GetCertListAsyncContext context = CreateGetCertListAsyncContext();
229 if (context == nullptr) {
230 CM_LOG_E("could not create context");
231 return nullptr;
232 }
233 napi_value result = GetCertListParseParams(env, info, context);
234 if (result == nullptr) {
235 CM_LOG_E("could not parse params");
236 DeleteGetCertListAsyncContext(env, context);
237 return nullptr;
238 }
239 result = GetCertListAsyncWork(env, context);
240 if (result == nullptr) {
241 CM_LOG_E("could not start async work");
242 DeleteGetCertListAsyncContext(env, context);
243 return nullptr;
244 }
245 return result;
246 }
247
CMNapiGetUserTrustedCertList(napi_env env,napi_callback_info info)248 napi_value CMNapiGetUserTrustedCertList(napi_env env, napi_callback_info info)
249 {
250 GetCertListAsyncContext context = CreateGetCertListAsyncContext();
251 if (context == nullptr) {
252 CM_LOG_E("create context failed");
253 return nullptr;
254 }
255
256 napi_value result = GetUserTrustedCertListParseParams(env, info, context);
257 if (result == nullptr) {
258 CM_LOG_E("could not parse user trusted cert list params");
259 DeleteGetCertListAsyncContext(env, context);
260 return nullptr;
261 }
262
263 result = GetCertListAsyncWork(env, context);
264 if (result == nullptr) {
265 CM_LOG_E("get user trusted cert list async work failed");
266 DeleteGetCertListAsyncContext(env, context);
267 return nullptr;
268 }
269 return result;
270 }
271 } // namespace CertManagerNapi
272