transport_security.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
28 
29 /* --- Tracing. --- */
30 
32 
33 /* --- tsi_result common implementation. --- */
34 
36  switch (result) {
37  case TSI_OK:
38  return "TSI_OK";
39  case TSI_UNKNOWN_ERROR:
40  return "TSI_UNKNOWN_ERROR";
42  return "TSI_INVALID_ARGUMENT";
44  return "TSI_PERMISSION_DENIED";
46  return "TSI_INCOMPLETE_DATA";
48  return "TSI_FAILED_PRECONDITION";
49  case TSI_UNIMPLEMENTED:
50  return "TSI_UNIMPLEMENTED";
51  case TSI_INTERNAL_ERROR:
52  return "TSI_INTERNAL_ERROR";
53  case TSI_DATA_CORRUPTED:
54  return "TSI_DATA_CORRUPTED";
55  case TSI_NOT_FOUND:
56  return "TSI_NOT_FOUND";
58  return "TSI_PROTOCOL_FAILURE";
60  return "TSI_HANDSHAKE_IN_PROGRESS";
62  return "TSI_OUT_OF_RESOURCES";
63  case TSI_ASYNC:
64  return "TSI_ASYNC";
65  default:
66  return "UNKNOWN";
67  }
68 }
69 
70 const char* tsi_security_level_to_string(tsi_security_level security_level) {
71  switch (security_level) {
72  case TSI_SECURITY_NONE:
73  return "TSI_SECURITY_NONE";
74  case TSI_INTEGRITY_ONLY:
75  return "TSI_INTEGRITY_ONLY";
77  return "TSI_PRIVACY_AND_INTEGRITY";
78  default:
79  return "UNKNOWN";
80  }
81 }
82 
83 /* --- tsi_frame_protector common implementation. ---
84 
85  Calls specific implementation after state/input validation. */
86 
88  const unsigned char* unprotected_bytes,
89  size_t* unprotected_bytes_size,
90  unsigned char* protected_output_frames,
91  size_t* protected_output_frames_size) {
92  if (self == nullptr || self->vtable == nullptr ||
93  unprotected_bytes == nullptr || unprotected_bytes_size == nullptr ||
94  protected_output_frames == nullptr ||
95  protected_output_frames_size == nullptr) {
96  return TSI_INVALID_ARGUMENT;
97  }
98  if (self->vtable->protect == nullptr) return TSI_UNIMPLEMENTED;
99  return self->vtable->protect(self, unprotected_bytes, unprotected_bytes_size,
100  protected_output_frames,
101  protected_output_frames_size);
102 }
103 
105  tsi_frame_protector* self, unsigned char* protected_output_frames,
106  size_t* protected_output_frames_size, size_t* still_pending_size) {
107  if (self == nullptr || self->vtable == nullptr ||
108  protected_output_frames == nullptr ||
109  protected_output_frames_size == nullptr ||
110  still_pending_size == nullptr) {
111  return TSI_INVALID_ARGUMENT;
112  }
113  if (self->vtable->protect_flush == nullptr) return TSI_UNIMPLEMENTED;
114  return self->vtable->protect_flush(self, protected_output_frames,
115  protected_output_frames_size,
116  still_pending_size);
117 }
118 
120  tsi_frame_protector* self, const unsigned char* protected_frames_bytes,
121  size_t* protected_frames_bytes_size, unsigned char* unprotected_bytes,
122  size_t* unprotected_bytes_size) {
123  if (self == nullptr || self->vtable == nullptr ||
124  protected_frames_bytes == nullptr ||
125  protected_frames_bytes_size == nullptr || unprotected_bytes == nullptr ||
126  unprotected_bytes_size == nullptr) {
127  return TSI_INVALID_ARGUMENT;
128  }
129  if (self->vtable->unprotect == nullptr) return TSI_UNIMPLEMENTED;
130  return self->vtable->unprotect(self, protected_frames_bytes,
131  protected_frames_bytes_size, unprotected_bytes,
132  unprotected_bytes_size);
133 }
134 
136  if (self == nullptr) return;
137  self->vtable->destroy(self);
138 }
139 
140 /* --- tsi_handshaker common implementation. ---
141 
142  Calls specific implementation after state/input validation. */
143 
145  unsigned char* bytes,
146  size_t* bytes_size) {
147  if (self == nullptr || self->vtable == nullptr || bytes == nullptr ||
148  bytes_size == nullptr) {
149  return TSI_INVALID_ARGUMENT;
150  }
151  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
152  if (self->handshake_shutdown) return TSI_HANDSHAKE_SHUTDOWN;
153  if (self->vtable->get_bytes_to_send_to_peer == nullptr) {
154  return TSI_UNIMPLEMENTED;
155  }
156  return self->vtable->get_bytes_to_send_to_peer(self, bytes, bytes_size);
157 }
158 
160  const unsigned char* bytes,
161  size_t* bytes_size) {
162  if (self == nullptr || self->vtable == nullptr || bytes == nullptr ||
163  bytes_size == nullptr) {
164  return TSI_INVALID_ARGUMENT;
165  }
166  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
167  if (self->handshake_shutdown) return TSI_HANDSHAKE_SHUTDOWN;
168  if (self->vtable->process_bytes_from_peer == nullptr) {
169  return TSI_UNIMPLEMENTED;
170  }
171  return self->vtable->process_bytes_from_peer(self, bytes, bytes_size);
172 }
173 
175  if (self == nullptr || self->vtable == nullptr) return TSI_INVALID_ARGUMENT;
176  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
177  if (self->handshake_shutdown) return TSI_HANDSHAKE_SHUTDOWN;
178  if (self->vtable->get_result == nullptr) return TSI_UNIMPLEMENTED;
179  return self->vtable->get_result(self);
180 }
181 
183  if (self == nullptr || self->vtable == nullptr || peer == nullptr) {
184  return TSI_INVALID_ARGUMENT;
185  }
186  memset(peer, 0, sizeof(tsi_peer));
187  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
188  if (self->handshake_shutdown) return TSI_HANDSHAKE_SHUTDOWN;
189  if (tsi_handshaker_get_result(self) != TSI_OK) {
191  }
192  if (self->vtable->extract_peer == nullptr) return TSI_UNIMPLEMENTED;
193  return self->vtable->extract_peer(self, peer);
194 }
195 
197  tsi_handshaker* self, size_t* max_output_protected_frame_size,
198  tsi_frame_protector** protector) {
200  if (self == nullptr || self->vtable == nullptr || protector == nullptr) {
201  return TSI_INVALID_ARGUMENT;
202  }
203  if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
204  if (self->handshake_shutdown) return TSI_HANDSHAKE_SHUTDOWN;
206  if (self->vtable->create_frame_protector == nullptr) return TSI_UNIMPLEMENTED;
207  result = self->vtable->create_frame_protector(
208  self, max_output_protected_frame_size, protector);
209  if (result == TSI_OK) {
210  self->frame_protector_created = true;
211  }
212  return result;
213 }
214 
216  tsi_handshaker* self, const unsigned char* received_bytes,
217  size_t received_bytes_size, const unsigned char** bytes_to_send,
218  size_t* bytes_to_send_size, tsi_handshaker_result** handshaker_result,
219  tsi_handshaker_on_next_done_cb cb, void* user_data) {
220  if (self == nullptr || self->vtable == nullptr) return TSI_INVALID_ARGUMENT;
221  if (self->handshaker_result_created) return TSI_FAILED_PRECONDITION;
222  if (self->handshake_shutdown) return TSI_HANDSHAKE_SHUTDOWN;
223  if (self->vtable->next == nullptr) return TSI_UNIMPLEMENTED;
224  return self->vtable->next(self, received_bytes, received_bytes_size,
225  bytes_to_send, bytes_to_send_size,
226  handshaker_result, cb, user_data);
227 }
228 
230  if (self == nullptr || self->vtable == nullptr) return;
231  if (self->vtable->shutdown != nullptr) {
232  self->vtable->shutdown(self);
233  }
234  self->handshake_shutdown = true;
235 }
236 
238  if (self == nullptr) return;
239  self->vtable->destroy(self);
240 }
241 
242 /* --- tsi_handshaker_result implementation. --- */
243 
245  tsi_peer* peer) {
246  if (self == nullptr || self->vtable == nullptr || peer == nullptr) {
247  return TSI_INVALID_ARGUMENT;
248  }
249  memset(peer, 0, sizeof(tsi_peer));
250  if (self->vtable->extract_peer == nullptr) return TSI_UNIMPLEMENTED;
251  return self->vtable->extract_peer(self, peer);
252 }
253 
255  const tsi_handshaker_result* self,
256  tsi_frame_protector_type* frame_protector_type) {
257  if (self == nullptr || frame_protector_type == nullptr) {
258  return TSI_INVALID_ARGUMENT;
259  }
260  if (self->vtable->get_frame_protector_type == nullptr) {
261  return TSI_UNIMPLEMENTED;
262  }
263  return self->vtable->get_frame_protector_type(self, frame_protector_type);
264 }
265 
267  const tsi_handshaker_result* self, size_t* max_output_protected_frame_size,
268  tsi_frame_protector** protector) {
269  if (self == nullptr || self->vtable == nullptr || protector == nullptr) {
270  return TSI_INVALID_ARGUMENT;
271  }
272  if (self->vtable->create_frame_protector == nullptr) return TSI_UNIMPLEMENTED;
273  return self->vtable->create_frame_protector(
274  self, max_output_protected_frame_size, protector);
275 }
276 
278  const tsi_handshaker_result* self, const unsigned char** bytes,
279  size_t* bytes_size) {
280  if (self == nullptr || self->vtable == nullptr || bytes == nullptr ||
281  bytes_size == nullptr) {
282  return TSI_INVALID_ARGUMENT;
283  }
284  if (self->vtable->get_unused_bytes == nullptr) return TSI_UNIMPLEMENTED;
285  return self->vtable->get_unused_bytes(self, bytes, bytes_size);
286 }
287 
289  if (self == nullptr) return;
290  self->vtable->destroy(self);
291 }
292 
293 /* --- tsi_peer implementation. --- */
294 
296  tsi_peer_property property;
297  memset(&property, 0, sizeof(tsi_peer_property));
298  return property;
299 }
300 
302  size_t child_count) {
303  size_t i;
304  for (i = 0; i < child_count; i++) {
306  }
308 }
309 
311  if (property->name != nullptr) {
312  gpr_free(property->name);
313  }
314  if (property->value.data != nullptr) {
315  gpr_free(property->value.data);
316  }
317  *property = tsi_init_peer_property(); /* Reset everything to 0. */
318 }
319 
321  if (self == nullptr) return;
322  if (self->properties != nullptr) {
323  tsi_peer_destroy_list_property(self->properties, self->property_count);
324  self->properties = nullptr;
325  }
326  self->property_count = 0;
327 }
328 
330  const char* name, size_t value_length, tsi_peer_property* property) {
331  *property = tsi_init_peer_property();
332  if (name != nullptr) property->name = gpr_strdup(name);
333  if (value_length > 0) {
334  property->value.data = static_cast<char*>(gpr_zalloc(value_length));
335  property->value.length = value_length;
336  }
337  return TSI_OK;
338 }
339 
341  const char* name, const char* value, tsi_peer_property* property) {
343  property);
344 }
345 
347  const char* value,
348  size_t value_length,
349  tsi_peer_property* property) {
351  name, value_length, property);
352  if (result != TSI_OK) return result;
353  if (value_length > 0) {
354  memcpy(property->value.data, value, value_length);
355  }
356  return TSI_OK;
357 }
358 
359 tsi_result tsi_construct_peer(size_t property_count, tsi_peer* peer) {
360  memset(peer, 0, sizeof(tsi_peer));
361  if (property_count > 0) {
362  peer->properties = static_cast<tsi_peer_property*>(
363  gpr_zalloc(property_count * sizeof(tsi_peer_property)));
364  peer->property_count = property_count;
365  }
366  return TSI_OK;
367 }
368 
370  const char* name) {
371  size_t i;
372  if (peer == nullptr) return nullptr;
373  for (i = 0; i < peer->property_count; i++) {
374  const tsi_peer_property* property = &peer->properties[i];
375  if (name == nullptr && property->name == nullptr) {
376  return property;
377  }
378  if (name != nullptr && property->name != nullptr &&
379  strcmp(property->name, name) == 0) {
380  return property;
381  }
382  }
383  return nullptr;
384 }
tsi_security_level_to_string
const char * tsi_security_level_to_string(tsi_security_level security_level)
Definition: transport_security.cc:70
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
tsi_peer_property_destruct
void tsi_peer_property_destruct(tsi_peer_property *property)
Definition: transport_security.cc:310
tsi_peer::properties
tsi_peer_property * properties
Definition: transport_security_interface.h:239
tsi_peer_property::value
struct tsi_peer_property::@48 value
memset
return memset(p, 0, total)
tsi_tracing_enabled
grpc_core::TraceFlag tsi_tracing_enabled(false, "tsi")
tsi_handshaker
Definition: transport_security.h:84
tsi_handshaker_destroy
void tsi_handshaker_destroy(tsi_handshaker *self)
Definition: transport_security.cc:237
TSI_INTERNAL_ERROR
@ TSI_INTERNAL_ERROR
Definition: transport_security_interface.h:39
tsi_handshaker_result_get_unused_bytes
tsi_result tsi_handshaker_result_get_unused_bytes(const tsi_handshaker_result *self, const unsigned char **bytes, size_t *bytes_size)
Definition: transport_security.cc:277
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
setup.name
name
Definition: setup.py:542
tsi_peer_destroy_list_property
static void tsi_peer_destroy_list_property(tsi_peer_property *children, size_t child_count)
Definition: transport_security.cc:301
TSI_FAILED_PRECONDITION
@ TSI_FAILED_PRECONDITION
Definition: transport_security_interface.h:37
TSI_HANDSHAKE_SHUTDOWN
@ TSI_HANDSHAKE_SHUTDOWN
Definition: transport_security_interface.h:46
tsi_frame_protector_protect_flush
tsi_result tsi_frame_protector_protect_flush(tsi_frame_protector *self, unsigned char *protected_output_frames, size_t *protected_output_frames_size, size_t *still_pending_size)
Definition: transport_security.cc:104
tsi_handshaker_result_get_frame_protector_type
tsi_result tsi_handshaker_result_get_frame_protector_type(const tsi_handshaker_result *self, tsi_frame_protector_type *frame_protector_type)
Definition: transport_security.cc:254
TSI_UNKNOWN_ERROR
@ TSI_UNKNOWN_ERROR
Definition: transport_security_interface.h:33
tsi_frame_protector_unprotect
tsi_result tsi_frame_protector_unprotect(tsi_frame_protector *self, const unsigned char *protected_frames_bytes, size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t *unprotected_bytes_size)
Definition: transport_security.cc:119
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
tsi_construct_allocated_string_peer_property
tsi_result tsi_construct_allocated_string_peer_property(const char *name, size_t value_length, tsi_peer_property *property)
Definition: transport_security.cc:329
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
string_util.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
tsi_frame_protector_type
tsi_frame_protector_type
Definition: transport_security_interface.h:69
tsi_handshaker_create_frame_protector
tsi_result tsi_handshaker_create_frame_protector(tsi_handshaker *self, size_t *max_output_protected_frame_size, tsi_frame_protector **protector)
Definition: transport_security.cc:196
tsi_init_peer_property
tsi_peer_property tsi_init_peer_property(void)
Definition: transport_security.cc:295
tsi_handshaker_on_next_done_cb
void(* tsi_handshaker_on_next_done_cb)(tsi_result status, void *user_data, const unsigned char *bytes_to_send, size_t bytes_to_send_size, tsi_handshaker_result *handshaker_result)
Definition: transport_security_interface.h:462
tsi_result
tsi_result
Definition: transport_security_interface.h:31
TSI_PERMISSION_DENIED
@ TSI_PERMISSION_DENIED
Definition: transport_security_interface.h:35
tsi_peer_property::data
char * data
Definition: transport_security_interface.h:233
tsi_peer_get_property_by_name
const tsi_peer_property * tsi_peer_get_property_by_name(const tsi_peer *peer, const char *name)
Definition: transport_security.cc:369
TSI_INTEGRITY_ONLY
@ TSI_INTEGRITY_ONLY
Definition: transport_security_interface.h:55
TSI_SECURITY_NONE
@ TSI_SECURITY_NONE
Definition: transport_security_interface.h:54
tsi_peer_property::name
char * name
Definition: transport_security_interface.h:231
tsi_handshaker_extract_peer
tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer)
Definition: transport_security.cc:182
tsi_handshaker_result_extract_peer
tsi_result tsi_handshaker_result_extract_peer(const tsi_handshaker_result *self, tsi_peer *peer)
Definition: transport_security.cc:244
TSI_UNIMPLEMENTED
@ TSI_UNIMPLEMENTED
Definition: transport_security_interface.h:38
grpc_core::TraceFlag
Definition: debug/trace.h:63
tsi_handshaker_shutdown
void tsi_handshaker_shutdown(tsi_handshaker *self)
Definition: transport_security.cc:229
tsi_peer_property
Definition: transport_security_interface.h:230
value
const char * value
Definition: hpack_parser_table.cc:165
tsi_handshaker_next
tsi_result tsi_handshaker_next(tsi_handshaker *self, const unsigned char *received_bytes, size_t received_bytes_size, const unsigned char **bytes_to_send, size_t *bytes_to_send_size, tsi_handshaker_result **handshaker_result, tsi_handshaker_on_next_done_cb cb, void *user_data)
Definition: transport_security.cc:215
TSI_DATA_CORRUPTED
@ TSI_DATA_CORRUPTED
Definition: transport_security_interface.h:40
tsi_peer
Definition: transport_security_interface.h:238
transport_security.h
tsi_handshaker_result_destroy
void tsi_handshaker_result_destroy(tsi_handshaker_result *self)
Definition: transport_security.cc:288
TSI_INCOMPLETE_DATA
@ TSI_INCOMPLETE_DATA
Definition: transport_security_interface.h:36
tsi_handshaker_get_result
tsi_result tsi_handshaker_get_result(tsi_handshaker *self)
Definition: transport_security.cc:174
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
TSI_ASYNC
@ TSI_ASYNC
Definition: transport_security_interface.h:45
tsi_result_to_string
const char * tsi_result_to_string(tsi_result result)
Definition: transport_security.cc:35
tsi_frame_protector_protect
tsi_result tsi_frame_protector_protect(tsi_frame_protector *self, const unsigned char *unprotected_bytes, size_t *unprotected_bytes_size, unsigned char *protected_output_frames, size_t *protected_output_frames_size)
Definition: transport_security.cc:87
tsi_security_level
tsi_security_level
Definition: transport_security_interface.h:52
alloc.h
TSI_INVALID_ARGUMENT
@ TSI_INVALID_ARGUMENT
Definition: transport_security_interface.h:34
TSI_OUT_OF_RESOURCES
@ TSI_OUT_OF_RESOURCES
Definition: transport_security_interface.h:44
tsi_handshaker_result_create_frame_protector
tsi_result tsi_handshaker_result_create_frame_protector(const tsi_handshaker_result *self, size_t *max_output_protected_frame_size, tsi_frame_protector **protector)
Definition: transport_security.cc:266
TSI_PROTOCOL_FAILURE
@ TSI_PROTOCOL_FAILURE
Definition: transport_security_interface.h:42
tsi_handshaker_result
Definition: transport_security.h:121
tsi_handshaker_process_bytes_from_peer
tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self, const unsigned char *bytes, size_t *bytes_size)
Definition: transport_security.cc:159
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
TSI_PRIVACY_AND_INTEGRITY
@ TSI_PRIVACY_AND_INTEGRITY
Definition: transport_security_interface.h:56
tsi_frame_protector
Definition: transport_security.h:51
tsi_frame_protector_destroy
void tsi_frame_protector_destroy(tsi_frame_protector *self)
Definition: transport_security.cc:135
tsi_handshaker_get_bytes_to_send_to_peer
tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self, unsigned char *bytes, size_t *bytes_size)
Definition: transport_security.cc:144
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
children
std::map< std::string, Node * > children
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/field_mask_util.cc:257
tsi_peer_destruct
void tsi_peer_destruct(tsi_peer *self)
Definition: transport_security.cc:320
tsi_construct_string_peer_property
tsi_result tsi_construct_string_peer_property(const char *name, const char *value, size_t value_length, tsi_peer_property *property)
Definition: transport_security.cc:346
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
tsi_construct_string_peer_property_from_cstring
tsi_result tsi_construct_string_peer_property_from_cstring(const char *name, const char *value, tsi_peer_property *property)
Definition: transport_security.cc:340
tsi_construct_peer
tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer)
Definition: transport_security.cc:359
tsi_peer::property_count
size_t property_count
Definition: transport_security_interface.h:240
TSI_NOT_FOUND
@ TSI_NOT_FOUND
Definition: transport_security_interface.h:41
TSI_HANDSHAKE_IN_PROGRESS
@ TSI_HANDSHAKE_IN_PROGRESS
Definition: transport_security_interface.h:43
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:40