Skip to content

Commit

Permalink
Narrow the scope of SIG_ATTRIBUTE_TYPES to LIEF::PE::Attribute::TYPE
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Dec 9, 2023
1 parent 3aa5497 commit 8dd8a25
Show file tree
Hide file tree
Showing 33 changed files with 295 additions and 460 deletions.
45 changes: 22 additions & 23 deletions api/python/lief/PE.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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] = ...
Expand Down Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions api/python/src/PE/enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_<SIG_ATTRIBUTE_TYPES>(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));

}
}
28 changes: 24 additions & 4 deletions api/python/src/PE/objects/signature/pyAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,36 @@

#include <string>
#include <sstream>
#include "enums_wrapper.hpp"
#include <nanobind/stl/string.h>

namespace LIEF::PE::py {

template<>
void create<Attribute>(nb::module_& m) {
nb::class_<Attribute, Object>(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_<Attribute, Object> Class(m,
"Attribute", "Interface over PKCS #7 attribute"_doc
);

#define ENTRY(X) .value(to_string(Attribute::TYPE::X), Attribute::TYPE::X)
enum_<Attribute::TYPE>(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);
}
Expand Down
3 changes: 0 additions & 3 deletions doc/sphinx/api/cpp/pe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,3 @@ Enums

.. doxygenenum:: LIEF::PE::ALGORITHMS
:project: lief

.. doxygenenum:: LIEF::PE::SIG_ATTRIBUTE_TYPES
:project: lief
6 changes: 0 additions & 6 deletions doc/sphinx/api/python/pe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,3 @@ ALGORITHMS
~~~~~~~~~~

.. autoclass:: lief.PE.ALGORITHMS


SIG_ATTRIBUTE_TYPES
~~~~~~~~~~~~~~~~~~~

.. autoclass:: lief.PE.SIG_ATTRIBUTE_TYPES
2 changes: 0 additions & 2 deletions include/LIEF/PE/EnumToString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions include/LIEF/PE/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 28 additions & 10 deletions include/LIEF/PE/signature/Attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,38 @@
namespace LIEF {
namespace PE {

class Parser;
class SignatureParser;

//! Interface over PKCS #7 attribute
class LIEF_API Attribute : public Object {

friend class Parser;
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<Attribute> clone() const = 0;

//! Concrete type of the attribute
virtual SIG_ATTRIBUTE_TYPES type() const {
virtual TYPE type() const {
return type_;
}

Expand All @@ -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);

}
}

Expand Down
31 changes: 22 additions & 9 deletions include/LIEF/PE/signature/SignerInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
24 changes: 14 additions & 10 deletions include/LIEF/PE/signature/attributes/ContentType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -56,14 +58,16 @@ class LIEF_API ContentType : public Attribute {
//! Print information about the attribute
std::string print() const override;

std::unique_ptr<Attribute> clone() const override;
std::unique_ptr<Attribute> clone() const override {
return std::unique_ptr<Attribute>(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_;
Expand Down
Loading

0 comments on commit 8dd8a25

Please sign in to comment.