From 8dd8a255a824f4f266d238133f8a8b2942198f5b Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sat, 9 Dec 2023 05:02:47 +0100 Subject: [PATCH] Narrow the scope of `SIG_ATTRIBUTE_TYPES` to `LIEF::PE::Attribute::TYPE` --- api/python/lief/PE.pyi | 45 ++++++++-------- api/python/src/PE/enums.cpp | 15 ------ .../src/PE/objects/signature/pyAttribute.cpp | 28 ++++++++-- doc/sphinx/api/cpp/pe.rst | 3 -- doc/sphinx/api/python/pe.rst | 6 --- include/LIEF/PE/EnumToString.hpp | 2 - include/LIEF/PE/enums.hpp | 19 ------- include/LIEF/PE/signature/Attribute.hpp | 38 ++++++++++---- include/LIEF/PE/signature/SignerInfo.hpp | 31 +++++++---- .../PE/signature/attributes/ContentType.hpp | 24 +++++---- .../PE/signature/attributes/GenericType.hpp | 28 +++++----- .../attributes/MsSpcNestedSignature.hpp | 24 +++++---- .../attributes/MsSpcStatementType.hpp | 23 ++++---- .../attributes/PKCS9AtSequenceNumber.hpp | 23 +++++--- .../attributes/PKCS9CounterSignature.hpp | 24 +++++---- .../attributes/PKCS9MessageDigest.hpp | 20 ++++--- .../signature/attributes/PKCS9SigningTime.hpp | 24 +++++---- .../PE/signature/attributes/SpcSpOpusInfo.hpp | 27 ++++++---- src/PE/EnumToString.cpp | 18 +------ src/PE/signature/Attribute.cpp | 43 +++++++++------ src/PE/signature/Signature.cpp | 52 +++++++++---------- src/PE/signature/SignatureParser.cpp | 2 +- src/PE/signature/SignerInfo.cpp | 45 ++++------------ src/PE/signature/attributes/ContentType.cpp | 21 -------- src/PE/signature/attributes/GenericType.cpp | 21 +------- .../attributes/MsSpcNestedSignature.cpp | 19 ------- .../attributes/MsSpcStatementType.cpp | 20 ------- .../attributes/PKCS9AtSequenceNumber.cpp | 19 ------- .../attributes/PKCS9CounterSignature.cpp | 18 ------- .../attributes/PKCS9MessageDigest.cpp | 21 -------- .../signature/attributes/PKCS9SigningTime.cpp | 19 ------- src/PE/signature/attributes/SpcSpOpusInfo.cpp | 19 ------- tests/pe/test_authenticode.py | 14 ++--- 33 files changed, 295 insertions(+), 460 deletions(-) diff --git a/api/python/lief/PE.pyi b/api/python/lief/PE.pyi index 38f0d0a2b6..1dabb0fff9 100644 --- a/api/python/lief/PE.pyi +++ b/api/python/lief/PE.pyi @@ -4,6 +4,7 @@ from typing import overload import io import lief # type: ignore import lief.PE # type: ignore +import lief.PE.Attribute # type: ignore import lief.PE.Binary # type: ignore import lief.PE.CodeView # type: ignore import lief.PE.ContentInfo # type: ignore @@ -266,9 +267,26 @@ class ALGORITHMS: def value(self) -> int: ... class Attribute(lief.Object): + class TYPE: + CONTENT_TYPE: ClassVar[Attribute.TYPE] = ... + GENERIC_TYPE: ClassVar[Attribute.TYPE] = ... + MS_COUNTER_SIGN: ClassVar[Attribute.TYPE] = ... + MS_SPC_STATEMENT_TYPE: ClassVar[Attribute.TYPE] = ... + PKCS9_AT_SEQUENCE_NUMBER: ClassVar[Attribute.TYPE] = ... + PKCS9_COUNTER_SIGNATURE: ClassVar[Attribute.TYPE] = ... + PKCS9_MESSAGE_DIGEST: ClassVar[Attribute.TYPE] = ... + PKCS9_SIGNING_TIME: ClassVar[Attribute.TYPE] = ... + SPC_SP_OPUS_INFO: ClassVar[Attribute.TYPE] = ... + UNKNOWN: ClassVar[Attribute.TYPE] = ... + __name__: Any + def __init__(self, *args, **kwargs) -> None: ... + @staticmethod + def from_value(arg: int, /) -> lief.PE.Attribute.TYPE: ... + @property + def value(self) -> int: ... def __init__(self, *args, **kwargs) -> None: ... @property - def type(self) -> lief.PE.SIG_ATTRIBUTE_TYPES: ... + def type(self) -> lief.PE.Attribute.TYPE: ... class Binary(lief.Binary): class it_const_signatures: @@ -2195,25 +2213,6 @@ class SECTION_TYPES: @property def value(self) -> int: ... -class SIG_ATTRIBUTE_TYPES: - CONTENT_TYPE: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - GENERIC_TYPE: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - MS_COUNTER_SIGN: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - MS_SPC_NESTED_SIGN: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - MS_SPC_STATEMENT_TYPE: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - PKCS9_AT_SEQUENCE_NUMBER: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - PKCS9_COUNTER_SIGNATURE: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - PKCS9_MESSAGE_DIGEST: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - PKCS9_SIGNING_TIME: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - SPC_SP_OPUS_INFO: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - UNKNOWN: ClassVar[SIG_ATTRIBUTE_TYPES] = ... - __name__: Any - def __init__(self, *args, **kwargs) -> None: ... - @staticmethod - def from_value(arg: int, /) -> lief.PE.SIG_ATTRIBUTE_TYPES: ... - @property - def value(self) -> int: ... - class SYMBOL_BASE_TYPES: BYTE: ClassVar[SYMBOL_BASE_TYPES] = ... CHAR: ClassVar[SYMBOL_BASE_TYPES] = ... @@ -2485,9 +2484,9 @@ class SignerInfo(lief.Object): def __len__(self) -> int: ... def __next__(self) -> lief.PE.Attribute: ... def __init__(self, *args, **kwargs) -> None: ... - def get_attribute(self, type: lief.PE.SIG_ATTRIBUTE_TYPES) -> lief.PE.Attribute: ... - def get_auth_attribute(self, type: lief.PE.SIG_ATTRIBUTE_TYPES) -> lief.PE.Attribute: ... - def get_unauth_attribute(self, type: lief.PE.SIG_ATTRIBUTE_TYPES) -> lief.PE.Attribute: ... + def get_attribute(self, type: lief.PE.Attribute.TYPE) -> lief.PE.Attribute: ... + def get_auth_attribute(self, type: lief.PE.Attribute.TYPE) -> lief.PE.Attribute: ... + def get_unauth_attribute(self, type: lief.PE.Attribute.TYPE) -> lief.PE.Attribute: ... @property def authenticated_attributes(self) -> lief.PE.SignerInfo.it_const_attributes_t: ... @property diff --git a/api/python/src/PE/enums.cpp b/api/python/src/PE/enums.cpp index 4dc492fbad..cd68ca70a7 100644 --- a/api/python/src/PE/enums.cpp +++ b/api/python/src/PE/enums.cpp @@ -976,20 +976,5 @@ void init_enums(nb::module_& m) { .value(PY_ENUM(ALGORITHMS::SHA_256_ECDSA)) .value(PY_ENUM(ALGORITHMS::SHA_384_ECDSA)) .value(PY_ENUM(ALGORITHMS::SHA_512_ECDSA)); - - - enum_(m, "SIG_ATTRIBUTE_TYPES") - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::UNKNOWN)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::CONTENT_TYPE)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::GENERIC_TYPE)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::SPC_SP_OPUS_INFO)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::MS_COUNTER_SIGN)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::MS_SPC_NESTED_SIGN)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::MS_SPC_STATEMENT_TYPE)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::PKCS9_AT_SEQUENCE_NUMBER)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST)) - .value(PY_ENUM(SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME)); - } } diff --git a/api/python/src/PE/objects/signature/pyAttribute.cpp b/api/python/src/PE/objects/signature/pyAttribute.cpp index 796b091418..e4ce26e44c 100644 --- a/api/python/src/PE/objects/signature/pyAttribute.cpp +++ b/api/python/src/PE/objects/signature/pyAttribute.cpp @@ -19,16 +19,36 @@ #include #include +#include "enums_wrapper.hpp" #include namespace LIEF::PE::py { template<> void create(nb::module_& m) { - nb::class_(m, "Attribute", "Interface over PKCS #7 attribute"_doc) - .def_prop_ro("type", - &Attribute::type, - "Concrete type (" RST_CLASS_REF(lief.PE.SIG_ATTRIBUTE_TYPES) ") of the attribute"_doc) + nb::class_ Class(m, + "Attribute", "Interface over PKCS #7 attribute"_doc + ); + + #define ENTRY(X) .value(to_string(Attribute::TYPE::X), Attribute::TYPE::X) + enum_(Class, "TYPE") + ENTRY(UNKNOWN) + ENTRY(CONTENT_TYPE) + ENTRY(GENERIC_TYPE) + ENTRY(SPC_SP_OPUS_INFO) + ENTRY(MS_COUNTER_SIGN) + ENTRY(MS_SPC_NESTED_SIGN) + ENTRY(MS_SPC_STATEMENT_TYPE) + ENTRY(PKCS9_AT_SEQUENCE_NUMBER) + ENTRY(PKCS9_COUNTER_SIGNATURE) + ENTRY(PKCS9_MESSAGE_DIGEST) + ENTRY(PKCS9_SIGNING_TIME) + ; + #undef ENTRY + + Class + .def_prop_ro("type", &Attribute::type, + "Concrete type of the attribute"_doc) LIEF_DEFAULT_STR(Attribute); } diff --git a/doc/sphinx/api/cpp/pe.rst b/doc/sphinx/api/cpp/pe.rst index 86d1bf6684..988f5bda1e 100644 --- a/doc/sphinx/api/cpp/pe.rst +++ b/doc/sphinx/api/cpp/pe.rst @@ -692,6 +692,3 @@ Enums .. doxygenenum:: LIEF::PE::ALGORITHMS :project: lief - -.. doxygenenum:: LIEF::PE::SIG_ATTRIBUTE_TYPES - :project: lief diff --git a/doc/sphinx/api/python/pe.rst b/doc/sphinx/api/python/pe.rst index 0ce4bbe041..51a387e5ad 100644 --- a/doc/sphinx/api/python/pe.rst +++ b/doc/sphinx/api/python/pe.rst @@ -646,9 +646,3 @@ ALGORITHMS ~~~~~~~~~~ .. autoclass:: lief.PE.ALGORITHMS - - -SIG_ATTRIBUTE_TYPES -~~~~~~~~~~~~~~~~~~~ - -.. autoclass:: lief.PE.SIG_ATTRIBUTE_TYPES diff --git a/include/LIEF/PE/EnumToString.hpp b/include/LIEF/PE/EnumToString.hpp index 284d3f4991..6bad7610db 100644 --- a/include/LIEF/PE/EnumToString.hpp +++ b/include/LIEF/PE/EnumToString.hpp @@ -46,7 +46,6 @@ enum class RELOCATIONS_I386 : size_t; enum class RESOURCE_LANGS : size_t; enum class RESOURCE_SUBLANGS : size_t; enum class RESOURCE_TYPES : size_t; -enum class SIG_ATTRIBUTE_TYPES : uint32_t; enum class SUBSYSTEM : size_t; enum class SYMBOL_BASE_TYPES : size_t; enum class SYMBOL_COMPLEX_TYPES : size_t; @@ -89,7 +88,6 @@ LIEF_API const char* to_string(ACCELERATOR_FLAGS e); LIEF_API const char* to_string(ACCELERATOR_VK_CODES e); LIEF_API const char* to_string(ALGORITHMS e); -LIEF_API const char* to_string(SIG_ATTRIBUTE_TYPES e); } // namespace PE } // namespace LIEF diff --git a/include/LIEF/PE/enums.hpp b/include/LIEF/PE/enums.hpp index 52dc4da18d..6d3209be70 100644 --- a/include/LIEF/PE/enums.hpp +++ b/include/LIEF/PE/enums.hpp @@ -1130,25 +1130,6 @@ enum class ALGORITHMS : uint32_t { SHA_512_ECDSA, }; -//! Typed enum for LIEF::PE::Attribute -enum class SIG_ATTRIBUTE_TYPES : uint32_t { - UNKNOWN = 0, - CONTENT_TYPE, - GENERIC_TYPE, - - SPC_SP_OPUS_INFO, - - MS_COUNTER_SIGN, - MS_SPC_NESTED_SIGN, - MS_SPC_STATEMENT_TYPE, - - PKCS9_AT_SEQUENCE_NUMBER, - PKCS9_COUNTER_SIGNATURE, - PKCS9_MESSAGE_DIGEST, - PKCS9_SIGNING_TIME, -}; - - static const RESOURCE_TYPES resource_types_array[] = { RESOURCE_TYPES::CURSOR, RESOURCE_TYPES::BITMAP, diff --git a/include/LIEF/PE/signature/Attribute.hpp b/include/LIEF/PE/signature/Attribute.hpp index bb7e165710..54e5732315 100644 --- a/include/LIEF/PE/signature/Attribute.hpp +++ b/include/LIEF/PE/signature/Attribute.hpp @@ -26,9 +26,6 @@ namespace LIEF { namespace PE { -class Parser; -class SignatureParser; - //! Interface over PKCS #7 attribute class LIEF_API Attribute : public Object { @@ -36,14 +33,31 @@ class LIEF_API Attribute : public Object { friend class SignatureParser; public: - Attribute(); - Attribute(const Attribute&); - Attribute& operator=(const Attribute&); + enum class TYPE { + UNKNOWN = 0, + CONTENT_TYPE, + GENERIC_TYPE, + + SPC_SP_OPUS_INFO, + + MS_COUNTER_SIGN, + MS_SPC_NESTED_SIGN, + MS_SPC_STATEMENT_TYPE, + + PKCS9_AT_SEQUENCE_NUMBER, + PKCS9_COUNTER_SIGNATURE, + PKCS9_MESSAGE_DIGEST, + PKCS9_SIGNING_TIME, + }; + + Attribute() = delete; + Attribute(const Attribute&) = default; + Attribute& operator=(const Attribute&) = default; virtual std::unique_ptr clone() const = 0; //! Concrete type of the attribute - virtual SIG_ATTRIBUTE_TYPES type() const { + virtual TYPE type() const { return type_; } @@ -52,15 +66,19 @@ class LIEF_API Attribute : public Object { void accept(Visitor& visitor) const override; - ~Attribute() override; + ~Attribute() override = default; LIEF_API friend std::ostream& operator<<(std::ostream& os, const Attribute& Attribute); protected: - Attribute(SIG_ATTRIBUTE_TYPES type); - SIG_ATTRIBUTE_TYPES type_ = SIG_ATTRIBUTE_TYPES::UNKNOWN; + Attribute(TYPE type) : + type_(type) + {} + TYPE type_ = TYPE::UNKNOWN; }; +LIEF_API const char* to_string(Attribute::TYPE e); + } } diff --git a/include/LIEF/PE/signature/SignerInfo.hpp b/include/LIEF/PE/signature/SignerInfo.hpp index f65024be39..2d789bd3cd 100644 --- a/include/LIEF/PE/signature/SignerInfo.hpp +++ b/include/LIEF/PE/signature/SignerInfo.hpp @@ -24,6 +24,7 @@ #include "LIEF/PE/signature/types.hpp" #include "LIEF/iterators.hpp" #include "LIEF/PE/enums.hpp" +#include "LIEF/PE/signature/Attribute.hpp" namespace LIEF { namespace PE { @@ -76,7 +77,9 @@ class LIEF_API SignerInfo : public Object { void swap(SignerInfo& other); //! Should be 1 - uint32_t version() const; + uint32_t version() const { + return version_; + } //! Return the serial number associated with the x509 certificate //! used by this signer. @@ -97,40 +100,50 @@ class LIEF_API SignerInfo : public Object { //! //! This value should match LIEF::PE::ContentInfo::digest_algorithm and //! LIEF::PE::Signature::digest_algorithm - ALGORITHMS digest_algorithm() const; + ALGORITHMS digest_algorithm() const { + return digest_algorithm_; + } //! Return the (public-key) algorithm used to encrypt //! the signature - ALGORITHMS encryption_algorithm() const; + ALGORITHMS encryption_algorithm() const { + return digest_enc_algorithm_; + } //! Return the signature created by the signing //! certificate's private key - const encrypted_digest_t& encrypted_digest() const; + const encrypted_digest_t& encrypted_digest() const { + return encrypted_digest_; + } //! Iterator over LIEF::PE::Attribute for **authenticated** attributes - it_const_attributes_t authenticated_attributes() const; + it_const_attributes_t authenticated_attributes() const { + return authenticated_attributes_; + } //! Iterator over LIEF::PE::Attribute for **unauthenticated** attributes - it_const_attributes_t unauthenticated_attributes() const; + it_const_attributes_t unauthenticated_attributes() const { + return unauthenticated_attributes_; + } //! Return the authenticated or un-authenticated attribute matching the //! given PE::SIG_ATTRIBUTE_TYPES. //! //! It returns **the first** entry that matches the given type. If it can't be //! found, it returns a nullptr. - const Attribute* get_attribute(PE::SIG_ATTRIBUTE_TYPES type) const; + const Attribute* get_attribute(Attribute::TYPE type) const; //! Return the authenticated attribute matching the given PE::SIG_ATTRIBUTE_TYPES. //! //! It returns **the first** entry that matches the given type. If it can't be //! found, it returns a nullptr. - const Attribute* get_auth_attribute(PE::SIG_ATTRIBUTE_TYPES type) const; + const Attribute* get_auth_attribute(Attribute::TYPE type) const; //! Return the un-authenticated attribute matching the given PE::SIG_ATTRIBUTE_TYPES. //! //! It returns **the first** entry that matches the given type. If it can't be //! found, it returns a nullptr. - const Attribute* get_unauth_attribute(PE::SIG_ATTRIBUTE_TYPES type) const; + const Attribute* get_unauth_attribute(Attribute::TYPE type) const; //! x509 certificate used by this signer. If it can't be found, it returns a nullptr const x509* cert() const { diff --git a/include/LIEF/PE/signature/attributes/ContentType.hpp b/include/LIEF/PE/signature/attributes/ContentType.hpp index 6fa0a98beb..12a9c1e2d7 100644 --- a/include/LIEF/PE/signature/attributes/ContentType.hpp +++ b/include/LIEF/PE/signature/attributes/ContentType.hpp @@ -26,9 +26,6 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - //! Interface over the structure described by the OID ``1.2.840.113549.1.9.3`` (PKCS #9) //! //! The internal structure is described in the @@ -43,10 +40,15 @@ class LIEF_API ContentType : public Attribute { friend class SignatureParser; public: - ContentType(); - ContentType(oid_t oid); - ContentType(const ContentType&); - ContentType& operator=(const ContentType&); + ContentType() : + Attribute(Attribute::TYPE::CONTENT_TYPE) + {} + ContentType(oid_t oid) : + Attribute(Attribute::TYPE::CONTENT_TYPE), + oid_{std::move(oid)} + {} + ContentType(const ContentType&) = default; + ContentType& operator=(const ContentType&) = default; //! OID as described in RFC #2985 const oid_t& oid() const { @@ -56,14 +58,16 @@ class LIEF_API ContentType : public Attribute { //! Print information about the attribute std::string print() const override; - std::unique_ptr clone() const override; + std::unique_ptr clone() const override { + return std::unique_ptr(new ContentType{*this}); + } static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::CONTENT_TYPE; + return attr->type() == Attribute::TYPE::CONTENT_TYPE; } void accept(Visitor& visitor) const override; - ~ContentType() override; + ~ContentType() override = default; private: oid_t oid_; diff --git a/include/LIEF/PE/signature/attributes/GenericType.hpp b/include/LIEF/PE/signature/attributes/GenericType.hpp index 9f78fb6d6c..0dabc844fd 100644 --- a/include/LIEF/PE/signature/attributes/GenericType.hpp +++ b/include/LIEF/PE/signature/attributes/GenericType.hpp @@ -29,22 +29,26 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - //! Interface over an attribute for which the internal structure is not supported by LIEF class LIEF_API GenericType : public Attribute { - friend class Parser; friend class SignatureParser; public: - GenericType(); - GenericType(oid_t oid, std::vector raw); - GenericType(const GenericType&); - GenericType& operator=(const GenericType&); - - std::unique_ptr clone() const override; + GenericType() : + Attribute(Attribute::TYPE::GENERIC_TYPE) + {} + GenericType(oid_t oid, std::vector raw) : + Attribute(Attribute::TYPE::GENERIC_TYPE), + oid_{std::move(oid)}, + raw_{std::move(raw)} + {} + GenericType(const GenericType&) = default; + GenericType& operator=(const GenericType&) = default; + + std::unique_ptr clone() const override { + return std::unique_ptr(new GenericType{*this}); + } //! OID of the original attribute const oid_t& oid() const { @@ -61,10 +65,10 @@ class LIEF_API GenericType : public Attribute { void accept(Visitor& visitor) const override; - ~GenericType() override; + ~GenericType() override = default; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::GENERIC_TYPE; + return attr->type() == Attribute::TYPE::GENERIC_TYPE; } private: diff --git a/include/LIEF/PE/signature/attributes/MsSpcNestedSignature.hpp b/include/LIEF/PE/signature/attributes/MsSpcNestedSignature.hpp index 0a564216d3..563c1154fa 100644 --- a/include/LIEF/PE/signature/attributes/MsSpcNestedSignature.hpp +++ b/include/LIEF/PE/signature/attributes/MsSpcNestedSignature.hpp @@ -26,9 +26,6 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - //! Interface over the structure described by the OID ``1.3.6.1.4.1.311.2.4.1`` //! //! The internal structure is not documented but we can infer the following structure: @@ -44,12 +41,17 @@ class LIEF_API MsSpcNestedSignature : public Attribute { friend class SignatureParser; public: - MsSpcNestedSignature(); - MsSpcNestedSignature(Signature sig); - MsSpcNestedSignature(const MsSpcNestedSignature&); - MsSpcNestedSignature& operator=(const MsSpcNestedSignature&); - - std::unique_ptr clone() const override; + MsSpcNestedSignature() = delete; + MsSpcNestedSignature(Signature sig) : + Attribute(Attribute::TYPE::MS_SPC_NESTED_SIGN), + sig_{std::move(sig)} + {} + MsSpcNestedSignature(const MsSpcNestedSignature&) = default; + MsSpcNestedSignature& operator=(const MsSpcNestedSignature&) = default; + + std::unique_ptr clone() const override { + return std::unique_ptr(new MsSpcNestedSignature{*this}); + } //! Underlying Signature object const Signature& sig() const { @@ -62,10 +64,10 @@ class LIEF_API MsSpcNestedSignature : public Attribute { void accept(Visitor& visitor) const override; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::MS_SPC_NESTED_SIGN; + return attr->type() == Attribute::TYPE::MS_SPC_NESTED_SIGN; } - ~MsSpcNestedSignature() override; + ~MsSpcNestedSignature() override = default; private: Signature sig_; diff --git a/include/LIEF/PE/signature/attributes/MsSpcStatementType.hpp b/include/LIEF/PE/signature/attributes/MsSpcStatementType.hpp index 9a8abe14d8..d29b3195ab 100644 --- a/include/LIEF/PE/signature/attributes/MsSpcStatementType.hpp +++ b/include/LIEF/PE/signature/attributes/MsSpcStatementType.hpp @@ -25,9 +25,6 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - //! Interface over the structure described by the OID ``1.3.6.1.4.1.311.2.1.11`` //! //! The internal structure is described in the official document: @@ -42,12 +39,18 @@ class LIEF_API MsSpcStatementType : public Attribute { friend class SignatureParser; public: - MsSpcStatementType(); - MsSpcStatementType(oid_t oid); - MsSpcStatementType(const MsSpcStatementType&); - MsSpcStatementType& operator=(const MsSpcStatementType&); + MsSpcStatementType() = delete; + MsSpcStatementType(oid_t oid) : + Attribute(Attribute::TYPE::MS_SPC_STATEMENT_TYPE), + oid_{std::move(oid)} + {} + + MsSpcStatementType(const MsSpcStatementType&) = default; + MsSpcStatementType& operator=(const MsSpcStatementType&) = default; - std::unique_ptr clone() const override; + std::unique_ptr clone() const override { + return std::unique_ptr(new MsSpcStatementType{*this}); + } //! According to the documentation: //! > The SpcStatementType MUST contain one Object Identifier with either @@ -61,11 +64,11 @@ class LIEF_API MsSpcStatementType : public Attribute { std::string print() const override; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::MS_SPC_STATEMENT_TYPE; + return attr->type() == Attribute::TYPE::MS_SPC_STATEMENT_TYPE; } void accept(Visitor& visitor) const override; - ~MsSpcStatementType() override; + ~MsSpcStatementType() override = default; private: oid_t oid_; diff --git a/include/LIEF/PE/signature/attributes/PKCS9AtSequenceNumber.hpp b/include/LIEF/PE/signature/attributes/PKCS9AtSequenceNumber.hpp index 89a4cf1033..0c1f15290d 100644 --- a/include/LIEF/PE/signature/attributes/PKCS9AtSequenceNumber.hpp +++ b/include/LIEF/PE/signature/attributes/PKCS9AtSequenceNumber.hpp @@ -48,12 +48,18 @@ class LIEF_API PKCS9AtSequenceNumber : public Attribute { friend class SignatureParser; public: - PKCS9AtSequenceNumber(); - PKCS9AtSequenceNumber(uint32_t num); - PKCS9AtSequenceNumber(const PKCS9AtSequenceNumber&); - PKCS9AtSequenceNumber& operator=(const PKCS9AtSequenceNumber&); + PKCS9AtSequenceNumber() = delete; + PKCS9AtSequenceNumber(uint32_t num) : + Attribute(Attribute::TYPE::PKCS9_AT_SEQUENCE_NUMBER), + number_{num} + {} - std::unique_ptr clone() const override; + PKCS9AtSequenceNumber(const PKCS9AtSequenceNumber&) = default; + PKCS9AtSequenceNumber& operator=(const PKCS9AtSequenceNumber&) = default; + + std::unique_ptr clone() const override { + return std::unique_ptr(new PKCS9AtSequenceNumber{*this}); + } //! Number as described in the RFC uint32_t number() const { @@ -64,14 +70,15 @@ class LIEF_API PKCS9AtSequenceNumber : public Attribute { std::string print() const override; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::PKCS9_AT_SEQUENCE_NUMBER; + return attr->type() == Attribute::TYPE::PKCS9_AT_SEQUENCE_NUMBER; } void accept(Visitor& visitor) const override; - ~PKCS9AtSequenceNumber() override; + + ~PKCS9AtSequenceNumber() override = default; private: - uint32_t number_; + uint32_t number_ = 0; }; } diff --git a/include/LIEF/PE/signature/attributes/PKCS9CounterSignature.hpp b/include/LIEF/PE/signature/attributes/PKCS9CounterSignature.hpp index c50c6e8195..25f15bc8de 100644 --- a/include/LIEF/PE/signature/attributes/PKCS9CounterSignature.hpp +++ b/include/LIEF/PE/signature/attributes/PKCS9CounterSignature.hpp @@ -24,10 +24,6 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - - //! Interface over the structure described by the OID ``1.2.840.113549.1.9.6`` (PKCS #9) //! //! The internal structure is described in the @@ -45,12 +41,18 @@ class LIEF_API PKCS9CounterSignature : public Attribute { friend class SignatureParser; public: - PKCS9CounterSignature(); - PKCS9CounterSignature(SignerInfo signer); - PKCS9CounterSignature(const PKCS9CounterSignature&); - PKCS9CounterSignature& operator=(const PKCS9CounterSignature&); + PKCS9CounterSignature() = delete; + PKCS9CounterSignature(SignerInfo signer) : + Attribute(Attribute::TYPE::PKCS9_COUNTER_SIGNATURE), + signer_{std::move(signer)} + {} - std::unique_ptr clone() const override; + PKCS9CounterSignature(const PKCS9CounterSignature&) = default; + PKCS9CounterSignature& operator=(const PKCS9CounterSignature&) = default; + + std::unique_ptr clone() const override { + return std::unique_ptr(new PKCS9CounterSignature{*this}); + } //! SignerInfo as described in the RFC #2985 const SignerInfo& signer() const { @@ -61,12 +63,12 @@ class LIEF_API PKCS9CounterSignature : public Attribute { std::string print() const override; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE; + return attr->type() == Attribute::TYPE::PKCS9_COUNTER_SIGNATURE; } void accept(Visitor& visitor) const override; - ~PKCS9CounterSignature() override; + ~PKCS9CounterSignature() override = default; private: SignerInfo signer_; diff --git a/include/LIEF/PE/signature/attributes/PKCS9MessageDigest.hpp b/include/LIEF/PE/signature/attributes/PKCS9MessageDigest.hpp index 79def32bd9..aa633bb41e 100644 --- a/include/LIEF/PE/signature/attributes/PKCS9MessageDigest.hpp +++ b/include/LIEF/PE/signature/attributes/PKCS9MessageDigest.hpp @@ -51,12 +51,18 @@ class LIEF_API PKCS9MessageDigest : public Attribute { friend class SignatureParser; public: - PKCS9MessageDigest(); - PKCS9MessageDigest(std::vector digest); - PKCS9MessageDigest(const PKCS9MessageDigest&); - PKCS9MessageDigest& operator=(const PKCS9MessageDigest&); + PKCS9MessageDigest() = delete; + PKCS9MessageDigest(std::vector digest) : + Attribute(Attribute::TYPE::PKCS9_MESSAGE_DIGEST), + digest_{std::move(digest)} + {} - std::unique_ptr clone() const override; + PKCS9MessageDigest(const PKCS9MessageDigest&) = default; + PKCS9MessageDigest& operator=(const PKCS9MessageDigest&) = default; + + std::unique_ptr clone() const override { + return std::unique_ptr(new PKCS9MessageDigest{*this}); + } //! Message digeset as a blob of bytes as described in the RFC span digest() const { @@ -67,12 +73,12 @@ class LIEF_API PKCS9MessageDigest : public Attribute { std::string print() const override; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST; + return attr->type() == Attribute::TYPE::PKCS9_MESSAGE_DIGEST; } void accept(Visitor& visitor) const override; - ~PKCS9MessageDigest() override; + ~PKCS9MessageDigest() override = default; private: std::vector digest_; diff --git a/include/LIEF/PE/signature/attributes/PKCS9SigningTime.hpp b/include/LIEF/PE/signature/attributes/PKCS9SigningTime.hpp index 7a89377d31..88ebdf3dca 100644 --- a/include/LIEF/PE/signature/attributes/PKCS9SigningTime.hpp +++ b/include/LIEF/PE/signature/attributes/PKCS9SigningTime.hpp @@ -26,9 +26,6 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - //! Interface over the structure described by the OID ``1.2.840.113549.1.9.5`` (PKCS #9) //! //! The internal structure is described in the @@ -53,10 +50,14 @@ class LIEF_API PKCS9SigningTime : public Attribute { //! Time as an array [year, month, day, hour, min, sec] using time_t = std::array; - PKCS9SigningTime(); - PKCS9SigningTime(time_t time); - PKCS9SigningTime(const PKCS9SigningTime&); - PKCS9SigningTime& operator=(const PKCS9SigningTime&); + PKCS9SigningTime() = delete; + PKCS9SigningTime(time_t time) : + Attribute(Attribute::TYPE::PKCS9_SIGNING_TIME), + time_{time} + {} + + PKCS9SigningTime(const PKCS9SigningTime&) = default; + PKCS9SigningTime& operator=(const PKCS9SigningTime&) = default; //! Time as an array [year, month, day, hour, min, sec] const time_t& time() const { @@ -66,14 +67,17 @@ class LIEF_API PKCS9SigningTime : public Attribute { //! Print information about the attribute std::string print() const override; - std::unique_ptr clone() const override; + std::unique_ptr clone() const override { + return std::unique_ptr(new PKCS9SigningTime{*this}); + } static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME; + return attr->type() == Attribute::TYPE::PKCS9_SIGNING_TIME; } void accept(Visitor& visitor) const override; - ~PKCS9SigningTime() override; + + ~PKCS9SigningTime() override = default; private: time_t time_; diff --git a/include/LIEF/PE/signature/attributes/SpcSpOpusInfo.hpp b/include/LIEF/PE/signature/attributes/SpcSpOpusInfo.hpp index 46d067d477..15e589d552 100644 --- a/include/LIEF/PE/signature/attributes/SpcSpOpusInfo.hpp +++ b/include/LIEF/PE/signature/attributes/SpcSpOpusInfo.hpp @@ -27,9 +27,6 @@ namespace LIEF { class VectorStream; namespace PE { -class Parser; -class SignatureParser; - //! Interface over the structure described by the OID ``1.3.6.1.4.1.311.2.1.12`` //! //! The internal structure is described in the official document: @@ -47,12 +44,22 @@ class LIEF_API SpcSpOpusInfo : public Attribute { friend class SignatureParser; public: - SpcSpOpusInfo(); - SpcSpOpusInfo(std::string program_name, std::string more_info); - SpcSpOpusInfo(const SpcSpOpusInfo&); - SpcSpOpusInfo& operator=(const SpcSpOpusInfo&); + SpcSpOpusInfo(std::string program_name, std::string more_info) : + Attribute(Attribute::TYPE::SPC_SP_OPUS_INFO), + program_name_(std::move(program_name)), + more_info_(std::move(more_info)) + {} + + SpcSpOpusInfo() : + SpcSpOpusInfo("", "") + {} - std::unique_ptr clone() const override; + SpcSpOpusInfo(const SpcSpOpusInfo&) = default; + SpcSpOpusInfo& operator=(const SpcSpOpusInfo&) = default; + + std::unique_ptr clone() const override { + return std::unique_ptr(new SpcSpOpusInfo{*this}); + } //! Program description provided by the publisher const std::string& program_name() const { @@ -68,12 +75,12 @@ class LIEF_API SpcSpOpusInfo : public Attribute { std::string print() const override; static bool classof(const Attribute* attr) { - return attr->type() == SIG_ATTRIBUTE_TYPES::SPC_SP_OPUS_INFO; + return attr->type() == Attribute::TYPE::SPC_SP_OPUS_INFO; } void accept(Visitor& visitor) const override; - ~SpcSpOpusInfo() override; + ~SpcSpOpusInfo() override = default; private: std::string program_name_; diff --git a/src/PE/EnumToString.cpp b/src/PE/EnumToString.cpp index 824cdc4498..8ba771186d 100644 --- a/src/PE/EnumToString.cpp +++ b/src/PE/EnumToString.cpp @@ -1161,23 +1161,7 @@ const char* to_string(ALGORITHMS e) { } -const char* to_string(SIG_ATTRIBUTE_TYPES e) { - CONST_MAP(SIG_ATTRIBUTE_TYPES, const char*, 11) enumStrings { - { SIG_ATTRIBUTE_TYPES::UNKNOWN, "UNKNOWN" }, - { SIG_ATTRIBUTE_TYPES::CONTENT_TYPE, "CONTENT_TYPE" }, - { SIG_ATTRIBUTE_TYPES::GENERIC_TYPE, "GENERIC_TYPE" }, - { SIG_ATTRIBUTE_TYPES::SPC_SP_OPUS_INFO, "SPC_SP_OPUS_INFO" }, - { SIG_ATTRIBUTE_TYPES::MS_COUNTER_SIGN, "MS_COUNTER_SIGN" }, - { SIG_ATTRIBUTE_TYPES::MS_SPC_NESTED_SIGN, "MS_SPC_NESTED_SIGN" }, - { SIG_ATTRIBUTE_TYPES::MS_SPC_STATEMENT_TYPE, "MS_SPC_STATEMENT_TYPE" }, - { SIG_ATTRIBUTE_TYPES::PKCS9_AT_SEQUENCE_NUMBER, "PKCS9_AT_SEQUENCE_NUMBER" }, - { SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE, "PKCS9_COUNTER_SIGNATURE" }, - { SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST, "PKCS9_MESSAGE_DIGEST" }, - { SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME, "PKCS9_SIGNING_TIME" }, - }; - const auto it = enumStrings.find(e); - return it == enumStrings.end() ? "UNKNOWN" : it->second; -} + } // namespace PE } // namespace LIEF diff --git a/src/PE/signature/Attribute.cpp b/src/PE/signature/Attribute.cpp index 3833e1134a..6d07606a52 100644 --- a/src/PE/signature/Attribute.cpp +++ b/src/PE/signature/Attribute.cpp @@ -13,32 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include #include #include +#include "frozen.hpp" + #include "LIEF/Visitor.hpp" #include "LIEF/PE/signature/Attribute.hpp" namespace LIEF { namespace PE { -Attribute::Attribute() = default; -Attribute::Attribute(const Attribute& other) = default; - -Attribute::Attribute(SIG_ATTRIBUTE_TYPES type) : - type_{type} -{} - -Attribute& Attribute::operator=(const Attribute& other) { - if (this != &other) { - type_ = other.type_; - } - return *this; -} - -Attribute::~Attribute() = default; - void Attribute::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -48,5 +33,29 @@ std::ostream& operator<<(std::ostream& os, const Attribute& attribute) { return os; } +const char* to_string(Attribute::TYPE e) { + #define ENTRY(X) std::pair(Attribute::TYPE::X, #X) + STRING_MAP enums2str { + ENTRY(UNKNOWN), + ENTRY(CONTENT_TYPE), + ENTRY(GENERIC_TYPE), + ENTRY(SPC_SP_OPUS_INFO), + ENTRY(MS_COUNTER_SIGN), + ENTRY(MS_SPC_NESTED_SIGN), + ENTRY(MS_SPC_STATEMENT_TYPE), + ENTRY(PKCS9_AT_SEQUENCE_NUMBER), + ENTRY(PKCS9_COUNTER_SIGNATURE), + ENTRY(PKCS9_MESSAGE_DIGEST), + ENTRY(PKCS9_SIGNING_TIME), + }; + #undef ENTRY + + if (auto it = enums2str.find(e); it != enums2str.end()) { + return it->second; + } + + return "UNKNOWN"; +} + } } diff --git a/src/PE/signature/Signature.cpp b/src/PE/signature/Signature.cpp index 97ddc92bbf..60276fba19 100644 --- a/src/PE/signature/Signature.cpp +++ b/src/PE/signature/Signature.cpp @@ -114,7 +114,7 @@ Signature::VERIFICATION_FLAGS verify_ts_counter_signature(const SignerInfo& sign */ // Verify 1. - const auto* content_type_data = reinterpret_cast(cs_signer.get_auth_attribute(SIG_ATTRIBUTE_TYPES::CONTENT_TYPE)); + const auto* content_type_data = static_cast(cs_signer.get_auth_attribute(Attribute::TYPE::CONTENT_TYPE)); if (content_type_data == nullptr) { LIEF_WARN("Missing ContentType in authenticated attributes in the counter signature's signer"); return flags | Signature::VERIFICATION_FLAGS::INVALID_SIGNER; @@ -127,7 +127,7 @@ Signature::VERIFICATION_FLAGS verify_ts_counter_signature(const SignerInfo& sign } // Verify 3. - const auto* message_dg = reinterpret_cast(cs_signer.get_auth_attribute(SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST)); + const auto* message_dg = static_cast(cs_signer.get_auth_attribute(Attribute::TYPE::PKCS9_MESSAGE_DIGEST)); if (message_dg == nullptr) { LIEF_WARN("Missing MessageDigest in authenticated attributes in the counter signature's signer"); return flags | Signature::VERIFICATION_FLAGS::INVALID_SIGNER; @@ -143,7 +143,7 @@ Signature::VERIFICATION_FLAGS verify_ts_counter_signature(const SignerInfo& sign * Verify that signing's time is valid within the signer's certificate * validity window. */ - const auto* signing_time = reinterpret_cast(cs_signer.get_auth_attribute(SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME)); + const auto* signing_time = static_cast(cs_signer.get_auth_attribute(Attribute::TYPE::PKCS9_SIGNING_TIME)); if (signing_time != nullptr && !is_true(checks & Signature::VERIFICATION_CHECKS::SKIP_CERT_TIME)) { LIEF_DEBUG("PKCS #9 signing time found"); PKCS9SigningTime::time_t time = signing_time->time(); @@ -340,7 +340,7 @@ Signature::VERIFICATION_FLAGS Signature::check(VERIFICATION_CHECKS checks) const // Check that content_info_hash matches pkcs9-message-digest auto it_pkcs9_digest = std::find_if(std::begin(auth_attrs), std::end(auth_attrs), [] (const Attribute& attr) { - return attr.type() == SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST; + return attr.type() == Attribute::TYPE::PKCS9_MESSAGE_DIGEST; }); if (it_pkcs9_digest == std::end(auth_attrs)) { @@ -348,7 +348,7 @@ Signature::VERIFICATION_FLAGS Signature::check(VERIFICATION_CHECKS checks) const return flags | VERIFICATION_FLAGS::MISSING_PKCS9_MESSAGE_DIGEST; } - const auto& digest_attr = reinterpret_cast(*it_pkcs9_digest); + const auto& digest_attr = static_cast(*it_pkcs9_digest); LIEF_DEBUG("pkcs9-message-digest:\n {}\n {}", hex_dump(digest_attr.digest()), hex_dump(content_info_hash)); if (as_vector(digest_attr.digest()) != content_info_hash) { return flags | VERIFICATION_FLAGS::BAD_DIGEST; @@ -365,11 +365,11 @@ Signature::VERIFICATION_FLAGS Signature::check(VERIFICATION_CHECKS checks) const /* * CounterSignature Checks */ - const auto* counter = reinterpret_cast(signer.get_unauth_attribute(SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE)); + const auto* counter = static_cast(signer.get_unauth_attribute(Attribute::TYPE::PKCS9_COUNTER_SIGNATURE)); bool has_ms_counter_sig = false; for (const Attribute& attr : signer.unauthenticated_attributes()) { - if (attr.type() == SIG_ATTRIBUTE_TYPES::GENERIC_TYPE) { - if (reinterpret_cast(attr).oid() == /* Ms-CounterSign */ "1.3.6.1.4.1.311.3.3.1") { + if (attr.type() == Attribute::TYPE::GENERIC_TYPE) { + if (static_cast(attr).oid() == /* Ms-CounterSign */ "1.3.6.1.4.1.311.3.3.1") { has_ms_counter_sig = true; break; } @@ -469,23 +469,23 @@ inline void print_attr(SignerInfo::it_const_attributes_t& attrs, std::ostream& o for (const Attribute& attr : attrs) { std::string suffix; switch (attr.type()) { - case SIG_ATTRIBUTE_TYPES::CONTENT_TYPE: + case Attribute::TYPE::CONTENT_TYPE: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); suffix = ct.oid() + " (" + oid_to_string(ct.oid()) + ")"; break; } - case SIG_ATTRIBUTE_TYPES::MS_SPC_STATEMENT_TYPE: + case Attribute::TYPE::MS_SPC_STATEMENT_TYPE: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); suffix = ct.oid() + " (" + oid_to_string(ct.oid()) + ")"; break; } - case SIG_ATTRIBUTE_TYPES::SPC_SP_OPUS_INFO: + case Attribute::TYPE::SPC_SP_OPUS_INFO: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); if (!ct.program_name().empty()) { suffix = ct.program_name(); } @@ -498,16 +498,16 @@ inline void print_attr(SignerInfo::it_const_attributes_t& attrs, std::ostream& o break; } - case SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST: + case Attribute::TYPE::PKCS9_MESSAGE_DIGEST: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); suffix = hex_dump(ct.digest()).substr(0, 41) + "..."; break; } - case SIG_ATTRIBUTE_TYPES::MS_SPC_NESTED_SIGN: + case Attribute::TYPE::MS_SPC_NESTED_SIGN: { - const auto& nested_attr = reinterpret_cast(attr); + const auto& nested_attr = static_cast(attr); const Signature& ct = nested_attr.sig(); auto signers = ct.signers(); auto crts = ct.certificates(); @@ -519,31 +519,31 @@ inline void print_attr(SignerInfo::it_const_attributes_t& attrs, std::ostream& o break; } - case SIG_ATTRIBUTE_TYPES::GENERIC_TYPE: + case Attribute::TYPE::GENERIC_TYPE: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); suffix = ct.oid(); break; } - case SIG_ATTRIBUTE_TYPES::PKCS9_AT_SEQUENCE_NUMBER: + case Attribute::TYPE::PKCS9_AT_SEQUENCE_NUMBER: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); suffix = std::to_string(ct.number()); break; } - case SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE: + case Attribute::TYPE::PKCS9_COUNTER_SIGNATURE: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); const SignerInfo& signer = ct.signer(); suffix = signer.issuer(); break; } - case SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME: + case Attribute::TYPE::PKCS9_SIGNING_TIME: { - const auto& ct = reinterpret_cast(attr); + const auto& ct = static_cast(attr); const PKCS9SigningTime::time_t time = ct.time(); suffix = fmt::format("{}/{}/{} - {}:{}:{}", time[0], time[1], time[2], time[3], time[4], time[5]); diff --git a/src/PE/signature/SignatureParser.cpp b/src/PE/signature/SignatureParser.cpp index 72a28db487..25ebad9581 100644 --- a/src/PE/signature/SignatureParser.cpp +++ b/src/PE/signature/SignatureParser.cpp @@ -327,7 +327,7 @@ result SignatureParser::parse_signature(BinaryStream& stream) { } else { LIEF_INFO("Can't find x509 certificate associated with signer '{}'", signer.issuer()); } - const auto* cs = static_cast(signer.get_attribute(SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE)); + const auto* cs = static_cast(signer.get_attribute(Attribute::TYPE::PKCS9_COUNTER_SIGNATURE)); if (cs != nullptr) { SignerInfo& cs_signer = const_cast(cs)->signer_; const x509* crt = signature.find_crt_issuer(cs_signer.issuer(), as_vector(cs_signer.serial_number())); diff --git a/src/PE/signature/SignerInfo.cpp b/src/PE/signature/SignerInfo.cpp index d885b5f801..fc167746fb 100644 --- a/src/PE/signature/SignerInfo.cpp +++ b/src/PE/signature/SignerInfo.cpp @@ -76,41 +76,12 @@ void SignerInfo::swap(SignerInfo& other) { std::swap(cert_, other.cert_); } - -uint32_t SignerInfo::version() const { - return version_; -} - -ALGORITHMS SignerInfo::digest_algorithm() const { - return digest_algorithm_; -} - -ALGORITHMS SignerInfo::encryption_algorithm() const { - return digest_enc_algorithm_; -} - -const SignerInfo::encrypted_digest_t& SignerInfo::encrypted_digest() const { - return encrypted_digest_; -} - -SignerInfo::it_const_attributes_t SignerInfo::authenticated_attributes() const { - return authenticated_attributes_; -} - -SignerInfo::it_const_attributes_t SignerInfo::unauthenticated_attributes() const { - return unauthenticated_attributes_; -} - - -const Attribute* SignerInfo::get_attribute(PE::SIG_ATTRIBUTE_TYPES type) const { - const Attribute* attr = get_auth_attribute(type); - if (attr != nullptr) { +const Attribute* SignerInfo::get_attribute(Attribute::TYPE type) const { + if (const Attribute* attr = get_auth_attribute(type)) { return attr; } - attr = get_unauth_attribute(type); - - if (attr != nullptr) { + if (const Attribute* attr = get_unauth_attribute(type)) { return attr; } @@ -118,8 +89,9 @@ const Attribute* SignerInfo::get_attribute(PE::SIG_ATTRIBUTE_TYPES type) const { return nullptr; } -const Attribute* SignerInfo::get_auth_attribute(PE::SIG_ATTRIBUTE_TYPES type) const { - auto it_auth = std::find_if(std::begin(authenticated_attributes_), std::end(authenticated_attributes_), +const Attribute* SignerInfo::get_auth_attribute(Attribute::TYPE type) const { + auto it_auth = std::find_if( + std::begin(authenticated_attributes_), std::end(authenticated_attributes_), [type] (const std::unique_ptr& attr) { return attr->type() == type; }); @@ -129,8 +101,9 @@ const Attribute* SignerInfo::get_auth_attribute(PE::SIG_ATTRIBUTE_TYPES type) co return nullptr; } -const Attribute* SignerInfo::get_unauth_attribute(PE::SIG_ATTRIBUTE_TYPES type) const { - auto it_uauth = std::find_if(std::begin(unauthenticated_attributes_), std::end(unauthenticated_attributes_), +const Attribute* SignerInfo::get_unauth_attribute(Attribute::TYPE type) const { + auto it_uauth = std::find_if( + std::begin(unauthenticated_attributes_), std::end(unauthenticated_attributes_), [type] (const std::unique_ptr& attr) { return attr->type() == type; }); diff --git a/src/PE/signature/attributes/ContentType.cpp b/src/PE/signature/attributes/ContentType.cpp index c449eb8945..c11d8ab963 100644 --- a/src/PE/signature/attributes/ContentType.cpp +++ b/src/PE/signature/attributes/ContentType.cpp @@ -15,29 +15,12 @@ */ #include "LIEF/PE/signature/attributes/ContentType.hpp" #include "LIEF/PE/signature/OIDToString.hpp" -#include "LIEF/PE/signature/Attribute.hpp" #include "LIEF/Visitor.hpp" namespace LIEF { namespace PE { -ContentType::ContentType() : - Attribute(SIG_ATTRIBUTE_TYPES::CONTENT_TYPE) -{} - -ContentType::ContentType(const ContentType&) = default; -ContentType& ContentType::operator=(const ContentType&) = default; - -std::unique_ptr ContentType::clone() const { - return std::unique_ptr(new ContentType{*this}); -} - -ContentType::ContentType(oid_t oid) : - Attribute(SIG_ATTRIBUTE_TYPES::CONTENT_TYPE), - oid_{std::move(oid)} -{} - void ContentType::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -46,9 +29,5 @@ std::string ContentType::print() const { return oid() + " (" + oid_to_string(oid()) + ")"; } - -ContentType::~ContentType() = default; - - } } diff --git a/src/PE/signature/attributes/GenericType.cpp b/src/PE/signature/attributes/GenericType.cpp index 25c1f46196..e9e7e6ba36 100644 --- a/src/PE/signature/attributes/GenericType.cpp +++ b/src/PE/signature/attributes/GenericType.cpp @@ -15,26 +15,10 @@ */ #include "LIEF/Visitor.hpp" #include "LIEF/PE/signature/attributes/GenericType.hpp" + namespace LIEF { namespace PE { -GenericType::GenericType() : - Attribute(SIG_ATTRIBUTE_TYPES::GENERIC_TYPE) -{} - -GenericType::GenericType(const GenericType&) = default; -GenericType& GenericType::operator=(const GenericType&) = default; - -std::unique_ptr GenericType::clone() const { - return std::unique_ptr(new GenericType{*this}); -} - -GenericType::GenericType(oid_t oid, std::vector raw) : - Attribute(SIG_ATTRIBUTE_TYPES::GENERIC_TYPE), - oid_{std::move(oid)}, - raw_{std::move(raw)} -{} - void GenericType::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -43,8 +27,5 @@ std::string GenericType::print() const { return oid() + " (" + std::to_string(raw_content().size()) + " bytes)"; } - -GenericType::~GenericType() = default; - } } diff --git a/src/PE/signature/attributes/MsSpcNestedSignature.cpp b/src/PE/signature/attributes/MsSpcNestedSignature.cpp index 0d252c98ff..ad6bbfbdae 100644 --- a/src/PE/signature/attributes/MsSpcNestedSignature.cpp +++ b/src/PE/signature/attributes/MsSpcNestedSignature.cpp @@ -21,23 +21,6 @@ namespace LIEF { namespace PE { -MsSpcNestedSignature::MsSpcNestedSignature() : - Attribute(SIG_ATTRIBUTE_TYPES::MS_SPC_NESTED_SIGN) -{} - -MsSpcNestedSignature::MsSpcNestedSignature(const MsSpcNestedSignature&) = default; -MsSpcNestedSignature& MsSpcNestedSignature::operator=(const MsSpcNestedSignature&) = default; - -MsSpcNestedSignature::MsSpcNestedSignature(Signature sig) : - Attribute(SIG_ATTRIBUTE_TYPES::MS_SPC_NESTED_SIGN), - sig_{std::move(sig)} -{} - -std::unique_ptr MsSpcNestedSignature::clone() const { - return std::unique_ptr(new MsSpcNestedSignature{*this}); -} - - void MsSpcNestedSignature::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -50,7 +33,5 @@ std::string MsSpcNestedSignature::print() const { } -MsSpcNestedSignature::~MsSpcNestedSignature() = default; - } } diff --git a/src/PE/signature/attributes/MsSpcStatementType.cpp b/src/PE/signature/attributes/MsSpcStatementType.cpp index d6f064088e..d5762555de 100644 --- a/src/PE/signature/attributes/MsSpcStatementType.cpp +++ b/src/PE/signature/attributes/MsSpcStatementType.cpp @@ -19,22 +19,6 @@ namespace LIEF { namespace PE { -MsSpcStatementType::MsSpcStatementType() : - Attribute(SIG_ATTRIBUTE_TYPES::MS_SPC_STATEMENT_TYPE) -{} - -MsSpcStatementType::MsSpcStatementType(const MsSpcStatementType&) = default; -MsSpcStatementType& MsSpcStatementType::operator=(const MsSpcStatementType&) = default; - -std::unique_ptr MsSpcStatementType::clone() const { - return std::unique_ptr(new MsSpcStatementType{*this}); -} - -MsSpcStatementType::MsSpcStatementType(oid_t oid) : - Attribute(SIG_ATTRIBUTE_TYPES::MS_SPC_STATEMENT_TYPE), - oid_{std::move(oid)} -{} - void MsSpcStatementType::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -42,9 +26,5 @@ void MsSpcStatementType::accept(Visitor& visitor) const { std::string MsSpcStatementType::print() const { return oid() + " (" + oid_to_string(oid()) + ")"; } - - -MsSpcStatementType::~MsSpcStatementType() = default; - } } diff --git a/src/PE/signature/attributes/PKCS9AtSequenceNumber.cpp b/src/PE/signature/attributes/PKCS9AtSequenceNumber.cpp index 26a9e92776..cf24311b0f 100644 --- a/src/PE/signature/attributes/PKCS9AtSequenceNumber.cpp +++ b/src/PE/signature/attributes/PKCS9AtSequenceNumber.cpp @@ -19,22 +19,6 @@ namespace LIEF { namespace PE { -PKCS9AtSequenceNumber::PKCS9AtSequenceNumber() : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_AT_SEQUENCE_NUMBER) -{} - -PKCS9AtSequenceNumber::PKCS9AtSequenceNumber(const PKCS9AtSequenceNumber&) = default; -PKCS9AtSequenceNumber& PKCS9AtSequenceNumber::operator=(const PKCS9AtSequenceNumber&) = default; - -std::unique_ptr PKCS9AtSequenceNumber::clone() const { - return std::unique_ptr(new PKCS9AtSequenceNumber{*this}); -} - -PKCS9AtSequenceNumber::PKCS9AtSequenceNumber(uint32_t num) : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_AT_SEQUENCE_NUMBER), - number_{num} -{} - void PKCS9AtSequenceNumber::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -43,8 +27,5 @@ std::string PKCS9AtSequenceNumber::print() const { return std::to_string(number()); } - -PKCS9AtSequenceNumber::~PKCS9AtSequenceNumber() = default; - } } diff --git a/src/PE/signature/attributes/PKCS9CounterSignature.cpp b/src/PE/signature/attributes/PKCS9CounterSignature.cpp index f2004843af..e64fa204cf 100644 --- a/src/PE/signature/attributes/PKCS9CounterSignature.cpp +++ b/src/PE/signature/attributes/PKCS9CounterSignature.cpp @@ -20,22 +20,6 @@ namespace LIEF { namespace PE { -PKCS9CounterSignature::PKCS9CounterSignature() : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE) -{} - -PKCS9CounterSignature::PKCS9CounterSignature(const PKCS9CounterSignature&) = default; -PKCS9CounterSignature& PKCS9CounterSignature::operator=(const PKCS9CounterSignature&) = default; - -PKCS9CounterSignature::PKCS9CounterSignature(SignerInfo signer) : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_COUNTER_SIGNATURE), - signer_{std::move(signer)} -{} - -std::unique_ptr PKCS9CounterSignature::clone() const { - return std::unique_ptr(new PKCS9CounterSignature{*this}); -} - void PKCS9CounterSignature::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -47,7 +31,5 @@ std::string PKCS9CounterSignature::print() const { } -PKCS9CounterSignature::~PKCS9CounterSignature() = default; - } } diff --git a/src/PE/signature/attributes/PKCS9MessageDigest.cpp b/src/PE/signature/attributes/PKCS9MessageDigest.cpp index d36c39cb91..9101545fde 100644 --- a/src/PE/signature/attributes/PKCS9MessageDigest.cpp +++ b/src/PE/signature/attributes/PKCS9MessageDigest.cpp @@ -20,23 +20,6 @@ namespace LIEF { namespace PE { -PKCS9MessageDigest::PKCS9MessageDigest() : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST) -{} - -PKCS9MessageDigest::PKCS9MessageDigest(const PKCS9MessageDigest&) = default; -PKCS9MessageDigest& PKCS9MessageDigest::operator=(const PKCS9MessageDigest&) = default; - -PKCS9MessageDigest::PKCS9MessageDigest(std::vector digest) : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_MESSAGE_DIGEST), - digest_{std::move(digest)} -{} - -std::unique_ptr PKCS9MessageDigest::clone() const { - return std::unique_ptr(new PKCS9MessageDigest{*this}); -} - - void PKCS9MessageDigest::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -44,9 +27,5 @@ void PKCS9MessageDigest::accept(Visitor& visitor) const { std::string PKCS9MessageDigest::print() const { return hex_dump(digest()); } - - -PKCS9MessageDigest::~PKCS9MessageDigest() = default; - } } diff --git a/src/PE/signature/attributes/PKCS9SigningTime.cpp b/src/PE/signature/attributes/PKCS9SigningTime.cpp index 5a55de6e0c..ee280497bb 100644 --- a/src/PE/signature/attributes/PKCS9SigningTime.cpp +++ b/src/PE/signature/attributes/PKCS9SigningTime.cpp @@ -19,22 +19,6 @@ namespace LIEF { namespace PE { -PKCS9SigningTime::PKCS9SigningTime() : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME) -{} - -PKCS9SigningTime::PKCS9SigningTime(const PKCS9SigningTime&) = default; -PKCS9SigningTime& PKCS9SigningTime::operator=(const PKCS9SigningTime&) = default; - -std::unique_ptr PKCS9SigningTime::clone() const { - return std::unique_ptr(new PKCS9SigningTime{*this}); -} - -PKCS9SigningTime::PKCS9SigningTime(time_t time) : - Attribute(SIG_ATTRIBUTE_TYPES::PKCS9_SIGNING_TIME), - time_{time} -{} - void PKCS9SigningTime::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -45,8 +29,5 @@ std::string PKCS9SigningTime::print() const { time[0], time[1], time[2], time[3], time[4], time[5]); } - -PKCS9SigningTime::~PKCS9SigningTime() = default; - } } diff --git a/src/PE/signature/attributes/SpcSpOpusInfo.cpp b/src/PE/signature/attributes/SpcSpOpusInfo.cpp index b218ac6082..d6094c5c6b 100644 --- a/src/PE/signature/attributes/SpcSpOpusInfo.cpp +++ b/src/PE/signature/attributes/SpcSpOpusInfo.cpp @@ -18,23 +18,6 @@ namespace LIEF { namespace PE { -SpcSpOpusInfo::SpcSpOpusInfo() : - Attribute(SIG_ATTRIBUTE_TYPES::SPC_SP_OPUS_INFO) -{} - -SpcSpOpusInfo::SpcSpOpusInfo(const SpcSpOpusInfo&) = default; -SpcSpOpusInfo& SpcSpOpusInfo::operator=(const SpcSpOpusInfo&) = default; - -std::unique_ptr SpcSpOpusInfo::clone() const { - return std::unique_ptr(new SpcSpOpusInfo{*this}); -} - -SpcSpOpusInfo::SpcSpOpusInfo(std::string program_name, std::string more_info) : - Attribute(SIG_ATTRIBUTE_TYPES::SPC_SP_OPUS_INFO), - program_name_{std::move(program_name)}, - more_info_{std::move(more_info)} -{} - void SpcSpOpusInfo::accept(Visitor& visitor) const { visitor.visit(*this); } @@ -54,7 +37,5 @@ std::string SpcSpOpusInfo::print() const { } -SpcSpOpusInfo::~SpcSpOpusInfo() = default; - } } diff --git a/tests/pe/test_authenticode.py b/tests/pe/test_authenticode.py index ac1bacdeba..72f24c3182 100644 --- a/tests/pe/test_authenticode.py +++ b/tests/pe/test_authenticode.py @@ -148,30 +148,30 @@ def test_fail(): def test_pkcs9_signing_time(): sig = lief.PE.Signature.parse(get_sample("pkcs7/cert0.p7b")) - attr = sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.PKCS9_SIGNING_TIME) + attr = sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.PKCS9_SIGNING_TIME) assert attr.time == [2018, 8, 2, 15, 0, 12] def test_pkcs9_at_sequence_number(): sig = lief.PE.Signature.parse(get_sample("pkcs7/cert3.p7b")) - nested_sig = sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.MS_SPC_NESTED_SIGN).signature - at_seq_nb = nested_sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.PKCS9_AT_SEQUENCE_NUMBER) + nested_sig = sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.MS_SPC_NESTED_SIGN).signature + at_seq_nb = nested_sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.PKCS9_AT_SEQUENCE_NUMBER) assert at_seq_nb.number == 1 def test_spc_sp_opus_info(): sig = lief.PE.Signature.parse(get_sample("pkcs7/cert11.p7b")) - spc = sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.SPC_SP_OPUS_INFO) + spc = sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.SPC_SP_OPUS_INFO) assert spc.program_name == "Slideshow Generator Powertoy for WinXP" assert spc.more_info == "http://www.microsoft.com/windowsxp" sig = lief.PE.Signature.parse(get_sample("pkcs7/cert9.p7b")) - spc = sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.SPC_SP_OPUS_INFO) + spc = sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.SPC_SP_OPUS_INFO) assert spc.program_name == "Microsoft Windows" assert spc.more_info == "http://www.microsoft.com/windows" def test_pkcs9_counter_signature(): sig = lief.PE.Signature.parse(get_sample("pkcs7/cert10.p7b")) - counter_sign = sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.PKCS9_COUNTER_SIGNATURE) + counter_sign = sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.PKCS9_COUNTER_SIGNATURE) signer = counter_sign.signer @@ -191,7 +191,7 @@ def test_pkcs9_counter_signature(): def test_ms_spc_nested_signature(): sig = lief.PE.Signature.parse(get_sample("pkcs7/cert0.p7b")) - attr = sig.signers[0].get_attribute(lief.PE.SIG_ATTRIBUTE_TYPES.MS_SPC_NESTED_SIGN) + attr = sig.signers[0].get_attribute(lief.PE.Attribute.TYPE.MS_SPC_NESTED_SIGN) nested_sig = attr.signature assert nested_sig.version == 1