summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2015-09-25 17:46:32 +0000
committerjsing <>2015-09-25 17:46:32 +0000
commit4ee5488145cd4786898d2ab37c349dec093b9810 (patch)
treebefba964a7522405d7a0bd62cdd02884f76e687b
parent7cde7bd1f58e1043f515e7b33b358fe89477b273 (diff)
downloadopenbsd-4ee5488145cd4786898d2ab37c349dec093b9810.tar.gz
openbsd-4ee5488145cd4786898d2ab37c349dec093b9810.tar.bz2
openbsd-4ee5488145cd4786898d2ab37c349dec093b9810.zip
Add DER encoding/decoding coverage for ASN.1 GENERALIZEDTIME and UTCTIME.
-rw-r--r--src/regress/lib/libcrypto/asn1/asn1time.c197
1 files changed, 158 insertions, 39 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1time.c b/src/regress/lib/libcrypto/asn1/asn1time.c
index 342fcf5779..033172a129 100644
--- a/src/regress/lib/libcrypto/asn1/asn1time.c
+++ b/src/regress/lib/libcrypto/asn1/asn1time.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1time.c,v 1.1 2015/09/25 16:12:30 jsing Exp $ */ 1/* $OpenBSD: asn1time.c,v 1.2 2015/09/25 17:46:32 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2015 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -24,6 +24,7 @@
24struct asn1_time_test { 24struct asn1_time_test {
25 const char *str; 25 const char *str;
26 const char *data; 26 const char *data;
27 const unsigned char der[32];
27 time_t time; 28 time_t time;
28}; 29};
29 30
@@ -69,31 +70,62 @@ struct asn1_time_test asn1_gentime_tests[] = {
69 .str = "19700101000000Z", 70 .str = "19700101000000Z",
70 .data = "19700101000000Z", 71 .data = "19700101000000Z",
71 .time = 0, 72 .time = 0,
73 .der = {
74 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31,
75 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
76 0x5a,
77 },
72 }, 78 },
73 { 79 {
74 .str = "20150923032700Z", 80 .str = "20150923032700Z",
75 .data = "20150923032700Z", 81 .data = "20150923032700Z",
76 .time = 1442978820, 82 .time = 1442978820,
83 .der = {
84 0x18, 0x0f, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39,
85 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x30, 0x30,
86 0x5a,
87 },
77 }, 88 },
78 { 89 {
79 .str = "20150923032700.22-0700", 90 .str = "20150923032700.22-0700",
80 .data = "20150923102700Z", 91 .data = "20150923102700Z",
81 .time = 1443004020, 92 .time = 1443004020,
93 .der = {
94 0x18, 0x16, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39,
95 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x30, 0x30,
96 0x2e, 0x32, 0x32, 0x2d, 0x30, 0x37, 0x30, 0x30,
97 },
82 }, 98 },
83 { 99 {
84 .str = "20150923032712+1100", 100 .str = "20150923032712+1100",
85 .data = "20150922162712Z", 101 .data = "20150922162712Z",
86 .time = 1442939232, 102 .time = 1442939232,
103 .der = {
104 0x18, 0x13, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39,
105 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x31, 0x32,
106 0x2b, 0x31, 0x31, 0x30, 0x30,
107 },
87 }, 108 },
88 { 109 {
89 .str = "20150923032712+1115", 110 .str = "20150923032712+1115",
90 .data = "20150922161212Z", 111 .data = "20150922161212Z",
91 .time = 1442938332, 112 .time = 1442938332,
113 .der = {
114 0x18, 0x13, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39,
115 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x31, 0x32,
116 0x2b, 0x31, 0x31, 0x31, 0x35,
117 },
92 }, 118 },
93 { 119 {
94 .str = "20150923032700.12345678Z", 120 .str = "20150923032700.12345678Z",
95 .data = "20150923032700Z", 121 .data = "20150923032700Z",
96 .time = 1442978820, 122 .time = 1442978820,
123 .der = {
124 0x18, 0x18, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39,
125 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x30, 0x30,
126 0x2e, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
127 0x38, 0x5a,
128 },
97 }, 129 },
98}; 130};
99 131
@@ -102,21 +134,57 @@ struct asn1_time_test asn1_utctime_tests[] = {
102 .str = "7001010000Z", 134 .str = "7001010000Z",
103 .data = "700101000000Z", 135 .data = "700101000000Z",
104 .time = 0, 136 .time = 0,
137 .der = {
138 0x17, 0x0b, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31,
139 0x30, 0x30, 0x30, 0x30, 0x5a,
140 },
105 }, 141 },
106 { 142 {
107 .str = "150923032700Z", 143 .str = "150923032700Z",
108 .data = "150923032700Z", 144 .data = "150923032700Z",
109 .time = 1442978820, 145 .time = 1442978820,
146 .der = {
147 0x17, 0x0d, 0x31, 0x35, 0x30, 0x39, 0x32, 0x33,
148 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, 0x5a,
149 },
110 }, 150 },
111 { 151 {
112 .str = "150923032700-0700", 152 .str = "150923032700-0700",
113 .data = "150923102700Z", 153 .data = "150923102700Z",
114 .time = 1443004020, 154 .time = 1443004020,
155 .der = {
156 0x17, 0x11, 0x31, 0x35, 0x30, 0x39, 0x32, 0x33,
157 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, 0x2d, 0x30,
158 0x37, 0x30, 0x30,
159 },
115 }, 160 },
116 { 161 {
117 .str = "150923032712+1100", 162 .str = "150923032712+1100",
118 .data = "150922162712Z", 163 .data = "150922162712Z",
119 .time = 1442939232, 164 .time = 1442939232,
165 .der = {
166 0x17, 0x11, 0x31, 0x35, 0x30, 0x39, 0x32, 0x33,
167 0x30, 0x33, 0x32, 0x37, 0x31, 0x32, 0x2b, 0x31,
168 0x31, 0x30, 0x30,
169 },
170 },
171 {
172 .str = "140524144512Z",
173 .data = "140524144512Z",
174 .time = 1400942712,
175 .der = {
176 0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x32, 0x34,
177 0x31, 0x34, 0x34, 0x35, 0x31, 0x32, 0x5a,
178 },
179 },
180 {
181 .str = "240401144512Z",
182 .data = "240401144512Z",
183 .time = 1711982712,
184 .der = {
185 0x17, 0x0d, 0x32, 0x34, 0x30, 0x34, 0x30, 0x31,
186 0x31, 0x34, 0x34, 0x35, 0x31, 0x32, 0x5a
187 },
120 }, 188 },
121}; 189};
122 190
@@ -127,6 +195,55 @@ struct asn1_time_test asn1_utctime_tests[] = {
127#define N_UTCTIME_TESTS \ 195#define N_UTCTIME_TESTS \
128 (sizeof(asn1_utctime_tests) / sizeof(*asn1_utctime_tests)) 196 (sizeof(asn1_utctime_tests) / sizeof(*asn1_utctime_tests))
129 197
198static void
199hexdump(const unsigned char *buf, size_t len)
200{
201 size_t i;
202
203 for (i = 1; i <= len; i++)
204 fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n");
205
206 fprintf(stderr, "\n");
207}
208
209static int
210asn1_compare_bytes(int test_no, const unsigned char *d1,
211 const unsigned char *d2, int len1, int len2)
212{
213 if (len1 != len2) {
214 fprintf(stderr, "FAIL: test %i - byte lengths differ "
215 "(%i != %i)\n", test_no, len1, len2);
216 return (1);
217 }
218 if (memcmp(d1, d2, len1) != 0) {
219 fprintf(stderr, "FAIL: test %i - bytes differ\n", test_no);
220 fprintf(stderr, "Got:\n");
221 hexdump(d1, len1);
222 fprintf(stderr, "Want:\n");
223 hexdump(d2, len2);
224 }
225 return (0);
226}
227
228static int
229asn1_compare_str(int test_no, struct asn1_string_st *asn1str, const char *str)
230{
231 int length = strlen(str);
232
233 if (asn1str->length != length) {
234 fprintf(stderr, "FAIL: test %i - string lengths differ "
235 "(%i != %i)\n", test_no, asn1str->length, length);
236 return (1);
237 }
238 if (strncmp(asn1str->data, str, length) != 0) {
239 fprintf(stderr, "FAIL: test %i - strings differ "
240 "('%s' != '%s')\n", test_no, asn1str->data, str);
241 return (1);
242 }
243
244 return (0);
245}
246
130static int 247static int
131asn1_invtime_test(int test_no, struct asn1_time_test *att) 248asn1_invtime_test(int test_no, struct asn1_time_test *att)
132{ 249{
@@ -171,9 +288,11 @@ asn1_invtime_test(int test_no, struct asn1_time_test *att)
171static int 288static int
172asn1_gentime_test(int test_no, struct asn1_time_test *att) 289asn1_gentime_test(int test_no, struct asn1_time_test *att)
173{ 290{
291 const unsigned char *der;
292 unsigned char *p = NULL;
174 ASN1_GENERALIZEDTIME *gt; 293 ASN1_GENERALIZEDTIME *gt;
175 int failure = 1; 294 int failure = 1;
176 int length; 295 int len;
177 296
178 if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL) 297 if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
179 goto done; 298 goto done;
@@ -183,19 +302,26 @@ asn1_gentime_test(int test_no, struct asn1_time_test *att)
183 test_no, att->str); 302 test_no, att->str);
184 goto done; 303 goto done;
185 } 304 }
305 if (asn1_compare_str(test_no, gt, att->str) != 0)
306 goto done;
186 307
187 /* ASN.1 preserves the original input. */ 308 if ((len = i2d_ASN1_GENERALIZEDTIME(gt, &p)) <= 0) {
188 length = strlen(att->str); 309 fprintf(stderr, "FAIL: test %i - i2d_ASN1_GENERALIZEDTIME "
189 if (gt->length != length) { 310 "failed\n", test_no);
190 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
191 test_no, gt->length, length);
192 goto done; 311 goto done;
193 } 312 }
194 if (strncmp(gt->data, att->str, length) != 0) { 313 der = att->der;
195 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n", 314 if (asn1_compare_bytes(test_no, p, der, len, strlen(der)) != 0)
196 test_no, gt->data, att->str); 315 goto done;
316
317 len = strlen(att->der);
318 if (d2i_ASN1_GENERALIZEDTIME(&gt, &der, len) == NULL) {
319 fprintf(stderr, "FAIL: test %i - d2i_ASN1_GENERALIZEDTIME "
320 "failed\n", test_no);
197 goto done; 321 goto done;
198 } 322 }
323 if (asn1_compare_str(test_no, gt, att->str) != 0)
324 goto done;
199 325
200 ASN1_GENERALIZEDTIME_free(gt); 326 ASN1_GENERALIZEDTIME_free(gt);
201 327
@@ -204,22 +330,14 @@ asn1_gentime_test(int test_no, struct asn1_time_test *att)
204 test_no, (long long)att->time); 330 test_no, (long long)att->time);
205 goto done; 331 goto done;
206 } 332 }
207 length = strlen(att->data); 333 if (asn1_compare_str(test_no, gt, att->data) != 0)
208 if (gt->length != length) {
209 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
210 test_no, gt->length, length);
211 goto done; 334 goto done;
212 }
213 if (strncmp(gt->data, att->data, length) != 0) {
214 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n",
215 test_no, gt->data, att->data);
216 goto done;
217 }
218 335
219 failure = 0; 336 failure = 0;
220 337
221 done: 338 done:
222 ASN1_GENERALIZEDTIME_free(gt); 339 ASN1_GENERALIZEDTIME_free(gt);
340 free(p);
223 341
224 return (failure); 342 return (failure);
225} 343}
@@ -227,9 +345,11 @@ asn1_gentime_test(int test_no, struct asn1_time_test *att)
227static int 345static int
228asn1_utctime_test(int test_no, struct asn1_time_test *att) 346asn1_utctime_test(int test_no, struct asn1_time_test *att)
229{ 347{
348 const unsigned char *der;
349 unsigned char *p = NULL;
230 ASN1_UTCTIME *ut; 350 ASN1_UTCTIME *ut;
231 int failure = 1; 351 int failure = 1;
232 int length; 352 int len;
233 353
234 if ((ut = ASN1_UTCTIME_new()) == NULL) 354 if ((ut = ASN1_UTCTIME_new()) == NULL)
235 goto done; 355 goto done;
@@ -239,19 +359,26 @@ asn1_utctime_test(int test_no, struct asn1_time_test *att)
239 test_no, att->str); 359 test_no, att->str);
240 goto done; 360 goto done;
241 } 361 }
362 if (asn1_compare_str(test_no, ut, att->str) != 0)
363 goto done;
242 364
243 /* ASN.1 preserves the original input. */ 365 if ((len = i2d_ASN1_UTCTIME(ut, &p)) <= 0) {
244 length = strlen(att->str); 366 fprintf(stderr, "FAIL: test %i - i2d_ASN1_UTCTIME failed\n",
245 if (ut->length != length) { 367 test_no);
246 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
247 test_no, ut->length, length);
248 goto done; 368 goto done;
249 } 369 }
250 if (strncmp(ut->data, att->str, length) != 0) { 370 der = att->der;
251 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n", 371 if (asn1_compare_bytes(test_no, p, der, len, strlen(der)) != 0)
252 test_no, ut->data, att->str); 372 goto done;
373
374 len = strlen(att->der);
375 if (d2i_ASN1_UTCTIME(&ut, &der, len) == NULL) {
376 fprintf(stderr, "FAIL: test %i - d2i_ASN1_UTCTIME failed\n",
377 test_no);
253 goto done; 378 goto done;
254 } 379 }
380 if (asn1_compare_str(test_no, ut, att->str) != 0)
381 goto done;
255 382
256 ASN1_UTCTIME_free(ut); 383 ASN1_UTCTIME_free(ut);
257 384
@@ -260,22 +387,14 @@ asn1_utctime_test(int test_no, struct asn1_time_test *att)
260 test_no, (long long)att->time); 387 test_no, (long long)att->time);
261 goto done; 388 goto done;
262 } 389 }
263 length = strlen(att->data); 390 if (asn1_compare_str(test_no, ut, att->data) != 0)
264 if (ut->length != length) {
265 fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n",
266 test_no, ut->length, length);
267 goto done;
268 }
269 if (strncmp(ut->data, att->data, length) != 0) {
270 fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n",
271 test_no, ut->data, att->data);
272 goto done; 391 goto done;
273 }
274 392
275 failure = 0; 393 failure = 0;
276 394
277 done: 395 done:
278 ASN1_UTCTIME_free(ut); 396 ASN1_UTCTIME_free(ut);
397 free(p);
279 398
280 return (failure); 399 return (failure);
281} 400}