Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / src / core / tsi / alts / handshaker / transport_security_common_api.cc
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/tsi/alts/handshaker/transport_security_common_api.h"
22
23 bool grpc_gcp_rpc_protocol_versions_set_max(
24     grpc_gcp_rpc_protocol_versions* versions, uint32_t max_major,
25     uint32_t max_minor) {
26   if (versions == nullptr) {
27     gpr_log(GPR_ERROR,
28             "versions is nullptr in "
29             "grpc_gcp_rpc_protocol_versions_set_max().");
30     return false;
31   }
32   versions->max_rpc_version.major = max_major;
33   versions->max_rpc_version.minor = max_minor;
34   return true;
35 }
36
37 bool grpc_gcp_rpc_protocol_versions_set_min(
38     grpc_gcp_rpc_protocol_versions* versions, uint32_t min_major,
39     uint32_t min_minor) {
40   if (versions == nullptr) {
41     gpr_log(GPR_ERROR,
42             "versions is nullptr in "
43             "grpc_gcp_rpc_protocol_versions_set_min().");
44     return false;
45   }
46   versions->min_rpc_version.major = min_major;
47   versions->min_rpc_version.minor = min_minor;
48   return true;
49 }
50
51 bool grpc_gcp_rpc_protocol_versions_encode(
52     const grpc_gcp_rpc_protocol_versions* versions, grpc_slice* slice) {
53   if (versions == nullptr || slice == nullptr) {
54     gpr_log(GPR_ERROR,
55             "Invalid nullptr arguments to "
56             "grpc_gcp_rpc_protocol_versions_encode().");
57     return false;
58   }
59   upb::Arena arena;
60   grpc_gcp_RpcProtocolVersions* versions_msg =
61       grpc_gcp_RpcProtocolVersions_new(arena.ptr());
62   grpc_gcp_RpcProtocolVersions_assign_from_struct(versions_msg, arena.ptr(),
63                                                   versions);
64   return grpc_gcp_rpc_protocol_versions_encode(versions_msg, arena.ptr(),
65                                                slice);
66 }
67
68 bool grpc_gcp_rpc_protocol_versions_encode(
69     const grpc_gcp_RpcProtocolVersions* versions, upb_arena* arena,
70     grpc_slice* slice) {
71   if (versions == nullptr || arena == nullptr || slice == nullptr) {
72     gpr_log(GPR_ERROR,
73             "Invalid nullptr arguments to "
74             "grpc_gcp_rpc_protocol_versions_encode().");
75     return false;
76   }
77   size_t buf_length;
78   char* buf =
79       grpc_gcp_RpcProtocolVersions_serialize(versions, arena, &buf_length);
80   if (buf == nullptr) {
81     return false;
82   }
83   *slice = grpc_slice_from_copied_buffer(buf, buf_length);
84   return true;
85 }
86
87 bool grpc_gcp_rpc_protocol_versions_decode(
88     const grpc_slice& slice, grpc_gcp_rpc_protocol_versions* versions) {
89   if (versions == nullptr) {
90     gpr_log(GPR_ERROR,
91             "version is nullptr in "
92             "grpc_gcp_rpc_protocol_versions_decode().");
93     return false;
94   }
95   upb::Arena arena;
96   grpc_gcp_RpcProtocolVersions* versions_msg =
97       grpc_gcp_RpcProtocolVersions_parse(
98           reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)),
99           GRPC_SLICE_LENGTH(slice), arena.ptr());
100   if (versions_msg == nullptr) {
101     gpr_log(GPR_ERROR, "cannot deserialize RpcProtocolVersions message");
102     return false;
103   }
104   grpc_gcp_rpc_protocol_versions_assign_from_upb(versions, versions_msg);
105   return true;
106 }
107
108 void grpc_gcp_rpc_protocol_versions_assign_from_upb(
109     grpc_gcp_rpc_protocol_versions* versions,
110     const grpc_gcp_RpcProtocolVersions* value) {
111   const grpc_gcp_RpcProtocolVersions_Version* max_version_msg =
112       grpc_gcp_RpcProtocolVersions_max_rpc_version(value);
113   if (max_version_msg != nullptr) {
114     versions->max_rpc_version.major =
115         grpc_gcp_RpcProtocolVersions_Version_major(max_version_msg);
116     versions->max_rpc_version.minor =
117         grpc_gcp_RpcProtocolVersions_Version_minor(max_version_msg);
118   } else {
119     versions->max_rpc_version.major = 0;
120     versions->max_rpc_version.minor = 0;
121   }
122   const grpc_gcp_RpcProtocolVersions_Version* min_version_msg =
123       grpc_gcp_RpcProtocolVersions_min_rpc_version(value);
124   if (min_version_msg != nullptr) {
125     versions->min_rpc_version.major =
126         grpc_gcp_RpcProtocolVersions_Version_major(min_version_msg);
127     versions->min_rpc_version.minor =
128         grpc_gcp_RpcProtocolVersions_Version_minor(min_version_msg);
129   } else {
130     versions->min_rpc_version.major = 0;
131     versions->min_rpc_version.minor = 0;
132   }
133 }
134
135 void grpc_gcp_RpcProtocolVersions_assign_from_struct(
136     grpc_gcp_RpcProtocolVersions* versions, upb_arena* arena,
137     const grpc_gcp_rpc_protocol_versions* value) {
138   grpc_gcp_RpcProtocolVersions_Version* max_version_msg =
139       grpc_gcp_RpcProtocolVersions_mutable_max_rpc_version(versions, arena);
140   grpc_gcp_RpcProtocolVersions_Version_set_major(max_version_msg,
141                                                  value->max_rpc_version.major);
142   grpc_gcp_RpcProtocolVersions_Version_set_minor(max_version_msg,
143                                                  value->max_rpc_version.minor);
144   grpc_gcp_RpcProtocolVersions_Version* min_version_msg =
145       grpc_gcp_RpcProtocolVersions_mutable_min_rpc_version(versions, arena);
146   grpc_gcp_RpcProtocolVersions_Version_set_major(min_version_msg,
147                                                  value->min_rpc_version.major);
148   grpc_gcp_RpcProtocolVersions_Version_set_minor(min_version_msg,
149                                                  value->min_rpc_version.minor);
150 }
151
152 bool grpc_gcp_rpc_protocol_versions_copy(
153     const grpc_gcp_rpc_protocol_versions* src,
154     grpc_gcp_rpc_protocol_versions* dst) {
155   if ((src == nullptr && dst != nullptr) ||
156       (src != nullptr && dst == nullptr)) {
157     gpr_log(GPR_ERROR,
158             "Invalid arguments to "
159             "grpc_gcp_rpc_protocol_versions_copy().");
160     return false;
161   }
162   if (src == nullptr) {
163     return true;
164   }
165   grpc_gcp_rpc_protocol_versions_set_max(dst, src->max_rpc_version.major,
166                                          src->max_rpc_version.minor);
167   grpc_gcp_rpc_protocol_versions_set_min(dst, src->min_rpc_version.major,
168                                          src->min_rpc_version.minor);
169   return true;
170 }
171
172 namespace grpc_core {
173 namespace internal {
174
175 int grpc_gcp_rpc_protocol_version_compare(
176     const grpc_gcp_rpc_protocol_versions_version* v1,
177     const grpc_gcp_rpc_protocol_versions_version* v2) {
178   if ((v1->major > v2->major) ||
179       (v1->major == v2->major && v1->minor > v2->minor)) {
180     return 1;
181   }
182   if ((v1->major < v2->major) ||
183       (v1->major == v2->major && v1->minor < v2->minor)) {
184     return -1;
185   }
186   return 0;
187 }
188
189 }  // namespace internal
190 }  // namespace grpc_core
191
192 bool grpc_gcp_rpc_protocol_versions_check(
193     const grpc_gcp_rpc_protocol_versions* local_versions,
194     const grpc_gcp_rpc_protocol_versions* peer_versions,
195     grpc_gcp_rpc_protocol_versions_version* highest_common_version) {
196   if (local_versions == nullptr || peer_versions == nullptr) {
197     gpr_log(GPR_ERROR,
198             "Invalid arguments to "
199             "grpc_gcp_rpc_protocol_versions_check().");
200     return false;
201   }
202   /* max_common_version is MIN(local.max, peer.max) */
203   const grpc_gcp_rpc_protocol_versions_version* max_common_version =
204       grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
205           &local_versions->max_rpc_version, &peer_versions->max_rpc_version) > 0
206           ? &peer_versions->max_rpc_version
207           : &local_versions->max_rpc_version;
208   /* min_common_version is MAX(local.min, peer.min) */
209   const grpc_gcp_rpc_protocol_versions_version* min_common_version =
210       grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
211           &local_versions->min_rpc_version, &peer_versions->min_rpc_version) > 0
212           ? &local_versions->min_rpc_version
213           : &peer_versions->min_rpc_version;
214   bool result = grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
215                     max_common_version, min_common_version) >= 0
216                     ? true
217                     : false;
218   if (result && highest_common_version != nullptr) {
219     memcpy(highest_common_version, max_common_version,
220            sizeof(grpc_gcp_rpc_protocol_versions_version));
221   }
222   return result;
223 }