Botan 3.9.0
Crypto and TLS for C&
x509_crl.cpp
Go to the documentation of this file.
1/*
2* X.509 CRL
3* (C) 1999-2007 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/ber_dec.h>
11#include <botan/x509_ext.h>
12#include <botan/x509cert.h>
13
14#include <sstream>
15
16namespace Botan {
17
18struct CRL_Data {
19 X509_DN m_issuer;
20 size_t m_version{};
21 X509_Time m_this_update;
22 X509_Time m_next_update;
23 std::vector<CRL_Entry> m_entries;
24 Extensions m_extensions;
25
26 // cached values from extensions
27 size_t m_crl_number = 0;
28 std::vector<uint8_t> m_auth_key_id;
29 std::vector<std::string> m_idp_urls;
30};
31
32std::string X509_CRL::PEM_label() const {
33 return "X509 CRL";
34}
35
36std::vector<std::string> X509_CRL::alternate_PEM_labels() const {
37 return {"CRL"};
38}
39
43
44X509_CRL::X509_CRL(const std::vector<uint8_t>& vec) {
45 DataSource_Memory src(vec.data(), vec.size());
46 load_data(src);
47}
48
49#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
50X509_CRL::X509_CRL(std::string_view fsname) {
51 DataSource_Stream src(fsname, true);
52 load_data(src);
53}
54#endif
55
59 const std::vector<CRL_Entry>& revoked) {
60 m_data = std::make_shared<CRL_Data>();
61 m_data->m_issuer = issuer;
62 m_data->m_this_update = this_update;
63 m_data->m_next_update = next_update;
64 m_data->m_entries = revoked;
65}
66
67/**
68* Check if this particular certificate is listed in the CRL
69*/
70bool X509_CRL::is_revoked(const X509_Certificate& cert) const {
71 /*
72 If the cert wasn't issued by the CRL issuer, it's possible the cert
73 is revoked, but not by this CRL. Maybe throw an exception instead?
74 */
75 if(cert.issuer_dn() != issuer_dn()) {
76 return false;
77 }
78
79 std::vector<uint8_t> crl_akid = authority_key_id();
80 const std::vector<uint8_t>& cert_akid = cert.authority_key_id();
81
82 if(!crl_akid.empty() && !cert_akid.empty()) {
83 if(crl_akid != cert_akid) {
84 return false;
85 }
86 }
87
88 const std::vector<uint8_t>& cert_serial = cert.serial_number();
89
90 bool is_revoked = false;
91
92 // FIXME would be nice to avoid a linear scan here - maybe sort the entries?
93 for(const CRL_Entry& entry : get_revoked()) {
94 if(cert_serial == entry.serial_number()) {
95 if(entry.reason_code() == CRL_Code::RemoveFromCrl) {
96 is_revoked = false;
97 } else {
98 is_revoked = true;
99 }
100 }
101 }
102
103 return is_revoked;
104}
105
106/*
107* Decode the TBSCertList data
108*/
109namespace {
110
111std::unique_ptr<CRL_Data> decode_crl_body(const std::vector<uint8_t>& body, const AlgorithmIdentifier& sig_algo) {
112 auto data = std::make_unique<CRL_Data>();
113
114 BER_Decoder tbs_crl(body);
115
116 tbs_crl.decode_optional(data->m_version, ASN1_Type::Integer, ASN1_Class::Universal);
117 data->m_version += 1; // wire-format is 0-based
118
119 if(data->m_version != 1 && data->m_version != 2) {
120 throw Decoding_Error("Unknown X.509 CRL version " + std::to_string(data->m_version));
121 }
122
123 AlgorithmIdentifier sig_algo_inner;
124 tbs_crl.decode(sig_algo_inner);
125
126 if(sig_algo != sig_algo_inner) {
127 throw Decoding_Error("Algorithm identifier mismatch in CRL");
128 }
129
130 tbs_crl.decode(data->m_issuer).decode(data->m_this_update);
131
132 // According to RFC 5280 Section 5.1, nextUpdate is OPTIONAL and may be
133 // encoded as either a UTCTime or a GeneralizedTime. Section 5.1.2.5
134 // further states that "[c]onforming CRL issuers MUST include the nextUpdate
135 // field in all CRLs". Obviously, not everyone complies...
136 //
137 // See https://github.com/randombit/botan/issues/4722 for more details.
138 tbs_crl.decode_optional(data->m_next_update, ASN1_Type::UtcTime, ASN1_Class::Universal);
139 if(!data->m_next_update.time_is_set()) {
140 tbs_crl.decode_optional(data->m_next_update, ASN1_Type::GeneralizedTime, ASN1_Class::Universal);
141 }
142
143 BER_Object next = tbs_crl.get_next_object();
144
146 BER_Decoder cert_list(std::move(next));
147
148 while(cert_list.more_items()) {
149 CRL_Entry entry;
150 cert_list.decode(entry);
151 data->m_entries.push_back(entry);
152 }
153 next = tbs_crl.get_next_object();
154 }
155
157 BER_Decoder crl_options(std::move(next));
158 crl_options.decode(data->m_extensions).verify_end();
159 next = tbs_crl.get_next_object();
160 }
161
162 if(next.is_set()) {
163 throw Decoding_Error("Unknown tag following extensions in CRL");
164 }
165
166 tbs_crl.verify_end();
167
168 // Now cache some fields from the extensions
169 if(const auto* ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_Number>()) {
170 data->m_crl_number = ext->get_crl_number();
171 }
172 if(const auto* ext = data->m_extensions.get_extension_object_as<Cert_Extension::Authority_Key_ID>()) {
173 data->m_auth_key_id = ext->get_key_id();
174 }
175 if(const auto* ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_Issuing_Distribution_Point>()) {
176 data->m_idp_urls = ext->get_point().get_attribute("URL");
177 }
178
179 return data;
180}
181
182} // namespace
183
184void X509_CRL::force_decode() {
185 m_data.reset(decode_crl_body(signed_body(), signature_algorithm()).release());
186}
187
188const CRL_Data& X509_CRL::data() const {
189 if(!m_data) {
190 throw Invalid_State("X509_CRL uninitialized");
191 }
192 return *m_data;
193}
194
196 return data().m_extensions;
197}
198
199/*
200* Return the list of revoked certificates
201*/
202const std::vector<CRL_Entry>& X509_CRL::get_revoked() const {
203 return data().m_entries;
204}
205
206uint32_t X509_CRL::x509_version() const {
207 return static_cast<uint32_t>(data().m_version);
208}
209
210/*
211* Return the distinguished name of the issuer
212*/
214 return data().m_issuer;
215}
216
217/*
218* Return the key identifier of the issuer
219*/
220const std::vector<uint8_t>& X509_CRL::authority_key_id() const {
221 return data().m_auth_key_id;
222}
223
224/*
225* Return the CRL number of this CRL
226*/
227uint32_t X509_CRL::crl_number() const {
228 return static_cast<uint32_t>(data().m_crl_number);
229}
230
231/*
232* Return the issue data of the CRL
233*/
235 return data().m_this_update;
236}
237
238/*
239* Return the date when a new CRL will be issued
240*/
242 return data().m_next_update;
243}
244
245/*
246* Return the CRL's distribution point
247*/
249 if(!data().m_idp_urls.empty()) {
250 return data().m_idp_urls[0];
251 }
252 return "";
253}
254
255/*
256* Return the CRL's issuing distribution point
257*/
258std::vector<std::string> X509_CRL::issuing_distribution_points() const {
259 return data().m_idp_urls;
260}
261
262} // namespace Botan
Definition x509_crl.h:29
const std::vector< CRL_Entry > & get_revoked() const
Definition x509_crl.cpp:202
const std::vector< uint8_t > & authority_key_id() const
Definition x509_crl.cpp:220
const X509_Time & this_update() const
Definition x509_crl.cpp:234
std::vector< std::string > issuing_distribution_points() const
Definition x509_crl.cpp:258
X509_CRL()=default
const Extensions & extensions() const
Definition x509_crl.cpp:195
uint32_t crl_number() const
Definition x509_crl.cpp:227
const X509_Time & next_update() const
Definition x509_crl.cpp:241
const X509_DN & issuer_dn() const
Definition x509_crl.cpp:213
bool is_revoked(const X509_Certificate &cert) const
Definition x509_crl.cpp:70
std::string crl_issuing_distribution_point() const
Definition x509_crl.cpp:248
uint32_t x509_version() const
Definition x509_crl.cpp:206
const std::vector< uint8_t > & serial_number() const
Definition x509cert.cpp:399
const std::vector< uint8_t > & authority_key_id() const
Definition x509cert.cpp:391
const X509_DN & issuer_dn() const
Definition x509cert.cpp:407
const std::vector< uint8_t > & signed_body() const
Definition x509_obj.h:45
const AlgorithmIdentifier & signature_algorithm() const
Definition x509_obj.h:50
virtual std::vector< std::string > alternate_PEM_labels() const
Definition x509_obj.h:101
void load_data(DataSource &src)
Definition x509_obj.cpp:24
virtual std::string PEM_label() const =0
ASN1_Time X509_Time
Definition asn1_obj.h:424