server_secure_chttp2.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/server.h"
  45. #include "src/core/transport/chttp2_transport.h"
  46. #include <grpc/support/alloc.h>
  47. #include <grpc/support/log.h>
  48. #include <grpc/support/sync.h>
  49. #include <grpc/support/useful.h>
  50. typedef struct tcp_endpoint_list {
  51. grpc_endpoint *tcp_endpoint;
  52. struct tcp_endpoint_list *next;
  53. } tcp_endpoint_list;
  54. typedef struct grpc_server_secure_state {
  55. grpc_server *server;
  56. grpc_tcp_server *tcp;
  57. grpc_security_connector *sc;
  58. grpc_server_credentials *creds;
  59. tcp_endpoint_list *handshaking_tcp_endpoints;
  60. int is_shutdown;
  61. gpr_mu mu;
  62. gpr_refcount refcount;
  63. } grpc_server_secure_state;
  64. static void state_ref(grpc_server_secure_state *state) {
  65. gpr_ref(&state->refcount);
  66. }
  67. static void state_unref(grpc_server_secure_state *state) {
  68. if (gpr_unref(&state->refcount)) {
  69. /* ensure all threads have unlocked */
  70. gpr_mu_lock(&state->mu);
  71. gpr_mu_unlock(&state->mu);
  72. /* clean up */
  73. GRPC_SECURITY_CONNECTOR_UNREF(state->sc, "server");
  74. grpc_server_credentials_unref(state->creds);
  75. gpr_free(state);
  76. }
  77. }
  78. static void setup_transport(void *statep, grpc_transport *transport,
  79. grpc_mdctx *mdctx) {
  80. static grpc_channel_filter const *extra_filters[] = {
  81. &grpc_server_auth_filter, &grpc_http_server_filter};
  82. grpc_server_secure_state *state = statep;
  83. grpc_channel_args *args_copy;
  84. grpc_arg args_to_add[2];
  85. args_to_add[0] = grpc_security_connector_to_arg(state->sc);
  86. args_to_add[1] =
  87. grpc_auth_metadata_processor_to_arg(&state->creds->processor);
  88. args_copy = grpc_channel_args_copy_and_add(
  89. grpc_server_get_channel_args(state->server), args_to_add,
  90. GPR_ARRAY_SIZE(args_to_add));
  91. grpc_server_setup_transport(state->server, transport, extra_filters,
  92. GPR_ARRAY_SIZE(extra_filters), mdctx, args_copy);
  93. grpc_channel_args_destroy(args_copy);
  94. }
  95. static int remove_tcp_from_list_locked(grpc_server_secure_state *state,
  96. grpc_endpoint *tcp) {
  97. tcp_endpoint_list *node = state->handshaking_tcp_endpoints;
  98. tcp_endpoint_list *tmp = NULL;
  99. if (node && node->tcp_endpoint == tcp) {
  100. state->handshaking_tcp_endpoints = state->handshaking_tcp_endpoints->next;
  101. gpr_free(node);
  102. return 0;
  103. }
  104. while (node) {
  105. if (node->next->tcp_endpoint == tcp) {
  106. tmp = node->next;
  107. node->next = node->next->next;
  108. gpr_free(tmp);
  109. return 0;
  110. }
  111. node = node->next;
  112. }
  113. return -1;
  114. }
  115. static void on_secure_handshake_done(void *statep, grpc_security_status status,
  116. grpc_endpoint *wrapped_endpoint,
  117. grpc_endpoint *secure_endpoint) {
  118. grpc_server_secure_state *state = statep;
  119. grpc_transport *transport;
  120. grpc_mdctx *mdctx;
  121. if (status == GRPC_SECURITY_OK) {
  122. gpr_mu_lock(&state->mu);
  123. remove_tcp_from_list_locked(state, wrapped_endpoint);
  124. if (!state->is_shutdown) {
  125. mdctx = grpc_mdctx_create();
  126. transport = grpc_create_chttp2_transport(
  127. grpc_server_get_channel_args(state->server), secure_endpoint, mdctx,
  128. 0);
  129. setup_transport(state, transport, mdctx);
  130. grpc_chttp2_transport_start_reading(transport, NULL, 0);
  131. } else {
  132. /* We need to consume this here, because the server may already have gone
  133. * away. */
  134. grpc_endpoint_destroy(secure_endpoint);
  135. }
  136. gpr_mu_unlock(&state->mu);
  137. } else {
  138. gpr_mu_lock(&state->mu);
  139. remove_tcp_from_list_locked(state, wrapped_endpoint);
  140. gpr_mu_unlock(&state->mu);
  141. gpr_log(GPR_ERROR, "Secure transport failed with error %d", status);
  142. }
  143. state_unref(state);
  144. }
  145. static void on_accept(void *statep, grpc_endpoint *tcp) {
  146. grpc_server_secure_state *state = statep;
  147. tcp_endpoint_list *node;
  148. state_ref(state);
  149. node = gpr_malloc(sizeof(tcp_endpoint_list));
  150. node->tcp_endpoint = tcp;
  151. gpr_mu_lock(&state->mu);
  152. node->next = state->handshaking_tcp_endpoints;
  153. state->handshaking_tcp_endpoints = node;
  154. gpr_mu_unlock(&state->mu);
  155. grpc_security_connector_do_handshake(state->sc, tcp, on_secure_handshake_done,
  156. state);
  157. }
  158. /* Server callback: start listening on our ports */
  159. static void start(grpc_server *server, void *statep, grpc_pollset **pollsets,
  160. size_t pollset_count) {
  161. grpc_server_secure_state *state = statep;
  162. grpc_tcp_server_start(state->tcp, pollsets, pollset_count, on_accept, state);
  163. }
  164. static void destroy_done(void *statep) {
  165. grpc_server_secure_state *state = statep;
  166. grpc_server_listener_destroy_done(state->server);
  167. gpr_mu_lock(&state->mu);
  168. while (state->handshaking_tcp_endpoints != NULL) {
  169. grpc_endpoint_shutdown(state->handshaking_tcp_endpoints->tcp_endpoint);
  170. remove_tcp_from_list_locked(state,
  171. state->handshaking_tcp_endpoints->tcp_endpoint);
  172. }
  173. gpr_mu_unlock(&state->mu);
  174. state_unref(state);
  175. }
  176. /* Server callback: destroy the tcp listener (so we don't generate further
  177. callbacks) */
  178. static void destroy(grpc_server *server, void *statep) {
  179. grpc_server_secure_state *state = statep;
  180. grpc_tcp_server *tcp;
  181. gpr_mu_lock(&state->mu);
  182. state->is_shutdown = 1;
  183. tcp = state->tcp;
  184. gpr_mu_unlock(&state->mu);
  185. grpc_tcp_server_destroy(tcp, destroy_done, state);
  186. }
  187. int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
  188. grpc_server_credentials *creds) {
  189. grpc_resolved_addresses *resolved = NULL;
  190. grpc_tcp_server *tcp = NULL;
  191. grpc_server_secure_state *state = NULL;
  192. size_t i;
  193. unsigned count = 0;
  194. int port_num = -1;
  195. int port_temp;
  196. grpc_security_status status = GRPC_SECURITY_ERROR;
  197. grpc_security_connector *sc = NULL;
  198. /* create security context */
  199. if (creds == NULL) goto error;
  200. status = grpc_server_credentials_create_security_connector(creds, &sc);
  201. if (status != GRPC_SECURITY_OK) {
  202. gpr_log(GPR_ERROR,
  203. "Unable to create secure server with credentials of type %s.",
  204. creds->type);
  205. goto error;
  206. }
  207. /* resolve address */
  208. resolved = grpc_blocking_resolve_address(addr, "https");
  209. if (!resolved) {
  210. goto error;
  211. }
  212. tcp = grpc_tcp_server_create();
  213. if (!tcp) {
  214. goto error;
  215. }
  216. for (i = 0; i < resolved->naddrs; i++) {
  217. port_temp = grpc_tcp_server_add_port(
  218. tcp, (struct sockaddr *)&resolved->addrs[i].addr,
  219. resolved->addrs[i].len);
  220. if (port_temp >= 0) {
  221. if (port_num == -1) {
  222. port_num = port_temp;
  223. } else {
  224. GPR_ASSERT(port_num == port_temp);
  225. }
  226. count++;
  227. }
  228. }
  229. if (count == 0) {
  230. gpr_log(GPR_ERROR, "No address added out of total %d resolved",
  231. resolved->naddrs);
  232. goto error;
  233. }
  234. if (count != resolved->naddrs) {
  235. gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
  236. count, resolved->naddrs);
  237. /* if it's an error, don't we want to goto error; here ? */
  238. }
  239. grpc_resolved_addresses_destroy(resolved);
  240. state = gpr_malloc(sizeof(*state));
  241. memset(state, 0, sizeof(*state));
  242. state->server = server;
  243. state->tcp = tcp;
  244. state->sc = sc;
  245. state->creds = grpc_server_credentials_ref(creds);
  246. state->handshaking_tcp_endpoints = NULL;
  247. state->is_shutdown = 0;
  248. gpr_mu_init(&state->mu);
  249. gpr_ref_init(&state->refcount, 1);
  250. /* Register with the server only upon success */
  251. grpc_server_add_listener(server, state, start, destroy);
  252. return port_num;
  253. /* Error path: cleanup and return */
  254. error:
  255. if (sc) {
  256. GRPC_SECURITY_CONNECTOR_UNREF(sc, "server");
  257. }
  258. if (resolved) {
  259. grpc_resolved_addresses_destroy(resolved);
  260. }
  261. if (tcp) {
  262. grpc_tcp_server_destroy(tcp, NULL, NULL);
  263. }
  264. if (state) {
  265. gpr_free(state);
  266. }
  267. return 0;
  268. }