diff options
author | beck <> | 2015-09-29 04:54:23 +0000 |
---|---|---|
committer | beck <> | 2015-09-29 04:54:23 +0000 |
commit | de844b83c11dcdd32e0d6d1bc81777bc60620c70 (patch) | |
tree | 06f00d73c86b532cc0915dc0c4c00792a0b82dfd | |
parent | fad0b6cea3bf75ce2428a6d4f8ad79068eb81444 (diff) | |
download | openbsd-de844b83c11dcdd32e0d6d1bc81777bc60620c70.tar.gz openbsd-de844b83c11dcdd32e0d6d1bc81777bc60620c70.tar.bz2 openbsd-de844b83c11dcdd32e0d6d1bc81777bc60620c70.zip |
Add an rfc5280 test suite to test x509_cmp_time.
Note some of these will yet fail with the current libcrypto as the current
X509_cmp_time is not RFC5280 compliant
ok jsing@
-rw-r--r-- | src/regress/lib/libcrypto/asn1/Makefile | 4 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/asn1/rfc5280time.c | 360 |
2 files changed, 362 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile index f7a2df5459..d4da1bf7a9 100644 --- a/src/regress/lib/libcrypto/asn1/Makefile +++ b/src/regress/lib/libcrypto/asn1/Makefile | |||
@@ -1,7 +1,7 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2015/09/25 16:12:30 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.2 2015/09/29 04:54:23 beck Exp $ |
2 | 2 | ||
3 | TESTS = \ | 3 | TESTS = \ |
4 | asn1time | 4 | asn1time rfc5280time |
5 | 5 | ||
6 | REGRESS_TARGETS= all_tests | 6 | REGRESS_TARGETS= all_tests |
7 | 7 | ||
diff --git a/src/regress/lib/libcrypto/asn1/rfc5280time.c b/src/regress/lib/libcrypto/asn1/rfc5280time.c new file mode 100644 index 0000000000..b74c5668d7 --- /dev/null +++ b/src/regress/lib/libcrypto/asn1/rfc5280time.c | |||
@@ -0,0 +1,360 @@ | |||
1 | /* $OpenBSD: rfc5280time.c,v 1.1 2015/09/29 04:54:23 beck Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2015 Joel Sing <jsing@openbsd.org> | ||
4 | * Copyright (c) 2015 Bob Beck <beck@opebsd.org> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <openssl/asn1.h> | ||
20 | #include <openssl/x509.h> | ||
21 | |||
22 | #include <err.h> | ||
23 | #include <stdio.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | struct rfc5280_time_test { | ||
27 | const char *str; | ||
28 | const char *data; | ||
29 | time_t time; | ||
30 | }; | ||
31 | |||
32 | struct rfc5280_time_test rfc5280_invtime_tests[] = { | ||
33 | { | ||
34 | .str = "", | ||
35 | }, | ||
36 | { | ||
37 | .str = "2015", | ||
38 | }, | ||
39 | { | ||
40 | .str = "201509", | ||
41 | }, | ||
42 | { | ||
43 | .str = "20150923", | ||
44 | }, | ||
45 | { | ||
46 | .str = "20150923032700", | ||
47 | }, | ||
48 | { | ||
49 | /* UTC time must have seconds */ | ||
50 | .str = "7001010000Z", | ||
51 | }, | ||
52 | { | ||
53 | .str = "201509230327Z", | ||
54 | }, | ||
55 | { | ||
56 | .str = "20150923032700.Z", | ||
57 | }, | ||
58 | { | ||
59 | .str = "20150923032700.123", | ||
60 | }, | ||
61 | { | ||
62 | .str = "20150923032700+1100Z", | ||
63 | }, | ||
64 | { | ||
65 | .str = "20150923032700-11001", | ||
66 | }, | ||
67 | { | ||
68 | /* UTC time cannot have fractional seconds. */ | ||
69 | .str = "150923032700.123Z", | ||
70 | }, | ||
71 | { | ||
72 | /* Gen time cannot have +- TZ. */ | ||
73 | .str = "20150923032712+1115", | ||
74 | }, | ||
75 | { | ||
76 | /* Gen time cannot have fractional seconds */ | ||
77 | .str = "20150923032712.123Z", | ||
78 | }, | ||
79 | { | ||
80 | .str = "aaaaaaaaaaaaaaZ", | ||
81 | }, | ||
82 | }; | ||
83 | |||
84 | struct rfc5280_time_test rfc5280_gentime_tests[] = { | ||
85 | { | ||
86 | .str = "19700101000000Z", | ||
87 | .data = "19700101000000Z", | ||
88 | .time = 0, | ||
89 | }, | ||
90 | { | ||
91 | .str = "20150923032700Z", | ||
92 | .data = "20150923032700Z", | ||
93 | .time = 1442978820, | ||
94 | }, | ||
95 | { | ||
96 | .str = "20150922162712Z", | ||
97 | .data = "20150922162712Z", | ||
98 | .time = 1442939232, | ||
99 | }, | ||
100 | { | ||
101 | .str = "20150922161212Z", | ||
102 | .data = "20150922161212Z", | ||
103 | .time = 1442938332, | ||
104 | }, | ||
105 | { | ||
106 | .str = "20150923032700Z", | ||
107 | .data = "20150923032700Z", | ||
108 | .time = 1442978820, | ||
109 | }, | ||
110 | { | ||
111 | /* Biggest RFC 5280 time */ | ||
112 | .str = "99991231235959Z", | ||
113 | .data = "99991231235959Z", | ||
114 | .time = 253402300799, | ||
115 | }, | ||
116 | { | ||
117 | /* Smallest RFC 5280 time */ | ||
118 | .str = "00000101000000Z", | ||
119 | .data = "00000101000000Z", | ||
120 | .time = -62167219200, | ||
121 | }, | ||
122 | }; | ||
123 | |||
124 | struct rfc5280_time_test rfc5280_utctime_tests[] = { | ||
125 | { | ||
126 | .str = "700101000000Z", | ||
127 | .data = "700101000000Z", | ||
128 | .time = 0, | ||
129 | }, | ||
130 | { | ||
131 | .str = "150923032700Z", | ||
132 | .data = "150923032700Z", | ||
133 | .time = 1442978820, | ||
134 | }, | ||
135 | { | ||
136 | .str = "150923102700Z", | ||
137 | .data = "150923102700Z", | ||
138 | .time = 1443004020, | ||
139 | }, | ||
140 | { | ||
141 | .str = "150922162712Z", | ||
142 | .data = "150922162712Z", | ||
143 | .time = 1442939232, | ||
144 | }, | ||
145 | { | ||
146 | .str = "140524144512Z", | ||
147 | .data = "140524144512Z", | ||
148 | .time = 1400942712, | ||
149 | }, | ||
150 | { | ||
151 | .str = "240401144512Z", | ||
152 | .data = "240401144512Z", | ||
153 | .time = 1711982712, | ||
154 | }, | ||
155 | }; | ||
156 | |||
157 | #define N_INVTIME_TESTS \ | ||
158 | (sizeof(rfc5280_invtime_tests) / sizeof(*rfc5280_invtime_tests)) | ||
159 | #define N_GENTIME_TESTS \ | ||
160 | (sizeof(rfc5280_gentime_tests) / sizeof(*rfc5280_gentime_tests)) | ||
161 | #define N_UTCTIME_TESTS \ | ||
162 | (sizeof(rfc5280_utctime_tests) / sizeof(*rfc5280_utctime_tests)) | ||
163 | |||
164 | static int | ||
165 | asn1_compare_str(int test_no, struct asn1_string_st *asn1str, const char *str) | ||
166 | { | ||
167 | int length = strlen(str); | ||
168 | |||
169 | if (asn1str->length != length) { | ||
170 | fprintf(stderr, "FAIL: test %i - string lengths differ " | ||
171 | "(%i != %i)\n", test_no, asn1str->length, length); | ||
172 | return (1); | ||
173 | } | ||
174 | if (strncmp(asn1str->data, str, length) != 0) { | ||
175 | fprintf(stderr, "FAIL: test %i - strings differ " | ||
176 | "('%s' != '%s')\n", test_no, asn1str->data, str); | ||
177 | return (1); | ||
178 | } | ||
179 | |||
180 | return (0); | ||
181 | } | ||
182 | |||
183 | static int | ||
184 | rfc5280_invtime_test(int test_no, struct rfc5280_time_test *att) | ||
185 | { | ||
186 | ASN1_GENERALIZEDTIME *gt = NULL; | ||
187 | ASN1_UTCTIME *ut = NULL; | ||
188 | ASN1_TIME *t = NULL; | ||
189 | int failure = 1; | ||
190 | time_t now = time(NULL); | ||
191 | |||
192 | if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL) | ||
193 | goto done; | ||
194 | if ((ut = ASN1_UTCTIME_new()) == NULL) | ||
195 | goto done; | ||
196 | if ((t = ASN1_TIME_new()) == NULL) | ||
197 | goto done; | ||
198 | |||
199 | if (ASN1_GENERALIZEDTIME_set_string(gt, att->str) != 0) { | ||
200 | if (X509_cmp_time(gt, &now) != 0) { | ||
201 | fprintf(stderr, "FAIL: test %i - successfully parsed as GENTIME " | ||
202 | "string '%s'\n", test_no, att->str); | ||
203 | goto done; | ||
204 | } | ||
205 | } | ||
206 | if (ASN1_UTCTIME_set_string(ut, att->str) != 0) { | ||
207 | if (X509_cmp_time(ut, &now) != 0) { | ||
208 | fprintf(stderr, "FAIL: test %i - successfully parsed as UTCTIME " | ||
209 | "string '%s'\n", test_no, att->str); | ||
210 | goto done; | ||
211 | } | ||
212 | } | ||
213 | if (ASN1_UTCTIME_set_string(ut, att->str) != 0) { | ||
214 | if (X509_cmp_time(ut, &now) != 0) { | ||
215 | fprintf(stderr, "FAIL: test %i - successfully parsed as UTCTIME " | ||
216 | "string '%s'\n", test_no, att->str); | ||
217 | goto done; | ||
218 | } | ||
219 | } | ||
220 | |||
221 | failure = 0; | ||
222 | |||
223 | done: | ||
224 | ASN1_GENERALIZEDTIME_free(gt); | ||
225 | ASN1_UTCTIME_free(ut); | ||
226 | ASN1_TIME_free(t); | ||
227 | |||
228 | return (failure); | ||
229 | } | ||
230 | |||
231 | static int | ||
232 | rfc5280_gentime_test(int test_no, struct rfc5280_time_test *att) | ||
233 | { | ||
234 | unsigned char *p = NULL; | ||
235 | ASN1_GENERALIZEDTIME *gt; | ||
236 | int failure = 1; | ||
237 | int i; | ||
238 | |||
239 | if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL) | ||
240 | goto done; | ||
241 | |||
242 | if (ASN1_GENERALIZEDTIME_set_string(gt, att->str) != 1) { | ||
243 | fprintf(stderr, "FAIL: test %i - failed to set string '%s'\n", | ||
244 | test_no, att->str); | ||
245 | goto done; | ||
246 | } | ||
247 | if (asn1_compare_str(test_no, gt, att->str) != 0) | ||
248 | goto done; | ||
249 | |||
250 | if ((i = X509_cmp_time(gt, &att->time) != -1)) { | ||
251 | fprintf(stderr, "FAIL: test %i - X509_cmp_time failed - returned %d compared to %lld\n", | ||
252 | test_no, i, att->time); | ||
253 | goto done; | ||
254 | } | ||
255 | |||
256 | att->time--; | ||
257 | if ((i = X509_cmp_time(gt, &att->time) != 1)) { | ||
258 | fprintf(stderr, "FAIL: test %i - X509_cmp_time failed - returned %d compared to %lld\n", | ||
259 | test_no, i, att->time); | ||
260 | goto done; | ||
261 | } | ||
262 | att->time++; | ||
263 | |||
264 | ASN1_GENERALIZEDTIME_free(gt); | ||
265 | |||
266 | if ((gt = ASN1_GENERALIZEDTIME_set(NULL, att->time)) == NULL) { | ||
267 | fprintf(stderr, "FAIL: test %i - failed to set time %lli\n", | ||
268 | test_no, (long long)att->time); | ||
269 | goto done; | ||
270 | } | ||
271 | if (asn1_compare_str(test_no, gt, att->data) != 0) | ||
272 | goto done; | ||
273 | |||
274 | failure = 0; | ||
275 | |||
276 | done: | ||
277 | ASN1_GENERALIZEDTIME_free(gt); | ||
278 | free(p); | ||
279 | |||
280 | return (failure); | ||
281 | } | ||
282 | |||
283 | static int | ||
284 | rfc5280_utctime_test(int test_no, struct rfc5280_time_test *att) | ||
285 | { | ||
286 | unsigned char *p = NULL; | ||
287 | ASN1_UTCTIME *ut; | ||
288 | int failure = 1; | ||
289 | int i; | ||
290 | |||
291 | if ((ut = ASN1_UTCTIME_new()) == NULL) | ||
292 | goto done; | ||
293 | |||
294 | if (ASN1_UTCTIME_set_string(ut, att->str) != 1) { | ||
295 | fprintf(stderr, "FAIL: test %i - failed to set string '%s'\n", | ||
296 | test_no, att->str); | ||
297 | goto done; | ||
298 | } | ||
299 | if (asn1_compare_str(test_no, ut, att->str) != 0) | ||
300 | goto done; | ||
301 | |||
302 | if ((i = X509_cmp_time(ut, &att->time) != -1)) { | ||
303 | fprintf(stderr, "FAIL: test %i - X509_cmp_time failed - returned %d compared to %lld\n", | ||
304 | test_no, i, att->time); | ||
305 | goto done; | ||
306 | } | ||
307 | |||
308 | att->time--; | ||
309 | if ((i = X509_cmp_time(ut, &att->time) != 1)) { | ||
310 | fprintf(stderr, "FAIL: test %i - X509_cmp_time failed - returned %d compared to %lld\n", | ||
311 | test_no, i, att->time); | ||
312 | goto done; | ||
313 | } | ||
314 | att->time++; | ||
315 | |||
316 | ASN1_UTCTIME_free(ut); | ||
317 | |||
318 | if ((ut = ASN1_UTCTIME_set(NULL, att->time)) == NULL) { | ||
319 | fprintf(stderr, "FAIL: test %i - failed to set time %lli\n", | ||
320 | test_no, (long long)att->time); | ||
321 | goto done; | ||
322 | } | ||
323 | if (asn1_compare_str(test_no, ut, att->data) != 0) | ||
324 | goto done; | ||
325 | |||
326 | failure = 0; | ||
327 | |||
328 | done: | ||
329 | ASN1_UTCTIME_free(ut); | ||
330 | free(p); | ||
331 | |||
332 | return (failure); | ||
333 | } | ||
334 | |||
335 | int | ||
336 | main(int argc, char **argv) | ||
337 | { | ||
338 | struct rfc5280_time_test *att; | ||
339 | int failed = 0; | ||
340 | size_t i; | ||
341 | |||
342 | fprintf(stderr, "RFC5280 Invalid time tests...\n"); | ||
343 | for (i = 0; i < N_INVTIME_TESTS; i++) { | ||
344 | att = &rfc5280_invtime_tests[i]; | ||
345 | failed |= rfc5280_invtime_test(i, att); | ||
346 | } | ||
347 | |||
348 | fprintf(stderr, "RFC5280 GENERALIZEDTIME tests...\n"); | ||
349 | for (i = 0; i < N_GENTIME_TESTS; i++) { | ||
350 | att = &rfc5280_gentime_tests[i]; | ||
351 | failed |= rfc5280_gentime_test(i, att); | ||
352 | } | ||
353 | |||
354 | fprintf(stderr, "RFC5280 UTCTIME tests...\n"); | ||
355 | for (i = 0; i < N_UTCTIME_TESTS; i++) { | ||
356 | att = &rfc5280_utctime_tests[i]; | ||
357 | failed |= rfc5280_utctime_test(i, att); | ||
358 | } | ||
359 | return (failed); | ||
360 | } | ||