xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/x509_time_test.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1132cc1c4Schristos /*
221497c5cSchristos  * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3132cc1c4Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5132cc1c4Schristos  * this file except in compliance with the License.  You can obtain a copy
6132cc1c4Schristos  * in the file LICENSE in the source distribution or at
7132cc1c4Schristos  * https://www.openssl.org/source/license.html
8132cc1c4Schristos  */
9132cc1c4Schristos 
10132cc1c4Schristos /* Tests for X509 time functions */
11132cc1c4Schristos 
12132cc1c4Schristos #include <string.h>
13132cc1c4Schristos #include <time.h>
14132cc1c4Schristos 
15132cc1c4Schristos #include <openssl/asn1.h>
16132cc1c4Schristos #include <openssl/x509.h>
17132cc1c4Schristos #include "testutil.h"
1813d40330Schristos #include "internal/nelem.h"
19132cc1c4Schristos 
20132cc1c4Schristos typedef struct {
21132cc1c4Schristos     const char *data;
22132cc1c4Schristos     int type;
23132cc1c4Schristos     time_t cmp_time;
24132cc1c4Schristos     /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
25132cc1c4Schristos     int expected;
26132cc1c4Schristos } TESTDATA;
27132cc1c4Schristos 
2813d40330Schristos typedef struct {
2913d40330Schristos     const char *data;
3013d40330Schristos     /* 0 for check-only mode, 1 for set-string mode */
3113d40330Schristos     int set_string;
3213d40330Schristos     /* 0 for error, 1 if succeed */
3313d40330Schristos     int expected;
3413d40330Schristos     /*
3513d40330Schristos      * The following 2 fields are ignored if set_string field is set to '0'
3613d40330Schristos      * (in check only mode).
3713d40330Schristos      *
3813d40330Schristos      * But they can still be ignored explicitly in set-string mode by:
3913d40330Schristos      * setting -1 to expected_type and setting NULL to expected_string.
4013d40330Schristos      *
4113d40330Schristos      * It's useful in a case of set-string mode but the expected result
4213d40330Schristos      * is a 'parsing error'.
4313d40330Schristos      */
4413d40330Schristos     int expected_type;
4513d40330Schristos     const char *expected_string;
4613d40330Schristos } TESTDATA_FORMAT;
4713d40330Schristos 
4813d40330Schristos /*
4913d40330Schristos  * Actually, the "loose" mode has been tested in
5013d40330Schristos  * those time-compare-cases, so we may not test it again.
5113d40330Schristos  */
5213d40330Schristos static TESTDATA_FORMAT x509_format_tests[] = {
5313d40330Schristos     /* GeneralizedTime */
5413d40330Schristos     {
5513d40330Schristos         /* good format, check only */
5613d40330Schristos         "20170217180105Z", 0, 1, -1, NULL,
5713d40330Schristos     },
5813d40330Schristos     {
5913d40330Schristos         /* not leap year, check only */
6013d40330Schristos         "20170229180105Z", 0, 0, -1, NULL,
6113d40330Schristos     },
6213d40330Schristos     {
6313d40330Schristos         /* leap year, check only */
6413d40330Schristos         "20160229180105Z", 0, 1, -1, NULL,
6513d40330Schristos     },
6613d40330Schristos     {
6713d40330Schristos         /* SS is missing, check only */
6813d40330Schristos         "201702171801Z", 0, 0, -1, NULL,
6913d40330Schristos     },
7013d40330Schristos     {
7113d40330Schristos         /* fractional seconds, check only */
7213d40330Schristos         "20170217180105.001Z", 0, 0, -1, NULL,
7313d40330Schristos     },
7413d40330Schristos     {
7513d40330Schristos         /* time zone, check only */
7613d40330Schristos         "20170217180105+0800", 0, 0, -1, NULL,
7713d40330Schristos     },
7813d40330Schristos     {
7913d40330Schristos         /* SS is missing, set string */
8013d40330Schristos         "201702171801Z", 1, 0, -1, NULL,
8113d40330Schristos     },
8213d40330Schristos     {
8313d40330Schristos         /* fractional seconds, set string */
8413d40330Schristos         "20170217180105.001Z", 1, 0, -1, NULL,
8513d40330Schristos     },
8613d40330Schristos     {
8713d40330Schristos         /* time zone, set string */
8813d40330Schristos         "20170217180105+0800", 1, 0, -1, NULL,
8913d40330Schristos     },
9013d40330Schristos     {
9113d40330Schristos         /* good format, check returned 'turned' string */
9213d40330Schristos         "20170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z",
9313d40330Schristos     },
9413d40330Schristos     {
9513d40330Schristos         /* good format, check returned string */
9613d40330Schristos         "20510217180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "20510217180154Z",
9713d40330Schristos     },
9813d40330Schristos     {
9913d40330Schristos         /* good format but out of UTC range, check returned string */
10013d40330Schristos         "19230419180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "19230419180154Z",
10113d40330Schristos     },
10213d40330Schristos     /* UTC */
10313d40330Schristos     {
10413d40330Schristos         /* SS is missing, check only */
10513d40330Schristos         "1702171801Z", 0, 0, -1, NULL,
10613d40330Schristos     },
10713d40330Schristos     {
10813d40330Schristos         /* not leap year, check only */
10913d40330Schristos         "050229180101Z", 0, 0, -1, NULL,
11013d40330Schristos     },
11113d40330Schristos     {
11213d40330Schristos         /* leap year, check only */
11313d40330Schristos         "040229180101Z", 0, 1, -1, NULL,
11413d40330Schristos     },
11513d40330Schristos     {
11613d40330Schristos         /* time zone, check only */
11713d40330Schristos         "170217180154+0800", 0, 0, -1, NULL,
11813d40330Schristos     },
11913d40330Schristos     {
12013d40330Schristos         /* SS is missing, set string */
12113d40330Schristos         "1702171801Z", 1, 0, -1, NULL,
12213d40330Schristos     },
12313d40330Schristos     {
12413d40330Schristos         /* time zone, set string */
12513d40330Schristos         "170217180154+0800", 1, 0, -1, NULL,
12613d40330Schristos     },
12713d40330Schristos     {
12813d40330Schristos         /* 2017, good format, check returned string */
12913d40330Schristos         "170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z",
13013d40330Schristos     },
13113d40330Schristos     {
13213d40330Schristos         /* 1998, good format, check returned string */
13313d40330Schristos         "981223180154Z", 1, 1, V_ASN1_UTCTIME, "981223180154Z",
13413d40330Schristos     },
13513d40330Schristos };
13613d40330Schristos 
137132cc1c4Schristos static TESTDATA x509_cmp_tests[] = {
138132cc1c4Schristos     {
139132cc1c4Schristos         "20170217180154Z", V_ASN1_GENERALIZEDTIME,
140132cc1c4Schristos         /* The same in seconds since epoch. */
141132cc1c4Schristos         1487354514, -1,
142132cc1c4Schristos     },
143132cc1c4Schristos     {
144132cc1c4Schristos         "20170217180154Z", V_ASN1_GENERALIZEDTIME,
145132cc1c4Schristos         /* One second more. */
146132cc1c4Schristos         1487354515, -1,
147132cc1c4Schristos     },
148132cc1c4Schristos     {
149132cc1c4Schristos         "20170217180154Z", V_ASN1_GENERALIZEDTIME,
150132cc1c4Schristos         /* One second less. */
151132cc1c4Schristos         1487354513, 1,
152132cc1c4Schristos     },
153132cc1c4Schristos     /* Same as UTC time. */
154132cc1c4Schristos     {
155132cc1c4Schristos         "170217180154Z", V_ASN1_UTCTIME,
156132cc1c4Schristos         /* The same in seconds since epoch. */
157132cc1c4Schristos         1487354514, -1,
158132cc1c4Schristos     },
159132cc1c4Schristos     {
160132cc1c4Schristos         "170217180154Z", V_ASN1_UTCTIME,
161132cc1c4Schristos         /* One second more. */
162132cc1c4Schristos         1487354515, -1,
163132cc1c4Schristos     },
164132cc1c4Schristos     {
165132cc1c4Schristos         "170217180154Z", V_ASN1_UTCTIME,
166132cc1c4Schristos         /* One second less. */
167132cc1c4Schristos         1487354513, 1,
168132cc1c4Schristos     },
169132cc1c4Schristos     /* UTCTime from the 20th century. */
170132cc1c4Schristos     {
171132cc1c4Schristos         "990217180154Z", V_ASN1_UTCTIME,
172132cc1c4Schristos         /* The same in seconds since epoch. */
173132cc1c4Schristos         919274514, -1,
174132cc1c4Schristos     },
175132cc1c4Schristos     {
176132cc1c4Schristos         "990217180154Z", V_ASN1_UTCTIME,
177132cc1c4Schristos         /* One second more. */
178132cc1c4Schristos         919274515, -1,
179132cc1c4Schristos     },
180132cc1c4Schristos     {
181132cc1c4Schristos         "990217180154Z", V_ASN1_UTCTIME,
182132cc1c4Schristos         /* One second less. */
183132cc1c4Schristos         919274513, 1,
184132cc1c4Schristos     },
185132cc1c4Schristos     /* Various invalid formats. */
186132cc1c4Schristos     {
187132cc1c4Schristos         /* No trailing Z. */
188132cc1c4Schristos         "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
189132cc1c4Schristos     },
190132cc1c4Schristos     {
191132cc1c4Schristos         /* No trailing Z, UTCTime. */
192132cc1c4Schristos         "170217180154", V_ASN1_UTCTIME, 0, 0,
193132cc1c4Schristos     },
194132cc1c4Schristos     {
195132cc1c4Schristos         /* No seconds. */
196132cc1c4Schristos         "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
197132cc1c4Schristos     },
198132cc1c4Schristos     {
199132cc1c4Schristos         /* No seconds, UTCTime. */
200132cc1c4Schristos         "1702171801Z", V_ASN1_UTCTIME, 0, 0,
201132cc1c4Schristos     },
202132cc1c4Schristos     {
203132cc1c4Schristos         /* Fractional seconds. */
204132cc1c4Schristos         "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
205132cc1c4Schristos     },
206132cc1c4Schristos     {
207132cc1c4Schristos         /* Fractional seconds, UTCTime. */
208132cc1c4Schristos         "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
209132cc1c4Schristos     },
210132cc1c4Schristos     {
211132cc1c4Schristos         /* Timezone offset. */
212132cc1c4Schristos         "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
213132cc1c4Schristos     },
214132cc1c4Schristos     {
215132cc1c4Schristos         /* Timezone offset, UTCTime. */
216132cc1c4Schristos         "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
217132cc1c4Schristos     },
218132cc1c4Schristos     {
219132cc1c4Schristos         /* Extra digits. */
220132cc1c4Schristos         "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
221132cc1c4Schristos     },
222132cc1c4Schristos     {
223132cc1c4Schristos         /* Extra digits, UTCTime. */
224132cc1c4Schristos         "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
225132cc1c4Schristos     },
226132cc1c4Schristos     {
227132cc1c4Schristos         /* Non-digits. */
228132cc1c4Schristos         "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
229132cc1c4Schristos     },
230132cc1c4Schristos     {
231132cc1c4Schristos         /* Non-digits, UTCTime. */
232132cc1c4Schristos         "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
233132cc1c4Schristos     },
234132cc1c4Schristos     {
235132cc1c4Schristos         /* Trailing garbage. */
236132cc1c4Schristos         "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
237132cc1c4Schristos     },
238132cc1c4Schristos     {
239132cc1c4Schristos         /* Trailing garbage, UTCTime. */
240132cc1c4Schristos         "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
241132cc1c4Schristos     },
242132cc1c4Schristos     {
243132cc1c4Schristos          /* Swapped type. */
244132cc1c4Schristos         "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
245132cc1c4Schristos     },
246132cc1c4Schristos     {
247132cc1c4Schristos         /* Swapped type. */
248132cc1c4Schristos         "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
249132cc1c4Schristos     },
250132cc1c4Schristos     {
251132cc1c4Schristos         /* Bad type. */
252132cc1c4Schristos         "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
253132cc1c4Schristos     },
254132cc1c4Schristos };
255132cc1c4Schristos 
test_x509_cmp_time(int idx)256132cc1c4Schristos static int test_x509_cmp_time(int idx)
257132cc1c4Schristos {
258132cc1c4Schristos     ASN1_TIME t;
259132cc1c4Schristos     int result;
260132cc1c4Schristos 
261132cc1c4Schristos     memset(&t, 0, sizeof(t));
262132cc1c4Schristos     t.type = x509_cmp_tests[idx].type;
263132cc1c4Schristos     t.data = (unsigned char*)(x509_cmp_tests[idx].data);
264132cc1c4Schristos     t.length = strlen(x509_cmp_tests[idx].data);
26513d40330Schristos     t.flags = 0;
266132cc1c4Schristos 
267132cc1c4Schristos     result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
26813d40330Schristos     if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
26913d40330Schristos         TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n",
270132cc1c4Schristos                 idx, x509_cmp_tests[idx].expected, result);
271132cc1c4Schristos         return 0;
272132cc1c4Schristos     }
273132cc1c4Schristos     return 1;
274132cc1c4Schristos }
275132cc1c4Schristos 
test_x509_cmp_time_current(void)27613d40330Schristos static int test_x509_cmp_time_current(void)
277132cc1c4Schristos {
278132cc1c4Schristos     time_t now = time(NULL);
279132cc1c4Schristos     /* Pick a day earlier and later, relative to any system clock. */
280132cc1c4Schristos     ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
281132cc1c4Schristos     int cmp_result, failed = 0;
282132cc1c4Schristos 
283132cc1c4Schristos     asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
284132cc1c4Schristos     asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
285132cc1c4Schristos 
286132cc1c4Schristos     cmp_result  = X509_cmp_time(asn1_before, NULL);
28713d40330Schristos     if (!TEST_int_eq(cmp_result, -1))
288132cc1c4Schristos         failed = 1;
289132cc1c4Schristos 
290132cc1c4Schristos     cmp_result = X509_cmp_time(asn1_after, NULL);
29113d40330Schristos     if (!TEST_int_eq(cmp_result, 1))
292132cc1c4Schristos         failed = 1;
293132cc1c4Schristos 
294132cc1c4Schristos     ASN1_TIME_free(asn1_before);
295132cc1c4Schristos     ASN1_TIME_free(asn1_after);
296132cc1c4Schristos 
297132cc1c4Schristos     return failed == 0;
298132cc1c4Schristos }
299132cc1c4Schristos 
test_X509_cmp_timeframe_vpm(const X509_VERIFY_PARAM * vpm,ASN1_TIME * asn1_before,ASN1_TIME * asn1_mid,ASN1_TIME * asn1_after)300*b0d17251Schristos static int test_X509_cmp_timeframe_vpm(const X509_VERIFY_PARAM *vpm,
301*b0d17251Schristos                                        ASN1_TIME *asn1_before,
302*b0d17251Schristos                                        ASN1_TIME *asn1_mid,
303*b0d17251Schristos                                        ASN1_TIME *asn1_after)
304*b0d17251Schristos {
305*b0d17251Schristos     int always_0 = vpm != NULL
306*b0d17251Schristos         && (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_USE_CHECK_TIME) == 0
307*b0d17251Schristos         && (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_NO_CHECK_TIME) != 0;
308*b0d17251Schristos 
309*b0d17251Schristos     return asn1_before != NULL && asn1_mid != NULL && asn1_after != NULL
310*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_after), 0)
311*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, NULL), 0)
312*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, NULL, asn1_after), 0)
313*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, NULL, NULL), 0)
314*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_after),
315*b0d17251Schristos                        always_0 ? 0 : -1)
316*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_before),
317*b0d17251Schristos                        always_0 ? 0 : 1)
318*b0d17251Schristos         && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_before),
319*b0d17251Schristos                        always_0 ? 0 : 1);
320*b0d17251Schristos }
321*b0d17251Schristos 
test_X509_cmp_timeframe(void)322*b0d17251Schristos static int test_X509_cmp_timeframe(void)
323*b0d17251Schristos {
324*b0d17251Schristos     time_t now = time(NULL);
325*b0d17251Schristos     ASN1_TIME *asn1_mid = ASN1_TIME_adj(NULL, now, 0, 0);
326*b0d17251Schristos     /* Pick a day earlier and later, relative to any system clock. */
327*b0d17251Schristos     ASN1_TIME *asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
328*b0d17251Schristos     ASN1_TIME *asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
329*b0d17251Schristos     X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
330*b0d17251Schristos     int res = 0;
331*b0d17251Schristos 
332*b0d17251Schristos     if (vpm == NULL)
333*b0d17251Schristos         goto finish;
334*b0d17251Schristos     res = test_X509_cmp_timeframe_vpm(NULL, asn1_before, asn1_mid, asn1_after)
335*b0d17251Schristos         && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after);
336*b0d17251Schristos 
337*b0d17251Schristos     X509_VERIFY_PARAM_set_time(vpm, now);
338*b0d17251Schristos     res = res
339*b0d17251Schristos         && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after)
340*b0d17251Schristos         && X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME)
341*b0d17251Schristos         && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after);
342*b0d17251Schristos 
343*b0d17251Schristos     X509_VERIFY_PARAM_free(vpm);
344*b0d17251Schristos finish:
345*b0d17251Schristos     ASN1_TIME_free(asn1_mid);
346*b0d17251Schristos     ASN1_TIME_free(asn1_before);
347*b0d17251Schristos     ASN1_TIME_free(asn1_after);
348*b0d17251Schristos 
349*b0d17251Schristos     return res;
350*b0d17251Schristos }
351*b0d17251Schristos 
test_x509_time(int idx)35213d40330Schristos static int test_x509_time(int idx)
353132cc1c4Schristos {
35413d40330Schristos     ASN1_TIME *t = NULL;
35513d40330Schristos     int result, rv = 0;
356132cc1c4Schristos 
35713d40330Schristos     if (x509_format_tests[idx].set_string) {
35813d40330Schristos         /* set-string mode */
35913d40330Schristos         t = ASN1_TIME_new();
36013d40330Schristos         if (t == NULL) {
36113d40330Schristos             TEST_info("test_x509_time(%d) failed: internal error\n", idx);
36213d40330Schristos             return 0;
36313d40330Schristos         }
364132cc1c4Schristos     }
365132cc1c4Schristos 
36613d40330Schristos     result = ASN1_TIME_set_string_X509(t, x509_format_tests[idx].data);
36713d40330Schristos     /* time string parsing result is always checked against what's expected */
36813d40330Schristos     if (!TEST_int_eq(result, x509_format_tests[idx].expected)) {
36913d40330Schristos         TEST_info("test_x509_time(%d) failed: expected %d, got %d\n",
37013d40330Schristos                 idx, x509_format_tests[idx].expected, result);
37113d40330Schristos         goto out;
37213d40330Schristos     }
37313d40330Schristos 
37413d40330Schristos     /* if t is not NULL but expected_type is ignored(-1), it is an 'OK' case */
37513d40330Schristos     if (t != NULL && x509_format_tests[idx].expected_type != -1) {
37613d40330Schristos         if (!TEST_int_eq(t->type, x509_format_tests[idx].expected_type)) {
37713d40330Schristos             TEST_info("test_x509_time(%d) failed: expected_type %d, got %d\n",
37813d40330Schristos                     idx, x509_format_tests[idx].expected_type, t->type);
37913d40330Schristos             goto out;
38013d40330Schristos         }
38113d40330Schristos     }
38213d40330Schristos 
38313d40330Schristos     /* if t is not NULL but expected_string is NULL, it is an 'OK' case too */
38413d40330Schristos     if (t != NULL && x509_format_tests[idx].expected_string) {
38521497c5cSchristos         if (!TEST_mem_eq((const char *)t->data, t->length,
38621497c5cSchristos                     x509_format_tests[idx].expected_string,
38721497c5cSchristos                     strlen(x509_format_tests[idx].expected_string))) {
38821497c5cSchristos             TEST_info("test_x509_time(%d) failed: expected_string %s, got %.*s\n",
38921497c5cSchristos                     idx, x509_format_tests[idx].expected_string, t->length,
39021497c5cSchristos                     t->data);
39113d40330Schristos             goto out;
39213d40330Schristos         }
39313d40330Schristos     }
39413d40330Schristos 
39513d40330Schristos     rv = 1;
39613d40330Schristos out:
39713d40330Schristos     if (t != NULL)
39813d40330Schristos         ASN1_TIME_free(t);
39913d40330Schristos     return rv;
40013d40330Schristos }
40113d40330Schristos 
40213d40330Schristos static const struct {
40313d40330Schristos     int y, m, d;
40413d40330Schristos     int yd, wd;
40513d40330Schristos } day_of_week_tests[] = {
40613d40330Schristos     /*YYYY  MM  DD  DoY  DoW */
40713d40330Schristos     { 1900,  1,  1,   0, 1 },
40813d40330Schristos     { 1900,  2, 28,  58, 3 },
40913d40330Schristos     { 1900,  3,  1,  59, 4 },
41013d40330Schristos     { 1900, 12, 31, 364, 1 },
41113d40330Schristos     { 1901,  1,  1,   0, 2 },
41213d40330Schristos     { 1970,  1,  1,   0, 4 },
41313d40330Schristos     { 1999,  1, 10,   9, 0 },
41413d40330Schristos     { 1999, 12, 31, 364, 5 },
41513d40330Schristos     { 2000,  1,  1,   0, 6 },
41613d40330Schristos     { 2000,  2, 28,  58, 1 },
41713d40330Schristos     { 2000,  2, 29,  59, 2 },
41813d40330Schristos     { 2000,  3,  1,  60, 3 },
41913d40330Schristos     { 2000, 12, 31, 365, 0 },
42013d40330Schristos     { 2001,  1,  1,   0, 1 },
42113d40330Schristos     { 2008,  1,  1,   0, 2 },
42213d40330Schristos     { 2008,  2, 28,  58, 4 },
42313d40330Schristos     { 2008,  2, 29,  59, 5 },
42413d40330Schristos     { 2008,  3,  1,  60, 6 },
42513d40330Schristos     { 2008, 12, 31, 365, 3 },
42613d40330Schristos     { 2009,  1,  1,   0, 4 },
42713d40330Schristos     { 2011,  1,  1,   0, 6 },
42813d40330Schristos     { 2011,  2, 28,  58, 1 },
42913d40330Schristos     { 2011,  3,  1,  59, 2 },
43013d40330Schristos     { 2011, 12, 31, 364, 6 },
43113d40330Schristos     { 2012,  1,  1,   0, 0 },
43213d40330Schristos     { 2019,  1,  2,   1, 3 },
43313d40330Schristos     { 2019,  2,  2,  32, 6 },
43413d40330Schristos     { 2019,  3,  2,  60, 6 },
43513d40330Schristos     { 2019,  4,  2,  91, 2 },
43613d40330Schristos     { 2019,  5,  2, 121, 4 },
43713d40330Schristos     { 2019,  6,  2, 152, 0 },
43813d40330Schristos     { 2019,  7,  2, 182, 2 },
43913d40330Schristos     { 2019,  8,  2, 213, 5 },
44013d40330Schristos     { 2019,  9,  2, 244, 1 },
44113d40330Schristos     { 2019, 10,  2, 274, 3 },
44213d40330Schristos     { 2019, 11,  2, 305, 6 },
44313d40330Schristos     { 2019, 12,  2, 335, 1 },
44413d40330Schristos     { 2020,  1,  2,   1, 4 },
44513d40330Schristos     { 2020,  2,  2,  32, 0 },
44613d40330Schristos     { 2020,  3,  2,  61, 1 },
44713d40330Schristos     { 2020,  4,  2,  92, 4 },
44813d40330Schristos     { 2020,  5,  2, 122, 6 },
44913d40330Schristos     { 2020,  6,  2, 153, 2 },
45013d40330Schristos     { 2020,  7,  2, 183, 4 },
45113d40330Schristos     { 2020,  8,  2, 214, 0 },
45213d40330Schristos     { 2020,  9,  2, 245, 3 },
45313d40330Schristos     { 2020, 10,  2, 275, 5 },
45413d40330Schristos     { 2020, 11,  2, 306, 1 },
45513d40330Schristos     { 2020, 12,  2, 336, 3 }
45613d40330Schristos };
45713d40330Schristos 
test_days(int n)45813d40330Schristos static int test_days(int n)
45913d40330Schristos {
46013d40330Schristos     char d[16];
46113d40330Schristos     ASN1_TIME *a = NULL;
46213d40330Schristos     struct tm t;
46313d40330Schristos     int r;
46413d40330Schristos 
46513d40330Schristos     BIO_snprintf(d, sizeof(d), "%04d%02d%02d050505Z",
46613d40330Schristos                  day_of_week_tests[n].y, day_of_week_tests[n].m,
46713d40330Schristos                  day_of_week_tests[n].d);
46813d40330Schristos 
46913d40330Schristos     if (!TEST_ptr(a = ASN1_TIME_new()))
47013d40330Schristos         return 0;
47113d40330Schristos 
47213d40330Schristos     r = TEST_true(ASN1_TIME_set_string(a, d))
47313d40330Schristos         && TEST_true(ASN1_TIME_to_tm(a, &t))
47413d40330Schristos         && TEST_int_eq(t.tm_yday, day_of_week_tests[n].yd)
47513d40330Schristos         && TEST_int_eq(t.tm_wday, day_of_week_tests[n].wd);
47613d40330Schristos 
47713d40330Schristos     ASN1_TIME_free(a);
47813d40330Schristos     return r;
47913d40330Schristos }
48013d40330Schristos 
48113d40330Schristos #define construct_asn1_time(s, t, e) \
48213d40330Schristos     { { sizeof(s) - 1, t, (unsigned char*)s, 0 }, e }
48313d40330Schristos 
48413d40330Schristos static const struct {
48513d40330Schristos     ASN1_TIME asn1;
48613d40330Schristos     const char *readable;
487*b0d17251Schristos } x509_print_tests_rfc_822 [] = {
48813d40330Schristos     /* Generalized Time */
48913d40330Schristos     construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME,
49013d40330Schristos             "Jul 31 22:20:50 2017 GMT"),
49113d40330Schristos     /* Generalized Time, no seconds */
49213d40330Schristos     construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME,
49313d40330Schristos             "Jul 31 22:20:00 2017 GMT"),
49413d40330Schristos     /* Generalized Time, fractional seconds (3 digits) */
49513d40330Schristos     construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME,
49613d40330Schristos             "Jul 31 22:20:50.123 2017 GMT"),
49713d40330Schristos     /* Generalized Time, fractional seconds (1 digit) */
49813d40330Schristos     construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME,
49913d40330Schristos             "Jul 31 22:20:50.1 2017 GMT"),
50013d40330Schristos     /* Generalized Time, fractional seconds (0 digit) */
50113d40330Schristos     construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME,
50213d40330Schristos             "Bad time value"),
50313d40330Schristos     /* UTC Time */
50413d40330Schristos     construct_asn1_time("170731222050Z", V_ASN1_UTCTIME,
50513d40330Schristos             "Jul 31 22:20:50 2017 GMT"),
50613d40330Schristos     /* UTC Time, no seconds */
50713d40330Schristos     construct_asn1_time("1707312220Z", V_ASN1_UTCTIME,
50813d40330Schristos             "Jul 31 22:20:00 2017 GMT"),
50913d40330Schristos };
51013d40330Schristos 
511*b0d17251Schristos static const struct {
512*b0d17251Schristos     ASN1_TIME asn1;
513*b0d17251Schristos     const char *readable;
514*b0d17251Schristos } x509_print_tests_iso_8601 [] = {
515*b0d17251Schristos     /* Generalized Time */
516*b0d17251Schristos     construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME,
517*b0d17251Schristos             "2017-07-31 22:20:50Z"),
518*b0d17251Schristos     /* Generalized Time, no seconds */
519*b0d17251Schristos     construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME,
520*b0d17251Schristos             "2017-07-31 22:20:00Z"),
521*b0d17251Schristos     /* Generalized Time, fractional seconds (3 digits) */
522*b0d17251Schristos     construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME,
523*b0d17251Schristos             "2017-07-31 22:20:50.123Z"),
524*b0d17251Schristos     /* Generalized Time, fractional seconds (1 digit) */
525*b0d17251Schristos     construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME,
526*b0d17251Schristos             "2017-07-31 22:20:50.1Z"),
527*b0d17251Schristos     /* Generalized Time, fractional seconds (0 digit) */
528*b0d17251Schristos     construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME,
529*b0d17251Schristos             "Bad time value"),
530*b0d17251Schristos     /* UTC Time */
531*b0d17251Schristos     construct_asn1_time("170731222050Z", V_ASN1_UTCTIME,
532*b0d17251Schristos             "2017-07-31 22:20:50Z"),
533*b0d17251Schristos     /* UTC Time, no seconds */
534*b0d17251Schristos     construct_asn1_time("1707312220Z", V_ASN1_UTCTIME,
535*b0d17251Schristos             "2017-07-31 22:20:00Z"),
536*b0d17251Schristos };
537*b0d17251Schristos 
test_x509_time_print_rfc_822(int idx)538*b0d17251Schristos static int test_x509_time_print_rfc_822(int idx)
53913d40330Schristos {
54013d40330Schristos     BIO *m;
54113d40330Schristos     int ret = 0, rv;
54213d40330Schristos     char *pp;
54313d40330Schristos     const char *readable;
54413d40330Schristos 
54513d40330Schristos     if (!TEST_ptr(m = BIO_new(BIO_s_mem())))
54613d40330Schristos         goto err;
54713d40330Schristos 
548*b0d17251Schristos     rv = ASN1_TIME_print_ex(m, &x509_print_tests_rfc_822[idx].asn1, ASN1_DTFLGS_RFC822);
549*b0d17251Schristos     readable = x509_print_tests_rfc_822[idx].readable;
550*b0d17251Schristos 
551*b0d17251Schristos     if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) {
552*b0d17251Schristos         /* only if the test case intends to fail... */
553*b0d17251Schristos         goto err;
554*b0d17251Schristos     }
555*b0d17251Schristos     if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0)
556*b0d17251Schristos         || !TEST_int_eq(rv, (int)strlen(readable))
557*b0d17251Schristos         || !TEST_strn_eq(pp, readable, rv))
558*b0d17251Schristos         goto err;
559*b0d17251Schristos 
560*b0d17251Schristos     ret = 1;
561*b0d17251Schristos  err:
562*b0d17251Schristos     BIO_free(m);
563*b0d17251Schristos     return ret;
564*b0d17251Schristos }
565*b0d17251Schristos 
test_x509_time_print_iso_8601(int idx)566*b0d17251Schristos static int test_x509_time_print_iso_8601(int idx)
567*b0d17251Schristos {
568*b0d17251Schristos     BIO *m;
569*b0d17251Schristos     int ret = 0, rv;
570*b0d17251Schristos     char *pp;
571*b0d17251Schristos     const char *readable;
572*b0d17251Schristos 
573*b0d17251Schristos     if (!TEST_ptr(m = BIO_new(BIO_s_mem())))
574*b0d17251Schristos         goto err;
575*b0d17251Schristos 
576*b0d17251Schristos     rv = ASN1_TIME_print_ex(m, &x509_print_tests_iso_8601[idx].asn1, ASN1_DTFLGS_ISO8601);
577*b0d17251Schristos     readable = x509_print_tests_iso_8601[idx].readable;
57813d40330Schristos 
57913d40330Schristos     if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) {
58013d40330Schristos         /* only if the test case intends to fail... */
58113d40330Schristos         goto err;
58213d40330Schristos     }
58313d40330Schristos     if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0)
58413d40330Schristos         || !TEST_int_eq(rv, (int)strlen(readable))
58513d40330Schristos         || !TEST_strn_eq(pp, readable, rv))
58613d40330Schristos         goto err;
58713d40330Schristos 
58813d40330Schristos     ret = 1;
58913d40330Schristos  err:
59013d40330Schristos     BIO_free(m);
591132cc1c4Schristos     return ret;
592132cc1c4Schristos }
59313d40330Schristos 
setup_tests(void)59413d40330Schristos int setup_tests(void)
59513d40330Schristos {
59613d40330Schristos     ADD_TEST(test_x509_cmp_time_current);
597*b0d17251Schristos     ADD_TEST(test_X509_cmp_timeframe);
59813d40330Schristos     ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));
59913d40330Schristos     ADD_ALL_TESTS(test_x509_time, OSSL_NELEM(x509_format_tests));
60013d40330Schristos     ADD_ALL_TESTS(test_days, OSSL_NELEM(day_of_week_tests));
601*b0d17251Schristos     ADD_ALL_TESTS(test_x509_time_print_rfc_822, OSSL_NELEM(x509_print_tests_rfc_822));
602*b0d17251Schristos     ADD_ALL_TESTS(test_x509_time_print_iso_8601, OSSL_NELEM(x509_print_tests_iso_8601));
60313d40330Schristos     return 1;
60413d40330Schristos }
605