monotone

monotone Mtn Source Tree

Root/botan/x509opt.cpp

1/*************************************************
2* X.509 Certificate Options Source File *
3* (C) 1999-2007 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/config.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 =
83 global_config().option_as_time("x509/ca/default_expire");
84 const u32bit OFFSET_FROM_NOW =
85 global_config().option_as_time("x509/ca/signing_offset");
86
87 is_CA = false;
88 path_limit = 0;
89 constraints = NO_CONSTRAINTS;
90
91 const u64bit current_time = system_time();
92
93 start = X509_Time(current_time - OFFSET_FROM_NOW);
94 end = X509_Time(current_time - OFFSET_FROM_NOW + DEFAULT_EXPIRE);
95
96 if(initial_opts == "")
97 return;
98
99 std::vector<std::string> parsed = split_on(initial_opts, '/');
100
101 if(parsed.size() > 4)
102 throw Invalid_Argument("X.509 cert options: Too many names: "
103 + initial_opts);
104
105 if(parsed.size() >= 1) common_name = parsed[0];
106 if(parsed.size() >= 2) country = parsed[1];
107 if(parsed.size() >= 3) organization = parsed[2];
108 if(parsed.size() == 4) org_unit = parsed[3];
109 }
110
111}

Archive Download this file

Branches

Tags

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