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 LOGE("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 LOGE("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 = GetBlobFromNapiValue(env, argv[PARAM0]);
301
302 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiRand));
303 if (status != napi_ok || napiRand == nullptr) {
304 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiRand obj!"));
305 LOGE("failed to unwrap NapiRand obj!");
306 return nullptr;
307 }
308 HcfRand *rand = napiRand->GetRand();
309 if (rand == nullptr) {
310 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get rand obj!"));
311 LOGE("fail to get rand obj!");
312 return nullptr;
313 }
314 HcfResult res = rand->setSeed(rand, seedBlob);
315 if (res != HCF_SUCCESS) {
316 napi_throw(env, GenerateBusinessError(env, res, "set seed failed."));
317 LOGE("set seed failed.");
318 return nullptr;
319 }
320 return thisVar;
321 }
322
JsGetAlgorithm(napi_env env,napi_callback_info info)323 napi_value NapiRand::JsGetAlgorithm(napi_env env, napi_callback_info info)
324 {
325 napi_value thisVar = nullptr;
326 NapiRand *napiRand = nullptr;
327
328 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
329 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiRand));
330 if (status != napi_ok || napiRand == nullptr) {
331 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiRand obj!"));
332 LOGE("failed to unwrap NapiRand obj!");
333 return nullptr;
334 }
335
336 HcfRand *rand = napiRand->GetRand();
337 if (rand == nullptr) {
338 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get rand obj!"));
339 LOGE("fail to get rand obj!");
340 return nullptr;
341 }
342
343 const char *algoName = rand->getAlgoName(rand);
344 napi_value instance = nullptr;
345 napi_create_string_utf8(env, algoName, NAPI_AUTO_LENGTH, &instance);
346 return instance;
347 }
348
RandConstructor(napi_env env,napi_callback_info info)349 napi_value NapiRand::RandConstructor(napi_env env, napi_callback_info info)
350 {
351 napi_value thisVar = nullptr;
352 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
353 return thisVar;
354 }
355
CreateRand(napi_env env,napi_callback_info info)356 napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info)
357 {
358 HcfRand *randObj = nullptr;
359 HcfResult res = HcfRandCreate(&randObj);
360 if (res != HCF_SUCCESS) {
361 napi_throw(env, GenerateBusinessError(env, res, "create C obj failed."));
362 LOGE("create c randObj failed.");
363 return nullptr;
364 }
365 napi_value instance = nullptr;
366 napi_value constructor = nullptr;
367 napi_get_reference_value(env, classRef_, &constructor);
368 napi_new_instance(env, constructor, 0, nullptr, &instance);
369 NapiRand *randNapiObj = new (std::nothrow) NapiRand(randObj);
370 if (randNapiObj == nullptr) {
371 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new rand napi obj failed."));
372 HcfObjDestroy(randObj);
373 LOGE("create rand napi obj failed");
374 return nullptr;
375 }
376 napi_status status = napi_wrap(
377 env, instance, randNapiObj,
378 [](napi_env env, void *data, void *hint) {
379 NapiRand *rand = static_cast<NapiRand *>(data);
380 delete rand;
381 return;
382 }, nullptr, nullptr);
383 if (status != napi_ok) {
384 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap NapiRand obj!"));
385 delete randNapiObj;
386 LOGE("failed to wrap NapiRand obj!");
387 return nullptr;
388 }
389 return instance;
390 }
391
DefineRandJSClass(napi_env env,napi_value exports)392 void NapiRand::DefineRandJSClass(napi_env env, napi_value exports)
393 {
394 napi_property_descriptor desc[] = {
395 DECLARE_NAPI_FUNCTION("createRandom", NapiRand::CreateRand),
396 };
397 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
398 napi_property_descriptor classDesc[] = {
399 DECLARE_NAPI_FUNCTION("generateRandom", NapiRand::JsGenerateRandom),
400 DECLARE_NAPI_FUNCTION("generateRandomSync", NapiRand::JsGenerateRandomSync),
401 DECLARE_NAPI_FUNCTION("setSeed", NapiRand::JsSetSeed),
402 {.utf8name = "algName", .getter = NapiRand::JsGetAlgorithm},
403 };
404 napi_value constructor = nullptr;
405 napi_define_class(env, "Random", NAPI_AUTO_LENGTH, RandConstructor, nullptr,
406 sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
407 napi_create_reference(env, constructor, 1, &classRef_);
408 }
409 } // CryptoFramework
410 } // OHOS
411