X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=test%2Fd2i_test.c;h=9c6fcb6a72adaea62eb5d7867d0f15d3d05fe8e7;hb=aad22ba2c6172508f70263bcf53f6af6257c8b14;hp=cf01012ef98b4caa654feab967eab77a9b81539a;hpb=1400f013e10c8ec624947d9187bebb20274385dc;p=openssl.git diff --git a/test/d2i_test.c b/test/d2i_test.c index cf01012ef9..9c6fcb6a72 100644 --- a/test/d2i_test.c +++ b/test/d2i_test.c @@ -1,11 +1,10 @@ /* * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. * - * Licensed under the OpenSSL licenses, (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html - * or in the file LICENSE in the source distribution. */ /* Regression tests for ASN.1 parsing bugs. */ @@ -16,19 +15,36 @@ #include "testutil.h" #include +#include #include #include #include #include +#include "e_os.h" static const ASN1_ITEM *item_type; static const char *test_file; +typedef enum { + ASN1_UNKNOWN, + ASN1_OK, + ASN1_BIO, + ASN1_DECODE, + ASN1_ENCODE, + ASN1_COMPARE +} expected_error_t; + +typedef struct { + const char *str; + expected_error_t code; +} error_enum; + +static expected_error_t expected_error = ASN1_UNKNOWN; + typedef struct d2i_test_fixture { const char *test_case_name; } D2I_TEST_FIXTURE; - static D2I_TEST_FIXTURE set_up(const char *const test_case_name) { D2I_TEST_FIXTURE fixture; @@ -40,30 +56,62 @@ static int execute_test(D2I_TEST_FIXTURE fixture) { BIO *bio = NULL; ASN1_VALUE *value = NULL; - int ret = 1; + int ret = 0; unsigned char buf[2048]; const unsigned char *buf_ptr = buf; + unsigned char *der = NULL; + int derlen; int len; if ((bio = BIO_new_file(test_file, "r")) == NULL) - return 1; + return 0; + + if (expected_error == ASN1_BIO) { + value = ASN1_item_d2i_bio(item_type, bio, NULL); + if (value == NULL) + ret = 1; + goto err; + } /* - * We don't use ASN1_item_d2i_bio because it, apparently, - * errors too early for some inputs. + * Unless we are testing it we don't use ASN1_item_d2i_bio because it + * performs sanity checks on the input and can reject it before the + * decoder is called. */ len = BIO_read(bio, buf, sizeof buf); if (len < 0) goto err; value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type); - if (value != NULL) + if (value == NULL) { + if (expected_error == ASN1_DECODE) + ret = 1; goto err; + } - ret = 0; + derlen = ASN1_item_i2d(value, &der, item_type); + + if (der == NULL || derlen < 0) { + if (expected_error == ASN1_ENCODE) + ret = 1; + goto err; + } + + if (derlen != len || memcmp(der, buf, derlen) != 0) { + if (expected_error == ASN1_COMPARE) + ret = 1; + goto err; + } + + if (expected_error == ASN1_OK) + ret = 1; err: + /* Don't indicate success for memory allocation errors */ + if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE) + ret = 0; BIO_free(bio); + OPENSSL_free(der); ASN1_item_free(value, item_type); return ret; } @@ -93,19 +141,57 @@ int main(int argc, char **argv) { int result = 0; const char *test_type_name; - - if (argc != 3) + const char *expected_error_string; + const char *p = getenv("OPENSSL_DEBUG_MEMORY"); + + size_t i; + + static error_enum expected_errors[] = { + {"OK", ASN1_OK}, + {"BIO", ASN1_BIO}, + {"decode", ASN1_DECODE}, + {"encode", ASN1_ENCODE}, + {"compare", ASN1_COMPARE} + }; + + if (p != NULL && strcmp(p, "on") == 0) + CRYPTO_set_mem_debug(1); + CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); + + if (argc != 4) { + fprintf(stderr, + "Usage: d2i_test item_name expected_error file.der\n"); return 1; + } test_type_name = argv[1]; - test_file = argv[2]; - - if (strcmp(test_type_name, "generalname") == 0) { - item_type = ASN1_ITEM_rptr(GENERAL_NAME); - } else if (strcmp(test_type_name, "x509") == 0) { - item_type = ASN1_ITEM_rptr(X509); - } else { - fprintf(stderr, "Bad type %s\n", test_type_name); + expected_error_string = argv[2]; + test_file = argv[3]; + + item_type = ASN1_ITEM_lookup(test_type_name); + + if (item_type == NULL) { + fprintf(stderr, "Unknown type %s\n", test_type_name); + fprintf(stderr, "Supported types:\n"); + for (i = 0;; i++) { + const ASN1_ITEM *it = ASN1_ITEM_get(i); + + if (it == NULL) + break; + fprintf(stderr, "\t%s\n", it->sname); + } + return 1; + } + + for (i = 0; i < OSSL_NELEM(expected_errors); i++) { + if (strcmp(expected_errors[i].str, expected_error_string) == 0) { + expected_error = expected_errors[i].code; + break; + } + } + + if (expected_error == ASN1_UNKNOWN) { + fprintf(stderr, "Unknown expected error %s\n", expected_error_string); return 1; } @@ -113,5 +199,10 @@ int main(int argc, char **argv) result = run_tests(argv[0]); +#ifndef OPENSSL_NO_CRYPTO_MDEBUG + if (CRYPTO_mem_leaks_fp(stderr) <= 0) + result = 1; +#endif + return result; }