• 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_pri_key.h"
17 
18 #include "log.h"
19 #include "napi_crypto_framework_defines.h"
20 #include "napi_utils.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace CryptoFramework {
25 thread_local napi_ref NapiPriKey::classRef_ = nullptr;
26 
NapiPriKey(HcfPriKey * priKey)27 NapiPriKey::NapiPriKey(HcfPriKey *priKey) : NapiKey(reinterpret_cast<HcfKey *>(priKey)) {}
28 
~NapiPriKey()29 NapiPriKey::~NapiPriKey() {}
30 
GetPriKey()31 HcfPriKey *NapiPriKey::GetPriKey()
32 {
33     return reinterpret_cast<HcfPriKey *>(NapiKey::GetHcfKey());
34 }
35 
PriKeyConstructor(napi_env env,napi_callback_info info)36 napi_value NapiPriKey::PriKeyConstructor(napi_env env, napi_callback_info info)
37 {
38     LOGI("enter ...");
39 
40     napi_value thisVar = nullptr;
41     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
42 
43     LOGI("out ...");
44     return thisVar;
45 }
46 
ConvertToJsPriKey(napi_env env)47 napi_value NapiPriKey::ConvertToJsPriKey(napi_env env)
48 {
49     LOGI("enter ...");
50 
51     napi_value instance;
52     napi_value constructor = nullptr;
53     napi_get_reference_value(env, classRef_, &constructor);
54     napi_new_instance(env, constructor, 0, nullptr, &instance);
55 
56     const char *algName = this->GetPriKey()->base.getAlgorithm(&(this->GetPriKey()->base));
57     const char *format = this->GetPriKey()->base.getFormat(&(this->GetPriKey()->base));
58 
59     napi_value napiAlgName = nullptr;
60     napi_create_string_utf8(env, algName, NAPI_AUTO_LENGTH, &napiAlgName);
61     napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
62 
63     napi_value napiFormat = nullptr;
64     napi_create_string_utf8(env, format, NAPI_AUTO_LENGTH, &napiFormat);
65     napi_set_named_property(env, instance, CRYPTO_TAG_FORMAT.c_str(), napiFormat);
66 
67     LOGI("out ...");
68     return instance;
69 }
70 
JsGetEncoded(napi_env env,napi_callback_info info)71 napi_value NapiPriKey::JsGetEncoded(napi_env env, napi_callback_info info)
72 {
73     napi_value thisVar = nullptr;
74     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
75     NapiPriKey *napiPriKey = nullptr;
76     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPriKey));
77 
78     HcfPriKey *priKey = napiPriKey->GetPriKey();
79 
80     HcfBlob returnBlob;
81     HcfResult res = priKey->base.getEncoded(&priKey->base, &returnBlob);
82     if (res != HCF_SUCCESS) {
83         LOGE("c getEncoded fail.");
84         return nullptr;
85     }
86 
87     napi_value instance = ConvertBlobToNapiValue(env, &returnBlob);
88     HcfBlobDataFree(&returnBlob);
89     return instance;
90 }
91 
JsClearMem(napi_env env,napi_callback_info info)92 napi_value NapiPriKey::JsClearMem(napi_env env, napi_callback_info info)
93 {
94     napi_value thisVar = nullptr;
95     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
96     NapiPriKey *napiPriKey = nullptr;
97     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPriKey));
98 
99     HcfPriKey *priKey = napiPriKey->GetPriKey();
100 
101     priKey->clearMem(priKey);
102     return nullptr;
103 }
104 
DefinePriKeyJSClass(napi_env env)105 void NapiPriKey::DefinePriKeyJSClass(napi_env env)
106 {
107     napi_property_descriptor classDesc[] = {
108         DECLARE_NAPI_FUNCTION("getEncoded", NapiPriKey::JsGetEncoded),
109         DECLARE_NAPI_FUNCTION("clearMem", NapiPriKey::JsClearMem),
110     };
111     napi_value constructor = nullptr;
112     napi_define_class(env, "PriKey", NAPI_AUTO_LENGTH, NapiPriKey::PriKeyConstructor, nullptr,
113         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
114     napi_create_reference(env, constructor, 1, &classRef_);
115 }
116 } // CryptoFramework
117 } // OHOS
118