Botan 3.0.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#include <botan/x509cert.h>
10#include <botan/x509_ext.h>
11#include <botan/der_enc.h>
12#include <botan/ber_dec.h>
13#include <botan/bigint.h>
14
15namespace Botan {
16
17struct CRL_Entry_Data
18 {
19 std::vector<uint8_t> m_serial;
20 X509_Time m_time;
22 Extensions m_extensions;
23 };
24
25/*
26* Create a CRL_Entry
27*/
29 {
30 m_data = std::make_shared<CRL_Entry_Data>();
31 m_data->m_serial = cert.serial_number();
32 m_data->m_time = X509_Time(std::chrono::system_clock::now());
33 m_data->m_reason = why;
34
35 if(why != CRL_Code::Unspecified)
36 {
37 m_data->m_extensions.add(std::make_unique<Cert_Extension::CRL_ReasonCode>(why));
38 }
39 }
40
41/*
42* Compare two CRL_Entrys for equality
43*/
44bool operator==(const CRL_Entry& a1, const CRL_Entry& a2)
45 {
46 if(a1.serial_number() != a2.serial_number())
47 return false;
48 if(a1.expire_time() != a2.expire_time())
49 return false;
50 if(a1.reason_code() != a2.reason_code())
51 return false;
52 return true;
53 }
54
55/*
56* Compare two CRL_Entrys for inequality
57*/
58bool operator!=(const CRL_Entry& a1, const CRL_Entry& a2)
59 {
60 return !(a1 == a2);
61 }
62
63/*
64* DER encode a CRL_Entry
65*/
67 {
68 der.start_sequence()
73 .end_cons()
74 .end_cons();
75 }
76
77/*
78* Decode a BER encoded CRL_Entry
79*/
81 {
82 BigInt serial_number_bn;
83
84 auto data = std::make_unique<CRL_Entry_Data>();
85
86 BER_Decoder entry = source.start_sequence();
87
88 entry.decode(serial_number_bn).decode(data->m_time);
89 data->m_serial = BigInt::encode(serial_number_bn);
90
91 if(entry.more_items())
92 {
93 entry.decode(data->m_extensions);
94 if(auto ext = data->m_extensions.get_extension_object_as<Cert_Extension::CRL_ReasonCode>())
95 {
96 data->m_reason = ext->get_reason();
97 }
98 else
99 {
100 data->m_reason = CRL_Code::Unspecified;
101 }
102 }
103
104 entry.end_cons();
105
106 m_data = std::move(data);
107 }
108
109const CRL_Entry_Data& CRL_Entry::data() const
110 {
111 if(!m_data)
112 {
113 throw Invalid_State("CRL_Entry_Data uninitialized");
114 }
115
116 return *m_data;
117 }
118
119const std::vector<uint8_t>& CRL_Entry::serial_number() const
120 {
121 return data().m_serial;
122 }
123
125 {
126 return data().m_time;
127 }
128
130 {
131 return data().m_reason;
132 }
133
135 {
136 return data().m_extensions;
137 }
138
139
140}
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:193
bool more_items() const
Definition: ber_dec.cpp:201
BER_Decoder & end_cons()
Definition: ber_dec.cpp:304
BER_Decoder start_sequence()
Definition: ber_dec.h:117
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:805
static std::vector< uint8_t > encode(const BigInt &n)
Definition: bigint.h:780
Definition: x509_crl.h:29
CRL_Code reason_code() const
Definition: crl_ent.cpp:129
const X509_Time & expire_time() const
Definition: crl_ent.cpp:124
CRL_Entry()=default
const std::vector< uint8_t > & serial_number() const
Definition: crl_ent.cpp:119
void decode_from(BER_Decoder &) override
Definition: crl_ent.cpp:80
void encode_into(DER_Encoder &) const override
Definition: crl_ent.cpp:66
const Extensions & extensions() const
Definition: crl_ent.cpp:134
DER_Encoder & start_sequence()
Definition: der_enc.h:66
DER_Encoder & end_cons()
Definition: der_enc.cpp:196
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:290
const std::vector< uint8_t > & serial_number() const
Definition: x509cert.cpp:400
Definition: alg_id.cpp:12
ASN1_Time X509_Time
Definition: asn1_obj.h:422
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:81
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:64