1 /*
2  * Copyright (c) 2021-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_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.h"
26 
27 namespace HuksNapi {
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     HKS_FREE(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, nullptr, "invalid arguments");
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 
GetKeyPropertiesWriteResult(napi_env env,GetKeyPropertiesAsyncContext context)98 static napi_value GetKeyPropertiesWriteResult(napi_env env, GetKeyPropertiesAsyncContext context)
99 {
100     return GenerateHksResult(env, context->result, nullptr, 0, *context->paramSetOut);
101 }
102 
GetKeyPropertiesAsyncWork(napi_env env,GetKeyPropertiesAsyncContext & context)103 static napi_value GetKeyPropertiesAsyncWork(napi_env env, GetKeyPropertiesAsyncContext &context)
104 {
105     napi_value promise = nullptr;
106     if (context->callback == nullptr) {
107         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
108     }
109 
110     napi_value resourceName = nullptr;
111     napi_create_string_latin1(env, "getKeyPropertiesAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
112 
113     napi_create_async_work(
114         env,
115         nullptr,
116         resourceName,
117         [](napi_env env, void *data) {
118             GetKeyPropertiesAsyncContext napiContext = static_cast<GetKeyPropertiesAsyncContext>(data);
119 
120             napiContext->paramSetOut = static_cast<struct HksParamSet *>(HksMalloc(HKS_DEFAULT_OUTPARAMSET_SIZE));
121             if (napiContext->paramSetOut != nullptr) {
122                 napiContext->paramSetOut->paramSetSize = HKS_DEFAULT_OUTPARAMSET_SIZE;
123                 napiContext->paramSetOut->paramsCnt = 0;
124             }
125 
126             napiContext->result = HksGetKeyParamSet(napiContext->keyAlias,
127                 napiContext->paramSetIn, napiContext->paramSetOut);
128         },
129         [](napi_env env, napi_status status, void *data) {
130             GetKeyPropertiesAsyncContext napiContext = static_cast<GetKeyPropertiesAsyncContext>(data);
131             napi_value result = GetKeyPropertiesWriteResult(env, napiContext);
132             if (napiContext->callback == nullptr) {
133                 napi_resolve_deferred(env, napiContext->deferred, result);
134             } else if (result != nullptr) {
135                 CallAsyncCallback(env, napiContext->callback, napiContext->result, result);
136             }
137             DeleteGetKeyPropertiesAsyncContext(env, napiContext);
138         },
139         static_cast<void *>(context),
140         &context->asyncWork);
141 
142     napi_status status = napi_queue_async_work(env, context->asyncWork);
143     if (status != napi_ok) {
144         GET_AND_THROW_LAST_ERROR((env));
145         DeleteGetKeyPropertiesAsyncContext(env, context);
146         HKS_LOG_E("could not queue async work");
147         return nullptr;
148     }
149 
150     if (context->callback == nullptr) {
151         return promise;
152     } else {
153         return GetNull(env);
154     }
155 }
156 
HuksNapiGetKeyProperties(napi_env env,napi_callback_info info)157 napi_value HuksNapiGetKeyProperties(napi_env env, napi_callback_info info)
158 {
159     GetKeyPropertiesAsyncContext context = CreateGetKeyPropertiesAsyncContext();
160     if (context == nullptr) {
161         HKS_LOG_E("could not create context");
162         return nullptr;
163     }
164 
165     napi_value result = GetKeyPropertiesParseParams(env, info, context);
166     if (result == nullptr) {
167         HKS_LOG_E("could not parse params");
168         DeleteGetKeyPropertiesAsyncContext(env, context);
169         return nullptr;
170     }
171 
172     result = GetKeyPropertiesAsyncWork(env, context);
173     if (result == nullptr) {
174         HKS_LOG_E("could not start async work");
175         DeleteGetKeyPropertiesAsyncContext(env, context);
176         return nullptr;
177     }
178     return result;
179 }
180 }  // namespace HuksNapi
181