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