Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 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 "src/core/tsi/transport_security_grpc.h" |
| 20 | |
| 21 | /* This method creates a tsi_zero_copy_grpc_protector object. */ |
| 22 | tsi_result tsi_handshaker_result_create_zero_copy_grpc_protector( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 23 | grpc_exec_ctx* exec_ctx, const tsi_handshaker_result* self, |
| 24 | size_t* max_output_protected_frame_size, |
| 25 | tsi_zero_copy_grpc_protector** protector) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 26 | if (exec_ctx == nullptr || self == nullptr || self->vtable == nullptr || |
| 27 | protector == nullptr) { |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 28 | return TSI_INVALID_ARGUMENT; |
| 29 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 30 | if (self->vtable->create_zero_copy_grpc_protector == nullptr) { |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 31 | return TSI_UNIMPLEMENTED; |
| 32 | } |
| 33 | return self->vtable->create_zero_copy_grpc_protector( |
jiangtaoli2016 | f68978c | 2017-09-14 09:18:24 -0700 | [diff] [blame] | 34 | exec_ctx, self, max_output_protected_frame_size, protector); |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* --- tsi_zero_copy_grpc_protector common implementation. --- |
| 38 | |
| 39 | Calls specific implementation after state/input validation. */ |
| 40 | |
| 41 | tsi_result tsi_zero_copy_grpc_protector_protect( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 42 | grpc_exec_ctx* exec_ctx, tsi_zero_copy_grpc_protector* self, |
| 43 | grpc_slice_buffer* unprotected_slices, |
| 44 | grpc_slice_buffer* protected_slices) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 45 | if (exec_ctx == nullptr || self == nullptr || self->vtable == nullptr || |
| 46 | unprotected_slices == nullptr || protected_slices == nullptr) { |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 47 | return TSI_INVALID_ARGUMENT; |
| 48 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 49 | if (self->vtable->protect == nullptr) return TSI_UNIMPLEMENTED; |
jiangtaoli2016 | e12f5f2 | 2017-09-06 14:42:02 -0700 | [diff] [blame] | 50 | return self->vtable->protect(exec_ctx, self, unprotected_slices, |
| 51 | protected_slices); |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | tsi_result tsi_zero_copy_grpc_protector_unprotect( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 55 | grpc_exec_ctx* exec_ctx, tsi_zero_copy_grpc_protector* self, |
| 56 | grpc_slice_buffer* protected_slices, |
| 57 | grpc_slice_buffer* unprotected_slices) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 58 | if (exec_ctx == nullptr || self == nullptr || self->vtable == nullptr || |
| 59 | protected_slices == nullptr || unprotected_slices == nullptr) { |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 60 | return TSI_INVALID_ARGUMENT; |
| 61 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 62 | if (self->vtable->unprotect == nullptr) return TSI_UNIMPLEMENTED; |
jiangtaoli2016 | e12f5f2 | 2017-09-06 14:42:02 -0700 | [diff] [blame] | 63 | return self->vtable->unprotect(exec_ctx, self, protected_slices, |
| 64 | unprotected_slices); |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 67 | void tsi_zero_copy_grpc_protector_destroy(grpc_exec_ctx* exec_ctx, |
| 68 | tsi_zero_copy_grpc_protector* self) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 69 | if (self == nullptr) return; |
jiangtaoli2016 | e12f5f2 | 2017-09-06 14:42:02 -0700 | [diff] [blame] | 70 | self->vtable->destroy(exec_ctx, self); |
Jiangtao Li | 0211cfb | 2017-08-07 11:24:07 -0700 | [diff] [blame] | 71 | } |