• 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_user_trusted_cert.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_USER_INSTALL_ARGS_CNT = 2;
29 constexpr int CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT = 2;
30 constexpr int CM_NAPI_USER_UNINSTALL_ARGS_CNT = 2;
31 constexpr int CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT = 1;
32 constexpr int CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT = 1;
33 constexpr int CM_NAPI_CALLBACK_ARG_CNT = 1;
34 constexpr uint32_t OUT_AUTH_URI_SIZE = 1000;
35 } // namespace
36 
37 struct UserCertAsyncContextT {
38     napi_async_work asyncWork = nullptr;
39     napi_deferred deferred = nullptr;
40     napi_ref callback = nullptr;
41 
42     int32_t errCode = 0;
43 
44     struct CmBlob *userCert = nullptr;
45     struct CmBlob *certAlias = nullptr;
46     struct CmBlob *certUri = nullptr;
47 };
48 using UserCertAsyncContext = UserCertAsyncContextT *;
49 
InitUserCertAsyncContext(void)50 static UserCertAsyncContext InitUserCertAsyncContext(void)
51 {
52     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(CmMalloc(sizeof(UserCertAsyncContextT)));
53     if (context != nullptr) {
54         (void)memset_s(context, sizeof(UserCertAsyncContextT), 0, sizeof(UserCertAsyncContextT));
55     }
56     return context;
57 }
58 
FreeUserCertAsyncContext(napi_env env,UserCertAsyncContext & context)59 static void FreeUserCertAsyncContext(napi_env env, UserCertAsyncContext &context)
60 {
61     if (context == nullptr) {
62         return;
63     }
64 
65     DeleteNapiContext(env, context->asyncWork, context->callback);
66     FreeCmBlob(context->userCert);
67     FreeCmBlob(context->certAlias);
68     FreeCmBlob(context->certUri);
69     CM_FREE_PTR(context);
70 }
71 
GetUserCertData(napi_env env,napi_value object,CmBlob ** outCert)72 static int32_t GetUserCertData(napi_env env, napi_value object, CmBlob **outCert)
73 {
74     CmBlob *userCert = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
75     if (userCert == nullptr) {
76         CM_LOG_E("could not alloc userCert blob memory");
77         return CMR_ERROR_MALLOC_FAIL;
78     }
79     (void)memset_s(userCert, sizeof(CmBlob), 0, sizeof(CmBlob));
80 
81     napi_value result = GetUint8Array(env, object, *(userCert));
82     if (result == nullptr) {
83         CM_LOG_E("could not get userCert data");
84         CM_FREE_PTR(userCert);
85         return CMR_ERROR_INVALID_ARGUMENT;
86     }
87 
88     *outCert = userCert;
89     return CM_SUCCESS;
90 }
91 
GetCertAliasData(napi_env env,napi_value object,UserCertAsyncContext context)92 static int32_t GetCertAliasData(napi_env env, napi_value object, UserCertAsyncContext context)
93 {
94     napi_value result = ParseCertAlias(env, object, context->certAlias);
95     if (result == nullptr) {
96         CM_LOG_E("could not get certAlias data");
97         return CMR_ERROR_INVALID_OPERATION;
98     }
99 
100     return CM_SUCCESS;
101 }
102 
ParseCertInfo(napi_env env,napi_value object,UserCertAsyncContext context)103 static napi_value ParseCertInfo(napi_env env, napi_value object, UserCertAsyncContext context)
104 {
105     napi_valuetype type = napi_undefined;
106     NAPI_CALL(env, napi_typeof(env, object, &type));
107     if (type != napi_object) {
108         CM_LOG_E("type of param CertBlob is not object");
109         return nullptr;
110     }
111 
112     napi_value userCertValue = nullptr;
113     napi_status status = napi_get_named_property(env, object, "inData", &userCertValue);
114     if (status != napi_ok || userCertValue == nullptr) {
115         CM_LOG_E("get inData failed");
116         return nullptr;
117     }
118 
119     napi_value certAliasValue = nullptr;
120     status = napi_get_named_property(env, object, "alias", &certAliasValue);
121     if (status != napi_ok || certAliasValue == nullptr) {
122         CM_LOG_E("get cert alias failed");
123         return nullptr;
124     }
125 
126     int32_t ret = GetUserCertData(env, userCertValue, &context->userCert);
127     if (ret != CM_SUCCESS) {
128         return nullptr;
129     }
130 
131     ret = GetCertAliasData(env, certAliasValue, context);
132     if (ret != CM_SUCCESS) {
133         return nullptr;
134     }
135 
136     return GetInt32(env, 0);
137 }
138 
ParseInstallUserCertParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)139 static napi_value ParseInstallUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
140 {
141     size_t argc = CM_NAPI_USER_INSTALL_ARGS_CNT;
142     napi_value argv[CM_NAPI_USER_INSTALL_ARGS_CNT] = { nullptr };
143     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
144 
145     if ((argc != CM_NAPI_USER_INSTALL_ARGS_CNT) &&
146         (argc != (CM_NAPI_USER_INSTALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) {
147         ThrowError(env, PARAM_ERROR, "arguments count invalid when installing user cert");
148         CM_LOG_E("arguments count is not expected when installing user cert");
149         return nullptr;
150     }
151 
152     size_t index = 0;
153     napi_value result = ParseCertInfo(env, argv[index], context);
154     if (result == nullptr) {
155         ThrowError(env, PARAM_ERROR, "get context type error");
156         CM_LOG_E("get CertBlob failed when installing user cert");
157         return nullptr;
158     }
159 
160     index++;
161     if (index < argc) {
162         int32_t ret = GetCallback(env, argv[index], context->callback);
163         if (ret != CM_SUCCESS) {
164             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
165             CM_LOG_E("get callback function failed when install user cert");
166             return nullptr;
167         }
168     }
169 
170     return GetInt32(env, 0);
171 }
172 
ParseUninstallUserCertParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)173 static napi_value ParseUninstallUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
174 {
175     size_t argc = CM_NAPI_USER_UNINSTALL_ARGS_CNT;
176     napi_value argv[CM_NAPI_USER_UNINSTALL_ARGS_CNT] = { nullptr };
177     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
178 
179     if ((argc != CM_NAPI_USER_UNINSTALL_ARGS_CNT) &&
180         (argc != (CM_NAPI_USER_UNINSTALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) {
181         ThrowError(env, PARAM_ERROR, "arguments count invalid when uninstalling user cert");
182         CM_LOG_E("arguments count is not expected when uninstalling user cert");
183         return nullptr;
184     }
185 
186     size_t index = 0;
187     napi_value result = ParseString(env, argv[index], context->certUri);
188     if (result == nullptr) {
189         ThrowError(env, PARAM_ERROR, "get certUri type error");
190         CM_LOG_E("get CertBlob failed when uninstalling user cert");
191         return nullptr;
192     }
193 
194     index++;
195     if (index < argc) {
196         int32_t ret = GetCallback(env, argv[index], context->callback);
197         if (ret != CM_SUCCESS) {
198             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
199             CM_LOG_E("get callback function failed when uninstalling user cert");
200             return nullptr;
201         }
202     }
203 
204     return GetInt32(env, 0);
205 }
206 
ParseUninstallUserCertSyncParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)207 static int32_t ParseUninstallUserCertSyncParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
208 {
209     size_t argc = CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT;
210     napi_value argv[CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT] = { nullptr };
211     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
212 
213     if (argc != CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT) {
214         CM_LOG_E("arguments count is not expected when uninstalling user cert sync");
215         return CMR_ERROR_INVALID_ARGUMENT;
216     }
217 
218     napi_value result = ParseString(env, argv[0], context->certUri);
219     if (result == nullptr) {
220         CM_LOG_E("get certUri failed when uninstalling user cert sync");
221         return CMR_ERROR_INVALID_ARGUMENT;
222     }
223 
224     return CM_SUCCESS;
225 }
226 
ParseInstallUserCertSyncParams(napi_env env,napi_callback_info info,CmBlob ** userCert,CmCertScope & installScope)227 static int32_t ParseInstallUserCertSyncParams(napi_env env, napi_callback_info info, CmBlob **userCert,
228     CmCertScope &installScope)
229 {
230     size_t argc = CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT;
231     napi_value argv[CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT] = { nullptr };
232     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
233 
234     if (argc != CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT) {
235         CM_LOG_E("arguments count is not expected when installing user cert sync");
236         return CMR_ERROR_INVALID_ARGUMENT;
237     }
238 
239     size_t index = 0;
240     int32_t ret = GetUserCertData(env, argv[index], userCert);
241     if (ret != CM_SUCCESS) {
242         CM_LOG_E("could not get userCert");
243         return CMR_ERROR_INVALID_ARGUMENT;
244     }
245 
246     index++;
247     uint32_t scope = CM_ALL_USER;
248     napi_value result = ParseUint32(env, argv[index], scope);
249     if (result == nullptr) {
250         CM_LOG_E("could not get install scope");
251         return CMR_ERROR_INVALID_ARGUMENT;
252     }
253     installScope = static_cast<CmCertScope>(scope);
254     return CM_SUCCESS;
255 }
256 
ParseUninstallAllUserCertParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)257 static napi_value ParseUninstallAllUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
258 {
259     size_t argc = CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT;
260     napi_value argv[CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT] = { nullptr };
261     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
262 
263     if ((argc != CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT) &&
264         (argc != (CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) {
265         ThrowError(env, PARAM_ERROR, "arguments count invalid when uninstalling all user cert");
266         CM_LOG_E("arguments count is not expected when uninstalling all user cert");
267         return nullptr;
268     }
269 
270     size_t index = 0;
271     if (index < argc) {
272         int32_t ret = GetCallback(env, argv[index], context->callback);
273         if (ret != CM_SUCCESS) {
274             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
275             CM_LOG_E("get callback function failed when uninstalling all user cert");
276             return nullptr;
277         }
278     }
279 
280     return GetInt32(env, 0);
281 }
282 
InstallUserCertExecute(napi_env env,void * data)283 static void InstallUserCertExecute(napi_env env, void *data)
284 {
285     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
286     context->certUri = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
287     if (context->certUri == nullptr) {
288         CM_LOG_E("malloc certUri failed");
289         context->errCode = CMR_ERROR_MALLOC_FAIL;
290         return;
291     }
292     (void)memset_s(context->certUri, sizeof(CmBlob), 0, sizeof(CmBlob));
293 
294     context->certUri->data = static_cast<uint8_t *>(CmMalloc(OUT_AUTH_URI_SIZE));
295     if (context->certUri->data == nullptr) {
296         CM_LOG_E("malloc certUri.data failed");
297         context->errCode = CMR_ERROR_MALLOC_FAIL;
298         return;
299     }
300     (void)memset_s(context->certUri->data, OUT_AUTH_URI_SIZE, 0, OUT_AUTH_URI_SIZE);
301     context->certUri->size = OUT_AUTH_URI_SIZE;
302 
303     context->errCode = CmInstallUserTrustedCert(context->userCert, context->certAlias, context->certUri);
304 }
305 
ConvertResultCertUri(napi_env env,const CmBlob * certUri)306 static napi_value ConvertResultCertUri(napi_env env, const CmBlob *certUri)
307 {
308     napi_value result = nullptr;
309     NAPI_CALL(env, napi_create_object(env, &result));
310 
311     napi_value certUriNapi = nullptr;
312     NAPI_CALL(env, napi_create_string_latin1(env, reinterpret_cast<const char *>(certUri->data),
313         NAPI_AUTO_LENGTH, &certUriNapi));
314     NAPI_CALL(env, napi_set_named_property(env, result, "uri", certUriNapi));
315 
316     return result;
317 }
318 
InstallUserCertComplete(napi_env env,napi_status status,void * data)319 static void InstallUserCertComplete(napi_env env, napi_status status, void *data)
320 {
321     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
322     napi_value result[RESULT_NUMBER] = { nullptr };
323     if (context->errCode == CM_SUCCESS) {
324         napi_create_uint32(env, 0, &result[0]);
325         result[1] = ConvertResultCertUri(env, context->certUri);
326     } else {
327         result[0] = GenerateBusinessError(env, context->errCode);
328         napi_get_undefined(env, &result[1]);
329     }
330 
331     if (context->deferred != nullptr) {
332         GeneratePromise(env, context->deferred, context->errCode, result, CM_ARRAY_SIZE(result));
333     } else {
334         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->errCode);
335     }
336     FreeUserCertAsyncContext(env, context);
337 }
338 
InstallUserCertAsyncWork(napi_env env,UserCertAsyncContext context)339 static napi_value InstallUserCertAsyncWork(napi_env env, UserCertAsyncContext context)
340 {
341     napi_value promise = nullptr;
342     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
343 
344     napi_value resourceName = nullptr;
345     NAPI_CALL(env, napi_create_string_latin1(env, "installUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
346 
347     NAPI_CALL(env, napi_create_async_work(
348         env, nullptr, resourceName,
349         InstallUserCertExecute,
350         InstallUserCertComplete,
351         static_cast<void *>(context),
352         &context->asyncWork));
353 
354     napi_status status = napi_queue_async_work(env, context->asyncWork);
355     if (status != napi_ok) {
356         ThrowError(env, PARAM_ERROR, "queue async work error");
357         CM_LOG_E("queue async work failed when installing user cert");
358         return nullptr;
359     }
360     return promise;
361 }
362 
UninstallUserCertExecute(napi_env env,void * data)363 static void UninstallUserCertExecute(napi_env env, void *data)
364 {
365     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
366     context->errCode = CmUninstallUserTrustedCert(context->certUri);
367 }
368 
UninstallComplete(napi_env env,napi_status status,void * data)369 static void UninstallComplete(napi_env env, napi_status status, void *data)
370 {
371     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
372     napi_value result[RESULT_NUMBER] = { nullptr };
373     if (context->errCode == CM_SUCCESS) {
374         napi_create_uint32(env, 0, &result[0]);
375         napi_get_boolean(env, true, &result[1]);
376     } else {
377         result[0] = GenerateBusinessError(env, context->errCode);
378         napi_get_undefined(env, &result[1]);
379     }
380 
381     if (context->deferred != nullptr) {
382         GeneratePromise(env, context->deferred, context->errCode, result, CM_ARRAY_SIZE(result));
383     } else {
384         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->errCode);
385     }
386     FreeUserCertAsyncContext(env, context);
387 }
388 
UninstallUserCertAsyncWork(napi_env env,UserCertAsyncContext context)389 static napi_value UninstallUserCertAsyncWork(napi_env env, UserCertAsyncContext context)
390 {
391     napi_value promise = nullptr;
392     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
393 
394     napi_value resourceName = nullptr;
395     NAPI_CALL(env, napi_create_string_latin1(env, "uninstallUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
396 
397     NAPI_CALL(env, napi_create_async_work(
398         env, nullptr, resourceName,
399         UninstallUserCertExecute,
400         UninstallComplete,
401         static_cast<void *>(context),
402         &context->asyncWork));
403 
404     napi_status status = napi_queue_async_work(env, context->asyncWork);
405     if (status != napi_ok) {
406         ThrowError(env, PARAM_ERROR, "queue async work error");
407         CM_LOG_E("queue async work failed when uninstalling user cert");
408         return nullptr;
409     }
410     return promise;
411 }
412 
UninstallAllUserCertExecute(napi_env env,void * data)413 static void UninstallAllUserCertExecute(napi_env env, void *data)
414 {
415     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
416     context->errCode = CmUninstallAllUserTrustedCert();
417 }
418 
UninstallAllUserCertAsyncWork(napi_env env,UserCertAsyncContext context)419 static napi_value UninstallAllUserCertAsyncWork(napi_env env, UserCertAsyncContext context)
420 {
421     napi_value promise = nullptr;
422     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
423 
424     napi_value resourceName = nullptr;
425     NAPI_CALL(env, napi_create_string_latin1(env, "uninstallAllUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
426 
427     NAPI_CALL(env, napi_create_async_work(
428         env, nullptr, resourceName,
429         UninstallAllUserCertExecute,
430         UninstallComplete,
431         static_cast<void *>(context),
432         &context->asyncWork));
433 
434     napi_status status = napi_queue_async_work(env, context->asyncWork);
435     if (status != napi_ok) {
436         ThrowError(env, PARAM_ERROR, "queue async work error");
437         CM_LOG_E("queue async work failed uninstall all user cert");
438         return nullptr;
439     }
440     return promise;
441 }
442 
InstallUserCertSyncExecute(CmBlob * userCert,const CmCertScope scope,CmBlob * certUri)443 static int32_t InstallUserCertSyncExecute(CmBlob *userCert, const CmCertScope scope, CmBlob *certUri)
444 {
445     int32_t ret;
446     // alias is empty string
447     uint8_t alias[1] = { 0 };
448     CmBlob certAlias = { .size = sizeof(alias), .data = alias };
449 
450     uint32_t userId = 0;
451     if (scope == CM_CURRENT_USER) {
452         userId = INIT_INVALID_VALUE;
453     } else if (scope == CM_GLOBAL_USER) {
454         userId = 0;
455     } else {
456         CM_LOG_E("invalid certificate scope");
457         return CMR_ERROR_INVALID_ARGUMENT;
458     }
459 
460     ret = CmInstallUserCACert(userCert, &certAlias, userId, true, certUri);
461     if (ret != CM_SUCCESS) {
462         CM_LOG_E("install user cert sync, init certUri failed");
463         return ret;
464     }
465     return ret;
466 }
467 
CMNapiInstallUserTrustedCert(napi_env env,napi_callback_info info)468 napi_value CMNapiInstallUserTrustedCert(napi_env env, napi_callback_info info)
469 {
470     UserCertAsyncContext context = InitUserCertAsyncContext();
471     if (context == nullptr) {
472         CM_LOG_E("init install user cert context failed");
473         return nullptr;
474     }
475 
476     napi_value result = ParseInstallUserCertParams(env, info, context);
477     if (result == nullptr) {
478         CM_LOG_E("parse install user cert params failed");
479         FreeUserCertAsyncContext(env, context);
480         return nullptr;
481     }
482 
483     result = InstallUserCertAsyncWork(env, context);
484     if (result == nullptr) {
485         CM_LOG_E("start install user cert async work failed");
486         FreeUserCertAsyncContext(env, context);
487         return nullptr;
488     }
489 
490     return result;
491 }
492 
CMNapiInstallUserTrustedCertSync(napi_env env,napi_callback_info info)493 napi_value CMNapiInstallUserTrustedCertSync(napi_env env, napi_callback_info info)
494 {
495     CmBlob *userCert = nullptr;
496     CmCertScope installScope;
497     uint8_t uri[OUT_AUTH_URI_SIZE] = { 0 };
498     CmBlob certUri = { sizeof(uri), uri };
499 
500     int32_t ret = CM_SUCCESS;
501     do {
502         ret = ParseInstallUserCertSyncParams(env, info, &userCert, installScope);
503         if (ret != CM_SUCCESS) {
504             CM_LOG_E("parse install user cert sync params failed");
505             break;
506         }
507 
508         ret = InstallUserCertSyncExecute(userCert, installScope, &certUri);
509         if (ret != CM_SUCCESS) {
510             CM_LOG_E("install user cert sync execute failed");
511             break;
512         }
513     } while (0);
514 
515     if (ret != CM_SUCCESS) {
516         CM_LOG_E("install user cert sync failed, ret = %d", ret);
517         napi_throw(env, GenerateBusinessError(env, ret));
518         return nullptr;
519     }
520     napi_value result = ConvertResultCertUri(env, &certUri);
521     FreeCmBlob(userCert);
522     return result;
523 }
524 
CMNapiUninstallUserTrustedCert(napi_env env,napi_callback_info info)525 napi_value CMNapiUninstallUserTrustedCert(napi_env env, napi_callback_info info)
526 {
527     UserCertAsyncContext context = InitUserCertAsyncContext();
528     if (context == nullptr) {
529         CM_LOG_E("init uninstall user cert context failed");
530         return nullptr;
531     }
532 
533     napi_value result = ParseUninstallUserCertParams(env, info, context);
534     if (result == nullptr) {
535         CM_LOG_E("parse uninstall user cert params failed");
536         FreeUserCertAsyncContext(env, context);
537         return nullptr;
538     }
539 
540     result = UninstallUserCertAsyncWork(env, context);
541     if (result == nullptr) {
542         CM_LOG_E("start uninstall user cert async work failed");
543         FreeUserCertAsyncContext(env, context);
544         return nullptr;
545     }
546 
547     return result;
548 }
549 
CMNapiUninstallAllUserTrustedCert(napi_env env,napi_callback_info info)550 napi_value CMNapiUninstallAllUserTrustedCert(napi_env env, napi_callback_info info)
551 {
552     UserCertAsyncContext context = InitUserCertAsyncContext();
553     if (context == nullptr) {
554         CM_LOG_E("init uninstall all user cert context failed");
555         return nullptr;
556     }
557 
558     napi_value result = ParseUninstallAllUserCertParams(env, info, context);
559     if (result == nullptr) {
560         CM_LOG_E("parse uninstall all user cert params failed");
561         FreeUserCertAsyncContext(env, context);
562         return nullptr;
563     }
564 
565     result = UninstallAllUserCertAsyncWork(env, context);
566     if (result == nullptr) {
567         CM_LOG_E("start uninstall all user cert async work failed");
568         FreeUserCertAsyncContext(env, context);
569         return nullptr;
570     }
571 
572     return result;
573 }
574 
CMNapiUninstallUserCertSync(napi_env env,napi_callback_info info)575 napi_value CMNapiUninstallUserCertSync(napi_env env, napi_callback_info info)
576 {
577     UserCertAsyncContext context = InitUserCertAsyncContext();
578     int32_t ret;
579     do {
580         if (context == nullptr) {
581             ret = CMR_ERROR_NULL_POINTER;
582             CM_LOG_E("init uninstall user cert context failed");
583             break;
584         }
585 
586         ret = ParseUninstallUserCertSyncParams(env, info, context);
587         if (ret != CM_SUCCESS) {
588             CM_LOG_E("parse uninstall user cert params failed");
589             break;
590         }
591 
592         ret = CmUninstallUserTrustedCert(context->certUri);
593         if (ret != CM_SUCCESS) {
594             CM_LOG_E("start uninstall user cert sync work failed");
595             break;
596         }
597     } while (0);
598 
599     if (ret != CM_SUCCESS) {
600         CM_LOG_E("uninstall user cert sync failed, ret = %d", ret);
601         napi_throw(env, GenerateBusinessError(env, ret));
602     }
603 
604     FreeUserCertAsyncContext(env, context);
605     return nullptr;
606 }
607 }  // namespace CMNapi
608 
609