blob: 2422f9207674cceff710b81f80757989c1ce379c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller730ddc22017-03-29 08:38:47 -070034#ifndef GRPC_CORE_TSI_TRANSPORT_SECURITY_H
35#define GRPC_CORE_TSI_TRANSPORT_SECURITY_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
jiangtaoli2016e69881d2017-04-10 14:29:43 -070037#include <stdbool.h>
38
Craig Tiller84f75d42017-05-03 13:06:35 -070039#include "src/core/lib/debug/trace.h"
Craig Tillerb29f1fe2017-03-28 15:49:23 -070040#include "src/core/tsi/transport_security_interface.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
42#ifdef __cplusplus
Craig Tillera82950e2015-09-22 12:33:20 -070043extern "C" {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#endif
45
Craig Tiller84f75d42017-05-03 13:06:35 -070046extern grpc_tracer_flag tsi_tracing_enabled;
Julien Boeuf980f6002015-02-26 16:41:41 -080047
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048/* Base for tsi_frame_protector implementations.
49 See transport_security_interface.h for documentation. */
Craig Tillera82950e2015-09-22 12:33:20 -070050typedef struct {
51 tsi_result (*protect)(tsi_frame_protector *self,
52 const unsigned char *unprotected_bytes,
53 size_t *unprotected_bytes_size,
54 unsigned char *protected_output_frames,
55 size_t *protected_output_frames_size);
56 tsi_result (*protect_flush)(tsi_frame_protector *self,
57 unsigned char *protected_output_frames,
58 size_t *protected_output_frames_size,
59 size_t *still_pending_size);
60 tsi_result (*unprotect)(tsi_frame_protector *self,
61 const unsigned char *protected_frames_bytes,
62 size_t *protected_frames_bytes_size,
63 unsigned char *unprotected_bytes,
64 size_t *unprotected_bytes_size);
65 void (*destroy)(tsi_frame_protector *self);
66} tsi_frame_protector_vtable;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
Craig Tillera82950e2015-09-22 12:33:20 -070068struct tsi_frame_protector {
69 const tsi_frame_protector_vtable *vtable;
70};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
72/* Base for tsi_handshaker implementations.
73 See transport_security_interface.h for documentation. */
Craig Tillera82950e2015-09-22 12:33:20 -070074typedef struct {
75 tsi_result (*get_bytes_to_send_to_peer)(tsi_handshaker *self,
76 unsigned char *bytes,
77 size_t *bytes_size);
78 tsi_result (*process_bytes_from_peer)(tsi_handshaker *self,
79 const unsigned char *bytes,
80 size_t *bytes_size);
81 tsi_result (*get_result)(tsi_handshaker *self);
82 tsi_result (*extract_peer)(tsi_handshaker *self, tsi_peer *peer);
83 tsi_result (*create_frame_protector)(tsi_handshaker *self,
84 size_t *max_protected_frame_size,
85 tsi_frame_protector **protector);
86 void (*destroy)(tsi_handshaker *self);
jiangtaoli201620b9f942017-04-07 12:50:33 -070087 tsi_result (*next)(tsi_handshaker *self, const unsigned char *received_bytes,
88 size_t received_bytes_size, unsigned char **bytes_to_send,
89 size_t *bytes_to_send_size,
90 tsi_handshaker_result **handshaker_result,
91 tsi_handshaker_on_next_done_cb cb, void *user_data);
Craig Tillera82950e2015-09-22 12:33:20 -070092} tsi_handshaker_vtable;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093
Craig Tillera82950e2015-09-22 12:33:20 -070094struct tsi_handshaker {
95 const tsi_handshaker_vtable *vtable;
jiangtaoli2016e69881d2017-04-10 14:29:43 -070096 bool frame_protector_created;
97 bool handshaker_result_created;
jiangtaoli201620b9f942017-04-07 12:50:33 -070098};
99
100/* Base for tsi_handshaker_result implementations.
101 See transport_security_interface.h for documentation. */
102typedef struct {
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700103 tsi_result (*extract_peer)(const tsi_handshaker_result *self, tsi_peer *peer);
104 tsi_result (*create_frame_protector)(const tsi_handshaker_result *self,
jiangtaoli201620b9f942017-04-07 12:50:33 -0700105 size_t *max_output_protected_frame_size,
106 tsi_frame_protector **protector);
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700107 tsi_result (*get_unused_bytes)(const tsi_handshaker_result *self,
jiangtaoli201620b9f942017-04-07 12:50:33 -0700108 unsigned char **bytes, size_t *bytes_size);
109 void (*destroy)(tsi_handshaker_result *self);
110} tsi_handshaker_result_vtable;
111
112struct tsi_handshaker_result {
113 const tsi_handshaker_result_vtable *vtable;
Craig Tillera82950e2015-09-22 12:33:20 -0700114};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115
116/* Peer and property construction/destruction functions. */
Craig Tillera82950e2015-09-22 12:33:20 -0700117tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer);
118tsi_peer_property tsi_init_peer_property(void);
119void tsi_peer_property_destruct(tsi_peer_property *property);
120tsi_result tsi_construct_string_peer_property(const char *name,
121 const char *value,
122 size_t value_length,
123 tsi_peer_property *property);
124tsi_result tsi_construct_allocated_string_peer_property(
125 const char *name, size_t value_length, tsi_peer_property *property);
126tsi_result tsi_construct_string_peer_property_from_cstring(
127 const char *name, const char *value, tsi_peer_property *property);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128
129/* Utils. */
Craig Tillera82950e2015-09-22 12:33:20 -0700130char *tsi_strdup(const char *src); /* Sadly, no strdup in C89. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
132#ifdef __cplusplus
133}
134#endif
135
Craig Tiller730ddc22017-03-29 08:38:47 -0700136#endif /* GRPC_CORE_TSI_TRANSPORT_SECURITY_H */