d2i_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /* Regression tests for ASN.1 parsing bugs. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "testutil.h"
  13. #include <openssl/asn1.h>
  14. #include <openssl/asn1t.h>
  15. #include <openssl/bio.h>
  16. #include <openssl/err.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/x509v3.h>
  19. #include "e_os.h"
  20. static const ASN1_ITEM *item_type;
  21. static const char *test_file;
  22. typedef enum {
  23. ASN1_UNKNOWN,
  24. ASN1_OK,
  25. ASN1_BIO,
  26. ASN1_DECODE,
  27. ASN1_ENCODE,
  28. ASN1_COMPARE
  29. } expected_error_t;
  30. typedef struct {
  31. const char *str;
  32. expected_error_t code;
  33. } error_enum;
  34. static expected_error_t expected_error = ASN1_UNKNOWN;
  35. typedef struct d2i_test_fixture {
  36. const char *test_case_name;
  37. } D2I_TEST_FIXTURE;
  38. static D2I_TEST_FIXTURE set_up(const char *const test_case_name)
  39. {
  40. D2I_TEST_FIXTURE fixture;
  41. fixture.test_case_name = test_case_name;
  42. return fixture;
  43. }
  44. static int execute_test(D2I_TEST_FIXTURE fixture)
  45. {
  46. BIO *bio = NULL;
  47. ASN1_VALUE *value = NULL;
  48. int ret = 0;
  49. unsigned char buf[2048];
  50. const unsigned char *buf_ptr = buf;
  51. unsigned char *der = NULL;
  52. int derlen;
  53. int len;
  54. if ((bio = BIO_new_file(test_file, "r")) == NULL)
  55. return 0;
  56. if (expected_error == ASN1_BIO) {
  57. value = ASN1_item_d2i_bio(item_type, bio, NULL);
  58. if (value == NULL)
  59. ret = 1;
  60. goto err;
  61. }
  62. /*
  63. * Unless we are testing it we don't use ASN1_item_d2i_bio because it
  64. * performs sanity checks on the input and can reject it before the
  65. * decoder is called.
  66. */
  67. len = BIO_read(bio, buf, sizeof buf);
  68. if (len < 0)
  69. goto err;
  70. value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type);
  71. if (value == NULL) {
  72. if (expected_error == ASN1_DECODE)
  73. ret = 1;
  74. goto err;
  75. }
  76. derlen = ASN1_item_i2d(value, &der, item_type);
  77. if (der == NULL || derlen < 0) {
  78. if (expected_error == ASN1_ENCODE)
  79. ret = 1;
  80. goto err;
  81. }
  82. if (derlen != len || memcmp(der, buf, derlen) != 0) {
  83. if (expected_error == ASN1_COMPARE)
  84. ret = 1;
  85. goto err;
  86. }
  87. if (expected_error == ASN1_OK)
  88. ret = 1;
  89. err:
  90. /* Don't indicate success for memory allocation errors */
  91. if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)
  92. ret = 0;
  93. BIO_free(bio);
  94. OPENSSL_free(der);
  95. ASN1_item_free(value, item_type);
  96. return ret;
  97. }
  98. static void tear_down(D2I_TEST_FIXTURE fixture)
  99. {
  100. ERR_print_errors_fp(stderr);
  101. }
  102. #define SETUP_D2I_TEST_FIXTURE() \
  103. SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up)
  104. #define EXECUTE_D2I_TEST() \
  105. EXECUTE_TEST(execute_test, tear_down)
  106. static int test_bad_asn1()
  107. {
  108. SETUP_D2I_TEST_FIXTURE();
  109. EXECUTE_D2I_TEST();
  110. }
  111. /*
  112. * Usage: d2i_test <type> <file>, e.g.
  113. * d2i_test generalname bad_generalname.der
  114. */
  115. int main(int argc, char **argv)
  116. {
  117. int result = 0;
  118. const char *test_type_name;
  119. const char *expected_error_string;
  120. size_t i;
  121. static ASN1_ITEM_EXP *items[] = {
  122. ASN1_ITEM_ref(ASN1_ANY),
  123. ASN1_ITEM_ref(X509),
  124. ASN1_ITEM_ref(GENERAL_NAME),
  125. ASN1_ITEM_ref(ASN1_INTEGER)
  126. };
  127. static error_enum expected_errors[] = {
  128. {"OK", ASN1_OK},
  129. {"BIO", ASN1_BIO},
  130. {"decode", ASN1_DECODE},
  131. {"encode", ASN1_ENCODE},
  132. {"compare", ASN1_COMPARE}
  133. };
  134. if (argc != 4) {
  135. fprintf(stderr,
  136. "Usage: d2i_test item_name expected_error file.der\n");
  137. return 1;
  138. }
  139. test_type_name = argv[1];
  140. expected_error_string = argv[2];
  141. test_file = argv[3];
  142. for (i = 0; i < OSSL_NELEM(items); i++) {
  143. const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]);
  144. if (strcmp(test_type_name, it->sname) == 0) {
  145. item_type = it;
  146. break;
  147. }
  148. }
  149. if (item_type == NULL) {
  150. fprintf(stderr, "Unknown type %s\n", test_type_name);
  151. fprintf(stderr, "Supported types:\n");
  152. for (i = 0; i < OSSL_NELEM(items); i++) {
  153. const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]);
  154. fprintf(stderr, "\t%s\n", it->sname);
  155. }
  156. return 1;
  157. }
  158. for (i = 0; i < OSSL_NELEM(expected_errors); i++) {
  159. if (strcmp(expected_errors[i].str, expected_error_string) == 0) {
  160. expected_error = expected_errors[i].code;
  161. break;
  162. }
  163. }
  164. if (expected_error == ASN1_UNKNOWN) {
  165. fprintf(stderr, "Unknown expected error %s\n", expected_error_string);
  166. return 1;
  167. }
  168. ADD_TEST(test_bad_asn1);
  169. result = run_tests(argv[0]);
  170. return result;
  171. }