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_set_cert_status.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_SET_CERT_STATUS_MIN_ARGS = 3;
29 constexpr int CM_NAPI_SET_CERT_STATUS_MAX_ARGS = 4;
30 } // namespace
31
32 struct SetCertStatusAsyncContextT {
33 napi_async_work asyncWork = nullptr;
34 napi_deferred deferred = nullptr;
35 napi_ref callback = nullptr;
36
37 int32_t result = 0;
38 struct CmBlob *certUri = nullptr;
39 uint32_t store = 0;
40 bool status = false;
41 };
42 using SetCertStatusAsyncContext = SetCertStatusAsyncContextT *;
43
CreateSetCertStatusAsyncContext()44 static SetCertStatusAsyncContext CreateSetCertStatusAsyncContext()
45 {
46 SetCertStatusAsyncContext context =
47 static_cast<SetCertStatusAsyncContext>(CmMalloc(sizeof(SetCertStatusAsyncContextT)));
48 if (context != nullptr) {
49 (void)memset_s(
50 context, sizeof(SetCertStatusAsyncContextT), 0, sizeof(SetCertStatusAsyncContextT));
51 }
52 return context;
53 }
54
DeleteSetCertStatusAsyncContext(napi_env env,SetCertStatusAsyncContext & context)55 static void DeleteSetCertStatusAsyncContext(napi_env env, SetCertStatusAsyncContext &context)
56 {
57 if (context == nullptr) {
58 return;
59 }
60
61 DeleteNapiContext(env, context->asyncWork, context->callback);
62
63 if (context->certUri != nullptr) {
64 FreeCmBlob(context->certUri);
65 }
66
67 CmFree(context);
68 context = nullptr;
69 }
70
SetCertStatusParseParams(napi_env env,napi_callback_info info,SetCertStatusAsyncContext context)71 static napi_value SetCertStatusParseParams(
72 napi_env env, napi_callback_info info, SetCertStatusAsyncContext context)
73 {
74 size_t argc = CM_NAPI_SET_CERT_STATUS_MAX_ARGS;
75 napi_value argv[CM_NAPI_SET_CERT_STATUS_MAX_ARGS] = { nullptr };
76 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
77
78 if (argc < CM_NAPI_SET_CERT_STATUS_MIN_ARGS) {
79 ThrowParamsError(env, PARAM_ERROR, "Missing parameter");
80 CM_LOG_E("CertStatus Missing parameter");
81 return nullptr;
82 }
83
84 size_t index = 0;
85 napi_value result = ParseString(env, argv[index], context->certUri);
86 if (result == nullptr) {
87 ThrowParamsError(env, PARAM_ERROR, "get certUri type error");
88 CM_LOG_E("could not get cert uri when set cert status");
89 return nullptr;
90 }
91
92 index++;
93 result = ParseUint32(env, argv[index], context->store);
94 if (result == nullptr) {
95 ThrowParamsError(env, PARAM_ERROR, "get store type error");
96 CM_LOG_E("could not get store");
97 return nullptr;
98 }
99
100 index++;
101 result = ParseBoolean(env, argv[index], context->status);
102 if (result == nullptr) {
103 ThrowParamsError(env, PARAM_ERROR, "get status type error");
104 CM_LOG_E("could not get status");
105 return nullptr;
106 }
107
108 index++;
109 if (index < argc) {
110 context->callback = GetCallback(env, argv[index]);
111 }
112 return GetInt32(env, 0);
113 }
114
SetCertStatusExecute(napi_env env,void * data)115 static void SetCertStatusExecute(napi_env env, void *data)
116 {
117 SetCertStatusAsyncContext context = static_cast<SetCertStatusAsyncContext>(data);
118 if (context->store == CM_SYSTEM_TRUSTED_STORE) {
119 context->result = CmSetCertStatus(context->certUri, context->store,
120 context->status);
121 } else if (context->store == CM_USER_TRUSTED_STORE) {
122 context->result = CmSetUserCertStatus(context->certUri, context->store, context->status);
123 } else {
124 context->result = CMR_ERROR_INVALID_ARGUMENT;
125 }
126 }
127
SetCertStatusComplete(napi_env env,napi_status status,void * data)128 static void SetCertStatusComplete(napi_env env, napi_status status, void *data)
129 {
130 SetCertStatusAsyncContext context = static_cast<SetCertStatusAsyncContext>(data);
131 napi_value result[RESULT_NUMBER] = { nullptr };
132 if (context->result == CM_SUCCESS) {
133 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
134 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1]));
135 } else {
136 const char *errorMsg = "set cert status error";
137 result[0] = GenerateBusinessError(env, context->result, errorMsg);
138 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
139 }
140 if (context->deferred != nullptr) {
141 GeneratePromise(env, context->deferred, context->result, result, sizeof(result));
142 } else {
143 GenerateCallback(env, context->callback, result, sizeof(result));
144 }
145 DeleteSetCertStatusAsyncContext(env, context);
146 }
147
SetCertStatusAsyncWork(napi_env env,SetCertStatusAsyncContext context)148 static napi_value SetCertStatusAsyncWork(napi_env env, SetCertStatusAsyncContext context)
149 {
150 napi_value promise = nullptr;
151 GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
152
153 napi_value resourceName = nullptr;
154 NAPI_CALL(env, napi_create_string_latin1(env, "SetCertStatusAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
155
156 NAPI_CALL(env, napi_create_async_work(
157 env,
158 nullptr,
159 resourceName,
160 SetCertStatusExecute,
161 SetCertStatusComplete,
162 static_cast<void *>(context),
163 &context->asyncWork));
164
165 napi_status status = napi_queue_async_work(env, context->asyncWork);
166 if (status != napi_ok) {
167 GET_AND_THROW_LAST_ERROR((env));
168 DeleteSetCertStatusAsyncContext(env, context);
169 CM_LOG_E("could not queue async work");
170 return nullptr;
171 }
172 return promise;
173 }
174
CMNapiSetCertStatus(napi_env env,napi_callback_info info)175 napi_value CMNapiSetCertStatus(napi_env env, napi_callback_info info)
176 {
177 SetCertStatusAsyncContext context = CreateSetCertStatusAsyncContext();
178 if (context == nullptr) {
179 CM_LOG_E("could not create context");
180 return nullptr;
181 }
182
183 napi_value result = SetCertStatusParseParams(env, info, context);
184 if (result == nullptr) {
185 CM_LOG_E("could not parse params");
186 DeleteSetCertStatusAsyncContext(env, context);
187 return nullptr;
188 }
189 result = SetCertStatusAsyncWork(env, context);
190 if (result == nullptr) {
191 CM_LOG_E("could not start async work");
192 DeleteSetCertStatusAsyncContext(env, context);
193 return nullptr;
194 }
195 return result;
196 }
197 } // namespace CertManagerNapi
198