1 /*
2 * Copyright (c) 2021-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 "huks_napi_delete_key.h"
17
18 #include "securec.h"
19
20 #include "hks_api.h"
21 #include "hks_log.h"
22 #include "hks_mem.h"
23 #include "hks_param.h"
24 #include "hks_type.h"
25 #include "huks_napi_common.h"
26
27 namespace HuksNapi {
28 namespace {
29 constexpr int HUKS_NAPI_DELETE_KEY_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_DELETE_KEY_MAX_ARGS = 3;
31 } // namespace
32
33 struct DeleteKeyAsyncContextT {
34 napi_async_work asyncWork = nullptr;
35 napi_deferred deferred = nullptr;
36 napi_ref callback = nullptr;
37
38 int32_t result = 0;
39 struct HksBlob *keyAlias = nullptr;
40 struct HksParamSet *paramSet = nullptr;
41 };
42 using DeleteKeyAsyncContext = DeleteKeyAsyncContextT *;
43
CreateDeleteKeyAsyncContext()44 static DeleteKeyAsyncContext CreateDeleteKeyAsyncContext()
45 {
46 DeleteKeyAsyncContext context = (DeleteKeyAsyncContext)HksMalloc(sizeof(DeleteKeyAsyncContextT));
47 if (context != nullptr) {
48 (void)memset_s(context, sizeof(DeleteKeyAsyncContextT), 0, sizeof(DeleteKeyAsyncContextT));
49 }
50 return context;
51 }
52
DeleteDeleteKeyAsyncContext(napi_env env,DeleteKeyAsyncContext & context)53 static void DeleteDeleteKeyAsyncContext(napi_env env, DeleteKeyAsyncContext &context)
54 {
55 if (context == nullptr) {
56 return;
57 }
58
59 if (context->asyncWork != nullptr) {
60 napi_delete_async_work(env, context->asyncWork);
61 context->asyncWork = nullptr;
62 }
63
64 if (context->callback != nullptr) {
65 napi_delete_reference(env, context->callback);
66 context->callback = nullptr;
67 }
68
69 if (context->keyAlias != nullptr) {
70 FreeHksBlob(context->keyAlias);
71 }
72
73 if (context->paramSet != nullptr) {
74 HksFreeParamSet(&context->paramSet);
75 }
76
77 HksFree(context);
78 context = nullptr;
79 }
80
DeleteKeyParseParams(napi_env env,napi_callback_info info,DeleteKeyAsyncContext context)81 static napi_value DeleteKeyParseParams(napi_env env, napi_callback_info info, DeleteKeyAsyncContext context)
82 {
83 size_t argc = HUKS_NAPI_DELETE_KEY_MAX_ARGS;
84 napi_value argv[HUKS_NAPI_DELETE_KEY_MAX_ARGS] = {0};
85 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
86
87 if (argc < HUKS_NAPI_DELETE_KEY_MIN_ARGS) {
88 napi_throw_error(env, NULL, "invalid arguments");
89 HKS_LOG_E("no enough params");
90 return nullptr;
91 }
92
93 size_t index = 0;
94 napi_value result = ParseKeyAlias(env, argv[index], context->keyAlias);
95 if (result == nullptr) {
96 HKS_LOG_E("could not get alias");
97 return nullptr;
98 }
99
100 index++;
101 napi_value properties = nullptr;
102 napi_status status =
103 napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_PROPERTIES.c_str(), &properties);
104 if (status != napi_ok || properties == nullptr) {
105 GET_AND_THROW_LAST_ERROR((env));
106 HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_PROPERTIES.c_str());
107 return nullptr;
108 }
109 result = ParseHksParamSet(env, properties, context->paramSet);
110 if (result == nullptr) {
111 HKS_LOG_E("could not get paramset");
112 return nullptr;
113 }
114
115 index++;
116 if (index < argc) {
117 context->callback = GetCallback(env, argv[index]);
118 }
119
120 return GetInt32(env, 0);
121 }
122
DeleteKeyWriteResult(napi_env env,DeleteKeyAsyncContext context)123 static napi_value DeleteKeyWriteResult(napi_env env, DeleteKeyAsyncContext context)
124 {
125 return GenerateHksResult(env, context->result, nullptr, 0);
126 }
127
DeleteKeyAsyncWork(napi_env env,DeleteKeyAsyncContext context)128 static napi_value DeleteKeyAsyncWork(napi_env env, DeleteKeyAsyncContext context)
129 {
130 napi_value promise = nullptr;
131 if (context->callback == nullptr) {
132 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
133 }
134
135 napi_value resourceName = nullptr;
136 napi_create_string_latin1(env, "deleteKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
137
138 napi_create_async_work(
139 env,
140 nullptr,
141 resourceName,
142 [](napi_env env, void *data) {
143 DeleteKeyAsyncContext context = static_cast<DeleteKeyAsyncContext>(data);
144
145 context->result = HksDeleteKey(context->keyAlias, context->paramSet);
146 },
147 [](napi_env env, napi_status status, void *data) {
148 DeleteKeyAsyncContext context = static_cast<DeleteKeyAsyncContext>(data);
149 napi_value result = DeleteKeyWriteResult(env, context);
150 if (result == nullptr) {
151 return;
152 }
153 if (context->callback != nullptr) {
154 CallAsyncCallback(env, context->callback, context->result, result);
155 } else {
156 napi_resolve_deferred(env, context->deferred, result);
157 }
158 DeleteDeleteKeyAsyncContext(env, context);
159 },
160 (void *)context,
161 &context->asyncWork);
162
163 napi_status status = napi_queue_async_work(env, context->asyncWork);
164 if (status != napi_ok) {
165 GET_AND_THROW_LAST_ERROR((env));
166 DeleteDeleteKeyAsyncContext(env, context);
167 HKS_LOG_E("could not queue async work");
168 return nullptr;
169 }
170
171 if (context->callback == nullptr) {
172 return promise;
173 } else {
174 return GetNull(env);
175 }
176 }
177
HuksNapiDeleteKey(napi_env env,napi_callback_info info)178 napi_value HuksNapiDeleteKey(napi_env env, napi_callback_info info)
179 {
180 DeleteKeyAsyncContext context = CreateDeleteKeyAsyncContext();
181 if (context == nullptr) {
182 HKS_LOG_E("could not create context");
183 return nullptr;
184 }
185
186 napi_value result = DeleteKeyParseParams(env, info, context);
187 if (result == nullptr) {
188 HKS_LOG_E("could not parse params");
189 DeleteDeleteKeyAsyncContext(env, context);
190 return nullptr;
191 }
192
193 result = DeleteKeyAsyncWork(env, context);
194 if (result == nullptr) {
195 HKS_LOG_E("could not start async work");
196 DeleteDeleteKeyAsyncContext(env, context);
197 return nullptr;
198 }
199 return result;
200 }
201 } // namespace HuksNapi