8#include <botan/asn1_obj.h>
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>
24 m_month = cal.
month();
38 if(t_spec.size() == 13) {
40 }
else if(t_spec.size() == 15) {
43 throw Invalid_Argument(
"Time string could not be parsed as GeneralizedTime or UTCTime.");
64 uint32_t full_year = m_year;
67 if(m_year < 1950 || m_year >= 2050) {
71 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
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;
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;
83 std::string repr = std::to_string(int_repr) +
"Z";
87 const std::string zero_padding(desired_size - repr.size(),
'0');
89 return zero_padding + repr;
94 throw Invalid_State(
"ASN1_Time::readable_string: No time set");
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";
107 return (m_year != 0);
112 throw Invalid_State(
"ASN1_Time::cmp: Cannot compare empty times");
115 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
117 if(m_year < other.m_year) {
120 if(m_year > other.m_year) {
123 if(m_month < other.m_month) {
126 if(m_month > other.m_month) {
129 if(m_day < other.m_day) {
132 if(m_day > other.m_day) {
135 if(m_hour < other.m_hour) {
138 if(m_hour > other.m_hour) {
141 if(m_minute < other.m_minute) {
144 if(m_minute > other.m_minute) {
147 if(m_second < other.m_second) {
150 if(m_second > other.m_second) {
157void ASN1_Time::set_to(std::string_view t_spec,
ASN1_Type spec_tag) {
159 "Invalid tag for ASN1_Time");
162 BOTAN_ARG_CHECK(t_spec.size() == 15,
"Invalid GeneralizedTime input string");
167 BOTAN_ARG_CHECK(t_spec.back() ==
'Z',
"Botan does not support ASN1 times with timezones other than Z");
169 const size_t field_len = 2;
171 const size_t year_start = 0;
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;
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));
195 if(!passes_sanity_check()) {
196 throw Invalid_Argument(
fmt(
"ASN1_Time string '{}' does not seem to be valid", t_spec));
203bool ASN1_Time::passes_sanity_check()
const {
205 if(m_year < 1950 || m_year > 3100) {
208 if(m_month == 0 || m_month > 12) {
212 const uint32_t days_in_month[12] = {31, 28 + 1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
214 if(m_day == 0 || m_day > days_in_month[m_month - 1]) {
218 if(m_month == 2 && m_day == 29) {
219 if(m_year % 4 != 0) {
223 if(m_year % 100 == 0 && m_year % 400 != 0) {
228 if(m_hour >= 24 || m_minute >= 60 || m_second > 60) {
254 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
261 return (t1.
cmp(t2) == 0);
265 return (t1.
cmp(t2) != 0);
269 return (t1.
cmp(t2) <= 0);
273 return (t1.
cmp(t2) >= 0);
277 return (t1.
cmp(t2) < 0);
281 return (t1.
cmp(t2) > 0);
#define BOTAN_ARG_CHECK(expr, msg)
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.
void decode_from(BER_Decoder &) override
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.
void encode_into(DER_Encoder &) const override
DER encode a ASN1_Time.
BER_Object get_next_object()
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
std::chrono::system_clock::time_point to_std_timepoint() const
std::string to_string(const BER_Object &obj)
uint32_t to_u32bit(std::string_view str_view)
bool operator>=(const ASN1_Time &, const ASN1_Time &)
std::string fmt(std::string_view format, const T &... args)
bool operator<=(const ASN1_Time &, const ASN1_Time &)
bool operator<(const OID &a, const OID &b)
bool operator>(const ASN1_Time &, const ASN1_Time &)
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)