• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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     enum CmCertScope scope = CM_ALL_USER;
40     struct CertList *certificateList = nullptr;
41 };
42 using GetCertListAsyncContext = GetCertListAsyncContextT *;
43 
CreateGetCertListAsyncContext()44 static GetCertListAsyncContext CreateGetCertListAsyncContext()
45 {
46     GetCertListAsyncContext context =
47         static_cast<GetCertListAsyncContext>(CmMalloc(sizeof(GetCertListAsyncContextT)));
48     if (context != nullptr) {
49         (void)memset_s(
50             context, sizeof(GetCertListAsyncContextT), 0, sizeof(GetCertListAsyncContextT));
51     }
52     return context;
53 }
54 
DeleteGetCertListAsyncContext(napi_env env,GetCertListAsyncContext & context)55 static void DeleteGetCertListAsyncContext(napi_env env, GetCertListAsyncContext &context)
56 {
57     if (context == nullptr) {
58         return;
59     }
60 
61     DeleteNapiContext(env, context->asyncWork, context->callback);
62 
63     if (context->certificateList != nullptr) {
64         FreeCertList(context->certificateList);
65     }
66 
67     CmFree(context);
68     context = nullptr;
69 }
70 
GetAndCheckScope(napi_env env,napi_value arg,enum CmCertScope & certScope)71 static int32_t GetAndCheckScope(napi_env env, napi_value arg, enum CmCertScope &certScope)
72 {
73     uint32_t scope = 0;
74     napi_value result = ParseUint32(env, arg, scope);
75     if (result == nullptr) {
76         CM_LOG_E("Failed to get scope value");
77         return CM_FAILURE;
78     }
79 
80     if (!IsValidCertScope(scope)) {
81         CM_LOG_E("scope[%u] is invalid", scope);
82         return CM_FAILURE;
83     }
84 
85     certScope = static_cast<enum CmCertScope>(scope);
86     return CM_SUCCESS;
87 }
88 
GetCertListParseParams(napi_env env,napi_callback_info info,GetCertListAsyncContext context,uint32_t store)89 static napi_value GetCertListParseParams(    napi_env env, napi_callback_info info,
90     GetCertListAsyncContext context, uint32_t store)
91 {
92     size_t argc = CM_NAPI_GET_CERT_LIST_MAX_ARGS;
93     napi_value argv[CM_NAPI_GET_CERT_LIST_MAX_ARGS] = { nullptr };
94     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
95 
96     /* get system ca list */
97     if (store == CM_SYSTEM_TRUSTED_STORE) {
98         if (argc != CM_NAPI_GET_CERT_LIST_MIN_ARGS) { /* no args when get system ca list */
99             ThrowError(env, PARAM_ERROR, "arguments count invalid when getting system trusted certificate list");
100             CM_LOG_E("arguments count is not expected when getting system trusted certificate list");
101             return nullptr;
102         }
103         context->store = store;
104         return GetInt32(env, 0);
105     }
106 
107     /* get user ca list */
108     if ((argc != CM_NAPI_GET_CERT_LIST_MIN_ARGS) &&
109         (argc != CM_NAPI_GET_CERT_LIST_MAX_ARGS)) {
110         ThrowError(env, PARAM_ERROR, "arguments count invalid when getting user trusted certificate list");
111         CM_LOG_E("arguments count is not expected when getting trusted certificate list");
112         return nullptr;
113     }
114 
115     if (argc == CM_NAPI_GET_CERT_LIST_MAX_ARGS) {
116         int32_t ret = GetAndCheckScope(env, argv[0], context->scope);
117         if (ret != CM_SUCCESS) {
118             ThrowError(env, PARAM_ERROR, "Failed to get scope");
119             CM_LOG_E("Failed to get scope when get certlist function");
120             return nullptr;
121         }
122     }
123 
124     context->store = store;
125     return GetInt32(env, 0);
126 }
127 
GetCertListWriteResult(napi_env env,GetCertListAsyncContext context)128 static napi_value GetCertListWriteResult(napi_env env, GetCertListAsyncContext context)
129 {
130     napi_value result = nullptr;
131     NAPI_CALL(env, napi_create_object(env, &result));
132     napi_value certChains = GenerateCertAbstractArray(env,
133         context->certificateList->certAbstract, context->certificateList->certsCount);
134     if (certChains != nullptr) {
135         napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CERTLIST.c_str(), certChains);
136     } else {
137         NAPI_CALL(env, napi_get_undefined(env, &result));
138     }
139     return result;
140 }
141 
GetCertListExecute(napi_env env,void * data)142 static void GetCertListExecute(napi_env env, void *data)
143 {
144     GetCertListAsyncContext context = static_cast<GetCertListAsyncContext>(data);
145 
146     context->certificateList = static_cast<struct CertList *>(CmMalloc(sizeof(struct CertList)));
147     if (context->certificateList == nullptr) {
148         CM_LOG_E("malloc certificateList fail");
149         context->result = CMR_ERROR_MALLOC_FAIL;
150         return;
151     }
152     context->certificateList->certAbstract = nullptr;
153     context->certificateList->certsCount = 0;
154 
155     uint32_t buffSize = MAX_COUNT_CERTIFICATE_ALL * sizeof(struct CertAbstract);
156     context->certificateList->certAbstract = static_cast<struct CertAbstract *>(CmMalloc(buffSize));
157     if (context->certificateList->certAbstract == nullptr) {
158         CM_LOG_E("malloc certificateList certAbstract fail");
159         context->result = CMR_ERROR_MALLOC_FAIL;
160         return;
161     }
162     (void)memset_s(context->certificateList->certAbstract, buffSize, 0, buffSize);
163     context->certificateList->certsCount = MAX_COUNT_CERTIFICATE_ALL;
164 
165     if (context->store == CM_SYSTEM_TRUSTED_STORE) {
166         context->result = CmGetCertList(context->store, context->certificateList);
167         return;
168     }
169 
170     if (context->scope == CM_CURRENT_USER || context->scope == CM_GLOBAL_USER) {
171         struct UserCAProperty prop = { INIT_INVALID_VALUE, context->scope };
172         context->result = CmGetUserCACertList(&prop, context->certificateList);
173     } else {
174         context->result = CmGetUserCertList(context->store, context->certificateList);
175     }
176 }
177 
GetCertListComplete(napi_env env,napi_status status,void * data)178 static void GetCertListComplete(napi_env env, napi_status status, void *data)
179 {
180     GetCertListAsyncContext context = static_cast<GetCertListAsyncContext>(data);
181     napi_value result[RESULT_NUMBER] = { nullptr };
182     if (context->result == CM_SUCCESS) {
183         NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
184         result[1] = GetCertListWriteResult(env, context);
185     } else {
186         result[0] = GenerateBusinessError(env, context->result);
187         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
188     }
189     if (context->deferred != nullptr) {
190         GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
191     } else {
192         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
193     }
194     DeleteGetCertListAsyncContext(env, context);
195 }
196 
GetCertListAsyncWork(napi_env env,GetCertListAsyncContext context)197 static napi_value GetCertListAsyncWork(napi_env env, GetCertListAsyncContext context)
198 {
199     napi_value promise = nullptr;
200     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
201 
202     napi_value resourceName = nullptr;
203     NAPI_CALL(env, napi_create_string_latin1(env, "GetCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
204 
205     NAPI_CALL(env, napi_create_async_work(
206         env,
207         nullptr,
208         resourceName,
209         GetCertListExecute,
210         GetCertListComplete,
211         static_cast<void *>(context),
212         &context->asyncWork));
213 
214     napi_status status = napi_queue_async_work(env, context->asyncWork);
215     if (status != napi_ok) {
216         GET_AND_THROW_LAST_ERROR((env));
217         DeleteGetCertListAsyncContext(env, context);
218         CM_LOG_E("could not queue async work");
219         return nullptr;
220     }
221     return promise;
222 }
223 
CMNapiGetSystemCertList(napi_env env,napi_callback_info info)224 napi_value CMNapiGetSystemCertList(napi_env env, napi_callback_info info)
225 {
226     CM_LOG_I("get system cert list enter");
227     GetCertListAsyncContext context = CreateGetCertListAsyncContext();
228     if (context == nullptr) {
229         CM_LOG_E("could not create context");
230         return nullptr;
231     }
232     napi_value result = GetCertListParseParams(env, info, context, CM_SYSTEM_TRUSTED_STORE);
233     if (result == nullptr) {
234         CM_LOG_E("could not parse params");
235         DeleteGetCertListAsyncContext(env, context);
236         return nullptr;
237     }
238     result = GetCertListAsyncWork(env, context);
239     if (result == nullptr) {
240         CM_LOG_E("could not start async work");
241         DeleteGetCertListAsyncContext(env, context);
242         return nullptr;
243     }
244     CM_LOG_I("get system cert list end");
245     return result;
246 }
247 
CMNapiGetAllUserTrustedCertList(napi_env env,napi_callback_info info)248 napi_value CMNapiGetAllUserTrustedCertList(napi_env env, napi_callback_info info)
249 {
250     CM_LOG_I("get all user cert list enter");
251 
252     GetCertListAsyncContext context = CreateGetCertListAsyncContext();
253     if (context == nullptr) {
254         CM_LOG_E("create context failed");
255         return nullptr;
256     }
257 
258     napi_value result = GetCertListParseParams(env, info, context, CM_USER_TRUSTED_STORE);
259     if (result == nullptr) {
260         CM_LOG_E("could not parse user trusted cert list params");
261         DeleteGetCertListAsyncContext(env, context);
262         return nullptr;
263     }
264 
265     result = GetCertListAsyncWork(env, context);
266     if (result == nullptr) {
267         CM_LOG_E("get user trusted cert list async work failed");
268         DeleteGetCertListAsyncContext(env, context);
269         return nullptr;
270     }
271 
272     CM_LOG_I("get all user cert list end");
273     return result;
274 }
275 }  // namespace CertManagerNapi
276