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