1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* time_t/offset (+/-XXXX) tests for ASN1 and X509 */
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#include <stdio.h>
13e1051a39Sopenharmony_ci#include <string.h>
14e1051a39Sopenharmony_ci#include <time.h>
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/asn1.h>
17e1051a39Sopenharmony_ci#include <openssl/x509.h>
18e1051a39Sopenharmony_ci#include "testutil.h"
19e1051a39Sopenharmony_ci#include "internal/nelem.h"
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_citypedef struct {
22e1051a39Sopenharmony_ci    const char *data;
23e1051a39Sopenharmony_ci    int time_result;
24e1051a39Sopenharmony_ci    int type;
25e1051a39Sopenharmony_ci} TESTDATA;
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci/**********************************************************************
29e1051a39Sopenharmony_ci *
30e1051a39Sopenharmony_ci * Test driver
31e1051a39Sopenharmony_ci *
32e1051a39Sopenharmony_ci ***/
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_cistatic TESTDATA tests[] = {
35e1051a39Sopenharmony_ci    { "20001201000000Z",      0, V_ASN1_GENERALIZEDTIME },
36e1051a39Sopenharmony_ci    { "20001201010000+0100",  0, V_ASN1_GENERALIZEDTIME },
37e1051a39Sopenharmony_ci    { "20001201050000+0500",  0, V_ASN1_GENERALIZEDTIME },
38e1051a39Sopenharmony_ci    { "20001130230000-0100",  0, V_ASN1_GENERALIZEDTIME },
39e1051a39Sopenharmony_ci    { "20001130190000-0500",  0, V_ASN1_GENERALIZEDTIME },
40e1051a39Sopenharmony_ci    { "20001130190001-0500",  1, V_ASN1_GENERALIZEDTIME }, /* +1 second */
41e1051a39Sopenharmony_ci    { "20001130185959-0500", -1, V_ASN1_GENERALIZEDTIME }, /* -1 second */
42e1051a39Sopenharmony_ci    { "001201000000Z",        0, V_ASN1_UTCTIME },
43e1051a39Sopenharmony_ci    { "001201010000+0100",    0, V_ASN1_UTCTIME },
44e1051a39Sopenharmony_ci    { "001201050000+0500",    0, V_ASN1_UTCTIME },
45e1051a39Sopenharmony_ci    { "001130230000-0100",    0, V_ASN1_UTCTIME },
46e1051a39Sopenharmony_ci    { "001130190000-0500",    0, V_ASN1_UTCTIME },
47e1051a39Sopenharmony_ci    { "001201000000-0000",    0, V_ASN1_UTCTIME },
48e1051a39Sopenharmony_ci    { "001201000001-0000",    1, V_ASN1_UTCTIME }, /* +1 second */
49e1051a39Sopenharmony_ci    { "001130235959-0000",   -1, V_ASN1_UTCTIME }, /* -1 second */
50e1051a39Sopenharmony_ci    { "20001201000000+0000",  0, V_ASN1_GENERALIZEDTIME },
51e1051a39Sopenharmony_ci    { "20001201000000+0100", -1, V_ASN1_GENERALIZEDTIME },
52e1051a39Sopenharmony_ci    { "001201000000+0100",   -1, V_ASN1_UTCTIME },
53e1051a39Sopenharmony_ci    { "20001201000000-0100",  1, V_ASN1_GENERALIZEDTIME },
54e1051a39Sopenharmony_ci    { "001201000000-0100",    1, V_ASN1_UTCTIME },
55e1051a39Sopenharmony_ci    { "20001201123400+1234",  0, V_ASN1_GENERALIZEDTIME },
56e1051a39Sopenharmony_ci    { "20001130112600-1234",  0, V_ASN1_GENERALIZEDTIME },
57e1051a39Sopenharmony_ci};
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_cistatic time_t the_time = 975628800;
60e1051a39Sopenharmony_cistatic ASN1_TIME the_asn1_time = {
61e1051a39Sopenharmony_ci    15,
62e1051a39Sopenharmony_ci    V_ASN1_GENERALIZEDTIME,
63e1051a39Sopenharmony_ci    (unsigned char*)"20001201000000Z",
64e1051a39Sopenharmony_ci    0
65e1051a39Sopenharmony_ci};
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_cistatic int test_offset(int idx)
68e1051a39Sopenharmony_ci{
69e1051a39Sopenharmony_ci    ASN1_TIME at;
70e1051a39Sopenharmony_ci    const TESTDATA *testdata = &tests[idx];
71e1051a39Sopenharmony_ci    int ret = -2;
72e1051a39Sopenharmony_ci    int day, sec;
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    at.data = (unsigned char*)testdata->data;
75e1051a39Sopenharmony_ci    at.length = strlen(testdata->data);
76e1051a39Sopenharmony_ci    at.type = testdata->type;
77e1051a39Sopenharmony_ci    at.flags = 0;
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    if (!TEST_true(ASN1_TIME_diff(&day, &sec, &the_asn1_time, &at))) {
80e1051a39Sopenharmony_ci        TEST_info("ASN1_TIME_diff() failed for %s\n", at.data);
81e1051a39Sopenharmony_ci        return 0;
82e1051a39Sopenharmony_ci    }
83e1051a39Sopenharmony_ci    if (day > 0)
84e1051a39Sopenharmony_ci        ret = 1;
85e1051a39Sopenharmony_ci    else if (day < 0)
86e1051a39Sopenharmony_ci        ret = -1;
87e1051a39Sopenharmony_ci    else if (sec > 0)
88e1051a39Sopenharmony_ci        ret = 1;
89e1051a39Sopenharmony_ci    else if (sec < 0)
90e1051a39Sopenharmony_ci        ret = -1;
91e1051a39Sopenharmony_ci    else
92e1051a39Sopenharmony_ci        ret = 0;
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci    if (!TEST_int_eq(testdata->time_result, ret)) {
95e1051a39Sopenharmony_ci        TEST_info("ASN1_TIME_diff() test failed for %s day=%d sec=%d\n", at.data, day, sec);
96e1051a39Sopenharmony_ci        return 0;
97e1051a39Sopenharmony_ci    }
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ci    ret = ASN1_TIME_cmp_time_t(&at, the_time);
100e1051a39Sopenharmony_ci
101e1051a39Sopenharmony_ci    if (!TEST_int_eq(testdata->time_result, ret)) {
102e1051a39Sopenharmony_ci        TEST_info("ASN1_UTCTIME_cmp_time_t() test failed for %s\n", at.data);
103e1051a39Sopenharmony_ci        return 0;
104e1051a39Sopenharmony_ci    }
105e1051a39Sopenharmony_ci
106e1051a39Sopenharmony_ci    return 1;
107e1051a39Sopenharmony_ci}
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ciint setup_tests(void)
110e1051a39Sopenharmony_ci{
111e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_offset, OSSL_NELEM(tests));
112e1051a39Sopenharmony_ci    return 1;
113e1051a39Sopenharmony_ci}
114