• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_cert_cms_generator.h"
17 #include "napi/native_common.h"
18 #include "napi/native_api.h"
19 #include "cf_log.h"
20 #include "cf_memory.h"
21 #include "utils.h"
22 #include "cf_result.h"
23 #include "cf_object_base.h"
24 #include "securec.h"
25 #include "napi_cert_defines.h"
26 #include "napi_cert_utils.h"
27 #include "napi_x509_certificate.h"
28 #include "napi_common.h"
29 
30 namespace OHOS {
31 namespace CertFramework {
32 thread_local napi_ref NapiCertCmsGenerator::classRef_ = nullptr;
33 struct CmsDoFinalCtx {
34     napi_env env = nullptr;
35 
36     napi_deferred deferred = nullptr;
37     napi_value promise = nullptr;
38     napi_async_work asyncWork = nullptr;
39     napi_ref generatorRef = nullptr;
40 
41     HcfCmsGenerator *cmsGenerator = nullptr;
42     CfBlob *content = nullptr;
43     HcfCmsGeneratorOptions *options = nullptr;
44 
45     CfResult errCode = CF_SUCCESS;
46     const char *errMsg = nullptr;
47 
48     CfBlob outBlob = { 0, nullptr };
49 };
50 
FreeCmsSignerOptions(HcfCmsSignerOptions * options)51 static void FreeCmsSignerOptions(HcfCmsSignerOptions *options)
52 {
53     if (options != nullptr) {
54         CfFree(options->mdName);
55         options->mdName = nullptr;
56         CfFree(options);
57         options = nullptr;
58     }
59 }
60 
FreeCmsGeneratorOptions(HcfCmsGeneratorOptions * options)61 static void FreeCmsGeneratorOptions(HcfCmsGeneratorOptions *options)
62 {
63     if (options != nullptr) {
64         CfFree(options);
65         options = nullptr;
66     }
67 }
68 
FreeCmsDoFinalCtx(napi_env env,CmsDoFinalCtx * ctx)69 static void FreeCmsDoFinalCtx(napi_env env, CmsDoFinalCtx *ctx)
70 {
71     if (ctx == nullptr) {
72         return;
73     }
74     if (ctx->asyncWork != nullptr) {
75         napi_delete_async_work(env, ctx->asyncWork);
76     }
77     if (ctx->generatorRef != nullptr) {
78         napi_delete_reference(env, ctx->generatorRef);
79         ctx->generatorRef = nullptr;
80     }
81     if (ctx->content != nullptr) {
82         CfBlobDataFree(ctx->content);
83         ctx->content = nullptr;
84     }
85     if (ctx->options != nullptr) {
86         FreeCmsGeneratorOptions(ctx->options);
87         ctx->options = nullptr;
88     }
89     if (ctx->outBlob.data != nullptr) {
90         CfBlobDataFree(&ctx->outBlob);
91         ctx->outBlob.data = nullptr;
92     }
93     CfFree(ctx);
94 }
95 
NapiCertCmsGenerator(HcfCmsGenerator * certCmsGenerator)96 NapiCertCmsGenerator::NapiCertCmsGenerator(HcfCmsGenerator *certCmsGenerator)
97 {
98     this->cmsGenerator_ = certCmsGenerator;
99 }
100 
~NapiCertCmsGenerator()101 NapiCertCmsGenerator::~NapiCertCmsGenerator()
102 {
103     CfObjDestroy(this->cmsGenerator_);
104 }
105 
AddSigner(napi_env env,napi_callback_info info)106 napi_value NapiCertCmsGenerator::AddSigner(napi_env env, napi_callback_info info)
107 {
108     napi_value thisVar = nullptr;
109     size_t expectedArgc = ARGS_SIZE_THREE;
110     size_t argc = expectedArgc;
111     napi_value argv[ARGS_SIZE_THREE] = { nullptr };
112     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
113     if (argc != expectedArgc) {
114         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument num."));
115         LOGE("wrong argument num. require %{public}zu arguments. [Argc]: %{public}zu!", ARGS_SIZE_THREE, argc);
116         return nullptr;
117     }
118 
119     NapiCertCmsGenerator *napiCmsGenerator = nullptr;
120     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiCmsGenerator));
121     if (status != napi_ok || napiCmsGenerator == nullptr) {
122         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "failed to unwrap napi cms generator obj."));
123         LOGE("failed to unwrap napi cms generator obj.");
124         return nullptr;
125     }
126 
127     HcfCmsGenerator *cmsGenerator = napiCmsGenerator->GetCertCmsGenerator();
128     NapiX509Certificate *napiX509Cert = nullptr;
129     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
130     if (napiX509Cert == nullptr) {
131         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "napiX509Cert is null."));
132         LOGE("napiX509Cert is null!");
133         return nullptr;
134     }
135 
136     PrivateKeyInfo *privateKey = nullptr;
137     if (!GetPrivateKeyInfoFromValue(env, argv[PARAM1], &privateKey)) {
138         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "get private key info from data failed!"));
139         LOGE("get private key info from data failed!");
140         return nullptr;
141     }
142 
143     HcfCmsSignerOptions *options = nullptr;
144     if (!GetCmsSignerOptionsFromValue(env, argv[PARAM2], &options)) {
145         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "get cms signer options from data failed!"));
146         LOGE("get cms signer options from data failed!");
147         FreePrivateKeyInfo(privateKey);
148         return nullptr;
149     }
150 
151     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
152     CfResult ret = cmsGenerator->addSigner(cmsGenerator, &(certificate->base), privateKey, options);
153     FreePrivateKeyInfo(privateKey);
154     FreeCmsSignerOptions(options);
155     if (ret != CF_SUCCESS) {
156         LOGE("add signer fail.");
157         napi_throw(env, CertGenerateBusinessError(env, ret, "add signer fail."));
158         return nullptr;
159     }
160     return NapiGetNull(env);
161 }
162 
NapiAddSigner(napi_env env,napi_callback_info info)163 static napi_value NapiAddSigner(napi_env env, napi_callback_info info)
164 {
165     napi_value thisVar = nullptr;
166     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
167     NapiCertCmsGenerator *cmsGenerator = nullptr;
168     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&cmsGenerator));
169     if (cmsGenerator == nullptr) {
170         LOGE("cmsGenerator is nullptr!");
171         return nullptr;
172     }
173     return cmsGenerator->AddSigner(env, info);
174 }
175 
AddCert(napi_env env,napi_callback_info info)176 napi_value NapiCertCmsGenerator::AddCert(napi_env env, napi_callback_info info)
177 {
178     size_t argc = ARGS_SIZE_ONE;
179     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
180     napi_value thisVar = nullptr;
181     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
182     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, true)) {
183         return nullptr;
184     }
185 
186     NapiX509Certificate *napiX509Cert = nullptr;
187     napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&napiX509Cert));
188     if (napiX509Cert == nullptr) {
189         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "napiX509Cert is null"));
190         LOGE("napiX509Cert is null!");
191         return nullptr;
192     }
193 
194     NapiCertCmsGenerator *napiCmsGenerator = nullptr;
195     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiCmsGenerator));
196     if (status != napi_ok || napiCmsGenerator == nullptr) {
197         LOGE("failed to unwrap napi cms generator obj.");
198         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "failed to unwrap napi cms generator obj."));
199         return nullptr;
200     }
201 
202     HcfCmsGenerator *cmsGenerator = napiCmsGenerator->GetCertCmsGenerator();
203     HcfX509Certificate *certificate = napiX509Cert->GetX509Cert();
204 
205     CfResult ret = cmsGenerator->addCert(cmsGenerator, &(certificate->base));
206     if (ret != CF_SUCCESS) {
207         LOGE("add cert fail.");
208         napi_throw(env, CertGenerateBusinessError(env, ret, "add cert fail."));
209         return nullptr;
210     }
211     napi_value instance = NapiGetNull(env);
212     return instance;
213 }
214 
NapiAddCert(napi_env env,napi_callback_info info)215 static napi_value NapiAddCert(napi_env env, napi_callback_info info)
216 {
217     napi_value thisVar = nullptr;
218     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
219     NapiCertCmsGenerator *cmsGenerator = nullptr;
220     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&cmsGenerator));
221     if (cmsGenerator == nullptr) {
222         LOGE("cmsGenerator is nullptr!");
223         return nullptr;
224     }
225     return cmsGenerator->AddCert(env, info);
226 }
227 
BuildCmsDoFinalCtx(napi_env env,napi_callback_info info,CmsDoFinalCtx * ctx)228 static bool BuildCmsDoFinalCtx(napi_env env, napi_callback_info info, CmsDoFinalCtx *ctx)
229 {
230     napi_value thisVar = nullptr;
231     size_t expectedArgc = ARGS_SIZE_TWO;
232     size_t argc = expectedArgc;
233     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
234     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
235     if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) {
236         LOGE("wrong argument num. require %{public}zu arguments. [Argc]: %{public}zu!", expectedArgc, argc);
237         return false;
238     }
239     NapiCertCmsGenerator *napiCmsGenerator = nullptr;
240     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiCmsGenerator));
241     if (status != napi_ok || napiCmsGenerator == nullptr) {
242         LOGE("failed to unwrap napi cms generator obj.");
243         return false;
244     }
245     ctx->content = CertGetBlobFromUint8ArrJSParams(env, argv[PARAM0]);
246     if (ctx->content == nullptr) {
247         return false;
248     }
249     if (argc == expectedArgc) {
250         if (!GetCmsGeneratorOptionsFromValue(env, argv[PARAM1], &ctx->options)) {
251             return false;
252         }
253     }
254     ctx->cmsGenerator = napiCmsGenerator->GetCertCmsGenerator();
255     if (napi_create_reference(env, thisVar, 1, &ctx->generatorRef) != napi_ok) {
256         LOGE("create generator ref failed!");
257         return false;
258     }
259     napi_create_promise(env, &ctx->deferred, &ctx->promise);
260     return true;
261 }
262 
CmsDoFinalAsyncWorkProcess(napi_env env,void * data)263 static void CmsDoFinalAsyncWorkProcess(napi_env env, void *data)
264 {
265     CmsDoFinalCtx *ctx = static_cast<CmsDoFinalCtx *>(data);
266     ctx->errCode = ctx->cmsGenerator->doFinal(ctx->cmsGenerator, ctx->content, ctx->options, &(ctx->outBlob));
267     if (ctx->errCode != CF_SUCCESS) {
268         LOGE("Cms do final fail.");
269         ctx->errMsg = "Cms do final fail.";
270     }
271 }
272 
ReturnPromiseResult(napi_env env,CmsDoFinalCtx * ctx,napi_value result)273 static void ReturnPromiseResult(napi_env env, CmsDoFinalCtx *ctx, napi_value result)
274 {
275     if (ctx->errCode == CF_SUCCESS) {
276         napi_resolve_deferred(env, ctx->deferred, result);
277     } else {
278         napi_reject_deferred(env, ctx->deferred,
279             CertGenerateBusinessError(env, ctx->errCode, ctx->errMsg));
280     }
281 }
282 
CmsDoFinalAsyncWorkReturn(napi_env env,napi_status status,void * data)283 static void CmsDoFinalAsyncWorkReturn(napi_env env, napi_status status, void *data)
284 {
285     CmsDoFinalCtx *ctx = static_cast<CmsDoFinalCtx *>(data);
286     if (ctx->errCode != CF_SUCCESS) {
287         ReturnPromiseResult(env, ctx, nullptr);
288         FreeCmsDoFinalCtx(env, ctx);
289         return;
290     }
291     napi_value instance = nullptr;
292     if (ctx->options->outFormat == CMS_PEM) {
293         napi_create_string_utf8(env, reinterpret_cast<char *>(ctx->outBlob.data), ctx->outBlob.size, &instance);
294     } else {
295         instance = ConvertBlobToUint8ArrNapiValue(env, &ctx->outBlob);
296     }
297     ReturnPromiseResult(env, ctx, instance);
298     FreeCmsDoFinalCtx(env, ctx);
299 }
300 
NewCmsDoFinalAsyncWork(napi_env env,CmsDoFinalCtx * ctx)301 static napi_value NewCmsDoFinalAsyncWork(napi_env env, CmsDoFinalCtx *ctx)
302 {
303     napi_value resourceName = nullptr;
304     napi_create_string_utf8(env, "doFinal", NAPI_AUTO_LENGTH, &resourceName);
305     napi_create_async_work(
306         env, nullptr, resourceName,
307         [](napi_env env, void *data) {
308             CmsDoFinalAsyncWorkProcess(env, data);
309             return;
310         },
311         [](napi_env env, napi_status status, void *data) {
312             CmsDoFinalAsyncWorkReturn(env, status, data);
313             return;
314         },
315         static_cast<void *>(ctx),
316         &ctx->asyncWork);
317 
318     napi_queue_async_work(env, ctx->asyncWork);
319     return ctx->promise;
320 }
321 
DoFinal(napi_env env,napi_callback_info info)322 napi_value NapiCertCmsGenerator::DoFinal(napi_env env, napi_callback_info info)
323 {
324     CmsDoFinalCtx *ctx = static_cast<CmsDoFinalCtx *>(CfMalloc(sizeof(CmsDoFinalCtx), 0));
325     if (ctx == nullptr) {
326         LOGE("create context fail.");
327         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "create context fail!"));
328         return nullptr;
329     }
330     ctx->options = static_cast<HcfCmsGeneratorOptions *>(CfMalloc(sizeof(HcfCmsGeneratorOptions), 0));
331     if (ctx->options == nullptr) {
332         LOGE("create options fail.");
333         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "create options fail!"));
334         FreeCmsDoFinalCtx(env, ctx);
335         return nullptr;
336     }
337     ctx->options->dataFormat = BINARY;
338     ctx->options->outFormat = CMS_DER;
339     ctx->options->isDetachedContent = false;
340 
341     if (!BuildCmsDoFinalCtx(env, info, ctx)) {
342         LOGE("build context fail.");
343         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "build cms doFinal Ctx fail."));
344         FreeCmsDoFinalCtx(env, ctx);
345         return nullptr;
346     }
347     return NewCmsDoFinalAsyncWork(env, ctx);
348 }
349 
NapiDoFinal(napi_env env,napi_callback_info info)350 static napi_value NapiDoFinal(napi_env env, napi_callback_info info)
351 {
352     napi_value thisVar = nullptr;
353     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
354     NapiCertCmsGenerator *cmsGenerator = nullptr;
355     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&cmsGenerator));
356     if (cmsGenerator == nullptr) {
357         LOGE("cmsGenerator is nullptr!");
358         return nullptr;
359     }
360     return cmsGenerator->DoFinal(env, info);
361 }
362 
GetDoFinalResult(napi_env env,NapiCertCmsGenerator * napiCmsGenerator,CfBlob * content,HcfCmsGeneratorOptions * options)363 static napi_value GetDoFinalResult(napi_env env, NapiCertCmsGenerator *napiCmsGenerator, CfBlob *content,
364     HcfCmsGeneratorOptions *options)
365 {
366     CfBlob outBlob = { 0,  nullptr, };
367     HcfCmsGenerator *cmsGenerator = napiCmsGenerator->GetCertCmsGenerator();
368     CfResult ret = cmsGenerator->doFinal(cmsGenerator, content, options, &outBlob);
369     if (ret != CF_SUCCESS) {
370         LOGE("Cms do final fail.");
371         napi_throw(env, CertGenerateBusinessError(env, ret, "Cms do final fail."));
372         CfBlobDataFree(content);
373         FreeCmsGeneratorOptions(options);
374         return nullptr;
375     }
376     napi_value instance = nullptr;
377     if (options->outFormat == CMS_PEM) {
378         napi_create_string_utf8(env, reinterpret_cast<char *>(outBlob.data), outBlob.size, &instance);
379     } else {
380         instance = ConvertBlobToUint8ArrNapiValue(env, &outBlob);
381     }
382     CfBlobDataFree(&outBlob);
383     CfBlobDataFree(content);
384     FreeCmsGeneratorOptions(options);
385     return instance;
386 }
387 
DoFinalSync(napi_env env,napi_callback_info info)388 napi_value NapiCertCmsGenerator::DoFinalSync(napi_env env, napi_callback_info info)
389 {
390     napi_value thisVar = nullptr;
391     size_t expectedArgc = ARGS_SIZE_TWO;
392     size_t argc = expectedArgc;
393     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
394     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
395     if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) {
396         LOGE("wrong argument num. require %{public}zu arguments. [Argc]: %{public}zu!", expectedArgc, argc);
397         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument num."));
398         return nullptr;
399     }
400     NapiCertCmsGenerator *napiCmsGenerator = nullptr;
401     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiCmsGenerator));
402     if (status != napi_ok || napiCmsGenerator == nullptr) {
403         LOGE("failed to unwrap napi cms generator obj.");
404         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "failed to unwrap napi cms generator obj."));
405         return nullptr;
406     }
407     CfBlob *content = CertGetBlobFromUint8ArrJSParams(env, argv[PARAM0]);
408     if (content == nullptr) {
409         return nullptr;
410     }
411     HcfCmsGeneratorOptions *options = nullptr;
412     options = static_cast<HcfCmsGeneratorOptions *>(CfMalloc(sizeof(HcfCmsGeneratorOptions), 0));
413     if (options == nullptr) {
414         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "malloc options failed!"));
415         CfBlobDataFree(content);
416         return nullptr;
417     }
418     options->dataFormat = BINARY;
419     options->outFormat = CMS_DER;
420     options->isDetachedContent = false;
421     if (argc == expectedArgc) {
422         if (!GetCmsGeneratorOptionsFromValue(env, argv[PARAM1], &options)) {
423             napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS,
424             "GetCmsGeneratorOptionsFromValue failed!"));
425             CfBlobDataFree(content);
426             FreeCmsGeneratorOptions(options);
427             return nullptr;
428         }
429     }
430     return GetDoFinalResult(env, napiCmsGenerator, content, options);
431 }
432 
NapiDoFinalSync(napi_env env,napi_callback_info info)433 static napi_value NapiDoFinalSync(napi_env env, napi_callback_info info)
434 {
435     napi_value thisVar = nullptr;
436     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
437     NapiCertCmsGenerator *cmsGenerator = nullptr;
438     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&cmsGenerator));
439     if (cmsGenerator == nullptr) {
440         LOGE("cmsGenerator is nullptr!");
441         return nullptr;
442     }
443     return cmsGenerator->DoFinalSync(env, info);
444 }
445 
CmsGeneratorConstructor(napi_env env,napi_callback_info info)446 static napi_value CmsGeneratorConstructor(napi_env env, napi_callback_info info)
447 {
448     napi_value thisVar = nullptr;
449     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
450     return thisVar;
451 }
452 
CreateCmsGenerator(napi_env env,napi_callback_info info)453 napi_value NapiCertCmsGenerator::CreateCmsGenerator(napi_env env, napi_callback_info info)
454 {
455     napi_value thisVar = nullptr;
456     size_t argc = ARGS_SIZE_ONE;
457     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
458     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
459 
460     if (argc != ARGS_SIZE_ONE) {
461         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "invalid params count"));
462         LOGE("invalid params count!");
463         return nullptr;
464     }
465 
466     napi_value instance;
467     napi_value constructor = nullptr;
468     napi_get_reference_value(env, classRef_, &constructor);
469     napi_new_instance(env, constructor, argc, argv, &instance);
470 
471     int32_t cmsContentType = 0;
472     if (!CertGetInt32FromJSParams(env, argv[PARAM0], cmsContentType)) {
473         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "get cmsContentType failed!"));
474         LOGE("get cmsContentType failed!");
475         return nullptr;
476     }
477     HcfCmsGenerator *cmsGenerator = nullptr;
478     CfResult res = HcfCreateCmsGenerator(static_cast<HcfCmsContentType>(cmsContentType), &cmsGenerator);
479     if (res != CF_SUCCESS) {
480         napi_throw(env, CertGenerateBusinessError(env, res, "create cms generator failed"));
481         LOGE("Failed to create cms generator.");
482         return nullptr;
483     }
484 
485     NapiCertCmsGenerator *napiCmsGenerator = new (std::nothrow) NapiCertCmsGenerator(cmsGenerator);
486     if (napiCmsGenerator == nullptr) {
487         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a cmsGenerator class"));
488         LOGE("Failed to create a cmsGenerator class");
489         CfObjDestroy(cmsGenerator);
490         return nullptr;
491     }
492 
493     napi_status status = napi_wrap(env, instance, napiCmsGenerator,
494         [](napi_env env, void *data, void *hint) {
495             NapiCertCmsGenerator *napiCertCmsGenerator = static_cast<NapiCertCmsGenerator *>(data);
496             delete napiCertCmsGenerator;
497             return;
498         }, nullptr, nullptr);
499     if (status != napi_ok) {
500         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "failed to wrap napiCertCmsGenerator obj!"));
501         LOGE("failed to wrap napiCertCmsGenerator obj!");
502         delete napiCmsGenerator;
503         return nullptr;
504     }
505     return instance;
506 }
507 
DefineCertCmsGeneratorJSClass(napi_env env,napi_value exports)508 void NapiCertCmsGenerator::DefineCertCmsGeneratorJSClass(napi_env env, napi_value exports)
509 {
510     napi_property_descriptor desc[] = {
511         DECLARE_NAPI_FUNCTION("createCmsGenerator", CreateCmsGenerator),
512     };
513     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
514 
515     napi_property_descriptor classDesc[] = {
516         DECLARE_NAPI_FUNCTION("addSigner", NapiAddSigner),
517         DECLARE_NAPI_FUNCTION("addCert", NapiAddCert),
518         DECLARE_NAPI_FUNCTION("doFinal", NapiDoFinal),
519         DECLARE_NAPI_FUNCTION("doFinalSync", NapiDoFinalSync),
520     };
521     napi_value constructor = nullptr;
522     napi_define_class(env, "CmsGenerator", NAPI_AUTO_LENGTH, CmsGeneratorConstructor, nullptr,
523         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
524     napi_create_reference(env, constructor, 1, &classRef_);
525 }
526 } // namespace CertFramework
527 } // namespace OHOS