• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_unwrap_key.h"
17 
18 #include "hks_errcode_adapter.h"
19 #include "hks_template.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 
29 namespace HuksNapiItem {
30 namespace {
31 constexpr int HUKS_NAPI_UNWRAP_KEY_ARGS = 3;
32 }  // namespace
33 
34 struct UnwrapKeyAsyncContextT {
35     napi_async_work asyncWork = nullptr;
36     napi_deferred deferred = nullptr;
37     napi_ref callback = nullptr;
38 
39     int32_t result = 0;
40     struct HksBlob *keyAlias = nullptr;
41     struct HksParamSet *paramSet = nullptr;
42     struct HksBlob *wrappedKey = nullptr;
43 };
44 using UnwrapKeyAsyncContext = UnwrapKeyAsyncContextT *;
45 
CreateUnwrapKeyAsyncContext()46 static UnwrapKeyAsyncContext CreateUnwrapKeyAsyncContext()
47 {
48     UnwrapKeyAsyncContext context = static_cast<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     DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
62 
63     if (context->wrappedKey != nullptr) {
64         FreeHksBlob(context->wrappedKey);
65     }
66 
67     HKS_FREE(context);
68     context = nullptr;
69 }
70 
UnwrapKeyParseParams(napi_env env,napi_callback_info info,UnwrapKeyAsyncContext context)71 static napi_value UnwrapKeyParseParams(napi_env env, napi_callback_info info, UnwrapKeyAsyncContext context)
72 {
73     size_t argc = HUKS_NAPI_UNWRAP_KEY_ARGS;
74     napi_value argv[HUKS_NAPI_UNWRAP_KEY_ARGS] = { 0 };
75     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
76 
77     if (argc != HUKS_NAPI_UNWRAP_KEY_ARGS) {
78         HksNapiThrow(env, HUKS_ERR_CODE_INVALID_ARGUMENT, "no enough params input");
79         HKS_LOG_E("no enough params");
80         return nullptr;
81     }
82 
83     size_t index = 0;
84     napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
85     if (result == nullptr) {
86         HksNapiThrow(env, HUKS_ERR_CODE_INVALID_ARGUMENT, "could not get paramset");
87         HKS_LOG_E("UnwrapKey parse params failed");
88         return nullptr;
89     }
90 
91     index++;
92     context->wrappedKey = static_cast<HksBlob *>(HksMalloc(sizeof(HksBlob)));
93     if (context->wrappedKey == nullptr) {
94         HKS_LOG_E("malloc context wrappedKey failed");
95         return nullptr;
96     }
97 
98     result = GetUint8Array(env, argv[index], *context->wrappedKey);
99     if (result == nullptr) {
100         HKS_LOG_E("importWrappedKey parse keyData failed");
101         return nullptr;
102     }
103 
104     return GetInt32(env, 0);
105 }
106 
UnwrapKeyAsyncWork(napi_env env,UnwrapKeyAsyncContext & context)107 static napi_value UnwrapKeyAsyncWork(napi_env env, UnwrapKeyAsyncContext &context)
108 {
109     napi_value promise = nullptr;
110     if (context->callback == nullptr) {
111         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
112     }
113 
114     napi_value resourceName = nullptr;
115     napi_create_string_latin1(env, "UnwrapKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
116 
117     napi_create_async_work(
118         env,
119         nullptr,
120         resourceName,
121         [](napi_env env, void *data) {
122             UnwrapKeyAsyncContext napiContext = static_cast<UnwrapKeyAsyncContext>(data);
123             napiContext->result = HksUnwrapKey(napiContext->keyAlias, nullptr, napiContext->wrappedKey,
124                 napiContext->paramSet);
125             struct HksResult result = HksConvertErrCode(napiContext->result);
126             if (result.errorCode == HUKS_ERR_CODE_ILLEGAL_ARGUMENT) {
127                 napiContext->result = HKS_ERROR_NEW_INVALID_ARGUMENT;
128             }
129         },
130         [](napi_env env, napi_status status, void *data) {
131             UnwrapKeyAsyncContext napiContext = static_cast<UnwrapKeyAsyncContext>(data);
132             HksSuccessReturnResult resultData;
133             SuccessReturnResultInit(resultData);
134             HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result,
135                 resultData);
136             DeleteUnwrapKeyAsyncContext(env, napiContext);
137         },
138         static_cast<void *>(context),
139         &context->asyncWork);
140 
141     napi_status status = napi_queue_async_work(env, context->asyncWork);
142     if (status != napi_ok) {
143         DeleteUnwrapKeyAsyncContext(env, context);
144         HKS_LOG_E("could not queue async work");
145         return nullptr;
146     }
147 
148     return promise;
149 }
150 
HuksNapiUnwrapKey(napi_env env,napi_callback_info info)151 napi_value HuksNapiUnwrapKey(napi_env env, napi_callback_info info)
152 {
153     UnwrapKeyAsyncContext context = CreateUnwrapKeyAsyncContext();
154     if (context == nullptr) {
155         HKS_LOG_E("could not create context");
156         return nullptr;
157     }
158 
159     napi_value result = UnwrapKeyParseParams(env, info, context);
160     if (result == nullptr) {
161         HKS_LOG_E("could not parse params");
162         DeleteUnwrapKeyAsyncContext(env, context);
163         return nullptr;
164     }
165 
166     result = UnwrapKeyAsyncWork(env, context);
167     if (result == nullptr) {
168         HKS_LOG_E("could not start async work");
169         DeleteUnwrapKeyAsyncContext(env, context);
170         return nullptr;
171     }
172     return result;
173 }
174 }  // namespace HuksNapiItem
175 
176