Botan 3.8.1
Crypto and TLS for C&
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.
 
 ASN1_Time (const std::chrono::system_clock::time_point &time)
 Create a ASN1_Time from a time point.
 
 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 &) override
 
void encode_into (DER_Encoder &) const override
 DER encode a ASN1_Time.
 
std::string readable_string () const
 Returns a human friendly string replesentation 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.
 

Detailed Description

Time (GeneralizedTime/UniversalTime)

Definition at line 356 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ ASN1_Time() [1/4]

Botan::ASN1_Time::ASN1_Time ( )
default

Create an invalid ASN1_Time.

References ASN1_Time(), Botan::NoObject, time_since_epoch(), and to_std_timepoint().

Referenced by ASN1_Time(), and cmp().

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

21 {
22 calendar_point cal(time);
23
24 m_year = cal.year();
25 m_month = cal.month();
26 m_day = cal.day();
27 m_hour = cal.hour();
28 m_minute = cal.minutes();
29 m_second = cal.seconds();
30
31 m_tag = (m_year >= 2050) ? ASN1_Type::GeneralizedTime : ASN1_Type::UtcTime;
32}
ASN1_Type
Definition asn1_obj.h:44

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

38 {
39 if(t_spec.size() == 13) {
40 set_to(t_spec, ASN1_Type::UtcTime);
41 } else if(t_spec.size() == 15) {
42 set_to(t_spec, ASN1_Type::GeneralizedTime);
43 } else {
44 throw Invalid_Argument("Time string could not be parsed as GeneralizedTime or UTCTime.");
45 }
46}

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

19 {
20 std::vector<uint8_t> output;
21 DER_Encoder der(output);
22 this->encode_into(der);
23 return output;
24}
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 111 of file asn1_time.cpp.

111 {
112 if(!time_is_set() || !other.time_is_set()) {
113 throw Invalid_State("ASN1_Time::cmp: Cannot compare empty times");
114 }
115
116 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
117
118 if(m_year < other.m_year) {
119 return EARLIER;
120 }
121 if(m_year > other.m_year) {
122 return LATER;
123 }
124 if(m_month < other.m_month) {
125 return EARLIER;
126 }
127 if(m_month > other.m_month) {
128 return LATER;
129 }
130 if(m_day < other.m_day) {
131 return EARLIER;
132 }
133 if(m_day > other.m_day) {
134 return LATER;
135 }
136 if(m_hour < other.m_hour) {
137 return EARLIER;
138 }
139 if(m_hour > other.m_hour) {
140 return LATER;
141 }
142 if(m_minute < other.m_minute) {
143 return EARLIER;
144 }
145 if(m_minute > other.m_minute) {
146 return LATER;
147 }
148 if(m_second < other.m_second) {
149 return EARLIER;
150 }
151 if(m_second > other.m_second) {
152 return LATER;
153 }
154
155 return SAME_TIME;
156}
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 54 of file asn1_time.cpp.

54 {
55 BER_Object ber_time = source.get_next_object();
56
57 set_to(ASN1::to_string(ber_time), ber_time.type());
58}
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:185

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

48 {
49 BOTAN_ARG_CHECK(m_tag == ASN1_Type::UtcTime || m_tag == ASN1_Type::GeneralizedTime, "ASN1_Time: Bad encoding tag");
50
51 der.add_object(m_tag, ASN1_Class::Universal, to_string());
52}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:31
std::string to_string() const
Return an internal string representation of the time.
Definition asn1_time.cpp:60

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

◆ 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.

93 {
94 if(time_is_set() == false) {
95 throw Invalid_State("ASN1_Time::readable_string: No time set");
96 }
97
98 // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
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";
103
104 return output.str();
105}

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

107 {
108 return (m_year != 0);
109}

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

253 {
254 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).seconds_since_epoch();
255}

References Botan::calendar_point::seconds_since_epoch().

Referenced by ASN1_Time().

◆ to_std_timepoint()

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

Returns a STL timepoint object.

Definition at line 249 of file asn1_time.cpp.

249 {
250 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
251}

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

60 {
61 if(time_is_set() == false) {
62 throw Invalid_State("ASN1_Time::to_string: No time set");
63 }
64
65 uint32_t full_year = m_year;
66
67 if(m_tag == ASN1_Type::UtcTime) {
68 if(m_year < 1950 || m_year >= 2050) {
69 throw Encoding_Error(fmt("ASN_Time: The time {} cannot be encoded as UTCTime", readable_string()));
70 }
71
72 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
73 }
74
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;
80
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;
83
84 std::string repr = std::to_string(int_repr) + "Z";
85
86 const size_t desired_size = (m_tag == ASN1_Type::UtcTime) ? 13 : 15;
87
88 const std::string zero_padding(desired_size - repr.size(), '0');
89
90 return zero_padding + repr;
91}
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition asn1_time.cpp:93
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

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: