Home | History | Annotate | Download | only in handshaker
      1 /*
      2  *
      3  * Copyright 2018 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 
     19 #ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_UTIL_H
     20 #define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_UTIL_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include "pb_decode.h"
     25 #include "pb_encode.h"
     26 
     27 #include <grpc/slice.h>
     28 #include <grpc/slice_buffer.h>
     29 #include <grpc/support/alloc.h>
     30 #include <grpc/support/log.h>
     31 
     32 #include "src/core/tsi/alts/handshaker/handshaker.pb.h"
     33 
     34 /**
     35  * An implementation of utility functions used to serialize/
     36  * de-serialize ALTS handshake requests/responses. All APIs in the header
     37  * are thread-compatible.
     38  */
     39 
     40 /* Renaming of message/field structs generated by nanopb compiler. */
     41 typedef grpc_gcp_HandshakeProtocol grpc_gcp_handshake_protocol;
     42 typedef grpc_gcp_NetworkProtocol grpc_gcp_network_protocol;
     43 typedef grpc_gcp_Identity grpc_gcp_identity;
     44 typedef grpc_gcp_NextHandshakeMessageReq grpc_gcp_next_handshake_message_req;
     45 typedef grpc_gcp_ServerHandshakeParameters grpc_gcp_server_handshake_parameters;
     46 typedef grpc_gcp_Endpoint grpc_gcp_endpoint;
     47 typedef grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry
     48     grpc_gcp_handshake_parameters_entry;
     49 typedef grpc_gcp_StartClientHandshakeReq grpc_gcp_start_client_handshake_req;
     50 typedef grpc_gcp_StartServerHandshakeReq grpc_gcp_start_server_handshake_req;
     51 typedef grpc_gcp_HandshakerReq grpc_gcp_handshaker_req;
     52 typedef grpc_gcp_HandshakerResult grpc_gcp_handshaker_result;
     53 typedef grpc_gcp_HandshakerStatus grpc_gcp_handshaker_status;
     54 typedef grpc_gcp_HandshakerResp grpc_gcp_handshaker_resp;
     55 
     56 typedef enum {
     57   CLIENT_START_REQ = 0, /* StartClientHandshakeReq. */
     58   SERVER_START_REQ = 1, /* StartServerHandshakeReq. */
     59   NEXT_REQ = 2,         /* NextHandshakeMessageReq. */
     60 } grpc_gcp_handshaker_req_type;
     61 
     62 /**
     63  *  A struct representing a repeated field. The struct is used to organize all
     64  *  instances of a specific repeated field into a linked list, which then will
     65  *  be used at encode/decode phase. For instance at the encode phase, the encode
     66  *  function will iterate through the list, encode each field, and then output
     67  *  the result to the stream.
     68  */
     69 typedef struct repeated_field_ {
     70   struct repeated_field_* next;
     71   const void* data;
     72 } repeated_field;
     73 
     74 /**
     75  * This method adds a repeated field to the head of repeated field list.
     76  *
     77  * - head: a head of repeated field list.
     78  * - field: a repeated field to be added to the list.
     79  */
     80 void add_repeated_field(repeated_field** head, const void* field);
     81 
     82 /**
     83  * This method destroys a repeated field list that consists of string type
     84  * fields.
     85  *
     86  * - head: a head of repeated field list.
     87  */
     88 void destroy_repeated_field_list_string(repeated_field* head);
     89 
     90 /**
     91  * This method destroys a repeated field list that consists of
     92  * grpc_gcp_identity type fields.
     93  *
     94  * - head: a head of repeated field list.
     95  */
     96 void destroy_repeated_field_list_identity(repeated_field* head);
     97 
     98 /**
     99  * This method creates a grpc_slice instance by copying a data buffer. It is
    100  * similar to grpc_slice_from_copied_buffer() except that it returns an instance
    101  * allocated from the heap.
    102  *
    103  * - data: a data buffer to be copied to grpc_slice instance.
    104  * - size: size of data buffer.
    105  */
    106 grpc_slice* create_slice(const char* data, size_t size);
    107 
    108 /* This method destroys a grpc_slice instance. */
    109 void destroy_slice(grpc_slice* slice);
    110 
    111 /**
    112  * The following encode/decode functions will be assigned to encode/decode
    113  * function pointers of pb_callback_t struct (defined in
    114  * //third_party/nanopb/pb.h), that represent a repeated field with a dynamic
    115  * length (e.g., a string type or repeated field).
    116  */
    117 
    118 /* This method is an encode callback function for a string or byte array. */
    119 bool encode_string_or_bytes_cb(pb_ostream_t* stream, const pb_field_t* field,
    120                                void* const* arg);
    121 
    122 /**
    123  * This method is an encode callback function for a repeated grpc_gcp_identity
    124  * field.
    125  */
    126 bool encode_repeated_identity_cb(pb_ostream_t* stream, const pb_field_t* field,
    127                                  void* const* arg);
    128 
    129 /* This method is an encode callback function for a repeated string field. */
    130 bool encode_repeated_string_cb(pb_ostream_t* stream, const pb_field_t* field,
    131                                void* const* arg);
    132 
    133 /**
    134  * This method is a decode callback function for a string or byte array field.
    135  */
    136 bool decode_string_or_bytes_cb(pb_istream_t* stream, const pb_field_t* field,
    137                                void** arg);
    138 /**
    139  * This method is a decode callback function for a repeated grpc_gcp_identity
    140  * field.
    141  */
    142 bool decode_repeated_identity_cb(pb_istream_t* stream, const pb_field_t* field,
    143                                  void** arg);
    144 
    145 /* This method is a decode callback function for a repeated string field. */
    146 bool decode_repeated_string_cb(pb_istream_t* stream, const pb_field_t* field,
    147                                void** arg);
    148 
    149 #endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_UTIL_H */
    150