• 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_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     struct CertUriList *certUriList = nullptr;
48     CmCertFileFormat certFormat = PEM_DER;
49     CmCertScope certScope = CM_ALL_USER;
50 };
51 using UserCertAsyncContext = UserCertAsyncContextT *;
52 
InitUserCertAsyncContext(void)53 static UserCertAsyncContext InitUserCertAsyncContext(void)
54 {
55     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(CmMalloc(sizeof(UserCertAsyncContextT)));
56     if (context != nullptr) {
57         (void)memset_s(context, sizeof(UserCertAsyncContextT), 0, sizeof(UserCertAsyncContextT));
58     }
59     return context;
60 }
61 
FreeUserCertAsyncContext(napi_env env,UserCertAsyncContext & context)62 static void FreeUserCertAsyncContext(napi_env env, UserCertAsyncContext &context)
63 {
64     if (context == nullptr) {
65         return;
66     }
67 
68     DeleteNapiContext(env, context->asyncWork, context->callback);
69     FreeCmBlob(context->userCert);
70     FreeCmBlob(context->certAlias);
71     FreeCmBlob(context->certUri);
72     if (context->certUriList != nullptr) {
73         CM_FREE_PTR(context->certUriList->uriList);
74         CM_FREE_PTR(context->certUriList);
75     }
76     CM_FREE_PTR(context);
77 }
78 
GetUserCertData(napi_env env,napi_value object,CmBlob ** outCert)79 static int32_t GetUserCertData(napi_env env, napi_value object, CmBlob **outCert)
80 {
81     CmBlob *userCert = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
82     if (userCert == nullptr) {
83         CM_LOG_E("could not alloc userCert blob memory");
84         return CMR_ERROR_MALLOC_FAIL;
85     }
86     (void)memset_s(userCert, sizeof(CmBlob), 0, sizeof(CmBlob));
87 
88     napi_value result = GetUint8Array(env, object, *(userCert));
89     if (result == nullptr) {
90         CM_LOG_E("could not get userCert data");
91         CM_FREE_PTR(userCert);
92         return CMR_ERROR_INVALID_ARGUMENT;
93     }
94 
95     *outCert = userCert;
96     return CM_SUCCESS;
97 }
98 
GetCertAliasData(napi_env env,napi_value object,UserCertAsyncContext context)99 static int32_t GetCertAliasData(napi_env env, napi_value object, UserCertAsyncContext context)
100 {
101     napi_value result = ParseCertAlias(env, object, context->certAlias);
102     if (result == nullptr) {
103         CM_LOG_E("could not get certAlias data");
104         return CMR_ERROR_INVALID_OPERATION;
105     }
106 
107     return CM_SUCCESS;
108 }
109 
ParseCertFormat(napi_env env,napi_value object,UserCertAsyncContext context)110 static napi_value ParseCertFormat(napi_env env, napi_value object, UserCertAsyncContext context)
111 {
112     napi_value certFormatValue = nullptr;
113     napi_status status = napi_get_named_property(env, object, "certFormat", &certFormatValue);
114     if (status != napi_ok || certFormatValue == nullptr) {
115         return GetInt32(env, 0);
116     }
117     uint32_t certFormat = PEM_DER;
118     if (ParseUint32(env, certFormatValue, certFormat) == nullptr) {
119         CM_LOG_E("parse uint32 failed");
120         return nullptr;
121     }
122     // check support certFormat
123     switch (certFormat) {
124         case PEM_DER:
125         case P7B:
126             break;
127         default:
128             CM_LOG_E("invalid cert format: %u", certFormat);
129             return nullptr;
130     }
131     context->certFormat = static_cast<enum CmCertFileFormat>(certFormat);
132     return GetInt32(env, 0);
133 }
134 
ParseCertScope(napi_env env,napi_value object,UserCertAsyncContext context)135 static napi_value ParseCertScope(napi_env env, napi_value object, UserCertAsyncContext context)
136 {
137     napi_value certScopeValue = nullptr;
138     napi_status status = napi_get_named_property(env, object, "certScope", &certScopeValue);
139     if (status != napi_ok || certScopeValue == nullptr) {
140         return GetInt32(env, 0);
141     }
142     uint32_t certScope = PEM_DER;
143     if (ParseUint32(env, certScopeValue, certScope) == nullptr) {
144         CM_LOG_E("parse uint32 failed");
145         return nullptr;
146     }
147     // check support certScope
148     switch (certScope) {
149         case CM_ALL_USER:
150         case CM_GLOBAL_USER:
151         case CM_CURRENT_USER:
152             break;
153         default:
154             CM_LOG_E("invalid cert scope: %u", certScope);
155             return nullptr;
156     }
157     context->certScope = static_cast<enum CmCertScope>(certScope);
158     return GetInt32(env, 0);
159 }
160 
ParseCertInfo(napi_env env,napi_value object,UserCertAsyncContext context)161 static napi_value ParseCertInfo(napi_env env, napi_value object, UserCertAsyncContext context)
162 {
163     napi_valuetype type = napi_undefined;
164     NAPI_CALL(env, napi_typeof(env, object, &type));
165     if (type != napi_object) {
166         CM_LOG_E("type of param CertBlob is not object");
167         return nullptr;
168     }
169 
170     napi_value userCertValue = nullptr;
171     napi_status status = napi_get_named_property(env, object, "inData", &userCertValue);
172     if (status != napi_ok || userCertValue == nullptr) {
173         CM_LOG_E("get inData failed");
174         return nullptr;
175     }
176 
177     napi_value certAliasValue = nullptr;
178     status = napi_get_named_property(env, object, "alias", &certAliasValue);
179     if (status != napi_ok || certAliasValue == nullptr) {
180         CM_LOG_E("get cert alias failed");
181         return nullptr;
182     }
183 
184     // parse certFormat
185     if (ParseCertFormat(env, object, context) == nullptr) {
186         CM_LOG_E("parse cert file format failed");
187         return nullptr;
188     }
189 
190     // parse certScope
191     if (ParseCertScope(env, object, context) == nullptr) {
192         CM_LOG_E("parse cert scope failed");
193         return nullptr;
194     }
195 
196     int32_t ret = GetUserCertData(env, userCertValue, &context->userCert);
197     if (ret != CM_SUCCESS) {
198         CM_LOG_E("get user certData failed, ret = %d", ret);
199         return nullptr;
200     }
201 
202     ret = GetCertAliasData(env, certAliasValue, context);
203     if (ret != CM_SUCCESS) {
204         CM_LOG_E("get cert aliasData failed, ret = %d", ret);
205         return nullptr;
206     }
207 
208     return GetInt32(env, 0);
209 }
210 
ParseInstallUserCertParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)211 static napi_value ParseInstallUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
212 {
213     size_t argc = CM_NAPI_USER_INSTALL_ARGS_CNT;
214     napi_value argv[CM_NAPI_USER_INSTALL_ARGS_CNT] = { nullptr };
215     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
216 
217     if ((argc != CM_NAPI_USER_INSTALL_ARGS_CNT) &&
218         (argc != (CM_NAPI_USER_INSTALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) {
219         ThrowError(env, PARAM_ERROR, "arguments count invalid when installing user cert");
220         CM_LOG_E("arguments count is not expected when installing user cert");
221         return nullptr;
222     }
223 
224     size_t index = 0;
225     napi_value result = ParseCertInfo(env, argv[index], context);
226     if (result == nullptr) {
227         ThrowError(env, PARAM_ERROR, "get context type error");
228         CM_LOG_E("get CertBlob failed when installing user cert");
229         return nullptr;
230     }
231 
232     index++;
233     if (index < argc) {
234         int32_t ret = GetCallback(env, argv[index], context->callback);
235         if (ret != CM_SUCCESS) {
236             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
237             CM_LOG_E("get callback function failed when install user cert");
238             return nullptr;
239         }
240     }
241 
242     return GetInt32(env, 0);
243 }
244 
ParseUninstallUserCertParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)245 static napi_value ParseUninstallUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
246 {
247     size_t argc = CM_NAPI_USER_UNINSTALL_ARGS_CNT;
248     napi_value argv[CM_NAPI_USER_UNINSTALL_ARGS_CNT] = { nullptr };
249     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
250 
251     if ((argc != CM_NAPI_USER_UNINSTALL_ARGS_CNT) &&
252         (argc != (CM_NAPI_USER_UNINSTALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) {
253         ThrowError(env, PARAM_ERROR, "arguments count invalid when uninstalling user cert");
254         CM_LOG_E("arguments count is not expected when uninstalling user cert");
255         return nullptr;
256     }
257 
258     size_t index = 0;
259     napi_value result = ParseString(env, argv[index], context->certUri);
260     if (result == nullptr) {
261         ThrowError(env, PARAM_ERROR, "get certUri type error");
262         CM_LOG_E("get CertBlob failed when uninstalling user cert");
263         return nullptr;
264     }
265 
266     index++;
267     if (index < argc) {
268         int32_t ret = GetCallback(env, argv[index], context->callback);
269         if (ret != CM_SUCCESS) {
270             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
271             CM_LOG_E("get callback function failed when uninstalling user cert");
272             return nullptr;
273         }
274     }
275 
276     return GetInt32(env, 0);
277 }
278 
ParseUninstallUserCertSyncParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)279 static int32_t ParseUninstallUserCertSyncParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
280 {
281     size_t argc = CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT;
282     napi_value argv[CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT] = { nullptr };
283     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
284 
285     if (argc != CM_NAPI_USER_UNINSTALL_SYNC_ARGS_CNT) {
286         CM_LOG_E("arguments count is not expected when uninstalling user cert sync");
287         return CMR_ERROR_INVALID_ARGUMENT;
288     }
289 
290     napi_value result = ParseString(env, argv[0], context->certUri);
291     if (result == nullptr) {
292         CM_LOG_E("get certUri failed when uninstalling user cert sync");
293         return CMR_ERROR_INVALID_ARGUMENT;
294     }
295 
296     return CM_SUCCESS;
297 }
298 
ParseInstallUserCertSyncParams(napi_env env,napi_callback_info info,CmBlob ** userCert,CmCertScope & installScope)299 static int32_t ParseInstallUserCertSyncParams(napi_env env, napi_callback_info info, CmBlob **userCert,
300     CmCertScope &installScope)
301 {
302     size_t argc = CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT;
303     napi_value argv[CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT] = { nullptr };
304     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
305 
306     if (argc != CM_NAPI_USER_INSTALL_SYNC_ARGS_CNT) {
307         CM_LOG_E("arguments count is not expected when installing user cert sync");
308         return CMR_ERROR_INVALID_ARGUMENT;
309     }
310 
311     size_t index = 0;
312     int32_t ret = GetUserCertData(env, argv[index], userCert);
313     if (ret != CM_SUCCESS) {
314         CM_LOG_E("could not get userCert");
315         return CMR_ERROR_INVALID_ARGUMENT;
316     }
317 
318     index++;
319     uint32_t scope = CM_ALL_USER;
320     napi_value result = ParseUint32(env, argv[index], scope);
321     if (result == nullptr) {
322         CM_LOG_E("could not get install scope");
323         return CMR_ERROR_INVALID_ARGUMENT;
324     }
325     installScope = static_cast<CmCertScope>(scope);
326     return CM_SUCCESS;
327 }
328 
ParseUninstallAllUserCertParams(napi_env env,napi_callback_info info,UserCertAsyncContext context)329 static napi_value ParseUninstallAllUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context)
330 {
331     size_t argc = CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT;
332     napi_value argv[CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT] = { nullptr };
333     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
334 
335     if ((argc != CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT) &&
336         (argc != (CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) {
337         ThrowError(env, PARAM_ERROR, "arguments count invalid when uninstalling all user cert");
338         CM_LOG_E("arguments count is not expected when uninstalling all user cert");
339         return nullptr;
340     }
341 
342     size_t index = 0;
343     if (index < argc) {
344         int32_t ret = GetCallback(env, argv[index], context->callback);
345         if (ret != CM_SUCCESS) {
346             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
347             CM_LOG_E("get callback function failed when uninstalling all user cert");
348             return nullptr;
349         }
350     }
351 
352     return GetInt32(env, 0);
353 }
354 
InitCertUri(UserCertAsyncContext context)355 static int32_t InitCertUri(UserCertAsyncContext context)
356 {
357     context->certUri = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
358     if (context->certUri == nullptr) {
359         CM_LOG_E("malloc certUri failed");
360         context->errCode = CMR_ERROR_MALLOC_FAIL;
361         return CMR_ERROR_MALLOC_FAIL;
362     }
363     (void)memset_s(context->certUri, sizeof(CmBlob), 0, sizeof(CmBlob));
364 
365     context->certUri->data = static_cast<uint8_t *>(CmMalloc(OUT_AUTH_URI_SIZE));
366     if (context->certUri->data == nullptr) {
367         CM_LOG_E("malloc certUri.data failed");
368         context->errCode = CMR_ERROR_MALLOC_FAIL;
369         return CMR_ERROR_MALLOC_FAIL;
370     }
371     (void)memset_s(context->certUri->data, OUT_AUTH_URI_SIZE, 0, OUT_AUTH_URI_SIZE);
372     context->certUri->size = OUT_AUTH_URI_SIZE;
373     return CM_SUCCESS;
374 }
375 
InitCertUriList(UserCertAsyncContext context)376 static int32_t InitCertUriList(UserCertAsyncContext context)
377 {
378     CertUriList *certUriList = static_cast<CertUriList *>(CmMalloc(sizeof(CertUriList)));
379     if (certUriList == nullptr) {
380         CM_LOG_E("malloc certUriList failed");
381         context->errCode = CMR_ERROR_MALLOC_FAIL;
382         return CMR_ERROR_MALLOC_FAIL;
383     }
384     (void)memset_s(certUriList, sizeof(CertUriList), 0, sizeof(CertUriList));
385     certUriList->certCount = 0;
386     context->certUriList = certUriList;
387     return CM_SUCCESS;
388 }
389 
InstallUserCertExecute(napi_env env,void * data)390 static void InstallUserCertExecute(napi_env env, void *data)
391 {
392     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
393     if (context == nullptr) {
394         CM_LOG_E("context is null");
395         return;
396     }
397     int32_t ret = CM_SUCCESS;
398     uint32_t userId = 0;
399     if (context->certScope == CM_CURRENT_USER) {
400         userId = INIT_INVALID_VALUE;
401     } else if (context->certScope == CM_GLOBAL_USER) {
402         userId = 0;
403     } else {
404         CM_LOG_E("invalid certificate scope");
405         context->errCode = CMR_ERROR_INVALID_ARGUMENT;
406         return;
407     }
408 
409     if (context->certFormat == P7B) {
410         ret = InitCertUriList(context);
411         if (ret != CM_SUCCESS) {
412             CM_LOG_E("init cert uriList failed, ret = %d", ret);
413             context->errCode = ret;
414             return;
415         }
416         CmInstallCertInfo installCertInfo = {
417             .userCert = context->userCert,
418             .certAlias = context->certAlias,
419             .userId = userId
420         };
421         context->errCode = CmInstallUserTrustedP7BCert(&installCertInfo, true, context->certUriList);
422         return;
423     }
424 
425     ret = InitCertUri(context);
426     if (ret != CM_SUCCESS) {
427         CM_LOG_E("init certUri failed");
428         context->errCode = ret;
429         return;
430     }
431     context->errCode = CmInstallUserCACert(context->userCert, context->certAlias, userId, true, context->certUri);
432     return;
433 }
434 
ConvertResultCertUri(napi_env env,const CmBlob * certUri)435 static napi_value ConvertResultCertUri(napi_env env, const CmBlob *certUri)
436 {
437     napi_value result = nullptr;
438     NAPI_CALL(env, napi_create_object(env, &result));
439 
440     napi_value certUriNapi = nullptr;
441     NAPI_CALL(env, napi_create_string_latin1(env, reinterpret_cast<const char *>(certUri->data),
442         NAPI_AUTO_LENGTH, &certUriNapi));
443     NAPI_CALL(env, napi_set_named_property(env, result, "uri", certUriNapi));
444 
445     return result;
446 }
447 
ConvertResultCertUriList(napi_env env,const CertUriList * certUriList)448 static napi_value ConvertResultCertUriList(napi_env env, const CertUriList *certUriList)
449 {
450     if (certUriList == nullptr) {
451         CM_LOG_E("null pointer");
452         return nullptr;
453     }
454     napi_value result = nullptr;
455     NAPI_CALL(env, napi_create_object(env, &result));
456 
457     napi_value uriArray = nullptr;
458     NAPI_CALL(env, napi_create_array(env, &uriArray));
459 
460     for (uint32_t i = 0; i < certUriList->certCount; ++i) {
461         napi_value certUri = nullptr;
462         NAPI_CALL(env, napi_create_string_latin1(env, reinterpret_cast<const char *>(certUriList->uriList[i].data),
463             NAPI_AUTO_LENGTH, &certUri));
464         NAPI_CALL(env, napi_set_element(env, uriArray, i, certUri));
465     }
466 
467     NAPI_CALL(env, napi_set_named_property(env, result, "uriList", uriArray));
468     return result;
469 }
470 
ConvertInstallCertResult(napi_env env,const UserCertAsyncContext context)471 static napi_value ConvertInstallCertResult(napi_env env, const UserCertAsyncContext context)
472 {
473     if (context->certFormat == P7B) {
474         return ConvertResultCertUriList(env, context->certUriList);
475     }
476     return ConvertResultCertUri(env, context->certUri);
477 }
478 
InstallUserCertComplete(napi_env env,napi_status status,void * data)479 static void InstallUserCertComplete(napi_env env, napi_status status, void *data)
480 {
481     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
482     napi_value result[RESULT_NUMBER] = { nullptr };
483     if (context->errCode == CM_SUCCESS) {
484         napi_create_uint32(env, 0, &result[0]);
485         result[1] = ConvertInstallCertResult(env, context);
486     } else {
487         result[0] = GenerateBusinessError(env, context->errCode);
488         napi_get_undefined(env, &result[1]);
489     }
490 
491     if (context->deferred != nullptr) {
492         GeneratePromise(env, context->deferred, context->errCode, result, CM_ARRAY_SIZE(result));
493     } else {
494         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->errCode);
495     }
496     FreeUserCertAsyncContext(env, context);
497 }
498 
InstallUserCertAsyncWork(napi_env env,UserCertAsyncContext context)499 static napi_value InstallUserCertAsyncWork(napi_env env, UserCertAsyncContext context)
500 {
501     napi_value promise = nullptr;
502     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
503 
504     napi_value resourceName = nullptr;
505     NAPI_CALL(env, napi_create_string_latin1(env, "installUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
506 
507     NAPI_CALL(env, napi_create_async_work(
508         env, nullptr, resourceName,
509         InstallUserCertExecute,
510         InstallUserCertComplete,
511         static_cast<void *>(context),
512         &context->asyncWork));
513 
514     napi_status status = napi_queue_async_work(env, context->asyncWork);
515     if (status != napi_ok) {
516         ThrowError(env, PARAM_ERROR, "queue async work error");
517         CM_LOG_E("queue async work failed when installing user cert");
518         return nullptr;
519     }
520     return promise;
521 }
522 
UninstallUserCertExecute(napi_env env,void * data)523 static void UninstallUserCertExecute(napi_env env, void *data)
524 {
525     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
526     context->errCode = CmUninstallUserTrustedCert(context->certUri);
527 }
528 
UninstallComplete(napi_env env,napi_status status,void * data)529 static void UninstallComplete(napi_env env, napi_status status, void *data)
530 {
531     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
532     napi_value result[RESULT_NUMBER] = { nullptr };
533     if (context->errCode == CM_SUCCESS) {
534         napi_create_uint32(env, 0, &result[0]);
535         napi_get_boolean(env, true, &result[1]);
536     } else {
537         result[0] = GenerateBusinessError(env, context->errCode);
538         napi_get_undefined(env, &result[1]);
539     }
540 
541     if (context->deferred != nullptr) {
542         GeneratePromise(env, context->deferred, context->errCode, result, CM_ARRAY_SIZE(result));
543     } else {
544         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->errCode);
545     }
546     FreeUserCertAsyncContext(env, context);
547 }
548 
UninstallUserCertAsyncWork(napi_env env,UserCertAsyncContext context)549 static napi_value UninstallUserCertAsyncWork(napi_env env, UserCertAsyncContext context)
550 {
551     napi_value promise = nullptr;
552     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
553 
554     napi_value resourceName = nullptr;
555     NAPI_CALL(env, napi_create_string_latin1(env, "uninstallUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
556 
557     NAPI_CALL(env, napi_create_async_work(
558         env, nullptr, resourceName,
559         UninstallUserCertExecute,
560         UninstallComplete,
561         static_cast<void *>(context),
562         &context->asyncWork));
563 
564     napi_status status = napi_queue_async_work(env, context->asyncWork);
565     if (status != napi_ok) {
566         ThrowError(env, PARAM_ERROR, "queue async work error");
567         CM_LOG_E("queue async work failed when uninstalling user cert");
568         return nullptr;
569     }
570     return promise;
571 }
572 
UninstallAllUserCertExecute(napi_env env,void * data)573 static void UninstallAllUserCertExecute(napi_env env, void *data)
574 {
575     UserCertAsyncContext context = static_cast<UserCertAsyncContext>(data);
576     context->errCode = CmUninstallAllUserTrustedCert();
577 }
578 
UninstallAllUserCertAsyncWork(napi_env env,UserCertAsyncContext context)579 static napi_value UninstallAllUserCertAsyncWork(napi_env env, UserCertAsyncContext context)
580 {
581     napi_value promise = nullptr;
582     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
583 
584     napi_value resourceName = nullptr;
585     NAPI_CALL(env, napi_create_string_latin1(env, "uninstallAllUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
586 
587     NAPI_CALL(env, napi_create_async_work(
588         env, nullptr, resourceName,
589         UninstallAllUserCertExecute,
590         UninstallComplete,
591         static_cast<void *>(context),
592         &context->asyncWork));
593 
594     napi_status status = napi_queue_async_work(env, context->asyncWork);
595     if (status != napi_ok) {
596         ThrowError(env, PARAM_ERROR, "queue async work error");
597         CM_LOG_E("queue async work failed uninstall all user cert");
598         return nullptr;
599     }
600     return promise;
601 }
602 
InstallUserCertSyncExecute(CmBlob * userCert,const CmCertScope scope,CmBlob * certUri)603 static int32_t InstallUserCertSyncExecute(CmBlob *userCert, const CmCertScope scope, CmBlob *certUri)
604 {
605     int32_t ret;
606     // alias is empty string
607     uint8_t alias[1] = { 0 };
608     CmBlob certAlias = { .size = sizeof(alias), .data = alias };
609 
610     uint32_t userId = 0;
611     if (scope == CM_CURRENT_USER) {
612         userId = INIT_INVALID_VALUE;
613     } else if (scope == CM_GLOBAL_USER) {
614         userId = 0;
615     } else {
616         CM_LOG_E("invalid certificate scope");
617         return CMR_ERROR_INVALID_ARGUMENT;
618     }
619 
620     ret = CmInstallUserCACert(userCert, &certAlias, userId, true, certUri);
621     if (ret != CM_SUCCESS) {
622         CM_LOG_E("install user cert sync, init certUri failed");
623         return ret;
624     }
625     return ret;
626 }
627 
CMNapiInstallUserTrustedCert(napi_env env,napi_callback_info info)628 napi_value CMNapiInstallUserTrustedCert(napi_env env, napi_callback_info info)
629 {
630     CM_LOG_I("install user trusted cert enter");
631 
632     UserCertAsyncContext context = InitUserCertAsyncContext();
633     if (context == nullptr) {
634         CM_LOG_E("init install user cert context failed");
635         return nullptr;
636     }
637 
638     napi_value result = ParseInstallUserCertParams(env, info, context);
639     if (result == nullptr) {
640         CM_LOG_E("parse install user cert params failed");
641         FreeUserCertAsyncContext(env, context);
642         return nullptr;
643     }
644 
645     result = InstallUserCertAsyncWork(env, context);
646     if (result == nullptr) {
647         CM_LOG_E("start install user cert async work failed");
648         FreeUserCertAsyncContext(env, context);
649         return nullptr;
650     }
651 
652     CM_LOG_I("install user trusted cert end");
653     return result;
654 }
655 
CMNapiInstallUserTrustedCertSync(napi_env env,napi_callback_info info)656 napi_value CMNapiInstallUserTrustedCertSync(napi_env env, napi_callback_info info)
657 {
658     CM_LOG_I("install user trusted cert sync enter");
659 
660     CmBlob *userCert = nullptr;
661     CmCertScope installScope;
662     uint8_t uri[OUT_AUTH_URI_SIZE] = { 0 };
663     CmBlob certUri = { sizeof(uri), uri };
664 
665     int32_t ret = CM_SUCCESS;
666     do {
667         ret = ParseInstallUserCertSyncParams(env, info, &userCert, installScope);
668         if (ret != CM_SUCCESS) {
669             CM_LOG_E("parse install user cert sync params failed");
670             break;
671         }
672 
673         ret = InstallUserCertSyncExecute(userCert, installScope, &certUri);
674         if (ret != CM_SUCCESS) {
675             CM_LOG_E("install user cert sync execute failed");
676             break;
677         }
678     } while (0);
679 
680     FreeCmBlob(userCert);
681     if (ret != CM_SUCCESS) {
682         CM_LOG_E("install user cert sync failed, ret = %d", ret);
683         napi_throw(env, GenerateBusinessError(env, ret));
684         return nullptr;
685     }
686 
687     napi_value result = ConvertResultCertUri(env, &certUri);
688     CM_LOG_I("install user trusted cert sync end");
689     return result;
690 }
691 
CMNapiUninstallUserTrustedCert(napi_env env,napi_callback_info info)692 napi_value CMNapiUninstallUserTrustedCert(napi_env env, napi_callback_info info)
693 {
694     CM_LOG_I("uninstall user trusted cert enter");
695 
696     UserCertAsyncContext context = InitUserCertAsyncContext();
697     if (context == nullptr) {
698         CM_LOG_E("init uninstall user cert context failed");
699         return nullptr;
700     }
701 
702     napi_value result = ParseUninstallUserCertParams(env, info, context);
703     if (result == nullptr) {
704         CM_LOG_E("parse uninstall user cert params failed");
705         FreeUserCertAsyncContext(env, context);
706         return nullptr;
707     }
708 
709     result = UninstallUserCertAsyncWork(env, context);
710     if (result == nullptr) {
711         CM_LOG_E("start uninstall user cert async work failed");
712         FreeUserCertAsyncContext(env, context);
713         return nullptr;
714     }
715 
716     CM_LOG_I("uninstall user trusted cert end");
717     return result;
718 }
719 
CMNapiUninstallAllUserTrustedCert(napi_env env,napi_callback_info info)720 napi_value CMNapiUninstallAllUserTrustedCert(napi_env env, napi_callback_info info)
721 {
722     CM_LOG_I("uninstall all user trusted cert enter");
723 
724     UserCertAsyncContext context = InitUserCertAsyncContext();
725     if (context == nullptr) {
726         CM_LOG_E("init uninstall all user cert context failed");
727         return nullptr;
728     }
729 
730     napi_value result = ParseUninstallAllUserCertParams(env, info, context);
731     if (result == nullptr) {
732         CM_LOG_E("parse uninstall all user cert params failed");
733         FreeUserCertAsyncContext(env, context);
734         return nullptr;
735     }
736 
737     result = UninstallAllUserCertAsyncWork(env, context);
738     if (result == nullptr) {
739         CM_LOG_E("start uninstall all user cert async work failed");
740         FreeUserCertAsyncContext(env, context);
741         return nullptr;
742     }
743 
744     CM_LOG_I("uninstall all user trusted cert end");
745     return result;
746 }
747 
CMNapiUninstallUserCertSync(napi_env env,napi_callback_info info)748 napi_value CMNapiUninstallUserCertSync(napi_env env, napi_callback_info info)
749 {
750     CM_LOG_I("uninstall user trusted cert sync enter");
751 
752     UserCertAsyncContext context = InitUserCertAsyncContext();
753     int32_t ret;
754     do {
755         if (context == nullptr) {
756             ret = CMR_ERROR_NULL_POINTER;
757             CM_LOG_E("init uninstall user cert context failed");
758             break;
759         }
760 
761         ret = ParseUninstallUserCertSyncParams(env, info, context);
762         if (ret != CM_SUCCESS) {
763             CM_LOG_E("parse uninstall user cert params failed");
764             break;
765         }
766 
767         ret = CmUninstallUserTrustedCert(context->certUri);
768         if (ret != CM_SUCCESS) {
769             CM_LOG_E("start uninstall user cert sync work failed");
770             break;
771         }
772     } while (0);
773 
774     if (ret != CM_SUCCESS) {
775         CM_LOG_E("uninstall user cert sync failed, ret = %d", ret);
776         napi_throw(env, GenerateBusinessError(env, ret));
777     }
778 
779     FreeUserCertAsyncContext(env, context);
780 
781     CM_LOG_I("uninstall user trusted cert sync end");
782     return nullptr;
783 }
784 }  // namespace CMNapi
785 
786