Botan  2.4.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::X509_Time Class Referencefinal

#include <asn1_time.h>

Inheritance diagram for Botan::X509_Time:
Botan::ASN1_Object

Public Member Functions

int32_t cmp (const X509_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 X509_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...
 
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...
 
 X509_Time ()=default
 Create an invalid X509_Time. More...
 
 X509_Time (const std::chrono::system_clock::time_point &time)
 Create a X509_Time from a time point. More...
 
 X509_Time (const std::string &t_spec, ASN1_Tag tag)
 Create an X509_Time from string. More...
 

Detailed Description

X.509 Time

Definition at line 19 of file asn1_time.h.

Constructor & Destructor Documentation

◆ X509_Time() [1/3]

Botan::X509_Time::X509_Time ( )
default

Create an invalid X509_Time.

◆ X509_Time() [2/3]

Botan::X509_Time::X509_Time ( const std::chrono::system_clock::time_point &  time)
explicit

Create a X509_Time from a time point.

Definition at line 19 of file asn1_time.cpp.

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.

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  }
calendar_point calendar_value(const std::chrono::system_clock::time_point &time_point)
Definition: calendar.cpp:106

◆ X509_Time() [3/3]

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

Create an X509_Time from string.

Definition at line 33 of file asn1_time.cpp.

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

Member Function Documentation

◆ cmp()

int32_t Botan::X509_Time::cmp ( const X509_Time other) const

Compare this time against another.

Definition at line 118 of file asn1_time.cpp.

References BOTAN_ASSERT, Botan::GENERALIZED_TIME, time_is_set(), Botan::to_u32bit(), Botan::UTC_OR_GENERALIZED_TIME, and Botan::UTC_TIME.

Referenced by Botan::operator!=(), Botan::operator<(), Botan::operator<=(), Botan::operator==(), Botan::operator>(), and Botan::operator>=().

119  {
120  if(time_is_set() == false)
121  throw Invalid_State("X509_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

◆ decode_from()

void Botan::X509_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.

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

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

◆ encode_into()

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

DER encode a X509_Time.

Implements Botan::ASN1_Object.

Definition at line 38 of file asn1_time.cpp.

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

Referenced by Botan::Certificate_Store_In_SQL::revoke_cert().

39  {
40  if(m_tag != GENERALIZED_TIME && m_tag != UTC_TIME)
41  throw Invalid_Argument("X509_Time: Bad encoding tag");
42 
43  der.add_object(m_tag, UNIVERSAL, to_string());
44  }
std::string to_string() const
Return an internal string representation of the time.
Definition: asn1_time.cpp:53

◆ readable_string()

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

Returns a human friendly string replesentation of no particular formatting.

Definition at line 93 of file asn1_time.cpp.

References time_is_set().

Referenced by Botan::ASN1_Formatter::print_to_stream(), and to_string().

94  {
95  if(time_is_set() == false)
96  throw Invalid_State("X509_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  }
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:113

◆ time_is_set()

bool Botan::X509_Time::time_is_set ( ) const

Return if the time has been set somehow.

Definition at line 113 of file asn1_time.cpp.

Referenced by cmp(), readable_string(), Botan::Certificate_Store_In_SQL::revoke_cert(), and to_string().

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

◆ to_std_timepoint()

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

Returns a STL timepoint object.

Definition at line 251 of file asn1_time.cpp.

References Botan::calendar_point::to_std_timepoint().

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

◆ to_string()

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

Return an internal string representation of the time.

Definition at line 53 of file asn1_time.cpp.

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

Referenced by encode_into().

54  {
55  if(time_is_set() == false)
56  throw Invalid_State("X509_Time::as_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("X509_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 to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:113
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition: asn1_time.cpp:93

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