Botan  2.4.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::X509_CA Class Referencefinal

#include <x509_ca.h>

Public Member Functions

X509_Certificate ca_certificate () const
 
X509_CRL new_crl (RandomNumberGenerator &rng, std::chrono::system_clock::time_point issue_time, std::chrono::seconds next_update) const
 
X509_CRL new_crl (RandomNumberGenerator &rng, uint32_t next_update=604800) const
 
X509_CAoperator= (const X509_CA &)=delete
 
X509_CAoperator= (X509_CA &&)=default
 
X509_Certificate sign_request (const PKCS10_Request &req, RandomNumberGenerator &rng, const X509_Time &not_before, const X509_Time &not_after) const
 
X509_CRL update_crl (const X509_CRL &last_crl, const std::vector< CRL_Entry > &new_entries, RandomNumberGenerator &rng, std::chrono::system_clock::time_point issue_time, std::chrono::seconds next_update) const
 
X509_CRL update_crl (const X509_CRL &last_crl, const std::vector< CRL_Entry > &new_entries, RandomNumberGenerator &rng, uint32_t next_update=604800) const
 
 X509_CA (const X509_Certificate &ca_certificate, const Private_Key &key, const std::string &hash_fn, RandomNumberGenerator &rng)
 
 X509_CA (const X509_Certificate &ca_certificate, const Private_Key &key, const std::map< std::string, std::string > &opts, const std::string &hash_fn, RandomNumberGenerator &rng)
 
 X509_CA (const X509_CA &)=delete
 
 X509_CA (X509_CA &&)=default
 
 ~X509_CA ()
 

Static Public Member Functions

static X509_Certificate make_cert (PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &sig_algo, const std::vector< uint8_t > &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)
 

Detailed Description

This class represents X.509 Certificate Authorities (CAs).

Definition at line 28 of file x509_ca.h.

Constructor & Destructor Documentation

◆ X509_CA() [1/4]

Botan::X509_CA::X509_CA ( const X509_Certificate ca_certificate,
const Private_Key key,
const std::string &  hash_fn,
RandomNumberGenerator rng 
)

Create a new CA object.

Parameters
ca_certificatethe certificate of the CA
keythe private key of the CA
hash_fnname of a hash function to use for signing
rngthe random generator to use

Definition at line 29 of file x509_ca.cpp.

References Botan::choose_sig_format(), Botan::X509_Certificate::is_CA_cert(), Botan::OIDS::lookup(), Botan::AlgorithmIdentifier::oid, and Botan::X509_Object::signature_algorithm().

32  :
33  m_ca_cert(c),
34  m_hash_fn(hash_fn)
35  {
36  if(!m_ca_cert.is_CA_cert())
37  throw Invalid_Argument("X509_CA: This certificate is not for a CA");
38 
39  std::map<std::string,std::string> opts;
40  // constructor without additional options: use the padding used in the CA certificate
41  // sig_oid_str = <sig_alg>/<padding>, so padding with all its options will look
42  // like a cipher mode to the scanner
43  std::string sig_oid_str = OIDS::lookup(c.signature_algorithm().oid);
44  SCAN_Name scanner(sig_oid_str);
45  std::string pad = scanner.cipher_mode();
46  if(!pad.empty())
47  opts.insert({"padding",pad});
48 
49  m_signer.reset(choose_sig_format(key, opts, rng, hash_fn, m_ca_sig_algo));
50  }
bool is_CA_cert() const
Definition: x509cert.cpp:430
PK_Signer * choose_sig_format(const Private_Key &key, RandomNumberGenerator &rng, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition: x509_ca.cpp:275
std::string lookup(const OID &oid)
Definition: oids.cpp:18

◆ X509_CA() [2/4]

Botan::X509_CA::X509_CA ( const X509_Certificate ca_certificate,
const Private_Key key,
const std::map< std::string, std::string > &  opts,
const std::string &  hash_fn,
RandomNumberGenerator rng 
)

Create a new CA object.

Parameters
ca_certificatethe certificate of the CA
keythe private key of the CA
optsadditional options, e.g. padding, as key value pairs
hash_fnname of a hash function to use for signing
rngthe random generator to use

Definition at line 55 of file x509_ca.cpp.

References Botan::choose_sig_format(), and Botan::X509_Certificate::is_CA_cert().

59  : m_ca_cert(ca_certificate), m_hash_fn(hash_fn)
60  {
61  if(!m_ca_cert.is_CA_cert())
62  throw Invalid_Argument("X509_CA: This certificate is not for a CA");
63 
64  m_signer.reset(choose_sig_format(key, opts, rng, hash_fn, m_ca_sig_algo));
65  }
X509_Certificate ca_certificate() const
Definition: x509_ca.cpp:266
bool is_CA_cert() const
Definition: x509cert.cpp:430
PK_Signer * choose_sig_format(const Private_Key &key, RandomNumberGenerator &rng, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition: x509_ca.cpp:275

◆ X509_CA() [3/4]

Botan::X509_CA::X509_CA ( const X509_CA )
delete

◆ X509_CA() [4/4]

Botan::X509_CA::X509_CA ( X509_CA &&  )
default

◆ ~X509_CA()

Botan::X509_CA::~X509_CA ( )

Definition at line 70 of file x509_ca.cpp.

71  {
72  /* for unique_ptr */
73  }

Member Function Documentation

◆ ca_certificate()

X509_Certificate Botan::X509_CA::ca_certificate ( ) const

Get the certificate of this CA.

Returns
CA certificate

Definition at line 266 of file x509_ca.cpp.

267  {
268  return m_ca_cert;
269  }

◆ make_cert()

X509_Certificate Botan::X509_CA::make_cert ( PK_Signer signer,
RandomNumberGenerator rng,
const AlgorithmIdentifier sig_algo,
const std::vector< uint8_t > &  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 
)
static

Interface for creating new certificates

Parameters
signera signing object
rnga random number generator
sig_algothe signature algorithm identifier
pub_keythe serialized public key
not_beforethe start time of the certificate
not_afterthe end time of the certificate
issuer_dnthe DN of the issuer
subject_dnthe DN of the subject
extensionsan optional list of certificate extensions
Returns
newly minted certificate

Definition at line 125 of file x509_ca.cpp.

References Botan::PEM_Code::encode(), Botan::X509_Object::make_signed(), and Botan::SEQUENCE.

Referenced by Botan::X509::create_self_signed_cert(), and sign_request().

134  {
135  const size_t X509_CERT_VERSION = 3;
136  const size_t SERIAL_BITS = 128;
137 
138  BigInt serial_no(rng, SERIAL_BITS);
139 
140  // clang-format off
141  return X509_Certificate(X509_Object::make_signed(
142  signer, rng, sig_algo,
143  DER_Encoder().start_cons(SEQUENCE)
144  .start_explicit(0)
145  .encode(X509_CERT_VERSION-1)
146  .end_explicit()
147 
148  .encode(serial_no)
149 
150  .encode(sig_algo)
151  .encode(issuer_dn)
152 
153  .start_cons(SEQUENCE)
154  .encode(not_before)
155  .encode(not_after)
156  .end_cons()
157 
158  .encode(subject_dn)
159  .raw_bytes(pub_key)
160 
161  .start_explicit(3)
162  .start_cons(SEQUENCE)
163  .encode(extensions)
164  .end_cons()
165  .end_explicit()
166  .end_cons()
167  .get_contents()
168  ));;
169  // clang-format on
170  }
static std::vector< uint8_t > make_signed(class PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &tbs)
Definition: x509_obj.cpp:271
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43

◆ new_crl() [1/2]

X509_CRL Botan::X509_CA::new_crl ( RandomNumberGenerator rng,
std::chrono::system_clock::time_point  issue_time,
std::chrono::seconds  next_update 
) const

Create a new and empty CRL for this CA.

Parameters
rngthe random number generator to use
issue_timethe issue time (typically system_clock::now)
next_updatethe time interval after issue_data within which a new CRL will be produced.
Returns
new CRL

Definition at line 197 of file x509_ca.cpp.

Referenced by new_crl().

200  {
201  std::vector<CRL_Entry> empty;
202  return make_crl(empty, 1, rng, issue_time, next_update);
203  }

◆ new_crl() [2/2]

X509_CRL Botan::X509_CA::new_crl ( RandomNumberGenerator rng,
uint32_t  next_update = 604800 
) const

Create a new and empty CRL for this CA.

Parameters
rngthe random number generator to use
next_updatethe time to set in next update in seconds as the offset from the current time
Returns
new CRL

Definition at line 175 of file x509_ca.cpp.

References new_crl().

177  {
178  return new_crl(rng,
179  std::chrono::system_clock::now(),
180  std::chrono::seconds(next_update));
181  }
X509_CRL new_crl(RandomNumberGenerator &rng, std::chrono::system_clock::time_point issue_time, std::chrono::seconds next_update) const
Definition: x509_ca.cpp:197

◆ operator=() [1/2]

X509_CA& Botan::X509_CA::operator= ( const X509_CA )
delete

◆ operator=() [2/2]

X509_CA& Botan::X509_CA::operator= ( X509_CA &&  )
default

◆ sign_request()

X509_Certificate Botan::X509_CA::sign_request ( const PKCS10_Request req,
RandomNumberGenerator rng,
const X509_Time not_before,
const X509_Time not_after 
) const

Sign a PKCS#10 Request.

Parameters
reqthe request to sign
rngthe rng to use
not_beforethe starting time for the certificate
not_afterthe expiration time for the certificate
Returns
resulting certificate

Definition at line 78 of file x509_ca.cpp.

References Botan::PKCS10_Request::constraints(), Botan::CRL_SIGN, Botan::PKCS10_Request::ex_constraints(), Botan::PKCS10_Request::extensions(), Botan::PKCS10_Request::is_CA(), Botan::KEY_CERT_SIGN, make_cert(), Botan::NO_CONSTRAINTS, Botan::PKCS10_Request::path_limit(), Botan::PKCS10_Request::raw_public_key(), Botan::Extensions::replace(), Botan::PKCS10_Request::subject_alt_name(), Botan::PKCS10_Request::subject_dn(), Botan::X509_Certificate::subject_dn(), Botan::X509_Certificate::subject_key_id(), Botan::PKCS10_Request::subject_public_key(), and Botan::verify_cert_constraints_valid_for_key_type().

82  {
83  Key_Constraints constraints;
84  if(req.is_CA())
85  {
86  constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
87  }
88  else
89  {
90  std::unique_ptr<Public_Key> key(req.subject_public_key());
91  verify_cert_constraints_valid_for_key_type(*key, req.constraints());
92  constraints = req.constraints();
93  }
94 
95  Extensions extensions = req.extensions();
96 
97  extensions.replace(
98  new Cert_Extension::Basic_Constraints(req.is_CA(), req.path_limit()),
99  true);
100 
101  if(constraints != NO_CONSTRAINTS)
102  {
103  extensions.replace(new Cert_Extension::Key_Usage(constraints), true);
104  }
105 
106  extensions.replace(new Cert_Extension::Authority_Key_ID(m_ca_cert.subject_key_id()));
107  extensions.replace(new Cert_Extension::Subject_Key_ID(req.raw_public_key(), m_hash_fn));
108 
109  extensions.replace(
110  new Cert_Extension::Subject_Alternative_Name(req.subject_alt_name()));
111 
112  extensions.replace(
113  new Cert_Extension::Extended_Key_Usage(req.ex_constraints()));
114 
115  return make_cert(m_signer.get(), rng, m_ca_sig_algo,
116  req.raw_public_key(),
117  not_before, not_after,
118  m_ca_cert.subject_dn(), req.subject_dn(),
119  extensions);
120  }
static X509_Certificate make_cert(PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &sig_algo, const std::vector< uint8_t > &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:125
const std::vector< uint8_t > & subject_key_id() const
Definition: x509cert.cpp:394
const X509_DN & subject_dn() const
Definition: x509cert.cpp:415
void verify_cert_constraints_valid_for_key_type(const Public_Key &pub_key, Key_Constraints constraints)

◆ update_crl() [1/2]

X509_CRL Botan::X509_CA::update_crl ( const X509_CRL last_crl,
const std::vector< CRL_Entry > &  new_entries,
RandomNumberGenerator rng,
std::chrono::system_clock::time_point  issue_time,
std::chrono::seconds  next_update 
) const

Create a new CRL by with additional entries.

Parameters
last_crlthe last CRL of this CA to add the new entries to
new_entriescontains the new CRL entries to be added to the CRL
rngthe random number generator to use
issue_timethe issue time (typically system_clock::now)
next_updatethe time interval after issue_data within which a new CRL will be produced.

Definition at line 205 of file x509_ca.cpp.

References Botan::Extensions::add(), Botan::X509_CRL::crl_number(), Botan::PEM_Code::encode(), Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode_list(), Botan::DER_Encoder::end_cons(), Botan::X509_CRL::get_revoked(), Botan::X509_Object::make_signed(), Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), Botan::X509_Certificate::subject_dn(), and Botan::X509_Certificate::subject_key_id().

Referenced by update_crl().

210  {
211  std::vector<CRL_Entry> revoked = last_crl.get_revoked();
212 
213  std::copy(new_revoked.begin(), new_revoked.end(),
214  std::back_inserter(revoked));
215 
216  return make_crl(revoked, last_crl.crl_number() + 1, rng, issue_time, next_update);
217  }

◆ update_crl() [2/2]

X509_CRL Botan::X509_CA::update_crl ( const X509_CRL last_crl,
const std::vector< CRL_Entry > &  new_entries,
RandomNumberGenerator rng,
uint32_t  next_update = 604800 
) const

Create a new CRL by with additional entries.

Parameters
last_crlthe last CRL of this CA to add the new entries to
new_entriescontains the new CRL entries to be added to the CRL
rngthe random number generator to use
next_updatethe time to set in next update in seconds as the offset from the current time

Definition at line 186 of file x509_ca.cpp.

References update_crl().

190  {
191  return update_crl(crl, new_revoked, rng,
192  std::chrono::system_clock::now(),
193  std::chrono::seconds(next_update));
194  }
X509_CRL update_crl(const X509_CRL &last_crl, const std::vector< CRL_Entry > &new_entries, RandomNumberGenerator &rng, std::chrono::system_clock::time_point issue_time, std::chrono::seconds next_update) const
Definition: x509_ca.cpp:205

The documentation for this class was generated from the following files: