Botan 2.19.1
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::ASN1_Time Class Referencefinal

#include <asn1_obj.h>

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

Public Member Functions

 ASN1_Time ()=default
 Create an invalid ASN1_Time. More...
 
 ASN1_Time (const std::chrono::system_clock::time_point &time)
 Create a ASN1_Time from a time point. More...
 
 ASN1_Time (const std::string &t_spec, ASN1_Tag tag)
 Create an ASN1_Time from string. More...
 
std::vector< uint8_t > BER_encode () const
 
int32_t cmp (const ASN1_Time &other) const
 Compare this time against another. More...
 
void decode_from (BER_Decoder &) override
 
void encode_into (DER_Encoder &) const override
 DER encode a ASN1_Time. More...
 
std::string readable_string () const
 Returns a human friendly string replesentation of no particular formatting. More...
 
bool time_is_set () const
 Return if the time has been set somehow. More...
 
uint64_t time_since_epoch () const
 Return time since epoch. More...
 
std::chrono::system_clock::time_point to_std_timepoint () const
 Returns a STL timepoint object. More...
 
std::string to_string () const
 Return an internal string representation of the time. More...
 

Detailed Description

Time (GeneralizedTime/UniversalTime)

Definition at line 327 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ ASN1_Time() [1/3]

Botan::ASN1_Time::ASN1_Time ( )
default

Create an invalid ASN1_Time.

◆ ASN1_Time() [2/3]

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 19 of file asn1_time.cpp.

20 {
21 calendar_point cal = calendar_value(time);
22
23 m_year = cal.get_year();
24 m_month = cal.get_month();
25 m_day = cal.get_day();
26 m_hour = cal.get_hour();
27 m_minute = cal.get_minutes();
28 m_second = cal.get_seconds();
29
30 m_tag = (m_year >= 2050) ? GENERALIZED_TIME : UTC_TIME;
31 }
@ UTC_TIME
Definition: asn1_obj.h:54
@ GENERALIZED_TIME
Definition: asn1_obj.h:55
calendar_point calendar_value(const std::chrono::system_clock::time_point &time_point)
Definition: calendar.cpp:111

References Botan::calendar_value(), Botan::GENERALIZED_TIME, Botan::calendar_point::get_day(), Botan::calendar_point::get_hour(), Botan::calendar_point::get_minutes(), Botan::calendar_point::get_month(), Botan::calendar_point::get_seconds(), Botan::calendar_point::get_year(), and Botan::UTC_TIME.

◆ ASN1_Time() [3/3]

Botan::ASN1_Time::ASN1_Time ( const std::string &  t_spec,
ASN1_Tag  tag 
)

Create an ASN1_Time from string.

Definition at line 33 of file asn1_time.cpp.

34 {
35 set_to(t_spec, tag);
36 }

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 16 of file asn1_obj.cpp.

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

References Botan::ASN1_Object::encode_into().

Referenced by Botan::PSSR::config_for_x509(), 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(), 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 118 of file asn1_time.cpp.

119 {
120 if(time_is_set() == false)
121 throw Invalid_State("ASN1_Time::cmp: No time set");
122
123 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
124
125 if(m_year < other.m_year) return EARLIER;
126 if(m_year > other.m_year) return LATER;
127 if(m_month < other.m_month) return EARLIER;
128 if(m_month > other.m_month) return LATER;
129 if(m_day < other.m_day) return EARLIER;
130 if(m_day > other.m_day) return LATER;
131 if(m_hour < other.m_hour) return EARLIER;
132 if(m_hour > other.m_hour) return LATER;
133 if(m_minute < other.m_minute) return EARLIER;
134 if(m_minute > other.m_minute) return LATER;
135 if(m_second < other.m_second) return EARLIER;
136 if(m_second > other.m_second) return LATER;
137
138 return SAME_TIME;
139 }
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:113

References 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 46 of file asn1_time.cpp.

47 {
48 BER_Object ber_time = source.get_next_object();
49
50 set_to(ASN1::to_string(ber_time), ber_time.type());
51 }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213

References Botan::BER_Decoder::get_next_object(), Botan::ASN1::to_string(), and Botan::BER_Object::type().

◆ encode_into()

void Botan::ASN1_Time::encode_into ( DER_Encoder der) const
overridevirtual

DER encode a ASN1_Time.

Implements Botan::ASN1_Object.

Definition at line 38 of file asn1_time.cpp.

39 {
40 BOTAN_ARG_CHECK(m_tag == UTC_TIME || m_tag == GENERALIZED_TIME,
41 "ASN1_Time: Bad encoding tag");
42
43 der.add_object(m_tag, UNIVERSAL, to_string());
44 }
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
std::string to_string() const
Return an internal string representation of the time.
Definition: asn1_time.cpp:53
@ UNIVERSAL
Definition: asn1_obj.h:26

References Botan::DER_Encoder::add_object(), BOTAN_ARG_CHECK, Botan::GENERALIZED_TIME, to_string(), Botan::UNIVERSAL, and Botan::UTC_TIME.

◆ readable_string()

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

Returns a human friendly string replesentation of no particular formatting.

Definition at line 93 of file asn1_time.cpp.

94 {
95 if(time_is_set() == false)
96 throw Invalid_State("ASN1_Time::readable_string: No time set");
97
98 // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
99 std::stringstream output;
100 output << std::setfill('0')
101 << std::setw(4) << m_year << "/"
102 << std::setw(2) << m_month << "/"
103 << std::setw(2) << m_day
104 << " "
105 << std::setw(2) << m_hour << ":"
106 << std::setw(2) << m_minute << ":"
107 << std::setw(2) << m_second
108 << " UTC";
109
110 return output.str();
111 }

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 113 of file asn1_time.cpp.

114 {
115 return (m_year != 0);
116 }

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 266 of file asn1_time.cpp.

267 {
268 auto tp = this->to_std_timepoint();
269 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
270 }
std::chrono::system_clock::time_point to_std_timepoint() const
Returns a STL timepoint object.
Definition: asn1_time.cpp:261

References to_std_timepoint().

◆ to_std_timepoint()

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

Returns a STL timepoint object.

Definition at line 261 of file asn1_time.cpp.

262 {
263 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
264 }

References Botan::calendar_point::to_std_timepoint().

Referenced by time_since_epoch().

◆ to_string()

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

Return an internal string representation of the time.

Definition at line 53 of file asn1_time.cpp.

54 {
55 if(time_is_set() == false)
56 throw Invalid_State("ASN1_Time::to_string: No time set");
57
58 uint32_t full_year = m_year;
59
60 if(m_tag == UTC_TIME)
61 {
62 if(m_year < 1950 || m_year >= 2050)
63 throw Encoding_Error("ASN1_Time: The time " + readable_string() +
64 " cannot be encoded as a UTCTime");
65
66 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
67 }
68
69 const uint64_t YEAR_FACTOR = 10000000000ULL;
70 const uint64_t MON_FACTOR = 100000000;
71 const uint64_t DAY_FACTOR = 1000000;
72 const uint64_t HOUR_FACTOR = 10000;
73 const uint64_t MIN_FACTOR = 100;
74
75 const uint64_t int_repr =
76 YEAR_FACTOR * full_year +
77 MON_FACTOR * m_month +
78 DAY_FACTOR * m_day +
79 HOUR_FACTOR * m_hour +
80 MIN_FACTOR * m_minute +
81 m_second;
82
83 std::string repr = std::to_string(int_repr) + "Z";
84
85 uint32_t desired_size = (m_tag == UTC_TIME) ? 13 : 15;
86
87 while(repr.size() < desired_size)
88 repr = "0" + repr;
89
90 return repr;
91 }
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition: asn1_time.cpp:93

References readable_string(), time_is_set(), Botan::ASN1::to_string(), and Botan::UTC_TIME.

Referenced by encode_into(), and Botan::X509_Certificate::subject_info().


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