• 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 "cm_napi_common.h"
17 
18 #include "securec.h"
19 
20 #include "cm_log.h"
21 #include "cm_type.h"
22 
23 namespace CMNapi {
24 namespace {
25 constexpr int CM_MAX_DATA_LEN = 0x6400000; // The maximum length is 100M
26 constexpr int RESULT_ARG_NUMBER = 2;
27 }  // namespace
28 
GetCmContextAttribute(napi_env env,napi_value object,const char * type,int maxSize,char * & srcData)29 static napi_value GetCmContextAttribute(napi_env env, napi_value object, const char *type, int maxSize,
30     char *&srcData)
31 {
32     napi_value typeValue = nullptr;
33     napi_status  status = napi_get_named_property(env, object, type, &typeValue);
34     if (status != napi_ok || typeValue == nullptr) {
35         GET_AND_THROW_LAST_ERROR((env));
36         CM_LOG_E("could not get property %s", type);
37         return nullptr;
38     }
39 
40     size_t length = 0;
41     status = napi_get_value_string_utf8(env, typeValue, nullptr, 0, &length);
42     if (status != napi_ok) {
43         GET_AND_THROW_LAST_ERROR((env));
44         CM_LOG_E("could not get string length");
45         return nullptr;
46     }
47 
48     if (static_cast<int>(length) > maxSize) {
49         CM_LOG_E("input param length too large");
50         return nullptr;
51     }
52 
53     srcData = static_cast<char *>(CmMalloc(length + 1));
54     if (srcData == nullptr) {
55         napi_throw_error(env, nullptr, "could not alloc memory");
56         CM_LOG_E("could not alloc memory");
57         return nullptr;
58     }
59     (void)memset_s(srcData, length + 1, 0, length + 1);
60 
61     size_t result = 0;
62     status = napi_get_value_string_utf8(env, typeValue, srcData, length + 1, &result);
63     if (status != napi_ok) {
64         CmFree(srcData);
65         srcData = nullptr;
66         GET_AND_THROW_LAST_ERROR((env));
67         CM_LOG_E("could not get string");
68         return nullptr;
69     }
70 
71     return GetInt32(env, 0);
72 }
73 
ParseCmContext(napi_env env,napi_value object,CmContext * & cmContext)74 napi_value ParseCmContext(napi_env env, napi_value object, CmContext *&cmContext)
75 {
76     char *userIdData = nullptr;
77     char *uidData = nullptr;
78     char *packageNameData = nullptr;
79 
80     int32_t ret = CM_SUCCESS;
81     do {
82         napi_value userIdStatus = GetCmContextAttribute(env, object,
83             CM_CONTEXT_PROPERTY_USERID.c_str(), CM_MAX_DATA_LEN, userIdData);
84         napi_value uidStatus = GetCmContextAttribute(env, object,
85             CM_CONTEXT_PROPERTY_UID.c_str(), CM_MAX_DATA_LEN, uidData);
86         napi_value packageNameStatus = GetCmContextAttribute(env, object,
87             CM_CONTEXT_PROPERTY_PACKAGENAME.c_str(), CM_MAX_DATA_LEN, packageNameData);
88         if (userIdStatus == nullptr || uidStatus == nullptr || packageNameStatus == nullptr) {
89             CM_LOG_E("get userid or uid or packName failed");
90             ret = CMR_ERROR;
91             break;
92         }
93 
94         cmContext = static_cast<CmContext *>(CmMalloc(sizeof(CmContext)));
95         if (cmContext == nullptr) {
96             CM_LOG_E("could not alloc memory");
97             ret = CMR_ERROR_MALLOC_FAIL;
98             break;
99         }
100 
101         cmContext->userId = static_cast<uint32_t>(atoi(userIdData));
102         cmContext->uid = static_cast<uint32_t>(atoi(uidData));
103         if (strcpy_s(cmContext->packageName, sizeof(cmContext->packageName), packageNameData) != EOK) {
104             CM_LOG_E("copy package name failed");
105             ret = CMR_ERROR_INVALID_OPERATION;
106             break;
107         }
108     } while (0);
109     CM_FREE_PTR(userIdData);
110     CM_FREE_PTR(uidData);
111     CM_FREE_PTR(packageNameData);
112     if (ret != CM_SUCCESS) {
113         return nullptr;
114     }
115     return GetInt32(env, 0);
116 }
117 
ParseUint32(napi_env env,napi_value object,uint32_t & store)118 napi_value ParseUint32(napi_env env, napi_value object, uint32_t &store)
119 {
120     napi_valuetype valueType;
121     napi_typeof(env, object, &valueType);
122     if (valueType != napi_number) {
123         CM_LOG_E("param type is not number");
124         return nullptr;
125     }
126     uint32_t temp = 0;
127     napi_get_value_uint32(env, object, &temp);
128     store = temp;
129     return GetInt32(env, 0);
130 }
131 
ParseBoolean(napi_env env,napi_value object,bool & status)132 napi_value ParseBoolean(napi_env env, napi_value object, bool &status)
133 {
134     napi_valuetype valueType;
135     napi_typeof(env, object, &valueType);
136     if (valueType != napi_boolean) {
137         CM_LOG_E("param type is not bool");
138         return nullptr;
139     }
140     bool temp = false;
141     napi_get_value_bool(env, object, &temp);
142     status = temp;
143     return GetInt32(env, 0);
144 }
145 
ParseString(napi_env env,napi_value object,CmBlob * & certUri)146 napi_value ParseString(napi_env env, napi_value object, CmBlob *&certUri)
147 {
148     napi_valuetype valueType = napi_undefined;
149     NAPI_CALL(env, napi_typeof(env, object, &valueType));
150     if (valueType != napi_string) {
151         CM_LOG_E("the type of param is not string");
152         return nullptr;
153     }
154     size_t length = 0;
155     napi_status status = napi_get_value_string_utf8(env, object, nullptr, 0, &length);
156     if (status != napi_ok) {
157         GET_AND_THROW_LAST_ERROR((env));
158         CM_LOG_E("could not get string length");
159         return nullptr;
160     }
161 
162     if (length > CM_MAX_DATA_LEN) {
163         CM_LOG_E("input key alias length too large");
164         return nullptr;
165     }
166 
167     char *data = static_cast<char *>(CmMalloc(length + 1));
168     if (data == nullptr) {
169         napi_throw_error(env, nullptr, "could not alloc memory");
170         CM_LOG_E("could not alloc memory");
171         return nullptr;
172     }
173     (void)memset_s(data, length + 1, 0, length + 1);
174 
175     size_t result = 0;
176     status = napi_get_value_string_utf8(env, object, data, length + 1, &result);
177     if (status != napi_ok) {
178         CmFree(data);
179         GET_AND_THROW_LAST_ERROR((env));
180         CM_LOG_E("could not get string");
181         return nullptr;
182     }
183 
184     certUri = static_cast<CmBlob *>(CmMalloc(sizeof(CmBlob)));
185     if (certUri == nullptr) {
186         CmFree(data);
187         napi_throw_error(env, nullptr, "could not alloc memory");
188         CM_LOG_E("could not alloc memory");
189         return nullptr;
190     }
191     certUri->data = reinterpret_cast<uint8_t *>(data);
192     certUri->size = static_cast<uint32_t>((length + 1) & UINT32_MAX);
193 
194     return GetInt32(env, 0);
195 }
196 
GetUint8Array(napi_env env,napi_value object,CmBlob & arrayBlob)197 napi_value GetUint8Array(napi_env env, napi_value object, CmBlob &arrayBlob)
198 {
199     napi_typedarray_type arrayType;
200     napi_value arrayBuffer = nullptr;
201     size_t length = 0;
202     size_t offset = 0;
203     void *rawData = nullptr;
204     NAPI_CALL(
205         env, napi_get_typedarray_info(env, object, &arrayType, &length,
206         static_cast<void **>(&rawData), &arrayBuffer, &offset));
207     NAPI_ASSERT(env, arrayType == napi_uint8_array, "Param is not uint8 array");
208 
209     if (length > CM_MAX_DATA_LEN) {
210         CM_LOG_E("Data is too large, length = %x", length);
211         return nullptr;
212     }
213     if (length == 0) {
214         CM_LOG_I("The memory length created is only 1 Byte");
215         // The memory length created is only 1 Byte
216         arrayBlob.data = static_cast<uint8_t *>(CmMalloc(1));
217     } else {
218         arrayBlob.data = static_cast<uint8_t *>(CmMalloc(length));
219     }
220     if (arrayBlob.data == nullptr) {
221         CM_LOG_E("Malloc failed");
222         return nullptr;
223     }
224     (void)memset_s(arrayBlob.data, length, 0, length);
225     if (memcpy_s(arrayBlob.data, length, rawData, length) != EOK) {
226         return nullptr;
227     }
228     arrayBlob.size = static_cast<uint32_t>(length);
229 
230     return GetInt32(env, 0);
231 }
232 
GetCallback(napi_env env,napi_value object)233 napi_ref GetCallback(napi_env env, napi_value object)
234 {
235     napi_valuetype valueType = napi_undefined;
236     napi_status status = napi_typeof(env, object, &valueType);
237     if (status != napi_ok) {
238         CM_LOG_E("could not get object type");
239         return nullptr;
240     }
241 
242     if (valueType != napi_function) {
243         CM_LOG_E("invalid type");
244         return nullptr;
245     }
246 
247     napi_ref ref = nullptr;
248     status = napi_create_reference(env, object, 1, &ref);
249     if (status != napi_ok) {
250         CM_LOG_E("could not create reference");
251         return nullptr;
252     }
253     return ref;
254 }
255 
GenerateAarrayBuffer(napi_env env,uint8_t * data,uint32_t size)256 static napi_value GenerateAarrayBuffer(napi_env env, uint8_t *data, uint32_t size)
257 {
258     uint8_t *buffer = static_cast<uint8_t *>(CmMalloc(size));
259     if (buffer == nullptr) {
260         return nullptr;
261     }
262     (void)memcpy_s(buffer, size, data, size);
263 
264     napi_value outBuffer = nullptr;
265     napi_status status = napi_create_external_arraybuffer(
266         env, buffer, size, [](napi_env env, void *data, void *hint) { CmFree(data); }, nullptr, &outBuffer);
267     if (status == napi_ok) {
268         // free by finalize callback
269         buffer = nullptr;
270     } else {
271         CmFree(buffer);
272         GET_AND_THROW_LAST_ERROR((env));
273     }
274 
275     return outBuffer;
276 }
277 
GenerateCertAbstractArray(napi_env env,const struct CertAbstract * certAbstract,const uint32_t certCount)278 napi_value GenerateCertAbstractArray(napi_env env, const struct CertAbstract *certAbstract, const uint32_t certCount)
279 {
280     if (certCount == 0 || certAbstract == nullptr) {
281         return nullptr;
282     }
283     napi_value array = nullptr;
284     NAPI_CALL(env, napi_create_array(env, &array));
285     for (uint32_t i = 0; i < certCount; i++) {
286         napi_value uri = nullptr;
287         napi_value certAlias = nullptr;
288         napi_value subjectName = nullptr;
289         napi_value status = nullptr;
290 
291         napi_create_string_latin1(env, static_cast<const char *>(certAbstract[i].uri), NAPI_AUTO_LENGTH, &uri);
292         napi_create_string_latin1(env, static_cast<const char *>(certAbstract[i].certAlias),
293             NAPI_AUTO_LENGTH, &certAlias);
294         napi_create_string_latin1(env, static_cast<const char *>(certAbstract[i].subjectName),
295             NAPI_AUTO_LENGTH, &subjectName);
296         napi_get_boolean(env, certAbstract[i].status, &status);
297 
298         napi_value element = nullptr;
299         napi_create_object(env, &element);
300         napi_set_named_property (env, element, CM_CERT_PROPERTY_URI.c_str(), uri);
301         napi_set_named_property (env, element, CM_CERT_PROPERTY_CERTALIAS.c_str(), certAlias);
302         napi_set_named_property (env, element, CM_CERT_PROPERTY_STATUS.c_str(), status);
303         napi_set_named_property (env, element, CM_CERT_PROPERTY_SUBJECTNAME.c_str(), subjectName);
304 
305         napi_set_element(env, array, i, element);
306     }
307     return array;
308 }
309 
GenerateCredentialAbstractArray(napi_env env,const struct CredentialAbstract * credentialAbstract,const uint32_t credentialCount)310 napi_value GenerateCredentialAbstractArray(napi_env env,
311     const struct CredentialAbstract *credentialAbstract, const uint32_t credentialCount)
312 {
313     if (credentialCount == 0 || credentialAbstract == nullptr) {
314         return nullptr;
315     }
316     napi_value array = nullptr;
317     NAPI_CALL(env, napi_create_array(env, &array));
318     for (uint32_t i = 0; i < credentialCount; i++) {
319         napi_value type = nullptr;
320         napi_value alias = nullptr;
321         napi_value keyUri = nullptr;
322         napi_create_string_latin1(env, static_cast<const char *>(credentialAbstract[i].type),
323             NAPI_AUTO_LENGTH, &type);
324         napi_create_string_latin1(env, static_cast<const char *>(credentialAbstract[i].alias),
325             NAPI_AUTO_LENGTH, &alias);
326         napi_create_string_latin1(env, static_cast<const char *>(credentialAbstract[i].keyUri),
327             NAPI_AUTO_LENGTH, &keyUri);
328 
329         napi_value element = nullptr;
330         napi_create_object(env, &element);
331         napi_set_named_property (env, element, CM_CERT_PROPERTY_TYPE.c_str(), type);
332         napi_set_named_property (env, element, CM_CERT_PROPERTY_CREDENTIAL_ALIAS.c_str(), alias);
333         napi_set_named_property (env, element, CM_CERT_PROPERTY_KEY_URI.c_str(), keyUri);
334 
335         napi_set_element(env, array, i, element);
336     }
337     return array;
338 }
339 
GenerateCertInfo(napi_env env,const struct CertInfo * certInfo)340 napi_value GenerateCertInfo(napi_env env, const struct CertInfo *certInfo)
341 {
342     if (certInfo == nullptr) {
343         return nullptr;
344     }
345     napi_value result = nullptr;
346     NAPI_CALL(env, napi_create_object(env, &result));
347 
348     struct CertInfoValue cInfVal = { nullptr };
349     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->uri),
350         NAPI_AUTO_LENGTH, &cInfVal.uri));
351     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->certAlias),
352         NAPI_AUTO_LENGTH, &cInfVal.certAlias));
353     NAPI_CALL(env, napi_get_boolean(env, certInfo->status, &cInfVal.status));
354     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->issuerName),
355         NAPI_AUTO_LENGTH, &cInfVal.issuerName));
356     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->subjectName),
357         NAPI_AUTO_LENGTH, &cInfVal.subjectName));
358     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->serial),
359         NAPI_AUTO_LENGTH, &cInfVal.serial));
360     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->notBefore),
361         NAPI_AUTO_LENGTH, &cInfVal.notBefore));
362     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->notAfter),
363         NAPI_AUTO_LENGTH, &cInfVal.notAfter));
364     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(certInfo->fingerprintSha256),
365         NAPI_AUTO_LENGTH, &cInfVal.fingerprintSha256));
366 
367     napi_value certBuffer = GenerateAarrayBuffer(env, certInfo->certInfo.data, certInfo->certInfo.size);
368     if (certBuffer != nullptr) {
369         NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, certInfo->certInfo.size,
370             certBuffer, 0, &cInfVal.certInfoBlob));
371     }
372 
373     napi_value elem = nullptr;
374     NAPI_CALL(env, napi_create_object(env, &elem));
375     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_URI.c_str(), cInfVal.uri));
376     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_CERTALIAS.c_str(), cInfVal.certAlias));
377     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_STATUS.c_str(), cInfVal.status));
378     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_ISSUERNAME.c_str(), cInfVal.issuerName));
379     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_SUBJECTNAME.c_str(), cInfVal.subjectName));
380     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_SERIAL.c_str(), cInfVal.serial));
381     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_BEFORE.c_str(), cInfVal.notBefore));
382     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_AFTER.c_str(), cInfVal.notAfter));
383     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_FINGERSHA256.c_str(),
384         cInfVal.fingerprintSha256));
385     NAPI_CALL(env, napi_set_named_property(env, elem, CM_CERT_PROPERTY_CERT_DATA.c_str(), cInfVal.certInfoBlob));
386 
387     return elem;
388 }
389 
TranformErrorCode(int32_t errorCode)390 int32_t TranformErrorCode(int32_t errorCode)
391 {
392     if (errorCode == CMR_ERROR_INVALID_CERT_FORMAT || errorCode == CMR_ERROR_INSUFFICIENT_DATA) {
393         return INVALID_CERT_FORMAT;
394     }
395     if (errorCode == CMR_ERROR_NOT_FOUND || errorCode == CMR_ERROR_NOT_EXIST) {
396         return NOT_FOUND;
397     }
398     if (errorCode == CMR_ERROR_NOT_PERMITTED) {
399         return NO_PERMISSION;
400     }
401     if (errorCode == CMR_ERROR_NOT_SYSTEMP_APP) {
402         return NOT_SYSTEM_APP;
403     }
404     return INNER_FAILURE;
405 }
406 
GenerateBusinessError(napi_env env,int32_t errorCode,const char * errorMsg)407 napi_value GenerateBusinessError(napi_env env, int32_t errorCode, const char *errorMsg)
408 {
409     napi_value businessError = nullptr;
410     NAPI_CALL(env, napi_create_object(env, &businessError));
411 
412     napi_value code = nullptr;
413     int32_t outCode = TranformErrorCode(errorCode);
414     NAPI_CALL(env, napi_create_int32(env, outCode, &code));
415     NAPI_CALL(env, napi_set_named_property(env, businessError, BUSINESS_ERROR_PROPERTY_CODE.c_str(), code));
416     napi_value message = nullptr;
417     NAPI_CALL(env, napi_create_string_utf8(env, errorMsg, NAPI_AUTO_LENGTH, &message));
418     NAPI_CALL(env, napi_set_named_property(env, businessError, BUSINESS_ERROR_PROPERTY_MESSAGE.c_str(), message));
419     return businessError;
420 }
421 
ThrowParamsError(napi_env env,int32_t errorCode,std::string errMsg)422 void ThrowParamsError(napi_env env, int32_t errorCode, std::string errMsg)
423 {
424     napi_value paramsError = nullptr;
425     napi_value code = nullptr;
426     napi_value message = nullptr;
427     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, errorCode, &code));
428     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message));
429     NAPI_CALL_RETURN_VOID(env, napi_create_error(env, nullptr, message, &paramsError));
430     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, paramsError, BUSINESS_ERROR_PROPERTY_CODE.c_str(), code));
431     NAPI_CALL_RETURN_VOID(env, napi_throw(env, paramsError));
432 }
433 
GenerateAppCertInfo(napi_env env,const struct Credential * credential)434 napi_value GenerateAppCertInfo(napi_env env, const struct Credential *credential)
435 {
436     if (credential == nullptr) {
437         return nullptr;
438     }
439     napi_value result = nullptr;
440     NAPI_CALL(env, napi_create_object(env, &result));
441     napi_value type = nullptr;
442     napi_value alias = nullptr;
443     napi_value keyUri = nullptr;
444     napi_value certNum = nullptr;
445     napi_value keyNum = nullptr;
446     napi_value credData = nullptr;
447     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(credential->type),
448         NAPI_AUTO_LENGTH, &type));
449     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(credential->alias),
450         NAPI_AUTO_LENGTH, &alias));
451     NAPI_CALL(env, napi_create_string_latin1(env, static_cast<const char *>(credential->keyUri),
452         NAPI_AUTO_LENGTH, &keyUri));
453 
454     NAPI_CALL(env, napi_create_int32(env, credential->certNum, &certNum));
455     NAPI_CALL(env, napi_create_int32(env, credential->keyNum, &keyNum));
456 
457     napi_value crendentialBuffer = GenerateAarrayBuffer(env, credential->credData.data, credential->credData.size);
458     if (crendentialBuffer != nullptr) {
459         NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, credential->credData.size,
460             crendentialBuffer, 0, &credData));
461     }
462 
463     napi_value element = nullptr;
464     NAPI_CALL(env, napi_create_object(env, &element));
465     NAPI_CALL(env, napi_set_named_property(env, element, CM_CERT_PROPERTY_TYPE.c_str(), type));
466     NAPI_CALL(env, napi_set_named_property(env, element, CM_CERT_PROPERTY_CREDENTIAL_ALIAS.c_str(), alias));
467     NAPI_CALL(env, napi_set_named_property(env, element, CM_CERT_PROPERTY_KEY_URI.c_str(), keyUri));
468     NAPI_CALL(env, napi_set_named_property(env, element, CM_CERT_PROPERTY_CERT_NUM.c_str(), certNum));
469     NAPI_CALL(env, napi_set_named_property(env, element, CM_CERT_PROPERTY_KEY_NUM.c_str(), keyNum));
470 
471     NAPI_CALL(env, napi_set_named_property(env, element, CM_CERT_PROPERTY_CREDENTIAL_DATA.c_str(), credData));
472 
473     return element;
474 }
475 
GeneratePromise(napi_env env,napi_deferred deferred,int32_t resultCode,napi_value * result,int32_t arrLength)476 void GeneratePromise(napi_env env, napi_deferred deferred, int32_t resultCode,
477     napi_value *result, int32_t arrLength)
478 {
479     if (arrLength < RESULT_NUMBER) {
480         return;
481     }
482     if (resultCode == CM_SUCCESS) {
483         NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, deferred, result[1]));
484     } else {
485         NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, deferred, result[0]));
486     }
487 }
488 
GenerateCallback(napi_env env,napi_ref callback,napi_value * result,int32_t arrLength)489 void GenerateCallback(napi_env env, napi_ref callback, napi_value *result, int32_t arrLength)
490 {
491     napi_value func = nullptr;
492     napi_value returnVal = nullptr;
493     if (arrLength < RESULT_NUMBER) {
494         return;
495     }
496     NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, callback, &func));
497     NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, func, RESULT_ARG_NUMBER, result, &returnVal));
498 }
499 
GenerateNapiPromise(napi_env env,napi_ref callback,napi_deferred * deferred,napi_value * promise)500 void GenerateNapiPromise(napi_env env, napi_ref callback, napi_deferred *deferred, napi_value *promise)
501 {
502     if (callback == nullptr) {
503         NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, deferred, promise));
504     } else {
505         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, promise));
506     }
507 }
508 
DeleteNapiContext(napi_env env,napi_async_work & asyncWork,napi_ref & callback)509 void DeleteNapiContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback)
510 {
511     if (asyncWork != nullptr) {
512         napi_delete_async_work(env, asyncWork);
513         asyncWork = nullptr;
514     }
515 
516     if (callback != nullptr) {
517         napi_delete_reference(env, callback);
518         callback = nullptr;
519     }
520 }
521 
FreeCmContext(CmContext * & context)522 void FreeCmContext(CmContext *&context)
523 {
524     if (context == nullptr) {
525         return;
526     }
527 
528     context->userId = 0;
529     context->uid = 0;
530 
531     CmFree(context);
532     context = nullptr;
533 }
534 
FreeCertList(CertList * & certList)535 void FreeCertList(CertList *&certList)
536 {
537     if (certList == nullptr || certList->certAbstract == nullptr) {
538         return;
539     }
540 
541     FreeCertAbstract(certList->certAbstract);
542     certList->certAbstract = nullptr;
543 
544     CmFree(certList);
545     certList = nullptr;
546 }
547 
FreeCredentialList(CredentialList * & credentialList)548 void FreeCredentialList(CredentialList *&credentialList)
549 {
550     if (credentialList == nullptr || credentialList->credentialAbstract == nullptr) {
551         return;
552     }
553 
554     FreeCredentialAbstract(credentialList->credentialAbstract);
555     credentialList->credentialAbstract = nullptr;
556 
557     CmFree(credentialList);
558     credentialList = nullptr;
559 }
560 
FreeCertInfo(CertInfo * & certInfo)561 void FreeCertInfo(CertInfo *&certInfo)
562 {
563     if (certInfo == nullptr) {
564         return;
565     }
566 
567     certInfo->status = false;
568 
569     if (certInfo->certInfo.data != nullptr) {
570         CmFree(certInfo->certInfo.data);
571     }
572 
573     CmFree(certInfo);
574     certInfo = nullptr;
575 }
576 
FreeCredential(Credential * & credential)577 void FreeCredential(Credential *&credential)
578 {
579     if (credential == nullptr) {
580         return;
581     }
582 
583     if (credential->credData.data != nullptr) {
584         CmFree(credential->credData.data);
585     }
586 
587     CmFree(credential);
588     credential = nullptr;
589 }
590 }  // namespace CertManagerNapi
591