1 /*
2  * Copyright (c) 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_item.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_item.h"
26 
27 namespace HuksNapiItem {
28 constexpr int HUKS_NAPI_IMPORT_KEY_MIN_ARGS = 2;
29 constexpr int HUKS_NAPI_IMPORT_KEY_MAX_ARGS = 3;
CreateImportKeyAsyncContext()30 ImportKeyAsyncContext CreateImportKeyAsyncContext()
31 {
32     ImportKeyAsyncContext context = static_cast<ImportKeyAsyncContext>(HksMalloc(sizeof(ImportKeyAsyncContextT)));
33     if (context != nullptr) {
34         (void)memset_s(context, sizeof(ImportKeyAsyncContextT), 0, sizeof(ImportKeyAsyncContextT));
35     }
36     return context;
37 }
38 
DeleteImportKeyAsyncContext(napi_env env,ImportKeyAsyncContext & context)39 void DeleteImportKeyAsyncContext(napi_env env, ImportKeyAsyncContext &context)
40 {
41     if (context == nullptr) {
42         return;
43     }
44     DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
45     if (context->key != nullptr) {
46         if (context->key->data != nullptr && context->key->size != 0) {
47             (void)memset_s(context->key->data, context->key->size, 0, context->key->size);
48         }
49         FreeHksBlob(context->key);
50     }
51     HKS_FREE(context);
52     context = nullptr;
53 }
54 
ImportKeyParseParams(napi_env env,napi_callback_info info,ImportKeyAsyncContext context)55 static napi_value ImportKeyParseParams(napi_env env, napi_callback_info info, ImportKeyAsyncContext context)
56 {
57     size_t argc = HUKS_NAPI_IMPORT_KEY_MAX_ARGS;
58     napi_value argv[HUKS_NAPI_IMPORT_KEY_MAX_ARGS] = { 0 };
59     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
60 
61     if (argc < HUKS_NAPI_IMPORT_KEY_MIN_ARGS) {
62         HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "no enough params input");
63         HKS_LOG_E("no enough params");
64         return nullptr;
65     }
66 
67     size_t index = 0;
68     napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
69     if (result == nullptr) {
70         HKS_LOG_E("importKey parse keyAlias or paramSet failed");
71         return nullptr;
72     }
73 
74     result = ParseKeyData(env, argv[index], context->key);
75     if (result == nullptr) {
76         HKS_LOG_E("importKey parse keyData failed");
77         return nullptr;
78     }
79 
80     index++;
81     if (index < argc) {
82         context->callback = GetCallback(env, argv[index]);
83     }
84 
85     return GetInt32(env, 0);
86 }
87 
ImportKeyAsyncWork(napi_env env,ImportKeyAsyncContext & context)88 napi_value ImportKeyAsyncWork(napi_env env, ImportKeyAsyncContext &context)
89 {
90     napi_value promise = nullptr;
91     if (context->callback == nullptr) {
92         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
93     }
94 
95     napi_value resourceName = nullptr;
96     napi_create_string_latin1(env, "importKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
97 
98     napi_create_async_work(
99         env,
100         nullptr,
101         resourceName,
102         [](napi_env env, void *data) {
103             ImportKeyAsyncContext napiContext = static_cast<ImportKeyAsyncContext>(data);
104 
105             napiContext->result = HksImportKey(napiContext->keyAlias, napiContext->paramSet, napiContext->key);
106         },
107         [](napi_env env, napi_status status, void *data) {
108             ImportKeyAsyncContext napiContext = static_cast<ImportKeyAsyncContext>(data);
109             HksSuccessReturnResult resultData;
110             SuccessReturnResultInit(resultData);
111             HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
112             DeleteImportKeyAsyncContext(env, napiContext);
113         },
114         static_cast<void *>(context),
115         &context->asyncWork);
116 
117     napi_status status = napi_queue_async_work(env, context->asyncWork);
118     if (status != napi_ok) {
119         DeleteImportKeyAsyncContext(env, context);
120         HKS_LOG_E("could not queue async work");
121         return nullptr;
122     }
123 
124     if (context->callback == nullptr) {
125         return promise;
126     } else {
127         return GetNull(env);
128     }
129 }
130 
HuksNapiImportKeyItem(napi_env env,napi_callback_info info)131 napi_value HuksNapiImportKeyItem(napi_env env, napi_callback_info info)
132 {
133     ImportKeyAsyncContext context = CreateImportKeyAsyncContext();
134     if (context == nullptr) {
135         HKS_LOG_E("could not create context");
136         return nullptr;
137     }
138 
139     napi_value result = ImportKeyParseParams(env, info, context);
140     if (result == nullptr) {
141         HKS_LOG_E("could not parse params");
142         DeleteImportKeyAsyncContext(env, context);
143         return nullptr;
144     }
145 
146     result = ImportKeyAsyncWork(env, context);
147     if (result == nullptr) {
148         HKS_LOG_E("could not start async work");
149         DeleteImportKeyAsyncContext(env, context);
150         return nullptr;
151     }
152     return result;
153 }
154 }  // namespace HuksNapiItem
155