verify_extra_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2015-2021 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. #include <stdio.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/err.h>
  15. #include "testutil.h"
  16. static const char *roots_f;
  17. static const char *untrusted_f;
  18. static const char *bad_f;
  19. static const char *good_f;
  20. static X509 *load_cert_pem(const char *file)
  21. {
  22. X509 *cert = NULL;
  23. BIO *bio = NULL;
  24. if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
  25. return NULL;
  26. if (TEST_int_gt(BIO_read_filename(bio, file), 0))
  27. (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  28. BIO_free(bio);
  29. return cert;
  30. }
  31. static STACK_OF(X509) *load_certs_from_file(const char *filename)
  32. {
  33. STACK_OF(X509) *certs;
  34. BIO *bio;
  35. X509 *x;
  36. bio = BIO_new_file(filename, "r");
  37. if (bio == NULL) {
  38. return NULL;
  39. }
  40. certs = sk_X509_new_null();
  41. if (certs == NULL) {
  42. BIO_free(bio);
  43. return NULL;
  44. }
  45. ERR_set_mark();
  46. do {
  47. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  48. if (x != NULL && !sk_X509_push(certs, x)) {
  49. sk_X509_pop_free(certs, X509_free);
  50. BIO_free(bio);
  51. return NULL;
  52. } else if (x == NULL) {
  53. /*
  54. * We probably just ran out of certs, so ignore any errors
  55. * generated
  56. */
  57. ERR_pop_to_mark();
  58. }
  59. } while (x != NULL);
  60. BIO_free(bio);
  61. return certs;
  62. }
  63. /*-
  64. * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
  65. *
  66. * Chain is as follows:
  67. *
  68. * rootCA (self-signed)
  69. * |
  70. * interCA
  71. * |
  72. * subinterCA subinterCA (self-signed)
  73. * | |
  74. * leaf ------------------
  75. * |
  76. * bad
  77. *
  78. * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
  79. * leaf and bad have CA=FALSE
  80. *
  81. * subinterCA and subinterCA (ss) have the same subject name and keys
  82. *
  83. * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
  84. * (roots.pem)
  85. * leaf and subinterCA are in the untrusted list (untrusted.pem)
  86. * bad is the certificate being verified (bad.pem)
  87. *
  88. * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
  89. * CA=FALSE, and will therefore incorrectly verify bad
  90. *
  91. */
  92. static int test_alt_chains_cert_forgery(void)
  93. {
  94. int ret = 0;
  95. int i;
  96. X509 *x = NULL;
  97. STACK_OF(X509) *untrusted = NULL;
  98. BIO *bio = NULL;
  99. X509_STORE_CTX *sctx = NULL;
  100. X509_STORE *store = NULL;
  101. X509_LOOKUP *lookup = NULL;
  102. store = X509_STORE_new();
  103. if (store == NULL)
  104. goto err;
  105. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  106. if (lookup == NULL)
  107. goto err;
  108. if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
  109. goto err;
  110. untrusted = load_certs_from_file(untrusted_f);
  111. if ((bio = BIO_new_file(bad_f, "r")) == NULL)
  112. goto err;
  113. if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
  114. goto err;
  115. sctx = X509_STORE_CTX_new();
  116. if (sctx == NULL)
  117. goto err;
  118. if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
  119. goto err;
  120. i = X509_verify_cert(sctx);
  121. if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
  122. /* This is the result we were expecting: Test passed */
  123. ret = 1;
  124. }
  125. err:
  126. X509_STORE_CTX_free(sctx);
  127. X509_free(x);
  128. BIO_free(bio);
  129. sk_X509_pop_free(untrusted, X509_free);
  130. X509_STORE_free(store);
  131. return ret;
  132. }
  133. static int test_store_ctx(void)
  134. {
  135. X509_STORE_CTX *sctx = NULL;
  136. X509 *x = NULL;
  137. BIO *bio = NULL;
  138. int testresult = 0, ret;
  139. bio = BIO_new_file(bad_f, "r");
  140. if (bio == NULL)
  141. goto err;
  142. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  143. if (x == NULL)
  144. goto err;
  145. sctx = X509_STORE_CTX_new();
  146. if (sctx == NULL)
  147. goto err;
  148. if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
  149. goto err;
  150. /* Verifying a cert where we have no trusted certs should fail */
  151. ret = X509_verify_cert(sctx);
  152. if (ret == 0) {
  153. /* This is the result we were expecting: Test passed */
  154. testresult = 1;
  155. }
  156. err:
  157. X509_STORE_CTX_free(sctx);
  158. X509_free(x);
  159. BIO_free(bio);
  160. return testresult;
  161. }
  162. static int test_self_signed(const char *filename, int expected)
  163. {
  164. X509 *cert = load_cert_pem(filename);
  165. STACK_OF(X509) *trusted = sk_X509_new_null();
  166. X509_STORE_CTX *ctx = X509_STORE_CTX_new();
  167. int ret;
  168. ret = TEST_ptr(cert)
  169. && TEST_true(sk_X509_push(trusted, cert))
  170. && TEST_true(X509_STORE_CTX_init(ctx, NULL, cert, NULL));
  171. X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
  172. ret = ret && TEST_int_eq(X509_verify_cert(ctx), expected);
  173. X509_STORE_CTX_free(ctx);
  174. sk_X509_free(trusted);
  175. X509_free(cert);
  176. return ret;
  177. }
  178. static int test_self_signed_good(void)
  179. {
  180. return test_self_signed(good_f, 1);
  181. }
  182. static int test_self_signed_bad(void)
  183. {
  184. return test_self_signed(bad_f, 0);
  185. }
  186. int setup_tests(void)
  187. {
  188. if (!TEST_ptr(roots_f = test_get_argument(0))
  189. || !TEST_ptr(untrusted_f = test_get_argument(1))
  190. || !TEST_ptr(bad_f = test_get_argument(2))
  191. || !TEST_ptr(good_f = test_get_argument(3))) {
  192. TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem good.pem\n");
  193. return 0;
  194. }
  195. ADD_TEST(test_alt_chains_cert_forgery);
  196. ADD_TEST(test_store_ctx);
  197. ADD_TEST(test_self_signed_good);
  198. ADD_TEST(test_self_signed_bad);
  199. return 1;
  200. }