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_generate_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_GENERATE_KEY_MIN_ARGS = 2;
29 constexpr int HUKS_NAPI_GENERATE_KEY_MAX_ARGS = 3;
30
CreateGenerateKeyAsyncContext()31 GenerateKeyAsyncContext CreateGenerateKeyAsyncContext()
32 {
33 GenerateKeyAsyncContext context = static_cast<GenerateKeyAsyncContext>(HksMalloc(sizeof(GenerateKeyAsyncContextT)));
34 if (context != nullptr) {
35 (void)memset_s(context, sizeof(GenerateKeyAsyncContextT), 0, sizeof(GenerateKeyAsyncContextT));
36 }
37 return context;
38 }
39
DeleteGenerateKeyAsyncContext(napi_env env,GenerateKeyAsyncContext & context)40 void DeleteGenerateKeyAsyncContext(napi_env env, GenerateKeyAsyncContext &context)
41 {
42 if (context == nullptr) {
43 return;
44 }
45 DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSetIn);
46 if (context->paramSetOut != nullptr) {
47 HksFreeParamSet(&context->paramSetOut);
48 }
49 HKS_FREE(context);
50 context = nullptr;
51 }
52
GenerateKeyParseParams(napi_env env,napi_callback_info info,GenerateKeyAsyncContext context)53 static napi_value GenerateKeyParseParams(napi_env env, napi_callback_info info, GenerateKeyAsyncContext context)
54 {
55 size_t argc = HUKS_NAPI_GENERATE_KEY_MAX_ARGS;
56 napi_value argv[HUKS_NAPI_GENERATE_KEY_MAX_ARGS] = { 0 };
57 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
58
59 if (argc < HUKS_NAPI_GENERATE_KEY_MIN_ARGS) {
60 HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "no enough params input");
61 HKS_LOG_E("no enough params");
62 return nullptr;
63 }
64
65 size_t index = 0;
66 napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSetIn);
67 if (result == nullptr) {
68 HKS_LOG_E("generateKey parse params failed");
69 return nullptr;
70 }
71
72 index++;
73 if (index < argc) {
74 context->callback = GetCallback(env, argv[index]);
75 }
76
77 return GetInt32(env, 0);
78 }
79
GenerateKeyAsyncWork(napi_env env,GenerateKeyAsyncContext & context)80 napi_value GenerateKeyAsyncWork(napi_env env, GenerateKeyAsyncContext &context)
81 {
82 napi_value promise = nullptr;
83 if (context->callback == nullptr) {
84 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
85 }
86
87 napi_value resourceName = nullptr;
88 napi_create_string_latin1(env, "generateKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
89
90 napi_create_async_work(
91 env,
92 nullptr,
93 resourceName,
94 [](napi_env env, void *data) {
95 GenerateKeyAsyncContext napiContext = static_cast<GenerateKeyAsyncContext>(data);
96
97 napiContext->result = HksGenerateKey(napiContext->keyAlias,
98 napiContext->paramSetIn, napiContext->paramSetOut);
99 },
100 [](napi_env env, napi_status status, void *data) {
101 GenerateKeyAsyncContext napiContext = static_cast<GenerateKeyAsyncContext>(data);
102 HksSuccessReturnResult resultData;
103 SuccessReturnResultInit(resultData);
104 HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
105 DeleteGenerateKeyAsyncContext(env, napiContext);
106 },
107 static_cast<void *>(context),
108 &context->asyncWork);
109
110 napi_status status = napi_queue_async_work(env, context->asyncWork);
111 if (status != napi_ok) {
112 DeleteGenerateKeyAsyncContext(env, context);
113 HKS_LOG_E("could not queue async work");
114 return nullptr;
115 }
116
117 if (context->callback == nullptr) {
118 return promise;
119 } else {
120 return GetNull(env);
121 }
122 }
123
HuksNapiItemGenerateKey(napi_env env,napi_callback_info info)124 napi_value HuksNapiItemGenerateKey(napi_env env, napi_callback_info info)
125 {
126 GenerateKeyAsyncContext context = CreateGenerateKeyAsyncContext();
127 if (context == nullptr) {
128 HKS_LOG_E("could not create context");
129 return nullptr;
130 }
131
132 napi_value result = GenerateKeyParseParams(env, info, context);
133 if (result == nullptr) {
134 HKS_LOG_E("could not parse params");
135 DeleteGenerateKeyAsyncContext(env, context);
136 return nullptr;
137 }
138
139 result = GenerateKeyAsyncWork(env, context);
140 if (result == nullptr) {
141 HKS_LOG_E("could not start async work");
142 DeleteGenerateKeyAsyncContext(env, context);
143 return nullptr;
144 }
145 return result;
146 }
147 } // namespace HuksNapi
148