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

Botan::X509_Time::X509_Time ( )
default

Create an invalid X509_Time.

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

References Botan::calendar_value(), Botan::calendar_point::day, Botan::GENERALIZED_TIME, Botan::calendar_point::hour, Botan::calendar_point::minutes, Botan::calendar_point::month, Botan::calendar_point::seconds, Botan::UTC_TIME, and Botan::calendar_point::year.

21  {
22  calendar_point cal = calendar_value(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) ? GENERALIZED_TIME : UTC_TIME;
32  }
calendar_point calendar_value(const std::chrono::system_clock::time_point &time_point)
Definition: calendar.cpp:177
Botan::X509_Time::X509_Time ( const std::string &  t_spec,
ASN1_Tag  tag 
)

Create an X509_Time from string.

Definition at line 34 of file asn1_time.cpp.

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

Member Function Documentation

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

Compare this time against another.

Definition at line 120 of file asn1_time.cpp.

References time_is_set().

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

121  {
122  if(time_is_set() == false)
123  throw Invalid_State("X509_Time::cmp: No time set");
124 
125  const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
126 
127  if(m_year < other.m_year) return EARLIER;
128  if(m_year > other.m_year) return LATER;
129  if(m_month < other.m_month) return EARLIER;
130  if(m_month > other.m_month) return LATER;
131  if(m_day < other.m_day) return EARLIER;
132  if(m_day > other.m_day) return LATER;
133  if(m_hour < other.m_hour) return EARLIER;
134  if(m_hour > other.m_hour) return LATER;
135  if(m_minute < other.m_minute) return EARLIER;
136  if(m_minute > other.m_minute) return LATER;
137  if(m_second < other.m_second) return EARLIER;
138  if(m_second > other.m_second) return LATER;
139 
140  return SAME_TIME;
141  }
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:115
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 50 of file asn1_time.cpp.

References Botan::BER_Decoder::get_next_object(), Botan::LATIN1_CHARSET, Botan::LOCAL_CHARSET, Botan::ASN1::to_string(), Botan::Charset::transcode(), and Botan::BER_Object::type_tag.

51  {
52  BER_Object ber_time = source.get_next_object();
53 
54  set_to(Charset::transcode(ASN1::to_string(ber_time),
57  ber_time.type_tag);
58  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
void Botan::X509_Time::encode_into ( DER_Encoder der) const
overridevirtual

DER encode a X509_Time.

Implements Botan::ASN1_Object.

Definition at line 39 of file asn1_time.cpp.

References Botan::DER_Encoder::add_object(), Botan::GENERALIZED_TIME, Botan::LATIN1_CHARSET, Botan::LOCAL_CHARSET, to_string(), Botan::Charset::transcode(), Botan::UNIVERSAL, and Botan::UTC_TIME.

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

40  {
41  if(m_tag != GENERALIZED_TIME && m_tag != UTC_TIME)
42  throw Invalid_Argument("X509_Time: Bad encoding tag");
43 
44  der.add_object(m_tag, UNIVERSAL,
48  }
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
std::string to_string() const
Return an internal string representation of the time.
Definition: asn1_time.cpp:60
std::string Botan::X509_Time::readable_string ( ) const

Returns a human friendly string replesentation of no particular formatting.

Definition at line 97 of file asn1_time.cpp.

References time_is_set().

Referenced by to_string().

98  {
99  if(time_is_set() == false)
100  throw Invalid_State("X509_Time::readable_string: No time set");
101 
102  // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
103  std::stringstream output;
104  {
105  using namespace std;
106  output << setfill('0')
107  << setw(4) << m_year << "/" << setw(2) << m_month << "/" << setw(2) << m_day
108  << " "
109  << setw(2) << m_hour << ":" << setw(2) << m_minute << ":" << setw(2) << m_second
110  << " UTC";
111  }
112  return output.str();
113  }
Definition: bigint.h:619
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:115
bool Botan::X509_Time::time_is_set ( ) const

Return if the time has been set somehow.

Definition at line 115 of file asn1_time.cpp.

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

116  {
117  return (m_year != 0);
118  }
std::chrono::system_clock::time_point Botan::X509_Time::to_std_timepoint ( ) const

Returns a STL timepoint object.

Definition at line 253 of file asn1_time.cpp.

References Botan::calendar_point::to_std_timepoint().

254  {
255  return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
256  }
std::string Botan::X509_Time::to_string ( ) const

Return an internal string representation of the time.

Definition at line 60 of file asn1_time.cpp.

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

Referenced by encode_into(), and Botan::X509_CRL::X509_CRL().

61  {
62  if(time_is_set() == false)
63  throw Invalid_State("X509_Time::as_string: No time set");
64 
65  uint32_t full_year = m_year;
66 
67  if(m_tag == UTC_TIME)
68  {
69  if(m_year < 1950 || m_year >= 2050)
70  throw Encoding_Error("X509_Time: The time " + readable_string() +
71  " cannot be encoded as a UTCTime");
72 
73  full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
74  }
75 
76  const auto factor_y = uint64_t{10000000000ull}; // literal exceeds 32bit int range
77  const auto factor_m = uint64_t{100000000ull};
78  const auto factor_d = uint64_t{1000000ull};
79  const auto factor_h = uint64_t{10000ull};
80  const auto factor_i = uint64_t{100ull};
81 
82  std::string repr = std::to_string(factor_y * full_year +
83  factor_m * m_month +
84  factor_d * m_day +
85  factor_h * m_hour +
86  factor_i * m_minute +
87  m_second) + "Z";
88 
89  uint32_t desired_size = (m_tag == UTC_TIME) ? 13 : 15;
90 
91  while(repr.size() < desired_size)
92  repr = "0" + repr;
93 
94  return repr;
95  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:115
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition: asn1_time.cpp:97

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