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 = static_cast<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 DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
59 HKS_FREE(context);
60 context = nullptr;
61 }
62
DeleteKeyParseParams(napi_env env,napi_callback_info info,DeleteKeyAsyncContext context)63 static napi_value DeleteKeyParseParams(napi_env env, napi_callback_info info, DeleteKeyAsyncContext context)
64 {
65 size_t argc = HUKS_NAPI_DELETE_KEY_MAX_ARGS;
66 napi_value argv[HUKS_NAPI_DELETE_KEY_MAX_ARGS] = { 0 };
67 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
68
69 if (argc < HUKS_NAPI_DELETE_KEY_MIN_ARGS) {
70 napi_throw_error(env, nullptr, "invalid arguments");
71 HKS_LOG_E("no enough params");
72 return nullptr;
73 }
74
75 size_t index = 0;
76 napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
77 if (result == nullptr) {
78 HKS_LOG_E("deleteKey parse params failed");
79 return nullptr;
80 }
81
82 index++;
83 if (index < argc) {
84 context->callback = GetCallback(env, argv[index]);
85 }
86
87 return GetInt32(env, 0);
88 }
89
DeleteKeyWriteResult(napi_env env,DeleteKeyAsyncContext context)90 static napi_value DeleteKeyWriteResult(napi_env env, DeleteKeyAsyncContext context)
91 {
92 return GenerateHksResult(env, context->result, nullptr, 0);
93 }
94
DeleteKeyAsyncWork(napi_env env,DeleteKeyAsyncContext & context)95 static napi_value DeleteKeyAsyncWork(napi_env env, DeleteKeyAsyncContext &context)
96 {
97 napi_value promise = nullptr;
98 if (context->callback == nullptr) {
99 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
100 }
101
102 napi_value resourceName = nullptr;
103 napi_create_string_latin1(env, "deleteKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
104
105 napi_create_async_work(
106 env,
107 nullptr,
108 resourceName,
109 [](napi_env env, void *data) {
110 DeleteKeyAsyncContext napiContext = static_cast<DeleteKeyAsyncContext>(data);
111
112 napiContext->result = HksDeleteKey(napiContext->keyAlias, napiContext->paramSet);
113 },
114 [](napi_env env, napi_status status, void *data) {
115 DeleteKeyAsyncContext napiContext = static_cast<DeleteKeyAsyncContext>(data);
116 napi_value result = DeleteKeyWriteResult(env, napiContext);
117 if (napiContext->callback == nullptr) {
118 napi_resolve_deferred(env, napiContext->deferred, result);
119 } else if (result != nullptr) {
120 CallAsyncCallback(env, napiContext->callback, napiContext->result, result);
121 }
122 DeleteDeleteKeyAsyncContext(env, napiContext);
123 },
124 static_cast<void *>(context),
125 &context->asyncWork);
126
127 napi_status status = napi_queue_async_work(env, context->asyncWork);
128 if (status != napi_ok) {
129 GET_AND_THROW_LAST_ERROR((env));
130 DeleteDeleteKeyAsyncContext(env, context);
131 HKS_LOG_E("could not queue async work");
132 return nullptr;
133 }
134
135 if (context->callback == nullptr) {
136 return promise;
137 } else {
138 return GetNull(env);
139 }
140 }
141
HuksNapiDeleteKey(napi_env env,napi_callback_info info)142 napi_value HuksNapiDeleteKey(napi_env env, napi_callback_info info)
143 {
144 DeleteKeyAsyncContext context = CreateDeleteKeyAsyncContext();
145 if (context == nullptr) {
146 HKS_LOG_E("could not create context");
147 return nullptr;
148 }
149
150 napi_value result = DeleteKeyParseParams(env, info, context);
151 if (result == nullptr) {
152 HKS_LOG_E("could not parse params");
153 DeleteDeleteKeyAsyncContext(env, context);
154 return nullptr;
155 }
156
157 result = DeleteKeyAsyncWork(env, context);
158 if (result == nullptr) {
159 HKS_LOG_E("could not start async work");
160 DeleteDeleteKeyAsyncContext(env, context);
161 return nullptr;
162 }
163 return result;
164 }
165 } // namespace HuksNapi
166