Debian Codegraph @

src/core/tsi/fake_transport_security.cc

raw
1 /*
2 *
3 * Copyright 2015 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
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/tsi/fake_transport_security.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/gprpp/memory.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/tsi/transport_security_grpc.h"
33
34 /* --- Constants. ---*/
35 #define TSI_FAKE_FRAME_HEADER_SIZE 4
36 #define TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE 64
37 #define TSI_FAKE_DEFAULT_FRAME_SIZE 16384
38 #define TSI_FAKE_HANDSHAKER_OUTGOING_BUFFER_INITIAL_SIZE 256
39
40 /* --- Structure definitions. ---*/
41
42 /* a frame is encoded like this:
43 | size | data |
44 where the size field value is the size of the size field plus the size of
45 the data encoded in little endian on 4 bytes. */
46 struct tsi_fake_frame {
47 unsigned char* data;
48 size_t size;
49 size_t allocated_size;
50 size_t offset;
51 int needs_draining;
52 };
53 typedef enum {
54 TSI_FAKE_CLIENT_INIT = 0,
55 TSI_FAKE_SERVER_INIT = 1,
56 TSI_FAKE_CLIENT_FINISHED = 2,
57 TSI_FAKE_SERVER_FINISHED = 3,
58 TSI_FAKE_HANDSHAKE_MESSAGE_MAX = 4
59 } tsi_fake_handshake_message;
60
61 struct tsi_fake_handshaker {
62 tsi_handshaker base;
63 int is_client;
64 tsi_fake_handshake_message next_message_to_send;
65 int needs_incoming_message;
66 tsi_fake_frame incoming_frame;
67 tsi_fake_frame outgoing_frame;
68 unsigned char* outgoing_bytes_buffer;
69 size_t outgoing_bytes_buffer_size;
70 tsi_result result;
71 };
72 struct tsi_fake_frame_protector {
73 tsi_frame_protector base;
74 tsi_fake_frame protect_frame;
75 tsi_fake_frame unprotect_frame;
76 size_t max_frame_size;
77 };
78 struct tsi_fake_zero_copy_grpc_protector {
79 tsi_zero_copy_grpc_protector base;
80 grpc_slice_buffer header_sb;
81 grpc_slice_buffer protected_sb;
82 size_t max_frame_size;
83 size_t parsed_frame_size;
84 };
85 /* --- Utils. ---*/
86
87 static const char* tsi_fake_handshake_message_strings[] = {
88 "CLIENT_INIT", "SERVER_INIT", "CLIENT_FINISHED", "SERVER_FINISHED"};
89
90 static const char* tsi_fake_handshake_message_to_string(int msg) {
91 if (msg < 0 || msg >= TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
92 gpr_log(GPR_ERROR, "Invalid message %d", msg);
93 return "UNKNOWN";
94 }
95 return tsi_fake_handshake_message_strings[msg];
96 }
97
98 static tsi_result tsi_fake_handshake_message_from_string(
99 const char* msg_string, tsi_fake_handshake_message* msg,
100 std::string* error) {
101 for (int i = 0; i < TSI_FAKE_HANDSHAKE_MESSAGE_MAX; i++) {
102 if (strncmp(msg_string, tsi_fake_handshake_message_strings[i],
103 strlen(tsi_fake_handshake_message_strings[i])) == 0) {
104 *msg = static_cast<tsi_fake_handshake_message>(i);
105 return TSI_OK;
106 }
107 }
108 gpr_log(GPR_ERROR, "Invalid handshake message.");
109 if (error != nullptr) *error = "invalid handshake message";
110 return TSI_DATA_CORRUPTED;
111 }
112
113 static uint32_t load32_little_endian(const unsigned char* buf) {
114 return (static_cast<uint32_t>(buf[0]) | static_cast<uint32_t>(buf[1] << 8) |
115 static_cast<uint32_t>(buf[2] << 16) |
116 static_cast<uint32_t>(buf[3] << 24));
117 }
118
119 static void store32_little_endian(uint32_t value, unsigned char* buf) {
120 buf[3] = static_cast<unsigned char>((value >> 24) & 0xFF);
121 buf[2] = static_cast<unsigned char>((value >> 16) & 0xFF);
122 buf[1] = static_cast<unsigned char>((value >> 8) & 0xFF);
123 buf[0] = static_cast<unsigned char>((value)&0xFF);
124 }
125
126 static uint32_t read_frame_size(const grpc_slice_buffer* sb) {
127 GPR_ASSERT(sb != nullptr && sb->length >= TSI_FAKE_FRAME_HEADER_SIZE);
128 uint8_t frame_size_buffer[TSI_FAKE_FRAME_HEADER_SIZE];
129 uint8_t* buf = frame_size_buffer;
130 /* Copies the first 4 bytes to a temporary buffer. */
131 size_t remaining = TSI_FAKE_FRAME_HEADER_SIZE;
132 for (size_t i = 0; i < sb->count; i++) {
133 size_t slice_length = GRPC_SLICE_LENGTH(sb->slices[i]);
134 if (remaining <= slice_length) {
135 memcpy(buf, GRPC_SLICE_START_PTR(sb->slices[i]), remaining);
136 remaining = 0;
137 break;
138 } else {
139 memcpy(buf, GRPC_SLICE_START_PTR(sb->slices[i]), slice_length);
140 buf += slice_length;
141 remaining -= slice_length;
142 }
143 }
144 GPR_ASSERT(remaining == 0);
145 return load32_little_endian(frame_size_buffer);
146 }
147
148 uint32_t tsi_fake_zero_copy_grpc_protector_next_frame_size(
149 const grpc_slice_buffer* protected_slices) {
150 return read_frame_size(protected_slices);
151 }
152
153 static void tsi_fake_frame_reset(tsi_fake_frame* frame, int needs_draining) {
154 frame->offset = 0;
155 frame->needs_draining = needs_draining;
156 if (!needs_draining) frame->size = 0;
157 }
158
159 /* Checks if the frame's allocated size is at least frame->size, and reallocs
160 * more memory if necessary. */
161 static void tsi_fake_frame_ensure_size(tsi_fake_frame* frame) {
162 if (frame->data == nullptr) {
163 frame->allocated_size = frame->size;
164 frame->data =
165 static_cast<unsigned char*>(gpr_malloc(frame->allocated_size));
166 } else if (frame->size > frame->allocated_size) {
167 unsigned char* new_data =
168 static_cast<unsigned char*>(gpr_realloc(frame->data, frame->size));
169 frame->data = new_data;
170 frame->allocated_size = frame->size;
171 }
172 }
173
174 /* Decodes the serialized fake frame contained in incoming_bytes, and fills
175 * frame with the contents of the decoded frame.
176 * This method should not be called if frame->needs_framing is not 0. */
177 static tsi_result tsi_fake_frame_decode(const unsigned char* incoming_bytes,
178 size_t* incoming_bytes_size,
179 tsi_fake_frame* frame,
180 std::string* error) {
181 size_t available_size = *incoming_bytes_size;
182 size_t to_read_size = 0;
183 const unsigned char* bytes_cursor = incoming_bytes;
184
185 if (frame->needs_draining) {
186 if (error != nullptr) *error = "fake handshaker frame needs draining";
187 return TSI_INTERNAL_ERROR;
188 }
189 if (frame->data == nullptr) {
190 frame->allocated_size = TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE;
191 frame->data =
192 static_cast<unsigned char*>(gpr_malloc(frame->allocated_size));
193 }
194
195 if (frame->offset < TSI_FAKE_FRAME_HEADER_SIZE) {
196 to_read_size = TSI_FAKE_FRAME_HEADER_SIZE - frame->offset;
197 if (to_read_size > available_size) {
198 /* Just fill what we can and exit. */
199 memcpy(frame->data + frame->offset, bytes_cursor, available_size);
200 bytes_cursor += available_size;
201 frame->offset += available_size;
202 *incoming_bytes_size = static_cast<size_t>(bytes_cursor - incoming_bytes);
203 return TSI_INCOMPLETE_DATA;
204 }
205 memcpy(frame->data + frame->offset, bytes_cursor, to_read_size);
206 bytes_cursor += to_read_size;
207 frame->offset += to_read_size;
208 available_size -= to_read_size;
209 frame->size = load32_little_endian(frame->data);
210 tsi_fake_frame_ensure_size(frame);
211 }
212
213 to_read_size = frame->size - frame->offset;
214 if (to_read_size > available_size) {
215 memcpy(frame->data + frame->offset, bytes_cursor, available_size);
216 frame->offset += available_size;
217 bytes_cursor += available_size;
218 *incoming_bytes_size = static_cast<size_t>(bytes_cursor - incoming_bytes);
219 return TSI_INCOMPLETE_DATA;
220 }
221 memcpy(frame->data + frame->offset, bytes_cursor, to_read_size);
222 bytes_cursor += to_read_size;
223 *incoming_bytes_size = static_cast<size_t>(bytes_cursor - incoming_bytes);
224 tsi_fake_frame_reset(frame, 1 /* needs_draining */);
225 return TSI_OK;
226 }
227
228 /* Encodes a fake frame into its wire format and places the result in
229 * outgoing_bytes. outgoing_bytes_size indicates the size of the encoded frame.
230 * This method should not be called if frame->needs_framing is 0. */
231 static tsi_result tsi_fake_frame_encode(unsigned char* outgoing_bytes,
232 size_t* outgoing_bytes_size,
233 tsi_fake_frame* frame,
234 std::string* error) {
235 size_t to_write_size = frame->size - frame->offset;
236 if (!frame->needs_draining) {
237 if (error != nullptr) *error = "fake frame needs draining";
238 return TSI_INTERNAL_ERROR;
239 }
240 if (*outgoing_bytes_size < to_write_size) {
241 memcpy(outgoing_bytes, frame->data + frame->offset, *outgoing_bytes_size);
242 frame->offset += *outgoing_bytes_size;
243 return TSI_INCOMPLETE_DATA;
244 }
245 memcpy(outgoing_bytes, frame->data + frame->offset, to_write_size);
246 *outgoing_bytes_size = to_write_size;
247 tsi_fake_frame_reset(frame, 0 /* needs_draining */);
248 return TSI_OK;
249 }
250
251 /* Sets the payload of a fake frame to contain the given data blob, where
252 * data_size indicates the size of data. */
253 static void tsi_fake_frame_set_data(unsigned char* data, size_t data_size,
254 tsi_fake_frame* frame) {
255 frame->offset = 0;
256 frame->size = data_size + TSI_FAKE_FRAME_HEADER_SIZE;
257 tsi_fake_frame_ensure_size(frame);
258 store32_little_endian(static_cast<uint32_t>(frame->size), frame->data);
259 memcpy(frame->data + TSI_FAKE_FRAME_HEADER_SIZE, data, data_size);
260 tsi_fake_frame_reset(frame, 1 /* needs draining */);
261 }
262
263 /* Destroys the contents of a fake frame. */
264 static void tsi_fake_frame_destruct(tsi_fake_frame* frame) {
265 if (frame->data != nullptr) gpr_free(frame->data);
266 }
267
268 /* --- tsi_frame_protector methods implementation. ---*/
269
270 static tsi_result fake_protector_protect(tsi_frame_protector* self,
271 const unsigned char* unprotected_bytes,
272 size_t* unprotected_bytes_size,
273 unsigned char* protected_output_frames,
274 size_t* protected_output_frames_size) {
275 tsi_result result = TSI_OK;
276 tsi_fake_frame_protector* impl =
277 reinterpret_cast<tsi_fake_frame_protector*>(self);
278 unsigned char frame_header[TSI_FAKE_FRAME_HEADER_SIZE];
279 tsi_fake_frame* frame = &impl->protect_frame;
280 size_t saved_output_size = *protected_output_frames_size;
281 size_t drained_size = 0;
282 size_t* num_bytes_written = protected_output_frames_size;
283 *num_bytes_written = 0;
284
285 /* Try to drain first. */
286 if (frame->needs_draining) {
287 drained_size = saved_output_size - *num_bytes_written;
288 result = tsi_fake_frame_encode(protected_output_frames, &drained_size,
289 frame, /*error=*/nullptr);
290 *num_bytes_written += drained_size;
291 protected_output_frames += drained_size;
292 if (result != TSI_OK) {
293 if (result == TSI_INCOMPLETE_DATA) {
294 *unprotected_bytes_size = 0;
295 result = TSI_OK;
296 }
297 return result;
298 }
299 }
300
301 /* Now process the unprotected_bytes. */
302 if (frame->needs_draining) return TSI_INTERNAL_ERROR;
303 if (frame->size == 0) {
304 /* New frame, create a header. */
305 size_t written_in_frame_size = 0;
306 store32_little_endian(static_cast<uint32_t>(impl->max_frame_size),
307 frame_header);
308 written_in_frame_size = TSI_FAKE_FRAME_HEADER_SIZE;
309 result = tsi_fake_frame_decode(frame_header, &written_in_frame_size, frame,
310 /*error=*/nullptr);
311 if (result != TSI_INCOMPLETE_DATA) {
312 gpr_log(GPR_ERROR, "tsi_fake_frame_decode returned %s",
313 tsi_result_to_string(result));
314 return result;
315 }
316 }
317 result =
318 tsi_fake_frame_decode(unprotected_bytes, unprotected_bytes_size, frame,
319 /*error=*/nullptr);
320 if (result != TSI_OK) {
321 if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
322 return result;
323 }
324
325 /* Try to drain again. */
326 if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
327 if (frame->offset != 0) return TSI_INTERNAL_ERROR;
328 drained_size = saved_output_size - *num_bytes_written;
329 result = tsi_fake_frame_encode(protected_output_frames, &drained_size, frame,
330 /*error=*/nullptr);
331 *num_bytes_written += drained_size;
332 if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
333 return result;
334 }
335
336 static tsi_result fake_protector_protect_flush(
337 tsi_frame_protector* self, unsigned char* protected_output_frames,
338 size_t* protected_output_frames_size, size_t* still_pending_size) {
339 tsi_result result = TSI_OK;
340 tsi_fake_frame_protector* impl =
341 reinterpret_cast<tsi_fake_frame_protector*>(self);
342 tsi_fake_frame* frame = &impl->protect_frame;
343 if (!frame->needs_draining) {
344 /* Create a short frame. */
345 frame->size = frame->offset;
346 frame->offset = 0;
347 frame->needs_draining = 1;
348 store32_little_endian(static_cast<uint32_t>(frame->size),
349 frame->data); /* Overwrite header. */
350 }
351 result = tsi_fake_frame_encode(protected_output_frames,
352 protected_output_frames_size, frame,
353 /*error=*/nullptr);
354 if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
355 *still_pending_size = frame->size - frame->offset;
356 return result;
357 }
358
359 static tsi_result fake_protector_unprotect(
360 tsi_frame_protector* self, const unsigned char* protected_frames_bytes,
361 size_t* protected_frames_bytes_size, unsigned char* unprotected_bytes,
362 size_t* unprotected_bytes_size) {
363 tsi_result result = TSI_OK;
364 tsi_fake_frame_protector* impl =
365 reinterpret_cast<tsi_fake_frame_protector*>(self);
366 tsi_fake_frame* frame = &impl->unprotect_frame;
367 size_t saved_output_size = *unprotected_bytes_size;
368 size_t drained_size = 0;
369 size_t* num_bytes_written = unprotected_bytes_size;
370 *num_bytes_written = 0;
371
372 /* Try to drain first. */
373 if (frame->needs_draining) {
374 /* Go past the header if needed. */
375 if (frame->offset == 0) frame->offset = TSI_FAKE_FRAME_HEADER_SIZE;
376 drained_size = saved_output_size - *num_bytes_written;
377 result = tsi_fake_frame_encode(unprotected_bytes, &drained_size, frame,
378 /*error=*/nullptr);
379 unprotected_bytes += drained_size;
380 *num_bytes_written += drained_size;
381 if (result != TSI_OK) {
382 if (result == TSI_INCOMPLETE_DATA) {
383 *protected_frames_bytes_size = 0;
384 result = TSI_OK;
385 }
386 return result;
387 }
388 }
389
390 /* Now process the protected_bytes. */
391 if (frame->needs_draining) return TSI_INTERNAL_ERROR;
392 result = tsi_fake_frame_decode(protected_frames_bytes,
393 protected_frames_bytes_size, frame,
394 /*error=*/nullptr);
395 if (result != TSI_OK) {
396 if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
397 return result;
398 }
399
400 /* Try to drain again. */
401 if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
402 if (frame->offset != 0) return TSI_INTERNAL_ERROR;
403 frame->offset = TSI_FAKE_FRAME_HEADER_SIZE; /* Go past the header. */
404 drained_size = saved_output_size - *num_bytes_written;
405 result = tsi_fake_frame_encode(unprotected_bytes, &drained_size, frame,
406 /*error=*/nullptr);
407 *num_bytes_written += drained_size;
408 if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
409 return result;
410 }
411
412 static void fake_protector_destroy(tsi_frame_protector* self) {
413 tsi_fake_frame_protector* impl =
414 reinterpret_cast<tsi_fake_frame_protector*>(self);
415 tsi_fake_frame_destruct(&impl->protect_frame);
416 tsi_fake_frame_destruct(&impl->unprotect_frame);
417 gpr_free(self);
418 }
419
420 static const tsi_frame_protector_vtable frame_protector_vtable = {
421 fake_protector_protect,
422 fake_protector_protect_flush,
423 fake_protector_unprotect,
424 fake_protector_destroy,
425 };
426
427 /* --- tsi_zero_copy_grpc_protector methods implementation. ---*/
428
429 static tsi_result fake_zero_copy_grpc_protector_protect(
430 tsi_zero_copy_grpc_protector* self, grpc_slice_buffer* unprotected_slices,
431 grpc_slice_buffer* protected_slices) {
432 if (self == nullptr || unprotected_slices == nullptr ||
433 protected_slices == nullptr) {
434 return TSI_INVALID_ARGUMENT;
435 }
436 tsi_fake_zero_copy_grpc_protector* impl =
437 reinterpret_cast<tsi_fake_zero_copy_grpc_protector*>(self);
438 /* Protects each frame. */
439 while (unprotected_slices->length > 0) {
440 size_t frame_length =
441 std::min(impl->max_frame_size,
442 unprotected_slices->length + TSI_FAKE_FRAME_HEADER_SIZE);
443 grpc_slice slice = GRPC_SLICE_MALLOC(TSI_FAKE_FRAME_HEADER_SIZE);
444 store32_little_endian(static_cast<uint32_t>(frame_length),
445 GRPC_SLICE_START_PTR(slice));
446 grpc_slice_buffer_add(protected_slices, slice);
447 size_t data_length = frame_length - TSI_FAKE_FRAME_HEADER_SIZE;
448 grpc_slice_buffer_move_first(unprotected_slices, data_length,
449 protected_slices);
450 }
451 return TSI_OK;
452 }
453
454 static tsi_result fake_zero_copy_grpc_protector_unprotect(
455 tsi_zero_copy_grpc_protector* self, grpc_slice_buffer* protected_slices,
456 grpc_slice_buffer* unprotected_slices, int* min_progress_size) {
457 if (self == nullptr || unprotected_slices == nullptr ||
458 protected_slices == nullptr) {
459 return TSI_INVALID_ARGUMENT;
460 }
461 tsi_fake_zero_copy_grpc_protector* impl =
462 reinterpret_cast<tsi_fake_zero_copy_grpc_protector*>(self);
463 grpc_slice_buffer_move_into(protected_slices, &impl->protected_sb);
464 /* Unprotect each frame, if we get a full frame. */
465 while (impl->protected_sb.length >= TSI_FAKE_FRAME_HEADER_SIZE) {
466 if (impl->parsed_frame_size == 0) {
467 impl->parsed_frame_size = read_frame_size(&impl->protected_sb);
468 if (impl->parsed_frame_size <= 4) {
469 gpr_log(GPR_ERROR, "Invalid frame size.");
470 return TSI_DATA_CORRUPTED;
471 }
472 }
473 /* If we do not have a full frame, return with OK status. */
474 if (impl->protected_sb.length < impl->parsed_frame_size) break;
475 /* Strips header bytes. */
476 grpc_slice_buffer_move_first(&impl->protected_sb,
477 TSI_FAKE_FRAME_HEADER_SIZE, &impl->header_sb);
478 /* Moves data to unprotected slices. */
479 grpc_slice_buffer_move_first(
480 &impl->protected_sb,
481 impl->parsed_frame_size - TSI_FAKE_FRAME_HEADER_SIZE,
482 unprotected_slices);
483 impl->parsed_frame_size = 0;
484 grpc_slice_buffer_reset_and_unref(&impl->header_sb);
485 }
486 if (min_progress_size != nullptr) {
487 if (impl->parsed_frame_size > TSI_FAKE_FRAME_HEADER_SIZE) {
488 *min_progress_size = impl->parsed_frame_size - impl->protected_sb.length;
489 } else {
490 *min_progress_size = 1;
491 }
492 }
493 return TSI_OK;
494 }
495
496 static void fake_zero_copy_grpc_protector_destroy(
497 tsi_zero_copy_grpc_protector* self) {
498 if (self == nullptr) return;
499 tsi_fake_zero_copy_grpc_protector* impl =
500 reinterpret_cast<tsi_fake_zero_copy_grpc_protector*>(self);
501 grpc_slice_buffer_destroy(&impl->header_sb);
502 grpc_slice_buffer_destroy(&impl->protected_sb);
503 gpr_free(impl);
504 }
505
506 static tsi_result fake_zero_copy_grpc_protector_max_frame_size(
507 tsi_zero_copy_grpc_protector* self, size_t* max_frame_size) {
508 if (self == nullptr || max_frame_size == nullptr) return TSI_INVALID_ARGUMENT;
509 tsi_fake_zero_copy_grpc_protector* impl =
510 reinterpret_cast<tsi_fake_zero_copy_grpc_protector*>(self);
511 *max_frame_size = impl->max_frame_size;
512 return TSI_OK;
513 }
514
515 static const tsi_zero_copy_grpc_protector_vtable
516 zero_copy_grpc_protector_vtable = {
517 fake_zero_copy_grpc_protector_protect,
518 fake_zero_copy_grpc_protector_unprotect,
519 fake_zero_copy_grpc_protector_destroy,
520 fake_zero_copy_grpc_protector_max_frame_size,
521 };
522
523 /* --- tsi_handshaker_result methods implementation. ---*/
524
525 struct fake_handshaker_result {
526 tsi_handshaker_result base;
527 unsigned char* unused_bytes;
528 size_t unused_bytes_size;
529 };
530
531 static tsi_result fake_handshaker_result_extract_peer(
532 const tsi_handshaker_result* /*self*/, tsi_peer* peer) {
533 /* Construct a tsi_peer with 1 property: certificate type, security_level. */
534 tsi_result result = tsi_construct_peer(2, peer);
535 if (result != TSI_OK) return result;
536 result = tsi_construct_string_peer_property_from_cstring(
537 TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_FAKE_CERTIFICATE_TYPE,
538 &peer->properties[0]);
539 if (result != TSI_OK) tsi_peer_destruct(peer);
540 result = tsi_construct_string_peer_property_from_cstring(
541 TSI_SECURITY_LEVEL_PEER_PROPERTY,
542 tsi_security_level_to_string(TSI_SECURITY_NONE), &peer->properties[1]);
543 if (result != TSI_OK) tsi_peer_destruct(peer);
544 return result;
545 }
546
547 static tsi_result fake_handshaker_result_get_frame_protector_type(
548 const tsi_handshaker_result* /*self*/,
549 tsi_frame_protector_type* frame_protector_type) {
550 *frame_protector_type = TSI_FRAME_PROTECTOR_NORMAL_OR_ZERO_COPY;
551 return TSI_OK;
552 }
553
554 static tsi_result fake_handshaker_result_create_zero_copy_grpc_protector(
555 const tsi_handshaker_result* /*self*/,
556 size_t* max_output_protected_frame_size,
557 tsi_zero_copy_grpc_protector** protector) {
558 *protector =
559 tsi_create_fake_zero_copy_grpc_protector(max_output_protected_frame_size);
560 return TSI_OK;
561 }
562
563 static tsi_result fake_handshaker_result_create_frame_protector(
564 const tsi_handshaker_result* /*self*/,
565 size_t* max_output_protected_frame_size, tsi_frame_protector** protector) {
566 *protector = tsi_create_fake_frame_protector(max_output_protected_frame_size);
567 return TSI_OK;
568 }
569
570 static tsi_result fake_handshaker_result_get_unused_bytes(
571 const tsi_handshaker_result* self, const unsigned char** bytes,
572 size_t* bytes_size) {
573 fake_handshaker_result* result = reinterpret_cast<fake_handshaker_result*>(
574 const_cast<tsi_handshaker_result*>(self));
575 *bytes_size = result->unused_bytes_size;
576 *bytes = result->unused_bytes;
577 return TSI_OK;
578 }
579
580 static void fake_handshaker_result_destroy(tsi_handshaker_result* self) {
581 fake_handshaker_result* result =
582 reinterpret_cast<fake_handshaker_result*>(self);
583 gpr_free(result->unused_bytes);
584 gpr_free(self);
585 }
586
587 static const tsi_handshaker_result_vtable handshaker_result_vtable = {
588 fake_handshaker_result_extract_peer,
589 fake_handshaker_result_get_frame_protector_type,
590 fake_handshaker_result_create_zero_copy_grpc_protector,
591 fake_handshaker_result_create_frame_protector,
592 fake_handshaker_result_get_unused_bytes,
593 fake_handshaker_result_destroy,
594 };
595
596 static tsi_result fake_handshaker_result_create(
597 const unsigned char* unused_bytes, size_t unused_bytes_size,
598 tsi_handshaker_result** handshaker_result, std::string* error) {
599 if ((unused_bytes_size > 0 && unused_bytes == nullptr) ||
600 handshaker_result == nullptr) {
601 if (error != nullptr) *error = "invalid argument";
602 return TSI_INVALID_ARGUMENT;
603 }
604 fake_handshaker_result* result = grpc_core::Zalloc<fake_handshaker_result>();
605 result->base.vtable = &handshaker_result_vtable;
606 if (unused_bytes_size > 0) {
607 result->unused_bytes =
608 static_cast<unsigned char*>(gpr_malloc(unused_bytes_size));
609 memcpy(result->unused_bytes, unused_bytes, unused_bytes_size);
610 }
611 result->unused_bytes_size = unused_bytes_size;
612 *handshaker_result = &result->base;
613 return TSI_OK;
614 }
615
616 /* --- tsi_handshaker methods implementation. ---*/
617
618 static tsi_result fake_handshaker_get_bytes_to_send_to_peer(
619 tsi_handshaker* self, unsigned char* bytes, size_t* bytes_size,
620 std::string* error) {
621 tsi_fake_handshaker* impl = reinterpret_cast<tsi_fake_handshaker*>(self);
622 tsi_result result = TSI_OK;
623 if (impl->needs_incoming_message || impl->result == TSI_OK) {
624 *bytes_size = 0;
625 return TSI_OK;
626 }
627 if (!impl->outgoing_frame.needs_draining) {
628 tsi_fake_handshake_message next_message_to_send =
629 // NOLINTNEXTLINE(bugprone-misplaced-widening-cast)
630 static_cast<tsi_fake_handshake_message>(impl->next_message_to_send + 2);
631 const char* msg_string =
632 tsi_fake_handshake_message_to_string(impl->next_message_to_send);
633 tsi_fake_frame_set_data(
634 reinterpret_cast<unsigned char*>(const_cast<char*>(msg_string)),
635 strlen(msg_string), &impl->outgoing_frame);
636 if (next_message_to_send > TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
637 next_message_to_send = TSI_FAKE_HANDSHAKE_MESSAGE_MAX;
638 }
639 if (GRPC_TRACE_FLAG_ENABLED(tsi_tracing_enabled)) {
640 gpr_log(GPR_INFO, "%s prepared %s.",
641 impl->is_client ? "Client" : "Server",
642 tsi_fake_handshake_message_to_string(impl->next_message_to_send));
643 }
644 impl->next_message_to_send = next_message_to_send;
645 }
646 result =
647 tsi_fake_frame_encode(bytes, bytes_size, &impl->outgoing_frame, error);
648 if (result != TSI_OK) return result;
649 if (!impl->is_client &&
650 impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
651 /* We're done. */
652 if (GRPC_TRACE_FLAG_ENABLED(tsi_tracing_enabled)) {
653 gpr_log(GPR_INFO, "Server is done.");
654 }
655 impl->result = TSI_OK;
656 } else {
657 impl->needs_incoming_message = 1;
658 }
659 return TSI_OK;
660 }
661
662 static tsi_result fake_handshaker_process_bytes_from_peer(
663 tsi_handshaker* self, const unsigned char* bytes, size_t* bytes_size,
664 std::string* error) {
665 tsi_result result = TSI_OK;
666 tsi_fake_handshaker* impl = reinterpret_cast<tsi_fake_handshaker*>(self);
667 tsi_fake_handshake_message expected_msg =
668 static_cast<tsi_fake_handshake_message>(impl->next_message_to_send - 1);
669 tsi_fake_handshake_message received_msg;
670
671 if (!impl->needs_incoming_message || impl->result == TSI_OK) {
672 *bytes_size = 0;
673 return TSI_OK;
674 }
675 result =
676 tsi_fake_frame_decode(bytes, bytes_size, &impl->incoming_frame, error);
677 if (result != TSI_OK) return result;
678
679 /* We now have a complete frame. */
680 result = tsi_fake_handshake_message_from_string(
681 reinterpret_cast<const char*>(impl->incoming_frame.data) +
682 TSI_FAKE_FRAME_HEADER_SIZE,
683 &received_msg, error);
684 if (result != TSI_OK) {
685 impl->result = result;
686 return result;
687 }
688 if (received_msg != expected_msg) {
689 gpr_log(GPR_ERROR, "Invalid received message (%s instead of %s)",
690 tsi_fake_handshake_message_to_string(received_msg),
691 tsi_fake_handshake_message_to_string(expected_msg));
692 }
693 if (GRPC_TRACE_FLAG_ENABLED(tsi_tracing_enabled)) {
694 gpr_log(GPR_INFO, "%s received %s.", impl->is_client ? "Client" : "Server",
695 tsi_fake_handshake_message_to_string(received_msg));
696 }
697 tsi_fake_frame_reset(&impl->incoming_frame, 0 /* needs_draining */);
698 impl->needs_incoming_message = 0;
699 if (impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
700 /* We're done. */
701 if (GRPC_TRACE_FLAG_ENABLED(tsi_tracing_enabled)) {
702 gpr_log(GPR_INFO, "%s is done.", impl->is_client ? "Client" : "Server");
703 }
704 impl->result = TSI_OK;
705 }
706 return TSI_OK;
707 }
708
709 static tsi_result fake_handshaker_get_result(tsi_handshaker* self) {
710 tsi_fake_handshaker* impl = reinterpret_cast<tsi_fake_handshaker*>(self);
711 return impl->result;
712 }
713
714 static void fake_handshaker_destroy(tsi_handshaker* self) {
715 tsi_fake_handshaker* impl = reinterpret_cast<tsi_fake_handshaker*>(self);
716 tsi_fake_frame_destruct(&impl->incoming_frame);
717 tsi_fake_frame_destruct(&impl->outgoing_frame);
718 gpr_free(impl->outgoing_bytes_buffer);
719 gpr_free(self);
720 }
721
722 static tsi_result fake_handshaker_next(
723 tsi_handshaker* self, const unsigned char* received_bytes,
724 size_t received_bytes_size, const unsigned char** bytes_to_send,
725 size_t* bytes_to_send_size, tsi_handshaker_result** handshaker_result,
726 tsi_handshaker_on_next_done_cb /*cb*/, void* /*user_data*/,
727 std::string* error) {
728 /* Sanity check the arguments. */
729 if ((received_bytes_size > 0 && received_bytes == nullptr) ||
730 bytes_to_send == nullptr || bytes_to_send_size == nullptr ||
731 handshaker_result == nullptr) {
732 if (error != nullptr) *error = "invalid argument";
733 return TSI_INVALID_ARGUMENT;
734 }
735 tsi_fake_handshaker* handshaker =
736 reinterpret_cast<tsi_fake_handshaker*>(self);
737 tsi_result result = TSI_OK;
738
739 /* Decode and process a handshake frame from the peer. */
740 size_t consumed_bytes_size = received_bytes_size;
741 if (received_bytes_size > 0) {
742 result = fake_handshaker_process_bytes_from_peer(
743 self, received_bytes, &consumed_bytes_size, error);
744 if (result != TSI_OK) return result;
745 }
746
747 /* Create a handshake message to send to the peer and encode it as a fake
748 * frame. */
749 size_t offset = 0;
750 do {
751 size_t sent_bytes_size = handshaker->outgoing_bytes_buffer_size - offset;
752 result = fake_handshaker_get_bytes_to_send_to_peer(
753 self, handshaker->outgoing_bytes_buffer + offset, &sent_bytes_size,
754 error);
755 offset += sent_bytes_size;
756 if (result == TSI_INCOMPLETE_DATA) {
757 handshaker->outgoing_bytes_buffer_size *= 2;
758 handshaker->outgoing_bytes_buffer = static_cast<unsigned char*>(
759 gpr_realloc(handshaker->outgoing_bytes_buffer,
760 handshaker->outgoing_bytes_buffer_size));
761 }
762 } while (result == TSI_INCOMPLETE_DATA);
763 if (result != TSI_OK) return result;
764 *bytes_to_send = handshaker->outgoing_bytes_buffer;
765 *bytes_to_send_size = offset;
766
767 /* Check if the handshake was completed. */
768 if (fake_handshaker_get_result(self) == TSI_HANDSHAKE_IN_PROGRESS) {
769 *handshaker_result = nullptr;
770 } else {
771 /* Calculate the unused bytes. */
772 const unsigned char* unused_bytes = nullptr;
773 size_t unused_bytes_size = received_bytes_size - consumed_bytes_size;
774 if (unused_bytes_size > 0) {
775 unused_bytes = received_bytes + consumed_bytes_size;
776 }
777
778 /* Create a handshaker_result containing the unused bytes. */
779 result = fake_handshaker_result_create(unused_bytes, unused_bytes_size,
780 handshaker_result, error);
781 if (result == TSI_OK) {
782 /* Indicate that the handshake has completed and that a handshaker_result
783 * has been created. */
784 self->handshaker_result_created = true;
785 }
786 }
787 return result;
788 }
789
790 static const tsi_handshaker_vtable handshaker_vtable = {
791 nullptr, /* get_bytes_to_send_to_peer -- deprecated */
792 nullptr, /* process_bytes_from_peer -- deprecated */
793 nullptr, /* get_result -- deprecated */
794 nullptr, /* extract_peer -- deprecated */
795 nullptr, /* create_frame_protector -- deprecated */
796 fake_handshaker_destroy,
797 fake_handshaker_next,
798 nullptr, /* shutdown */
799 };
800
801 tsi_handshaker* tsi_create_fake_handshaker(int is_client) {
802 tsi_fake_handshaker* impl = grpc_core::Zalloc<tsi_fake_handshaker>();
803 impl->base.vtable = &handshaker_vtable;
804 impl->is_client = is_client;
805 impl->result = TSI_HANDSHAKE_IN_PROGRESS;
806 impl->outgoing_bytes_buffer_size =
807 TSI_FAKE_HANDSHAKER_OUTGOING_BUFFER_INITIAL_SIZE;
808 impl->outgoing_bytes_buffer =
809 static_cast<unsigned char*>(gpr_malloc(impl->outgoing_bytes_buffer_size));
810 if (is_client) {
811 impl->needs_incoming_message = 0;
812 impl->next_message_to_send = TSI_FAKE_CLIENT_INIT;
813 } else {
814 impl->needs_incoming_message = 1;
815 impl->next_message_to_send = TSI_FAKE_SERVER_INIT;
816 }
817 return &impl->base;
818 }
819
820 tsi_frame_protector* tsi_create_fake_frame_protector(
821 size_t* max_protected_frame_size) {
822 tsi_fake_frame_protector* impl =
823 grpc_core::Zalloc<tsi_fake_frame_protector>();
824 impl->max_frame_size = (max_protected_frame_size == nullptr)
825 ? TSI_FAKE_DEFAULT_FRAME_SIZE
826 : *max_protected_frame_size;
827 impl->base.vtable = &frame_protector_vtable;
828 return &impl->base;
829 }
830
831 tsi_zero_copy_grpc_protector* tsi_create_fake_zero_copy_grpc_protector(
832 size_t* max_protected_frame_size) {
833 tsi_fake_zero_copy_grpc_protector* impl =
834 static_cast<tsi_fake_zero_copy_grpc_protector*>(
835 gpr_zalloc(sizeof(*impl)));
836 grpc_slice_buffer_init(&impl->header_sb);
837 grpc_slice_buffer_init(&impl->protected_sb);
838 impl->max_frame_size = (max_protected_frame_size == nullptr)
839 ? TSI_FAKE_DEFAULT_FRAME_SIZE
840 : *max_protected_frame_size;
841 impl->parsed_frame_size = 0;
842 impl->base.vtable = &zero_copy_grpc_protector_vtable;
843 return &impl->base;
844 }