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