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