1 /*
2 * Copyright (C) 2022-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_asy_key_generator.h"
17
18 #include "securec.h"
19 #include "log.h"
20 #include "memory.h"
21
22 #include "napi_crypto_framework_defines.h"
23 #include "napi_utils.h"
24 #include "napi_key_pair.h"
25 #include "napi_pri_key.h"
26 #include "napi_pub_key.h"
27
28 namespace OHOS {
29 namespace CryptoFramework {
30 struct GenKeyPairCtx {
31 napi_env env = nullptr;
32
33 AsyncType asyncType = ASYNC_CALLBACK;
34 napi_ref callback = nullptr;
35 napi_deferred deferred = nullptr;
36 napi_value promise = nullptr;
37 napi_async_work asyncWork = nullptr;
38 napi_ref generatorRef = nullptr;
39
40 HcfAsyKeyGenerator *generator = nullptr;
41 HcfParamsSpec *params = nullptr;
42
43 HcfResult errCode = HCF_SUCCESS;
44 const char *errMsg = nullptr;
45 HcfKeyPair *returnKeyPair = nullptr;
46 };
47
48 struct ConvertKeyCtx {
49 napi_env env = nullptr;
50
51 AsyncType asyncType = ASYNC_CALLBACK;
52 napi_ref callback = nullptr;
53 napi_deferred deferred = nullptr;
54 napi_value promise = nullptr;
55 napi_async_work asyncWork = nullptr;
56 napi_ref generatorRef = nullptr;
57
58 HcfAsyKeyGenerator *generator = nullptr;
59 HcfParamsSpec *params = nullptr;
60 HcfBlob *pubKey = nullptr;
61 HcfBlob *priKey = nullptr;
62
63 HcfResult errCode = HCF_SUCCESS;
64 const char *errMsg = nullptr;
65 HcfKeyPair *returnKeyPair = nullptr;
66 };
67
68 struct ConvertPemKeyCtx {
69 napi_env env = nullptr;
70
71 napi_deferred deferred = nullptr;
72 napi_value promise = nullptr;
73 napi_async_work asyncWork = nullptr;
74 napi_ref generatorRef = nullptr;
75
76 HcfAsyKeyGenerator *generator = nullptr;
77 HcfParamsSpec *params = nullptr;
78 HcfBlob *pubKey = nullptr;
79 HcfBlob *priKey = nullptr;
80
81 HcfResult errCode = HCF_SUCCESS;
82 const char *errMsg = nullptr;
83 HcfKeyPair *returnKeyPair = nullptr;
84 };
85
86 thread_local napi_ref NapiAsyKeyGenerator::classRef_ = nullptr;
87
FreeGenKeyPairCtx(napi_env env,GenKeyPairCtx * ctx)88 static void FreeGenKeyPairCtx(napi_env env, GenKeyPairCtx *ctx)
89 {
90 if (ctx == nullptr) {
91 return;
92 }
93
94 if (ctx->asyncWork != nullptr) {
95 napi_delete_async_work(env, ctx->asyncWork);
96 ctx->asyncWork = nullptr;
97 }
98
99 if (ctx->callback != nullptr) {
100 napi_delete_reference(env, ctx->callback);
101 ctx->callback = nullptr;
102 }
103
104 if (ctx->generatorRef != nullptr) {
105 napi_delete_reference(env, ctx->generatorRef);
106 ctx->generatorRef = nullptr;
107 }
108
109 HcfFree(ctx);
110 }
111
FreeConvertKeyCtx(napi_env env,ConvertKeyCtx * ctx)112 static void FreeConvertKeyCtx(napi_env env, ConvertKeyCtx *ctx)
113 {
114 if (ctx == nullptr) {
115 return;
116 }
117
118 if (ctx->asyncWork != nullptr) {
119 napi_delete_async_work(env, ctx->asyncWork);
120 ctx->asyncWork = nullptr;
121 }
122
123 if (ctx->callback != nullptr) {
124 napi_delete_reference(env, ctx->callback);
125 ctx->callback = nullptr;
126 }
127
128 if (ctx->generatorRef != nullptr) {
129 napi_delete_reference(env, ctx->generatorRef);
130 ctx->generatorRef = nullptr;
131 }
132
133 HcfBlobDataFree(ctx->pubKey);
134 HcfFree(ctx->pubKey);
135 ctx->pubKey = nullptr;
136 HcfBlobDataClearAndFree(ctx->priKey);
137 HcfFree(ctx->priKey);
138 ctx->priKey = nullptr;
139 HcfFree(ctx);
140 }
141
FreeDecodeParamsSpec(HcfParamsSpec * paramsSpec)142 static void FreeDecodeParamsSpec(HcfParamsSpec *paramsSpec)
143 {
144 if (paramsSpec == nullptr) {
145 return;
146 }
147 HcfKeyDecodingParamsSpec *spec = reinterpret_cast<HcfKeyDecodingParamsSpec *>(paramsSpec);
148 if (spec->password != nullptr) {
149 size_t pwdLen = strlen(spec->password);
150 (void)memset_s((void*)spec->password, pwdLen, 0, pwdLen);
151 HcfFree(static_cast<void *>(spec->password));
152 spec->password = nullptr;
153 }
154 HcfFree(paramsSpec);
155 paramsSpec = nullptr;
156 }
157
FreeConvertPemKeyCtx(napi_env env,ConvertPemKeyCtx * ctx)158 static void FreeConvertPemKeyCtx(napi_env env, ConvertPemKeyCtx *ctx)
159 {
160 if (ctx == nullptr) {
161 return;
162 }
163
164 if (ctx->asyncWork != nullptr) {
165 napi_delete_async_work(env, ctx->asyncWork);
166 ctx->asyncWork = nullptr;
167 }
168 if (ctx->generatorRef != nullptr) {
169 napi_delete_reference(env, ctx->generatorRef);
170 ctx->generatorRef = nullptr;
171 }
172 FreeDecodeParamsSpec(ctx->params);
173
174 ctx->errMsg = nullptr;
175 if (ctx->pubKey != nullptr) {
176 HcfBlobDataFree(ctx->pubKey);
177 HcfFree(ctx->pubKey);
178 ctx->pubKey = nullptr;
179 }
180 if (ctx->priKey != nullptr) {
181 HcfBlobDataClearAndFree(ctx->priKey);
182 HcfFree(ctx->priKey);
183 ctx->priKey = nullptr;
184 }
185 HcfFree(ctx);
186 }
187
HcfFreePubKeyAndPriKey(HcfBlob * pubKey,HcfBlob * priKey)188 static void HcfFreePubKeyAndPriKey(HcfBlob *pubKey, HcfBlob *priKey)
189 {
190 HcfBlobDataFree(pubKey);
191 HCF_FREE_PTR(pubKey);
192 HcfBlobDataClearAndFree(priKey);
193 HCF_FREE_PTR(priKey);
194 }
195
BuildGenKeyPairCtx(napi_env env,napi_callback_info info,GenKeyPairCtx * ctx)196 static bool BuildGenKeyPairCtx(napi_env env, napi_callback_info info, GenKeyPairCtx *ctx)
197 {
198 napi_value thisVar = nullptr;
199 size_t expectedArgc = PARAMS_NUM_ONE;
200 size_t argc = expectedArgc;
201 napi_value argv[PARAMS_NUM_ONE] = { nullptr };
202 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
203 if (argc != expectedArgc && argc != expectedArgc - 1) {
204 LOGE("wrong argument num. require %{public}zu or %{public}zu arguments. [Argc]: %{public}zu!",
205 expectedArgc - 1, expectedArgc, argc);
206 return false;
207 }
208 ctx->asyncType = isCallback(env, argv[0], argc, expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
209
210 NapiAsyKeyGenerator *napiGenerator = nullptr;
211 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
212 if (status != napi_ok || napiGenerator == nullptr) {
213 LOGE("failed to unwrap napi asyKeyGenerator obj.");
214 return false;
215 }
216
217 ctx->generator = napiGenerator->GetAsyKeyGenerator();
218 ctx->params = nullptr;
219
220 if (napi_create_reference(env, thisVar, 1, &ctx->generatorRef) != napi_ok) {
221 LOGE("create generator ref failed generator key pair!");
222 return false;
223 }
224
225 if (ctx->asyncType == ASYNC_PROMISE) {
226 napi_create_promise(env, &ctx->deferred, &ctx->promise);
227 return true;
228 } else {
229 return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
230 }
231 }
232
GetPkAndSkBlobFromNapiValueIfInput(napi_env env,napi_value pkValue,napi_value skValue,HcfBlob ** returnPubKey,HcfBlob ** returnPriKey)233 static bool GetPkAndSkBlobFromNapiValueIfInput(napi_env env, napi_value pkValue, napi_value skValue,
234 HcfBlob **returnPubKey, HcfBlob **returnPriKey)
235 {
236 napi_valuetype valueType;
237 napi_typeof(env, pkValue, &valueType);
238 HcfBlob *pubKey = nullptr;
239 if (valueType != napi_null) {
240 pubKey = GetBlobFromNapiDataBlob(env, pkValue);
241 if (pubKey == nullptr) {
242 LOGE("failed to get pubKey.");
243 return false;
244 }
245 }
246
247 napi_typeof(env, skValue, &valueType);
248 HcfBlob *priKey = nullptr;
249 if (valueType != napi_null) {
250 priKey = GetBlobFromNapiDataBlob(env, skValue);
251 if (priKey == nullptr) {
252 // if the prikey get func fails, the return pointer will not take the ownership of pubkey and not free it.
253 HcfBlobDataFree(pubKey);
254 HcfFree(pubKey);
255 pubKey = nullptr;
256 LOGE("failed to get priKey.");
257 return false;
258 }
259 }
260
261 *returnPubKey = pubKey;
262 *returnPriKey = priKey;
263 return true;
264 }
265
GetPkAndSkStringFromNapiValueIfInput(napi_env env,napi_value pkValue,napi_value skValue,HcfBlob ** returnPubKey,HcfBlob ** returnPriKey)266 static bool GetPkAndSkStringFromNapiValueIfInput(napi_env env, napi_value pkValue, napi_value skValue,
267 HcfBlob **returnPubKey, HcfBlob **returnPriKey)
268 {
269 napi_valuetype valueTypePk;
270 napi_valuetype valueTypeSk;
271 napi_typeof(env, pkValue, &valueTypePk);
272 napi_typeof(env, skValue, &valueTypeSk);
273 if (valueTypePk == napi_null && valueTypeSk == napi_null) {
274 LOGE("valueTypePk and valueTypeSk is all null.");
275 return false;
276 }
277 HcfBlob *pubKey = nullptr;
278 if (valueTypePk != napi_null) {
279 pubKey = GetBlobFromStringJSParams(env, pkValue);
280 if (pubKey == nullptr) {
281 LOGE("GetBlobFromStringJSParams failed for pubKey.");
282 return false;
283 }
284 }
285 HcfBlob *priKey = nullptr;
286 if (valueTypeSk != napi_null) {
287 priKey = GetBlobFromStringJSParams(env, skValue);
288 if (priKey == nullptr) {
289 HcfBlobDataFree(pubKey);
290 HcfFree(pubKey);
291 pubKey = nullptr;
292 LOGE("GetBlobFromStringJSParams failed for priKey.");
293 return false;
294 }
295 }
296 *returnPubKey = pubKey;
297 *returnPriKey = priKey;
298 return true;
299 }
300
BuildConvertKeyCtx(napi_env env,napi_callback_info info,ConvertKeyCtx * ctx)301 static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKeyCtx *ctx)
302 {
303 napi_value thisVar = nullptr;
304 size_t expectedArgc = PARAMS_NUM_THREE;
305 size_t argc = expectedArgc;
306 napi_value argv[PARAMS_NUM_THREE] = { nullptr };
307 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
308 if (argc != expectedArgc && argc != expectedArgc - 1) {
309 LOGE("wrong argument num. require %{public}zu or %{public}zu arguments. [Argc]: %{public}zu!",
310 expectedArgc - 1, expectedArgc, argc);
311 return false;
312 }
313 ctx->asyncType = isCallback(env, argv[expectedArgc - 1], argc, expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
314
315 NapiAsyKeyGenerator *napiGenerator = nullptr;
316 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
317 if (status != napi_ok || napiGenerator == nullptr) {
318 LOGE("failed to unwrap napi asyKeyGenerator obj.");
319 return false;
320 }
321
322 HcfBlob *pubKey = nullptr;
323 HcfBlob *priKey = nullptr;
324 if (!GetPkAndSkBlobFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], &pubKey, &priKey)) {
325 return false;
326 }
327
328 ctx->generator = napiGenerator->GetAsyKeyGenerator();
329 ctx->params = nullptr;
330 ctx->pubKey = pubKey;
331 ctx->priKey = priKey;
332
333 if (napi_create_reference(env, thisVar, 1, &ctx->generatorRef) != napi_ok) {
334 LOGE("create generator ref failed when convert asym key!");
335 return false;
336 }
337
338 if (ctx->asyncType == ASYNC_PROMISE) {
339 napi_create_promise(env, &ctx->deferred, &ctx->promise);
340 return true;
341 } else {
342 return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
343 }
344 }
345
ValidateAndGetParams(napi_env env,napi_callback_info info,HcfBlob ** pubKey,HcfBlob ** priKey,HcfParamsSpec ** paramsSpec)346 static bool ValidateAndGetParams(napi_env env, napi_callback_info info, HcfBlob **pubKey, HcfBlob **priKey,
347 HcfParamsSpec **paramsSpec)
348 {
349 napi_value thisVar = nullptr;
350 size_t expectedArgc = PARAMS_NUM_THREE;
351 size_t argc = expectedArgc;
352 napi_value argv[PARAMS_NUM_THREE] = { nullptr };
353 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
354 if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) {
355 LOGE("wrong argument num. require %{public}zu arguments. [Argc]: %{public}zu!", expectedArgc, argc);
356 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters."));
357 return false;
358 }
359
360 if (!GetPkAndSkStringFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], pubKey, priKey)) {
361 LOGE("GetPkAndSkStringFromNapiValueIfInput failed.");
362 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "GetPkAndSkStringFromNapiValueIfInput failed."));
363 return false;
364 }
365
366 if (argc == expectedArgc) {
367 if (!GetDecodingParamsSpec(env, argv[PARAM2], paramsSpec)) {
368 HcfFreePubKeyAndPriKey(*pubKey, *priKey);
369 LOGE("get params failed!");
370 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get napi paramsSpec failed!"));
371 return false;
372 }
373 }
374 return true;
375 }
376
BuildConvertPemKeyCtx(napi_env env,napi_callback_info info,ConvertPemKeyCtx * ctx)377 static bool BuildConvertPemKeyCtx(napi_env env, napi_callback_info info, ConvertPemKeyCtx *ctx)
378 {
379 napi_value thisVar = nullptr;
380 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
381 NapiAsyKeyGenerator *napiGenerator = nullptr;
382 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
383 if (status != napi_ok || napiGenerator == nullptr) {
384 LOGE("failed to unwrap napi asyKeyGenerator obj.");
385 return false;
386 }
387 HcfBlob *pubKey = nullptr;
388 HcfBlob *priKey = nullptr;
389 HcfParamsSpec *paramsSpec = nullptr;
390 if (!ValidateAndGetParams(env, info, &pubKey, &priKey, ¶msSpec)) {
391 return false;
392 }
393
394 ctx->generator = napiGenerator->GetAsyKeyGenerator();
395 ctx->params = paramsSpec;
396 ctx->pubKey = pubKey;
397 ctx->priKey = priKey;
398 if (napi_create_reference(env, thisVar, 1, &ctx->generatorRef) != napi_ok) {
399 LOGE("create generator ref failed when convert pem asym key!");
400 return false;
401 }
402 napi_create_promise(env, &ctx->deferred, &ctx->promise);
403 return true;
404 }
405
ReturnGenKeyPairCallbackResult(napi_env env,GenKeyPairCtx * ctx,napi_value result)406 static void ReturnGenKeyPairCallbackResult(napi_env env, GenKeyPairCtx *ctx, napi_value result)
407 {
408 napi_value businessError = nullptr;
409 if (ctx->errCode != HCF_SUCCESS) {
410 businessError = GenerateBusinessError(env, ctx->errCode, ctx->errMsg);
411 }
412
413 napi_value params[ARGS_SIZE_TWO] = { businessError, result };
414
415 napi_value func = nullptr;
416 napi_get_reference_value(env, ctx->callback, &func);
417
418 napi_value recv = nullptr;
419 napi_value callFuncRet = nullptr;
420 napi_get_undefined(env, &recv);
421 napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
422 }
423
ReturnGenKeyPairPromiseResult(napi_env env,GenKeyPairCtx * ctx,napi_value result)424 static void ReturnGenKeyPairPromiseResult(napi_env env, GenKeyPairCtx *ctx, napi_value result)
425 {
426 if (ctx->errCode == HCF_SUCCESS) {
427 napi_resolve_deferred(env, ctx->deferred, result);
428 } else {
429 napi_reject_deferred(env, ctx->deferred,
430 GenerateBusinessError(env, ctx->errCode, ctx->errMsg));
431 }
432 }
433
ReturnConvertKeyCallbackResult(napi_env env,ConvertKeyCtx * ctx,napi_value result)434 static void ReturnConvertKeyCallbackResult(napi_env env, ConvertKeyCtx *ctx, napi_value result)
435 {
436 napi_value businessError = nullptr;
437 if (ctx->errCode != HCF_SUCCESS) {
438 businessError = GenerateBusinessError(env, ctx->errCode, ctx->errMsg);
439 }
440
441 napi_value params[ARGS_SIZE_TWO] = { businessError, result };
442
443 napi_value func = nullptr;
444 napi_get_reference_value(env, ctx->callback, &func);
445
446 napi_value recv = nullptr;
447 napi_value callFuncRet = nullptr;
448 napi_get_undefined(env, &recv);
449 napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
450 }
451
ReturnConvertKeyPromiseResult(napi_env env,ConvertKeyCtx * ctx,napi_value result)452 static void ReturnConvertKeyPromiseResult(napi_env env, ConvertKeyCtx *ctx, napi_value result)
453 {
454 if (ctx->errCode == HCF_SUCCESS) {
455 napi_resolve_deferred(env, ctx->deferred, result);
456 } else {
457 napi_reject_deferred(env, ctx->deferred,
458 GenerateBusinessError(env, ctx->errCode, ctx->errMsg));
459 }
460 }
461
ReturnConvertPemKeyPromiseResult(napi_env env,ConvertPemKeyCtx * ctx,napi_value result)462 static void ReturnConvertPemKeyPromiseResult(napi_env env, ConvertPemKeyCtx *ctx, napi_value result)
463 {
464 if (ctx->errCode == HCF_SUCCESS) {
465 napi_resolve_deferred(env, ctx->deferred, result);
466 } else {
467 napi_reject_deferred(env, ctx->deferred,
468 GenerateBusinessError(env, ctx->errCode, ctx->errMsg));
469 }
470 }
471
GenKeyPairAsyncWorkProcess(napi_env env,void * data)472 static void GenKeyPairAsyncWorkProcess(napi_env env, void *data)
473 {
474 GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(data);
475
476 ctx->errCode = ctx->generator->generateKeyPair(ctx->generator, ctx->params, &(ctx->returnKeyPair));
477 if (ctx->errCode != HCF_SUCCESS) {
478 LOGD("[error] generate key pair fail.");
479 ctx->errMsg = "generate key pair fail.";
480 }
481 }
482
GenKeyPairAsyncWorkReturn(napi_env env,napi_status status,void * data)483 static void GenKeyPairAsyncWorkReturn(napi_env env, napi_status status, void *data)
484 {
485 GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(data);
486
487 napi_value instance = nullptr;
488 if (ctx->errCode == HCF_SUCCESS) {
489 NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(ctx->returnKeyPair);
490 if (napiKeyPair == nullptr) {
491 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!"));
492 LOGE("new napi key pair failed");
493 FreeGenKeyPairCtx(env, ctx);
494 return;
495 }
496 instance = napiKeyPair->ConvertToJsKeyPair(env);
497
498 napi_status ret = napi_wrap(
499 env, instance, napiKeyPair,
500 [](napi_env env, void *data, void *hint) {
501 NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
502 delete keyPair;
503 return;
504 }, nullptr, nullptr);
505 if (ret != napi_ok) {
506 LOGE("failed to wrap napiKeyPair obj!");
507 ctx->errCode = HCF_INVALID_PARAMS;
508 ctx->errMsg = "failed to wrap napiKeyPair obj!";
509 delete napiKeyPair;
510 }
511 }
512
513 if (ctx->asyncType == ASYNC_CALLBACK) {
514 ReturnGenKeyPairCallbackResult(env, ctx, instance);
515 } else {
516 ReturnGenKeyPairPromiseResult(env, ctx, instance);
517 }
518 FreeGenKeyPairCtx(env, ctx);
519 }
520
ConvertKeyAsyncWorkProcess(napi_env env,void * data)521 static void ConvertKeyAsyncWorkProcess(napi_env env, void *data)
522 {
523 ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(data);
524
525 ctx->errCode = ctx->generator->convertKey(ctx->generator, ctx->params,
526 ctx->pubKey, ctx->priKey, &(ctx->returnKeyPair));
527 if (ctx->errCode != HCF_SUCCESS) {
528 LOGD("[error] convert key fail.");
529 ctx->errMsg = "convert key fail.";
530 }
531 }
532
ConvertPemKeyAsyncWorkProcess(napi_env env,void * data)533 static void ConvertPemKeyAsyncWorkProcess(napi_env env, void *data)
534 {
535 ConvertPemKeyCtx *ctx = static_cast<ConvertPemKeyCtx *>(data);
536 const char *pubKeyStr = nullptr;
537 const char *priKeyStr = nullptr;
538 if (ctx->pubKey != nullptr) {
539 pubKeyStr = reinterpret_cast<const char *>(ctx->pubKey->data);
540 }
541 if (ctx->priKey != nullptr) {
542 priKeyStr = reinterpret_cast<const char *>(ctx->priKey->data);
543 }
544 ctx->errCode = ctx->generator->convertPemKey(ctx->generator, ctx->params,
545 pubKeyStr, priKeyStr, &(ctx->returnKeyPair));
546 if (ctx->errCode != HCF_SUCCESS) {
547 LOGE("ConvertPemKey fail.");
548 ctx->errMsg = "ConvertPemKey fail.";
549 }
550 }
551
ConvertKeyAsyncWorkReturn(napi_env env,napi_status status,void * data)552 static void ConvertKeyAsyncWorkReturn(napi_env env, napi_status status, void *data)
553 {
554 ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(data);
555
556 napi_value instance = nullptr;
557 if (ctx->errCode == HCF_SUCCESS) {
558 NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(ctx->returnKeyPair);
559 if (napiKeyPair == nullptr) {
560 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!"));
561 LOGE("new napi key pair failed");
562 FreeConvertKeyCtx(env, ctx);
563 return;
564 }
565 instance = napiKeyPair->ConvertToJsKeyPair(env);
566
567 napi_status ret = napi_wrap(
568 env, instance, napiKeyPair,
569 [](napi_env env, void *data, void *hint) {
570 NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
571 delete keyPair;
572 return;
573 }, nullptr, nullptr);
574 if (ret != napi_ok) {
575 LOGE("failed to wrap napiKeyPair obj!");
576 ctx->errCode = HCF_INVALID_PARAMS;
577 ctx->errMsg = "failed to wrap napiKeyPair obj!";
578 delete napiKeyPair;
579 }
580 }
581
582 if (ctx->asyncType == ASYNC_CALLBACK) {
583 ReturnConvertKeyCallbackResult(env, ctx, instance);
584 } else {
585 ReturnConvertKeyPromiseResult(env, ctx, instance);
586 }
587 FreeConvertKeyCtx(env, ctx);
588 }
589
ConvertPemKeyAsyncWorkReturn(napi_env env,napi_status status,void * data)590 static void ConvertPemKeyAsyncWorkReturn(napi_env env, napi_status status, void *data)
591 {
592 ConvertPemKeyCtx *ctx = static_cast<ConvertPemKeyCtx *>(data);
593
594 napi_value instance = nullptr;
595 if (ctx->errCode == HCF_SUCCESS) {
596 NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(ctx->returnKeyPair);
597 if (napiKeyPair == nullptr) {
598 LOGE("new napi key pair failed.");
599 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!"));
600 HcfObjDestroy(ctx->returnKeyPair);
601 ctx->returnKeyPair = nullptr;
602 FreeConvertPemKeyCtx(env, ctx);
603 return;
604 }
605 instance = napiKeyPair->ConvertToJsKeyPair(env);
606
607 napi_status ret = napi_wrap(
608 env, instance, napiKeyPair,
609 [](napi_env env, void *data, void *hint) {
610 NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
611 delete keyPair;
612 return;
613 }, nullptr, nullptr);
614 if (ret != napi_ok) {
615 LOGE("failed to wrap napiKeyPair obj!");
616 ctx->errCode = HCF_INVALID_PARAMS;
617 ctx->errMsg = "failed to wrap napiKeyPair obj!";
618 ctx->returnKeyPair = nullptr;
619 delete napiKeyPair;
620 }
621 }
622
623 ReturnConvertPemKeyPromiseResult(env, ctx, instance);
624 FreeConvertPemKeyCtx(env, ctx);
625 }
626
NewGenKeyPairAsyncWork(napi_env env,GenKeyPairCtx * ctx)627 static napi_value NewGenKeyPairAsyncWork(napi_env env, GenKeyPairCtx *ctx)
628 {
629 napi_value resourceName = nullptr;
630 napi_create_string_utf8(env, "generatorKeyPair", NAPI_AUTO_LENGTH, &resourceName);
631
632 napi_create_async_work(
633 env, nullptr, resourceName,
634 [](napi_env env, void *data) {
635 GenKeyPairAsyncWorkProcess(env, data);
636 return;
637 },
638 [](napi_env env, napi_status status, void *data) {
639 GenKeyPairAsyncWorkReturn(env, status, data);
640 return;
641 },
642 static_cast<void *>(ctx),
643 &ctx->asyncWork);
644
645 napi_queue_async_work(env, ctx->asyncWork);
646 if (ctx->asyncType == ASYNC_PROMISE) {
647 return ctx->promise;
648 } else {
649 return NapiGetNull(env);
650 }
651 }
652
NewConvertKeyAsyncWork(napi_env env,ConvertKeyCtx * ctx)653 static napi_value NewConvertKeyAsyncWork(napi_env env, ConvertKeyCtx *ctx)
654 {
655 napi_value resourceName = nullptr;
656 napi_create_string_utf8(env, "convertKey", NAPI_AUTO_LENGTH, &resourceName);
657
658 napi_create_async_work(
659 env, nullptr, resourceName,
660 [](napi_env env, void *data) {
661 ConvertKeyAsyncWorkProcess(env, data);
662 return;
663 },
664 [](napi_env env, napi_status status, void *data) {
665 ConvertKeyAsyncWorkReturn(env, status, data);
666 return;
667 },
668 static_cast<void *>(ctx),
669 &ctx->asyncWork);
670
671 napi_queue_async_work(env, ctx->asyncWork);
672 if (ctx->asyncType == ASYNC_PROMISE) {
673 return ctx->promise;
674 } else {
675 return NapiGetNull(env);
676 }
677 }
678
NewConvertPemKeyAsyncWork(napi_env env,ConvertPemKeyCtx * ctx)679 static napi_value NewConvertPemKeyAsyncWork(napi_env env, ConvertPemKeyCtx *ctx)
680 {
681 napi_value resourceName = nullptr;
682 napi_create_string_utf8(env, "convertPemKey", NAPI_AUTO_LENGTH, &resourceName);
683 napi_create_async_work(
684 env, nullptr, resourceName,
685 [](napi_env env, void *data) {
686 ConvertPemKeyAsyncWorkProcess(env, data);
687 return;
688 },
689 [](napi_env env, napi_status status, void *data) {
690 ConvertPemKeyAsyncWorkReturn(env, status, data);
691 return;
692 },
693 static_cast<void *>(ctx),
694 &ctx->asyncWork);
695
696 napi_queue_async_work(env, ctx->asyncWork);
697 return ctx->promise;
698 }
699
NapiAsyKeyGenerator(HcfAsyKeyGenerator * generator)700 NapiAsyKeyGenerator::NapiAsyKeyGenerator(HcfAsyKeyGenerator *generator)
701 {
702 this->generator_ = generator;
703 }
704
~NapiAsyKeyGenerator()705 NapiAsyKeyGenerator::~NapiAsyKeyGenerator()
706 {
707 HcfObjDestroy(this->generator_);
708 this->generator_ = nullptr;
709 }
710
GetAsyKeyGenerator()711 HcfAsyKeyGenerator *NapiAsyKeyGenerator::GetAsyKeyGenerator()
712 {
713 return this->generator_;
714 }
715
JsGenerateKeyPair(napi_env env,napi_callback_info info)716 napi_value NapiAsyKeyGenerator::JsGenerateKeyPair(napi_env env, napi_callback_info info)
717 {
718 GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(HcfMalloc(sizeof(GenKeyPairCtx), 0));
719 if (ctx == nullptr) {
720 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc ctx fail."));
721 LOGE("create context fail.");
722 return nullptr;
723 }
724
725 if (!BuildGenKeyPairCtx(env, info, ctx)) {
726 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail."));
727 LOGE("build context fail.");
728 FreeGenKeyPairCtx(env, ctx);
729 return nullptr;
730 }
731
732 return NewGenKeyPairAsyncWork(env, ctx);
733 }
734
GetHcfKeyPairInstance(napi_env env,HcfKeyPair * returnKeyPair,napi_value * instance)735 static bool GetHcfKeyPairInstance(napi_env env, HcfKeyPair *returnKeyPair, napi_value *instance)
736 {
737 NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair);
738 if (napiKeyPair == nullptr) {
739 HcfObjDestroy(returnKeyPair);
740 returnKeyPair = nullptr;
741 LOGE("new napi key pair failed");
742 return false;
743 }
744
745 *instance = napiKeyPair->ConvertToJsKeyPair(env);
746 napi_status ret = napi_wrap(
747 env, *instance, napiKeyPair,
748 [](napi_env env, void *data, void *hint) {
749 NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
750 delete keyPair;
751 return;
752 }, nullptr, nullptr);
753 if (ret != napi_ok) {
754 LOGE("failed to wrap napiKeyPair obj!");
755 delete napiKeyPair;
756 return false;
757 }
758
759 return true;
760 }
761
JsGenerateKeyPairSync(napi_env env,napi_callback_info info)762 napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callback_info info)
763 {
764 napi_value thisVar = nullptr;
765 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
766
767 NapiAsyKeyGenerator *napiGenerator = nullptr;
768 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
769 if (status != napi_ok || napiGenerator == nullptr) {
770 LOGE("failed to unwrap napi asyKeyGenerator obj.");
771 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj."));
772 return nullptr;
773 }
774
775 HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator();
776 if (generator == nullptr) {
777 LOGE("get generator fail.");
778 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get generator fail!"));
779 return nullptr;
780 }
781
782 HcfParamsSpec *params = nullptr;
783 HcfKeyPair *returnKeyPair = nullptr;
784 HcfResult errCode = generator->generateKeyPair(generator, params, &returnKeyPair);
785 if (errCode != HCF_SUCCESS) {
786 LOGE("generate key pair fail.");
787 napi_throw(env, GenerateBusinessError(env, errCode, "generate key pair fail."));
788 return nullptr;
789 }
790
791 napi_value instance = nullptr;
792 if (!GetHcfKeyPairInstance(env, returnKeyPair, &instance)) {
793 LOGE("failed to get generate key pair instance!");
794 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to get generate key pair instance!"));
795 return nullptr;
796 }
797 return instance;
798 }
799
JsConvertKey(napi_env env,napi_callback_info info)800 napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info info)
801 {
802 ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(HcfMalloc(sizeof(ConvertKeyCtx), 0));
803 if (ctx == nullptr) {
804 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "create context fail!"));
805 LOGE("create context fail.");
806 return nullptr;
807 }
808
809 if (!BuildConvertKeyCtx(env, info, ctx)) {
810 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail."));
811 LOGE("build context fail.");
812 FreeConvertKeyCtx(env, ctx);
813 return nullptr;
814 }
815
816 return NewConvertKeyAsyncWork(env, ctx);
817 }
818
JsConvertKeySync(napi_env env,napi_callback_info info)819 napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_info info)
820 {
821 napi_value thisVar = nullptr;
822 size_t argc = PARAMS_NUM_TWO;
823 napi_value argv[PARAMS_NUM_TWO] = { nullptr };
824 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
825 if (argc != PARAMS_NUM_TWO) {
826 LOGE("wrong argument num. require %{public}d arguments. [Argc]: %{public}zu!", PARAMS_NUM_TWO, argc);
827 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "wrong argument num."));
828 return nullptr;
829 }
830
831 NapiAsyKeyGenerator *napiGenerator = nullptr;
832 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
833 if (status != napi_ok || napiGenerator == nullptr) {
834 LOGE("failed to unwrap napi asyKeyGenerator obj.");
835 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj."));
836 return nullptr;
837 }
838
839 HcfBlob *pubKey = nullptr;
840 HcfBlob *priKey = nullptr;
841 if (!GetPkAndSkBlobFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], &pubKey, &priKey)) {
842 LOGE("failed to unwrap napi asyKeyGenerator obj.");
843 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj."));
844 return nullptr;
845 }
846
847 HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator();
848 if (generator == nullptr) {
849 HcfFreePubKeyAndPriKey(pubKey, priKey);
850 LOGE("get generator fail.");
851 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get generator fail!"));
852 return nullptr;
853 }
854
855 HcfParamsSpec *params = nullptr;
856 HcfKeyPair *returnKeyPair = nullptr;
857 HcfResult errCode = generator->convertKey(generator, params, pubKey, priKey, &(returnKeyPair));
858 HcfFreePubKeyAndPriKey(pubKey, priKey);
859 if (errCode != HCF_SUCCESS) {
860 LOGE("convert key fail.");
861 napi_throw(env, GenerateBusinessError(env, errCode, "convert key fail."));
862 return nullptr;
863 }
864
865 napi_value instance = nullptr;
866 if (!GetHcfKeyPairInstance(env, returnKeyPair, &instance)) {
867 LOGE("failed to get convert key instance!");
868 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to get convert key instance!"));
869 return nullptr;
870 }
871
872 return instance;
873 }
874
JsConvertPemKey(napi_env env,napi_callback_info info)875 napi_value NapiAsyKeyGenerator::JsConvertPemKey(napi_env env, napi_callback_info info)
876 {
877 ConvertPemKeyCtx *ctx = static_cast<ConvertPemKeyCtx *>(HcfMalloc(sizeof(ConvertPemKeyCtx), 0));
878 if (ctx == nullptr) {
879 LOGE("create context fail.");
880 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "create context fail!"));
881 return nullptr;
882 }
883 if (!BuildConvertPemKeyCtx(env, info, ctx)) {
884 LOGE("build context fail.");
885 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail."));
886 FreeConvertPemKeyCtx(env, ctx);
887 return nullptr;
888 }
889 return NewConvertPemKeyAsyncWork(env, ctx);
890 }
891
ConvertPemKeySync(HcfBlob * pubKey,HcfBlob * priKey,HcfAsyKeyGenerator * generator,HcfParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)892 static HcfResult ConvertPemKeySync(HcfBlob *pubKey, HcfBlob *priKey, HcfAsyKeyGenerator *generator,
893 HcfParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
894 {
895 const char *pubKeyStr = nullptr;
896 const char *priKeyStr = nullptr;
897 if (pubKey != nullptr) {
898 pubKeyStr = reinterpret_cast<const char *>(pubKey->data);
899 }
900 if (priKey != nullptr) {
901 priKeyStr = reinterpret_cast<const char *>(priKey->data);
902 }
903 HcfResult errCode = generator->convertPemKey(generator, paramsSpec,
904 pubKeyStr, priKeyStr, returnKeyPair);
905 if (errCode != HCF_SUCCESS) {
906 LOGE("convertPemKey error!");
907 return errCode;
908 }
909 return HCF_SUCCESS;
910 }
911
JsConvertPemKeySync(napi_env env,napi_callback_info info)912 napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_info info)
913 {
914 napi_value thisVar = nullptr;
915 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
916 HcfBlob *pubKey = nullptr;
917 HcfBlob *priKey = nullptr;
918 HcfParamsSpec *paramsSpec = nullptr;
919 if (!ValidateAndGetParams(env, info, &pubKey, &priKey, ¶msSpec)) {
920 FreeDecodeParamsSpec(paramsSpec);
921 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters."));
922 return NapiGetNull(env);
923 }
924
925 NapiAsyKeyGenerator *napiGenerator = nullptr;
926 napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
927 if (status != napi_ok || napiGenerator == nullptr) {
928 FreeDecodeParamsSpec(paramsSpec);
929 HcfFreePubKeyAndPriKey(pubKey, priKey);
930 LOGE("failed to unwrap napi asyKeyGenerator obj.");
931 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj."));
932 return nullptr;
933 }
934
935 HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator();
936 if (generator == nullptr) {
937 FreeDecodeParamsSpec(paramsSpec);
938 HcfFreePubKeyAndPriKey(pubKey, priKey);
939 LOGE("GetAsyKeyGenerator failed!");
940 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "GetAsyKeyGenerator failed!"));
941 return nullptr;
942 }
943
944 HcfKeyPair *returnKeyPair = nullptr;
945 HcfResult errCode = ConvertPemKeySync(pubKey, priKey, generator, paramsSpec, &(returnKeyPair));
946 HcfFreePubKeyAndPriKey(pubKey, priKey);
947 if (errCode != HCF_SUCCESS) {
948 FreeDecodeParamsSpec(paramsSpec);
949 LOGE("ConvertPemKeySync error!");
950 napi_throw(env, GenerateBusinessError(env, errCode, "ConvertPemKeySync error!"));
951 return nullptr;
952 }
953
954 napi_value instance = nullptr;
955 if (!GetHcfKeyPairInstance(env, returnKeyPair, &instance)) {
956 FreeDecodeParamsSpec(paramsSpec);
957 LOGE("failed to get convert key instance!");
958 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to get convert key instance!"));
959 return nullptr;
960 }
961 FreeDecodeParamsSpec(paramsSpec);
962 return instance;
963 }
964
AsyKeyGeneratorConstructor(napi_env env,napi_callback_info info)965 napi_value NapiAsyKeyGenerator::AsyKeyGeneratorConstructor(napi_env env, napi_callback_info info)
966 {
967 napi_value thisVar = nullptr;
968 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
969 return thisVar;
970 }
971
NapiWrapAsyKeyGen(napi_env env,napi_value instance,NapiAsyKeyGenerator * napiAsyKeyGenerator)972 static napi_value NapiWrapAsyKeyGen(napi_env env, napi_value instance, NapiAsyKeyGenerator *napiAsyKeyGenerator)
973 {
974 napi_status status = napi_wrap(
975 env, instance, napiAsyKeyGenerator,
976 [](napi_env env, void *data, void *hint) {
977 NapiAsyKeyGenerator *napiAsyKeyGenerator = static_cast<NapiAsyKeyGenerator *>(data);
978 delete napiAsyKeyGenerator;
979 return;
980 }, nullptr, nullptr);
981 if (status != napi_ok) {
982 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiAsyKeyGenerator obj!"));
983 delete napiAsyKeyGenerator;
984 napiAsyKeyGenerator = nullptr;
985 LOGE("failed to wrap napiAsyKeyGenerator obj!");
986 return nullptr;
987 }
988 return instance;
989 }
990
CreateJsAsyKeyGenerator(napi_env env,napi_callback_info info)991 napi_value NapiAsyKeyGenerator::CreateJsAsyKeyGenerator(napi_env env, napi_callback_info info)
992 {
993 size_t expectedArgc = PARAMS_NUM_ONE;
994 size_t argc = expectedArgc;
995 napi_value argv[PARAMS_NUM_ONE] = { nullptr };
996 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
997 if (argc != expectedArgc) {
998 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
999 LOGE("The input args num is invalid.");
1000 return NapiGetNull(env);
1001 }
1002
1003 napi_value instance;
1004 napi_value constructor = nullptr;
1005 napi_get_reference_value(env, classRef_, &constructor);
1006 napi_new_instance(env, constructor, argc, argv, &instance);
1007
1008 std::string algName;
1009 if (!GetStringFromJSParams(env, argv[0], algName)) {
1010 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get algoName."));
1011 LOGE("failed to get algoName.");
1012 return NapiGetNull(env);
1013 }
1014
1015 HcfAsyKeyGenerator *generator = nullptr;
1016 HcfResult res = HcfAsyKeyGeneratorCreate(algName.c_str(), &generator);
1017 if (res != HCF_SUCCESS) {
1018 napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create c generator fail."));
1019 LOGE("create c generator fail.");
1020 return NapiGetNull(env);
1021 }
1022
1023 NapiAsyKeyGenerator *napiAsyKeyGenerator = new (std::nothrow) NapiAsyKeyGenerator(generator);
1024 if (napiAsyKeyGenerator == nullptr) {
1025 napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi asy key napi generator failed!"));
1026 LOGE("new napi asy key napi generator failed");
1027 HcfObjDestroy(generator);
1028 generator = nullptr;
1029 return NapiGetNull(env);
1030 }
1031
1032 napi_value napiAlgName = nullptr;
1033 napi_create_string_utf8(env, algName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
1034 napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
1035
1036 return NapiWrapAsyKeyGen(env, instance, napiAsyKeyGenerator);
1037 }
1038
DefineAsyKeyGeneratorJSClass(napi_env env,napi_value exports)1039 void NapiAsyKeyGenerator::DefineAsyKeyGeneratorJSClass(napi_env env, napi_value exports)
1040 {
1041 napi_property_descriptor desc[] = {
1042 DECLARE_NAPI_FUNCTION("createAsyKeyGenerator", NapiAsyKeyGenerator::CreateJsAsyKeyGenerator),
1043 };
1044 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1045
1046 napi_property_descriptor classDesc[] = {
1047 DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGenerator::JsGenerateKeyPair),
1048 DECLARE_NAPI_FUNCTION("generateKeyPairSync", NapiAsyKeyGenerator::JsGenerateKeyPairSync),
1049 DECLARE_NAPI_FUNCTION("convertKey", NapiAsyKeyGenerator::JsConvertKey),
1050 DECLARE_NAPI_FUNCTION("convertKeySync", NapiAsyKeyGenerator::JsConvertKeySync),
1051 DECLARE_NAPI_FUNCTION("convertPemKey", NapiAsyKeyGenerator::JsConvertPemKey),
1052 DECLARE_NAPI_FUNCTION("convertPemKeySync", NapiAsyKeyGenerator::JsConvertPemKeySync),
1053 };
1054 napi_value constructor = nullptr;
1055 napi_define_class(env, "AsyKeyGenerator", NAPI_AUTO_LENGTH, NapiAsyKeyGenerator::AsyKeyGeneratorConstructor,
1056 nullptr, sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
1057 napi_create_reference(env, constructor, 1, &classRef_);
1058 }
1059 } // CryptoFramework
1060 } // OHOS
1061