Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Tiller | 730ddc2 | 2017-03-29 08:38:47 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H |
| 35 | #define GRPC_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <stdint.h> |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 38 | #include <stdlib.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 40 | #include "src/core/lib/debug/trace.h" |
| 41 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #ifdef __cplusplus |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 43 | extern "C" { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | /* --- tsi result --- */ |
| 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | typedef 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, |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 61 | TSI_OUT_OF_RESOURCES = 12, |
| 62 | TSI_ASYNC = 13 |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | } tsi_result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 65 | typedef 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | const char *tsi_result_to_string(tsi_result result); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | |
Julien Boeuf | 980f600 | 2015-02-26 16:41:41 -0800 | [diff] [blame] | 76 | /* --- tsi tracing --- */ |
| 77 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 78 | extern grpc_tracer_flag tsi_tracing_enabled; |
Julien Boeuf | 980f600 | 2015-02-26 16:41:41 -0800 | [diff] [blame] | 79 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | /* --- 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | typedef struct tsi_frame_protector tsi_frame_protector; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | |
| 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 Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 109 | size_t protected_buffer_size = sizeof(protected_buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | tsi_result result = TSI_OK; |
| 111 | while (message_size > 0) { |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 112 | size_t protected_buffer_size_to_send = protected_buffer_size; |
| 113 | size_t processed_message_size = message_size; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | 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 Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 126 | size_t still_pending_size; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | tsi_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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | |
| 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | tsi_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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | |
| 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 Boeuf | f8b4b98 | 2015-08-10 12:55:58 -0700 | [diff] [blame] | 172 | If *unprotected_bytes_size is unchanged, there may be more data remaining |
| 173 | to unprotect, and the caller should call this function again. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | |
| 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | tsi_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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 184 | |
| 185 | /* Destroys the tsi_frame_protector object. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | void tsi_frame_protector_destroy(tsi_frame_protector *self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | /* --- 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 Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 195 | /* Property values may contain NULL characters just like C++ strings. |
| 196 | The length field gives the length of the string. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 197 | typedef struct tsi_peer_property { |
| 198 | char *name; |
| 199 | struct { |
| 200 | char *data; |
| 201 | size_t length; |
| 202 | } value; |
| 203 | } tsi_peer_property; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | typedef struct { |
| 206 | tsi_peer_property *properties; |
| 207 | size_t property_count; |
| 208 | } tsi_peer; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 210 | /* Destructs the tsi_peer object. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | void tsi_peer_destruct(tsi_peer *self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 213 | /* --- 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 | |
| 219 | typedef 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. */ |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 224 | tsi_result tsi_handshaker_result_extract_peer(const tsi_handshaker_result *self, |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 225 | 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. */ |
| 230 | tsi_result tsi_handshaker_result_create_frame_protector( |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 231 | const tsi_handshaker_result *self, size_t *max_output_protected_frame_size, |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 232 | 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. |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 236 | Ownership of the bytes is retained by the handshaker result. As a |
| 237 | consequence, the caller must not free the bytes. */ |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 238 | tsi_result tsi_handshaker_result_get_unused_bytes( |
| 239 | const tsi_handshaker_result *self, unsigned char **bytes, |
| 240 | size_t *byte_size); |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 241 | |
| 242 | /* This method releases the tsi_handshaker_handshaker object. After this method |
| 243 | is called, no other method can be called on the object. */ |
| 244 | void tsi_handshaker_result_destroy(tsi_handshaker_result *self); |
| 245 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | /* --- tsi_handshaker objects ---- |
| 247 | |
| 248 | Implementations of this object must be thread compatible. |
| 249 | |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 250 | ------------------------------------------------------------------------ |
| 251 | |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 252 | A typical usage supporting both synchronous and asynchronous TSI handshaker |
| 253 | implementations would be: |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 254 | |
| 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 | |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 271 | // 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 |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 273 | // 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, |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 291 | &bytes_to_send_size, &result, on_handshake_next_done, h); |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 292 | // If TSI handshaker is asynchronous, on_handshake_next_done will be |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 293 | // executed inside tsi_handshaker_next. |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 294 | if (status == TSI_ASYNC) return; |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 295 | // If TSI handshaker is synchronous, invoke callback directly in this |
| 296 | // thread. |
| 297 | on_handshake_next_done(status, (void *)h, bytes_to_send, |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 298 | bytes_to_send_size, result); |
| 299 | } |
| 300 | |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 301 | // This is the callback function to execute after tsi_handshaker_next. |
| 302 | // It is passed to tsi_handshaker_next as a function parameter. |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 303 | void on_handshake_next_done( |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 304 | 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; |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 307 | 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); |
jiangtaoli2016 | e69881d | 2017-04-10 14:29:43 -0700 | [diff] [blame] | 325 | if (status != TSI_OK) return; |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 326 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 340 | ------------------------------------------------------------------------ */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 341 | typedef struct tsi_handshaker tsi_handshaker; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 342 | |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 343 | /* TO BE DEPRECATED SOON. Use tsi_handshaker_next instead. |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 344 | Gets bytes that need to be sent to the peer. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 345 | - 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 355 | tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self, |
| 356 | unsigned char *bytes, |
| 357 | size_t *bytes_size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 358 | |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 359 | /* TO BE DEPRECATED SOON. Use tsi_handshaker_next instead. |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 360 | Processes bytes received from the peer. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 361 | - 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 369 | tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self, |
| 370 | const unsigned char *bytes, |
| 371 | size_t *bytes_size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 372 | |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 373 | /* TO BE DEPRECATED SOON. |
| 374 | Gets the result of the handshaker. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 375 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | tsi_result tsi_handshaker_get_result(tsi_handshaker *self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 380 | |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 381 | /* TO BE DEPRECATED SOON. |
| 382 | Returns 1 if the handshake is in progress, 0 otherwise. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 383 | #define tsi_handshaker_is_in_progress(h) \ |
| 384 | (tsi_handshaker_get_result((h)) == TSI_HANDSHAKE_IN_PROGRESS) |
| 385 | |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 386 | /* TO BE DEPRECATED SOON. Use tsi_handshaker_result_extract_peer instead. |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 387 | This method may return TSI_FAILED_PRECONDITION if |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 388 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 391 | tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 392 | |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 393 | /* TO BE DEPRECATED SOON. Use tsi_handshaker_result_create_frame_protector |
| 394 | instead. |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 395 | This method creates a tsi_frame_protector object after the handshake phase |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 396 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | tsi_result tsi_handshaker_create_frame_protector( |
| 412 | tsi_handshaker *self, size_t *max_output_protected_frame_size, |
| 413 | tsi_frame_protector **protector); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 414 | |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 415 | /* 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. */ |
| 422 | typedef 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 |
jiangtaoli2016 | c195502 | 2017-04-11 09:36:19 -0700 | [diff] [blame] | 436 | 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 |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 444 | tsi_handshaker object. */ |
| 445 | tsi_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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 451 | /* This method releases the tsi_handshaker object. After this method is called, |
| 452 | no other method can be called on the object. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 453 | void tsi_handshaker_destroy(tsi_handshaker *self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 454 | |
jiangtaoli2016 | 20b9f94 | 2017-04-07 12:50:33 -0700 | [diff] [blame] | 455 | /* This method initializes the necessary shared objects used for tsi |
| 456 | implementation. */ |
| 457 | void tsi_init(); |
| 458 | |
| 459 | /* This method destroys the shared objects created by tsi_init. */ |
| 460 | void tsi_destroy(); |
| 461 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 462 | #ifdef __cplusplus |
| 463 | } |
| 464 | #endif |
| 465 | |
Craig Tiller | 730ddc2 | 2017-03-29 08:38:47 -0700 | [diff] [blame] | 466 | #endif /* GRPC_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H */ |