Botan 3.3.0
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.
 
 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 341 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ ASN1_Time() [1/4]

Botan::ASN1_Time::ASN1_Time ( )
default

Create an invalid ASN1_Time.

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

20 {
21 calendar_point cal(time);
22
23 m_year = cal.year();
24 m_month = cal.month();
25 m_day = cal.day();
26 m_hour = cal.hour();
27 m_minute = cal.minutes();
28 m_second = cal.seconds();
29
30 m_tag = (m_year >= 2050) ? ASN1_Type::GeneralizedTime : ASN1_Type::UtcTime;
31}
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 37 of file asn1_time.cpp.

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

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

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

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 Botan::ASN1_Object::encode_into().

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

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

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

53 {
54 BER_Object ber_time = source.get_next_object();
55
56 set_to(ASN1::to_string(ber_time), ber_time.type());
57}
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 47 of file asn1_time.cpp.

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

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

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

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

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

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

252 {
253 auto tp = this->to_std_timepoint();
254 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
255}
std::chrono::system_clock::time_point to_std_timepoint() const
Returns a STL timepoint object.

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

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

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

59 {
60 if(time_is_set() == false) {
61 throw Invalid_State("ASN1_Time::to_string: No time set");
62 }
63
64 uint32_t full_year = m_year;
65
66 if(m_tag == ASN1_Type::UtcTime) {
67 if(m_year < 1950 || m_year >= 2050) {
68 throw Encoding_Error(fmt("ASN_Time: The time {} cannot be encoded as UTCTime", readable_string()));
69 }
70
71 full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
72 }
73
74 const uint64_t year_factor = 10000000000;
75 const uint64_t mon_factor = 100000000;
76 const uint64_t day_factor = 1000000;
77 const uint64_t hour_factor = 10000;
78 const uint64_t min_factor = 100;
79
80 const uint64_t int_repr = year_factor * full_year + mon_factor * m_month + day_factor * m_day +
81 hour_factor * m_hour + min_factor * m_minute + m_second;
82
83 std::string repr = std::to_string(int_repr) + "Z";
84
85 const size_t desired_size = (m_tag == ASN1_Type::UtcTime) ? 13 : 15;
86
87 const std::string zero_padding(desired_size - repr.size(), '0');
88
89 return zero_padding + repr;
90}
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition asn1_time.cpp:92
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: