Coverage Report

Created: 2025-07-11 06:15

/src/Botan-3.4.0/src/lib/asn1/asn1_time.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* X.509 Time Types
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/asn1_obj.h>
9
10
#include <botan/ber_dec.h>
11
#include <botan/der_enc.h>
12
#include <botan/exceptn.h>
13
#include <botan/internal/calendar.h>
14
#include <botan/internal/fmt.h>
15
#include <botan/internal/parsing.h>
16
#include <iomanip>
17
18
namespace Botan {
19
20
0
ASN1_Time::ASN1_Time(const std::chrono::system_clock::time_point& time) {
21
0
   calendar_point cal(time);
22
23
0
   m_year = cal.year();
24
0
   m_month = cal.month();
25
0
   m_day = cal.day();
26
0
   m_hour = cal.hour();
27
0
   m_minute = cal.minutes();
28
0
   m_second = cal.seconds();
29
30
0
   m_tag = (m_year >= 2050) ? ASN1_Type::GeneralizedTime : ASN1_Type::UtcTime;
31
0
}
32
33
0
ASN1_Time::ASN1_Time(std::string_view t_spec, ASN1_Type tag) {
34
0
   set_to(t_spec, tag);
35
0
}
36
37
0
ASN1_Time::ASN1_Time(std::string_view t_spec) {
38
0
   if(t_spec.size() == 13) {
39
0
      set_to(t_spec, ASN1_Type::UtcTime);
40
0
   } else if(t_spec.size() == 15) {
41
0
      set_to(t_spec, ASN1_Type::GeneralizedTime);
42
0
   } else {
43
0
      throw Invalid_Argument("Time string could not be parsed as GeneralizedTime or UTCTime.");
44
0
   }
45
0
}
46
47
0
void ASN1_Time::encode_into(DER_Encoder& der) const {
48
0
   BOTAN_ARG_CHECK(m_tag == ASN1_Type::UtcTime || m_tag == ASN1_Type::GeneralizedTime, "ASN1_Time: Bad encoding tag");
49
50
0
   der.add_object(m_tag, ASN1_Class::Universal, to_string());
51
0
}
52
53
0
void ASN1_Time::decode_from(BER_Decoder& source) {
54
0
   BER_Object ber_time = source.get_next_object();
55
56
0
   set_to(ASN1::to_string(ber_time), ber_time.type());
57
0
}
58
59
0
std::string ASN1_Time::to_string() const {
60
0
   if(time_is_set() == false) {
61
0
      throw Invalid_State("ASN1_Time::to_string: No time set");
62
0
   }
63
64
0
   uint32_t full_year = m_year;
65
66
0
   if(m_tag == ASN1_Type::UtcTime) {
67
0
      if(m_year < 1950 || m_year >= 2050) {
68
0
         throw Encoding_Error(fmt("ASN_Time: The time {} cannot be encoded as UTCTime", readable_string()));
69
0
      }
70
71
0
      full_year = (m_year >= 2000) ? (m_year - 2000) : (m_year - 1900);
72
0
   }
73
74
0
   const uint64_t year_factor = 10000000000;
75
0
   const uint64_t mon_factor = 100000000;
76
0
   const uint64_t day_factor = 1000000;
77
0
   const uint64_t hour_factor = 10000;
78
0
   const uint64_t min_factor = 100;
79
80
0
   const uint64_t int_repr = year_factor * full_year + mon_factor * m_month + day_factor * m_day +
81
0
                             hour_factor * m_hour + min_factor * m_minute + m_second;
82
83
0
   std::string repr = std::to_string(int_repr) + "Z";
84
85
0
   const size_t desired_size = (m_tag == ASN1_Type::UtcTime) ? 13 : 15;
86
87
0
   const std::string zero_padding(desired_size - repr.size(), '0');
88
89
0
   return zero_padding + repr;
90
0
}
91
92
0
std::string ASN1_Time::readable_string() const {
93
0
   if(time_is_set() == false) {
94
0
      throw Invalid_State("ASN1_Time::readable_string: No time set");
95
0
   }
96
97
   // desired format: "%04d/%02d/%02d %02d:%02d:%02d UTC"
98
0
   std::stringstream output;
99
0
   output << std::setfill('0') << std::setw(4) << m_year << "/" << std::setw(2) << m_month << "/" << std::setw(2)
100
0
          << m_day << " " << std::setw(2) << m_hour << ":" << std::setw(2) << m_minute << ":" << std::setw(2)
101
0
          << m_second << " UTC";
102
103
0
   return output.str();
104
0
}
105
106
0
bool ASN1_Time::time_is_set() const {
107
0
   return (m_year != 0);
108
0
}
109
110
0
int32_t ASN1_Time::cmp(const ASN1_Time& other) const {
111
0
   if(!time_is_set() || !other.time_is_set()) {
112
0
      throw Invalid_State("ASN1_Time::cmp: Cannot compare empty times");
113
0
   }
114
115
0
   const int32_t EARLIER = -1, LATER = 1, SAME_TIME = 0;
116
117
0
   if(m_year < other.m_year) {
118
0
      return EARLIER;
119
0
   }
120
0
   if(m_year > other.m_year) {
121
0
      return LATER;
122
0
   }
123
0
   if(m_month < other.m_month) {
124
0
      return EARLIER;
125
0
   }
126
0
   if(m_month > other.m_month) {
127
0
      return LATER;
128
0
   }
129
0
   if(m_day < other.m_day) {
130
0
      return EARLIER;
131
0
   }
132
0
   if(m_day > other.m_day) {
133
0
      return LATER;
134
0
   }
135
0
   if(m_hour < other.m_hour) {
136
0
      return EARLIER;
137
0
   }
138
0
   if(m_hour > other.m_hour) {
139
0
      return LATER;
140
0
   }
141
0
   if(m_minute < other.m_minute) {
142
0
      return EARLIER;
143
0
   }
144
0
   if(m_minute > other.m_minute) {
145
0
      return LATER;
146
0
   }
147
0
   if(m_second < other.m_second) {
148
0
      return EARLIER;
149
0
   }
150
0
   if(m_second > other.m_second) {
151
0
      return LATER;
152
0
   }
153
154
0
   return SAME_TIME;
155
0
}
156
157
0
void ASN1_Time::set_to(std::string_view t_spec, ASN1_Type spec_tag) {
158
0
   BOTAN_ARG_CHECK(spec_tag == ASN1_Type::UtcTime || spec_tag == ASN1_Type::GeneralizedTime,
159
0
                   "Invalid tag for ASN1_Time");
160
161
0
   if(spec_tag == ASN1_Type::GeneralizedTime) {
162
0
      BOTAN_ARG_CHECK(t_spec.size() == 15, "Invalid GeneralizedTime input string");
163
0
   } else if(spec_tag == ASN1_Type::UtcTime) {
164
0
      BOTAN_ARG_CHECK(t_spec.size() == 13, "Invalid UTCTime input string");
165
0
   }
166
167
0
   BOTAN_ARG_CHECK(t_spec.back() == 'Z', "Botan does not support ASN1 times with timezones other than Z");
168
169
0
   const size_t field_len = 2;
170
171
0
   const size_t year_start = 0;
172
0
   const size_t year_len = (spec_tag == ASN1_Type::UtcTime) ? 2 : 4;
173
0
   const size_t month_start = year_start + year_len;
174
0
   const size_t day_start = month_start + field_len;
175
0
   const size_t hour_start = day_start + field_len;
176
0
   const size_t min_start = hour_start + field_len;
177
0
   const size_t sec_start = min_start + field_len;
178
179
0
   m_year = to_u32bit(t_spec.substr(year_start, year_len));
180
0
   m_month = to_u32bit(t_spec.substr(month_start, field_len));
181
0
   m_day = to_u32bit(t_spec.substr(day_start, field_len));
182
0
   m_hour = to_u32bit(t_spec.substr(hour_start, field_len));
183
0
   m_minute = to_u32bit(t_spec.substr(min_start, field_len));
184
0
   m_second = to_u32bit(t_spec.substr(sec_start, field_len));
185
0
   m_tag = spec_tag;
186
187
0
   if(spec_tag == ASN1_Type::UtcTime) {
188
0
      if(m_year >= 50) {
189
0
         m_year += 1900;
190
0
      } else {
191
0
         m_year += 2000;
192
0
      }
193
0
   }
194
195
0
   if(!passes_sanity_check()) {
196
0
      throw Invalid_Argument(fmt("ASN1_Time string '{}' does not seem to be valid", t_spec));
197
0
   }
198
0
}
199
200
/*
201
* Do a general sanity check on the time
202
*/
203
0
bool ASN1_Time::passes_sanity_check() const {
204
   // AppVeyor's trust store includes a cert with expiration date in 3016 ...
205
0
   if(m_year < 1950 || m_year > 3100) {
206
0
      return false;
207
0
   }
208
0
   if(m_month == 0 || m_month > 12) {
209
0
      return false;
210
0
   }
211
212
0
   const uint32_t days_in_month[12] = {31, 28 + 1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
213
214
0
   if(m_day == 0 || m_day > days_in_month[m_month - 1]) {
215
0
      return false;
216
0
   }
217
218
0
   if(m_month == 2 && m_day == 29) {
219
0
      if(m_year % 4 != 0) {
220
0
         return false;  // not a leap year
221
0
      }
222
223
0
      if(m_year % 100 == 0 && m_year % 400 != 0) {
224
0
         return false;
225
0
      }
226
0
   }
227
228
0
   if(m_hour >= 24 || m_minute >= 60 || m_second > 60) {
229
0
      return false;
230
0
   }
231
232
0
   if(m_tag == ASN1_Type::UtcTime) {
233
      /*
234
      UTCTime limits the value of components such that leap seconds
235
      are not covered. See "UNIVERSAL 23" in "Information technology
236
      Abstract Syntax Notation One (ASN.1): Specification of basic notation"
237
238
      http://www.itu.int/ITU-T/studygroups/com17/languages/
239
      */
240
0
      if(m_second > 59) {
241
0
         return false;
242
0
      }
243
0
   }
244
245
0
   return true;
246
0
}
247
248
0
std::chrono::system_clock::time_point ASN1_Time::to_std_timepoint() const {
249
0
   return calendar_point(m_year, m_month, m_day, m_hour, m_minute, m_second).to_std_timepoint();
250
0
}
251
252
0
uint64_t ASN1_Time::time_since_epoch() const {
253
0
   auto tp = this->to_std_timepoint();
254
0
   return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
255
0
}
256
257
/*
258
* Compare two ASN1_Times for in various ways
259
*/
260
0
bool operator==(const ASN1_Time& t1, const ASN1_Time& t2) {
261
0
   return (t1.cmp(t2) == 0);
262
0
}
263
264
0
bool operator!=(const ASN1_Time& t1, const ASN1_Time& t2) {
265
0
   return (t1.cmp(t2) != 0);
266
0
}
267
268
0
bool operator<=(const ASN1_Time& t1, const ASN1_Time& t2) {
269
0
   return (t1.cmp(t2) <= 0);
270
0
}
271
272
0
bool operator>=(const ASN1_Time& t1, const ASN1_Time& t2) {
273
0
   return (t1.cmp(t2) >= 0);
274
0
}
275
276
0
bool operator<(const ASN1_Time& t1, const ASN1_Time& t2) {
277
0
   return (t1.cmp(t2) < 0);
278
0
}
279
280
0
bool operator>(const ASN1_Time& t1, const ASN1_Time& t2) {
281
0
   return (t1.cmp(t2) > 0);
282
0
}
283
284
}  // namespace Botan