Botan  1.11.26
x509_ca.cpp
Go to the documentation of this file.
1 /*
2 * X.509 Certificate Authority
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_ca.h>
9 #include <botan/pubkey.h>
10 #include <botan/der_enc.h>
11 #include <botan/ber_dec.h>
12 #include <botan/bigint.h>
13 #include <botan/parsing.h>
14 #include <botan/oids.h>
15 #include <botan/hash.h>
16 #include <botan/key_constraint.h>
17 #include <algorithm>
18 #include <typeinfo>
19 #include <iterator>
20 #include <set>
21 
22 namespace Botan {
23 
24 /*
25 * Load the certificate and private key
26 */
28  const Private_Key& key,
29  const std::string& hash_fn) : m_cert(c)
30  {
31  if(!m_cert.is_CA_cert())
32  throw Invalid_Argument("X509_CA: This certificate is not for a CA");
33 
34  m_signer = choose_sig_format(key, hash_fn, m_ca_sig_algo);
35  }
36 
37 /*
38 * X509_CA Destructor
39 */
41  {
42  delete m_signer;
43  }
44 
45 /*
46 * Sign a PKCS #10 certificate request
47 */
50  const X509_Time& not_before,
51  const X509_Time& not_after)
52  {
53  Key_Constraints constraints;
54  if(req.is_CA())
55  constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
56  else
57  {
58  std::unique_ptr<Public_Key> key(req.subject_public_key());
59  constraints = find_constraints(*key, req.constraints());
60  }
61 
62  Extensions extensions;
63 
64  extensions.add(
66  true);
67 
68  extensions.add(new Cert_Extension::Key_Usage(constraints), true);
69 
70  extensions.add(new Cert_Extension::Authority_Key_ID(m_cert.subject_key_id()));
71  extensions.add(new Cert_Extension::Subject_Key_ID(req.raw_public_key()));
72 
73  extensions.add(
75 
76  extensions.add(
78 
79  return make_cert(m_signer, rng, m_ca_sig_algo,
80  req.raw_public_key(),
81  not_before, not_after,
82  m_cert.subject_dn(), req.subject_dn(),
83  extensions);
84  }
85 
86 /*
87 * Create a new certificate
88 */
91  const AlgorithmIdentifier& sig_algo,
92  const std::vector<byte>& pub_key,
93  const X509_Time& not_before,
94  const X509_Time& not_after,
95  const X509_DN& issuer_dn,
96  const X509_DN& subject_dn,
97  const Extensions& extensions)
98  {
99  const size_t X509_CERT_VERSION = 3;
100  const size_t SERIAL_BITS = 128;
101 
102  BigInt serial_no(rng, SERIAL_BITS);
103 
104  // clang-format off
106  signer, rng, sig_algo,
107  DER_Encoder().start_cons(SEQUENCE)
108  .start_explicit(0)
109  .encode(X509_CERT_VERSION-1)
110  .end_explicit()
111 
112  .encode(serial_no)
113 
114  .encode(sig_algo)
115  .encode(issuer_dn)
116 
117  .start_cons(SEQUENCE)
118  .encode(not_before)
119  .encode(not_after)
120  .end_cons()
121 
122  .encode(subject_dn)
123  .raw_bytes(pub_key)
124 
125  .start_explicit(3)
126  .start_cons(SEQUENCE)
127  .encode(extensions)
128  .end_cons()
129  .end_explicit()
130  .end_cons()
131  .get_contents()
132  ));;
133  // clang-format on
134  }
135 
136 /*
137 * Create a new, empty CRL
138 */
140  u32bit next_update) const
141  {
142  std::vector<CRL_Entry> empty;
143  return make_crl(empty, 1, next_update, rng);
144  }
145 
146 /*
147 * Update a CRL with new entries
148 */
150  const std::vector<CRL_Entry>& new_revoked,
152  u32bit next_update) const
153  {
154  std::vector<CRL_Entry> revoked = crl.get_revoked();
155 
156  std::copy(new_revoked.begin(), new_revoked.end(),
157  std::back_inserter(revoked));
158 
159  return make_crl(revoked, crl.crl_number() + 1, next_update, rng);
160  }
161 
162 /*
163 * Create a CRL
164 */
165 X509_CRL X509_CA::make_crl(const std::vector<CRL_Entry>& revoked,
166  u32bit crl_number, u32bit next_update,
167  RandomNumberGenerator& rng) const
168  {
169  const size_t X509_CRL_VERSION = 2;
170 
171  if(next_update == 0)
172  next_update = timespec_to_u32bit("7d");
173 
174  // Totally stupid: ties encoding logic to the return of std::time!!
175  auto current_time = std::chrono::system_clock::now();
176  auto expire_time = current_time + std::chrono::seconds(next_update);
177 
178  Extensions extensions;
179  extensions.add(
181  extensions.add(new Cert_Extension::CRL_Number(crl_number));
182 
183  // clang-format off
184  const std::vector<byte> crl = X509_Object::make_signed(
185  m_signer, rng, m_ca_sig_algo,
186  DER_Encoder().start_cons(SEQUENCE)
187  .encode(X509_CRL_VERSION-1)
188  .encode(m_ca_sig_algo)
189  .encode(m_cert.issuer_dn())
190  .encode(X509_Time(current_time))
191  .encode(X509_Time(expire_time))
192  .encode_if(revoked.size() > 0,
193  DER_Encoder()
195  .encode_list(revoked)
196  .end_cons()
197  )
198  .start_explicit(0)
199  .start_cons(SEQUENCE)
200  .encode(extensions)
201  .end_cons()
202  .end_explicit()
203  .end_cons()
204  .get_contents());
205  // clang-format on
206 
207  return X509_CRL(crl);
208  }
209 
210 /*
211 * Return the CA's certificate
212 */
214  {
215  return m_cert;
216  }
217 
218 /*
219 * Choose a signing format for the key
220 */
222  const std::string& hash_fn,
223  AlgorithmIdentifier& sig_algo)
224  {
225  const std::string algo_name = key.algo_name();
226 
227  std::unique_ptr<HashFunction> hash(HashFunction::create(hash_fn));
228  if(!hash)
229  throw Algorithm_Not_Found(hash_fn);
230 
231  if(key.max_input_bits() < hash->output_length() * 8)
232  throw Invalid_Argument("Key is too small for chosen hash function");
233 
234  std::string padding;
235  if(algo_name == "RSA")
236  padding = "EMSA3";
237  else if(algo_name == "DSA")
238  padding = "EMSA1";
239  else if(algo_name == "ECDSA")
240  padding = "EMSA1_BSI";
241  else
242  throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name);
243 
244  const Signature_Format format = (key.message_parts() > 1) ? DER_SEQUENCE : IEEE_1363;
245 
246  padding = padding + "(" + hash->name() + ")";
247 
248  sig_algo.oid = OIDS::lookup(algo_name + "/" + padding);
249  sig_algo.parameters = key.algorithm_identifier().parameters;
250 
251  return new PK_Signer(key, padding, format);
252  }
253 
254 }
u32bit crl_number() const
Definition: x509_crl.cpp:170
DER_Encoder & encode_list(const std::vector< T > &values)
Definition: der_enc.h:85
X509_CRL new_crl(RandomNumberGenerator &rng, u32bit next_update=0) const
Definition: x509_ca.cpp:139
X509_CA(const X509_Certificate &ca_certificate, const Private_Key &key, const std::string &hash_fn)
Definition: x509_ca.cpp:27
std::vector< OID > ex_constraints() const
Definition: pkcs10.cpp:182
Public_Key * subject_public_key() const
Definition: pkcs10.cpp:157
bool is_CA() const
Definition: pkcs10.cpp:195
virtual AlgorithmIdentifier algorithm_identifier() const =0
std::vector< byte > raw_public_key() const
Definition: pkcs10.cpp:148
Signature_Format
Definition: pubkey.h:24
bool is_CA_cert() const
Definition: x509cert.cpp:223
virtual std::string algo_name() const =0
std::uint32_t u32bit
Definition: types.h:33
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
X509_CRL update_crl(const X509_CRL &last_crl, const std::vector< CRL_Entry > &new_entries, RandomNumberGenerator &rng, u32bit next_update=0) const
Definition: x509_ca.cpp:149
void add(Certificate_Extension *extn, bool critical=false)
Definition: x509_ext.cpp:79
std::vector< byte > parameters
Definition: alg_id.h:39
AlternativeName subject_alt_name() const
Definition: pkcs10.cpp:166
static std::vector< byte > make_signed(class PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &alg_id, const secure_vector< byte > &tbs)
Definition: x509_obj.cpp:213
Key_Constraints find_constraints(const Public_Key &pub_key, Key_Constraints limits)
std::string lookup(const OID &oid)
Definition: oids.cpp:155
virtual size_t message_parts() const
Definition: pk_keys.h:61
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:98
X509_Certificate ca_certificate() const
Definition: x509_ca.cpp:213
X509_DN subject_dn() const
Definition: pkcs10.cpp:140
Definition: alg_id.cpp:13
std::vector< byte > subject_key_id() const
Definition: x509cert.cpp:361
Key_Constraints constraints() const
Definition: pkcs10.cpp:174
std::vector< CRL_Entry > get_revoked() const
Definition: x509_crl.cpp:146
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
static X509_Certificate make_cert(PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &sig_algo, const std::vector< byte > &pub_key, const X509_Time &not_before, const X509_Time &not_after, const X509_DN &issuer_dn, const X509_DN &subject_dn, const Extensions &extensions)
Definition: x509_ca.cpp:89
X509_DN issuer_dn() const
Definition: x509cert.cpp:374
u32bit timespec_to_u32bit(const std::string &timespec)
Definition: parsing.cpp:54
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137
virtual size_t max_input_bits() const =0
u32bit path_limit() const
Definition: pkcs10.cpp:203
X509_Certificate sign_request(const PKCS10_Request &req, RandomNumberGenerator &rng, const X509_Time &not_before, const X509_Time &not_after)
Definition: x509_ca.cpp:48
X509_DN subject_dn() const
Definition: x509cert.cpp:384
PK_Signer * choose_sig_format(const Private_Key &key, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition: x509_ca.cpp:221