8#include <botan/asn1_obj.h>
9#include <botan/der_enc.h>
10#include <botan/ber_dec.h>
11#include <botan/exceptn.h>
12#include <botan/internal/parsing.h>
13#include <botan/internal/calendar.h>
24 m_month = cal.
month();
40 if(t_spec.size() == 13)
42 else if(t_spec.size() == 15)
45 throw Invalid_Argument(
"Time string could not be parsed as GeneralizedTime or UTCTime.");
51 "ASN1_Time: Bad encoding tag");
68 uint32_t full_year = m_year;
72 if(m_year < 1950 || m_year >= 2050)
74 " cannot be encoded as a UTCTime");
76 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
79 const uint64_t year_factor = 10000000000;
80 const uint64_t mon_factor = 100000000;
81 const uint64_t day_factor = 1000000;
82 const uint64_t hour_factor = 10000;
83 const uint64_t min_factor = 100;
85 const uint64_t int_repr =
86 year_factor * full_year +
87 mon_factor * m_month +
89 hour_factor * m_hour +
90 min_factor * m_minute +
97 while(repr.size() < desired_size)
106 throw Invalid_State(
"ASN1_Time::readable_string: No time set");
109 std::stringstream output;
110 output << std::setfill(
'0')
111 << std::setw(4) << m_year <<
"/"
112 << std::setw(2) << m_month <<
"/"
113 << std::setw(2) << m_day
115 << std::setw(2) << m_hour <<
":"
116 << std::setw(2) << m_minute <<
":"
117 << std::setw(2) << m_second
125 return (m_year != 0);
133 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
135 if(m_year < other.m_year)
return EARLIER;
136 if(m_year > other.m_year)
return LATER;
137 if(m_month < other.m_month)
return EARLIER;
138 if(m_month > other.m_month)
return LATER;
139 if(m_day < other.m_day)
return EARLIER;
140 if(m_day > other.m_day)
return LATER;
141 if(m_hour < other.m_hour)
return EARLIER;
142 if(m_hour > other.m_hour)
return LATER;
143 if(m_minute < other.m_minute)
return EARLIER;
144 if(m_minute > other.m_minute)
return LATER;
145 if(m_second < other.m_second)
return EARLIER;
146 if(m_second > other.m_second)
return LATER;
151void ASN1_Time::set_to(
const std::string& t_spec,
ASN1_Type spec_tag)
157 BOTAN_ARG_CHECK(t_spec.size() == 15,
"Invalid GeneralizedTime string");
164 BOTAN_ARG_CHECK(t_spec.back() ==
'Z',
"Botan does not support ASN1 times with timezones other than Z");
168 std::vector<std::string> params;
171 for(
size_t j = 0; j != YEAR_SIZE; ++j)
172 current += t_spec[j];
173 params.push_back(current);
176 for(
size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j)
178 current += t_spec[j];
179 if(current.size() == 2)
181 params.push_back(current);
191 m_second = (params.size() == 6) ?
to_u32bit(params[5]) : 0;
196 if(m_year >= 50) m_year += 1900;
200 if(!passes_sanity_check())
201 throw Invalid_Argument(
"Time " + t_spec +
" does not seem to be valid");
207bool ASN1_Time::passes_sanity_check()
const
210 if(m_year < 1950 || m_year > 3100)
212 if(m_month == 0 || m_month > 12)
215 const uint32_t days_in_month[12] = { 31, 28+1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
217 if(m_day == 0 || m_day > days_in_month[m_month-1])
220 if(m_month == 2 && m_day == 29)
225 if(m_year % 100 == 0 && m_year % 400 != 0)
229 if(m_hour >= 24 || m_minute >= 60 || m_second > 60)
258 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
265 {
return (t1.
cmp(t2) == 0); }
267 {
return (t1.
cmp(t2) != 0); }
270 {
return (t1.
cmp(t2) <= 0); }
272 {
return (t1.
cmp(t2) >= 0); }
275 {
return (t1.
cmp(t2) < 0); }
277 {
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)
bool operator>=(const ASN1_Time &, const ASN1_Time &)
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)
uint32_t to_u32bit(const std::string &str)