aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/security/secure_transport_setup.c
blob: 1412ab21ad8b48edce58d4bc58dc39d3a7ec4e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "src/core/security/secure_transport_setup.h"

#include <string.h>

#include "src/core/security/secure_endpoint.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/slice_buffer.h>

#define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256

typedef struct {
  grpc_security_connector *connector;
  tsi_handshaker *handshaker;
  unsigned char *handshake_buffer;
  size_t handshake_buffer_size;
  grpc_endpoint *wrapped_endpoint;
  grpc_endpoint *secure_endpoint;
  gpr_slice_buffer left_overs;
  gpr_slice_buffer incoming;
  gpr_slice_buffer outgoing;
  grpc_secure_transport_setup_done_cb cb;
  void *user_data;
  grpc_closure on_handshake_data_sent_to_peer;
  grpc_closure on_handshake_data_received_from_peer;
} grpc_secure_transport_setup;

static void on_handshake_data_received_from_peer(void *setup, int success,
                                                 grpc_call_list *call_list);

static void on_handshake_data_sent_to_peer(void *setup, int success,
                                           grpc_call_list *call_list);

static void secure_transport_setup_done(grpc_secure_transport_setup *s,
                                        int is_success,
                                        grpc_call_list *call_list) {
  if (is_success) {
    s->cb(s->user_data, GRPC_SECURITY_OK, s->wrapped_endpoint,
          s->secure_endpoint, call_list);
  } else {
    if (s->secure_endpoint != NULL) {
      grpc_endpoint_shutdown(s->secure_endpoint, call_list);
      grpc_endpoint_destroy(s->secure_endpoint, call_list);
    } else {
      grpc_endpoint_destroy(s->wrapped_endpoint, call_list);
    }
    s->cb(s->user_data, GRPC_SECURITY_ERROR, s->wrapped_endpoint, NULL,
          call_list);
  }
  if (s->handshaker != NULL) tsi_handshaker_destroy(s->handshaker);
  if (s->handshake_buffer != NULL) gpr_free(s->handshake_buffer);
  gpr_slice_buffer_destroy(&s->left_overs);
  gpr_slice_buffer_destroy(&s->outgoing);
  gpr_slice_buffer_destroy(&s->incoming);
  GRPC_SECURITY_CONNECTOR_UNREF(s->connector, "secure_transport_setup");
  gpr_free(s);
}

static void on_peer_checked(void *user_data, grpc_security_status status,
                            grpc_call_list *call_list) {
  grpc_secure_transport_setup *s = user_data;
  tsi_frame_protector *protector;
  tsi_result result;
  if (status != GRPC_SECURITY_OK) {
    gpr_log(GPR_ERROR, "Error checking peer.");
    secure_transport_setup_done(s, 0, call_list);
    return;
  }
  result =
      tsi_handshaker_create_frame_protector(s->handshaker, NULL, &protector);
  if (result != TSI_OK) {
    gpr_log(GPR_ERROR, "Frame protector creation failed with error %s.",
            tsi_result_to_string(result));
    secure_transport_setup_done(s, 0, call_list);
    return;
  }
  s->secure_endpoint =
      grpc_secure_endpoint_create(protector, s->wrapped_endpoint,
                                  s->left_overs.slices, s->left_overs.count);
  s->left_overs.count = 0;
  s->left_overs.length = 0;
  secure_transport_setup_done(s, 1, call_list);
  return;
}

static void check_peer(grpc_secure_transport_setup *s,
                       grpc_call_list *call_list) {
  grpc_security_status peer_status;
  tsi_peer peer;
  tsi_result result = tsi_handshaker_extract_peer(s->handshaker, &peer);

  if (result != TSI_OK) {
    gpr_log(GPR_ERROR, "Peer extraction failed with error %s",
            tsi_result_to_string(result));
    secure_transport_setup_done(s, 0, call_list);
    return;
  }
  peer_status = grpc_security_connector_check_peer(s->connector, peer,
                                                   on_peer_checked, s);
  if (peer_status == GRPC_SECURITY_ERROR) {
    gpr_log(GPR_ERROR, "Peer check failed.");
    secure_transport_setup_done(s, 0, call_list);
    return;
  } else if (peer_status == GRPC_SECURITY_OK) {
    on_peer_checked(s, peer_status, call_list);
  }
}

static void send_handshake_bytes_to_peer(grpc_secure_transport_setup *s,
                                         grpc_call_list *call_list) {
  size_t offset = 0;
  tsi_result result = TSI_OK;
  gpr_slice to_send;

  do {
    size_t to_send_size = s->handshake_buffer_size - offset;
    result = tsi_handshaker_get_bytes_to_send_to_peer(
        s->handshaker, s->handshake_buffer + offset, &to_send_size);
    offset += to_send_size;
    if (result == TSI_INCOMPLETE_DATA) {
      s->handshake_buffer_size *= 2;
      s->handshake_buffer =
          gpr_realloc(s->handshake_buffer, s->handshake_buffer_size);
    }
  } while (result == TSI_INCOMPLETE_DATA);

  if (result != TSI_OK) {
    gpr_log(GPR_ERROR, "Handshake failed with error %s",
            tsi_result_to_string(result));
    secure_transport_setup_done(s, 0, call_list);
    return;
  }

  to_send =
      gpr_slice_from_copied_buffer((const char *)s->handshake_buffer, offset);
  gpr_slice_buffer_reset_and_unref(&s->outgoing);
  gpr_slice_buffer_add(&s->outgoing, to_send);
  /* TODO(klempner,jboeuf): This should probably use the client setup
         deadline */
  grpc_endpoint_write(s->wrapped_endpoint, &s->outgoing,
                      &s->on_handshake_data_sent_to_peer, call_list);
}

static void on_handshake_data_received_from_peer(void *setup, int success,
                                                 grpc_call_list *call_list) {
  grpc_secure_transport_setup *s = setup;
  size_t consumed_slice_size = 0;
  tsi_result result = TSI_OK;
  size_t i;
  size_t num_left_overs;
  int has_left_overs_in_current_slice = 0;

  if (!success) {
    gpr_log(GPR_ERROR, "Read failed.");
    secure_transport_setup_done(s, 0, call_list);
    return;
  }

  for (i = 0; i < s->incoming.count; i++) {
    consumed_slice_size = GPR_SLICE_LENGTH(s->incoming.slices[i]);
    result = tsi_handshaker_process_bytes_from_peer(
        s->handshaker, GPR_SLICE_START_PTR(s->incoming.slices[i]),
        &consumed_slice_size);
    if (!tsi_handshaker_is_in_progress(s->handshaker)) break;
  }

  if (tsi_handshaker_is_in_progress(s->handshaker)) {
    /* We may need more data. */
    if (result == TSI_INCOMPLETE_DATA) {
      grpc_endpoint_read(s->wrapped_endpoint, &s->incoming,
                         &s->on_handshake_data_received_from_peer, call_list);
      return;
    } else {
      send_handshake_bytes_to_peer(s, call_list);
      return;
    }
  }

  if (result != TSI_OK) {
    gpr_log(GPR_ERROR, "Handshake failed with error %s",
            tsi_result_to_string(result));
    secure_transport_setup_done(s, 0, call_list);
    return;
  }

  /* Handshake is done and successful this point. */
  has_left_overs_in_current_slice =
      (consumed_slice_size < GPR_SLICE_LENGTH(s->incoming.slices[i]));
  num_left_overs =
      (has_left_overs_in_current_slice ? 1 : 0) + s->incoming.count - i - 1;
  if (num_left_overs == 0) {
    check_peer(s, call_list);
    return;
  }
  /* Put the leftovers in our buffer (ownership transfered). */
  if (has_left_overs_in_current_slice) {
    gpr_slice_buffer_add(
        &s->left_overs,
        gpr_slice_split_tail(&s->incoming.slices[i], consumed_slice_size));
    gpr_slice_unref(
        s->incoming.slices[i]); /* split_tail above increments refcount. */
  }
  gpr_slice_buffer_addn(
      &s->left_overs, &s->incoming.slices[i + 1],
      num_left_overs - (size_t)has_left_overs_in_current_slice);
  check_peer(s, call_list);
}

/* If setup is NULL, the setup is done. */
static void on_handshake_data_sent_to_peer(void *setup, int success,
                                           grpc_call_list *call_list) {
  grpc_secure_transport_setup *s = setup;

  /* Make sure that write is OK. */
  if (!success) {
    gpr_log(GPR_ERROR, "Write failed.");
    if (setup != NULL) secure_transport_setup_done(s, 0, call_list);
    return;
  }

  /* We may be done. */
  if (tsi_handshaker_is_in_progress(s->handshaker)) {
    grpc_endpoint_read(s->wrapped_endpoint, &s->incoming,
                       &s->on_handshake_data_received_from_peer, call_list);
  } else {
    check_peer(s, call_list);
  }
}

void grpc_setup_secure_transport(grpc_security_connector *connector,
                                 grpc_endpoint *nonsecure_endpoint,
                                 grpc_secure_transport_setup_done_cb cb,
                                 void *user_data, grpc_call_list *call_list) {
  grpc_security_status result = GRPC_SECURITY_OK;
  grpc_secure_transport_setup *s =
      gpr_malloc(sizeof(grpc_secure_transport_setup));
  memset(s, 0, sizeof(grpc_secure_transport_setup));
  result = grpc_security_connector_create_handshaker(connector, &s->handshaker);
  if (result != GRPC_SECURITY_OK) {
    secure_transport_setup_done(s, 0, call_list);
    return;
  }
  s->connector =
      GRPC_SECURITY_CONNECTOR_REF(connector, "secure_transport_setup");
  s->handshake_buffer_size = GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE;
  s->handshake_buffer = gpr_malloc(s->handshake_buffer_size);
  s->wrapped_endpoint = nonsecure_endpoint;
  s->user_data = user_data;
  s->cb = cb;
  grpc_closure_init(&s->on_handshake_data_sent_to_peer,
                    on_handshake_data_sent_to_peer, s);
  grpc_closure_init(&s->on_handshake_data_received_from_peer,
                    on_handshake_data_received_from_peer, s);
  gpr_slice_buffer_init(&s->left_overs);
  gpr_slice_buffer_init(&s->outgoing);
  gpr_slice_buffer_init(&s->incoming);
  send_handshake_bytes_to_peer(s, call_list);
}