blob: d639f857fe9dec66b0b2e33fb7e0ca70a3ccb77e [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Craig Tiller730ddc22017-03-29 08:38:47 -070019#ifndef GRPC_CORE_TSI_TRANSPORT_SECURITY_H
20#define GRPC_CORE_TSI_TRANSPORT_SECURITY_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080021
jiangtaoli2016e69881d2017-04-10 14:29:43 -070022#include <stdbool.h>
23
Craig Tiller84f75d42017-05-03 13:06:35 -070024#include "src/core/lib/debug/trace.h"
Craig Tillerb29f1fe2017-03-28 15:49:23 -070025#include "src/core/tsi/transport_security_interface.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026
27#ifdef __cplusplus
Craig Tillera82950e2015-09-22 12:33:20 -070028extern "C" {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080029#endif
30
Craig Tiller84f75d42017-05-03 13:06:35 -070031extern grpc_tracer_flag tsi_tracing_enabled;
Julien Boeuf980f6002015-02-26 16:41:41 -080032
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080033/* Base for tsi_frame_protector implementations.
34 See transport_security_interface.h for documentation. */
Craig Tillera82950e2015-09-22 12:33:20 -070035typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -070036 tsi_result (*protect)(tsi_frame_protector* self,
37 const unsigned char* unprotected_bytes,
38 size_t* unprotected_bytes_size,
39 unsigned char* protected_output_frames,
40 size_t* protected_output_frames_size);
41 tsi_result (*protect_flush)(tsi_frame_protector* self,
42 unsigned char* protected_output_frames,
43 size_t* protected_output_frames_size,
44 size_t* still_pending_size);
45 tsi_result (*unprotect)(tsi_frame_protector* self,
46 const unsigned char* protected_frames_bytes,
47 size_t* protected_frames_bytes_size,
48 unsigned char* unprotected_bytes,
49 size_t* unprotected_bytes_size);
50 void (*destroy)(tsi_frame_protector* self);
Craig Tillera82950e2015-09-22 12:33:20 -070051} tsi_frame_protector_vtable;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Craig Tillera82950e2015-09-22 12:33:20 -070053struct tsi_frame_protector {
Craig Tillerbaa14a92017-11-03 09:09:36 -070054 const tsi_frame_protector_vtable* vtable;
Craig Tillera82950e2015-09-22 12:33:20 -070055};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
57/* Base for tsi_handshaker implementations.
58 See transport_security_interface.h for documentation. */
Craig Tillera82950e2015-09-22 12:33:20 -070059typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -070060 tsi_result (*get_bytes_to_send_to_peer)(tsi_handshaker* self,
61 unsigned char* bytes,
62 size_t* bytes_size);
63 tsi_result (*process_bytes_from_peer)(tsi_handshaker* self,
64 const unsigned char* bytes,
65 size_t* bytes_size);
66 tsi_result (*get_result)(tsi_handshaker* self);
67 tsi_result (*extract_peer)(tsi_handshaker* self, tsi_peer* peer);
68 tsi_result (*create_frame_protector)(tsi_handshaker* self,
69 size_t* max_protected_frame_size,
70 tsi_frame_protector** protector);
71 void (*destroy)(tsi_handshaker* self);
72 tsi_result (*next)(tsi_handshaker* self, const unsigned char* received_bytes,
jiangtaoli20162f94a262017-08-07 22:07:17 -070073 size_t received_bytes_size,
Craig Tillerbaa14a92017-11-03 09:09:36 -070074 const unsigned char** bytes_to_send,
75 size_t* bytes_to_send_size,
76 tsi_handshaker_result** handshaker_result,
77 tsi_handshaker_on_next_done_cb cb, void* user_data);
Craig Tillera82950e2015-09-22 12:33:20 -070078} tsi_handshaker_vtable;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079
Craig Tillera82950e2015-09-22 12:33:20 -070080struct tsi_handshaker {
Craig Tillerbaa14a92017-11-03 09:09:36 -070081 const tsi_handshaker_vtable* vtable;
jiangtaoli2016e69881d2017-04-10 14:29:43 -070082 bool frame_protector_created;
83 bool handshaker_result_created;
jiangtaoli201620b9f942017-04-07 12:50:33 -070084};
85
86/* Base for tsi_handshaker_result implementations.
jiangtaoli2016f68978c2017-09-14 09:18:24 -070087 See transport_security_interface.h for documentation.
88 The exec_ctx parameter in create_zero_copy_grpc_protector is supposed to be
89 of type grpc_exec_ctx*, but we're using void* instead to avoid making the TSI
90 API depend on grpc. The create_zero_copy_grpc_protector() method is only used
91 in grpc, where we do need the exec_ctx passed through, but the API still
92 needs to compile in other applications, where grpc_exec_ctx is not defined.
93*/
jiangtaoli201620b9f942017-04-07 12:50:33 -070094typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -070095 tsi_result (*extract_peer)(const tsi_handshaker_result* self, tsi_peer* peer);
Jiangtao Li0211cfb2017-08-07 11:24:07 -070096 tsi_result (*create_zero_copy_grpc_protector)(
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 void* exec_ctx, const tsi_handshaker_result* self,
98 size_t* max_output_protected_frame_size,
99 tsi_zero_copy_grpc_protector** protector);
100 tsi_result (*create_frame_protector)(const tsi_handshaker_result* self,
101 size_t* max_output_protected_frame_size,
102 tsi_frame_protector** protector);
103 tsi_result (*get_unused_bytes)(const tsi_handshaker_result* self,
104 const unsigned char** bytes,
105 size_t* bytes_size);
106 void (*destroy)(tsi_handshaker_result* self);
jiangtaoli201620b9f942017-04-07 12:50:33 -0700107} tsi_handshaker_result_vtable;
108
109struct tsi_handshaker_result {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700110 const tsi_handshaker_result_vtable* vtable;
Craig Tillera82950e2015-09-22 12:33:20 -0700111};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112
113/* Peer and property construction/destruction functions. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700114tsi_result tsi_construct_peer(size_t property_count, tsi_peer* peer);
Craig Tillera82950e2015-09-22 12:33:20 -0700115tsi_peer_property tsi_init_peer_property(void);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700116void tsi_peer_property_destruct(tsi_peer_property* property);
117tsi_result tsi_construct_string_peer_property(const char* name,
118 const char* value,
Craig Tillera82950e2015-09-22 12:33:20 -0700119 size_t value_length,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700120 tsi_peer_property* property);
Craig Tillera82950e2015-09-22 12:33:20 -0700121tsi_result tsi_construct_allocated_string_peer_property(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700122 const char* name, size_t value_length, tsi_peer_property* property);
Craig Tillera82950e2015-09-22 12:33:20 -0700123tsi_result tsi_construct_string_peer_property_from_cstring(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700124 const char* name, const char* value, tsi_peer_property* property);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125
126/* Utils. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700127char* tsi_strdup(const char* src); /* Sadly, no strdup in C89. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128
129#ifdef __cplusplus
130}
131#endif
132
Craig Tiller730ddc22017-03-29 08:38:47 -0700133#endif /* GRPC_CORE_TSI_TRANSPORT_SECURITY_H */