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_unwrap_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_WRAP_KEY_MIN_ARGS = 3;
30 constexpr int HUKS_NAPI_WRAP_KEY_MAX_ARGS = 4;
31 } // namespace
32
33 struct UnwrapKeyAsyncContextT {
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 HksBlob *targetKeyAlias = nullptr;
41 struct HksBlob *wrappedData = nullptr;
42 struct HksParamSet *paramSet = nullptr;
43 };
44 using UnwrapKeyAsyncContext = UnwrapKeyAsyncContextT *;
45
CreateUnwrapKeyAsyncContext()46 static UnwrapKeyAsyncContext CreateUnwrapKeyAsyncContext()
47 {
48 UnwrapKeyAsyncContext context = (UnwrapKeyAsyncContext)HksMalloc(sizeof(UnwrapKeyAsyncContextT));
49 if (context != nullptr) {
50 (void)memset_s(context, sizeof(UnwrapKeyAsyncContextT), 0, sizeof(UnwrapKeyAsyncContextT));
51 }
52 return context;
53 }
54
DeleteUnwrapKeyAsyncContext(napi_env env,UnwrapKeyAsyncContext & context)55 static void DeleteUnwrapKeyAsyncContext(napi_env env, UnwrapKeyAsyncContext &context)
56 {
57 if (context == nullptr) {
58 return;
59 }
60
61 if (context->asyncWork != nullptr) {
62 napi_delete_async_work(env, context->asyncWork);
63 context->asyncWork = nullptr;
64 }
65
66 if (context->callback != nullptr) {
67 napi_delete_reference(env, context->callback);
68 context->callback = nullptr;
69 }
70
71 if (context->keyAlias != nullptr) {
72 FreeHksBlob(context->keyAlias);
73 }
74
75 if (context->targetKeyAlias != nullptr) {
76 FreeHksBlob(context->targetKeyAlias);
77 }
78
79 if (context->paramSet != nullptr) {
80 HksFreeParamSet(&context->paramSet);
81 }
82
83 if (context->wrappedData != nullptr) {
84 if (context->wrappedData->data != nullptr && context->wrappedData->size != 0) {
85 (void)memset_s(context->wrappedData->data, context->wrappedData->size, 0, context->wrappedData->size);
86 }
87 FreeHksBlob(context->wrappedData);
88 }
89
90 HksFree(context);
91 context = nullptr;
92 }
93
UnwrapKeyParseOptions(napi_env env,napi_value options,UnwrapKeyAsyncContext context)94 static napi_value UnwrapKeyParseOptions(napi_env env, napi_value options, UnwrapKeyAsyncContext context)
95 {
96 napi_value result = nullptr;
97
98 napi_value properties = nullptr;
99 napi_status status = napi_get_named_property(env, options, HKS_OPTIONS_PROPERTY_PROPERTIES.c_str(), &properties);
100 if (status != napi_ok || properties == nullptr) {
101 GET_AND_THROW_LAST_ERROR((env));
102 HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_PROPERTIES.c_str());
103 return nullptr;
104 }
105 result = ParseHksParamSet(env, properties, context->paramSet);
106 if (result == nullptr) {
107 HKS_LOG_E("could not get paramset");
108 return nullptr;
109 }
110 napi_value inData = nullptr;
111 status = napi_get_named_property(env, options, HKS_OPTIONS_PROPERTY_INDATA.c_str(), &inData);
112 if (status != napi_ok || inData == nullptr) {
113 GET_AND_THROW_LAST_ERROR((env));
114 HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_INDATA.c_str());
115 return nullptr;
116 }
117 context->wrappedData = (HksBlob *)HksMalloc(sizeof(HksBlob));
118 if (context->wrappedData == nullptr) {
119 HKS_LOG_E("could not alloc memory");
120 return nullptr;
121 }
122 (void)memset_s(context->wrappedData, sizeof(HksBlob), 0, sizeof(HksBlob));
123
124 if (GetUint8Array(env, inData, *context->wrappedData) == nullptr) {
125 HKS_LOG_E("could not get indata");
126 return nullptr;
127 }
128
129 return GetInt32(env, 0);
130 }
131
UnwrapKeyParseParams(napi_env env,napi_callback_info info,UnwrapKeyAsyncContext context)132 static napi_value UnwrapKeyParseParams(napi_env env, napi_callback_info info, UnwrapKeyAsyncContext context)
133 {
134 size_t argc = HUKS_NAPI_WRAP_KEY_MAX_ARGS;
135 napi_value argv[HUKS_NAPI_WRAP_KEY_MAX_ARGS] = {0};
136 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
137
138 if (argc < HUKS_NAPI_WRAP_KEY_MIN_ARGS) {
139 napi_throw_error(env, NULL, "invalid arguments");
140 HKS_LOG_E("no enough params");
141 return nullptr;
142 }
143
144 size_t index = 0;
145 napi_value result = ParseKeyAlias(env, argv[index], context->keyAlias);
146 if (result == nullptr) {
147 HKS_LOG_E("could not get alias");
148 return nullptr;
149 }
150
151 index++;
152 result = ParseKeyAlias(env, argv[index], context->targetKeyAlias);
153 if (result == nullptr) {
154 HKS_LOG_E("could not get target alias");
155 return nullptr;
156 }
157
158 index++;
159 result = UnwrapKeyParseOptions(env, argv[index], context);
160 if (result == nullptr) {
161 HKS_LOG_E("could not parse options");
162 return nullptr;
163 }
164
165 index++;
166 if (index < argc) {
167 context->callback = GetCallback(env, argv[index]);
168 }
169
170 return GetInt32(env, 0);
171 }
172
UnwrapKeyWriteResult(napi_env env,UnwrapKeyAsyncContext context)173 static napi_value UnwrapKeyWriteResult(napi_env env, UnwrapKeyAsyncContext context)
174 {
175 return GenerateHksResult(env, context->result, nullptr, 0);
176 }
177
UnwrapKeyAsyncWork(napi_env env,UnwrapKeyAsyncContext context)178 static napi_value UnwrapKeyAsyncWork(napi_env env, UnwrapKeyAsyncContext context)
179 {
180 napi_value promise = nullptr;
181 if (context->callback == nullptr) {
182 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
183 }
184
185 napi_value resourceName = nullptr;
186 napi_create_string_latin1(env, "unwrapKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
187
188 napi_create_async_work(
189 env,
190 nullptr,
191 resourceName,
192 [](napi_env env, void *data) {
193 UnwrapKeyAsyncContext context = static_cast<UnwrapKeyAsyncContext>(data);
194
195 context->result =
196 HksUnwrapKey(context->keyAlias, context->targetKeyAlias, context->wrappedData, context->paramSet);
197 },
198 [](napi_env env, napi_status status, void *data) {
199 UnwrapKeyAsyncContext context = static_cast<UnwrapKeyAsyncContext>(data);
200 napi_value result = UnwrapKeyWriteResult(env, context);
201 if (result == nullptr) {
202 return;
203 }
204 if (context->callback != nullptr) {
205 CallAsyncCallback(env, context->callback, context->result, result);
206 } else {
207 napi_resolve_deferred(env, context->deferred, result);
208 }
209 DeleteUnwrapKeyAsyncContext(env, context);
210 },
211 (void *)context,
212 &context->asyncWork);
213
214 napi_status status = napi_queue_async_work(env, context->asyncWork);
215 if (status != napi_ok) {
216 GET_AND_THROW_LAST_ERROR((env));
217 DeleteUnwrapKeyAsyncContext(env, context);
218 HKS_LOG_E("could not queue async work");
219 return nullptr;
220 }
221
222 if (context->callback == nullptr) {
223 return promise;
224 } else {
225 return GetNull(env);
226 }
227 return nullptr;
228 }
229
HuksNapiUnwrapKey(napi_env env,napi_callback_info info)230 napi_value HuksNapiUnwrapKey(napi_env env, napi_callback_info info)
231 {
232 UnwrapKeyAsyncContext context = CreateUnwrapKeyAsyncContext();
233 if (context == nullptr) {
234 HKS_LOG_E("could not create context");
235 return nullptr;
236 }
237
238 napi_value result = UnwrapKeyParseParams(env, info, context);
239 if (result == nullptr) {
240 HKS_LOG_E("could not parse params");
241 DeleteUnwrapKeyAsyncContext(env, context);
242 return nullptr;
243 }
244
245 result = UnwrapKeyAsyncWork(env, context);
246 if (result == nullptr) {
247 HKS_LOG_E("could not start async work");
248 DeleteUnwrapKeyAsyncContext(env, context);
249 return nullptr;
250 }
251 return result;
252 }
253 } // namespace HuksNapi
254