Debian Codegraph @

src/core/tsi/local_transport_security.cc

raw
1 /*
2 *
3 * Copyright 2018 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/local_transport_security.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "src/core/tsi/transport_security_grpc.h"
33
34 namespace {
35
36 /* Main struct for local TSI zero-copy frame protector. */
37 typedef struct local_zero_copy_grpc_protector {
38 tsi_zero_copy_grpc_protector base;
39 } local_zero_copy_grpc_protector;
40
41 /* Main struct for local TSI handshaker result. */
42 typedef struct local_tsi_handshaker_result {
43 tsi_handshaker_result base;
44 unsigned char* unused_bytes;
45 size_t unused_bytes_size;
46 } local_tsi_handshaker_result;
47
48 /* Main struct for local TSI handshaker. */
49 typedef struct local_tsi_handshaker {
50 tsi_handshaker base;
51 } local_tsi_handshaker;
52
53 /* --- tsi_handshaker_result methods implementation. --- */
54
55 tsi_result handshaker_result_extract_peer(const tsi_handshaker_result* /*self*/,
56 tsi_peer* /*peer*/) {
57 return TSI_OK;
58 }
59
60 tsi_result handshaker_result_get_frame_protector_type(
61 const tsi_handshaker_result* /*self*/,
62 tsi_frame_protector_type* frame_protector_type) {
63 *frame_protector_type = TSI_FRAME_PROTECTOR_NONE;
64 return TSI_OK;
65 }
66
67 tsi_result handshaker_result_get_unused_bytes(const tsi_handshaker_result* self,
68 const unsigned char** bytes,
69 size_t* bytes_size) {
70 if (self == nullptr || bytes == nullptr || bytes_size == nullptr) {
71 gpr_log(GPR_ERROR, "Invalid arguments to get_unused_bytes()");
72 return TSI_INVALID_ARGUMENT;
73 }
74 auto* result = reinterpret_cast<local_tsi_handshaker_result*>(
75 const_cast<tsi_handshaker_result*>(self));
76 *bytes_size = result->unused_bytes_size;
77 *bytes = result->unused_bytes;
78 return TSI_OK;
79 }
80
81 void handshaker_result_destroy(tsi_handshaker_result* self) {
82 if (self == nullptr) {
83 return;
84 }
85 local_tsi_handshaker_result* result =
86 reinterpret_cast<local_tsi_handshaker_result*>(
87 const_cast<tsi_handshaker_result*>(self));
88 gpr_free(result->unused_bytes);
89 gpr_free(result);
90 }
91
92 const tsi_handshaker_result_vtable result_vtable = {
93 handshaker_result_extract_peer,
94 handshaker_result_get_frame_protector_type,
95 nullptr, /* handshaker_result_create_zero_copy_grpc_protector */
96 nullptr, /* handshaker_result_create_frame_protector */
97 handshaker_result_get_unused_bytes,
98 handshaker_result_destroy};
99
100 tsi_result create_handshaker_result(const unsigned char* received_bytes,
101 size_t received_bytes_size,
102 tsi_handshaker_result** self) {
103 if (self == nullptr) {
104 gpr_log(GPR_ERROR, "Invalid arguments to create_handshaker_result()");
105 return TSI_INVALID_ARGUMENT;
106 }
107 local_tsi_handshaker_result* result =
108 grpc_core::Zalloc<local_tsi_handshaker_result>();
109 if (received_bytes_size > 0) {
110 result->unused_bytes =
111 static_cast<unsigned char*>(gpr_malloc(received_bytes_size));
112 memcpy(result->unused_bytes, received_bytes, received_bytes_size);
113 }
114 result->unused_bytes_size = received_bytes_size;
115 result->base.vtable = &result_vtable;
116 *self = &result->base;
117 return TSI_OK;
118 }
119
120 /* --- tsi_handshaker methods implementation. --- */
121
122 tsi_result handshaker_next(tsi_handshaker* self,
123 const unsigned char* received_bytes,
124 size_t received_bytes_size,
125 const unsigned char** /*bytes_to_send*/,
126 size_t* bytes_to_send_size,
127 tsi_handshaker_result** result,
128 tsi_handshaker_on_next_done_cb /*cb*/,
129 void* /*user_data*/, std::string* error) {
130 if (self == nullptr) {
131 gpr_log(GPR_ERROR, "Invalid arguments to handshaker_next()");
132 if (error != nullptr) *error = "invalid argument";
133 return TSI_INVALID_ARGUMENT;
134 }
135 /* Note that there is no interaction between TSI peers, and all operations are
136 * local.
137 */
138 *bytes_to_send_size = 0;
139 create_handshaker_result(received_bytes, received_bytes_size, result);
140 return TSI_OK;
141 }
142
143 void handshaker_destroy(tsi_handshaker* self) {
144 if (self == nullptr) {
145 return;
146 }
147 local_tsi_handshaker* handshaker =
148 reinterpret_cast<local_tsi_handshaker*>(self);
149 gpr_free(handshaker);
150 }
151
152 const tsi_handshaker_vtable handshaker_vtable = {
153 nullptr, /* get_bytes_to_send_to_peer -- deprecated */
154 nullptr, /* process_bytes_from_peer -- deprecated */
155 nullptr, /* get_result -- deprecated */
156 nullptr, /* extract_peer -- deprecated */
157 nullptr, /* create_frame_protector -- deprecated */
158 handshaker_destroy,
159 handshaker_next,
160 nullptr, /* shutdown */
161 };
162
163 } // namespace
164
165 tsi_result tsi_local_handshaker_create(tsi_handshaker** self) {
166 if (self == nullptr) {
167 gpr_log(GPR_ERROR, "Invalid arguments to local_tsi_handshaker_create()");
168 return TSI_INVALID_ARGUMENT;
169 }
170 local_tsi_handshaker* handshaker = grpc_core::Zalloc<local_tsi_handshaker>();
171 handshaker->base.vtable = &handshaker_vtable;
172 *self = &handshaker->base;
173 return TSI_OK;
174 }