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