Botan  1.11.31
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  {
56  constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
57  }
58  else
59  {
60  std::unique_ptr<Public_Key> key(req.subject_public_key());
62  constraints = req.constraints();
63  }
64 
65  Extensions extensions;
66 
67  extensions.add(
69  true);
70 
71  if(constraints != NO_CONSTRAINTS)
72  {
73  extensions.add(new Cert_Extension::Key_Usage(constraints), true);
74  }
75 
76  extensions.add(new Cert_Extension::Authority_Key_ID(m_cert.subject_key_id()));
77  extensions.add(new Cert_Extension::Subject_Key_ID(req.raw_public_key()));
78 
79  extensions.add(
81 
82  extensions.add(
84 
85  return make_cert(m_signer, rng, m_ca_sig_algo,
86  req.raw_public_key(),
87  not_before, not_after,
88  m_cert.subject_dn(), req.subject_dn(),
89  extensions);
90  }
91 
92 /*
93 * Create a new certificate
94 */
97  const AlgorithmIdentifier& sig_algo,
98  const std::vector<byte>& pub_key,
99  const X509_Time& not_before,
100  const X509_Time& not_after,
101  const X509_DN& issuer_dn,
102  const X509_DN& subject_dn,
103  const Extensions& extensions)
104  {
105  const size_t X509_CERT_VERSION = 3;
106  const size_t SERIAL_BITS = 128;
107 
108  BigInt serial_no(rng, SERIAL_BITS);
109 
110  // clang-format off
112  signer, rng, sig_algo,
113  DER_Encoder().start_cons(SEQUENCE)
114  .start_explicit(0)
115  .encode(X509_CERT_VERSION-1)
116  .end_explicit()
117 
118  .encode(serial_no)
119 
120  .encode(sig_algo)
121  .encode(issuer_dn)
122 
123  .start_cons(SEQUENCE)
124  .encode(not_before)
125  .encode(not_after)
126  .end_cons()
127 
128  .encode(subject_dn)
129  .raw_bytes(pub_key)
130 
131  .start_explicit(3)
132  .start_cons(SEQUENCE)
133  .encode(extensions)
134  .end_cons()
135  .end_explicit()
136  .end_cons()
137  .get_contents()
138  ));;
139  // clang-format on
140  }
141 
142 /*
143 * Create a new, empty CRL
144 */
146  u32bit next_update) const
147  {
148  std::vector<CRL_Entry> empty;
149  return make_crl(empty, 1, next_update, rng);
150  }
151 
152 /*
153 * Update a CRL with new entries
154 */
156  const std::vector<CRL_Entry>& new_revoked,
158  u32bit next_update) const
159  {
160  std::vector<CRL_Entry> revoked = crl.get_revoked();
161 
162  std::copy(new_revoked.begin(), new_revoked.end(),
163  std::back_inserter(revoked));
164 
165  return make_crl(revoked, crl.crl_number() + 1, next_update, rng);
166  }
167 
168 /*
169 * Create a CRL
170 */
171 X509_CRL X509_CA::make_crl(const std::vector<CRL_Entry>& revoked,
172  u32bit crl_number, u32bit next_update,
173  RandomNumberGenerator& rng) const
174  {
175  const size_t X509_CRL_VERSION = 2;
176 
177  if(next_update == 0)
178  next_update = timespec_to_u32bit("7d");
179 
180  // Totally stupid: ties encoding logic to the return of std::time!!
181  auto current_time = std::chrono::system_clock::now();
182  auto expire_time = current_time + std::chrono::seconds(next_update);
183 
184  Extensions extensions;
185  extensions.add(
187  extensions.add(new Cert_Extension::CRL_Number(crl_number));
188 
189  // clang-format off
190  const std::vector<byte> crl = X509_Object::make_signed(
191  m_signer, rng, m_ca_sig_algo,
192  DER_Encoder().start_cons(SEQUENCE)
193  .encode(X509_CRL_VERSION-1)
194  .encode(m_ca_sig_algo)
195  .encode(m_cert.issuer_dn())
196  .encode(X509_Time(current_time))
197  .encode(X509_Time(expire_time))
198  .encode_if(revoked.size() > 0,
199  DER_Encoder()
201  .encode_list(revoked)
202  .end_cons()
203  )
204  .start_explicit(0)
205  .start_cons(SEQUENCE)
206  .encode(extensions)
207  .end_cons()
208  .end_explicit()
209  .end_cons()
210  .get_contents());
211  // clang-format on
212 
213  return X509_CRL(crl);
214  }
215 
216 /*
217 * Return the CA's certificate
218 */
220  {
221  return m_cert;
222  }
223 
224 /*
225 * Choose a signing format for the key
226 */
228  const std::string& hash_fn,
229  AlgorithmIdentifier& sig_algo)
230  {
231  const std::string algo_name = key.algo_name();
232 
233  std::unique_ptr<HashFunction> hash(HashFunction::create(hash_fn));
234  if(!hash)
235  throw Algorithm_Not_Found(hash_fn);
236 
237  if(key.max_input_bits() < hash->output_length() * 8)
238  throw Invalid_Argument("Key is too small for chosen hash function");
239 
240  std::string padding;
241  if(algo_name == "RSA")
242  {
243  padding = "EMSA3";
244  }
245  else if(algo_name == "DSA" || algo_name == "ECDSA" || algo_name == "ECGDSA" || algo_name == "ECKCDSA")
246  {
247  padding = "EMSA1";
248  }
249  else
250  {
251  throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name);
252  }
253 
254  const Signature_Format format = (key.message_parts() > 1) ? DER_SEQUENCE : IEEE_1363;
255 
256  padding = padding + "(" + hash->name() + ")";
257 
258  sig_algo.oid = OIDS::lookup(algo_name + "/" + padding);
259  sig_algo.parameters = key.algorithm_identifier().parameters;
260 
261  return new PK_Signer(key, padding, format);
262  }
263 
264 }
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:145
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:249
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:155
void verify_cert_constraints_valid_for_key_type(const Public_Key &pub_key, Key_Constraints constraints)
void add(Certificate_Extension *extn, bool critical=false)
Definition: x509_ext.cpp:92
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
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:219
X509_DN subject_dn() const
Definition: pkcs10.cpp:140
Definition: alg_id.cpp:13
std::vector< byte > subject_key_id() const
Definition: x509cert.cpp:416
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:95
X509_DN issuer_dn() const
Definition: x509cert.cpp:429
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:439
MechanismType hash
PK_Signer * choose_sig_format(const Private_Key &key, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition: x509_ca.cpp:227