• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "cipher.h"
22 #include "napi_crypto_framework_defines.h"
23 #include "detailed_iv_params.h"
24 #include "detailed_gcm_params.h"
25 #include "detailed_ccm_params.h"
26 
27 namespace OHOS {
28 namespace CryptoFramework {
29 using namespace std;
30 
NapiGetNull(napi_env env)31 napi_value NapiGetNull(napi_env env)
32 {
33     napi_value result = nullptr;
34     napi_get_null(env, &result);
35     return result;
36 }
37 
ConvertArrayToNapiValue(napi_env env,HcfArray * array)38 napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array)
39 {
40     if (array == nullptr) {
41         LOGE("array is null!");
42         return nullptr;
43     }
44     if (array->count == 0) {
45         LOGE("array count is 0!");
46         return nullptr;
47     }
48     napi_value returnArray = nullptr;
49     napi_create_array(env, &returnArray);
50     if (returnArray == nullptr) {
51         LOGE("create return array failed!");
52         return nullptr;
53     }
54     for (uint32_t i = 0; i < array->count; i++) {
55         HcfBlob *blob = reinterpret_cast<HcfBlob *>(array->data + i);
56         napi_value outBuffer = GenerateArrayBuffer(env, blob->data, blob->len);
57         if (outBuffer == nullptr) {
58             LOGE("generate array buffer failed!");
59             return nullptr;
60         }
61         napi_value element = nullptr;
62         napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &element);
63         napi_set_element(env, returnArray, i, element);
64     }
65     napi_value returnValue = nullptr;
66     napi_create_object(env, &returnValue);
67     napi_set_named_property(env, returnValue, CRYPTO_TAG_DATA.c_str(), returnArray);
68     return returnValue;
69 }
70 
GenerateArrayBuffer(napi_env env,uint8_t * data,uint32_t size)71 napi_value GenerateArrayBuffer(napi_env env, uint8_t *data, uint32_t size)
72 {
73     uint8_t *buffer = static_cast<uint8_t *>(HcfMalloc(size, 0));
74     if (buffer == nullptr) {
75         LOGE("malloc uint8 array buffer failed!");
76         return nullptr;
77     }
78 
79     if (memcpy_s(buffer, size, data, size) != EOK) {
80         LOGE("memcpy_s data to buffer failed!");
81         HcfFree(buffer);
82         return nullptr;
83     }
84 
85     napi_value outBuffer = nullptr;
86     napi_status status = napi_create_external_arraybuffer(
87         env, buffer, size, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
88     if (status != napi_ok) {
89         LOGE("create uint8 array buffer failed!");
90         HcfFree(buffer);
91         return nullptr;
92     }
93     buffer = nullptr;
94     return outBuffer;
95 }
96 
GetDataOfEncodingBlob(napi_env env,napi_value data,HcfEncodingBlob * encodingBlob)97 static bool GetDataOfEncodingBlob(napi_env env, napi_value data, HcfEncodingBlob *encodingBlob)
98 {
99     napi_typedarray_type arrayType;
100     napi_value arrayBuffer = nullptr;
101     size_t length = 0;
102     size_t offset = 0;
103     void *rawData = nullptr;
104 
105     napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
106         reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
107     if (status != napi_ok) {
108         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed", true));
109         LOGE("failed to get array data!");
110         return false;
111     }
112     if (arrayType != napi_uint8_array) {
113         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array", true));
114         LOGE("array is not uint8 array!");
115         return false;
116     }
117 
118     if (length == 0) {
119         LOGE("input data length is 0");
120         return false;
121     }
122     encodingBlob->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
123     if (encodingBlob->data == nullptr) {
124         LOGE("malloc encoding blob data failed!");
125         return false;
126     }
127     if (memcpy_s(encodingBlob->data, length, rawData, length) != EOK) {
128         LOGE("memcpy_s encoding blob data failed!");
129         HcfFree(encodingBlob->data);
130         encodingBlob->data = nullptr;
131         return false;
132     }
133     encodingBlob->len = length;
134     return true;
135 }
136 
GetEncodingBlobFromValue(napi_env env,napi_value obj,HcfEncodingBlob ** encodingBlob)137 bool GetEncodingBlobFromValue(napi_env env, napi_value obj, HcfEncodingBlob **encodingBlob)
138 {
139     *encodingBlob = static_cast<HcfEncodingBlob *>(HcfMalloc(sizeof(HcfEncodingBlob), 0));
140     if (*encodingBlob == nullptr) {
141         LOGE("malloc encoding blob failed!");
142         return false;
143     }
144     napi_value data = nullptr;
145     napi_status status = napi_get_named_property(env, obj, CRYPTO_TAG_DATA.c_str(), &data);
146     if (status != napi_ok) {
147         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob data failed", true));
148         LOGE("failed to get encoding blob data!");
149         HcfFree(*encodingBlob);
150         *encodingBlob = nullptr;
151         return false;
152     }
153     if (!GetDataOfEncodingBlob(env, data, *encodingBlob)) {
154         HcfFree(*encodingBlob);
155         *encodingBlob = nullptr;
156         return false;
157     }
158     napi_value format = nullptr;
159     status = napi_get_named_property(env, obj, CRYPTO_TAG_ENCODING_FORMAT.c_str(), &format);
160     if (status != napi_ok) {
161         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob format failed", true));
162         LOGE("failed to get encoding blob format!");
163         HcfFree((*encodingBlob)->data);
164         (*encodingBlob)->data = nullptr;
165         HcfFree(*encodingBlob);
166         *encodingBlob = nullptr;
167         return false;
168     }
169     napi_get_value_uint32(env, format, reinterpret_cast<uint32_t *>(&(*encodingBlob)->encodingFormat));
170     return true;
171 }
172 
ConvertEncodingBlobToNapiValue(napi_env env,HcfEncodingBlob * encodingBlob)173 napi_value ConvertEncodingBlobToNapiValue(napi_env env, HcfEncodingBlob *encodingBlob)
174 {
175     napi_value outBuffer = GenerateArrayBuffer(env, encodingBlob->data, encodingBlob->len);
176     if (outBuffer == nullptr) {
177         LOGE("generate array buffer failed!");
178         return nullptr;
179     }
180     napi_value outData = nullptr;
181     napi_create_typedarray(env, napi_uint8_array, encodingBlob->len, outBuffer, 0, &outData);
182     napi_value encoding = nullptr;
183     napi_create_uint32(env, encodingBlob->encodingFormat, &encoding);
184     napi_value returnEncodingBlob = nullptr;
185     napi_create_object(env, &returnEncodingBlob);
186     napi_set_named_property(env, returnEncodingBlob, CRYPTO_TAG_DATA.c_str(), outData);
187     napi_set_named_property(env, returnEncodingBlob, CRYPTO_TAG_ENCODING_FORMAT.c_str(), encoding);
188     return returnEncodingBlob;
189 }
190 
GetBlobFromNapiValue(napi_env env,napi_value arg)191 HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg)
192 {
193     if ((env == nullptr) || (arg == nullptr)) {
194         LOGE("Invalid parmas!");
195         return nullptr;
196     }
197     napi_value data = nullptr;
198     napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_DATA.c_str(), &data);
199     if ((status != napi_ok) || (data == nullptr)) {
200         LOGE("failed to get valid data property!");
201         return nullptr;
202     }
203 
204     size_t length = 0;
205     size_t offset = 0;
206     void *rawData = nullptr;
207     napi_value arrayBuffer = nullptr;
208     napi_typedarray_type arrayType;
209     // Warning: Do not release the rawData returned by this interface because the rawData is managed by VM.
210     status = napi_get_typedarray_info(env, data, &arrayType, &length,
211         reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
212     if ((status != napi_ok) || (length == 0) || (rawData == nullptr)) {
213         LOGE("failed to get valid rawData.");
214         return nullptr;
215     }
216     if (arrayType != napi_uint8_array) {
217         LOGE("input data is not uint8 array.");
218         return nullptr;
219     }
220 
221     HcfBlob *newBlob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
222     if (newBlob == nullptr) {
223         LOGE("Failed to allocate newBlob memory!");
224         return nullptr;
225     }
226     newBlob->len = length;
227     newBlob->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
228     if (newBlob->data == nullptr) {
229         LOGE("malloc blob data failed!");
230         HcfFree(newBlob);
231         return nullptr;
232     }
233     if (memcpy_s(newBlob->data, length, rawData, length) != EOK) {
234         LOGE("memcpy_s blob data failed!");
235         HcfFree(newBlob->data);
236         HcfFree(newBlob);
237         return nullptr;
238     }
239 
240     return newBlob;
241 }
242 
GetIvParamsSpecType()243 static const char *GetIvParamsSpecType()
244 {
245     return IV_PARAMS_SPEC.c_str();
246 }
247 
GetGcmParamsSpecType()248 static const char *GetGcmParamsSpecType()
249 {
250     return GCM_PARAMS_SPEC.c_str();
251 }
252 
GetCcmParamsSpecType()253 static const char *GetCcmParamsSpecType()
254 {
255     return CCM_PARAMS_SPEC.c_str();
256 }
257 
GetBlobFromParamsSpec(napi_env env,napi_value arg,const string & type)258 static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, const string &type)
259 {
260     napi_value data = nullptr;
261     HcfBlob *blob = nullptr;
262 
263     napi_status status = napi_get_named_property(env, arg, type.c_str(), &data);
264     if ((status != napi_ok) || (data == nullptr)) {
265         LOGE("failed to get valid param property!");
266         return nullptr;
267     }
268     blob = GetBlobFromNapiValue(env, data);
269     if (blob == nullptr) {
270         LOGE("GetBlobFromNapiValue failed!");
271         return nullptr;
272     }
273     return blob;
274 }
275 
GetIvParamsSpec(napi_env env,napi_value arg,HcfParamsSpec ** paramsSpec)276 static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **paramsSpec)
277 {
278     HcfIvParamsSpec *ivParamsSpec = reinterpret_cast<HcfIvParamsSpec *>(HcfMalloc(sizeof(HcfIvParamsSpec), 0));
279     if (ivParamsSpec == nullptr) {
280         LOGE("ivParamsSpec malloc failed!");
281         return false;
282     }
283 
284     HcfBlob *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
285     if (iv == nullptr) {
286         LOGE("GetBlobFromNapiValue failed!");
287         HcfFree(ivParamsSpec);
288         return false;
289     }
290     ivParamsSpec->base.getType = GetIvParamsSpecType;
291     ivParamsSpec->iv = *iv;
292     *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ivParamsSpec);
293     HcfFree(iv);
294     return true;
295 }
296 
GetIvAndAadBlob(napi_env env,napi_value arg,HcfBlob ** iv,HcfBlob ** aad)297 static bool GetIvAndAadBlob(napi_env env, napi_value arg, HcfBlob **iv, HcfBlob **aad)
298 {
299     *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
300     if (*iv == nullptr) {
301         LOGE("get iv failed!");
302         return false;
303     }
304 
305     *aad = GetBlobFromParamsSpec(env, arg, AAD_PARAMS);
306     if (*aad == nullptr) {
307         LOGE("get aad failed!");
308         HcfFree((*iv)->data);
309         HcfFree(*iv);
310         return false;
311     }
312     return true;
313 }
314 
GetGcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)315 static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
316 {
317     HcfBlob *iv = nullptr;
318     HcfBlob *aad = nullptr;
319     HcfBlob *tag = nullptr;
320     HcfBlob authTag = {};
321     bool ret = false;
322 
323     HcfGcmParamsSpec *gcmParamsSpec = reinterpret_cast<HcfGcmParamsSpec *>(HcfMalloc(sizeof(HcfGcmParamsSpec), 0));
324     if (gcmParamsSpec == nullptr) {
325         LOGE("gcmParamsSpec malloc failed!");
326         return false;
327     }
328 
329     ret = GetIvAndAadBlob(env, arg, &iv, &aad);
330     if (!ret) {
331         LOGE("GetIvAndAadBlob failed!");
332         goto clearup;
333     }
334 
335     if (opMode == DECRYPT_MODE) {
336         tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
337         if (tag == nullptr) {
338             LOGE("get tag failed!");
339             goto clearup;
340         }
341     } else if (opMode == ENCRYPT_MODE) {
342         authTag.data = static_cast<uint8_t *>(HcfMalloc(GCM_AUTH_TAG_LEN, 0));
343         if (authTag.data == nullptr) {
344             LOGE("get tag failed!");
345             goto clearup;
346         }
347         authTag.len = GCM_AUTH_TAG_LEN;
348     } else {
349         goto clearup;
350     }
351 
352     gcmParamsSpec->base.getType = GetGcmParamsSpecType;
353     gcmParamsSpec->iv = *iv;
354     gcmParamsSpec->aad = *aad;
355     gcmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
356     *paramsSpec = reinterpret_cast<HcfParamsSpec *>(gcmParamsSpec);
357     ret = true;
358 clearup:
359    if (!ret) {
360         HcfBlobDataFree(iv);
361         HcfBlobDataFree(aad);
362         HcfBlobDataFree(tag);
363         HcfFree(gcmParamsSpec);
364     }
365     HcfFree(iv);
366     HcfFree(aad);
367     HcfFree(tag);
368     return ret;
369 }
370 
GetCcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)371 static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
372 {
373     HcfBlob *iv = nullptr;
374     HcfBlob *aad = nullptr;
375     HcfBlob *tag = nullptr;
376     HcfBlob authTag = {};
377     bool ret = false;
378 
379     HcfCcmParamsSpec *ccmParamsSpec = reinterpret_cast<HcfCcmParamsSpec *>(HcfMalloc(sizeof(HcfCcmParamsSpec), 0));
380     if (ccmParamsSpec == nullptr) {
381         LOGE("ccmParamsSpec malloc failed!");
382         return ret;
383     }
384     ret = GetIvAndAadBlob(env, arg, &iv, &aad);
385     if (!ret) {
386         LOGE("GetIvAndAadBlob failed!");
387         goto clearup;
388     }
389 
390     if (opMode == DECRYPT_MODE) {
391         tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
392         if (tag == nullptr) {
393             LOGE("get tag failed!");
394             goto clearup;
395         }
396     } else if (opMode == ENCRYPT_MODE) {
397         authTag.data = static_cast<uint8_t *>(HcfMalloc(CCM_AUTH_TAG_LEN, 0));
398         if (authTag.data == nullptr) {
399             LOGE("get tag failed!");
400             goto clearup;
401         }
402         authTag.len = CCM_AUTH_TAG_LEN;
403     } else {
404         goto clearup;
405     }
406     ccmParamsSpec->base.getType = GetCcmParamsSpecType;
407     ccmParamsSpec->iv = *iv;
408     ccmParamsSpec->aad = *aad;
409     ccmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
410     *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ccmParamsSpec);
411     ret = true;
412 clearup:
413     if (!ret) {
414         HcfBlobDataFree(iv);
415         HcfBlobDataFree(aad);
416         HcfBlobDataFree(tag);
417         HcfFree(ccmParamsSpec);
418     }
419     HcfFree(iv);
420     HcfFree(aad);
421     HcfFree(tag);
422     return ret;
423 }
424 
GetParamsSpecFromNapiValue(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)425 bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
426 {
427     napi_value data = nullptr;
428     napi_valuetype valueType = napi_undefined;
429     if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) {
430         LOGE("Invalid parmas!");
431         return false;
432     }
433 
434     napi_status status = napi_get_named_property(env, arg, ALGO_PARAMS.c_str(), &data);
435     napi_typeof(env, data, &valueType);
436     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
437         status = napi_get_named_property(env, arg, ALGO_PARAMS_OLD.c_str(), &data);
438         napi_typeof(env, data, &valueType);
439         if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
440             LOGE("failed to get valid algo name!");
441             return false;
442         }
443     }
444     string algoName;
445     if (!GetStringFromJSParams(env, data, algoName, false)) {
446         LOGE("GetStringFromJSParams failed!");
447         return false;
448     }
449     if (algoName.compare(IV_PARAMS_SPEC) == 0) {
450         return GetIvParamsSpec(env, arg, paramsSpec);
451     } else if (algoName.compare(GCM_PARAMS_SPEC) == 0) {
452         return GetGcmParamsSpec(env, arg, opMode, paramsSpec);
453     } else if (algoName.compare(CCM_PARAMS_SPEC) == 0) {
454         return GetCcmParamsSpec(env, arg, opMode, paramsSpec);
455     } else {
456         return false;
457     }
458 }
459 
ConvertBlobToNapiValue(napi_env env,HcfBlob * blob)460 napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob)
461 {
462     if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
463         LOGE("Invalid blob!");
464         return nullptr;
465     }
466     uint8_t *buffer = static_cast<uint8_t *>(HcfMalloc(blob->len, 0));
467     if (buffer == nullptr) {
468         LOGE("malloc uint8 array buffer failed!");
469         return nullptr;
470     }
471 
472     if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
473         LOGE("memcpy_s data to buffer failed!");
474         HcfFree(buffer);
475         return nullptr;
476     }
477 
478     napi_value outBuffer = nullptr;
479     napi_status status = napi_create_external_arraybuffer(
480         env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
481     if (status != napi_ok) {
482         LOGE("create uint8 array buffer failed!");
483         HcfFree(buffer);
484         return nullptr;
485     }
486     buffer = nullptr;
487 
488     napi_value outData = nullptr;
489     napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
490     napi_value dataBlob = nullptr;
491     napi_create_object(env, &dataBlob);
492     napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
493 
494     return dataBlob;
495 }
496 
GetDataOfCertChain(napi_env env,napi_value data,HcfCertChainData * certChain)497 static bool GetDataOfCertChain(napi_env env, napi_value data, HcfCertChainData *certChain)
498 {
499     napi_typedarray_type arrayType;
500     napi_value arrayBuffer = nullptr;
501     size_t length = 0;
502     size_t offset = 0;
503     void *rawData = nullptr;
504 
505     napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
506         reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
507     if (status != napi_ok) {
508         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed", true));
509         LOGE("failed to get array data!");
510         return false;
511     }
512     if (arrayType != napi_uint8_array) {
513         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array", true));
514         LOGE("array is not uint8 array!");
515         return false;
516     }
517 
518     if (length == 0) {
519         LOGE("input data length is 0");
520         return false;
521     }
522     certChain->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
523     if (certChain->data == nullptr) {
524         LOGE("malloc cert chain data failed!");
525         return false;
526     }
527     if (memcpy_s(certChain->data, length, rawData, length) != EOK) {
528         LOGE("memcpy_s cert chain data failed!");
529         HcfFree(certChain->data);
530         certChain->data = nullptr;
531         return false;
532     }
533     certChain->dataLen = length;
534     return true;
535 }
536 
GetCertChainFromValue(napi_env env,napi_value obj,HcfCertChainData ** certChainData)537 bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **certChainData)
538 {
539     *certChainData = static_cast<HcfCertChainData *>(HcfMalloc(sizeof(HcfCertChainData), 0));
540     if (*certChainData == nullptr) {
541         LOGE("malloc certChainData failed!");
542         return false;
543     }
544     napi_value data = nullptr;
545     napi_status status = napi_get_named_property(env, obj, CRYPTO_TAG_DATA.c_str(), &data);
546     if (status != napi_ok) {
547         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain data failed", true));
548         LOGE("failed to get cert chain data!");
549         HcfFree(*certChainData);
550         *certChainData = nullptr;
551         return false;
552     }
553     if (!GetDataOfCertChain(env, data, *certChainData)) {
554         HcfFree(*certChainData);
555         *certChainData = nullptr;
556         return false;
557     }
558 
559     napi_value certCount = nullptr;
560     status = napi_get_named_property(env, obj, CRYPTO_TAG_COUNT.c_str(), &certCount);
561     if (status != napi_ok) {
562         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain count failed", true));
563         LOGE("failed to get cert count!");
564         HcfFree((*certChainData)->data);
565         (*certChainData)->data = nullptr;
566         HcfFree(*certChainData);
567         *certChainData = nullptr;
568         return false;
569     }
570     napi_get_value_uint32(env, certCount, reinterpret_cast<uint32_t *>(&(*certChainData)->count));
571 
572     napi_value format = nullptr;
573     status = napi_get_named_property(env, obj, CRYPTO_TAG_ENCODING_FORMAT.c_str(), &format);
574     if (status != napi_ok) {
575         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain format failed", true));
576         LOGE("failed to get cert chain format!");
577         HcfFree((*certChainData)->data);
578         (*certChainData)->data = nullptr;
579         HcfFree(*certChainData);
580         *certChainData = nullptr;
581         return false;
582     }
583     napi_get_value_uint32(env, format, reinterpret_cast<uint32_t *>(&(*certChainData)->format));
584     return true;
585 }
586 
GetStringFromJSParams(napi_env env,napi_value arg,string & returnStr,bool isCertFunc)587 bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr, bool isCertFunc)
588 {
589     napi_valuetype valueType;
590     napi_typeof(env, arg, &valueType);
591     if (valueType != napi_string) {
592         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not string", isCertFunc));
593         LOGE("wrong argument type. expect string type. [Type]: %d", valueType);
594         return false;
595     }
596 
597     size_t length = 0;
598     if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
599         LOGE("can not get string length");
600         return false;
601     }
602     returnStr.reserve(length + 1);
603     returnStr.resize(length);
604     if (napi_get_value_string_utf8(env, arg, returnStr.data(), (length + 1), &length) != napi_ok) {
605         LOGE("can not get string value");
606         return false;
607     }
608     return true;
609 }
610 
GetInt32FromJSParams(napi_env env,napi_value arg,int32_t & returnInt,bool isCertFunc)611 bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt, bool isCertFunc)
612 {
613     napi_valuetype valueType;
614     napi_typeof(env, arg, &valueType);
615     if (valueType != napi_number) {
616         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number", isCertFunc));
617         LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
618         return false;
619     }
620 
621     if (napi_get_value_int32(env, arg, &returnInt) != napi_ok) {
622         LOGE("can not get int value");
623         return false;
624     }
625     return true;
626 }
627 
GetUint32FromJSParams(napi_env env,napi_value arg,uint32_t & returnInt,bool isCertFunc)628 bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt, bool isCertFunc)
629 {
630     napi_valuetype valueType;
631     napi_typeof(env, arg, &valueType);
632     if (valueType != napi_number) {
633         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number", isCertFunc));
634         LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
635         return false;
636     }
637 
638     if (napi_get_value_uint32(env, arg, &returnInt) != napi_ok) {
639         LOGE("can not get int value");
640         return false;
641     }
642     return true;
643 }
644 
GetCallbackFromJSParams(napi_env env,napi_value arg,napi_ref * returnCb,bool isCertFunc)645 bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb, bool isCertFunc)
646 {
647     napi_valuetype valueType = napi_undefined;
648     napi_typeof(env, arg, &valueType);
649     if (valueType != napi_function) {
650         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not function", isCertFunc));
651         LOGE("wrong argument type. expect callback type. [Type]: %d", valueType);
652         return false;
653     }
654 
655     napi_create_reference(env, arg, 1, returnCb);
656     return true;
657 }
658 
GetJsErrValueByErrCode(int32_t errCode)659 static uint32_t GetJsErrValueByErrCode(int32_t errCode)
660 {
661     switch (errCode) {
662         case HCF_INVALID_PARAMS:
663             return JS_ERR_INVALID_PARAMS;
664         case HCF_NOT_SUPPORT:
665             return JS_ERR_NOT_SUPPORT;
666         case HCF_ERR_MALLOC:
667             return JS_ERR_OUT_OF_MEMORY;
668         case HCF_ERR_COPY:
669             return JS_ERR_RUNTIME_ERROR;
670         case HCF_ERR_CRYPTO_OPERATION:
671             return JS_ERR_CRYPTO_OPERATION;
672         default:
673             return JS_ERR_DEFAULT_ERR;
674     }
675 }
676 
GetCertErrValueByErrCode(int32_t errCode)677 static uint32_t GetCertErrValueByErrCode(int32_t errCode)
678 {
679     switch (errCode) {
680         case HCF_INVALID_PARAMS:
681             return JS_ERR_CERT_INVALID_PARAMS;
682         case HCF_NOT_SUPPORT:
683             return JS_ERR_CERT_NOT_SUPPORT;
684         case HCF_ERR_MALLOC:
685             return JS_ERR_CERT_OUT_OF_MEMORY;
686         case HCF_ERR_COPY:
687             return JS_ERR_CERT_RUNTIME_ERROR;
688         case HCF_ERR_CRYPTO_OPERATION:
689             return JS_ERR_CERT_CRYPTO_OPERATION;
690         case HCF_ERR_CERT_SIGNATURE_FAILURE:
691             return JS_ERR_CERT_SIGNATURE_FAILURE;
692         case HCF_ERR_CERT_NOT_YET_VALID:
693             return JS_ERR_CERT_NOT_YET_VALID;
694         case HCF_ERR_CERT_HAS_EXPIRED:
695             return JS_ERR_CERT_HAS_EXPIRED;
696         case HCF_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
697             return JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
698         case HCF_ERR_KEYUSAGE_NO_CERTSIGN:
699             return JS_ERR_KEYUSAGE_NO_CERTSIGN;
700         case HCF_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE:
701             return JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
702         default:
703             return JS_ERR_DEFAULT_ERR;
704     }
705 }
706 
GenerateBusinessError(napi_env env,int32_t errCode,const char * errMsg,bool isCertFunc)707 napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg, bool isCertFunc)
708 {
709     napi_value businessError = nullptr;
710 
711     napi_value code = nullptr;
712     if (isCertFunc) {
713         napi_create_uint32(env, GetCertErrValueByErrCode(errCode), &code);
714     } else {
715         napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code);
716     }
717 
718     napi_value msg = nullptr;
719     napi_create_string_utf8(env, errMsg, NAPI_AUTO_LENGTH, &msg);
720 
721     napi_create_error(env, nullptr, msg, &businessError);
722     napi_set_named_property(env, businessError, CRYPTO_TAG_ERR_CODE.c_str(), code);
723 
724     return businessError;
725 }
726 
CheckArgsCount(napi_env env,size_t argc,size_t expectedCount,bool isSync,bool isCertFunc)727 bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync, bool isCertFunc)
728 {
729     if (isSync) {
730         if (argc != expectedCount) {
731             napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", isCertFunc));
732             LOGE("invalid params count!");
733             return false;
734         }
735     } else {
736         if ((argc != expectedCount) && (argc != (expectedCount - ARGS_SIZE_ONE))) {
737             napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", isCertFunc));
738             LOGE("invalid params count!");
739             return false;
740         }
741     }
742     return true;
743 }
744 
GetResourceName(napi_env env,const char * name)745 napi_value GetResourceName(napi_env env, const char *name)
746 {
747     napi_value resourceName = nullptr;
748     napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &resourceName);
749     return resourceName;
750 }
751 }  // namespace CryptoFramework
752 }  // namespace OHOS
753