cipherlist_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL licenses, (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <stdio.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/err.h>
  13. #include <openssl/e_os2.h>
  14. #include <openssl/ssl.h>
  15. #include <openssl/ssl3.h>
  16. #include <openssl/tls1.h>
  17. #include "e_os.h"
  18. #include "testutil.h"
  19. typedef struct cipherlist_test_fixture {
  20. const char *test_case_name;
  21. SSL_CTX *server;
  22. SSL_CTX *client;
  23. } CIPHERLIST_TEST_FIXTURE;
  24. static CIPHERLIST_TEST_FIXTURE set_up(const char *const test_case_name)
  25. {
  26. CIPHERLIST_TEST_FIXTURE fixture;
  27. fixture.test_case_name = test_case_name;
  28. fixture.server = SSL_CTX_new(TLS_server_method());
  29. fixture.client = SSL_CTX_new(TLS_client_method());
  30. TEST_check(fixture.client != NULL && fixture.server != NULL);
  31. return fixture;
  32. }
  33. /*
  34. * All ciphers in the DEFAULT cipherlist meet the default security level.
  35. * However, default supported ciphers exclude SRP and PSK ciphersuites
  36. * for which no callbacks have been set up.
  37. *
  38. * Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled,
  39. * and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA
  40. * are currently broken and should be considered mission impossible in libssl.
  41. */
  42. static const uint32_t default_ciphers_in_order[] = {
  43. #ifndef OPENSSL_NO_TLS1_2
  44. # ifndef OPENSSL_NO_EC
  45. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  46. TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  47. # endif
  48. # ifndef OPENSSL_NO_DH
  49. TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
  50. # endif
  51. # if !defined OPENSSL_NO_CHACHA && !defined OPENSSL_NO_POLY1305
  52. # ifndef OPENSSL_NO_EC
  53. TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  54. TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  55. # endif
  56. # ifndef OPENSSL_NO_DH
  57. TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305,
  58. # endif
  59. # endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */
  60. # ifndef OPENSSL_NO_EC
  61. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  62. TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  63. # endif
  64. # ifndef OPENSSL_NO_DH
  65. TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
  66. # endif
  67. # ifndef OPENSSL_NO_EC
  68. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
  69. TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
  70. # endif
  71. # ifndef OPENSSL_NO_DH
  72. TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
  73. # endif
  74. # ifndef OPENSSL_NO_EC
  75. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
  76. TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
  77. # endif
  78. # ifndef OPENSSL_NO_DH
  79. TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
  80. # endif
  81. #endif /* !OPENSSL_NO_TLS1_2 */
  82. #ifndef OPENSSL_NO_EC
  83. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  84. TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  85. #endif
  86. #ifndef OPENSSL_NO_DH
  87. TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
  88. #endif
  89. #ifndef OPENSSL_NO_EC
  90. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  91. TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  92. #endif
  93. #ifndef OPENSSL_NO_DH
  94. TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
  95. #endif
  96. #ifndef OPENSSL_NO_TLS1_2
  97. TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  98. TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  99. #endif
  100. #ifndef OPENSSL_NO_TLS1_3
  101. TLS1_3_CK_AES_256_GCM_SHA384,
  102. TLS1_3_CK_CHACHA20_POLY1305_SHA256,
  103. TLS1_3_CK_AES_128_GCM_SHA256,
  104. #endif
  105. #ifndef OPENSSL_NO_TLS1_2
  106. TLS1_CK_RSA_WITH_AES_256_SHA256,
  107. TLS1_CK_RSA_WITH_AES_128_SHA256,
  108. #endif
  109. TLS1_CK_RSA_WITH_AES_256_SHA,
  110. TLS1_CK_RSA_WITH_AES_128_SHA,
  111. };
  112. static int test_default_cipherlist(SSL_CTX *ctx)
  113. {
  114. STACK_OF(SSL_CIPHER) *ciphers;
  115. SSL *ssl;
  116. int i, ret = 0, num_expected_ciphers, num_ciphers;
  117. uint32_t expected_cipher_id, cipher_id;
  118. ssl = SSL_new(ctx);
  119. TEST_check(ssl != NULL);
  120. ciphers = SSL_get1_supported_ciphers(ssl);
  121. TEST_check(ciphers != NULL);
  122. num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
  123. num_ciphers = sk_SSL_CIPHER_num(ciphers);
  124. if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
  125. goto err;
  126. for (i = 0; i < num_ciphers; i++) {
  127. expected_cipher_id = default_ciphers_in_order[i];
  128. cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i));
  129. if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
  130. TEST_info("Wrong cipher at position %d", i);
  131. goto err;
  132. }
  133. }
  134. ret = 1;
  135. err:
  136. sk_SSL_CIPHER_free(ciphers);
  137. SSL_free(ssl);
  138. return ret;
  139. }
  140. static int execute_test(CIPHERLIST_TEST_FIXTURE fixture)
  141. {
  142. return test_default_cipherlist(fixture.server)
  143. && test_default_cipherlist(fixture.client);
  144. }
  145. static void tear_down(CIPHERLIST_TEST_FIXTURE fixture)
  146. {
  147. SSL_CTX_free(fixture.server);
  148. SSL_CTX_free(fixture.client);
  149. }
  150. #define SETUP_CIPHERLIST_TEST_FIXTURE() \
  151. SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
  152. #define EXECUTE_CIPHERLIST_TEST() \
  153. EXECUTE_TEST(execute_test, tear_down)
  154. static int test_default_cipherlist_implicit()
  155. {
  156. SETUP_CIPHERLIST_TEST_FIXTURE();
  157. EXECUTE_CIPHERLIST_TEST();
  158. }
  159. static int test_default_cipherlist_explicit()
  160. {
  161. SETUP_CIPHERLIST_TEST_FIXTURE();
  162. TEST_check(SSL_CTX_set_cipher_list(fixture.server, "DEFAULT"));
  163. TEST_check(SSL_CTX_set_cipher_list(fixture.client, "DEFAULT"));
  164. EXECUTE_CIPHERLIST_TEST();
  165. }
  166. void register_tests()
  167. {
  168. ADD_TEST(test_default_cipherlist_implicit);
  169. ADD_TEST(test_default_cipherlist_explicit);
  170. }