blob: 8a3fff6a17064bebc6fff681d4605ca25ef72a21 [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_INTERFACE_H
35#define GRPC_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <stdint.h>
Julien Boeufb222b4d2015-01-15 17:01:39 -080038#include <stdlib.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Craig Tiller84f75d42017-05-03 13:06:35 -070040#include "src/core/lib/debug/trace.h"
41
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#ifdef __cplusplus
Craig Tillera82950e2015-09-22 12:33:20 -070043extern "C" {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#endif
45
46/* --- tsi result --- */
47
Craig Tillera82950e2015-09-22 12:33:20 -070048typedef enum {
49 TSI_OK = 0,
50 TSI_UNKNOWN_ERROR = 1,
51 TSI_INVALID_ARGUMENT = 2,
52 TSI_PERMISSION_DENIED = 3,
53 TSI_INCOMPLETE_DATA = 4,
54 TSI_FAILED_PRECONDITION = 5,
55 TSI_UNIMPLEMENTED = 6,
56 TSI_INTERNAL_ERROR = 7,
57 TSI_DATA_CORRUPTED = 8,
58 TSI_NOT_FOUND = 9,
59 TSI_PROTOCOL_FAILURE = 10,
60 TSI_HANDSHAKE_IN_PROGRESS = 11,
jiangtaoli201620b9f942017-04-07 12:50:33 -070061 TSI_OUT_OF_RESOURCES = 12,
62 TSI_ASYNC = 13
Craig Tillera82950e2015-09-22 12:33:20 -070063} tsi_result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064
Deepak Lukosedba4c5f2016-03-25 12:54:25 -070065typedef enum {
66 // Default option
67 TSI_DONT_REQUEST_CLIENT_CERTIFICATE,
68 TSI_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY,
69 TSI_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY,
70 TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY,
71 TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY,
72} tsi_client_certificate_request_type;
73
Craig Tillera82950e2015-09-22 12:33:20 -070074const char *tsi_result_to_string(tsi_result result);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075
Julien Boeuf980f6002015-02-26 16:41:41 -080076/* --- tsi tracing --- */
77
Craig Tiller84f75d42017-05-03 13:06:35 -070078extern grpc_tracer_flag tsi_tracing_enabled;
Julien Boeuf980f6002015-02-26 16:41:41 -080079
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080/* --- tsi_frame_protector object ---
81
82 This object protects and unprotects buffers once the handshake is done.
83 Implementations of this object must be thread compatible. */
84
Craig Tillera82950e2015-09-22 12:33:20 -070085typedef struct tsi_frame_protector tsi_frame_protector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086
87/* Outputs protected frames.
88 - unprotected_bytes is an input only parameter and points to the data
89 to be protected.
90 - unprotected_bytes_size is an input/output parameter used by the caller to
91 specify how many bytes are available in unprotected_bytes. The output
92 value is the number of bytes consumed during the call.
93 - protected_output_frames points to a buffer allocated by the caller that
94 will be written.
95 - protected_output_frames_size is an input/output parameter used by the
96 caller to specify how many bytes are available in protected_output_frames.
97 As an output, this value indicates the number of bytes written.
98 - This method returns TSI_OK in case of success or a specific error code in
99 case of failure. Note that even if all the input unprotected bytes are
100 consumed, they may not have been processed into the returned protected
101 output frames. The caller should call the protect_flush method
102 to make sure that there are no more protected bytes buffered in the
103 protector.
104
105 A typical way to call this method would be:
106
107 ------------------------------------------------------------------------
108 unsigned char protected_buffer[4096];
Julien Boeufb222b4d2015-01-15 17:01:39 -0800109 size_t protected_buffer_size = sizeof(protected_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 tsi_result result = TSI_OK;
111 while (message_size > 0) {
Julien Boeufb222b4d2015-01-15 17:01:39 -0800112 size_t protected_buffer_size_to_send = protected_buffer_size;
113 size_t processed_message_size = message_size;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 result = tsi_frame_protector_protect(protector,
115 message_bytes,
116 &processed_message_size,
117 protected_buffer,
118 &protected_buffer_size_to_send);
119 if (result != TSI_OK) break;
120 send_bytes_to_peer(protected_buffer, protected_buffer_size_to_send);
121 message_bytes += processed_message_size;
122 message_size -= processed_message_size;
123
124 // Don't forget to flush.
125 if (message_size == 0) {
Julien Boeufb222b4d2015-01-15 17:01:39 -0800126 size_t still_pending_size;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127 do {
128 protected_buffer_size_to_send = protected_buffer_size;
129 result = tsi_frame_protector_protect_flush(
130 protector, protected_buffer,
131 &protected_buffer_size_to_send, &still_pending_size);
132 if (result != TSI_OK) break;
133 send_bytes_to_peer(protected_buffer, protected_buffer_size_to_send);
134 } while (still_pending_size > 0);
135 }
136 }
137
138 if (result != TSI_OK) HandleError(result);
139 ------------------------------------------------------------------------ */
Craig Tillera82950e2015-09-22 12:33:20 -0700140tsi_result tsi_frame_protector_protect(tsi_frame_protector *self,
141 const unsigned char *unprotected_bytes,
142 size_t *unprotected_bytes_size,
143 unsigned char *protected_output_frames,
144 size_t *protected_output_frames_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145
146/* Indicates that we need to flush the bytes buffered in the protector and get
147 the resulting frame.
148 - protected_output_frames points to a buffer allocated by the caller that
149 will be written.
150 - protected_output_frames_size is an input/output parameter used by the
151 caller to specify how many bytes are available in protected_output_frames.
152 - still_pending_bytes is an output parameter indicating the number of bytes
153 that still need to be flushed from the protector.*/
Craig Tillera82950e2015-09-22 12:33:20 -0700154tsi_result tsi_frame_protector_protect_flush(
155 tsi_frame_protector *self, unsigned char *protected_output_frames,
156 size_t *protected_output_frames_size, size_t *still_pending_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157
158/* Outputs unprotected bytes.
159 - protected_frames_bytes is an input only parameter and points to the
160 protected frames to be unprotected.
161 - protected_frames_bytes_size is an input/output only parameter used by the
162 caller to specify how many bytes are available in protected_bytes. The
163 output value is the number of bytes consumed during the call.
164 Implementations will buffer up to a frame of protected data.
165 - unprotected_bytes points to a buffer allocated by the caller that will be
166 written.
167 - unprotected_bytes_size is an input/output parameter used by the caller to
168 specify how many bytes are available in unprotected_bytes. This
169 value is expected to be at most max_protected_frame_size minus overhead
170 which means that max_protected_frame_size is a safe bet. The output value
171 is the number of bytes actually written.
Julien Boeuff8b4b982015-08-10 12:55:58 -0700172 If *unprotected_bytes_size is unchanged, there may be more data remaining
173 to unprotect, and the caller should call this function again.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174
175 - This method returns TSI_OK in case of success. Success includes cases where
176 there is not enough data to output a frame in which case
177 unprotected_bytes_size will be set to 0 and cases where the internal buffer
178 needs to be read before new protected data can be processed in which case
179 protected_frames_size will be set to 0. */
Craig Tillera82950e2015-09-22 12:33:20 -0700180tsi_result tsi_frame_protector_unprotect(
181 tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
182 size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
183 size_t *unprotected_bytes_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184
185/* Destroys the tsi_frame_protector object. */
Craig Tillera82950e2015-09-22 12:33:20 -0700186void tsi_frame_protector_destroy(tsi_frame_protector *self);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188/* --- tsi_peer objects ---
189
190 tsi_peer objects are a set of properties. The peer owns the properties. */
191
192/* This property is of type TSI_PEER_PROPERTY_STRING. */
193#define TSI_CERTIFICATE_TYPE_PEER_PROPERTY "certificate_type"
194
Julien Boeuf77e8c1c2015-05-13 13:50:59 -0700195/* Property values may contain NULL characters just like C++ strings.
196 The length field gives the length of the string. */
Craig Tillera82950e2015-09-22 12:33:20 -0700197typedef struct tsi_peer_property {
198 char *name;
199 struct {
200 char *data;
201 size_t length;
202 } value;
203} tsi_peer_property;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204
Craig Tillera82950e2015-09-22 12:33:20 -0700205typedef struct {
206 tsi_peer_property *properties;
207 size_t property_count;
208} tsi_peer;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210/* Destructs the tsi_peer object. */
Craig Tillera82950e2015-09-22 12:33:20 -0700211void tsi_peer_destruct(tsi_peer *self);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212
jiangtaoli201620b9f942017-04-07 12:50:33 -0700213/* --- tsi_handshaker_result object ---
214
215 This object contains all necessary handshake results and data such as peer
216 info, negotiated keys, unused handshake bytes, when the handshake completes.
217 Implementations of this object must be thread compatible. */
218
219typedef struct tsi_handshaker_result tsi_handshaker_result;
220
221/* This method extracts tsi peer. It returns TSI_OK assuming there is no fatal
222 error.
223 The caller is responsible for destructing the peer. */
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700224tsi_result tsi_handshaker_result_extract_peer(const tsi_handshaker_result *self,
jiangtaoli201620b9f942017-04-07 12:50:33 -0700225 tsi_peer *peer);
226
227/* This method creates a tsi_frame_protector object. It returns TSI_OK assuming
228 there is no fatal error.
229 The caller is responsible for destroying the protector. */
230tsi_result tsi_handshaker_result_create_frame_protector(
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700231 const tsi_handshaker_result *self, size_t *max_output_protected_frame_size,
jiangtaoli201620b9f942017-04-07 12:50:33 -0700232 tsi_frame_protector **protector);
233
234/* This method returns the unused bytes from the handshake. It returns TSI_OK
235 assuming there is no fatal error.
jiangtaoli2016c1955022017-04-11 09:36:19 -0700236 Ownership of the bytes is retained by the handshaker result. As a
237 consequence, the caller must not free the bytes. */
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700238tsi_result tsi_handshaker_result_get_unused_bytes(
239 const tsi_handshaker_result *self, unsigned char **bytes,
240 size_t *byte_size);
jiangtaoli201620b9f942017-04-07 12:50:33 -0700241
242/* This method releases the tsi_handshaker_handshaker object. After this method
243 is called, no other method can be called on the object. */
244void tsi_handshaker_result_destroy(tsi_handshaker_result *self);
245
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246/* --- tsi_handshaker objects ----
247
248 Implementations of this object must be thread compatible.
249
jiangtaoli201620b9f942017-04-07 12:50:33 -0700250 ------------------------------------------------------------------------
251
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700252 A typical usage supporting both synchronous and asynchronous TSI handshaker
253 implementations would be:
jiangtaoli201620b9f942017-04-07 12:50:33 -0700254
255 ------------------------------------------------------------------------
256
257 typedef struct {
258 tsi_handshaker *handshaker;
259 tsi_handshaker_result *handshaker_result;
260 unsigned char *handshake_buffer;
261 size_t handshake_buffer_size;
262 ...
263 } security_handshaker;
264
265 void do_handshake(security_handshaker *h, ...) {
266 // Start the handshake by the calling do_handshake_next.
267 do_handshake_next(h, NULL, 0);
268 ...
269 }
270
jiangtaoli2016c1955022017-04-11 09:36:19 -0700271 // This method is the callback function when data is received from the
272 // peer. This method will read bytes into the handshake buffer and call
jiangtaoli201620b9f942017-04-07 12:50:33 -0700273 // do_handshake_next.
274 void on_handshake_data_received_from_peer(void *user_data) {
275 security_handshaker *h = (security_handshaker *)user_data;
276 size_t bytes_received_size = h->handshake_buffer_size;
277 read_bytes_from_peer(h->handshake_buffer, &bytes_received_size);
278 do_handshake_next(h, h->handshake_buffer, bytes_received_size);
279 }
280
281 // This method processes a step of handshake, calling tsi_handshaker_next.
282 void do_handshake_next(security_handshaker *h,
283 const unsigned char* bytes_received,
284 size_t bytes_received_size) {
285 tsi_result status = TSI_OK;
286 unsigned char *bytes_to_send = NULL;
287 size_t bytes_to_send_size = 0;
288 tsi_handshaker_result *result = NULL;
289 status = tsi_handshaker_next(
290 handshaker, bytes_received, bytes_received_size, &bytes_to_send,
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700291 &bytes_to_send_size, &result, on_handshake_next_done, h);
jiangtaoli201620b9f942017-04-07 12:50:33 -0700292 // If TSI handshaker is asynchronous, on_handshake_next_done will be
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700293 // executed inside tsi_handshaker_next.
jiangtaoli201620b9f942017-04-07 12:50:33 -0700294 if (status == TSI_ASYNC) return;
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700295 // If TSI handshaker is synchronous, invoke callback directly in this
296 // thread.
297 on_handshake_next_done(status, (void *)h, bytes_to_send,
jiangtaoli201620b9f942017-04-07 12:50:33 -0700298 bytes_to_send_size, result);
299 }
300
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700301 // This is the callback function to execute after tsi_handshaker_next.
302 // It is passed to tsi_handshaker_next as a function parameter.
jiangtaoli201620b9f942017-04-07 12:50:33 -0700303 void on_handshake_next_done(
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700304 tsi_result status, void *user_data, const unsigned char *bytes_to_send,
305 size_t bytes_to_send_size, tsi_handshaker_result *result) {
306 security_handshaker *h = (security_handshaker *)user_data;
jiangtaoli201620b9f942017-04-07 12:50:33 -0700307 if (status == TSI_INCOMPLETE_DATA) {
308 // Schedule an asynchronous read from the peer. If handshake data are
309 // received, on_handshake_data_received_from_peer will be called.
310 async_read_from_peer(..., ..., on_handshake_data_received_from_peer);
311 return;
312 }
313 if (status != TSI_OK) return;
314
315 if (bytes_to_send_size > 0) {
316 send_bytes_to_peer(bytes_to_send, bytes_to_send_size);
317 }
318
319 if (result != NULL) {
320 // Handshake completed.
321 h->result = result;
322 // Check the Peer.
323 tsi_peer peer;
324 status = tsi_handshaker_result_extract_peer(result, &peer);
jiangtaoli2016e69881d2017-04-10 14:29:43 -0700325 if (status != TSI_OK) return;
jiangtaoli201620b9f942017-04-07 12:50:33 -0700326 status = check_peer(&peer);
327 tsi_peer_destruct(&peer);
328 if (status != TSI_OK) return;
329
330 // Create the protector.
331 tsi_frame_protector* protector = NULL;
332 status = tsi_handshaker_result_create_frame_protector(result, NULL,
333 &protector);
334 if (status != TSI_OK) return;
335
336 // Do not forget to unprotect outstanding data if any.
337 ....
338 }
339 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800340 ------------------------------------------------------------------------ */
Craig Tillera82950e2015-09-22 12:33:20 -0700341typedef struct tsi_handshaker tsi_handshaker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800342
jiangtaoli2016c1955022017-04-11 09:36:19 -0700343/* TO BE DEPRECATED SOON. Use tsi_handshaker_next instead.
jiangtaoli201620b9f942017-04-07 12:50:33 -0700344 Gets bytes that need to be sent to the peer.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800345 - bytes is the buffer that will be written with the data to be sent to the
346 peer.
347 - bytes_size is an input/output parameter specifying the capacity of the
348 bytes parameter as input and the number of bytes written as output.
349 Returns TSI_OK if all the data to send to the peer has been written or if
350 nothing has to be sent to the peer (in which base bytes_size outputs to 0),
351 otherwise returns TSI_INCOMPLETE_DATA which indicates that this method
352 needs to be called again to get all the bytes to send to the peer (there
353 was more data to write than the specified bytes_size). In case of a fatal
354 error in the handshake, another specific error code is returned. */
Craig Tillera82950e2015-09-22 12:33:20 -0700355tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
356 unsigned char *bytes,
357 size_t *bytes_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800358
jiangtaoli2016c1955022017-04-11 09:36:19 -0700359/* TO BE DEPRECATED SOON. Use tsi_handshaker_next instead.
jiangtaoli201620b9f942017-04-07 12:50:33 -0700360 Processes bytes received from the peer.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800361 - bytes is the buffer containing the data.
362 - bytes_size is an input/output parameter specifying the size of the data as
363 input and the number of bytes consumed as output.
364 Return TSI_OK if the handshake has all the data it needs to process,
365 otherwise return TSI_INCOMPLETE_DATA which indicates that this method
366 needs to be called again to complete the data needed for processing. In
367 case of a fatal error in the handshake, another specific error code is
368 returned. */
Craig Tillera82950e2015-09-22 12:33:20 -0700369tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self,
370 const unsigned char *bytes,
371 size_t *bytes_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372
jiangtaoli201620b9f942017-04-07 12:50:33 -0700373/* TO BE DEPRECATED SOON.
374 Gets the result of the handshaker.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800375 Returns TSI_OK if the hanshake completed successfully and there has been no
376 errors. Returns TSI_HANDSHAKE_IN_PROGRESS if the handshaker is not done yet
377 but no error has been encountered so far. Otherwise the handshaker failed
378 with the returned error. */
Craig Tillera82950e2015-09-22 12:33:20 -0700379tsi_result tsi_handshaker_get_result(tsi_handshaker *self);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800380
jiangtaoli201620b9f942017-04-07 12:50:33 -0700381/* TO BE DEPRECATED SOON.
382 Returns 1 if the handshake is in progress, 0 otherwise. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800383#define tsi_handshaker_is_in_progress(h) \
384 (tsi_handshaker_get_result((h)) == TSI_HANDSHAKE_IN_PROGRESS)
385
jiangtaoli2016c1955022017-04-11 09:36:19 -0700386/* TO BE DEPRECATED SOON. Use tsi_handshaker_result_extract_peer instead.
jiangtaoli201620b9f942017-04-07 12:50:33 -0700387 This method may return TSI_FAILED_PRECONDITION if
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800388 tsi_handshaker_is_in_progress returns 1, it returns TSI_OK otherwise
389 assuming the handshaker is not in a fatal error state.
390 The caller is responsible for destructing the peer. */
Craig Tillera82950e2015-09-22 12:33:20 -0700391tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800392
jiangtaoli2016c1955022017-04-11 09:36:19 -0700393/* TO BE DEPRECATED SOON. Use tsi_handshaker_result_create_frame_protector
394 instead.
jiangtaoli201620b9f942017-04-07 12:50:33 -0700395 This method creates a tsi_frame_protector object after the handshake phase
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800396 is done. After this method has been called successfully, the only method
397 that can be called on this object is Destroy.
398 - max_output_protected_frame_size is an input/output parameter specifying the
399 desired max output protected frame size as input and outputing the actual
400 max output frame size as the output. Passing NULL is OK and will result in
401 the implementation choosing the default maximum protected frame size. Note
402 that this size only applies to outgoing frames (generated with
403 tsi_frame_protector_protect) and not incoming frames (input of
404 tsi_frame_protector_unprotect).
405 - protector is an output parameter pointing to the newly created
406 tsi_frame_protector object.
407 This method may return TSI_FAILED_PRECONDITION if
408 tsi_handshaker_is_in_progress returns 1, it returns TSI_OK otherwise assuming
409 the handshaker is not in a fatal error state.
410 The caller is responsible for destroying the protector. */
Craig Tillera82950e2015-09-22 12:33:20 -0700411tsi_result tsi_handshaker_create_frame_protector(
412 tsi_handshaker *self, size_t *max_output_protected_frame_size,
413 tsi_frame_protector **protector);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414
jiangtaoli201620b9f942017-04-07 12:50:33 -0700415/* Callback function definition for tsi_handshaker_next.
416 - status indicates the status of the next operation.
417 - user_data is the argument to callback function passed from the caller.
418 - bytes_to_send is the data buffer to be sent to the peer.
419 - bytes_to_send_size is the size of data buffer to be sent to the peer.
420 - handshaker_result is the result of handshake when the handshake completes,
421 is NULL otherwise. */
422typedef void (*tsi_handshaker_on_next_done_cb)(
423 tsi_result status, void *user_data, const unsigned char *bytes_to_send,
424 size_t bytes_to_send_size, tsi_handshaker_result *handshaker_result);
425
426/* Conduct a next step of the handshake.
427 - received_bytes is the buffer containing the data received from the peer.
428 - received_bytes_size is the size of the data received from the peer.
429 - bytes_to_send is the data buffer to be sent to the peer.
430 - bytes_to_send_size is the size of data buffer to be sent to the peer.
431 - handshaker_result is the result of handshake if the handshake completes.
432 - cb is the callback function defined above. It can be NULL for synchronous
433 TSI handshaker implementation.
434 - user_data is the argument to callback function passed from the caller.
435 This method returns TSI_ASYNC if the TSI handshaker implementation is
jiangtaoli2016c1955022017-04-11 09:36:19 -0700436 asynchronous, and in this case, the callback is guaranteed to run in another
437 thread owned by TSI. It returns TSI_OK if the handshake completes or if
438 there are data to send to the peer, otherwise returns TSI_INCOMPLETE_DATA
439 which indicates that this method needs to be called again with more data
440 from the peer. In case of a fatal error in the handshake, another specific
441 error code is returned.
442 The caller is responsible for destroying the handshaker_result. However,
443 the caller should not free bytes_to_send, as the buffer is owned by the
jiangtaoli201620b9f942017-04-07 12:50:33 -0700444 tsi_handshaker object. */
445tsi_result tsi_handshaker_next(
446 tsi_handshaker *self, const unsigned char *received_bytes,
447 size_t received_bytes_size, unsigned char **bytes_to_send,
448 size_t *bytes_to_send_size, tsi_handshaker_result **handshaker_result,
449 tsi_handshaker_on_next_done_cb cb, void *user_data);
450
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800451/* This method releases the tsi_handshaker object. After this method is called,
452 no other method can be called on the object. */
Craig Tillera82950e2015-09-22 12:33:20 -0700453void tsi_handshaker_destroy(tsi_handshaker *self);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800454
jiangtaoli201620b9f942017-04-07 12:50:33 -0700455/* This method initializes the necessary shared objects used for tsi
456 implementation. */
457void tsi_init();
458
459/* This method destroys the shared objects created by tsi_init. */
460void tsi_destroy();
461
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800462#ifdef __cplusplus
463}
464#endif
465
Craig Tiller730ddc22017-03-29 08:38:47 -0700466#endif /* GRPC_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H */