Botan 3.13.0
Crypto and TLS for C&
crl_ent.cpp
Go to the documentation of this file.
1/*
2* CRL Entry
3* (C) 1999-2010 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/x509_crl.h>
9
10#include <botan/asn1_obj.h>
11#include <botan/asn1_time.h>
12#include <botan/ber_dec.h>
13#include <botan/bigint.h>
14#include <botan/der_enc.h>
15#include <botan/x509_ext.h>
16#include <botan/x509cert.h>
17
18namespace Botan {
19
20class CRL_Entry_Data final {
21 public:
22 CRL_Entry_Data(const X509_Certificate& cert, CRL_Code why) :
23 m_serial(cert.serial()),
24 m_serial_bits(cert.serial_number()),
25 m_time(X509_Time(std::chrono::system_clock::now())),
26 m_reason(why) {
27 if(why != CRL_Code::Unspecified) {
28 m_extensions.add(std::make_unique<Cert_Extension::CRL_ReasonCode>(why));
29 }
30 }
31
32 CRL_Entry_Data() = default;
33
34 // NOLINTBEGIN(*non-private-member-variables-in-classes)
35 X509_Serial_Number m_serial;
36 std::vector<uint8_t> m_serial_bits;
37 X509_Time m_time;
39 Extensions m_extensions;
40 // NOLINTEND(*non-private-member-variables-in-classes)
41};
42
43/*
44* Create a CRL_Entry
45*/
47 m_data = std::make_shared<CRL_Entry_Data>(cert, why);
48}
49
50/*
51* Compare two CRL_Entry structs for equality
52*/
53bool operator==(const CRL_Entry& a1, const CRL_Entry& a2) {
54 if(a1.serial() != a2.serial()) {
55 return false;
56 }
57 if(a1.expire_time() != a2.expire_time()) {
58 return false;
59 }
60 if(a1.reason_code() != a2.reason_code()) {
61 return false;
62 }
63 return true;
64}
65
66/*
67* Compare two CRL_Entry structs for inequality
68*/
69bool operator!=(const CRL_Entry& a1, const CRL_Entry& a2) {
70 return !(a1 == a2);
71}
72
73/*
74* DER encode a CRL_Entry
75*/
78
79 if(extensions().count() > 0) {
81 }
82
83 der.end_cons();
84}
85
86/*
87* Decode a BER encoded CRL_Entry
88*/
90 auto data = std::make_unique<CRL_Entry_Data>();
91
92 BER_Decoder entry = source.start_sequence();
93
94 entry.decode(data->m_serial);
95 entry.decode(data->m_time);
96
97 data->m_serial_bits = data->m_serial.magnitude();
98
99 if(entry.more_items()) {
100 data->m_extensions.decode_from(entry, Extension_Context::CRL_Entry);
101
102 // TODO(Botan4) start checking that CRLEntry extensions is not empty
103 // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
104
105 if(const auto* ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_ReasonCode>()) {
106 const auto reason = ext->get_reason();
107
108 /*
109 * This reason code only makes sense in the context of delta CRL processing, which
110 * we do not currently support. Seeing it in a regular CRL suggests that something
111 * has gone very wrong (eg we are somehow processing a delta CRL, though that
112 * shouldn't happen since the delta CRL indicator extension should be a critical
113 * extension which we will reject.)
114 */
115 if(reason == CRL_Code::RemoveFromCrl) {
116 throw Decoding_Error("CRL entry ReasonCode extension included invalid RemoveFromCrl");
117 }
118 data->m_reason = reason;
119 } else {
120 data->m_reason = CRL_Code::Unspecified;
121 }
122 }
123
124 entry.end_cons();
125
126 m_data = std::move(data);
127}
128
129const CRL_Entry_Data& CRL_Entry::data() const {
130 if(!m_data) {
131 throw Invalid_State("CRL_Entry_Data uninitialized");
132 }
133
134 return *m_data;
135}
136
137const std::vector<uint8_t>& CRL_Entry::serial_number() const {
138 return data().m_serial_bits;
139}
140
142 return data().m_serial;
143}
144
146 return data().m_time;
147}
148
150 return data().m_reason;
151}
152
154 return data().m_extensions;
155}
156
157} // namespace Botan
BER_Decoder & decode(bool &out)
Definition ber_dec.h:358
bool more_items() const
Definition ber_dec.cpp:461
BER_Decoder & end_cons()
Definition ber_dec.cpp:630
BER_Decoder start_sequence()
Definition ber_dec.h:275
Definition x509_crl.h:32
CRL_Code reason_code() const
Definition crl_ent.cpp:149
void encode_into(DER_Encoder &to) const override
Definition crl_ent.cpp:76
const X509_Serial_Number & serial() const
Definition crl_ent.cpp:141
const X509_Time & expire_time() const
Definition crl_ent.cpp:145
CRL_Entry()=default
const std::vector< uint8_t > & serial_number() const
Definition crl_ent.cpp:137
void decode_from(BER_Decoder &from) override
Definition crl_ent.cpp:89
const Extensions & extensions() const
Definition crl_ent.cpp:153
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
ASN1_Time X509_Time
Definition asn1_obj.h:27
bool operator!=(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:58
bool operator==(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:54