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