sslv2conftest.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Written by Matt Caswell for the OpenSSL Project */
  2. /* ====================================================================
  3. * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. #include <stdlib.h>
  56. #include <openssl/bio.h>
  57. #include <openssl/ssl.h>
  58. #include <openssl/err.h>
  59. #define TOTAL_NUM_TESTS 2
  60. #define TEST_SSL_CTX 0
  61. #define SSLV2ON 1
  62. #define SSLV2OFF 0
  63. SSL_CONF_CTX *confctx;
  64. SSL_CTX *ctx;
  65. SSL *ssl;
  66. static int checksslv2(int test, int sslv2)
  67. {
  68. int options;
  69. if (test == TEST_SSL_CTX) {
  70. options = SSL_CTX_get_options(ctx);
  71. } else {
  72. options = SSL_get_options(ssl);
  73. }
  74. return ((options & SSL_OP_NO_SSLv2) == 0) ^ (sslv2 == SSLV2OFF);
  75. }
  76. int main(int argc, char *argv[])
  77. {
  78. BIO *err;
  79. int testresult = 0;
  80. int currtest = 0;
  81. SSL_library_init();
  82. SSL_load_error_strings();
  83. err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  84. CRYPTO_malloc_debug_init();
  85. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  86. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  87. confctx = SSL_CONF_CTX_new();
  88. ctx = SSL_CTX_new(SSLv23_method());
  89. ssl = SSL_new(ctx);
  90. if (confctx == NULL || ctx == NULL)
  91. goto end;
  92. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  93. | SSL_CONF_FLAG_CLIENT
  94. | SSL_CONF_FLAG_SERVER);
  95. /*
  96. * For each test set up an SSL_CTX and SSL and see whether SSLv2 is enabled
  97. * as expected after various SSL_CONF_cmd("Protocol", ...) calls.
  98. */
  99. for (currtest = 0; currtest < TOTAL_NUM_TESTS; currtest++) {
  100. BIO_printf(err, "SSLv2 CONF Test number %d\n", currtest);
  101. if (currtest == TEST_SSL_CTX)
  102. SSL_CONF_CTX_set_ssl_ctx(confctx, ctx);
  103. else
  104. SSL_CONF_CTX_set_ssl(confctx, ssl);
  105. /* SSLv2 should be off by default */
  106. if (!checksslv2(currtest, SSLV2OFF)) {
  107. BIO_printf(err, "SSLv2 CONF Test: Off by default test FAIL\n");
  108. goto end;
  109. }
  110. if (SSL_CONF_cmd(confctx, "Protocol", "ALL") != 2
  111. || !SSL_CONF_CTX_finish(confctx)) {
  112. BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");
  113. goto end;
  114. }
  115. /* Should still be off even after ALL Protocols on */
  116. if (!checksslv2(currtest, SSLV2OFF)) {
  117. BIO_printf(err, "SSLv2 CONF Test: Off after config #1 FAIL\n");
  118. goto end;
  119. }
  120. if (SSL_CONF_cmd(confctx, "Protocol", "SSLv2") != 2
  121. || !SSL_CONF_CTX_finish(confctx)) {
  122. BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");
  123. goto end;
  124. }
  125. /* Should still be off even if explicitly asked for */
  126. if (!checksslv2(currtest, SSLV2OFF)) {
  127. BIO_printf(err, "SSLv2 CONF Test: Off after config #2 FAIL\n");
  128. goto end;
  129. }
  130. if (SSL_CONF_cmd(confctx, "Protocol", "-SSLv2") != 2
  131. || !SSL_CONF_CTX_finish(confctx)) {
  132. BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");;
  133. goto end;
  134. }
  135. if (!checksslv2(currtest, SSLV2OFF)) {
  136. BIO_printf(err, "SSLv2 CONF Test: Off after config #3 FAIL\n");
  137. goto end;
  138. }
  139. if (currtest == TEST_SSL_CTX)
  140. SSL_CTX_clear_options(ctx, SSL_OP_NO_SSLv2);
  141. else
  142. SSL_clear_options(ssl, SSL_OP_NO_SSLv2);
  143. if (!checksslv2(currtest, SSLV2ON)) {
  144. BIO_printf(err, "SSLv2 CONF Test: On after clear FAIL\n");
  145. goto end;
  146. }
  147. if (SSL_CONF_cmd(confctx, "Protocol", "ALL") != 2
  148. || !SSL_CONF_CTX_finish(confctx)) {
  149. BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");
  150. goto end;
  151. }
  152. /* Option has been cleared and config says have SSLv2 so should be on */
  153. if (!checksslv2(currtest, SSLV2ON)) {
  154. BIO_printf(err, "SSLv2 CONF Test: On after config #1 FAIL\n");
  155. goto end;
  156. }
  157. if (SSL_CONF_cmd(confctx, "Protocol", "SSLv2") != 2
  158. || !SSL_CONF_CTX_finish(confctx)) {
  159. BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");
  160. goto end;
  161. }
  162. /* Option has been cleared and config says have SSLv2 so should be on */
  163. if (!checksslv2(currtest, SSLV2ON)) {
  164. BIO_printf(err, "SSLv2 CONF Test: On after config #2 FAIL\n");
  165. goto end;
  166. }
  167. if (SSL_CONF_cmd(confctx, "Protocol", "-SSLv2") != 2
  168. || !SSL_CONF_CTX_finish(confctx)) {
  169. BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");
  170. goto end;
  171. }
  172. /* Option has been cleared but config says no SSLv2 so should be off */
  173. if (!checksslv2(currtest, SSLV2OFF)) {
  174. BIO_printf(err, "SSLv2 CONF Test: Off after config #4 FAIL\n");
  175. goto end;
  176. }
  177. }
  178. testresult = 1;
  179. end:
  180. SSL_free(ssl);
  181. SSL_CTX_free(ctx);
  182. SSL_CONF_CTX_free(confctx);
  183. if (!testresult) {
  184. printf("SSLv2 CONF test: FAILED (Test %d)\n", currtest);
  185. ERR_print_errors(err);
  186. } else {
  187. printf("SSLv2 CONF test: PASSED\n");
  188. }
  189. ERR_free_strings();
  190. ERR_remove_thread_state(NULL);
  191. EVP_cleanup();
  192. CRYPTO_cleanup_all_ex_data();
  193. CRYPTO_mem_leaks(err);
  194. BIO_free(err);
  195. return testresult ? EXIT_SUCCESS : EXIT_FAILURE;
  196. }