From 085b08c42d457a952a1d5358cd8e1656472a31f6 Mon Sep 17 00:00:00 2001 From: xwb Date: Mon, 28 Nov 2022 17:26:37 +0800 Subject: [PATCH] fix bug (AES and RSA crash with abnormal input) Signed-off-by: xwb --- frameworks/certificate/cert_chain_validator.c | 2 +- frameworks/certificate/x509_certificate.c | 2 +- frameworks/certificate/x509_crl.c | 2 +- frameworks/key/sym_key_generator.c | 2 +- .../hcfciphercreate_fuzzer/hcfciphercreate_fuzzer.cpp | 2 +- test/unittest/src/crypto_rsa_asy_key_generator_test.cpp | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/frameworks/certificate/cert_chain_validator.c b/frameworks/certificate/cert_chain_validator.c index 60c63ce..09fa4d2 100644 --- a/frameworks/certificate/cert_chain_validator.c +++ b/frameworks/certificate/cert_chain_validator.c @@ -53,7 +53,7 @@ static const HcfCertChainValidatorAbility CERT_PATH_VALIDATOR_ABILITY_SET[] = { static const HcfCertChainValidatorFuncSet *FindAbility(const char *algorithm) { - for (uint32_t i = 0; i < sizeof(CERT_PATH_VALIDATOR_ABILITY_SET); i++) { + for (uint32_t i = 0; i < sizeof(CERT_PATH_VALIDATOR_ABILITY_SET) / sizeof(HcfCertChainValidatorAbility); i++) { if (strcmp(CERT_PATH_VALIDATOR_ABILITY_SET[i].algorithm, algorithm) == 0) { return &(CERT_PATH_VALIDATOR_ABILITY_SET[i].funcSet); } diff --git a/frameworks/certificate/x509_certificate.c b/frameworks/certificate/x509_certificate.c index 4095f99..abadcf4 100644 --- a/frameworks/certificate/x509_certificate.c +++ b/frameworks/certificate/x509_certificate.c @@ -50,7 +50,7 @@ static const HcfX509CertificateFuncSet *FindAbility(const char *certType) LOGE("CertType is null!"); return NULL; } - for (uint32_t i = 0; i < sizeof(X509_CERTIFICATE_ABILITY_SET); i++) { + for (uint32_t i = 0; i < sizeof(X509_CERTIFICATE_ABILITY_SET) / sizeof(HcfCCertFactoryAbility); i++) { if (strcmp(X509_CERTIFICATE_ABILITY_SET[i].certType, certType) == 0) { return &(X509_CERTIFICATE_ABILITY_SET[i].funcSet); } diff --git a/frameworks/certificate/x509_crl.c b/frameworks/certificate/x509_crl.c index 4109df0..4ed3818 100644 --- a/frameworks/certificate/x509_crl.c +++ b/frameworks/certificate/x509_crl.c @@ -60,7 +60,7 @@ static const HcfX509CrlFuncSet *FindAbility(const char *certType) LOGE("CertType is null!"); return NULL; } - for (uint32_t i = 0; i < sizeof(X509_CRL_ABILITY_SET); i++) { + for (uint32_t i = 0; i < sizeof(X509_CRL_ABILITY_SET) / sizeof(HcfCCertFactoryAbility); i++) { if (strcmp(X509_CRL_ABILITY_SET[i].certType, certType) == 0) { return &(X509_CRL_ABILITY_SET[i].funcSet); } diff --git a/frameworks/key/sym_key_generator.c b/frameworks/key/sym_key_generator.c index fce9ebc..465de2f 100644 --- a/frameworks/key/sym_key_generator.c +++ b/frameworks/key/sym_key_generator.c @@ -57,7 +57,7 @@ static const SymKeyGenFuncSet *FindAbility(SymKeyAttr *attr) if (attr == NULL) { return NULL; } - for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET); i++) { + for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET) / sizeof(SymKeyGenAbility); i++) { if (SYMKEY_ABILITY_SET[i].algo == attr->algo) { return &(SYMKEY_ABILITY_SET[i].funcSet); } diff --git a/test/fuzztest/crypto_operation/hcfciphercreate_fuzzer/hcfciphercreate_fuzzer.cpp b/test/fuzztest/crypto_operation/hcfciphercreate_fuzzer/hcfciphercreate_fuzzer.cpp index e088d16..addfee1 100755 --- a/test/fuzztest/crypto_operation/hcfciphercreate_fuzzer/hcfciphercreate_fuzzer.cpp +++ b/test/fuzztest/crypto_operation/hcfciphercreate_fuzzer/hcfciphercreate_fuzzer.cpp @@ -159,7 +159,7 @@ namespace OHOS { return; } - HcfBlob input = {.data = reinterpret_cast(plan), .len = strlen((char *)plan)}; + HcfBlob input = { .data = plan, .len = strlen(reinterpret_cast(plan)) }; HcfBlob encoutput = {.data = nullptr, .len = 0}; HcfCipher *cipher = nullptr; res = HcfCipherCreate("RSA1024|PKCS1", &cipher); diff --git a/test/unittest/src/crypto_rsa_asy_key_generator_test.cpp b/test/unittest/src/crypto_rsa_asy_key_generator_test.cpp index b9e9afb..dfa43e9 100644 --- a/test/unittest/src/crypto_rsa_asy_key_generator_test.cpp +++ b/test/unittest/src/crypto_rsa_asy_key_generator_test.cpp @@ -425,8 +425,8 @@ HWTEST_F(CryptoRsaAsyKeyGeneratorTest, CryptoRsaAsyKeyGeneratorTest511, TestSize HcfKeyPair *keyPair = NULL; res = generator->generateKeyPair(generator, NULL, &keyPair); - HcfBlob pubKeyBlob; - HcfBlob priKeyBlob; + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; HcfPubKey *pubKey = keyPair->pubKey; HcfPriKey *priKey = keyPair->priKey; -- Gitee