• 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_sign.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_SIGN_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_SIGN_MAX_ARGS = 3;
31 
32 constexpr int HKS_MAX_SIGNATURE_SIZE = 512;
33 }  // namespace
34 
35 struct SignAsyncContextT {
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 *paramSet = nullptr;
43     struct HksBlob *srcData = nullptr;
44     struct HksBlob *signature = nullptr;
45 };
46 using SignAsyncContext = SignAsyncContextT *;
47 
CreateSignAsyncContext()48 static SignAsyncContext CreateSignAsyncContext()
49 {
50     SignAsyncContext context = (SignAsyncContext)HksMalloc(sizeof(SignAsyncContextT));
51     if (context != nullptr) {
52         (void)memset_s(context, sizeof(SignAsyncContextT), 0, sizeof(SignAsyncContextT));
53     }
54     return context;
55 }
56 
DeleteSignAsyncContext(napi_env env,SignAsyncContext & context)57 static void DeleteSignAsyncContext(napi_env env, SignAsyncContext &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->signature != nullptr) {
89         if (context->signature->data != nullptr && context->signature->size != 0) {
90             (void)memset_s(context->signature->data, context->signature->size, 0, context->signature->size);
91         }
92         FreeHksBlob(context->signature);
93     }
94 
95     HksFree(context);
96     context = nullptr;
97 }
98 
SignParseParams(napi_env env,napi_callback_info info,SignAsyncContext context)99 static napi_value SignParseParams(napi_env env, napi_callback_info info, SignAsyncContext context)
100 {
101     size_t argc = HUKS_NAPI_SIGN_MAX_ARGS;
102     napi_value argv[HUKS_NAPI_SIGN_MAX_ARGS] = {0};
103     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
104 
105     if (argc < HUKS_NAPI_SIGN_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     napi_value inData = nullptr;
133     status = napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_INDATA.c_str(), &inData);
134     if (status != napi_ok || inData == nullptr) {
135         GET_AND_THROW_LAST_ERROR((env));
136         HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_INDATA.c_str());
137         return nullptr;
138     }
139     context->srcData = (HksBlob *)HksMalloc(sizeof(HksBlob));
140     if (context->srcData == nullptr) {
141         HKS_LOG_E("could not alloc memory");
142         return nullptr;
143     }
144     (void)memset_s(context->srcData, sizeof(HksBlob), 0, sizeof(HksBlob));
145 
146     if (GetUint8Array(env, inData, *context->srcData) == nullptr) {
147         HKS_LOG_E("could not get indata");
148         return nullptr;
149     }
150 
151     index++;
152     if (index < argc) {
153         context->callback = GetCallback(env, argv[index]);
154     }
155 
156     return GetInt32(env, 0);
157 }
158 
SignWriteResult(napi_env env,SignAsyncContext context)159 static napi_value SignWriteResult(napi_env env, SignAsyncContext context)
160 {
161     return GenerateHksResult(env,
162         context->result,
163         ((context->result == HKS_SUCCESS && context->signature != nullptr) ? context->signature->data : nullptr),
164         (context->result == HKS_SUCCESS && context->signature != nullptr) ? context->signature->size : 0);
165 }
166 
SignAsyncWork(napi_env env,SignAsyncContext context)167 static napi_value SignAsyncWork(napi_env env, SignAsyncContext context)
168 {
169     napi_value promise = nullptr;
170     if (context->callback == nullptr) {
171         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
172     }
173 
174     napi_value resourceName = nullptr;
175     napi_create_string_latin1(env, "signAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
176 
177     napi_create_async_work(
178         env,
179         nullptr,
180         resourceName,
181         [](napi_env env, void *data) {
182             SignAsyncContext context = static_cast<SignAsyncContext>(data);
183 
184             context->signature = (HksBlob *)HksMalloc(sizeof(HksBlob));
185             if (context->signature != NULL) {
186                 context->signature->data = (uint8_t *)HksMalloc(HKS_MAX_SIGNATURE_SIZE);
187                 context->signature->size = HKS_MAX_SIGNATURE_SIZE;
188             }
189 
190             context->result = HksSign(context->keyAlias, context->paramSet, context->srcData, context->signature);
191         },
192         [](napi_env env, napi_status status, void *data) {
193             SignAsyncContext context = static_cast<SignAsyncContext>(data);
194             napi_value result = SignWriteResult(env, context);
195             if (result == nullptr) {
196                 return;
197             }
198             if (context->callback != nullptr) {
199                 CallAsyncCallback(env, context->callback, context->result, result);
200             } else {
201                 napi_resolve_deferred(env, context->deferred, result);
202             }
203             DeleteSignAsyncContext(env, context);
204         },
205         (void *)context,
206         &context->asyncWork);
207 
208     napi_status status = napi_queue_async_work(env, context->asyncWork);
209     if (status != napi_ok) {
210         GET_AND_THROW_LAST_ERROR((env));
211         DeleteSignAsyncContext(env, context);
212         HKS_LOG_E("could not queue async work");
213         return nullptr;
214     }
215 
216     if (context->callback == nullptr) {
217         return promise;
218     } else {
219         return GetNull(env);
220     }
221     return nullptr;
222 }
223 
HuksNapiSign(napi_env env,napi_callback_info info)224 napi_value HuksNapiSign(napi_env env, napi_callback_info info)
225 {
226     SignAsyncContext context = CreateSignAsyncContext();
227     if (context == nullptr) {
228         HKS_LOG_E("could not create context");
229         return nullptr;
230     }
231 
232     napi_value result = SignParseParams(env, info, context);
233     if (result == nullptr) {
234         HKS_LOG_E("could not parse params");
235         DeleteSignAsyncContext(env, context);
236         return nullptr;
237     }
238 
239     result = SignAsyncWork(env, context);
240     if (result == nullptr) {
241         HKS_LOG_E("could not start async work");
242         DeleteSignAsyncContext(env, context);
243         return nullptr;
244     }
245     return result;
246 }
247 }  // namespace HuksNapi
248