/src/Botan-3.13.0/build/include/public/botan/asn1_time.h
Line | Count | Source |
1 | | /* |
2 | | * (C) 1999-2007,2018,2020,2026 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #ifndef BOTAN_ASN1_TIME_TYPE_H_ |
8 | | #define BOTAN_ASN1_TIME_TYPE_H_ |
9 | | |
10 | | #include <botan/asn1_obj.h> |
11 | | #include <chrono> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | /** |
16 | | * Time (GeneralizedTime/UniversalTime) |
17 | | */ |
18 | | class BOTAN_PUBLIC_API(2, 0) ASN1_Time final : public ASN1_Object { |
19 | | public: |
20 | | /// DER encode a ASN1_Time |
21 | | void encode_into(DER_Encoder& to) const override; |
22 | | |
23 | | // Decode a BER encoded ASN1_Time |
24 | | void decode_from(BER_Decoder& from) override; |
25 | | |
26 | | /// Return an internal string representation of the time |
27 | | std::string to_string() const; |
28 | | |
29 | | /// Returns a human friendly string representation of no particular formatting |
30 | | std::string readable_string() const; |
31 | | |
32 | | /// Return if the time has been set somehow |
33 | | bool time_is_set() const; |
34 | | |
35 | | /// Return the tag (UtcTime or GeneralizedTime) this time was encoded with |
36 | 0 | ASN1_Type tagging() const { return m_tag; } |
37 | | |
38 | | /// Compare this time against another |
39 | | int32_t cmp(const ASN1_Time& other) const; |
40 | | |
41 | | /// Create an invalid ASN1_Time |
42 | 0 | ASN1_Time() = default; |
43 | | |
44 | | /// Create an ASN1_Time from seconds since epoch |
45 | | static ASN1_Time from_seconds_since_epoch(uint64_t seconds); |
46 | | |
47 | | /// Create a ASN1_Time from a time point |
48 | | static ASN1_Time from_time_point(const std::chrono::system_clock::time_point& time); |
49 | | |
50 | | /// Create a ASN1_Time from a string |
51 | | /// |
52 | | /// Only the fixed 13 or 15 char RFC 5280 format (eg [YY]YYMMDDHHMMSSZ) is accepted, |
53 | | /// tag is set based on the use of 2 or 4 digit year, 2-digit years use the |
54 | | /// 1950 breakeven point (see RFC 5280 Section 4.1.2.5.1) |
55 | | static ASN1_Time from_string(std::string_view t_spec); |
56 | | |
57 | | /// Create a ASN1_Time from a string |
58 | | /// |
59 | | /// Only the fixed 13 or 15 char RFC 5280 format (eg [YY]YYMMDDHHMMSSZ) is accepted, |
60 | | /// based on the provided tag (which must be UtcTime or GeneralizedTime) |
61 | | static ASN1_Time from_string(std::string_view t_spec, ASN1_Type tag); |
62 | | |
63 | | /// Create a ASN1_Time from a time point |
64 | 0 | explicit ASN1_Time(const std::chrono::system_clock::time_point& time) { |
65 | 0 | *this = ASN1_Time::from_time_point(time); |
66 | 0 | } |
67 | | |
68 | | /// Create an ASN1_Time from string |
69 | 0 | BOTAN_FUTURE_EXPLICIT ASN1_Time(std::string_view t_spec) { *this = ASN1_Time::from_string(t_spec); } |
70 | | |
71 | | /// Create an ASN1_Time from string and a specified tagging (Utc or Generalized) |
72 | 0 | ASN1_Time(std::string_view t_spec, ASN1_Type tag) { *this = ASN1_Time::from_string(t_spec, tag); } |
73 | | |
74 | | /// Returns a STL timepoint object |
75 | | std::chrono::system_clock::time_point to_std_timepoint() const; |
76 | | |
77 | | /// Return time since epoch |
78 | | uint64_t time_since_epoch() const; |
79 | | |
80 | | private: |
81 | | ASN1_Time(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, ASN1_Type tag); |
82 | | |
83 | | uint16_t m_year = 0; // range 0-9999 |
84 | | uint8_t m_month = 0; // range 1-12 |
85 | | uint8_t m_day = 0; // range 1-31 |
86 | | uint8_t m_hour = 0; // range 0-23 |
87 | | uint8_t m_minute = 0; // range 0-59 |
88 | | uint8_t m_second = 0; // range 0-59 (leap seconds not supported) |
89 | | ASN1_Type m_tag = ASN1_Type::NoObject; |
90 | | }; |
91 | | |
92 | | /* |
93 | | * Comparison Operations |
94 | | * |
95 | | * These compare the instants represented, and ignore whether the times were |
96 | | * tagged as UtcTime or GeneralizedTime. |
97 | | */ |
98 | | |
99 | | /** |
100 | | * Compare two times |
101 | | * @return true if x is equal to y |
102 | | */ |
103 | | BOTAN_PUBLIC_API(2, 0) bool operator==(const ASN1_Time& x, const ASN1_Time& y); |
104 | | |
105 | | /** |
106 | | * Compare two times |
107 | | * @return true if x is not equal to y |
108 | | */ |
109 | | BOTAN_PUBLIC_API(2, 0) bool operator!=(const ASN1_Time& x, const ASN1_Time& y); |
110 | | |
111 | | /** |
112 | | * Compare two times |
113 | | * @return true if x is not later than y |
114 | | */ |
115 | | BOTAN_PUBLIC_API(2, 0) bool operator<=(const ASN1_Time& x, const ASN1_Time& y); |
116 | | |
117 | | /** |
118 | | * Compare two times |
119 | | * @return true if x is not earlier than y |
120 | | */ |
121 | | BOTAN_PUBLIC_API(2, 0) bool operator>=(const ASN1_Time& x, const ASN1_Time& y); |
122 | | |
123 | | /** |
124 | | * Compare two times |
125 | | * @return true if x is earlier than y |
126 | | */ |
127 | | BOTAN_PUBLIC_API(2, 0) bool operator<(const ASN1_Time& x, const ASN1_Time& y); |
128 | | |
129 | | /** |
130 | | * Compare two times |
131 | | * @return true if x is later than y |
132 | | */ |
133 | | BOTAN_PUBLIC_API(2, 0) bool operator>(const ASN1_Time& x, const ASN1_Time& y); |
134 | | |
135 | | } // namespace Botan |
136 | | |
137 | | #endif |