server_secure_chttp2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/grpc.h>
  34. #include <string.h>
  35. #include "src/core/channel/channel_args.h"
  36. #include "src/core/channel/http_server_filter.h"
  37. #include "src/core/iomgr/endpoint.h"
  38. #include "src/core/iomgr/resolve_address.h"
  39. #include "src/core/iomgr/tcp_server.h"
  40. #include "src/core/security/auth_filters.h"
  41. #include "src/core/security/credentials.h"
  42. #include "src/core/security/security_connector.h"
  43. #include "src/core/security/security_context.h"
  44. #include "src/core/surface/api_trace.h"
  45. #include "src/core/surface/server.h"
  46. #include "src/core/transport/chttp2_transport.h"
  47. #include <grpc/support/alloc.h>
  48. #include <grpc/support/log.h>
  49. #include <grpc/support/sync.h>
  50. #include <grpc/support/useful.h>
  51. typedef struct grpc_server_secure_state {
  52. grpc_server *server;
  53. grpc_tcp_server *tcp;
  54. grpc_security_connector *sc;
  55. grpc_server_credentials *creds;
  56. int is_shutdown;
  57. gpr_mu mu;
  58. gpr_refcount refcount;
  59. grpc_closure destroy_closure;
  60. grpc_closure *destroy_callback;
  61. } grpc_server_secure_state;
  62. static void state_ref(grpc_server_secure_state *state) {
  63. gpr_ref(&state->refcount);
  64. }
  65. static void state_unref(grpc_server_secure_state *state) {
  66. if (gpr_unref(&state->refcount)) {
  67. /* ensure all threads have unlocked */
  68. gpr_mu_lock(&state->mu);
  69. gpr_mu_unlock(&state->mu);
  70. /* clean up */
  71. GRPC_SECURITY_CONNECTOR_UNREF(state->sc, "server");
  72. grpc_server_credentials_unref(state->creds);
  73. gpr_free(state);
  74. }
  75. }
  76. static void setup_transport(grpc_exec_ctx *exec_ctx, void *statep,
  77. grpc_transport *transport) {
  78. static grpc_channel_filter const *extra_filters[] = {
  79. &grpc_server_auth_filter, &grpc_http_server_filter};
  80. grpc_server_secure_state *state = statep;
  81. grpc_channel_args *args_copy;
  82. grpc_arg args_to_add[2];
  83. args_to_add[0] = grpc_server_credentials_to_arg(state->creds);
  84. args_to_add[1] = grpc_auth_context_to_arg(state->sc->auth_context);
  85. args_copy = grpc_channel_args_copy_and_add(
  86. grpc_server_get_channel_args(state->server), args_to_add,
  87. GPR_ARRAY_SIZE(args_to_add));
  88. grpc_server_setup_transport(exec_ctx, state->server, transport, extra_filters,
  89. GPR_ARRAY_SIZE(extra_filters), args_copy);
  90. grpc_channel_args_destroy(args_copy);
  91. }
  92. static void on_secure_handshake_done(grpc_exec_ctx *exec_ctx, void *statep,
  93. grpc_security_status status,
  94. grpc_endpoint *secure_endpoint) {
  95. grpc_server_secure_state *state = statep;
  96. grpc_transport *transport;
  97. if (status == GRPC_SECURITY_OK) {
  98. if (secure_endpoint) {
  99. gpr_mu_lock(&state->mu);
  100. if (!state->is_shutdown) {
  101. transport = grpc_create_chttp2_transport(
  102. exec_ctx, grpc_server_get_channel_args(state->server),
  103. secure_endpoint, 0);
  104. setup_transport(exec_ctx, state, transport);
  105. grpc_chttp2_transport_start_reading(exec_ctx, transport, NULL, 0);
  106. } else {
  107. /* We need to consume this here, because the server may already have
  108. * gone away. */
  109. grpc_endpoint_destroy(exec_ctx, secure_endpoint);
  110. }
  111. gpr_mu_unlock(&state->mu);
  112. }
  113. } else {
  114. gpr_log(GPR_ERROR, "Secure transport failed with error %d", status);
  115. }
  116. state_unref(state);
  117. }
  118. static void on_accept(grpc_exec_ctx *exec_ctx, void *statep,
  119. grpc_endpoint *tcp) {
  120. grpc_server_secure_state *state = statep;
  121. state_ref(state);
  122. grpc_security_connector_do_handshake(exec_ctx, state->sc, tcp,
  123. on_secure_handshake_done, state);
  124. }
  125. /* Server callback: start listening on our ports */
  126. static void start(grpc_exec_ctx *exec_ctx, grpc_server *server, void *statep,
  127. grpc_pollset **pollsets, size_t pollset_count) {
  128. grpc_server_secure_state *state = statep;
  129. grpc_tcp_server_start(exec_ctx, state->tcp, pollsets, pollset_count,
  130. on_accept, state);
  131. }
  132. static void destroy_done(grpc_exec_ctx *exec_ctx, void *statep, int success) {
  133. grpc_server_secure_state *state = statep;
  134. state->destroy_callback->cb(exec_ctx, state->destroy_callback->cb_arg,
  135. success);
  136. grpc_security_connector_shutdown(exec_ctx, state->sc);
  137. state_unref(state);
  138. }
  139. /* Server callback: destroy the tcp listener (so we don't generate further
  140. callbacks) */
  141. static void destroy(grpc_exec_ctx *exec_ctx, grpc_server *server, void *statep,
  142. grpc_closure *callback) {
  143. grpc_server_secure_state *state = statep;
  144. grpc_tcp_server *tcp;
  145. gpr_mu_lock(&state->mu);
  146. state->is_shutdown = 1;
  147. state->destroy_callback = callback;
  148. tcp = state->tcp;
  149. gpr_mu_unlock(&state->mu);
  150. grpc_closure_init(&state->destroy_closure, destroy_done, state);
  151. grpc_tcp_server_destroy(exec_ctx, tcp, &state->destroy_closure);
  152. }
  153. int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
  154. grpc_server_credentials *creds) {
  155. grpc_resolved_addresses *resolved = NULL;
  156. grpc_tcp_server *tcp = NULL;
  157. grpc_server_secure_state *state = NULL;
  158. size_t i;
  159. unsigned count = 0;
  160. int port_num = -1;
  161. int port_temp;
  162. grpc_security_status status = GRPC_SECURITY_ERROR;
  163. grpc_security_connector *sc = NULL;
  164. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  165. GRPC_API_TRACE(
  166. "grpc_server_add_secure_http2_port("
  167. "server=%p, addr=%s, creds=%p)",
  168. 3, (server, addr, creds));
  169. /* create security context */
  170. if (creds == NULL) goto error;
  171. status = grpc_server_credentials_create_security_connector(creds, &sc);
  172. if (status != GRPC_SECURITY_OK) {
  173. gpr_log(GPR_ERROR,
  174. "Unable to create secure server with credentials of type %s.",
  175. creds->type);
  176. goto error;
  177. }
  178. sc->channel_args = grpc_server_get_channel_args(server);
  179. /* resolve address */
  180. resolved = grpc_blocking_resolve_address(addr, "https");
  181. if (!resolved) {
  182. goto error;
  183. }
  184. tcp = grpc_tcp_server_create();
  185. if (!tcp) {
  186. goto error;
  187. }
  188. for (i = 0; i < resolved->naddrs; i++) {
  189. grpc_tcp_listener *listener;
  190. listener = grpc_tcp_server_add_port(
  191. tcp, (struct sockaddr *)&resolved->addrs[i].addr,
  192. resolved->addrs[i].len);
  193. port_temp = grpc_tcp_listener_get_port(listener);
  194. if (port_temp > 0) {
  195. if (port_num == -1) {
  196. port_num = port_temp;
  197. } else {
  198. GPR_ASSERT(port_num == port_temp);
  199. }
  200. count++;
  201. }
  202. }
  203. if (count == 0) {
  204. gpr_log(GPR_ERROR, "No address added out of total %d resolved",
  205. resolved->naddrs);
  206. goto error;
  207. }
  208. if (count != resolved->naddrs) {
  209. gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
  210. count, resolved->naddrs);
  211. /* if it's an error, don't we want to goto error; here ? */
  212. }
  213. grpc_resolved_addresses_destroy(resolved);
  214. state = gpr_malloc(sizeof(*state));
  215. memset(state, 0, sizeof(*state));
  216. state->server = server;
  217. state->tcp = tcp;
  218. state->sc = sc;
  219. state->creds = grpc_server_credentials_ref(creds);
  220. state->is_shutdown = 0;
  221. gpr_mu_init(&state->mu);
  222. gpr_ref_init(&state->refcount, 1);
  223. /* Register with the server only upon success */
  224. grpc_server_add_listener(&exec_ctx, server, state, start, destroy);
  225. grpc_exec_ctx_finish(&exec_ctx);
  226. return port_num;
  227. /* Error path: cleanup and return */
  228. error:
  229. if (sc) {
  230. GRPC_SECURITY_CONNECTOR_UNREF(sc, "server");
  231. }
  232. if (resolved) {
  233. grpc_resolved_addresses_destroy(resolved);
  234. }
  235. if (tcp) {
  236. grpc_tcp_server_destroy(&exec_ctx, tcp, NULL);
  237. }
  238. if (state) {
  239. gpr_free(state);
  240. }
  241. grpc_exec_ctx_finish(&exec_ctx);
  242. return 0;
  243. }