monotone

monotone Mtn Source Tree

Root/botan/x509opt.cpp

1/*************************************************
2* X.509 Certificate Options Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/x509self.h>
7#include <botan/util.h>
8#include <botan/parsing.h>
9#include <botan/oids.h>
10#include <botan/conf.h>
11#include <ctime>
12
13namespace Botan {
14
15/*************************************************
16* Set when the certificate should become valid *
17*************************************************/
18void X509_Cert_Options::not_before(const std::string& time_string)
19 {
20 start = X509_Time(time_string);
21 }
22
23/*************************************************
24* Set when the certificate should expire *
25*************************************************/
26void X509_Cert_Options::not_after(const std::string& time_string)
27 {
28 end = X509_Time(time_string);
29 }
30
31/*************************************************
32* Set key constraint information *
33*************************************************/
34void X509_Cert_Options::add_constraints(Key_Constraints usage)
35 {
36 constraints = usage;
37 }
38
39/*************************************************
40* Set key constraint information *
41*************************************************/
42void X509_Cert_Options::add_ex_constraint(const OID& oid)
43 {
44 ex_constraints.push_back(oid);
45 }
46
47/*************************************************
48* Set key constraint information *
49*************************************************/
50void 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*************************************************/
58void X509_Cert_Options::CA_key(u32bit limit)
59 {
60 is_CA = true;
61 path_limit = limit;
62 }
63
64/*************************************************
65* Do basic sanity checks *
66*************************************************/
67void X509_Cert_Options::sanity_check() const
68 {
69 if(common_name == "" || country == "")
70 throw Encoding_Error("X.509 certificate: name and country MUST be set");
71 if(country.size() != 2)
72 throw Encoding_Error("Invalid ISO country code: " + country);
73 if(start >= end)
74 throw Encoding_Error("X509_Cert_Options: invalid time constraints");
75 }
76
77/*************************************************
78* Initialize the certificate options *
79*************************************************/
80X509_Cert_Options::X509_Cert_Options(const std::string& initial_opts)
81 {
82 const u32bit DEFAULT_EXPIRE = Config::get_time("x509/ca/default_expire");
83 const u32bit OFFSET_FROM_NOW = Config::get_time("x509/ca/signing_offset");
84
85 is_CA = false;
86 path_limit = 0;
87 constraints = NO_CONSTRAINTS;
88
89 const u64bit current_time = system_time();
90
91 start = X509_Time(current_time - OFFSET_FROM_NOW);
92 end = X509_Time(current_time - OFFSET_FROM_NOW + DEFAULT_EXPIRE);
93
94 if(initial_opts == "")
95 return;
96
97 std::vector<std::string> parsed = split_on(initial_opts, '/');
98
99 if(parsed.size() > 4)
100 throw Invalid_Argument("X.509 cert options: Too many names: "
101 + initial_opts);
102
103 if(parsed.size() >= 1) common_name = parsed[0];
104 if(parsed.size() >= 2) country = parsed[1];
105 if(parsed.size() >= 3) organization = parsed[2];
106 if(parsed.size() == 4) org_unit = parsed[3];
107 }
108
109}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status