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_entry.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 "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "napi_cert_defines.h"
25 #include "napi_cert_utils.h"
26 #include "utils.h"
27 #include "napi_x509_distinguished_name.h"
28 #include "napi_cert_extension.h"
29
30 namespace OHOS {
31 namespace CertFramework {
32 thread_local napi_ref NapiX509CrlEntry::classCrlRef_ = nullptr;
33 thread_local napi_ref NapiX509CrlEntry::classCRLRef_ = nullptr;
34
35 struct CfCtx {
36 AsyncType 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 napi_ref cfRef = nullptr;
42
43 NapiX509CrlEntry *crlEntryClass = nullptr;
44
45 int32_t errCode = 0;
46 const char *errMsg = nullptr;
47 CfEncodingBlob *encoded = nullptr;
48 CfBlob *blob = nullptr;
49 };
50
FreeCryptoFwkCtx(napi_env env,CfCtx * context)51 static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
52 {
53 if (context == nullptr) {
54 return;
55 }
56
57 if (context->asyncWork != nullptr) {
58 napi_delete_async_work(env, context->asyncWork);
59 }
60
61 if (context->callback != nullptr) {
62 napi_delete_reference(env, context->callback);
63 }
64
65 if (context->cfRef != nullptr) {
66 napi_delete_reference(env, context->cfRef);
67 context->cfRef = nullptr;
68 }
69
70 CfEncodingBlobDataFree(context->encoded);
71 CfFree(context->encoded);
72 context->encoded = nullptr;
73
74 CfBlobDataFree(context->blob);
75 CfFree(context->blob);
76 context->blob = nullptr;
77
78 CfFree(context);
79 }
80
ReturnCallbackResult(napi_env env,CfCtx * context,napi_value result)81 static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result)
82 {
83 napi_value businessError = nullptr;
84 if (context->errCode != CF_SUCCESS) {
85 businessError = CertGenerateBusinessError(env, context->errCode, context->errMsg);
86 }
87 napi_value params[ARGS_SIZE_TWO] = { businessError, result };
88
89 napi_value func = nullptr;
90 napi_get_reference_value(env, context->callback, &func);
91
92 napi_value recv = nullptr;
93 napi_value callFuncRet = nullptr;
94 napi_get_undefined(env, &recv);
95 napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
96 }
97
ReturnPromiseResult(napi_env env,CfCtx * context,napi_value result)98 static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
99 {
100 if (context->errCode == CF_SUCCESS) {
101 napi_resolve_deferred(env, context->deferred, result);
102 } else {
103 napi_reject_deferred(env, context->deferred, CertGenerateBusinessError(env, context->errCode, context->errMsg));
104 }
105 }
106
ReturnResult(napi_env env,CfCtx * context,napi_value result)107 static void ReturnResult(napi_env env, CfCtx *context, napi_value result)
108 {
109 if (context->asyncType == ASYNC_TYPE_CALLBACK) {
110 ReturnCallbackResult(env, context, result);
111 } else {
112 ReturnPromiseResult(env, context, result);
113 }
114 }
115
CreateCallbackAndPromise(napi_env env,CfCtx * context,size_t argc,size_t maxCount,napi_value callbackValue)116 static bool CreateCallbackAndPromise(
117 napi_env env, CfCtx *context, size_t argc, size_t maxCount, napi_value callbackValue)
118 {
119 context->asyncType = GetAsyncType(env, argc, maxCount, callbackValue);
120 if (context->asyncType == ASYNC_TYPE_CALLBACK) {
121 if (!CertGetCallbackFromJSParams(env, callbackValue, &context->callback)) {
122 LOGE("x509 crl entry: get callback failed!");
123 return false;
124 }
125 } else {
126 napi_create_promise(env, &context->deferred, &context->promise);
127 }
128 return true;
129 }
130
NapiX509CrlEntry(HcfX509CrlEntry * x509CrlEntry)131 NapiX509CrlEntry::NapiX509CrlEntry(HcfX509CrlEntry *x509CrlEntry)
132 {
133 this->x509CrlEntry_ = x509CrlEntry;
134 }
135
~NapiX509CrlEntry()136 NapiX509CrlEntry::~NapiX509CrlEntry()
137 {
138 CfObjDestroy(this->x509CrlEntry_);
139 }
140
GetEncodedExecute(napi_env env,void * data)141 static void GetEncodedExecute(napi_env env, void *data)
142 {
143 CfCtx *context = static_cast<CfCtx *>(data);
144 HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry();
145 CfEncodingBlob *encodingBlob = static_cast<CfEncodingBlob *>(CfMalloc(sizeof(CfEncodingBlob), 0));
146 if (encodingBlob == nullptr) {
147 LOGE("malloc encoding blob failed!");
148 context->errCode = CF_ERR_MALLOC;
149 context->errMsg = "malloc encoding blob failed";
150 return;
151 }
152
153 context->errCode = x509CrlEntry->getEncoded(x509CrlEntry, encodingBlob);
154 if (context->errCode != CF_SUCCESS) {
155 LOGE("get encoded failed!");
156 context->errMsg = "get encoded failed";
157 }
158 context->encoded = encodingBlob;
159 }
160
GetEncodedComplete(napi_env env,napi_status status,void * data)161 static void GetEncodedComplete(napi_env env, napi_status status, void *data)
162 {
163 CfCtx *context = static_cast<CfCtx *>(data);
164 if (context->errCode != CF_SUCCESS) {
165 ReturnResult(env, context, nullptr);
166 FreeCryptoFwkCtx(env, context);
167 return;
168 }
169 napi_value returnEncodingBlob = ConvertEncodingBlobToNapiValue(env, context->encoded);
170 ReturnResult(env, context, returnEncodingBlob);
171 FreeCryptoFwkCtx(env, context);
172 }
173
GetEncoded(napi_env env,napi_callback_info info)174 napi_value NapiX509CrlEntry::GetEncoded(napi_env env, napi_callback_info info)
175 {
176 size_t argc = ARGS_SIZE_ONE;
177 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
178 napi_value thisVar = nullptr;
179 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
180 if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
181 return nullptr;
182 }
183
184 CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
185 if (context == nullptr) {
186 LOGE("malloc context failed!");
187 return nullptr;
188 }
189 context->crlEntryClass = this;
190
191 if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
192 LOGE("create reference failed!");
193 FreeCryptoFwkCtx(env, context);
194 napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
195 return nullptr;
196 }
197
198 if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_ONE, argv[PARAM0])) {
199 FreeCryptoFwkCtx(env, context);
200 return nullptr;
201 }
202
203 napi_create_async_work(env, nullptr, CertGetResourceName(env, "GetEncoded"), GetEncodedExecute, GetEncodedComplete,
204 static_cast<void *>(context), &context->asyncWork);
205
206 napi_queue_async_work(env, context->asyncWork);
207 if (context->asyncType == ASYNC_TYPE_PROMISE) {
208 return context->promise;
209 } else {
210 return CertNapiGetNull(env);
211 }
212 }
213
GetCrlEntrySerialNumber(napi_env env,napi_callback_info info)214 napi_value NapiX509CrlEntry::GetCrlEntrySerialNumber(napi_env env, napi_callback_info info)
215 {
216 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
217 CfBlob blob = { 0, nullptr };
218 CfResult ret = x509CrlEntry->getSerialNumber(x509CrlEntry, &blob);
219 if (ret != CF_SUCCESS) {
220 napi_throw(env, CertGenerateBusinessError(env, ret, "crl entry get serial num failed"));
221 LOGE("crl entry get serial num failed!");
222 return nullptr;
223 }
224
225 napi_value result = ConvertBlobToInt64(env, blob);
226 CfBlobDataFree(&blob);
227 return result;
228 }
229
GetCRLEntrySerialNumber(napi_env env,napi_callback_info info)230 napi_value NapiX509CrlEntry::GetCRLEntrySerialNumber(napi_env env, napi_callback_info info)
231 {
232 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
233 CfBlob blob = { 0, nullptr };
234 CfResult ret = x509CrlEntry->getSerialNumber(x509CrlEntry, &blob);
235 if (ret != CF_SUCCESS) {
236 napi_throw(env, CertGenerateBusinessError(env, ret, "crl entry get serial num failed"));
237 LOGE("crl entry get serial num failed!");
238 return nullptr;
239 }
240
241 napi_value result = ConvertBlobToBigIntWords(env, blob);
242 CfBlobDataFree(&blob);
243 return result;
244 }
245
GetCertificateIssuer(napi_env env,napi_callback_info info)246 napi_value NapiX509CrlEntry::GetCertificateIssuer(napi_env env, napi_callback_info info)
247 {
248 CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
249 if (blob == nullptr) {
250 LOGE("malloc blob failed!");
251 return nullptr;
252 }
253
254 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
255 CfResult ret = x509CrlEntry->getCertIssuer(x509CrlEntry, blob);
256 if (ret != CF_SUCCESS) {
257 napi_throw(env, CertGenerateBusinessError(env, ret, "get subject name failed"));
258 LOGE("get cert issuer failed!");
259 CfFree(blob);
260 blob = nullptr;
261 return nullptr;
262 }
263 napi_value returnValue = CertConvertBlobToNapiValue(env, blob);
264 CfBlobDataFree(blob);
265 CfFree(blob);
266 blob = nullptr;
267 return returnValue;
268 }
269
GetRevocationDate(napi_env env,napi_callback_info info)270 napi_value NapiX509CrlEntry::GetRevocationDate(napi_env env, napi_callback_info info)
271 {
272 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
273 CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
274 if (blob == nullptr) {
275 LOGE("malloc blob failed!");
276 return nullptr;
277 }
278 CfResult ret = x509CrlEntry->getRevocationDate(x509CrlEntry, blob);
279 if (ret != CF_SUCCESS) {
280 napi_throw(env, CertGenerateBusinessError(env, ret, "get revocation date failed"));
281 LOGE("get revocation date failed!");
282 CfFree(blob);
283 blob = nullptr;
284 return nullptr;
285 }
286 napi_value returnDate = nullptr;
287 uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
288 napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &returnDate);
289 CfBlobDataFree(blob);
290 CfFree(blob);
291 blob = nullptr;
292 return returnDate;
293 }
294
GetExtensions(napi_env env,napi_callback_info info)295 napi_value NapiX509CrlEntry::GetExtensions(napi_env env, napi_callback_info info)
296 {
297 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
298 CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
299 if (blob == nullptr) {
300 LOGE("malloc blob failed!");
301 return nullptr;
302 }
303 CfResult result = x509CrlEntry->getExtensions(x509CrlEntry, blob);
304 if (result != CF_SUCCESS) {
305 napi_throw(env, CertGenerateBusinessError(env, result, "get extensions failed"));
306 LOGE("getExtensions failed!");
307 CfFree(blob);
308 blob = nullptr;
309 return nullptr;
310 }
311 napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
312 CfBlobDataFree(blob);
313 CfFree(blob);
314 blob = nullptr;
315 return returnBlob;
316 }
317
HasExtensions(napi_env env,napi_callback_info info)318 napi_value NapiX509CrlEntry::HasExtensions(napi_env env, napi_callback_info info)
319 {
320 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
321 bool boolResult = false;
322 CfResult result = x509CrlEntry->hasExtensions(x509CrlEntry, &boolResult);
323 if (result != CF_SUCCESS) {
324 napi_throw(env, CertGenerateBusinessError(env, result, "has extensions failed"));
325 LOGE("hasExtensions failed!");
326 return nullptr;
327 }
328 napi_value ret = nullptr;
329 napi_get_boolean(env, boolResult, &ret);
330 return ret;
331 }
332
ToString(napi_env env,napi_callback_info info)333 napi_value NapiX509CrlEntry::ToString(napi_env env, napi_callback_info info)
334 {
335 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
336 CfBlob blob = { 0, nullptr };
337 CfResult result = x509CrlEntry->toString(x509CrlEntry, &blob);
338 if (result != CF_SUCCESS) {
339 LOGE("toString failed!");
340 napi_throw(env, CertGenerateBusinessError(env, result, "toString failed"));
341 return nullptr;
342 }
343 napi_value returnBlob = nullptr;
344 napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnBlob);
345 CfBlobDataFree(&blob);
346 return returnBlob;
347 }
348
HashCode(napi_env env,napi_callback_info info)349 napi_value NapiX509CrlEntry::HashCode(napi_env env, napi_callback_info info)
350 {
351 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
352 CfBlob blob = { 0, nullptr };
353 CfResult result = x509CrlEntry->hashCode(x509CrlEntry, &blob);
354 if (result != CF_SUCCESS) {
355 LOGE("HashCode failed!");
356 napi_throw(env, CertGenerateBusinessError(env, result, "HashCode failed"));
357 return nullptr;
358 }
359 napi_value returnBlob = ConvertBlobToUint8ArrNapiValue(env, &blob);
360 CfBlobDataFree(&blob);
361 return returnBlob;
362 }
363
CreateCertExtsJSInstance(napi_env env)364 static napi_value CreateCertExtsJSInstance(napi_env env)
365 {
366 napi_value constructor = nullptr;
367 napi_value instance = nullptr;
368 napi_get_reference_value(env, NapiCertExtension::classRef_, &constructor);
369 napi_new_instance(env, constructor, 0, nullptr, &instance);
370 return instance;
371 }
372
BuildCertExtsObject(napi_env env,CfEncodingBlob * encodingBlob)373 static napi_value BuildCertExtsObject(napi_env env, CfEncodingBlob *encodingBlob)
374 {
375 CfObject *extsObj = nullptr;
376 int32_t res = CfCreate(CF_OBJ_TYPE_EXTENSION, encodingBlob, &extsObj);
377 if (res != CF_SUCCESS) {
378 LOGE("CfCreate error!");
379 return nullptr;
380 }
381 napi_value jsObject = CreateCertExtsJSInstance(env);
382 NapiCertExtension *napiObject = new (std::nothrow) NapiCertExtension(extsObj);
383 if (napiObject == nullptr) {
384 LOGE("Failed to create napi extension class");
385 extsObj->destroy(&(extsObj));
386 return nullptr;
387 }
388 napi_wrap(
389 env, jsObject, napiObject,
390 [](napi_env env, void *data, void *hint) {
391 NapiCertExtension *certExts = static_cast<NapiCertExtension *>(data);
392 delete certExts;
393 return;
394 }, nullptr, nullptr);
395 return jsObject;
396 }
397
GetExtensionsObject(napi_env env,napi_callback_info info)398 napi_value NapiX509CrlEntry::GetExtensionsObject(napi_env env, napi_callback_info info)
399 {
400 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
401 CfBlob blob = { 0, nullptr };
402 CfResult result = x509CrlEntry->getExtensionsObject(x509CrlEntry, &blob);
403 if (result != CF_SUCCESS) {
404 LOGE("get Extensions Object failed!");
405 napi_throw(env, CertGenerateBusinessError(env, result, "get Extensions Object failed"));
406 return nullptr;
407 }
408
409 CfEncodingBlob *encodingBlob = static_cast<CfEncodingBlob *>(CfMalloc(sizeof(CfEncodingBlob), 0));
410 if (encodingBlob == nullptr) {
411 LOGE("malloc encoding blob failed!");
412 CfBlobDataFree(&blob);
413 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "CfMalloc failed"));
414 return nullptr;
415 }
416 if (!ConvertBlobToEncodingBlob(blob, encodingBlob)) {
417 LOGE("ConvertBlobToEncodingBlob failed!");
418 CfBlobDataFree(&blob);
419 CfFree(encodingBlob);
420 encodingBlob = nullptr;
421 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_CRYPTO_OPERATION, "ConvertBlobToEncodingBlob failed"));
422 return nullptr;
423 }
424 CfBlobDataFree(&blob);
425
426 napi_value object = BuildCertExtsObject(env, encodingBlob);
427 CfEncodingBlobDataFree(encodingBlob);
428 CfFree(encodingBlob);
429 encodingBlob = nullptr;
430 if (object == nullptr) {
431 LOGE("BuildCertExtsObject failed!");
432 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "BuildCertExtsObject failed"));
433 return nullptr;
434 }
435
436 return object;
437 }
438
GetCertIssuerX500DistinguishedName(napi_env env,napi_callback_info info)439 napi_value NapiX509CrlEntry::GetCertIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
440 {
441 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
442 CfBlob blob = { 0, nullptr };
443 CfResult result = x509CrlEntry->getCertIssuer(x509CrlEntry, &blob);
444 if (result != CF_SUCCESS) {
445 LOGE("getIssuerDN failed!");
446 napi_throw(env, CertGenerateBusinessError(env, result, "get issuer name failed"));
447 return nullptr;
448 }
449 HcfX509DistinguishedName *x509Name = nullptr;
450 CfResult ret = HcfX509DistinguishedNameCreate(&blob, true, &x509Name);
451 CfBlobDataFree(&blob);
452 if (ret != CF_SUCCESS) {
453 LOGE("HcfX509DistinguishedNameCreate failed");
454 napi_throw(env, CertGenerateBusinessError(env, ret, "HcfX509DistinguishedNameCreate failed"));
455 return nullptr;
456 }
457 napi_value instance = NapiX509DistinguishedName::CreateX509DistinguishedName(env);
458 NapiX509DistinguishedName *x509NameClass = new (std::nothrow) NapiX509DistinguishedName(x509Name);
459 if (x509NameClass == nullptr) {
460 LOGE("Failed to create a NapiX509DistinguishedName class");
461 CfObjDestroy(x509Name);
462 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "NapiX509DistinguishedName new failed"));
463 return nullptr;
464 }
465 napi_wrap(
466 env, instance, x509NameClass,
467 [](napi_env env, void *data, void *hint) {
468 NapiX509DistinguishedName *nameClass = static_cast<NapiX509DistinguishedName *>(data);
469 delete nameClass;
470 return;
471 }, nullptr, nullptr);
472 return instance;
473 }
474
NapiGetEncoded(napi_env env,napi_callback_info info)475 static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
476 {
477 napi_value thisVar = nullptr;
478 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
479 NapiX509CrlEntry *x509CrlEntry = nullptr;
480 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
481 if (x509CrlEntry == nullptr) {
482 LOGE("x509CrlEntry is nullptr!");
483 return nullptr;
484 }
485 return x509CrlEntry->GetEncoded(env, info);
486 }
487
NapiCrlEntryGetSerialNumber(napi_env env,napi_callback_info info)488 static napi_value NapiCrlEntryGetSerialNumber(napi_env env, napi_callback_info info)
489 {
490 napi_value thisVar = nullptr;
491 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
492 NapiX509CrlEntry *x509CrlEntry = nullptr;
493 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
494 if (x509CrlEntry == nullptr) {
495 LOGE("x509CrlEntry is nullptr!");
496 return nullptr;
497 }
498 return x509CrlEntry->GetCrlEntrySerialNumber(env, info);
499 }
500
NapiCRLEntryGetSerialNumber(napi_env env,napi_callback_info info)501 static napi_value NapiCRLEntryGetSerialNumber(napi_env env, napi_callback_info info)
502 {
503 napi_value thisVar = nullptr;
504 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
505 NapiX509CrlEntry *x509CrlEntry = nullptr;
506 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
507 if (x509CrlEntry == nullptr) {
508 LOGE("x509CrlEntry is nullptr!");
509 return nullptr;
510 }
511 return x509CrlEntry->GetCRLEntrySerialNumber(env, info);
512 }
513
NapiGetCertificateIssuer(napi_env env,napi_callback_info info)514 static napi_value NapiGetCertificateIssuer(napi_env env, napi_callback_info info)
515 {
516 napi_value thisVar = nullptr;
517 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
518 NapiX509CrlEntry *x509CrlEntry = nullptr;
519 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
520 if (x509CrlEntry == nullptr) {
521 LOGE("x509CrlEntry is nullptr!");
522 return nullptr;
523 }
524 return x509CrlEntry->GetCertificateIssuer(env, info);
525 }
526
NapiGetRevocationDate(napi_env env,napi_callback_info info)527 static napi_value NapiGetRevocationDate(napi_env env, napi_callback_info info)
528 {
529 napi_value thisVar = nullptr;
530 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
531 NapiX509CrlEntry *x509CrlEntry = nullptr;
532 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
533 if (x509CrlEntry == nullptr) {
534 LOGE("x509CrlEntry is nullptr!");
535 return nullptr;
536 }
537 return x509CrlEntry->GetRevocationDate(env, info);
538 }
539
NapiGetExtensions(napi_env env,napi_callback_info info)540 static napi_value NapiGetExtensions(napi_env env, napi_callback_info info)
541 {
542 napi_value thisVar = nullptr;
543 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
544 NapiX509CrlEntry *x509CrlEntry = nullptr;
545 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
546 if (x509CrlEntry == nullptr) {
547 LOGE("x509CrlEntry is nullptr!");
548 return nullptr;
549 }
550 return x509CrlEntry->GetExtensions(env, info);
551 }
552
NapiHasExtensions(napi_env env,napi_callback_info info)553 static napi_value NapiHasExtensions(napi_env env, napi_callback_info info)
554 {
555 napi_value thisVar = nullptr;
556 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
557 NapiX509CrlEntry *x509CrlEntry = nullptr;
558 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
559 if (x509CrlEntry == nullptr) {
560 LOGE("x509CrlEntry is nullptr!");
561 return nullptr;
562 }
563 return x509CrlEntry->HasExtensions(env, info);
564 }
565
NapiToString(napi_env env,napi_callback_info info)566 static napi_value NapiToString(napi_env env, napi_callback_info info)
567 {
568 napi_value thisVar = nullptr;
569 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
570 NapiX509CrlEntry *x509CrlEntry = nullptr;
571 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
572 if (x509CrlEntry == nullptr) {
573 LOGE("x509CrlEntry is nullptr!");
574 return nullptr;
575 }
576 return x509CrlEntry->ToString(env, info);
577 }
578
NapiHashCode(napi_env env,napi_callback_info info)579 static napi_value NapiHashCode(napi_env env, napi_callback_info info)
580 {
581 napi_value thisVar = nullptr;
582 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
583 NapiX509CrlEntry *x509CrlEntry = nullptr;
584 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
585 if (x509CrlEntry == nullptr) {
586 LOGE("x509CrlEntry is nullptr!");
587 return nullptr;
588 }
589 return x509CrlEntry->HashCode(env, info);
590 }
591
NapiGetExtensionsObject(napi_env env,napi_callback_info info)592 static napi_value NapiGetExtensionsObject(napi_env env, napi_callback_info info)
593 {
594 napi_value thisVar = nullptr;
595 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
596 NapiX509CrlEntry *x509CrlEntry = nullptr;
597 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
598 if (x509CrlEntry == nullptr) {
599 LOGE("x509CrlEntry is nullptr!");
600 return nullptr;
601 }
602 return x509CrlEntry->GetExtensionsObject(env, info);
603 }
604
NapiGetCertIssuerX500DistinguishedName(napi_env env,napi_callback_info info)605 static napi_value NapiGetCertIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
606 {
607 napi_value thisVar = nullptr;
608 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
609 NapiX509CrlEntry *x509CrlEntry = nullptr;
610 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
611 if (x509CrlEntry == nullptr) {
612 LOGE("x509CrlEntry is nullptr!");
613 return nullptr;
614 }
615 return x509CrlEntry->GetCertIssuerX500DistinguishedName(env, info);
616 }
617
X509CrlEntryConstructor(napi_env env,napi_callback_info info)618 static napi_value X509CrlEntryConstructor(napi_env env, napi_callback_info info)
619 {
620 napi_value thisVar = nullptr;
621 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
622 return thisVar;
623 }
624
DefineX509CrlEntryJSClass(napi_env env,std::string className)625 void NapiX509CrlEntry::DefineX509CrlEntryJSClass(napi_env env, std::string className)
626 {
627 if (className == std::string("X509CrlEntry")) {
628 napi_property_descriptor x509CrlEntryDesc[] = {
629 DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
630 DECLARE_NAPI_FUNCTION("getSerialNumber", NapiCrlEntryGetSerialNumber),
631 DECLARE_NAPI_FUNCTION("getCertIssuer", NapiGetCertificateIssuer),
632 DECLARE_NAPI_FUNCTION("getRevocationDate", NapiGetRevocationDate),
633 DECLARE_NAPI_FUNCTION("toString", NapiToString),
634 DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
635 DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
636 DECLARE_NAPI_FUNCTION("getCertIssuerX500DistinguishedName", NapiGetCertIssuerX500DistinguishedName),
637 };
638 napi_value constructor = nullptr;
639 napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlEntryConstructor, nullptr,
640 sizeof(x509CrlEntryDesc) / sizeof(x509CrlEntryDesc[0]), x509CrlEntryDesc, &constructor);
641 napi_create_reference(env, constructor, 1, &classCrlRef_);
642 } else {
643 napi_property_descriptor x509CrlEntryDesc[] = {
644 DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
645 DECLARE_NAPI_FUNCTION("getSerialNumber", NapiCRLEntryGetSerialNumber),
646 DECLARE_NAPI_FUNCTION("getCertIssuer", NapiGetCertificateIssuer),
647 DECLARE_NAPI_FUNCTION("getRevocationDate", NapiGetRevocationDate),
648 DECLARE_NAPI_FUNCTION("getExtensions", NapiGetExtensions),
649 DECLARE_NAPI_FUNCTION("hasExtensions", NapiHasExtensions),
650 DECLARE_NAPI_FUNCTION("toString", NapiToString),
651 DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
652 DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
653 DECLARE_NAPI_FUNCTION("getCertIssuerX500DistinguishedName", NapiGetCertIssuerX500DistinguishedName),
654 };
655 napi_value constructor = nullptr;
656 napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlEntryConstructor, nullptr,
657 sizeof(x509CrlEntryDesc) / sizeof(x509CrlEntryDesc[0]), x509CrlEntryDesc, &constructor);
658 napi_create_reference(env, constructor, 1, &classCRLRef_);
659 }
660 }
661
CreateX509CrlEntry(napi_env env,std::string className)662 napi_value NapiX509CrlEntry::CreateX509CrlEntry(napi_env env, std::string className)
663 {
664 napi_value constructor = nullptr;
665 napi_value instance = nullptr;
666 if (className == std::string("X509CrlEntry")) {
667 napi_get_reference_value(env, classCrlRef_, &constructor);
668 } else {
669 napi_get_reference_value(env, classCRLRef_, &constructor);
670 }
671 napi_new_instance(env, constructor, 0, nullptr, &instance);
672 return instance;
673 }
674 } // namespace CertFramework
675 } // namespace OHOS
676