Botan 3.11.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
20struct CRL_Entry_Data {
21 std::vector<uint8_t> m_serial;
22 X509_Time m_time;
24 Extensions m_extensions;
25};
26
27/*
28* Create a CRL_Entry
29*/
31 m_data = std::make_shared<CRL_Entry_Data>();
32 m_data->m_serial = cert.serial_number();
33 m_data->m_time = X509_Time(std::chrono::system_clock::now());
34 m_data->m_reason = why;
35
36 if(why != CRL_Code::Unspecified) {
37 m_data->m_extensions.add(std::make_unique<Cert_Extension::CRL_ReasonCode>(why));
38 }
39}
40
41/*
42* Compare two CRL_Entry structs for equality
43*/
44bool operator==(const CRL_Entry& a1, const CRL_Entry& a2) {
45 if(a1.serial_number() != a2.serial_number()) {
46 return false;
47 }
48 if(a1.expire_time() != a2.expire_time()) {
49 return false;
50 }
51 if(a1.reason_code() != a2.reason_code()) {
52 return false;
53 }
54 return true;
55}
56
57/*
58* Compare two CRL_Entry structs for inequality
59*/
60bool operator!=(const CRL_Entry& a1, const CRL_Entry& a2) {
61 return !(a1 == a2);
62}
63
64/*
65* DER encode a CRL_Entry
66*/
76
77namespace {
78
79std::vector<uint8_t> decode_serial_number(const BER_Object& obj) {
81
82 if(!obj.data().empty() && obj.data()[0] == 0x00) {
83 return std::vector<uint8_t>(obj.data().begin() + 1, obj.data().end());
84 } else if(!obj.data().empty() && ((obj.data()[0] & 0x80) == 0x80)) {
85 std::vector<uint8_t> vec(obj.data().begin(), obj.data().end());
86 for(size_t i = vec.size(); i > 0; --i) {
87 const bool gt0 = vec[i - 1] > 0;
88 vec[i - 1] -= 1;
89 if(gt0) {
90 break;
91 }
92 }
93 for(auto& b : vec) {
94 b = ~b;
95 }
96
97 return vec;
98 } else {
99 return std::vector<uint8_t>(obj.data().begin(), obj.data().end());
100 }
101}
102
103} // namespace
104
105/*
106* Decode a BER encoded CRL_Entry
107*/
109 auto data = std::make_unique<CRL_Entry_Data>();
110
111 BER_Decoder entry = source.start_sequence();
112
113 data->m_serial = decode_serial_number(entry.get_next_object());
114
115 entry.decode(data->m_time);
116
117 if(entry.more_items()) {
118 entry.decode(data->m_extensions);
119 if(const auto* ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_ReasonCode>()) {
120 data->m_reason = ext->get_reason();
121 } else {
122 data->m_reason = CRL_Code::Unspecified;
123 }
124 }
125
126 entry.end_cons();
127
128 m_data = std::move(data);
129}
130
131const CRL_Entry_Data& CRL_Entry::data() const {
132 if(!m_data) {
133 throw Invalid_State("CRL_Entry_Data uninitialized");
134 }
135
136 return *m_data;
137}
138
139const std::vector<uint8_t>& CRL_Entry::serial_number() const {
140 return data().m_serial;
141}
142
144 return data().m_time;
145}
146
148 return data().m_reason;
149}
150
152 return data().m_extensions;
153}
154
155} // namespace Botan
BER_Object get_next_object()
Definition ber_dec.cpp:261
BER_Decoder & decode(bool &out)
Definition ber_dec.h:188
bool more_items() const
Definition ber_dec.cpp:207
BER_Decoder & end_cons()
Definition ber_dec.cpp:337
BER_Decoder start_sequence()
Definition ber_dec.h:128
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr="object") const
Definition asn1_obj.cpp:34
std::span< const uint8_t > data() const
Definition asn1_obj.h:154
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:83
Definition x509_crl.h:29
CRL_Code reason_code() const
Definition crl_ent.cpp:147
void encode_into(DER_Encoder &to) const override
Definition crl_ent.cpp:67
const X509_Time & expire_time() const
Definition crl_ent.cpp:143
CRL_Entry()=default
const std::vector< uint8_t > & serial_number() const
Definition crl_ent.cpp:139
void decode_from(BER_Decoder &from) override
Definition crl_ent.cpp:108
const Extensions & extensions() const
Definition crl_ent.cpp:151
DER_Encoder & start_sequence()
Definition der_enc.h:67
DER_Encoder & end_cons()
Definition der_enc.cpp:173
DER_Encoder & encode(bool b)
Definition der_enc.cpp:252
const std::vector< uint8_t > & serial_number() const
Definition x509cert.cpp:402
ASN1_Time X509_Time
Definition asn1_obj.h:23
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:68
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:53