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_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
InstallAppCertParseParams(napi_env env,napi_callback_info info,InstallAppCertAsyncContext context)71 napi_value InstallAppCertParseParams(
72 napi_env env, napi_callback_info info, InstallAppCertAsyncContext context)
73 {
74 size_t argc = CM_NAPI_INSTALL_APP_CERT_MAX_ARGS;
75 napi_value argv[CM_NAPI_INSTALL_APP_CERT_MAX_ARGS] = { nullptr };
76 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
77
78 if (argc < CM_NAPI_INSTALL_APP_CERT_MIN_ARGS) {
79 ThrowParamsError(env, PARAM_ERROR, "Missing parameter");
80 CM_LOG_E("Missing parameter");
81 return nullptr;
82 }
83
84 size_t index = 0;
85 context->keystore = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
86 if (context->keystore == nullptr) {
87 CM_LOG_E("could not alloc memory");
88 return nullptr;
89 }
90 (void)memset_s(context->keystore, sizeof(CmBlob), 0, sizeof(CmBlob));
91
92 napi_value result = GetUint8Array(env, argv[index], *context->keystore);
93 if (result == nullptr) {
94 ThrowParamsError(env, PARAM_ERROR, "get keystore type error");
95 CM_LOG_E("could not get keystore");
96 return nullptr;
97 }
98
99 index++;
100 result = ParseString(env, argv[index], context->keystorePwd);
101 if (result == nullptr) {
102 ThrowParamsError(env, PARAM_ERROR, "get keystore Pwd type error");
103 CM_LOG_E("could not get keystore Pwd");
104 return nullptr;
105 }
106
107 index++;
108 result = ParseString(env, argv[index], context->keyAlias);
109 if (result == nullptr) {
110 ThrowParamsError(env, PARAM_ERROR, "get keyAlias type error");
111 CM_LOG_E("could not get uri");
112 return nullptr;
113 }
114
115 index++;
116 if (index < argc) {
117 context->callback = GetCallback(env, argv[index]);
118 if (context->callback == nullptr) {
119 ThrowParamsError(env, PARAM_ERROR, "Get callback type error");
120 CM_LOG_E("get callback function faild when install application cert");
121 return nullptr;
122 }
123 }
124
125 return GetInt32(env, 0);
126 }
127
InitKeyUri(struct CmBlob * & keyUri)128 static void InitKeyUri(struct CmBlob *&keyUri)
129 {
130 keyUri = static_cast<struct CmBlob *>(CmMalloc(sizeof(struct CmBlob)));
131 if (keyUri == nullptr) {
132 CM_LOG_E("malloc keyUri buffer failed");
133 return;
134 }
135
136 keyUri->data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_URI));
137 if (keyUri->data == nullptr) {
138 CM_LOG_E("malloc keyUri->data buffer failed");
139 return;
140 }
141
142 (void)memset_s(keyUri->data, MAX_LEN_URI, 0, MAX_LEN_URI);
143 keyUri->size = MAX_LEN_URI;
144 }
145
InstallAppCertWriteResult(napi_env env,InstallAppCertAsyncContext context)146 static napi_value InstallAppCertWriteResult(napi_env env, InstallAppCertAsyncContext context)
147 {
148 napi_value result = nullptr;
149 NAPI_CALL(env, napi_create_object(env, &result));
150
151 napi_value keyUri = nullptr;
152 NAPI_CALL(env, napi_create_string_latin1(env, reinterpret_cast<char *>(context->keyUri->data),
153 NAPI_AUTO_LENGTH, &keyUri));
154 if (keyUri != nullptr) {
155 napi_set_named_property(env, result, CM_CERT_PROPERTY_URI.c_str(), keyUri);
156 } else {
157 NAPI_CALL(env, napi_get_undefined(env, &result));
158 }
159 return result;
160 }
161
InstallAppCertAsyncWork(napi_env env,InstallAppCertAsyncContext asyncContext)162 napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext asyncContext)
163 {
164 napi_value promise = nullptr;
165 GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
166
167 napi_value resourceName = nullptr;
168 NAPI_CALL(env, napi_create_string_latin1(env, "InstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
169
170 NAPI_CALL(env, napi_create_async_work(
171 env,
172 nullptr,
173 resourceName,
174 [](napi_env env, void *data) {
175 InstallAppCertAsyncContext context = static_cast<InstallAppCertAsyncContext>(data);
176 InitKeyUri(context->keyUri);
177 context->result = CmInstallAppCert(context->keystore,
178 context->keystorePwd, context->keyAlias, context->store, context->keyUri);
179 },
180 [](napi_env env, napi_status status, void *data) {
181 InstallAppCertAsyncContext context = static_cast<InstallAppCertAsyncContext>(data);
182 napi_value result[RESULT_NUMBER] = { nullptr };
183 if (context->result == CM_SUCCESS) {
184 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
185 result[1] = InstallAppCertWriteResult(env, context);
186 } else {
187 const char *errorMsg = "install app cert error";
188 result[0] = GenerateBusinessError(env, context->result, errorMsg);
189 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
190 }
191 if (context->deferred != nullptr) {
192 GeneratePromise(env, context->deferred, context->result, result, sizeof(result));
193 } else {
194 GenerateCallback(env, context->callback, result, sizeof(result));
195 }
196 DeleteInstallAppCertAsyncContext(env, context);
197 },
198 static_cast<void *>(asyncContext),
199 &asyncContext->asyncWork));
200
201 napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
202 if (status != napi_ok) {
203 GET_AND_THROW_LAST_ERROR((env));
204 DeleteInstallAppCertAsyncContext(env, asyncContext);
205 CM_LOG_E("could not queue async work");
206 return nullptr;
207 }
208 return promise;
209 }
210
CMNapiInstallAppCertCommon(napi_env env,napi_callback_info info,uint32_t store)211 napi_value CMNapiInstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store)
212 {
213 InstallAppCertAsyncContext context = CreateInstallAppCertAsyncContext();
214 if (context == nullptr) {
215 CM_LOG_E("could not create context");
216 return nullptr;
217 }
218
219 context->store = store;
220
221 napi_value result = InstallAppCertParseParams(env, info, context);
222 if (result == nullptr) {
223 CM_LOG_E("could not parse params");
224 DeleteInstallAppCertAsyncContext(env, context);
225 return nullptr;
226 }
227 result = InstallAppCertAsyncWork(env, context);
228 if (result == nullptr) {
229 CM_LOG_E("could not start async work");
230 DeleteInstallAppCertAsyncContext(env, context);
231 return nullptr;
232 }
233 return result;
234 }
235 } // namespace CertManagerNapi
236