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_utils.h"
17 #include "params_parser.h"
18 #include "log.h"
19 #include "memory.h"
20 #include "securec.h"
21 #include "napi_crypto_framework_defines.h"
22 #include "detailed_iv_params.h"
23 #include "detailed_gcm_params.h"
24 #include "detailed_ccm_params.h"
25 #include "detailed_dsa_key_params.h"
26 #include "detailed_ecc_key_params.h"
27 #include "detailed_rsa_key_params.h"
28 #include "detailed_alg_25519_key_params.h"
29 #include "detailed_dh_key_params.h"
30 #include "utils.h"
31 #include "pri_key.h"
32 #include "asy_key_generator.h"
33
34 namespace OHOS {
35 namespace CryptoFramework {
36 using namespace std;
37
38 constexpr int PASSWORD_MAX_LENGTH = 4096;
39 struct AsyKeySpecItemRelationT {
40 AsyKeySpecItem item;
41 int32_t itemType;
42 };
43 using AsyKeySpecItemRelation = AsyKeySpecItemRelationT;
44
45 static const AsyKeySpecItemRelation ASY_KEY_SPEC_RELATION_SET[] = {
46 { DSA_P_BN, SPEC_ITEM_TYPE_BIG_INT },
47 { DSA_Q_BN, SPEC_ITEM_TYPE_BIG_INT },
48 { DSA_G_BN, SPEC_ITEM_TYPE_BIG_INT },
49 { DSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
50 { DSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
51
52 { ECC_FP_P_BN, SPEC_ITEM_TYPE_BIG_INT },
53 { ECC_A_BN, SPEC_ITEM_TYPE_BIG_INT },
54 { ECC_B_BN, SPEC_ITEM_TYPE_BIG_INT },
55 { ECC_G_X_BN, SPEC_ITEM_TYPE_BIG_INT },
56 { ECC_G_Y_BN, SPEC_ITEM_TYPE_BIG_INT },
57 { ECC_N_BN, SPEC_ITEM_TYPE_BIG_INT },
58 { ECC_H_INT, SPEC_ITEM_TYPE_NUM }, // warning: ECC_H_NUM in JS
59 { ECC_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
60 { ECC_PK_X_BN, SPEC_ITEM_TYPE_BIG_INT },
61 { ECC_PK_Y_BN, SPEC_ITEM_TYPE_BIG_INT },
62 { ECC_FIELD_TYPE_STR, SPEC_ITEM_TYPE_STR },
63 { ECC_FIELD_SIZE_INT, SPEC_ITEM_TYPE_NUM }, // warning: ECC_FIELD_SIZE_NUM in JS
64 { ECC_CURVE_NAME_STR, SPEC_ITEM_TYPE_STR },
65
66 { RSA_N_BN, SPEC_ITEM_TYPE_BIG_INT },
67 { RSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
68 { RSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
69 { DH_P_BN, SPEC_ITEM_TYPE_BIG_INT },
70 { DH_G_BN, SPEC_ITEM_TYPE_BIG_INT },
71 { DH_L_NUM, SPEC_ITEM_TYPE_NUM },
72 { DH_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
73 { DH_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
74 { ED25519_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
75 { ED25519_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
76 { X25519_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
77 { X25519_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
78 };
79
GetAsyKeySpecType(AsyKeySpecItem targetItemType)80 int32_t GetAsyKeySpecType(AsyKeySpecItem targetItemType)
81 {
82 for (uint32_t i = 0; i < sizeof(ASY_KEY_SPEC_RELATION_SET) / sizeof(AsyKeySpecItemRelation); i++) {
83 if (ASY_KEY_SPEC_RELATION_SET[i].item == targetItemType) {
84 return ASY_KEY_SPEC_RELATION_SET[i].itemType;
85 }
86 }
87 LOGE("AsyKeySpecItem not support! ItemType: %{public}d", targetItemType);
88 return -1;
89 }
90
GetSignSpecType(SignSpecItem targetItemType)91 int32_t GetSignSpecType(SignSpecItem targetItemType)
92 {
93 if (targetItemType == PSS_MD_NAME_STR || targetItemType == PSS_MGF_NAME_STR ||
94 targetItemType == PSS_MGF1_MD_STR) {
95 return SPEC_ITEM_TYPE_STR;
96 }
97 if (targetItemType == SM2_USER_ID_UINT8ARR) {
98 return SPEC_ITEM_TYPE_UINT8ARR;
99 }
100 if (targetItemType == PSS_SALT_LEN_INT || targetItemType == PSS_TRAILER_FIELD_INT) {
101 return SPEC_ITEM_TYPE_NUM;
102 }
103 LOGE("SignSpecItem not support! ItemType: %{public}d", targetItemType);
104 return -1;
105 }
106
GetCipherSpecType(CipherSpecItem targetItemType)107 int32_t GetCipherSpecType(CipherSpecItem targetItemType)
108 {
109 if (targetItemType == OAEP_MD_NAME_STR || targetItemType == OAEP_MGF_NAME_STR ||
110 targetItemType == OAEP_MGF1_MD_STR || targetItemType == SM2_MD_NAME_STR) {
111 return SPEC_ITEM_TYPE_STR;
112 }
113 if (targetItemType == OAEP_MGF1_PSRC_UINT8ARR) {
114 return SPEC_ITEM_TYPE_UINT8ARR;
115 }
116 LOGE("CipherSpecItem not support! ItemType: %{public}d", targetItemType);
117 return -1;
118 }
119
NapiGetNull(napi_env env)120 napi_value NapiGetNull(napi_env env)
121 {
122 napi_value result = nullptr;
123 napi_get_null(env, &result);
124 return result;
125 }
126
GetUint8ArrFromNapiDataBlob(napi_env env,napi_value arg)127 static napi_value GetUint8ArrFromNapiDataBlob(napi_env env, napi_value arg)
128 {
129 if ((env == nullptr) || (arg == nullptr)) {
130 LOGE("Invalid params!");
131 return nullptr;
132 }
133 napi_value data = nullptr;
134 napi_valuetype valueType = napi_undefined;
135 napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_DATA.c_str(), &data);
136 napi_typeof(env, data, &valueType);
137 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
138 LOGE("failed to get valid data property!");
139 return nullptr;
140 }
141 return data;
142 }
143
GetBlobFromNapiUint8Arr(napi_env env,napi_value data)144 HcfBlob *GetBlobFromNapiUint8Arr(napi_env env, napi_value data)
145 {
146 size_t length = 0;
147 size_t offset = 0;
148 void *rawData = nullptr;
149 napi_value arrayBuffer = nullptr;
150 napi_typedarray_type arrayType;
151 // Warning: Do not release the rawData returned by this interface because the rawData is managed by VM.
152 napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
153 reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
154 if ((status != napi_ok)) {
155 LOGE("failed to get valid rawData.");
156 return nullptr;
157 }
158 if (arrayType != napi_uint8_array) {
159 LOGE("input data is not uint8 array.");
160 return nullptr;
161 }
162
163 HcfBlob *newBlob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
164 if (newBlob == nullptr) {
165 LOGE("Failed to allocate newBlob memory!");
166 return nullptr;
167 }
168
169 // input empty uint8Arr, ex: new Uint8Arr(), the length is 0 and rawData is nullptr;
170 if ((length == 0) || (rawData == nullptr)) {
171 newBlob->len = 0;
172 newBlob->data = nullptr;
173 LOGD("napi Uint8Arr is null");
174 return newBlob;
175 }
176 newBlob->len = length;
177 newBlob->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
178 if (newBlob->data == nullptr) {
179 LOGE("malloc blob data failed!");
180 HcfFree(newBlob);
181 newBlob = nullptr;
182 return nullptr;
183 }
184 (void)memcpy_s(newBlob->data, length, rawData, length);
185 return newBlob;
186 }
187
GetBlobFromNapiDataBlob(napi_env env,napi_value arg)188 HcfBlob *GetBlobFromNapiDataBlob(napi_env env, napi_value arg)
189 {
190 napi_value data = GetUint8ArrFromNapiDataBlob(env, arg);
191 if (data == nullptr) {
192 LOGE("failed to get data in DataBlob");
193 return nullptr;
194 }
195 return GetBlobFromNapiUint8Arr(env, data);
196 }
197
GetNapiUint8ArrayDataNoCopy(napi_env env,napi_value arg,HcfBlob * blob)198 HcfResult GetNapiUint8ArrayDataNoCopy(napi_env env, napi_value arg, HcfBlob *blob)
199 {
200 napi_value data = GetUint8ArrFromNapiDataBlob(env, arg);
201 if (data == nullptr) {
202 LOGE("failed to get data in DataBlob");
203 return HCF_INVALID_PARAMS;
204 }
205
206 void *rawData = nullptr;
207 size_t len = 0;
208 napi_typedarray_type arrayType;
209 napi_status status = napi_get_typedarray_info(env, data, &arrayType, &len, &rawData, nullptr, nullptr);
210 if (status != napi_ok) {
211 LOGE("failed to get valid rawData.");
212 return HCF_ERR_NAPI;
213 }
214 if (arrayType != napi_uint8_array) {
215 LOGE("input data is not uint8 array.");
216 return HCF_INVALID_PARAMS;
217 }
218
219 blob->data = reinterpret_cast<uint8_t *>(rawData);
220 blob->len = len;
221 return HCF_SUCCESS;
222 }
223
GetBlobFromNapiValue(napi_env env,napi_value arg,HcfBlob * blob)224 HcfResult GetBlobFromNapiValue(napi_env env, napi_value arg, HcfBlob *blob)
225 {
226 napi_value data = GetUint8ArrFromNapiDataBlob(env, arg);
227 if (data == nullptr) {
228 LOGE("failed to get data in DataBlob");
229 return HCF_INVALID_PARAMS;
230 }
231
232 void *rawData = nullptr;
233 napi_typedarray_type arrayType;
234 napi_status status = napi_get_typedarray_info(env, data, &arrayType, &(blob->len),
235 reinterpret_cast<void **>(&rawData), nullptr, nullptr);
236 if (status != napi_ok) {
237 LOGE("failed to get valid rawData.");
238 return HCF_ERR_NAPI;
239 }
240 if (arrayType != napi_uint8_array) {
241 LOGE("input data is not uint8 array.");
242 return HCF_INVALID_PARAMS;
243 }
244
245 blob->data = nullptr;
246 if (blob->len == 0 || rawData == nullptr) {
247 LOGD("napi Uint8Arr is null");
248 return HCF_SUCCESS;
249 }
250
251 blob->data = static_cast<uint8_t *>(HcfMalloc(blob->len, 0));
252 if (blob->data == nullptr) {
253 LOGE("malloc blob data failed!");
254 return HCF_ERR_MALLOC;
255 }
256 (void)memcpy_s(blob->data, blob->len, rawData, blob->len);
257 return HCF_SUCCESS;
258 }
259
GetAadFromParamsSpec(napi_env env,napi_value arg)260 static HcfBlob *GetAadFromParamsSpec(napi_env env, napi_value arg)
261 {
262 napi_value data = nullptr;
263 HcfBlob *blob = nullptr;
264 napi_valuetype valueType = napi_undefined;
265
266 napi_status status = napi_get_named_property(env, arg, AAD_PARAMS.c_str(), &data);
267 napi_typeof(env, data, &valueType);
268 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
269 LOGE("failed to get valid param property!");
270 return nullptr;
271 }
272 if (valueType == napi_null) {
273 blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
274 if (blob == nullptr) {
275 LOGE("Failed to allocate newBlob memory!");
276 return nullptr;
277 }
278 return blob;
279 }
280 blob = GetBlobFromNapiDataBlob(env, data);
281 if (blob == nullptr) {
282 LOGE("GetBlobFromNapiDataBlob failed!");
283 return nullptr;
284 }
285 return blob;
286 }
287
GetBigIntFromNapiValue(napi_env env,napi_value arg,HcfBigInteger * bigInt)288 bool GetBigIntFromNapiValue(napi_env env, napi_value arg, HcfBigInteger *bigInt)
289 {
290 if ((env == nullptr) || (arg == nullptr) || (bigInt == nullptr)) {
291 LOGE("Invalid params!");
292 return false;
293 }
294
295 int signBit;
296 size_t wordCount;
297
298 napi_get_value_bigint_words(env, arg, nullptr, &wordCount, nullptr);
299 if ((wordCount == 0) || (wordCount > (INT_MAX / sizeof(uint64_t)))) {
300 LOGE("Get big int failed.");
301 return false;
302 }
303 int length = wordCount * sizeof(uint64_t);
304 uint8_t *retArr = reinterpret_cast<uint8_t *>(HcfMalloc(length, 0));
305 if (retArr == nullptr) {
306 LOGE("malloc blob data failed!");
307 return false;
308 }
309 if (napi_get_value_bigint_words(env, arg, &signBit, &wordCount, reinterpret_cast<uint64_t *>(retArr)) != napi_ok) {
310 HcfFree(retArr);
311 retArr = nullptr;
312 LOGE("failed to get valid rawData.");
313 return false;
314 }
315 if (signBit != 0) {
316 HcfFree(retArr);
317 retArr = nullptr;
318 LOGE("failed to get gegative rawData.");
319 return false;
320 }
321 bigInt->data = retArr;
322 bigInt->len = length;
323 return true;
324 }
325
GetPointFromNapiValue(napi_env env,napi_value arg,HcfPoint * point)326 bool GetPointFromNapiValue(napi_env env, napi_value arg, HcfPoint *point)
327 {
328 if ((env == nullptr) || (arg == nullptr) || (point == nullptr)) {
329 LOGE("Invalid params!");
330 return false;
331 }
332 napi_value dataX = nullptr;
333 napi_value dataY = nullptr;
334 napi_valuetype valueType = napi_undefined;
335 napi_status status = napi_get_named_property(env, arg, "x", &dataX);
336 napi_typeof(env, dataX, &valueType);
337 if ((status != napi_ok) || (dataX == nullptr) || (valueType == napi_undefined)) {
338 LOGE("failed to get valid algo name!");
339 return false;
340 }
341 status = napi_get_named_property(env, arg, "y", &dataY);
342 napi_typeof(env, dataY, &valueType);
343 if ((status != napi_ok) || (dataY == nullptr) || (valueType == napi_undefined)) {
344 LOGE("failed to get valid algo name!");
345 return false;
346 }
347
348 bool ret = GetBigIntFromNapiValue(env, dataX, &point->x);
349 if (!ret) {
350 LOGE("get point x failed!");
351 return false;
352 }
353 ret = GetBigIntFromNapiValue(env, dataY, &point->y);
354 if (!ret) {
355 LOGE("get point y failed!");
356 HcfFree((point->x).data);
357 (point->x).data = nullptr;
358 return false;
359 }
360 return true;
361 }
362
GetIvParamsSpecType()363 static const char *GetIvParamsSpecType()
364 {
365 return IV_PARAMS_SPEC.c_str();
366 }
367
GetGcmParamsSpecType()368 static const char *GetGcmParamsSpecType()
369 {
370 return GCM_PARAMS_SPEC.c_str();
371 }
372
GetCcmParamsSpecType()373 static const char *GetCcmParamsSpecType()
374 {
375 return CCM_PARAMS_SPEC.c_str();
376 }
377
GetBlobFromParamsSpec(napi_env env,napi_value arg,const string & type)378 static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, const string &type)
379 {
380 napi_value data = nullptr;
381 HcfBlob *blob = nullptr;
382 napi_valuetype valueType = napi_undefined;
383
384 napi_status status = napi_get_named_property(env, arg, type.c_str(), &data);
385 napi_typeof(env, data, &valueType);
386 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
387 LOGE("failed to get valid param property!");
388 return nullptr;
389 }
390 blob = GetBlobFromNapiDataBlob(env, data);
391 if (blob == nullptr) {
392 LOGE("GetBlobFromNapiDataBlob failed!");
393 return nullptr;
394 }
395 return blob;
396 }
397
GetIvParamsSpec(napi_env env,napi_value arg,HcfParamsSpec ** paramsSpec)398 static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **paramsSpec)
399 {
400 HcfIvParamsSpec *ivParamsSpec = reinterpret_cast<HcfIvParamsSpec *>(HcfMalloc(sizeof(HcfIvParamsSpec), 0));
401 if (ivParamsSpec == nullptr) {
402 LOGE("ivParamsSpec malloc failed!");
403 return false;
404 }
405
406 HcfBlob *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
407 if (iv == nullptr) {
408 LOGE("GetBlobFromNapiDataBlob failed!");
409 HcfFree(ivParamsSpec);
410 ivParamsSpec = nullptr;
411 return false;
412 }
413 ivParamsSpec->base.getType = GetIvParamsSpecType;
414 ivParamsSpec->iv = *iv;
415 *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ivParamsSpec);
416 HcfFree(iv);
417 iv = nullptr;
418 return true;
419 }
420
GetIvAndAadBlob(napi_env env,napi_value arg,HcfBlob ** iv,HcfBlob ** aad)421 static bool GetIvAndAadBlob(napi_env env, napi_value arg, HcfBlob **iv, HcfBlob **aad)
422 {
423 *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
424 if (*iv == nullptr) {
425 LOGE("get iv failed!");
426 return false;
427 }
428
429 *aad = GetAadFromParamsSpec(env, arg);
430 // error case free is in get paramspec func.
431 if (*aad == nullptr) {
432 LOGE("get aad failed!");
433 return false;
434 }
435 return true;
436 }
437
GetGcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)438 static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
439 {
440 HcfBlob *iv = nullptr;
441 HcfBlob *aad = nullptr;
442 HcfBlob *tag = nullptr;
443 HcfBlob authTag = {};
444 bool ret = false;
445
446 HcfGcmParamsSpec *gcmParamsSpec = reinterpret_cast<HcfGcmParamsSpec *>(HcfMalloc(sizeof(HcfGcmParamsSpec), 0));
447 if (gcmParamsSpec == nullptr) {
448 LOGE("gcmParamsSpec malloc failed!");
449 return false;
450 }
451
452 if (!GetIvAndAadBlob(env, arg, &iv, &aad)) {
453 LOGE("GetIvAndAadBlob failed!");
454 goto clearup;
455 }
456
457 if (opMode == DECRYPT_MODE) {
458 tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
459 if (tag == nullptr) {
460 LOGE("get tag failed!");
461 goto clearup;
462 }
463 } else if (opMode == ENCRYPT_MODE) {
464 authTag.data = static_cast<uint8_t *>(HcfMalloc(GCM_AUTH_TAG_LEN, 0));
465 if (authTag.data == nullptr) {
466 LOGE("get tag failed!");
467 goto clearup;
468 }
469 authTag.len = GCM_AUTH_TAG_LEN;
470 } else {
471 goto clearup;
472 }
473
474 gcmParamsSpec->base.getType = GetGcmParamsSpecType;
475 gcmParamsSpec->iv = *iv;
476 gcmParamsSpec->aad = *aad;
477 gcmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
478 *paramsSpec = reinterpret_cast<HcfParamsSpec *>(gcmParamsSpec);
479 ret = true;
480 clearup:
481 if (!ret) {
482 HcfBlobDataFree(iv);
483 HcfBlobDataFree(aad);
484 HcfBlobDataFree(tag);
485 HCF_FREE_PTR(gcmParamsSpec);
486 }
487 HCF_FREE_PTR(iv);
488 HCF_FREE_PTR(aad);
489 HCF_FREE_PTR(tag);
490 return ret;
491 }
492
GetCcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)493 static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
494 {
495 HcfBlob *iv = nullptr;
496 HcfBlob *aad = nullptr;
497 HcfBlob *tag = nullptr;
498 HcfBlob authTag = {};
499 bool ret = false;
500
501 HcfCcmParamsSpec *ccmParamsSpec = reinterpret_cast<HcfCcmParamsSpec *>(HcfMalloc(sizeof(HcfCcmParamsSpec), 0));
502 if (ccmParamsSpec == nullptr) {
503 LOGE("ccmParamsSpec malloc failed!");
504 return ret;
505 }
506
507 if (!GetIvAndAadBlob(env, arg, &iv, &aad)) {
508 LOGE("GetIvAndAadBlob failed!");
509 goto clearup;
510 }
511
512 if (opMode == DECRYPT_MODE) {
513 tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
514 if (tag == nullptr) {
515 LOGE("get tag failed!");
516 goto clearup;
517 }
518 } else if (opMode == ENCRYPT_MODE) {
519 authTag.data = static_cast<uint8_t *>(HcfMalloc(CCM_AUTH_TAG_LEN, 0));
520 if (authTag.data == nullptr) {
521 LOGE("get tag failed!");
522 goto clearup;
523 }
524 authTag.len = CCM_AUTH_TAG_LEN;
525 } else {
526 goto clearup;
527 }
528 ccmParamsSpec->base.getType = GetCcmParamsSpecType;
529 ccmParamsSpec->iv = *iv;
530 ccmParamsSpec->aad = *aad;
531 ccmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
532 *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ccmParamsSpec);
533 ret = true;
534 clearup:
535 if (!ret) {
536 HcfBlobDataFree(iv);
537 HcfBlobDataFree(aad);
538 HcfBlobDataFree(tag);
539 HCF_FREE_PTR(ccmParamsSpec);
540 }
541 HCF_FREE_PTR(iv);
542 HCF_FREE_PTR(aad);
543 HCF_FREE_PTR(tag);
544 return ret;
545 }
546
GetParamsSpecFromNapiValue(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)547 bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
548 {
549 napi_value data = nullptr;
550 napi_valuetype valueType = napi_undefined;
551 if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) {
552 LOGE("Invalid params!");
553 return false;
554 }
555
556 napi_status status = napi_get_named_property(env, arg, ALGO_PARAMS.c_str(), &data);
557 napi_typeof(env, data, &valueType);
558 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
559 status = napi_get_named_property(env, arg, ALGO_PARAMS_OLD.c_str(), &data);
560 napi_typeof(env, data, &valueType);
561 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
562 LOGE("failed to get valid algo name!");
563 return false;
564 }
565 }
566 string algoName;
567 if (!GetStringFromJSParams(env, data, algoName)) {
568 LOGE("GetStringFromJSParams failed!");
569 return false;
570 }
571 if (algoName.compare(IV_PARAMS_SPEC) == 0) {
572 return GetIvParamsSpec(env, arg, paramsSpec);
573 } else if (algoName.compare(GCM_PARAMS_SPEC) == 0) {
574 return GetGcmParamsSpec(env, arg, opMode, paramsSpec);
575 } else if (algoName.compare(CCM_PARAMS_SPEC) == 0) {
576 return GetCcmParamsSpec(env, arg, opMode, paramsSpec);
577 } else {
578 return false;
579 }
580 }
581
GetCharArrayFromJsString(napi_env env,napi_value arg,HcfBlob * retBlob)582 static bool GetCharArrayFromJsString(napi_env env, napi_value arg, HcfBlob *retBlob)
583 {
584 size_t length = 0;
585 if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
586 LOGE("can not get char string length");
587 return false;
588 }
589 if (length > PASSWORD_MAX_LENGTH) {
590 LOGE("password length should not exceed 4096");
591 return false;
592 }
593 if (length == 0) {
594 LOGD("empty string");
595 return false;
596 }
597 char *tmpPassword = static_cast<char *>(HcfMalloc(length + 1, 0));
598 if (tmpPassword == nullptr) {
599 LOGE("malloc string failed");
600 return false;
601 }
602 if (napi_get_value_string_utf8(env, arg, tmpPassword, (length + 1), &length) != napi_ok) {
603 LOGE("can not get char string value");
604 HcfFree(tmpPassword);
605 tmpPassword = nullptr;
606 return false;
607 }
608 retBlob->data = reinterpret_cast<uint8_t *>(tmpPassword);
609 retBlob->len = length;
610 return true;
611 }
612
InitEncodingParams(napi_env env,napi_value arg,HcfKeyEncodingParamsSpec * spec,HcfBlob * tmpPw,HcfBlob * tmpCipher)613 static bool InitEncodingParams(napi_env env, napi_value arg, HcfKeyEncodingParamsSpec *spec, HcfBlob *tmpPw,
614 HcfBlob *tmpCipher)
615 {
616 napi_value passWd = GetDetailAsyKeySpecValue(env, arg, PASSWD_PARAMS);
617 napi_value cipher = GetDetailAsyKeySpecValue(env, arg, CIPHER_PARAMS);
618 if ((passWd == nullptr) || (cipher == nullptr)) {
619 LOGE("Invalid params.");
620 return false;
621 }
622
623 if (!GetCharArrayFromJsString(env, passWd, tmpPw)) {
624 LOGE("Failed to get passWord string from napi!");
625 return false;
626 }
627
628 if (!GetCharArrayFromJsString(env, cipher, tmpCipher)) {
629 LOGE("Failed to get cipher string from napi!");
630 HcfBlobDataClearAndFree(tmpPw);
631 return false;
632 }
633
634 spec->cipher = reinterpret_cast<char *>(tmpCipher->data);
635 spec->password = reinterpret_cast<char *>(tmpPw->data);
636 return true;
637 }
638
GetEncodingParamsSpec(napi_env env,napi_value arg,HcfParamsSpec ** returnSpec)639 bool GetEncodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnSpec)
640 {
641 if ((env == nullptr) || (arg == nullptr) || (returnSpec == nullptr)) {
642 LOGE("Invalid params.");
643 return false;
644 }
645
646 HcfKeyEncodingParamsSpec *encodingParamsSpec =
647 reinterpret_cast<HcfKeyEncodingParamsSpec *>(HcfMalloc(sizeof(HcfKeyEncodingParamsSpec), 0));
648 if (encodingParamsSpec == nullptr) {
649 LOGE("encodingParamsSpec malloc failed!");
650 return false;
651 }
652
653 HcfBlob tmpPw = { .data = nullptr, .len = 0 };
654 HcfBlob tmpCipher = { .data = nullptr, .len = 0 };
655 if (!InitEncodingParams(env, arg, encodingParamsSpec, &tmpPw, &tmpCipher)) {
656 LOGE("Failed to get passWord string from napi!");
657 HcfFree(encodingParamsSpec);
658 encodingParamsSpec = nullptr;
659 return false;
660 }
661 *returnSpec = reinterpret_cast<HcfParamsSpec *>(encodingParamsSpec);
662 return true;
663 }
664
GetBlobFromStringJSParams(napi_env env,napi_value arg)665 HcfBlob *GetBlobFromStringJSParams(napi_env env, napi_value arg)
666 {
667 napi_valuetype valueType;
668 napi_typeof(env, arg, &valueType);
669 if (valueType != napi_string) {
670 LOGE("wrong argument type. expect string type. [Type]: %{public}d", valueType);
671 return nullptr;
672 }
673
674 size_t length = 0;
675 if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
676 LOGE("can not get string length");
677 return nullptr;
678 }
679
680 if (length == 0) {
681 LOGE("string length is 0");
682 return nullptr;
683 }
684
685 HcfBlob *newBlob = static_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
686 if (newBlob == nullptr) {
687 LOGE("Failed to allocate newBlob memory!");
688 return nullptr;
689 }
690
691 newBlob->len = length + 1;
692 newBlob->data = static_cast<uint8_t *>(HcfMalloc(newBlob->len, 0));
693 if (newBlob->data == nullptr) {
694 LOGE("malloc blob data failed!");
695 HcfFree(newBlob);
696 newBlob = nullptr;
697 return nullptr;
698 }
699
700 if (napi_get_value_string_utf8(env, arg, reinterpret_cast<char *>(newBlob->data), newBlob->len, &length) !=
701 napi_ok) {
702 LOGE("can not get string value");
703 HcfBlobDataClearAndFree(newBlob);
704 HcfFree(newBlob);
705 newBlob = nullptr;
706 return nullptr;
707 }
708
709 return newBlob;
710 }
711
GetDecodingParamsSpec(napi_env env,napi_value arg,HcfParamsSpec ** returnSpec)712 bool GetDecodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnSpec)
713 {
714 HcfKeyDecodingParamsSpec *decodingParamsSpec =
715 reinterpret_cast<HcfKeyDecodingParamsSpec *>(HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0));
716 if (decodingParamsSpec == nullptr) {
717 LOGE("decodingParamsSpec malloc failed!");
718 return false;
719 }
720
721 HcfBlob *tmpPw = GetBlobFromStringJSParams(env, arg);
722 if (tmpPw == nullptr) {
723 LOGE("Failed to get passWord string from napi!");
724 HcfFree(decodingParamsSpec);
725 decodingParamsSpec = nullptr;
726 return false;
727 }
728 if (tmpPw->len > PASSWORD_MAX_LENGTH) {
729 LOGE("Password length exceeds max length limit of 4096 bytes!");
730 HcfBlobDataClearAndFree(tmpPw);
731 HcfFree(tmpPw);
732 tmpPw = nullptr;
733 HcfFree(decodingParamsSpec);
734 decodingParamsSpec = nullptr;
735 return false;
736 }
737 decodingParamsSpec->password = reinterpret_cast<char *>(tmpPw->data);
738
739 *returnSpec = reinterpret_cast<HcfParamsSpec *>(decodingParamsSpec);
740 HcfFree(tmpPw);
741 tmpPw = nullptr;
742 return true;
743 }
744
GetDetailAsyKeySpecValue(napi_env env,napi_value arg,string argName)745 napi_value GetDetailAsyKeySpecValue(napi_env env, napi_value arg, string argName)
746 {
747 napi_value data = nullptr;
748 napi_valuetype valueType = napi_undefined;
749 if ((env == nullptr) || (arg == nullptr)) {
750 LOGE("Invalid params!");
751 return nullptr;
752 }
753 napi_status status = napi_get_named_property(env, arg, argName.c_str(), &data);
754 napi_typeof(env, data, &valueType);
755 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
756 LOGE("failed to get valid algo name!");
757 return nullptr;
758 }
759 return data;
760 }
761
GetCommSpecNapiValue(napi_env env,napi_value arg)762 static napi_value GetCommSpecNapiValue(napi_env env, napi_value arg)
763 {
764 napi_value data = nullptr;
765 napi_valuetype valueType = napi_undefined;
766
767 napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_COMM_PARAMS.c_str(), &data);
768 napi_typeof(env, data, &valueType);
769
770 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
771 LOGE("failed to get valid algo name!");
772 return nullptr;
773 }
774 return data;
775 }
776
InitDsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfDsaCommParamsSpec * spec)777 static bool InitDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfDsaCommParamsSpec *spec)
778 {
779 size_t algNameLen = DSA_ASY_KEY_SPEC.length();
780 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
781 if (spec->base.algName == nullptr) {
782 LOGE("malloc DSA algName failed!");
783 return false;
784 }
785 (void)memcpy_s(spec->base.algName, algNameLen+ 1, DSA_ASY_KEY_SPEC.c_str(), algNameLen);
786 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
787
788 napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
789 napi_value q = GetDetailAsyKeySpecValue(env, arg, "q");
790 napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
791 bool ret = GetBigIntFromNapiValue(env, p, &spec->p);
792 if (!ret) {
793 HcfFree(spec->base.algName);
794 spec->base.algName = nullptr;
795 return false;
796 }
797 ret = GetBigIntFromNapiValue(env, q, &spec->q);
798 if (!ret) {
799 FreeDsaCommParamsSpec(spec);
800 return false;
801 }
802 ret = GetBigIntFromNapiValue(env, g, &spec->g);
803 if (!ret) {
804 FreeDsaCommParamsSpec(spec);
805 return false;
806 }
807 return true;
808 }
809
GetDsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)810 static bool GetDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
811 {
812 HcfDsaCommParamsSpec *spec = reinterpret_cast<HcfDsaCommParamsSpec *>(HcfMalloc(sizeof(HcfDsaCommParamsSpec), 0));
813 if (spec == nullptr) {
814 LOGE("malloc failed!");
815 return false;
816 }
817 if (!InitDsaCommonAsyKeySpec(env, arg, spec)) {
818 LOGE("InitDsaCommonAsyKeySpec failed!");
819 HcfFree(spec);
820 spec = nullptr;
821 return false;
822 }
823 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
824 return true;
825 }
826
GetDsaPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)827 static bool GetDsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
828 {
829 HcfDsaPubKeyParamsSpec *spec = reinterpret_cast<HcfDsaPubKeyParamsSpec *>(
830 HcfMalloc(sizeof(HcfDsaPubKeyParamsSpec), 0));
831 if (spec == nullptr) {
832 LOGE("malloc failed!");
833 return false;
834 }
835
836 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
837 if (commSpecValue == nullptr) {
838 LOGE("Get comm spec napi value failed.");
839 HcfFree(spec);
840 spec = nullptr;
841 return false;
842 }
843 if (!InitDsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDsaCommParamsSpec *>(spec))) {
844 LOGE("InitDsaCommonAsyKeySpec failed.");
845 HcfFree(spec);
846 spec = nullptr;
847 return false;
848 }
849 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
850
851 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
852 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
853 if (!ret) {
854 DestroyDsaPubKeySpec(spec);
855 return false;
856 }
857 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
858 return true;
859 }
860
GetDsaKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)861 static bool GetDsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
862 {
863 HcfDsaKeyPairParamsSpec *spec = reinterpret_cast<HcfDsaKeyPairParamsSpec *>(
864 HcfMalloc(sizeof(HcfDsaKeyPairParamsSpec), 0));
865 if (spec == nullptr) {
866 LOGE("malloc failed!");
867 return false;
868 }
869
870 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
871 if (commSpecValue == nullptr) {
872 LOGE("Get comm spec napi value failed.");
873 HcfFree(spec);
874 spec = nullptr;
875 return false;
876 }
877 if (!InitDsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDsaCommParamsSpec *>(spec))) {
878 LOGE("InitDsaCommonAsyKeySpec failed!");
879 HcfFree(spec);
880 spec = nullptr;
881 return false;
882 }
883 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
884
885 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
886 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
887 if (!ret) {
888 FreeDsaCommParamsSpec(reinterpret_cast<HcfDsaCommParamsSpec *>(spec));
889 HcfFree(spec);
890 spec = nullptr;
891 return false;
892 }
893 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
894 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
895 if (!ret) {
896 FreeDsaCommParamsSpec(reinterpret_cast<HcfDsaCommParamsSpec *>(spec));
897 HcfFree(spec->pk.data);
898 spec->pk.data = nullptr;
899 HcfFree(spec);
900 spec = nullptr;
901 return false;
902 }
903 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
904 return true;
905 }
906
GetDsaAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)907 static bool GetDsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
908 {
909 napi_value data = nullptr;
910 napi_valuetype valueType = napi_undefined;
911
912 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
913 napi_typeof(env, data, &valueType);
914 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
915 LOGE("failed to get valid algo name!");
916 return false;
917 }
918 HcfAsyKeySpecType asyKeySpecType;
919 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
920 if (status != napi_ok) {
921 LOGE("failed to get valid asyKeySpecType!");
922 return false;
923 }
924 if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
925 return GetDsaCommonAsyKeySpec(env, arg, asyKeySpec);
926 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
927 return GetDsaPubKeySpec(env, arg, asyKeySpec);
928 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
929 return GetDsaKeyPairAsyKeySpec(env, arg, asyKeySpec);
930 } else {
931 return false;
932 }
933 }
934
GetFpField(napi_env env,napi_value arg,HcfECField ** ecField)935 static bool GetFpField(napi_env env, napi_value arg, HcfECField **ecField)
936 {
937 HcfECFieldFp *fp = reinterpret_cast<HcfECFieldFp *>(HcfMalloc(sizeof(HcfECFieldFp), 0));
938 if (fp == nullptr) {
939 LOGE("malloc fp failed!");
940 return false;
941 }
942
943 size_t fieldTpyeLen = ECC_FIELD_TYPE_FP.length();
944 fp->base.fieldType = static_cast<char *>(HcfMalloc(fieldTpyeLen + 1, 0));
945 if (fp->base.fieldType == nullptr) {
946 LOGE("malloc fieldType failed!");
947 HcfFree(fp);
948 fp = nullptr;
949 return false;
950 }
951 (void)memcpy_s(fp->base.fieldType, fieldTpyeLen+ 1, ECC_FIELD_TYPE_FP.c_str(), fieldTpyeLen);
952
953 napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
954 bool ret = GetBigIntFromNapiValue(env, p, &fp->p);
955 if (!ret) {
956 HcfFree(fp->base.fieldType);
957 fp->base.fieldType = nullptr;
958 HcfFree(fp);
959 fp = nullptr;
960 return false;
961 }
962 *ecField = reinterpret_cast<HcfECField *>(fp);
963 return true;
964 }
965
GetField(napi_env env,napi_value arg,HcfECField ** ecField)966 static bool GetField(napi_env env, napi_value arg, HcfECField **ecField)
967 {
968 // get fieldData in { field : fieldData, a : xxx, b : xxx, ... } of ECCCommonParamsSpec first
969 napi_value fieldData = nullptr;
970 napi_valuetype valueType = napi_undefined;
971 napi_status status = napi_get_named_property(env, arg, "field", &fieldData);
972 napi_typeof(env, fieldData, &valueType);
973 if ((status != napi_ok) || (fieldData == nullptr) || (valueType == napi_undefined)) {
974 LOGE("failed to get valid field data!");
975 return false;
976 }
977
978 // get fieldType in { fieldType : fieldTypeData } of ECField
979 napi_value fieldTypeData = nullptr;
980 status = napi_get_named_property(env, fieldData, "fieldType", &fieldTypeData);
981 napi_typeof(env, fieldTypeData, &valueType);
982 if ((status != napi_ok) || (fieldTypeData == nullptr) || (valueType == napi_undefined)) {
983 LOGE("failed to get valid fieldType data!");
984 return false;
985 }
986 string fieldType;
987 if (!GetStringFromJSParams(env, fieldTypeData, fieldType)) {
988 LOGE("GetStringFromJSParams failed when extracting fieldType!");
989 return false;
990 }
991
992 // get p in { p : pData } of ECField, and generateECField
993 if (fieldType.compare("Fp") == 0) {
994 return GetFpField(env, fieldData, ecField);
995 }
996 return false;
997 }
998
InitEccDetailAsyKeySpec(napi_env env,napi_value arg,HcfEccCommParamsSpec * spec)999 static bool InitEccDetailAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec)
1000 {
1001 napi_value a = GetDetailAsyKeySpecValue(env, arg, "a");
1002 napi_value b = GetDetailAsyKeySpecValue(env, arg, "b");
1003 napi_value n = GetDetailAsyKeySpecValue(env, arg, "n");
1004 napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
1005 bool ret = GetBigIntFromNapiValue(env, a, &spec->a);
1006 if (!ret) {
1007 LOGE("get ecc asyKeySpec a failed!");
1008 return false;
1009 }
1010 ret = GetBigIntFromNapiValue(env, b, &spec->b);
1011 if (!ret) {
1012 LOGE("get ecc asyKeySpec b failed!");
1013 return false;
1014 }
1015 ret = GetBigIntFromNapiValue(env, n, &spec->n);
1016 if (!ret) {
1017 LOGE("get ecc asyKeySpec n failed!");
1018 return false;
1019 }
1020 ret = GetPointFromNapiValue(env, g, &spec->g);
1021 if (!ret) {
1022 LOGE("get ecc asyKeySpec g failed!");
1023 return false;
1024 }
1025 return true;
1026 }
1027
InitEccCommonAsyKeySpec(napi_env env,napi_value arg,HcfEccCommParamsSpec * spec,const string & algName)1028 static bool InitEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec, const string &algName)
1029 {
1030 size_t algNameLen = ECC_ASY_KEY_SPEC.length();
1031 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
1032 if (spec->base.algName == nullptr) {
1033 LOGE("malloc ECC algName failed!");
1034 return false;
1035 }
1036 (void)memcpy_s(spec->base.algName, algNameLen+ 1, algName.c_str(), algNameLen);
1037 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
1038
1039 // get h
1040 napi_value hData = nullptr;
1041 napi_valuetype valueType = napi_undefined;
1042 napi_status status = napi_get_named_property(env, arg, "h", &hData);
1043 napi_typeof(env, hData, &valueType);
1044 if ((status != napi_ok) || (hData == nullptr) || (valueType == napi_undefined)) {
1045 LOGE("failed to get valid h!");
1046 HcfFree(spec->base.algName);
1047 spec->base.algName = nullptr;
1048 return false;
1049 }
1050 if (!GetInt32FromJSParams(env, hData, spec->h)) {
1051 LOGE("get ecc asyKeySpec h failed!");
1052 HcfFree(spec->base.algName);
1053 spec->base.algName = nullptr;
1054 return false;
1055 }
1056 // get field
1057 if (!GetField(env, arg, &spec->field)) {
1058 LOGE("GetField failed!");
1059 HcfFree(spec->base.algName);
1060 spec->base.algName = nullptr;
1061 return false;
1062 }
1063 bool ret = InitEccDetailAsyKeySpec(env, arg, spec);
1064 if (!ret) {
1065 LOGE("get ecc asyKeySpec g failed!");
1066 FreeEccCommParamsSpec(spec);
1067 return false;
1068 }
1069 return true;
1070 }
1071
GetEccCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1072 static bool GetEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec,
1073 const string &algName)
1074 {
1075 HcfEccCommParamsSpec *spec = reinterpret_cast<HcfEccCommParamsSpec *>(HcfMalloc(sizeof(HcfEccCommParamsSpec), 0));
1076 if (spec == nullptr) {
1077 LOGE("malloc failed!");
1078 return false;
1079 }
1080 if (!InitEccCommonAsyKeySpec(env, arg, spec, algName)) {
1081 LOGE("InitEccCommonAsyKeySpec failed!");
1082 HcfFree(spec);
1083 spec = nullptr;
1084 return false;
1085 }
1086 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1087 return true;
1088 }
1089
GetEccPriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1090 static bool GetEccPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1091 {
1092 HcfEccPriKeyParamsSpec *spec =
1093 reinterpret_cast<HcfEccPriKeyParamsSpec *>(HcfMalloc(sizeof(HcfEccPriKeyParamsSpec), 0));
1094 if (spec == nullptr) {
1095 LOGE("malloc failed!");
1096 return false;
1097 }
1098
1099 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1100 if (commSpecValue == nullptr) {
1101 LOGE("Get comm spec napi value failed.");
1102 HcfFree(spec);
1103 spec = nullptr;
1104 return false;
1105 }
1106 if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec), algName)) {
1107 LOGE("InitEccCommonAsyKeySpec failed!");
1108 HcfFree(spec);
1109 spec = nullptr;
1110 return false;
1111 }
1112 spec->base.base.specType = HCF_PRIVATE_KEY_SPEC;
1113
1114 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1115 bool ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1116 if (!ret) {
1117 // get big int fail, sk is null
1118 FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
1119 HcfFree(spec);
1120 spec = nullptr;
1121 return false;
1122 }
1123 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1124 return true;
1125 }
1126
GetEccPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1127 static bool GetEccPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1128 {
1129 HcfEccPubKeyParamsSpec *spec =
1130 reinterpret_cast<HcfEccPubKeyParamsSpec *>(HcfMalloc(sizeof(HcfEccPubKeyParamsSpec), 0));
1131 if (spec == nullptr) {
1132 LOGE("malloc failed!");
1133 return false;
1134 }
1135
1136 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1137 if (commSpecValue == nullptr) {
1138 LOGE("Get comm spec napi value failed.");
1139 HcfFree(spec);
1140 spec = nullptr;
1141 return false;
1142 }
1143 if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec), algName)) {
1144 LOGE("InitEccCommonAsyKeySpec failed!");
1145 HcfFree(spec);
1146 spec = nullptr;
1147 return false;
1148 }
1149 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
1150
1151 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1152 bool ret = GetPointFromNapiValue(env, pk, &spec->pk);
1153 if (!ret) {
1154 DestroyEccPubKeySpec(spec);
1155 return false;
1156 }
1157 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1158 return true;
1159 }
1160
GetEccKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1161 static bool GetEccKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec,
1162 const string &algName)
1163 {
1164 HcfEccKeyPairParamsSpec *spec =
1165 reinterpret_cast<HcfEccKeyPairParamsSpec *>(HcfMalloc(sizeof(HcfEccKeyPairParamsSpec), 0));
1166 if (spec == nullptr) {
1167 LOGE("malloc failed!");
1168 return false;
1169 }
1170
1171 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1172 if (commSpecValue == nullptr) {
1173 LOGE("Get comm spec napi value failed.");
1174 HcfFree(spec);
1175 spec = nullptr;
1176 return false;
1177 }
1178 if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec), algName)) {
1179 LOGE("InitEccCommonAsyKeySpec failed!");
1180 HcfFree(spec);
1181 spec = nullptr;
1182 return false;
1183 }
1184 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
1185
1186 // get big int fail, sk is null
1187 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1188 bool ret = GetPointFromNapiValue(env, pk, &spec->pk);
1189 if (!ret) {
1190 FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
1191 HcfFree(spec);
1192 spec = nullptr;
1193 return false;
1194 }
1195 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1196 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1197 if (!ret) {
1198 FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
1199 HcfFree(spec->pk.x.data);
1200 spec->pk.x.data = nullptr;
1201 HcfFree(spec->pk.y.data);
1202 spec->pk.y.data = nullptr;
1203 HcfFree(spec);
1204 spec = nullptr;
1205 return false;
1206 }
1207 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1208 return true;
1209 }
1210
GetEccAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1211 static bool GetEccAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1212 {
1213 napi_value data = nullptr;
1214 napi_valuetype valueType = napi_undefined;
1215
1216 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1217 napi_typeof(env, data, &valueType);
1218 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1219 LOGE("failed to get valid algo name!");
1220 return false;
1221 }
1222 HcfAsyKeySpecType asyKeySpecType;
1223 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1224 if (status != napi_ok) {
1225 LOGE("failed to get valid asyKeySpecType!");
1226 return false;
1227 }
1228 if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
1229 return GetEccCommonAsyKeySpec(env, arg, asyKeySpec, algName);
1230 } else if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
1231 return GetEccPriKeySpec(env, arg, asyKeySpec, algName);
1232 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1233 return GetEccPubKeySpec(env, arg, asyKeySpec, algName);
1234 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1235 return GetEccKeyPairAsyKeySpec(env, arg, asyKeySpec, algName);
1236 } else {
1237 LOGE("keySpec not support!");
1238 return false;
1239 }
1240 }
1241
InitRsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfRsaCommParamsSpec * spec)1242 static bool InitRsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfRsaCommParamsSpec *spec)
1243 {
1244 size_t algNameLen = RSA_ASY_KEY_SPEC.length();
1245 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
1246 if (spec->base.algName == nullptr) {
1247 LOGE("malloc RSA algName failed!");
1248 return false;
1249 }
1250 (void)memcpy_s(spec->base.algName, algNameLen+ 1, RSA_ASY_KEY_SPEC.c_str(), algNameLen);
1251 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
1252
1253 napi_value n = GetDetailAsyKeySpecValue(env, arg, "n");
1254
1255 bool ret = GetBigIntFromNapiValue(env, n, &spec->n);
1256 if (!ret) {
1257 LOGE("Rsa asyKeySpec get n failed!");
1258 HcfFree(spec->base.algName);
1259 spec->base.algName = nullptr;
1260 return false;
1261 }
1262 return true;
1263 }
1264
GetRsaPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1265 static bool GetRsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1266 {
1267 HcfRsaPubKeyParamsSpec *spec =
1268 reinterpret_cast<HcfRsaPubKeyParamsSpec *>(HcfMalloc(sizeof(HcfRsaPubKeyParamsSpec), 0));
1269 if (spec == nullptr) {
1270 LOGE("malloc failed!");
1271 return false;
1272 }
1273
1274 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1275 if (commSpecValue == nullptr) {
1276 LOGE("Get comm spec napi value failed.");
1277 HcfFree(spec);
1278 spec = nullptr;
1279 return false;
1280 }
1281 if (!InitRsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfRsaCommParamsSpec *>(spec))) {
1282 LOGE("InitRsaCommonAsyKeySpec failed!");
1283 HcfFree(spec);
1284 spec = nullptr;
1285 return false;
1286 }
1287 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
1288
1289 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1290 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1291 if (!ret) {
1292 DestroyRsaPubKeySpec(spec);
1293 return false;
1294 }
1295 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1296 return true;
1297 }
1298
GetRsaKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1299 static bool GetRsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1300 {
1301 HcfRsaKeyPairParamsSpec *spec =
1302 reinterpret_cast<HcfRsaKeyPairParamsSpec *>(HcfMalloc(sizeof(HcfRsaKeyPairParamsSpec), 0));
1303 if (spec == nullptr) {
1304 LOGE("malloc failed!");
1305 return false;
1306 }
1307
1308 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1309 if (commSpecValue == nullptr) {
1310 LOGE("Get comm spec napi value failed.");
1311 HcfFree(spec);
1312 spec = nullptr;
1313 return false;
1314 }
1315 if (!InitRsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfRsaCommParamsSpec *>(spec))) {
1316 LOGE("InitRsaCommonAsyKeySpec failed!");
1317 HcfFree(spec);
1318 spec = nullptr;
1319 return false;
1320 }
1321 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
1322
1323 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1324 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1325 if (!ret) {
1326 FreeRsaCommParamsSpec(&(spec->base));
1327 HcfFree(spec);
1328 spec = nullptr;
1329 return false;
1330 }
1331 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1332 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1333 if (!ret) {
1334 FreeRsaCommParamsSpec(&(spec->base));
1335 HcfFree(spec->pk.data);
1336 spec->pk.data = nullptr;
1337 HcfFree(spec);
1338 spec = nullptr;
1339 return false;
1340 }
1341 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1342 return true;
1343 }
1344
GetRsaAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1345 static bool GetRsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1346 {
1347 napi_value data = nullptr;
1348 napi_valuetype valueType = napi_undefined;
1349
1350 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1351 napi_typeof(env, data, &valueType);
1352 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1353 LOGE("failed to get valid algo name!");
1354 return false;
1355 }
1356 HcfAsyKeySpecType asyKeySpecType;
1357 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1358 if (status != napi_ok) {
1359 LOGE("failed to get valid asyKeySpecType!");
1360 return false;
1361 }
1362 if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
1363 LOGE("RSA not support comm key spec");
1364 return false;
1365 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1366 return GetRsaPubKeySpec(env, arg, asyKeySpec);
1367 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1368 return GetRsaKeyPairAsyKeySpec(env, arg, asyKeySpec);
1369 } else {
1370 return false;
1371 }
1372 }
1373
InitAlg25519CommonAsyKeySpec(HcfAsyKeyParamsSpec * spec,const string & algName)1374 static bool InitAlg25519CommonAsyKeySpec(HcfAsyKeyParamsSpec *spec, const string &algName)
1375 {
1376 size_t algNameLen = algName.length();
1377 spec->algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
1378 if (spec->algName == nullptr) {
1379 LOGE("malloc alg25519 algName failed!");
1380 return false;
1381 }
1382 (void)memcpy_s(spec->algName, algNameLen + 1, algName.c_str(), algNameLen);
1383 return true;
1384 }
1385
GetAlg25519PriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1386 static bool GetAlg25519PriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1387 {
1388 HcfAlg25519PriKeyParamsSpec *spec =
1389 reinterpret_cast<HcfAlg25519PriKeyParamsSpec *>(HcfMalloc(sizeof(HcfAlg25519PriKeyParamsSpec), 0));
1390 if (spec == nullptr) {
1391 LOGE("malloc failed!");
1392 return false;
1393 }
1394 if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast<HcfAsyKeyParamsSpec *>(spec), algName)) {
1395 LOGE("InitRsaCommonAsyKeySpec failed!");
1396 DestroyAlg25519PriKeySpec(spec);
1397 return false;
1398 }
1399 spec->base.specType = HCF_PRIVATE_KEY_SPEC;
1400
1401 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1402 bool ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1403 if (!ret) {
1404 // get big int fail, sk is null
1405 DestroyAlg25519PriKeySpec(spec);
1406 return false;
1407 }
1408 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1409 return true;
1410 }
1411
GetAlg25519PubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1412 static bool GetAlg25519PubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1413 {
1414 HcfAlg25519PubKeyParamsSpec *spec =
1415 reinterpret_cast<HcfAlg25519PubKeyParamsSpec *>(HcfMalloc(sizeof(HcfAlg25519PubKeyParamsSpec), 0));
1416 if (spec == nullptr) {
1417 LOGE("malloc failed!");
1418 return false;
1419 }
1420 if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast<HcfAsyKeyParamsSpec *>(spec), algName)) {
1421 LOGE("InitRsaCommonAsyKeySpec failed!");
1422 DestroyAlg25519PubKeySpec(spec);
1423 return false;
1424 }
1425 spec->base.specType = HCF_PUBLIC_KEY_SPEC;
1426
1427 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1428 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1429 if (!ret) {
1430 DestroyAlg25519PubKeySpec(spec);
1431 return false;
1432 }
1433 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1434 return true;
1435 }
1436
GetAlg25519KeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1437 static bool GetAlg25519KeyPairAsyKeySpec(napi_env env, napi_value arg,
1438 HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1439 {
1440 HcfAlg25519KeyPairParamsSpec *spec =
1441 reinterpret_cast<HcfAlg25519KeyPairParamsSpec *>(HcfMalloc(sizeof(HcfAlg25519KeyPairParamsSpec), 0));
1442 if (spec == nullptr) {
1443 LOGE("malloc failed!");
1444 return false;
1445 }
1446 if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast<HcfAsyKeyParamsSpec *>(spec), algName)) {
1447 LOGE("InitRsaCommonAsyKeySpec failed!");
1448 HcfFree(spec);
1449 spec = nullptr;
1450 return false;
1451 }
1452 spec->base.specType = HCF_KEY_PAIR_SPEC;
1453
1454 // get big int fail, sk is null
1455 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1456 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1457 if (!ret) {
1458 DestroyAlg25519KeyPairSpec(spec);
1459 return false;
1460 }
1461 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1462 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1463 if (!ret) {
1464 DestroyAlg25519KeyPairSpec(spec);
1465 return false;
1466 }
1467 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1468 return true;
1469 }
1470
GetAlg25519AsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1471 static bool GetAlg25519AsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1472 {
1473 napi_value data = nullptr;
1474 napi_valuetype valueType = napi_undefined;
1475
1476 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1477 napi_typeof(env, data, &valueType);
1478 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1479 LOGE("failed to get valid algo name!");
1480 return false;
1481 }
1482 HcfAsyKeySpecType asyKeySpecType;
1483 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1484 if (status != napi_ok) {
1485 LOGE("failed to get valid asyKeySpecType!");
1486 return false;
1487 }
1488 if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
1489 return GetAlg25519PriKeySpec(env, arg, asyKeySpec, algName);
1490 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1491 return GetAlg25519PubKeySpec(env, arg, asyKeySpec, algName);
1492 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1493 return GetAlg25519KeyPairAsyKeySpec(env, arg, asyKeySpec, algName);
1494 } else {
1495 LOGE("keySpec not support!");
1496 return false;
1497 }
1498 }
1499
InitDhCommonAsyKeySpec(napi_env env,napi_value arg,HcfDhCommParamsSpec * spec)1500 static bool InitDhCommonAsyKeySpec(napi_env env, napi_value arg, HcfDhCommParamsSpec *spec)
1501 {
1502 size_t algNameLen = DH_ASY_KEY_SPEC.length();
1503 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
1504 if (spec->base.algName == nullptr) {
1505 LOGE("malloc DH algName failed!");
1506 return false;
1507 }
1508 (void)memcpy_s(spec->base.algName, algNameLen+ 1, DH_ASY_KEY_SPEC.c_str(), algNameLen);
1509 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
1510
1511 napi_value length = nullptr;
1512 napi_valuetype valueType = napi_undefined;
1513 napi_status status = napi_get_named_property(env, arg, "l", &length);
1514 napi_typeof(env, length, &valueType);
1515 if ((status != napi_ok) || (length == nullptr) || (valueType == napi_undefined)) {
1516 LOGE("failed to get valid l!");
1517 HcfFree(spec->base.algName);
1518 spec->base.algName = nullptr;
1519 return false;
1520 }
1521 if (!GetInt32FromJSParams(env, length, spec->length)) {
1522 LOGE("get dh asyKeySpec length failed!");
1523 HcfFree(spec->base.algName);
1524 spec->base.algName = nullptr;
1525 return false;
1526 }
1527 napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
1528 napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
1529 bool ret = GetBigIntFromNapiValue(env, p, &spec->p);
1530 if (!ret) {
1531 HcfFree(spec->base.algName);
1532 spec->base.algName = nullptr;
1533 return false;
1534 }
1535 ret = GetBigIntFromNapiValue(env, g, &spec->g);
1536 if (!ret) {
1537 FreeDhCommParamsSpec(spec);
1538 return false;
1539 }
1540 return true;
1541 }
1542
GetDhCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1543 static bool GetDhCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1544 {
1545 HcfDhCommParamsSpec *spec = reinterpret_cast<HcfDhCommParamsSpec *>(HcfMalloc(sizeof(HcfDhCommParamsSpec), 0));
1546 if (spec == nullptr) {
1547 LOGE("malloc failed!");
1548 return false;
1549 }
1550 if (!InitDhCommonAsyKeySpec(env, arg, spec)) {
1551 LOGE("InitDhCommonAsyKeySpec failed!");
1552 HcfFree(spec);
1553 spec = nullptr;
1554 return false;
1555 }
1556 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1557 return true;
1558 }
1559
GetDhPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1560 static bool GetDhPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1561 {
1562 HcfDhPubKeyParamsSpec *spec = reinterpret_cast<HcfDhPubKeyParamsSpec *>(
1563 HcfMalloc(sizeof(HcfDhPubKeyParamsSpec), 0));
1564 if (spec == nullptr) {
1565 LOGE("malloc failed!");
1566 return false;
1567 }
1568
1569 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1570 if (commSpecValue == nullptr) {
1571 LOGE("Get comm spec napi value failed.");
1572 HcfFree(spec);
1573 spec = nullptr;
1574 return false;
1575 }
1576 if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDhCommParamsSpec *>(spec))) {
1577 LOGE("InitDhCommonAsyKeySpec failed.");
1578 HcfFree(spec);
1579 spec = nullptr;
1580 return false;
1581 }
1582 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
1583
1584 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1585 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1586 if (!ret) {
1587 DestroyDhPubKeySpec(spec);
1588 return false;
1589 }
1590 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1591 return true;
1592 }
1593
GetDhPriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1594 static bool GetDhPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1595 {
1596 HcfDhPriKeyParamsSpec *spec = reinterpret_cast<HcfDhPriKeyParamsSpec *>(
1597 HcfMalloc(sizeof(HcfDhPriKeyParamsSpec), 0));
1598 if (spec == nullptr) {
1599 LOGE("malloc failed!");
1600 return false;
1601 }
1602
1603 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1604 if (commSpecValue == nullptr) {
1605 LOGE("Get comm spec napi value failed.");
1606 HcfFree(spec);
1607 spec = nullptr;
1608 return false;
1609 }
1610 if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDhCommParamsSpec *>(spec))) {
1611 LOGE("InitDhCommonAsyKeySpec failed.");
1612 HcfFree(spec);
1613 spec = nullptr;
1614 return false;
1615 }
1616 spec->base.base.specType = HCF_PRIVATE_KEY_SPEC;
1617
1618 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "sk");
1619 bool ret = GetBigIntFromNapiValue(env, pk, &spec->sk);
1620 if (!ret) {
1621 DestroyDhPriKeySpec(spec);
1622 return false;
1623 }
1624 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1625 return true;
1626 }
1627
GetDhKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1628 static bool GetDhKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1629 {
1630 HcfDhKeyPairParamsSpec *spec = reinterpret_cast<HcfDhKeyPairParamsSpec *>(
1631 HcfMalloc(sizeof(HcfDhKeyPairParamsSpec), 0));
1632 if (spec == nullptr) {
1633 LOGE("malloc failed!");
1634 return false;
1635 }
1636
1637 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1638 if (commSpecValue == nullptr) {
1639 LOGE("Get comm spec napi value failed.");
1640 HcfFree(spec);
1641 spec = nullptr;
1642 return false;
1643 }
1644 if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDhCommParamsSpec *>(spec))) {
1645 LOGE("InitDhCommonAsyKeySpec failed!");
1646 HcfFree(spec);
1647 spec = nullptr;
1648 return false;
1649 }
1650 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
1651
1652 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1653 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1654 if (!ret) {
1655 FreeDhCommParamsSpec(reinterpret_cast<HcfDhCommParamsSpec *>(spec));
1656 HcfFree(spec);
1657 spec = nullptr;
1658 return false;
1659 }
1660 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1661 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1662 if (!ret) {
1663 FreeDhCommParamsSpec(reinterpret_cast<HcfDhCommParamsSpec *>(spec));
1664 HcfFree(spec->pk.data);
1665 spec->pk.data = nullptr;
1666 HcfFree(spec);
1667 spec = nullptr;
1668 return false;
1669 }
1670 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1671 return true;
1672 }
1673
GetDh25519AsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1674 static bool GetDh25519AsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1675 {
1676 napi_value data = nullptr;
1677 napi_valuetype valueType = napi_undefined;
1678
1679 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1680 napi_typeof(env, data, &valueType);
1681 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1682 LOGE("failed to get valid algo name!");
1683 return false;
1684 }
1685 HcfAsyKeySpecType asyKeySpecType;
1686 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1687 if (status != napi_ok) {
1688 LOGE("failed to get valid asyKeySpecType!");
1689 return false;
1690 }
1691 if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
1692 return GetDhPriKeySpec(env, arg, asyKeySpec);
1693 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1694 return GetDhPubKeySpec(env, arg, asyKeySpec);
1695 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1696 return GetDhKeyPairAsyKeySpec(env, arg, asyKeySpec);
1697 } else if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
1698 return GetDhCommonAsyKeySpec(env, arg, asyKeySpec);
1699 } else {
1700 LOGE("keySpec not support!");
1701 return false;
1702 }
1703 }
1704
GetAsyKeySpecFromNapiValue(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1705 bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1706 {
1707 napi_value data = nullptr;
1708
1709 if ((env == nullptr) || (arg == nullptr) || (asyKeySpec == nullptr)) {
1710 LOGE("Invalid params!");
1711 return false;
1712 }
1713
1714 napi_valuetype valueType = napi_undefined;
1715
1716 napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_ALG_NAME.c_str(), &data);
1717 napi_typeof(env, data, &valueType);
1718 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1719 LOGE("failed to get valid algName!");
1720 return false;
1721 }
1722 string algName;
1723 if (!GetStringFromJSParams(env, data, algName)) {
1724 LOGE("GetStringFromJSParams failed!");
1725 return false;
1726 }
1727 if (algName.compare(DSA_ASY_KEY_SPEC) == 0) {
1728 return GetDsaAsyKeySpec(env, arg, asyKeySpec);
1729 } else if (algName.compare(ECC_ASY_KEY_SPEC) == 0 || algName.compare(SM2_ASY_KEY_SPEC) == 0) {
1730 return GetEccAsyKeySpec(env, arg, asyKeySpec, algName);
1731 } else if (algName.compare(RSA_ASY_KEY_SPEC) == 0) {
1732 return GetRsaAsyKeySpec(env, arg, asyKeySpec);
1733 } else if (algName.compare(ED25519_ASY_KEY_SPEC) == 0 || algName.compare(X25519_ASY_KEY_SPEC) == 0) {
1734 return GetAlg25519AsyKeySpec(env, arg, asyKeySpec, algName);
1735 } else if (algName.compare(DH_ASY_KEY_SPEC) == 0) {
1736 return GetDh25519AsyKeySpec(env, arg, asyKeySpec);
1737 } else {
1738 LOGE("AlgName not support! [AlgName]: %{public}s", algName.c_str());
1739 return false;
1740 }
1741 }
1742
ConvertBlobToNapiValue(napi_env env,HcfBlob * blob)1743 napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob)
1744 {
1745 if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1746 LOGD("Invalid blob!");
1747 return NapiGetNull(env);
1748 }
1749 uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1750 if (buffer == nullptr) {
1751 LOGE("malloc uint8 array buffer failed!");
1752 return NapiGetNull(env);
1753 }
1754
1755 if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
1756 LOGE("memcpy_s data to buffer failed!");
1757 HcfFree(buffer);
1758 buffer = nullptr;
1759 return NapiGetNull(env);
1760 }
1761
1762 napi_value outBuffer = nullptr;
1763 napi_status status = napi_create_external_arraybuffer(
1764 env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1765 if (status != napi_ok) {
1766 LOGE("create uint8 array buffer failed!");
1767 (void)memset_s(buffer, blob->len, 0, blob->len);
1768 HcfFree(buffer);
1769 buffer = nullptr;
1770 return NapiGetNull(env);
1771 }
1772 buffer = nullptr;
1773
1774 napi_value outData = nullptr;
1775 napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1776 napi_value dataBlob = nullptr;
1777 napi_create_object(env, &dataBlob);
1778 napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
1779
1780 return dataBlob;
1781 }
1782
CreateNapiUint8ArrayNoCopy(napi_env env,HcfBlob * blob,napi_value * napiValue)1783 HcfResult CreateNapiUint8ArrayNoCopy(napi_env env, HcfBlob *blob, napi_value *napiValue)
1784 {
1785 if (blob->data == nullptr || blob->len == 0) { // inner api, allow empty data
1786 *napiValue = NapiGetNull(env);
1787 return HCF_SUCCESS;
1788 }
1789
1790 napi_value outBuffer = nullptr;
1791 napi_status status = napi_create_external_arraybuffer(
1792 env, blob->data, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1793 if (status != napi_ok) {
1794 LOGE("create napi uint8 array buffer failed!");
1795 return HCF_ERR_NAPI;
1796 }
1797
1798 napi_value outData = nullptr;
1799 napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1800 napi_value dataBlob = nullptr;
1801 napi_create_object(env, &dataBlob);
1802 napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
1803 *napiValue = dataBlob;
1804
1805 blob->data = nullptr;
1806 blob->len = 0;
1807 return HCF_SUCCESS;
1808 }
1809
ConvertDataBlobToNapiValue(napi_env env,HcfBlob * blob,napi_value * napiValue)1810 HcfResult ConvertDataBlobToNapiValue(napi_env env, HcfBlob *blob, napi_value *napiValue)
1811 {
1812 if (blob->data == nullptr || blob->len == 0) { // inner api, allow empty data
1813 *napiValue = NapiGetNull(env);
1814 return HCF_SUCCESS;
1815 }
1816
1817 uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1818 if (buffer == nullptr) {
1819 LOGE("malloc uint8 array buffer failed!");
1820 return HCF_ERR_MALLOC;
1821 }
1822 (void)memcpy_s(buffer, blob->len, blob->data, blob->len);
1823
1824 napi_value outBuffer = nullptr;
1825 napi_status status = napi_create_external_arraybuffer(
1826 env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1827 if (status != napi_ok) {
1828 LOGE("create napi uint8 array buffer failed!");
1829 HcfFree(buffer);
1830 buffer = nullptr;
1831 return HCF_ERR_NAPI;
1832 }
1833
1834 napi_value outData = nullptr;
1835 napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1836 napi_value dataBlob = nullptr;
1837 napi_create_object(env, &dataBlob);
1838 napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
1839 *napiValue = dataBlob;
1840 return HCF_SUCCESS;
1841 }
1842
ConvertObjectBlobToNapiValue(napi_env env,HcfBlob * blob)1843 napi_value ConvertObjectBlobToNapiValue(napi_env env, HcfBlob *blob)
1844 {
1845 if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1846 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1847 LOGE("Invalid blob!");
1848 return NapiGetNull(env);
1849 }
1850 uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1851 if (buffer == nullptr) {
1852 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc uint8 array buffer failed!"));
1853 LOGE("malloc uint8 array buffer failed!");
1854 return NapiGetNull(env);
1855 }
1856
1857 if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
1858 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "memcpy_s data to buffer failed!"));
1859 LOGE("memcpy_s data to buffer failed!");
1860 HcfFree(buffer);
1861 buffer = nullptr;
1862 return NapiGetNull(env);
1863 }
1864
1865 napi_value outBuffer = nullptr;
1866 napi_status status = napi_create_external_arraybuffer(
1867 env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1868 if (status != napi_ok) {
1869 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create uint8 array buffer failed!"));
1870 LOGE("create uint8 array buffer failed!");
1871 HcfFree(buffer);
1872 buffer = nullptr;
1873 return NapiGetNull(env);
1874 }
1875 buffer = nullptr;
1876
1877 napi_value outData = nullptr;
1878 napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1879 return outData;
1880 }
1881
ConvertBigIntToNapiValue(napi_env env,HcfBigInteger * blob)1882 napi_value ConvertBigIntToNapiValue(napi_env env, HcfBigInteger *blob)
1883 {
1884 if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1885 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1886 LOGE("Invalid blob!");
1887 return NapiGetNull(env);
1888 }
1889 size_t wordsCount = (blob->len / sizeof(uint64_t)) + ((blob->len % sizeof(uint64_t)) == 0 ? 0 : 1);
1890 uint64_t *words = reinterpret_cast<uint64_t *>(HcfMalloc(wordsCount * sizeof(uint64_t), 0));
1891 if (words == nullptr) {
1892 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc uint8 array buffer failed!"));
1893 LOGE("malloc uint8 array buffer failed!");
1894 return NapiGetNull(env);
1895 }
1896
1897 size_t index = 0;
1898 for (size_t i = 0; index < wordsCount; i += sizeof(uint64_t), index++) {
1899 uint64_t tmp = 0;
1900 for (size_t j = 0; j < sizeof(uint64_t); j++) {
1901 if (i + j < blob->len) {
1902 tmp += ((uint64_t)blob->data[i + j] << (sizeof(uint64_t) * j));
1903 }
1904 }
1905 words[index] = tmp;
1906 }
1907 napi_value bigInt = nullptr;
1908 napi_status status = napi_create_bigint_words(env, 0, wordsCount, words, &bigInt);
1909 if (status != napi_ok) {
1910 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create bigint failed!"));
1911 LOGE("create bigint failed!");
1912 (void)memset_s(words, wordsCount * sizeof(uint64_t), 0, wordsCount * sizeof(uint64_t));
1913 HcfFree(words);
1914 words = nullptr;
1915 return NapiGetNull(env);
1916 }
1917 if (bigInt == nullptr) {
1918 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "bigInt is null!"));
1919 LOGE("bigInt is null!");
1920 }
1921 (void)memset_s(words, wordsCount * sizeof(uint64_t), 0, wordsCount * sizeof(uint64_t));
1922 HcfFree(words);
1923 words = nullptr;
1924 return bigInt;
1925 }
1926
GetStringFromJSParams(napi_env env,napi_value arg,string & returnStr)1927 bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr)
1928 {
1929 napi_valuetype valueType;
1930 napi_typeof(env, arg, &valueType);
1931 if (valueType != napi_string) {
1932 LOGE("wrong argument type. expect string type.");
1933 return false;
1934 }
1935
1936 size_t length = 0;
1937 if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
1938 LOGE("can not get string length");
1939 return false;
1940 }
1941 returnStr.reserve(length + 1);
1942 returnStr.resize(length);
1943 if (napi_get_value_string_utf8(env, arg, returnStr.data(), (length + 1), &length) != napi_ok) {
1944 LOGE("can not get string value");
1945 return false;
1946 }
1947 return true;
1948 }
1949
GetInt32FromJSParams(napi_env env,napi_value arg,int32_t & returnInt)1950 bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt)
1951 {
1952 napi_valuetype valueType;
1953 napi_typeof(env, arg, &valueType);
1954 if (valueType != napi_number) {
1955 LOGE("wrong argument type. expect int type. [Type]: %{public}d", valueType);
1956 return false;
1957 }
1958
1959 if (napi_get_value_int32(env, arg, &returnInt) != napi_ok) {
1960 LOGE("can not get int value");
1961 return false;
1962 }
1963 return true;
1964 }
1965
GetUint32FromJSParams(napi_env env,napi_value arg,uint32_t & returnInt)1966 bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt)
1967 {
1968 napi_valuetype valueType;
1969 napi_typeof(env, arg, &valueType);
1970 if (valueType != napi_number) {
1971 LOGE("wrong argument type. expect int type. [Type]: %{public}d", valueType);
1972 return false;
1973 }
1974
1975 if (napi_get_value_uint32(env, arg, &returnInt) != napi_ok) {
1976 LOGE("can not get int value");
1977 return false;
1978 }
1979 return true;
1980 }
1981
GetUint64FromJSParams(napi_env env,napi_value arg,uint64_t & returnInt)1982 bool GetUint64FromJSParams(napi_env env, napi_value arg, uint64_t &returnInt)
1983 {
1984 napi_valuetype valueType;
1985 napi_typeof(env, arg, &valueType);
1986 if (valueType != napi_number) {
1987 LOGE("wrong argument type. expect int type. [Type]: %{public}d", valueType);
1988 return false;
1989 }
1990 int64_t int64Value = 0;
1991 if (napi_get_value_int64(env, arg, &int64Value) != napi_ok) {
1992 LOGE("can not get int value");
1993 return false;
1994 }
1995 if (int64Value < 0) {
1996 LOGE("int64Value is less than 0");
1997 return false;
1998 }
1999 returnInt = static_cast<uint64_t>(int64Value);
2000 return true;
2001 }
2002
GetCallbackFromJSParams(napi_env env,napi_value arg,napi_ref * returnCb)2003 bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb)
2004 {
2005 napi_valuetype valueType = napi_undefined;
2006 napi_typeof(env, arg, &valueType);
2007 if (valueType != napi_function) {
2008 LOGE("wrong argument type. expect callback type. [Type]: %{public}d", valueType);
2009 return false;
2010 }
2011
2012 napi_create_reference(env, arg, 1, returnCb);
2013 return true;
2014 }
2015
GetJsErrValueByErrCode(HcfResult errCode)2016 static uint32_t GetJsErrValueByErrCode(HcfResult errCode)
2017 {
2018 switch (errCode) {
2019 case HCF_INVALID_PARAMS:
2020 return JS_ERR_INVALID_PARAMS;
2021 case HCF_NOT_SUPPORT:
2022 return JS_ERR_NOT_SUPPORT;
2023 case HCF_ERR_MALLOC:
2024 return JS_ERR_OUT_OF_MEMORY;
2025 case HCF_ERR_NAPI:
2026 return JS_ERR_RUNTIME_ERROR;
2027 case HCF_ERR_CRYPTO_OPERATION:
2028 return JS_ERR_CRYPTO_OPERATION;
2029 case HCF_ERR_PARAMETER_CHECK_FAILED:
2030 return JS_ERR_PARAMETER_CHECK_FAILED;
2031 default:
2032 return JS_ERR_DEFAULT_ERR;
2033 }
2034 }
2035
GenerateBusinessError(napi_env env,HcfResult errCode,const char * errMsg)2036 napi_value GenerateBusinessError(napi_env env, HcfResult errCode, const char *errMsg)
2037 {
2038 napi_value businessError = nullptr;
2039
2040 napi_value code = nullptr;
2041 napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code);
2042
2043 napi_value msg = nullptr;
2044 napi_create_string_utf8(env, errMsg, NAPI_AUTO_LENGTH, &msg);
2045
2046 napi_create_error(env, nullptr, msg, &businessError);
2047 napi_set_named_property(env, businessError, CRYPTO_TAG_ERR_CODE.c_str(), code);
2048
2049 return businessError;
2050 }
2051
CheckArgsCount(napi_env env,size_t argc,size_t expectedCount,bool isSync)2052 bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync)
2053 {
2054 if (isSync) {
2055 if (argc != expectedCount) {
2056 LOGE("invalid params count!");
2057 return false;
2058 }
2059 } else {
2060 if ((argc != expectedCount) && (argc != (expectedCount - ARGS_SIZE_ONE))) {
2061 LOGE("invalid params count!");
2062 return false;
2063 }
2064 }
2065 return true;
2066 }
2067
isCallback(napi_env env,napi_value argv,size_t argc,size_t expectedArgc)2068 bool isCallback(napi_env env, napi_value argv, size_t argc, size_t expectedArgc)
2069 {
2070 if (argc == expectedArgc - 1) {
2071 return false;
2072 }
2073 napi_valuetype valueType = napi_undefined;
2074 napi_typeof(env, argv, &valueType);
2075 if (valueType == napi_undefined || valueType == napi_null) {
2076 return false;
2077 }
2078 return true;
2079 }
2080
GetResourceName(napi_env env,const char * name)2081 napi_value GetResourceName(napi_env env, const char *name)
2082 {
2083 napi_value resourceName = nullptr;
2084 napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &resourceName);
2085 return resourceName;
2086 }
2087
BuildSetNamedProperty(napi_env env,HcfBigInteger * number,const char * name,napi_value * instance)2088 bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *instance)
2089 {
2090 napi_value value = ConvertBigIntToNapiValue(env, number);
2091 napi_status status = napi_set_named_property(env, *instance, name, value);
2092 if (status != napi_ok) {
2093 LOGE("create value failed!");
2094 return false;
2095 }
2096 return true;
2097 }
2098 } // namespace CryptoFramework
2099 } // namespace OHOS
2100