• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_x509_crl.h"
17 
18 #include "napi/native_node_api.h"
19 #include "napi/native_api.h"
20 #include "log.h"
21 #include "memory.h"
22 #include "utils.h"
23 #include "object_base.h"
24 #include "result.h"
25 #include "napi_crypto_framework_defines.h"
26 #include "napi_pub_key.h"
27 #include "napi_utils.h"
28 #include "napi_x509_certificate.h"
29 #include "napi_x509_crl_entry.h"
30 
31 namespace OHOS {
32 namespace CryptoFramework {
33 thread_local napi_ref NapiX509Crl::classRef_ = nullptr;
34 
35 struct CfCtx {
36     CfAsyncType asyncType = ASYNC_TYPE_CALLBACK;
37     napi_value promise = nullptr;
38     napi_ref callback = nullptr;
39     napi_deferred deferred = nullptr;
40     napi_async_work asyncWork = nullptr;
41 
42     HcfEncodingBlob *encodingBlob = nullptr;
43     NapiX509Crl *crlClass = nullptr;
44     HcfX509Certificate *certificate = nullptr;
45     HcfPubKey *pubKey = nullptr;
46     int32_t serialNumber = 0;
47 
48     HcfX509CrlEntry *crlEntry = nullptr;
49     int32_t errCode = 0;
50     const char *errMsg = nullptr;
51     HcfX509Crl *crl;
52     HcfEncodingBlob *encoded = nullptr;
53     HcfBlob *blob = nullptr;
54     HcfArray *array = nullptr;
55 };
56 
FreeCryptoFwkCtx(napi_env env,CfCtx * context)57 static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
58 {
59     if (context == nullptr) {
60         return;
61     }
62 
63     if (context->asyncWork != nullptr) {
64         napi_delete_async_work(env, context->asyncWork);
65     }
66 
67     if (context->callback != nullptr) {
68         napi_delete_reference(env, context->callback);
69     }
70 
71     HcfEncodingBlobDataFree(context->encodingBlob);
72     HcfFree(context->encodingBlob);
73     context->encodingBlob = nullptr;
74 
75     HcfEncodingBlobDataFree(context->encoded);
76     HcfFree(context->encoded);
77     context->encoded = nullptr;
78 
79     HcfBlobDataFree(context->blob);
80     HcfFree(context->blob);
81     context->blob = nullptr;
82 
83     if (context->array != nullptr) {
84         HcfFree(context->array->data);
85         context->array->data = nullptr;
86         HcfFree(context->array);
87         context->array = nullptr;
88     }
89 
90     HcfFree(context);
91 }
92 
ReturnCallbackResult(napi_env env,CfCtx * context,napi_value result)93 static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result)
94 {
95     napi_value businessError = nullptr;
96     if (context->errCode != HCF_SUCCESS) {
97         businessError = GenerateBusinessError(env, context->errCode, context->errMsg, true);
98     }
99     napi_value params[ARGS_SIZE_TWO] = { businessError, result };
100 
101     napi_value func = nullptr;
102     napi_get_reference_value(env, context->callback, &func);
103 
104     napi_value recv = nullptr;
105     napi_value callFuncRet = nullptr;
106     napi_get_undefined(env, &recv);
107     napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
108 }
109 
ReturnPromiseResult(napi_env env,CfCtx * context,napi_value result)110 static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
111 {
112     if (context->errCode == HCF_SUCCESS) {
113         napi_resolve_deferred(env, context->deferred, result);
114     } else {
115         napi_reject_deferred(env, context->deferred,
116             GenerateBusinessError(env, context->errCode, context->errMsg, true));
117     }
118 }
119 
ReturnResult(napi_env env,CfCtx * context,napi_value result)120 static void ReturnResult(napi_env env, CfCtx *context, napi_value result)
121 {
122     if (context->asyncType == ASYNC_TYPE_CALLBACK) {
123         ReturnCallbackResult(env, context, result);
124     } else {
125         ReturnPromiseResult(env, context, result);
126     }
127 }
128 
CreateCallbackAndPromise(napi_env env,CfCtx * context,size_t argc,size_t maxCount,napi_value callbackValue)129 static bool CreateCallbackAndPromise(napi_env env, CfCtx *context, size_t argc,
130     size_t maxCount, napi_value callbackValue)
131 {
132     context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE;
133     if (context->asyncType == ASYNC_TYPE_CALLBACK) {
134         if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, true)) {
135             LOGE("get callback failed!");
136             return false;
137         }
138     } else {
139         napi_create_promise(env, &context->deferred, &context->promise);
140     }
141     return true;
142 }
143 
NapiX509Crl(HcfX509Crl * x509Crl)144 NapiX509Crl::NapiX509Crl(HcfX509Crl *x509Crl)
145 {
146     this->x509Crl_ = x509Crl;
147 }
148 
~NapiX509Crl()149 NapiX509Crl::~NapiX509Crl()
150 {
151     HcfObjDestroy(this->x509Crl_);
152 }
153 
GetEncodedExecute(napi_env env,void * data)154 static void GetEncodedExecute(napi_env env, void *data)
155 {
156     CfCtx *context = static_cast<CfCtx *>(data);
157     HcfX509Crl *x509Crl = context->crlClass->GetX509Crl();
158     HcfEncodingBlob *encodingBlob = static_cast<HcfEncodingBlob *>(HcfMalloc(sizeof(HcfEncodingBlob), 0));
159     if (encodingBlob == nullptr) {
160         LOGE("malloc encoding blob failed!");
161         context->errCode = HCF_ERR_MALLOC;
162         context->errMsg = "malloc encoding blob failed";
163         return;
164     }
165     context->errCode = x509Crl->getEncoded(x509Crl, encodingBlob);
166     if (context->errCode != HCF_SUCCESS) {
167         LOGE("get encoded failed!");
168         context->errMsg = "get encoded failed";
169     }
170     context->encoded = encodingBlob;
171 }
172 
GetEncodedComplete(napi_env env,napi_status status,void * data)173 static void GetEncodedComplete(napi_env env, napi_status status, void *data)
174 {
175     CfCtx *context = static_cast<CfCtx *>(data);
176     if (context->errCode != HCF_SUCCESS) {
177         ReturnResult(env, context, nullptr);
178         FreeCryptoFwkCtx(env, context);
179         return;
180     }
181     napi_value returnEncodingBlob = ConvertEncodingBlobToNapiValue(env, context->encoded);
182     ReturnResult(env, context, returnEncodingBlob);
183     FreeCryptoFwkCtx(env, context);
184 }
185 
VerifyExecute(napi_env env,void * data)186 static void VerifyExecute(napi_env env, void *data)
187 {
188     CfCtx *context = static_cast<CfCtx *>(data);
189     HcfX509Crl *x509Crl = context->crlClass->GetX509Crl();
190     context->errCode = x509Crl->verify(x509Crl, context->pubKey);
191     if (context->errCode != HCF_SUCCESS) {
192         LOGE("verify crl failed!");
193         context->errMsg = "verify crl failed";
194     }
195 }
196 
VerifyComplete(napi_env env,napi_status status,void * data)197 static void VerifyComplete(napi_env env, napi_status status, void *data)
198 {
199     CfCtx *context = static_cast<CfCtx *>(data);
200     ReturnResult(env, context, NapiGetNull(env));
201     FreeCryptoFwkCtx(env, context);
202 }
203 
GetRevokedCertificatesExecute(napi_env env,void * data)204 void GetRevokedCertificatesExecute(napi_env env, void *data)
205 {
206     CfCtx *context = static_cast<CfCtx *>(data);
207     HcfX509Crl *x509Crl = context->crlClass->GetX509Crl();
208     HcfArray *array = reinterpret_cast<HcfArray *>(HcfMalloc(sizeof(HcfArray), 0));
209     if (array == nullptr) {
210         LOGE("malloc array failed!");
211         context->errCode = HCF_ERR_MALLOC;
212         context->errMsg = "malloc array failed";
213         return;
214     }
215     context->errCode = x509Crl->getRevokedCerts(x509Crl, array);
216     if (context->errCode != HCF_SUCCESS) {
217         LOGE("get revoked certs failed!");
218         context->errMsg = "get revoked certs failed";
219     }
220     context->array = array;
221 }
222 
GenerateCrlEntryArray(napi_env env,HcfArray * array)223 static napi_value GenerateCrlEntryArray(napi_env env, HcfArray *array)
224 {
225     if (array == nullptr) {
226         LOGE("crl entry array is null!");
227         return nullptr;
228     }
229     if (array->count == 0) {
230         LOGE("crl entry array count is 0!");
231         return nullptr;
232     }
233     napi_value returnArray = nullptr;
234     napi_create_array(env, &returnArray);
235     for (uint32_t i = 0; i < array->count; i++) {
236         HcfBlob *blob = reinterpret_cast<HcfBlob *>(array->data + i);
237         HcfX509CrlEntry *entry = reinterpret_cast<HcfX509CrlEntry *>(blob->data);
238         napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env);
239         NapiX509CrlEntry *x509CrlEntryClass = new NapiX509CrlEntry(entry);
240         napi_wrap(
241             env, instance, x509CrlEntryClass,
242             [](napi_env env, void *data, void *hint) {
243                 NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
244                 delete x509CrlEntryClass;
245                 return;
246             },
247             nullptr, nullptr);
248         napi_set_element(env, returnArray, i, instance);
249     }
250     return returnArray;
251 }
252 
GetRevokedCertificatesComplete(napi_env env,napi_status status,void * data)253 void GetRevokedCertificatesComplete(napi_env env, napi_status status, void *data)
254 {
255     CfCtx *context = static_cast<CfCtx *>(data);
256     if (context->errCode != HCF_SUCCESS) {
257         ReturnResult(env, context, nullptr);
258         FreeCryptoFwkCtx(env, context);
259         return;
260     }
261     napi_value returnArray = GenerateCrlEntryArray(env, context->array);
262     ReturnResult(env, context, returnArray);
263     FreeCryptoFwkCtx(env, context);
264 }
265 
IsRevoked(napi_env env,napi_callback_info info)266 napi_value NapiX509Crl::IsRevoked(napi_env env, napi_callback_info info)
267 {
268     size_t argc = ARGS_SIZE_ONE;
269     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
270     napi_value thisVar = nullptr;
271     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
272     if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, true, true)) {
273         return nullptr;
274     }
275 
276     NapiX509Certificate *napiX509Cert = nullptr;
277     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
278     if (napiX509Cert == nullptr) {
279         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "napiX509Cert is null", true));
280         LOGE("napiX509Cert is null!");
281         return nullptr;
282     }
283 
284     HcfX509Crl *x509Crl = GetX509Crl();
285     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
286     bool isRevoked = x509Crl->base.isRevoked(&(x509Crl->base), &(certificate->base));
287     napi_value result = nullptr;
288     napi_get_boolean(env, isRevoked, &result);
289     return result;
290 }
291 
GetType(napi_env env,napi_callback_info info)292 napi_value NapiX509Crl::GetType(napi_env env, napi_callback_info info)
293 {
294     HcfX509Crl *x509Crl = GetX509Crl();
295     const char *type = x509Crl->base.getType(&(x509Crl->base));
296     napi_value result = nullptr;
297     napi_create_string_utf8(env, type, strlen(type), &result);
298     return result;
299 }
300 
GetEncoded(napi_env env,napi_callback_info info)301 napi_value NapiX509Crl::GetEncoded(napi_env env, napi_callback_info info)
302 {
303     size_t argc = ARGS_SIZE_ONE;
304     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
305     napi_value thisVar = nullptr;
306     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
307     if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) {
308         return nullptr;
309     }
310 
311     CfCtx *context = static_cast<CfCtx *>(HcfMalloc(sizeof(CfCtx), 0));
312     if (context == nullptr) {
313         LOGE("malloc context failed!");
314         return nullptr;
315     }
316     context->crlClass = this;
317 
318     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
319         FreeCryptoFwkCtx(env, context);
320         return nullptr;
321     }
322 
323     napi_create_async_work(
324         env, nullptr, GetResourceName(env, "GetEncoded"),
325         GetEncodedExecute,
326         GetEncodedComplete,
327         static_cast<void *>(context),
328         &context->asyncWork);
329 
330     napi_queue_async_work(env, context->asyncWork);
331     if (context->asyncType == ASYNC_TYPE_PROMISE) {
332         return context->promise;
333     } else {
334         return NapiGetNull(env);
335     }
336 }
337 
Verify(napi_env env,napi_callback_info info)338 napi_value NapiX509Crl::Verify(napi_env env, napi_callback_info info)
339 {
340     size_t argc = ARGS_SIZE_TWO;
341     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
342     napi_value thisVar = nullptr;
343     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
344     if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) {
345         return nullptr;
346     }
347 
348     NapiPubKey *pubKey = nullptr;
349     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&pubKey));
350     if (pubKey == nullptr) {
351         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "public key is null", true));
352         LOGE("pubKey is null!");
353         return nullptr;
354     }
355 
356     CfCtx *context = static_cast<CfCtx *>(HcfMalloc(sizeof(CfCtx), 0));
357     if (context == nullptr) {
358         LOGE("malloc context failed!");
359         return nullptr;
360     }
361     context->pubKey = pubKey->GetPubKey();
362     context->crlClass = this;
363 
364     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
365         FreeCryptoFwkCtx(env, context);
366         return nullptr;
367     }
368 
369     napi_create_async_work(
370         env, nullptr, GetResourceName(env, "Verify"),
371         VerifyExecute,
372         VerifyComplete,
373         static_cast<void *>(context),
374         &context->asyncWork);
375 
376     napi_queue_async_work(env, context->asyncWork);
377     if (context->asyncType == ASYNC_TYPE_PROMISE) {
378         return context->promise;
379     } else {
380         return NapiGetNull(env);
381     }
382 }
383 
GetVersion(napi_env env,napi_callback_info info)384 napi_value NapiX509Crl::GetVersion(napi_env env, napi_callback_info info)
385 {
386     HcfX509Crl *x509Crl = GetX509Crl();
387     int version = x509Crl->getVersion(x509Crl);
388     napi_value result = nullptr;
389     napi_create_int32(env, version, &result);
390     return result;
391 }
392 
GetIssuerDN(napi_env env,napi_callback_info info)393 napi_value NapiX509Crl::GetIssuerDN(napi_env env, napi_callback_info info)
394 {
395     HcfX509Crl *x509Crl = GetX509Crl();
396     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
397     if (blob == nullptr) {
398         LOGE("malloc blob failed!");
399         return nullptr;
400     }
401     HcfResult ret = x509Crl->getIssuerName(x509Crl, blob);
402     if (ret != HCF_SUCCESS) {
403         napi_throw(env, GenerateBusinessError(env, ret, "get issuer name failed", true));
404         LOGE("getIssuerDN failed!");
405         HcfFree(blob);
406         blob = nullptr;
407         return nullptr;
408     }
409     napi_value returnBlob = ConvertBlobToNapiValue(env, blob);
410     HcfBlobDataFree(blob);
411     HcfFree(blob);
412     blob = nullptr;
413     return returnBlob;
414 }
415 
GetThisUpdate(napi_env env,napi_callback_info info)416 napi_value NapiX509Crl::GetThisUpdate(napi_env env, napi_callback_info info)
417 {
418     HcfX509Crl *x509Crl = GetX509Crl();
419     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
420     if (blob == nullptr) {
421         LOGE("malloc blob failed!");
422         return nullptr;
423     }
424     HcfResult ret = x509Crl->getLastUpdate(x509Crl, blob);
425     if (ret != HCF_SUCCESS) {
426         napi_throw(env, GenerateBusinessError(env, ret, "get last update failed", true));
427         LOGE("getLastUpdate failed!");
428         HcfFree(blob);
429         blob = nullptr;
430         return nullptr;
431     }
432     napi_value result = nullptr;
433     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), blob->len, &result);
434     HcfBlobDataFree(blob);
435     HcfFree(blob);
436     blob = nullptr;
437     return result;
438 }
439 
GetNextUpdate(napi_env env,napi_callback_info info)440 napi_value NapiX509Crl::GetNextUpdate(napi_env env, napi_callback_info info)
441 {
442     HcfX509Crl *x509Crl = GetX509Crl();
443     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
444     if (blob == nullptr) {
445         LOGE("malloc blob failed!");
446         return nullptr;
447     }
448     HcfResult ret = x509Crl->getNextUpdate(x509Crl, blob);
449     if (ret != HCF_SUCCESS) {
450         napi_throw(env, GenerateBusinessError(env, ret, "get next update failed", true));
451         LOGE("getNextUpdate failed!");
452         HcfFree(blob);
453         blob = nullptr;
454         return nullptr;
455     }
456     napi_value result = nullptr;
457     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), blob->len, &result);
458     HcfBlobDataFree(blob);
459     HcfFree(blob);
460     blob = nullptr;
461     return result;
462 }
463 
GetRevokedCertificate(napi_env env,napi_callback_info info)464 napi_value NapiX509Crl::GetRevokedCertificate(napi_env env, napi_callback_info info)
465 {
466     size_t argc = ARGS_SIZE_ONE;
467     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
468     napi_value thisVar = nullptr;
469     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
470     if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, true, true)) {
471         return nullptr;
472     }
473     int32_t serialNumber = 0;
474     if (!GetInt32FromJSParams(env, argv[PARAM0], serialNumber, true)) {
475         LOGE("get serialNumber failed!");
476         return nullptr;
477     }
478     HcfX509Crl *x509Crl = GetX509Crl();
479     HcfX509CrlEntry *crlEntry = nullptr;
480     HcfResult ret = x509Crl->getRevokedCert(x509Crl, serialNumber, &crlEntry);
481     if (ret != HCF_SUCCESS) {
482         napi_throw(env, GenerateBusinessError(env, ret, "get revoked cert failed!", true));
483         LOGE("get revoked cert failed!");
484         return nullptr;
485     }
486     napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env);
487     NapiX509CrlEntry *x509CrlEntryClass = new NapiX509CrlEntry(crlEntry);
488     napi_wrap(
489         env, instance, x509CrlEntryClass,
490         [](napi_env env, void *data, void *hint) {
491             NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
492             delete x509CrlEntryClass;
493             return;
494         },
495         nullptr, nullptr);
496     return instance;
497 }
498 
GetRevokedCertificateWithCert(napi_env env,napi_callback_info info)499 napi_value NapiX509Crl::GetRevokedCertificateWithCert(napi_env env, napi_callback_info info)
500 {
501     size_t argc = ARGS_SIZE_ONE;
502     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
503     napi_value thisVar = nullptr;
504     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
505     if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, true, true)) {
506         return nullptr;
507     }
508 
509     NapiX509Certificate *napiX509Cert = nullptr;
510     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
511     if (napiX509Cert == nullptr) {
512         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "napiX509Cert is null", true));
513         LOGE("napiX509Cert is null!");
514         return nullptr;
515     }
516 
517     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
518     HcfX509Crl *x509Crl = GetX509Crl();
519     HcfX509CrlEntry *crlEntry = nullptr;
520     HcfResult ret = x509Crl->getRevokedCertWithCert(x509Crl, certificate, &crlEntry);
521     if (ret != HCF_SUCCESS) {
522         napi_throw(env, GenerateBusinessError(env, ret, "get revoked cert with cert failed!", true));
523         LOGE("get revoked cert with cert failed!");
524         return nullptr;
525     }
526 
527     napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env);
528     NapiX509CrlEntry *x509CrlEntryClass = new NapiX509CrlEntry(crlEntry);
529     napi_wrap(
530         env, instance, x509CrlEntryClass,
531         [](napi_env env, void *data, void *hint) {
532             NapiX509CrlEntry *x509CrlEntryClass = static_cast<NapiX509CrlEntry *>(data);
533             delete x509CrlEntryClass;
534             return;
535         },
536         nullptr, nullptr);
537     return instance;
538 }
539 
GetRevokedCertificates(napi_env env,napi_callback_info info)540 napi_value NapiX509Crl::GetRevokedCertificates(napi_env env, napi_callback_info info)
541 {
542     size_t argc = ARGS_SIZE_ONE;
543     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
544     napi_value thisVar = nullptr;
545     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
546     if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) {
547         return nullptr;
548     }
549 
550     CfCtx *context = static_cast<CfCtx *>(HcfMalloc(sizeof(CfCtx), 0));
551     if (context == nullptr) {
552         LOGE("malloc context failed!");
553         return nullptr;
554     }
555     context->crlClass = this;
556 
557     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
558         FreeCryptoFwkCtx(env, context);
559         return nullptr;
560     }
561 
562     napi_create_async_work(
563         env, nullptr, GetResourceName(env, "GetRevokedCertificates"),
564         GetRevokedCertificatesExecute,
565         GetRevokedCertificatesComplete,
566         static_cast<void *>(context),
567         &context->asyncWork);
568 
569     napi_queue_async_work(env, context->asyncWork);
570     if (context->asyncType == ASYNC_TYPE_PROMISE) {
571         return context->promise;
572     } else {
573         return NapiGetNull(env);
574     }
575 }
576 
GetTBSCertList(napi_env env,napi_callback_info info)577 napi_value NapiX509Crl::GetTBSCertList(napi_env env, napi_callback_info info)
578 {
579     HcfX509Crl *x509Crl = GetX509Crl();
580     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
581     if (blob == nullptr) {
582         LOGE("malloc blob failed!");
583         return nullptr;
584     }
585     HcfResult result = x509Crl->getSignature(x509Crl, blob);
586     if (result != HCF_SUCCESS) {
587         napi_throw(env, GenerateBusinessError(env, result, "get tbs info failed", true));
588         LOGE("get tbs info failed!");
589         HcfFree(blob);
590         blob = nullptr;
591         return nullptr;
592     }
593     napi_value returnBlob = ConvertBlobToNapiValue(env, blob);
594     HcfBlobDataFree(blob);
595     HcfFree(blob);
596     blob = nullptr;
597     return returnBlob;
598 }
599 
GetSignature(napi_env env,napi_callback_info info)600 napi_value NapiX509Crl::GetSignature(napi_env env, napi_callback_info info)
601 {
602     HcfX509Crl *x509Crl = GetX509Crl();
603     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
604     if (blob == nullptr) {
605         LOGE("malloc blob failed!");
606         return nullptr;
607     }
608     HcfResult result = x509Crl->getSignature(x509Crl, blob);
609     if (result != HCF_SUCCESS) {
610         napi_throw(env, GenerateBusinessError(env, result, "get signature failed", true));
611         LOGE("getSignature failed!");
612         HcfFree(blob);
613         blob = nullptr;
614         return nullptr;
615     }
616     napi_value returnBlob = ConvertBlobToNapiValue(env, blob);
617     HcfBlobDataFree(blob);
618     HcfFree(blob);
619     blob = nullptr;
620     return returnBlob;
621 }
622 
GetSigAlgName(napi_env env,napi_callback_info info)623 napi_value NapiX509Crl::GetSigAlgName(napi_env env, napi_callback_info info)
624 {
625     HcfX509Crl *x509Crl = GetX509Crl();
626     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
627     if (blob == nullptr) {
628         LOGE("malloc blob failed!");
629         return nullptr;
630     }
631     HcfResult ret = x509Crl->getSignatureAlgName(x509Crl, blob);
632     if (ret != HCF_SUCCESS) {
633         napi_throw(env, GenerateBusinessError(env, ret, "get signature alg name failed", true));
634         LOGE("getSigAlgName failed!");
635         HcfFree(blob);
636         blob = nullptr;
637         return nullptr;
638     }
639     napi_value result = nullptr;
640     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), blob->len, &result);
641     HcfBlobDataFree(blob);
642     HcfFree(blob);
643     blob = nullptr;
644     return result;
645 }
646 
GetSigAlgOID(napi_env env,napi_callback_info info)647 napi_value NapiX509Crl::GetSigAlgOID(napi_env env, napi_callback_info info)
648 {
649     HcfX509Crl *x509Crl = GetX509Crl();
650     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
651     if (blob == nullptr) {
652         LOGE("malloc blob failed!");
653         return nullptr;
654     }
655     HcfResult ret = x509Crl->getSignatureAlgOid(x509Crl, blob);
656     if (ret != HCF_SUCCESS) {
657         napi_throw(env, GenerateBusinessError(env, ret, "get signature alg oid failed", true));
658         LOGE("getSigAlgOID failed!");
659         HcfFree(blob);
660         blob = nullptr;
661         return nullptr;
662     }
663     napi_value result = nullptr;
664     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), blob->len, &result);
665     HcfBlobDataFree(blob);
666     HcfFree(blob);
667     blob = nullptr;
668     return result;
669 }
670 
GetSigAlgParams(napi_env env,napi_callback_info info)671 napi_value NapiX509Crl::GetSigAlgParams(napi_env env, napi_callback_info info)
672 {
673     HcfX509Crl *x509Crl = GetX509Crl();
674     HcfBlob *blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
675     if (blob == nullptr) {
676         LOGE("malloc blob failed!");
677         return nullptr;
678     }
679     HcfResult result = x509Crl->getSignatureAlgParams(x509Crl, blob);
680     if (result != HCF_SUCCESS) {
681         napi_throw(env, GenerateBusinessError(env, result, "get signature alg params failed", true));
682         LOGE("getSigAlgParams failed!");
683         HcfFree(blob);
684         blob = nullptr;
685         return nullptr;
686     }
687     napi_value returnBlob = ConvertBlobToNapiValue(env, blob);
688     HcfBlobDataFree(blob);
689     HcfFree(blob);
690     blob = nullptr;
691     return returnBlob;
692 }
693 
NapiIsRevoked(napi_env env,napi_callback_info info)694 static napi_value NapiIsRevoked(napi_env env, napi_callback_info info)
695 {
696     napi_value thisVar = nullptr;
697     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
698     NapiX509Crl *x509Crl = nullptr;
699     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
700     if (x509Crl == nullptr) {
701         LOGE("x509Crl is nullptr!");
702         return nullptr;
703     }
704     return x509Crl->IsRevoked(env, info);
705 }
706 
NapiGetType(napi_env env,napi_callback_info info)707 static napi_value NapiGetType(napi_env env, napi_callback_info info)
708 {
709     LOGI("napi get crl type called.");
710     napi_value thisVar = nullptr;
711     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
712     NapiX509Crl *x509Crl = nullptr;
713     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
714     if (x509Crl == nullptr) {
715         LOGE("x509Crl is nullptr!");
716         return nullptr;
717     }
718     LOGI("unwrap x509 crl class success.");
719     return x509Crl->GetType(env, info);
720 }
721 
NapiGetEncoded(napi_env env,napi_callback_info info)722 static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
723 {
724     napi_value thisVar = nullptr;
725     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
726     NapiX509Crl *x509Crl = nullptr;
727     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
728     if (x509Crl == nullptr) {
729         LOGE("x509Crl is nullptr!");
730         return nullptr;
731     }
732     return x509Crl->GetEncoded(env, info);
733 }
734 
NapiVerify(napi_env env,napi_callback_info info)735 static napi_value NapiVerify(napi_env env, napi_callback_info info)
736 {
737     napi_value thisVar = nullptr;
738     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
739     NapiX509Crl *x509Crl = nullptr;
740     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
741     if (x509Crl == nullptr) {
742         LOGE("x509Crl is nullptr!");
743         return nullptr;
744     }
745     return x509Crl->Verify(env, info);
746 }
747 
NapiGetVersion(napi_env env,napi_callback_info info)748 static napi_value NapiGetVersion(napi_env env, napi_callback_info info)
749 {
750     napi_value thisVar = nullptr;
751     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
752     NapiX509Crl *x509Crl = nullptr;
753     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
754     if (x509Crl == nullptr) {
755         LOGE("x509Crl is nullptr!");
756         return nullptr;
757     }
758     return x509Crl->GetVersion(env, info);
759 }
760 
NapiGetIssuerDN(napi_env env,napi_callback_info info)761 static napi_value NapiGetIssuerDN(napi_env env, napi_callback_info info)
762 {
763     napi_value thisVar = nullptr;
764     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
765     NapiX509Crl *x509Crl = nullptr;
766     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
767     if (x509Crl == nullptr) {
768         LOGE("x509Crl is nullptr!");
769         return nullptr;
770     }
771     return x509Crl->GetIssuerDN(env, info);
772 }
773 
NapiGetThisUpdate(napi_env env,napi_callback_info info)774 static napi_value NapiGetThisUpdate(napi_env env, napi_callback_info info)
775 {
776     napi_value thisVar = nullptr;
777     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
778     NapiX509Crl *x509Crl = nullptr;
779     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
780     if (x509Crl == nullptr) {
781         LOGE("x509Crl is nullptr!");
782         return nullptr;
783     }
784     return x509Crl->GetThisUpdate(env, info);
785 }
786 
NapiGetNextUpdate(napi_env env,napi_callback_info info)787 static napi_value NapiGetNextUpdate(napi_env env, napi_callback_info info)
788 {
789     napi_value thisVar = nullptr;
790     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
791     NapiX509Crl *x509Crl = nullptr;
792     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
793     if (x509Crl == nullptr) {
794         LOGE("x509Crl is nullptr!");
795         return nullptr;
796     }
797     return x509Crl->GetNextUpdate(env, info);
798 }
799 
NapiGetRevokedCertificate(napi_env env,napi_callback_info info)800 static napi_value NapiGetRevokedCertificate(napi_env env, napi_callback_info info)
801 {
802     napi_value thisVar = nullptr;
803     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
804     NapiX509Crl *x509Crl = nullptr;
805     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
806     if (x509Crl == nullptr) {
807         LOGE("x509Crl is nullptr!");
808         return nullptr;
809     }
810     return x509Crl->GetRevokedCertificate(env, info);
811 }
812 
NapiGetRevokedCertificateWithCert(napi_env env,napi_callback_info info)813 static napi_value NapiGetRevokedCertificateWithCert(napi_env env, napi_callback_info info)
814 {
815     napi_value thisVar = nullptr;
816     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
817     NapiX509Crl *x509Crl = nullptr;
818     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
819     if (x509Crl == nullptr) {
820         LOGE("x509Crl is nullptr!");
821         return nullptr;
822     }
823     return x509Crl->GetRevokedCertificateWithCert(env, info);
824 }
825 
NapiGetRevokedCertificates(napi_env env,napi_callback_info info)826 static napi_value NapiGetRevokedCertificates(napi_env env, napi_callback_info info)
827 {
828     napi_value thisVar = nullptr;
829     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
830     NapiX509Crl *x509Crl = nullptr;
831     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
832     if (x509Crl == nullptr) {
833         LOGE("x509Crl is nullptr!");
834         return nullptr;
835     }
836     return x509Crl->GetRevokedCertificates(env, info);
837 }
838 
NapiGetTBSCertList(napi_env env,napi_callback_info info)839 static napi_value NapiGetTBSCertList(napi_env env, napi_callback_info info)
840 {
841     napi_value thisVar = nullptr;
842     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
843     NapiX509Crl *x509Crl = nullptr;
844     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
845     if (x509Crl == nullptr) {
846         LOGE("x509Crl is nullptr!");
847         return nullptr;
848     }
849     return x509Crl->GetTBSCertList(env, info);
850 }
851 
NapiGetSignature(napi_env env,napi_callback_info info)852 static napi_value NapiGetSignature(napi_env env, napi_callback_info info)
853 {
854     napi_value thisVar = nullptr;
855     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
856     NapiX509Crl *x509Crl = nullptr;
857     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
858     if (x509Crl == nullptr) {
859         LOGE("x509Crl is nullptr!");
860         return nullptr;
861     }
862     return x509Crl->GetSignature(env, info);
863 }
864 
NapiGetSigAlgName(napi_env env,napi_callback_info info)865 static napi_value NapiGetSigAlgName(napi_env env, napi_callback_info info)
866 {
867     napi_value thisVar = nullptr;
868     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
869     NapiX509Crl *x509Crl = nullptr;
870     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
871     if (x509Crl == nullptr) {
872         LOGE("x509Crl is nullptr!");
873         return nullptr;
874     }
875     return x509Crl->GetSigAlgName(env, info);
876 }
877 
NapiGetSigAlgOID(napi_env env,napi_callback_info info)878 static napi_value NapiGetSigAlgOID(napi_env env, napi_callback_info info)
879 {
880     napi_value thisVar = nullptr;
881     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
882     NapiX509Crl *x509Crl = nullptr;
883     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
884     if (x509Crl == nullptr) {
885         LOGE("x509Crl is nullptr!");
886         return nullptr;
887     }
888     return x509Crl->GetSigAlgOID(env, info);
889 }
890 
NapiGetSigAlgParams(napi_env env,napi_callback_info info)891 static napi_value NapiGetSigAlgParams(napi_env env, napi_callback_info info)
892 {
893     napi_value thisVar = nullptr;
894     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
895     NapiX509Crl *x509Crl = nullptr;
896     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Crl));
897     if (x509Crl == nullptr) {
898         LOGE("x509Crl is nullptr!");
899         return nullptr;
900     }
901     return x509Crl->GetSigAlgParams(env, info);
902 }
903 
CreateX509CrlExecute(napi_env env,void * data)904 void NapiX509Crl::CreateX509CrlExecute(napi_env env, void *data)
905 {
906     CfCtx *context = static_cast<CfCtx *>(data);
907     context->errCode = HcfX509CrlCreate(context->encodingBlob, &context->crl);
908     if (context->errCode != HCF_SUCCESS) {
909         context->errMsg = "create X509Crl failed";
910     }
911 }
912 
CreateX509CrlComplete(napi_env env,napi_status status,void * data)913 void NapiX509Crl::CreateX509CrlComplete(napi_env env, napi_status status, void *data)
914 {
915     CfCtx *context = static_cast<CfCtx *>(data);
916     if (context->errCode != HCF_SUCCESS) {
917         LOGE("call create X509Crl failed!");
918         ReturnResult(env, context, nullptr);
919         FreeCryptoFwkCtx(env, context);
920         return;
921     }
922     napi_value instance = CreateX509Crl(env);
923     NapiX509Crl *x509CrlClass = new NapiX509Crl(context->crl);
924     napi_wrap(
925         env, instance, x509CrlClass,
926         [](napi_env env, void *data, void *hint) {
927             NapiX509Crl *crlClass = static_cast<NapiX509Crl *>(data);
928             delete crlClass;
929             return;
930         },
931         nullptr, nullptr);
932     ReturnResult(env, context, instance);
933     FreeCryptoFwkCtx(env, context);
934 }
935 
NapiCreateX509Crl(napi_env env,napi_callback_info info)936 napi_value NapiX509Crl::NapiCreateX509Crl(napi_env env, napi_callback_info info)
937 {
938     size_t argc = ARGS_SIZE_TWO;
939     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
940     napi_value thisVar = nullptr;
941     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
942     if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) {
943         return nullptr;
944     }
945 
946     CfCtx *context = static_cast<CfCtx *>(HcfMalloc(sizeof(CfCtx), 0));
947     if (context == nullptr) {
948         LOGE("malloc context failed!");
949         return nullptr;
950     }
951     if (!GetEncodingBlobFromValue(env, argv[PARAM0], &context->encodingBlob)) {
952         LOGE("get encoding blob from data failed!");
953         FreeCryptoFwkCtx(env, context);
954         return nullptr;
955     }
956 
957     if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) {
958         FreeCryptoFwkCtx(env, context);
959         return nullptr;
960     }
961 
962     napi_create_async_work(
963         env, nullptr, GetResourceName(env, "createX509Crl"),
964         CreateX509CrlExecute,
965         CreateX509CrlComplete,
966         static_cast<void *>(context),
967         &context->asyncWork);
968 
969     napi_queue_async_work(env, context->asyncWork);
970     if (context->asyncType == ASYNC_TYPE_PROMISE) {
971         return context->promise;
972     } else {
973         return NapiGetNull(env);
974     }
975 }
976 
X509CrlConstructor(napi_env env,napi_callback_info info)977 static napi_value X509CrlConstructor(napi_env env, napi_callback_info info)
978 {
979     napi_value thisVar = nullptr;
980     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
981     return thisVar;
982 }
983 
DefineX509CrlJSClass(napi_env env,napi_value exports)984 void NapiX509Crl::DefineX509CrlJSClass(napi_env env, napi_value exports)
985 {
986     napi_property_descriptor desc[] = {
987         DECLARE_NAPI_FUNCTION("createX509Crl", NapiCreateX509Crl),
988     };
989     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
990 
991     napi_property_descriptor x509CrlDesc[] = {
992         DECLARE_NAPI_FUNCTION("isRevoked", NapiIsRevoked),
993         DECLARE_NAPI_FUNCTION("getType", NapiGetType),
994         DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
995         DECLARE_NAPI_FUNCTION("verify", NapiVerify),
996         DECLARE_NAPI_FUNCTION("getVersion", NapiGetVersion),
997         DECLARE_NAPI_FUNCTION("getIssuerName", NapiGetIssuerDN),
998         DECLARE_NAPI_FUNCTION("getLastUpdate", NapiGetThisUpdate),
999         DECLARE_NAPI_FUNCTION("getNextUpdate", NapiGetNextUpdate),
1000         DECLARE_NAPI_FUNCTION("getRevokedCert", NapiGetRevokedCertificate),
1001         DECLARE_NAPI_FUNCTION("getRevokedCertWithCert", NapiGetRevokedCertificateWithCert),
1002         DECLARE_NAPI_FUNCTION("getRevokedCerts", NapiGetRevokedCertificates),
1003         DECLARE_NAPI_FUNCTION("getTbsInfo", NapiGetTBSCertList),
1004         DECLARE_NAPI_FUNCTION("getSignature", NapiGetSignature),
1005         DECLARE_NAPI_FUNCTION("getSignatureAlgName", NapiGetSigAlgName),
1006         DECLARE_NAPI_FUNCTION("getSignatureAlgOid", NapiGetSigAlgOID),
1007         DECLARE_NAPI_FUNCTION("getSignatureAlgParams", NapiGetSigAlgParams),
1008     };
1009     napi_value constructor = nullptr;
1010     napi_define_class(env, "X509Crl", NAPI_AUTO_LENGTH, X509CrlConstructor, nullptr,
1011         sizeof(x509CrlDesc) / sizeof(x509CrlDesc[0]), x509CrlDesc, &constructor);
1012     napi_create_reference(env, constructor, 1, &classRef_);
1013 }
1014 
CreateX509Crl(napi_env env)1015 napi_value NapiX509Crl::CreateX509Crl(napi_env env)
1016 {
1017     napi_value constructor = nullptr;
1018     napi_value instance = nullptr;
1019     napi_get_reference_value(env, classRef_, &constructor);
1020     napi_new_instance(env, constructor, 0, nullptr, &instance);
1021     return instance;
1022 }
1023 } // namespace CryptoFramework
1024 } // namespace OHOS
1025