2016-03-31 04:37:05 +08:00
|
|
|
/*
|
2017-04-10 05:44:08 +08:00
|
|
|
* Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2016-03-31 04:37:05 +08:00
|
|
|
*
|
2016-05-18 02:20:24 +08:00
|
|
|
* 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
|
2016-03-31 04:37:05 +08:00
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Regression tests for ASN.1 parsing bugs. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "testutil.h"
|
|
|
|
|
|
|
|
#include <openssl/asn1.h>
|
2016-04-21 04:37:57 +08:00
|
|
|
#include <openssl/asn1t.h>
|
2016-03-31 04:37:05 +08:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/x509v3.h>
|
2017-08-22 20:35:43 +08:00
|
|
|
#include "internal/nelem.h"
|
2016-03-31 04:37:05 +08:00
|
|
|
|
|
|
|
static const ASN1_ITEM *item_type;
|
|
|
|
static const char *test_file;
|
|
|
|
|
2016-04-21 04:37:57 +08:00
|
|
|
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;
|
|
|
|
|
2017-08-16 05:39:03 +08:00
|
|
|
static int test_bad_asn1(void)
|
2016-03-31 04:37:05 +08:00
|
|
|
{
|
|
|
|
BIO *bio = NULL;
|
|
|
|
ASN1_VALUE *value = NULL;
|
2016-04-05 20:29:06 +08:00
|
|
|
int ret = 0;
|
2016-03-31 04:37:05 +08:00
|
|
|
unsigned char buf[2048];
|
|
|
|
const unsigned char *buf_ptr = buf;
|
2016-04-21 04:37:57 +08:00
|
|
|
unsigned char *der = NULL;
|
|
|
|
int derlen;
|
2016-03-31 04:37:05 +08:00
|
|
|
int len;
|
|
|
|
|
2017-04-10 05:44:08 +08:00
|
|
|
bio = BIO_new_file(test_file, "r");
|
|
|
|
if (!TEST_ptr(bio))
|
2016-04-05 20:29:06 +08:00
|
|
|
return 0;
|
2016-03-31 04:37:05 +08:00
|
|
|
|
2016-04-21 04:37:57 +08:00
|
|
|
if (expected_error == ASN1_BIO) {
|
2017-04-10 05:44:08 +08:00
|
|
|
if (TEST_ptr_null(ASN1_item_d2i_bio(item_type, bio, NULL)))
|
2016-04-21 04:37:57 +08:00
|
|
|
ret = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-03-31 04:37:05 +08:00
|
|
|
/*
|
2016-04-21 04:37:57 +08:00
|
|
|
* 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.
|
2016-03-31 04:37:05 +08:00
|
|
|
*/
|
2017-12-08 02:39:34 +08:00
|
|
|
len = BIO_read(bio, buf, sizeof(buf));
|
2017-04-10 05:44:08 +08:00
|
|
|
if (!TEST_int_ge(len, 0))
|
2016-03-31 04:37:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
|
2016-04-21 04:37:57 +08:00
|
|
|
if (value == NULL) {
|
2017-04-10 05:44:08 +08:00
|
|
|
if (TEST_int_eq(expected_error, ASN1_DECODE))
|
2016-04-21 04:37:57 +08:00
|
|
|
ret = 1;
|
2016-03-31 04:37:05 +08:00
|
|
|
goto err;
|
2016-04-21 04:37:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
derlen = ASN1_item_i2d(value, &der, item_type);
|
2016-03-31 04:37:05 +08:00
|
|
|
|
2016-04-21 04:37:57 +08:00
|
|
|
if (der == NULL || derlen < 0) {
|
2017-04-10 05:44:08 +08:00
|
|
|
if (TEST_int_eq(expected_error, ASN1_ENCODE))
|
2016-04-21 04:37:57 +08:00
|
|
|
ret = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (derlen != len || memcmp(der, buf, derlen) != 0) {
|
2017-04-10 05:44:08 +08:00
|
|
|
if (TEST_int_eq(expected_error, ASN1_COMPARE))
|
2016-04-21 04:37:57 +08:00
|
|
|
ret = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:44:08 +08:00
|
|
|
if (TEST_int_eq(expected_error, ASN1_OK))
|
2016-04-21 04:37:57 +08:00
|
|
|
ret = 1;
|
2016-03-31 04:37:05 +08:00
|
|
|
|
|
|
|
err:
|
2016-04-23 01:37:42 +08:00
|
|
|
/* Don't indicate success for memory allocation errors */
|
2017-04-10 05:44:08 +08:00
|
|
|
if (ret == 1
|
|
|
|
&& !TEST_false(ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE))
|
2016-04-23 01:37:42 +08:00
|
|
|
ret = 0;
|
2016-03-31 04:37:05 +08:00
|
|
|
BIO_free(bio);
|
2016-04-21 04:37:57 +08:00
|
|
|
OPENSSL_free(der);
|
2016-03-31 04:37:05 +08:00
|
|
|
ASN1_item_free(value, item_type);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-18 09:48:27 +08:00
|
|
|
* Usage: d2i_test <name> <type> <file>, e.g.
|
2016-03-31 04:37:05 +08:00
|
|
|
* d2i_test generalname bad_generalname.der
|
|
|
|
*/
|
2017-07-18 09:48:27 +08:00
|
|
|
int setup_tests(void)
|
2016-03-31 04:37:05 +08:00
|
|
|
{
|
|
|
|
const char *test_type_name;
|
2016-04-21 04:37:57 +08:00
|
|
|
const char *expected_error_string;
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
static error_enum expected_errors[] = {
|
|
|
|
{"OK", ASN1_OK},
|
|
|
|
{"BIO", ASN1_BIO},
|
|
|
|
{"decode", ASN1_DECODE},
|
|
|
|
{"encode", ASN1_ENCODE},
|
|
|
|
{"compare", ASN1_COMPARE}
|
|
|
|
};
|
|
|
|
|
2017-07-18 09:48:27 +08:00
|
|
|
if (!TEST_ptr(test_type_name = test_get_argument(0))
|
|
|
|
|| !TEST_ptr(expected_error_string = test_get_argument(1))
|
|
|
|
|| !TEST_ptr(test_file = test_get_argument(2))) {
|
|
|
|
TEST_note("Usage: d2i_test item_name expected_error file.der");
|
|
|
|
return 0;
|
2016-04-21 04:37:57 +08:00
|
|
|
}
|
2016-03-31 04:37:05 +08:00
|
|
|
|
2016-09-28 07:24:58 +08:00
|
|
|
item_type = ASN1_ITEM_lookup(test_type_name);
|
|
|
|
|
2016-04-21 04:37:57 +08:00
|
|
|
if (item_type == NULL) {
|
2017-06-19 09:21:22 +08:00
|
|
|
TEST_error("Unknown type %s", test_type_name);
|
|
|
|
TEST_note("Supported types:");
|
2016-09-28 07:24:58 +08:00
|
|
|
for (i = 0;; i++) {
|
|
|
|
const ASN1_ITEM *it = ASN1_ITEM_get(i);
|
|
|
|
|
|
|
|
if (it == NULL)
|
|
|
|
break;
|
2017-06-19 09:21:22 +08:00
|
|
|
TEST_note("\t%s", it->sname);
|
2016-04-21 04:37:57 +08:00
|
|
|
}
|
2017-07-18 09:48:27 +08:00
|
|
|
return 0;
|
2016-04-21 04:37:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-04-10 05:44:08 +08:00
|
|
|
TEST_error("Unknown expected error %s\n", expected_error_string);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 0;
|
2016-03-31 04:37:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_TEST(test_bad_asn1);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
2016-03-31 04:37:05 +08:00
|
|
|
}
|