verify_extra_test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 2015-2020 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 *root_f;
  18. static const char *roots_f;
  19. static const char *untrusted_f;
  20. static const char *bad_f;
  21. static const char *req_f;
  22. static X509 *load_cert_from_file(const char *filename)
  23. {
  24. X509 *cert = NULL;
  25. BIO *bio;
  26. bio = BIO_new_file(filename, "r");
  27. if (bio != NULL)
  28. cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
  29. BIO_free(bio);
  30. return cert;
  31. }
  32. static STACK_OF(X509) *load_certs_from_file(const char *filename)
  33. {
  34. STACK_OF(X509) *certs;
  35. BIO *bio;
  36. X509 *x;
  37. bio = BIO_new_file(filename, "r");
  38. if (bio == NULL) {
  39. return NULL;
  40. }
  41. certs = sk_X509_new_null();
  42. if (certs == NULL) {
  43. BIO_free(bio);
  44. return NULL;
  45. }
  46. ERR_set_mark();
  47. do {
  48. x = PEM_read_bio_X509(bio, NULL, 0, NULL);
  49. if (x != NULL && !sk_X509_push(certs, x)) {
  50. sk_X509_pop_free(certs, X509_free);
  51. BIO_free(bio);
  52. return NULL;
  53. } else if (x == NULL) {
  54. /*
  55. * We probably just ran out of certs, so ignore any errors
  56. * generated
  57. */
  58. ERR_pop_to_mark();
  59. }
  60. } while (x != NULL);
  61. BIO_free(bio);
  62. return certs;
  63. }
  64. /*
  65. * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
  66. *
  67. * Chain is as follows:
  68. *
  69. * rootCA (self-signed)
  70. * |
  71. * interCA
  72. * |
  73. * subinterCA subinterCA (self-signed)
  74. * | |
  75. * leaf ------------------
  76. * |
  77. * bad
  78. *
  79. * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
  80. * leaf and bad have CA=FALSE
  81. *
  82. * subinterCA and subinterCA (ss) have the same subject name and keys
  83. *
  84. * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
  85. * (roots.pem)
  86. * leaf and subinterCA are in the untrusted list (untrusted.pem)
  87. * bad is the certificate being verified (bad.pem)
  88. *
  89. * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
  90. * CA=FALSE, and will therefore incorrectly verify bad
  91. *
  92. */
  93. static int test_alt_chains_cert_forgery(void)
  94. {
  95. int ret = 0;
  96. int i;
  97. X509 *x = NULL;
  98. STACK_OF(X509) *untrusted = 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 ((x = load_cert_from_file(bad_f)) == NULL)
  112. goto err;
  113. sctx = X509_STORE_CTX_new();
  114. if (sctx == NULL)
  115. goto err;
  116. if (!X509_STORE_CTX_init(sctx, store, x, untrusted))
  117. goto err;
  118. i = X509_verify_cert(sctx);
  119. if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) {
  120. /* This is the result we were expecting: Test passed */
  121. ret = 1;
  122. }
  123. err:
  124. X509_STORE_CTX_free(sctx);
  125. X509_free(x);
  126. sk_X509_pop_free(untrusted, X509_free);
  127. X509_STORE_free(store);
  128. return ret;
  129. }
  130. static int test_store_ctx(void)
  131. {
  132. X509_STORE_CTX *sctx = NULL;
  133. X509 *x = NULL;
  134. int testresult = 0, ret;
  135. x = load_cert_from_file(bad_f);
  136. if (x == NULL)
  137. goto err;
  138. sctx = X509_STORE_CTX_new();
  139. if (sctx == NULL)
  140. goto err;
  141. if (!X509_STORE_CTX_init(sctx, NULL, x, NULL))
  142. goto err;
  143. /* Verifying a cert where we have no trusted certs should fail */
  144. ret = X509_verify_cert(sctx);
  145. if (ret == 0) {
  146. /* This is the result we were expecting: Test passed */
  147. testresult = 1;
  148. }
  149. err:
  150. X509_STORE_CTX_free(sctx);
  151. X509_free(x);
  152. return testresult;
  153. }
  154. OPT_TEST_DECLARE_USAGE("roots.pem untrusted.pem bad.pem\n")
  155. static int test_distinguishing_id(void)
  156. {
  157. X509 *x = NULL;
  158. int ret = 0;
  159. ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
  160. char *distid = "this is an ID";
  161. x = load_cert_from_file(bad_f);
  162. if (x == NULL)
  163. goto err;
  164. v = ASN1_OCTET_STRING_new();
  165. if (v == NULL)
  166. goto err;
  167. if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
  168. (int)strlen(distid))) {
  169. ASN1_OCTET_STRING_free(v);
  170. goto err;
  171. }
  172. X509_set0_distinguishing_id(x, v);
  173. v2 = X509_get0_distinguishing_id(x);
  174. if (!TEST_ptr(v2)
  175. || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
  176. goto err;
  177. ret = 1;
  178. err:
  179. X509_free(x);
  180. return ret;
  181. }
  182. static int test_req_distinguishing_id(void)
  183. {
  184. X509_REQ *x = NULL;
  185. BIO *bio = NULL;
  186. int ret = 0;
  187. ASN1_OCTET_STRING *v = NULL, *v2 = NULL;
  188. char *distid = "this is an ID";
  189. bio = BIO_new_file(req_f, "r");
  190. if (bio == NULL)
  191. goto err;
  192. x = PEM_read_bio_X509_REQ(bio, NULL, 0, NULL);
  193. if (x == NULL)
  194. goto err;
  195. v = ASN1_OCTET_STRING_new();
  196. if (v == NULL)
  197. goto err;
  198. if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid,
  199. (int)strlen(distid))) {
  200. ASN1_OCTET_STRING_free(v);
  201. goto err;
  202. }
  203. X509_REQ_set0_distinguishing_id(x, v);
  204. v2 = X509_REQ_get0_distinguishing_id(x);
  205. if (!TEST_ptr(v2)
  206. || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0))
  207. goto err;
  208. ret = 1;
  209. err:
  210. X509_REQ_free(x);
  211. BIO_free(bio);
  212. return ret;
  213. }
  214. static int test_self_signed(const char *filename, int expected)
  215. {
  216. X509 *cert;
  217. int ret;
  218. cert = load_cert_from_file(filename); /* may result in NULL */
  219. ret = TEST_int_eq(X509_self_signed(cert, 1), expected);
  220. X509_free(cert);
  221. return ret;
  222. }
  223. static int test_self_signed_good(void)
  224. {
  225. return test_self_signed(root_f, 1);
  226. }
  227. static int test_self_signed_bad(void)
  228. {
  229. return test_self_signed(bad_f, 0);
  230. }
  231. static int test_self_signed_error(void)
  232. {
  233. return test_self_signed("nonexistent file name", -1);
  234. }
  235. int setup_tests(void)
  236. {
  237. if (!test_skip_common_options()) {
  238. TEST_error("Error parsing test options\n");
  239. return 0;
  240. }
  241. if (!TEST_ptr(root_f = test_get_argument(0))
  242. || !TEST_ptr(roots_f = test_get_argument(1))
  243. || !TEST_ptr(untrusted_f = test_get_argument(2))
  244. || !TEST_ptr(bad_f = test_get_argument(3))
  245. || !TEST_ptr(req_f = test_get_argument(4)))
  246. return 0;
  247. ADD_TEST(test_alt_chains_cert_forgery);
  248. ADD_TEST(test_store_ctx);
  249. ADD_TEST(test_distinguishing_id);
  250. ADD_TEST(test_req_distinguishing_id);
  251. ADD_TEST(test_self_signed_good);
  252. ADD_TEST(test_self_signed_bad);
  253. ADD_TEST(test_self_signed_error);
  254. return 1;
  255. }