LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Signature.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_PE_SIGNATURE_H
17#define LIEF_PE_SIGNATURE_H
18
19#include "LIEF/Object.hpp"
20#include "LIEF/visibility.h"
21#include "LIEF/span.hpp"
22
26
27#include "LIEF/PE/enums.hpp"
28
29#include "LIEF/iterators.hpp"
30#include "LIEF/enums.hpp"
31
32namespace LIEF {
33namespace PE {
34
35class SignatureParser;
36class Binary;
37class LIEF_API Signature : public Object {
40
41 friend class SignatureParser;
42 friend class Parser;
43 friend class Binary;
44
45 public: static std::vector<uint8_t> hash(const std::vector<uint8_t>& input, ALGORITHMS algo) {
48 return hash(input.data(), input.size(), algo);
49 }
50
51 static std::vector<uint8_t> hash(const uint8_t* buffer, size_t size, ALGORITHMS algo);
52
53 public:
54 using it_const_crt = const_ref_iterator<const std::vector<x509>&>;
57 using it_crt = ref_iterator<std::vector<x509>&>;
60 using it_const_signers_t = const_ref_iterator<const std::vector<SignerInfo>&>;
63 using it_signers_t = ref_iterator<std::vector<SignerInfo>&>;
66 enum class VERIFICATION_FLAGS : uint32_t {
69 OK = 0,
70 INVALID_SIGNER = 1 << 0,
71 UNSUPPORTED_ALGORITHM = 1 << 1,
72 INCONSISTENT_DIGEST_ALGORITHM = 1 << 2,
73 CERT_NOT_FOUND = 1 << 3,
74 CORRUPTED_CONTENT_INFO = 1 << 4,
75 CORRUPTED_AUTH_DATA = 1 << 5,
76 MISSING_PKCS9_MESSAGE_DIGEST = 1 << 6,
77 BAD_DIGEST = 1 << 7,
78 BAD_SIGNATURE = 1 << 8,
79 NO_SIGNATURE = 1 << 9,
80 CERT_EXPIRED = 1 << 10,
81 CERT_FUTURE = 1 << 11,
82 };
83 static std::string flag_to_string(VERIFICATION_FLAGS flag);
87 enum class VERIFICATION_CHECKS : uint32_t {
92 DEFAULT = 1 << 0,
93 HASH_ONLY = 1 << 1,
94 LIFETIME_SIGNING = 1 << 2,
95 SKIP_CERT_TIME = 1 << 3,
96 };
97
98 Signature();
99 Signature(const Signature&);
100 Signature& operator=(const Signature&);
101
102 Signature(Signature&&);
103 Signature& operator=(Signature&&);
104 uint32_t version() const {
107 return version_;
108 }
109 ALGORITHMS digest_algorithm() const {
114 return digest_algorithm_;
115 }
116 const ContentInfo& content_info() const {
119 return content_info_;
120 }
121 it_const_crt certificates() const {
124 return certificates_;
125 }
126
127 it_crt certificates() {
128 return certificates_;
129 }
130 it_const_signers_t signers() const {
133 return signers_;
134 }
135
136 it_signers_t signers() {
137 return signers_;
138 }
139 span<const uint8_t> raw_der() const {
142 return original_raw_signature_;
143 }
144 const x509* find_crt(const std::vector<uint8_t>& serialno) const;
147 const x509* find_crt_subject(const std::string& subject) const;
150 const x509* find_crt_subject(const std::string& subject, const std::vector<uint8_t>& serialno) const;
153 const x509* find_crt_issuer(const std::string& issuer) const;
156 const x509* find_crt_issuer(const std::string& issuer, const std::vector<uint8_t>& serialno) const;
159 VERIFICATION_FLAGS check(VERIFICATION_CHECKS checks = VERIFICATION_CHECKS::DEFAULT) const;
181
182 void accept(Visitor& visitor) const override;
183
184 ~Signature() override;
185
186 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Signature& signature);
187
188 private:
189 uint32_t version_ = 0;
190 ALGORITHMS digest_algorithm_ = ALGORITHMS::UNKNOWN;
191 ContentInfo content_info_;
192 std::vector<x509> certificates_;
193 std::vector<SignerInfo> signers_;
194
195 uint64_t content_info_start_ = 0;
196 uint64_t content_info_end_ = 0;
197
198 std::vector<uint8_t> original_raw_signature_;
199};
200
201
202}
203}
204
205ENABLE_BITMASK_OPERATORS(LIEF::PE::Signature::VERIFICATION_FLAGS)
206ENABLE_BITMASK_OPERATORS(LIEF::PE::Signature::VERIFICATION_CHECKS)
207
208
209#endif
210
ContentInfo.hpp
Object.hpp
enums.hpp
SignerInfo.hpp
LIEF::PE::Binary
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:52
LIEF::PE::SignatureParser
Definition SignatureParser.hpp:37
LIEF::PE::Signature
Main interface for the PKCS #7 signature scheme.
Definition Signature.hpp:39
LIEF::PE::Signature::Signature
Signature(Signature &&)
LIEF::PE::Signature::find_crt_issuer
const x509 * find_crt_issuer(const std::string &issuer, const std::vector< uint8_t > &serialno) const
Find x509 certificate according to its issuer AND serial number.
LIEF::PE::Signature::signers
it_signers_t signers()
Definition Signature.hpp:136
LIEF::PE::Signature::find_crt
const x509 * find_crt(const std::vector< uint8_t > &serialno) const
Find x509 certificate according to its serial number.
LIEF::PE::Signature::hash
static std::vector< uint8_t > hash(const uint8_t *buffer, size_t size, ALGORITHMS algo)
LIEF::PE::Signature::operator=
Signature & operator=(Signature &&)
LIEF::PE::Signature::check
VERIFICATION_FLAGS check(VERIFICATION_CHECKS checks=VERIFICATION_CHECKS::DEFAULT) const
Check if this signature is valid according to the Authenticode/PKCS #7 verification scheme.
LIEF::PE::Signature::raw_der
span< const uint8_t > raw_der() const
Return the raw original PKCS7 signature.
Definition Signature.hpp:141
LIEF::PE::Signature::flag_to_string
static std::string flag_to_string(VERIFICATION_FLAGS flag)
Convert a verification flag into a humman representation. e.g VERIFICATION_FLAGS.BAD_DIGEST | VERIFIC...
LIEF::PE::Signature::content_info
const ContentInfo & content_info() const
Return the ContentInfo.
Definition Signature.hpp:118
LIEF::PE::Signature::Signature
Signature(const Signature &)
LIEF::PE::Signature::~Signature
~Signature() override
LIEF::PE::Signature::certificates
it_crt certificates()
Definition Signature.hpp:127
LIEF::PE::Signature::operator=
Signature & operator=(const Signature &)
LIEF::PE::Signature::find_crt_issuer
const x509 * find_crt_issuer(const std::string &issuer) const
Find x509 certificate according to its issuer.
LIEF::PE::Signature::hash
static std::vector< uint8_t > hash(const std::vector< uint8_t > &input, ALGORITHMS algo)
Hash the input given the algorithm.
Definition Signature.hpp:47
LIEF::PE::Signature::Signature
Signature()
LIEF::PE::Signature::find_crt_subject
const x509 * find_crt_subject(const std::string &subject) const
Find x509 certificate according to its subject.
LIEF::PE::Signature::accept
void accept(Visitor &visitor) const override
LIEF::PE::Signature::signers
it_const_signers_t signers() const
Return an iterator over the signers (SignerInfo) defined in the PKCS #7 signature.
Definition Signature.hpp:132
LIEF::PE::Signature::certificates
it_const_crt certificates() const
Return an iterator over x509 certificates.
Definition Signature.hpp:123
LIEF::PE::Signature::find_crt_subject
const x509 * find_crt_subject(const std::string &subject, const std::vector< uint8_t > &serialno) const
Find x509 certificate according to its subject AND serial number.
LIEF::PE::Signature::version
uint32_t version() const
Should be 1.
Definition Signature.hpp:106
LIEF::PE::Signature::operator<<
friend std::ostream & operator<<(std::ostream &os, const Signature &signature)
LIEF::PE::Signature::digest_algorithm
ALGORITHMS digest_algorithm() const
Algorithm used to digest the file.
Definition Signature.hpp:113
LIEF::PE::Signature::VERIFICATION_FLAGS
VERIFICATION_FLAGS
Flags returned by the verification functions.
Definition Signature.hpp:68
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
iterators.hpp
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::ALGORITHMS
ALGORITHMS
Cryptography algorithms.
Definition PE/enums.hpp:686
LIEF::PE::IMPHASH_MODE::DEFAULT
@ DEFAULT
Definition PE/utils.hpp:34
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
LIEF::hash
Hash::value_type hash(const Object &v)
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
x509.hpp