2
0

verify_extra_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2015-2017 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 STACK_OF(X509) *load_certs_from_file(const char *filename)
  20. {
  21. STACK_OF(X509) *certs;
  22. BIO *bio;
  23. X509 *x;
  24. bio = BIO_new_file(filename, "r");
  25. if (bio == NULL) {
  26. return NULL;
  27. }
  28. certs = sk_X509_new_null();
  29. if (certs == NULL) {
  30. BIO_free(bio);
  31. return NULL;
  32. }
  33. ERR_set_mark();
  34. do {
  35. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  36. if (x != NULL && !sk_X509_push(certs, x)) {
  37. sk_X509_pop_free(certs, X509_free);
  38. BIO_free(bio);
  39. return NULL;
  40. } else if (x == NULL) {
  41. /*
  42. * We probably just ran out of certs, so ignore any errors
  43. * generated
  44. */
  45. ERR_pop_to_mark();
  46. }
  47. } while (x != NULL);
  48. BIO_free(bio);
  49. return certs;
  50. }
  51. /*
  52. * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
  53. *
  54. * Chain is as follows:
  55. *
  56. * rootCA (self-signed)
  57. * |
  58. * interCA
  59. * |
  60. * subinterCA subinterCA (self-signed)
  61. * | |
  62. * leaf ------------------
  63. * |
  64. * bad
  65. *
  66. * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
  67. * leaf and bad have CA=FALSE
  68. *
  69. * subinterCA and subinterCA (ss) have the same subject name and keys
  70. *
  71. * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
  72. * (roots.pem)
  73. * leaf and subinterCA are in the untrusted list (untrusted.pem)
  74. * bad is the certificate being verified (bad.pem)
  75. *
  76. * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
  77. * CA=FALSE, and will therefore incorrectly verify bad
  78. *
  79. */
  80. static int test_alt_chains_cert_forgery(void)
  81. {
  82. int ret = 0;
  83. int i;
  84. X509 *x = NULL;
  85. STACK_OF(X509) *untrusted = NULL;
  86. BIO *bio = NULL;
  87. X509_STORE_CTX *sctx = NULL;
  88. X509_STORE *store = NULL;
  89. X509_LOOKUP *lookup = NULL;
  90. store = X509_STORE_new();
  91. if (store == NULL)
  92. goto err;
  93. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  94. if (lookup == NULL)
  95. goto err;
  96. if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM))
  97. goto err;
  98. untrusted = load_certs_from_file(untrusted_f);
  99. if ((bio = BIO_new_file(bad_f, "r")) == NULL)
  100. goto err;
  101. if ((x = PEM_read_bio_X509(bio, NULL, 0, NULL)) == NULL)
  102. goto err;
  103. sctx = X509_STORE_CTX_new();
  104. if (sctx == NULL)
  105. goto err;
  106. if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
  107. goto err;
  108. i = X509_verify_cert(sctx);
  109. if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
  110. /* This is the result we were expecting: Test passed */
  111. ret = 1;
  112. }
  113. err:
  114. X509_STORE_CTX_free(sctx);
  115. X509_free(x);
  116. BIO_free(bio);
  117. sk_X509_pop_free(untrusted, X509_free);
  118. X509_STORE_free(store);
  119. return ret;
  120. }
  121. int setup_tests(void)
  122. {
  123. if (!TEST_ptr(roots_f = test_get_argument(0))
  124. || !TEST_ptr(untrusted_f = test_get_argument(1))
  125. || !TEST_ptr(bad_f = test_get_argument(2))) {
  126. TEST_error("usage: verify_extra_test roots.pem untrusted.pem bad.pem\n");
  127. return 0;
  128. }
  129. ADD_TEST(test_alt_chains_cert_forgery);
  130. return 1;
  131. }