verify_extra_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. goto err;
  123. /* repeat with X509_V_FLAG_X509_STRICT */
  124. X509_STORE_CTX_cleanup(sctx);
  125. X509_STORE_set_flags(store, X509_V_FLAG_X509_STRICT);
  126. if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
  127. goto err;
  128. i = X509_verify_cert(sctx);
  129. if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA)
  130. /* This is the result we were expecting: Test passed */
  131. ret = 1;
  132. err:
  133. X509_STORE_CTX_free(sctx);
  134. X509_free(x);
  135. BIO_free(bio);
  136. sk_X509_pop_free(untrusted, X509_free);
  137. X509_STORE_free(store);
  138. return ret;
  139. }
  140. static int test_store_ctx(void)
  141. {
  142. X509_STORE_CTX *sctx = NULL;
  143. X509 *x = NULL;
  144. BIO *bio = NULL;
  145. int testresult = 0, ret;
  146. bio = BIO_new_file(bad_f, "r");
  147. if (bio == NULL)
  148. goto err;
  149. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  150. if (x == NULL)
  151. goto err;
  152. sctx = X509_STORE_CTX_new();
  153. if (sctx == NULL)
  154. goto err;
  155. if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
  156. goto err;
  157. /* Verifying a cert where we have no trusted certs should fail */
  158. ret = X509_verify_cert(sctx);
  159. if (ret == 0) {
  160. /* This is the result we were expecting: Test passed */
  161. testresult = 1;
  162. }
  163. err:
  164. X509_STORE_CTX_free(sctx);
  165. X509_free(x);
  166. BIO_free(bio);
  167. return testresult;
  168. }
  169. static int test_self_signed(const char *filename, int expected)
  170. {
  171. X509 *cert = load_cert_pem(filename);
  172. STACK_OF(X509) *trusted = sk_X509_new_null();
  173. X509_STORE_CTX *ctx = X509_STORE_CTX_new();
  174. int ret;
  175. ret = TEST_ptr(cert)
  176. && TEST_true(sk_X509_push(trusted, cert))
  177. && TEST_true(X509_STORE_CTX_init(ctx, NULL, cert, NULL));
  178. X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
  179. ret = ret && TEST_int_eq(X509_verify_cert(ctx), expected);
  180. X509_STORE_CTX_free(ctx);
  181. sk_X509_free(trusted);
  182. X509_free(cert);
  183. return ret;
  184. }
  185. static int test_self_signed_good(void)
  186. {
  187. return test_self_signed(good_f, 1);
  188. }
  189. static int test_self_signed_bad(void)
  190. {
  191. return test_self_signed(bad_f, 0);
  192. }
  193. int setup_tests(void)
  194. {
  195. if (!TEST_ptr(roots_f = test_get_argument(0))
  196. || !TEST_ptr(untrusted_f = test_get_argument(1))
  197. || !TEST_ptr(bad_f = test_get_argument(2))
  198. || !TEST_ptr(good_f = test_get_argument(3))) {
  199. TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem good.pem\n");
  200. return 0;
  201. }
  202. ADD_TEST(test_alt_chains_cert_forgery);
  203. ADD_TEST(test_store_ctx);
  204. ADD_TEST(test_self_signed_good);
  205. ADD_TEST(test_self_signed_bad);
  206. return 1;
  207. }