Botan 3.12.0
Crypto and TLS for C&
Botan::ASN1_Time Class Referencefinal

#include <asn1_time.h>

Inheritance diagram for Botan::ASN1_Time:
Botan::ASN1_Object

Public Member Functions

 ASN1_Time ()=default
 Create an invalid ASN1_Time.
 ASN1_Time (const std::chrono::system_clock::time_point &time)
 Create a ASN1_Time from a time point.
BOTAN_FUTURE_EXPLICIT ASN1_Time (std::string_view t_spec)
 Create an ASN1_Time from string.
 ASN1_Time (std::string_view t_spec, ASN1_Type tag)
 Create an ASN1_Time from string and a specified tagging (Utc or Generalized).
std::vector< uint8_t > BER_encode () const
int32_t cmp (const ASN1_Time &other) const
 Compare this time against another.
void decode_from (BER_Decoder &from) override
void encode_into (DER_Encoder &to) const override
 DER encode a ASN1_Time.
std::string readable_string () const
 Returns a human friendly string representation of no particular formatting.
bool time_is_set () const
 Return if the time has been set somehow.
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.

Static Public Member Functions

static ASN1_Time from_seconds_since_epoch (uint64_t seconds)
 Create an ASN1_Time from seconds since epoch.

Detailed Description

Time (GeneralizedTime/UniversalTime)

Definition at line 18 of file asn1_time.h.

Constructor & Destructor Documentation

◆ ASN1_Time() [1/4]

Botan::ASN1_Time::ASN1_Time ( )
default

◆ ASN1_Time() [2/4]

Botan::ASN1_Time::ASN1_Time ( const std::chrono::system_clock::time_point & time)
explicit

Create a ASN1_Time from a time point.

Definition at line 25 of file asn1_time.cpp.

25 {
26 const 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}
ASN1_Type
Definition asn1_obj.h:43

References Botan::calendar_point::day(), Botan::GeneralizedTime, Botan::calendar_point::hour(), Botan::calendar_point::minutes(), Botan::calendar_point::month(), Botan::calendar_point::seconds(), Botan::UtcTime, and Botan::calendar_point::year().

◆ ASN1_Time() [3/4]

Botan::ASN1_Time::ASN1_Time ( std::string_view t_spec)

Create an ASN1_Time from string.

Definition at line 43 of file asn1_time.cpp.

43 {
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}

References Botan::GeneralizedTime, and Botan::UtcTime.

◆ ASN1_Time() [4/4]

Botan::ASN1_Time::ASN1_Time ( std::string_view t_spec,
ASN1_Type tag )

Create an ASN1_Time from string and a specified tagging (Utc or Generalized).

Definition at line 39 of file asn1_time.cpp.

39 {
40 set_to(t_spec, tag);
41}

Member Function Documentation

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 20 of file asn1_obj.cpp.

20 {
21 std::vector<uint8_t> output;
22 DER_Encoder der(output);
23 this->encode_into(der);
24 return output;
25}
virtual void encode_into(DER_Encoder &to) const =0

References encode_into().

Referenced by decode_from(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), Botan::PSS_Params::PSS_Params(), and Botan::Certificate_Store_In_SQL::revoke_cert().

◆ cmp()

int32_t Botan::ASN1_Time::cmp ( const ASN1_Time & other) const

Compare this time against another.

Definition at line 127 of file asn1_time.cpp.

127 {
128 if(!time_is_set() || !other.time_is_set()) {
129 throw Invalid_State("ASN1_Time::cmp: Cannot compare empty times");
130 }
131
132 constexpr int32_t EARLIER = -1;
133 constexpr int32_t LATER = 1;
134 constexpr int32_t SAME_TIME = 0;
135
136 if(m_year < other.m_year) {
137 return EARLIER;
138 }
139 if(m_year > other.m_year) {
140 return LATER;
141 }
142 if(m_month < other.m_month) {
143 return EARLIER;
144 }
145 if(m_month > other.m_month) {
146 return LATER;
147 }
148 if(m_day < other.m_day) {
149 return EARLIER;
150 }
151 if(m_day > other.m_day) {
152 return LATER;
153 }
154 if(m_hour < other.m_hour) {
155 return EARLIER;
156 }
157 if(m_hour > other.m_hour) {
158 return LATER;
159 }
160 if(m_minute < other.m_minute) {
161 return EARLIER;
162 }
163 if(m_minute > other.m_minute) {
164 return LATER;
165 }
166 if(m_second < other.m_second) {
167 return EARLIER;
168 }
169 if(m_second > other.m_second) {
170 return LATER;
171 }
172
173 return SAME_TIME;
174}
bool time_is_set() const
Return if the time has been set somehow.

References ASN1_Time(), and time_is_set().

Referenced by Botan::operator!=(), Botan::operator<(), Botan::operator<=(), Botan::operator==(), Botan::operator>(), and Botan::operator>=().

◆ decode_from()

void Botan::ASN1_Time::decode_from ( BER_Decoder & from)
overridevirtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 59 of file asn1_time.cpp.

59 {
60 const BER_Object ber_time = source.get_next_object();
61
62 if(ber_time.get_class() != ASN1_Class::Universal ||
63 (ber_time.type() != ASN1_Type::UtcTime && ber_time.type() != ASN1_Type::GeneralizedTime)) {
64 throw Decoding_Error(fmt("ASN1_Time: Unexpected tag {}/{}",
65 static_cast<uint32_t>(ber_time.type()),
66 static_cast<uint32_t>(ber_time.get_class())));
67 }
68
69 try {
70 set_to(ASN1::to_string(ber_time), ber_time.type());
71 } catch(Invalid_Argument& e) {
72 throw Decoding_Error(fmt("Invalid ASN1_Time encoding: {}", e.what()));
73 }
74}
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:190
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt(), Botan::GeneralizedTime, Botan::BER_Object::get_class(), Botan::BER_Decoder::get_next_object(), Botan::ASN1::to_string(), Botan::BER_Object::type(), Botan::Universal, Botan::UtcTime, and Botan::Exception::what().

◆ encode_into()

void Botan::ASN1_Time::encode_into ( DER_Encoder & to) const
overridevirtual

DER encode a ASN1_Time.

Implements Botan::ASN1_Object.

Definition at line 53 of file asn1_time.cpp.

53 {
54 BOTAN_ARG_CHECK(m_tag == ASN1_Type::UtcTime || m_tag == ASN1_Type::GeneralizedTime, "ASN1_Time: Bad encoding tag");
55
56 der.add_object(m_tag, ASN1_Class::Universal, to_string());
57}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
std::string to_string() const
Return an internal string representation of the time.
Definition asn1_time.cpp:76

References Botan::DER_Encoder::add_object(), BOTAN_ARG_CHECK, Botan::GeneralizedTime, to_string(), Botan::Universal, and Botan::UtcTime.

◆ from_seconds_since_epoch()

ASN1_Time Botan::ASN1_Time::from_seconds_since_epoch ( uint64_t seconds)
static

Create an ASN1_Time from seconds since epoch.

Definition at line 21 of file asn1_time.cpp.

21 {
22 return ASN1_Time(std::chrono::system_clock::time_point(std::chrono::seconds(time_since_epoch)));
23}
uint64_t time_since_epoch() const
Return time since epoch.
ASN1_Time()=default
Create an invalid ASN1_Time.

References ASN1_Time(), and time_since_epoch().

Referenced by ASN1_Time().

◆ readable_string()

std::string Botan::ASN1_Time::readable_string ( ) const

Returns a human friendly string representation of no particular formatting.

Definition at line 109 of file asn1_time.cpp.

109 {
110 if(!time_is_set()) {
111 throw Invalid_State("ASN1_Time::readable_string: No time set");
112 }
113
114 // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
115 std::stringstream output;
116 output << std::setfill('0') << std::setw(4) << m_year << "/" << std::setw(2) << m_month << "/" << std::setw(2)
117 << m_day << " " << std::setw(2) << m_hour << ":" << std::setw(2) << m_minute << ":" << std::setw(2)
118 << m_second << " UTC";
119
120 return output.str();
121}

References time_is_set().

Referenced by to_string(), and Botan::X509_Certificate::to_string().

◆ time_is_set()

bool Botan::ASN1_Time::time_is_set ( ) const

Return if the time has been set somehow.

Definition at line 123 of file asn1_time.cpp.

123 {
124 return (m_year != 0);
125}

Referenced by cmp(), readable_string(), Botan::Certificate_Store_In_SQL::revoke_cert(), and to_string().

◆ time_since_epoch()

uint64_t Botan::ASN1_Time::time_since_epoch ( ) const

Return time since epoch.

Definition at line 271 of file asn1_time.cpp.

271 {
272 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).seconds_since_epoch();
273}

References Botan::calendar_point::seconds_since_epoch().

Referenced by ASN1_Time(), botan_x509_crl_entry_revocation_date(), and from_seconds_since_epoch().

◆ to_std_timepoint()

std::chrono::system_clock::time_point Botan::ASN1_Time::to_std_timepoint ( ) const

Returns a STL timepoint object.

Definition at line 267 of file asn1_time.cpp.

267 {
268 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
269}

References Botan::calendar_point::to_std_timepoint().

Referenced by ASN1_Time().

◆ to_string()

std::string Botan::ASN1_Time::to_string ( ) const

Return an internal string representation of the time.

Definition at line 76 of file asn1_time.cpp.

76 {
77 if(!time_is_set()) {
78 throw Invalid_State("ASN1_Time::to_string: No time set");
79 }
80
81 uint32_t full_year = m_year;
82
83 if(m_tag == ASN1_Type::UtcTime) {
84 if(m_year < 1950 || m_year >= 2050) {
85 throw Encoding_Error(fmt("ASN_Time: The time {} cannot be encoded as UTCTime", readable_string()));
86 }
87
88 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
89 }
90
91 const uint64_t year_factor = 10000000000;
92 const uint64_t mon_factor = 100000000;
93 const uint64_t day_factor = 1000000;
94 const uint64_t hour_factor = 10000;
95 const uint64_t min_factor = 100;
96
97 const uint64_t int_repr = year_factor * full_year + mon_factor * m_month + day_factor * m_day +
98 hour_factor * m_hour + min_factor * m_minute + m_second;
99
100 const std::string repr = std::to_string(int_repr) + "Z";
101
102 const size_t desired_size = (m_tag == ASN1_Type::UtcTime) ? 13 : 15;
103
104 const std::string zero_padding(desired_size - repr.size(), '0');
105
106 return zero_padding + repr;
107}
std::string readable_string() const
Returns a human friendly string representation of no particular formatting.

References Botan::fmt(), readable_string(), time_is_set(), and Botan::UtcTime.

Referenced by encode_into().


The documentation for this class was generated from the following files: