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
GetCertificateIssuerEx(napi_env env,napi_callback_info info,CfEncodinigType encodingType)270 napi_value NapiX509CrlEntry::GetCertificateIssuerEx(napi_env env, napi_callback_info info, CfEncodinigType encodingType)
271 {
272 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
273 CfBlob blob = { 0, nullptr };
274 CfResult ret = x509CrlEntry->getCertIssuerEx(x509CrlEntry, encodingType, &blob);
275 if (ret != CF_SUCCESS) {
276 napi_throw(env, CertGenerateBusinessError(env, ret, "getCertIssuerEx failed!"));
277 LOGE("getcertissuerEx failed!");
278 return nullptr;
279 }
280 napi_value returnValue = nullptr;
281 napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnValue);
282 CfBlobDataFree(&blob);
283 return returnValue;
284 }
285
GetRevocationDate(napi_env env,napi_callback_info info)286 napi_value NapiX509CrlEntry::GetRevocationDate(napi_env env, napi_callback_info info)
287 {
288 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
289 CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
290 if (blob == nullptr) {
291 LOGE("malloc blob failed!");
292 return nullptr;
293 }
294 CfResult ret = x509CrlEntry->getRevocationDate(x509CrlEntry, blob);
295 if (ret != CF_SUCCESS) {
296 napi_throw(env, CertGenerateBusinessError(env, ret, "get revocation date failed"));
297 LOGE("get revocation date failed!");
298 CfFree(blob);
299 blob = nullptr;
300 return nullptr;
301 }
302 napi_value returnDate = nullptr;
303 uint32_t size = blob->data[blob->size - 1] == '\0' ? blob->size - 1 : blob->size;
304 napi_create_string_utf8(env, reinterpret_cast<char *>(blob->data), size, &returnDate);
305 CfBlobDataFree(blob);
306 CfFree(blob);
307 blob = nullptr;
308 return returnDate;
309 }
310
GetExtensions(napi_env env,napi_callback_info info)311 napi_value NapiX509CrlEntry::GetExtensions(napi_env env, napi_callback_info info)
312 {
313 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
314 CfBlob *blob = reinterpret_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
315 if (blob == nullptr) {
316 LOGE("malloc blob failed!");
317 return nullptr;
318 }
319 CfResult result = x509CrlEntry->getExtensions(x509CrlEntry, blob);
320 if (result != CF_SUCCESS) {
321 napi_throw(env, CertGenerateBusinessError(env, result, "get extensions failed"));
322 LOGE("getExtensions failed!");
323 CfFree(blob);
324 blob = nullptr;
325 return nullptr;
326 }
327 napi_value returnBlob = CertConvertBlobToNapiValue(env, blob);
328 CfBlobDataFree(blob);
329 CfFree(blob);
330 blob = nullptr;
331 return returnBlob;
332 }
333
HasExtensions(napi_env env,napi_callback_info info)334 napi_value NapiX509CrlEntry::HasExtensions(napi_env env, napi_callback_info info)
335 {
336 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
337 bool boolResult = false;
338 CfResult result = x509CrlEntry->hasExtensions(x509CrlEntry, &boolResult);
339 if (result != CF_SUCCESS) {
340 napi_throw(env, CertGenerateBusinessError(env, result, "has extensions failed"));
341 LOGE("hasExtensions failed!");
342 return nullptr;
343 }
344 napi_value ret = nullptr;
345 napi_get_boolean(env, boolResult, &ret);
346 return ret;
347 }
348
ToString(napi_env env,napi_callback_info info)349 napi_value NapiX509CrlEntry::ToString(napi_env env, napi_callback_info info)
350 {
351 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
352 CfBlob blob = { 0, nullptr };
353 CfResult result = x509CrlEntry->toString(x509CrlEntry, &blob);
354 if (result != CF_SUCCESS) {
355 LOGE("toString failed!");
356 napi_throw(env, CertGenerateBusinessError(env, result, "toString failed"));
357 return nullptr;
358 }
359 napi_value returnBlob = nullptr;
360 napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &returnBlob);
361 CfBlobDataFree(&blob);
362 return returnBlob;
363 }
364
HashCode(napi_env env,napi_callback_info info)365 napi_value NapiX509CrlEntry::HashCode(napi_env env, napi_callback_info info)
366 {
367 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
368 CfBlob blob = { 0, nullptr };
369 CfResult result = x509CrlEntry->hashCode(x509CrlEntry, &blob);
370 if (result != CF_SUCCESS) {
371 LOGE("HashCode failed!");
372 napi_throw(env, CertGenerateBusinessError(env, result, "HashCode failed"));
373 return nullptr;
374 }
375 napi_value returnBlob = ConvertBlobToUint8ArrNapiValue(env, &blob);
376 CfBlobDataFree(&blob);
377 return returnBlob;
378 }
379
CreateCertExtsJSInstance(napi_env env)380 static napi_value CreateCertExtsJSInstance(napi_env env)
381 {
382 napi_value constructor = nullptr;
383 napi_value instance = nullptr;
384 napi_get_reference_value(env, NapiCertExtension::classRef_, &constructor);
385 napi_new_instance(env, constructor, 0, nullptr, &instance);
386 return instance;
387 }
388
BuildCertExtsObject(napi_env env,CfEncodingBlob * encodingBlob)389 static napi_value BuildCertExtsObject(napi_env env, CfEncodingBlob *encodingBlob)
390 {
391 CfObject *extsObj = nullptr;
392 int32_t res = CfCreate(CF_OBJ_TYPE_EXTENSION, encodingBlob, &extsObj);
393 if (res != CF_SUCCESS) {
394 LOGE("CfCreate error!");
395 return nullptr;
396 }
397 napi_value jsObject = CreateCertExtsJSInstance(env);
398 NapiCertExtension *napiObject = new (std::nothrow) NapiCertExtension(extsObj);
399 if (napiObject == nullptr) {
400 LOGE("Failed to create napi extension class");
401 extsObj->destroy(&(extsObj));
402 extsObj = nullptr;
403 return nullptr;
404 }
405 napi_status status = napi_wrap(
406 env, jsObject, napiObject,
407 [](napi_env env, void *data, void *hint) {
408 NapiCertExtension *certExts = static_cast<NapiCertExtension *>(data);
409 delete certExts;
410 return;
411 }, nullptr, nullptr);
412 if (status != napi_ok) {
413 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
414 LOGE("failed to wrap obj!");
415 delete napiObject;
416 return nullptr;
417 }
418 return jsObject;
419 }
420
GetExtensionsObject(napi_env env,napi_callback_info info)421 napi_value NapiX509CrlEntry::GetExtensionsObject(napi_env env, napi_callback_info info)
422 {
423 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
424 CfBlob blob = { 0, nullptr };
425 CfResult result = x509CrlEntry->getExtensionsObject(x509CrlEntry, &blob);
426 if (result != CF_SUCCESS) {
427 LOGE("get Extensions Object failed!");
428 napi_throw(env, CertGenerateBusinessError(env, result, "get Extensions Object failed"));
429 return nullptr;
430 }
431
432 CfEncodingBlob *encodingBlob = static_cast<CfEncodingBlob *>(CfMalloc(sizeof(CfEncodingBlob), 0));
433 if (encodingBlob == nullptr) {
434 LOGE("malloc encoding blob failed!");
435 CfBlobDataFree(&blob);
436 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "CfMalloc failed"));
437 return nullptr;
438 }
439 if (!ConvertBlobToEncodingBlob(blob, encodingBlob)) {
440 LOGE("ConvertBlobToEncodingBlob failed!");
441 CfBlobDataFree(&blob);
442 CfFree(encodingBlob);
443 encodingBlob = nullptr;
444 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_CRYPTO_OPERATION, "ConvertBlobToEncodingBlob failed"));
445 return nullptr;
446 }
447 CfBlobDataFree(&blob);
448
449 napi_value object = BuildCertExtsObject(env, encodingBlob);
450 CfEncodingBlobDataFree(encodingBlob);
451 CfFree(encodingBlob);
452 encodingBlob = nullptr;
453 if (object == nullptr) {
454 LOGE("BuildCertExtsObject failed!");
455 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "BuildCertExtsObject failed"));
456 return nullptr;
457 }
458
459 return object;
460 }
461
GetCertIssuerX500DistinguishedName(napi_env env,napi_callback_info info)462 napi_value NapiX509CrlEntry::GetCertIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
463 {
464 HcfX509CrlEntry *x509CrlEntry = GetX509CrlEntry();
465 CfBlob blob = { 0, nullptr };
466 CfResult result = x509CrlEntry->getCertIssuer(x509CrlEntry, &blob);
467 if (result != CF_SUCCESS) {
468 LOGE("getIssuerDN failed!");
469 napi_throw(env, CertGenerateBusinessError(env, result, "get issuer name failed"));
470 return nullptr;
471 }
472 HcfX509DistinguishedName *x509Name = nullptr;
473 CfResult ret = HcfX509DistinguishedNameCreate(&blob, true, &x509Name);
474 CfBlobDataFree(&blob);
475 if (ret != CF_SUCCESS) {
476 LOGE("HcfX509DistinguishedNameCreate failed");
477 napi_throw(env, CertGenerateBusinessError(env, ret, "HcfX509DistinguishedNameCreate failed"));
478 return nullptr;
479 }
480
481 result = x509CrlEntry->getCertIssuerDer(x509CrlEntry, &blob);
482 if (result != CF_SUCCESS) {
483 LOGE("getCertIssuerDer failed!");
484 CfObjDestroy(x509Name);
485 napi_throw(env, CertGenerateBusinessError(env, result, "getCertIssuerDer failed"));
486 return nullptr;
487 }
488 HcfX509DistinguishedName *x509NameUtf8 = nullptr;
489 ret = HcfX509DistinguishedNameCreate(&blob, false, &x509NameUtf8);
490 CfBlobDataFree(&blob);
491 if (ret != CF_SUCCESS) {
492 LOGE("HcfX509DistinguishedNameCreate failed");
493 CfObjDestroy(x509Name);
494 napi_throw(env, CertGenerateBusinessError(env, ret, "HcfX509DistinguishedNameCreate failed"));
495 return nullptr;
496 }
497
498 napi_value instance = ConstructX509DistinguishedName(x509Name, x509NameUtf8, env);
499 return instance;
500 }
501
NapiGetEncoded(napi_env env,napi_callback_info info)502 static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
503 {
504 napi_value thisVar = nullptr;
505 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
506 NapiX509CrlEntry *x509CrlEntry = nullptr;
507 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
508 if (x509CrlEntry == nullptr) {
509 LOGE("x509CrlEntry is nullptr!");
510 return nullptr;
511 }
512 return x509CrlEntry->GetEncoded(env, info);
513 }
514
NapiCrlEntryGetSerialNumber(napi_env env,napi_callback_info info)515 static napi_value NapiCrlEntryGetSerialNumber(napi_env env, napi_callback_info info)
516 {
517 napi_value thisVar = nullptr;
518 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
519 NapiX509CrlEntry *x509CrlEntry = nullptr;
520 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
521 if (x509CrlEntry == nullptr) {
522 LOGE("x509CrlEntry is nullptr!");
523 return nullptr;
524 }
525 return x509CrlEntry->GetCrlEntrySerialNumber(env, info);
526 }
527
NapiCRLEntryGetSerialNumber(napi_env env,napi_callback_info info)528 static napi_value NapiCRLEntryGetSerialNumber(napi_env env, napi_callback_info info)
529 {
530 napi_value thisVar = nullptr;
531 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
532 NapiX509CrlEntry *x509CrlEntry = nullptr;
533 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
534 if (x509CrlEntry == nullptr) {
535 LOGE("x509CrlEntry is nullptr!");
536 return nullptr;
537 }
538 return x509CrlEntry->GetCRLEntrySerialNumber(env, info);
539 }
540
NapiGetCertificateIssuer(napi_env env,napi_callback_info info)541 static napi_value NapiGetCertificateIssuer(napi_env env, napi_callback_info info)
542 {
543 size_t argc = ARGS_SIZE_ONE;
544 napi_value thisVar = nullptr;
545 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
546 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
547 if (argc != 0 && argc != ARGS_SIZE_ONE) {
548 napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument num!"));
549 LOGE("wrong argument num!");
550 return nullptr;
551 }
552 NapiX509CrlEntry *x509CrlEntry = nullptr;
553 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
554 if (x509CrlEntry == nullptr) {
555 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "x509CrlEntry is nullptr!"));
556 LOGE("x509CrlEntry is nullptr!");
557 return nullptr;
558 }
559 if (argc == ARGS_SIZE_ONE) {
560 napi_valuetype valueType;
561 napi_typeof(env, argv[PARAM0], &valueType);
562 if ((valueType != napi_number)) {
563 napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument type!"));
564 LOGE("wrong argument type!");
565 return nullptr;
566 }
567 CfEncodinigType encodingType;
568 if (napi_get_value_uint32(env, argv[PARAM0], reinterpret_cast<uint32_t *>(&encodingType)) != napi_ok) {
569 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "napi_get_value_uint32 failed!"));
570 LOGE("napi_get_value_uint32 failed!");
571 return nullptr;
572 }
573 return x509CrlEntry->GetCertificateIssuerEx(env, info, encodingType);
574 }
575 return x509CrlEntry->GetCertificateIssuer(env, info);
576 }
577
NapiGetRevocationDate(napi_env env,napi_callback_info info)578 static napi_value NapiGetRevocationDate(napi_env env, napi_callback_info info)
579 {
580 napi_value thisVar = nullptr;
581 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
582 NapiX509CrlEntry *x509CrlEntry = nullptr;
583 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
584 if (x509CrlEntry == nullptr) {
585 LOGE("x509CrlEntry is nullptr!");
586 return nullptr;
587 }
588 return x509CrlEntry->GetRevocationDate(env, info);
589 }
590
NapiGetExtensions(napi_env env,napi_callback_info info)591 static napi_value NapiGetExtensions(napi_env env, napi_callback_info info)
592 {
593 napi_value thisVar = nullptr;
594 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
595 NapiX509CrlEntry *x509CrlEntry = nullptr;
596 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
597 if (x509CrlEntry == nullptr) {
598 LOGE("x509CrlEntry is nullptr!");
599 return nullptr;
600 }
601 return x509CrlEntry->GetExtensions(env, info);
602 }
603
NapiHasExtensions(napi_env env,napi_callback_info info)604 static napi_value NapiHasExtensions(napi_env env, napi_callback_info info)
605 {
606 napi_value thisVar = nullptr;
607 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
608 NapiX509CrlEntry *x509CrlEntry = nullptr;
609 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
610 if (x509CrlEntry == nullptr) {
611 LOGE("x509CrlEntry is nullptr!");
612 return nullptr;
613 }
614 return x509CrlEntry->HasExtensions(env, info);
615 }
616
NapiToString(napi_env env,napi_callback_info info)617 static napi_value NapiToString(napi_env env, napi_callback_info info)
618 {
619 napi_value thisVar = nullptr;
620 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
621 NapiX509CrlEntry *x509CrlEntry = nullptr;
622 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
623 if (x509CrlEntry == nullptr) {
624 LOGE("x509CrlEntry is nullptr!");
625 return nullptr;
626 }
627 return x509CrlEntry->ToString(env, info);
628 }
629
NapiHashCode(napi_env env,napi_callback_info info)630 static napi_value NapiHashCode(napi_env env, napi_callback_info info)
631 {
632 napi_value thisVar = nullptr;
633 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
634 NapiX509CrlEntry *x509CrlEntry = nullptr;
635 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
636 if (x509CrlEntry == nullptr) {
637 LOGE("x509CrlEntry is nullptr!");
638 return nullptr;
639 }
640 return x509CrlEntry->HashCode(env, info);
641 }
642
NapiGetExtensionsObject(napi_env env,napi_callback_info info)643 static napi_value NapiGetExtensionsObject(napi_env env, napi_callback_info info)
644 {
645 napi_value thisVar = nullptr;
646 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
647 NapiX509CrlEntry *x509CrlEntry = nullptr;
648 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
649 if (x509CrlEntry == nullptr) {
650 LOGE("x509CrlEntry is nullptr!");
651 return nullptr;
652 }
653 return x509CrlEntry->GetExtensionsObject(env, info);
654 }
655
NapiGetCertIssuerX500DistinguishedName(napi_env env,napi_callback_info info)656 static napi_value NapiGetCertIssuerX500DistinguishedName(napi_env env, napi_callback_info info)
657 {
658 napi_value thisVar = nullptr;
659 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
660 NapiX509CrlEntry *x509CrlEntry = nullptr;
661 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509CrlEntry));
662 if (x509CrlEntry == nullptr) {
663 LOGE("x509CrlEntry is nullptr!");
664 return nullptr;
665 }
666 return x509CrlEntry->GetCertIssuerX500DistinguishedName(env, info);
667 }
668
X509CrlEntryConstructor(napi_env env,napi_callback_info info)669 static napi_value X509CrlEntryConstructor(napi_env env, napi_callback_info info)
670 {
671 napi_value thisVar = nullptr;
672 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
673 return thisVar;
674 }
675
DefineX509CrlEntryJSClass(napi_env env,std::string className)676 void NapiX509CrlEntry::DefineX509CrlEntryJSClass(napi_env env, std::string className)
677 {
678 if (className == std::string("X509CrlEntry")) {
679 napi_property_descriptor x509CrlEntryDesc[] = {
680 DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
681 DECLARE_NAPI_FUNCTION("getSerialNumber", NapiCrlEntryGetSerialNumber),
682 DECLARE_NAPI_FUNCTION("getCertIssuer", NapiGetCertificateIssuer),
683 DECLARE_NAPI_FUNCTION("getRevocationDate", NapiGetRevocationDate),
684 DECLARE_NAPI_FUNCTION("toString", NapiToString),
685 DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
686 DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
687 DECLARE_NAPI_FUNCTION("getCertIssuerX500DistinguishedName", NapiGetCertIssuerX500DistinguishedName),
688 };
689 napi_value constructor = nullptr;
690 napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlEntryConstructor, nullptr,
691 sizeof(x509CrlEntryDesc) / sizeof(x509CrlEntryDesc[0]), x509CrlEntryDesc, &constructor);
692 napi_create_reference(env, constructor, 1, &classCrlRef_);
693 } else {
694 napi_property_descriptor x509CrlEntryDesc[] = {
695 DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
696 DECLARE_NAPI_FUNCTION("getSerialNumber", NapiCRLEntryGetSerialNumber),
697 DECLARE_NAPI_FUNCTION("getCertIssuer", NapiGetCertificateIssuer),
698 DECLARE_NAPI_FUNCTION("getRevocationDate", NapiGetRevocationDate),
699 DECLARE_NAPI_FUNCTION("getExtensions", NapiGetExtensions),
700 DECLARE_NAPI_FUNCTION("hasExtensions", NapiHasExtensions),
701 DECLARE_NAPI_FUNCTION("toString", NapiToString),
702 DECLARE_NAPI_FUNCTION("hashCode", NapiHashCode),
703 DECLARE_NAPI_FUNCTION("getExtensionsObject", NapiGetExtensionsObject),
704 DECLARE_NAPI_FUNCTION("getCertIssuerX500DistinguishedName", NapiGetCertIssuerX500DistinguishedName),
705 };
706 napi_value constructor = nullptr;
707 napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, X509CrlEntryConstructor, nullptr,
708 sizeof(x509CrlEntryDesc) / sizeof(x509CrlEntryDesc[0]), x509CrlEntryDesc, &constructor);
709 napi_create_reference(env, constructor, 1, &classCRLRef_);
710 }
711 }
712
CreateX509CrlEntry(napi_env env,std::string className)713 napi_value NapiX509CrlEntry::CreateX509CrlEntry(napi_env env, std::string className)
714 {
715 napi_value constructor = nullptr;
716 napi_value instance = nullptr;
717 if (className == std::string("X509CrlEntry")) {
718 napi_get_reference_value(env, classCrlRef_, &constructor);
719 } else {
720 napi_get_reference_value(env, classCRLRef_, &constructor);
721 }
722 napi_new_instance(env, constructor, 0, nullptr, &instance);
723 return instance;
724 }
725 } // namespace CertFramework
726 } // namespace OHOS
727