xref: /freebsd-src/crypto/openssl/test/asn1_time_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /* Time tests for the asn1 module */
11*e0c4386eSCy Schubert 
12*e0c4386eSCy Schubert #include <stdio.h>
13*e0c4386eSCy Schubert #include <string.h>
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert #include <openssl/asn1.h>
16*e0c4386eSCy Schubert #include <openssl/evp.h>
17*e0c4386eSCy Schubert #include <openssl/objects.h>
18*e0c4386eSCy Schubert #include "testutil.h"
19*e0c4386eSCy Schubert #include "internal/nelem.h"
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert struct testdata {
22*e0c4386eSCy Schubert     char *data;             /* TIME string value */
23*e0c4386eSCy Schubert     int type;               /* GENERALIZED OR UTC */
24*e0c4386eSCy Schubert     int expected_type;      /* expected type after set/set_string_gmt */
25*e0c4386eSCy Schubert     int check_result;       /* check result */
26*e0c4386eSCy Schubert     time_t t;               /* expected time_t*/
27*e0c4386eSCy Schubert     int cmp_result;         /* comparison to baseline result */
28*e0c4386eSCy Schubert     int convert_result;     /* conversion result */
29*e0c4386eSCy Schubert };
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert static struct testdata tbl_testdata_pos[] = {
32*e0c4386eSCy Schubert     { "0",                 V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, }, /* Bad time */
33*e0c4386eSCy Schubert     { "ABCD",              V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
34*e0c4386eSCy Schubert     { "0ABCD",             V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
35*e0c4386eSCy Schubert     { "1-700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
36*e0c4386eSCy Schubert     { "`9700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
37*e0c4386eSCy Schubert     { "19700101000000Z",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 0, },
38*e0c4386eSCy Schubert     { "A00101000000Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 0, },
39*e0c4386eSCy Schubert     { "A9700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
40*e0c4386eSCy Schubert     { "1A700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
41*e0c4386eSCy Schubert     { "19A00101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
42*e0c4386eSCy Schubert     { "197A0101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
43*e0c4386eSCy Schubert     { "1970A101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
44*e0c4386eSCy Schubert     { "19700A01000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
45*e0c4386eSCy Schubert     { "197001A1000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
46*e0c4386eSCy Schubert     { "1970010A000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
47*e0c4386eSCy Schubert     { "19700101A00000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
48*e0c4386eSCy Schubert     { "197001010A0000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
49*e0c4386eSCy Schubert     { "1970010100A000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
50*e0c4386eSCy Schubert     { "19700101000A00Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
51*e0c4386eSCy Schubert     { "197001010000A0Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
52*e0c4386eSCy Schubert     { "1970010100000AZ",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
53*e0c4386eSCy Schubert     { "700101000000X",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 0, },
54*e0c4386eSCy Schubert     { "19700101000000X",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0,           0,  0, 0, },
55*e0c4386eSCy Schubert     { "19700101000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,           0, -1, 1, }, /* Epoch begins */
56*e0c4386eSCy Schubert     { "700101000000Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,           0, -1, 1, }, /* ditto */
57*e0c4386eSCy Schubert     { "20380119031407Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  0x7FFFFFFF,  1, 1, }, /* Max 32bit time_t */
58*e0c4386eSCy Schubert     { "380119031407Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,  0x7FFFFFFF,  1, 1, },
59*e0c4386eSCy Schubert     { "20371231235959Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  2145916799,  1, 1, }, /* Just before 2038 */
60*e0c4386eSCy Schubert     { "20371231235959Z",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         0,           0,  0, 1, }, /* Bad UTC time */
61*e0c4386eSCy Schubert     { "371231235959Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,  2145916799,  1, 1, },
62*e0c4386eSCy Schubert     { "19701006121456Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,    24063296, -1, 1, },
63*e0c4386eSCy Schubert     { "701006121456Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,    24063296, -1, 1, },
64*e0c4386eSCy Schubert     { "19991231000000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, }, /* Match baseline */
65*e0c4386eSCy Schubert     { "199912310000Z",     V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, }, /* In various flavors */
66*e0c4386eSCy Schubert     { "991231000000Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
67*e0c4386eSCy Schubert     { "9912310000Z",       V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
68*e0c4386eSCy Schubert     { "9912310000+0000",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
69*e0c4386eSCy Schubert     { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
70*e0c4386eSCy Schubert     { "9912310000-0000",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
71*e0c4386eSCy Schubert     { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
72*e0c4386eSCy Schubert     { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
73*e0c4386eSCy Schubert     { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
74*e0c4386eSCy Schubert     { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         0,   946598400,  0, 1, },
75*e0c4386eSCy Schubert     { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         0,   946598400,  0, 1, },
76*e0c4386eSCy Schubert     { "9912310100+0100",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
77*e0c4386eSCy Schubert     { "9912302300-0100",   V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,   946598400,  0, 1, },
78*e0c4386eSCy Schubert };
79*e0c4386eSCy Schubert 
80*e0c4386eSCy Schubert /* ASSUMES SIGNED TIME_T */
81*e0c4386eSCy Schubert static struct testdata tbl_testdata_neg[] = {
82*e0c4386eSCy Schubert     { "19011213204552Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1,     INT_MIN, -1, 0, },
83*e0c4386eSCy Schubert     { "691006121456Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,    -7472704, -1, 1, },
84*e0c4386eSCy Schubert     { "19691006121456Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,    -7472704, -1, 1, },
85*e0c4386eSCy Schubert };
86*e0c4386eSCy Schubert 
87*e0c4386eSCy Schubert /* explicit casts to time_t short warnings on systems with 32-bit time_t */
88*e0c4386eSCy Schubert static struct testdata tbl_testdata_pos_64bit[] = {
89*e0c4386eSCy Schubert     { "20380119031408Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  (time_t)0x80000000,  1, 1, },
90*e0c4386eSCy Schubert     { "20380119031409Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME,         1,  (time_t)0x80000001,  1, 1, },
91*e0c4386eSCy Schubert     { "380119031408Z",     V_ASN1_UTCTIME,         V_ASN1_UTCTIME,         1,  (time_t)0x80000000,  1, 1, },
92*e0c4386eSCy Schubert     { "20500101120000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1,  (time_t)0x967b1ec0,  1, 0, },
93*e0c4386eSCy Schubert };
94*e0c4386eSCy Schubert 
95*e0c4386eSCy Schubert /* ASSUMES SIGNED TIME_T */
96*e0c4386eSCy Schubert static struct testdata tbl_testdata_neg_64bit[] = {
97*e0c4386eSCy Schubert     { "19011213204551Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
98*e0c4386eSCy Schubert     { "19000101120000Z",   V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
99*e0c4386eSCy Schubert };
100*e0c4386eSCy Schubert 
101*e0c4386eSCy Schubert /* A baseline time to compare to */
102*e0c4386eSCy Schubert static ASN1_TIME gtime = {
103*e0c4386eSCy Schubert     15,
104*e0c4386eSCy Schubert     V_ASN1_GENERALIZEDTIME,
105*e0c4386eSCy Schubert     (unsigned char*)"19991231000000Z",
106*e0c4386eSCy Schubert     0
107*e0c4386eSCy Schubert };
108*e0c4386eSCy Schubert static time_t gtime_t = 946598400;
109*e0c4386eSCy Schubert 
test_table(struct testdata * tbl,int idx)110*e0c4386eSCy Schubert static int test_table(struct testdata *tbl, int idx)
111*e0c4386eSCy Schubert {
112*e0c4386eSCy Schubert     int error = 0;
113*e0c4386eSCy Schubert     ASN1_TIME atime;
114*e0c4386eSCy Schubert     ASN1_TIME *ptime;
115*e0c4386eSCy Schubert     struct testdata *td = &tbl[idx];
116*e0c4386eSCy Schubert     int day, sec;
117*e0c4386eSCy Schubert 
118*e0c4386eSCy Schubert     atime.data = (unsigned char*)td->data;
119*e0c4386eSCy Schubert     atime.length = strlen((char*)atime.data);
120*e0c4386eSCy Schubert     atime.type = td->type;
121*e0c4386eSCy Schubert     atime.flags = 0;
122*e0c4386eSCy Schubert 
123*e0c4386eSCy Schubert     if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
124*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
125*e0c4386eSCy Schubert         error = 1;
126*e0c4386eSCy Schubert     }
127*e0c4386eSCy Schubert     if (td->check_result == 0)
128*e0c4386eSCy Schubert         return 1;
129*e0c4386eSCy Schubert 
130*e0c4386eSCy Schubert     if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
131*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
132*e0c4386eSCy Schubert         error = 1;
133*e0c4386eSCy Schubert     }
134*e0c4386eSCy Schubert 
135*e0c4386eSCy Schubert     if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
136*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
137*e0c4386eSCy Schubert         error = 1;
138*e0c4386eSCy Schubert     }
139*e0c4386eSCy Schubert     if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
140*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
141*e0c4386eSCy Schubert         error = 1;
142*e0c4386eSCy Schubert     }
143*e0c4386eSCy Schubert 
144*e0c4386eSCy Schubert     if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
145*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
146*e0c4386eSCy Schubert         error = 1;
147*e0c4386eSCy Schubert     } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
148*e0c4386eSCy Schubert                  (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
149*e0c4386eSCy Schubert                  (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
150*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
151*e0c4386eSCy Schubert         error = 1;
152*e0c4386eSCy Schubert     }
153*e0c4386eSCy Schubert 
154*e0c4386eSCy Schubert     if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
155*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
156*e0c4386eSCy Schubert         error = 1;
157*e0c4386eSCy Schubert     }
158*e0c4386eSCy Schubert 
159*e0c4386eSCy Schubert     ptime = ASN1_TIME_set(NULL, td->t);
160*e0c4386eSCy Schubert     if (!TEST_ptr(ptime)) {
161*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
162*e0c4386eSCy Schubert         error = 1;
163*e0c4386eSCy Schubert     } else {
164*e0c4386eSCy Schubert         int local_error = 0;
165*e0c4386eSCy Schubert         if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
166*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
167*e0c4386eSCy Schubert                     (long)td->t, td->data, ptime->data);
168*e0c4386eSCy Schubert             local_error = error = 1;
169*e0c4386eSCy Schubert         }
170*e0c4386eSCy Schubert         if (!TEST_int_eq(ptime->type, td->expected_type)) {
171*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
172*e0c4386eSCy Schubert             local_error = error = 1;
173*e0c4386eSCy Schubert         }
174*e0c4386eSCy Schubert         if (local_error)
175*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
176*e0c4386eSCy Schubert         ASN1_TIME_free(ptime);
177*e0c4386eSCy Schubert     }
178*e0c4386eSCy Schubert 
179*e0c4386eSCy Schubert     ptime = ASN1_TIME_new();
180*e0c4386eSCy Schubert     if (!TEST_ptr(ptime)) {
181*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_new() failed");
182*e0c4386eSCy Schubert         error = 1;
183*e0c4386eSCy Schubert     } else {
184*e0c4386eSCy Schubert         int local_error = 0;
185*e0c4386eSCy Schubert         if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
186*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
187*e0c4386eSCy Schubert             local_error = error = 1;
188*e0c4386eSCy Schubert         }
189*e0c4386eSCy Schubert         if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
190*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
191*e0c4386eSCy Schubert             local_error = error = 1;
192*e0c4386eSCy Schubert         }
193*e0c4386eSCy Schubert         if (!TEST_int_eq(ptime->type, td->expected_type)) {
194*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
195*e0c4386eSCy Schubert             local_error = error = 1;
196*e0c4386eSCy Schubert         }
197*e0c4386eSCy Schubert         day = sec = 0;
198*e0c4386eSCy Schubert         if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
199*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
200*e0c4386eSCy Schubert             local_error = error = 1;
201*e0c4386eSCy Schubert         }
202*e0c4386eSCy Schubert         if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
203*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
204*e0c4386eSCy Schubert             local_error = error = 1;
205*e0c4386eSCy Schubert         }
206*e0c4386eSCy Schubert         if (local_error)
207*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
208*e0c4386eSCy Schubert         ASN1_TIME_free(ptime);
209*e0c4386eSCy Schubert     }
210*e0c4386eSCy Schubert 
211*e0c4386eSCy Schubert     ptime = ASN1_TIME_new();
212*e0c4386eSCy Schubert     if (!TEST_ptr(ptime)) {
213*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_new() failed");
214*e0c4386eSCy Schubert         error = 1;
215*e0c4386eSCy Schubert     } else {
216*e0c4386eSCy Schubert         int local_error = 0;
217*e0c4386eSCy Schubert         if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
218*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
219*e0c4386eSCy Schubert             local_error = error = 1;
220*e0c4386eSCy Schubert         }
221*e0c4386eSCy Schubert         day = sec = 0;
222*e0c4386eSCy Schubert         if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
223*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
224*e0c4386eSCy Schubert             local_error = error = 1;
225*e0c4386eSCy Schubert         }
226*e0c4386eSCy Schubert         if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
227*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
228*e0c4386eSCy Schubert             local_error = error = 1;
229*e0c4386eSCy Schubert         }
230*e0c4386eSCy Schubert         if (local_error)
231*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
232*e0c4386eSCy Schubert         ASN1_TIME_free(ptime);
233*e0c4386eSCy Schubert     }
234*e0c4386eSCy Schubert 
235*e0c4386eSCy Schubert     if (td->type == V_ASN1_UTCTIME) {
236*e0c4386eSCy Schubert         ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
237*e0c4386eSCy Schubert         if (td->convert_result == 1 && !TEST_ptr(ptime)) {
238*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
239*e0c4386eSCy Schubert             error = 1;
240*e0c4386eSCy Schubert         } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
241*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
242*e0c4386eSCy Schubert             error = 1;
243*e0c4386eSCy Schubert         }
244*e0c4386eSCy Schubert         if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
245*e0c4386eSCy Schubert             TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
246*e0c4386eSCy Schubert             error = 1;
247*e0c4386eSCy Schubert         }
248*e0c4386eSCy Schubert         ASN1_TIME_free(ptime);
249*e0c4386eSCy Schubert     }
250*e0c4386eSCy Schubert     /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
251*e0c4386eSCy Schubert 
252*e0c4386eSCy Schubert     if (error)
253*e0c4386eSCy Schubert         TEST_error("atime=%s", atime.data);
254*e0c4386eSCy Schubert 
255*e0c4386eSCy Schubert     return !error;
256*e0c4386eSCy Schubert }
257*e0c4386eSCy Schubert 
test_table_pos(int idx)258*e0c4386eSCy Schubert static int test_table_pos(int idx)
259*e0c4386eSCy Schubert {
260*e0c4386eSCy Schubert     return test_table(tbl_testdata_pos, idx);
261*e0c4386eSCy Schubert }
262*e0c4386eSCy Schubert 
test_table_neg(int idx)263*e0c4386eSCy Schubert static int test_table_neg(int idx)
264*e0c4386eSCy Schubert {
265*e0c4386eSCy Schubert     return test_table(tbl_testdata_neg, idx);
266*e0c4386eSCy Schubert }
267*e0c4386eSCy Schubert 
test_table_pos_64bit(int idx)268*e0c4386eSCy Schubert static int test_table_pos_64bit(int idx)
269*e0c4386eSCy Schubert {
270*e0c4386eSCy Schubert     return test_table(tbl_testdata_pos_64bit, idx);
271*e0c4386eSCy Schubert }
272*e0c4386eSCy Schubert 
test_table_neg_64bit(int idx)273*e0c4386eSCy Schubert static int test_table_neg_64bit(int idx)
274*e0c4386eSCy Schubert {
275*e0c4386eSCy Schubert     return test_table(tbl_testdata_neg_64bit, idx);
276*e0c4386eSCy Schubert }
277*e0c4386eSCy Schubert 
278*e0c4386eSCy Schubert struct compare_testdata {
279*e0c4386eSCy Schubert     ASN1_TIME t1;
280*e0c4386eSCy Schubert     ASN1_TIME t2;
281*e0c4386eSCy Schubert     int result;
282*e0c4386eSCy Schubert };
283*e0c4386eSCy Schubert 
284*e0c4386eSCy Schubert static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
285*e0c4386eSCy Schubert static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
286*e0c4386eSCy Schubert static unsigned char TODAY_UTC_STR[] = "170825000000Z";
287*e0c4386eSCy Schubert static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
288*e0c4386eSCy Schubert 
289*e0c4386eSCy Schubert #define TODAY_GEN    { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
290*e0c4386eSCy Schubert #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
291*e0c4386eSCy Schubert #define TODAY_UTC    { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
292*e0c4386eSCy Schubert #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
293*e0c4386eSCy Schubert 
294*e0c4386eSCy Schubert static struct compare_testdata tbl_compare_testdata[] = {
295*e0c4386eSCy Schubert     { TODAY_GEN,    TODAY_GEN,     0 },
296*e0c4386eSCy Schubert     { TODAY_GEN,    TODAY_UTC,     0 },
297*e0c4386eSCy Schubert     { TODAY_GEN,    TOMORROW_GEN, -1 },
298*e0c4386eSCy Schubert     { TODAY_GEN,    TOMORROW_UTC, -1 },
299*e0c4386eSCy Schubert 
300*e0c4386eSCy Schubert     { TODAY_UTC,    TODAY_GEN,     0 },
301*e0c4386eSCy Schubert     { TODAY_UTC,    TODAY_UTC,     0 },
302*e0c4386eSCy Schubert     { TODAY_UTC,    TOMORROW_GEN, -1 },
303*e0c4386eSCy Schubert     { TODAY_UTC,    TOMORROW_UTC, -1 },
304*e0c4386eSCy Schubert 
305*e0c4386eSCy Schubert     { TOMORROW_GEN, TODAY_GEN,     1 },
306*e0c4386eSCy Schubert     { TOMORROW_GEN, TODAY_UTC,     1 },
307*e0c4386eSCy Schubert     { TOMORROW_GEN, TOMORROW_GEN,  0 },
308*e0c4386eSCy Schubert     { TOMORROW_GEN, TOMORROW_UTC,  0 },
309*e0c4386eSCy Schubert 
310*e0c4386eSCy Schubert     { TOMORROW_UTC, TODAY_GEN,     1 },
311*e0c4386eSCy Schubert     { TOMORROW_UTC, TODAY_UTC,     1 },
312*e0c4386eSCy Schubert     { TOMORROW_UTC, TOMORROW_GEN,  0 },
313*e0c4386eSCy Schubert     { TOMORROW_UTC, TOMORROW_UTC,  0 }
314*e0c4386eSCy Schubert };
315*e0c4386eSCy Schubert 
test_table_compare(int idx)316*e0c4386eSCy Schubert static int test_table_compare(int idx)
317*e0c4386eSCy Schubert {
318*e0c4386eSCy Schubert     struct compare_testdata *td = &tbl_compare_testdata[idx];
319*e0c4386eSCy Schubert 
320*e0c4386eSCy Schubert     return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
321*e0c4386eSCy Schubert }
322*e0c4386eSCy Schubert 
test_time_dup(void)323*e0c4386eSCy Schubert static int test_time_dup(void)
324*e0c4386eSCy Schubert {
325*e0c4386eSCy Schubert     int ret = 0;
326*e0c4386eSCy Schubert     ASN1_TIME *asn1_time = NULL;
327*e0c4386eSCy Schubert     ASN1_TIME *asn1_time_dup = NULL;
328*e0c4386eSCy Schubert     ASN1_TIME *asn1_gentime = NULL;
329*e0c4386eSCy Schubert 
330*e0c4386eSCy Schubert     asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
331*e0c4386eSCy Schubert     if (asn1_time == NULL) {
332*e0c4386eSCy Schubert         TEST_info("Internal error.");
333*e0c4386eSCy Schubert         goto err;
334*e0c4386eSCy Schubert     }
335*e0c4386eSCy Schubert 
336*e0c4386eSCy Schubert     asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
337*e0c4386eSCy Schubert     if (asn1_gentime == NULL) {
338*e0c4386eSCy Schubert         TEST_info("Internal error.");
339*e0c4386eSCy Schubert         goto err;
340*e0c4386eSCy Schubert     }
341*e0c4386eSCy Schubert 
342*e0c4386eSCy Schubert     asn1_time_dup = ASN1_TIME_dup(asn1_time);
343*e0c4386eSCy Schubert     if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
344*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_dup() failed.");
345*e0c4386eSCy Schubert         goto err;
346*e0c4386eSCy Schubert     }
347*e0c4386eSCy Schubert     if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
348*e0c4386eSCy Schubert         TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
349*e0c4386eSCy Schubert         goto err;
350*e0c4386eSCy Schubert     }
351*e0c4386eSCy Schubert     ASN1_STRING_free(asn1_time_dup);
352*e0c4386eSCy Schubert 
353*e0c4386eSCy Schubert     asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
354*e0c4386eSCy Schubert     if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
355*e0c4386eSCy Schubert         TEST_info("ASN1_UTCTIME_dup() failed.");
356*e0c4386eSCy Schubert         goto err;
357*e0c4386eSCy Schubert     }
358*e0c4386eSCy Schubert     if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
359*e0c4386eSCy Schubert         TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
360*e0c4386eSCy Schubert         goto err;
361*e0c4386eSCy Schubert     }
362*e0c4386eSCy Schubert     ASN1_STRING_free(asn1_time_dup);
363*e0c4386eSCy Schubert 
364*e0c4386eSCy Schubert     asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
365*e0c4386eSCy Schubert     if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
366*e0c4386eSCy Schubert         TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
367*e0c4386eSCy Schubert         goto err;
368*e0c4386eSCy Schubert     }
369*e0c4386eSCy Schubert     if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
370*e0c4386eSCy Schubert         TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
371*e0c4386eSCy Schubert         goto err;
372*e0c4386eSCy Schubert     }
373*e0c4386eSCy Schubert 
374*e0c4386eSCy Schubert     ret = 1;
375*e0c4386eSCy Schubert  err:
376*e0c4386eSCy Schubert     ASN1_STRING_free(asn1_time);
377*e0c4386eSCy Schubert     ASN1_STRING_free(asn1_gentime);
378*e0c4386eSCy Schubert     ASN1_STRING_free(asn1_time_dup);
379*e0c4386eSCy Schubert     return ret;
380*e0c4386eSCy Schubert }
381*e0c4386eSCy Schubert 
setup_tests(void)382*e0c4386eSCy Schubert int setup_tests(void)
383*e0c4386eSCy Schubert {
384*e0c4386eSCy Schubert     /*
385*e0c4386eSCy Schubert      * On platforms where |time_t| is an unsigned integer, t will be a
386*e0c4386eSCy Schubert      * positive number.
387*e0c4386eSCy Schubert      *
388*e0c4386eSCy Schubert      * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
389*e0c4386eSCy Schubert      * because some compilers are picky if you do 't < 0', or even 't <= 0'
390*e0c4386eSCy Schubert      * if |t| is unsigned.
391*e0c4386eSCy Schubert      */
392*e0c4386eSCy Schubert     time_t t = -1;
393*e0c4386eSCy Schubert     /*
394*e0c4386eSCy Schubert      * On some platforms, |time_t| is signed, but a negative value is an
395*e0c4386eSCy Schubert      * error, and using it with gmtime() or localtime() generates a NULL.
396*e0c4386eSCy Schubert      * If that is the case, we can't perform tests on negative values.
397*e0c4386eSCy Schubert      */
398*e0c4386eSCy Schubert     struct tm *ptm = localtime(&t);
399*e0c4386eSCy Schubert 
400*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
401*e0c4386eSCy Schubert     if (!(t > 0) && ptm != NULL) {
402*e0c4386eSCy Schubert         TEST_info("Adding negative-sign time_t tests");
403*e0c4386eSCy Schubert         ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
404*e0c4386eSCy Schubert     }
405*e0c4386eSCy Schubert     if (sizeof(time_t) > sizeof(uint32_t)) {
406*e0c4386eSCy Schubert         TEST_info("Adding 64-bit time_t tests");
407*e0c4386eSCy Schubert         ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
408*e0c4386eSCy Schubert #ifndef __hpux
409*e0c4386eSCy Schubert         if (!(t > 0) && ptm != NULL) {
410*e0c4386eSCy Schubert             TEST_info("Adding negative-sign 64-bit time_t tests");
411*e0c4386eSCy Schubert             ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
412*e0c4386eSCy Schubert         }
413*e0c4386eSCy Schubert #endif
414*e0c4386eSCy Schubert     }
415*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
416*e0c4386eSCy Schubert     ADD_TEST(test_time_dup);
417*e0c4386eSCy Schubert     return 1;
418*e0c4386eSCy Schubert }
419