| 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 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/security/credentials/xds/xds_credentials.h" |
| 22 |
|
| 23 |
#include "absl/strings/string_view.h" |
| 24 |
#include "absl/types/optional.h" |
| 25 |
|
| 26 |
#include <grpc/grpc_security_constants.h> |
| 27 |
#include <grpc/impl/codegen/grpc_types.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
#include "src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_args.h" |
| 31 |
#include "src/core/ext/xds/xds_certificate_provider.h" |
| 32 |
#include "src/core/lib/channel/channel_args.h" |
| 33 |
#include "src/core/lib/gpr/useful.h" |
| 34 |
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h" |
| 35 |
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h" |
| 36 |
#include "src/core/lib/security/credentials/tls/tls_credentials.h" |
| 37 |
#include "src/core/lib/security/credentials/tls/tls_utils.h" |
| 38 |
|
| 39 |
namespace grpc_core { |
| 40 |
|
| 41 |
namespace { |
| 42 |
|
| 43 |
bool XdsVerifySubjectAlternativeNames( |
| 44 |
const char* const* subject_alternative_names, |
| 45 |
size_t subject_alternative_names_size, |
| 46 |
const std::vector<StringMatcher>& matchers) { |
| 47 |
if (matchers.empty()) return true; |
| 48 |
for (size_t i = 0; i < subject_alternative_names_size; ++i) { |
| 49 |
for (const auto& matcher : matchers) { |
| 50 |
if (matcher.type() == StringMatcher::Type::kExact) { |
| 51 |
// For Exact match, use DNS rules for verifying SANs |
| 52 |
// TODO(zhenlian): Right now, the SSL layer does not save the type of |
| 53 |
// the SAN, so we are doing a DNS style verification for all SANs when |
| 54 |
// the type is EXACT. When we expose the SAN type, change this to only |
| 55 |
// do this verification when the SAN type is DNS and match type is |
| 56 |
// kExact. For all other cases, we should use matcher.Match(). |
| 57 |
if (VerifySubjectAlternativeName(subject_alternative_names[i], |
| 58 |
matcher.string_matcher())) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} else { |
| 62 |
if (matcher.Match(subject_alternative_names[i])) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
} // namespace |
| 72 |
|
| 73 |
// |
| 74 |
// XdsCertificateVerifier |
| 75 |
// |
| 76 |
|
| 77 |
XdsCertificateVerifier::XdsCertificateVerifier( |
| 78 |
RefCountedPtr<XdsCertificateProvider> xds_certificate_provider, |
| 79 |
std::string cluster_name) |
| 80 |
: xds_certificate_provider_(std::move(xds_certificate_provider)), |
| 81 |
cluster_name_(std::move(cluster_name)) {} |
| 82 |
|
| 83 |
bool XdsCertificateVerifier::Verify( |
| 84 |
grpc_tls_custom_verification_check_request* request, |
| 85 |
std::function<void(absl::Status)>, absl::Status* sync_status) { |
| 86 |
GPR_ASSERT(request != nullptr); |
| 87 |
if (!XdsVerifySubjectAlternativeNames( |
| 88 |
request->peer_info.san_names.uri_names, |
| 89 |
request->peer_info.san_names.uri_names_size, |
| 90 |
xds_certificate_provider_->GetSanMatchers(cluster_name_)) && |
| 91 |
!XdsVerifySubjectAlternativeNames( |
| 92 |
request->peer_info.san_names.ip_names, |
| 93 |
request->peer_info.san_names.ip_names_size, |
| 94 |
xds_certificate_provider_->GetSanMatchers(cluster_name_)) && |
| 95 |
!XdsVerifySubjectAlternativeNames( |
| 96 |
request->peer_info.san_names.dns_names, |
| 97 |
request->peer_info.san_names.dns_names_size, |
| 98 |
xds_certificate_provider_->GetSanMatchers(cluster_name_))) { |
| 99 |
*sync_status = absl::Status( |
| 100 |
absl::StatusCode::kUnauthenticated, |
| 101 |
"SANs from certificate did not match SANs from xDS control plane"); |
| 102 |
} |
| 103 |
return true; /* synchronous check */ |
| 104 |
} |
| 105 |
|
| 106 |
void XdsCertificateVerifier::Cancel( |
| 107 |
grpc_tls_custom_verification_check_request*) {} |
| 108 |
|
| 109 |
int XdsCertificateVerifier::CompareImpl( |
| 110 |
const grpc_tls_certificate_verifier* other) const { |
| 111 |
auto* o = static_cast<const XdsCertificateVerifier*>(other); |
| 112 |
int r = QsortCompare(xds_certificate_provider_, o->xds_certificate_provider_); |
| 113 |
if (r != 0) return r; |
| 114 |
return cluster_name_.compare(o->cluster_name_); |
| 115 |
} |
| 116 |
|
| 117 |
UniqueTypeName XdsCertificateVerifier::type() const { |
| 118 |
static UniqueTypeName::Factory kFactory("Xds"); |
| 119 |
return kFactory.Create(); |
| 120 |
} |
| 121 |
|
| 122 |
bool TestOnlyXdsVerifySubjectAlternativeNames( |
| 123 |
const char* const* subject_alternative_names, |
| 124 |
size_t subject_alternative_names_size, |
| 125 |
const std::vector<StringMatcher>& matchers) { |
| 126 |
return XdsVerifySubjectAlternativeNames( |
| 127 |
subject_alternative_names, subject_alternative_names_size, matchers); |
| 128 |
} |
| 129 |
|
| 130 |
// |
| 131 |
// XdsCredentials |
| 132 |
// |
| 133 |
|
| 134 |
RefCountedPtr<grpc_channel_security_connector> |
| 135 |
XdsCredentials::create_security_connector( |
| 136 |
RefCountedPtr<grpc_call_credentials> call_creds, const char* target_name, |
| 137 |
ChannelArgs* args) { |
| 138 |
// TODO(yashykt): This arg will no longer need to be added after b/173119596 |
| 139 |
// is fixed. |
| 140 |
*args = args->SetIfUnset(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, target_name); |
| 141 |
RefCountedPtr<grpc_channel_security_connector> security_connector; |
| 142 |
auto xds_certificate_provider = args->GetObjectRef<XdsCertificateProvider>(); |
| 143 |
if (xds_certificate_provider != nullptr) { |
| 144 |
std::string cluster_name( |
| 145 |
args->GetString(GRPC_ARG_XDS_CLUSTER_NAME).value()); |
| 146 |
const bool watch_root = |
| 147 |
xds_certificate_provider->ProvidesRootCerts(cluster_name); |
| 148 |
const bool watch_identity = |
| 149 |
xds_certificate_provider->ProvidesIdentityCerts(cluster_name); |
| 150 |
if (watch_root || watch_identity) { |
| 151 |
auto tls_credentials_options = |
| 152 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 153 |
tls_credentials_options->set_certificate_provider( |
| 154 |
xds_certificate_provider); |
| 155 |
if (watch_root) { |
| 156 |
tls_credentials_options->set_watch_root_cert(true); |
| 157 |
tls_credentials_options->set_root_cert_name(cluster_name); |
| 158 |
} |
| 159 |
if (watch_identity) { |
| 160 |
tls_credentials_options->set_watch_identity_pair(true); |
| 161 |
tls_credentials_options->set_identity_cert_name(cluster_name); |
| 162 |
} |
| 163 |
tls_credentials_options->set_verify_server_cert(true); |
| 164 |
tls_credentials_options->set_certificate_verifier( |
| 165 |
MakeRefCounted<XdsCertificateVerifier>(xds_certificate_provider, |
| 166 |
std::move(cluster_name))); |
| 167 |
tls_credentials_options->set_check_call_host(false); |
| 168 |
auto tls_credentials = |
| 169 |
MakeRefCounted<TlsCredentials>(std::move(tls_credentials_options)); |
| 170 |
return tls_credentials->create_security_connector(std::move(call_creds), |
| 171 |
target_name, args); |
| 172 |
} |
| 173 |
} |
| 174 |
GPR_ASSERT(fallback_credentials_ != nullptr); |
| 175 |
return fallback_credentials_->create_security_connector(std::move(call_creds), |
| 176 |
target_name, args); |
| 177 |
} |
| 178 |
|
| 179 |
UniqueTypeName XdsCredentials::Type() { |
| 180 |
static UniqueTypeName::Factory kFactory("Xds"); |
| 181 |
return kFactory.Create(); |
| 182 |
} |
| 183 |
|
| 184 |
// |
| 185 |
// XdsServerCredentials |
| 186 |
// |
| 187 |
|
| 188 |
RefCountedPtr<grpc_server_security_connector> |
| 189 |
XdsServerCredentials::create_security_connector(const ChannelArgs& args) { |
| 190 |
auto xds_certificate_provider = args.GetObjectRef<XdsCertificateProvider>(); |
| 191 |
// Identity certs are a must for TLS. |
| 192 |
if (xds_certificate_provider != nullptr && |
| 193 |
xds_certificate_provider->ProvidesIdentityCerts("")) { |
| 194 |
auto tls_credentials_options = |
| 195 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 196 |
tls_credentials_options->set_watch_identity_pair(true); |
| 197 |
tls_credentials_options->set_certificate_provider(xds_certificate_provider); |
| 198 |
if (xds_certificate_provider->ProvidesRootCerts("")) { |
| 199 |
tls_credentials_options->set_watch_root_cert(true); |
| 200 |
if (xds_certificate_provider->GetRequireClientCertificate("")) { |
| 201 |
tls_credentials_options->set_cert_request_type( |
| 202 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 203 |
} else { |
| 204 |
tls_credentials_options->set_cert_request_type( |
| 205 |
GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY); |
| 206 |
} |
| 207 |
} else { |
| 208 |
// Do not request client certificate if there is no way to verify. |
| 209 |
tls_credentials_options->set_cert_request_type( |
| 210 |
GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 211 |
} |
| 212 |
auto tls_credentials = MakeRefCounted<TlsServerCredentials>( |
| 213 |
std::move(tls_credentials_options)); |
| 214 |
return tls_credentials->create_security_connector(args); |
| 215 |
} |
| 216 |
return fallback_credentials_->create_security_connector(args); |
| 217 |
} |
| 218 |
|
| 219 |
UniqueTypeName XdsServerCredentials::Type() { |
| 220 |
static UniqueTypeName::Factory kFactory("Xds"); |
| 221 |
return kFactory.Create(); |
| 222 |
} |
| 223 |
|
| 224 |
} // namespace grpc_core |
| 225 |
|
| 226 |
grpc_channel_credentials* grpc_xds_credentials_create( |
| 227 |
grpc_channel_credentials* fallback_credentials) { |
| 228 |
GPR_ASSERT(fallback_credentials != nullptr); |
| 229 |
return new grpc_core::XdsCredentials(fallback_credentials->Ref()); |
| 230 |
} |
| 231 |
|
| 232 |
grpc_server_credentials* grpc_xds_server_credentials_create( |
| 233 |
grpc_server_credentials* fallback_credentials) { |
| 234 |
GPR_ASSERT(fallback_credentials != nullptr); |
| 235 |
return new grpc_core::XdsServerCredentials(fallback_credentials->Ref()); |
| 236 |
} |