Botan  2.13.0
Crypto and TLS for C++11
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  {
20  }
21 
22 /*
23 * Set when the certificate should expire
24 */
25 void X509_Cert_Options::not_after(const std::string& time_string)
26  {
28  }
29 
30 /*
31 * Set key constraint information
32 */
34  {
35  constraints = usage;
36  }
37 
38 /*
39 * Set key constraint information
40 */
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;
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 }
void add_constraints(Key_Constraints constr)
Definition: x509opt.cpp:33
std::string org_unit
Definition: x509self.h:45
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:148
std::string country
Definition: x509self.h:35
void not_before(const std::string &time)
Definition: x509opt.cpp:17
void not_after(const std::string &time)
Definition: x509opt.cpp:25
void add_ex_constraint(const OID &oid)
Definition: x509opt.cpp:41
void CA_key(size_t limit=1)
Definition: x509opt.cpp:57
std::string common_name
Definition: x509self.h:30
Key_Constraints constraints
Definition: x509self.h:126
std::string padding_scheme
Definition: x509self.h:121
Definition: alg_id.cpp:13
X509_Cert_Options(const std::string &opts="", uint32_t expire_time=365 *24 *60 *60)
Definition: x509opt.cpp:71
void set_padding_scheme(const std::string &scheme)
Definition: x509opt.cpp:63
std::vector< OID > ex_constraints
Definition: x509self.h:131
std::string organization
Definition: x509self.h:40
static OID from_string(const std::string &str)
Definition: asn1_oid.cpp:62