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_pub_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 NapiPubKey::classRef_ = nullptr;
26
NapiPubKey(HcfPubKey * pubKey)27 NapiPubKey::NapiPubKey(HcfPubKey *pubKey) : NapiKey(reinterpret_cast<HcfKey *>(pubKey)) {}
28
~NapiPubKey()29 NapiPubKey::~NapiPubKey() {}
30
GetPubKey()31 HcfPubKey *NapiPubKey::GetPubKey()
32 {
33 return reinterpret_cast<HcfPubKey *>(NapiKey::GetHcfKey());
34 }
35
PubKeyConstructor(napi_env env,napi_callback_info info)36 napi_value NapiPubKey::PubKeyConstructor(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
ConvertToJsPubKey(napi_env env)47 napi_value NapiPubKey::ConvertToJsPubKey(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->GetPubKey()->base.getAlgorithm(&(this->GetPubKey()->base));
57 const char *format = this->GetPubKey()->base.getFormat(&(this->GetPubKey()->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 NapiPubKey::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 NapiPubKey *napiPubKey = nullptr;
76 napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiPubKey));
77
78 HcfPubKey *pubKey = napiPubKey->GetPubKey();
79 HcfBlob returnBlob;
80 HcfResult res = pubKey->base.getEncoded(&pubKey->base, &returnBlob);
81 if (res != HCF_SUCCESS) {
82 LOGE("c getEncoded fail.");
83 return nullptr;
84 }
85
86 napi_value instance = ConvertBlobToNapiValue(env, &returnBlob);
87 HcfBlobDataFree(&returnBlob);
88 return instance;
89 }
90
DefinePubKeyJSClass(napi_env env)91 void NapiPubKey::DefinePubKeyJSClass(napi_env env)
92 {
93 napi_property_descriptor classDesc[] = {
94 DECLARE_NAPI_FUNCTION("getEncoded", NapiPubKey::JsGetEncoded),
95 };
96 napi_value constructor = nullptr;
97 napi_define_class(env, "PubKey", NAPI_AUTO_LENGTH, NapiPubKey::PubKeyConstructor, nullptr,
98 sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
99 napi_create_reference(env, constructor, 1, &classRef_);
100 }
101 } // CryptoFramework
102 } // OHOS
103