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