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_sym_key.h"
17
18 #include "securec.h"
19 #include "log.h"
20 #include "napi_utils.h"
21 #include "napi_crypto_framework_defines.h"
22
23 namespace OHOS {
24 namespace CryptoFramework {
25 thread_local napi_ref NapiSymKey::classRef_ = nullptr;
26
NapiSymKey(HcfSymKey * symKey)27 NapiSymKey::NapiSymKey(HcfSymKey *symKey) : NapiKey(reinterpret_cast<HcfKey *>(symKey)) {}
28
~NapiSymKey()29 NapiSymKey::~NapiSymKey()
30 {
31 HcfObjDestroy(this->hcfKey_);
32 this->hcfKey_ = nullptr;
33 }
34
GetSymKey() const35 HcfSymKey *NapiSymKey::GetSymKey() const
36 {
37 return reinterpret_cast<HcfSymKey *>(NapiKey::GetHcfKey());
38 }
39
JsClearMem(napi_env env,napi_callback_info info)40 napi_value NapiSymKey::JsClearMem(napi_env env, napi_callback_info info)
41 {
42 napi_value thisVar = nullptr;
43 NapiSymKey *napiSymKey = nullptr;
44 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
45
46 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiSymKey)));
47 HcfSymKey *key = napiSymKey->GetSymKey();
48 key->clearMem(key);
49 return nullptr;
50 }
51
SymKeyConstructor(napi_env env,napi_callback_info info)52 napi_value NapiSymKey::SymKeyConstructor(napi_env env, napi_callback_info info)
53 {
54 napi_value thisVar = nullptr;
55 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
56 return thisVar;
57 }
58
CreateSymKey(napi_env env)59 napi_value NapiSymKey::CreateSymKey(napi_env env)
60 {
61 napi_value instance = nullptr;
62 napi_value constructor = nullptr;
63 NAPI_CALL(env, napi_get_reference_value(env, classRef_, &constructor));
64 NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &instance));
65 return instance;
66 }
67
DefineSymKeyJSClass(napi_env env)68 void NapiSymKey::DefineSymKeyJSClass(napi_env env)
69 {
70 napi_property_descriptor classDesc[] = {
71 DECLARE_NAPI_FUNCTION("getEncoded", NapiKey::JsGetEncoded),
72 DECLARE_NAPI_FUNCTION("clearMem", NapiSymKey::JsClearMem),
73 {.utf8name = "format", .getter = NapiKey::JsGetFormat},
74 {.utf8name = "algName", .getter = NapiKey::JsGetAlgorithm},
75 };
76 napi_value constructor = nullptr;
77 napi_define_class(env, "SymKey", NAPI_AUTO_LENGTH, SymKeyConstructor, nullptr,
78 sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
79 napi_create_reference(env, constructor, 1, &classRef_);
80 }
81 } // CryptoFramework
82 } // OHOS
83