Botan 3.4.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 X509_Time m_this_update;
21 X509_Time m_next_update;
22 std::vector<CRL_Entry> m_entries;
23 Extensions m_extensions;
24
25 // cached values from extensions
26 size_t m_crl_number = 0;
27 std::vector<uint8_t> m_auth_key_id;
28 std::string m_issuing_distribution_point;
29};
30
31std::string X509_CRL::PEM_label() const {
32 return "X509 CRL";
33}
34
35std::vector<std::string> X509_CRL::alternate_PEM_labels() const {
36 return {"CRL"};
37}
38
42
43X509_CRL::X509_CRL(const std::vector<uint8_t>& vec) {
44 DataSource_Memory src(vec.data(), vec.size());
45 load_data(src);
46}
47
48#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
49X509_CRL::X509_CRL(std::string_view fsname) {
50 DataSource_Stream src(fsname, true);
51 load_data(src);
52}
53#endif
54
56 const X509_Time& this_update,
57 const X509_Time& next_update,
58 const std::vector<CRL_Entry>& revoked) :
59 X509_Object() {
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 size_t version;
117 tbs_crl.decode_optional(version, ASN1_Type::Integer, ASN1_Class::Universal);
118
119 if(version != 0 && version != 1) {
120 throw Decoding_Error("Unknown X.509 CRL version " + std::to_string(version + 1));
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).decode(data->m_next_update);
131
132 BER_Object next = tbs_crl.get_next_object();
133
135 BER_Decoder cert_list(std::move(next));
136
137 while(cert_list.more_items()) {
138 CRL_Entry entry;
139 cert_list.decode(entry);
140 data->m_entries.push_back(entry);
141 }
142 next = tbs_crl.get_next_object();
143 }
144
146 BER_Decoder crl_options(std::move(next));
147 crl_options.decode(data->m_extensions).verify_end();
148 next = tbs_crl.get_next_object();
149 }
150
151 if(next.is_set()) {
152 throw Decoding_Error("Unknown tag following extensions in CRL");
153 }
154
155 tbs_crl.verify_end();
156
157 // Now cache some fields from the extensions
158 if(auto ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_Number>()) {
159 data->m_crl_number = ext->get_crl_number();
160 }
161 if(auto ext = data->m_extensions.get_extension_object_as<Cert_Extension::Authority_Key_ID>()) {
162 data->m_auth_key_id = ext->get_key_id();
163 }
164 if(auto ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_Issuing_Distribution_Point>()) {
165 std::stringstream ss;
166
167 for(const auto& pair : ext->get_point().contents()) {
168 ss << pair.first << ": " << pair.second << " ";
169 }
170 data->m_issuing_distribution_point = ss.str();
171 }
172
173 return data;
174}
175
176} // namespace
177
178void X509_CRL::force_decode() {
179 m_data.reset(decode_crl_body(signed_body(), signature_algorithm()).release());
180}
181
182const CRL_Data& X509_CRL::data() const {
183 if(!m_data) {
184 throw Invalid_State("X509_CRL uninitialized");
185 }
186 return *m_data;
187}
188
190 return data().m_extensions;
191}
192
193/*
194* Return the list of revoked certificates
195*/
196const std::vector<CRL_Entry>& X509_CRL::get_revoked() const {
197 return data().m_entries;
198}
199
200/*
201* Return the distinguished name of the issuer
202*/
204 return data().m_issuer;
205}
206
207/*
208* Return the key identifier of the issuer
209*/
210const std::vector<uint8_t>& X509_CRL::authority_key_id() const {
211 return data().m_auth_key_id;
212}
213
214/*
215* Return the CRL number of this CRL
216*/
217uint32_t X509_CRL::crl_number() const {
218 return static_cast<uint32_t>(data().m_crl_number);
219}
220
221/*
222* Return the issue data of the CRL
223*/
225 return data().m_this_update;
226}
227
228/*
229* Return the date when a new CRL will be issued
230*/
232 return data().m_next_update;
233}
234
235/*
236* Return the CRL's distribution point
237*/
239 return data().m_issuing_distribution_point;
240}
241} // namespace Botan
Definition x509_crl.h:28
const std::vector< CRL_Entry > & get_revoked() const
Definition x509_crl.cpp:196
const std::vector< uint8_t > & authority_key_id() const
Definition x509_crl.cpp:210
const X509_Time & this_update() const
Definition x509_crl.cpp:224
X509_CRL()=default
const Extensions & extensions() const
Definition x509_crl.cpp:189
uint32_t crl_number() const
Definition x509_crl.cpp:217
const X509_Time & next_update() const
Definition x509_crl.cpp:231
const X509_DN & issuer_dn() const
Definition x509_crl.cpp:203
bool is_revoked(const X509_Certificate &cert) const
Definition x509_crl.cpp:70
std::string crl_issuing_distribution_point() const
Definition x509_crl.cpp:238
const std::vector< uint8_t > & serial_number() const
Definition x509cert.cpp:350
const std::vector< uint8_t > & authority_key_id() const
Definition x509cert.cpp:342
const X509_DN & issuer_dn() const
Definition x509cert.cpp:358
const std::vector< uint8_t > & signed_body() const
Definition x509_obj.h:42
const AlgorithmIdentifier & signature_algorithm() const
Definition x509_obj.h:47
void load_data(DataSource &src)
Definition x509_obj.cpp:24
ASN1_Time X509_Time
Definition asn1_obj.h:402