• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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_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 #include "napi_utils.h"
24 
25 namespace OHOS {
26 namespace CryptoFramework {
27 thread_local napi_ref NapiKeyPair::classRef_ = nullptr;
28 
NapiKeyPair(HcfKeyPair * keyPair)29 NapiKeyPair::NapiKeyPair(HcfKeyPair *keyPair)
30 {
31     this->keyPair_ = keyPair;
32 }
33 
~NapiKeyPair()34 NapiKeyPair::~NapiKeyPair()
35 {
36     HcfObjDestroy(this->keyPair_);
37     this->keyPair_ = nullptr;
38 }
39 
KeyPairConstructor(napi_env env,napi_callback_info info)40 napi_value NapiKeyPair::KeyPairConstructor(napi_env env, napi_callback_info info)
41 {
42     napi_value thisVar = nullptr;
43     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
44     return thisVar;
45 }
46 
ConvertToJsKeyPair(napi_env env)47 napi_value NapiKeyPair::ConvertToJsKeyPair(napi_env env)
48 {
49     napi_value instance;
50     napi_value constructor = nullptr;
51     napi_get_reference_value(env, classRef_, &constructor);
52     napi_new_instance(env, constructor, 0, nullptr, &instance);
53 
54     if (this->keyPair_->pubKey != nullptr) {
55         NapiPubKey *napiPubKey = new (std::nothrow) NapiPubKey(this->keyPair_->pubKey);
56         if (napiPubKey == nullptr) {
57             LOGE("new napi pub key failed");
58             return nullptr;
59         }
60         napi_value pubKey = napiPubKey->ConvertToJsPubKey(env);
61         napi_status status =  napi_wrap(
62             env, pubKey, napiPubKey,
63             [](napi_env env, void *data, void *hint) {
64                 NapiPubKey *napiPubKey = static_cast<NapiPubKey *>(data);
65                 delete napiPubKey;
66                 return;
67             }, nullptr, nullptr);
68         if (status != napi_ok) {
69             LOGE("failed to wrap napiPubKey obj!");
70             delete napiPubKey;
71             return nullptr;
72         }
73         napi_set_named_property(env, instance, CRYPTO_TAG_PUB_KEY.c_str(), pubKey);
74     }
75 
76     if (this->keyPair_->priKey != nullptr) {
77         NapiPriKey *napiPriKey = new (std::nothrow) NapiPriKey(this->keyPair_->priKey);
78         if (napiPriKey == nullptr) {
79             LOGE("new napi pri key failed");
80             return nullptr;
81         }
82         napi_value priKey = napiPriKey->ConvertToJsPriKey(env);
83         napi_status status =  napi_wrap(
84             env, priKey, napiPriKey,
85             [](napi_env env, void *data, void *hint) {
86                 NapiPriKey *napiPriKey = static_cast<NapiPriKey *>(data);
87                 delete napiPriKey;
88                 return;
89             }, nullptr, nullptr);
90         if (status != napi_ok) {
91             napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiPriKey obj!"));
92             LOGE("failed to wrap napiPriKey obj!");
93             delete napiPriKey;
94             return nullptr;
95         }
96         napi_set_named_property(env, instance, CRYPTO_TAG_PRI_KEY.c_str(), priKey);
97     }
98     return instance;
99 }
100 
DefineKeyPairJSClass(napi_env env)101 void NapiKeyPair::DefineKeyPairJSClass(napi_env env)
102 {
103     napi_property_descriptor classDesc[] = {};
104     napi_value constructor = nullptr;
105     napi_define_class(env, "KeyPair", NAPI_AUTO_LENGTH, KeyPairConstructor, nullptr,
106         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
107     napi_create_reference(env, constructor, 1, &classRef_);
108 }
109 } // CryptoFramework
110 } // OHOS
111