2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
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 /* SS is missing, check only */
60 "201702171801Z", 0, 0, -1, NULL,
63 /* fractional seconds, check only */
64 "20170217180105.001Z", 0, 0, -1, NULL,
67 /* time zone, check only */
68 "20170217180105+0800", 0, 0, -1, NULL,
71 /* SS is missing, set string */
72 "201702171801Z", 1, 0, -1, NULL,
75 /* fractional seconds, set string */
76 "20170217180105.001Z", 1, 0, -1, NULL,
79 /* time zone, set string */
80 "20170217180105+0800", 1, 0, -1, NULL,
83 /* good format, check returned 'turned' string */
84 "20170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z",
87 /* good format, check returned string */
88 "20510217180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "20510217180154Z",
91 /* good format but out of UTC range, check returned string */
92 "19230419180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "19230419180154Z",
96 /* SS is missing, check only */
97 "1702171801Z", 0, 0, -1, NULL,
100 /* time zone, check only */
101 "170217180154+0800", 0, 0, -1, NULL,
104 /* SS is missing, set string */
105 "1702171801Z", 1, 0, -1, NULL,
108 /* time zone, set string */
109 "170217180154+0800", 1, 0, -1, NULL,
112 /* 2017, good format, check returned string */
113 "170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z",
116 /* 1998, good format, check returned string */
117 "981223180154Z", 1, 1, V_ASN1_UTCTIME, "981223180154Z",
121 static TESTDATA x509_cmp_tests[] = {
123 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
124 /* The same in seconds since epoch. */
128 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
129 /* One second more. */
133 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
134 /* One second less. */
137 /* Same as UTC time. */
139 "170217180154Z", V_ASN1_UTCTIME,
140 /* The same in seconds since epoch. */
144 "170217180154Z", V_ASN1_UTCTIME,
145 /* One second more. */
149 "170217180154Z", V_ASN1_UTCTIME,
150 /* One second less. */
153 /* UTCTime from the 20th century. */
155 "990217180154Z", V_ASN1_UTCTIME,
156 /* The same in seconds since epoch. */
160 "990217180154Z", V_ASN1_UTCTIME,
161 /* One second more. */
165 "990217180154Z", V_ASN1_UTCTIME,
166 /* One second less. */
169 /* Various invalid formats. */
172 "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
175 /* No trailing Z, UTCTime. */
176 "170217180154", V_ASN1_UTCTIME, 0, 0,
180 "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
183 /* No seconds, UTCTime. */
184 "1702171801Z", V_ASN1_UTCTIME, 0, 0,
187 /* Fractional seconds. */
188 "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
191 /* Fractional seconds, UTCTime. */
192 "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
195 /* Timezone offset. */
196 "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
199 /* Timezone offset, UTCTime. */
200 "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
204 "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
207 /* Extra digits, UTCTime. */
208 "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
212 "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
215 /* Non-digits, UTCTime. */
216 "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
219 /* Trailing garbage. */
220 "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
223 /* Trailing garbage, UTCTime. */
224 "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
228 "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
232 "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
236 "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
240 static int test_x509_cmp_time(int idx)
245 memset(&t, 0, sizeof(t));
246 t.type = x509_cmp_tests[idx].type;
247 t.data = (unsigned char*)(x509_cmp_tests[idx].data);
248 t.length = strlen(x509_cmp_tests[idx].data);
251 result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
252 if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
253 TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n",
254 idx, x509_cmp_tests[idx].expected, result);
260 static int test_x509_cmp_time_current()
262 time_t now = time(NULL);
263 /* Pick a day earlier and later, relative to any system clock. */
264 ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
265 int cmp_result, failed = 0;
267 asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
268 asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
270 cmp_result = X509_cmp_time(asn1_before, NULL);
271 if (!TEST_int_eq(cmp_result, -1))
274 cmp_result = X509_cmp_time(asn1_after, NULL);
275 if (!TEST_int_eq(cmp_result, 1))
278 ASN1_TIME_free(asn1_before);
279 ASN1_TIME_free(asn1_after);
284 static int test_x509_time(int idx)
289 if (x509_format_tests[idx].set_string) {
290 /* set-string mode */
293 TEST_info("test_x509_time(%d) failed: internal error\n", idx);
298 result = ASN1_TIME_set_string_X509(t, x509_format_tests[idx].data);
299 /* time string parsing result is always checked against what's expected */
300 if (!TEST_int_eq(result, x509_format_tests[idx].expected)) {
301 TEST_info("test_x509_time(%d) failed: expected %d, got %d\n",
302 idx, x509_format_tests[idx].expected, result);
306 /* if t is not NULL but expected_type is ignored(-1), it is an 'OK' case */
307 if (t != NULL && x509_format_tests[idx].expected_type != -1) {
308 if (!TEST_int_eq(t->type, x509_format_tests[idx].expected_type)) {
309 TEST_info("test_x509_time(%d) failed: expected_type %d, got %d\n",
310 idx, x509_format_tests[idx].expected_type, t->type);
315 /* if t is not NULL but expected_string is NULL, it is an 'OK' case too */
316 if (t != NULL && x509_format_tests[idx].expected_string) {
317 if (!TEST_str_eq((const char *)t->data,
318 x509_format_tests[idx].expected_string)) {
319 TEST_info("test_x509_time(%d) failed: expected_string %s, got %s\n",
320 idx, x509_format_tests[idx].expected_string, t->data);
332 void register_tests()
334 ADD_TEST(test_x509_cmp_time_current);
335 ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));
336 ADD_ALL_TESTS(test_x509_time, OSSL_NELEM(x509_format_tests));