• 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_init_session.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_INIT_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_INIT_MAX_ARGS = 3;
31 
32 constexpr int HKS_MAX_TOKEN_SIZE = 2048;
33 }  // namespace
34 
35 struct InitAsyncContext {
36     napi_async_work asyncWork = nullptr;
37     napi_deferred deferred = nullptr;
38     napi_ref callback = nullptr;
39 
40     int64_t result = 0;
41     struct HksBlob *keyAlias = nullptr;
42     struct HksParamSet *paramSet = nullptr;
43     struct HksBlob *handle = nullptr;
44     struct HksBlob *token = nullptr;
45 };
46 
47 using InitAsyncCtxPtr = InitAsyncContext *;
48 
CreateInitAsyncContext()49 static InitAsyncCtxPtr CreateInitAsyncContext()
50 {
51     InitAsyncCtxPtr context = static_cast<InitAsyncCtxPtr>(HksMalloc(sizeof(InitAsyncContext)));
52     if (context != nullptr) {
53         (void)memset_s(context, sizeof(InitAsyncContext), 0, sizeof(InitAsyncContext));
54     }
55     return context;
56 }
57 
DeleteInitAsyncContext(napi_env env,InitAsyncCtxPtr & context)58 static void DeleteInitAsyncContext(napi_env env, InitAsyncCtxPtr &context)
59 {
60     if (context == nullptr) {
61         return;
62     }
63 
64     DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
65 
66     if (context->handle != nullptr) {
67         FreeHksBlob(context->handle);
68     }
69 
70     if (context->token != nullptr) {
71         FreeHksBlob(context->token);
72     }
73 
74     HksFree(context);
75     context = nullptr;
76 }
77 
ParseInitParams(napi_env env,napi_callback_info info,InitAsyncCtxPtr context)78 static napi_value ParseInitParams(napi_env env, napi_callback_info info, InitAsyncCtxPtr context)
79 {
80     size_t argc = HUKS_NAPI_INIT_MAX_ARGS;
81     napi_value argv[HUKS_NAPI_INIT_MAX_ARGS] = { 0 };
82     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
83 
84     if (argc < HUKS_NAPI_INIT_MIN_ARGS) {
85         napi_throw_error(env, std::to_string(HUKS_ERR_CODE_ILLEGAL_ARGUMENT).c_str(), "no enough params input");
86         HKS_LOG_E("no enough params");
87         return nullptr;
88     }
89 
90     size_t index = 0;
91     napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
92     if (result == nullptr) {
93         HKS_LOG_E("init parse params failed");
94         return nullptr;
95     }
96 
97     index++;
98     if (index < argc) {
99         context->callback = GetCallback(env, argv[index]);
100     }
101 
102     return GetInt32(env, 0);
103 }
104 
InitOutParams(InitAsyncCtxPtr context)105 static int32_t InitOutParams(InitAsyncCtxPtr context)
106 {
107     /* free buffer use DeleteInitAsyncContext */
108     context->handle = static_cast<struct HksBlob *>(HksMalloc(sizeof(HksBlob)));
109     if (context->handle == nullptr) {
110         HKS_LOG_E("malloc handle failed");
111         return HKS_ERROR_MALLOC_FAIL;
112     }
113     context->handle->data = static_cast<uint8_t *>(HksMalloc(HKS_MAX_TOKEN_SIZE));
114     if (context->handle->data == nullptr) {
115         HKS_LOG_E("malloc handle data failed");
116         return HKS_ERROR_MALLOC_FAIL;
117     }
118     context->handle->size = HKS_MAX_TOKEN_SIZE;
119 
120     context->token = static_cast<struct HksBlob *>(HksMalloc(sizeof(HksBlob)));
121     if (context->token == nullptr) {
122         HKS_LOG_E("malloc token failed");
123         return HKS_ERROR_MALLOC_FAIL;
124     }
125     context->token->data = static_cast<uint8_t *>(HksMalloc(HKS_MAX_TOKEN_SIZE));
126     if (context->token->data == nullptr) {
127         HKS_LOG_E("malloc token data failed");
128         return HKS_ERROR_MALLOC_FAIL;
129     }
130     context->token->size = HKS_MAX_TOKEN_SIZE;
131     return HKS_SUCCESS;
132 }
133 
InitAsyncWork(napi_env env,InitAsyncCtxPtr context)134 static napi_value InitAsyncWork(napi_env env, InitAsyncCtxPtr context)
135 {
136     napi_value promise = nullptr;
137     if (context->callback == nullptr) {
138         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
139     }
140 
141     napi_value resourceName;
142     napi_create_string_latin1(env, "InitAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
143 
144     napi_create_async_work(
145         env,
146         nullptr,
147         resourceName,
148         [](napi_env env, void *data) {
149             (void)env;
150             InitAsyncCtxPtr napiContext = static_cast<InitAsyncCtxPtr>(data);
151             int32_t ret = InitOutParams(napiContext);
152             if (ret != HKS_SUCCESS) {
153                 napiContext->result = ret;
154                 return;
155             }
156             napiContext->result = HksInit(napiContext->keyAlias, napiContext->paramSet,
157                 napiContext->handle, napiContext->token);
158         },
159         [](napi_env env, napi_status status, void *data) {
160             InitAsyncCtxPtr napiContext = static_cast<InitAsyncCtxPtr>(data);
161             HksSuccessReturnResult resultData;
162             SuccessReturnResultInit(resultData);
163             resultData.handle = napiContext->handle;
164             resultData.challenge = napiContext->token;
165             HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
166             DeleteInitAsyncContext(env, napiContext);
167         },
168         static_cast<void *>(context),
169         &context->asyncWork);
170 
171     napi_status status = napi_queue_async_work(env, context->asyncWork);
172     if (status != napi_ok) {
173         DeleteInitAsyncContext(env, context);
174         HKS_LOG_E("could not queue async work");
175         return nullptr;
176     }
177 
178     if (context->callback == nullptr) {
179         return promise;
180     }
181     return GetNull(env);
182 }
183 
HuksNapiInitSession(napi_env env,napi_callback_info info)184 napi_value HuksNapiInitSession(napi_env env, napi_callback_info info)
185 {
186     InitAsyncCtxPtr context = CreateInitAsyncContext();
187     if (context == nullptr) {
188         HKS_LOG_E("could not create context");
189         return nullptr;
190     }
191 
192     napi_value result = ParseInitParams(env, info, context);
193     if (result == nullptr) {
194         HKS_LOG_E("could not parse params");
195         DeleteInitAsyncContext(env, context);
196         return nullptr;
197     }
198 
199     result = InitAsyncWork(env, context);
200     if (result == nullptr) {
201         HKS_LOG_E("could not start async work");
202         DeleteInitAsyncContext(env, context);
203         return nullptr;
204     }
205     return result;
206 }
207 }  // namespace HuksNapiItem
208