/src/openssl32/crypto/asn1/a_time.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /*- |
11 | | * This is an implementation of the ASN1 Time structure which is: |
12 | | * Time ::= CHOICE { |
13 | | * utcTime UTCTime, |
14 | | * generalTime GeneralizedTime } |
15 | | */ |
16 | | |
17 | | #include <stdio.h> |
18 | | #include <time.h> |
19 | | #include "crypto/asn1.h" |
20 | | #include "crypto/ctype.h" |
21 | | #include "internal/cryptlib.h" |
22 | | #include <openssl/asn1t.h> |
23 | | #include "asn1_local.h" |
24 | | |
25 | | IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME) |
26 | | |
27 | | IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME) |
28 | | IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME) |
29 | | |
30 | | static int is_utc(const int year) |
31 | 10.0k | { |
32 | 10.0k | if (50 <= year && year <= 149) |
33 | 10.0k | return 1; |
34 | 0 | return 0; |
35 | 10.0k | } |
36 | | |
37 | | static int leap_year(const int year) |
38 | 140k | { |
39 | 140k | if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) |
40 | 41.5k | return 1; |
41 | 99.3k | return 0; |
42 | 140k | } |
43 | | |
44 | | /* |
45 | | * Compute the day of the week and the day of the year from the year, month |
46 | | * and day. The day of the year is straightforward, the day of the week uses |
47 | | * a form of Zeller's congruence. For this months start with March and are |
48 | | * numbered 4 through 15. |
49 | | */ |
50 | | static void determine_days(struct tm *tm) |
51 | 195k | { |
52 | 195k | static const int ydays[12] = { |
53 | 195k | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
54 | 195k | }; |
55 | 195k | int y = tm->tm_year + 1900; |
56 | 195k | int m = tm->tm_mon; |
57 | 195k | int d = tm->tm_mday; |
58 | 195k | int c; |
59 | | |
60 | 195k | tm->tm_yday = ydays[m] + d - 1; |
61 | 195k | if (m >= 2) { |
62 | | /* March and onwards can be one day further into the year */ |
63 | 112k | tm->tm_yday += leap_year(y); |
64 | 112k | m += 2; |
65 | 112k | } else { |
66 | | /* Treat January and February as part of the previous year */ |
67 | 82.8k | m += 14; |
68 | 82.8k | y--; |
69 | 82.8k | } |
70 | 195k | c = y / 100; |
71 | 195k | y %= 100; |
72 | | /* Zeller's congruence */ |
73 | 195k | tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7; |
74 | 195k | } |
75 | | |
76 | | int ossl_asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d) |
77 | 242k | { |
78 | 242k | static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 }; |
79 | 242k | static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 }; |
80 | 242k | static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
81 | 242k | char *a; |
82 | 242k | int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md; |
83 | 242k | struct tm tmp; |
84 | | #if defined(CHARSET_EBCDIC) |
85 | | const char upper_z = 0x5A, num_zero = 0x30, period = 0x2E, minus = 0x2D, plus = 0x2B; |
86 | | #else |
87 | 242k | const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+'; |
88 | 242k | #endif |
89 | | /* |
90 | | * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280 |
91 | | * time string format, in which: |
92 | | * |
93 | | * 1. "seconds" is a 'MUST' |
94 | | * 2. "Zulu" timezone is a 'MUST' |
95 | | * 3. "+|-" is not allowed to indicate a timezone |
96 | | */ |
97 | 242k | if (d->type == V_ASN1_UTCTIME) { |
98 | 91.1k | if (d->flags & ASN1_STRING_FLAG_X509_TIME) { |
99 | 0 | min_l = 13; |
100 | 0 | strict = 1; |
101 | 0 | } |
102 | 151k | } else if (d->type == V_ASN1_GENERALIZEDTIME) { |
103 | 151k | end = 7; |
104 | 151k | btz = 6; |
105 | 151k | if (d->flags & ASN1_STRING_FLAG_X509_TIME) { |
106 | 0 | min_l = 15; |
107 | 0 | strict = 1; |
108 | 151k | } else { |
109 | 151k | min_l = 13; |
110 | 151k | } |
111 | 151k | } else { |
112 | 0 | return 0; |
113 | 0 | } |
114 | | |
115 | 242k | l = d->length; |
116 | 242k | a = (char *)d->data; |
117 | 242k | o = 0; |
118 | 242k | memset(&tmp, 0, sizeof(tmp)); |
119 | | |
120 | | /* |
121 | | * GENERALIZEDTIME is similar to UTCTIME except the year is represented |
122 | | * as YYYY. This stuff treats everything as a two digit field so make |
123 | | * first two fields 00 to 99 |
124 | | */ |
125 | | |
126 | 242k | if (l < min_l) |
127 | 77.0k | goto err; |
128 | 1.03M | for (i = 0; i < end; i++) { |
129 | 948k | if (!strict && (i == btz) && ((a[o] == upper_z) || (a[o] == plus) || (a[o] == minus))) { |
130 | 31.3k | i++; |
131 | 31.3k | break; |
132 | 31.3k | } |
133 | 916k | if (!ossl_ascii_isdigit(a[o])) |
134 | 15.8k | goto err; |
135 | 900k | n = a[o] - num_zero; |
136 | | /* incomplete 2-digital number */ |
137 | 900k | if (++o == l) |
138 | 2.93k | goto err; |
139 | | |
140 | 898k | if (!ossl_ascii_isdigit(a[o])) |
141 | 14.3k | goto err; |
142 | 883k | n = (n * 10) + a[o] - num_zero; |
143 | | /* no more bytes to read, but we haven't seen time-zone yet */ |
144 | 883k | if (++o == l) |
145 | 1.41k | goto err; |
146 | | |
147 | 882k | i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i; |
148 | | |
149 | 882k | if ((n < min[i2]) || (n > max[i2])) |
150 | 9.45k | goto err; |
151 | 872k | switch (i2) { |
152 | 103k | case 0: |
153 | | /* UTC will never be here */ |
154 | 103k | tmp.tm_year = n * 100 - 1900; |
155 | 103k | break; |
156 | 151k | case 1: |
157 | 151k | if (d->type == V_ASN1_UTCTIME) |
158 | 51.3k | tmp.tm_year = n < 50 ? n + 100 : n; |
159 | 100k | else |
160 | 100k | tmp.tm_year += n; |
161 | 151k | break; |
162 | 140k | case 2: |
163 | 140k | tmp.tm_mon = n - 1; |
164 | 140k | break; |
165 | 136k | case 3: |
166 | | /* check if tm_mday is valid in tm_mon */ |
167 | 136k | if (tmp.tm_mon == 1) { |
168 | | /* it's February */ |
169 | 20.3k | md = mdays[1] + leap_year(tmp.tm_year + 1900); |
170 | 116k | } else { |
171 | 116k | md = mdays[tmp.tm_mon]; |
172 | 116k | } |
173 | 136k | if (n > md) |
174 | 2.71k | goto err; |
175 | 134k | tmp.tm_mday = n; |
176 | 134k | determine_days(&tmp); |
177 | 134k | break; |
178 | 127k | case 4: |
179 | 127k | tmp.tm_hour = n; |
180 | 127k | break; |
181 | 125k | case 5: |
182 | 125k | tmp.tm_min = n; |
183 | 125k | break; |
184 | 87.3k | case 6: |
185 | 87.3k | tmp.tm_sec = n; |
186 | 87.3k | break; |
187 | 872k | } |
188 | 872k | } |
189 | | |
190 | | /* |
191 | | * Optional fractional seconds: decimal point followed by one or more |
192 | | * digits. |
193 | | */ |
194 | 118k | if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == period) { |
195 | 55.6k | if (strict) |
196 | | /* RFC 5280 forbids fractional seconds */ |
197 | 0 | goto err; |
198 | 55.6k | if (++o == l) |
199 | 848 | goto err; |
200 | 54.8k | i = o; |
201 | 236k | while ((o < l) && ossl_ascii_isdigit(a[o])) |
202 | 181k | o++; |
203 | | /* Must have at least one digit after decimal point */ |
204 | 54.8k | if (i == o) |
205 | 1.27k | goto err; |
206 | | /* no more bytes to read, but we haven't seen time-zone yet */ |
207 | 53.5k | if (o == l) |
208 | 529 | goto err; |
209 | 53.5k | } |
210 | | |
211 | | /* |
212 | | * 'o' will never point to '\0' at this point, the only chance |
213 | | * 'o' can point to '\0' is either the subsequent if or the first |
214 | | * else if is true. |
215 | | */ |
216 | 116k | if (a[o] == upper_z) { |
217 | 82.8k | o++; |
218 | 82.8k | } else if (!strict && ((a[o] == plus) || (a[o] == minus))) { |
219 | 29.1k | int offsign = a[o] == minus ? 1 : -1; |
220 | 29.1k | int offset = 0; |
221 | | |
222 | 29.1k | o++; |
223 | | /* |
224 | | * if not equal, no need to do subsequent checks |
225 | | * since the following for-loop will add 'o' by 4 |
226 | | * and the final return statement will check if 'l' |
227 | | * and 'o' are equal. |
228 | | */ |
229 | 29.1k | if (o + 4 != l) |
230 | 2.07k | goto err; |
231 | 68.8k | for (i = end; i < end + 2; i++) { |
232 | 49.6k | if (!ossl_ascii_isdigit(a[o])) |
233 | 2.74k | goto err; |
234 | 46.8k | n = a[o] - num_zero; |
235 | 46.8k | o++; |
236 | 46.8k | if (!ossl_ascii_isdigit(a[o])) |
237 | 3.35k | goto err; |
238 | 43.5k | n = (n * 10) + a[o] - num_zero; |
239 | 43.5k | i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i; |
240 | 43.5k | if ((n < min[i2]) || (n > max[i2])) |
241 | 1.77k | goto err; |
242 | | /* if tm is NULL, no need to adjust */ |
243 | 41.7k | if (tm != NULL) { |
244 | 41.7k | if (i == end) |
245 | 22.5k | offset = n * 3600; |
246 | 19.2k | else if (i == end + 1) |
247 | 19.2k | offset += n * 60; |
248 | 41.7k | } |
249 | 41.7k | o++; |
250 | 41.7k | } |
251 | 19.2k | if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign)) |
252 | 1.77k | goto err; |
253 | 19.2k | } else { |
254 | | /* not Z, or not +/- in non-strict mode */ |
255 | 4.01k | goto err; |
256 | 4.01k | } |
257 | 100k | if (o == l) { |
258 | | /* success, check if tm should be filled */ |
259 | 97.0k | if (tm != NULL) |
260 | 71.8k | *tm = tmp; |
261 | 97.0k | return 1; |
262 | 97.0k | } |
263 | 145k | err: |
264 | 145k | return 0; |
265 | 100k | } |
266 | | |
267 | | ASN1_TIME *ossl_asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type) |
268 | 40.9k | { |
269 | 40.9k | char* p; |
270 | 40.9k | ASN1_TIME *tmps = NULL; |
271 | 40.9k | const size_t len = 20; |
272 | | |
273 | 40.9k | if (type == V_ASN1_UNDEF) { |
274 | 6.79k | if (is_utc(ts->tm_year)) |
275 | 6.79k | type = V_ASN1_UTCTIME; |
276 | 0 | else |
277 | 0 | type = V_ASN1_GENERALIZEDTIME; |
278 | 34.1k | } else if (type == V_ASN1_UTCTIME) { |
279 | 0 | if (!is_utc(ts->tm_year)) |
280 | 0 | goto err; |
281 | 34.1k | } else if (type != V_ASN1_GENERALIZEDTIME) { |
282 | 0 | goto err; |
283 | 0 | } |
284 | | |
285 | 40.9k | if (s == NULL) |
286 | 6.79k | tmps = ASN1_STRING_new(); |
287 | 34.1k | else |
288 | 34.1k | tmps = s; |
289 | 40.9k | if (tmps == NULL) |
290 | 0 | return NULL; |
291 | | |
292 | 40.9k | if (!ASN1_STRING_set(tmps, NULL, len)) |
293 | 0 | goto err; |
294 | | |
295 | 40.9k | tmps->type = type; |
296 | 40.9k | p = (char*)tmps->data; |
297 | | |
298 | 40.9k | if (ts->tm_mon > INT_MAX - 1) |
299 | 0 | goto err; |
300 | | |
301 | 40.9k | if (type == V_ASN1_GENERALIZEDTIME) { |
302 | 34.1k | if (ts->tm_year > INT_MAX - 1900) |
303 | 0 | goto err; |
304 | 34.1k | tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", |
305 | 34.1k | ts->tm_year + 1900, ts->tm_mon + 1, |
306 | 34.1k | ts->tm_mday, ts->tm_hour, ts->tm_min, |
307 | 34.1k | ts->tm_sec); |
308 | 34.1k | } else { |
309 | 6.79k | tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", |
310 | 6.79k | ts->tm_year % 100, ts->tm_mon + 1, |
311 | 6.79k | ts->tm_mday, ts->tm_hour, ts->tm_min, |
312 | 6.79k | ts->tm_sec); |
313 | 6.79k | } |
314 | | |
315 | | #ifdef CHARSET_EBCDIC |
316 | | ebcdic2ascii(tmps->data, tmps->data, tmps->length); |
317 | | #endif |
318 | 40.9k | return tmps; |
319 | 0 | err: |
320 | 0 | if (tmps != s) |
321 | 0 | ASN1_STRING_free(tmps); |
322 | 0 | return NULL; |
323 | 40.9k | } |
324 | | |
325 | | ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) |
326 | 0 | { |
327 | 0 | return ASN1_TIME_adj(s, t, 0, 0); |
328 | 0 | } |
329 | | |
330 | | ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, |
331 | | int offset_day, long offset_sec) |
332 | 10.0k | { |
333 | 10.0k | struct tm *ts; |
334 | 10.0k | struct tm data; |
335 | | |
336 | 10.0k | ts = OPENSSL_gmtime(&t, &data); |
337 | 10.0k | if (ts == NULL) { |
338 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME); |
339 | 0 | return NULL; |
340 | 0 | } |
341 | 10.0k | if (offset_day || offset_sec) { |
342 | 0 | if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) |
343 | 0 | return NULL; |
344 | 0 | } |
345 | 10.0k | return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF); |
346 | 10.0k | } |
347 | | |
348 | | int ASN1_TIME_check(const ASN1_TIME *t) |
349 | 0 | { |
350 | 0 | if (t->type == V_ASN1_GENERALIZEDTIME) |
351 | 0 | return ASN1_GENERALIZEDTIME_check(t); |
352 | 0 | else if (t->type == V_ASN1_UTCTIME) |
353 | 0 | return ASN1_UTCTIME_check(t); |
354 | 0 | return 0; |
355 | 0 | } |
356 | | |
357 | | /* Convert an ASN1_TIME structure to GeneralizedTime */ |
358 | | ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t, |
359 | | ASN1_GENERALIZEDTIME **out) |
360 | 0 | { |
361 | 0 | ASN1_GENERALIZEDTIME *ret = NULL; |
362 | 0 | struct tm tm; |
363 | |
|
364 | 0 | if (!ASN1_TIME_to_tm(t, &tm)) |
365 | 0 | return NULL; |
366 | | |
367 | 0 | if (out != NULL) |
368 | 0 | ret = *out; |
369 | |
|
370 | 0 | ret = ossl_asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME); |
371 | |
|
372 | 0 | if (out != NULL && ret != NULL) |
373 | 0 | *out = ret; |
374 | |
|
375 | 0 | return ret; |
376 | 0 | } |
377 | | |
378 | | int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) |
379 | 0 | { |
380 | | /* Try UTC, if that fails, try GENERALIZED */ |
381 | 0 | if (ASN1_UTCTIME_set_string(s, str)) |
382 | 0 | return 1; |
383 | 0 | return ASN1_GENERALIZEDTIME_set_string(s, str); |
384 | 0 | } |
385 | | |
386 | | int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str) |
387 | 0 | { |
388 | 0 | ASN1_TIME t; |
389 | 0 | struct tm tm; |
390 | 0 | int rv = 0; |
391 | |
|
392 | 0 | t.length = strlen(str); |
393 | 0 | t.data = (unsigned char *)str; |
394 | 0 | t.flags = ASN1_STRING_FLAG_X509_TIME; |
395 | |
|
396 | 0 | t.type = V_ASN1_UTCTIME; |
397 | |
|
398 | 0 | if (!ASN1_TIME_check(&t)) { |
399 | 0 | t.type = V_ASN1_GENERALIZEDTIME; |
400 | 0 | if (!ASN1_TIME_check(&t)) |
401 | 0 | goto out; |
402 | 0 | } |
403 | | |
404 | | /* |
405 | | * Per RFC 5280 (section 4.1.2.5.), the valid input time |
406 | | * strings should be encoded with the following rules: |
407 | | * |
408 | | * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ |
409 | | * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ |
410 | | * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ |
411 | | * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ |
412 | | * |
413 | | * Only strings of the 4th rule should be reformatted, but since a |
414 | | * UTC can only present [1950, 2050), so if the given time string |
415 | | * is less than 1950 (e.g. 19230419000000Z), we do nothing... |
416 | | */ |
417 | | |
418 | 0 | if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) { |
419 | 0 | if (!ossl_asn1_time_to_tm(&tm, &t)) |
420 | 0 | goto out; |
421 | 0 | if (is_utc(tm.tm_year)) { |
422 | 0 | t.length -= 2; |
423 | | /* |
424 | | * it's OK to let original t.data go since that's assigned |
425 | | * to a piece of memory allocated outside of this function. |
426 | | * new t.data would be freed after ASN1_STRING_copy is done. |
427 | | */ |
428 | 0 | t.data = OPENSSL_zalloc(t.length + 1); |
429 | 0 | if (t.data == NULL) |
430 | 0 | goto out; |
431 | 0 | memcpy(t.data, str + 2, t.length); |
432 | 0 | t.type = V_ASN1_UTCTIME; |
433 | 0 | } |
434 | 0 | } |
435 | | |
436 | 0 | if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t)) |
437 | 0 | rv = 1; |
438 | |
|
439 | 0 | if (t.data != (unsigned char *)str) |
440 | 0 | OPENSSL_free(t.data); |
441 | 0 | out: |
442 | 0 | return rv; |
443 | 0 | } |
444 | | |
445 | | int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm) |
446 | 18.7k | { |
447 | 18.7k | if (s == NULL) { |
448 | 0 | time_t now_t; |
449 | |
|
450 | 0 | time(&now_t); |
451 | 0 | memset(tm, 0, sizeof(*tm)); |
452 | 0 | if (OPENSSL_gmtime(&now_t, tm) != NULL) |
453 | 0 | return 1; |
454 | 0 | return 0; |
455 | 0 | } |
456 | | |
457 | 18.7k | return ossl_asn1_time_to_tm(tm, s); |
458 | 18.7k | } |
459 | | |
460 | | int ASN1_TIME_diff(int *pday, int *psec, |
461 | | const ASN1_TIME *from, const ASN1_TIME *to) |
462 | 10.2k | { |
463 | 10.2k | struct tm tm_from, tm_to; |
464 | | |
465 | 10.2k | if (!ASN1_TIME_to_tm(from, &tm_from)) |
466 | 1.77k | return 0; |
467 | 8.46k | if (!ASN1_TIME_to_tm(to, &tm_to)) |
468 | 37 | return 0; |
469 | 8.42k | return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to); |
470 | 8.46k | } |
471 | | |
472 | | static const char _asn1_mon[12][4] = { |
473 | | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
474 | | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
475 | | }; |
476 | | |
477 | | /* prints the time with the default date format (RFC 822) */ |
478 | | int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) |
479 | 209k | { |
480 | 209k | return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822); |
481 | 209k | } |
482 | | |
483 | | /* returns 1 on success, 0 on BIO write error or parse failure */ |
484 | | int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags) |
485 | 209k | { |
486 | 209k | return ossl_asn1_time_print_ex(bp, tm, flags) > 0; |
487 | 209k | } |
488 | | |
489 | | |
490 | | /* prints the time with the date format of ISO 8601 */ |
491 | | /* returns 0 on BIO write error, else -1 in case of parse failure, else 1 */ |
492 | | int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags) |
493 | 263k | { |
494 | 263k | char *v; |
495 | 263k | int gmt = 0, l; |
496 | 263k | struct tm stm; |
497 | 263k | const char upper_z = 0x5A, period = 0x2E; |
498 | | |
499 | | /* ossl_asn1_time_to_tm will check the time type */ |
500 | 263k | if (!ossl_asn1_time_to_tm(&stm, tm)) |
501 | 176k | return BIO_write(bp, "Bad time value", 14) ? -1 : 0; |
502 | | |
503 | 86.9k | l = tm->length; |
504 | 86.9k | v = (char *)tm->data; |
505 | 86.9k | if (v[l - 1] == upper_z) |
506 | 66.4k | gmt = 1; |
507 | | |
508 | 86.9k | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
509 | 59.4k | char *f = NULL; |
510 | 59.4k | int f_len = 0; |
511 | | |
512 | | /* |
513 | | * Try to parse fractional seconds. '14' is the place of |
514 | | * 'fraction point' in a GeneralizedTime string. |
515 | | */ |
516 | 59.4k | if (tm->length > 15 && v[14] == period) { |
517 | 39.2k | f = &v[14]; |
518 | 39.2k | f_len = 1; |
519 | 183k | while (14 + f_len < l && ossl_ascii_isdigit(f[f_len])) |
520 | 144k | ++f_len; |
521 | 39.2k | } |
522 | | |
523 | 59.4k | if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) { |
524 | 0 | return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d%.*s%s", |
525 | 0 | stm.tm_year + 1900, stm.tm_mon + 1, |
526 | 0 | stm.tm_mday, stm.tm_hour, |
527 | 0 | stm.tm_min, stm.tm_sec, f_len, f, |
528 | 0 | (gmt ? "Z" : "")) > 0; |
529 | 0 | } |
530 | 59.4k | else { |
531 | 59.4k | return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s", |
532 | 59.4k | _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour, |
533 | 59.4k | stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900, |
534 | 59.4k | (gmt ? " GMT" : "")) > 0; |
535 | 59.4k | } |
536 | 59.4k | } else { |
537 | 27.5k | if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) { |
538 | 0 | return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d%s", |
539 | 0 | stm.tm_year + 1900, stm.tm_mon + 1, |
540 | 0 | stm.tm_mday, stm.tm_hour, |
541 | 0 | stm.tm_min, stm.tm_sec, |
542 | 0 | (gmt ? "Z" : "")) > 0; |
543 | 0 | } |
544 | 27.5k | else { |
545 | 27.5k | return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", |
546 | 27.5k | _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour, |
547 | 27.5k | stm.tm_min, stm.tm_sec, stm.tm_year + 1900, |
548 | 27.5k | (gmt ? " GMT" : "")) > 0; |
549 | 27.5k | } |
550 | 27.5k | } |
551 | 86.9k | } |
552 | | |
553 | | int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t) |
554 | 0 | { |
555 | 0 | struct tm stm, ttm; |
556 | 0 | int day, sec; |
557 | |
|
558 | 0 | if (!ASN1_TIME_to_tm(s, &stm)) |
559 | 0 | return -2; |
560 | | |
561 | 0 | if (!OPENSSL_gmtime(&t, &ttm)) |
562 | 0 | return -2; |
563 | | |
564 | 0 | if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) |
565 | 0 | return -2; |
566 | | |
567 | 0 | if (day > 0 || sec > 0) |
568 | 0 | return 1; |
569 | 0 | if (day < 0 || sec < 0) |
570 | 0 | return -1; |
571 | 0 | return 0; |
572 | 0 | } |
573 | | |
574 | | int ASN1_TIME_normalize(ASN1_TIME *t) |
575 | 0 | { |
576 | 0 | struct tm tm; |
577 | |
|
578 | 0 | if (t == NULL || !ASN1_TIME_to_tm(t, &tm)) |
579 | 0 | return 0; |
580 | | |
581 | 0 | return ossl_asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL; |
582 | 0 | } |
583 | | |
584 | | int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b) |
585 | 224 | { |
586 | 224 | int day, sec; |
587 | | |
588 | 224 | if (!ASN1_TIME_diff(&day, &sec, b, a)) |
589 | 125 | return -2; |
590 | 99 | if (day > 0 || sec > 0) |
591 | 23 | return 1; |
592 | 76 | if (day < 0 || sec < 0) |
593 | 73 | return -1; |
594 | 3 | return 0; |
595 | 76 | } |
596 | | |
597 | | /* |
598 | | * tweak for Windows |
599 | | */ |
600 | | #ifdef WIN32 |
601 | | # define timezone _timezone |
602 | | #endif |
603 | | |
604 | | #if defined(__FreeBSD__) || defined(__wasi__) |
605 | | # define USE_TIMEGM |
606 | | #endif |
607 | | |
608 | | time_t ossl_asn1_string_to_time_t(const char *asn1_string) |
609 | 0 | { |
610 | 0 | ASN1_TIME *timestamp_asn1 = NULL; |
611 | 0 | struct tm *timestamp_tm = NULL; |
612 | | #if defined(__DJGPP__) |
613 | | char *tz = NULL; |
614 | | #elif !defined(USE_TIMEGM) |
615 | | time_t timestamp_local; |
616 | 0 | #endif |
617 | 0 | time_t timestamp_utc; |
618 | |
|
619 | 0 | timestamp_asn1 = ASN1_TIME_new(); |
620 | 0 | if (!ASN1_TIME_set_string(timestamp_asn1, asn1_string)) |
621 | 0 | { |
622 | 0 | ASN1_TIME_free(timestamp_asn1); |
623 | 0 | return -1; |
624 | 0 | } |
625 | | |
626 | 0 | timestamp_tm = OPENSSL_malloc(sizeof(*timestamp_tm)); |
627 | 0 | if (timestamp_tm == NULL) { |
628 | 0 | ASN1_TIME_free(timestamp_asn1); |
629 | 0 | return -1; |
630 | 0 | } |
631 | 0 | if (!(ASN1_TIME_to_tm(timestamp_asn1, timestamp_tm))) { |
632 | 0 | OPENSSL_free(timestamp_tm); |
633 | 0 | ASN1_TIME_free(timestamp_asn1); |
634 | 0 | return -1; |
635 | 0 | } |
636 | 0 | ASN1_TIME_free(timestamp_asn1); |
637 | |
|
638 | | #if defined(__DJGPP__) |
639 | | /* |
640 | | * This is NOT thread-safe. Do not use this method for platforms other |
641 | | * than djgpp. |
642 | | */ |
643 | | tz = getenv("TZ"); |
644 | | if (tz != NULL) { |
645 | | tz = OPENSSL_strdup(tz); |
646 | | if (tz == NULL) { |
647 | | OPENSSL_free(timestamp_tm); |
648 | | return -1; |
649 | | } |
650 | | } |
651 | | setenv("TZ", "UTC", 1); |
652 | | |
653 | | timestamp_utc = mktime(timestamp_tm); |
654 | | |
655 | | if (tz != NULL) { |
656 | | setenv("TZ", tz, 1); |
657 | | OPENSSL_free(tz); |
658 | | } else { |
659 | | unsetenv("TZ"); |
660 | | } |
661 | | #elif defined(USE_TIMEGM) |
662 | | timestamp_utc = timegm(timestamp_tm); |
663 | | #else |
664 | 0 | timestamp_local = mktime(timestamp_tm); |
665 | 0 | timestamp_utc = timestamp_local - timezone; |
666 | 0 | #endif |
667 | 0 | OPENSSL_free(timestamp_tm); |
668 | |
|
669 | 0 | return timestamp_utc; |
670 | 0 | } |