From 60b30cacfbc9ebc5815182bdf0774ef8800c551a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=97=8B=E9=A3=8Elc?= Date: Tue, 19 Nov 2024 17:30:50 +0800 Subject: [PATCH] modify Signed-off-by: liuchang --- hapsigntool_cpp/api/src/cert_tools.cpp | 8 ++--- .../api/src/sign_tool_service_impl.cpp | 35 ++++++++++--------- hapsigntool_cpp/common/src/byte_buffer.cpp | 2 +- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/hapsigntool_cpp/api/src/cert_tools.cpp b/hapsigntool_cpp/api/src/cert_tools.cpp index e6f6186d..e634a2e1 100644 --- a/hapsigntool_cpp/api/src/cert_tools.cpp +++ b/hapsigntool_cpp/api/src/cert_tools.cpp @@ -335,14 +335,11 @@ X509* CertTools::GenerateSubCert(EVP_PKEY* keyPair, X509_REQ* rootcsr, Options* SIGNATURE_TOOLS_LOGE("failed to generate the subCert"); break; } - EVP_PKEY_free(subKey); - X509_REQ_free(subcsr); - return subCert; } while (0); EVP_PKEY_free(subKey); X509_REQ_free(subcsr); - return nullptr; + return subCert; } bool CertTools::SetKeyUsage(X509* cert, Options* options) @@ -458,6 +455,7 @@ X509* CertTools::GenerateCert(EVP_PKEY* keyPair, X509_REQ* certReq, Options* opt X509* cert = X509_new(); if (cert == nullptr) { SIGNATURE_TOOLS_LOGE("failed to create X509 cert"); + X509_REQ_free(issuercsr); return nullptr; } do { @@ -503,7 +501,7 @@ X509_REQ* CertTools::GenerateCsr(EVP_PKEY* evpPkey, std::string signAlgorithm, s } name = BuildDN(subject, req); - if (!name) { + if (name == nullptr) { SIGNATURE_TOOLS_LOGE("failed to add subject into cert"); break; } diff --git a/hapsigntool_cpp/api/src/sign_tool_service_impl.cpp b/hapsigntool_cpp/api/src/sign_tool_service_impl.cpp index c595576b..c2338ffa 100644 --- a/hapsigntool_cpp/api/src/sign_tool_service_impl.cpp +++ b/hapsigntool_cpp/api/src/sign_tool_service_impl.cpp @@ -77,30 +77,32 @@ bool SignToolServiceImpl::GenerateRootCertToFile(Options* options, EVP_PKEY* roo std::string subject = options->GetString(Options::SUBJECT); X509* certPtr = nullptr; X509_REQ* csr = nullptr; + bool result = false; do { csr = CertTools::GenerateCsr(rootKey, signAlg, subject); - if (!csr) { + if (csr == nullptr) { + SIGNATURE_TOOLS_LOGE("generate root cert failed because csr is nullptr!"); break; } certPtr = CertTools::GenerateRootCertificate(rootKey, csr, options); - if (!certPtr) { + if (certPtr == nullptr) { + SIGNATURE_TOOLS_LOGE("generate root cert failed because certPtr is nullptr!"); break; } if (!X509CertVerify(certPtr, rootKey)) { + SIGNATURE_TOOLS_LOGE("generate root cert failed because verify failed!"); break; } if (!OutputModeOfCert(certPtr, options)) { + SIGNATURE_TOOLS_LOGE("generate root cert failed because output failed!"); break; } - X509_free(certPtr); - X509_REQ_free(csr); - return true; + result = true; } while (0); - SIGNATURE_TOOLS_LOGE("generate root cert failed!"); X509_free(certPtr); X509_REQ_free(csr); - return false; + return result; } bool SignToolServiceImpl::GenerateSubCertToFile(Options* options, EVP_PKEY* rootKey) @@ -113,33 +115,32 @@ bool SignToolServiceImpl::GenerateSubCertToFile(Options* options, EVP_PKEY* root std::string issuer = options->GetString(Options::ISSUER); X509* cert = nullptr; X509_REQ* csr = nullptr; + bool result = false; do { - if (rootKey == nullptr) { - break; - } csr = CertTools::GenerateCsr(rootKey, signAlg, issuer); - if (!csr) { + if (csr == nullptr) { + SIGNATURE_TOOLS_LOGE("generate sub cert failed because csr is nullptr!"); break; } cert = CertTools::GenerateSubCert(rootKey, csr, options); - if (!cert) { + if (cert == nullptr) { + SIGNATURE_TOOLS_LOGE("generate sub cert failed because cert is nullptr!"); break; } if (!X509CertVerify(cert, rootKey)) { + SIGNATURE_TOOLS_LOGE("generate sub cert failed because verify failed!"); break; } if (!OutputModeOfCert(cert, options)) { + SIGNATURE_TOOLS_LOGE("generate sub cert failed because output failed!"); break; } - X509_free(cert); - X509_REQ_free(csr); - return true; + result = true; } while (0); X509_free(cert); X509_REQ_free(csr); - SIGNATURE_TOOLS_LOGE("generate sub cert failed!"); - return false; + return result; } int SignToolServiceImpl::HandleIssuerKeyAliasEmpty(Options* options) diff --git a/hapsigntool_cpp/common/src/byte_buffer.cpp b/hapsigntool_cpp/common/src/byte_buffer.cpp index 8e4bb061..f8a9ae5a 100644 --- a/hapsigntool_cpp/common/src/byte_buffer.cpp +++ b/hapsigntool_cpp/common/src/byte_buffer.cpp @@ -23,7 +23,7 @@ namespace SignatureTools { const int32_t ByteBuffer::MAX_PRINT_LENGTH = 200; const int32_t ByteBuffer::HEX_PRINT_LENGTH = 3; -const int32_t MAX_MEMORY = 2 * 1024 * 1024 * 1024; +const int64_t MAX_MEMORY = 2L * 1024L * 1024L * 1024L; template std::shared_ptr make_shared_array(size_t size) -- Gitee