8#include <botan/asn1_obj.h>
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>
25 m_month = cal.
month();
39 if(t_spec.size() == 13) {
41 }
else if(t_spec.size() == 15) {
44 throw Invalid_Argument(
"Time string could not be parsed as GeneralizedTime or UTCTime.");
65 uint32_t full_year = m_year;
68 if(m_year < 1950 || m_year >= 2050) {
72 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
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;
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;
84 std::string repr = std::to_string(int_repr) +
"Z";
88 const std::string zero_padding(desired_size - repr.size(),
'0');
90 return zero_padding + repr;
95 throw Invalid_State(
"ASN1_Time::readable_string: No time set");
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";
108 return (m_year != 0);
113 throw Invalid_State(
"ASN1_Time::cmp: Cannot compare empty times");
116 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
118 if(m_year < other.m_year) {
121 if(m_year > other.m_year) {
124 if(m_month < other.m_month) {
127 if(m_month > other.m_month) {
130 if(m_day < other.m_day) {
133 if(m_day > other.m_day) {
136 if(m_hour < other.m_hour) {
139 if(m_hour > other.m_hour) {
142 if(m_minute < other.m_minute) {
145 if(m_minute > other.m_minute) {
148 if(m_second < other.m_second) {
151 if(m_second > other.m_second) {
158void ASN1_Time::set_to(std::string_view t_spec,
ASN1_Type spec_tag) {
160 "Invalid tag for ASN1_Time");
163 BOTAN_ARG_CHECK(t_spec.size() == 15,
"Invalid GeneralizedTime input string");
168 BOTAN_ARG_CHECK(t_spec.back() ==
'Z',
"Botan does not support ASN1 times with timezones other than Z");
170 const size_t field_len = 2;
172 const size_t year_start = 0;
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;
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));
196 if(!passes_sanity_check()) {
197 throw Invalid_Argument(
fmt(
"ASN1_Time string '{}' does not seem to be valid", t_spec));
204bool ASN1_Time::passes_sanity_check()
const {
206 if(m_year < 1950 || m_year > 3100) {
209 if(m_month == 0 || m_month > 12) {
213 const uint32_t days_in_month[12] = {31, 28 + 1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
215 if(m_day == 0 || m_day > days_in_month[m_month - 1]) {
219 if(m_month == 2 && m_day == 29) {
220 if(m_year % 4 != 0) {
224 if(m_year % 100 == 0 && m_year % 400 != 0) {
229 if(m_hour >= 24 || m_minute >= 60 || m_second > 60) {
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)
uint64_t seconds_since_epoch() const
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)