Botan  1.10.9
asn1_tm.cpp
Go to the documentation of this file.
1 /*
2 * X.509 Time Types
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/asn1_obj.h>
9 #include <botan/der_enc.h>
10 #include <botan/ber_dec.h>
11 #include <botan/charset.h>
12 #include <botan/parsing.h>
13 #include <botan/time.h>
14 
15 namespace Botan {
16 
17 /*
18 * Create an X509_Time
19 */
20 X509_Time::X509_Time(const std::string& time_str)
21  {
22  set_to(time_str);
23  }
24 
25 /*
26 * Create an X509_Time
27 */
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  }
41 
42 /*
43 * Create an X509_Time
44 */
45 X509_Time::X509_Time(const std::string& t_spec, ASN1_Tag t) : tag(t)
46  {
47  set_to(t_spec, tag);
48  }
49 
50 /*
51 * Set the time with a human readable string
52 */
53 void X509_Time::set_to(const std::string& time_str)
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  }
94 
95 /*
96 * Set the time with an ISO time format string
97 */
98 void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag)
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  }
149 
150 /*
151 * DER encode a X509_Time
152 */
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,
161  LATIN1_CHARSET));
162  }
163 
164 /*
165 * Decode a BER encoded X509_Time
166 */
168  {
169  BER_Object ber_time = source.get_next_object();
170 
173  LOCAL_CHARSET),
174  ber_time.type_tag);
175  }
176 
177 /*
178 * Return a string representation of the time
179 */
180 std::string X509_Time::as_string() const
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  }
204 
205 /*
206 * Return if the time has been set somehow
207 */
209  {
210  return (year != 0);
211  }
212 
213 /*
214 * Return a human readable string representation
215 */
216 std::string X509_Time::readable_string() const
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  }
230 
231 /*
232 * Do a general sanity check on the time
233 */
234 bool X509_Time::passes_sanity_check() const
235  {
236  if(year < 1950 || year > 2100)
237  return false;
238  if(month == 0 || month > 12)
239  return false;
240  if(day == 0 || day > 31)
241  return false;
242  if(hour >= 24 || minute > 60 || second > 60)
243  return false;
244  return true;
245  }
246 
247 /*
248 * Compare this time against another
249 */
250 s32bit X509_Time::cmp(const X509_Time& other) const
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  }
272 
273 /*
274 * Compare two X509_Times for in various ways
275 */
276 bool operator==(const X509_Time& t1, const X509_Time& t2)
277  { return (t1.cmp(t2) == 0); }
278 bool operator!=(const X509_Time& t1, const X509_Time& t2)
279  { return (t1.cmp(t2) != 0); }
280 
281 bool operator<=(const X509_Time& t1, const X509_Time& t2)
282  { return (t1.cmp(t2) <= 0); }
283 bool operator>=(const X509_Time& t1, const X509_Time& t2)
284  { return (t1.cmp(t2) >= 0); }
285 
286 bool operator<(const X509_Time& t1, const X509_Time& t2)
287  { return (t1.cmp(t2) < 0); }
288 bool operator>(const X509_Time& t1, const X509_Time& t2)
289  { return (t1.cmp(t2) > 0); }
290 
291 }
void decode_from(class BER_Decoder &)
Definition: asn1_tm.cpp:167
bool operator!=(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:106
bool BOTAN_DLL operator>=(const X509_Time &, const X509_Time &)
Definition: asn1_tm.cpp:283
bool operator==(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:98
void encode_into(class DER_Encoder &) const
Definition: asn1_tm.cpp:153
std::string as_string() const
Definition: asn1_tm.cpp:180
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::string to_string(const BER_Object &obj)
Definition: asn1_int.cpp:46
calendar_point calendar_value(u64bit a_time_t)
Definition: time.cpp:81
signed int s32bit
Definition: types.h:37
unsigned long long u64bit
Definition: types.h:49
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
bool BOTAN_DLL operator>(const X509_Time &, const X509_Time &)
Definition: asn1_tm.cpp:288
ASN1_Tag
Definition: asn1_int.h:19
bool time_is_set() const
Definition: asn1_tm.cpp:208
s32bit cmp(const X509_Time &) const
Definition: asn1_tm.cpp:250
bool is_digit(char c)
Definition: charset.cpp:128
BER_Object get_next_object()
Definition: ber_dec.cpp:193
void set_to(const std::string &)
Definition: asn1_tm.cpp:53
ASN1_Tag type_tag
Definition: asn1_int.h:82
bool BOTAN_DLL operator<(const X509_Time &, const X509_Time &)
Definition: asn1_tm.cpp:286
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
std::string readable_string() const
Definition: asn1_tm.cpp:216
X509_Time(u64bit)
Definition: asn1_tm.cpp:28
bool BOTAN_DLL operator<=(const X509_Time &, const X509_Time &)
Definition: asn1_tm.cpp:281
u32bit to_u32bit(const std::string &number)
Definition: parsing.cpp:18
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346
unsigned int u32bit
Definition: types.h:32