verify_peer_options.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. *
  3. * Copyright 2018 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 "src/core/lib/iomgr/port.h"
  19. // This test won't work except with posix sockets enabled
  20. #ifdef GRPC_POSIX_SOCKET_TCP
  21. #include <arpa/inet.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/socket.h>
  25. #include <unistd.h>
  26. #include <string>
  27. #include <openssl/err.h>
  28. #include <openssl/ssl.h>
  29. #include "absl/strings/str_cat.h"
  30. #include <grpc/grpc.h>
  31. #include <grpc/grpc_security.h>
  32. #include <grpc/support/alloc.h>
  33. #include <grpc/support/log.h>
  34. #include "src/core/lib/gprpp/thd.h"
  35. #include "src/core/lib/iomgr/load_file.h"
  36. #include "test/core/util/port.h"
  37. #include "test/core/util/test_config.h"
  38. #define SSL_CERT_PATH "src/core/tsi/test_creds/server1.pem"
  39. #define SSL_KEY_PATH "src/core/tsi/test_creds/server1.key"
  40. #define SSL_CA_PATH "src/core/tsi/test_creds/ca.pem"
  41. // Simple gRPC server. This listens until client_handshake_complete occurs.
  42. static gpr_event client_handshake_complete;
  43. static void server_thread(void* arg) {
  44. const int port = *static_cast<int*>(arg);
  45. // Load key pair and establish server SSL credentials.
  46. grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
  47. grpc_slice ca_slice, cert_slice, key_slice;
  48. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  49. grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
  50. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  51. grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
  52. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  53. grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
  54. const char* ca_cert =
  55. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
  56. pem_key_cert_pair.private_key =
  57. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
  58. pem_key_cert_pair.cert_chain =
  59. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  60. grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
  61. ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
  62. // Start server listening on local port.
  63. std::string addr = absl::StrCat("127.0.0.1:", port);
  64. grpc_server* server = grpc_server_create(nullptr, nullptr);
  65. GPR_ASSERT(grpc_server_add_http2_port(server, addr.c_str(), ssl_creds));
  66. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  67. grpc_server_register_completion_queue(server, cq, nullptr);
  68. grpc_server_start(server);
  69. // Wait a bounded number of time until client_handshake_complete is set,
  70. // sleeping between polls. The total time spent (deadline * retries)
  71. // should be strictly greater than the client retry limit so that the
  72. // client will always timeout first.
  73. int retries = 60;
  74. while (!gpr_event_get(&client_handshake_complete) && retries-- > 0) {
  75. const gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(1);
  76. grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
  77. GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
  78. }
  79. gpr_log(GPR_INFO, "Shutting down server");
  80. grpc_server_shutdown_and_notify(server, cq, nullptr);
  81. grpc_server_cancel_all_calls(server);
  82. grpc_completion_queue_shutdown(cq);
  83. const gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(60);
  84. grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
  85. GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
  86. grpc_server_destroy(server);
  87. grpc_completion_queue_destroy(cq);
  88. grpc_server_credentials_release(ssl_creds);
  89. grpc_slice_unref(cert_slice);
  90. grpc_slice_unref(key_slice);
  91. grpc_slice_unref(ca_slice);
  92. }
  93. // This test launches a minimal TLS grpc server on a separate thread and then
  94. // establishes a TLS handshake via the core library to the server. The client
  95. // uses the supplied verify options.
  96. static bool verify_peer_options_test(verify_peer_options* verify_options) {
  97. bool success = true;
  98. grpc_init();
  99. int port = grpc_pick_unused_port_or_die();
  100. gpr_event_init(&client_handshake_complete);
  101. // Load key pair and establish client SSL credentials.
  102. // NOTE: we intentionally load the credential files before starting
  103. // the server thread because grpc_load_file can experience trouble
  104. // when two threads attempt to load the same file concurrently
  105. // and server thread also reads the same files as soon as it starts.
  106. // See https://github.com/grpc/grpc/issues/23503 for details.
  107. grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
  108. grpc_slice ca_slice, cert_slice, key_slice;
  109. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  110. grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
  111. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  112. grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
  113. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  114. grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
  115. const char* ca_cert =
  116. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
  117. pem_key_cert_pair.private_key =
  118. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
  119. pem_key_cert_pair.cert_chain =
  120. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  121. grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
  122. ca_cert, &pem_key_cert_pair, verify_options, nullptr);
  123. // Launch the gRPC server thread.
  124. bool ok;
  125. grpc_core::Thread thd("grpc_client_ssl_test", server_thread, &port, &ok);
  126. GPR_ASSERT(ok);
  127. thd.Start();
  128. // Establish a channel pointing at the TLS server. Since the gRPC runtime is
  129. // lazy, this won't necessarily establish a connection yet.
  130. std::string target = absl::StrCat("127.0.0.1:", port);
  131. grpc_arg ssl_name_override = {
  132. GRPC_ARG_STRING,
  133. const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
  134. {const_cast<char*>("foo.test.google.fr")}};
  135. grpc_channel_args grpc_args;
  136. grpc_args.num_args = 1;
  137. grpc_args.args = &ssl_name_override;
  138. grpc_channel* channel =
  139. grpc_channel_create(target.c_str(), ssl_creds, &grpc_args);
  140. GPR_ASSERT(channel);
  141. // Initially the channel will be idle, the
  142. // grpc_channel_check_connectivity_state triggers an attempt to connect.
  143. GPR_ASSERT(grpc_channel_check_connectivity_state(
  144. channel, 1 /* try_to_connect */) == GRPC_CHANNEL_IDLE);
  145. // Wait a bounded number of times for the channel to be ready. When the
  146. // channel is ready, the initial TLS handshake will have successfully
  147. // completed. The total time spent on the client side (retries * deadline)
  148. // should be greater than the server side time limit.
  149. int retries = 10;
  150. grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
  151. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  152. while (state != GRPC_CHANNEL_READY && retries-- > 0) {
  153. grpc_channel_watch_connectivity_state(
  154. channel, state, grpc_timeout_seconds_to_deadline(3), cq, nullptr);
  155. gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(5);
  156. grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
  157. GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
  158. state =
  159. grpc_channel_check_connectivity_state(channel, 0 /* try_to_connect */);
  160. }
  161. grpc_completion_queue_destroy(cq);
  162. if (retries < 0) {
  163. success = false;
  164. }
  165. grpc_channel_destroy(channel);
  166. grpc_channel_credentials_release(ssl_creds);
  167. grpc_slice_unref(cert_slice);
  168. grpc_slice_unref(key_slice);
  169. grpc_slice_unref(ca_slice);
  170. // Now that the client is completely cleaned up, trigger the server to
  171. // shutdown
  172. gpr_event_set(&client_handshake_complete, &client_handshake_complete);
  173. // Wait for the server to completely shutdown
  174. thd.Join();
  175. grpc_shutdown();
  176. return success;
  177. }
  178. static int callback_return_value = 0;
  179. static char callback_target_host[4096];
  180. static char callback_target_pem[4096];
  181. static void* callback_userdata = nullptr;
  182. static void* destruct_userdata = nullptr;
  183. static int verify_callback(const char* target_host, const char* target_pem,
  184. void* userdata) {
  185. if (target_host != nullptr) {
  186. snprintf(callback_target_host, sizeof(callback_target_host), "%s",
  187. target_host);
  188. } else {
  189. callback_target_host[0] = '\0';
  190. }
  191. if (target_pem != nullptr) {
  192. snprintf(callback_target_pem, sizeof(callback_target_pem), "%s",
  193. target_pem);
  194. } else {
  195. callback_target_pem[0] = '\0';
  196. }
  197. callback_userdata = userdata;
  198. return callback_return_value;
  199. }
  200. static void verify_destruct(void* userdata) { destruct_userdata = userdata; }
  201. int main(int argc, char* argv[]) {
  202. grpc::testing::TestEnvironment env(argc, argv);
  203. grpc_init();
  204. int userdata = 42;
  205. verify_peer_options verify_options;
  206. // Load the server's cert so that we can assert it gets passed to the callback
  207. grpc_slice cert_slice;
  208. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  209. grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
  210. const char* server_cert =
  211. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  212. // Running with all-null values should have no effect
  213. verify_options.verify_peer_callback = nullptr;
  214. verify_options.verify_peer_callback_userdata = nullptr;
  215. verify_options.verify_peer_destruct = nullptr;
  216. GPR_ASSERT(verify_peer_options_test(&verify_options));
  217. GPR_ASSERT(strlen(callback_target_host) == 0);
  218. GPR_ASSERT(strlen(callback_target_pem) == 0);
  219. GPR_ASSERT(callback_userdata == nullptr);
  220. GPR_ASSERT(destruct_userdata == nullptr);
  221. // Running with the callbacks and verify we get the expected values
  222. verify_options.verify_peer_callback = verify_callback;
  223. verify_options.verify_peer_callback_userdata = static_cast<void*>(&userdata);
  224. verify_options.verify_peer_destruct = verify_destruct;
  225. GPR_ASSERT(verify_peer_options_test(&verify_options));
  226. GPR_ASSERT(strcmp(callback_target_host, "foo.test.google.fr") == 0);
  227. GPR_ASSERT(strcmp(callback_target_pem, server_cert) == 0);
  228. GPR_ASSERT(callback_userdata == static_cast<void*>(&userdata));
  229. GPR_ASSERT(destruct_userdata == static_cast<void*>(&userdata));
  230. // If the callback returns non-zero, initializing the channel should fail.
  231. callback_return_value = 1;
  232. GPR_ASSERT(!verify_peer_options_test(&verify_options));
  233. grpc_slice_unref(cert_slice);
  234. grpc_shutdown();
  235. return 0;
  236. }
  237. #else /* GRPC_POSIX_SOCKET_TCP */
  238. int main(int argc, char** argv) { return 1; }
  239. #endif /* GRPC_POSIX_SOCKET_TCP */