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