Botan 3.4.0
Crypto and TLS for C&
asn1_time.cpp
Go to the documentation of this file.
1/*
2* X.509 Time Types
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/asn1_obj.h>
9
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/exceptn.h>
13#include <botan/internal/calendar.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/parsing.h>
16#include <iomanip>
17
18namespace Botan {
19
20ASN1_Time::ASN1_Time(const std::chrono::system_clock::time_point& time) {
21 calendar_point cal(time);
22
23 m_year = cal.year();
24 m_month = cal.month();
25 m_day = cal.day();
26 m_hour = cal.hour();
27 m_minute = cal.minutes();
28 m_second = cal.seconds();
29
30 m_tag = (m_year >= 2050) ? ASN1_Type::GeneralizedTime : ASN1_Type::UtcTime;
31}
32
33ASN1_Time::ASN1_Time(std::string_view t_spec, ASN1_Type tag) {
34 set_to(t_spec, tag);
35}
36
37ASN1_Time::ASN1_Time(std::string_view t_spec) {
38 if(t_spec.size() == 13) {
39 set_to(t_spec, ASN1_Type::UtcTime);
40 } else if(t_spec.size() == 15) {
41 set_to(t_spec, ASN1_Type::GeneralizedTime);
42 } else {
43 throw Invalid_Argument("Time string could not be parsed as GeneralizedTime or UTCTime.");
44 }
45}
46
48 BOTAN_ARG_CHECK(m_tag == ASN1_Type::UtcTime || m_tag == ASN1_Type::GeneralizedTime, "ASN1_Time: Bad encoding tag");
49
51}
52
54 BER_Object ber_time = source.get_next_object();
55
56 set_to(ASN1::to_string(ber_time), ber_time.type());
57}
58
59std::string ASN1_Time::to_string() const {
60 if(time_is_set() == false) {
61 throw Invalid_State("ASN1_Time::to_string: No time set");
62 }
63
64 uint32_t full_year = m_year;
65
66 if(m_tag == ASN1_Type::UtcTime) {
67 if(m_year < 1950 || m_year >= 2050) {
68 throw Encoding_Error(fmt("ASN_Time: The time {} cannot be encoded as UTCTime", readable_string()));
69 }
70
71 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
72 }
73
74 const uint64_t year_factor = 10000000000;
75 const uint64_t mon_factor = 100000000;
76 const uint64_t day_factor = 1000000;
77 const uint64_t hour_factor = 10000;
78 const uint64_t min_factor = 100;
79
80 const uint64_t int_repr = year_factor * full_year + mon_factor * m_month + day_factor * m_day +
81 hour_factor * m_hour + min_factor * m_minute + m_second;
82
83 std::string repr = std::to_string(int_repr) + "Z";
84
85 const size_t desired_size = (m_tag == ASN1_Type::UtcTime) ? 13 : 15;
86
87 const std::string zero_padding(desired_size - repr.size(), '0');
88
89 return zero_padding + repr;
90}
91
92std::string ASN1_Time::readable_string() const {
93 if(time_is_set() == false) {
94 throw Invalid_State("ASN1_Time::readable_string: No time set");
95 }
96
97 // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
98 std::stringstream output;
99 output << std::setfill('0') << std::setw(4) << m_year << "/" << std::setw(2) << m_month << "/" << std::setw(2)
100 << m_day << " " << std::setw(2) << m_hour << ":" << std::setw(2) << m_minute << ":" << std::setw(2)
101 << m_second << " UTC";
102
103 return output.str();
104}
105
107 return (m_year != 0);
108}
109
110int32_t ASN1_Time::cmp(const ASN1_Time& other) const {
111 if(!time_is_set() || !other.time_is_set()) {
112 throw Invalid_State("ASN1_Time::cmp: Cannot compare empty times");
113 }
114
115 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
116
117 if(m_year < other.m_year) {
118 return EARLIER;
119 }
120 if(m_year > other.m_year) {
121 return LATER;
122 }
123 if(m_month < other.m_month) {
124 return EARLIER;
125 }
126 if(m_month > other.m_month) {
127 return LATER;
128 }
129 if(m_day < other.m_day) {
130 return EARLIER;
131 }
132 if(m_day > other.m_day) {
133 return LATER;
134 }
135 if(m_hour < other.m_hour) {
136 return EARLIER;
137 }
138 if(m_hour > other.m_hour) {
139 return LATER;
140 }
141 if(m_minute < other.m_minute) {
142 return EARLIER;
143 }
144 if(m_minute > other.m_minute) {
145 return LATER;
146 }
147 if(m_second < other.m_second) {
148 return EARLIER;
149 }
150 if(m_second > other.m_second) {
151 return LATER;
152 }
153
154 return SAME_TIME;
155}
156
157void ASN1_Time::set_to(std::string_view t_spec, ASN1_Type spec_tag) {
159 "Invalid tag for ASN1_Time");
160
161 if(spec_tag == ASN1_Type::GeneralizedTime) {
162 BOTAN_ARG_CHECK(t_spec.size() == 15, "Invalid GeneralizedTime input string");
163 } else if(spec_tag == ASN1_Type::UtcTime) {
164 BOTAN_ARG_CHECK(t_spec.size() == 13, "Invalid UTCTime input string");
165 }
166
167 BOTAN_ARG_CHECK(t_spec.back() == 'Z', "Botan does not support ASN1 times with timezones other than Z");
168
169 const size_t field_len = 2;
170
171 const size_t year_start = 0;
172 const size_t year_len = (spec_tag == ASN1_Type::UtcTime) ? 2 : 4;
173 const size_t month_start = year_start + year_len;
174 const size_t day_start = month_start + field_len;
175 const size_t hour_start = day_start + field_len;
176 const size_t min_start = hour_start + field_len;
177 const size_t sec_start = min_start + field_len;
178
179 m_year = to_u32bit(t_spec.substr(year_start, year_len));
180 m_month = to_u32bit(t_spec.substr(month_start, field_len));
181 m_day = to_u32bit(t_spec.substr(day_start, field_len));
182 m_hour = to_u32bit(t_spec.substr(hour_start, field_len));
183 m_minute = to_u32bit(t_spec.substr(min_start, field_len));
184 m_second = to_u32bit(t_spec.substr(sec_start, field_len));
185 m_tag = spec_tag;
186
187 if(spec_tag == ASN1_Type::UtcTime) {
188 if(m_year >= 50) {
189 m_year += 1900;
190 } else {
191 m_year += 2000;
192 }
193 }
194
195 if(!passes_sanity_check()) {
196 throw Invalid_Argument(fmt("ASN1_Time string '{}' does not seem to be valid", t_spec));
197 }
198}
199
200/*
201* Do a general sanity check on the time
202*/
203bool ASN1_Time::passes_sanity_check() const {
204 // AppVeyor's trust store includes a cert with expiration date in 3016 ...
205 if(m_year < 1950 || m_year > 3100) {
206 return false;
207 }
208 if(m_month == 0 || m_month > 12) {
209 return false;
210 }
211
212 const uint32_t days_in_month[12] = {31, 28 + 1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
213
214 if(m_day == 0 || m_day > days_in_month[m_month - 1]) {
215 return false;
216 }
217
218 if(m_month == 2 && m_day == 29) {
219 if(m_year % 4 != 0) {
220 return false; // not a leap year
221 }
222
223 if(m_year % 100 == 0 && m_year % 400 != 0) {
224 return false;
225 }
226 }
227
228 if(m_hour >= 24 || m_minute >= 60 || m_second > 60) {
229 return false;
230 }
231
232 if(m_tag == ASN1_Type::UtcTime) {
233 /*
234 UTCTime limits the value of components such that leap seconds
235 are not covered. See "UNIVERSAL 23" in "Information technology
236 Abstract Syntax Notation One (ASN.1): Specification of basic notation"
237
238 http://www.itu.int/ITU-T/studygroups/com17/languages/
239 */
240 if(m_second > 59) {
241 return false;
242 }
243 }
244
245 return true;
246}
247
248std::chrono::system_clock::time_point ASN1_Time::to_std_timepoint() const {
249 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
250}
251
253 auto tp = this->to_std_timepoint();
254 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
255}
256
257/*
258* Compare two ASN1_Times for in various ways
259*/
260bool operator==(const ASN1_Time& t1, const ASN1_Time& t2) {
261 return (t1.cmp(t2) == 0);
262}
263
264bool operator!=(const ASN1_Time& t1, const ASN1_Time& t2) {
265 return (t1.cmp(t2) != 0);
266}
267
268bool operator<=(const ASN1_Time& t1, const ASN1_Time& t2) {
269 return (t1.cmp(t2) <= 0);
270}
271
272bool operator>=(const ASN1_Time& t1, const ASN1_Time& t2) {
273 return (t1.cmp(t2) >= 0);
274}
275
276bool operator<(const ASN1_Time& t1, const ASN1_Time& t2) {
277 return (t1.cmp(t2) < 0);
278}
279
280bool operator>(const ASN1_Time& t1, const ASN1_Time& t2) {
281 return (t1.cmp(t2) > 0);
282}
283
284} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
uint64_t time_since_epoch() const
Return time since epoch.
std::chrono::system_clock::time_point to_std_timepoint() const
Returns a STL timepoint object.
std::string to_string() const
Return an internal string representation of the time.
Definition asn1_time.cpp:59
void decode_from(BER_Decoder &) override
Definition asn1_time.cpp:53
ASN1_Time()=default
Create an invalid ASN1_Time.
bool time_is_set() const
Return if the time has been set somehow.
int32_t cmp(const ASN1_Time &other) const
Compare this time against another.
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition asn1_time.cpp:92
void encode_into(DER_Encoder &) const override
DER encode a ASN1_Time.
Definition asn1_time.cpp:47
BER_Object get_next_object()
Definition ber_dec.cpp:231
ASN1_Type type() const
Definition asn1_obj.h:146
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition der_enc.cpp:222
uint32_t hour() const
Definition calendar.h:33
uint32_t seconds() const
Definition calendar.h:41
uint32_t day() const
Definition calendar.h:30
std::chrono::system_clock::time_point to_std_timepoint() const
Definition calendar.cpp:59
uint32_t minutes() const
Definition calendar.h:36
uint32_t month() const
Definition calendar.h:27
uint32_t year() const
Definition calendar.h:24
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:185
uint32_t to_u32bit(std::string_view str_view)
Definition parsing.cpp:32
bool operator>=(const ASN1_Time &, const ASN1_Time &)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
bool operator<=(const ASN1_Time &, const ASN1_Time &)
ASN1_Type
Definition asn1_obj.h:43
bool operator<(const OID &a, const OID &b)
Definition asn1_oid.cpp:133
bool operator>(const ASN1_Time &, const ASN1_Time &)
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:69
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54