1 /*
2 * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_utils.h"
17
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
29 namespace OHOS {
30 namespace CryptoFramework {
31 using namespace std;
32
33 struct AsyKeySpecItemRelationT {
34 AsyKeySpecItem item;
35 int32_t itemType;
36 };
37 using AsyKeySpecItemRelation = AsyKeySpecItemRelationT;
38
39 static const AsyKeySpecItemRelation ASY_KEY_SPEC_RELATION_SET[] = {
40 { DSA_P_BN, SPEC_ITEM_TYPE_BIG_INT },
41 { DSA_Q_BN, SPEC_ITEM_TYPE_BIG_INT },
42 { DSA_G_BN, SPEC_ITEM_TYPE_BIG_INT },
43 { DSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
44 { DSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
45
46 { ECC_FP_P_BN, SPEC_ITEM_TYPE_BIG_INT },
47 { ECC_A_BN, SPEC_ITEM_TYPE_BIG_INT },
48 { ECC_B_BN, SPEC_ITEM_TYPE_BIG_INT },
49 { ECC_G_X_BN, SPEC_ITEM_TYPE_BIG_INT },
50 { ECC_G_Y_BN, SPEC_ITEM_TYPE_BIG_INT },
51 { ECC_N_BN, SPEC_ITEM_TYPE_BIG_INT },
52 { ECC_H_INT, SPEC_ITEM_TYPE_NUM }, // warning: ECC_H_NUM in JS
53 { ECC_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
54 { ECC_PK_X_BN, SPEC_ITEM_TYPE_BIG_INT },
55 { ECC_PK_Y_BN, SPEC_ITEM_TYPE_BIG_INT },
56 { ECC_FIELD_TYPE_STR, SPEC_ITEM_TYPE_STR },
57 { ECC_FIELD_SIZE_INT, SPEC_ITEM_TYPE_NUM }, // warning: ECC_FIELD_SIZE_NUM in JS
58 { ECC_CURVE_NAME_STR, SPEC_ITEM_TYPE_STR },
59
60 { RSA_N_BN, SPEC_ITEM_TYPE_BIG_INT },
61 { RSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
62 { RSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT }
63 };
64
GetAsyKeySpecType(AsyKeySpecItem targetItemType)65 int32_t GetAsyKeySpecType(AsyKeySpecItem targetItemType)
66 {
67 for (uint32_t i = 0; i < sizeof(ASY_KEY_SPEC_RELATION_SET) / sizeof(AsyKeySpecItemRelation); i++) {
68 if (ASY_KEY_SPEC_RELATION_SET[i].item == targetItemType) {
69 return ASY_KEY_SPEC_RELATION_SET[i].itemType;
70 }
71 }
72 LOGE("AsyKeySpecItem not support! ItemType: %d", targetItemType);
73 return -1;
74 }
75
GetSignSpecType(SignSpecItem targetItemType)76 int32_t GetSignSpecType(SignSpecItem targetItemType)
77 {
78 if (targetItemType == PSS_MD_NAME_STR || targetItemType == PSS_MGF_NAME_STR ||
79 targetItemType == PSS_MGF1_MD_STR) {
80 return SPEC_ITEM_TYPE_STR;
81 }
82 if (targetItemType == PSS_SALT_LEN_INT || targetItemType == PSS_TRAILER_FIELD_INT) {
83 return SPEC_ITEM_TYPE_NUM;
84 }
85 LOGE("SignSpecItem not support! ItemType: %d", targetItemType);
86 return -1;
87 }
88
GetCipherSpecType(CipherSpecItem targetItemType)89 int32_t GetCipherSpecType(CipherSpecItem targetItemType)
90 {
91 if (targetItemType == OAEP_MD_NAME_STR || targetItemType == OAEP_MGF_NAME_STR ||
92 targetItemType == OAEP_MGF1_MD_STR) {
93 return SPEC_ITEM_TYPE_STR;
94 }
95 if (targetItemType == OAEP_MGF1_PSRC_UINT8ARR) {
96 return SPEC_ITEM_TYPE_UINT8ARR;
97 }
98 LOGE("CipherSpecItem not support! ItemType: %d", targetItemType);
99 return -1;
100 }
101
NapiGetNull(napi_env env)102 napi_value NapiGetNull(napi_env env)
103 {
104 napi_value result = nullptr;
105 napi_get_null(env, &result);
106 return result;
107 }
108
GetBlobFromNapiValue(napi_env env,napi_value arg)109 HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg)
110 {
111 if ((env == nullptr) || (arg == nullptr)) {
112 LOGE("Invalid parmas!");
113 return nullptr;
114 }
115 napi_value data = nullptr;
116 napi_valuetype valueType = napi_undefined;
117 napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_DATA.c_str(), &data);
118 napi_typeof(env, data, &valueType);
119 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
120 LOGE("failed to get valid data property!");
121 return nullptr;
122 }
123
124 size_t length = 0;
125 size_t offset = 0;
126 void *rawData = nullptr;
127 napi_value arrayBuffer = nullptr;
128 napi_typedarray_type arrayType;
129 // Warning: Do not release the rawData returned by this interface because the rawData is managed by VM.
130 status = napi_get_typedarray_info(env, data, &arrayType, &length,
131 reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
132 if ((status != napi_ok) || (length == 0) || (rawData == nullptr)) {
133 LOGE("failed to get valid rawData.");
134 return nullptr;
135 }
136 if (arrayType != napi_uint8_array) {
137 LOGE("input data is not uint8 array.");
138 return nullptr;
139 }
140
141 HcfBlob *newBlob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
142 if (newBlob == nullptr) {
143 LOGE("Failed to allocate newBlob memory!");
144 return nullptr;
145 }
146 newBlob->len = length;
147 newBlob->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
148 if (newBlob->data == nullptr) {
149 LOGE("malloc blob data failed!");
150 HcfFree(newBlob);
151 return nullptr;
152 }
153 (void)memcpy_s(newBlob->data, length, rawData, length);
154 return newBlob;
155 }
156
GeneralGetBlobFromNapiValue(napi_env env,napi_value data)157 HcfBlob *GeneralGetBlobFromNapiValue(napi_env env, napi_value data)
158 {
159 size_t length = 0;
160 size_t offset = 0;
161 void *rawData = nullptr;
162 napi_value arrayBuffer = nullptr;
163 napi_typedarray_type arrayType;
164 // Warning: Do not release the rawData returned by this interface because the rawData is managed by VM.
165 napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
166 reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
167 if ((status != napi_ok) || (length == 0) || (rawData == nullptr)) {
168 LOGE("failed to get valid rawData.");
169 return nullptr;
170 }
171 if (arrayType != napi_uint8_array) {
172 LOGE("input data is not uint8 array.");
173 return nullptr;
174 }
175
176 HcfBlob *newBlob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
177 if (newBlob == nullptr) {
178 LOGE("Failed to allocate newBlob memory!");
179 return nullptr;
180 }
181 newBlob->len = length;
182 newBlob->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
183 if (newBlob->data == nullptr) {
184 LOGE("malloc blob data failed!");
185 HcfFree(newBlob);
186 return nullptr;
187 }
188 (void)memcpy_s(newBlob->data, length, rawData, length);
189 return newBlob;
190 }
191
GetBigIntFromNapiValue(napi_env env,napi_value arg,HcfBigInteger * bigInt)192 bool GetBigIntFromNapiValue(napi_env env, napi_value arg, HcfBigInteger *bigInt)
193 {
194 if ((env == nullptr) || (arg == nullptr)) {
195 LOGE("Invalid parmas!");
196 return false;
197 }
198
199 int signBit;
200 size_t wordCount;
201
202 napi_get_value_bigint_words(env, arg, nullptr, &wordCount, nullptr);
203 if ((wordCount) == 0 &&(wordCount > (INT_MAX / sizeof(uint64_t)))) {
204 LOGE("Get big int failed.");
205 return false;
206 }
207 int length = wordCount * sizeof(uint64_t);
208 uint8_t *retArr = reinterpret_cast<uint8_t *>(HcfMalloc(length, 0));
209 if (retArr == nullptr) {
210 LOGE("malloc blob data failed!");
211 return false;
212 }
213 if (napi_get_value_bigint_words(env, arg, &signBit, &wordCount, reinterpret_cast<uint64_t *>(retArr)) != napi_ok) {
214 HcfFree(retArr);
215 LOGE("failed to get valid rawData.");
216 return false;
217 }
218 if (signBit != 0) {
219 HcfFree(retArr);
220 LOGE("failed to get gegative rawData.");
221 return false;
222 }
223 bigInt->data = retArr;
224 bigInt->len = length;
225 return true;
226 }
227
GetPointFromNapiValue(napi_env env,napi_value arg,HcfPoint * point)228 static bool GetPointFromNapiValue(napi_env env, napi_value arg, HcfPoint *point)
229 {
230 if ((env == nullptr) || (arg == nullptr)) {
231 LOGE("Invalid parmas!");
232 return false;
233 }
234 napi_value dataX = nullptr;
235 napi_value dataY = nullptr;
236 napi_valuetype valueType = napi_undefined;
237 napi_status status = napi_get_named_property(env, arg, "x", &dataX);
238 napi_typeof(env, dataX, &valueType);
239 if ((status != napi_ok) || (dataX == nullptr) || (valueType == napi_undefined)) {
240 LOGE("failed to get valid algo name!");
241 return false;
242 }
243 status = napi_get_named_property(env, arg, "y", &dataY);
244 napi_typeof(env, dataY, &valueType);
245 if ((status != napi_ok) || (dataY == nullptr) || (valueType == napi_undefined)) {
246 LOGE("failed to get valid algo name!");
247 return false;
248 }
249
250 bool ret = GetBigIntFromNapiValue(env, dataX, &point->x);
251 if (!ret) {
252 LOGE("get point x failed!");
253 return false;
254 }
255 ret = GetBigIntFromNapiValue(env, dataY, &point->y);
256 if (!ret) {
257 LOGE("get point y failed!");
258 HcfFree((point->x).data);
259 (point->x).data = nullptr;
260 return false;
261 }
262 return true;
263 }
264
GetIvParamsSpecType()265 static const char *GetIvParamsSpecType()
266 {
267 return IV_PARAMS_SPEC.c_str();
268 }
269
GetGcmParamsSpecType()270 static const char *GetGcmParamsSpecType()
271 {
272 return GCM_PARAMS_SPEC.c_str();
273 }
274
GetCcmParamsSpecType()275 static const char *GetCcmParamsSpecType()
276 {
277 return CCM_PARAMS_SPEC.c_str();
278 }
279
GetBlobFromParamsSpec(napi_env env,napi_value arg,const string & type)280 static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, const string &type)
281 {
282 napi_value data = nullptr;
283 HcfBlob *blob = nullptr;
284 napi_valuetype valueType = napi_undefined;
285
286 napi_status status = napi_get_named_property(env, arg, type.c_str(), &data);
287 napi_typeof(env, data, &valueType);
288 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
289 LOGE("failed to get valid param property!");
290 return nullptr;
291 }
292 blob = GetBlobFromNapiValue(env, data);
293 if (blob == nullptr) {
294 LOGE("GetBlobFromNapiValue failed!");
295 return nullptr;
296 }
297 return blob;
298 }
299
GetIvParamsSpec(napi_env env,napi_value arg,HcfParamsSpec ** paramsSpec)300 static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **paramsSpec)
301 {
302 HcfIvParamsSpec *ivParamsSpec = reinterpret_cast<HcfIvParamsSpec *>(HcfMalloc(sizeof(HcfIvParamsSpec), 0));
303 if (ivParamsSpec == nullptr) {
304 LOGE("ivParamsSpec malloc failed!");
305 return false;
306 }
307
308 HcfBlob *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
309 if (iv == nullptr) {
310 LOGE("GetBlobFromNapiValue failed!");
311 HcfFree(ivParamsSpec);
312 return false;
313 }
314 ivParamsSpec->base.getType = GetIvParamsSpecType;
315 ivParamsSpec->iv = *iv;
316 *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ivParamsSpec);
317 HcfFree(iv);
318 return true;
319 }
320
GetIvAndAadBlob(napi_env env,napi_value arg,HcfBlob ** iv,HcfBlob ** aad)321 static bool GetIvAndAadBlob(napi_env env, napi_value arg, HcfBlob **iv, HcfBlob **aad)
322 {
323 *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
324 if (*iv == nullptr) {
325 LOGE("get iv failed!");
326 return false;
327 }
328
329 *aad = GetBlobFromParamsSpec(env, arg, AAD_PARAMS);
330 if (*aad == nullptr) {
331 LOGE("get aad failed!");
332 HcfFree((*iv)->data);
333 HcfFree(*iv);
334 return false;
335 }
336 return true;
337 }
338
GetGcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)339 static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
340 {
341 HcfBlob *iv = nullptr;
342 HcfBlob *aad = nullptr;
343 HcfBlob *tag = nullptr;
344 HcfBlob authTag = {};
345 bool ret = false;
346
347 HcfGcmParamsSpec *gcmParamsSpec = reinterpret_cast<HcfGcmParamsSpec *>(HcfMalloc(sizeof(HcfGcmParamsSpec), 0));
348 if (gcmParamsSpec == nullptr) {
349 LOGE("gcmParamsSpec malloc failed!");
350 return false;
351 }
352
353 ret = GetIvAndAadBlob(env, arg, &iv, &aad);
354 if (!ret) {
355 LOGE("GetIvAndAadBlob failed!");
356 goto clearup;
357 }
358
359 if (opMode == DECRYPT_MODE) {
360 tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
361 if (tag == nullptr) {
362 LOGE("get tag failed!");
363 goto clearup;
364 }
365 } else if (opMode == ENCRYPT_MODE) {
366 authTag.data = static_cast<uint8_t *>(HcfMalloc(GCM_AUTH_TAG_LEN, 0));
367 if (authTag.data == nullptr) {
368 LOGE("get tag failed!");
369 goto clearup;
370 }
371 authTag.len = GCM_AUTH_TAG_LEN;
372 } else {
373 goto clearup;
374 }
375
376 gcmParamsSpec->base.getType = GetGcmParamsSpecType;
377 gcmParamsSpec->iv = *iv;
378 gcmParamsSpec->aad = *aad;
379 gcmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
380 *paramsSpec = reinterpret_cast<HcfParamsSpec *>(gcmParamsSpec);
381 ret = true;
382 clearup:
383 if (!ret) {
384 HcfBlobDataFree(iv);
385 HcfBlobDataFree(aad);
386 HcfBlobDataFree(tag);
387 HcfFree(gcmParamsSpec);
388 }
389 HcfFree(iv);
390 HcfFree(aad);
391 HcfFree(tag);
392 return ret;
393 }
394
GetCcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)395 static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
396 {
397 HcfBlob *iv = nullptr;
398 HcfBlob *aad = nullptr;
399 HcfBlob *tag = nullptr;
400 HcfBlob authTag = {};
401 bool ret = false;
402
403 HcfCcmParamsSpec *ccmParamsSpec = reinterpret_cast<HcfCcmParamsSpec *>(HcfMalloc(sizeof(HcfCcmParamsSpec), 0));
404 if (ccmParamsSpec == nullptr) {
405 LOGE("ccmParamsSpec malloc failed!");
406 return ret;
407 }
408 ret = GetIvAndAadBlob(env, arg, &iv, &aad);
409 if (!ret) {
410 LOGE("GetIvAndAadBlob failed!");
411 goto clearup;
412 }
413
414 if (opMode == DECRYPT_MODE) {
415 tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
416 if (tag == nullptr) {
417 LOGE("get tag failed!");
418 goto clearup;
419 }
420 } else if (opMode == ENCRYPT_MODE) {
421 authTag.data = static_cast<uint8_t *>(HcfMalloc(CCM_AUTH_TAG_LEN, 0));
422 if (authTag.data == nullptr) {
423 LOGE("get tag failed!");
424 goto clearup;
425 }
426 authTag.len = CCM_AUTH_TAG_LEN;
427 } else {
428 goto clearup;
429 }
430 ccmParamsSpec->base.getType = GetCcmParamsSpecType;
431 ccmParamsSpec->iv = *iv;
432 ccmParamsSpec->aad = *aad;
433 ccmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
434 *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ccmParamsSpec);
435 ret = true;
436 clearup:
437 if (!ret) {
438 HcfBlobDataFree(iv);
439 HcfBlobDataFree(aad);
440 HcfBlobDataFree(tag);
441 HcfFree(ccmParamsSpec);
442 }
443 HcfFree(iv);
444 HcfFree(aad);
445 HcfFree(tag);
446 return ret;
447 }
448
GetParamsSpecFromNapiValue(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)449 bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
450 {
451 napi_value data = nullptr;
452 napi_valuetype valueType = napi_undefined;
453 if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) {
454 LOGE("Invalid parmas!");
455 return false;
456 }
457
458 napi_status status = napi_get_named_property(env, arg, ALGO_PARAMS.c_str(), &data);
459 napi_typeof(env, data, &valueType);
460 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
461 status = napi_get_named_property(env, arg, ALGO_PARAMS_OLD.c_str(), &data);
462 napi_typeof(env, data, &valueType);
463 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
464 LOGE("failed to get valid algo name!");
465 return false;
466 }
467 }
468 string algoName;
469 if (!GetStringFromJSParams(env, data, algoName)) {
470 LOGE("GetStringFromJSParams failed!");
471 return false;
472 }
473 if (algoName.compare(IV_PARAMS_SPEC) == 0) {
474 return GetIvParamsSpec(env, arg, paramsSpec);
475 } else if (algoName.compare(GCM_PARAMS_SPEC) == 0) {
476 return GetGcmParamsSpec(env, arg, opMode, paramsSpec);
477 } else if (algoName.compare(CCM_PARAMS_SPEC) == 0) {
478 return GetCcmParamsSpec(env, arg, opMode, paramsSpec);
479 } else {
480 return false;
481 }
482 }
483
GetDetailAsyKeySpecValue(napi_env env,napi_value arg,string argName)484 static napi_value GetDetailAsyKeySpecValue(napi_env env, napi_value arg, string argName)
485 {
486 napi_value data = nullptr;
487 napi_valuetype valueType = napi_undefined;
488 napi_status status = napi_get_named_property(env, arg, argName.c_str(), &data);
489 napi_typeof(env, data, &valueType);
490 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
491 LOGE("failed to get valid algo name!");
492 return nullptr;
493 }
494 return data;
495 }
496
GetCommSpecNapiValue(napi_env env,napi_value arg)497 static napi_value GetCommSpecNapiValue(napi_env env, napi_value arg)
498 {
499 napi_value data = nullptr;
500 napi_valuetype valueType = napi_undefined;
501
502 napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_COMM_PARAMS.c_str(), &data);
503 napi_typeof(env, data, &valueType);
504
505 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
506 LOGE("failed to get valid algo name!");
507 return nullptr;
508 }
509 return data;
510 }
511
InitDsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfDsaCommParamsSpec * spec)512 static bool InitDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfDsaCommParamsSpec *spec)
513 {
514 size_t algNameLen = DSA_ASY_KEY_SPEC.length();
515 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
516 if (spec->base.algName == nullptr) {
517 LOGE("malloc DSA algName failed!");
518 return false;
519 }
520 (void)memcpy_s(spec->base.algName, algNameLen+ 1, DSA_ASY_KEY_SPEC.c_str(), algNameLen);
521 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
522
523 napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
524 napi_value q = GetDetailAsyKeySpecValue(env, arg, "q");
525 napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
526 bool ret = GetBigIntFromNapiValue(env, p, &spec->p);
527 if (!ret) {
528 HcfFree(spec->base.algName);
529 spec->base.algName = nullptr;
530 return false;
531 }
532 ret = GetBigIntFromNapiValue(env, q, &spec->q);
533 if (!ret) {
534 FreeDsaCommParamsSpec(spec);
535 return false;
536 }
537 ret = GetBigIntFromNapiValue(env, g, &spec->g);
538 if (!ret) {
539 FreeDsaCommParamsSpec(spec);
540 return false;
541 }
542 return true;
543 }
544
GetDsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)545 static bool GetDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
546 {
547 HcfDsaCommParamsSpec *spec = reinterpret_cast<HcfDsaCommParamsSpec *>(HcfMalloc(sizeof(HcfDsaCommParamsSpec), 0));
548 if (spec == nullptr) {
549 LOGE("malloc falied!");
550 return false;
551 }
552 if (!InitDsaCommonAsyKeySpec(env, arg, spec)) {
553 LOGE("InitDsaCommonAsyKeySpec failed!");
554 HcfFree(spec);
555 return false;
556 }
557 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
558 return true;
559 }
560
GetDsaPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)561 static bool GetDsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
562 {
563 HcfDsaPubKeyParamsSpec *spec = reinterpret_cast<HcfDsaPubKeyParamsSpec *>(
564 HcfMalloc(sizeof(HcfDsaPubKeyParamsSpec), 0));
565 if (spec == nullptr) {
566 LOGE("malloc falied!");
567 return false;
568 }
569
570 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
571 if (commSpecValue == nullptr) {
572 LOGE("Get comm spec napi value failed.");
573 HcfFree(spec);
574 return false;
575 }
576 if (!InitDsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDsaCommParamsSpec *>(spec))) {
577 LOGE("InitDsaCommonAsyKeySpec failed.");
578 HcfFree(spec);
579 return false;
580 }
581 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
582
583 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
584 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
585 if (!ret) {
586 DestroyDsaPubKeySpec(spec);
587 return false;
588 }
589 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
590 return true;
591 }
592
GetDsaKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)593 static bool GetDsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
594 {
595 HcfDsaKeyPairParamsSpec *spec = reinterpret_cast<HcfDsaKeyPairParamsSpec *>(
596 HcfMalloc(sizeof(HcfDsaKeyPairParamsSpec), 0));
597 if (spec == nullptr) {
598 LOGE("malloc falied!");
599 return false;
600 }
601
602 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
603 if (commSpecValue == nullptr) {
604 LOGE("Get comm spec napi value failed.");
605 HcfFree(spec);
606 return false;
607 }
608 if (!InitDsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDsaCommParamsSpec *>(spec))) {
609 LOGE("InitDsaCommonAsyKeySpec failed!");
610 HcfFree(spec);
611 return false;
612 }
613 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
614
615 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
616 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
617 if (!ret) {
618 FreeDsaCommParamsSpec(reinterpret_cast<HcfDsaCommParamsSpec *>(spec));
619 HcfFree(spec);
620 return false;
621 }
622 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
623 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
624 if (!ret) {
625 FreeDsaCommParamsSpec(reinterpret_cast<HcfDsaCommParamsSpec *>(spec));
626 HcfFree(spec->pk.data);
627 HcfFree(spec);
628 return false;
629 }
630 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
631 return true;
632 }
633
GetDsaAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)634 static bool GetDsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
635 {
636 napi_value data = nullptr;
637 napi_valuetype valueType = napi_undefined;
638
639 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
640 napi_typeof(env, data, &valueType);
641 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
642 LOGE("failed to get valid algo name!");
643 return false;
644 }
645 HcfAsyKeySpecType asyKeySpecType;
646 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
647 if (status != napi_ok) {
648 LOGE("failed to get valid asyKeySpecType!");
649 return false;
650 }
651 if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
652 return GetDsaCommonAsyKeySpec(env, arg, asyKeySpec);
653 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
654 return GetDsaPubKeySpec(env, arg, asyKeySpec);
655 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
656 return GetDsaKeyPairAsyKeySpec(env, arg, asyKeySpec);
657 } else {
658 return false;
659 }
660 }
661
GetFpField(napi_env env,napi_value arg,HcfECField ** ecField)662 static bool GetFpField(napi_env env, napi_value arg, HcfECField **ecField)
663 {
664 HcfECFieldFp *fp = reinterpret_cast<HcfECFieldFp *>(HcfMalloc(sizeof(HcfECFieldFp), 0));
665 if (fp == nullptr) {
666 LOGE("malloc fp failed!");
667 return false;
668 }
669
670 size_t fieldTpyeLen = ECC_FIELD_TYPE_FP.length();
671 fp->base.fieldType = static_cast<char *>(HcfMalloc(fieldTpyeLen + 1, 0));
672 if (fp->base.fieldType == nullptr) {
673 LOGE("malloc fieldType failed!");
674 HcfFree(fp);
675 return false;
676 }
677 (void)memcpy_s(fp->base.fieldType, fieldTpyeLen+ 1, ECC_FIELD_TYPE_FP.c_str(), fieldTpyeLen);
678
679 napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
680 bool ret = GetBigIntFromNapiValue(env, p, &fp->p);
681 if (!ret) {
682 HcfFree(fp->base.fieldType);
683 HcfFree(fp);
684 return false;
685 }
686 *ecField = reinterpret_cast<HcfECField *>(fp);
687 return true;
688 }
689
GetField(napi_env env,napi_value arg,HcfECField ** ecField)690 static bool GetField(napi_env env, napi_value arg, HcfECField **ecField)
691 {
692 // get fieldData in { field : fieldData, a : xxx, b : xxx, ... } of ECCCommonParamsSpec first
693 napi_value fieldData = nullptr;
694 napi_valuetype valueType = napi_undefined;
695 napi_status status = napi_get_named_property(env, arg, "field", &fieldData);
696 napi_typeof(env, fieldData, &valueType);
697 if ((status != napi_ok) || (fieldData == nullptr) || (valueType == napi_undefined)) {
698 LOGE("failed to get valid field data!");
699 return false;
700 }
701
702 // get fieldType in { fieldType : fieldTypeData } of ECField
703 napi_value fieldTypeData = nullptr;
704 status = napi_get_named_property(env, fieldData, "fieldType", &fieldTypeData);
705 napi_typeof(env, fieldTypeData, &valueType);
706 if ((status != napi_ok) || (fieldTypeData == nullptr) || (valueType == napi_undefined)) {
707 LOGE("failed to get valid fieldType data!");
708 return false;
709 }
710 string fieldType;
711 if (!GetStringFromJSParams(env, fieldTypeData, fieldType)) {
712 LOGE("GetStringFromJSParams failed when extracting fieldType!");
713 return false;
714 }
715
716 // get p in { p : pData } of ECField, and generateECField
717 if (fieldType.compare("Fp") == 0) {
718 return GetFpField(env, fieldData, ecField);
719 }
720 return false;
721 }
722
InitEccDetailAsyKeySpec(napi_env env,napi_value arg,HcfEccCommParamsSpec * spec)723 static bool InitEccDetailAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec)
724 {
725 napi_value a = GetDetailAsyKeySpecValue(env, arg, "a");
726 napi_value b = GetDetailAsyKeySpecValue(env, arg, "b");
727 napi_value n = GetDetailAsyKeySpecValue(env, arg, "n");
728 napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
729 bool ret = GetBigIntFromNapiValue(env, a, &spec->a);
730 if (!ret) {
731 LOGE("get ecc asyKeySpec a failed!");
732 return false;
733 }
734 ret = GetBigIntFromNapiValue(env, b, &spec->b);
735 if (!ret) {
736 LOGE("get ecc asyKeySpec b failed!");
737 return false;
738 }
739 ret = GetBigIntFromNapiValue(env, n, &spec->n);
740 if (!ret) {
741 LOGE("get ecc asyKeySpec n failed!");
742 return false;
743 }
744 ret = GetPointFromNapiValue(env, g, &spec->g);
745 if (!ret) {
746 LOGE("get ecc asyKeySpec g failed!");
747 return false;
748 }
749 return true;
750 }
751
InitEccCommonAsyKeySpec(napi_env env,napi_value arg,HcfEccCommParamsSpec * spec)752 static bool InitEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec)
753 {
754 size_t algNameLen = ECC_ASY_KEY_SPEC.length();
755 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
756 if (spec->base.algName == nullptr) {
757 LOGE("malloc ECC algName failed!");
758 return false;
759 }
760 (void)memcpy_s(spec->base.algName, algNameLen+ 1, ECC_ASY_KEY_SPEC.c_str(), algNameLen);
761 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
762
763 // get h
764 napi_value hData = nullptr;
765 napi_valuetype valueType = napi_undefined;
766 napi_status status = napi_get_named_property(env, arg, "h", &hData);
767 napi_typeof(env, hData, &valueType);
768 if ((status != napi_ok) || (hData == nullptr) || (valueType == napi_undefined)) {
769 LOGE("failed to get valid h!");
770 HcfFree(spec->base.algName);
771 spec->base.algName = nullptr;
772 return false;
773 }
774 if (!GetInt32FromJSParams(env, hData, spec->h)) {
775 LOGE("get ecc asyKeySpec h failed!");
776 HcfFree(spec->base.algName);
777 spec->base.algName = nullptr;
778 return false;
779 }
780 // get field
781 if (!GetField(env, arg, &spec->field)) {
782 LOGE("GetField failed!");
783 HcfFree(spec->base.algName);
784 spec->base.algName = nullptr;
785 return false;
786 }
787 bool ret = InitEccDetailAsyKeySpec(env, arg, spec);
788 if (!ret) {
789 LOGE("get ecc asyKeySpec g failed!");
790 FreeEccCommParamsSpec(spec);
791 return false;
792 }
793 return true;
794 }
795
GetEccCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)796 static bool GetEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
797 {
798 HcfEccCommParamsSpec *spec = reinterpret_cast<HcfEccCommParamsSpec *>(HcfMalloc(sizeof(HcfEccCommParamsSpec), 0));
799 if (spec == nullptr) {
800 LOGE("malloc falied!");
801 return false;
802 }
803 if (!InitEccCommonAsyKeySpec(env, arg, spec)) {
804 LOGE("InitEccCommonAsyKeySpec failed!");
805 HcfFree(spec);
806 return false;
807 }
808 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
809 return true;
810 }
811
GetEccPriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)812 static bool GetEccPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
813 {
814 HcfEccPriKeyParamsSpec *spec =
815 reinterpret_cast<HcfEccPriKeyParamsSpec *>(HcfMalloc(sizeof(HcfEccPriKeyParamsSpec), 0));
816 if (spec == nullptr) {
817 LOGE("malloc falied!");
818 return false;
819 }
820
821 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
822 if (commSpecValue == nullptr) {
823 LOGE("Get comm spec napi value failed.");
824 HcfFree(spec);
825 return false;
826 }
827 if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec))) {
828 LOGE("InitEccCommonAsyKeySpec failed!");
829 HcfFree(spec);
830 return false;
831 }
832 spec->base.base.specType = HCF_PRIVATE_KEY_SPEC;
833
834 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
835 bool ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
836 if (!ret) {
837 // get big int fail, sk is null
838 FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
839 HcfFree(spec);
840 return false;
841 }
842 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
843 return true;
844 }
845
GetEccPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)846 static bool GetEccPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
847 {
848 HcfEccPubKeyParamsSpec *spec =
849 reinterpret_cast<HcfEccPubKeyParamsSpec *>(HcfMalloc(sizeof(HcfEccPubKeyParamsSpec), 0));
850 if (spec == nullptr) {
851 LOGE("malloc falied!");
852 return false;
853 }
854
855 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
856 if (commSpecValue == nullptr) {
857 LOGE("Get comm spec napi value failed.");
858 HcfFree(spec);
859 return false;
860 }
861 if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec))) {
862 LOGE("InitEccCommonAsyKeySpec failed!");
863 HcfFree(spec);
864 return false;
865 }
866 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
867
868 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
869 bool ret = GetPointFromNapiValue(env, pk, &spec->pk);
870 if (!ret) {
871 DestroyEccPubKeySpec(spec);
872 return false;
873 }
874 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
875 return true;
876 }
877
GetEccKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)878 static bool GetEccKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
879 {
880 HcfEccKeyPairParamsSpec *spec =
881 reinterpret_cast<HcfEccKeyPairParamsSpec *>(HcfMalloc(sizeof(HcfEccKeyPairParamsSpec), 0));
882 if (spec == nullptr) {
883 LOGE("malloc falied!");
884 return false;
885 }
886
887 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
888 if (commSpecValue == nullptr) {
889 LOGE("Get comm spec napi value failed.");
890 HcfFree(spec);
891 return false;
892 }
893 if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec))) {
894 LOGE("InitEccCommonAsyKeySpec failed!");
895 HcfFree(spec);
896 return false;
897 }
898 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
899
900 // get big int fail, sk is null
901 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
902 bool ret = GetPointFromNapiValue(env, pk, &spec->pk);
903 if (!ret) {
904 FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
905 HcfFree(spec);
906 return false;
907 }
908 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
909 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
910 if (!ret) {
911 FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
912 HcfFree(spec->pk.x.data);
913 HcfFree(spec->pk.y.data);
914 HcfFree(spec);
915 return false;
916 }
917 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
918 return true;
919 }
920
GetEccAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)921 static bool GetEccAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
922 {
923 napi_value data = nullptr;
924 napi_valuetype valueType = napi_undefined;
925
926 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
927 napi_typeof(env, data, &valueType);
928 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
929 LOGE("failed to get valid algo name!");
930 return false;
931 }
932 HcfAsyKeySpecType asyKeySpecType;
933 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
934 if (status != napi_ok) {
935 LOGE("failed to get valid asyKeySpecType!");
936 return false;
937 }
938 if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
939 return GetEccCommonAsyKeySpec(env, arg, asyKeySpec);
940 } else if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
941 return GetEccPriKeySpec(env, arg, asyKeySpec);
942 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
943 return GetEccPubKeySpec(env, arg, asyKeySpec);
944 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
945 return GetEccKeyPairAsyKeySpec(env, arg, asyKeySpec);
946 } else {
947 return false;
948 }
949 }
950
InitRsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfRsaCommParamsSpec * spec)951 static bool InitRsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfRsaCommParamsSpec *spec)
952 {
953 size_t algNameLen = RSA_ASY_KEY_SPEC.length();
954 spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
955 if (spec->base.algName == nullptr) {
956 LOGE("malloc RSA algName failed!");
957 return false;
958 }
959 (void)memcpy_s(spec->base.algName, algNameLen+ 1, RSA_ASY_KEY_SPEC.c_str(), algNameLen);
960 spec->base.specType = HCF_COMMON_PARAMS_SPEC;
961
962 napi_value n = GetDetailAsyKeySpecValue(env, arg, "n");
963
964 bool ret = GetBigIntFromNapiValue(env, n, &spec->n);
965 if (!ret) {
966 LOGE("Rsa asyKeySpec get n failed!");
967 HcfFree(spec->base.algName);
968 spec->base.algName = nullptr;
969 return false;
970 }
971 return true;
972 }
973
GetRsaPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)974 static bool GetRsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
975 {
976 HcfRsaPubKeyParamsSpec *spec =
977 reinterpret_cast<HcfRsaPubKeyParamsSpec *>(HcfMalloc(sizeof(HcfRsaPubKeyParamsSpec), 0));
978 if (spec == nullptr) {
979 LOGE("malloc falied!");
980 return false;
981 }
982
983 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
984 if (commSpecValue == nullptr) {
985 LOGE("Get comm spec napi value failed.");
986 HcfFree(spec);
987 return false;
988 }
989 if (!InitRsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfRsaCommParamsSpec *>(spec))) {
990 LOGE("InitRsaCommonAsyKeySpec failed!");
991 HcfFree(spec);
992 return false;
993 }
994 spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
995
996 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
997 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
998 if (!ret) {
999 DestroyRsaPubKeySpec(spec);
1000 return false;
1001 }
1002 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1003 return true;
1004 }
1005
GetRsaKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1006 static bool GetRsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1007 {
1008 HcfRsaKeyPairParamsSpec *spec =
1009 reinterpret_cast<HcfRsaKeyPairParamsSpec *>(HcfMalloc(sizeof(HcfRsaKeyPairParamsSpec), 0));
1010 if (spec == nullptr) {
1011 LOGE("malloc falied!");
1012 return false;
1013 }
1014
1015 napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1016 if (commSpecValue == nullptr) {
1017 LOGE("Get comm spec napi value failed.");
1018 HcfFree(spec);
1019 return false;
1020 }
1021 if (!InitRsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfRsaCommParamsSpec *>(spec))) {
1022 LOGE("InitRsaCommonAsyKeySpec failed!");
1023 HcfFree(spec);
1024 return false;
1025 }
1026 spec->base.base.specType = HCF_KEY_PAIR_SPEC;
1027
1028 napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1029 bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1030 if (!ret) {
1031 FreeRsaCommParamsSpec(&(spec->base));
1032 HcfFree(spec);
1033 return false;
1034 }
1035 napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1036 ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1037 if (!ret) {
1038 FreeRsaCommParamsSpec(&(spec->base));
1039 HcfFree(spec->pk.data);
1040 HcfFree(spec);
1041 return false;
1042 }
1043 *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1044 return true;
1045 }
1046
GetRsaAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1047 static bool GetRsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1048 {
1049 napi_value data = nullptr;
1050 napi_valuetype valueType = napi_undefined;
1051
1052 napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1053 napi_typeof(env, data, &valueType);
1054 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1055 LOGE("failed to get valid algo name!");
1056 return false;
1057 }
1058 HcfAsyKeySpecType asyKeySpecType;
1059 status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1060 if (status != napi_ok) {
1061 LOGE("failed to get valid asyKeySpecType!");
1062 return false;
1063 }
1064 if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
1065 LOGE("RSA not support comm key spec");
1066 return false;
1067 } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1068 return GetRsaPubKeySpec(env, arg, asyKeySpec);
1069 } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1070 return GetRsaKeyPairAsyKeySpec(env, arg, asyKeySpec);
1071 } else {
1072 return false;
1073 }
1074 }
1075
GetAsyKeySpecFromNapiValue(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1076 bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1077 {
1078 napi_value data = nullptr;
1079
1080 if ((env == nullptr) || (arg == nullptr) || (asyKeySpec == nullptr)) {
1081 LOGE("Invalid parmas!");
1082 return false;
1083 }
1084
1085 napi_valuetype valueType = napi_undefined;
1086
1087 napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_ALG_NAME.c_str(), &data);
1088 napi_typeof(env, data, &valueType);
1089 if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1090 LOGE("failed to get valid algName!");
1091 return false;
1092 }
1093 string algName;
1094 if (!GetStringFromJSParams(env, data, algName)) {
1095 LOGE("GetStringFromJSParams failed!");
1096 return false;
1097 }
1098 if (algName.compare(DSA_ASY_KEY_SPEC) == 0) {
1099 return GetDsaAsyKeySpec(env, arg, asyKeySpec);
1100 } else if (algName.compare(ECC_ASY_KEY_SPEC) == 0) {
1101 return GetEccAsyKeySpec(env, arg, asyKeySpec);
1102 } else if (algName.compare(RSA_ASY_KEY_SPEC) == 0) {
1103 return GetRsaAsyKeySpec(env, arg, asyKeySpec);
1104 } else {
1105 LOGE("GetAsyKeySpecFromNapiValue fialed, algName not support.");
1106 return false;
1107 }
1108 }
1109
ConvertBlobToNapiValue(napi_env env,HcfBlob * blob)1110 napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob)
1111 {
1112 if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1113 LOGE("Invalid blob!");
1114 return NapiGetNull(env);
1115 }
1116 uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1117 if (buffer == nullptr) {
1118 LOGE("malloc uint8 array buffer failed!");
1119 return NapiGetNull(env);
1120 }
1121
1122 if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
1123 LOGE("memcpy_s data to buffer failed!");
1124 HcfFree(buffer);
1125 return NapiGetNull(env);
1126 }
1127
1128 napi_value outBuffer = nullptr;
1129 napi_status status = napi_create_external_arraybuffer(
1130 env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1131 if (status != napi_ok) {
1132 LOGE("create uint8 array buffer failed!");
1133 HcfFree(buffer);
1134 return NapiGetNull(env);
1135 }
1136 buffer = nullptr;
1137
1138 napi_value outData = nullptr;
1139 napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1140 napi_value dataBlob = nullptr;
1141 napi_create_object(env, &dataBlob);
1142 napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
1143
1144 return dataBlob;
1145 }
1146
ConvertCipherBlobToNapiValue(napi_env env,HcfBlob * blob)1147 napi_value ConvertCipherBlobToNapiValue(napi_env env, HcfBlob *blob)
1148 {
1149 if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1150 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1151 LOGE("Invalid blob!");
1152 return NapiGetNull(env);
1153 }
1154 uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1155 if (buffer == nullptr) {
1156 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc uint8 array buffer failed!"));
1157 LOGE("malloc uint8 array buffer failed!");
1158 return NapiGetNull(env);
1159 }
1160
1161 if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
1162 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "memcpy_s data to buffer failed!"));
1163 LOGE("memcpy_s data to buffer failed!");
1164 HcfFree(buffer);
1165 return NapiGetNull(env);
1166 }
1167
1168 napi_value outBuffer = nullptr;
1169 napi_status status = napi_create_external_arraybuffer(
1170 env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1171 if (status != napi_ok) {
1172 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create uint8 array buffer failed!"));
1173 LOGE("create uint8 array buffer failed!");
1174 HcfFree(buffer);
1175 return NapiGetNull(env);
1176 }
1177 buffer = nullptr;
1178
1179 napi_value outData = nullptr;
1180 napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1181 return outData;
1182 }
1183
ConvertBigIntToNapiValue(napi_env env,HcfBigInteger * blob)1184 napi_value ConvertBigIntToNapiValue(napi_env env, HcfBigInteger *blob)
1185 {
1186 if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1187 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1188 LOGE("Invalid blob!");
1189 return NapiGetNull(env);
1190 }
1191 size_t wordsCount = (blob->len / sizeof(uint64_t)) + ((blob->len % sizeof(uint64_t)) == 0 ? 0 : 1);
1192 uint64_t *words = reinterpret_cast<uint64_t *>(HcfMalloc(wordsCount * sizeof(uint64_t), 0));
1193 if (words == nullptr) {
1194 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc uint8 array buffer failed!"));
1195 LOGE("malloc uint8 array buffer failed!");
1196 return NapiGetNull(env);
1197 }
1198
1199 size_t index = 0;
1200 for (size_t i = 0; index < wordsCount; i += sizeof(uint64_t), index++) {
1201 uint64_t tmp = 0;
1202 for (size_t j = 0; j < sizeof(uint64_t); j++) {
1203 if (i + j < blob->len) {
1204 tmp += ((uint64_t)blob->data[i + j] << (sizeof(uint64_t) * j));
1205 }
1206 }
1207 words[index] = tmp;
1208 }
1209 napi_value bigInt = nullptr;
1210 napi_status status = napi_create_bigint_words(env, 0, wordsCount, words, &bigInt);
1211 if (status != napi_ok) {
1212 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create bigint failed!"));
1213 LOGE("create bigint failed!");
1214 HcfFree(words);
1215 return NapiGetNull(env);
1216 }
1217 if (bigInt == nullptr) {
1218 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "bigInt is null!"));
1219 LOGE("bigInt is null!");
1220 }
1221 HcfFree(words);
1222 words = nullptr;
1223 return bigInt;
1224 }
1225
GetStringFromJSParams(napi_env env,napi_value arg,string & returnStr)1226 bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr)
1227 {
1228 napi_valuetype valueType;
1229 napi_typeof(env, arg, &valueType);
1230 if (valueType != napi_string) {
1231 LOGE("wrong argument type. expect string type.");
1232 return false;
1233 }
1234
1235 size_t length = 0;
1236 if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
1237 LOGE("can not get string length");
1238 return false;
1239 }
1240 returnStr.reserve(length + 1);
1241 returnStr.resize(length);
1242 if (napi_get_value_string_utf8(env, arg, returnStr.data(), (length + 1), &length) != napi_ok) {
1243 LOGE("can not get string value");
1244 return false;
1245 }
1246 return true;
1247 }
1248
GetInt32FromJSParams(napi_env env,napi_value arg,int32_t & returnInt)1249 bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt)
1250 {
1251 napi_valuetype valueType;
1252 napi_typeof(env, arg, &valueType);
1253 if (valueType != napi_number) {
1254 LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
1255 return false;
1256 }
1257
1258 if (napi_get_value_int32(env, arg, &returnInt) != napi_ok) {
1259 LOGE("can not get int value");
1260 return false;
1261 }
1262 return true;
1263 }
1264
GetUint32FromJSParams(napi_env env,napi_value arg,uint32_t & returnInt)1265 bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt)
1266 {
1267 napi_valuetype valueType;
1268 napi_typeof(env, arg, &valueType);
1269 if (valueType != napi_number) {
1270 LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
1271 return false;
1272 }
1273
1274 if (napi_get_value_uint32(env, arg, &returnInt) != napi_ok) {
1275 LOGE("can not get int value");
1276 return false;
1277 }
1278 return true;
1279 }
1280
GetCallbackFromJSParams(napi_env env,napi_value arg,napi_ref * returnCb)1281 bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb)
1282 {
1283 napi_valuetype valueType = napi_undefined;
1284 napi_typeof(env, arg, &valueType);
1285 if (valueType != napi_function) {
1286 LOGE("wrong argument type. expect callback type. [Type]: %d", valueType);
1287 return false;
1288 }
1289
1290 napi_create_reference(env, arg, 1, returnCb);
1291 return true;
1292 }
1293
GetJsErrValueByErrCode(HcfResult errCode)1294 static uint32_t GetJsErrValueByErrCode(HcfResult errCode)
1295 {
1296 switch (errCode) {
1297 case HCF_INVALID_PARAMS:
1298 return JS_ERR_INVALID_PARAMS;
1299 case HCF_NOT_SUPPORT:
1300 return JS_ERR_NOT_SUPPORT;
1301 case HCF_ERR_MALLOC:
1302 return JS_ERR_OUT_OF_MEMORY;
1303 case HCF_ERR_COPY:
1304 return JS_ERR_RUNTIME_ERROR;
1305 case HCF_ERR_CRYPTO_OPERATION:
1306 return JS_ERR_CRYPTO_OPERATION;
1307 default:
1308 return JS_ERR_DEFAULT_ERR;
1309 }
1310 }
1311
GenerateBusinessError(napi_env env,HcfResult errCode,const char * errMsg)1312 napi_value GenerateBusinessError(napi_env env, HcfResult errCode, const char *errMsg)
1313 {
1314 napi_value businessError = nullptr;
1315
1316 napi_value code = nullptr;
1317 napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code);
1318
1319 napi_value msg = nullptr;
1320 napi_create_string_utf8(env, errMsg, NAPI_AUTO_LENGTH, &msg);
1321
1322 napi_create_error(env, nullptr, msg, &businessError);
1323 napi_set_named_property(env, businessError, CRYPTO_TAG_ERR_CODE.c_str(), code);
1324
1325 return businessError;
1326 }
1327
CheckArgsCount(napi_env env,size_t argc,size_t expectedCount,bool isSync)1328 bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync)
1329 {
1330 if (isSync) {
1331 if (argc != expectedCount) {
1332 LOGE("invalid params count!");
1333 return false;
1334 }
1335 } else {
1336 if ((argc != expectedCount) && (argc != (expectedCount - ARGS_SIZE_ONE))) {
1337 LOGE("invalid params count!");
1338 return false;
1339 }
1340 }
1341 return true;
1342 }
1343
isCallback(napi_env env,napi_value argv,size_t argc,size_t expectedArgc)1344 bool isCallback(napi_env env, napi_value argv, size_t argc, size_t expectedArgc)
1345 {
1346 if (argc == expectedArgc - 1) {
1347 return false;
1348 }
1349 napi_valuetype valueType = napi_undefined;
1350 napi_typeof(env, argv, &valueType);
1351 if (valueType == napi_undefined || valueType == napi_null) {
1352 return false;
1353 }
1354 return true;
1355 }
1356
GetResourceName(napi_env env,const char * name)1357 napi_value GetResourceName(napi_env env, const char *name)
1358 {
1359 napi_value resourceName = nullptr;
1360 napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &resourceName);
1361 return resourceName;
1362 }
1363 } // namespace CryptoFramework
1364 } // namespace OHOS
1365