• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mac.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_AGREE_KEY_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_AGREE_KEY_MAX_ARGS = 3;
31 
32 constexpr int HKS_MAX_MAC_SIZE = 512;
33 }  // namespace
34 
35 struct MacAsyncContextT {
36     napi_async_work asyncWork = nullptr;
37     napi_deferred deferred = nullptr;
38     napi_ref callback = nullptr;
39 
40     int32_t result = 0;
41     struct HksParamSet *paramSet = nullptr;
42     struct HksBlob *keyAlias = nullptr;
43     struct HksBlob *srcData = nullptr;
44     struct HksBlob *mac = nullptr;
45 };
46 using MacAsyncContext = MacAsyncContextT *;
47 
CreateMacAsyncContext()48 static MacAsyncContext CreateMacAsyncContext()
49 {
50     MacAsyncContext context = (MacAsyncContext)HksMalloc(sizeof(MacAsyncContextT));
51     if (context != nullptr) {
52         (void)memset_s(context, sizeof(MacAsyncContextT), 0, sizeof(MacAsyncContextT));
53     }
54     return context;
55 }
56 
DeleteMacAsyncContext(napi_env env,MacAsyncContext & context)57 static void DeleteMacAsyncContext(napi_env env, MacAsyncContext &context)
58 {
59     if (context == nullptr) {
60         return;
61     }
62 
63     if (context->asyncWork != nullptr) {
64         napi_delete_async_work(env, context->asyncWork);
65         context->asyncWork = nullptr;
66     }
67 
68     if (context->callback != nullptr) {
69         napi_delete_reference(env, context->callback);
70         context->callback = nullptr;
71     }
72 
73     if (context->keyAlias != nullptr) {
74         FreeHksBlob(context->keyAlias);
75     }
76 
77     if (context->paramSet != nullptr) {
78         HksFreeParamSet(&context->paramSet);
79     }
80 
81     if (context->srcData != nullptr) {
82         if (context->srcData->data != nullptr && context->srcData->size != 0) {
83             (void)memset_s(context->srcData->data, context->srcData->size, 0, context->srcData->size);
84         }
85         FreeHksBlob(context->srcData);
86     }
87 
88     if (context->mac != nullptr) {
89         if (context->mac->data != nullptr && context->mac->size != 0) {
90             (void)memset_s(context->mac->data, context->mac->size, 0, context->mac->size);
91         }
92         FreeHksBlob(context->mac);
93     }
94 
95     HksFree(context);
96     context = nullptr;
97 }
98 
MacParseParams(napi_env env,napi_callback_info info,MacAsyncContext context)99 static napi_value MacParseParams(napi_env env, napi_callback_info info, MacAsyncContext context)
100 {
101     size_t argc = HUKS_NAPI_AGREE_KEY_MAX_ARGS;
102     napi_value argv[HUKS_NAPI_AGREE_KEY_MAX_ARGS] = {0};
103     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
104 
105     if (argc < HUKS_NAPI_AGREE_KEY_MIN_ARGS) {
106         napi_throw_error(env, NULL, "invalid arguments");
107         HKS_LOG_E("no enough params");
108         return nullptr;
109     }
110 
111     size_t index = 0;
112     napi_value result = ParseKeyAlias(env, argv[index], context->keyAlias);
113     if (result == nullptr) {
114         HKS_LOG_E("could not get alias");
115         return nullptr;
116     }
117 
118     index++;
119     napi_value properties = nullptr;
120     napi_status status =
121         napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_PROPERTIES.c_str(), &properties);
122     if (status != napi_ok || properties == nullptr) {
123         GET_AND_THROW_LAST_ERROR((env));
124         HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_PROPERTIES.c_str());
125         return nullptr;
126     }
127     result = ParseHksParamSet(env, properties, context->paramSet);
128     if (result == nullptr) {
129         HKS_LOG_E("could not get paramset");
130         return nullptr;
131     }
132 
133     napi_value inData = nullptr;
134     status = napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_INDATA.c_str(), &inData);
135     if (status != napi_ok || inData == nullptr) {
136         GET_AND_THROW_LAST_ERROR((env));
137         HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_INDATA.c_str());
138         return nullptr;
139     }
140     context->srcData = (HksBlob *)HksMalloc(sizeof(HksBlob));
141     if (context->srcData == nullptr) {
142         HKS_LOG_E("could not alloc memory");
143         return nullptr;
144     }
145     (void)memset_s(context->srcData, sizeof(HksBlob), 0, sizeof(HksBlob));
146 
147     if (GetUint8Array(env, inData, *context->srcData) == nullptr) {
148         HKS_LOG_E("could not get indata");
149         return nullptr;
150     }
151 
152     index++;
153     if (index < argc) {
154         context->callback = GetCallback(env, argv[index]);
155     }
156 
157     return GetInt32(env, 0);
158 }
159 
MacWriteResult(napi_env env,MacAsyncContext context)160 static napi_value MacWriteResult(napi_env env, MacAsyncContext context)
161 {
162     return GenerateHksResult(env,
163         context->result,
164         ((context->result == HKS_SUCCESS && context->mac != nullptr) ? context->mac->data : nullptr),
165         (context->result == HKS_SUCCESS && context->mac != nullptr) ? context->mac->size : 0);
166 }
167 
MacAsyncWork(napi_env env,MacAsyncContext context)168 static napi_value MacAsyncWork(napi_env env, MacAsyncContext context)
169 {
170     napi_value promise = nullptr;
171     if (context->callback == nullptr) {
172         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
173     }
174 
175     napi_value resourceName = nullptr;
176     napi_create_string_latin1(env, "macAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
177 
178     napi_create_async_work(
179         env,
180         nullptr,
181         resourceName,
182         [](napi_env env, void *data) {
183             MacAsyncContext context = static_cast<MacAsyncContext>(data);
184 
185             context->mac = (HksBlob *)HksMalloc(sizeof(HksBlob));
186             if (context->mac != nullptr) {
187                 context->mac->data = (uint8_t *)HksMalloc(HKS_MAX_MAC_SIZE);
188                 context->mac->size = HKS_MAX_MAC_SIZE;
189             }
190 
191             context->result = HksMac(context->keyAlias, context->paramSet, context->srcData, context->mac);
192         },
193         [](napi_env env, napi_status status, void *data) {
194             MacAsyncContext context = static_cast<MacAsyncContext>(data);
195             napi_value result = MacWriteResult(env, context);
196             if (result == nullptr) {
197                 return;
198             }
199             if (context->callback != nullptr) {
200                 CallAsyncCallback(env, context->callback, context->result, result);
201             } else {
202                 napi_resolve_deferred(env, context->deferred, result);
203             }
204             DeleteMacAsyncContext(env, context);
205         },
206         (void *)context,
207         &context->asyncWork);
208 
209     napi_status status = napi_queue_async_work(env, context->asyncWork);
210     if (status != napi_ok) {
211         GET_AND_THROW_LAST_ERROR((env));
212         DeleteMacAsyncContext(env, context);
213         HKS_LOG_E("could not queue async work");
214         return nullptr;
215     }
216 
217     if (context->callback == nullptr) {
218         return promise;
219     } else {
220         return GetNull(env);
221     }
222     return nullptr;
223 }
224 
HuksNapiMac(napi_env env,napi_callback_info info)225 napi_value HuksNapiMac(napi_env env, napi_callback_info info)
226 {
227     MacAsyncContext context = CreateMacAsyncContext();
228     if (context == nullptr) {
229         HKS_LOG_E("could not create context");
230         return nullptr;
231     }
232 
233     napi_value result = MacParseParams(env, info, context);
234     if (result == nullptr) {
235         HKS_LOG_E("could not parse params");
236         DeleteMacAsyncContext(env, context);
237         return nullptr;
238     }
239 
240     result = MacAsyncWork(env, context);
241     if (result == nullptr) {
242         HKS_LOG_E("could not start async work");
243         DeleteMacAsyncContext(env, context);
244         return nullptr;
245     }
246     return result;
247 }
248 }  // namespace HuksNapi
249