Botan 3.13.0
Crypto and TLS for C&
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
10#include <botan/assert.h>
11#include <botan/pubkey.h>
12#include <botan/x509_ca.h>
13#include <botan/x509_ext.h>
14#include <botan/x509_key.h>
15#include <botan/internal/fmt.h>
16
17namespace Botan {
18
19namespace {
20
21/*
22* Load information from the X509_Cert_Options
23*/
24X509_DN load_dn_info(const X509_Cert_Options& opts) {
25 X509_DN subject_dn;
26
27 subject_dn.add_attribute("X520.CommonName", opts.common_name);
28 subject_dn.add_attribute("X520.Country", opts.country);
29 subject_dn.add_attribute("X520.State", opts.state);
30 subject_dn.add_attribute("X520.Locality", opts.locality);
31 subject_dn.add_attribute("X520.Organization", opts.organization);
32 subject_dn.add_attribute("X520.OrganizationalUnit", opts.org_unit);
33 subject_dn.add_attribute("X520.SerialNumber", opts.serial_number);
34
35 for(const auto& extra_ou : opts.more_org_units) {
36 subject_dn.add_attribute("X520.OrganizationalUnit", extra_ou);
37 }
38
39 return subject_dn;
40}
41
42auto create_alt_name_ext(const X509_Cert_Options& opts, const Extensions& extensions) {
43 AlternativeName subject_alt;
44
45 /*
46 If the extension was already created in opts.extension we need to
47 merge the values provided in opts with the values set in the extension.
48 */
49 if(const auto* ext = extensions.get_extension_object_as<Cert_Extension::Subject_Alternative_Name>()) {
50 subject_alt = ext->get_alt_name();
51 }
52
53 subject_alt.add_dns(opts.dns);
54 for(const auto& nm : opts.more_dns) {
55 subject_alt.add_dns(nm);
56 }
57 subject_alt.add_uri(opts.uri);
58 subject_alt.add_email(opts.email);
59 if(!opts.ip.empty()) {
60 if(auto ipv4 = IPv4Address::from_string(opts.ip)) {
61 subject_alt.add_ipv4_address(*ipv4);
62 } else {
63 throw Invalid_Argument(fmt("Invalid IPv4 address '{}'", opts.ip));
64 }
65 }
66
67 if(!opts.xmpp.empty()) {
68 subject_alt.add_other_name(OID::from_string("PKIX.XMPPAddr"), ASN1_String(opts.xmpp, ASN1_Type::Utf8String));
69 }
70
71 return std::make_unique<Cert_Extension::Subject_Alternative_Name>(subject_alt);
72}
73
74} // namespace
75
76namespace X509 {
77
78/*
79* Create a new self-signed X.509 certificate
80*/
82 const Private_Key& key,
83 std::string_view hash_fn,
85 const std::vector<uint8_t> pub_key = X509::BER_encode(key);
86 auto signer = X509_Object::choose_sig_format(key, rng, hash_fn, opts.padding_scheme);
87 const AlgorithmIdentifier sig_algo = signer->algorithm_identifier();
88 BOTAN_ASSERT_NOMSG(sig_algo.oid().has_value());
89
90 const auto subject_dn = load_dn_info(opts);
91
92 Extensions extensions = opts.extensions;
93
94 const auto constraints = opts.is_CA ? Key_Constraints::ca_constraints() : opts.constraints;
95
96 if(!constraints.compatible_with(key)) {
97 throw Invalid_Argument("The requested key constraints are incompatible with the algorithm");
98 }
99
100 extensions.add_new(std::make_unique<Cert_Extension::Basic_Constraints>(opts.is_CA, opts.path_limit), true);
101
102 if(!constraints.empty()) {
103 extensions.add_new(std::make_unique<Cert_Extension::Key_Usage>(constraints), true);
104 }
105
106 auto skid = std::make_unique<Cert_Extension::Subject_Key_ID>(key);
107
108 extensions.add_new(std::make_unique<Cert_Extension::Authority_Key_ID>(skid->get_key_id()));
109 extensions.add_new(std::move(skid));
110
111 extensions.replace(create_alt_name_ext(opts, extensions));
112
113 extensions.add_new(std::make_unique<Cert_Extension::Extended_Key_Usage>(opts.ex_constraints));
114
115 return X509_CA::make_cert(*signer, rng, sig_algo, pub_key, opts.start, opts.end, subject_dn, subject_dn, extensions);
116}
117
118/*
119* Create a PKCS #10 certificate request
120*/
122 const Private_Key& key,
123 std::string_view hash_fn,
125 const auto subject_dn = load_dn_info(opts);
126
127 const auto constraints = opts.is_CA ? Key_Constraints::ca_constraints() : opts.constraints;
128
129 if(!constraints.compatible_with(key)) {
130 throw Invalid_Argument("The requested key constraints are incompatible with the algorithm");
131 }
132
133 Extensions extensions = opts.extensions;
134
135 extensions.add_new(std::make_unique<Cert_Extension::Basic_Constraints>(opts.is_CA, opts.path_limit));
136
137 if(!constraints.empty()) {
138 extensions.add_new(std::make_unique<Cert_Extension::Key_Usage>(constraints));
139 }
140
141 extensions.replace(create_alt_name_ext(opts, extensions));
142
143 if(!opts.ex_constraints.empty()) {
144 extensions.add_new(std::make_unique<Cert_Extension::Extended_Key_Usage>(opts.ex_constraints));
145 }
146
147 return PKCS10_Request::create(key, subject_dn, extensions, hash_fn, rng, opts.padding_scheme, opts.challenge);
148}
149
150} // namespace X509
151
152} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
const OID & oid() const
Definition asn1_obj.h:688
void add_dns(std::string_view dns)
Add a DNS name to this AlternativeName, parsing and validating the input.
Definition alt_name.cpp:63
void replace(std::unique_ptr< Certificate_Extension > extn, bool critical=false)
Definition x509_ext.cpp:225
bool add_new(std::unique_ptr< Certificate_Extension > extn, bool critical=false)
Definition x509_ext.cpp:203
static std::optional< IPv4Address > from_string(std::string_view str)
static Key_Constraints ca_constraints()
Definition pkix_enums.h:176
bool has_value() const
Definition asn1_obj.h:474
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
static PKCS10_Request create(const Private_Key &key, const X509_DN &subject_dn, const Extensions &extensions, std::string_view hash_fn, RandomNumberGenerator &rng, std::string_view padding_scheme="", std::string_view challenge="")
Definition pkcs10.cpp:54
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:115
std::vector< OID > ex_constraints
Definition x509self.h:134
Key_Constraints constraints
Definition x509self.h:129
std::string padding_scheme
Definition x509self.h:124
void add_attribute(std::string_view key, std::string_view val)
Definition x509_dn.cpp:137
static std::unique_ptr< PK_Signer > choose_sig_format(const Private_Key &key, RandomNumberGenerator &rng, std::string_view hash_fn, std::string_view padding_algo)
Definition x509_obj.cpp:240
PKCS10_Request create_cert_req(const X509_Cert_Options &opts, const Private_Key &key, std::string_view hash_fn, RandomNumberGenerator &rng)
Definition x509self.cpp:121
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition x509_key.h:24
X509_Certificate create_self_signed_cert(const X509_Cert_Options &opts, const Private_Key &key, std::string_view hash_fn, RandomNumberGenerator &rng)
Definition x509self.cpp:81
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53