Botan 3.9.0
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.
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 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.

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 360 of file asn1_obj.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 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 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 116 of file asn1_time.cpp.

116 {
117 if(!time_is_set() || !other.time_is_set()) {
118 throw Invalid_State("ASN1_Time::cmp: Cannot compare empty times");
119 }
120
121 constexpr int32_t EARLIER = -1;
122 constexpr int32_t LATER = 1;
123 constexpr int32_t SAME_TIME = 0;
124
125 if(m_year < other.m_year) {
126 return EARLIER;
127 }
128 if(m_year > other.m_year) {
129 return LATER;
130 }
131 if(m_month < other.m_month) {
132 return EARLIER;
133 }
134 if(m_month > other.m_month) {
135 return LATER;
136 }
137 if(m_day < other.m_day) {
138 return EARLIER;
139 }
140 if(m_day > other.m_day) {
141 return LATER;
142 }
143 if(m_hour < other.m_hour) {
144 return EARLIER;
145 }
146 if(m_hour > other.m_hour) {
147 return LATER;
148 }
149 if(m_minute < other.m_minute) {
150 return EARLIER;
151 }
152 if(m_minute > other.m_minute) {
153 return LATER;
154 }
155 if(m_second < other.m_second) {
156 return EARLIER;
157 }
158 if(m_second > other.m_second) {
159 return LATER;
160 }
161
162 return SAME_TIME;
163}
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 BER_Object ber_time = source.get_next_object();
61
62 set_to(ASN1::to_string(ber_time), ber_time.type());
63}
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 & 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:65

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 replesentation of no particular formatting.

Definition at line 98 of file asn1_time.cpp.

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

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

112 {
113 return (m_year != 0);
114}

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

260 {
261 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).seconds_since_epoch();
262}

References Botan::calendar_point::seconds_since_epoch().

Referenced by ASN1_Time(), 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 256 of file asn1_time.cpp.

256 {
257 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
258}

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

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