diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2015-09-25 17:46:33 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2015-09-25 17:46:33 +0000 |
commit | 65aa80e22a993e41c06d519905f0620221a1dcc3 (patch) | |
tree | 2d5db002bf2b912cd39973794a5ca14c55021e30 /regress/lib/libcrypto | |
parent | 846b0e3945d3eb88ef389f1db127f6401bd26087 (diff) |
Add DER encoding/decoding coverage for ASN.1 GENERALIZEDTIME and UTCTIME.
Diffstat (limited to 'regress/lib/libcrypto')
-rw-r--r-- | regress/lib/libcrypto/asn1/asn1time.c | 197 |
1 files changed, 158 insertions, 39 deletions
diff --git a/regress/lib/libcrypto/asn1/asn1time.c b/regress/lib/libcrypto/asn1/asn1time.c index 342fcf57791..033172a1291 100644 --- a/regress/lib/libcrypto/asn1/asn1time.c +++ b/regress/lib/libcrypto/asn1/asn1time.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asn1time.c,v 1.1 2015/09/25 16:12:30 jsing Exp $ */ +/* $OpenBSD: asn1time.c,v 1.2 2015/09/25 17:46:32 jsing Exp $ */ /* * Copyright (c) 2015 Joel Sing <jsing@openbsd.org> * @@ -24,6 +24,7 @@ struct asn1_time_test { const char *str; const char *data; + const unsigned char der[32]; time_t time; }; @@ -69,31 +70,62 @@ struct asn1_time_test asn1_gentime_tests[] = { .str = "19700101000000Z", .data = "19700101000000Z", .time = 0, + .der = { + 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, + 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x5a, + }, }, { .str = "20150923032700Z", .data = "20150923032700Z", .time = 1442978820, + .der = { + 0x18, 0x0f, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, + 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, + 0x5a, + }, }, { .str = "20150923032700.22-0700", .data = "20150923102700Z", .time = 1443004020, + .der = { + 0x18, 0x16, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, + 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, + 0x2e, 0x32, 0x32, 0x2d, 0x30, 0x37, 0x30, 0x30, + }, }, { .str = "20150923032712+1100", .data = "20150922162712Z", .time = 1442939232, + .der = { + 0x18, 0x13, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, + 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x31, 0x32, + 0x2b, 0x31, 0x31, 0x30, 0x30, + }, }, { .str = "20150923032712+1115", .data = "20150922161212Z", .time = 1442938332, + .der = { + 0x18, 0x13, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, + 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x31, 0x32, + 0x2b, 0x31, 0x31, 0x31, 0x35, + }, }, { .str = "20150923032700.12345678Z", .data = "20150923032700Z", .time = 1442978820, + .der = { + 0x18, 0x18, 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, + 0x32, 0x33, 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, + 0x2e, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x5a, + }, }, }; @@ -102,21 +134,57 @@ struct asn1_time_test asn1_utctime_tests[] = { .str = "7001010000Z", .data = "700101000000Z", .time = 0, + .der = { + 0x17, 0x0b, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, + 0x30, 0x30, 0x30, 0x30, 0x5a, + }, }, { .str = "150923032700Z", .data = "150923032700Z", .time = 1442978820, + .der = { + 0x17, 0x0d, 0x31, 0x35, 0x30, 0x39, 0x32, 0x33, + 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, 0x5a, + }, }, { .str = "150923032700-0700", .data = "150923102700Z", .time = 1443004020, + .der = { + 0x17, 0x11, 0x31, 0x35, 0x30, 0x39, 0x32, 0x33, + 0x30, 0x33, 0x32, 0x37, 0x30, 0x30, 0x2d, 0x30, + 0x37, 0x30, 0x30, + }, }, { .str = "150923032712+1100", .data = "150922162712Z", .time = 1442939232, + .der = { + 0x17, 0x11, 0x31, 0x35, 0x30, 0x39, 0x32, 0x33, + 0x30, 0x33, 0x32, 0x37, 0x31, 0x32, 0x2b, 0x31, + 0x31, 0x30, 0x30, + }, + }, + { + .str = "140524144512Z", + .data = "140524144512Z", + .time = 1400942712, + .der = { + 0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x32, 0x34, + 0x31, 0x34, 0x34, 0x35, 0x31, 0x32, 0x5a, + }, + }, + { + .str = "240401144512Z", + .data = "240401144512Z", + .time = 1711982712, + .der = { + 0x17, 0x0d, 0x32, 0x34, 0x30, 0x34, 0x30, 0x31, + 0x31, 0x34, 0x34, 0x35, 0x31, 0x32, 0x5a + }, }, }; @@ -127,6 +195,55 @@ struct asn1_time_test asn1_utctime_tests[] = { #define N_UTCTIME_TESTS \ (sizeof(asn1_utctime_tests) / sizeof(*asn1_utctime_tests)) +static void +hexdump(const unsigned char *buf, size_t len) +{ + size_t i; + + for (i = 1; i <= len; i++) + fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n"); + + fprintf(stderr, "\n"); +} + +static int +asn1_compare_bytes(int test_no, const unsigned char *d1, + const unsigned char *d2, int len1, int len2) +{ + if (len1 != len2) { + fprintf(stderr, "FAIL: test %i - byte lengths differ " + "(%i != %i)\n", test_no, len1, len2); + return (1); + } + if (memcmp(d1, d2, len1) != 0) { + fprintf(stderr, "FAIL: test %i - bytes differ\n", test_no); + fprintf(stderr, "Got:\n"); + hexdump(d1, len1); + fprintf(stderr, "Want:\n"); + hexdump(d2, len2); + } + return (0); +} + +static int +asn1_compare_str(int test_no, struct asn1_string_st *asn1str, const char *str) +{ + int length = strlen(str); + + if (asn1str->length != length) { + fprintf(stderr, "FAIL: test %i - string lengths differ " + "(%i != %i)\n", test_no, asn1str->length, length); + return (1); + } + if (strncmp(asn1str->data, str, length) != 0) { + fprintf(stderr, "FAIL: test %i - strings differ " + "('%s' != '%s')\n", test_no, asn1str->data, str); + return (1); + } + + return (0); +} + static int asn1_invtime_test(int test_no, struct asn1_time_test *att) { @@ -171,9 +288,11 @@ asn1_invtime_test(int test_no, struct asn1_time_test *att) static int asn1_gentime_test(int test_no, struct asn1_time_test *att) { + const unsigned char *der; + unsigned char *p = NULL; ASN1_GENERALIZEDTIME *gt; int failure = 1; - int length; + int len; if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL) goto done; @@ -183,19 +302,26 @@ asn1_gentime_test(int test_no, struct asn1_time_test *att) test_no, att->str); goto done; } + if (asn1_compare_str(test_no, gt, att->str) != 0) + goto done; - /* ASN.1 preserves the original input. */ - length = strlen(att->str); - if (gt->length != length) { - fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n", - test_no, gt->length, length); + if ((len = i2d_ASN1_GENERALIZEDTIME(gt, &p)) <= 0) { + fprintf(stderr, "FAIL: test %i - i2d_ASN1_GENERALIZEDTIME " + "failed\n", test_no); goto done; } - if (strncmp(gt->data, att->str, length) != 0) { - fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n", - test_no, gt->data, att->str); + der = att->der; + if (asn1_compare_bytes(test_no, p, der, len, strlen(der)) != 0) + goto done; + + len = strlen(att->der); + if (d2i_ASN1_GENERALIZEDTIME(>, &der, len) == NULL) { + fprintf(stderr, "FAIL: test %i - d2i_ASN1_GENERALIZEDTIME " + "failed\n", test_no); goto done; } + if (asn1_compare_str(test_no, gt, att->str) != 0) + goto done; ASN1_GENERALIZEDTIME_free(gt); @@ -204,22 +330,14 @@ asn1_gentime_test(int test_no, struct asn1_time_test *att) test_no, (long long)att->time); goto done; } - length = strlen(att->data); - if (gt->length != length) { - fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n", - test_no, gt->length, length); + if (asn1_compare_str(test_no, gt, att->data) != 0) goto done; - } - if (strncmp(gt->data, att->data, length) != 0) { - fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n", - test_no, gt->data, att->data); - goto done; - } failure = 0; done: ASN1_GENERALIZEDTIME_free(gt); + free(p); return (failure); } @@ -227,9 +345,11 @@ asn1_gentime_test(int test_no, struct asn1_time_test *att) static int asn1_utctime_test(int test_no, struct asn1_time_test *att) { + const unsigned char *der; + unsigned char *p = NULL; ASN1_UTCTIME *ut; int failure = 1; - int length; + int len; if ((ut = ASN1_UTCTIME_new()) == NULL) goto done; @@ -239,19 +359,26 @@ asn1_utctime_test(int test_no, struct asn1_time_test *att) test_no, att->str); goto done; } + if (asn1_compare_str(test_no, ut, att->str) != 0) + goto done; - /* ASN.1 preserves the original input. */ - length = strlen(att->str); - if (ut->length != length) { - fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n", - test_no, ut->length, length); + if ((len = i2d_ASN1_UTCTIME(ut, &p)) <= 0) { + fprintf(stderr, "FAIL: test %i - i2d_ASN1_UTCTIME failed\n", + test_no); goto done; } - if (strncmp(ut->data, att->str, length) != 0) { - fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n", - test_no, ut->data, att->str); + der = att->der; + if (asn1_compare_bytes(test_no, p, der, len, strlen(der)) != 0) + goto done; + + len = strlen(att->der); + if (d2i_ASN1_UTCTIME(&ut, &der, len) == NULL) { + fprintf(stderr, "FAIL: test %i - d2i_ASN1_UTCTIME failed\n", + test_no); goto done; } + if (asn1_compare_str(test_no, ut, att->str) != 0) + goto done; ASN1_UTCTIME_free(ut); @@ -260,22 +387,14 @@ asn1_utctime_test(int test_no, struct asn1_time_test *att) test_no, (long long)att->time); goto done; } - length = strlen(att->data); - if (ut->length != length) { - fprintf(stderr, "FAIL: test %i - length differs (%i != %i)\n", - test_no, ut->length, length); - goto done; - } - if (strncmp(ut->data, att->data, length) != 0) { - fprintf(stderr, "FAIL: test %i - data differs ('%s' != '%s')\n", - test_no, ut->data, att->data); + if (asn1_compare_str(test_no, ut, att->data) != 0) goto done; - } failure = 0; done: ASN1_UTCTIME_free(ut); + free(p); return (failure); } |