diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2017-11-28 16:47:56 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2017-11-28 16:47:56 +0000 |
commit | fc746af51cb23acd28b0c2bf32369347bcfb5e18 (patch) | |
tree | 208ab1cae278fc835fb1fda068dda4488406bc62 /regress | |
parent | e397fda1655257386c079a0f3a553675425ba24b (diff) |
Add regress coverage for ASN1_TYPE_{get,set}_int_octetstring()
Diffstat (limited to 'regress')
-rw-r--r-- | regress/lib/libcrypto/asn1/Makefile | 6 | ||||
-rw-r--r-- | regress/lib/libcrypto/asn1/asn1evp.c | 143 |
2 files changed, 147 insertions, 2 deletions
diff --git a/regress/lib/libcrypto/asn1/Makefile b/regress/lib/libcrypto/asn1/Makefile index d4da1bf7a9a..abb69cb3793 100644 --- a/regress/lib/libcrypto/asn1/Makefile +++ b/regress/lib/libcrypto/asn1/Makefile @@ -1,7 +1,9 @@ -# $OpenBSD: Makefile,v 1.2 2015/09/29 04:54:23 beck Exp $ +# $OpenBSD: Makefile,v 1.3 2017/11/28 16:47:55 jsing Exp $ TESTS = \ - asn1time rfc5280time + asn1evp \ + asn1time \ + rfc5280time REGRESS_TARGETS= all_tests diff --git a/regress/lib/libcrypto/asn1/asn1evp.c b/regress/lib/libcrypto/asn1/asn1evp.c new file mode 100644 index 00000000000..dd87bbb0209 --- /dev/null +++ b/regress/lib/libcrypto/asn1/asn1evp.c @@ -0,0 +1,143 @@ +/* $OpenBSD: asn1evp.c,v 1.1 2017/11/28 16:47:55 jsing Exp $ */ +/* + * Copyright (c) 2017 Joel Sing <jsing@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <limits.h> +#include <stdio.h> +#include <string.h> + +#include <openssl/asn1.h> + +unsigned char asn1_atios[] = { + 0x30, 0x14, 0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0x04, 0x08, 0x00, 0x01, + 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, +}; + +unsigned char test_octetstring[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, +}; + +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 +compare_data(const char *label, const unsigned char *d1, size_t d1_len, + const unsigned char *d2, size_t d2_len) +{ + if (d1_len != d2_len) { + fprintf(stderr, "FAIL: got %s with length %zu, want %zu\n", + label, d1_len, d2_len); + return -1; + } + if (memcmp(d1, d2, d1_len) != 0) { + fprintf(stderr, "FAIL: %s differs\n", label); + fprintf(stderr, "got:\n"); + hexdump(d1, d1_len); + fprintf(stderr, "want:\n"); + hexdump(d2, d2_len); + return -1; + } + return 0; +} + +int +main(int argc, char **argv) +{ + unsigned char data[16]; + long num = LONG_MAX; + int failed = 1; + ASN1_TYPE at; + int len; + + memset(&at, 0, sizeof(at)); + + if (!ASN1_TYPE_set_int_octetstring(&at, num, test_octetstring, + sizeof(test_octetstring))) { + fprintf(stderr, "FAIL: ASN1_TYPE_set_int_octetstring failed\n"); + goto done; + } + if (at.type != V_ASN1_SEQUENCE) { + fprintf(stderr, "FAIL: not a V_ASN1_SEQUENCE (%i != %i)\n", + at.type, V_ASN1_SEQUENCE); + goto done; + } + if (at.value.sequence->type != V_ASN1_OCTET_STRING) { + fprintf(stderr, "FAIL: not a V_ASN1_OCTET_STRING (%i != %i)\n", + at.type, V_ASN1_OCTET_STRING); + goto done; + } + if (compare_data("sequence", at.value.sequence->data, + at.value.sequence->length, asn1_atios, sizeof(asn1_atios)) == -1) + goto done; + + memset(&data, 0, sizeof(data)); + num = 0; + + if ((len = ASN1_TYPE_get_int_octetstring(&at, &num, data, + sizeof(data))) < 0) { + fprintf(stderr, "FAIL: ASN1_TYPE_get_int_octetstring failed\n"); + goto done; + } + if (num != LONG_MAX) { + fprintf(stderr, "FAIL: got num %li, want %li\n", num, LONG_MAX); + goto done; + } + if (compare_data("octet string", data, len, + test_octetstring, sizeof(test_octetstring)) == -1) + goto done; + if (data[len] != 0) { + fprintf(stderr, "FAIL: octet string overflowed buffer\n"); + goto done; + } + + memset(&data, 0, sizeof(data)); + num = 0; + + /* With a limit buffer, the output should be truncated... */ + if ((len = ASN1_TYPE_get_int_octetstring(&at, &num, data, 4)) < 0) { + fprintf(stderr, "FAIL: ASN1_TYPE_get_int_octetstring failed\n"); + goto done; + } + if (num != LONG_MAX) { + fprintf(stderr, "FAIL: got num %li, want %li\n", num, LONG_MAX); + goto done; + } + if (len != sizeof(test_octetstring)) { + fprintf(stderr, "FAIL: got length mismatch (%i != %zu)\n", + len, sizeof(test_octetstring)); + goto done; + } + if (compare_data("octet string", data, 4, test_octetstring, 4) == -1) + goto done; + if (data[4] != 0) { + fprintf(stderr, "FAIL: octet string overflowed buffer\n"); + goto done; + } + + failed = 0; + + done: + return failed; +} |