Botan  2.8.0
Crypto and TLS for C++11
x509self.cpp
Go to the documentation of this file.
1 /*
2 * PKCS #10/Self Signed Cert Creation
3 * (C) 1999-2008,2018 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/x509self.h>
9 #include <botan/x509_ext.h>
10 #include <botan/x509_ca.h>
11 #include <botan/der_enc.h>
12 #include <botan/pubkey.h>
13 #include <botan/oids.h>
14 #include <botan/hash.h>
15 
16 namespace Botan {
17 
18 namespace {
19 
20 /*
21 * Load information from the X509_Cert_Options
22 */
23 void load_info(const X509_Cert_Options& opts, X509_DN& subject_dn,
24  AlternativeName& subject_alt)
25  {
26  subject_dn.add_attribute("X520.CommonName", opts.common_name);
27  subject_dn.add_attribute("X520.Country", opts.country);
28  subject_dn.add_attribute("X520.State", opts.state);
29  subject_dn.add_attribute("X520.Locality", opts.locality);
30  subject_dn.add_attribute("X520.Organization", opts.organization);
31  subject_dn.add_attribute("X520.OrganizationalUnit", opts.org_unit);
32  subject_dn.add_attribute("X520.SerialNumber", opts.serial_number);
33  subject_alt = AlternativeName(opts.email, opts.uri, opts.dns, opts.ip);
34  subject_alt.add_othername(OIDS::lookup("PKIX.XMPPAddr"),
35  opts.xmpp, UTF8_STRING);
36 
37  for(auto dns : opts.more_dns)
38  subject_alt.add_attribute("DNS", dns);
39  }
40 }
41 
42 namespace X509 {
43 
44 /*
45 * Create a new self-signed X.509 certificate
46 */
48  const Private_Key& key,
49  const std::string& hash_fn,
51  {
52  AlgorithmIdentifier sig_algo;
53  X509_DN subject_dn;
54  AlternativeName subject_alt;
55 
56  // for now, only the padding option is used
57  std::map<std::string,std::string> sig_opts = { {"padding",opts.padding_scheme} };
58 
59  const std::vector<uint8_t> pub_key = X509::BER_encode(key);
60  std::unique_ptr<PK_Signer> signer(choose_sig_format(key, sig_opts, rng, hash_fn, sig_algo));
61  load_info(opts, subject_dn, subject_alt);
62 
63  Extensions extensions = opts.extensions;
64 
65  Key_Constraints constraints;
66  if(opts.is_CA)
67  {
68  constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
69  }
70  else
71  {
73  constraints = opts.constraints;
74  }
75 
76  extensions.add_new(
78  true);
79 
80  if(constraints != NO_CONSTRAINTS)
81  {
82  extensions.add_new(new Cert_Extension::Key_Usage(constraints), true);
83  }
84 
85  std::unique_ptr<Cert_Extension::Subject_Key_ID> skid(new Cert_Extension::Subject_Key_ID(pub_key, hash_fn));
86 
87  extensions.add_new(new Cert_Extension::Authority_Key_ID(skid->get_key_id()));
88  extensions.add_new(skid.release());
89 
90  extensions.add_new(
92 
93  extensions.add_new(
95 
96  return X509_CA::make_cert(signer.get(), rng, sig_algo, pub_key,
97  opts.start, opts.end,
98  subject_dn, subject_dn,
99  extensions);
100  }
101 
102 /*
103 * Create a PKCS #10 certificate request
104 */
106  const Private_Key& key,
107  const std::string& hash_fn,
109  {
110  X509_DN subject_dn;
111  AlternativeName subject_alt;
112  load_info(opts, subject_dn, subject_alt);
113 
114  Key_Constraints constraints;
115  if(opts.is_CA)
116  {
117  constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
118  }
119  else
120  {
122  constraints = opts.constraints;
123  }
124 
125  Extensions extensions = opts.extensions;
126 
127  extensions.add_new(new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit));
128 
129  if(constraints != NO_CONSTRAINTS)
130  {
131  extensions.add_new(new Cert_Extension::Key_Usage(constraints));
132  }
134  extensions.add_new(new Cert_Extension::Subject_Alternative_Name(subject_alt));
135 
136  return PKCS10_Request::create(key,
137  subject_dn,
138  extensions,
139  hash_fn,
140  rng,
141  opts.padding_scheme,
142  opts.challenge);
143  }
144 
145 }
146 
147 }
PKCS10_Request create_cert_req(const X509_Cert_Options &opts, const Private_Key &key, const std::string &hash_fn, RandomNumberGenerator &rng)
Definition: x509self.cpp:105
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:150
static PKCS10_Request create(const Private_Key &key, const X509_DN &subject_dn, const Extensions &extensions, const std::string &hash_fn, RandomNumberGenerator &rng, const std::string &padding_scheme="", const std::string &challenge="")
Definition: pkcs10.cpp:58
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition: x509_key.cpp:19
bool add_new(Certificate_Extension *extn, bool critical=false)
Definition: x509_ext.cpp:135
Key_Constraints constraints
Definition: x509self.h:118
std::string padding_scheme
Definition: x509self.h:113
X509_Certificate create_self_signed_cert(const X509_Cert_Options &opts, const Private_Key &key, const std::string &hash_fn, RandomNumberGenerator &rng)
Definition: x509self.cpp:47
Definition: alg_id.cpp:13
void verify_cert_constraints_valid_for_key_type(const Public_Key &pub_key, Key_Constraints constraints)
PK_Signer * choose_sig_format(const Private_Key &key, RandomNumberGenerator &rng, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition: x509_ca.cpp:318
std::vector< OID > ex_constraints
Definition: x509self.h:123
std::string lookup(const OID &oid)
Definition: oids.cpp:113
std::string challenge
Definition: x509self.h:92