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_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_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
45 
46     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiSymKey));
47     if (status != napi_ok || napiSymKey == nullptr) {
48         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiSymKey obj!"));
49         LOGE("failed to unwrap napiSymKey obj!");
50         return nullptr;
51     }
52     HcfSymKey *key = napiSymKey->GetSymKey();
53     key->clearMem(key);
54     return nullptr;
55 }
56 
SymKeyConstructor(napi_env env,napi_callback_info info)57 napi_value NapiSymKey::SymKeyConstructor(napi_env env, napi_callback_info info)
58 {
59     napi_value thisVar = nullptr;
60     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
61     return thisVar;
62 }
63 
CreateSymKey(napi_env env)64 napi_value NapiSymKey::CreateSymKey(napi_env env)
65 {
66     napi_value instance = nullptr;
67     napi_value constructor = nullptr;
68     napi_get_reference_value(env, classRef_, &constructor);
69     napi_new_instance(env, constructor, 0, nullptr, &instance);
70     return instance;
71 }
72 
DefineSymKeyJSClass(napi_env env)73 void NapiSymKey::DefineSymKeyJSClass(napi_env env)
74 {
75     napi_property_descriptor classDesc[] = {
76         DECLARE_NAPI_FUNCTION("getEncoded", NapiKey::JsGetEncoded),
77         DECLARE_NAPI_FUNCTION("clearMem", NapiSymKey::JsClearMem),
78         {.utf8name = "format", .getter = NapiKey::JsGetFormat},
79         {.utf8name = "algName", .getter = NapiKey::JsGetAlgorithm},
80     };
81     napi_value constructor = nullptr;
82     napi_define_class(env, "SymKey", NAPI_AUTO_LENGTH, SymKeyConstructor, nullptr,
83         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
84     napi_create_reference(env, constructor, 1, &classRef_);
85 }
86 } // CryptoFramework
87 } // OHOS
88