1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
* X.509 Time Types
* (C) 1999-2007 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/asn1_time.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/charset.h>
#include <botan/parsing.h>
#include <botan/calendar.h>
#include <sstream>
#include <iomanip>
namespace Botan {
X509_Time::X509_Time(const std::chrono::system_clock::time_point& time)
{
calendar_point cal = calendar_value(time);
m_year = cal.year;
m_month = cal.month;
m_day = cal.day;
m_hour = cal.hour;
m_minute = cal.minutes;
m_second = cal.seconds;
m_tag = (m_year >= 2050) ? GENERALIZED_TIME : UTC_TIME;
}
X509_Time::X509_Time(const std::string& t_spec, ASN1_Tag tag)
{
set_to(t_spec, tag);
}
void X509_Time::encode_into(DER_Encoder& der) const
{
if(m_tag != GENERALIZED_TIME && m_tag != UTC_TIME)
throw Invalid_Argument("X509_Time: Bad encoding tag");
der.add_object(m_tag, UNIVERSAL,
Charset::transcode(to_string(),
LOCAL_CHARSET,
LATIN1_CHARSET));
}
void X509_Time::decode_from(BER_Decoder& source)
{
BER_Object ber_time = source.get_next_object();
set_to(Charset::transcode(ASN1::to_string(ber_time),
LATIN1_CHARSET,
LOCAL_CHARSET),
ber_time.type_tag);
}
std::string X509_Time::to_string() const
{
if(time_is_set() == false)
throw Invalid_State("X509_Time::as_string: No time set");
u32bit full_year = m_year;
if(m_tag == UTC_TIME)
{
if(m_year < 1950 || m_year >= 2050)
throw Encoding_Error("X509_Time: The time " + readable_string() +
" cannot be encoded as a UTCTime");
full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
}
const auto factor_y = uint64_t{10000000000ull}; // literal exceeds 32bit int range
const auto factor_m = uint64_t{100000000ull};
const auto factor_d = uint64_t{1000000ull};
const auto factor_h = uint64_t{10000ull};
const auto factor_i = uint64_t{100ull};
std::string repr = std::to_string(factor_y * full_year +
factor_m * m_month +
factor_d * m_day +
factor_h * m_hour +
factor_i * m_minute +
m_second) + "Z";
u32bit desired_size = (m_tag == UTC_TIME) ? 13 : 15;
while(repr.size() < desired_size)
repr = "0" + repr;
return repr;
}
std::string X509_Time::readable_string() const
{
if(time_is_set() == false)
throw Invalid_State("X509_Time::readable_string: No time set");
// desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
std::stringstream output;
{
using namespace std;
output << setfill('0')
<< setw(4) << m_year << "/" << setw(2) << m_month << "/" << setw(2) << m_day
<< " "
<< setw(2) << m_hour << ":" << setw(2) << m_minute << ":" << setw(2) << m_second
<< " UTC";
}
return output.str();
}
bool X509_Time::time_is_set() const
{
return (m_year != 0);
}
s32bit X509_Time::cmp(const X509_Time& other) const
{
if(time_is_set() == false)
throw Invalid_State("X509_Time::cmp: No time set");
const s32bit EARLIER = -1, LATER = 1, SAME_TIME = 0;
if(m_year < other.m_year) return EARLIER;
if(m_year > other.m_year) return LATER;
if(m_month < other.m_month) return EARLIER;
if(m_month > other.m_month) return LATER;
if(m_day < other.m_day) return EARLIER;
if(m_day > other.m_day) return LATER;
if(m_hour < other.m_hour) return EARLIER;
if(m_hour > other.m_hour) return LATER;
if(m_minute < other.m_minute) return EARLIER;
if(m_minute > other.m_minute) return LATER;
if(m_second < other.m_second) return EARLIER;
if(m_second > other.m_second) return LATER;
return SAME_TIME;
}
void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag)
{
if(spec_tag == GENERALIZED_TIME)
{
if(t_spec.size() != 13 && t_spec.size() != 15)
throw Invalid_Argument("Invalid GeneralizedTime string: '" + t_spec + "'");
}
else if(spec_tag == UTC_TIME)
{
if(t_spec.size() != 11 && t_spec.size() != 13)
throw Invalid_Argument("Invalid UTCTime string: '" + t_spec + "'");
}
else
{
throw Invalid_Argument("Invalid time tag " + std::to_string(spec_tag) + " val " + t_spec);
}
if(t_spec[t_spec.size()-1] != 'Z')
throw Invalid_Argument("Invalid time encoding: " + t_spec);
const size_t YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4;
std::vector<std::string> params;
std::string current;
for(size_t j = 0; j != YEAR_SIZE; ++j)
current += t_spec[j];
params.push_back(current);
current.clear();
for(size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j)
{
current += t_spec[j];
if(current.size() == 2)
{
params.push_back(current);
current.clear();
}
}
m_year = to_u32bit(params[0]);
m_month = to_u32bit(params[1]);
m_day = to_u32bit(params[2]);
m_hour = to_u32bit(params[3]);
m_minute = to_u32bit(params[4]);
m_second = (params.size() == 6) ? to_u32bit(params[5]) : 0;
m_tag = spec_tag;
if(spec_tag == UTC_TIME)
{
if(m_year >= 50) m_year += 1900;
else m_year += 2000;
}
if(!passes_sanity_check())
throw Invalid_Argument("Time did not pass sanity check: " + t_spec);
}
/*
* Do a general sanity check on the time
*/
bool X509_Time::passes_sanity_check() const
{
if(m_year < 1950 || m_year > 2100)
return false;
if(m_month == 0 || m_month > 12)
return false;
if(m_day == 0 || m_day > 31)
return false;
if(m_hour >= 24 || m_minute > 60 || m_second > 60)
return false;
return true;
}
/*
* Compare two X509_Times for in various ways
*/
bool operator==(const X509_Time& t1, const X509_Time& t2)
{ return (t1.cmp(t2) == 0); }
bool operator!=(const X509_Time& t1, const X509_Time& t2)
{ return (t1.cmp(t2) != 0); }
bool operator<=(const X509_Time& t1, const X509_Time& t2)
{ return (t1.cmp(t2) <= 0); }
bool operator>=(const X509_Time& t1, const X509_Time& t2)
{ return (t1.cmp(t2) >= 0); }
bool operator<(const X509_Time& t1, const X509_Time& t2)
{ return (t1.cmp(t2) < 0); }
bool operator>(const X509_Time& t1, const X509_Time& t2)
{ return (t1.cmp(t2) > 0); }
}
|