handshake.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "src/core/security/handshake.h"
  34. #include <string.h>
  35. #include "src/core/security/secure_endpoint.h"
  36. #include <grpc/support/alloc.h>
  37. #include <grpc/support/log.h>
  38. #include <grpc/support/slice_buffer.h>
  39. #define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256
  40. typedef struct {
  41. grpc_security_connector *connector;
  42. tsi_handshaker *handshaker;
  43. unsigned char *handshake_buffer;
  44. size_t handshake_buffer_size;
  45. grpc_endpoint *wrapped_endpoint;
  46. grpc_endpoint *secure_endpoint;
  47. gpr_slice_buffer left_overs;
  48. gpr_slice_buffer incoming;
  49. gpr_slice_buffer outgoing;
  50. grpc_security_handshake_done_cb cb;
  51. void *user_data;
  52. grpc_iomgr_closure on_handshake_data_sent_to_peer;
  53. grpc_iomgr_closure on_handshake_data_received_from_peer;
  54. } grpc_security_handshake;
  55. static void on_handshake_data_received_from_peer(void *setup, int success);
  56. static void on_handshake_data_sent_to_peer(void *setup, int success);
  57. static void security_handshake_done(grpc_security_handshake *h,
  58. int is_success) {
  59. if (is_success) {
  60. h->cb(h->user_data, GRPC_SECURITY_OK, h->wrapped_endpoint,
  61. h->secure_endpoint);
  62. } else {
  63. if (h->secure_endpoint != NULL) {
  64. grpc_endpoint_shutdown(h->secure_endpoint);
  65. grpc_endpoint_destroy(h->secure_endpoint);
  66. } else {
  67. grpc_endpoint_destroy(h->wrapped_endpoint);
  68. }
  69. h->cb(h->user_data, GRPC_SECURITY_ERROR, h->wrapped_endpoint, NULL);
  70. }
  71. if (h->handshaker != NULL) tsi_handshaker_destroy(h->handshaker);
  72. if (h->handshake_buffer != NULL) gpr_free(h->handshake_buffer);
  73. gpr_slice_buffer_destroy(&h->left_overs);
  74. gpr_slice_buffer_destroy(&h->outgoing);
  75. gpr_slice_buffer_destroy(&h->incoming);
  76. GRPC_SECURITY_CONNECTOR_UNREF(h->connector, "handshake");
  77. gpr_free(h);
  78. }
  79. static void on_peer_checked(void *user_data, grpc_security_status status) {
  80. grpc_security_handshake *h = user_data;
  81. tsi_frame_protector *protector;
  82. tsi_result result;
  83. if (status != GRPC_SECURITY_OK) {
  84. gpr_log(GPR_ERROR, "Error checking peer.");
  85. security_handshake_done(h, 0);
  86. return;
  87. }
  88. result =
  89. tsi_handshaker_create_frame_protector(h->handshaker, NULL, &protector);
  90. if (result != TSI_OK) {
  91. gpr_log(GPR_ERROR, "Frame protector creation failed with error %s.",
  92. tsi_result_to_string(result));
  93. security_handshake_done(h, 0);
  94. return;
  95. }
  96. h->secure_endpoint =
  97. grpc_secure_endpoint_create(protector, h->wrapped_endpoint,
  98. h->left_overs.slices, h->left_overs.count);
  99. h->left_overs.count = 0;
  100. h->left_overs.length = 0;
  101. security_handshake_done(h, 1);
  102. return;
  103. }
  104. static void check_peer(grpc_security_handshake *h) {
  105. grpc_security_status peer_status;
  106. tsi_peer peer;
  107. tsi_result result = tsi_handshaker_extract_peer(h->handshaker, &peer);
  108. if (result != TSI_OK) {
  109. gpr_log(GPR_ERROR, "Peer extraction failed with error %s",
  110. tsi_result_to_string(result));
  111. security_handshake_done(h, 0);
  112. return;
  113. }
  114. peer_status = grpc_security_connector_check_peer(h->connector, peer,
  115. on_peer_checked, h);
  116. if (peer_status == GRPC_SECURITY_ERROR) {
  117. gpr_log(GPR_ERROR, "Peer check failed.");
  118. security_handshake_done(h, 0);
  119. return;
  120. } else if (peer_status == GRPC_SECURITY_OK) {
  121. on_peer_checked(h, peer_status);
  122. }
  123. }
  124. static void send_handshake_bytes_to_peer(grpc_security_handshake *h) {
  125. size_t offset = 0;
  126. tsi_result result = TSI_OK;
  127. gpr_slice to_send;
  128. do {
  129. size_t to_send_size = h->handshake_buffer_size - offset;
  130. result = tsi_handshaker_get_bytes_to_send_to_peer(
  131. h->handshaker, h->handshake_buffer + offset, &to_send_size);
  132. offset += to_send_size;
  133. if (result == TSI_INCOMPLETE_DATA) {
  134. h->handshake_buffer_size *= 2;
  135. h->handshake_buffer =
  136. gpr_realloc(h->handshake_buffer, h->handshake_buffer_size);
  137. }
  138. } while (result == TSI_INCOMPLETE_DATA);
  139. if (result != TSI_OK) {
  140. gpr_log(GPR_ERROR, "Handshake failed with error %s",
  141. tsi_result_to_string(result));
  142. security_handshake_done(h, 0);
  143. return;
  144. }
  145. to_send =
  146. gpr_slice_from_copied_buffer((const char *)h->handshake_buffer, offset);
  147. gpr_slice_buffer_reset_and_unref(&h->outgoing);
  148. gpr_slice_buffer_add(&h->outgoing, to_send);
  149. /* TODO(klempner,jboeuf): This should probably use the client setup
  150. deadline */
  151. switch (grpc_endpoint_write(h->wrapped_endpoint, &h->outgoing,
  152. &h->on_handshake_data_sent_to_peer)) {
  153. case GRPC_ENDPOINT_ERROR:
  154. gpr_log(GPR_ERROR, "Could not send handshake data to peer.");
  155. security_handshake_done(h, 0);
  156. break;
  157. case GRPC_ENDPOINT_DONE:
  158. on_handshake_data_sent_to_peer(h, 1);
  159. break;
  160. case GRPC_ENDPOINT_PENDING:
  161. break;
  162. }
  163. }
  164. static void on_handshake_data_received_from_peer(void *handshake, int success) {
  165. grpc_security_handshake *h = handshake;
  166. size_t consumed_slice_size = 0;
  167. tsi_result result = TSI_OK;
  168. size_t i;
  169. size_t num_left_overs;
  170. int has_left_overs_in_current_slice = 0;
  171. if (!success) {
  172. gpr_log(GPR_ERROR, "Read failed.");
  173. security_handshake_done(h, 0);
  174. return;
  175. }
  176. for (i = 0; i < h->incoming.count; i++) {
  177. consumed_slice_size = GPR_SLICE_LENGTH(h->incoming.slices[i]);
  178. result = tsi_handshaker_process_bytes_from_peer(
  179. h->handshaker, GPR_SLICE_START_PTR(h->incoming.slices[i]),
  180. &consumed_slice_size);
  181. if (!tsi_handshaker_is_in_progress(h->handshaker)) break;
  182. }
  183. if (tsi_handshaker_is_in_progress(h->handshaker)) {
  184. /* We may need more data. */
  185. if (result == TSI_INCOMPLETE_DATA) {
  186. switch (grpc_endpoint_read(h->wrapped_endpoint, &h->incoming,
  187. &h->on_handshake_data_received_from_peer)) {
  188. case GRPC_ENDPOINT_DONE:
  189. on_handshake_data_received_from_peer(h, 1);
  190. break;
  191. case GRPC_ENDPOINT_ERROR:
  192. on_handshake_data_received_from_peer(h, 0);
  193. break;
  194. case GRPC_ENDPOINT_PENDING:
  195. break;
  196. }
  197. return;
  198. } else {
  199. send_handshake_bytes_to_peer(h);
  200. return;
  201. }
  202. }
  203. if (result != TSI_OK) {
  204. gpr_log(GPR_ERROR, "Handshake failed with error %s",
  205. tsi_result_to_string(result));
  206. security_handshake_done(h, 0);
  207. return;
  208. }
  209. /* Handshake is done and successful this point. */
  210. has_left_overs_in_current_slice =
  211. (consumed_slice_size < GPR_SLICE_LENGTH(h->incoming.slices[i]));
  212. num_left_overs =
  213. (has_left_overs_in_current_slice ? 1 : 0) + h->incoming.count - i - 1;
  214. if (num_left_overs == 0) {
  215. check_peer(h);
  216. return;
  217. }
  218. /* Put the leftovers in our buffer (ownership transfered). */
  219. if (has_left_overs_in_current_slice) {
  220. gpr_slice_buffer_add(
  221. &h->left_overs,
  222. gpr_slice_split_tail(&h->incoming.slices[i], consumed_slice_size));
  223. gpr_slice_unref(
  224. h->incoming.slices[i]); /* split_tail above increments refcount. */
  225. }
  226. gpr_slice_buffer_addn(
  227. &h->left_overs, &h->incoming.slices[i + 1],
  228. num_left_overs - (size_t)has_left_overs_in_current_slice);
  229. check_peer(h);
  230. }
  231. /* If handshake is NULL, the handshake is done. */
  232. static void on_handshake_data_sent_to_peer(void *handshake, int success) {
  233. grpc_security_handshake *h = handshake;
  234. /* Make sure that write is OK. */
  235. if (!success) {
  236. gpr_log(GPR_ERROR, "Write failed.");
  237. if (handshake != NULL) security_handshake_done(h, 0);
  238. return;
  239. }
  240. /* We may be done. */
  241. if (tsi_handshaker_is_in_progress(h->handshaker)) {
  242. /* TODO(klempner,jboeuf): This should probably use the client setup
  243. deadline */
  244. switch (grpc_endpoint_read(h->wrapped_endpoint, &h->incoming,
  245. &h->on_handshake_data_received_from_peer)) {
  246. case GRPC_ENDPOINT_ERROR:
  247. on_handshake_data_received_from_peer(h, 0);
  248. break;
  249. case GRPC_ENDPOINT_PENDING:
  250. break;
  251. case GRPC_ENDPOINT_DONE:
  252. on_handshake_data_received_from_peer(h, 1);
  253. break;
  254. }
  255. } else {
  256. check_peer(h);
  257. }
  258. }
  259. void grpc_do_security_handshake(tsi_handshaker *handshaker,
  260. grpc_security_connector *connector,
  261. grpc_endpoint *nonsecure_endpoint,
  262. grpc_security_handshake_done_cb cb,
  263. void *user_data) {
  264. grpc_security_handshake *h = gpr_malloc(sizeof(grpc_security_handshake));
  265. memset(h, 0, sizeof(grpc_security_handshake));
  266. h->handshaker = handshaker;
  267. h->connector = GRPC_SECURITY_CONNECTOR_REF(connector, "handshake");
  268. h->handshake_buffer_size = GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE;
  269. h->handshake_buffer = gpr_malloc(h->handshake_buffer_size);
  270. h->wrapped_endpoint = nonsecure_endpoint;
  271. h->user_data = user_data;
  272. h->cb = cb;
  273. grpc_iomgr_closure_init(&h->on_handshake_data_sent_to_peer,
  274. on_handshake_data_sent_to_peer, h);
  275. grpc_iomgr_closure_init(&h->on_handshake_data_received_from_peer,
  276. on_handshake_data_received_from_peer, h);
  277. gpr_slice_buffer_init(&h->left_overs);
  278. gpr_slice_buffer_init(&h->outgoing);
  279. gpr_slice_buffer_init(&h->incoming);
  280. send_handshake_bytes_to_peer(h);
  281. }