1 /*
2  * Copyright (c) 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_x509_crl.h"
17 #include <string>
18 #include "cf_log.h"
19 #include "cf_memory.h"
20 #include "cf_object_base.h"
21 #include "cf_result.h"
22 #include "config.h"
23 #include "napi/native_api.h"
24 #include "napi/native_common.h"
25 #include "napi_cert_defines.h"
26 #include "napi_cert_utils.h"
27 #include "napi_pub_key.h"
28 #include "napi_x509_certificate.h"
29 #include "napi_x509_crl_entry.h"
30 #include "securec.h"
31 #include "napi_x509_crl_match_parameters.h"
32 #include "utils.h"
33 #include "napi_x509_distinguished_name.h"
34 #include "napi_cert_extension.h"
35 
36 namespace OHOS {
37 namespace CertFramework {
38 thread_local napi_ref NapiX509Crl::classCrlRef_ = nullptr;
39 thread_local napi_ref NapiX509Crl::classCRLRef_ = nullptr;
40 
41 struct CfCtx {
42     AsyncType asyncType = ASYNC_TYPE_CALLBACK;
43     napi_value promise = nullptr;
44     napi_ref callback = nullptr;
45     napi_deferred deferred = nullptr;
46     napi_async_work asyncWork = nullptr;
47     napi_ref cfRef = nullptr;
48     napi_ref pubKeyParamsRef = nullptr;
49 
50     CfEncodingBlob *encodingBlob = nullptr;
51     NapiX509Crl *crlClass = nullptr;
52     HcfX509Certificate *certificate = nullptr;
53     HcfPubKey *pubKey = nullptr;
54     int32_t serialNumber = 0;
55     std::string createX509CrlName;
56     std::string returnClassName;
57 
58     HcfX509CrlEntry *crlEntry = nullptr;
59     int32_t errCode = 0;
60     const char *errMsg = nullptr;
61     HcfX509Crl *crl;
62     CfEncodingBlob *encoded = nullptr;
63     CfBlob *blob = nullptr;
64     CfArray *array = nullptr;
65 };
66 
FreeCryptoFwkCtx(napi_env env,CfCtx * context)67 static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
68 {
69     if (context == nullptr) {
70         return;
71     }
72 
73     if (context->asyncWork != nullptr) {
74         napi_delete_async_work(env, context->asyncWork);
75     }
76 
77     if (context->callback != nullptr) {
78         napi_delete_reference(env, context->callback);
79     }
80 
81     if (context->cfRef != nullptr) {
82         napi_delete_reference(env, context->cfRef);
83         context->cfRef = nullptr;
84     }
85 
86     if (context->pubKeyParamsRef != nullptr) {
87         napi_delete_reference(env, context->pubKeyParamsRef);
88         context->pubKeyParamsRef = nullptr;
89     }
90 
91     CfEncodingBlobDataFree(context->encodingBlob);
92     CfFree(context->encodingBlob);
93     context->encodingBlob = nullptr;
94 
95     CfEncodingBlobDataFree(context->encoded);
96     CfFree(context->encoded);
97     context->encoded = nullptr;
98 
99     CfBlobDataFree(context->blob);
100     CfFree(context->blob);
101     context->blob = nullptr;
102 
103     if (context->array != nullptr) {
104         CfFree(context->array->data);
105         context->array->data = nullptr;
106         CfFree(context->array);
107         context->array = nullptr;
108     }
109 
110     CfFree(context);
111 }
112 
ReturnCallbackResult(napi_env env,CfCtx * context,napi_value result)113 static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result)
114 {
115     napi_value businessError = nullptr;
116     if (context->errCode != CF_SUCCESS) {
117         businessError = CertGenerateBusinessError(env, context->errCode, context->errMsg);
118     }
119     napi_value params[ARGS_SIZE_TWO] = { businessError, result };
120 
121     napi_value func = nullptr;
122     napi_get_reference_value(env, context->callback, &func);
123 
124     napi_value recv = nullptr;
125     napi_value callFuncRet = nullptr;
126     napi_get_undefined(env, &recv);
127     napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
128 }
129 
ReturnPromiseResult(napi_env env,CfCtx * context,napi_value result)130 static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
131 {
132     if (context->errCode == CF_SUCCESS) {
133         napi_resolve_deferred(env, context->deferred, result);
134     } else {
135         napi_reject_deferred(env, context->deferred, CertGenerateBusinessError(env, context->errCode, context->errMsg));
136     }
137 }
138 
ReturnResult(napi_env env,CfCtx * context,napi_value result)139 static void ReturnResult(napi_env env, CfCtx *context, napi_value result)
140 {
141     if (context->asyncType == ASYNC_TYPE_CALLBACK) {
142         ReturnCallbackResult(env, context, result);
143     } else {
144         ReturnPromiseResult(env, context, result);
145     }
146 }
147 
CreateCallbackAndPromise(napi_env env,CfCtx * context,size_t argc,size_t maxCount,napi_value callbackValue)148 static bool CreateCallbackAndPromise(
149     napi_env env, CfCtx *context, size_t argc, size_t maxCount, napi_value callbackValue)
150 {
151     context->asyncType = GetAsyncType(env, argc, maxCount, callbackValue);
152     if (context->asyncType == ASYNC_TYPE_CALLBACK) {
153         if (!CertGetCallbackFromJSParams(env, callbackValue, &context->callback)) {
154             LOGE("x509 crl: get callback failed!");
155             return false;
156         }
157     } else {
158         napi_create_promise(env, &context->deferred, &context->promise);
159     }
160     return true;
161 }
162 
NapiX509Crl(HcfX509Crl * x509Crl)163 NapiX509Crl::NapiX509Crl(HcfX509Crl *x509Crl)
164 {
165     this->x509Crl_ = x509Crl;
166 }
167 
~NapiX509Crl()168 NapiX509Crl::~NapiX509Crl()
169 {
170     CfObjDestroy(this->x509Crl_);
171 }
172 
GetEncodedExecute(napi_env env,void * data)173 static void GetEncodedExecute(napi_env env, void *data)
174 {
175     CfCtx *context = static_cast<CfCtx *>(data);
176     HcfX509Crl *x509Crl = context->crlClass->GetX509Crl();
177     CfEncodingBlob *encodingBlob = static_cast<CfEncodingBlob *>(CfMalloc(sizeof(CfEncodingBlob), 0));
178     if (encodingBlob == nullptr) {
179         LOGE("malloc encoding blob failed!");
180         context->errCode = CF_ERR_MALLOC;
181         context->errMsg = "malloc encoding blob failed";
182         return;
183     }
184     context->errCode = x509Crl->getEncoded(x509Crl, encodingBlob);
185     if (context->errCode != CF_SUCCESS) {
186         LOGE("get encoded failed!");
187         context->errMsg = "get encoded failed";
188     }
189     context->encoded = encodingBlob;
190 }
191 
GetEncodedComplete(napi_env env,napi_status status,void * data)192 static void GetEncodedComplete(napi_env env, napi_status status, void *data)
193 {
194     CfCtx *context = static_cast<CfCtx *>(data);
195     if (context->errCode != CF_SUCCESS) {
196         ReturnResult(env, context, nullptr);
197         FreeCryptoFwkCtx(env, context);
198         return;
199     }
200     napi_value returnEncodingBlob = ConvertEncodingBlobToNapiValue(env, context->encoded);
201     ReturnResult(env, context, returnEncodingBlob);
202     FreeCryptoFwkCtx(env, context);
203 }
204 
VerifyExecute(napi_env env,void * data)205 static void VerifyExecute(napi_env env, void *data)
206 {
207     CfCtx *context = static_cast<CfCtx *>(data);
208     HcfX509Crl *x509Crl = context->crlClass->GetX509Crl();
209     context->errCode = x509Crl->verify(x509Crl, context->pubKey);
210     if (context->errCode != CF_SUCCESS) {
211         LOGE("verify crl failed!");
212         context->errMsg = "verify crl failed";
213     }
214 }
215 
VerifyComplete(napi_env env,napi_status status,void * data)216 static void VerifyComplete(napi_env env, napi_status status, void *data)
217 {
218     CfCtx *context = static_cast<CfCtx *>(data);
219     ReturnResult(env, context, CertNapiGetNull(env));
220     FreeCryptoFwkCtx(env, context);
221 }
222 
GetRevokedCertificatesExecute(napi_env env,void * data)223 void GetRevokedCertificatesExecute(napi_env env, void *data)
224 {
225     CfCtx *context = static_cast<CfCtx *>(data);
226     HcfX509Crl *x509Crl = context->crlClass->GetX509Crl();
227     CfArray *array = reinterpret_cast<CfArray *>(CfMalloc(sizeof(CfArray), 0));
228     if (array == nullptr) {
229         LOGE("malloc array failed!");
230         context->errCode = CF_ERR_MALLOC;
231         context->errMsg = "malloc array failed";
232         return;
233     }
234     context->errCode = x509Crl->getRevokedCerts(x509Crl, array);
235     if (context->errCode != CF_SUCCESS) {
236         LOGE("get revoked certs failed!");
237         context->errMsg = "get revoked certs failed";
238     }
239     context->array = array;
240 }
241 
GenerateCrlEntryArray(napi_env env,CfArray * array,std::string returnClassName)242 static napi_value GenerateCrlEntryArray(napi_env env, CfArray *array, std::string returnClassName)
243 {
244     if (array == nullptr) {
245         LOGE("crl entry array is null!");
246         return nullptr;
247     }
248     if (array->count == 0) {
249         LOGE("crl entry array count is 0!");
250         return nullptr;
251     }
252     napi_value returnArray = nullptr;
253     napi_create_array(env, &returnArray);
254     for (uint32_t i = 0; i < array->count; i++) {
255         CfBlob *blob = reinterpret_cast<CfBlob *>(array->data + i);
256         HcfX509CrlEntry *entry = reinterpret_cast<HcfX509CrlEntry *>(blob->data);
257         napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env, returnClassName);
258         NapiX509CrlEntry *x509CrlEntryClass = new (std::nothrow) NapiX509CrlEntry(entry);
259         if (x509CrlEntryClass == nullptr) {
260             napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a x509CrlEntry class"));
261             LOGE("Failed to create a x509CrlEntry class");
262             CfObjDestroy(entry);
263             return nullptr; /* the C++ objects wrapped will be automatically released by scope manager. */
264         }
265         napi_wrap(
266             env, instance, x509CrlEntryClass,
267             [](napi_env env, void *data, void *hint) {
268                 NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
269                 delete x509CrlEntryClass;
270                 return;
271             },
272             nullptr, nullptr);
273         napi_set_element(env, returnArray, i, instance);
274     }
275     return returnArray;
276 }
277 
GetRevokedCertificatesComplete(napi_env env,napi_status status,void * data)278 void GetRevokedCertificatesComplete(napi_env env, napi_status status, void *data)
279 {
280     CfCtx *context = static_cast<CfCtx *>(data);
281     if (context->errCode != CF_SUCCESS) {
282         ReturnResult(env, context, nullptr);
283         FreeCryptoFwkCtx(env, context);
284         return;
285     }
286     napi_value returnArray = GenerateCrlEntryArray(env, context->array, context->returnClassName);
287     ReturnResult(env, context, returnArray);
288     FreeCryptoFwkCtx(env, context);
289 }
290 
IsRevoked(napi_env env,napi_callback_info info)291 napi_value NapiX509Crl::IsRevoked(napi_env env, napi_callback_info info)
292 {
293     size_t argc = ARGS_SIZE_ONE;
294     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
295     napi_value thisVar = nullptr;
296     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
297     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
298         return nullptr;
299     }
300 
301     NapiX509Certificate *napiX509Cert = nullptr;
302     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
303     if (napiX509Cert == nullptr) {
304         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "napiX509Cert is null"));
305         LOGE("napiX509Cert is null!");
306         return nullptr;
307     }
308 
309     HcfX509Crl *x509Crl = GetX509Crl();
310     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
311     bool isRevoked = x509Crl->base.isRevoked(&(x509Crl->base), &(certificate->base));
312     napi_value result = nullptr;
313     napi_get_boolean(env, isRevoked, &result);
314     return result;
315 }
316 
GetType(napi_env env,napi_callback_info info)317 napi_value NapiX509Crl::GetType(napi_env env, napi_callback_info info)
318 {
319     HcfX509Crl *x509Crl = GetX509Crl();
320     const char *type = x509Crl->base.getType(&(x509Crl->base));
321     napi_value result = nullptr;
322     napi_create_string_utf8(env, type, strlen(type), &result);
323     return result;
324 }
325 
GetEncoded(napi_env env,napi_callback_info info)326 napi_value NapiX509Crl::GetEncoded(napi_env env, napi_callback_info info)
327 {
328     size_t argc = ARGS_SIZE_ONE;
329     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
330     napi_value thisVar = nullptr;
331     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
332     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
333         return nullptr;
334     }
335 
336     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
337     if (context == nullptr) {
338         LOGE("malloc context failed!");
339         return nullptr;
340     }
341     context->crlClass = this;
342 
343     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
344         LOGE("create reference failed!");
345         FreeCryptoFwkCtx(env, context);
346         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
347         return nullptr;
348     }
349 
350     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
351         FreeCryptoFwkCtx(env, context);
352         return nullptr;
353     }
354 
355     napi_create_async_work(env, nullptr, CertGetResourceName(env, "GetEncoded"), GetEncodedExecute, GetEncodedComplete,
356         static_cast<void *>(context), &context->asyncWork);
357 
358     napi_queue_async_work(env, context->asyncWork);
359     if (context->asyncType == ASYNC_TYPE_PROMISE) {
360         return context->promise;
361     } else {
362         return CertNapiGetNull(env);
363     }
364 }
365 
Verify(napi_env env,napi_callback_info info)366 __attribute__((no_sanitize("cfi"))) napi_value NapiX509Crl::Verify(napi_env env, napi_callback_info info)
367 {
368     size_t argc = ARGS_SIZE_TWO;
369     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
370     napi_value thisVar = nullptr;
371     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
372     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
373         return nullptr;
374     }
375 
376     NapiPubKey *pubKey = nullptr;
377     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&pubKey));
378     if (pubKey == nullptr) {
379         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "public key is null"));
380         LOGE("pubKey is null!");
381         return nullptr;
382     }
383 
384     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
385     if (context == nullptr) {
386         LOGE("malloc context failed!");
387         return nullptr;
388     }
389     context->pubKey = pubKey->GetPubKey();
390     context->crlClass = this;
391 
392     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
393         LOGE("create reference failed!");
394         FreeCryptoFwkCtx(env, context);
395         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
396         return nullptr;
397     }
398 
399     if (napi_create_reference(env, argv[PARAM0], 1, &context->pubKeyParamsRef) != napi_ok) {
400         LOGE("create param ref failed!");
401         FreeCryptoFwkCtx(env, context);
402         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create param ref failed"));
403         return nullptr;
404     }
405 
406     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
407         FreeCryptoFwkCtx(env, context);
408         return nullptr;
409     }
410 
411     napi_create_async_work(env, nullptr, CertGetResourceName(env, "Verify"), VerifyExecute, VerifyComplete,
412         static_cast<void *>(context), &context->asyncWork);
413 
414     napi_queue_async_work(env, context->asyncWork);
415     if (context->asyncType == ASYNC_TYPE_PROMISE) {
416         return context->promise;
417     } else {
418         return CertNapiGetNull(env);
419     }
420 }
421 
GetVersion(napi_env env,napi_callback_info info)422 napi_value NapiX509Crl::GetVersion(napi_env env, napi_callback_info info)
423 {
424     HcfX509Crl *x509Crl = GetX509Crl();
425     int version = x509Crl->getVersion(x509Crl);
426     napi_value result = nullptr;
427     napi_create_int32(env, version, &result);
428     return result;
429 }
430 
GetIssuerDN(napi_env env,napi_callback_info info)431 napi_value NapiX509Crl::GetIssuerDN(napi_env env, napi_callback_info info)
432 {
433     HcfX509Crl *x509Crl = GetX509Crl();
434     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
435     if (blob == nullptr) {
436         LOGE("malloc blob failed!");
437         return nullptr;
438     }
439     CfResult ret = x509Crl->getIssuerName(x509Crl, blob);
440     if (ret != CF_SUCCESS) {
441         napi_throw(env, CertGenerateBusinessError(env, ret, "get issuer name failed"));
442         LOGE("getIssuerDN failed!");
443         CfFree(blob);
444         blob = nullptr;
445         return nullptr;
446     }
447     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
448     CfBlobDataFree(blob);
449     CfFree(blob);
450     blob = nullptr;
451     return returnBlob;
452 }
453 
GetThisUpdate(napi_env env,napi_callback_info info)454 napi_value NapiX509Crl::GetThisUpdate(napi_env env, napi_callback_info info)
455 {
456     HcfX509Crl *x509Crl = GetX509Crl();
457     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
458     if (blob == nullptr) {
459         LOGE("malloc blob failed!");
460         return nullptr;
461     }
462     CfResult ret = x509Crl->getLastUpdate(x509Crl, blob);
463     if (ret != CF_SUCCESS) {
464         napi_throw(env, CertGenerateBusinessError(env, ret, "get last update failed"));
465         LOGE("getLastUpdate failed!");
466         CfFree(blob);
467         blob = nullptr;
468         return nullptr;
469     }
470     napi_value result = nullptr;
471     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
472     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
473     CfBlobDataFree(blob);
474     CfFree(blob);
475     blob = nullptr;
476     return result;
477 }
478 
GetNextUpdate(napi_env env,napi_callback_info info)479 napi_value NapiX509Crl::GetNextUpdate(napi_env env, napi_callback_info info)
480 {
481     HcfX509Crl *x509Crl = GetX509Crl();
482     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
483     if (blob == nullptr) {
484         LOGE("malloc blob failed!");
485         return nullptr;
486     }
487     CfResult ret = x509Crl->getNextUpdate(x509Crl, blob);
488     if (ret != CF_SUCCESS) {
489         napi_throw(env, CertGenerateBusinessError(env, ret, "get next update failed"));
490         LOGE("getNextUpdate failed!");
491         CfFree(blob);
492         blob = nullptr;
493         return nullptr;
494     }
495     napi_value result = nullptr;
496     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
497     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
498     CfBlobDataFree(blob);
499     CfFree(blob);
500     blob = nullptr;
501     return result;
502 }
503 
GetCrlSerialNumberFromNapiValue(napi_env env,napi_value arg,CfBlob & outBlob)504 static bool GetCrlSerialNumberFromNapiValue(napi_env env, napi_value arg, CfBlob &outBlob)
505 {
506     napi_valuetype valueType;
507     napi_typeof(env, arg, &valueType);
508     if (valueType != napi_number) {
509         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "param type error"));
510         LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
511         return false;
512     }
513 
514     uint8_t serialBuf[MAX_SN_BYTE_CNT] = { 0 };
515     uint32_t serialLen = sizeof(int64_t);
516     int64_t tmpData = 0;
517     if (napi_get_value_int64(env, arg, &tmpData) != napi_ok || tmpData < 0) {
518         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "get serialNum failed"));
519         LOGE("can not get int64 value");
520         return false;
521     }
522 
523     if (memcpy_s(serialBuf, sizeof(serialBuf), &tmpData, sizeof(int64_t)) != EOK) {
524         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_COPY, "copy serialNum failed"));
525         LOGE("copy serialNum failed");
526         return false;
527     }
528 
529     outBlob.size = serialLen;
530     outBlob.data = static_cast<uint8_t *>(CfMalloc(serialLen, 0));
531     if (outBlob.data == nullptr) {
532         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "malloc serialNum failed"));
533         LOGE("malloc blob data failed!");
534         return false;
535     }
536     // reverse data: because BN_bin2bn() converts the positive integer in big-endian form of length len into a BIGNUM
537     for (uint32_t i = 0; i < serialLen; ++i) {
538         outBlob.data[i] = serialBuf[outBlob.size - 1 - i];
539     }
540 
541     return true;
542 }
543 
GetRevokedCertificate(napi_env env,napi_callback_info info,std::string returnClassName)544 napi_value NapiX509Crl::GetRevokedCertificate(napi_env env, napi_callback_info info, std::string returnClassName)
545 {
546     size_t argc = ARGS_SIZE_ONE;
547     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
548     napi_value thisVar = nullptr;
549     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
550     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
551         return nullptr;
552     }
553 
554     CfBlob serialNumber = { 0, nullptr };
555     bool getSnRet = false;
556     if (returnClassName == std::string("X509CrlEntry")) {
557         getSnRet = GetCrlSerialNumberFromNapiValue(env, argv[PARAM0], serialNumber);
558     } else {
559         getSnRet = CertGetSerialNumberFromBigIntJSParams(env, argv[PARAM0], serialNumber);
560     }
561     if (!getSnRet) {
562         LOGE("get serialNumber failed");
563         return nullptr;
564     }
565 
566     HcfX509Crl *x509Crl = GetX509Crl();
567     HcfX509CrlEntry *crlEntry = nullptr;
568     CfResult ret = x509Crl->getRevokedCert(x509Crl, &serialNumber, &crlEntry);
569     CF_FREE_PTR(serialNumber.data);
570     if (ret != CF_SUCCESS) {
571         napi_throw(env, CertGenerateBusinessError(env, ret, "get revoked cert failed!"));
572         LOGE("get revoked cert failed!");
573         return nullptr;
574     }
575 
576     napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env, returnClassName);
577     NapiX509CrlEntry *x509CrlEntryClass = new (std::nothrow) NapiX509CrlEntry(crlEntry);
578     if (x509CrlEntryClass == nullptr) {
579         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a x509CrlEntry class"));
580         LOGE("Failed to create a x509CrlEntry class");
581         CfObjDestroy(crlEntry);
582         return nullptr;
583     }
584 
585     napi_wrap(
586         env, instance, x509CrlEntryClass,
587         [](napi_env env, void *data, void *hint) {
588             NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
589             delete x509CrlEntryClass;
590             return;
591         },
592         nullptr, nullptr);
593     return instance;
594 }
595 
GetRevokedCertificateWithCert(napi_env env,napi_callback_info info,std::string returnClassName)596 napi_value NapiX509Crl::GetRevokedCertificateWithCert(
597     napi_env env, napi_callback_info info, std::string returnClassName)
598 {
599     size_t argc = ARGS_SIZE_ONE;
600     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
601     napi_value thisVar = nullptr;
602     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
603     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
604         return nullptr;
605     }
606 
607     NapiX509Certificate *napiX509Cert = nullptr;
608     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
609     if (napiX509Cert == nullptr) {
610         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "napiX509Cert is null"));
611         LOGE("napiX509Cert is null!");
612         return nullptr;
613     }
614 
615     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
616     HcfX509Crl *x509Crl = GetX509Crl();
617     HcfX509CrlEntry *crlEntry = nullptr;
618     CfResult ret = x509Crl->getRevokedCertWithCert(x509Crl, certificate, &crlEntry);
619     if (ret != CF_SUCCESS) {
620         napi_throw(env, CertGenerateBusinessError(env, ret, "get revoked cert with cert failed!"));
621         LOGE("get revoked cert with cert failed!");
622         return nullptr;
623     }
624 
625     napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env, returnClassName);
626     NapiX509CrlEntry *x509CrlEntryClass = new (std::nothrow) NapiX509CrlEntry(crlEntry);
627     if (x509CrlEntryClass == nullptr) {
628         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a x509CrlEntry class"));
629         LOGE("Failed to create a x509CrlEntry class");
630         CfObjDestroy(crlEntry);
631         return nullptr;
632     }
633     napi_wrap(
634         env, instance, x509CrlEntryClass,
635         [](napi_env env, void *data, void *hint) {
636             NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
637             delete x509CrlEntryClass;
638             return;
639         },
640         nullptr, nullptr);
641     return instance;
642 }
643 
GetRevokedCertificates(napi_env env,napi_callback_info info,std::string returnClassName)644 napi_value NapiX509Crl::GetRevokedCertificates(napi_env env, napi_callback_info info, std::string returnClassName)
645 {
646     size_t argc = ARGS_SIZE_ONE;
647     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
648     napi_value thisVar = nullptr;
649     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
650     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
651         return nullptr;
652     }
653 
654     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
655     if (context == nullptr) {
656         LOGE("malloc context failed!");
657         return nullptr;
658     }
659     context->crlClass = this;
660 
661     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
662         LOGE("create reference failed!");
663         FreeCryptoFwkCtx(env, context);
664         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
665         return nullptr;
666     }
667 
668     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
669         FreeCryptoFwkCtx(env, context);
670         return nullptr;
671     }
672 
673     context->returnClassName = returnClassName;
674 
675     napi_create_async_work(env, nullptr, CertGetResourceName(env, "GetRevokedCertificates"),
676         GetRevokedCertificatesExecute, GetRevokedCertificatesComplete, static_cast<void *>(context),
677         &context->asyncWork);
678 
679     napi_queue_async_work(env, context->asyncWork);
680     if (context->asyncType == ASYNC_TYPE_PROMISE) {
681         return context->promise;
682     } else {
683         return CertNapiGetNull(env);
684     }
685 }
686 
GetTBSCertList(napi_env env,napi_callback_info info)687 napi_value NapiX509Crl::GetTBSCertList(napi_env env, napi_callback_info info)
688 {
689     HcfX509Crl *x509Crl = GetX509Crl();
690     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
691     if (blob == nullptr) {
692         LOGE("malloc blob failed!");
693         return nullptr;
694     }
695     CfResult result = x509Crl->getTbsInfo(x509Crl, blob);
696     if (result != CF_SUCCESS) {
697         napi_throw(env, CertGenerateBusinessError(env, result, "get tbs info failed"));
698         LOGE("get tbs info failed!");
699         CfFree(blob);
700         blob = nullptr;
701         return nullptr;
702     }
703     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
704     CfBlobDataFree(blob);
705     CfFree(blob);
706     blob = nullptr;
707     return returnBlob;
708 }
709 
ToString(napi_env env,napi_callback_info info)710 napi_value NapiX509Crl::ToString(napi_env env, napi_callback_info info)
711 {
712     HcfX509Crl *x509Crl = GetX509Crl();
713     CfBlob blob = { 0, nullptr };
714     CfResult result = x509Crl->toString(x509Crl, &blob);
715     if (result != CF_SUCCESS) {
716         LOGE("toString failed!");
717         napi_throw(env, CertGenerateBusinessError(env, result, "toString failed"));
718         return nullptr;
719     }
720     napi_value returnBlob = nullptr;
721     napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnBlob);
722     CfBlobDataFree(&blob);
723     return returnBlob;
724 }
725 
HashCode(napi_env env,napi_callback_info info)726 napi_value NapiX509Crl::HashCode(napi_env env, napi_callback_info info)
727 {
728     HcfX509Crl *x509Crl = GetX509Crl();
729     CfBlob blob = { 0, nullptr };
730     CfResult result = x509Crl->hashCode(x509Crl, &blob);
731     if (result != CF_SUCCESS) {
732         LOGE("hashCode failed!");
733         napi_throw(env, CertGenerateBusinessError(env, result, "hashCode failed"));
734         return nullptr;
735     }
736     napi_value returnBlob = ConvertBlobToUint8ArrNapiValue(env, &blob);
737     CfBlobDataFree(&blob);
738     return returnBlob;
739 }
740 
CreateCertExtsJSInstance(napi_env env)741 static napi_value CreateCertExtsJSInstance(napi_env env)
742 {
743     napi_value constructor = nullptr;
744     napi_value instance = nullptr;
745     napi_get_reference_value(env, NapiCertExtension::classRef_, &constructor);
746     napi_new_instance(env, constructor, 0, nullptr, &instance);
747     return instance;
748 }
749 
BuildCertExtsObject(napi_env env,CfEncodingBlob * encodingBlob)750 static napi_value BuildCertExtsObject(napi_env env, CfEncodingBlob *encodingBlob)
751 {
752     CfObject *extsObj = nullptr;
753     int32_t res = CfCreate(CF_OBJ_TYPE_EXTENSION, encodingBlob, &extsObj);
754     if (res != CF_SUCCESS) {
755         LOGE("CfCreate error!");
756         return nullptr;
757     }
758     napi_value jsObject = CreateCertExtsJSInstance(env);
759     NapiCertExtension *napiObject = new (std::nothrow) NapiCertExtension(extsObj);
760     if (napiObject == nullptr) {
761         LOGE("Failed to create napi extension class");
762         extsObj->destroy(&(extsObj));
763         return nullptr;
764     }
765     napi_wrap(
766         env, jsObject, napiObject,
767         [](napi_env env, void *data, void *hint) {
768             NapiCertExtension *certExts = static_cast<NapiCertExtension *>(data);
769             delete certExts;
770             return;
771         }, nullptr, nullptr);
772     return jsObject;
773 }
774 
GetExtensionsObject(napi_env env,napi_callback_info info)775 napi_value NapiX509Crl::GetExtensionsObject(napi_env env, napi_callback_info info)
776 {
777     HcfX509Crl *x509Crl = GetX509Crl();
778     CfBlob blob = { 0, nullptr };
779     CfResult result = x509Crl->getExtensionsObject(x509Crl, &blob);
780     if (result != CF_SUCCESS) {
781         LOGE("get Extensions Object failed!");
782         napi_throw(env, CertGenerateBusinessError(env, result, "get Extensions Object failed"));
783         return nullptr;
784     }
785 
786     CfEncodingBlob *encodingBlob = static_cast<CfEncodingBlob *>(CfMalloc(sizeof(CfEncodingBlob), 0));
787     if (encodingBlob == nullptr) {
788         LOGE("malloc encoding blob failed!");
789         CfBlobDataFree(&blob);
790         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "CfMalloc failed"));
791         return nullptr;
792     }
793     if (!ConvertBlobToEncodingBlob(blob, encodingBlob)) {
794         LOGE("ConvertBlobToEncodingBlob failed!");
795         CfBlobDataFree(&blob);
796         CfFree(encodingBlob);
797         encodingBlob = nullptr;
798         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_CRYPTO_OPERATION, "ConvertBlobToEncodingBlob failed"));
799         return nullptr;
800     }
801     CfBlobDataFree(&blob);
802 
803     napi_value object = BuildCertExtsObject(env, encodingBlob);
804     CfEncodingBlobDataFree(encodingBlob);
805     CfFree(encodingBlob);
806     encodingBlob = nullptr;
807     if (object == nullptr) {
808         LOGE("BuildCertExtsObject failed!");
809         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "BuildCertExtsObject failed"));
810         return nullptr;
811     }
812 
813     return object;
814 }
815 
GetIssuerX500DistinguishedName(napi_env env,napi_callback_info info)816 napi_value NapiX509Crl::GetIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
817 {
818     HcfX509Crl *x509Crl = GetX509Crl();
819     CfBlob blob = { 0, nullptr };
820     CfResult ret = x509Crl->getIssuerName(x509Crl, &blob);
821     if (ret != CF_SUCCESS) {
822         LOGE("getIssuerName failed!");
823         napi_throw(env, CertGenerateBusinessError(env, ret, "get issuer name failed"));
824         return nullptr;
825     }
826     HcfX509DistinguishedName *x509Name = nullptr;
827     ret = HcfX509DistinguishedNameCreate(&blob, true, &x509Name);
828     CfBlobDataFree(&blob);
829     if (ret != CF_SUCCESS) {
830         LOGE("HcfX509DistinguishedNameCreate failed");
831         napi_throw(env, CertGenerateBusinessError(env, ret, "HcfX509DistinguishedNameCreate failed"));
832         return nullptr;
833     }
834     napi_value instance = NapiX509DistinguishedName::CreateX509DistinguishedName(env);
835     NapiX509DistinguishedName *x509NameClass = new (std::nothrow) NapiX509DistinguishedName(x509Name);
836     if (x509NameClass == nullptr) {
837         LOGE("Failed to create a NapiX509DistinguishedName class");
838         CfObjDestroy(x509Name);
839         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "NapiX509DistinguishedName new failed"));
840         return nullptr;
841     }
842     napi_wrap(
843         env, instance, x509NameClass,
844         [](napi_env env, void *data, void *hint) {
845             NapiX509DistinguishedName *nameClass = static_cast<NapiX509DistinguishedName *>(data);
846             delete nameClass;
847             return;
848         }, nullptr, nullptr);
849     return instance;
850 }
851 
GetSignature(napi_env env,napi_callback_info info)852 napi_value NapiX509Crl::GetSignature(napi_env env, napi_callback_info info)
853 {
854     HcfX509Crl *x509Crl = GetX509Crl();
855     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
856     if (blob == nullptr) {
857         LOGE("malloc blob failed!");
858         return nullptr;
859     }
860     CfResult result = x509Crl->getSignature(x509Crl, blob);
861     if (result != CF_SUCCESS) {
862         napi_throw(env, CertGenerateBusinessError(env, result, "get signature failed"));
863         LOGE("getSignature failed!");
864         CfFree(blob);
865         blob = nullptr;
866         return nullptr;
867     }
868     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
869     CfBlobDataFree(blob);
870     CfFree(blob);
871     blob = nullptr;
872     return returnBlob;
873 }
874 
GetSigAlgName(napi_env env,napi_callback_info info)875 napi_value NapiX509Crl::GetSigAlgName(napi_env env, napi_callback_info info)
876 {
877     HcfX509Crl *x509Crl = GetX509Crl();
878     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
879     if (blob == nullptr) {
880         LOGE("malloc blob failed!");
881         return nullptr;
882     }
883     CfResult ret = x509Crl->getSignatureAlgName(x509Crl, blob);
884     if (ret != CF_SUCCESS) {
885         napi_throw(env, CertGenerateBusinessError(env, ret, "get signature alg name failed"));
886         LOGE("getSigAlgName failed!");
887         CfFree(blob);
888         blob = nullptr;
889         return nullptr;
890     }
891     napi_value result = nullptr;
892     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
893     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
894     CfBlobDataFree(blob);
895     CfFree(blob);
896     blob = nullptr;
897     return result;
898 }
899 
GetSigAlgOID(napi_env env,napi_callback_info info)900 napi_value NapiX509Crl::GetSigAlgOID(napi_env env, napi_callback_info info)
901 {
902     HcfX509Crl *x509Crl = GetX509Crl();
903     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
904     if (blob == nullptr) {
905         LOGE("malloc blob failed!");
906         return nullptr;
907     }
908     CfResult ret = x509Crl->getSignatureAlgOid(x509Crl, blob);
909     if (ret != CF_SUCCESS) {
910         napi_throw(env, CertGenerateBusinessError(env, ret, "get signature alg oid failed"));
911         LOGE("getSigAlgOID failed!");
912         CfFree(blob);
913         blob = nullptr;
914         return nullptr;
915     }
916     napi_value result = nullptr;
917     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
918     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
919     CfBlobDataFree(blob);
920     CfFree(blob);
921     blob = nullptr;
922     return result;
923 }
924 
GetSigAlgParams(napi_env env,napi_callback_info info)925 napi_value NapiX509Crl::GetSigAlgParams(napi_env env, napi_callback_info info)
926 {
927     HcfX509Crl *x509Crl = GetX509Crl();
928     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
929     if (blob == nullptr) {
930         LOGE("malloc blob failed!");
931         return nullptr;
932     }
933     CfResult result = x509Crl->getSignatureAlgParams(x509Crl, blob);
934     if (result != CF_SUCCESS) {
935         napi_throw(env, CertGenerateBusinessError(env, result, "get signature alg params failed"));
936         LOGE("getSigAlgParams failed!");
937         CfFree(blob);
938         blob = nullptr;
939         return nullptr;
940     }
941     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
942     CfBlobDataFree(blob);
943     CfFree(blob);
944     blob = nullptr;
945     return returnBlob;
946 }
947 
GetExtensions(napi_env env,napi_callback_info info)948 napi_value NapiX509Crl::GetExtensions(napi_env env, napi_callback_info info)
949 {
950     HcfX509Crl *x509Crl = GetX509Crl();
951     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
952     if (blob == nullptr) {
953         LOGE("malloc blob failed!");
954         return nullptr;
955     }
956     CfResult result = x509Crl->getExtensions(x509Crl, blob);
957     if (result != CF_SUCCESS) {
958         napi_throw(env, CertGenerateBusinessError(env, result, "get extensions failed"));
959         LOGE("getExtensions failed!");
960         CfFree(blob);
961         blob = nullptr;
962         return nullptr;
963     }
964     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
965     CfBlobDataFree(blob);
966     CfFree(blob);
967     blob = nullptr;
968     return returnBlob;
969 }
970 
Match(napi_env env,napi_callback_info info)971 napi_value NapiX509Crl::Match(napi_env env, napi_callback_info info)
972 {
973     size_t argc = ARGS_SIZE_ONE;
974     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
975     napi_value thisVar = nullptr;
976     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
977     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
978         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "CertCheckArgsCount failed"));
979         LOGE("CertCheckArgsCount failed!");
980         return nullptr;
981     }
982 
983     HcfX509CrlMatchParams *param = static_cast<HcfX509CrlMatchParams *>(CfMalloc(sizeof(HcfX509CrlMatchParams), 0));
984     if (param == nullptr) {
985         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "malloc matchParams failed"));
986         LOGE("malloc matchParams failed!");
987         return nullptr;
988     }
989     if (!BuildX509CrlMatchParams(env, argv[PARAM0], param)) {
990         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "BuildX509CrlMatchParams failed"));
991         LOGE("BuildX509CrlMatchParams failed!");
992         FreeX509CrlMatchParams(param);
993         return nullptr;
994     }
995 
996     bool boolFlag = false;
997     CfResult result = MatchProc(param, boolFlag);
998     if (result != CF_SUCCESS) {
999         napi_throw(env, CertGenerateBusinessError(env, result, "match failed"));
1000         LOGE("call match failed!");
1001         FreeX509CrlMatchParams(param);
1002         return nullptr;
1003     }
1004     FreeX509CrlMatchParams(param);
1005     napi_value ret = nullptr;
1006     napi_get_boolean(env, boolFlag, &ret);
1007     return ret;
1008 }
1009 
MatchProc(HcfX509CrlMatchParams * param,bool & boolFlag)1010 CfResult NapiX509Crl::MatchProc(HcfX509CrlMatchParams *param, bool &boolFlag)
1011 {
1012     HcfX509Crl *x509Crl = GetX509Crl();
1013     return x509Crl->match(x509Crl, param, &boolFlag);
1014 }
1015 
NapiIsRevoked(napi_env env,napi_callback_info info)1016 static napi_value NapiIsRevoked(napi_env env, napi_callback_info info)
1017 {
1018     napi_value thisVar = nullptr;
1019     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1020     NapiX509Crl *x509Crl = nullptr;
1021     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1022     if (x509Crl == nullptr) {
1023         LOGE("x509Crl is nullptr!");
1024         return nullptr;
1025     }
1026     return x509Crl->IsRevoked(env, info);
1027 }
1028 
NapiGetType(napi_env env,napi_callback_info info)1029 static napi_value NapiGetType(napi_env env, napi_callback_info info)
1030 {
1031     napi_value thisVar = nullptr;
1032     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1033     NapiX509Crl *x509Crl = nullptr;
1034     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1035     if (x509Crl == nullptr) {
1036         LOGE("x509Crl is nullptr!");
1037         return nullptr;
1038     }
1039     return x509Crl->GetType(env, info);
1040 }
1041 
NapiGetEncoded(napi_env env,napi_callback_info info)1042 static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
1043 {
1044     napi_value thisVar = nullptr;
1045     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1046     NapiX509Crl *x509Crl = nullptr;
1047     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1048     if (x509Crl == nullptr) {
1049         LOGE("x509Crl is nullptr!");
1050         return nullptr;
1051     }
1052     return x509Crl->GetEncoded(env, info);
1053 }
1054 
NapiVerify(napi_env env,napi_callback_info info)1055 static napi_value NapiVerify(napi_env env, napi_callback_info info)
1056 {
1057     napi_value thisVar = nullptr;
1058     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1059     NapiX509Crl *x509Crl = nullptr;
1060     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1061     if (x509Crl == nullptr) {
1062         LOGE("x509Crl is nullptr!");
1063         return nullptr;
1064     }
1065     return x509Crl->Verify(env, info);
1066 }
1067 
NapiGetVersion(napi_env env,napi_callback_info info)1068 static napi_value NapiGetVersion(napi_env env, napi_callback_info info)
1069 {
1070     napi_value thisVar = nullptr;
1071     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1072     NapiX509Crl *x509Crl = nullptr;
1073     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1074     if (x509Crl == nullptr) {
1075         LOGE("x509Crl is nullptr!");
1076         return nullptr;
1077     }
1078     return x509Crl->GetVersion(env, info);
1079 }
1080 
NapiGetIssuerDN(napi_env env,napi_callback_info info)1081 static napi_value NapiGetIssuerDN(napi_env env, napi_callback_info info)
1082 {
1083     napi_value thisVar = nullptr;
1084     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1085     NapiX509Crl *x509Crl = nullptr;
1086     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1087     if (x509Crl == nullptr) {
1088         LOGE("x509Crl is nullptr!");
1089         return nullptr;
1090     }
1091     return x509Crl->GetIssuerDN(env, info);
1092 }
1093 
NapiGetThisUpdate(napi_env env,napi_callback_info info)1094 static napi_value NapiGetThisUpdate(napi_env env, napi_callback_info info)
1095 {
1096     napi_value thisVar = nullptr;
1097     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1098     NapiX509Crl *x509Crl = nullptr;
1099     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1100     if (x509Crl == nullptr) {
1101         LOGE("x509Crl is nullptr!");
1102         return nullptr;
1103     }
1104     return x509Crl->GetThisUpdate(env, info);
1105 }
1106 
NapiGetNextUpdate(napi_env env,napi_callback_info info)1107 static napi_value NapiGetNextUpdate(napi_env env, napi_callback_info info)
1108 {
1109     napi_value thisVar = nullptr;
1110     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1111     NapiX509Crl *x509Crl = nullptr;
1112     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1113     if (x509Crl == nullptr) {
1114         LOGE("x509Crl is nullptr!");
1115         return nullptr;
1116     }
1117     return x509Crl->GetNextUpdate(env, info);
1118 }
1119 
NapiCrlGetRevokedCertificate(napi_env env,napi_callback_info info)1120 static napi_value NapiCrlGetRevokedCertificate(napi_env env, napi_callback_info info)
1121 {
1122     napi_value thisVar = nullptr;
1123     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1124     NapiX509Crl *x509Crl = nullptr;
1125     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1126     if (x509Crl == nullptr) {
1127         LOGE("x509Crl is nullptr!");
1128         return nullptr;
1129     }
1130     return x509Crl->GetRevokedCertificate(env, info, std::string("X509CrlEntry"));
1131 }
1132 
NapiCrlGetRevokedCertificateWithCert(napi_env env,napi_callback_info info)1133 static napi_value NapiCrlGetRevokedCertificateWithCert(napi_env env, napi_callback_info info)
1134 {
1135     napi_value thisVar = nullptr;
1136     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1137     NapiX509Crl *x509Crl = nullptr;
1138     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1139     if (x509Crl == nullptr) {
1140         LOGE("x509Crl is nullptr!");
1141         return nullptr;
1142     }
1143     return x509Crl->GetRevokedCertificateWithCert(env, info, std::string("X509CrlEntry"));
1144 }
1145 
NapiCrlGetRevokedCertificates(napi_env env,napi_callback_info info)1146 static napi_value NapiCrlGetRevokedCertificates(napi_env env, napi_callback_info info)
1147 {
1148     napi_value thisVar = nullptr;
1149     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1150     NapiX509Crl *x509Crl = nullptr;
1151     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1152     if (x509Crl == nullptr) {
1153         LOGE("x509Crl is nullptr!");
1154         return nullptr;
1155     }
1156     return x509Crl->GetRevokedCertificates(env, info, std::string("X509CrlEntry"));
1157 }
1158 
NapiCRLGetRevokedCertificate(napi_env env,napi_callback_info info)1159 static napi_value NapiCRLGetRevokedCertificate(napi_env env, napi_callback_info info)
1160 {
1161     napi_value thisVar = nullptr;
1162     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1163     NapiX509Crl *x509Crl = nullptr;
1164     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1165     if (x509Crl == nullptr) {
1166         LOGE("x509Crl is nullptr!");
1167         return nullptr;
1168     }
1169     return x509Crl->GetRevokedCertificate(env, info, std::string("X509CRLEntry"));
1170 }
1171 
NapiCRLGetRevokedCertificateWithCert(napi_env env,napi_callback_info info)1172 static napi_value NapiCRLGetRevokedCertificateWithCert(napi_env env, napi_callback_info info)
1173 {
1174     napi_value thisVar = nullptr;
1175     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1176     NapiX509Crl *x509Crl = nullptr;
1177     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1178     if (x509Crl == nullptr) {
1179         LOGE("x509Crl is nullptr!");
1180         return nullptr;
1181     }
1182     return x509Crl->GetRevokedCertificateWithCert(env, info, std::string("X509CRLEntry"));
1183 }
1184 
NapiCRLGetRevokedCertificates(napi_env env,napi_callback_info info)1185 static napi_value NapiCRLGetRevokedCertificates(napi_env env, napi_callback_info info)
1186 {
1187     napi_value thisVar = nullptr;
1188     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1189     NapiX509Crl *x509Crl = nullptr;
1190     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1191     if (x509Crl == nullptr) {
1192         LOGE("x509Crl is nullptr!");
1193         return nullptr;
1194     }
1195     return x509Crl->GetRevokedCertificates(env, info, std::string("X509CRLEntry"));
1196 }
1197 
NapiCrlGetTBSCertList(napi_env env,napi_callback_info info)1198 static napi_value NapiCrlGetTBSCertList(napi_env env, napi_callback_info info)
1199 {
1200     napi_value thisVar = nullptr;
1201     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1202     NapiX509Crl *x509Crl = nullptr;
1203     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1204     if (x509Crl == nullptr) {
1205         LOGE("x509Crl is nullptr!");
1206         return nullptr;
1207     }
1208     return x509Crl->GetTBSCertList(env, info);
1209 }
1210 
NapiCRLGetTBSCertList(napi_env env,napi_callback_info info)1211 static napi_value NapiCRLGetTBSCertList(napi_env env, napi_callback_info info)
1212 {
1213     napi_value thisVar = nullptr;
1214     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1215     NapiX509Crl *x509Crl = nullptr;
1216     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1217     if (x509Crl == nullptr) {
1218         LOGE("x509Crl is nullptr!");
1219         return nullptr;
1220     }
1221     return x509Crl->GetTBSCertList(env, info);
1222 }
1223 
NapiToString(napi_env env,napi_callback_info info)1224 static napi_value NapiToString(napi_env env, napi_callback_info info)
1225 {
1226     napi_value thisVar = nullptr;
1227     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1228     NapiX509Crl *x509Crl = nullptr;
1229     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1230     if (x509Crl == nullptr) {
1231         LOGE("x509Crl is nullptr!");
1232         return nullptr;
1233     }
1234     return x509Crl->ToString(env, info);
1235 }
1236 
NapiHashCode(napi_env env,napi_callback_info info)1237 static napi_value NapiHashCode(napi_env env, napi_callback_info info)
1238 {
1239     napi_value thisVar = nullptr;
1240     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1241     NapiX509Crl *x509Crl = nullptr;
1242     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1243     if (x509Crl == nullptr) {
1244         LOGE("x509Crl is nullptr!");
1245         return nullptr;
1246     }
1247     return x509Crl->HashCode(env, info);
1248 }
1249 
NapiGetExtensionsObject(napi_env env,napi_callback_info info)1250 static napi_value NapiGetExtensionsObject(napi_env env, napi_callback_info info)
1251 {
1252     napi_value thisVar = nullptr;
1253     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1254     NapiX509Crl *x509Crl = nullptr;
1255     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1256     if (x509Crl == nullptr) {
1257         LOGE("x509Crl is nullptr!");
1258         return nullptr;
1259     }
1260     return x509Crl->GetExtensionsObject(env, info);
1261 }
1262 
NapiGetIssuerX500DistinguishedName(napi_env env,napi_callback_info info)1263 static napi_value NapiGetIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
1264 {
1265     napi_value thisVar = nullptr;
1266     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1267     NapiX509Crl *x509Crl = nullptr;
1268     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1269     if (x509Crl == nullptr) {
1270         LOGE("x509Crl is nullptr!");
1271         return nullptr;
1272     }
1273     return x509Crl->GetIssuerX500DistinguishedName(env, info);
1274 }
1275 
NapiGetSignature(napi_env env,napi_callback_info info)1276 static napi_value NapiGetSignature(napi_env env, napi_callback_info info)
1277 {
1278     napi_value thisVar = nullptr;
1279     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1280     NapiX509Crl *x509Crl = nullptr;
1281     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1282     if (x509Crl == nullptr) {
1283         LOGE("x509Crl is nullptr!");
1284         return nullptr;
1285     }
1286     return x509Crl->GetSignature(env, info);
1287 }
1288 
NapiGetSigAlgName(napi_env env,napi_callback_info info)1289 static napi_value NapiGetSigAlgName(napi_env env, napi_callback_info info)
1290 {
1291     napi_value thisVar = nullptr;
1292     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1293     NapiX509Crl *x509Crl = nullptr;
1294     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1295     if (x509Crl == nullptr) {
1296         LOGE("x509Crl is nullptr!");
1297         return nullptr;
1298     }
1299     return x509Crl->GetSigAlgName(env, info);
1300 }
1301 
NapiGetSigAlgOID(napi_env env,napi_callback_info info)1302 static napi_value NapiGetSigAlgOID(napi_env env, napi_callback_info info)
1303 {
1304     napi_value thisVar = nullptr;
1305     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1306     NapiX509Crl *x509Crl = nullptr;
1307     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1308     if (x509Crl == nullptr) {
1309         LOGE("x509Crl is nullptr!");
1310         return nullptr;
1311     }
1312     return x509Crl->GetSigAlgOID(env, info);
1313 }
1314 
NapiGetSigAlgParams(napi_env env,napi_callback_info info)1315 static napi_value NapiGetSigAlgParams(napi_env env, napi_callback_info info)
1316 {
1317     napi_value thisVar = nullptr;
1318     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1319     NapiX509Crl *x509Crl = nullptr;
1320     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1321     if (x509Crl == nullptr) {
1322         LOGE("x509Crl is nullptr!");
1323         return nullptr;
1324     }
1325     return x509Crl->GetSigAlgParams(env, info);
1326 }
1327 
NapiGetExtensions(napi_env env,napi_callback_info info)1328 static napi_value NapiGetExtensions(napi_env env, napi_callback_info info)
1329 {
1330     napi_value thisVar = nullptr;
1331     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1332     NapiX509Crl *x509Crl = nullptr;
1333     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1334     if (x509Crl == nullptr) {
1335         LOGE("x509Crl is nullptr!");
1336         return nullptr;
1337     }
1338     return x509Crl->GetExtensions(env, info);
1339 }
1340 
NapiMatch(napi_env env,napi_callback_info info)1341 static napi_value NapiMatch(napi_env env, napi_callback_info info)
1342 {
1343     napi_value thisVar = nullptr;
1344     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1345     NapiX509Crl *x509Crl = nullptr;
1346     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1347     if (x509Crl == nullptr) {
1348         LOGE("x509Crl is nullptr!");
1349         return nullptr;
1350     }
1351     return x509Crl->Match(env, info);
1352 }
1353 
CreateX509CrlExecute(napi_env env,void * data)1354 void NapiX509Crl::CreateX509CrlExecute(napi_env env, void *data)
1355 {
1356     CfCtx *context = static_cast<CfCtx *>(data);
1357     context->errCode = HcfX509CrlCreate(context->encodingBlob, &context->crl);
1358     if (context->errCode != CF_SUCCESS) {
1359         context->errMsg = "create X509Crl failed";
1360     }
1361 }
1362 
CreateX509CrlComplete(napi_env env,napi_status status,void * data)1363 void NapiX509Crl::CreateX509CrlComplete(napi_env env, napi_status status, void *data)
1364 {
1365     CfCtx *context = static_cast<CfCtx *>(data);
1366     if (context->errCode != CF_SUCCESS) {
1367         LOGE("call create X509Crl failed!");
1368         ReturnResult(env, context, nullptr);
1369         FreeCryptoFwkCtx(env, context);
1370         return;
1371     }
1372     napi_value instance = CreateX509Crl(env, context->createX509CrlName);
1373     NapiX509Crl *x509CrlClass = new (std::nothrow) NapiX509Crl(context->crl);
1374     if (x509CrlClass == nullptr) {
1375         context->errCode = CF_ERR_MALLOC;
1376         context->errMsg = "Failed to create a x509Crl class";
1377         LOGE("Failed to create a x509Crl class");
1378         CfObjDestroy(context->crl);
1379         ReturnResult(env, context, nullptr);
1380         FreeCryptoFwkCtx(env, context);
1381         return;
1382     }
1383     napi_wrap(
1384         env, instance, x509CrlClass,
1385         [](napi_env env, void *data, void *hint) {
1386             NapiX509Crl *crlClass = static_cast<NapiX509Crl *>(data);
1387             delete crlClass;
1388             return;
1389         },
1390         nullptr, nullptr);
1391     ReturnResult(env, context, instance);
1392     FreeCryptoFwkCtx(env, context);
1393 }
1394 
NapiCreateX509CrlBase(napi_env env,napi_callback_info info,std::string createName)1395 napi_value NapiX509Crl::NapiCreateX509CrlBase(napi_env env, napi_callback_info info, std::string createName)
1396 {
1397     size_t argc = ARGS_SIZE_TWO;
1398     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
1399     napi_value thisVar = nullptr;
1400     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1401     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
1402         return nullptr;
1403     }
1404 
1405     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
1406     if (context == nullptr) {
1407         LOGE("malloc context failed!");
1408         return nullptr;
1409     }
1410     if (!GetEncodingBlobFromValue(env, argv[PARAM0], &context->encodingBlob)) {
1411         LOGE("get encoding blob from data failed!");
1412         FreeCryptoFwkCtx(env, context);
1413         return nullptr;
1414     }
1415 
1416     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
1417         LOGE("create reference failed!");
1418         FreeCryptoFwkCtx(env, context);
1419         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
1420         return nullptr;
1421     }
1422 
1423     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
1424         FreeCryptoFwkCtx(env, context);
1425         return nullptr;
1426     }
1427 
1428     context->createX509CrlName = createName;
1429 
1430     napi_create_async_work(env, nullptr, CertGetResourceName(env, createName.c_str()), CreateX509CrlExecute,
1431         CreateX509CrlComplete, static_cast<void *>(context), &context->asyncWork);
1432 
1433     napi_queue_async_work(env, context->asyncWork);
1434     if (context->asyncType == ASYNC_TYPE_PROMISE) {
1435         return context->promise;
1436     } else {
1437         return CertNapiGetNull(env);
1438     }
1439 }
1440 
NapiCreateX509Crl(napi_env env,napi_callback_info info)1441 napi_value NapiX509Crl::NapiCreateX509Crl(napi_env env, napi_callback_info info)
1442 {
1443     return NapiCreateX509CrlBase(env, info, std::string("createX509Crl"));
1444 }
1445 
NapiCreateX509CRL(napi_env env,napi_callback_info info)1446 napi_value NapiX509Crl::NapiCreateX509CRL(napi_env env, napi_callback_info info)
1447 {
1448     return NapiCreateX509CrlBase(env, info, std::string("createX509CRL"));
1449 }
1450 
X509CrlConstructor(napi_env env,napi_callback_info info)1451 static napi_value X509CrlConstructor(napi_env env, napi_callback_info info)
1452 {
1453     napi_value thisVar = nullptr;
1454     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1455     return thisVar;
1456 }
1457 
DefineX509CrlJS(napi_env env,napi_value exports,std::string className)1458 void NapiX509Crl::DefineX509CrlJS(napi_env env, napi_value exports, std::string className)
1459 {
1460     napi_property_descriptor desc[] = {
1461         DECLARE_NAPI_FUNCTION(className.c_str(), NapiCreateX509Crl),
1462     };
1463     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1464 
1465     napi_property_descriptor x509CrlDesc[] = {
1466         DECLARE_NAPI_FUNCTION("isRevoked", NapiIsRevoked),
1467         DECLARE_NAPI_FUNCTION("getType", NapiGetType),
1468         DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
1469         DECLARE_NAPI_FUNCTION("verify", NapiVerify),
1470         DECLARE_NAPI_FUNCTION("getVersion", NapiGetVersion),
1471         DECLARE_NAPI_FUNCTION("getIssuerName", NapiGetIssuerDN),
1472         DECLARE_NAPI_FUNCTION("getLastUpdate", NapiGetThisUpdate),
1473         DECLARE_NAPI_FUNCTION("getNextUpdate", NapiGetNextUpdate),
1474         DECLARE_NAPI_FUNCTION("getSignature", NapiGetSignature),
1475         DECLARE_NAPI_FUNCTION("getSignatureAlgName", NapiGetSigAlgName),
1476         DECLARE_NAPI_FUNCTION("getSignatureAlgOid", NapiGetSigAlgOID),
1477         DECLARE_NAPI_FUNCTION("getSignatureAlgParams", NapiGetSigAlgParams),
1478         DECLARE_NAPI_FUNCTION("getRevokedCert", NapiCrlGetRevokedCertificate),
1479         DECLARE_NAPI_FUNCTION("getRevokedCerts", NapiCrlGetRevokedCertificates),
1480         DECLARE_NAPI_FUNCTION("getRevokedCertWithCert", NapiCrlGetRevokedCertificateWithCert),
1481         DECLARE_NAPI_FUNCTION("getTbsInfo", NapiCrlGetTBSCertList),
1482         DECLARE_NAPI_FUNCTION("toString", NapiToString),
1483         DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
1484         DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
1485         DECLARE_NAPI_FUNCTION("getIssuerX500DistinguishedName", NapiGetIssuerX500DistinguishedName),
1486     };
1487     napi_value constructor = nullptr;
1488     napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlConstructor, nullptr,
1489         sizeof(x509CrlDesc) / sizeof(x509CrlDesc[0]), x509CrlDesc, &constructor);
1490 
1491     napi_create_reference(env, constructor, 1, &classCrlRef_);
1492 }
1493 
DefineX509CRLJS(napi_env env,napi_value exports,std::string className)1494 void NapiX509Crl::DefineX509CRLJS(napi_env env, napi_value exports, std::string className)
1495 {
1496     napi_property_descriptor desc[] = {
1497         DECLARE_NAPI_FUNCTION(className.c_str(), NapiCreateX509CRL),
1498     };
1499     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1500 
1501     napi_property_descriptor x509CrlDesc[] = {
1502         DECLARE_NAPI_FUNCTION("isRevoked", NapiIsRevoked),
1503         DECLARE_NAPI_FUNCTION("getType", NapiGetType),
1504         DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
1505         DECLARE_NAPI_FUNCTION("verify", NapiVerify),
1506         DECLARE_NAPI_FUNCTION("getVersion", NapiGetVersion),
1507         DECLARE_NAPI_FUNCTION("getIssuerName", NapiGetIssuerDN),
1508         DECLARE_NAPI_FUNCTION("getLastUpdate", NapiGetThisUpdate),
1509         DECLARE_NAPI_FUNCTION("getNextUpdate", NapiGetNextUpdate),
1510         DECLARE_NAPI_FUNCTION("getSignature", NapiGetSignature),
1511         DECLARE_NAPI_FUNCTION("getSignatureAlgName", NapiGetSigAlgName),
1512         DECLARE_NAPI_FUNCTION("getSignatureAlgOid", NapiGetSigAlgOID),
1513         DECLARE_NAPI_FUNCTION("getSignatureAlgParams", NapiGetSigAlgParams),
1514         DECLARE_NAPI_FUNCTION("getExtensions", NapiGetExtensions),
1515         DECLARE_NAPI_FUNCTION("getRevokedCert", NapiCRLGetRevokedCertificate),
1516         DECLARE_NAPI_FUNCTION("getRevokedCerts", NapiCRLGetRevokedCertificates),
1517         DECLARE_NAPI_FUNCTION("getRevokedCertWithCert", NapiCRLGetRevokedCertificateWithCert),
1518         DECLARE_NAPI_FUNCTION("getTBSInfo", NapiCRLGetTBSCertList),
1519         DECLARE_NAPI_FUNCTION("match", NapiMatch),
1520         DECLARE_NAPI_FUNCTION("toString", NapiToString),
1521         DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
1522         DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
1523         DECLARE_NAPI_FUNCTION("getIssuerX500DistinguishedName", NapiGetIssuerX500DistinguishedName),
1524     };
1525     napi_value constructor = nullptr;
1526     napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlConstructor, nullptr,
1527         sizeof(x509CrlDesc) / sizeof(x509CrlDesc[0]), x509CrlDesc, &constructor);
1528 
1529     napi_create_reference(env, constructor, 1, &classCRLRef_);
1530 }
1531 
DefineX509CrlJSClass(napi_env env,napi_value exports,std::string className)1532 void NapiX509Crl::DefineX509CrlJSClass(napi_env env, napi_value exports, std::string className)
1533 {
1534     std::string createName;
1535     if (className == std::string("X509Crl")) {
1536         createName = "createX509Crl";
1537         DefineX509CrlJS(env, exports, createName);
1538     } else {
1539         createName = "createX509CRL";
1540         DefineX509CRLJS(env, exports, createName);
1541     }
1542 }
1543 
CreateX509Crl(napi_env env,std::string createName)1544 napi_value NapiX509Crl::CreateX509Crl(napi_env env, std::string createName)
1545 {
1546     napi_value constructor = nullptr;
1547     napi_value instance = nullptr;
1548     if (createName == std::string("createX509Crl")) {
1549         napi_get_reference_value(env, classCrlRef_, &constructor);
1550     } else {
1551         napi_get_reference_value(env, classCRLRef_, &constructor);
1552     }
1553     napi_new_instance(env, constructor, 0, nullptr, &instance);
1554     return instance;
1555 }
1556 } // namespace CertFramework
1557 } // namespace OHOS
1558