• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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         ThrowParamsError(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             ThrowParamsError(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         const char *errorMsg = "get system cert list error";
151         result[0] = GenerateBusinessError(env, context->result, errorMsg);
152         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
153     }
154     if (context->deferred != nullptr) {
155         GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
156     } else {
157         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
158     }
159     DeleteGetCertListAsyncContext(env, context);
160 }
161 
GetCertListAsyncWork(napi_env env,GetCertListAsyncContext context)162 static napi_value GetCertListAsyncWork(napi_env env, GetCertListAsyncContext context)
163 {
164     napi_value promise = nullptr;
165     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
166 
167     napi_value resourceName = nullptr;
168     NAPI_CALL(env, napi_create_string_latin1(env, "GetCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
169 
170     NAPI_CALL(env, napi_create_async_work(
171         env,
172         nullptr,
173         resourceName,
174         GetCertListExecute,
175         GetCertListComplete,
176         static_cast<void *>(context),
177         &context->asyncWork));
178 
179     napi_status status = napi_queue_async_work(env, context->asyncWork);
180     if (status != napi_ok) {
181         GET_AND_THROW_LAST_ERROR((env));
182         DeleteGetCertListAsyncContext(env, context);
183         CM_LOG_E("could not queue async work");
184         return nullptr;
185     }
186     return promise;
187 }
188 
CMNapiGetSystemCertList(napi_env env,napi_callback_info info)189 napi_value CMNapiGetSystemCertList(napi_env env, napi_callback_info info)
190 {
191     GetCertListAsyncContext context = CreateGetCertListAsyncContext();
192     if (context == nullptr) {
193         CM_LOG_E("could not create context");
194         return nullptr;
195     }
196     napi_value result = GetCertListParseParams(env, info, context, CM_SYSTEM_TRUSTED_STORE);
197     if (result == nullptr) {
198         CM_LOG_E("could not parse params");
199         DeleteGetCertListAsyncContext(env, context);
200         return nullptr;
201     }
202     result = GetCertListAsyncWork(env, context);
203     if (result == nullptr) {
204         CM_LOG_E("could not start async work");
205         DeleteGetCertListAsyncContext(env, context);
206         return nullptr;
207     }
208     return result;
209 }
210 
CMNapiGetUserTrustedCertList(napi_env env,napi_callback_info info)211 napi_value CMNapiGetUserTrustedCertList(napi_env env, napi_callback_info info)
212 {
213     GetCertListAsyncContext context = CreateGetCertListAsyncContext();
214     if (context == nullptr) {
215         CM_LOG_E("create context failed");
216         return nullptr;
217     }
218 
219     napi_value result = GetCertListParseParams(env, info, context, CM_USER_TRUSTED_STORE);
220     if (result == nullptr) {
221         CM_LOG_E("could not parse user trusted cert list params");
222         DeleteGetCertListAsyncContext(env, context);
223         return nullptr;
224     }
225 
226     result = GetCertListAsyncWork(env, context);
227     if (result == nullptr) {
228         CM_LOG_E("get user trusted cert list async work failed");
229         DeleteGetCertListAsyncContext(env, context);
230         return nullptr;
231     }
232     return result;
233 }
234 }  // namespace CertManagerNapi
235