• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_status status = 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         if (status != napi_ok) {
274             napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
275             LOGE("failed to wrap obj!");
276             delete x509CrlEntryClass;
277             return nullptr;
278         }
279         napi_set_element(env, returnArray, i, instance);
280     }
281     return returnArray;
282 }
283 
GetRevokedCertificatesComplete(napi_env env,napi_status status,void * data)284 void GetRevokedCertificatesComplete(napi_env env, napi_status status, void *data)
285 {
286     CfCtx *context = static_cast<CfCtx *>(data);
287     if (context->errCode != CF_SUCCESS) {
288         ReturnResult(env, context, nullptr);
289         FreeCryptoFwkCtx(env, context);
290         return;
291     }
292     napi_value returnArray = GenerateCrlEntryArray(env, context->array, context->returnClassName);
293     ReturnResult(env, context, returnArray);
294     FreeCryptoFwkCtx(env, context);
295 }
296 
IsRevoked(napi_env env,napi_callback_info info)297 napi_value NapiX509Crl::IsRevoked(napi_env env, napi_callback_info info)
298 {
299     size_t argc = ARGS_SIZE_ONE;
300     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
301     napi_value thisVar = nullptr;
302     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
303     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
304         return nullptr;
305     }
306 
307     NapiX509Certificate *napiX509Cert = nullptr;
308     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
309     if (napiX509Cert == nullptr) {
310         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "napiX509Cert is null"));
311         LOGE("napiX509Cert is null!");
312         return nullptr;
313     }
314 
315     HcfX509Crl *x509Crl = GetX509Crl();
316     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
317     bool isRevoked = x509Crl->base.isRevoked(&(x509Crl->base), &(certificate->base));
318     napi_value result = nullptr;
319     napi_get_boolean(env, isRevoked, &result);
320     return result;
321 }
322 
GetType(napi_env env,napi_callback_info info)323 napi_value NapiX509Crl::GetType(napi_env env, napi_callback_info info)
324 {
325     HcfX509Crl *x509Crl = GetX509Crl();
326     const char *type = x509Crl->base.getType(&(x509Crl->base));
327     napi_value result = nullptr;
328     napi_create_string_utf8(env, type, strlen(type), &result);
329     return result;
330 }
331 
GetEncoded(napi_env env,napi_callback_info info)332 napi_value NapiX509Crl::GetEncoded(napi_env env, napi_callback_info info)
333 {
334     size_t argc = ARGS_SIZE_ONE;
335     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
336     napi_value thisVar = nullptr;
337     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
338     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
339         return nullptr;
340     }
341 
342     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
343     if (context == nullptr) {
344         LOGE("malloc context failed!");
345         return nullptr;
346     }
347     context->crlClass = this;
348 
349     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
350         LOGE("create reference failed!");
351         FreeCryptoFwkCtx(env, context);
352         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
353         return nullptr;
354     }
355 
356     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
357         FreeCryptoFwkCtx(env, context);
358         return nullptr;
359     }
360 
361     napi_create_async_work(env, nullptr, CertGetResourceName(env, "GetEncoded"), GetEncodedExecute, GetEncodedComplete,
362         static_cast<void *>(context), &context->asyncWork);
363 
364     napi_queue_async_work(env, context->asyncWork);
365     if (context->asyncType == ASYNC_TYPE_PROMISE) {
366         return context->promise;
367     } else {
368         return CertNapiGetNull(env);
369     }
370 }
371 
Verify(napi_env env,napi_callback_info info)372 __attribute__((no_sanitize("cfi"))) napi_value NapiX509Crl::Verify(napi_env env, napi_callback_info info)
373 {
374     size_t argc = ARGS_SIZE_TWO;
375     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
376     napi_value thisVar = nullptr;
377     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
378     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
379         return nullptr;
380     }
381 
382     NapiPubKey *pubKey = nullptr;
383     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&pubKey));
384     if (pubKey == nullptr) {
385         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "public key is null"));
386         LOGE("pubKey is null!");
387         return nullptr;
388     }
389 
390     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
391     if (context == nullptr) {
392         LOGE("malloc context failed!");
393         return nullptr;
394     }
395     context->pubKey = pubKey->GetPubKey();
396     context->crlClass = this;
397 
398     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
399         LOGE("create reference failed!");
400         FreeCryptoFwkCtx(env, context);
401         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
402         return nullptr;
403     }
404 
405     if (napi_create_reference(env, argv[PARAM0], 1, &context->pubKeyParamsRef) != napi_ok) {
406         LOGE("create param ref failed!");
407         FreeCryptoFwkCtx(env, context);
408         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create param ref failed"));
409         return nullptr;
410     }
411 
412     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
413         FreeCryptoFwkCtx(env, context);
414         return nullptr;
415     }
416 
417     napi_create_async_work(env, nullptr, CertGetResourceName(env, "Verify"), VerifyExecute, VerifyComplete,
418         static_cast<void *>(context), &context->asyncWork);
419 
420     napi_queue_async_work(env, context->asyncWork);
421     if (context->asyncType == ASYNC_TYPE_PROMISE) {
422         return context->promise;
423     } else {
424         return CertNapiGetNull(env);
425     }
426 }
427 
GetVersion(napi_env env,napi_callback_info info)428 napi_value NapiX509Crl::GetVersion(napi_env env, napi_callback_info info)
429 {
430     HcfX509Crl *x509Crl = GetX509Crl();
431     int version = x509Crl->getVersion(x509Crl);
432     napi_value result = nullptr;
433     napi_create_int32(env, version, &result);
434     return result;
435 }
436 
GetIssuerDN(napi_env env,napi_callback_info info)437 napi_value NapiX509Crl::GetIssuerDN(napi_env env, napi_callback_info info)
438 {
439     HcfX509Crl *x509Crl = GetX509Crl();
440     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
441     if (blob == nullptr) {
442         LOGE("malloc blob failed!");
443         return nullptr;
444     }
445     CfResult ret = x509Crl->getIssuerName(x509Crl, blob);
446     if (ret != CF_SUCCESS) {
447         napi_throw(env, CertGenerateBusinessError(env, ret, "get issuer name failed"));
448         LOGE("getIssuerDN failed!");
449         CfFree(blob);
450         blob = nullptr;
451         return nullptr;
452     }
453     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
454     CfBlobDataFree(blob);
455     CfFree(blob);
456     blob = nullptr;
457     return returnBlob;
458 }
459 
GetIssuerDNEx(napi_env env,napi_callback_info info,CfEncodinigType encodingType)460 napi_value NapiX509Crl::GetIssuerDNEx(napi_env env, napi_callback_info info, CfEncodinigType encodingType)
461 {
462     HcfX509Crl *x509Crl = GetX509Crl();
463     CfBlob blob = { 0, nullptr };
464     CfResult ret = x509Crl->getIssuerNameEx(x509Crl, encodingType, &blob);
465     if (ret != CF_SUCCESS) {
466         napi_throw(env, CertGenerateBusinessError(env, ret, "getIssuerNameEx failed!"));
467         LOGE("GetIssuerDNEx failed!");
468         return nullptr;
469     }
470     napi_value returnValue = nullptr;
471     napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnValue);
472     CfBlobDataFree(&blob);
473     return returnValue;
474 }
475 
GetThisUpdate(napi_env env,napi_callback_info info)476 napi_value NapiX509Crl::GetThisUpdate(napi_env env, napi_callback_info info)
477 {
478     HcfX509Crl *x509Crl = GetX509Crl();
479     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
480     if (blob == nullptr) {
481         LOGE("malloc blob failed!");
482         return nullptr;
483     }
484     CfResult ret = x509Crl->getLastUpdate(x509Crl, blob);
485     if (ret != CF_SUCCESS) {
486         napi_throw(env, CertGenerateBusinessError(env, ret, "get last update failed"));
487         LOGE("getLastUpdate failed!");
488         CfFree(blob);
489         blob = nullptr;
490         return nullptr;
491     }
492     napi_value result = nullptr;
493     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
494     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
495     CfBlobDataFree(blob);
496     CfFree(blob);
497     blob = nullptr;
498     return result;
499 }
500 
GetNextUpdate(napi_env env,napi_callback_info info)501 napi_value NapiX509Crl::GetNextUpdate(napi_env env, napi_callback_info info)
502 {
503     HcfX509Crl *x509Crl = GetX509Crl();
504     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
505     if (blob == nullptr) {
506         LOGE("malloc blob failed!");
507         return nullptr;
508     }
509     CfResult ret = x509Crl->getNextUpdate(x509Crl, blob);
510     if (ret != CF_SUCCESS) {
511         napi_throw(env, CertGenerateBusinessError(env, ret, "get next update failed"));
512         LOGE("getNextUpdate failed!");
513         CfFree(blob);
514         blob = nullptr;
515         return nullptr;
516     }
517     napi_value result = nullptr;
518     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
519     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
520     CfBlobDataFree(blob);
521     CfFree(blob);
522     blob = nullptr;
523     return result;
524 }
525 
GetCrlSerialNumberFromNapiValue(napi_env env,napi_value arg,CfBlob & outBlob)526 static bool GetCrlSerialNumberFromNapiValue(napi_env env, napi_value arg, CfBlob &outBlob)
527 {
528     napi_valuetype valueType;
529     napi_typeof(env, arg, &valueType);
530     if (valueType != napi_number) {
531         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "param type error"));
532         LOGE("wrong argument type. expect int type. [Type]: %{public}d", valueType);
533         return false;
534     }
535 
536     uint8_t serialBuf[MAX_SN_BYTE_CNT] = { 0 };
537     uint32_t serialLen = sizeof(int64_t);
538     int64_t tmpData = 0;
539     if (napi_get_value_int64(env, arg, &tmpData) != napi_ok || tmpData < 0) {
540         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "get serialNum failed"));
541         LOGE("can not get int64 value");
542         return false;
543     }
544 
545     if (memcpy_s(serialBuf, sizeof(serialBuf), &tmpData, sizeof(int64_t)) != EOK) {
546         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_COPY, "copy serialNum failed"));
547         LOGE("copy serialNum failed");
548         return false;
549     }
550 
551     outBlob.size = serialLen;
552     outBlob.data = static_cast<uint8_t *>(CfMalloc(serialLen, 0));
553     if (outBlob.data == nullptr) {
554         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "malloc serialNum failed"));
555         LOGE("malloc blob data failed!");
556         return false;
557     }
558     // reverse data: because BN_bin2bn() converts the positive integer in big-endian form of length len into a BIGNUM
559     for (uint32_t i = 0; i < serialLen; ++i) {
560         outBlob.data[i] = serialBuf[outBlob.size - 1 - i];
561     }
562 
563     return true;
564 }
565 
WrapX509CrlEntryInstance(napi_env env,napi_value instance,HcfX509CrlEntry * crlEntry)566 static bool WrapX509CrlEntryInstance(napi_env env, napi_value instance, HcfX509CrlEntry *crlEntry)
567 {
568     NapiX509CrlEntry *x509CrlEntryClass = new (std::nothrow) NapiX509CrlEntry(crlEntry);
569     if (!x509CrlEntryClass) {
570         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a x509CrlEntry class"));
571         LOGE("Failed to create a x509CrlEntry class");
572         CfObjDestroy(crlEntry);
573         return false;
574     }
575     napi_status status = napi_wrap(
576         env, instance, x509CrlEntryClass,
577         [](napi_env env, void *data, void *hint) {
578             delete static_cast<NapiX509CrlEntry *>(data);
579         },
580         nullptr, nullptr);
581     if (status != napi_ok) {
582         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
583         LOGE("failed to wrap obj!");
584         delete x509CrlEntryClass;
585         return false;
586     }
587     return true;
588 }
589 
GetRevokedCertificate(napi_env env,napi_callback_info info,std::string returnClassName)590 napi_value NapiX509Crl::GetRevokedCertificate(napi_env env, napi_callback_info info, std::string returnClassName)
591 {
592     size_t argc = ARGS_SIZE_ONE;
593     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
594     napi_value thisVar = nullptr;
595     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
596     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
597         return nullptr;
598     }
599 
600     CfBlob serialNumber = { 0, nullptr };
601     bool getSnRet = false;
602     if (returnClassName == std::string("X509CrlEntry")) {
603         getSnRet = GetCrlSerialNumberFromNapiValue(env, argv[PARAM0], serialNumber);
604     } else {
605         getSnRet = CertGetSerialNumberFromBigIntJSParams(env, argv[PARAM0], serialNumber);
606     }
607     if (!getSnRet) {
608         LOGE("get serialNumber failed");
609         return nullptr;
610     }
611 
612     HcfX509Crl *x509Crl = GetX509Crl();
613     HcfX509CrlEntry *crlEntry = nullptr;
614     CfResult ret = x509Crl->getRevokedCert(x509Crl, &serialNumber, &crlEntry);
615     CF_FREE_PTR(serialNumber.data);
616     if (ret != CF_SUCCESS) {
617         napi_throw(env, CertGenerateBusinessError(env, ret, "get revoked cert failed!"));
618         LOGE("get revoked cert failed!");
619         return nullptr;
620     }
621 
622     napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env, returnClassName);
623     if (!WrapX509CrlEntryInstance(env, instance, crlEntry)) {
624         return nullptr;
625     }
626     return instance;
627 }
628 
GetRevokedCertificateWithCert(napi_env env,napi_callback_info info,std::string returnClassName)629 napi_value NapiX509Crl::GetRevokedCertificateWithCert(
630     napi_env env, napi_callback_info info, std::string returnClassName)
631 {
632     size_t argc = ARGS_SIZE_ONE;
633     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
634     napi_value thisVar = nullptr;
635     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
636     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
637         return nullptr;
638     }
639 
640     NapiX509Certificate *napiX509Cert = nullptr;
641     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
642     if (napiX509Cert == nullptr) {
643         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "napiX509Cert is null"));
644         LOGE("napiX509Cert is null!");
645         return nullptr;
646     }
647 
648     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
649     HcfX509Crl *x509Crl = GetX509Crl();
650     HcfX509CrlEntry *crlEntry = nullptr;
651     CfResult ret = x509Crl->getRevokedCertWithCert(x509Crl, certificate, &crlEntry);
652     if (ret != CF_SUCCESS) {
653         napi_throw(env, CertGenerateBusinessError(env, ret, "get revoked cert with cert failed!"));
654         LOGE("get revoked cert with cert failed!");
655         return nullptr;
656     }
657 
658     napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env, returnClassName);
659     NapiX509CrlEntry *x509CrlEntryClass = new (std::nothrow) NapiX509CrlEntry(crlEntry);
660     if (x509CrlEntryClass == nullptr) {
661         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a x509CrlEntry class"));
662         LOGE("Failed to create a x509CrlEntry class");
663         CfObjDestroy(crlEntry);
664         return nullptr;
665     }
666     napi_status status = napi_wrap(
667         env, instance, x509CrlEntryClass,
668         [](napi_env env, void *data, void *hint) {
669             NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
670             delete x509CrlEntryClass;
671             return;
672         },
673         nullptr, nullptr);
674     if (status != napi_ok) {
675         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
676         LOGE("failed to wrap obj!");
677         delete x509CrlEntryClass;
678         return nullptr;
679     }
680     return instance;
681 }
682 
GetRevokedCertificates(napi_env env,napi_callback_info info,std::string returnClassName)683 napi_value NapiX509Crl::GetRevokedCertificates(napi_env env, napi_callback_info info, std::string returnClassName)
684 {
685     size_t argc = ARGS_SIZE_ONE;
686     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
687     napi_value thisVar = nullptr;
688     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
689     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
690         return nullptr;
691     }
692 
693     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
694     if (context == nullptr) {
695         LOGE("malloc context failed!");
696         return nullptr;
697     }
698     context->crlClass = this;
699 
700     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
701         LOGE("create reference failed!");
702         FreeCryptoFwkCtx(env, context);
703         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
704         return nullptr;
705     }
706 
707     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
708         FreeCryptoFwkCtx(env, context);
709         return nullptr;
710     }
711 
712     context->returnClassName = returnClassName;
713 
714     napi_create_async_work(env, nullptr, CertGetResourceName(env, "GetRevokedCertificates"),
715         GetRevokedCertificatesExecute, GetRevokedCertificatesComplete, static_cast<void *>(context),
716         &context->asyncWork);
717 
718     napi_queue_async_work(env, context->asyncWork);
719     if (context->asyncType == ASYNC_TYPE_PROMISE) {
720         return context->promise;
721     } else {
722         return CertNapiGetNull(env);
723     }
724 }
725 
GetTBSCertList(napi_env env,napi_callback_info info)726 napi_value NapiX509Crl::GetTBSCertList(napi_env env, napi_callback_info info)
727 {
728     HcfX509Crl *x509Crl = GetX509Crl();
729     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
730     if (blob == nullptr) {
731         LOGE("malloc blob failed!");
732         return nullptr;
733     }
734     CfResult result = x509Crl->getTbsInfo(x509Crl, blob);
735     if (result != CF_SUCCESS) {
736         napi_throw(env, CertGenerateBusinessError(env, result, "get tbs info failed"));
737         LOGE("get tbs info failed!");
738         CfFree(blob);
739         blob = nullptr;
740         return nullptr;
741     }
742     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
743     CfBlobDataFree(blob);
744     CfFree(blob);
745     blob = nullptr;
746     return returnBlob;
747 }
748 
ToString(napi_env env,napi_callback_info info)749 napi_value NapiX509Crl::ToString(napi_env env, napi_callback_info info)
750 {
751     HcfX509Crl *x509Crl = GetX509Crl();
752     CfBlob blob = { 0, nullptr };
753     CfResult result = x509Crl->toString(x509Crl, &blob);
754     if (result != CF_SUCCESS) {
755         LOGE("toString failed!");
756         napi_throw(env, CertGenerateBusinessError(env, result, "toString failed"));
757         return nullptr;
758     }
759     napi_value returnBlob = nullptr;
760     napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnBlob);
761     CfBlobDataFree(&blob);
762     return returnBlob;
763 }
764 
ToStringEx(napi_env env,napi_callback_info info,CfEncodinigType encodingType)765 napi_value NapiX509Crl::ToStringEx(napi_env env, napi_callback_info info, CfEncodinigType encodingType)
766 {
767     HcfX509Crl *x509Crl = GetX509Crl();
768     CfBlob blob = { 0, nullptr };
769     CfResult result = x509Crl->toStringEx(x509Crl, encodingType, &blob);
770     if (result != CF_SUCCESS) {
771         napi_throw(env, CertGenerateBusinessError(env, result, "ToStringEx failed"));
772         LOGE("ToStringEx failed!");
773         return nullptr;
774     }
775     napi_value returnBlob = nullptr;
776     napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnBlob);
777     CfBlobDataFree(&blob);
778     return returnBlob;
779 }
780 
HashCode(napi_env env,napi_callback_info info)781 napi_value NapiX509Crl::HashCode(napi_env env, napi_callback_info info)
782 {
783     HcfX509Crl *x509Crl = GetX509Crl();
784     CfBlob blob = { 0, nullptr };
785     CfResult result = x509Crl->hashCode(x509Crl, &blob);
786     if (result != CF_SUCCESS) {
787         LOGE("hashCode failed!");
788         napi_throw(env, CertGenerateBusinessError(env, result, "hashCode failed"));
789         return nullptr;
790     }
791     napi_value returnBlob = ConvertBlobToUint8ArrNapiValue(env, &blob);
792     CfBlobDataFree(&blob);
793     return returnBlob;
794 }
795 
CreateCertExtsJSInstance(napi_env env)796 static napi_value CreateCertExtsJSInstance(napi_env env)
797 {
798     napi_value constructor = nullptr;
799     napi_value instance = nullptr;
800     napi_get_reference_value(env, NapiCertExtension::classRef_, &constructor);
801     napi_new_instance(env, constructor, 0, nullptr, &instance);
802     return instance;
803 }
804 
BuildCertExtsObject(napi_env env,CfEncodingBlob * encodingBlob)805 static napi_value BuildCertExtsObject(napi_env env, CfEncodingBlob *encodingBlob)
806 {
807     CfObject *extsObj = nullptr;
808     int32_t res = CfCreate(CF_OBJ_TYPE_EXTENSION, encodingBlob, &extsObj);
809     if (res != CF_SUCCESS) {
810         LOGE("CfCreate error!");
811         return nullptr;
812     }
813     napi_value jsObject = CreateCertExtsJSInstance(env);
814     NapiCertExtension *napiObject = new (std::nothrow) NapiCertExtension(extsObj);
815     if (napiObject == nullptr) {
816         LOGE("Failed to create napi extension class");
817         extsObj->destroy(&(extsObj));
818         extsObj = nullptr;
819         return nullptr;
820     }
821     napi_status status = napi_wrap(
822         env, jsObject, napiObject,
823         [](napi_env env, void *data, void *hint) {
824             NapiCertExtension *certExts = static_cast<NapiCertExtension *>(data);
825             delete certExts;
826             return;
827         }, nullptr, nullptr);
828     if (status != napi_ok) {
829         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
830         LOGE("failed to wrap obj!");
831         delete napiObject;
832         return nullptr;
833     }
834     return jsObject;
835 }
836 
GetExtensionsObject(napi_env env,napi_callback_info info)837 napi_value NapiX509Crl::GetExtensionsObject(napi_env env, napi_callback_info info)
838 {
839     HcfX509Crl *x509Crl = GetX509Crl();
840     CfBlob blob = { 0, nullptr };
841     CfResult result = x509Crl->getExtensionsObject(x509Crl, &blob);
842     if (result != CF_SUCCESS) {
843         LOGE("get Extensions Object failed!");
844         napi_throw(env, CertGenerateBusinessError(env, result, "get Extensions Object failed"));
845         return nullptr;
846     }
847 
848     CfEncodingBlob *encodingBlob = static_cast<CfEncodingBlob *>(CfMalloc(sizeof(CfEncodingBlob), 0));
849     if (encodingBlob == nullptr) {
850         LOGE("malloc encoding blob failed!");
851         CfBlobDataFree(&blob);
852         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "CfMalloc failed"));
853         return nullptr;
854     }
855     if (!ConvertBlobToEncodingBlob(blob, encodingBlob)) {
856         LOGE("ConvertBlobToEncodingBlob failed!");
857         CfBlobDataFree(&blob);
858         CfFree(encodingBlob);
859         encodingBlob = nullptr;
860         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_CRYPTO_OPERATION, "ConvertBlobToEncodingBlob failed"));
861         return nullptr;
862     }
863     CfBlobDataFree(&blob);
864 
865     napi_value object = BuildCertExtsObject(env, encodingBlob);
866     CfEncodingBlobDataFree(encodingBlob);
867     CfFree(encodingBlob);
868     encodingBlob = nullptr;
869     if (object == nullptr) {
870         LOGE("BuildCertExtsObject failed!");
871         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "BuildCertExtsObject failed"));
872         return nullptr;
873     }
874 
875     return object;
876 }
877 
GetIssuerX500DistinguishedName(napi_env env,napi_callback_info info)878 napi_value NapiX509Crl::GetIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
879 {
880     HcfX509Crl *x509Crl = GetX509Crl();
881     CfBlob blob = { 0, nullptr };
882     CfResult ret = x509Crl->getIssuerName(x509Crl, &blob);
883     if (ret != CF_SUCCESS) {
884         LOGE("getIssuerName failed!");
885         napi_throw(env, CertGenerateBusinessError(env, ret, "get issuer name failed"));
886         return nullptr;
887     }
888     HcfX509DistinguishedName *x509Name = nullptr;
889     ret = HcfX509DistinguishedNameCreate(&blob, true, &x509Name);
890     CfBlobDataFree(&blob);
891     if (ret != CF_SUCCESS) {
892         LOGE("HcfX509DistinguishedNameCreate failed");
893         napi_throw(env, CertGenerateBusinessError(env, ret, "HcfX509DistinguishedNameCreate failed"));
894         return nullptr;
895     }
896 
897     ret = x509Crl->getIssuerNameDer(x509Crl, &blob);
898     if (ret != CF_SUCCESS) {
899         LOGE("getIssuerNameDer failed!");
900         CfObjDestroy(x509Name);
901         napi_throw(env, CertGenerateBusinessError(env, ret, "get issuer name der failed"));
902         return nullptr;
903     }
904     HcfX509DistinguishedName *x509NameUtf8 = nullptr;
905     ret = HcfX509DistinguishedNameCreate(&blob, false, &x509NameUtf8);
906     CfBlobDataFree(&blob);
907     if (ret != CF_SUCCESS) {
908         LOGE("HcfX509DistinguishedNameCreate failed");
909         CfObjDestroy(x509Name);
910         napi_throw(env, CertGenerateBusinessError(env, ret, "HcfX509DistinguishedNameCreate failed"));
911         return nullptr;
912     }
913 
914     napi_value instance = ConstructX509DistinguishedName(x509Name, x509NameUtf8, env);
915     return instance;
916 }
917 
GetSignature(napi_env env,napi_callback_info info)918 napi_value NapiX509Crl::GetSignature(napi_env env, napi_callback_info info)
919 {
920     HcfX509Crl *x509Crl = GetX509Crl();
921     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
922     if (blob == nullptr) {
923         LOGE("malloc blob failed!");
924         return nullptr;
925     }
926     CfResult result = x509Crl->getSignature(x509Crl, blob);
927     if (result != CF_SUCCESS) {
928         napi_throw(env, CertGenerateBusinessError(env, result, "get signature failed"));
929         LOGE("getSignature failed!");
930         CfFree(blob);
931         blob = nullptr;
932         return nullptr;
933     }
934     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
935     CfBlobDataFree(blob);
936     CfFree(blob);
937     blob = nullptr;
938     return returnBlob;
939 }
940 
GetSigAlgName(napi_env env,napi_callback_info info)941 napi_value NapiX509Crl::GetSigAlgName(napi_env env, napi_callback_info info)
942 {
943     HcfX509Crl *x509Crl = GetX509Crl();
944     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
945     if (blob == nullptr) {
946         LOGE("malloc blob failed!");
947         return nullptr;
948     }
949     CfResult ret = x509Crl->getSignatureAlgName(x509Crl, blob);
950     if (ret != CF_SUCCESS) {
951         napi_throw(env, CertGenerateBusinessError(env, ret, "get signature alg name failed"));
952         LOGE("getSigAlgName failed!");
953         CfFree(blob);
954         blob = nullptr;
955         return nullptr;
956     }
957     napi_value result = nullptr;
958     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
959     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
960     CfBlobDataFree(blob);
961     CfFree(blob);
962     blob = nullptr;
963     return result;
964 }
965 
GetSigAlgOID(napi_env env,napi_callback_info info)966 napi_value NapiX509Crl::GetSigAlgOID(napi_env env, napi_callback_info info)
967 {
968     HcfX509Crl *x509Crl = GetX509Crl();
969     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
970     if (blob == nullptr) {
971         LOGE("malloc blob failed!");
972         return nullptr;
973     }
974     CfResult ret = x509Crl->getSignatureAlgOid(x509Crl, blob);
975     if (ret != CF_SUCCESS) {
976         napi_throw(env, CertGenerateBusinessError(env, ret, "get signature alg oid failed"));
977         LOGE("getSigAlgOID failed!");
978         CfFree(blob);
979         blob = nullptr;
980         return nullptr;
981     }
982     napi_value result = nullptr;
983     uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
984     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &result);
985     CfBlobDataFree(blob);
986     CfFree(blob);
987     blob = nullptr;
988     return result;
989 }
990 
GetSigAlgParams(napi_env env,napi_callback_info info)991 napi_value NapiX509Crl::GetSigAlgParams(napi_env env, napi_callback_info info)
992 {
993     HcfX509Crl *x509Crl = GetX509Crl();
994     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
995     if (blob == nullptr) {
996         LOGE("malloc blob failed!");
997         return nullptr;
998     }
999     CfResult result = x509Crl->getSignatureAlgParams(x509Crl, blob);
1000     if (result != CF_SUCCESS) {
1001         napi_throw(env, CertGenerateBusinessError(env, result, "get signature alg params failed"));
1002         LOGE("getSigAlgParams failed!");
1003         CfFree(blob);
1004         blob = nullptr;
1005         return nullptr;
1006     }
1007     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
1008     CfBlobDataFree(blob);
1009     CfFree(blob);
1010     blob = nullptr;
1011     return returnBlob;
1012 }
1013 
GetExtensions(napi_env env,napi_callback_info info)1014 napi_value NapiX509Crl::GetExtensions(napi_env env, napi_callback_info info)
1015 {
1016     HcfX509Crl *x509Crl = GetX509Crl();
1017     CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
1018     if (blob == nullptr) {
1019         LOGE("malloc blob failed!");
1020         return nullptr;
1021     }
1022     CfResult result = x509Crl->getExtensions(x509Crl, blob);
1023     if (result != CF_SUCCESS) {
1024         napi_throw(env, CertGenerateBusinessError(env, result, "get extensions failed"));
1025         LOGE("getExtensions failed!");
1026         CfFree(blob);
1027         blob = nullptr;
1028         return nullptr;
1029     }
1030     napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
1031     CfBlobDataFree(blob);
1032     CfFree(blob);
1033     blob = nullptr;
1034     return returnBlob;
1035 }
1036 
Match(napi_env env,napi_callback_info info)1037 napi_value NapiX509Crl::Match(napi_env env, napi_callback_info info)
1038 {
1039     size_t argc = ARGS_SIZE_ONE;
1040     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
1041     napi_value thisVar = nullptr;
1042     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1043     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
1044         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "CertCheckArgsCount failed"));
1045         LOGE("CertCheckArgsCount failed!");
1046         return nullptr;
1047     }
1048 
1049     HcfX509CrlMatchParams *param = static_cast<HcfX509CrlMatchParams *>(CfMalloc(sizeof(HcfX509CrlMatchParams), 0));
1050     if (param == nullptr) {
1051         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "malloc matchParams failed"));
1052         LOGE("malloc matchParams failed!");
1053         return nullptr;
1054     }
1055     if (!BuildX509CrlMatchParams(env, argv[PARAM0], param)) {
1056         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "BuildX509CrlMatchParams failed"));
1057         LOGE("BuildX509CrlMatchParams failed!");
1058         FreeX509CrlMatchParams(param);
1059         return nullptr;
1060     }
1061 
1062     bool boolFlag = false;
1063     CfResult result = MatchProc(param, boolFlag);
1064     if (result != CF_SUCCESS) {
1065         napi_throw(env, CertGenerateBusinessError(env, result, "match failed"));
1066         LOGE("call match failed!");
1067         FreeX509CrlMatchParams(param);
1068         return nullptr;
1069     }
1070     FreeX509CrlMatchParams(param);
1071     napi_value ret = nullptr;
1072     napi_get_boolean(env, boolFlag, &ret);
1073     return ret;
1074 }
1075 
MatchProc(HcfX509CrlMatchParams * param,bool & boolFlag)1076 CfResult NapiX509Crl::MatchProc(HcfX509CrlMatchParams *param, bool &boolFlag)
1077 {
1078     HcfX509Crl *x509Crl = GetX509Crl();
1079     return x509Crl->match(x509Crl, param, &boolFlag);
1080 }
1081 
NapiIsRevoked(napi_env env,napi_callback_info info)1082 static napi_value NapiIsRevoked(napi_env env, napi_callback_info info)
1083 {
1084     napi_value thisVar = nullptr;
1085     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1086     NapiX509Crl *x509Crl = nullptr;
1087     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1088     if (x509Crl == nullptr) {
1089         LOGE("x509Crl is nullptr!");
1090         return nullptr;
1091     }
1092     return x509Crl->IsRevoked(env, info);
1093 }
1094 
NapiGetType(napi_env env,napi_callback_info info)1095 static napi_value NapiGetType(napi_env env, napi_callback_info info)
1096 {
1097     napi_value thisVar = nullptr;
1098     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1099     NapiX509Crl *x509Crl = nullptr;
1100     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1101     if (x509Crl == nullptr) {
1102         LOGE("x509Crl is nullptr!");
1103         return nullptr;
1104     }
1105     return x509Crl->GetType(env, info);
1106 }
1107 
NapiGetEncoded(napi_env env,napi_callback_info info)1108 static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
1109 {
1110     napi_value thisVar = nullptr;
1111     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1112     NapiX509Crl *x509Crl = nullptr;
1113     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1114     if (x509Crl == nullptr) {
1115         LOGE("x509Crl is nullptr!");
1116         return nullptr;
1117     }
1118     return x509Crl->GetEncoded(env, info);
1119 }
1120 
NapiVerify(napi_env env,napi_callback_info info)1121 static napi_value NapiVerify(napi_env env, napi_callback_info info)
1122 {
1123     napi_value thisVar = nullptr;
1124     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1125     NapiX509Crl *x509Crl = nullptr;
1126     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1127     if (x509Crl == nullptr) {
1128         LOGE("x509Crl is nullptr!");
1129         return nullptr;
1130     }
1131     return x509Crl->Verify(env, info);
1132 }
1133 
NapiGetVersion(napi_env env,napi_callback_info info)1134 static napi_value NapiGetVersion(napi_env env, napi_callback_info info)
1135 {
1136     napi_value thisVar = nullptr;
1137     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1138     NapiX509Crl *x509Crl = nullptr;
1139     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1140     if (x509Crl == nullptr) {
1141         LOGE("x509Crl is nullptr!");
1142         return nullptr;
1143     }
1144     return x509Crl->GetVersion(env, info);
1145 }
1146 
NapiGetIssuerDN(napi_env env,napi_callback_info info)1147 static napi_value NapiGetIssuerDN(napi_env env, napi_callback_info info)
1148 {
1149     size_t argc = ARGS_SIZE_ONE;
1150     napi_value thisVar = nullptr;
1151     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
1152     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1153     if (argc != 0 && argc != ARGS_SIZE_ONE) {
1154         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument num!"));
1155         LOGE("wrong argument num!");
1156         return nullptr;
1157     }
1158     NapiX509Crl *x509Crl = nullptr;
1159     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1160     if (x509Crl == nullptr) {
1161         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "x509Crl is nullptr!"));
1162         LOGE("x509Crl is nullptr!");
1163         return nullptr;
1164     }
1165 
1166     if (argc == ARGS_SIZE_ONE) {
1167         napi_valuetype valueType;
1168         napi_typeof(env, argv[PARAM0], &valueType);
1169         if ((valueType != napi_number)) {
1170             napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument type!"));
1171             LOGE("wrong argument type!");
1172             return nullptr;
1173         }
1174         CfEncodinigType encodingType;
1175         if (napi_get_value_uint32(env, argv[PARAM0], reinterpret_cast<uint32_t *>(&encodingType)) != napi_ok) {
1176             napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "napi_get_value_uint32 failed!"));
1177             LOGE("napi_get_value_uint32 failed!");
1178             return nullptr;
1179         }
1180         return x509Crl->GetIssuerDNEx(env, info, encodingType);
1181     }
1182     return x509Crl->GetIssuerDN(env, info);
1183 }
1184 
NapiGetThisUpdate(napi_env env,napi_callback_info info)1185 static napi_value NapiGetThisUpdate(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->GetThisUpdate(env, info);
1196 }
1197 
NapiGetNextUpdate(napi_env env,napi_callback_info info)1198 static napi_value NapiGetNextUpdate(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->GetNextUpdate(env, info);
1209 }
1210 
NapiCrlGetRevokedCertificate(napi_env env,napi_callback_info info)1211 static napi_value NapiCrlGetRevokedCertificate(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->GetRevokedCertificate(env, info, std::string("X509CrlEntry"));
1222 }
1223 
NapiCrlGetRevokedCertificateWithCert(napi_env env,napi_callback_info info)1224 static napi_value NapiCrlGetRevokedCertificateWithCert(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->GetRevokedCertificateWithCert(env, info, std::string("X509CrlEntry"));
1235 }
1236 
NapiCrlGetRevokedCertificates(napi_env env,napi_callback_info info)1237 static napi_value NapiCrlGetRevokedCertificates(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->GetRevokedCertificates(env, info, std::string("X509CrlEntry"));
1248 }
1249 
NapiCRLGetRevokedCertificate(napi_env env,napi_callback_info info)1250 static napi_value NapiCRLGetRevokedCertificate(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->GetRevokedCertificate(env, info, std::string("X509CRLEntry"));
1261 }
1262 
NapiCRLGetRevokedCertificateWithCert(napi_env env,napi_callback_info info)1263 static napi_value NapiCRLGetRevokedCertificateWithCert(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->GetRevokedCertificateWithCert(env, info, std::string("X509CRLEntry"));
1274 }
1275 
NapiCRLGetRevokedCertificates(napi_env env,napi_callback_info info)1276 static napi_value NapiCRLGetRevokedCertificates(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->GetRevokedCertificates(env, info, std::string("X509CRLEntry"));
1287 }
1288 
NapiCrlGetTBSCertList(napi_env env,napi_callback_info info)1289 static napi_value NapiCrlGetTBSCertList(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->GetTBSCertList(env, info);
1300 }
1301 
NapiCRLGetTBSCertList(napi_env env,napi_callback_info info)1302 static napi_value NapiCRLGetTBSCertList(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->GetTBSCertList(env, info);
1313 }
1314 
NapiToString(napi_env env,napi_callback_info info)1315 static napi_value NapiToString(napi_env env, napi_callback_info info)
1316 {
1317     size_t argc = ARGS_SIZE_ONE;
1318     napi_value thisVar = nullptr;
1319     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
1320     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1321     if (argc != 0 && argc != ARGS_SIZE_ONE) {
1322         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument num!"));
1323         LOGE("wrong argument num!");
1324         return nullptr;
1325     }
1326     NapiX509Crl *x509Crl = nullptr;
1327     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1328     if (x509Crl == nullptr) {
1329         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "x509Crl is nullptr!"));
1330         LOGE("x509Crl is nullptr!");
1331         return nullptr;
1332     }
1333     if (argc == ARGS_SIZE_ONE) {
1334         napi_valuetype valueType;
1335         napi_typeof(env, argv[PARAM0], &valueType);
1336         if ((valueType != napi_number)) {
1337             napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument type!"));
1338             LOGE("wrong argument type!");
1339             return nullptr;
1340         }
1341         CfEncodinigType encodingType;
1342         if (napi_get_value_uint32(env, argv[PARAM0], reinterpret_cast<uint32_t *>(&encodingType)) != napi_ok) {
1343             napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "napi_get_value_uint32 failed!"));
1344             LOGE("napi_get_value_uint32 failed!");
1345             return nullptr;
1346         }
1347         return x509Crl->ToStringEx(env, info, encodingType);
1348     }
1349     return x509Crl->ToString(env, info);
1350 }
1351 
NapiHashCode(napi_env env,napi_callback_info info)1352 static napi_value NapiHashCode(napi_env env, napi_callback_info info)
1353 {
1354     napi_value thisVar = nullptr;
1355     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1356     NapiX509Crl *x509Crl = nullptr;
1357     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1358     if (x509Crl == nullptr) {
1359         LOGE("x509Crl is nullptr!");
1360         return nullptr;
1361     }
1362     return x509Crl->HashCode(env, info);
1363 }
1364 
NapiGetExtensionsObject(napi_env env,napi_callback_info info)1365 static napi_value NapiGetExtensionsObject(napi_env env, napi_callback_info info)
1366 {
1367     napi_value thisVar = nullptr;
1368     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1369     NapiX509Crl *x509Crl = nullptr;
1370     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1371     if (x509Crl == nullptr) {
1372         LOGE("x509Crl is nullptr!");
1373         return nullptr;
1374     }
1375     return x509Crl->GetExtensionsObject(env, info);
1376 }
1377 
NapiGetIssuerX500DistinguishedName(napi_env env,napi_callback_info info)1378 static napi_value NapiGetIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
1379 {
1380     napi_value thisVar = nullptr;
1381     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1382     NapiX509Crl *x509Crl = nullptr;
1383     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1384     if (x509Crl == nullptr) {
1385         LOGE("x509Crl is nullptr!");
1386         return nullptr;
1387     }
1388     return x509Crl->GetIssuerX500DistinguishedName(env, info);
1389 }
1390 
NapiGetSignature(napi_env env,napi_callback_info info)1391 static napi_value NapiGetSignature(napi_env env, napi_callback_info info)
1392 {
1393     napi_value thisVar = nullptr;
1394     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1395     NapiX509Crl *x509Crl = nullptr;
1396     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1397     if (x509Crl == nullptr) {
1398         LOGE("x509Crl is nullptr!");
1399         return nullptr;
1400     }
1401     return x509Crl->GetSignature(env, info);
1402 }
1403 
NapiGetSigAlgName(napi_env env,napi_callback_info info)1404 static napi_value NapiGetSigAlgName(napi_env env, napi_callback_info info)
1405 {
1406     napi_value thisVar = nullptr;
1407     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1408     NapiX509Crl *x509Crl = nullptr;
1409     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1410     if (x509Crl == nullptr) {
1411         LOGE("x509Crl is nullptr!");
1412         return nullptr;
1413     }
1414     return x509Crl->GetSigAlgName(env, info);
1415 }
1416 
NapiGetSigAlgOID(napi_env env,napi_callback_info info)1417 static napi_value NapiGetSigAlgOID(napi_env env, napi_callback_info info)
1418 {
1419     napi_value thisVar = nullptr;
1420     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1421     NapiX509Crl *x509Crl = nullptr;
1422     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1423     if (x509Crl == nullptr) {
1424         LOGE("x509Crl is nullptr!");
1425         return nullptr;
1426     }
1427     return x509Crl->GetSigAlgOID(env, info);
1428 }
1429 
NapiGetSigAlgParams(napi_env env,napi_callback_info info)1430 static napi_value NapiGetSigAlgParams(napi_env env, napi_callback_info info)
1431 {
1432     napi_value thisVar = nullptr;
1433     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1434     NapiX509Crl *x509Crl = nullptr;
1435     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1436     if (x509Crl == nullptr) {
1437         LOGE("x509Crl is nullptr!");
1438         return nullptr;
1439     }
1440     return x509Crl->GetSigAlgParams(env, info);
1441 }
1442 
NapiGetExtensions(napi_env env,napi_callback_info info)1443 static napi_value NapiGetExtensions(napi_env env, napi_callback_info info)
1444 {
1445     napi_value thisVar = nullptr;
1446     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1447     NapiX509Crl *x509Crl = nullptr;
1448     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1449     if (x509Crl == nullptr) {
1450         LOGE("x509Crl is nullptr!");
1451         return nullptr;
1452     }
1453     return x509Crl->GetExtensions(env, info);
1454 }
1455 
NapiMatch(napi_env env,napi_callback_info info)1456 static napi_value NapiMatch(napi_env env, napi_callback_info info)
1457 {
1458     napi_value thisVar = nullptr;
1459     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1460     NapiX509Crl *x509Crl = nullptr;
1461     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
1462     if (x509Crl == nullptr) {
1463         LOGE("x509Crl is nullptr!");
1464         return nullptr;
1465     }
1466     return x509Crl->Match(env, info);
1467 }
1468 
CreateX509CrlExecute(napi_env env,void * data)1469 void NapiX509Crl::CreateX509CrlExecute(napi_env env, void *data)
1470 {
1471     CfCtx *context = static_cast<CfCtx *>(data);
1472     context->errCode = HcfX509CrlCreate(context->encodingBlob, &context->crl);
1473     if (context->errCode != CF_SUCCESS) {
1474         context->errMsg = "create X509Crl failed";
1475     }
1476 }
1477 
CreateX509CrlComplete(napi_env env,napi_status status,void * data)1478 void NapiX509Crl::CreateX509CrlComplete(napi_env env, napi_status status, void *data)
1479 {
1480     CfCtx *context = static_cast<CfCtx *>(data);
1481     if (context->errCode != CF_SUCCESS) {
1482         LOGE("call create X509Crl failed!");
1483         ReturnResult(env, context, nullptr);
1484         FreeCryptoFwkCtx(env, context);
1485         return;
1486     }
1487     napi_value instance = CreateX509Crl(env, context->createX509CrlName);
1488     NapiX509Crl *x509CrlClass = new (std::nothrow) NapiX509Crl(context->crl);
1489     if (x509CrlClass == nullptr) {
1490         context->errCode = CF_ERR_MALLOC;
1491         context->errMsg = "Failed to create a x509Crl class";
1492         LOGE("Failed to create a x509Crl class");
1493         CfObjDestroy(context->crl);
1494         ReturnResult(env, context, nullptr);
1495         FreeCryptoFwkCtx(env, context);
1496         return;
1497     }
1498     status = napi_wrap(
1499         env, instance, x509CrlClass,
1500         [](napi_env env, void *data, void *hint) {
1501             NapiX509Crl *crlClass = static_cast<NapiX509Crl *>(data);
1502             delete crlClass;
1503             return;
1504         },
1505         nullptr, nullptr);
1506     if (status != napi_ok) {
1507         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
1508         LOGE("failed to wrap obj!");
1509         delete x509CrlClass;
1510         return;
1511     }
1512     ReturnResult(env, context, instance);
1513     FreeCryptoFwkCtx(env, context);
1514 }
1515 
NapiCreateX509CrlBase(napi_env env,napi_callback_info info,std::string createName)1516 napi_value NapiX509Crl::NapiCreateX509CrlBase(napi_env env, napi_callback_info info, std::string createName)
1517 {
1518     size_t argc = ARGS_SIZE_TWO;
1519     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
1520     napi_value thisVar = nullptr;
1521     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1522     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
1523         return nullptr;
1524     }
1525 
1526     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
1527     if (context == nullptr) {
1528         LOGE("malloc context failed!");
1529         return nullptr;
1530     }
1531     if (!GetEncodingBlobFromValue(env, argv[PARAM0], &context->encodingBlob)) {
1532         LOGE("get encoding blob from data failed!");
1533         FreeCryptoFwkCtx(env, context);
1534         return nullptr;
1535     }
1536 
1537     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
1538         LOGE("create reference failed!");
1539         FreeCryptoFwkCtx(env, context);
1540         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
1541         return nullptr;
1542     }
1543 
1544     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
1545         FreeCryptoFwkCtx(env, context);
1546         return nullptr;
1547     }
1548 
1549     context->createX509CrlName = createName;
1550 
1551     napi_create_async_work(env, nullptr, CertGetResourceName(env, createName.c_str()), CreateX509CrlExecute,
1552         CreateX509CrlComplete, static_cast<void *>(context), &context->asyncWork);
1553 
1554     napi_queue_async_work(env, context->asyncWork);
1555     if (context->asyncType == ASYNC_TYPE_PROMISE) {
1556         return context->promise;
1557     } else {
1558         return CertNapiGetNull(env);
1559     }
1560 }
1561 
NapiCreateX509Crl(napi_env env,napi_callback_info info)1562 napi_value NapiX509Crl::NapiCreateX509Crl(napi_env env, napi_callback_info info)
1563 {
1564     return NapiCreateX509CrlBase(env, info, std::string("createX509Crl"));
1565 }
1566 
NapiCreateX509CRL(napi_env env,napi_callback_info info)1567 napi_value NapiX509Crl::NapiCreateX509CRL(napi_env env, napi_callback_info info)
1568 {
1569     return NapiCreateX509CrlBase(env, info, std::string("createX509CRL"));
1570 }
1571 
X509CrlConstructor(napi_env env,napi_callback_info info)1572 static napi_value X509CrlConstructor(napi_env env, napi_callback_info info)
1573 {
1574     napi_value thisVar = nullptr;
1575     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1576     return thisVar;
1577 }
1578 
DefineX509CrlJS(napi_env env,napi_value exports,std::string className)1579 void NapiX509Crl::DefineX509CrlJS(napi_env env, napi_value exports, std::string className)
1580 {
1581     napi_property_descriptor desc[] = {
1582         DECLARE_NAPI_FUNCTION(className.c_str(), NapiCreateX509Crl),
1583     };
1584     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1585 
1586     napi_property_descriptor x509CrlDesc[] = {
1587         DECLARE_NAPI_FUNCTION("isRevoked", NapiIsRevoked),
1588         DECLARE_NAPI_FUNCTION("getType", NapiGetType),
1589         DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
1590         DECLARE_NAPI_FUNCTION("verify", NapiVerify),
1591         DECLARE_NAPI_FUNCTION("getVersion", NapiGetVersion),
1592         DECLARE_NAPI_FUNCTION("getIssuerName", NapiGetIssuerDN),
1593         DECLARE_NAPI_FUNCTION("getLastUpdate", NapiGetThisUpdate),
1594         DECLARE_NAPI_FUNCTION("getNextUpdate", NapiGetNextUpdate),
1595         DECLARE_NAPI_FUNCTION("getSignature", NapiGetSignature),
1596         DECLARE_NAPI_FUNCTION("getSignatureAlgName", NapiGetSigAlgName),
1597         DECLARE_NAPI_FUNCTION("getSignatureAlgOid", NapiGetSigAlgOID),
1598         DECLARE_NAPI_FUNCTION("getSignatureAlgParams", NapiGetSigAlgParams),
1599         DECLARE_NAPI_FUNCTION("getRevokedCert", NapiCrlGetRevokedCertificate),
1600         DECLARE_NAPI_FUNCTION("getRevokedCerts", NapiCrlGetRevokedCertificates),
1601         DECLARE_NAPI_FUNCTION("getRevokedCertWithCert", NapiCrlGetRevokedCertificateWithCert),
1602         DECLARE_NAPI_FUNCTION("getTbsInfo", NapiCrlGetTBSCertList),
1603         DECLARE_NAPI_FUNCTION("toString", NapiToString),
1604         DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
1605         DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
1606         DECLARE_NAPI_FUNCTION("getIssuerX500DistinguishedName", NapiGetIssuerX500DistinguishedName),
1607     };
1608     napi_value constructor = nullptr;
1609     napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlConstructor, nullptr,
1610         sizeof(x509CrlDesc) / sizeof(x509CrlDesc[0]), x509CrlDesc, &constructor);
1611 
1612     napi_create_reference(env, constructor, 1, &classCrlRef_);
1613 }
1614 
DefineX509CRLJS(napi_env env,napi_value exports,std::string className)1615 void NapiX509Crl::DefineX509CRLJS(napi_env env, napi_value exports, std::string className)
1616 {
1617     napi_property_descriptor desc[] = {
1618         DECLARE_NAPI_FUNCTION(className.c_str(), NapiCreateX509CRL),
1619     };
1620     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1621 
1622     napi_property_descriptor x509CrlDesc[] = {
1623         DECLARE_NAPI_FUNCTION("isRevoked", NapiIsRevoked),
1624         DECLARE_NAPI_FUNCTION("getType", NapiGetType),
1625         DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
1626         DECLARE_NAPI_FUNCTION("verify", NapiVerify),
1627         DECLARE_NAPI_FUNCTION("getVersion", NapiGetVersion),
1628         DECLARE_NAPI_FUNCTION("getIssuerName", NapiGetIssuerDN),
1629         DECLARE_NAPI_FUNCTION("getLastUpdate", NapiGetThisUpdate),
1630         DECLARE_NAPI_FUNCTION("getNextUpdate", NapiGetNextUpdate),
1631         DECLARE_NAPI_FUNCTION("getSignature", NapiGetSignature),
1632         DECLARE_NAPI_FUNCTION("getSignatureAlgName", NapiGetSigAlgName),
1633         DECLARE_NAPI_FUNCTION("getSignatureAlgOid", NapiGetSigAlgOID),
1634         DECLARE_NAPI_FUNCTION("getSignatureAlgParams", NapiGetSigAlgParams),
1635         DECLARE_NAPI_FUNCTION("getExtensions", NapiGetExtensions),
1636         DECLARE_NAPI_FUNCTION("getRevokedCert", NapiCRLGetRevokedCertificate),
1637         DECLARE_NAPI_FUNCTION("getRevokedCerts", NapiCRLGetRevokedCertificates),
1638         DECLARE_NAPI_FUNCTION("getRevokedCertWithCert", NapiCRLGetRevokedCertificateWithCert),
1639         DECLARE_NAPI_FUNCTION("getTBSInfo", NapiCRLGetTBSCertList),
1640         DECLARE_NAPI_FUNCTION("match", NapiMatch),
1641         DECLARE_NAPI_FUNCTION("toString", NapiToString),
1642         DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
1643         DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
1644         DECLARE_NAPI_FUNCTION("getIssuerX500DistinguishedName", NapiGetIssuerX500DistinguishedName),
1645     };
1646     napi_value constructor = nullptr;
1647     napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlConstructor, nullptr,
1648         sizeof(x509CrlDesc) / sizeof(x509CrlDesc[0]), x509CrlDesc, &constructor);
1649 
1650     napi_create_reference(env, constructor, 1, &classCRLRef_);
1651 }
1652 
DefineX509CrlJSClass(napi_env env,napi_value exports,std::string className)1653 void NapiX509Crl::DefineX509CrlJSClass(napi_env env, napi_value exports, std::string className)
1654 {
1655     std::string createName;
1656     if (className == std::string("X509Crl")) {
1657         createName = "createX509Crl";
1658         DefineX509CrlJS(env, exports, createName);
1659     } else {
1660         createName = "createX509CRL";
1661         DefineX509CRLJS(env, exports, createName);
1662     }
1663 }
1664 
CreateX509Crl(napi_env env,std::string createName)1665 napi_value NapiX509Crl::CreateX509Crl(napi_env env, std::string createName)
1666 {
1667     napi_value constructor = nullptr;
1668     napi_value instance = nullptr;
1669     if (createName == std::string("createX509Crl")) {
1670         napi_get_reference_value(env, classCrlRef_, &constructor);
1671     } else {
1672         napi_get_reference_value(env, classCRLRef_, &constructor);
1673     }
1674     napi_new_instance(env, constructor, 0, nullptr, &instance);
1675     return instance;
1676 }
1677 } // namespace CertFramework
1678 } // namespace OHOS
1679