Botan  2.11.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 */
28 CRL_Entry::CRL_Entry(const X509_Certificate& cert, CRL_Code why)
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 */
66 void CRL_Entry::encode_into(DER_Encoder& der) const
67  {
68  der.start_cons(SEQUENCE)
69  .encode(BigInt::decode(serial_number()))
70  .encode(expire_time())
71  .start_cons(SEQUENCE)
72  .encode(extensions())
73  .end_cons()
74  .end_cons();
75  }
76 
77 /*
78 * Decode a BER encoded CRL_Entry
79 */
80 void CRL_Entry::decode_from(BER_Decoder& source)
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 
124 const X509_Time& CRL_Entry::expire_time() const
125  {
126  return data().m_time;
127  }
128 
129 CRL_Code CRL_Entry::reason_code() const
130  {
131  return data().m_reason;
132  }
133 
134 const Extensions& CRL_Entry::extensions() const
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:23
const uint8_t cert[]
Definition: ffi.h:1483
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
Definition: alg_id.cpp:13
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition: pem.cpp:68
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:75
const botan_hash_t source
Definition: ffi.h:272