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