From 262ad9735a594c1c650af140d59d0aab5aa4ed97 Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Fri, 10 Nov 2023 07:41:31 +0100 Subject: [PATCH] Cleanup `std::vector` with `span` when it's suitable --- api/python/src/Abstract/pySection.cpp | 10 +++---- .../src/MachO/objects/pyThreadCommand.cpp | 5 +++- api/python/src/PE/objects/debug/pyRepro.cpp | 9 ++---- api/python/src/PE/objects/pyBinary.cpp | 16 ++++------ api/python/src/PE/objects/pySection.cpp | 5 ++-- .../signature/attributes/pyGenericType.cpp | 6 ++-- .../attributes/pyPKCS9MessageDigest.cpp | 4 +-- .../src/PE/objects/signature/pyRsaInfo.cpp | 19 +++++------- .../src/PE/objects/signature/pySignature.cpp | 4 +-- .../src/PE/objects/signature/pySignerInfo.cpp | 9 +++--- .../src/PE/objects/signature/pyx509.cpp | 14 ++++----- api/python/src/nanobind/utils.hpp | 24 ++++++++++++++- include/LIEF/MachO/ThreadCommand.hpp | 10 +++++-- include/LIEF/PE/Section.hpp | 2 +- include/LIEF/PE/signature/Signature.hpp | 10 +++++-- include/LIEF/PE/signature/SignerInfo.hpp | 6 ++-- src/MachO/Builder.tcc | 2 +- src/MachO/ThreadCommand.cpp | 8 ----- src/PE/Binary.cpp | 2 +- src/PE/signature/Signature.cpp | 30 ++++++++----------- src/PE/signature/SignatureParser.cpp | 5 ++-- 21 files changed, 105 insertions(+), 95 deletions(-) diff --git a/api/python/src/Abstract/pySection.cpp b/api/python/src/Abstract/pySection.cpp index a46fa8e1af..60f1abfc4f 100644 --- a/api/python/src/Abstract/pySection.cpp +++ b/api/python/src/Abstract/pySection.cpp @@ -23,6 +23,7 @@ #include "pySafeString.hpp" #include "typing.hpp" #include "nanobind/extra/memoryview.hpp" +#include "nanobind/utils.hpp" #include "LIEF/Abstract/Section.hpp" @@ -55,8 +56,7 @@ void create
(nb::module_& m) { .def_prop_ro("fullname", [] (const Section& obj) { - const std::string& fullname = obj.fullname(); - return nb::bytes(fullname.data(), fullname.size()); + return nb::to_bytes(obj.fullname()); }, "Return the **fullname** of the section including the trailing bytes"_doc) @@ -77,8 +77,7 @@ void create
(nb::module_& m) { .def_prop_rw("content", [] (const Section& self) { - const span content = self.content(); - return nb::memoryview::from_memory(content.data(), content.size()); + return nanobind::to_memoryview(self.content()); }, nb::overload_cast&>(&Section::content), "Section's content"_doc) @@ -114,8 +113,7 @@ void create
(nb::module_& m) { "str"_a, "pos"_a = 0) .def("search", - [] (const Section& self, - nb::bytes bytes, size_t pos) -> search_result + [] (const Section& self, nb::bytes bytes, size_t pos) -> search_result { std::string raw_str(bytes.c_str(), bytes.size()); const std::vector raw = { diff --git a/api/python/src/MachO/objects/pyThreadCommand.cpp b/api/python/src/MachO/objects/pyThreadCommand.cpp index 57ce46d8b5..daa0b84636 100644 --- a/api/python/src/MachO/objects/pyThreadCommand.cpp +++ b/api/python/src/MachO/objects/pyThreadCommand.cpp @@ -17,6 +17,7 @@ #include #include #include +#include "nanobind/utils.hpp" #include "LIEF/MachO/ThreadCommand.hpp" @@ -52,7 +53,9 @@ void create(nb::module_& m) { .def_prop_rw("state", - nb::overload_cast<>(&ThreadCommand::state, nb::const_), + [] (const ThreadCommand& self) { + return nb::to_memoryview(self.state()); + }, nb::overload_cast&>(&ThreadCommand::state), R"delim( The actual thread state as a vector of bytes. Depending on the architecture(), diff --git a/api/python/src/PE/objects/debug/pyRepro.cpp b/api/python/src/PE/objects/debug/pyRepro.cpp index dc588bb3ed..0ee00056c8 100644 --- a/api/python/src/PE/objects/debug/pyRepro.cpp +++ b/api/python/src/PE/objects/debug/pyRepro.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "nanobind/utils.hpp" namespace LIEF::PE::py { @@ -35,14 +36,10 @@ void create(nb::module_& m) { )delim"_doc) .def_prop_rw("hash", [] (const Repro& repro) { - const span hash = repro.hash(); - return nb::memoryview::from_memory(hash.data(), hash.size()); + return nb::to_memoryview(repro.hash()); }, [] (Repro& repro, nb::bytes bytes) { - const auto* start = reinterpret_cast(bytes.c_str()); - const auto* end = start + bytes.size(); - std::vector hash(start, end); - repro.hash(std::move(hash)); + repro.hash(nb::to_vector(bytes)); }, "The hash associated with the reproducible build"_doc) LIEF_DEFAULT_STR(Repro); } diff --git a/api/python/src/PE/objects/pyBinary.cpp b/api/python/src/PE/objects/pyBinary.cpp index d9efcd0379..13a2ae3e99 100644 --- a/api/python/src/PE/objects/pyBinary.cpp +++ b/api/python/src/PE/objects/pyBinary.cpp @@ -31,6 +31,7 @@ #include "pyErr.hpp" #include "pyIterator.hpp" #include "nanobind/extra/memoryview.hpp" +#include "nanobind/utils.hpp" #include #include @@ -197,8 +198,7 @@ void create(nb::module_& m) { .def("authentihash", [] (const Binary& bin, ALGORITHMS algo) { - const std::vector& data = bin.authentihash(algo); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(bin.authentihash(algo)); }, "Compute the authentihash according to the " RST_CLASS_REF(lief.PE.ALGORITHMS) " " "given in the first parameter"_doc, @@ -235,29 +235,25 @@ void create(nb::module_& m) { .def_prop_ro("authentihash_md5", [] (const Binary& bin) { - const std::vector& data = bin.authentihash(ALGORITHMS::MD5); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(bin.authentihash(ALGORITHMS::MD5)); }, "Authentihash **MD5** value"_doc) .def_prop_ro("authentihash_sha1", [] (const Binary& bin) { - const std::vector& data = bin.authentihash(ALGORITHMS::SHA_1); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(bin.authentihash(ALGORITHMS::SHA_1)); }, "Authentihash **SHA1** value"_doc) .def_prop_ro("authentihash_sha256", [] (const Binary& bin) { - const std::vector& data = bin.authentihash(ALGORITHMS::SHA_256); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(bin.authentihash(ALGORITHMS::SHA_256)); }, "Authentihash **SHA-256** value"_doc) .def_prop_ro("authentihash_sha512", [] (const Binary& bin) { - const std::vector& data = bin.authentihash(ALGORITHMS::SHA_512); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(bin.authentihash(ALGORITHMS::SHA_512)); }, "Authentihash **SHA-512** value"_doc) diff --git a/api/python/src/PE/objects/pySection.cpp b/api/python/src/PE/objects/pySection.cpp index 5046f1fe8b..b31e4999ef 100644 --- a/api/python/src/PE/objects/pySection.cpp +++ b/api/python/src/PE/objects/pySection.cpp @@ -23,6 +23,8 @@ #include #include #include +#include "nanobind/utils.hpp" + #define PY_ENUM(x) to_string(x), x @@ -167,8 +169,7 @@ void create
(nb::module_& m) { .def_prop_ro("padding", [] (const Section& sec) { - const std::vector& data = sec.padding(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(sec.padding()); }, "Section padding content as bytes"_doc) diff --git a/api/python/src/PE/objects/signature/attributes/pyGenericType.cpp b/api/python/src/PE/objects/signature/attributes/pyGenericType.cpp index 152221db2c..661aff5540 100644 --- a/api/python/src/PE/objects/signature/attributes/pyGenericType.cpp +++ b/api/python/src/PE/objects/signature/attributes/pyGenericType.cpp @@ -21,6 +21,7 @@ #include #include #include +#include "nanobind/utils.hpp" namespace LIEF::PE::py { @@ -35,9 +36,8 @@ void create(nb::module_& m) { "OID of the original attribute"_doc) .def_prop_ro("raw_content", - [] (const GenericType& type) -> nb::bytes { - const std::vector& raw = type.raw_content(); - return nb::bytes(reinterpret_cast(raw.data()), raw.size()); + [] (const GenericType& type) { + return nb::to_memoryview(type.raw_content()); }, "Original DER blob of the attribute"_doc); } diff --git a/api/python/src/PE/objects/signature/attributes/pyPKCS9MessageDigest.cpp b/api/python/src/PE/objects/signature/attributes/pyPKCS9MessageDigest.cpp index c7a2c9334b..b1213ef833 100644 --- a/api/python/src/PE/objects/signature/attributes/pyPKCS9MessageDigest.cpp +++ b/api/python/src/PE/objects/signature/attributes/pyPKCS9MessageDigest.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "nanobind/utils.hpp" namespace LIEF::PE::py { @@ -47,8 +48,7 @@ void create(nb::module_& m) { .def_prop_ro("digest", [] (const PKCS9MessageDigest& digest) { - const std::vector& data = digest.digest(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(digest.digest()); }, "Message digeset as a blob of bytes as described in the RFC"_doc); } diff --git a/api/python/src/PE/objects/signature/pyRsaInfo.cpp b/api/python/src/PE/objects/signature/pyRsaInfo.cpp index a3cea27906..4c5450ed6a 100644 --- a/api/python/src/PE/objects/signature/pyRsaInfo.cpp +++ b/api/python/src/PE/objects/signature/pyRsaInfo.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "nanobind/utils.hpp" namespace LIEF::PE::py { @@ -37,40 +38,34 @@ void create(nb::module_& m) { .def_prop_ro("N", [] (const RsaInfo& info) { - const std::vector& data = info.N(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(info.N()); }, "RSA public modulus (in bytes)"_doc) .def_prop_ro("E", [] (const RsaInfo& info) { - const std::vector& data = info.E(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(info.E()); }, "RSA public exponent (in bytes)"_doc) .def_prop_ro("D", [] (const RsaInfo& info) { - const std::vector& data = info.D(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(info.D()); }, "RSA private exponent (in bytes)"_doc) .def_prop_ro("P", [] (const RsaInfo& info) { - const std::vector& data = info.P(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(info.P()); }, "First prime factor (in bytes)"_doc) .def_prop_ro("Q", [] (const RsaInfo& info) { - const std::vector& data = info.Q(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(info.Q()); }, "Second prime factor (in bytes)") .def_prop_ro("key_size", &RsaInfo::key_size, "Size of the public modulus in bits"_doc) - .def_prop_ro("__len__", - &RsaInfo::key_size) + .def_prop_ro("__len__", &RsaInfo::key_size) LIEF_DEFAULT_STR(RsaInfo); } diff --git a/api/python/src/PE/objects/signature/pySignature.cpp b/api/python/src/PE/objects/signature/pySignature.cpp index 0ab1b0feae..54a86ef848 100644 --- a/api/python/src/PE/objects/signature/pySignature.cpp +++ b/api/python/src/PE/objects/signature/pySignature.cpp @@ -18,6 +18,7 @@ #include #include #include +#include "nanobind/utils.hpp" #include "enums_wrapper.hpp" @@ -219,8 +220,7 @@ void create(nb::module_& m) { .def_prop_ro("raw_der", [] (const Signature& sig) { - const std::vector& raw = sig.raw_der(); - return nb::bytes(reinterpret_cast(raw.data()), raw.size()); + return nb::to_memoryview(sig.raw_der()); }, "Return the raw original signature as a byte object"_doc, nb::rv_policy::reference_internal) diff --git a/api/python/src/PE/objects/signature/pySignerInfo.cpp b/api/python/src/PE/objects/signature/pySignerInfo.cpp index 4d7437c060..e43d352d19 100644 --- a/api/python/src/PE/objects/signature/pySignerInfo.cpp +++ b/api/python/src/PE/objects/signature/pySignerInfo.cpp @@ -24,6 +24,7 @@ #include #include #include +#include "nanobind/utils.hpp" namespace LIEF::PE::py { @@ -58,9 +59,8 @@ void create(nb::module_& m) { "Should be 1"_doc) .def_prop_ro("serial_number", - [] (const SignerInfo& info) -> nb::bytes { - const std::vector& data = info.serial_number(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + [] (const SignerInfo& info) { + return nb::to_bytes(info.serial_number()); }, "The X509 serial number used to sign the signed-data (see: :attr:`lief.PE.x509.serial_number`)"_doc) @@ -83,8 +83,7 @@ void create(nb::module_& m) { .def_prop_ro("encrypted_digest", [] (const SignerInfo& info) { - const std::vector& data = info.encrypted_digest(); - return nb::bytes(reinterpret_cast(data.data()), data.size()); + return nb::to_bytes(info.encrypted_digest()); }, "Return the signature created by the signing certificate's private key"_doc) diff --git a/api/python/src/PE/objects/signature/pyx509.cpp b/api/python/src/PE/objects/signature/pyx509.cpp index 6170122da5..df51e5bc84 100644 --- a/api/python/src/PE/objects/signature/pyx509.cpp +++ b/api/python/src/PE/objects/signature/pyx509.cpp @@ -27,6 +27,7 @@ #include #include #include +#include "nanobind/utils.hpp" namespace LIEF::PE::py { @@ -96,9 +97,8 @@ void create(nb::module_& m) { "X.509 version. (1=v1, 2=v2, 3=v3)"_doc) .def_prop_ro("serial_number", - [] (const x509& crt) -> nb::bytes { - const std::vector& sn = crt.serial_number(); - return nb::bytes(reinterpret_cast(sn.data()), sn.size()); + [] (const x509& crt) { + return nb::to_bytes(crt.serial_number()); }, "Unique id for certificate issued by a specific CA."_doc) @@ -127,9 +127,8 @@ void create(nb::module_& m) { "Subject of the certificate"_doc) .def_prop_ro("raw", - [] (const x509& crt) -> nb::bytes { - const std::vector& raw = crt.raw(); - return nb::bytes(reinterpret_cast(raw.data()), raw.size()); + [] (const x509& crt) { + return nb::to_bytes(crt.raw()); }, "The raw bytes associated with this x509 cert (DER encoded)"_doc) @@ -160,8 +159,7 @@ void create(nb::module_& m) { .def_prop_ro("signature", [] (const x509& cert) { - const std::vector& sig = cert.signature(); - return nb::bytes(reinterpret_cast(sig.data()), sig.size()); + return nb::to_bytes(cert.signature()); }, "The signature of the certificate") .def("verify", diff --git a/api/python/src/nanobind/utils.hpp b/api/python/src/nanobind/utils.hpp index 6e32a5d0c9..a30b31600b 100644 --- a/api/python/src/nanobind/utils.hpp +++ b/api/python/src/nanobind/utils.hpp @@ -4,10 +4,32 @@ #include #include #include -#include +#include + +#include "nanobind/extra/memoryview.hpp" NAMESPACE_BEGIN(NB_NAMESPACE) +inline nanobind::bytes to_bytes(const std::vector& vec) { + return nanobind::bytes(reinterpret_cast(vec.data()), vec.size()); +} + +inline nanobind::bytes to_bytes(LIEF::span sp) { + return nanobind::bytes(reinterpret_cast(sp.data()), sp.size()); +} + +inline nanobind::bytes to_bytes(const std::string& str) { + return nanobind::bytes(str.data(), str.size()); +} + +inline nanobind::memoryview to_memoryview(LIEF::span sp) { + return nanobind::memoryview::from_memory(sp.data(), sp.size()); +} + +inline nanobind::memoryview to_memoryview(const std::vector& vec) { + return nanobind::memoryview::from_memory(vec.data(), vec.size()); +} + inline std::vector to_vector(nanobind::bytes bytes) { const auto* ptr = reinterpret_cast(bytes.c_str()); return {ptr, ptr + bytes.size()}; diff --git a/include/LIEF/MachO/ThreadCommand.hpp b/include/LIEF/MachO/ThreadCommand.hpp index b5379d8853..03eb1b9e49 100644 --- a/include/LIEF/MachO/ThreadCommand.hpp +++ b/include/LIEF/MachO/ThreadCommand.hpp @@ -20,6 +20,7 @@ #include "LIEF/visibility.h" #include "LIEF/types.hpp" +#include "LIEF/span.hpp" #include "LIEF/MachO/LoadCommand.hpp" @@ -70,8 +71,13 @@ class LIEF_API ThreadCommand : public LoadCommand { //! The actual thread state as a vector of bytes. Depending on the architecture(), //! these data can be casted into x86_thread_state_t, x86_thread_state64_t, ... - const std::vector& state() const; - std::vector& state(); + span state() const { + return state_; + } + + span state() { + return state_; + } //! Return the initial Program Counter regardless of the underlying architecture. //! This value, when non null, can be used to determine the binary's entrypoint. diff --git a/include/LIEF/PE/Section.hpp b/include/LIEF/PE/Section.hpp index c8f60b74d9..3f6c1bf8d8 100644 --- a/include/LIEF/PE/Section.hpp +++ b/include/LIEF/PE/Section.hpp @@ -112,7 +112,7 @@ class LIEF_API Section : public LIEF::Section { } //! Content of the section's padding area - const std::vector& padding() const { + span padding() const { return padding_; } diff --git a/include/LIEF/PE/signature/Signature.hpp b/include/LIEF/PE/signature/Signature.hpp index 9e8e45552e..b17f9d4617 100644 --- a/include/LIEF/PE/signature/Signature.hpp +++ b/include/LIEF/PE/signature/Signature.hpp @@ -18,6 +18,7 @@ #include "LIEF/Object.hpp" #include "LIEF/visibility.h" +#include "LIEF/span.hpp" #include "LIEF/PE/signature/x509.hpp" #include "LIEF/PE/signature/SignerInfo.hpp" @@ -44,7 +45,10 @@ class LIEF_API Signature : public Object { public: //! Hash the input given the algorithm - static std::vector hash(const std::vector& input, ALGORITHMS algo); + static std::vector hash(const std::vector& input, ALGORITHMS algo) { + return hash(input.data(), input.size(), algo); + } + static std::vector hash(const uint8_t* buffer, size_t size, ALGORITHMS algo); public: @@ -112,7 +116,9 @@ class LIEF_API Signature : public Object { it_const_signers_t signers() const; //! Return the raw original PKCS7 signature - const std::vector& raw_der() const; + span raw_der() const { + return original_raw_signature_; + } //! Find x509 certificate according to its serial number const x509* find_crt(const std::vector& serialno) const; diff --git a/include/LIEF/PE/signature/SignerInfo.hpp b/include/LIEF/PE/signature/SignerInfo.hpp index 39c3d2e78f..f65024be39 100644 --- a/include/LIEF/PE/signature/SignerInfo.hpp +++ b/include/LIEF/PE/signature/SignerInfo.hpp @@ -19,7 +19,7 @@ #include "LIEF/Object.hpp" #include "LIEF/visibility.h" - +#include "LIEF/span.hpp" #include "LIEF/PE/signature/types.hpp" #include "LIEF/iterators.hpp" @@ -84,7 +84,7 @@ class LIEF_API SignerInfo : public Object { //! @see //! LIEF::PE::x509::serial_number //! SignerInfo::issuer - const std::vector& serial_number() const { + span serial_number() const { return serialno_; } @@ -143,7 +143,7 @@ class LIEF_API SignerInfo : public Object { } //! Raw blob that is signed by the signer certificate - const std::vector& raw_auth_data() const { + span raw_auth_data() const { return raw_auth_data_; } diff --git a/src/MachO/Builder.tcc b/src/MachO/Builder.tcc index f9b197fa05..1d3c234d62 100644 --- a/src/MachO/Builder.tcc +++ b/src/MachO/Builder.tcc @@ -934,7 +934,7 @@ ok_error_t Builder::build(ThreadCommand& tc) { details::thread_command raw_cmd; std::memset(&raw_cmd, 0, sizeof(details::thread_command)); - const std::vector& state = tc.state(); + const span state = tc.state(); const uint32_t raw_size = sizeof(details::thread_command) + state.size(); const uint32_t size_needed = align(raw_size, sizeof(typename T::uint)); diff --git a/src/MachO/ThreadCommand.cpp b/src/MachO/ThreadCommand.cpp index 13fd477a57..efeb1202bc 100644 --- a/src/MachO/ThreadCommand.cpp +++ b/src/MachO/ThreadCommand.cpp @@ -63,14 +63,6 @@ CPU_TYPES ThreadCommand::architecture() const { return architecture_; } -const std::vector& ThreadCommand::state() const { - return state_; -} - -std::vector& ThreadCommand::state() { - return const_cast&>(static_cast(this)->state()); -} - uint64_t ThreadCommand::pc() const { uint64_t entry = 0; switch(architecture_) { diff --git a/src/PE/Binary.cpp b/src/PE/Binary.cpp index 3928c03960..02a92ac0e0 100644 --- a/src/PE/Binary.cpp +++ b/src/PE/Binary.cpp @@ -849,7 +849,7 @@ std::vector Binary::authentihash(ALGORITHMS algo) const { if (sec->sizeof_raw_data() == 0) { continue; } - const std::vector& pad = sec->padding(); + span pad = sec->padding(); span content = sec->content(); LIEF_DEBUG("Authentihash: Append section {:<8}: [0x{:04x}, 0x{:04x}] + [0x{:04x}] = [0x{:04x}, 0x{:04x}]", sec->name(), diff --git a/src/PE/signature/Signature.cpp b/src/PE/signature/Signature.cpp index 3c7e1c5e1c..222c6e61aa 100644 --- a/src/PE/signature/Signature.cpp +++ b/src/PE/signature/Signature.cpp @@ -35,6 +35,7 @@ #include #include "frozen.hpp" +#include "internal_utils.hpp" namespace LIEF { namespace PE { @@ -85,7 +86,7 @@ Signature::VERIFICATION_FLAGS verify_ts_counter_signature(const SignerInfo& sign const x509& cs_cert = *cs_signer.cert(); const SignerInfo::encrypted_digest_t& cs_enc_digest = cs_signer.encrypted_digest(); - std::vector cs_auth_data = cs_signer.raw_auth_data(); + std::vector cs_auth_data = as_vector(cs_signer.raw_auth_data()); // According to the RFC: // // "[...] The Attributes value's tag is SET OF, and the DER encoding of @@ -162,15 +163,15 @@ Signature::VERIFICATION_FLAGS verify_ts_counter_signature(const SignerInfo& sign } -std::vector Signature::hash(const std::vector& input, ALGORITHMS algo) { +std::vector Signature::hash(const uint8_t* buffer, size_t size, ALGORITHMS algo) { switch (algo) { case ALGORITHMS::SHA_512: { std::vector out(64); - int ret = mbedtls_sha512(input.data(), input.size(), out.data(), /* is384 */ 0); + int ret = mbedtls_sha512(buffer, size, out.data(), /* is384 */ 0); if (ret != 0) { - LIEF_ERR("Hashing {} bytes with SHA-512 failed! (ret: 0x{:x})", input.size(), ret); + LIEF_ERR("Hashing {} bytes with SHA-512 failed! (ret: 0x{:x})", size, ret); return {}; } return out; @@ -179,9 +180,9 @@ std::vector Signature::hash(const std::vector& input, ALGORITH case ALGORITHMS::SHA_384: { std::vector out(64); - int ret = mbedtls_sha512(input.data(), input.size(), out.data(), /* is384 */ 1); + int ret = mbedtls_sha512(buffer, size, out.data(), /* is384 */ 1); if (ret != 0) { - LIEF_ERR("Hashing {} bytes with SHA-384 failed! (ret: 0x{:x})", input.size(), ret); + LIEF_ERR("Hashing {} bytes with SHA-384 failed! (ret: 0x{:x})", size, ret); return {}; } return out; @@ -190,9 +191,9 @@ std::vector Signature::hash(const std::vector& input, ALGORITH case ALGORITHMS::SHA_256: { std::vector out(32); - int ret = mbedtls_sha256(input.data(), input.size(), out.data(), /* is224 */ 0); + int ret = mbedtls_sha256(buffer, size, out.data(), /* is224 */ 0); if (ret != 0) { - LIEF_ERR("Hashing {} bytes with SHA-256 failed! (ret: 0x{:x})", input.size(), ret); + LIEF_ERR("Hashing {} bytes with SHA-256 failed! (ret: 0x{:x})", size, ret); return {}; } return out; @@ -201,9 +202,9 @@ std::vector Signature::hash(const std::vector& input, ALGORITH case ALGORITHMS::SHA_1: { std::vector out(20); - int ret = mbedtls_sha1(input.data(), input.size(), out.data()); + int ret = mbedtls_sha1(buffer, size, out.data()); if (ret != 0) { - LIEF_ERR("Hashing {} bytes with SHA-1 failed! (ret: 0x{:x})", input.size(), ret); + LIEF_ERR("Hashing {} bytes with SHA-1 failed! (ret: 0x{:x})", size, ret); return {}; } return out; @@ -212,9 +213,9 @@ std::vector Signature::hash(const std::vector& input, ALGORITH case ALGORITHMS::MD5: { std::vector out(16); - int ret = mbedtls_md5(input.data(), input.size(), out.data()); + int ret = mbedtls_md5(buffer, size, out.data()); if (ret != 0) { - LIEF_ERR("Hashing {} bytes with MD5 failed! (ret: 0x{:x})", input.size(), ret); + LIEF_ERR("Hashing {} bytes with MD5 failed! (ret: 0x{:x})", size, ret); return {}; } return out; @@ -412,11 +413,6 @@ Signature::VERIFICATION_FLAGS Signature::check(VERIFICATION_CHECKS checks) const } -const std::vector& Signature::raw_der() const { - return original_raw_signature_; -} - - const x509* Signature::find_crt(const std::vector& serialno) const { auto it_cert = std::find_if(std::begin(certificates_), std::end(certificates_), [&serialno] (const x509& cert) { diff --git a/src/PE/signature/SignatureParser.cpp b/src/PE/signature/SignatureParser.cpp index fcfc6b4ab5..72a28db487 100644 --- a/src/PE/signature/SignatureParser.cpp +++ b/src/PE/signature/SignatureParser.cpp @@ -49,6 +49,7 @@ #include "logging.hpp" #include "messages.hpp" +#include "internal_utils.hpp" namespace LIEF { namespace PE { @@ -320,7 +321,7 @@ result SignatureParser::parse_signature(BinaryStream& stream) { // Tied signer info with x509 certificates for (SignerInfo& signer : signature.signers_) { - const x509* crt = signature.find_crt_issuer(signer.issuer(), signer.serial_number()); + const x509* crt = signature.find_crt_issuer(signer.issuer(), as_vector(signer.serial_number())); if (crt != nullptr) { signer.cert_ = std::make_unique(*crt); } else { @@ -329,7 +330,7 @@ result SignatureParser::parse_signature(BinaryStream& stream) { const auto* cs = static_cast(signer.get_attribute(SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE)); if (cs != nullptr) { SignerInfo& cs_signer = const_cast(cs)->signer_; - const x509* crt = signature.find_crt_issuer(cs_signer.issuer(), cs_signer.serial_number()); + const x509* crt = signature.find_crt_issuer(cs_signer.issuer(), as_vector(cs_signer.serial_number())); if (crt != nullptr) { cs_signer.cert_ = std::make_unique(*crt); } else {