1 /* 2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 /* Regression tests for ASN.1 parsing bugs. */ 11 12 #include <stdio.h> 13 #include <string.h> 14 15 #include "testutil.h" 16 17 #include <openssl/asn1.h> 18 #include <openssl/asn1t.h> 19 #include <openssl/bio.h> 20 #include <openssl/err.h> 21 #include <openssl/x509.h> 22 #include <openssl/x509v3.h> 23 #ifndef OPENSSL_NO_CMS 24 # include <openssl/cms.h> 25 #endif 26 #include "e_os.h" 27 28 static const ASN1_ITEM *item_type; 29 static const char *test_file; 30 31 typedef enum { 32 ASN1_UNKNOWN, 33 ASN1_OK, 34 ASN1_BIO, 35 ASN1_DECODE, 36 ASN1_ENCODE, 37 ASN1_COMPARE 38 } expected_error_t; 39 40 typedef struct { 41 const char *str; 42 expected_error_t code; 43 } error_enum; 44 45 static expected_error_t expected_error = ASN1_UNKNOWN; 46 47 typedef struct d2i_test_fixture { 48 const char *test_case_name; 49 } D2I_TEST_FIXTURE; 50 51 static D2I_TEST_FIXTURE set_up(const char *const test_case_name) 52 { 53 D2I_TEST_FIXTURE fixture; 54 fixture.test_case_name = test_case_name; 55 return fixture; 56 } 57 58 static int execute_test(D2I_TEST_FIXTURE fixture) 59 { 60 BIO *bio = NULL; 61 ASN1_VALUE *value = NULL; 62 int ret = 0; 63 unsigned char buf[2048]; 64 const unsigned char *buf_ptr = buf; 65 unsigned char *der = NULL; 66 int derlen; 67 int len; 68 69 if ((bio = BIO_new_file(test_file, "r")) == NULL) 70 return 0; 71 72 if (expected_error == ASN1_BIO) { 73 value = ASN1_item_d2i_bio(item_type, bio, NULL); 74 if (value == NULL) 75 ret = 1; 76 goto err; 77 } 78 79 /* 80 * Unless we are testing it we don't use ASN1_item_d2i_bio because it 81 * performs sanity checks on the input and can reject it before the 82 * decoder is called. 83 */ 84 len = BIO_read(bio, buf, sizeof buf); 85 if (len < 0) 86 goto err; 87 88 value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type); 89 if (value == NULL) { 90 if (expected_error == ASN1_DECODE) 91 ret = 1; 92 goto err; 93 } 94 95 derlen = ASN1_item_i2d(value, &der, item_type); 96 97 if (der == NULL || derlen < 0) { 98 if (expected_error == ASN1_ENCODE) 99 ret = 1; 100 goto err; 101 } 102 103 if (derlen != len || memcmp(der, buf, derlen) != 0) { 104 if (expected_error == ASN1_COMPARE) 105 ret = 1; 106 goto err; 107 } 108 109 if (expected_error == ASN1_OK) 110 ret = 1; 111 112 err: 113 /* Don't indicate success for memory allocation errors */ 114 if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE) 115 ret = 0; 116 BIO_free(bio); 117 OPENSSL_free(der); 118 ASN1_item_free(value, item_type); 119 return ret; 120 } 121 122 static void tear_down(D2I_TEST_FIXTURE fixture) 123 { 124 ERR_print_errors_fp(stderr); 125 } 126 127 #define SETUP_D2I_TEST_FIXTURE() \ 128 SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up) 129 130 #define EXECUTE_D2I_TEST() \ 131 EXECUTE_TEST(execute_test, tear_down) 132 133 static int test_bad_asn1() 134 { 135 SETUP_D2I_TEST_FIXTURE(); 136 EXECUTE_D2I_TEST(); 137 } 138 139 /* 140 * Usage: d2i_test <type> <file>, e.g. 141 * d2i_test generalname bad_generalname.der 142 */ 143 int main(int argc, char **argv) 144 { 145 int result = 0; 146 const char *test_type_name; 147 const char *expected_error_string; 148 const char *p = getenv("OPENSSL_DEBUG_MEMORY"); 149 150 size_t i; 151 static ASN1_ITEM_EXP *items[] = { 152 ASN1_ITEM_ref(ASN1_ANY), 153 ASN1_ITEM_ref(X509), 154 ASN1_ITEM_ref(GENERAL_NAME), 155 ASN1_ITEM_ref(ASN1_INTEGER), 156 #ifndef OPENSSL_NO_CMS 157 ASN1_ITEM_ref(CMS_ContentInfo) 158 #endif 159 }; 160 161 static error_enum expected_errors[] = { 162 {"OK", ASN1_OK}, 163 {"BIO", ASN1_BIO}, 164 {"decode", ASN1_DECODE}, 165 {"encode", ASN1_ENCODE}, 166 {"compare", ASN1_COMPARE} 167 }; 168 169 if (p != NULL && strcmp(p, "on") == 0) 170 CRYPTO_set_mem_debug(1); 171 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 172 173 if (argc != 4) { 174 fprintf(stderr, 175 "Usage: d2i_test item_name expected_error file.der\n"); 176 return 1; 177 } 178 179 test_type_name = argv[1]; 180 expected_error_string = argv[2]; 181 test_file = argv[3]; 182 183 for (i = 0; i < OSSL_NELEM(items); i++) { 184 const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]); 185 if (strcmp(test_type_name, it->sname) == 0) { 186 item_type = it; 187 break; 188 } 189 } 190 if (item_type == NULL) { 191 fprintf(stderr, "Unknown type %s\n", test_type_name); 192 fprintf(stderr, "Supported types:\n"); 193 for (i = 0; i < OSSL_NELEM(items); i++) { 194 const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]); 195 fprintf(stderr, "\t%s\n", it->sname); 196 } 197 return 1; 198 } 199 200 for (i = 0; i < OSSL_NELEM(expected_errors); i++) { 201 if (strcmp(expected_errors[i].str, expected_error_string) == 0) { 202 expected_error = expected_errors[i].code; 203 break; 204 } 205 } 206 207 if (expected_error == ASN1_UNKNOWN) { 208 fprintf(stderr, "Unknown expected error %s\n", expected_error_string); 209 return 1; 210 } 211 212 ADD_TEST(test_bad_asn1); 213 214 result = run_tests(argv[0]); 215 216 #ifndef OPENSSL_NO_CRYPTO_MDEBUG 217 if (CRYPTO_mem_leaks_fp(stderr) <= 0) 218 result = 1; 219 #endif 220 221 return result; 222 } 223