1 //
2 //
3 // Copyright 2017 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/transport_security_grpc.h"
22 
23 // This method creates a tsi_zero_copy_grpc_protector object.
tsi_handshaker_result_create_zero_copy_grpc_protector(const tsi_handshaker_result * self,size_t * max_output_protected_frame_size,tsi_zero_copy_grpc_protector ** protector)24 tsi_result tsi_handshaker_result_create_zero_copy_grpc_protector(
25     const tsi_handshaker_result* self, size_t* max_output_protected_frame_size,
26     tsi_zero_copy_grpc_protector** protector) {
27   if (self == nullptr || self->vtable == nullptr || protector == nullptr) {
28     return TSI_INVALID_ARGUMENT;
29   }
30   if (self->vtable->create_zero_copy_grpc_protector == nullptr) {
31     return TSI_UNIMPLEMENTED;
32   }
33   return self->vtable->create_zero_copy_grpc_protector(
34       self, max_output_protected_frame_size, protector);
35 }
36 
37 // --- tsi_zero_copy_grpc_protector common implementation. ---
38 
39 // Calls specific implementation after state/input validation.
40 
tsi_zero_copy_grpc_protector_protect(tsi_zero_copy_grpc_protector * self,grpc_slice_buffer * unprotected_slices,grpc_slice_buffer * protected_slices)41 tsi_result tsi_zero_copy_grpc_protector_protect(
42     tsi_zero_copy_grpc_protector* self, grpc_slice_buffer* unprotected_slices,
43     grpc_slice_buffer* protected_slices) {
44   if (self == nullptr || self->vtable == nullptr ||
45       unprotected_slices == nullptr || protected_slices == nullptr) {
46     return TSI_INVALID_ARGUMENT;
47   }
48   if (self->vtable->protect == nullptr) return TSI_UNIMPLEMENTED;
49   return self->vtable->protect(self, unprotected_slices, protected_slices);
50 }
51 
tsi_zero_copy_grpc_protector_unprotect(tsi_zero_copy_grpc_protector * self,grpc_slice_buffer * protected_slices,grpc_slice_buffer * unprotected_slices,int * min_progress_size)52 tsi_result tsi_zero_copy_grpc_protector_unprotect(
53     tsi_zero_copy_grpc_protector* self, grpc_slice_buffer* protected_slices,
54     grpc_slice_buffer* unprotected_slices, int* min_progress_size) {
55   if (self == nullptr || self->vtable == nullptr ||
56       protected_slices == nullptr || unprotected_slices == nullptr) {
57     return TSI_INVALID_ARGUMENT;
58   }
59   if (self->vtable->unprotect == nullptr) return TSI_UNIMPLEMENTED;
60   return self->vtable->unprotect(self, protected_slices, unprotected_slices,
61                                  min_progress_size);
62 }
63 
tsi_zero_copy_grpc_protector_destroy(tsi_zero_copy_grpc_protector * self)64 void tsi_zero_copy_grpc_protector_destroy(tsi_zero_copy_grpc_protector* self) {
65   if (self == nullptr) return;
66   self->vtable->destroy(self);
67 }
68 
tsi_zero_copy_grpc_protector_max_frame_size(tsi_zero_copy_grpc_protector * self,size_t * max_frame_size)69 tsi_result tsi_zero_copy_grpc_protector_max_frame_size(
70     tsi_zero_copy_grpc_protector* self, size_t* max_frame_size) {
71   if (self == nullptr || max_frame_size == nullptr) return TSI_INVALID_ARGUMENT;
72   if (self->vtable->max_frame_size == nullptr) return TSI_UNIMPLEMENTED;
73   return self->vtable->max_frame_size(self, max_frame_size);
74 }
75