verify_extra_test.c 6.4 KB

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