• 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_app_cert_list.h"
17 #include "cm_napi_get_app_cert_list_common.h"
18 
19 #include "securec.h"
20 
21 #include "cert_manager_api.h"
22 #include "cm_log.h"
23 #include "cm_mem.h"
24 #include "cm_type.h"
25 #include "cm_napi_common.h"
26 
27 namespace CMNapi {
28 namespace {
29 constexpr int CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS = 0;
30 constexpr int CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS = 1;
31 }  // namespace
32 
CreateGetAppCertListAsyncContext()33 GetAppCertListAsyncContext CreateGetAppCertListAsyncContext()
34 {
35     GetAppCertListAsyncContext context =
36         static_cast<GetAppCertListAsyncContext>(CmMalloc(sizeof(GetAppCertListAsyncContextT)));
37     if (context != nullptr) {
38         (void)memset_s(context, sizeof(GetAppCertListAsyncContextT), 0, sizeof(GetAppCertListAsyncContextT));
39     }
40     return context;
41 }
42 
DeleteGetAppCertListAsyncContext(napi_env env,GetAppCertListAsyncContext & context)43 void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context)
44 {
45     if (context == nullptr) {
46         return;
47     }
48 
49     DeleteNapiContext(env, context->asyncWork, context->callback);
50 
51     if (context->credentialList != nullptr) {
52         FreeCredentialList(context->credentialList);
53     }
54 
55     CmFree(context);
56     context = nullptr;
57 }
58 
GetAppCertListParseParams(napi_env env,napi_callback_info info,GetAppCertListAsyncContext context)59 napi_value GetAppCertListParseParams(
60     napi_env env, napi_callback_info info, GetAppCertListAsyncContext context)
61 {
62     size_t argc = CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS;
63     napi_value argv[CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS] = { nullptr };
64     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
65 
66     if ((argc != CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS) && (argc != CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS)) {
67         ThrowParamsError(env, PARAM_ERROR, "Missing parameter");
68         CM_LOG_E("Missing parameter");
69         return nullptr;
70     }
71 
72     size_t index = 0;
73     if (index < argc) {
74         context->callback = GetCallback(env, argv[index]);
75         if (context->callback == nullptr) {
76             ThrowParamsError(env, PARAM_ERROR, "Get callback type error");
77             CM_LOG_E("get callback function faild when getting application certificate list");
78             return nullptr;
79         }
80     }
81 
82     return GetInt32(env, 0);
83 }
84 
GetAppCertListWriteResult(napi_env env,GetAppCertListAsyncContext context)85 napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context)
86 {
87     napi_value result = nullptr;
88     NAPI_CALL(env, napi_create_object(env, &result));
89     napi_value credentail = GenerateCredentialAbstractArray(env,
90         context->credentialList->credentialAbstract, context->credentialList->credentialCount);
91     if (credentail != nullptr) {
92         napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL_LIST.c_str(), credentail);
93     } else {
94         NAPI_CALL(env, napi_get_undefined(env, &result));
95     }
96     return result;
97 }
98 
InitAppCertList(struct CredentialList * credentialList)99 void InitAppCertList(struct CredentialList *credentialList)
100 {
101     uint32_t buffSize = (MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract));
102     credentialList->credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
103     if (credentialList->credentialAbstract == nullptr) {
104         CM_LOG_E("malloc file buffer failed");
105         return;
106     }
107     (void)memset_s(credentialList->credentialAbstract, buffSize, 0, buffSize);
108     credentialList->credentialCount = MAX_COUNT_CERTIFICATE;
109 }
110 
GetAppCertListAsyncWork(napi_env env,GetAppCertListAsyncContext asyncContext)111 napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext asyncContext)
112 {
113     napi_value promise = nullptr;
114     GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
115 
116     napi_value resourceName = nullptr;
117     NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
118 
119     NAPI_CALL(env, napi_create_async_work(
120         env,
121         nullptr,
122         resourceName,
123         [](napi_env env, void *data) {
124             GetAppCertListAsyncContext context = static_cast<GetAppCertListAsyncContext>(data);
125 
126             context->credentialList = static_cast<struct CredentialList *>(CmMalloc(sizeof(struct CredentialList)));
127             if (context->credentialList != nullptr) {
128                 InitAppCertList(context->credentialList);
129             }
130             context->result = CmGetAppCertList(context->store, context->credentialList);
131         },
132         [](napi_env env, napi_status status, void *data) {
133             GetAppCertListAsyncContext context = static_cast<GetAppCertListAsyncContext>(data);
134             napi_value result[RESULT_NUMBER] = { nullptr };
135             if (context->result == CM_SUCCESS) {
136                 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
137                 result[1] = GetAppCertListWriteResult(env, context);
138             } else {
139                 const char *errorMsg = "get app cert list info error";
140                 result[0] = GenerateBusinessError(env, context->result, errorMsg);
141                 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
142             }
143             if (context->deferred != nullptr) {
144                 GeneratePromise(env, context->deferred, context->result, result, sizeof(result));
145             } else {
146                 GenerateCallback(env, context->callback, result, sizeof(result));
147             }
148             DeleteGetAppCertListAsyncContext(env, context);
149             CM_LOG_I("get app cert list end");
150         },
151         static_cast<void *>(asyncContext),
152         &asyncContext->asyncWork));
153 
154     napi_status napiStatus = napi_queue_async_work(env, asyncContext->asyncWork);
155     if (napiStatus != napi_ok) {
156         GET_AND_THROW_LAST_ERROR((env));
157         DeleteGetAppCertListAsyncContext(env, asyncContext);
158         CM_LOG_E("get app cert list could not queue async work");
159         return nullptr;
160     }
161     return promise;
162 }
163 
CMNapiGetAppCertListCommon(napi_env env,napi_callback_info info,uint32_t store)164 napi_value CMNapiGetAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store)
165 {
166     GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext();
167     if (context == nullptr) {
168         CM_LOG_E("could not create context");
169         return nullptr;
170     }
171 
172     context->store = store;
173 
174     napi_value result = GetAppCertListParseParams(env, info, context);
175     if (result == nullptr) {
176         CM_LOG_E("could not parse params");
177         DeleteGetAppCertListAsyncContext(env, context);
178         return nullptr;
179     }
180     result = GetAppCertListAsyncWork(env, context);
181     if (result == nullptr) {
182         CM_LOG_E("could not start async work");
183         DeleteGetAppCertListAsyncContext(env, context);
184         return nullptr;
185     }
186     return result;
187 }
188 }  // namespace CertManagerNapi
189