Botan 3.0.0-alpha0
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)
 Create an ASN1_Time from string. More...
 
 ASN1_Time (const std::string &t_spec, ASN1_Type tag)
 Create an ASN1_Time from string and a specified tagging (Utc or Generalized) 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/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 19 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 }

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 ( const std::string &  t_spec)

Create an ASN1_Time from string.

Definition at line 38 of file asn1_time.cpp.

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

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

◆ ASN1_Time() [4/4]

Botan::ASN1_Time::ASN1_Time ( const std::string &  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.

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

129 {
130 if(time_is_set() == false)
131 throw Invalid_State("ASN1_Time::cmp: No time set");
132
133 const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
134
135 if(m_year < other.m_year) return EARLIER;
136 if(m_year > other.m_year) return LATER;
137 if(m_month < other.m_month) return EARLIER;
138 if(m_month > other.m_month) return LATER;
139 if(m_day < other.m_day) return EARLIER;
140 if(m_day > other.m_day) return LATER;
141 if(m_hour < other.m_hour) return EARLIER;
142 if(m_hour > other.m_hour) return LATER;
143 if(m_minute < other.m_minute) return EARLIER;
144 if(m_minute > other.m_minute) return LATER;
145 if(m_second < other.m_second) return EARLIER;
146 if(m_second > other.m_second) return LATER;
147
148 return SAME_TIME;
149 }
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:123

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

57 {
58 BER_Object ber_time = source.get_next_object();
59
60 set_to(ASN1::to_string(ber_time), ber_time.type());
61 }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209

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.

49 {
51 "ASN1_Time: Bad encoding tag");
52
53 der.add_object(m_tag, ASN1_Class::Universal, to_string());
54 }
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:36
std::string to_string() const
Return an internal string representation of the time.
Definition: asn1_time.cpp:63

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

104 {
105 if(time_is_set() == false)
106 throw Invalid_State("ASN1_Time::readable_string: No time set");
107
108 // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
109 std::stringstream output;
110 output << std::setfill('0')
111 << std::setw(4) << m_year << "/"
112 << std::setw(2) << m_month << "/"
113 << std::setw(2) << m_day
114 << " "
115 << std::setw(2) << m_hour << ":"
116 << std::setw(2) << m_minute << ":"
117 << std::setw(2) << m_second
118 << " 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.

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

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

256 {
257 auto tp = this->to_std_timepoint();
258 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
259 }
std::chrono::system_clock::time_point to_std_timepoint() const
Returns a STL timepoint object.
Definition: asn1_time.cpp:250

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

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

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

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

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

Referenced by encode_into().


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