h2_ssl.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/channel/channel_args.h"
  23. #include "src/core/lib/gpr/string.h"
  24. #include "src/core/lib/gpr/tmpfile.h"
  25. #include "src/core/lib/gprpp/host_port.h"
  26. #include "src/core/lib/iomgr/load_file.h"
  27. #include "src/core/lib/security/credentials/credentials.h"
  28. #include "src/core/lib/security/credentials/ssl/ssl_credentials.h"
  29. #include "src/core/lib/security/security_connector/ssl_utils_config.h"
  30. #include "test/core/end2end/end2end_tests.h"
  31. #include "test/core/util/port.h"
  32. #include "test/core/util/test_config.h"
  33. #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
  34. #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
  35. #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
  36. struct fullstack_secure_fixture_data {
  37. std::string localaddr;
  38. grpc_tls_version tls_version;
  39. };
  40. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
  41. const grpc_channel_args* /*client_args*/,
  42. const grpc_channel_args* /*server_args*/, grpc_tls_version tls_version) {
  43. grpc_end2end_test_fixture f;
  44. int port = grpc_pick_unused_port_or_die();
  45. fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data();
  46. memset(&f, 0, sizeof(f));
  47. ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
  48. ffd->tls_version = tls_version;
  49. f.fixture_data = ffd;
  50. f.cq = grpc_completion_queue_create_for_next(nullptr);
  51. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  52. return f;
  53. }
  54. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack_tls1_2(
  55. const grpc_channel_args* client_args,
  56. const grpc_channel_args* server_args) {
  57. return chttp2_create_fixture_secure_fullstack(client_args, server_args,
  58. grpc_tls_version::TLS1_2);
  59. }
  60. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack_tls1_3(
  61. const grpc_channel_args* client_args,
  62. const grpc_channel_args* server_args) {
  63. return chttp2_create_fixture_secure_fullstack(client_args, server_args,
  64. grpc_tls_version::TLS1_3);
  65. }
  66. static void process_auth_failure(void* state, grpc_auth_context* /*ctx*/,
  67. const grpc_metadata* /*md*/,
  68. size_t /*md_count*/,
  69. grpc_process_auth_metadata_done_cb cb,
  70. void* user_data) {
  71. GPR_ASSERT(state == nullptr);
  72. cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
  73. }
  74. static void chttp2_init_client_secure_fullstack(
  75. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args,
  76. grpc_channel_credentials* creds) {
  77. fullstack_secure_fixture_data* ffd =
  78. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  79. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
  80. GPR_ASSERT(f->client != nullptr);
  81. grpc_channel_credentials_release(creds);
  82. }
  83. static void chttp2_init_server_secure_fullstack(
  84. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args,
  85. grpc_server_credentials* server_creds) {
  86. fullstack_secure_fixture_data* ffd =
  87. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  88. if (f->server) {
  89. grpc_server_destroy(f->server);
  90. }
  91. f->server = grpc_server_create(server_args, nullptr);
  92. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  93. GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
  94. server_creds));
  95. grpc_server_credentials_release(server_creds);
  96. grpc_server_start(f->server);
  97. }
  98. void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
  99. fullstack_secure_fixture_data* ffd =
  100. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  101. delete ffd;
  102. }
  103. static void chttp2_init_client_simple_ssl_secure_fullstack(
  104. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
  105. grpc_channel_credentials* ssl_creds =
  106. grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr);
  107. if (f != nullptr && ssl_creds != nullptr) {
  108. // Set the min and max TLS version.
  109. grpc_ssl_credentials* creds =
  110. reinterpret_cast<grpc_ssl_credentials*>(ssl_creds);
  111. fullstack_secure_fixture_data* ffd =
  112. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  113. creds->set_min_tls_version(ffd->tls_version);
  114. creds->set_max_tls_version(ffd->tls_version);
  115. }
  116. grpc_arg ssl_name_override = {
  117. GRPC_ARG_STRING,
  118. const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
  119. {const_cast<char*>("foo.test.google.fr")}};
  120. const grpc_channel_args* new_client_args =
  121. grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
  122. chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds);
  123. grpc_channel_args_destroy(new_client_args);
  124. }
  125. static int fail_server_auth_check(const grpc_channel_args* server_args) {
  126. size_t i;
  127. if (server_args == nullptr) return 0;
  128. for (i = 0; i < server_args->num_args; i++) {
  129. if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
  130. 0) {
  131. return 1;
  132. }
  133. }
  134. return 0;
  135. }
  136. static void chttp2_init_server_simple_ssl_secure_fullstack(
  137. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
  138. grpc_slice cert_slice, key_slice;
  139. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  140. "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
  141. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  142. grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
  143. const char* server_cert =
  144. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  145. const char* server_key =
  146. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
  147. grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
  148. grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
  149. nullptr, &pem_key_cert_pair, 1, 0, nullptr);
  150. if (f != nullptr && ssl_creds != nullptr) {
  151. // Set the min and max TLS version.
  152. grpc_ssl_server_credentials* creds =
  153. reinterpret_cast<grpc_ssl_server_credentials*>(ssl_creds);
  154. fullstack_secure_fixture_data* ffd =
  155. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  156. creds->set_min_tls_version(ffd->tls_version);
  157. creds->set_max_tls_version(ffd->tls_version);
  158. }
  159. grpc_slice_unref(cert_slice);
  160. grpc_slice_unref(key_slice);
  161. if (fail_server_auth_check(server_args)) {
  162. grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
  163. nullptr};
  164. grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
  165. }
  166. chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
  167. }
  168. /* All test configurations */
  169. static grpc_end2end_test_config configs[] = {
  170. {"chttp2/simple_ssl_fullstack_tls1_2",
  171. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  172. FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
  173. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  174. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
  175. "foo.test.google.fr", chttp2_create_fixture_secure_fullstack_tls1_2,
  176. chttp2_init_client_simple_ssl_secure_fullstack,
  177. chttp2_init_server_simple_ssl_secure_fullstack,
  178. chttp2_tear_down_secure_fullstack},
  179. {"chttp2/simple_ssl_fullstack_tls1_3",
  180. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  181. FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
  182. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  183. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER |
  184. FEATURE_MASK_DOES_NOT_SUPPORT_CLIENT_HANDSHAKE_COMPLETE_FIRST,
  185. "foo.test.google.fr", chttp2_create_fixture_secure_fullstack_tls1_3,
  186. chttp2_init_client_simple_ssl_secure_fullstack,
  187. chttp2_init_server_simple_ssl_secure_fullstack,
  188. chttp2_tear_down_secure_fullstack},
  189. };
  190. int main(int argc, char** argv) {
  191. size_t i;
  192. grpc::testing::TestEnvironment env(argc, argv);
  193. grpc_end2end_tests_pre_init();
  194. GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, CA_CERT_PATH);
  195. grpc_init();
  196. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  197. grpc_end2end_tests(argc, argv, configs[i]);
  198. }
  199. grpc_shutdown();
  200. return 0;
  201. }