1 /*
2 * Copyright (c) 2025 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_wrap_key.h"
17
18 #include "hks_error_code.h"
19 #include "js_native_api_types.h"
20 #include "securec.h"
21
22 #include "hks_api.h"
23 #include "hks_log.h"
24 #include "hks_mem.h"
25 #include "hks_param.h"
26 #include "hks_type.h"
27 #include "huks_napi_common_item.h"
28 #include <cstdint>
29
30 namespace HuksNapiItem {
31 namespace {
32 constexpr int HUKS_NAPI_WRAP_KEY_ARGS = 2;
33 } // namespace
34
35 struct WrapKeyAsyncContextT {
36 napi_async_work asyncWork = nullptr;
37 napi_deferred deferred = nullptr;
38 napi_ref callback = nullptr;
39
40 int32_t result = 0;
41 struct HksBlob *keyAlias = nullptr;
42 struct HksParamSet *paramSet = nullptr;
43 struct HksBlob *wrappedKey = nullptr;
44 };
45 using WrapKeyAsyncContext = WrapKeyAsyncContextT *;
46
CreateWrapKeyAsyncContext()47 static WrapKeyAsyncContext CreateWrapKeyAsyncContext()
48 {
49 WrapKeyAsyncContext context = static_cast<WrapKeyAsyncContext>(HksMalloc(sizeof(WrapKeyAsyncContextT)));
50 if (context != nullptr) {
51 (void)memset_s(context, sizeof(WrapKeyAsyncContextT), 0, sizeof(WrapKeyAsyncContextT));
52 }
53 return context;
54 }
55
DeleteWrapKeyAsyncContext(napi_env env,WrapKeyAsyncContext & context)56 static void DeleteWrapKeyAsyncContext(napi_env env, WrapKeyAsyncContext &context)
57 {
58 if (context == nullptr) {
59 return;
60 }
61
62 DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
63
64 if (context->wrappedKey != nullptr) {
65 FreeHksBlob(context->wrappedKey);
66 }
67
68 HKS_FREE(context);
69 context = nullptr;
70 }
71
WrapKeyParseParams(napi_env env,napi_callback_info info,WrapKeyAsyncContext context)72 static napi_value WrapKeyParseParams(napi_env env, napi_callback_info info, WrapKeyAsyncContext context)
73 {
74 size_t argc = HUKS_NAPI_WRAP_KEY_ARGS;
75 napi_value argv[HUKS_NAPI_WRAP_KEY_ARGS] = { 0 };
76 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
77
78 if (argc != HUKS_NAPI_WRAP_KEY_ARGS) {
79 HksNapiThrow(env, HUKS_ERR_CODE_INVALID_ARGUMENT, "no enough params input");
80 HKS_LOG_E("no enough params");
81 return nullptr;
82 }
83
84 size_t index = 0;
85 napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
86 if (result == nullptr) {
87 HksNapiThrow(env, HUKS_ERR_CODE_INVALID_ARGUMENT, "could not get paramset");
88 HKS_LOG_E("WrapKey parse params failed");
89 return nullptr;
90 }
91
92 return GetInt32(env, 0);
93 }
94
PrepareWrapKeyContextBuffer(WrapKeyAsyncContext context)95 static int32_t PrepareWrapKeyContextBuffer(WrapKeyAsyncContext context)
96 {
97 context->wrappedKey = static_cast<HksBlob *>(HksMalloc(sizeof(HksBlob)));
98 if (context->wrappedKey == nullptr) {
99 return HKS_ERROR_MALLOC_FAIL;
100 }
101
102 context->wrappedKey->data = static_cast<uint8_t *>(HksMalloc(MAX_KEY_SIZE));
103 if (context->wrappedKey->data == nullptr) {
104 return HKS_ERROR_MALLOC_FAIL;
105 }
106
107 context->wrappedKey->size = MAX_KEY_SIZE;
108 return HKS_SUCCESS;
109 }
110
WrapKeyAsyncWork(napi_env env,WrapKeyAsyncContext & context)111 static napi_value WrapKeyAsyncWork(napi_env env, WrapKeyAsyncContext &context)
112 {
113 napi_value promise = nullptr;
114 if (context->callback == nullptr) {
115 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
116 }
117
118 napi_value resourceName = nullptr;
119 napi_status napiStatus = napi_create_string_latin1(env, "WrapKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
120 if (napiStatus != napi_ok) {
121 return nullptr;
122 }
123
124 napi_create_async_work(
125 env,
126 nullptr,
127 resourceName,
128 [](napi_env env, void *data) {
129 WrapKeyAsyncContext napiContext = static_cast<WrapKeyAsyncContext>(data);
130 int32_t ret = PrepareWrapKeyContextBuffer(napiContext);
131 if (ret == HKS_SUCCESS) {
132 napiContext->result = HksWrapKey(napiContext->keyAlias, nullptr, napiContext->paramSet,
133 napiContext->wrappedKey);
134 } else {
135 napiContext->result = ret;
136 }
137 struct HksResult result = HksConvertErrCode(napiContext->result);
138 if (result.errorCode == HUKS_ERR_CODE_ILLEGAL_ARGUMENT) {
139 napiContext->result = HKS_ERROR_NEW_INVALID_ARGUMENT;
140 }
141 },
142 [](napi_env env, napi_status status, void *data) {
143 WrapKeyAsyncContext napiContext = static_cast<WrapKeyAsyncContext>(data);
144 HksSuccessReturnResult resultData;
145 SuccessReturnResultInit(resultData);
146 resultData.outData = napiContext->wrappedKey;
147 HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result,
148 resultData);
149 DeleteWrapKeyAsyncContext(env, napiContext);
150 },
151 static_cast<void *>(context),
152 &context->asyncWork);
153
154 napi_status status = napi_queue_async_work(env, context->asyncWork);
155 if (status != napi_ok) {
156 DeleteWrapKeyAsyncContext(env, context);
157 HKS_LOG_E("could not queue async work");
158 return nullptr;
159 }
160
161 return promise;
162 }
163
HuksNapiWrapKey(napi_env env,napi_callback_info info)164 napi_value HuksNapiWrapKey(napi_env env, napi_callback_info info)
165 {
166 WrapKeyAsyncContext context = CreateWrapKeyAsyncContext();
167 if (context == nullptr) {
168 HKS_LOG_E("could not create context");
169 return nullptr;
170 }
171
172 napi_value result = WrapKeyParseParams(env, info, context);
173 if (result == nullptr) {
174 HKS_LOG_E("could not parse params");
175 DeleteWrapKeyAsyncContext(env, context);
176 return nullptr;
177 }
178
179 result = WrapKeyAsyncWork(env, context);
180 if (result == nullptr) {
181 HKS_LOG_E("could not start async work");
182 DeleteWrapKeyAsyncContext(env, context);
183 return nullptr;
184 }
185 return result;
186 }
187 } // namespace HuksNapiItem
188