From ffdd00b5dce36deeef490fcc68fea688f715d115 Mon Sep 17 00:00:00 2001 From: kang1024 Date: Wed, 28 May 2025 11:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AE=97=E6=B3=95=E5=BA=93ar?= =?UTF-8?q?kts=E9=9D=99=E6=80=81=E5=8C=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.修复运行时错误Cannot find class L@ohos/security/cryptoFramework/cryptoFramework/Result; 2.修复KeyPair中的pubKey和PriKey清理不正确,导致无法重复调用KeyPair.PubKey的问题; 3.修复createAsyKeyGeneratorBySpec传入的specType不正确的问题; 4.修复bigint符号位未处理的问题; Signed-off-by: kang1024 --- ...rity.cryptoFramework.cryptoFramework.taihe | 8 ++ frameworks/js/ani/inc/ani_pri_key.h | 3 +- frameworks/js/ani/inc/ani_pub_key.h | 3 +- .../ani/src/ani_asy_key_generator_by_spec.cpp | 97 +++++++++---------- frameworks/js/ani/src/ani_common.cpp | 15 ++- frameworks/js/ani/src/ani_key_pair.cpp | 6 +- frameworks/js/ani/src/ani_pri_key.cpp | 8 +- frameworks/js/ani/src/ani_pub_key.cpp | 8 +- frameworks/js/ani/test/arktsconfig.json | 3 +- frameworks/js/ani/test/test_utils.ets | 13 +++ 10 files changed, 93 insertions(+), 71 deletions(-) diff --git a/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe b/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe index 4b6b02c..21790fa 100644 --- a/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe +++ b/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe @@ -19,6 +19,14 @@ static { loadLibrary("crypto_framework_ani.z"); } """) +enum Result: i32 { + INVALID_PARAMS = 401, + NOT_SUPPORT = 801, + ERR_OUT_OF_MEMORY = 17620001, + ERR_RUNTIME_ERROR = 17620002, + ERR_CRYPTO_OPERATION = 17630001 +} + struct DataBlob { data: @typedarray Array; } diff --git a/frameworks/js/ani/inc/ani_pri_key.h b/frameworks/js/ani/inc/ani_pri_key.h index b29768d..f052d9e 100644 --- a/frameworks/js/ani/inc/ani_pri_key.h +++ b/frameworks/js/ani/inc/ani_pri_key.h @@ -23,7 +23,7 @@ namespace ANI::CryptoFramework { class PriKeyImpl { public: PriKeyImpl(); - explicit PriKeyImpl(HcfPriKey *priKey); + explicit PriKeyImpl(HcfPriKey *priKey, bool owner = true); ~PriKeyImpl(); int64_t GetPriKeyObj(); @@ -39,6 +39,7 @@ public: private: HcfPriKey *priKey_ = nullptr; + bool owner_ = false; }; } // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/inc/ani_pub_key.h b/frameworks/js/ani/inc/ani_pub_key.h index 5efe3bf..c361597 100644 --- a/frameworks/js/ani/inc/ani_pub_key.h +++ b/frameworks/js/ani/inc/ani_pub_key.h @@ -23,7 +23,7 @@ namespace ANI::CryptoFramework { class PubKeyImpl { public: PubKeyImpl(); - explicit PubKeyImpl(HcfPubKey *pubKey); + explicit PubKeyImpl(HcfPubKey *pubKey, bool owner = true); ~PubKeyImpl(); int64_t GetPubKeyObj(); @@ -37,6 +37,7 @@ public: private: HcfPubKey *pubKey_ = nullptr; + bool owner_ = false; }; } // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp b/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp index 3f8f144..f551a92 100644 --- a/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp +++ b/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp @@ -36,7 +36,7 @@ const std::string SM2_ALG_NAME = "SM2"; void SetDSAKeyPairParamsSpecAttribute(DSAKeyPairSpec const& dsaParams, HcfDsaKeyPairParamsSpec &dsaKeyPairSpec) { - dsaKeyPairSpec.base.base.specType = HCF_KEY_PAIR_SPEC; + dsaKeyPairSpec.base.base.specType = static_cast(dsaParams.base.specType.get_value()); dsaKeyPairSpec.base.base.algName = const_cast(dsaParams.base.algName.c_str()); ArrayU8ToDataBlob(dsaParams.params.p, dsaKeyPairSpec.base.p); ArrayU8ToDataBlob(dsaParams.params.q, dsaKeyPairSpec.base.q); @@ -47,7 +47,7 @@ void SetDSAKeyPairParamsSpecAttribute(DSAKeyPairSpec const& dsaParams, HcfDsaKey void SetDSAPubKeyParamsSpecAttribute(DSAPubKeySpec const& dsaParams, HcfDsaPubKeyParamsSpec &dsaPubKeySpec) { - dsaPubKeySpec.base.base.specType = HCF_PUBLIC_KEY_SPEC; + dsaPubKeySpec.base.base.specType = static_cast(dsaParams.base.specType.get_value()); dsaPubKeySpec.base.base.algName = const_cast(dsaParams.base.algName.c_str()); ArrayU8ToDataBlob(dsaParams.params.p, dsaPubKeySpec.base.p); ArrayU8ToDataBlob(dsaParams.params.q, dsaPubKeySpec.base.q); @@ -57,7 +57,7 @@ void SetDSAPubKeyParamsSpecAttribute(DSAPubKeySpec const& dsaParams, HcfDsaPubKe void SetDSACommonParamsSpecAttribute(DSACommonParamsSpec const& dsaParams, HcfDsaCommParamsSpec &dsaCommonParamsSpec) { - dsaCommonParamsSpec.base.specType = HCF_COMMON_PARAMS_SPEC; + dsaCommonParamsSpec.base.specType = static_cast(dsaParams.base.specType.get_value()); dsaCommonParamsSpec.base.algName = const_cast(dsaParams.base.algName.c_str()); ArrayU8ToDataBlob(dsaParams.p, dsaCommonParamsSpec.p); ArrayU8ToDataBlob(dsaParams.q, dsaCommonParamsSpec.q); @@ -66,13 +66,13 @@ void SetDSACommonParamsSpecAttribute(DSACommonParamsSpec const& dsaParams, HcfDs void SetECCKeyPairParamsSpecAttribute(ECCKeyPairSpec const& eccParams, HcfEccKeyPairParamsSpec &eccKeyPairSpec) { - eccKeyPairSpec.base.base.specType = HCF_KEY_PAIR_SPEC; + eccKeyPairSpec.base.base.specType = static_cast(eccParams.base.specType.get_value()); eccKeyPairSpec.base.base.algName = const_cast(eccParams.base.algName.c_str()); if (eccParams.params.field.get_tag() == OptECField::tag_t::ECFIELD) { eccKeyPairSpec.base.field->fieldType = const_cast(eccParams.params.field.get_ECFIELD_ref().fieldType.c_str()); } else if (eccParams.params.field.get_tag() == OptECField::tag_t::ECFIELDFP) { - HcfECFieldFp* fieldFp = reinterpret_cast(eccKeyPairSpec.base.field); + HcfECFieldFp* fieldFp = reinterpret_cast(eccKeyPairSpec.base.field); fieldFp->base.fieldType = const_cast(eccParams.params.field.get_ECFIELDFP_ref().base.fieldType.c_str()); ArrayU8ToDataBlob(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); @@ -90,13 +90,13 @@ void SetECCKeyPairParamsSpecAttribute(ECCKeyPairSpec const& eccParams, HcfEccKey void SetECCPubKeyParamsSpecAttribute(ECCPubKeySpec const& eccParams, HcfEccPubKeyParamsSpec &eccPubKeySpec) { - eccPubKeySpec.base.base.specType = HCF_PUBLIC_KEY_SPEC; + eccPubKeySpec.base.base.specType = static_cast(eccParams.base.specType.get_value()); eccPubKeySpec.base.base.algName = const_cast(eccParams.base.algName.c_str()); if (eccParams.params.field.get_tag() == OptECField::tag_t::ECFIELD) { eccPubKeySpec.base.field->fieldType = const_cast(eccParams.params.field.get_ECFIELD_ref().fieldType.c_str()); } else if (eccParams.params.field.get_tag() == OptECField::tag_t::ECFIELDFP) { - HcfECFieldFp* fieldFp = reinterpret_cast(eccPubKeySpec.base.field); + HcfECFieldFp* fieldFp = reinterpret_cast(eccPubKeySpec.base.field); fieldFp->base.fieldType = const_cast(eccParams.params.field.get_ECFIELDFP_ref().base.fieldType.c_str()); ArrayU8ToDataBlob(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); @@ -113,13 +113,13 @@ void SetECCPubKeyParamsSpecAttribute(ECCPubKeySpec const& eccParams, HcfEccPubKe void SetECCPriKeyParamsSpecAttribute(ECCPriKeySpec const& eccParams, HcfEccPriKeyParamsSpec &eccPriKeySpec) { - eccPriKeySpec.base.base.specType = HCF_PRIVATE_KEY_SPEC; + eccPriKeySpec.base.base.specType = static_cast(eccParams.base.specType.get_value()); eccPriKeySpec.base.base.algName = const_cast(eccParams.base.algName.c_str()); if (eccParams.params.field.get_tag() == OptECField::tag_t::ECFIELD) { eccPriKeySpec.base.field->fieldType = const_cast(eccParams.params.field.get_ECFIELD_ref().fieldType.c_str()); } else if (eccParams.params.field.get_tag() == OptECField::tag_t::ECFIELDFP) { - HcfECFieldFp* fieldFp = reinterpret_cast(eccPriKeySpec.base.field); + HcfECFieldFp* fieldFp = reinterpret_cast(eccPriKeySpec.base.field); fieldFp->base.fieldType = const_cast(eccParams.params.field.get_ECFIELDFP_ref().base.fieldType.c_str()); ArrayU8ToDataBlob(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); @@ -135,13 +135,12 @@ void SetECCPriKeyParamsSpecAttribute(ECCPriKeySpec const& eccParams, HcfEccPriKe void SetECCCommonParamsSpecAttribute(ECCCommonParamsSpec const& eccParams, HcfEccCommParamsSpec &eccCommonParamsSpec) { - eccCommonParamsSpec.base.specType = HCF_COMMON_PARAMS_SPEC; + eccCommonParamsSpec.base.specType = static_cast(eccParams.base.specType.get_value()); eccCommonParamsSpec.base.algName = const_cast(eccParams.base.algName.c_str()); - if (eccParams.field.get_tag() == OptECField::tag_t::ECFIELD) { eccCommonParamsSpec.field->fieldType = const_cast(eccParams.field.get_ECFIELD_ref().fieldType.c_str()); } else if (eccParams.field.get_tag() == OptECField::tag_t::ECFIELDFP) { - HcfECFieldFp* fieldFp = reinterpret_cast(eccCommonParamsSpec.field); + HcfECFieldFp* fieldFp = reinterpret_cast(eccCommonParamsSpec.field); fieldFp->base.fieldType = const_cast(eccParams.field.get_ECFIELDFP_ref().base.fieldType.c_str()); ArrayU8ToDataBlob(eccParams.field.get_ECFIELDFP_ref().p, fieldFp->p); } @@ -155,7 +154,7 @@ void SetECCCommonParamsSpecAttribute(ECCCommonParamsSpec const& eccParams, HcfEc void SetRSAKeyPairParamsSpecAttribute(RSAKeyPairSpec const& rsaParams, HcfRsaKeyPairParamsSpec &rsaKeyPairSpec) { - rsaKeyPairSpec.base.base.specType = HCF_KEY_PAIR_SPEC; + rsaKeyPairSpec.base.base.specType = static_cast(rsaParams.base.specType.get_value()); rsaKeyPairSpec.base.base.algName = const_cast(rsaParams.base.algName.c_str()); ArrayU8ToDataBlob(rsaParams.params.n, rsaKeyPairSpec.base.n); ArrayU8ToDataBlob(rsaParams.pk, rsaKeyPairSpec.pk); @@ -164,23 +163,16 @@ void SetRSAKeyPairParamsSpecAttribute(RSAKeyPairSpec const& rsaParams, HcfRsaKey void SetRSAPubKeyParamsSpecAttribute(RSAPubKeySpec const& rsaParams, HcfRsaPubKeyParamsSpec &rsaPubKeySpec) { - rsaPubKeySpec.base.base.specType = HCF_PUBLIC_KEY_SPEC; + rsaPubKeySpec.base.base.specType = static_cast(rsaParams.base.specType.get_value()); rsaPubKeySpec.base.base.algName = const_cast(rsaParams.base.algName.c_str()); ArrayU8ToDataBlob(rsaParams.params.n, rsaPubKeySpec.base.n); ArrayU8ToDataBlob(rsaParams.pk, rsaPubKeySpec.pk); } -void SetRSACommonParamsSpecAttribute(RSACommonParamsSpec const& rsaParams, HcfRsaCommParamsSpec &rsaCommonParamsSpec) -{ - rsaCommonParamsSpec.base.specType = HCF_COMMON_PARAMS_SPEC; - rsaCommonParamsSpec.base.algName = const_cast(rsaParams.base.algName.c_str()); - ArrayU8ToDataBlob(rsaParams.n, rsaCommonParamsSpec.n); -} - void SetEd25519KeyPairParamsSpecAttribute(ED25519KeyPairSpec const& ed25519Params, HcfAlg25519KeyPairParamsSpec &ed25519KeyPairSpec) { - ed25519KeyPairSpec.base.specType = HCF_KEY_PAIR_SPEC; + ed25519KeyPairSpec.base.specType = static_cast(ed25519Params.base.specType.get_value()); ed25519KeyPairSpec.base.algName = const_cast(ed25519Params.base.algName.c_str()); ArrayU8ToDataBlob(ed25519Params.pk, ed25519KeyPairSpec.pk); ArrayU8ToDataBlob(ed25519Params.sk, ed25519KeyPairSpec.sk); @@ -189,7 +181,7 @@ void SetEd25519KeyPairParamsSpecAttribute(ED25519KeyPairSpec const& ed25519Param void SetEd25519PubKeyParamsSpecAttribute(ED25519PubKeySpec const& ed25519Params, HcfAlg25519PubKeyParamsSpec &ed25519PubKeySpec) { - ed25519PubKeySpec.base.specType = HCF_PUBLIC_KEY_SPEC; + ed25519PubKeySpec.base.specType = static_cast(ed25519Params.base.specType.get_value()); ed25519PubKeySpec.base.algName = const_cast(ed25519Params.base.algName.c_str()); ArrayU8ToDataBlob(ed25519Params.pk, ed25519PubKeySpec.pk); } @@ -197,7 +189,7 @@ void SetEd25519PubKeyParamsSpecAttribute(ED25519PubKeySpec const& ed25519Params, void SetEd25519PriKeyParamsSpecAttribute(ED25519PriKeySpec const& ed25519Params, HcfAlg25519PriKeyParamsSpec &ed25519PriKeySpec) { - ed25519PriKeySpec.base.specType = HCF_PRIVATE_KEY_SPEC; + ed25519PriKeySpec.base.specType = static_cast(ed25519Params.base.specType.get_value()); ed25519PriKeySpec.base.algName = const_cast(ed25519Params.base.algName.c_str()); ArrayU8ToDataBlob(ed25519Params.sk, ed25519PriKeySpec.sk); } @@ -205,7 +197,7 @@ void SetEd25519PriKeyParamsSpecAttribute(ED25519PriKeySpec const& ed25519Params, void SetX25519KeyPairParamsSpecAttribute(X25519KeyPairSpec const& x25519Params, HcfAlg25519KeyPairParamsSpec &x25519KeyPairSpec) { - x25519KeyPairSpec.base.specType = HCF_KEY_PAIR_SPEC; + x25519KeyPairSpec.base.specType = static_cast(x25519Params.base.specType.get_value()); x25519KeyPairSpec.base.algName = const_cast(x25519Params.base.algName.c_str()); ArrayU8ToDataBlob(x25519Params.pk, x25519KeyPairSpec.pk); ArrayU8ToDataBlob(x25519Params.sk, x25519KeyPairSpec.sk); @@ -214,7 +206,7 @@ void SetX25519KeyPairParamsSpecAttribute(X25519KeyPairSpec const& x25519Params, void SetX25519PubKeyParamsSpecAttribute(X25519PubKeySpec const& x25519Params, HcfAlg25519PubKeyParamsSpec &x25519PubKeySpec) { - x25519PubKeySpec.base.specType = HCF_PUBLIC_KEY_SPEC; + x25519PubKeySpec.base.specType = static_cast(x25519Params.base.specType.get_value()); x25519PubKeySpec.base.algName = const_cast(x25519Params.base.algName.c_str()); ArrayU8ToDataBlob(x25519Params.pk, x25519PubKeySpec.pk); } @@ -222,14 +214,14 @@ void SetX25519PubKeyParamsSpecAttribute(X25519PubKeySpec const& x25519Params, void SetX25519PriKeyParamsSpecAttribute(X25519PriKeySpec const& x25519Params, HcfAlg25519PriKeyParamsSpec &x25519PriKeySpec) { - x25519PriKeySpec.base.specType = HCF_PRIVATE_KEY_SPEC; + x25519PriKeySpec.base.specType = static_cast(x25519Params.base.specType.get_value()); x25519PriKeySpec.base.algName = const_cast(x25519Params.base.algName.c_str()); ArrayU8ToDataBlob(x25519Params.sk, x25519PriKeySpec.sk); } void SetDhKeyPairParamsSpecAttribute(DHKeyPairSpec const& dhParams, HcfDhKeyPairParamsSpec &dhKeyPairSpec) { - dhKeyPairSpec.base.base.specType = HCF_KEY_PAIR_SPEC; + dhKeyPairSpec.base.base.specType = static_cast(dhParams.base.specType.get_value()); dhKeyPairSpec.base.base.algName = const_cast(dhParams.base.algName.c_str()); dhKeyPairSpec.base.length = dhParams.params.l; ArrayU8ToDataBlob(dhParams.params.p, dhKeyPairSpec.base.p); @@ -240,7 +232,7 @@ void SetDhKeyPairParamsSpecAttribute(DHKeyPairSpec const& dhParams, HcfDhKeyPair void SetDhPubKeyParamsSpecAttribute(DHPubKeySpec const& dhParams, HcfDhPubKeyParamsSpec &dhPubKeySpec) { - dhPubKeySpec.base.base.specType = HCF_PUBLIC_KEY_SPEC; + dhPubKeySpec.base.base.specType = static_cast(dhParams.base.specType.get_value()); dhPubKeySpec.base.base.algName = const_cast(dhParams.base.algName.c_str()); dhPubKeySpec.base.length = dhParams.params.l; ArrayU8ToDataBlob(dhParams.params.p, dhPubKeySpec.base.p); @@ -250,7 +242,7 @@ void SetDhPubKeyParamsSpecAttribute(DHPubKeySpec const& dhParams, HcfDhPubKeyPar void SetDhPriKeyParamsSpecAttribute(DHPriKeySpec const& dhParams, HcfDhPriKeyParamsSpec &dhPriKeySpec) { - dhPriKeySpec.base.base.specType = HCF_PRIVATE_KEY_SPEC; + dhPriKeySpec.base.base.specType = static_cast(dhParams.base.specType.get_value()); dhPriKeySpec.base.base.algName = const_cast(dhParams.base.algName.c_str()); dhPriKeySpec.base.length = dhParams.params.l; ArrayU8ToDataBlob(dhParams.params.p, dhPriKeySpec.base.p); @@ -260,7 +252,7 @@ void SetDhPriKeyParamsSpecAttribute(DHPriKeySpec const& dhParams, HcfDhPriKeyPar void SetDhCommonParamsSpecAttribute(DHCommonParamsSpec const& dhParams, HcfDhCommParamsSpec &dhCommonParamsSpec) { - dhCommonParamsSpec.base.specType = HCF_COMMON_PARAMS_SPEC; + dhCommonParamsSpec.base.specType = static_cast(dhParams.base.specType.get_value()); dhCommonParamsSpec.base.algName = const_cast(dhParams.base.algName.c_str()); dhCommonParamsSpec.length = dhParams.l; ArrayU8ToDataBlob(dhParams.p, dhCommonParamsSpec.p); @@ -275,13 +267,13 @@ HcfAsyKeyParamsSpec* CreateDSASpec(OptAsyKeySpec const& asyKeySpec) if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DSAKEYPAIRSPEC) { SetDSAKeyPairParamsSpecAttribute(asyKeySpec.get_DSAKEYPAIRSPEC_ref(), dsaKeyPairSpec); - return reinterpret_cast(&dsaKeyPairSpec); + return reinterpret_cast(&dsaKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DSAPUBKEYSPEC) { SetDSAPubKeyParamsSpecAttribute(asyKeySpec.get_DSAPUBKEYSPEC_ref(), dsaPubKeySpec); - return reinterpret_cast(&dsaPubKeySpec); + return reinterpret_cast(&dsaPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DSACOMMONPARAMSSPEC) { SetDSACommonParamsSpecAttribute(asyKeySpec.get_DSACOMMONPARAMSSPEC_ref(), dsaCommonParamsSpec); - return reinterpret_cast(&dsaCommonParamsSpec); + return reinterpret_cast(&dsaCommonParamsSpec); } return nullptr; } @@ -298,19 +290,19 @@ HcfAsyKeyParamsSpec* CreateECCSpec(OptAsyKeySpec const& asyKeySpec) if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCKEYPAIRSPEC) { eccKeyPairSpec.base.field = ecField; SetECCKeyPairParamsSpecAttribute(asyKeySpec.get_ECCKEYPAIRSPEC_ref(), eccKeyPairSpec); - return reinterpret_cast(&eccKeyPairSpec); + return reinterpret_cast(&eccKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCPUBKEYSPEC) { eccPubKeySpec.base.field = ecField; SetECCPubKeyParamsSpecAttribute(asyKeySpec.get_ECCPUBKEYSPEC_ref(), eccPubKeySpec); - return reinterpret_cast(&eccPubKeySpec); + return reinterpret_cast(&eccPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCPRIKEYSPEC) { eccPriKeySpec.base.field = ecField; SetECCPriKeyParamsSpecAttribute(asyKeySpec.get_ECCPRIKEYSPEC_ref(), eccPriKeySpec); - return reinterpret_cast(&eccPriKeySpec); + return reinterpret_cast(&eccPriKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCCOMMONPARAMSSPEC) { eccCommonParamsSpec.field = ecField; SetECCCommonParamsSpecAttribute(asyKeySpec.get_ECCCOMMONPARAMSSPEC_ref(), eccCommonParamsSpec); - return reinterpret_cast(&eccCommonParamsSpec); + return reinterpret_cast(&eccCommonParamsSpec); } return nullptr; } @@ -319,17 +311,16 @@ HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec) { static HcfRsaKeyPairParamsSpec rsaKeyPairSpec = {}; static HcfRsaPubKeyParamsSpec rsaPubKeySpec = {}; - static HcfRsaCommParamsSpec rsaCommonParamsSpec = {}; if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::RSAKEYPAIRSPEC) { SetRSAKeyPairParamsSpecAttribute(asyKeySpec.get_RSAKEYPAIRSPEC_ref(), rsaKeyPairSpec); - return reinterpret_cast(&rsaKeyPairSpec); + return reinterpret_cast(&rsaKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::RSAPUBKEYSPEC) { SetRSAPubKeyParamsSpecAttribute(asyKeySpec.get_RSAPUBKEYSPEC_ref(), rsaPubKeySpec); - return reinterpret_cast(&rsaPubKeySpec); + return reinterpret_cast(&rsaPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::RSACOMMONPARAMSSPEC) { - SetRSACommonParamsSpecAttribute(asyKeySpec.get_RSACOMMONPARAMSSPEC_ref(), rsaCommonParamsSpec); - return reinterpret_cast(&rsaCommonParamsSpec); + LOGE("RSA not support comm key spec"); + return nullptr; } return nullptr; } @@ -342,13 +333,13 @@ HcfAsyKeyParamsSpec* CreateEd25519Spec(OptAsyKeySpec const& asyKeySpec) if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ED25519KEYPAIRSPEC) { SetEd25519KeyPairParamsSpecAttribute(asyKeySpec.get_ED25519KEYPAIRSPEC_ref(), ed25519KeyPairSpec); - return reinterpret_cast(&ed25519KeyPairSpec); + return reinterpret_cast(&ed25519KeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ED25519PUBKEYSPEC) { SetEd25519PubKeyParamsSpecAttribute(asyKeySpec.get_ED25519PUBKEYSPEC_ref(), ed25519PubKeySpec); - return reinterpret_cast(&ed25519PubKeySpec); + return reinterpret_cast(&ed25519PubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ED25519PRIKEYSPEC) { SetEd25519PriKeyParamsSpecAttribute(asyKeySpec.get_ED25519PRIKEYSPEC_ref(), ed25519PriKeySpec); - return reinterpret_cast(&ed25519PriKeySpec); + return reinterpret_cast(&ed25519PriKeySpec); } return nullptr; } @@ -361,13 +352,13 @@ HcfAsyKeyParamsSpec* CreateX25519Spec(OptAsyKeySpec const& asyKeySpec) if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::X25519KEYPAIRSPEC) { SetX25519KeyPairParamsSpecAttribute(asyKeySpec.get_X25519KEYPAIRSPEC_ref(), x25519KeyPairSpec); - return reinterpret_cast(&x25519KeyPairSpec); + return reinterpret_cast(&x25519KeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::X25519PUBKEYSPEC) { SetX25519PubKeyParamsSpecAttribute(asyKeySpec.get_X25519PUBKEYSPEC_ref(), x25519PubKeySpec); - return reinterpret_cast(&x25519PubKeySpec); + return reinterpret_cast(&x25519PubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::X25519PRIKEYSPEC) { SetX25519PriKeyParamsSpecAttribute(asyKeySpec.get_X25519PRIKEYSPEC_ref(), x25519PriKeySpec); - return reinterpret_cast(&x25519PriKeySpec); + return reinterpret_cast(&x25519PriKeySpec); } return nullptr; } @@ -381,16 +372,16 @@ HcfAsyKeyParamsSpec* CreateDHSpec(OptAsyKeySpec const& asyKeySpec) if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHKEYPAIRSPEC) { SetDhKeyPairParamsSpecAttribute(asyKeySpec.get_DHKEYPAIRSPEC_ref(), dhKeyPairSpec); - return reinterpret_cast(&dhKeyPairSpec); + return reinterpret_cast(&dhKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHPUBKEYSPEC) { SetDhPubKeyParamsSpecAttribute(asyKeySpec.get_DHPUBKEYSPEC_ref(), dhPubKeySpec); - return reinterpret_cast(&dhPubKeySpec); + return reinterpret_cast(&dhPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHPRIKEYSPEC) { SetDhPriKeyParamsSpecAttribute(asyKeySpec.get_DHPRIKEYSPEC_ref(), dhPriKeySpec); - return reinterpret_cast(&dhPriKeySpec); + return reinterpret_cast(&dhPriKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHCOMMONPARAMSSPEC) { SetDhCommonParamsSpecAttribute(asyKeySpec.get_DHCOMMONPARAMSSPEC_ref(), dhCommonParamsSpec); - return reinterpret_cast(&dhCommonParamsSpec); + return reinterpret_cast(&dhCommonParamsSpec); } return nullptr; } diff --git a/frameworks/js/ani/src/ani_common.cpp b/frameworks/js/ani/src/ani_common.cpp index 428b8b5..5d21345 100644 --- a/frameworks/js/ani/src/ani_common.cpp +++ b/frameworks/js/ani/src/ani_common.cpp @@ -88,14 +88,21 @@ int ConvertResultCode(HcfResult res) return ERR_RUNTIME_ERROR; } -template void ArrayU8ToDataBlob(const array &arr, HcfBlob &blob); -template void ArrayU8ToDataBlob(const array &arr, HcfBigInteger &blob); +template<> +void ArrayU8ToDataBlob(const array &arr, HcfBlob &blob) +{ + blob.data = arr.empty() ? nullptr : arr.data(); + blob.len = arr.size(); +} -template -void ArrayU8ToDataBlob(const array &arr, T &blob) +template<> +void ArrayU8ToDataBlob(const array &arr, HcfBigInteger &blob) { blob.data = arr.empty() ? nullptr : arr.data(); blob.len = arr.size(); + if (blob.len > 0 && blob.data[blob.len - 1] == 0) { // remove the sign bit of big integer + blob.len--; + } } template<> diff --git a/frameworks/js/ani/src/ani_key_pair.cpp b/frameworks/js/ani/src/ani_key_pair.cpp index 6bc5c4d..1b1f335 100644 --- a/frameworks/js/ani/src/ani_key_pair.cpp +++ b/frameworks/js/ani/src/ani_key_pair.cpp @@ -35,8 +35,7 @@ PriKey KeyPairImpl::GetPriKey() return make_holder(); } HcfPriKey *priKey = this->keyPair_->priKey; - this->keyPair_->priKey = nullptr; - return make_holder(priKey); + return make_holder(priKey, false); } PubKey KeyPairImpl::GetPubKey() @@ -46,7 +45,6 @@ PubKey KeyPairImpl::GetPubKey() return make_holder(); } HcfPubKey *pubKey = this->keyPair_->pubKey; - this->keyPair_->pubKey = nullptr; - return make_holder(pubKey); + return make_holder(pubKey, false); } } // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/src/ani_pri_key.cpp b/frameworks/js/ani/src/ani_pri_key.cpp index defb6af..ae5a9de 100644 --- a/frameworks/js/ani/src/ani_pri_key.cpp +++ b/frameworks/js/ani/src/ani_pri_key.cpp @@ -78,12 +78,14 @@ string GetEncodedPemInner(const HcfPriKey *self, HcfParamsSpec *params, string_v namespace ANI::CryptoFramework { PriKeyImpl::PriKeyImpl() {} -PriKeyImpl::PriKeyImpl(HcfPriKey *priKey) : priKey_(priKey) {} +PriKeyImpl::PriKeyImpl(HcfPriKey *priKey, bool owner /* = false */) : priKey_(priKey), owner_(owner) {} PriKeyImpl::~PriKeyImpl() { - HcfObjDestroy(this->priKey_); - this->priKey_ = nullptr; + if (this->owner_) { + HcfObjDestroy(this->priKey_); + this->priKey_ = nullptr; + } } int64_t PriKeyImpl::GetPriKeyObj() diff --git a/frameworks/js/ani/src/ani_pub_key.cpp b/frameworks/js/ani/src/ani_pub_key.cpp index fa68b34..6b68158 100644 --- a/frameworks/js/ani/src/ani_pub_key.cpp +++ b/frameworks/js/ani/src/ani_pub_key.cpp @@ -61,12 +61,14 @@ OptKeySpec GetAsyKeySpecBigInt(HcfPubKey *pubKey, HcfAsyKeySpecItem item) namespace ANI::CryptoFramework { PubKeyImpl::PubKeyImpl() {} -PubKeyImpl::PubKeyImpl(HcfPubKey *pubKey) : pubKey_(pubKey) {} +PubKeyImpl::PubKeyImpl(HcfPubKey *pubKey, bool owner /* = false */) : pubKey_(pubKey), owner_(owner) {} PubKeyImpl::~PubKeyImpl() { - HcfObjDestroy(this->pubKey_); - this->pubKey_ = nullptr; + if (this->owner_) { + HcfObjDestroy(this->pubKey_); + this->pubKey_ = nullptr; + } } int64_t PubKeyImpl::GetPubKeyObj() diff --git a/frameworks/js/ani/test/arktsconfig.json b/frameworks/js/ani/test/arktsconfig.json index 53466af..75d080a 100644 --- a/frameworks/js/ani/test/arktsconfig.json +++ b/frameworks/js/ani/test/arktsconfig.json @@ -20,7 +20,6 @@ ], "#files": [ "./test_utils.ets", - "./test_main.ets", - "./test_rand.ets" + "./test_main.ets" ] } diff --git a/frameworks/js/ani/test/test_utils.ets b/frameworks/js/ani/test/test_utils.ets index 2eff593..f5db02f 100644 --- a/frameworks/js/ani/test/test_utils.ets +++ b/frameworks/js/ani/test/test_utils.ets @@ -55,6 +55,19 @@ export function uint8ArrayToString(data: Uint8Array): string { } return str; } + +export function doFullGC(): void { + try { + GC.waitForFinishGC(GC.startGC(GC.Cause.FULL, GC.IN_PLACE_MODE)); + } catch (err) { + assertTrue(false, "Unexpected exception during GC"); + } + Coroutine.Schedule(); +} + +export function StopTaskPool(): void { + CoroutineExtras.stopTaskpool(); +} } // namespace utils export default utils; -- Gitee