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