LIEF: Library to Instrument Executable Formats Version 0.15.1
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;
37
39class LIEF_API Signature : public Object {
40
41 friend class SignatureParser;
42 friend class Parser;
43 friend class Binary;
44
45 public:
47 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
57
60
63
66
68 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
86 static std::string flag_to_string(VERIFICATION_FLAGS flag);
87
91 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
101
104
106 uint32_t version() const {
107 return version_;
108 }
109
114 return digest_algorithm_;
115 }
116
118 const ContentInfo& content_info() const {
119 return content_info_;
120 }
121
124 return certificates_;
125 }
126
128 return certificates_;
129 }
130
133 return signers_;
134 }
135
137 return signers_;
138 }
139
142 return original_raw_signature_;
143 }
144
146 const x509* find_crt(const std::vector<uint8_t>& serialno) const;
147
149 const x509* find_crt_subject(const std::string& subject) const;
150
152 const x509* find_crt_subject(const std::string& subject, const std::vector<uint8_t>& serialno) const;
153
155 const x509* find_crt_issuer(const std::string& issuer) const;
156
158 const x509* find_crt_issuer(const std::string& issuer, const std::vector<uint8_t>& serialno) const;
159
180 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
206ENABLE_BITMASK_OPERATORS(LIEF::PE::Signature::VERIFICATION_CHECKS)
207
208
209#endif
210
Definition Object.hpp:25
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:52
Definition ContentInfo.hpp:78
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:47
Definition SignatureParser.hpp:37
Main interface for the PKCS #7 signature scheme.
Definition Signature.hpp:39
Signature(Signature &&)
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.
it_signers_t signers()
Definition Signature.hpp:136
const x509 * find_crt(const std::vector< uint8_t > &serialno) const
Find x509 certificate according to its serial number.
static std::vector< uint8_t > hash(const uint8_t *buffer, size_t size, ALGORITHMS algo)
Signature & operator=(Signature &&)
VERIFICATION_FLAGS check(VERIFICATION_CHECKS checks=VERIFICATION_CHECKS::DEFAULT) const
Check if this signature is valid according to the Authenticode/PKCS #7 verification scheme.
span< const uint8_t > raw_der() const
Return the raw original PKCS7 signature.
Definition Signature.hpp:141
static std::string flag_to_string(VERIFICATION_FLAGS flag)
Convert a verification flag into a humman representation. e.g VERIFICATION_FLAGS.BAD_DIGEST | VERIFIC...
const ContentInfo & content_info() const
Return the ContentInfo.
Definition Signature.hpp:118
Signature(const Signature &)
~Signature() override
it_crt certificates()
Definition Signature.hpp:127
Signature & operator=(const Signature &)
const x509 * find_crt_issuer(const std::string &issuer) const
Find x509 certificate according to its issuer.
static std::vector< uint8_t > hash(const std::vector< uint8_t > &input, ALGORITHMS algo)
Hash the input given the algorithm.
Definition Signature.hpp:47
const x509 * find_crt_subject(const std::string &subject) const
Find x509 certificate according to its subject.
VERIFICATION_CHECKS
Flags to tweak the verification process of the signature.
Definition Signature.hpp:91
void accept(Visitor &visitor) const override
it_const_signers_t signers() const
Return an iterator over the signers (SignerInfo) defined in the PKCS #7 signature.
Definition Signature.hpp:132
it_const_crt certificates() const
Return an iterator over x509 certificates.
Definition Signature.hpp:123
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.
uint32_t version() const
Should be 1.
Definition Signature.hpp:106
friend std::ostream & operator<<(std::ostream &os, const Signature &signature)
ALGORITHMS digest_algorithm() const
Algorithm used to digest the file.
Definition Signature.hpp:113
VERIFICATION_FLAGS
Flags returned by the verification functions.
Definition Signature.hpp:68
Interface over a x509 certificate.
Definition x509.hpp:43
Definition Visitor.hpp:224
Iterator which returns reference on container's values.
Definition iterators.hpp:48
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
ALGORITHMS
Cryptography algorithms.
Definition PE/enums.hpp:686
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:41