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