diff --git a/frameworks/js/napi/inc/napi_key.h b/frameworks/js/napi/inc/napi_key.h index 4213312f4b8aa810bd0d738b6e9452cdfc212b82..683206e0375856d893daeb6c92822238111f5417 100644 --- a/frameworks/js/napi/inc/napi_key.h +++ b/frameworks/js/napi/inc/napi_key.h @@ -26,12 +26,11 @@ namespace OHOS { namespace CryptoFramework { class NapiKey { public: - NapiKey(HcfKey *symKey); - ~NapiKey(); + NapiKey(HcfKey *hcfKey); + virtual ~NapiKey(); HcfKey *GetHcfKey(); static void DefineHcfKeyJSClass(napi_env env); - static napi_value CreateHcfKey(napi_env env); static napi_value KeyConstructor(napi_env env, napi_callback_info info); static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/inc/napi_pri_key.h b/frameworks/js/napi/inc/napi_pri_key.h index df3447d289545993cb05c2de27baec622ed850d9..011fbfb18496f599c0ad3ed4868ff8bcb775b24a 100644 --- a/frameworks/js/napi/inc/napi_pri_key.h +++ b/frameworks/js/napi/inc/napi_pri_key.h @@ -19,12 +19,13 @@ #include #include "log.h" #include "pri_key.h" +#include "napi_key.h" #include "napi/native_api.h" #include "napi/native_node_api.h" namespace OHOS { namespace CryptoFramework { -class NapiPriKey { +class NapiPriKey : public NapiKey { public: NapiPriKey(HcfPriKey *priKey); ~NapiPriKey(); @@ -39,9 +40,6 @@ public: static napi_value JsClearMem(napi_env env, napi_callback_info info); static thread_local napi_ref classRef_; - -private: - HcfPriKey *priKey_ = nullptr; }; } // namespace CryptoFramework } // namespace OHOS diff --git a/frameworks/js/napi/inc/napi_pub_key.h b/frameworks/js/napi/inc/napi_pub_key.h index eee18000d37f0b5930442cb1f839cafa2278fcec..bcbca925f307095236887426e5084d8b1b109595 100644 --- a/frameworks/js/napi/inc/napi_pub_key.h +++ b/frameworks/js/napi/inc/napi_pub_key.h @@ -19,12 +19,13 @@ #include #include "log.h" #include "pub_key.h" +#include "napi_key.h" #include "napi/native_api.h" #include "napi/native_node_api.h" namespace OHOS { namespace CryptoFramework { -class NapiPubKey { +class NapiPubKey : public NapiKey { public: NapiPubKey(HcfPubKey *pubKey); ~NapiPubKey(); @@ -38,9 +39,6 @@ public: static napi_value JsGetEncoded(napi_env env, napi_callback_info info); static thread_local napi_ref classRef_; - -private: - HcfPubKey *pubKey_ = nullptr; }; } // namespace CryptoFramework } // namespace OHOS diff --git a/frameworks/js/napi/inc/napi_sym_key.h b/frameworks/js/napi/inc/napi_sym_key.h index 88c4f98962b70f6f0d0121873847a0a5c5d4cb07..4a3c646a59011e2159cee0fd72b8d59988141a59 100644 --- a/frameworks/js/napi/inc/napi_sym_key.h +++ b/frameworks/js/napi/inc/napi_sym_key.h @@ -18,13 +18,14 @@ #include #include "log.h" +#include "napi_key.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "sym_key.h" namespace OHOS { namespace CryptoFramework { -class NapiSymKey { +class NapiSymKey : public NapiKey { public: NapiSymKey(HcfSymKey *symKey); ~NapiSymKey(); @@ -33,14 +34,9 @@ public: static void DefineSymKeyJSClass(napi_env env); static napi_value CreateSymKey(napi_env env); static napi_value SymKeyConstructor(napi_env env, napi_callback_info info); - - static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info); - static napi_value JsGetEncoded(napi_env env, napi_callback_info info); - static napi_value JsGetFormat(napi_env env, napi_callback_info info); + static napi_value JsClearMem(napi_env env, napi_callback_info info); static thread_local napi_ref classRef_; -private: - HcfSymKey *symKey_; }; } // namespace CryptoFramework } // namespace OHOS diff --git a/frameworks/js/napi/src/napi_key.cpp b/frameworks/js/napi/src/napi_key.cpp index 832f4b3c8613ee0529d107d8aa70d5d3fc52e57a..77d97bc40bb7676bcc92aa9132bce4163f7edf99 100644 --- a/frameworks/js/napi/src/napi_key.cpp +++ b/frameworks/js/napi/src/napi_key.cpp @@ -33,6 +33,7 @@ NapiKey::NapiKey(HcfKey *hcfKey) NapiKey::~NapiKey() { OH_HCF_OBJ_DESTROY(this->hcfKey_); + this->hcfKey_ = nullptr; } HcfKey *NapiKey::GetHcfKey() @@ -44,14 +45,14 @@ napi_value NapiKey::JsGetAlgorithm(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; NapiKey *napiKey = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - (void)napi_unwrap(env, thisVar, (void **)&napiKey); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&napiKey))); HcfKey *key = napiKey->GetHcfKey(); const char *algo = key->getAlgorithm(key); napi_value instance = nullptr; - napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance); + napi_create_string_utf8(env, algo, NAPI_AUTO_LENGTH, &instance); return instance; } @@ -59,14 +60,14 @@ napi_value NapiKey::JsGetFormat(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; NapiKey *napiKey = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - (void)napi_unwrap(env, thisVar, (void **)&napiKey); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&napiKey))); HcfKey *key = napiKey->GetHcfKey(); const char *format = key->getFormat(key); napi_value instance = nullptr; - napi_create_string_utf8(env, (const char *)format, NAPI_AUTO_LENGTH, &instance); + napi_create_string_utf8(env, format, NAPI_AUTO_LENGTH, &instance); return instance; } @@ -74,14 +75,15 @@ napi_value NapiKey::JsGetEncoded(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; NapiKey *napiKey = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - (void)napi_unwrap(env, thisVar, (void **)&napiKey); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&napiKey))); HcfKey *key = napiKey->GetHcfKey(); HcfBlob blob = {0}; HcfResult res = key->getEncoded(key, &blob); if (res != 0) { + napi_throw(env, GenerateBusinessError(env, res, "getEncoded failed.")); LOGE("getEncoded failed!"); return nullptr; } @@ -97,15 +99,6 @@ napi_value NapiKey::KeyConstructor(napi_env env, napi_callback_info info) return thisVar; } -napi_value NapiKey::CreateHcfKey(napi_env env) -{ - napi_value instance; - napi_value constructor = nullptr; - napi_get_reference_value(env, classRef_, &constructor); - napi_new_instance(env, constructor, 0, nullptr, &instance); - return instance; -} - void NapiKey::DefineHcfKeyJSClass(napi_env env) { napi_property_descriptor classDesc[] = { diff --git a/frameworks/js/napi/src/napi_pri_key.cpp b/frameworks/js/napi/src/napi_pri_key.cpp index 8d13c7eb2099ebaf86e7fea1a4bb15cdb477e04d..6c406eca929b549dabb7836966355497c2691742 100644 --- a/frameworks/js/napi/src/napi_pri_key.cpp +++ b/frameworks/js/napi/src/napi_pri_key.cpp @@ -25,16 +25,13 @@ namespace OHOS { namespace CryptoFramework { thread_local napi_ref NapiPriKey::classRef_ = nullptr; -NapiPriKey::NapiPriKey(HcfPriKey *priKey) -{ - this->priKey_ = priKey; -} +NapiPriKey::NapiPriKey(HcfPriKey *priKey) : NapiKey(reinterpret_cast(priKey)) {} NapiPriKey::~NapiPriKey() {} HcfPriKey *NapiPriKey::GetPriKey() { - return this->priKey_; + return reinterpret_cast(NapiKey::GetHcfKey()); } napi_value NapiPriKey::PriKeyConstructor(napi_env env, napi_callback_info info) @@ -57,8 +54,8 @@ napi_value NapiPriKey::ConvertToJsPriKey(napi_env env) napi_get_reference_value(env, classRef_, &constructor); napi_new_instance(env, constructor, 0, nullptr, &instance); - const char *algName = this->priKey_->base.getAlgorithm(&(this->priKey_->base)); - const char *format = this->priKey_->base.getFormat(&(this->priKey_->base)); + const char *algName = this->GetPriKey()->base.getAlgorithm(&(this->GetPriKey()->base)); + const char *format = this->GetPriKey()->base.getFormat(&(this->GetPriKey()->base)); napi_value napiAlgName = nullptr; napi_create_string_utf8(env, algName, NAPI_AUTO_LENGTH, &napiAlgName); diff --git a/frameworks/js/napi/src/napi_pub_key.cpp b/frameworks/js/napi/src/napi_pub_key.cpp index 6e91f71fbf1c9163cbcb613b7980e86abb7c3b89..7acd8072ae933d323a05205c5b82717d408c2d57 100644 --- a/frameworks/js/napi/src/napi_pub_key.cpp +++ b/frameworks/js/napi/src/napi_pub_key.cpp @@ -25,16 +25,13 @@ namespace OHOS { namespace CryptoFramework { thread_local napi_ref NapiPubKey::classRef_ = nullptr; -NapiPubKey::NapiPubKey(HcfPubKey *pubKey) -{ - this->pubKey_ = pubKey; -} +NapiPubKey::NapiPubKey(HcfPubKey *pubKey) : NapiKey(reinterpret_cast(pubKey)) {} NapiPubKey::~NapiPubKey() {} HcfPubKey *NapiPubKey::GetPubKey() { - return this->pubKey_; + return reinterpret_cast(NapiKey::GetHcfKey()); } napi_value NapiPubKey::PubKeyConstructor(napi_env env, napi_callback_info info) @@ -57,8 +54,8 @@ napi_value NapiPubKey::ConvertToJsPubKey(napi_env env) napi_get_reference_value(env, classRef_, &constructor); napi_new_instance(env, constructor, 0, nullptr, &instance); - const char *algName = this->pubKey_->base.getAlgorithm(&(this->pubKey_->base)); - const char *format = this->pubKey_->base.getFormat(&(this->pubKey_->base)); + const char *algName = this->GetPubKey()->base.getAlgorithm(&(this->GetPubKey()->base)); + const char *format = this->GetPubKey()->base.getFormat(&(this->GetPubKey()->base)); napi_value napiAlgName = nullptr; napi_create_string_utf8(env, algName, NAPI_AUTO_LENGTH, &napiAlgName); diff --git a/frameworks/js/napi/src/napi_sym_key.cpp b/frameworks/js/napi/src/napi_sym_key.cpp index ec80319687a33e1b4230b77fbb919376d8702ba2..842a9bad049ca296752ef4c08d543eb2fc4f4779 100644 --- a/frameworks/js/napi/src/napi_sym_key.cpp +++ b/frameworks/js/napi/src/napi_sym_key.cpp @@ -25,70 +25,25 @@ namespace OHOS { namespace CryptoFramework { thread_local napi_ref NapiSymKey::classRef_ = nullptr; -NapiSymKey::NapiSymKey(HcfSymKey *symKey) -{ - this->symKey_ = symKey; -} +NapiSymKey::NapiSymKey(HcfSymKey *symKey) : NapiKey(reinterpret_cast(symKey)) {} -NapiSymKey::~NapiSymKey() -{ - OH_HCF_OBJ_DESTROY(this->symKey_); -} +NapiSymKey::~NapiSymKey() {} HcfSymKey *NapiSymKey::GetSymKey() { - return this->symKey_; -} - -napi_value NapiSymKey::JsGetAlgorithm(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - NapiSymKey *napiSymKey = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKey)); - HcfSymKey *key = napiSymKey->GetSymKey(); - - const char *algo = key->key.getAlgorithm((HcfKey *)key); - napi_value instance = nullptr; - napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance); - return instance; -} - -napi_value NapiSymKey::JsGetFormat(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - NapiSymKey *napiSymKey = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKey)); - HcfSymKey *key = napiSymKey->GetSymKey(); - - const char *format = key->key.getFormat((HcfKey *)key); - napi_value instance = nullptr; - napi_create_string_utf8(env, (const char *)format, NAPI_AUTO_LENGTH, &instance); - return instance; + return reinterpret_cast(NapiKey::GetHcfKey()); } -napi_value NapiSymKey::JsGetEncoded(napi_env env, napi_callback_info info) +napi_value NapiSymKey::JsClearMem(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; NapiSymKey *napiSymKey = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKey)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&napiSymKey))); HcfSymKey *key = napiSymKey->GetSymKey(); - - HcfBlob blob = {0}; - HcfResult res = key->key.getEncoded((HcfKey *)key, &blob); - if (res != 0) { - napi_throw(env, GenerateBusinessError(env, res, "getEncoded failed.")); - LOGE("getEncoded failed!"); - return nullptr; - } - napi_value instance = ConvertBlobToNapiValue(env, &blob); - HcfFree(blob.data); - return instance; + key->clearMem(key); + return nullptr; } napi_value NapiSymKey::SymKeyConstructor(napi_env env, napi_callback_info info) @@ -110,9 +65,10 @@ napi_value NapiSymKey::CreateSymKey(napi_env env) void NapiSymKey::DefineSymKeyJSClass(napi_env env) { napi_property_descriptor classDesc[] = { - DECLARE_NAPI_FUNCTION("getEncoded", NapiSymKey::JsGetEncoded), - {.utf8name = "format", .getter = NapiSymKey::JsGetFormat}, - {.utf8name = "algName", .getter = NapiSymKey::JsGetAlgorithm}, + DECLARE_NAPI_FUNCTION("getEncoded", NapiKey::JsGetEncoded), + DECLARE_NAPI_FUNCTION("clearMem", NapiSymKey::JsClearMem), + {.utf8name = "format", .getter = NapiKey::JsGetFormat}, + {.utf8name = "algName", .getter = NapiKey::JsGetAlgorithm}, }; napi_value constructor = nullptr; napi_define_class(env, "SymKey", NAPI_AUTO_LENGTH, SymKeyConstructor, nullptr, diff --git a/frameworks/js/napi/src/napi_sym_key_generator.cpp b/frameworks/js/napi/src/napi_sym_key_generator.cpp index 71d08d59601afd8830444909124e63d5076d48bd..77120a98429477d057e75cafbc49feac1111b93f 100644 --- a/frameworks/js/napi/src/napi_sym_key_generator.cpp +++ b/frameworks/js/napi/src/napi_sym_key_generator.cpp @@ -18,7 +18,7 @@ #include "securec.h" #include "log.h" #include "memory.h" -#include "napi_key.h" +#include "napi_sym_key.h" #include "napi_utils.h" #include "napi_crypto_framework_defines.h" @@ -195,27 +195,28 @@ static void AsyncGenKeyProcess(napi_env env, void *data) static void AsyncKeyReturn(napi_env env, napi_status status, void *data) { - napi_value instance = NapiKey::CreateHcfKey(env); + napi_value instance = NapiSymKey::CreateSymKey(env); SymKeyGeneratorFwkCtx context = static_cast(data); - NapiKey *napiKey = new (std::nothrow) NapiKey((HcfKey *)context->returnSymKey); - if (napiKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key failed.")); + NapiSymKey *napiSymKey = new (std::nothrow) NapiSymKey(context->returnSymKey); + if (napiSymKey == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi sym key failed.")); FreeSymKeyGeneratorFwkCtx(env, context); - LOGE("new napi key failed."); + LOGE("new napi sym key failed."); return; } - napi_status ret = napi_wrap(env, instance, napiKey, + napi_status ret = napi_wrap(env, instance, napiSymKey, [](napi_env env, void *data, void *hint) { - NapiKey *napiKey = static_cast(data); - delete napiKey; + NapiSymKey *napiSymKey = static_cast(data); + delete napiSymKey; return; }, nullptr, nullptr); if (ret != napi_ok) { LOGE("failed to wrap napiSymKey obj!"); context->errCode = HCF_INVALID_PARAMS; - delete napiKey; + context->errMsg = "failed to wrap napiSymKey obj!"; + delete napiSymKey; } if (context->asyncType == ASYNC_CALLBACK) { diff --git a/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c b/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c index 05598cac0e622c151ec8bb37cc26a12968bcdbdb..5773ce7730c10cb0bb8b25368f17bb1989d718ba 100644 --- a/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c +++ b/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c @@ -58,6 +58,22 @@ static HcfResult GetEncoded(HcfKey *self, HcfBlob *key) return HCF_SUCCESS; } +static void ClearMem(HcfSymKey *self) +{ + if (self == NULL) { + LOGE("symKey is NULL."); + return; + } + if (!IsClassMatch((const HcfObjectBase *)self, OPENSSL_SYM_KEY_CLASS)) { + LOGE("Class is not match."); + return; + } + SymKeyImpl *impl = (SymKeyImpl *)self; + if ((impl->keyMaterial.data != NULL) && (impl->keyMaterial.len > 0)) { + (void)memset_s(impl->keyMaterial.data, impl->keyMaterial.len, 0, impl->keyMaterial.len); + } +} + static const char *GetFormat(HcfKey *self) { if (self == NULL) { @@ -249,6 +265,7 @@ static HcfResult GenerateSymmKey(OH_HCF_SymKeyGeneratorSpi *self, HcfSymKey **sy return res; } returnSymmKey->algoName = GetAlgoName(impl); + returnSymmKey->key.clearMem = ClearMem; returnSymmKey->key.key.getEncoded = GetEncoded; returnSymmKey->key.key.getFormat = GetFormat; returnSymmKey->key.key.getAlgorithm = GetAlgorithm; @@ -286,6 +303,7 @@ static HcfResult ConvertSymmKey(OH_HCF_SymKeyGeneratorSpi *self, const HcfBlob * return res; } returnSymmKey->algoName = GetAlgoName(impl); + returnSymmKey->key.clearMem = ClearMem; returnSymmKey->key.key.getEncoded = GetEncoded; returnSymmKey->key.key.getFormat = GetFormat; returnSymmKey->key.key.getAlgorithm = GetAlgorithm;