verify_extra_test.c 5.4 KB

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