verify_extra_test.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 2015-2022 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/x509v3.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/err.h>
  16. #include "testutil.h"
  17. static const char *certs_dir;
  18. static char *roots_f = NULL;
  19. static char *untrusted_f = NULL;
  20. static char *bad_f = NULL;
  21. static char *good_f = NULL;
  22. static char *sroot_cert = NULL;
  23. static char *ca_cert = NULL;
  24. static char *ee_cert = NULL;
  25. static X509 *load_cert_pem(const char *file)
  26. {
  27. X509 *cert = NULL;
  28. BIO *bio = NULL;
  29. if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
  30. return NULL;
  31. if (TEST_int_gt(BIO_read_filename(bio, file), 0))
  32. (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  33. BIO_free(bio);
  34. return cert;
  35. }
  36. static STACK_OF(X509) *load_certs_from_file(const char *filename)
  37. {
  38. STACK_OF(X509) *certs;
  39. BIO *bio;
  40. X509 *x;
  41. bio = BIO_new_file(filename, "r");
  42. if (bio == NULL) {
  43. return NULL;
  44. }
  45. certs = sk_X509_new_null();
  46. if (certs == NULL) {
  47. BIO_free(bio);
  48. return NULL;
  49. }
  50. ERR_set_mark();
  51. do {
  52. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  53. if (x != NULL && !sk_X509_push(certs, x)) {
  54. sk_X509_pop_free(certs, X509_free);
  55. BIO_free(bio);
  56. return NULL;
  57. } else if (x == NULL) {
  58. /*
  59. * We probably just ran out of certs, so ignore any errors
  60. * generated
  61. */
  62. ERR_pop_to_mark();
  63. }
  64. } while (x != NULL);
  65. BIO_free(bio);
  66. return certs;
  67. }
  68. /*-
  69. * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
  70. *
  71. * Chain is as follows:
  72. *
  73. * rootCA (self-signed)
  74. * |
  75. * interCA
  76. * |
  77. * subinterCA subinterCA (self-signed)
  78. * | |
  79. * leaf ------------------
  80. * |
  81. * bad
  82. *
  83. * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
  84. * leaf and bad have CA=FALSE
  85. *
  86. * subinterCA and subinterCA (ss) have the same subject name and keys
  87. *
  88. * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
  89. * (roots.pem)
  90. * leaf and subinterCA are in the untrusted list (untrusted.pem)
  91. * bad is the certificate being verified (bad.pem)
  92. *
  93. * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
  94. * CA=FALSE, and will therefore incorrectly verify bad
  95. *
  96. */
  97. static int test_alt_chains_cert_forgery(void)
  98. {
  99. int ret = 0;
  100. int i;
  101. X509 *x = NULL;
  102. STACK_OF(X509) *untrusted = NULL;
  103. BIO *bio = NULL;
  104. X509_STORE_CTX *sctx = NULL;
  105. X509_STORE *store = NULL;
  106. X509_LOOKUP *lookup = NULL;
  107. store = X509_STORE_new();
  108. if (store == NULL)
  109. goto err;
  110. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  111. if (lookup == NULL)
  112. goto err;
  113. if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
  114. goto err;
  115. untrusted = load_certs_from_file(untrusted_f);
  116. if ((bio = BIO_new_file(bad_f, "r")) == NULL)
  117. goto err;
  118. if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
  119. goto err;
  120. sctx = X509_STORE_CTX_new();
  121. if (sctx == NULL)
  122. goto err;
  123. if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
  124. goto err;
  125. i = X509_verify_cert(sctx);
  126. if (i != 0 || X509_STORE_CTX_get_error(sctx) != X509_V_ERR_INVALID_CA)
  127. goto err;
  128. /* repeat with X509_V_FLAG_X509_STRICT */
  129. X509_STORE_CTX_cleanup(sctx);
  130. X509_STORE_set_flags(store, X509_V_FLAG_X509_STRICT);
  131. if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
  132. goto err;
  133. i = X509_verify_cert(sctx);
  134. if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA)
  135. /* This is the result we were expecting: Test passed */
  136. ret = 1;
  137. err:
  138. X509_STORE_CTX_free(sctx);
  139. X509_free(x);
  140. BIO_free(bio);
  141. sk_X509_pop_free(untrusted, X509_free);
  142. X509_STORE_free(store);
  143. return ret;
  144. }
  145. static int test_store_ctx(void)
  146. {
  147. X509_STORE_CTX *sctx = NULL;
  148. X509 *x = NULL;
  149. BIO *bio = NULL;
  150. int testresult = 0, ret;
  151. bio = BIO_new_file(bad_f, "r");
  152. if (bio == NULL)
  153. goto err;
  154. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  155. if (x == NULL)
  156. goto err;
  157. sctx = X509_STORE_CTX_new();
  158. if (sctx == NULL)
  159. goto err;
  160. if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
  161. goto err;
  162. /* Verifying a cert where we have no trusted certs should fail */
  163. ret = X509_verify_cert(sctx);
  164. if (ret == 0) {
  165. /* This is the result we were expecting: Test passed */
  166. testresult = 1;
  167. }
  168. err:
  169. X509_STORE_CTX_free(sctx);
  170. X509_free(x);
  171. BIO_free(bio);
  172. return testresult;
  173. }
  174. static int test_self_signed(const char *filename, int expected)
  175. {
  176. X509 *cert = load_cert_pem(filename);
  177. STACK_OF(X509) *trusted = sk_X509_new_null();
  178. X509_STORE_CTX *ctx = X509_STORE_CTX_new();
  179. int ret;
  180. ret = TEST_ptr(cert)
  181. && TEST_true(sk_X509_push(trusted, cert))
  182. && TEST_true(X509_STORE_CTX_init(ctx, NULL, cert, NULL));
  183. X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
  184. ret = ret && TEST_int_eq(X509_verify_cert(ctx), expected);
  185. X509_STORE_CTX_free(ctx);
  186. sk_X509_free(trusted);
  187. X509_free(cert);
  188. return ret;
  189. }
  190. static int test_self_signed_good(void)
  191. {
  192. return test_self_signed(good_f, 1);
  193. }
  194. static int test_self_signed_bad(void)
  195. {
  196. return test_self_signed(bad_f, 0);
  197. }
  198. static int do_test_purpose(int purpose, int expected)
  199. {
  200. X509 *eecert = load_cert_pem(ee_cert); /* may result in NULL */
  201. X509 *untrcert = load_cert_pem(ca_cert);
  202. X509 *trcert = load_cert_pem(sroot_cert);
  203. STACK_OF(X509) *trusted = sk_X509_new_null();
  204. STACK_OF(X509) *untrusted = sk_X509_new_null();
  205. X509_STORE_CTX *ctx = X509_STORE_CTX_new();
  206. int testresult = 0;
  207. if (!TEST_ptr(eecert)
  208. || !TEST_ptr(untrcert)
  209. || !TEST_ptr(trcert)
  210. || !TEST_ptr(trusted)
  211. || !TEST_ptr(untrusted)
  212. || !TEST_ptr(ctx))
  213. goto err;
  214. if (!TEST_true(sk_X509_push(trusted, trcert)))
  215. goto err;
  216. trcert = NULL;
  217. if (!TEST_true(sk_X509_push(untrusted, untrcert)))
  218. goto err;
  219. untrcert = NULL;
  220. if (!TEST_true(X509_STORE_CTX_init(ctx, NULL, eecert, untrusted)))
  221. goto err;
  222. if (!TEST_true(X509_STORE_CTX_set_purpose(ctx, purpose)))
  223. goto err;
  224. /*
  225. * X509_STORE_CTX_set0_trusted_stack() is bady named. Despite the set0 name
  226. * we are still responsible for freeing trusted after we have finished with
  227. * it.
  228. */
  229. X509_STORE_CTX_set0_trusted_stack(ctx, trusted);
  230. if (!TEST_int_eq(X509_verify_cert(ctx), expected))
  231. goto err;
  232. testresult = 1;
  233. err:
  234. sk_X509_pop_free(trusted, X509_free);
  235. sk_X509_pop_free(untrusted, X509_free);
  236. X509_STORE_CTX_free(ctx);
  237. X509_free(eecert);
  238. X509_free(untrcert);
  239. X509_free(trcert);
  240. return testresult;
  241. }
  242. static int test_purpose_ssl_client(void)
  243. {
  244. return do_test_purpose(X509_PURPOSE_SSL_CLIENT, 0);
  245. }
  246. static int test_purpose_ssl_server(void)
  247. {
  248. return do_test_purpose(X509_PURPOSE_SSL_SERVER, 1);
  249. }
  250. static int test_purpose_any(void)
  251. {
  252. return do_test_purpose(X509_PURPOSE_ANY, 1);
  253. }
  254. int setup_tests(void)
  255. {
  256. if (!TEST_ptr(certs_dir = test_get_argument(0))) {
  257. TEST_error("usage: verify_extra_test certs-dir\n");
  258. return 0;
  259. }
  260. if (!TEST_ptr(roots_f = test_mk_file_path(certs_dir, "roots.pem"))
  261. || !TEST_ptr(untrusted_f = test_mk_file_path(certs_dir, "untrusted.pem"))
  262. || !TEST_ptr(bad_f = test_mk_file_path(certs_dir, "bad.pem"))
  263. || !TEST_ptr(good_f = test_mk_file_path(certs_dir, "rootCA.pem"))
  264. || !TEST_ptr(sroot_cert = test_mk_file_path(certs_dir, "sroot-cert.pem"))
  265. || !TEST_ptr(ca_cert = test_mk_file_path(certs_dir, "ca-cert.pem"))
  266. || !TEST_ptr(ee_cert = test_mk_file_path(certs_dir, "ee-cert.pem")))
  267. goto err;
  268. ADD_TEST(test_alt_chains_cert_forgery);
  269. ADD_TEST(test_store_ctx);
  270. ADD_TEST(test_self_signed_good);
  271. ADD_TEST(test_self_signed_bad);
  272. ADD_TEST(test_purpose_ssl_client);
  273. ADD_TEST(test_purpose_ssl_server);
  274. ADD_TEST(test_purpose_any);
  275. return 1;
  276. err:
  277. cleanup_tests();
  278. return 0;
  279. }
  280. void cleanup_tests(void)
  281. {
  282. OPENSSL_free(roots_f);
  283. OPENSSL_free(untrusted_f);
  284. OPENSSL_free(bad_f);
  285. OPENSSL_free(good_f);
  286. OPENSSL_free(sroot_cert);
  287. OPENSSL_free(ca_cert);
  288. OPENSSL_free(ee_cert);
  289. }