• 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_install_app_cert.h"
17 #include "cm_napi_install_app_cert_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_INSTALL_APP_CERT_MIN_ARGS = 3;
30 constexpr int CM_NAPI_INSTALL_APP_CERT_MAX_ARGS = 4;
31 }  // namespace
32 
CreateInstallAppCertAsyncContext()33 InstallAppCertAsyncContext CreateInstallAppCertAsyncContext()
34 {
35     InstallAppCertAsyncContext context =
36         static_cast<InstallAppCertAsyncContext>(CmMalloc(sizeof(InstallAppCertAsyncContextT)));
37     if (context != nullptr) {
38         (void)memset_s(context, sizeof(InstallAppCertAsyncContextT), 0, sizeof(InstallAppCertAsyncContextT));
39     }
40     return context;
41 }
42 
DeleteInstallAppCertAsyncContext(napi_env env,InstallAppCertAsyncContext & context)43 void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context)
44 {
45     if (context == nullptr) {
46         return;
47     }
48 
49     DeleteNapiContext(env, context->asyncWork, context->callback);
50 
51     if (context->keystore != nullptr) {
52         FreeCmBlob(context->keystore);
53     }
54 
55     if (context->keystorePwd != nullptr) {
56         FreeCmBlob(context->keystorePwd);
57     }
58 
59     if (context->keyAlias != nullptr) {
60         FreeCmBlob(context->keyAlias);
61     }
62 
63     if (context->keyUri != nullptr) {
64         FreeCmBlob(context->keyUri);
65     }
66 
67     CmFree(context);
68     context = nullptr;
69 }
70 
GetCredAlias(napi_env env,napi_value napiObject,CmBlob * & certAlias,uint32_t store)71 static napi_value GetCredAlias(napi_env env, napi_value napiObject, CmBlob *&certAlias, uint32_t store)
72 {
73     if (store == APPLICATION_PRIVATE_CERTIFICATE_STORE) {
74         return ParseString(env, napiObject, certAlias);
75     }
76     return ParseCertAlias(env, napiObject, certAlias);
77 }
78 
GetLevelOrCallback(napi_env env,InstallAppCertAsyncContext context,napi_value napiObject)79 static napi_value GetLevelOrCallback(napi_env env, InstallAppCertAsyncContext context, napi_value napiObject)
80 {
81     napi_valuetype type = napi_undefined;
82     NAPI_CALL(env, napi_typeof(env, napiObject, &type));
83     if (type == napi_number) {
84         uint32_t level = CM_AUTH_STORAGE_LEVEL_EL1;
85         napi_value result = ParseUint32(env, napiObject, level);
86         if (result == nullptr || CM_LEVEL_CHECK(level)) {
87             ThrowError(env, PARAM_ERROR, "level is not a uint32 or level is invalid.");
88             CM_LOG_E("could not get level");
89             return nullptr;
90         }
91         context->level = (enum CmAuthStorageLevel)level;
92     } else {
93         int32_t ret = GetCallback(env, napiObject, context->callback);
94         if (ret != CM_SUCCESS) {
95             ThrowError(env, PARAM_ERROR, "Get callback failed, callback must be a function.");
96             CM_LOG_E("get callback function faild when install application cert");
97             return nullptr;
98         }
99     }
100     return GetInt32(env, 0);
101 }
102 
InstallAppCertParseParams(napi_env env,napi_callback_info info,InstallAppCertAsyncContext context,uint32_t store)103 napi_value InstallAppCertParseParams(
104     napi_env env, napi_callback_info info, InstallAppCertAsyncContext context, uint32_t store)
105 {
106     size_t argc = CM_NAPI_INSTALL_APP_CERT_MAX_ARGS;
107     napi_value argv[CM_NAPI_INSTALL_APP_CERT_MAX_ARGS] = { nullptr };
108     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
109     if ((argc != CM_NAPI_INSTALL_APP_CERT_MIN_ARGS) && (argc != CM_NAPI_INSTALL_APP_CERT_MAX_ARGS)) {
110         ThrowError(env, PARAM_ERROR, "arguments count invalid, arguments count need between 3 and 4.");
111         CM_LOG_E("arguments count invalid. argc = %d", argc);
112         return nullptr;
113     }
114     size_t index = 0;
115     context->keystore = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
116     if (context->keystore == nullptr) {
117         CM_LOG_E("could not alloc memory");
118         return nullptr;
119     }
120     (void)memset_s(context->keystore, sizeof(CmBlob), 0, sizeof(CmBlob));
121     napi_value result = GetUint8Array(env, argv[index], *context->keystore);
122     if (result == nullptr) {
123         ThrowError(env, PARAM_ERROR, "keystore is not a uint8Array or the length is 0 or too long.");
124         CM_LOG_E("could not get keystore");
125         return nullptr;
126     }
127 
128     index++;
129     result = ParsePasswd(env, argv[index], context->keystorePwd);
130     if (result == nullptr) {
131         ThrowError(env, PARAM_ERROR, "keystore Pwd is not a string or the length is 0 or too long.");
132         CM_LOG_E("could not get keystore Pwd");
133         return nullptr;
134     }
135 
136     index++;
137     result = GetCredAlias(env, argv[index], context->keyAlias, store);
138     if (result == nullptr) {
139         ThrowError(env, PARAM_ERROR, "keyAlias is not a string or the length is 0 or too long.");
140         CM_LOG_E("could not get uri");
141         return nullptr;
142     }
143 
144     index++;
145     context->level = CM_AUTH_STORAGE_LEVEL_EL1;
146     if (index < argc) {
147         if (GetLevelOrCallback(env, context, argv[index]) == nullptr) {
148             return nullptr;
149         }
150     }
151     return GetInt32(env, 0);
152 }
153 
InitKeyUri(struct CmBlob * & keyUri)154 static void InitKeyUri(struct CmBlob *&keyUri)
155 {
156     keyUri = static_cast<struct CmBlob *>(CmMalloc(sizeof(struct CmBlob)));
157     if (keyUri == nullptr) {
158         CM_LOG_E("malloc keyUri buffer failed");
159         return;
160     }
161 
162     keyUri->data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_URI));
163     if (keyUri->data == nullptr) {
164         CM_LOG_E("malloc keyUri->data buffer failed");
165         return;
166     }
167 
168     (void)memset_s(keyUri->data, MAX_LEN_URI, 0, MAX_LEN_URI);
169     keyUri->size = MAX_LEN_URI;
170 }
171 
InstallAppCertWriteResult(napi_env env,InstallAppCertAsyncContext context)172 static napi_value InstallAppCertWriteResult(napi_env env, InstallAppCertAsyncContext context)
173 {
174     napi_value result = nullptr;
175     NAPI_CALL(env, napi_create_object(env, &result));
176 
177     napi_value keyUri = nullptr;
178     NAPI_CALL(env, napi_create_string_latin1(env, reinterpret_cast<char *>(context->keyUri->data),
179         NAPI_AUTO_LENGTH, &keyUri));
180     if (keyUri != nullptr) {
181         napi_set_named_property(env, result, CM_CERT_PROPERTY_URI.c_str(), keyUri);
182     } else {
183         NAPI_CALL(env, napi_get_undefined(env, &result));
184     }
185     return result;
186 }
187 
GenAppCertBusinessError(napi_env env,int32_t errorCode,uint32_t store)188 static napi_value GenAppCertBusinessError(napi_env env, int32_t errorCode, uint32_t store)
189 {
190     if ((errorCode == CMR_ERROR_PASSWORD_IS_ERR) && (store == APPLICATION_PRIVATE_CERTIFICATE_STORE)) {
191         errorCode = CMR_ERROR_INVALID_CERT_FORMAT;
192     }
193     return GenerateBusinessError(env, errorCode);
194 }
195 
InstallAppCertExecute(napi_env env,void * data)196 static void InstallAppCertExecute(napi_env env, void *data)
197 {
198     InstallAppCertAsyncContext context = static_cast<InstallAppCertAsyncContext>(data);
199     InitKeyUri(context->keyUri);
200     if (context->store == CM_PRI_CREDENTIAL_STORE) {
201         struct CmAppCertParam certParam = { (struct CmBlob *)context->keystore, (struct CmBlob *)context->keystorePwd,
202             (struct CmBlob *)context->keyAlias, context->store, INIT_INVALID_VALUE, context->level };
203 
204         context->result = CmInstallAppCertEx(&certParam, context->keyUri);
205     } else {
206         context->result = CmInstallAppCert(context->keystore,
207             context->keystorePwd, context->keyAlias, context->store, context->keyUri);
208     }
209 }
210 
InstallAppCertComplete(napi_env env,napi_status status,void * data)211 static void InstallAppCertComplete(napi_env env, napi_status status, void *data)
212 {
213     InstallAppCertAsyncContext context = static_cast<InstallAppCertAsyncContext>(data);
214     napi_value result[RESULT_NUMBER] = { nullptr };
215     if (context->result == CM_SUCCESS) {
216         NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
217         result[1] = InstallAppCertWriteResult(env, context);
218     } else {
219         result[0] = GenAppCertBusinessError(env, context->result, context->store);
220         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
221     }
222     if (context->deferred != nullptr) {
223         GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
224     } else {
225         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
226     }
227     DeleteInstallAppCertAsyncContext(env, context);
228 }
229 
InstallAppCertAsyncWork(napi_env env,InstallAppCertAsyncContext asyncContext)230 napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext asyncContext)
231 {
232     napi_value promise = nullptr;
233     GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
234 
235     napi_value resourceName = nullptr;
236     NAPI_CALL(env, napi_create_string_latin1(env, "InstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
237 
238     NAPI_CALL(env, napi_create_async_work(
239         env, nullptr, resourceName,
240         InstallAppCertExecute,
241         InstallAppCertComplete,
242         static_cast<void *>(asyncContext),
243         &asyncContext->asyncWork));
244 
245     napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
246     if (status != napi_ok) {
247         GET_AND_THROW_LAST_ERROR((env));
248         DeleteInstallAppCertAsyncContext(env, asyncContext);
249         CM_LOG_E("could not queue async work");
250         return nullptr;
251     }
252     return promise;
253 }
254 
CMNapiInstallAppCertCommon(napi_env env,napi_callback_info info,uint32_t store)255 napi_value CMNapiInstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store)
256 {
257     CM_LOG_I("install app cert enter, store = %u", store);
258 
259     InstallAppCertAsyncContext context = CreateInstallAppCertAsyncContext();
260     if (context == nullptr) {
261         CM_LOG_E("could not create context");
262         return nullptr;
263     }
264 
265     context->store = store;
266 
267     napi_value result = InstallAppCertParseParams(env, info, context, store);
268     if (result == nullptr) {
269         CM_LOG_E("could not parse params");
270         DeleteInstallAppCertAsyncContext(env, context);
271         return nullptr;
272     }
273     result = InstallAppCertAsyncWork(env, context);
274     if (result == nullptr) {
275         CM_LOG_E("could not start async work");
276         DeleteInstallAppCertAsyncContext(env, context);
277         return nullptr;
278     }
279 
280     CM_LOG_I("install app cert end");
281     return result;
282 }
283 }  // namespace CertManagerNapi
284