Botan 1.10.17

Public Member Functions | List of all members
Botan::X509_Time Class Reference

`#include <asn1_obj.h>`

Inheritance diagram for Botan::X509_Time:

## Public Member Functions
std::string | as_string () const
s32bit | cmp (const X509_Time &) const
void | decode_from (class BER_Decoder &)
void | encode_into (class DER_Encoder &) const
std::string | readable_string () const
void | set_to (const std::string &)
void | set_to (const std::string &, ASN1_Tag)
bool | time_is_set () const
| X509_Time (u64bit)
| X509_Time (const std::string &="")
| X509_Time (const std::string &, ASN1_Tag)

## Detailed Description

X.509 Time

Definition at line 41 of file asn1_obj.h.

## Constructor & Destructor Documentation

## ◆ X509_Time() [1/3]

Botan::X509_Time::X509_Time | ( | u64bit | timer | ) |

Definition at line 28 of file asn1_tm.cpp.

References Botan::calendar_value(), Botan::calendar_point::day, Botan::GENERALIZED_TIME, Botan::calendar_point::hour, Botan::calendar_point::minutes, Botan::calendar_point::month, Botan::calendar_point::seconds, Botan::UTC_TIME, and Botan::calendar_point::year.

29 {
30 calendar_point cal = calendar_value(timer);
31
32 year = cal.year;
33 month = cal.month;
34 day = cal.day;
35 hour = cal.hour;
36 minute = cal.minutes;
37 second = cal.seconds;
38
39 tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME;
40 }
Botan::calendar_value
calendar_point calendar_value(u64bit a_time_t)
Definition: time.cpp:81
Botan::UTC_TIME
Definition: asn1_int.h:46
Botan::GENERALIZED_TIME
Definition: asn1_int.h:47

## ◆ X509_Time() [2/3]

Botan::X509_Time::X509_Time | ( | const std::string & | time_str = `""` | ) |

Definition at line 20 of file asn1_tm.cpp.

References set_to().

21 {
22 set_to(time_str);
23 }
Botan::X509_Time::set_to
void set_to(const std::string &)
Definition: asn1_tm.cpp:53

## ◆ X509_Time() [3/3]

Botan::X509_Time::X509_Time | ( | const std::string & | t_spec,
|  | ASN1_Tag | t
| ) |  |

Definition at line 45 of file asn1_tm.cpp.

References set_to().

45 : tag(t)
46 {
47 set_to(t_spec, tag);
48 }
Botan::X509_Time::set_to
void set_to(const std::string &)
Definition: asn1_tm.cpp:53

## Member Function Documentation

## ◆ as_string()

std::string Botan::X509_Time::as_string | ( |  | ) | const

Definition at line 180 of file asn1_tm.cpp.

References Botan::GENERALIZED_TIME, readable_string(), time_is_set(), Botan::to_string(), and Botan::UTC_TIME.

Referenced by encode_into().

181 {
182 if(time_is_set() == false)
183 throw Invalid_State("X509_Time::as_string: No time set");
184
185 std::string asn1rep;
186 if(tag == GENERALIZED_TIME)
187 asn1rep = to_string(year, 4);
188 else if(tag == UTC_TIME)
189 {
190 if(year < 1950 || year >= 2050)
191 throw Encoding_Error("X509_Time: The time " + readable_string() +
192 " cannot be encoded as a UTCTime");
193 u32bit asn1year = (year >= 2000) ? (year - 2000) : (year - 1900);
194 asn1rep = to_string(asn1year, 2);
195 }
196 else
197 throw Invalid_Argument("X509_Time: Invalid tag " + to_string(tag));
198
199 asn1rep += to_string(month, 2) + to_string(day, 2);
200 asn1rep += to_string(hour, 2) + to_string(minute, 2) + to_string(second, 2);
201 asn1rep += "Z";
202 return asn1rep;
203 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::X509_Time::time_is_set
bool time_is_set() const
Definition: asn1_tm.cpp:208
Botan::X509_Time::readable_string
std::string readable_string() const
Definition: asn1_tm.cpp:216
Botan::UTC_TIME
Definition: asn1_int.h:46
Botan::GENERALIZED_TIME
Definition: asn1_int.h:47
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::u32bit
unsigned int u32bit
Definition: types.h:32

## ◆ cmp()

s32bit Botan::X509_Time::cmp | ( | const X509_Time & | other | ) | const

Definition at line 250 of file asn1_tm.cpp.

References time_is_set().

Referenced by Botan::operator!=(), Botan::operator<(), Botan::operator<=(), Botan::operator==(), Botan::operator>(), and Botan::operator>=().

251 {
252 if(time_is_set() == false)
253 throw Invalid_State("X509_Time::cmp: No time set");
254
255 const s32bit EARLIER = -1, LATER = 1, SAME_TIME = 0;
256
257 if(year < other.year) return EARLIER;
258 if(year > other.year) return LATER;
259 if(month < other.month) return EARLIER;
260 if(month > other.month) return LATER;
261 if(day < other.day) return EARLIER;
262 if(day > other.day) return LATER;
263 if(hour < other.hour) return EARLIER;
264 if(hour > other.hour) return LATER;
265 if(minute < other.minute) return EARLIER;
266 if(minute > other.minute) return LATER;
267 if(second < other.second) return EARLIER;
268 if(second > other.second) return LATER;
269
270 return SAME_TIME;
271 }
Botan::X509_Time::time_is_set
bool time_is_set() const
Definition: asn1_tm.cpp:208
Botan::s32bit
signed int s32bit
Definition: types.h:37

## ◆ decode_from()

void Botan::X509_Time::decode_from | ( | class BER_Decoder & | from | ) |
| virtual

Decode whatever this object is from from

Parameters

  from | the BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 167 of file asn1_tm.cpp.

References Botan::BER_Decoder::get_next_object(), Botan::LATIN1_CHARSET, Botan::LOCAL_CHARSET, set_to(), Botan::ASN1::to_string(), Botan::Charset::transcode(), and Botan::BER_Object::type_tag.

168 {
169 BER_Object ber_time = source.get_next_object();
170
171 set_to(Charset::transcode(ASN1::to_string(ber_time),
172 LATIN1_CHARSET,
173 LOCAL_CHARSET),
174 ber_time.type_tag);
175 }
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_int.cpp:46
Botan::Charset::transcode
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
Botan::LATIN1_CHARSET
Definition: charset.h:23
Botan::X509_Time::set_to
void set_to(const std::string &)
Definition: asn1_tm.cpp:53
Botan::LOCAL_CHARSET
Definition: charset.h:20

## ◆ encode_into()

void Botan::X509_Time::encode_into | ( | class DER_Encoder & | to | ) | const
| virtual

Encode whatever this object is into to

Parameters

  to | the DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 153 of file asn1_tm.cpp.

References Botan::DER_Encoder::add_object(), as_string(), Botan::GENERALIZED_TIME, Botan::LATIN1_CHARSET, Botan::LOCAL_CHARSET, Botan::Charset::transcode(), Botan::UNIVERSAL, and Botan::UTC_TIME.

154 {
155 if(tag != GENERALIZED_TIME && tag != UTC_TIME)
156 throw Invalid_Argument("X509_Time: Bad encoding tag");
157
158 der.add_object(tag, UNIVERSAL,
159 Charset::transcode(as_string(),
160 LOCAL_CHARSET,
161 LATIN1_CHARSET));
162 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::Charset::transcode
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
Botan::LATIN1_CHARSET
Definition: charset.h:23
Botan::X509_Time::as_string
std::string as_string() const
Definition: asn1_tm.cpp:180
Botan::UTC_TIME
Definition: asn1_int.h:46
Botan::LOCAL_CHARSET
Definition: charset.h:20
Botan::GENERALIZED_TIME
Definition: asn1_int.h:47

## ◆ readable_string()

std::string Botan::X509_Time::readable_string | ( |  | ) | const

Definition at line 216 of file asn1_tm.cpp.

References time_is_set(), and Botan::to_string().

Referenced by as_string().

217 {
218 if(time_is_set() == false)
219 throw Invalid_State("X509_Time::readable_string: No time set");
220
221 std::string readable;
222 readable += to_string(year, 4) + "/";
223 readable += to_string(month ) + "/";
224 readable += to_string(day ) + " ";
225 readable += to_string(hour ) + ":";
226 readable += to_string(minute, 2) + ":";
227 readable += to_string(second, 2) + " UTC";
228 return readable;
229 }
Botan::X509_Time::time_is_set
bool time_is_set() const
Definition: asn1_tm.cpp:208
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42

## ◆ set_to() [1/2]

void Botan::X509_Time::set_to | ( | const std::string & | time_str | ) |

Definition at line 53 of file asn1_tm.cpp.

References Botan::GENERALIZED_TIME, Botan::Charset::is_digit(), Botan::NO_OBJECT, Botan::to_u32bit(), and Botan::UTC_TIME.

Referenced by decode_from(), and X509_Time().

54 {
55 if(time_str == "")
56 {
57 year = month = day = hour = minute = second = 0;
58 tag = NO_OBJECT;
59 return;
60 }
61
62 std::vector<std::string> params;
63 std::string current;
64
65 for(size_t j = 0; j != time_str.size(); ++j)
66 {
67 if(Charset::is_digit(time_str[j]))
68 current += time_str[j];
69 else
70 {
71 if(current != "")
72 params.push_back(current);
73 current.clear();
74 }
75 }
76 if(current != "")
77 params.push_back(current);
78
79 if(params.size() < 3 || params.size() > 6)
80 throw Invalid_Argument("Invalid time specification " + time_str);
81
82 year = to_u32bit(params[0]);
83 month = to_u32bit(params[1]);
84 day = to_u32bit(params[2]);
85 hour = (params.size() >= 4) ? to_u32bit(params[3]) : 0;
86 minute = (params.size() >= 5) ? to_u32bit(params[4]) : 0;
87 second = (params.size() == 6) ? to_u32bit(params[5]) : 0;
88
89 tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME;
90
91 if(!passes_sanity_check())
92 throw Invalid_Argument("Invalid time specification " + time_str);
93 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::Charset::is_digit
bool is_digit(char c)
Definition: charset.cpp:128
Botan::UTC_TIME
Definition: asn1_int.h:46
Botan::NO_OBJECT
Definition: asn1_int.h:49
Botan::GENERALIZED_TIME
Definition: asn1_int.h:47
Botan::to_u32bit
u32bit to_u32bit(const std::string &number)
Definition: parsing.cpp:18

## ◆ set_to() [2/2]

void Botan::X509_Time::set_to | ( | const std::string & | t_spec,
|  | ASN1_Tag | spec_tag
| ) |  |

Definition at line 98 of file asn1_tm.cpp.

References Botan::GENERALIZED_TIME, Botan::to_string(), Botan::to_u32bit(), and Botan::UTC_TIME.

99 {
100 if(spec_tag != GENERALIZED_TIME && spec_tag != UTC_TIME)
101 throw Invalid_Argument("X509_Time: Invalid tag " + to_string(spec_tag));
102
103 if(spec_tag == GENERALIZED_TIME && t_spec.size() != 13 && t_spec.size() != 15)
104 throw Invalid_Argument("Invalid GeneralizedTime: " + t_spec);
105
106 if(spec_tag == UTC_TIME && t_spec.size() != 11 && t_spec.size() != 13)
107 throw Invalid_Argument("Invalid UTCTime: " + t_spec);
108
109 if(t_spec[t_spec.size()-1] != 'Z')
110 throw Invalid_Argument("Invalid time encoding: " + t_spec);
111
112 const size_t YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4;
113
114 std::vector<std::string> params;
115 std::string current;
116
117 for(size_t j = 0; j != YEAR_SIZE; ++j)
118 current += t_spec[j];
119 params.push_back(current);
120 current.clear();
121
122 for(size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j)
123 {
124 current += t_spec[j];
125 if(current.size() == 2)
126 {
127 params.push_back(current);
128 current.clear();
129 }
130 }
131
132 year = to_u32bit(params[0]);
133 month = to_u32bit(params[1]);
134 day = to_u32bit(params[2]);
135 hour = to_u32bit(params[3]);
136 minute = to_u32bit(params[4]);
137 second = (params.size() == 6) ? to_u32bit(params[5]) : 0;
138 tag = spec_tag;
139
140 if(spec_tag == UTC_TIME)
141 {
142 if(year >= 50) year += 1900;
143 else year += 2000;
144 }
145
146 if(!passes_sanity_check())
147 throw Invalid_Argument("Invalid time specification " + t_spec);
148 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::UTC_TIME
Definition: asn1_int.h:46
Botan::GENERALIZED_TIME
Definition: asn1_int.h:47
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::to_u32bit
u32bit to_u32bit(const std::string &number)
Definition: parsing.cpp:18

## ◆ time_is_set()

bool Botan::X509_Time::time_is_set | ( |  | ) | const

Definition at line 208 of file asn1_tm.cpp.

Referenced by as_string(), cmp(), and readable_string().

209 {
210 return (year != 0);
211 }

The documentation for this class was generated from the following files:

- src/asn1/asn1_obj.h
- src/asn1/asn1_tm.cpp