• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 "napi_rand.h"
17 
18 #include "securec.h"
19 #include "log.h"
20 #include "memory.h"
21 
22 #include "napi_utils.h"
23 #include "napi_crypto_framework_defines.h"
24 
25 namespace OHOS {
26 namespace CryptoFramework {
27 thread_local napi_ref NapiRand::classRef_ = nullptr;
28 
29 struct RandCtx {
30     napi_env env = nullptr;
31 
32     AsyncType asyncType = ASYNC_CALLBACK;
33     napi_ref callback = nullptr;
34     napi_deferred deferred = nullptr;
35     napi_value promise = nullptr;
36     napi_async_work asyncWork = nullptr;
37 
38     int32_t numBytes = 0;
39     HcfBlob *seedBlob = nullptr;
40 
41     HcfResult errCode = HCF_SUCCESS;
42     const char *errMsg = nullptr;
43     HcfBlob *randBlob = nullptr;
44     HcfRand *rand = nullptr;
45 };
46 
FreeCryptoFwkCtx(napi_env env,RandCtx * context)47 static void FreeCryptoFwkCtx(napi_env env, RandCtx *context)
48 {
49     if (context == nullptr) {
50         return;
51     }
52     if (context->asyncWork != nullptr) {
53         napi_delete_async_work(env, context->asyncWork);
54         context->asyncWork = nullptr;
55     }
56 
57     if (context->callback != nullptr) {
58         napi_delete_reference(env, context->callback);
59         context->callback = nullptr;
60     }
61     if (context->seedBlob != nullptr) {
62         HcfFree(context->seedBlob->data);
63         context->seedBlob->data = nullptr;
64         context->seedBlob->len = 0;
65         HcfFree(context->seedBlob);
66         context->seedBlob = nullptr;
67     }
68     if (context->randBlob != nullptr) {
69         HcfFree(context->randBlob->data);
70         context->randBlob->data = nullptr;
71         context->randBlob->len = 0;
72         HcfFree(context->randBlob);
73         context->randBlob = nullptr;
74     }
75     context->errMsg = nullptr;
76     context->rand = nullptr;
77     HcfFree(context);
78     context = nullptr;
79 }
80 
ReturnCallbackResult(napi_env env,RandCtx * context,napi_value result)81 static void ReturnCallbackResult(napi_env env, RandCtx *context, napi_value result)
82 {
83     napi_value businessError = nullptr;
84     if (context->errCode != HCF_SUCCESS) {
85         businessError = GenerateBusinessError(env, context->errCode, context->errMsg);
86     }
87     napi_value params[ARGS_SIZE_TWO] = { businessError, result };
88 
89     napi_value func = nullptr;
90     napi_get_reference_value(env, context->callback, &func);
91 
92     napi_value recv = nullptr;
93     napi_value callFuncRet = nullptr;
94     napi_get_undefined(env, &recv);
95     napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
96 }
97 
ReturnPromiseResult(napi_env env,RandCtx * context,napi_value result)98 static void ReturnPromiseResult(napi_env env, RandCtx *context, napi_value result)
99 {
100     if (context->errCode == HCF_SUCCESS) {
101         napi_resolve_deferred(env, context->deferred, result);
102     } else {
103         napi_reject_deferred(env, context->deferred,
104             GenerateBusinessError(env, context->errCode, context->errMsg));
105     }
106 }
107 
GenerateRandomExecute(napi_env env,void * data)108 static void GenerateRandomExecute(napi_env env, void *data)
109 {
110     RandCtx *context = static_cast<RandCtx *>(data);
111     HcfRand *randObj = context->rand;
112     HcfBlob *randBlob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
113     if (randBlob == nullptr) {
114         LOGE("randBlob is null!");
115         context->errCode = HCF_ERR_MALLOC;
116         context->errMsg = "malloc data blob failed";
117         return;
118     }
119     int32_t numBytes = context->numBytes;
120     context->errCode = randObj->generateRandom(randObj, numBytes, randBlob);
121     if (context->errCode != HCF_SUCCESS) {
122         LOGD("[error] generateRandom failed!");
123         context->errMsg = "generateRandom failed";
124         HcfFree(randBlob);
125         return;
126     }
127     context->randBlob = randBlob;
128 }
129 
GenerateRandomComplete(napi_env env,napi_status status,void * data)130 static void GenerateRandomComplete(napi_env env, napi_status status, void *data)
131 {
132     RandCtx *context = static_cast<RandCtx *>(data);
133     napi_value returnRandBlob = ConvertBlobToNapiValue(env, context->randBlob);
134     if (returnRandBlob == nullptr) {
135         LOGE("returnOutBlob is nullptr!");
136         returnRandBlob = NapiGetNull(env);
137     }
138     if (context->asyncType == ASYNC_CALLBACK) {
139         ReturnCallbackResult(env, context, returnRandBlob);
140     } else {
141         ReturnPromiseResult(env, context, returnRandBlob);
142     }
143     FreeCryptoFwkCtx(env, context);
144 }
145 
BuildGenerateRandomCtx(napi_env env,napi_callback_info info,RandCtx * context)146 static bool BuildGenerateRandomCtx(napi_env env, napi_callback_info info, RandCtx *context)
147 {
148     napi_value thisVar = nullptr;
149     size_t expectedArgsCount = ARGS_SIZE_TWO;
150     size_t argc = expectedArgsCount;
151     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
152     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
153     if ((argc != expectedArgsCount) && (argc != expectedArgsCount - CALLBACK_SIZE)) {
154         LOGE("The arguments count is not expected!");
155         return false;
156     }
157 
158     if (!GetInt32FromJSParams(env, argv[PARAM0], context->numBytes)) {
159         LOGE("get numBytes failed!");
160         return false;
161     }
162     context->asyncType = isCallback(env, argv[expectedArgsCount - 1], argc, expectedArgsCount) ?
163         ASYNC_CALLBACK : ASYNC_PROMISE;
164 
165     NapiRand *napiRand = nullptr;
166     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiRand));
167     if (status != napi_ok || napiRand == nullptr) {
168         LOGE("failed to unwrap NapiRand obj!");
169         return false;
170     }
171 
172     context->rand = napiRand->GetRand();
173 
174     if (context->asyncType == ASYNC_PROMISE) {
175         napi_create_promise(env, &context->deferred, &context->promise);
176         return true;
177     } else {
178         return GetCallbackFromJSParams(env, argv[PARAM1], &context->callback);
179     }
180 }
181 
NewRandJsGenerateAsyncWork(napi_env env,RandCtx * context)182 static napi_value NewRandJsGenerateAsyncWork(napi_env env, RandCtx *context)
183 {
184     napi_create_async_work(
185         env, nullptr, GetResourceName(env, "GenerateRandom"),
186         [](napi_env env, void *data) {
187             GenerateRandomExecute(env, data);
188             return;
189         },
190         [](napi_env env, napi_status status, void *data) {
191             GenerateRandomComplete(env, status, data);
192             return;
193         },
194         static_cast<void *>(context),
195         &context->asyncWork);
196 
197     napi_queue_async_work(env, context->asyncWork);
198     if (context->asyncType == ASYNC_PROMISE) {
199         return context->promise;
200     } else {
201         return NapiGetNull(env);
202     }
203 }
204 
NapiRand(HcfRand * randObj)205 NapiRand::NapiRand(HcfRand *randObj)
206 {
207     this->randObj_ = randObj;
208 }
209 
~NapiRand()210 NapiRand::~NapiRand()
211 {
212     HcfObjDestroy(this->randObj_);
213 }
214 
GetRand()215 HcfRand *NapiRand::GetRand()
216 {
217     return this->randObj_;
218 }
219 
JsGenerateRandom(napi_env env,napi_callback_info info)220 napi_value NapiRand::JsGenerateRandom(napi_env env, napi_callback_info info)
221 {
222     RandCtx *context = static_cast<RandCtx *>(HcfMalloc(sizeof(RandCtx), 0));
223     if (context == nullptr) {
224         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed"));
225         LOGE("malloc context failed!");
226         return nullptr;
227     }
228 
229     if (!BuildGenerateRandomCtx(env, info, context)) {
230         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail."));
231         LOGE("build context fail.");
232         FreeCryptoFwkCtx(env, context);
233         return nullptr;
234     }
235 
236     return NewRandJsGenerateAsyncWork(env, context);
237 }
238 
JsGenerateRandomSync(napi_env env,napi_callback_info info)239 napi_value NapiRand::JsGenerateRandomSync(napi_env env, napi_callback_info info)
240 {
241     napi_value thisVar = nullptr;
242     NapiRand *napiRand = nullptr;
243     size_t expectedArgsCount = ARGS_SIZE_ONE;
244     size_t argc = expectedArgsCount;
245     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
246 
247     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
248     if (argc != expectedArgsCount) {
249         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
250         LOGE("The arguments count is not expected!");
251         return nullptr;
252     }
253 
254     int32_t numBytes = 0;
255     if (!GetInt32FromJSParams(env, argv[PARAM0], numBytes)) {
256         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get numBytes failed!"));
257         LOGE("get numBytes failed!");
258         return nullptr;
259     }
260     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiRand));
261     if (status != napi_ok || napiRand == nullptr) {
262         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiRand obj!"));
263         LOGE("failed to unwrap NapiRand obj!");
264         return nullptr;
265     }
266     HcfRand *rand = napiRand->GetRand();
267     if (rand == nullptr) {
268         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get rand obj!"));
269         LOGE("fail to get rand obj!");
270         return nullptr;
271     }
272 
273     HcfBlob randBlob = { .data = nullptr, .len = 0};
274     HcfResult res = rand->generateRandom(rand, numBytes, &randBlob);
275     if (res != HCF_SUCCESS) {
276         napi_throw(env, GenerateBusinessError(env, res, "generateRandom failed!"));
277         LOGD("[error] generateRandom failed!");
278         return nullptr;
279     }
280 
281     napi_value instance = ConvertBlobToNapiValue(env, &randBlob);
282     HcfBlobDataClearAndFree(&randBlob);
283     return instance;
284 }
285 
JsSetSeed(napi_env env,napi_callback_info info)286 napi_value NapiRand::JsSetSeed(napi_env env, napi_callback_info info)
287 {
288     napi_value thisVar = nullptr;
289     NapiRand *napiRand = nullptr;
290     size_t expectedArgsCount = ARGS_SIZE_ONE;
291     size_t argc = expectedArgsCount;
292     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
293 
294     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
295     if (argc != expectedArgsCount) {
296         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count"));
297         LOGE("The arguments count is not expected!");
298         return nullptr;
299     }
300     HcfBlob *seedBlob = GetBlobFromNapiDataBlob(env, argv[PARAM0]);
301     if (seedBlob == nullptr) {
302         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get seedBlob!"));
303         LOGE("failed to get seedBlob!");
304         return nullptr;
305     }
306     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiRand));
307     if (status != napi_ok || napiRand == nullptr) {
308         HcfBlobDataFree(seedBlob);
309         HcfFree(seedBlob);
310         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiRand obj!"));
311         LOGE("failed to unwrap NapiRand obj!");
312         return nullptr;
313     }
314     HcfRand *rand = napiRand->GetRand();
315     if (rand == nullptr) {
316         HcfBlobDataFree(seedBlob);
317         HcfFree(seedBlob);
318         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get rand obj!"));
319         LOGE("fail to get rand obj!");
320         return nullptr;
321     }
322     HcfResult res = rand->setSeed(rand, seedBlob);
323     if (res != HCF_SUCCESS) {
324         HcfBlobDataFree(seedBlob);
325         HcfFree(seedBlob);
326         napi_throw(env, GenerateBusinessError(env, res, "set seed failed."));
327         LOGD("[error] set seed failed.");
328         return nullptr;
329     }
330     HcfBlobDataFree(seedBlob);
331     HcfFree(seedBlob);
332     return thisVar;
333 }
334 
JsGetAlgorithm(napi_env env,napi_callback_info info)335 napi_value NapiRand::JsGetAlgorithm(napi_env env, napi_callback_info info)
336 {
337     napi_value thisVar = nullptr;
338     NapiRand *napiRand = nullptr;
339 
340     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
341     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiRand));
342     if (status != napi_ok || napiRand == nullptr) {
343         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiRand obj!"));
344         LOGE("failed to unwrap NapiRand obj!");
345         return nullptr;
346     }
347 
348     HcfRand *rand = napiRand->GetRand();
349     if (rand == nullptr) {
350         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get rand obj!"));
351         LOGE("fail to get rand obj!");
352         return nullptr;
353     }
354 
355     const char *algoName = rand->getAlgoName(rand);
356     napi_value instance = nullptr;
357     napi_create_string_utf8(env, algoName, NAPI_AUTO_LENGTH, &instance);
358     return instance;
359 }
360 
RandConstructor(napi_env env,napi_callback_info info)361 napi_value NapiRand::RandConstructor(napi_env env, napi_callback_info info)
362 {
363     napi_value thisVar = nullptr;
364     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
365     return thisVar;
366 }
367 
CreateRand(napi_env env,napi_callback_info info)368 napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info)
369 {
370     HcfRand *randObj = nullptr;
371     HcfResult res = HcfRandCreate(&randObj);
372     if (res != HCF_SUCCESS) {
373         napi_throw(env, GenerateBusinessError(env, res, "create C obj failed."));
374         LOGE("create c randObj failed.");
375         return nullptr;
376     }
377     napi_value instance = nullptr;
378     napi_value constructor = nullptr;
379     napi_get_reference_value(env, classRef_, &constructor);
380     napi_new_instance(env, constructor, 0, nullptr, &instance);
381     NapiRand *randNapiObj = new (std::nothrow) NapiRand(randObj);
382     if (randNapiObj == nullptr) {
383         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new rand napi obj failed."));
384         HcfObjDestroy(randObj);
385         LOGE("create rand napi obj failed");
386         return nullptr;
387     }
388     napi_status status = napi_wrap(
389         env, instance, randNapiObj,
390         [](napi_env env, void *data, void *hint) {
391             NapiRand *rand = static_cast<NapiRand *>(data);
392             delete rand;
393             return;
394         }, nullptr, nullptr);
395     if (status != napi_ok) {
396         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap NapiRand obj!"));
397         delete randNapiObj;
398         LOGE("failed to wrap NapiRand obj!");
399         return nullptr;
400     }
401     return instance;
402 }
403 
DefineRandJSClass(napi_env env,napi_value exports)404 void NapiRand::DefineRandJSClass(napi_env env, napi_value exports)
405 {
406     napi_property_descriptor desc[] = {
407         DECLARE_NAPI_FUNCTION("createRandom", NapiRand::CreateRand),
408     };
409     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
410     napi_property_descriptor classDesc[] = {
411         DECLARE_NAPI_FUNCTION("generateRandom", NapiRand::JsGenerateRandom),
412         DECLARE_NAPI_FUNCTION("generateRandomSync", NapiRand::JsGenerateRandomSync),
413         DECLARE_NAPI_FUNCTION("setSeed", NapiRand::JsSetSeed),
414         {.utf8name = "algName", .getter = NapiRand::JsGetAlgorithm},
415     };
416     napi_value constructor = nullptr;
417     napi_define_class(env, "Random", NAPI_AUTO_LENGTH, RandConstructor, nullptr,
418         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
419     napi_create_reference(env, constructor, 1, &classRef_);
420 }
421 } // CryptoFramework
422 } // OHOS
423