blob: 875d36721820182004fbfd2383a5182cee82a24c [file] [log] [blame]
Jiangtao Li0211cfb2017-08-07 11:24:07 -07001/*
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. */
22tsi_result tsi_handshaker_result_create_zero_copy_grpc_protector(
Craig Tillerbaa14a92017-11-03 09:09:36 -070023 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 Tiller4782d922017-11-10 09:53:21 -080026 if (exec_ctx == nullptr || self == nullptr || self->vtable == nullptr ||
27 protector == nullptr) {
Jiangtao Li0211cfb2017-08-07 11:24:07 -070028 return TSI_INVALID_ARGUMENT;
29 }
Craig Tiller4782d922017-11-10 09:53:21 -080030 if (self->vtable->create_zero_copy_grpc_protector == nullptr) {
Jiangtao Li0211cfb2017-08-07 11:24:07 -070031 return TSI_UNIMPLEMENTED;
32 }
33 return self->vtable->create_zero_copy_grpc_protector(
jiangtaoli2016f68978c2017-09-14 09:18:24 -070034 exec_ctx, self, max_output_protected_frame_size, protector);
Jiangtao Li0211cfb2017-08-07 11:24:07 -070035}
36
37/* --- tsi_zero_copy_grpc_protector common implementation. ---
38
39 Calls specific implementation after state/input validation. */
40
41tsi_result tsi_zero_copy_grpc_protector_protect(
Craig Tillerbaa14a92017-11-03 09:09:36 -070042 grpc_exec_ctx* exec_ctx, tsi_zero_copy_grpc_protector* self,
43 grpc_slice_buffer* unprotected_slices,
44 grpc_slice_buffer* protected_slices) {
Craig Tiller4782d922017-11-10 09:53:21 -080045 if (exec_ctx == nullptr || self == nullptr || self->vtable == nullptr ||
46 unprotected_slices == nullptr || protected_slices == nullptr) {
Jiangtao Li0211cfb2017-08-07 11:24:07 -070047 return TSI_INVALID_ARGUMENT;
48 }
Craig Tiller4782d922017-11-10 09:53:21 -080049 if (self->vtable->protect == nullptr) return TSI_UNIMPLEMENTED;
jiangtaoli2016e12f5f22017-09-06 14:42:02 -070050 return self->vtable->protect(exec_ctx, self, unprotected_slices,
51 protected_slices);
Jiangtao Li0211cfb2017-08-07 11:24:07 -070052}
53
54tsi_result tsi_zero_copy_grpc_protector_unprotect(
Craig Tillerbaa14a92017-11-03 09:09:36 -070055 grpc_exec_ctx* exec_ctx, tsi_zero_copy_grpc_protector* self,
56 grpc_slice_buffer* protected_slices,
57 grpc_slice_buffer* unprotected_slices) {
Craig Tiller4782d922017-11-10 09:53:21 -080058 if (exec_ctx == nullptr || self == nullptr || self->vtable == nullptr ||
59 protected_slices == nullptr || unprotected_slices == nullptr) {
Jiangtao Li0211cfb2017-08-07 11:24:07 -070060 return TSI_INVALID_ARGUMENT;
61 }
Craig Tiller4782d922017-11-10 09:53:21 -080062 if (self->vtable->unprotect == nullptr) return TSI_UNIMPLEMENTED;
jiangtaoli2016e12f5f22017-09-06 14:42:02 -070063 return self->vtable->unprotect(exec_ctx, self, protected_slices,
64 unprotected_slices);
Jiangtao Li0211cfb2017-08-07 11:24:07 -070065}
66
Craig Tillerbaa14a92017-11-03 09:09:36 -070067void tsi_zero_copy_grpc_protector_destroy(grpc_exec_ctx* exec_ctx,
68 tsi_zero_copy_grpc_protector* self) {
Craig Tiller4782d922017-11-10 09:53:21 -080069 if (self == nullptr) return;
jiangtaoli2016e12f5f22017-09-06 14:42:02 -070070 self->vtable->destroy(exec_ctx, self);
Jiangtao Li0211cfb2017-08-07 11:24:07 -070071}