• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 // Tests for X509 time functions.
11 
12 #include <openssl/x509.h>
13 
14 #include <string.h>
15 #include <time.h>
16 
17 #include <gtest/gtest.h>
18 #include <openssl/asn1.h>
19 
20 struct TestData {
21   const char *data;
22   int type;
23   time_t cmp_time;
24   // -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error.
25   int expected;
26 };
27 
28 static TestData kX509CmpTests[] = {
29     {
30         "20170217180154Z",
31         V_ASN1_GENERALIZEDTIME,
32         // The same in seconds since epoch.
33         1487354514,
34         -1,
35     },
36     {
37         "20170217180154Z",
38         V_ASN1_GENERALIZEDTIME,
39         // One second more.
40         1487354515,
41         -1,
42     },
43     {
44         "20170217180154Z",
45         V_ASN1_GENERALIZEDTIME,
46         // One second less.
47         1487354513,
48         1,
49     },
50     // Same as UTC time.
51     {
52         "170217180154Z",
53         V_ASN1_UTCTIME,
54         // The same in seconds since epoch.
55         1487354514,
56         -1,
57     },
58     {
59         "170217180154Z",
60         V_ASN1_UTCTIME,
61         // One second more.
62         1487354515,
63         -1,
64     },
65     {
66         "170217180154Z",
67         V_ASN1_UTCTIME,
68         // One second less.
69         1487354513,
70         1,
71     },
72     // UTCTime from the 20th century.
73     {
74         "990217180154Z",
75         V_ASN1_UTCTIME,
76         // The same in seconds since epoch.
77         919274514,
78         -1,
79     },
80     {
81         "990217180154Z",
82         V_ASN1_UTCTIME,
83         // One second more.
84         919274515,
85         -1,
86     },
87     {
88         "990217180154Z",
89         V_ASN1_UTCTIME,
90         // One second less.
91         919274513,
92         1,
93     },
94     // Various invalid formats.
95     {
96         // No trailing Z.
97         "20170217180154",
98         V_ASN1_GENERALIZEDTIME,
99         0,
100         0,
101     },
102     {
103         // No trailing Z, UTCTime.
104         "170217180154",
105         V_ASN1_UTCTIME,
106         0,
107         0,
108     },
109     {
110         // No seconds.
111         "201702171801Z",
112         V_ASN1_GENERALIZEDTIME,
113         0,
114         0,
115     },
116     {
117         // No seconds, UTCTime.
118         "1702171801Z",
119         V_ASN1_UTCTIME,
120         0,
121         0,
122     },
123     {
124         // Fractional seconds.
125         "20170217180154.001Z",
126         V_ASN1_GENERALIZEDTIME,
127         0,
128         0,
129     },
130     {
131         // Fractional seconds, UTCTime.
132         "170217180154.001Z",
133         V_ASN1_UTCTIME,
134         0,
135         0,
136     },
137     {
138         // Timezone offset.
139         "20170217180154+0100",
140         V_ASN1_GENERALIZEDTIME,
141         0,
142         0,
143     },
144     {
145         // Timezone offset, UTCTime.
146         "170217180154+0100",
147         V_ASN1_UTCTIME,
148         0,
149         0,
150     },
151     {
152         // Extra digits.
153         "2017021718015400Z",
154         V_ASN1_GENERALIZEDTIME,
155         0,
156         0,
157     },
158     {
159         // Extra digits, UTCTime.
160         "17021718015400Z",
161         V_ASN1_UTCTIME,
162         0,
163         0,
164     },
165     {
166         // Non-digits.
167         "2017021718015aZ",
168         V_ASN1_GENERALIZEDTIME,
169         0,
170         0,
171     },
172     {
173         // Non-digits, UTCTime.
174         "17021718015aZ",
175         V_ASN1_UTCTIME,
176         0,
177         0,
178     },
179     {
180         // Trailing garbage.
181         "20170217180154Zlongtrailinggarbage",
182         V_ASN1_GENERALIZEDTIME,
183         0,
184         0,
185     },
186     {
187         // Trailing garbage, UTCTime.
188         "170217180154Zlongtrailinggarbage",
189         V_ASN1_UTCTIME,
190         0,
191         0,
192     },
193     {
194         // Swapped type.
195         "20170217180154Z",
196         V_ASN1_UTCTIME,
197         0,
198         0,
199     },
200     {
201         // Swapped type.
202         "170217180154Z",
203         V_ASN1_GENERALIZEDTIME,
204         0,
205         0,
206     },
207     {
208         // Bad type.
209         "20170217180154Z",
210         V_ASN1_OCTET_STRING,
211         0,
212         0,
213     },
214 };
215 
TEST(X509TimeTest,TestCmpTime)216 TEST(X509TimeTest, TestCmpTime) {
217   for (auto &test : kX509CmpTests) {
218     SCOPED_TRACE(test.data);
219 
220     ASN1_TIME t;
221 
222     memset(&t, 0, sizeof(t));
223     t.type = test.type;
224     t.data = (unsigned char*) test.data;
225     t.length = strlen(test.data);
226 
227     EXPECT_EQ(test.expected,
228               X509_cmp_time(&t, &test.cmp_time));
229   }
230 }
231 
TEST(X509TimeTest,TestCmpTimeCurrent)232 TEST(X509TimeTest, TestCmpTimeCurrent) {
233   time_t now = time(NULL);
234   // Pick a day earlier and later, relative to any system clock.
235   bssl::UniquePtr<ASN1_TIME> asn1_before(ASN1_TIME_adj(NULL, now, -1, 0));
236   bssl::UniquePtr<ASN1_TIME> asn1_after(ASN1_TIME_adj(NULL, now, 1, 0));
237 
238   ASSERT_EQ(-1, X509_cmp_time(asn1_before.get(), NULL));
239   ASSERT_EQ(1, X509_cmp_time(asn1_after.get(), NULL));
240 }
241