core/lib/security/credentials/xds/xds_credentials.cc
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2020 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 "absl/strings/string_view.h"
24 
26 #include <grpc/support/log.h>
27 
36 
37 namespace grpc_core {
38 
39 namespace {
40 
41 bool XdsVerifySubjectAlternativeNames(
42  const char* const* subject_alternative_names,
43  size_t subject_alternative_names_size,
44  const std::vector<StringMatcher>& matchers) {
45  if (matchers.empty()) return true;
46  for (size_t i = 0; i < subject_alternative_names_size; ++i) {
47  for (const auto& matcher : matchers) {
48  if (matcher.type() == StringMatcher::Type::kExact) {
49  // For Exact match, use DNS rules for verifying SANs
50  // TODO(zhenlian): Right now, the SSL layer does not save the type of
51  // the SAN, so we are doing a DNS style verification for all SANs when
52  // the type is EXACT. When we expose the SAN type, change this to only
53  // do this verification when the SAN type is DNS and match type is
54  // kExact. For all other cases, we should use matcher.Match().
55  if (VerifySubjectAlternativeName(subject_alternative_names[i],
56  matcher.string_matcher())) {
57  return true;
58  }
59  } else {
60  if (matcher.Match(subject_alternative_names[i])) {
61  return true;
62  }
63  }
64  }
65  }
66  return false;
67 }
68 
69 } // namespace
70 
71 //
72 // XdsCertificateVerifier
73 //
74 
76  RefCountedPtr<XdsCertificateProvider> xds_certificate_provider,
78  : xds_certificate_provider_(std::move(xds_certificate_provider)),
80 
83  std::function<void(absl::Status)>, absl::Status* sync_status) {
84  GPR_ASSERT(request != nullptr);
85  if (!XdsVerifySubjectAlternativeNames(
86  request->peer_info.san_names.uri_names,
87  request->peer_info.san_names.uri_names_size,
88  xds_certificate_provider_->GetSanMatchers(cluster_name_)) &&
89  !XdsVerifySubjectAlternativeNames(
90  request->peer_info.san_names.ip_names,
91  request->peer_info.san_names.ip_names_size,
92  xds_certificate_provider_->GetSanMatchers(cluster_name_)) &&
93  !XdsVerifySubjectAlternativeNames(
94  request->peer_info.san_names.dns_names,
95  request->peer_info.san_names.dns_names_size,
96  xds_certificate_provider_->GetSanMatchers(cluster_name_))) {
97  *sync_status = absl::Status(
99  "SANs from certificate did not match SANs from xDS control plane");
100  }
101  return true; /* synchronous check */
102 }
103 
106 
108  const grpc_tls_certificate_verifier* other) const {
109  auto* o = static_cast<const XdsCertificateVerifier*>(other);
110  int r = QsortCompare(xds_certificate_provider_, o->xds_certificate_provider_);
111  if (r != 0) return r;
112  return cluster_name_.compare(o->cluster_name_);
113 }
114 
116  static UniqueTypeName::Factory kFactory("Xds");
117  return kFactory.Create();
118 }
119 
121  const char* const* subject_alternative_names,
122  size_t subject_alternative_names_size,
123  const std::vector<StringMatcher>& matchers) {
124  return XdsVerifySubjectAlternativeNames(
125  subject_alternative_names, subject_alternative_names_size, matchers);
126 }
127 
128 //
129 // XdsCredentials
130 //
131 
132 RefCountedPtr<grpc_channel_security_connector>
134  RefCountedPtr<grpc_call_credentials> call_creds, const char* target_name,
135  const grpc_channel_args* args, grpc_channel_args** new_args) {
136  struct ChannelArgsDeleter {
137  const grpc_channel_args* args;
138  bool owned;
139  ~ChannelArgsDeleter() {
141  }
142  };
143  ChannelArgsDeleter temp_args{args, false};
144  // TODO(yashykt): This arg will no longer need to be added after b/173119596
145  // is fixed.
147  const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
148  const_cast<char*>(target_name));
149  const char* override_arg_name = GRPC_SSL_TARGET_NAME_OVERRIDE_ARG;
150  if (grpc_channel_args_find(args, override_arg_name) == nullptr) {
152  args, &override_arg_name, 1, &override_arg, 1);
153  temp_args.owned = true;
154  }
156  auto xds_certificate_provider =
158  if (xds_certificate_provider != nullptr) {
161  GPR_ASSERT(cluster_name.data() != nullptr);
162  const bool watch_root =
163  xds_certificate_provider->ProvidesRootCerts(cluster_name);
164  const bool watch_identity =
165  xds_certificate_provider->ProvidesIdentityCerts(cluster_name);
166  if (watch_root || watch_identity) {
167  auto tls_credentials_options =
168  MakeRefCounted<grpc_tls_credentials_options>();
169  tls_credentials_options->set_certificate_provider(
170  xds_certificate_provider);
171  if (watch_root) {
172  tls_credentials_options->set_watch_root_cert(true);
173  tls_credentials_options->set_root_cert_name(cluster_name);
174  }
175  if (watch_identity) {
176  tls_credentials_options->set_watch_identity_pair(true);
177  tls_credentials_options->set_identity_cert_name(cluster_name);
178  }
179  tls_credentials_options->set_verify_server_cert(true);
180  tls_credentials_options->set_certificate_verifier(
181  MakeRefCounted<XdsCertificateVerifier>(xds_certificate_provider,
183  tls_credentials_options->set_check_call_host(false);
184  auto tls_credentials =
185  MakeRefCounted<TlsCredentials>(std::move(tls_credentials_options));
186  return tls_credentials->create_security_connector(
187  std::move(call_creds), target_name, temp_args.args, new_args);
188  }
189  }
190  GPR_ASSERT(fallback_credentials_ != nullptr);
192  std::move(call_creds), target_name, temp_args.args, new_args);
193 }
194 
196  static UniqueTypeName::Factory kFactory("Xds");
197  return kFactory.Create();
198 }
199 
200 //
201 // XdsServerCredentials
202 //
203 
206  auto xds_certificate_provider =
208  // Identity certs are a must for TLS.
209  if (xds_certificate_provider != nullptr &&
210  xds_certificate_provider->ProvidesIdentityCerts("")) {
211  auto tls_credentials_options =
212  MakeRefCounted<grpc_tls_credentials_options>();
213  tls_credentials_options->set_watch_identity_pair(true);
214  tls_credentials_options->set_certificate_provider(xds_certificate_provider);
215  if (xds_certificate_provider->ProvidesRootCerts("")) {
216  tls_credentials_options->set_watch_root_cert(true);
217  if (xds_certificate_provider->GetRequireClientCertificate("")) {
218  tls_credentials_options->set_cert_request_type(
220  } else {
221  tls_credentials_options->set_cert_request_type(
223  }
224  } else {
225  // Do not request client certificate if there is no way to verify.
226  tls_credentials_options->set_cert_request_type(
228  }
229  auto tls_credentials = MakeRefCounted<TlsServerCredentials>(
230  std::move(tls_credentials_options));
231  return tls_credentials->create_security_connector(args);
232  }
234 }
235 
237  static UniqueTypeName::Factory kFactory("Xds");
238  return kFactory.Create();
239 }
240 
241 } // namespace grpc_core
242 
244  grpc_channel_credentials* fallback_credentials) {
245  GPR_ASSERT(fallback_credentials != nullptr);
246  return new grpc_core::XdsCredentials(fallback_credentials->Ref());
247 }
248 
250  grpc_server_credentials* fallback_credentials) {
251  GPR_ASSERT(fallback_credentials != nullptr);
252  return new grpc_core::XdsServerCredentials(fallback_credentials->Ref());
253 }
grpc_arg
Definition: grpc_types.h:103
grpc_core::XdsServerCredentials::fallback_credentials_
RefCountedPtr< grpc_server_credentials > fallback_credentials_
Definition: xds_credentials.h:104
grpc_core::XdsServerCredentials::create_security_connector
RefCountedPtr< grpc_server_security_connector > create_security_connector(const grpc_channel_args *) override
Definition: core/lib/security/credentials/xds/xds_credentials.cc:205
grpc_channel_args_find_string
char * grpc_channel_args_find_string(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:441
grpc_core::UniqueTypeName::Factory::Create
UniqueTypeName Create()
Definition: unique_type_name.h:67
grpc_core::XdsServerCredentials::Type
static UniqueTypeName Type()
Definition: core/lib/security/credentials/xds/xds_credentials.cc:236
tls_credentials.h
log.h
grpc_core::XdsCertificateVerifier
Definition: xds_credentials.h:47
grpc_core::TestOnlyXdsVerifySubjectAlternativeNames
bool TestOnlyXdsVerifySubjectAlternativeNames(const char *const *subject_alternative_names, size_t subject_alternative_names_size, const std::vector< StringMatcher > &matchers)
Definition: core/lib/security/credentials/xds/xds_credentials.cc:120
grpc_core::XdsCredentials::Type
static UniqueTypeName Type()
Definition: core/lib/security/credentials/xds/xds_credentials.cc:195
grpc_xds_credentials_create
grpc_channel_credentials * grpc_xds_credentials_create(grpc_channel_credentials *fallback_credentials)
Definition: core/lib/security/credentials/xds/xds_credentials.cc:243
grpc_core
Definition: call_metric_recorder.h:31
cluster_name
std::string cluster_name
Definition: xds_cluster_resolver.cc:91
benchmark.request
request
Definition: benchmark.py:77
matchers
XdsRouteConfigResource::Route::Matchers matchers
Definition: xds_server_config_fetcher.cc:317
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
cluster_name_
std::string cluster_name_
Definition: xds_cluster_impl.cc:176
grpc_channel_args_copy_and_add_and_remove
grpc_channel_args * grpc_channel_args_copy_and_add_and_remove(const grpc_channel_args *src, const char **to_remove, size_t num_to_remove, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:246
GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY
@ GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY
Definition: grpc_security_constants.h:105
grpc::XdsServerCredentials
std::shared_ptr< ServerCredentials > XdsServerCredentials(const std::shared_ptr< ServerCredentials > &fallback_credentials)
Builds Xds ServerCredentials given fallback credentials.
Definition: xds_server_credentials.cc:30
grpc_core::XdsCertificateVerifier::Verify
bool Verify(grpc_tls_custom_verification_check_request *request, std::function< void(absl::Status)>, absl::Status *sync_status) override
Definition: core/lib/security/credentials/xds/xds_credentials.cc:81
grpc_channel_arg_string_create
grpc_arg grpc_channel_arg_string_create(char *name, char *value)
Definition: channel_args.cc:476
grpc_channel_args
Definition: grpc_types.h:132
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
grpc_core::XdsCredentials::fallback_credentials_
RefCountedPtr< grpc_channel_credentials > fallback_credentials_
Definition: xds_credentials.h:87
grpc_core::XdsCertificateProvider::GetFromChannelArgs
static RefCountedPtr< XdsCertificateProvider > GetFromChannelArgs(const grpc_channel_args *args)
Definition: xds_certificate_provider.cc:411
grpc_tls_certificate_provider.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
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_SSL_TARGET_NAME_OVERRIDE_ARG
#define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
Definition: grpc_types.h:278
grpc_core::XdsCertificateVerifier::cluster_name_
std::string cluster_name_
Definition: xds_credentials.h:64
grpc_core::XdsCertificateVerifier::type
UniqueTypeName type() const override
Definition: core/lib/security/credentials/xds/xds_credentials.cc:115
call_creds
void call_creds(grpc_end2end_test_config config)
Definition: call_creds.cc:523
xds_certificate_provider_
RefCountedPtr< XdsCertificateProvider > xds_certificate_provider_
Definition: cds.cc:205
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
xds_channel_args.h
grpc_core::XdsCredentials::create_security_connector
RefCountedPtr< grpc_channel_security_connector > create_security_connector(RefCountedPtr< grpc_call_credentials > call_creds, const char *target_name, const grpc_channel_args *args, grpc_channel_args **new_args) override
Definition: core/lib/security/credentials/xds/xds_credentials.cc:133
absl::StatusCode::kUnauthenticated
@ kUnauthenticated
grpc_core::XdsCertificateVerifier::XdsCertificateVerifier
XdsCertificateVerifier(RefCountedPtr< XdsCertificateProvider > xds_certificate_provider, std::string cluster_name)
Definition: core/lib/security/credentials/xds/xds_credentials.cc:75
grpc_server_credentials::create_security_connector
virtual grpc_core::RefCountedPtr< grpc_server_security_connector > create_security_connector(const grpc_channel_args *args)=0
xds_credentials.h
absl::Status
ABSL_NAMESPACE_BEGIN class ABSL_MUST_USE_RESULT Status
Definition: abseil-cpp/absl/status/internal/status_internal.h:36
grpc_server_credentials
Definition: src/core/lib/security/credentials/credentials.h:259
grpc_core::UniqueTypeName
Definition: unique_type_name.h:56
grpc_tls_credentials_options.h
grpc_xds_server_credentials_create
grpc_server_credentials * grpc_xds_server_credentials_create(grpc_server_credentials *fallback_credentials)
Definition: core/lib/security/credentials/xds/xds_credentials.cc:249
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE
@ GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE
Definition: grpc_security_constants.h:84
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::XdsCredentials
std::shared_ptr< ChannelCredentials > XdsCredentials(const std::shared_ptr< ChannelCredentials > &fallback_creds)
Builds XDS Credentials.
Definition: cpp/client/xds_credentials.cc:30
grpc_security_constants.h
GRPC_ARG_XDS_CLUSTER_NAME
#define GRPC_ARG_XDS_CLUSTER_NAME
Definition: filters/client_channel/lb_policy/xds/xds_channel_args.h:22
grpc_core::StringMatcher::Type::kExact
@ kExact
grpc_tls_certificate_verifier
Definition: grpc_tls_certificate_verifier.h:38
grpc_core::XdsCertificateVerifier::CompareImpl
int CompareImpl(const grpc_tls_certificate_verifier *other) const override
Definition: core/lib/security/credentials/xds/xds_credentials.cc:107
grpc_tls_custom_verification_check_request
Definition: grpc_security.h:907
channel_args.h
xds_certificate_provider.h
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
@ GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
Definition: grpc_security_constants.h:125
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_channel_credentials::create_security_connector
virtual grpc_core::RefCountedPtr< grpc_channel_security_connector > create_security_connector(grpc_core::RefCountedPtr< grpc_call_credentials > call_creds, const char *target, const grpc_channel_args *args, grpc_channel_args **new_args)=0
grpc_core::XdsCertificateVerifier::xds_certificate_provider_
RefCountedPtr< XdsCertificateProvider > xds_certificate_provider_
Definition: xds_credentials.h:63
grpc_core::XdsCertificateVerifier::Cancel
void Cancel(grpc_tls_custom_verification_check_request *) override
Definition: core/lib/security/credentials/xds/xds_credentials.cc:104
grpc_core::UniqueTypeName::Factory
Definition: unique_type_name.h:60
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_core::VerifySubjectAlternativeName
bool VerifySubjectAlternativeName(absl::string_view subject_alternative_name, const std::string &matcher)
Definition: src/core/lib/security/credentials/tls/tls_utils.cc:33
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
tls_utils.h
grpc_core::RefCounted::Ref
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: ref_counted.h:287
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
owned
bool owned
Definition: src/php/ext/grpc/call.h:31
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:58