1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2017-2018 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_t/offset (+/-XXXX) tests for ASN1 and X509 */
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubert #include <stdio.h>
13*e0c4386eSCy Schubert #include <string.h>
14*e0c4386eSCy Schubert #include <time.h>
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <openssl/asn1.h>
17*e0c4386eSCy Schubert #include <openssl/x509.h>
18*e0c4386eSCy Schubert #include "testutil.h"
19*e0c4386eSCy Schubert #include "internal/nelem.h"
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert typedef struct {
22*e0c4386eSCy Schubert const char *data;
23*e0c4386eSCy Schubert int time_result;
24*e0c4386eSCy Schubert int type;
25*e0c4386eSCy Schubert } TESTDATA;
26*e0c4386eSCy Schubert
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert /**********************************************************************
29*e0c4386eSCy Schubert *
30*e0c4386eSCy Schubert * Test driver
31*e0c4386eSCy Schubert *
32*e0c4386eSCy Schubert ***/
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert static TESTDATA tests[] = {
35*e0c4386eSCy Schubert { "20001201000000Z", 0, V_ASN1_GENERALIZEDTIME },
36*e0c4386eSCy Schubert { "20001201010000+0100", 0, V_ASN1_GENERALIZEDTIME },
37*e0c4386eSCy Schubert { "20001201050000+0500", 0, V_ASN1_GENERALIZEDTIME },
38*e0c4386eSCy Schubert { "20001130230000-0100", 0, V_ASN1_GENERALIZEDTIME },
39*e0c4386eSCy Schubert { "20001130190000-0500", 0, V_ASN1_GENERALIZEDTIME },
40*e0c4386eSCy Schubert { "20001130190001-0500", 1, V_ASN1_GENERALIZEDTIME }, /* +1 second */
41*e0c4386eSCy Schubert { "20001130185959-0500", -1, V_ASN1_GENERALIZEDTIME }, /* -1 second */
42*e0c4386eSCy Schubert { "001201000000Z", 0, V_ASN1_UTCTIME },
43*e0c4386eSCy Schubert { "001201010000+0100", 0, V_ASN1_UTCTIME },
44*e0c4386eSCy Schubert { "001201050000+0500", 0, V_ASN1_UTCTIME },
45*e0c4386eSCy Schubert { "001130230000-0100", 0, V_ASN1_UTCTIME },
46*e0c4386eSCy Schubert { "001130190000-0500", 0, V_ASN1_UTCTIME },
47*e0c4386eSCy Schubert { "001201000000-0000", 0, V_ASN1_UTCTIME },
48*e0c4386eSCy Schubert { "001201000001-0000", 1, V_ASN1_UTCTIME }, /* +1 second */
49*e0c4386eSCy Schubert { "001130235959-0000", -1, V_ASN1_UTCTIME }, /* -1 second */
50*e0c4386eSCy Schubert { "20001201000000+0000", 0, V_ASN1_GENERALIZEDTIME },
51*e0c4386eSCy Schubert { "20001201000000+0100", -1, V_ASN1_GENERALIZEDTIME },
52*e0c4386eSCy Schubert { "001201000000+0100", -1, V_ASN1_UTCTIME },
53*e0c4386eSCy Schubert { "20001201000000-0100", 1, V_ASN1_GENERALIZEDTIME },
54*e0c4386eSCy Schubert { "001201000000-0100", 1, V_ASN1_UTCTIME },
55*e0c4386eSCy Schubert { "20001201123400+1234", 0, V_ASN1_GENERALIZEDTIME },
56*e0c4386eSCy Schubert { "20001130112600-1234", 0, V_ASN1_GENERALIZEDTIME },
57*e0c4386eSCy Schubert };
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert static time_t the_time = 975628800;
60*e0c4386eSCy Schubert static ASN1_TIME the_asn1_time = {
61*e0c4386eSCy Schubert 15,
62*e0c4386eSCy Schubert V_ASN1_GENERALIZEDTIME,
63*e0c4386eSCy Schubert (unsigned char*)"20001201000000Z",
64*e0c4386eSCy Schubert 0
65*e0c4386eSCy Schubert };
66*e0c4386eSCy Schubert
test_offset(int idx)67*e0c4386eSCy Schubert static int test_offset(int idx)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert ASN1_TIME at;
70*e0c4386eSCy Schubert const TESTDATA *testdata = &tests[idx];
71*e0c4386eSCy Schubert int ret = -2;
72*e0c4386eSCy Schubert int day, sec;
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert at.data = (unsigned char*)testdata->data;
75*e0c4386eSCy Schubert at.length = strlen(testdata->data);
76*e0c4386eSCy Schubert at.type = testdata->type;
77*e0c4386eSCy Schubert at.flags = 0;
78*e0c4386eSCy Schubert
79*e0c4386eSCy Schubert if (!TEST_true(ASN1_TIME_diff(&day, &sec, &the_asn1_time, &at))) {
80*e0c4386eSCy Schubert TEST_info("ASN1_TIME_diff() failed for %s\n", at.data);
81*e0c4386eSCy Schubert return 0;
82*e0c4386eSCy Schubert }
83*e0c4386eSCy Schubert if (day > 0)
84*e0c4386eSCy Schubert ret = 1;
85*e0c4386eSCy Schubert else if (day < 0)
86*e0c4386eSCy Schubert ret = -1;
87*e0c4386eSCy Schubert else if (sec > 0)
88*e0c4386eSCy Schubert ret = 1;
89*e0c4386eSCy Schubert else if (sec < 0)
90*e0c4386eSCy Schubert ret = -1;
91*e0c4386eSCy Schubert else
92*e0c4386eSCy Schubert ret = 0;
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert if (!TEST_int_eq(testdata->time_result, ret)) {
95*e0c4386eSCy Schubert TEST_info("ASN1_TIME_diff() test failed for %s day=%d sec=%d\n", at.data, day, sec);
96*e0c4386eSCy Schubert return 0;
97*e0c4386eSCy Schubert }
98*e0c4386eSCy Schubert
99*e0c4386eSCy Schubert ret = ASN1_TIME_cmp_time_t(&at, the_time);
100*e0c4386eSCy Schubert
101*e0c4386eSCy Schubert if (!TEST_int_eq(testdata->time_result, ret)) {
102*e0c4386eSCy Schubert TEST_info("ASN1_UTCTIME_cmp_time_t() test failed for %s\n", at.data);
103*e0c4386eSCy Schubert return 0;
104*e0c4386eSCy Schubert }
105*e0c4386eSCy Schubert
106*e0c4386eSCy Schubert return 1;
107*e0c4386eSCy Schubert }
108*e0c4386eSCy Schubert
setup_tests(void)109*e0c4386eSCy Schubert int setup_tests(void)
110*e0c4386eSCy Schubert {
111*e0c4386eSCy Schubert ADD_ALL_TESTS(test_offset, OSSL_NELEM(tests));
112*e0c4386eSCy Schubert return 1;
113*e0c4386eSCy Schubert }
114