2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Tests for X509 time functions */
15 #include <openssl/asn1.h>
16 #include <openssl/x509.h>
24 /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
28 static TESTDATA x509_cmp_tests[] = {
30 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
31 /* The same in seconds since epoch. */
35 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
36 /* One second more. */
40 "20170217180154Z", V_ASN1_GENERALIZEDTIME,
41 /* One second less. */
44 /* Same as UTC time. */
46 "170217180154Z", V_ASN1_UTCTIME,
47 /* The same in seconds since epoch. */
51 "170217180154Z", V_ASN1_UTCTIME,
52 /* One second more. */
56 "170217180154Z", V_ASN1_UTCTIME,
57 /* One second less. */
60 /* UTCTime from the 20th century. */
62 "990217180154Z", V_ASN1_UTCTIME,
63 /* The same in seconds since epoch. */
67 "990217180154Z", V_ASN1_UTCTIME,
68 /* One second more. */
72 "990217180154Z", V_ASN1_UTCTIME,
73 /* One second less. */
76 /* Various invalid formats. */
79 "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
82 /* No trailing Z, UTCTime. */
83 "170217180154", V_ASN1_UTCTIME, 0, 0,
87 "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
90 /* No seconds, UTCTime. */
91 "1702171801Z", V_ASN1_UTCTIME, 0, 0,
94 /* Fractional seconds. */
95 "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
98 /* Fractional seconds, UTCTime. */
99 "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
102 /* Timezone offset. */
103 "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
106 /* Timezone offset, UTCTime. */
107 "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
111 "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
114 /* Extra digits, UTCTime. */
115 "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
119 "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
122 /* Non-digits, UTCTime. */
123 "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
126 /* Trailing garbage. */
127 "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
130 /* Trailing garbage, UTCTime. */
131 "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
135 "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
139 "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
143 "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
147 static int test_x509_cmp_time(int idx)
152 memset(&t, 0, sizeof(t));
153 t.type = x509_cmp_tests[idx].type;
154 t.data = (unsigned char*)(x509_cmp_tests[idx].data);
155 t.length = strlen(x509_cmp_tests[idx].data);
157 result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
158 if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
159 TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n",
160 idx, x509_cmp_tests[idx].expected, result);
166 static int test_x509_cmp_time_current()
168 time_t now = time(NULL);
169 /* Pick a day earlier and later, relative to any system clock. */
170 ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
171 int cmp_result, failed = 0;
173 asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
174 asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
176 cmp_result = X509_cmp_time(asn1_before, NULL);
177 if (!TEST_int_eq(cmp_result, -1))
180 cmp_result = X509_cmp_time(asn1_after, NULL);
181 if (!TEST_int_eq(cmp_result, 1))
184 ASN1_TIME_free(asn1_before);
185 ASN1_TIME_free(asn1_after);
190 void register_tests()
192 ADD_TEST(test_x509_cmp_time_current);
193 ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));