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_TRANSPORT_SECURITY_COMMON_API_H
20 #define GRPC_CORE_TSI_ALTS_HANDSHAKER_TRANSPORT_SECURITY_COMMON_API_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/transport_security_common.pb.h"
33 
34 typedef grpc_gcp_RpcProtocolVersions grpc_gcp_rpc_protocol_versions;
35 
36 typedef grpc_gcp_RpcProtocolVersions_Version
37     grpc_gcp_rpc_protocol_versions_version;
38 
39 /**
40  * This method sets the value for max_rpc_versions field of rpc protocol
41  * versions.
42  *
43  * - versions: an rpc protocol version instance.
44  * - max_major: a major version of maximum supported RPC version.
45  * - max_minor: a minor version of maximum supported RPC version.
46  *
47  * The method returns true on success and false otherwise.
48  */
49 bool grpc_gcp_rpc_protocol_versions_set_max(
50     grpc_gcp_rpc_protocol_versions* versions, uint32_t max_major,
51     uint32_t max_minor);
52 
53 /**
54  * This method sets the value for min_rpc_versions field of rpc protocol
55  * versions.
56  *
57  * - versions: an rpc protocol version instance.
58  * - min_major: a major version of minimum supported RPC version.
59  * - min_minor: a minor version of minimum supported RPC version.
60  *
61  * The method returns true on success and false otherwise.
62  */
63 bool grpc_gcp_rpc_protocol_versions_set_min(
64     grpc_gcp_rpc_protocol_versions* versions, uint32_t min_major,
65     uint32_t min_minor);
66 
67 /**
68  * This method computes serialized byte length of rpc protocol versions.
69  *
70  * - versions: an rpc protocol versions instance.
71  *
72  * The method returns serialized byte length. It returns 0 on failure.
73  */
74 size_t grpc_gcp_rpc_protocol_versions_encode_length(
75     const grpc_gcp_rpc_protocol_versions* versions);
76 
77 /**
78  * This method serializes rpc protocol versions and writes the result to
79  * the memory buffer provided by the caller. Caller is responsible for
80  * allocating sufficient memory to store the serialized data.
81  *
82  * - versions: an rpc protocol versions instance.
83  * - bytes: bytes buffer where the result will be written to.
84  * - bytes_length: length of the bytes buffer.
85  *
86  * The method returns true on success and false otherwise.
87  */
88 bool grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
89     const grpc_gcp_rpc_protocol_versions* versions, uint8_t* bytes,
90     size_t bytes_length);
91 
92 /**
93  * This method serializes an rpc protocol version and returns serialized rpc
94  * versions in grpc slice.
95  *
96  * - versions: an rpc protocol versions instance.
97  * - slice: grpc slice where the serialized result will be written.
98  *
99  * The method returns true on success and false otherwise.
100  */
101 bool grpc_gcp_rpc_protocol_versions_encode(
102     const grpc_gcp_rpc_protocol_versions* versions, grpc_slice* slice);
103 
104 /**
105  * This method de-serializes input in grpc slice form and stores the result
106  * in rpc protocol versions.
107  *
108  * - slice: a data stream containing a serialized rpc protocol version.
109  * - versions: an rpc protocol version instance used to hold de-serialized
110  *   result.
111  *
112  * The method returns true on success and false otherwise.
113  */
114 bool grpc_gcp_rpc_protocol_versions_decode(
115     const grpc_slice& slice, grpc_gcp_rpc_protocol_versions* versions);
116 
117 /**
118  * This method performs a deep copy operation on rpc protocol versions
119  * instance.
120  *
121  * - src: rpc protocol versions instance that needs to be copied.
122  * - dst: rpc protocol versions instance that stores the copied result.
123  *
124  * The method returns true on success and false otherwise.
125  */
126 bool grpc_gcp_rpc_protocol_versions_copy(
127     const grpc_gcp_rpc_protocol_versions* src,
128     grpc_gcp_rpc_protocol_versions* dst);
129 
130 /**
131  * This method performs a version check between local and peer rpc protocol
132  * versions.
133  *
134  * - local_versions: local rpc protocol versions instance.
135  * - peer_versions: peer rpc protocol versions instance.
136  * - highest_common_version: an output parameter that will store the highest
137  *   common rpc protocol version both parties agreed on.
138  *
139  * The method returns true if the check passes which means both parties agreed
140  * on a common rpc protocol to use, and false otherwise.
141  */
142 bool grpc_gcp_rpc_protocol_versions_check(
143     const grpc_gcp_rpc_protocol_versions* local_versions,
144     const grpc_gcp_rpc_protocol_versions* peer_versions,
145     grpc_gcp_rpc_protocol_versions_version* highest_common_version);
146 
147 namespace grpc_core {
148 namespace internal {
149 
150 /**
151  * Exposed for testing only.
152  * The method returns 0 if v1 = v2,
153  *            returns 1 if v1 > v2,
154  *            returns -1 if v1 < v2.
155  */
156 int grpc_gcp_rpc_protocol_version_compare(
157     const grpc_gcp_rpc_protocol_versions_version* v1,
158     const grpc_gcp_rpc_protocol_versions_version* v2);
159 
160 }  // namespace internal
161 }  // namespace grpc_core
162 
163 #endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_TRANSPORT_SECURITY_COMMON_API_H */
164