Botan 2.19.5 Crypto and TLS for C&

x509opt.cpp
Go to the documentation of this file.
1/*
2* X.509 Certificate Options
3* (C) 1999-2007 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/parsing.h>
10#include <chrono>
11
12 namespace Botan {
13
14/*
15* Set when the certificate should become valid
16*/
17 void X509_Cert_Options::not_before(const std::string& time_string)
18 {
19 start = X509_Time(time_string, ASN1_Tag::UTC_OR_GENERALIZED_TIME);
20 }
21
22/*
23* Set when the certificate should expire
24*/
25 void X509_Cert_Options::not_after(const std::string& time_string)
26 {
27 end = X509_Time(time_string, ASN1_Tag::UTC_OR_GENERALIZED_TIME);
28 }
29
30/*
31* Set key constraint information
32*/
33 void X509_Cert_Options::add_constraints(Key_Constraints usage)
34 {
35 constraints = usage;
36 }
37
38/*
39* Set key constraint information
40*/
41 void X509_Cert_Options::add_ex_constraint(const OID& oid)
42 {
43 ex_constraints.push_back(oid);
44 }
45
46/*
47* Set key constraint information
48*/
49 void X509_Cert_Options::add_ex_constraint(const std::string& oid_str)
50 {
51 ex_constraints.push_back(OID::from_string(oid_str));
52 }
53
54/*
55* Mark this certificate for CA usage
56*/
57 void X509_Cert_Options::CA_key(size_t limit)
58 {
59 is_CA = true;
60 path_limit = limit;
61 }
62
63 void X509_Cert_Options::set_padding_scheme(const std::string& scheme)
64 {
65 padding_scheme = scheme;
66 }
67
68/*
69* Initialize the certificate options
70*/
71 X509_Cert_Options::X509_Cert_Options(const std::string& initial_opts,
72 uint32_t expiration_time)
73 {
74 is_CA = false;
75 path_limit = 0;
76 constraints = NO_CONSTRAINTS;
77 // use default for chosen algorithm
78 padding_scheme = "";
79
80 auto now = std::chrono::system_clock::now();
81
82 start = X509_Time(now);
83 end = X509_Time(now + std::chrono::seconds(expiration_time));
84
85 if(initial_opts.empty())
86 return;
87
88 std::vector<std::string> parsed = split_on(initial_opts, '/');
89
90 if(parsed.size() > 4)
91 throw Invalid_Argument("X.509 cert options: Too many names: "
92 + initial_opts);
93
94 if(parsed.size() >= 1) common_name = parsed[0];
95 if(parsed.size() >= 2) country = parsed[1];
96 if(parsed.size() >= 3) organization = parsed[2];
97 if(parsed.size() == 4) org_unit = parsed[3];
98 }
99
100}
Botan::Invalid_Argument
Definition: exceptn.h:137
Botan::OID
Definition: asn1_obj.h:194
Botan::OID::from_string
static OID from_string(const std::string &str)
Definition: asn1_oid.cpp:62
Botan::X509_Cert_Options::common_name
std::string common_name
Definition: x509self.h:29
Botan::X509_Cert_Options::org_unit
std::string org_unit
Definition: x509self.h:44
Botan::X509_Cert_Options::ex_constraints
std::vector< OID > ex_constraints
Definition: x509self.h:130
Botan::X509_Cert_Options::constraints
Key_Constraints constraints
Definition: x509self.h:125
Botan::X509_Cert_Options::X509_Cert_Options
X509_Cert_Options(const std::string &opts="", uint32_t expire_time=365 *24 *60 *60)
Definition: x509opt.cpp:71
Botan::X509_Cert_Options::add_constraints
void add_constraints(Key_Constraints constr)
Definition: x509opt.cpp:33
Botan::X509_Cert_Options::is_CA
bool is_CA
Definition: x509self.h:113
Botan::X509_Cert_Options::country
std::string country
Definition: x509self.h:34
Botan::X509_Cert_Options::end
X509_Time end
Definition: x509self.h:108
Botan::X509_Cert_Options::organization
std::string organization
Definition: x509self.h:39
Botan::X509_Cert_Options::start
X509_Time start
Definition: x509self.h:104
Botan::X509_Cert_Options::path_limit
size_t path_limit
Definition: x509self.h:118
Botan::X509_Cert_Options::set_padding_scheme
void set_padding_scheme(const std::string &scheme)
Definition: x509opt.cpp:63
Botan::X509_Cert_Options::add_ex_constraint
void add_ex_constraint(const OID &oid)
Definition: x509opt.cpp:41
Botan::X509_Cert_Options::CA_key
void CA_key(size_t limit=1)
Definition: x509opt.cpp:57
Botan::X509_Cert_Options::not_after
void not_after(const std::string &time)
Definition: x509opt.cpp:25
Botan::X509_Cert_Options::padding_scheme
std::string padding_scheme
Definition: x509self.h:120
Botan::X509_Cert_Options::not_before
void not_before(const std::string &time)
Definition: x509opt.cpp:17
Botan
Definition: alg_id.cpp:13
Botan::X509_Time
ASN1_Time X509_Time
Definition: asn1_obj.h:386
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:148
Botan::UTC_OR_GENERALIZED_TIME
@ UTC_OR_GENERALIZED_TIME
Definition: asn1_obj.h:56
Botan::Key_Constraints
Key_Constraints
Definition: pkix_enums.h:106
Botan::NO_CONSTRAINTS
@ NO_CONSTRAINTS
Definition: pkix_enums.h:107