• 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_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 NapiKey::classRef_ = nullptr;
26 
NapiKey(HcfKey * hcfKey)27 NapiKey::NapiKey(HcfKey *hcfKey)
28 {
29     this->hcfKey_ = hcfKey;
30 }
31 
~NapiKey()32 NapiKey::~NapiKey() {}
33 
GetHcfKey() const34 HcfKey *NapiKey::GetHcfKey() const
35 {
36     return this->hcfKey_;
37 }
38 
JsGetAlgorithm(napi_env env,napi_callback_info info)39 napi_value NapiKey::JsGetAlgorithm(napi_env env, napi_callback_info info)
40 {
41     napi_value thisVar = nullptr;
42     NapiKey *napiKey = nullptr;
43     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
44 
45     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiKey)));
46     HcfKey *key = napiKey->GetHcfKey();
47 
48     const char *algo = key->getAlgorithm(key);
49     napi_value instance = nullptr;
50     napi_create_string_utf8(env, algo, NAPI_AUTO_LENGTH, &instance);
51     return instance;
52 }
53 
JsGetFormat(napi_env env,napi_callback_info info)54 napi_value NapiKey::JsGetFormat(napi_env env, napi_callback_info info)
55 {
56     napi_value thisVar = nullptr;
57     NapiKey *napiKey = nullptr;
58     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
59 
60     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiKey)));
61     HcfKey *key = napiKey->GetHcfKey();
62 
63     const char *format = key->getFormat(key);
64     napi_value instance = nullptr;
65     napi_create_string_utf8(env, format, NAPI_AUTO_LENGTH, &instance);
66     return instance;
67 }
68 
JsGetEncoded(napi_env env,napi_callback_info info)69 napi_value NapiKey::JsGetEncoded(napi_env env, napi_callback_info info)
70 {
71     napi_value thisVar = nullptr;
72     NapiKey *napiKey = nullptr;
73     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
74 
75     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiKey)));
76     HcfKey *key = napiKey->GetHcfKey();
77 
78     HcfBlob blob = { .data = nullptr, .len = 0 };
79     HcfResult res = key->getEncoded(key, &blob);
80     if (res != 0) {
81         napi_throw(env, GenerateBusinessError(env, res, "getEncoded failed.", false));
82         LOGE("getEncoded failed!");
83         return nullptr;
84     }
85     napi_value instance = ConvertBlobToNapiValue(env, &blob);
86     HcfBlobDataFree(&blob);
87     return instance;
88 }
89 
KeyConstructor(napi_env env,napi_callback_info info)90 napi_value NapiKey::KeyConstructor(napi_env env, napi_callback_info info)
91 {
92     napi_value thisVar = nullptr;
93     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
94     return thisVar;
95 }
96 
DefineHcfKeyJSClass(napi_env env)97 void NapiKey::DefineHcfKeyJSClass(napi_env env)
98 {
99     napi_property_descriptor classDesc[] = {
100         DECLARE_NAPI_FUNCTION("getEncoded", NapiKey::JsGetEncoded),
101         {.utf8name = "format", .getter = NapiKey::JsGetFormat},
102         {.utf8name = "algName", .getter = NapiKey::JsGetAlgorithm},
103     };
104     napi_value constructor = nullptr;
105     napi_define_class(env, "HcfKey", NAPI_AUTO_LENGTH, KeyConstructor, nullptr,
106         sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
107     napi_create_reference(env, constructor, 1, &classRef_);
108 }
109 } // CryptoFramework
110 } // OHOS