Botan  2.4.0
Crypto and TLS for C++11
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/crl_ent.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 
15 namespace Botan {
16 
17 struct CRL_Entry_Data
18  {
19  std::vector<uint8_t> m_serial;
20  X509_Time m_time;
21  CRL_Code m_reason = UNSPECIFIED;
22  Extensions m_extensions;
23  };
24 
25 /*
26 * Create a CRL_Entry
27 */
29  {
30  m_data.reset(new 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 != UNSPECIFIED)
36  {
37  m_data->m_extensions.add(new Cert_Extension::CRL_ReasonCode(why));
38  }
39  }
40 
41 /*
42 * Compare two CRL_Entrys for equality
43 */
44 bool 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 */
58 bool 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_cons(SEQUENCE)
69  .encode(BigInt::decode(serial_number()))
70  .encode(expire_time())
72  .encode(extensions())
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  std::unique_ptr<CRL_Entry_Data> data(new CRL_Entry_Data);
85 
86  BER_Decoder entry = source.start_cons(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 = UNSPECIFIED;
101  }
102  }
103 
104  entry.end_cons();
105 
106  m_data.reset(data.release());
107  }
108 
109 const 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.get();
117  }
118 
119 const 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 }
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:90
CRL_Code
Definition: crl_ent.h:22
CRL_Entry()=default
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:355
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
void decode_from(class BER_Decoder &) override
Definition: crl_ent.cpp:80
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
BER_Decoder & end_cons()
Definition: ber_dec.cpp:265
const X509_Time & expire_time() const
Definition: crl_ent.cpp:124
const std::vector< uint8_t > & serial_number() const
Definition: crl_ent.cpp:119
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:251
Definition: alg_id.cpp:13
Definition: crl_ent.h:42
CRL_Code reason_code() const
Definition: crl_ent.cpp:129
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:75
const std::vector< uint8_t > & serial_number() const
Definition: x509cert.cpp:399
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54
bool more_items() const
Definition: ber_dec.cpp:166
void encode_into(class DER_Encoder &) const override
Definition: crl_ent.cpp:66
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
const Extensions & extensions() const
Definition: crl_ent.cpp:134