Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::X509_Time Class Referencefinal

`#include <asn1_time.h>`

Inheritance diagram for Botan::X509_Time:

## Public Member Functions
std::vector< uint8_t > | BER_encode () const
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...
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...
| 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 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 }
Botan::calendar_value
calendar_point calendar_value(const std::chrono::system_clock::time_point &time_point)
Definition: calendar.cpp:111
Botan::GENERALIZED_TIME
Definition: asn1_obj.h:53
Botan::UTC_TIME
Definition: asn1_obj.h:52

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

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.

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().

17 {
18 std::vector<uint8_t> output;
19 DER_Encoder der(output);
20 this->encode_into(der);
21 return output;
22 }
Botan::ASN1_Object::encode_into
virtual void encode_into(DER_Encoder &to) const =0

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 time_is_set().

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 }
Botan::X509_Time::time_is_set
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:113

void Botan::X509_Time::decode_from | ( | BER_Decoder & | from | ) |
| override virtual

Decode whatever this object is from from

Parameters

  from | the 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().

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

void Botan::X509_Time::encode_into | ( | DER_Encoder & | der | ) | const
| override virtual

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_ARG_CHECK, Botan::GENERALIZED_TIME, to_string(), Botan::UNIVERSAL, and Botan::UTC_TIME.

39 {
40 BOTAN_ARG_CHECK(m_tag == UTC_TIME || m_tag == GENERALIZED_TIME,
41 "X509_Time: Bad encoding tag");
42
43 der.add_object(m_tag, UNIVERSAL, to_string());
44 }
Botan::UNIVERSAL
Definition: asn1_obj.h:24
Botan::GENERALIZED_TIME
Definition: asn1_obj.h:53
BOTAN_ARG_CHECK
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
Botan::X509_Time::to_string
std::string to_string() const
Return an internal string representation of the time.
Definition: asn1_time.cpp:53
Botan::UTC_TIME
Definition: asn1_obj.h:52

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 to_string(), and Botan::X509_Certificate::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 }
Botan::X509_Time::time_is_set
bool time_is_set() const
Return if the time has been set somehow.
Definition: asn1_time.cpp:113

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 }

uint64_t Botan::X509_Time::time_since_epoch | ( |  | ) | const

Return time since epoch.

Definition at line 266 of file asn1_time.cpp.

References to_std_timepoint().

Referenced by botan_x509_cert_not_after(), and botan_x509_cert_not_before().

267 {
268 auto tp = this->to_std_timepoint();
269 return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
270 }
Botan::X509_Time::to_std_timepoint
std::chrono::system_clock::time_point to_std_timepoint() const
Returns a STL timepoint object.
Definition: asn1_time.cpp:261

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

Returns a STL timepoint object.

Definition at line 261 of file asn1_time.cpp.

References Botan::calendar_point::to_std_timepoint().

Referenced by time_since_epoch().

262 {
263 return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
264 }

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(), and Botan::X509_Certificate::subject_info().

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

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

- src/lib/asn1/asn1_time.h
- src/lib/asn1/asn1_time.cpp