LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
x509.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_PE_X509_H
16#define LIEF_PE_X509_H
17#include <array>
18#include <memory>
19#include <vector>
20
21#include "LIEF/Object.hpp"
22#include "LIEF/visibility.h"
23
24#include "LIEF/PE/enums.hpp"
25
27
28#include "LIEF/enums.hpp"
29
30struct mbedtls_x509_crt;
31
32
33namespace LIEF::PE {
34
35class Parser;
36class SignatureParser;
37class Signature;
38
39class RsaInfo;
40
42class LIEF_API x509 : public Object {
43
44 friend class Parser;
45 friend class SignatureParser;
46 friend class Signature;
47
48 public:
50 using date_t = std::array<int32_t, 6>;
51
52 using certificates_t = std::vector<x509>;
53
55 static certificates_t parse(const std::string& path);
56
58 static certificates_t parse(const std::vector<uint8_t>& content);
59
61 static bool check_time(const date_t& before, const date_t& after);
62
64 static bool time_is_past(const date_t& to);
65
67 static bool time_is_future(const date_t& from);
68
70 enum class KEY_TYPES : uint32_t {
71 NONE = 0,
73 ECKEY,
74 ECKEY_DH,
75 ECDSA,
76 RSA_ALT,
78 RSASSA_PSS,
79 };
80
84 enum class VERIFICATION_FLAGS : uint32_t {
86 OK = 0,
87
89 BADCERT_EXPIRED = 1 << 0,
90
92 BADCERT_REVOKED = 1 << 1,
93
95 BADCERT_CN_MISMATCH = 1 << 2,
96
98 BADCERT_NOT_TRUSTED = 1 << 3,
99
101 BADCRL_NOT_TRUSTED = 1 << 4,
102
104 BADCRL_EXPIRED = 1 << 5,
105
107 BADCERT_MISSING = 1 << 6,
108
110 BADCERT_SKIP_VERIFY = 1 << 7,
111
114 BADCERT_OTHER = 1 << 8,
115
117 BADCERT_FUTURE = 1 << 9,
118
120 BADCRL_FUTURE = 1 << 10,
121
123 BADCERT_KEY_USAGE = 1 << 11,
124
126 BADCERT_EXT_KEY_USAGE = 1 << 12,
127
129 BADCERT_NS_CERT_TYPE = 1 << 13,
130
132 BADCERT_BAD_MD = 1 << 14,
133
135 BADCERT_BAD_PK = 1 << 15,
136
139 BADCERT_BAD_KEY = 1 << 16,
140
142 BADCRL_BAD_MD = 1 << 17,
143
145 BADCRL_BAD_PK = 1 << 18,
146
148 BADCRL_BAD_KEY = 1 << 19,
149 };
150
153 enum class KEY_USAGE : uint32_t {
155 DIGITAL_SIGNATURE = 0,
158 NON_REPUDIATION,
159
161 KEY_ENCIPHERMENT,
162
165 DATA_ENCIPHERMENT,
166
168 KEY_AGREEMENT,
169
171 KEY_CERT_SIGN,
172
174 CRL_SIGN,
175
178 ENCIPHER_ONLY,
179
182 DECIPHER_ONLY,
183 };
184
185 x509(mbedtls_x509_crt* ca);
186 x509(const x509& other);
188 void swap(x509& other);
189
191 uint32_t version() const;
192
194 std::vector<uint8_t> serial_number() const;
195
198
201
204
206 std::string issuer() const;
207
209 std::string subject() const;
210
213 bool check_signature(const std::vector<uint8_t>& hash,
214 const std::vector<uint8_t>& signature,
215 ALGORITHMS digest) const;
216
218 std::vector<uint8_t> raw() const;
219
222
225 std::unique_ptr<RsaInfo> rsa_info() const;
226
229
231 VERIFICATION_FLAGS is_trusted_by(const std::vector<x509>& ca) const;
232
234 std::vector<oid_t> certificate_policies() const;
235
237 std::vector<KEY_USAGE> key_usage() const;
238
241 std::vector<oid_t> ext_key_usage() const;
242
243 bool is_ca() const;
244
246 std::vector<uint8_t> signature() const;
247
248 void accept(Visitor& visitor) const override;
249
250 ~x509() override;
251
252 LIEF_API friend std::ostream& operator<<(std::ostream& os,
253 const x509& x509_cert);
254
255 private:
256 x509();
257 mbedtls_x509_crt* x509_cert_ = nullptr;
258};
259
260}
261
262
264
265#endif
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:53
Object that wraps a RSA key.
Definition RsaInfo.hpp:28
Definition SignatureParser.hpp:37
Main interface for the PKCS #7 signature scheme.
Definition Signature.hpp:41
std::string issuer() const
Issuer information.
void swap(x509 &other)
date_t valid_from() const
Start time of certificate validity.
KEY_USAGE
Key usage as defined in RFC #5280 - section-4.2.1.3.
Definition x509.hpp:153
std::vector< uint8_t > serial_number() const
Unique id for certificate issued by a specific CA.
std::vector< uint8_t > raw() const
The raw x509 bytes (DER encoded).
static certificates_t parse(const std::string &path)
Parse x509 certificate(s) from file path.
std::vector< uint8_t > signature() const
The signature of the certificate.
VERIFICATION_FLAGS verify(const x509 &ca) const
Verify that this certificate has been used to trust the given certificate.
friend class Signature
Definition x509.hpp:46
std::unique_ptr< RsaInfo > rsa_info() const
If the underlying public-key scheme is RSA, return the RSA information. Otherwise,...
void accept(Visitor &visitor) const override
x509 & operator=(x509 other)
std::string subject() const
Subject information.
std::vector< KEY_USAGE > key_usage() const
Purpose of the key contained in the certificate.
static bool time_is_past(const date_t &to)
True if the given time is in the past according to the clock's system.
static bool check_time(const date_t &before, const date_t &after)
Return True if before is before than after. False otherwise.
std::vector< x509 > certificates_t
Definition x509.hpp:52
oid_t signature_algorithm() const
Signature algorithm (OID).
bool is_ca() const
KEY_TYPES key_type() const
Return the underlying public-key scheme.
std::array< int32_t, 6 > date_t
Tuple (Year, Month, Day, Hour, Minute, Second).
Definition x509.hpp:50
~x509() override
friend class SignatureParser
Definition x509.hpp:45
x509(mbedtls_x509_crt *ca)
uint32_t version() const
X.509 version. (1=v1, 2=v2, 3=v3).
friend class Parser
Definition x509.hpp:44
bool check_signature(const std::vector< uint8_t > &hash, const std::vector< uint8_t > &signature, ALGORITHMS digest) const
Try to decrypt the given signature and check if it matches the given hash according to the hash algor...
VERIFICATION_FLAGS is_trusted_by(const std::vector< x509 > &ca) const
Verify that this certificate is trusted by the given CA list.
VERIFICATION_FLAGS
Mirror of mbedtls's X509 Verify codes: MBEDTLS_X509_XX.
Definition x509.hpp:84
x509(const x509 &other)
KEY_TYPES
Public key scheme.
Definition x509.hpp:70
static bool time_is_future(const date_t &from)
True if the given time is in the future according to the clock's system.
std::vector< oid_t > ext_key_usage() const
Indicates one or more purposes for which the certified public key may be used (OID types).
static certificates_t parse(const std::vector< uint8_t > &content)
Parse x509 certificate(s) from raw blob.
std::vector< oid_t > certificate_policies() const
Policy information terms as OID (see RFC #5280).
date_t valid_to() const
End time of certificate validity.
friend std::ostream & operator<<(std::ostream &os, const x509 &x509_cert)
Definition Visitor.hpp:211
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:23
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:31
ALGORITHMS
Cryptography algorithms.
Definition PE/enums.hpp:28
@ RSA
Definition PE/enums.hpp:39
std::string oid_t
Definition PE/signature/types.hpp:22
Hash::value_type hash(const Object &v)
#define LIEF_API
Definition visibility.h:44