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