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