2 * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Tests for X509 time functions */
15 #include <openssl/asn1.h>
16 #include <openssl/x509.h>
18 #include "internal/nelem.h"
24 /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
30 /* 0 for check-only mode, 1 for set-string mode */
32 /* 0 for error, 1 if succeed */
35 * The following 2 fields are ignored if set_string field is set to '0'
36 * (in check only mode).
38 * But they can still be ignored explicitly in set-string mode by:
39 * setting -1 to expected_type and setting NULL to expected_string.
41 * It's useful in a case of set-string mode but the expected result
42 * is a 'parsing error'.
45 const char *expected_string;
49 * Actually, the "loose" mode has been tested in
50 * those time-compare-cases, so we may not test it again.
52 static TESTDATA_FORMAT x509_format_tests[] = {
55 /* good format, check only */
56 "20170217180105Z", 0, 1, -1, NULL,
59 /* not leap year, check only */
60 "20170229180105Z", 0, 0, -1, NULL,
63 /* leap year, check only */
64 "20160229180105Z", 0, 1, -1, NULL,
67 /* SS is missing, check only */
68 "201702171801Z", 0, 0, -1, NULL,
71 /* fractional seconds, check only */
72 "20170217180105.001Z", 0, 0, -1, NULL,
75 /* time zone, check only */
76 "20170217180105+0800", 0, 0, -1, NULL,
79 /* SS is missing, set string */
80 "201702171801Z", 1, 0, -1, NULL,
83 /* fractional seconds, set string */
84 "20170217180105.001Z", 1, 0, -1, NULL,
87 /* time zone, set string */
88 "20170217180105+0800", 1, 0, -1, NULL,
91 /* good format, check returned 'turned' string */
92 "20170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z",
95 /* good format, check returned string */
96 "20510217180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "20510217180154Z",
99 /* good format but out of UTC range, check returned string */
100 "19230419180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "19230419180154Z",
104 /* SS is missing, check only */
105 "1702171801Z", 0, 0, -1, NULL,
108 /* not leap year, check only */
109 "050229180101Z", 0, 0, -1, NULL,
112 /* leap year, check only */
113 "040229180101Z", 0, 1, -1, NULL,
116 /* time zone, check only */
117 "170217180154+0800", 0, 0, -1, NULL,
120 /* SS is missing, set string */
121 "1702171801Z", 1, 0, -1, NULL,
124 /* time zone, set string */
125 "170217180154+0800", 1, 0, -1, NULL,
128 /* 2017, good format, check returned string */
129 "170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z",
132 /* 1998, good format, check returned string */
133 "981223180154Z", 1, 1, V_ASN1_UTCTIME, "981223180154Z",
137 static TESTDATA x509_cmp_tests[] = {
139 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
140 /* The same in seconds since epoch. */
144 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
145 /* One second more. */
149 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
150 /* One second less. */
153 /* Same as UTC time. */
155 "170217180154Z", V_ASN1_UTCTIME,
156 /* The same in seconds since epoch. */
160 "170217180154Z", V_ASN1_UTCTIME,
161 /* One second more. */
165 "170217180154Z", V_ASN1_UTCTIME,
166 /* One second less. */
169 /* UTCTime from the 20th century. */
171 "990217180154Z", V_ASN1_UTCTIME,
172 /* The same in seconds since epoch. */
176 "990217180154Z", V_ASN1_UTCTIME,
177 /* One second more. */
181 "990217180154Z", V_ASN1_UTCTIME,
182 /* One second less. */
185 /* Various invalid formats. */
188 "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
191 /* No trailing Z, UTCTime. */
192 "170217180154", V_ASN1_UTCTIME, 0, 0,
196 "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
199 /* No seconds, UTCTime. */
200 "1702171801Z", V_ASN1_UTCTIME, 0, 0,
203 /* Fractional seconds. */
204 "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
207 /* Fractional seconds, UTCTime. */
208 "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
211 /* Timezone offset. */
212 "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
215 /* Timezone offset, UTCTime. */
216 "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
220 "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
223 /* Extra digits, UTCTime. */
224 "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
228 "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
231 /* Non-digits, UTCTime. */
232 "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
235 /* Trailing garbage. */
236 "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
239 /* Trailing garbage, UTCTime. */
240 "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
244 "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
248 "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
252 "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
256 static int test_x509_cmp_time(int idx)
261 memset(&t, 0, sizeof(t));
262 t.type = x509_cmp_tests[idx].type;
263 t.data = (unsigned char*)(x509_cmp_tests[idx].data);
264 t.length = strlen(x509_cmp_tests[idx].data);
267 result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
268 if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
269 TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n",
270 idx, x509_cmp_tests[idx].expected, result);
276 static int test_x509_cmp_time_current(void)
278 time_t now = time(NULL);
279 /* Pick a day earlier and later, relative to any system clock. */
280 ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
281 int cmp_result, failed = 0;
283 asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
284 asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
286 cmp_result = X509_cmp_time(asn1_before, NULL);
287 if (!TEST_int_eq(cmp_result, -1))
290 cmp_result = X509_cmp_time(asn1_after, NULL);
291 if (!TEST_int_eq(cmp_result, 1))
294 ASN1_TIME_free(asn1_before);
295 ASN1_TIME_free(asn1_after);
300 static int test_X509_cmp_timeframe_vpm(const X509_VERIFY_PARAM *vpm,
301 ASN1_TIME *asn1_before,
303 ASN1_TIME *asn1_after)
305 int always_0 = vpm != NULL
306 && (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_USE_CHECK_TIME) == 0
307 && (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_NO_CHECK_TIME) != 0;
309 return asn1_before != NULL && asn1_mid != NULL && asn1_after != NULL
310 && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_after), 0)
311 && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, NULL), 0)
312 && TEST_int_eq(X509_cmp_timeframe(vpm, NULL, asn1_after), 0)
313 && TEST_int_eq(X509_cmp_timeframe(vpm, NULL, NULL), 0)
314 && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_after),
316 && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_before),
318 && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_before),
322 static int test_X509_cmp_timeframe(void)
324 time_t now = time(NULL);
325 ASN1_TIME *asn1_mid = ASN1_TIME_adj(NULL, now, 0, 0);
326 /* Pick a day earlier and later, relative to any system clock. */
327 ASN1_TIME *asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
328 ASN1_TIME *asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
329 X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
334 res = test_X509_cmp_timeframe_vpm(NULL, asn1_before, asn1_mid, asn1_after)
335 && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after);
337 X509_VERIFY_PARAM_set_time(vpm, now);
339 && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after)
340 && X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME)
341 && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after);
343 X509_VERIFY_PARAM_free(vpm);
345 ASN1_TIME_free(asn1_mid);
346 ASN1_TIME_free(asn1_before);
347 ASN1_TIME_free(asn1_after);
352 static int test_x509_time(int idx)
357 if (x509_format_tests[idx].set_string) {
358 /* set-string mode */
361 TEST_info("test_x509_time(%d) failed: internal error\n", idx);
366 result = ASN1_TIME_set_string_X509(t, x509_format_tests[idx].data);
367 /* time string parsing result is always checked against what's expected */
368 if (!TEST_int_eq(result, x509_format_tests[idx].expected)) {
369 TEST_info("test_x509_time(%d) failed: expected %d, got %d\n",
370 idx, x509_format_tests[idx].expected, result);
374 /* if t is not NULL but expected_type is ignored(-1), it is an 'OK' case */
375 if (t != NULL && x509_format_tests[idx].expected_type != -1) {
376 if (!TEST_int_eq(t->type, x509_format_tests[idx].expected_type)) {
377 TEST_info("test_x509_time(%d) failed: expected_type %d, got %d\n",
378 idx, x509_format_tests[idx].expected_type, t->type);
383 /* if t is not NULL but expected_string is NULL, it is an 'OK' case too */
384 if (t != NULL && x509_format_tests[idx].expected_string) {
385 if (!TEST_str_eq((const char *)t->data,
386 x509_format_tests[idx].expected_string)) {
387 TEST_info("test_x509_time(%d) failed: expected_string %s, got %s\n",
388 idx, x509_format_tests[idx].expected_string, t->data);
400 static const struct {
403 } day_of_week_tests[] = {
404 /*YYYY MM DD DoY DoW */
405 { 1900, 1, 1, 0, 1 },
406 { 1900, 2, 28, 58, 3 },
407 { 1900, 3, 1, 59, 4 },
408 { 1900, 12, 31, 364, 1 },
409 { 1901, 1, 1, 0, 2 },
410 { 1970, 1, 1, 0, 4 },
411 { 1999, 1, 10, 9, 0 },
412 { 1999, 12, 31, 364, 5 },
413 { 2000, 1, 1, 0, 6 },
414 { 2000, 2, 28, 58, 1 },
415 { 2000, 2, 29, 59, 2 },
416 { 2000, 3, 1, 60, 3 },
417 { 2000, 12, 31, 365, 0 },
418 { 2001, 1, 1, 0, 1 },
419 { 2008, 1, 1, 0, 2 },
420 { 2008, 2, 28, 58, 4 },
421 { 2008, 2, 29, 59, 5 },
422 { 2008, 3, 1, 60, 6 },
423 { 2008, 12, 31, 365, 3 },
424 { 2009, 1, 1, 0, 4 },
425 { 2011, 1, 1, 0, 6 },
426 { 2011, 2, 28, 58, 1 },
427 { 2011, 3, 1, 59, 2 },
428 { 2011, 12, 31, 364, 6 },
429 { 2012, 1, 1, 0, 0 },
430 { 2019, 1, 2, 1, 3 },
431 { 2019, 2, 2, 32, 6 },
432 { 2019, 3, 2, 60, 6 },
433 { 2019, 4, 2, 91, 2 },
434 { 2019, 5, 2, 121, 4 },
435 { 2019, 6, 2, 152, 0 },
436 { 2019, 7, 2, 182, 2 },
437 { 2019, 8, 2, 213, 5 },
438 { 2019, 9, 2, 244, 1 },
439 { 2019, 10, 2, 274, 3 },
440 { 2019, 11, 2, 305, 6 },
441 { 2019, 12, 2, 335, 1 },
442 { 2020, 1, 2, 1, 4 },
443 { 2020, 2, 2, 32, 0 },
444 { 2020, 3, 2, 61, 1 },
445 { 2020, 4, 2, 92, 4 },
446 { 2020, 5, 2, 122, 6 },
447 { 2020, 6, 2, 153, 2 },
448 { 2020, 7, 2, 183, 4 },
449 { 2020, 8, 2, 214, 0 },
450 { 2020, 9, 2, 245, 3 },
451 { 2020, 10, 2, 275, 5 },
452 { 2020, 11, 2, 306, 1 },
453 { 2020, 12, 2, 336, 3 }
456 static int test_days(int n)
463 BIO_snprintf(d, sizeof(d), "%04d%02d%02d050505Z",
464 day_of_week_tests[n].y, day_of_week_tests[n].m,
465 day_of_week_tests[n].d);
467 if (!TEST_ptr(a = ASN1_TIME_new()))
470 r = TEST_true(ASN1_TIME_set_string(a, d))
471 && TEST_true(ASN1_TIME_to_tm(a, &t))
472 && TEST_int_eq(t.tm_yday, day_of_week_tests[n].yd)
473 && TEST_int_eq(t.tm_wday, day_of_week_tests[n].wd);
479 #define construct_asn1_time(s, t, e) \
480 { { sizeof(s) - 1, t, (unsigned char*)s, 0 }, e }
482 static const struct {
484 const char *readable;
485 } x509_print_tests [] = {
486 /* Generalized Time */
487 construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME,
488 "Jul 31 22:20:50 2017 GMT"),
489 /* Generalized Time, no seconds */
490 construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME,
491 "Jul 31 22:20:00 2017 GMT"),
492 /* Generalized Time, fractional seconds (3 digits) */
493 construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME,
494 "Jul 31 22:20:50.123 2017 GMT"),
495 /* Generalized Time, fractional seconds (1 digit) */
496 construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME,
497 "Jul 31 22:20:50.1 2017 GMT"),
498 /* Generalized Time, fractional seconds (0 digit) */
499 construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME,
502 construct_asn1_time("170731222050Z", V_ASN1_UTCTIME,
503 "Jul 31 22:20:50 2017 GMT"),
504 /* UTC Time, no seconds */
505 construct_asn1_time("1707312220Z", V_ASN1_UTCTIME,
506 "Jul 31 22:20:00 2017 GMT"),
509 static int test_x509_time_print(int idx)
514 const char *readable;
516 if (!TEST_ptr(m = BIO_new(BIO_s_mem())))
519 rv = ASN1_TIME_print(m, &x509_print_tests[idx].asn1);
520 readable = x509_print_tests[idx].readable;
522 if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) {
523 /* only if the test case intends to fail... */
526 if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0)
527 || !TEST_int_eq(rv, (int)strlen(readable))
528 || !TEST_strn_eq(pp, readable, rv))
537 int setup_tests(void)
539 ADD_TEST(test_x509_cmp_time_current);
540 ADD_TEST(test_X509_cmp_timeframe);
541 ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));
542 ADD_ALL_TESTS(test_x509_time, OSSL_NELEM(x509_format_tests));
543 ADD_ALL_TESTS(test_days, OSSL_NELEM(day_of_week_tests));
544 ADD_ALL_TESTS(test_x509_time_print, OSSL_NELEM(x509_print_tests));