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