• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_key_pair.h"
17 
18 #include "securec.h"
19 #include "log.h"
20 #include "napi_crypto_framework_defines.h"
21 #include "napi_pri_key.h"
22 #include "napi_pub_key.h"
23 
24 #include "napi_utils.h"
25 
26 namespace OHOS {
27 namespace CryptoFramework {
28 thread_local napi_ref NapiKeyPair::classRef_ = nullptr;
29 
NapiKeyPair(HcfKeyPair * keyPair)30 NapiKeyPair::NapiKeyPair(HcfKeyPair *keyPair)
31 {
32     this->keyPair_ = keyPair;
33 }
34 
~NapiKeyPair()35 NapiKeyPair::~NapiKeyPair()
36 {
37     HcfObjDestroy(this->keyPair_);
38     this->keyPair_ = nullptr;
39 }
40 
KeyPairConstructor(napi_env env,napi_callback_info info)41 napi_value NapiKeyPair::KeyPairConstructor(napi_env env, napi_callback_info info)
42 {
43     LOGI("enter ...");
44     napi_value thisVar = nullptr;
45     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
46     LOGI("out ...");
47     return thisVar;
48 }
49 
ConvertToJsKeyPair(napi_env env)50 napi_value NapiKeyPair::ConvertToJsKeyPair(napi_env env)
51 {
52     LOGI("enter ...");
53 
54     napi_value instance;
55     napi_value constructor = nullptr;
56     napi_get_reference_value(env, classRef_, &constructor);
57     napi_new_instance(env, constructor, 0, nullptr, &instance);
58 
59     if (this->keyPair_->pubKey != nullptr) {
60         NapiPubKey *napiPubKey = new NapiPubKey(this->keyPair_->pubKey);
61         napi_value pubKey = napiPubKey->ConvertToJsPubKey(env);
62         napi_wrap(
63             env, pubKey, napiPubKey,
64             [](napi_env env, void *data, void *hint) {
65                 NapiPubKey *napiPubKey = static_cast<NapiPubKey *>(data);
66                 delete napiPubKey;
67                 return;
68             },
69             nullptr, nullptr);
70         napi_set_named_property(env, instance, CRYPTO_TAG_PUB_KEY.c_str(), pubKey);
71     }
72 
73     if (this->keyPair_->priKey != nullptr) {
74         NapiPriKey *napiPriKey = new NapiPriKey(this->keyPair_->priKey);
75         napi_value priKey = napiPriKey->ConvertToJsPriKey(env);
76         napi_wrap(
77             env, priKey, napiPriKey,
78             [](napi_env env, void *data, void *hint) {
79                 NapiPriKey *napiPriKey = static_cast<NapiPriKey *>(data);
80                 delete napiPriKey;
81                 return;
82             },
83             nullptr, nullptr);
84         napi_set_named_property(env, instance, CRYPTO_TAG_PRI_KEY.c_str(), priKey);
85     }
86 
87     LOGI("out ...");
88     return instance;
89 }
90 
DefineKeyPairJSClass(napi_env env)91 void NapiKeyPair::DefineKeyPairJSClass(napi_env env)
92 {
93     napi_property_descriptor classDesc[] = {};
94     napi_value constructor = nullptr;
95     napi_define_class(env, "KeyPair", NAPI_AUTO_LENGTH, KeyPairConstructor, nullptr,
96         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
97     napi_create_reference(env, constructor, 1, &classRef_);
98 }
99 } // CryptoFramework
100 } // OHOS
101