alts_security_connector.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <string.h>
24 
25 #include <limits>
26 #include <utility>
27 
28 #include "absl/status/status.h"
29 #include "absl/strings/string_view.h"
30 
31 #include <grpc/grpc.h>
34 #include <grpc/slice.h>
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
38 
59 
61  grpc_gcp_rpc_protocol_versions* rpc_versions) {
68 }
69 
70 namespace {
71 
72 void alts_check_peer(tsi_peer peer,
74  grpc_closure* on_peer_checked) {
75  *auth_context =
77  tsi_peer_destruct(&peer);
79  *auth_context != nullptr
82  "Could not get ALTS auth context from TSI peer");
83  grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_peer_checked, error);
84 }
85 
86 class grpc_alts_channel_security_connector final
88  public:
89  grpc_alts_channel_security_connector(
92  const char* target_name)
94  std::move(channel_creds),
95  std::move(request_metadata_creds)),
96  target_name_(gpr_strdup(target_name)) {}
97 
98  ~grpc_alts_channel_security_connector() override { gpr_free(target_name_); }
99 
100  void add_handshakers(
101  const grpc_channel_args* args, grpc_pollset_set* interested_parties,
102  grpc_core::HandshakeManager* handshake_manager) override {
103  tsi_handshaker* handshaker = nullptr;
104  const grpc_alts_credentials* creds =
105  static_cast<const grpc_alts_credentials*>(channel_creds());
106  size_t user_specified_max_frame_size = 0;
107  const grpc_arg* arg =
109  if (arg != nullptr && arg->type == GRPC_ARG_INTEGER) {
110  user_specified_max_frame_size = grpc_channel_arg_get_integer(
112  }
114  creds->options(), target_name_,
115  creds->handshaker_service_url(), true, interested_parties,
116  &handshaker, user_specified_max_frame_size) == TSI_OK);
117  handshake_manager->Add(
118  grpc_core::SecurityHandshakerCreate(handshaker, this, args));
119  }
120 
121  void check_peer(tsi_peer peer, grpc_endpoint* /*ep*/,
123  grpc_closure* on_peer_checked) override {
124  alts_check_peer(peer, auth_context, on_peer_checked);
125  }
126 
127  void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
128  grpc_error_handle error) override {
130  }
131 
132  int cmp(const grpc_security_connector* other_sc) const override {
133  auto* other =
134  reinterpret_cast<const grpc_alts_channel_security_connector*>(other_sc);
135  int c = channel_security_connector_cmp(other);
136  if (c != 0) return c;
137  return strcmp(target_name_, other->target_name_);
138  }
139 
141  absl::string_view host, grpc_auth_context*) override {
142  if (host.empty() || host != target_name_) {
144  "ALTS call host does not match target name"));
145  }
147  }
148 
149  private:
150  char* target_name_;
151 };
152 
153 class grpc_alts_server_security_connector final
155  public:
156  explicit grpc_alts_server_security_connector(
159  std::move(server_creds)) {}
160 
161  ~grpc_alts_server_security_connector() override = default;
162 
163  void add_handshakers(
164  const grpc_channel_args* args, grpc_pollset_set* interested_parties,
165  grpc_core::HandshakeManager* handshake_manager) override {
166  tsi_handshaker* handshaker = nullptr;
167  const grpc_alts_server_credentials* creds =
168  static_cast<const grpc_alts_server_credentials*>(server_creds());
169  size_t user_specified_max_frame_size = 0;
170  const grpc_arg* arg =
172  if (arg != nullptr && arg->type == GRPC_ARG_INTEGER) {
173  user_specified_max_frame_size = grpc_channel_arg_get_integer(
175  }
177  creds->options(), nullptr, creds->handshaker_service_url(),
178  false, interested_parties, &handshaker,
179  user_specified_max_frame_size) == TSI_OK);
180  handshake_manager->Add(
181  grpc_core::SecurityHandshakerCreate(handshaker, this, args));
182  }
183 
184  void check_peer(tsi_peer peer, grpc_endpoint* /*ep*/,
186  grpc_closure* on_peer_checked) override {
187  alts_check_peer(peer, auth_context, on_peer_checked);
188  }
189 
190  void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
191  grpc_error_handle error) override {
193  }
194 
195  int cmp(const grpc_security_connector* other) const override {
197  static_cast<const grpc_server_security_connector*>(other));
198  }
199 };
200 } // namespace
201 
202 namespace grpc_core {
203 namespace internal {
205  const tsi_peer* peer) {
206  if (peer == nullptr) {
208  "Invalid arguments to grpc_alts_auth_context_from_tsi_peer()");
209  return nullptr;
210  }
211  /* Validate certificate type. */
212  const tsi_peer_property* cert_type_prop =
214  if (cert_type_prop == nullptr ||
215  strncmp(cert_type_prop->value.data, TSI_ALTS_CERTIFICATE_TYPE,
216  cert_type_prop->value.length) != 0) {
217  gpr_log(GPR_ERROR, "Invalid or missing certificate type property.");
218  return nullptr;
219  }
220  /* Check if security level exists. */
221  const tsi_peer_property* security_level_prop =
223  if (security_level_prop == nullptr) {
224  gpr_log(GPR_ERROR, "Missing security level property.");
225  return nullptr;
226  }
227  /* Validate RPC protocol versions. */
228  const tsi_peer_property* rpc_versions_prop =
230  if (rpc_versions_prop == nullptr) {
231  gpr_log(GPR_ERROR, "Missing rpc protocol versions property.");
232  return nullptr;
233  }
234  grpc_gcp_rpc_protocol_versions local_versions, peer_versions;
235  grpc_alts_set_rpc_protocol_versions(&local_versions);
237  rpc_versions_prop->value.data, rpc_versions_prop->value.length);
238  bool decode_result =
241  if (!decode_result) {
242  gpr_log(GPR_ERROR, "Invalid peer rpc protocol versions.");
243  return nullptr;
244  }
245  /* TODO: Pass highest common rpc protocol version to grpc caller. */
246  bool check_result = grpc_gcp_rpc_protocol_versions_check(
247  &local_versions, &peer_versions, nullptr);
248  if (!check_result) {
249  gpr_log(GPR_ERROR, "Mismatch of local and peer rpc protocol versions.");
250  return nullptr;
251  }
252  /* Validate ALTS Context. */
253  const tsi_peer_property* alts_context_prop =
255  if (alts_context_prop == nullptr) {
256  gpr_log(GPR_ERROR, "Missing alts context property.");
257  return nullptr;
258  }
259  /* Create auth context. */
260  auto ctx = MakeRefCounted<grpc_auth_context>(nullptr);
264  size_t i = 0;
265  for (i = 0; i < peer->property_count; i++) {
266  const tsi_peer_property* tsi_prop = &peer->properties[i];
267  /* Add service account to auth context. */
268  if (strcmp(tsi_prop->name, TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY) == 0) {
271  tsi_prop->value.data, tsi_prop->value.length);
274  }
275  /* Add alts context to auth context. */
276  if (strcmp(tsi_prop->name, TSI_ALTS_CONTEXT) == 0) {
278  tsi_prop->value.data,
279  tsi_prop->value.length);
280  }
281  /* Add security level to auth context. */
282  if (strcmp(tsi_prop->name, TSI_SECURITY_LEVEL_PEER_PROPERTY) == 0) {
285  tsi_prop->value.data, tsi_prop->value.length);
286  }
287  }
289  gpr_log(GPR_ERROR, "Invalid unauthenticated peer.");
290  ctx.reset(DEBUG_LOCATION, "test");
291  return nullptr;
292  }
293  return ctx;
294 }
295 
296 } // namespace internal
297 } // namespace grpc_core
298 
303  const char* target_name) {
304  if (channel_creds == nullptr || target_name == nullptr) {
305  gpr_log(
306  GPR_ERROR,
307  "Invalid arguments to grpc_alts_channel_security_connector_create()");
308  return nullptr;
309  }
310  return grpc_core::MakeRefCounted<grpc_alts_channel_security_connector>(
311  std::move(channel_creds), std::move(request_metadata_creds), target_name);
312 }
313 
317  if (server_creds == nullptr) {
318  gpr_log(
319  GPR_ERROR,
320  "Invalid arguments to grpc_alts_server_security_connector_create()");
321  return nullptr;
322  }
323  return grpc_core::MakeRefCounted<grpc_alts_server_security_connector>(
324  std::move(server_creds));
325 }
grpc_arg
Definition: grpc_types.h:103
GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME
#define GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME
Definition: grpc_security_constants.h:47
grpc_alts_credentials
Definition: alts_credentials.h:35
grpc_auth_context
Definition: security_context.h:63
grpc_alts_set_rpc_protocol_versions
void grpc_alts_set_rpc_protocol_versions(grpc_gcp_rpc_protocol_versions *rpc_versions)
Definition: alts_security_connector.cc:60
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
tsi_peer::properties
tsi_peer_property * properties
Definition: transport_security_interface.h:239
tsi_peer_property::value
struct tsi_peer_property::@48 value
GRPC_PROTOCOL_VERSION_MIN_MAJOR
#define GRPC_PROTOCOL_VERSION_MIN_MAJOR
Definition: transport.h:68
ctx
Definition: benchmark-async.c:30
grpc_security_connector::check_peer
virtual void check_peer(tsi_peer peer, grpc_endpoint *ep, grpc_core::RefCountedPtr< grpc_auth_context > *auth_context, grpc_closure *on_peer_checked)=0
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
grpc_server_security_connector::add_handshakers
virtual void add_handshakers(const grpc_channel_args *args, grpc_pollset_set *interested_parties, grpc_core::HandshakeManager *handshake_mgr)=0
grpc_server_security_connector
Definition: security_connector.h:171
tsi_handshaker
Definition: transport_security.h:84
grpc_channel_arg_get_integer
int grpc_channel_arg_get_integer(const grpc_arg *arg, const grpc_integer_options options)
Definition: channel_args.cc:405
grpc_core::SecurityHandshakerCreate
RefCountedPtr< Handshaker > SecurityHandshakerCreate(tsi_handshaker *handshaker, grpc_security_connector *connector, const grpc_channel_args *args)
Creates a security handshaker using handshaker.
Definition: security_handshaker.cc:658
slice.h
grpc_core
Definition: call_metric_recorder.h:31
TSI_SECURITY_LEVEL_PEER_PROPERTY
#define TSI_SECURITY_LEVEL_PEER_PROPERTY
Definition: transport_security_interface.h:226
alts_tsi_handshaker.h
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
tsi_peer_property::length
size_t length
Definition: transport_security_interface.h:234
string.h
grpc_core::internal::grpc_alts_auth_context_from_tsi_peer
RefCountedPtr< grpc_auth_context > grpc_alts_auth_context_from_tsi_peer(const tsi_peer *peer)
Definition: alts_security_connector.cc:204
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
GRPC_PROTOCOL_VERSION_MIN_MINOR
#define GRPC_PROTOCOL_VERSION_MIN_MINOR
Definition: transport.h:69
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_auth_context_peer_is_authenticated
GRPCAPI int grpc_auth_context_peer_is_authenticated(const grpc_auth_context *ctx)
Definition: security_context.cc:168
grpc_alts_credentials::options
const grpc_alts_credentials_options * options() const
Definition: alts_credentials.h:49
closure.h
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
grpc_alts_server_credentials
Definition: alts_credentials.h:65
grpc_alts_credentials::handshaker_service_url
const char * handshaker_service_url() const
Definition: alts_credentials.h:51
credentials.h
grpc_channel_args
Definition: grpc_types.h:132
GRPC_ARG_TSI_MAX_FRAME_SIZE
#define GRPC_ARG_TSI_MAX_FRAME_SIZE
Definition: grpc_types.h:290
grpc_gcp_rpc_protocol_versions_decode
bool grpc_gcp_rpc_protocol_versions_decode(const grpc_slice &slice, grpc_gcp_rpc_protocol_versions *versions)
Definition: transport_security_common_api.cc:89
grpc_types.h
absl::UnauthenticatedError
Status UnauthenticatedError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:371
alts_security_connector.h
grpc_security_connector
Definition: security_connector.h:61
grpc_auth_context_set_peer_identity_property_name
GRPCAPI int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context *ctx, const char *name)
Definition: security_context.cc:151
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_security_connector::cancel_check_peer
virtual void cancel_check_peer(grpc_closure *on_peer_checked, grpc_error_handle error)=0
string_util.h
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
grpc_alts_channel_security_connector_create
grpc_core::RefCountedPtr< grpc_channel_security_connector > grpc_alts_channel_security_connector_create(grpc_core::RefCountedPtr< grpc_channel_credentials > channel_creds, grpc_core::RefCountedPtr< grpc_call_credentials > request_metadata_creds, const char *target_name)
Definition: alts_security_connector.cc:300
arg::type
argtype type
Definition: cmdline.cc:43
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME
#define GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME
Definition: grpc_security_constants.h:26
grpc_core::RefCountedPtr< grpc_auth_context >
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_alts_server_credentials::handshaker_service_url
const char * handshaker_service_url() const
Definition: alts_credentials.h:78
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY
#define TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY
Definition: alts_tsi_handshaker.h:34
grpc_channel_security_connector::CheckCallHost
virtual grpc_core::ArenaPromise< absl::Status > CheckCallHost(absl::string_view host, grpc_auth_context *auth_context)=0
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
TSI_ALTS_RPC_VERSIONS
#define TSI_ALTS_RPC_VERSIONS
Definition: alts_tsi_handshaker.h:36
grpc_alts_server_credentials::options
const grpc_alts_credentials_options * options() const
Definition: alts_credentials.h:76
grpc.h
GRPC_PROTOCOL_VERSION_MAX_MAJOR
#define GRPC_PROTOCOL_VERSION_MAX_MAJOR
Definition: transport.h:66
arg
Definition: cmdline.cc:40
grpc_core::HandshakeManager::Add
void Add(RefCountedPtr< Handshaker > handshaker)
Definition: src/core/lib/transport/handshaker.cc:63
arena_promise.h
grpc_auth_context_add_cstring_property
GRPCAPI void grpc_auth_context_add_cstring_property(grpc_auth_context *ctx, const char *name, const char *value)
Definition: security_context.cc:268
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
tsi_peer_property::data
char * data
Definition: transport_security_interface.h:233
tsi_peer_get_property_by_name
const tsi_peer_property * tsi_peer_get_property_by_name(const tsi_peer *peer, const char *name)
Definition: transport_security.cc:369
error.h
grpc_channel_security_connector::add_handshakers
virtual void add_handshakers(const grpc_channel_args *args, grpc_pollset_set *interested_parties, grpc_core::HandshakeManager *handshake_mgr)=0
Registers handshakers with handshake_mgr.
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_gcp_rpc_protocol_versions_check
bool grpc_gcp_rpc_protocol_versions_check(const grpc_gcp_rpc_protocol_versions *local_versions, const grpc_gcp_rpc_protocol_versions *peer_versions, grpc_gcp_rpc_protocol_versions_version *highest_common_version)
Definition: transport_security_common_api.cc:194
tsi_peer_property::name
char * name
Definition: transport_security_interface.h:231
TSI_ALTS_CERTIFICATE_TYPE
#define TSI_ALTS_CERTIFICATE_TYPE
Definition: alts_tsi_handshaker.h:35
grpc_gcp_rpc_protocol_versions_set_min
bool grpc_gcp_rpc_protocol_versions_set_min(grpc_gcp_rpc_protocol_versions *versions, uint32_t min_major, uint32_t min_minor)
Definition: transport_security_common_api.cc:39
promise.h
alts_tsi_handshaker_create
tsi_result alts_tsi_handshaker_create(const grpc_alts_credentials_options *options, const char *target_name, const char *handshaker_service_url, bool is_client, grpc_pollset_set *interested_parties, tsi_handshaker **self, size_t user_specified_max_frame_size)
Definition: alts_tsi_handshaker.cc:639
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
tsi_peer_property
Definition: transport_security_interface.h:230
grpc_channel_security_connector::channel_security_connector_cmp
int channel_security_connector_cmp(const grpc_channel_security_connector *other) const
Definition: security_connector.cc:45
alts_credentials.h
security_handshaker.h
tsi_peer
Definition: transport_security_interface.h:238
security_context.h
grpc_alts_server_security_connector_create
grpc_core::RefCountedPtr< grpc_server_security_connector > grpc_alts_server_security_connector_create(grpc_core::RefCountedPtr< grpc_server_credentials > server_creds)
Definition: alts_security_connector.cc:315
transport_security.h
grpc_core::ImmediateOkStatus
Definition: promise/promise.h:78
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
debug_location.h
grpc_core::ArenaPromise
Definition: arena_promise.h:152
poll.h
alloc.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_security_constants.h
exec_ctx.h
slice_refcount.h
handshaker.h
grpc_auth_context_add_property
GRPCAPI void grpc_auth_context_add_property(grpc_auth_context *ctx, const char *name, const char *value, size_t value_length)
Definition: security_context.cc:248
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
ref_counted_ptr.h
transport.h
channel_args.h
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_gcp_rpc_protocol_versions_set_max
bool grpc_gcp_rpc_protocol_versions_set_max(grpc_gcp_rpc_protocol_versions *versions, uint32_t max_major, uint32_t max_minor)
Definition: transport_security_common_api.cc:25
grpc_channel_security_connector
Definition: security_connector.h:118
grpc_core::Immediate
promise_detail::Immediate< T > Immediate(T value)
Definition: promise/promise.h:73
internal
Definition: benchmark/test/output_test_helper.cc:20
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
grpc_core::HandshakeManager
Definition: handshaker.h:98
iomgr_fwd.h
endpoint.h
grpc_server_security_connector::server_creds
const grpc_server_credentials * server_creds() const
Definition: security_connector.h:181
TSI_CERTIFICATE_TYPE_PEER_PROPERTY
#define TSI_CERTIFICATE_TYPE_PEER_PROPERTY
Definition: transport_security_interface.h:223
GRPC_SSL_URL_SCHEME
#define GRPC_SSL_URL_SCHEME
Definition: security_connector.h:49
TSI_ALTS_CONTEXT
#define TSI_ALTS_CONTEXT
Definition: alts_tsi_handshaker.h:37
grpc_channel_security_connector::channel_creds
const grpc_channel_credentials * channel_creds() const
Definition: security_connector.h:135
grpc_error
Definition: error_internal.h:42
_grpc_gcp_RpcProtocolVersions
Definition: transport_security_common_api.h:38
grpc_security_connector::cmp
virtual int cmp(const grpc_security_connector *other) const =0
grpc_closure
Definition: closure.h:56
grpc_channel_args_find
const grpc_arg * grpc_channel_args_find(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:393
GRPC_ALTS_TRANSPORT_SECURITY_TYPE
#define GRPC_ALTS_TRANSPORT_SECURITY_TYPE
Definition: alts_security_connector.h:31
tsi_peer_destruct
void tsi_peer_destruct(tsi_peer *self)
Definition: transport_security.cc:320
grpc_endpoint
Definition: endpoint.h:105
GRPC_PROTOCOL_VERSION_MAX_MINOR
#define GRPC_PROTOCOL_VERSION_MAX_MINOR
Definition: transport.h:67
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
tsi_peer::property_count
size_t property_count
Definition: transport_security_interface.h:240
grpc_server_security_connector::server_security_connector_cmp
int server_security_connector_cmp(const grpc_server_security_connector *other) const
Definition: security_connector.cc:67
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:41