| 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 "src/core/lib/security/security_connector/tls/tls_security_connector.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include <gmock/gmock.h> |
| 25 |
#include <gtest/gtest.h> |
| 26 |
|
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
|
| 31 |
#include "src/core/lib/channel/channel_args.h" |
| 32 |
#include "src/core/lib/gprpp/unique_type_name.h" |
| 33 |
#include "src/core/lib/iomgr/load_file.h" |
| 34 |
#include "src/core/lib/security/context/security_context.h" |
| 35 |
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h" |
| 36 |
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h" |
| 37 |
#include "src/core/lib/security/credentials/tls/tls_credentials.h" |
| 38 |
#include "src/core/lib/security/security_connector/ssl_utils_config.h" |
| 39 |
#include "src/core/tsi/transport_security.h" |
| 40 |
#include "test/core/util/test_config.h" |
| 41 |
#include "test/core/util/tls_utils.h" |
| 42 |
|
| 43 |
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem" |
| 44 |
#define CLIENT_CERT_PATH "src/core/tsi/test_creds/multi-domain.pem" |
| 45 |
#define SERVER_CERT_PATH_0 "src/core/tsi/test_creds/server0.pem" |
| 46 |
#define SERVER_KEY_PATH_0 "src/core/tsi/test_creds/server0.key" |
| 47 |
#define SERVER_CERT_PATH_1 "src/core/tsi/test_creds/server1.pem" |
| 48 |
#define SERVER_KEY_PATH_1 "src/core/tsi/test_creds/server1.key" |
| 49 |
|
| 50 |
namespace grpc_core { |
| 51 |
namespace testing { |
| 52 |
|
| 53 |
constexpr const char* kRootCertName = "root_cert_name"; |
| 54 |
constexpr const char* kIdentityCertName = "identity_cert_name"; |
| 55 |
constexpr const char* kErrorMessage = "error_message"; |
| 56 |
constexpr const char* kTargetName = "foo.bar.com:443"; |
| 57 |
|
| 58 |
class TlsSecurityConnectorTest : public ::testing::Test { |
| 59 |
protected: |
| 60 |
TlsSecurityConnectorTest() {} |
| 61 |
|
| 62 |
void SetUp() override { |
| 63 |
grpc_slice ca_slice_1, ca_slice_0, cert_slice_1, key_slice_1, cert_slice_0, |
| 64 |
key_slice_0; |
| 65 |
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", |
| 66 |
grpc_load_file(CA_CERT_PATH, 1, &ca_slice_1))); |
| 67 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 68 |
"load_file", grpc_load_file(CLIENT_CERT_PATH, 1, &ca_slice_0))); |
| 69 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 70 |
"load_file", grpc_load_file(SERVER_CERT_PATH_1, 1, &cert_slice_1))); |
| 71 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 72 |
"load_file", grpc_load_file(SERVER_KEY_PATH_1, 1, &key_slice_1))); |
| 73 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 74 |
"load_file", grpc_load_file(SERVER_CERT_PATH_0, 1, &cert_slice_0))); |
| 75 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 76 |
"load_file", grpc_load_file(SERVER_KEY_PATH_0, 1, &key_slice_0))); |
| 77 |
root_cert_1_ = std::string(StringViewFromSlice(ca_slice_1)); |
| 78 |
root_cert_0_ = std::string(StringViewFromSlice(ca_slice_0)); |
| 79 |
std::string identity_key_1 = std::string(StringViewFromSlice(key_slice_1)); |
| 80 |
std::string identity_key_0 = std::string(StringViewFromSlice(key_slice_0)); |
| 81 |
std::string identity_cert_1 = |
| 82 |
std::string(StringViewFromSlice(cert_slice_1)); |
| 83 |
std::string identity_cert_0 = |
| 84 |
std::string(StringViewFromSlice(cert_slice_0)); |
| 85 |
identity_pairs_1_.emplace_back(identity_key_1, identity_cert_1); |
| 86 |
identity_pairs_0_.emplace_back(identity_key_0, identity_cert_0); |
| 87 |
grpc_slice_unref(ca_slice_1); |
| 88 |
grpc_slice_unref(ca_slice_0); |
| 89 |
grpc_slice_unref(cert_slice_1); |
| 90 |
grpc_slice_unref(key_slice_1); |
| 91 |
grpc_slice_unref(cert_slice_0); |
| 92 |
grpc_slice_unref(key_slice_0); |
| 93 |
} |
| 94 |
|
| 95 |
static void VerifyExpectedErrorCallback(void* arg, grpc_error_handle error) { |
| 96 |
const char* expected_error_msg = static_cast<const char*>(arg); |
| 97 |
if (expected_error_msg == nullptr) { |
| 98 |
EXPECT_EQ(error, absl::OkStatus()); |
| 99 |
} else { |
| 100 |
EXPECT_EQ(GetErrorMsg(error), expected_error_msg); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static std::string GetErrorMsg(grpc_error_handle error) { |
| 105 |
std::string error_str; |
| 106 |
GPR_ASSERT( |
| 107 |
grpc_error_get_str(error, StatusStrProperty::kDescription, &error_str)); |
| 108 |
return error_str; |
| 109 |
} |
| 110 |
|
| 111 |
std::string root_cert_1_; |
| 112 |
std::string root_cert_0_; |
| 113 |
PemKeyCertPairList identity_pairs_1_; |
| 114 |
PemKeyCertPairList identity_pairs_0_; |
| 115 |
HostNameCertificateVerifier hostname_certificate_verifier_; |
| 116 |
}; |
| 117 |
|
| 118 |
class TlsTestCertificateProvider : public grpc_tls_certificate_provider { |
| 119 |
public: |
| 120 |
explicit TlsTestCertificateProvider( |
| 121 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor) |
| 122 |
: distributor_(std::move(distributor)) {} |
| 123 |
~TlsTestCertificateProvider() override {} |
| 124 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor() const override { |
| 125 |
return distributor_; |
| 126 |
} |
| 127 |
|
| 128 |
UniqueTypeName type() const override { |
| 129 |
static UniqueTypeName::Factory kFactory("tls_test"); |
| 130 |
return kFactory.Create(); |
| 131 |
} |
| 132 |
|
| 133 |
private: |
| 134 |
int CompareImpl(const grpc_tls_certificate_provider* other) const override { |
| 135 |
// TODO(yashykt): Maybe do something better here. |
| 136 |
return QsortCompare(static_cast<const grpc_tls_certificate_provider*>(this), |
| 137 |
other); |
| 138 |
} |
| 139 |
|
| 140 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor_; |
| 141 |
}; |
| 142 |
|
| 143 |
// |
| 144 |
// Tests for Certificate Providers in ChannelSecurityConnector. |
| 145 |
// |
| 146 |
|
| 147 |
TEST_F(TlsSecurityConnectorTest, |
| 148 |
RootAndIdentityCertsObtainedWhenCreateChannelSecurityConnector) { |
| 149 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 150 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 151 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 152 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 153 |
identity_pairs_0_); |
| 154 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 155 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 156 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 157 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 158 |
options->set_certificate_provider(provider); |
| 159 |
options->set_watch_root_cert(true); |
| 160 |
options->set_watch_identity_pair(true); |
| 161 |
options->set_root_cert_name(kRootCertName); |
| 162 |
options->set_identity_cert_name(kIdentityCertName); |
| 163 |
RefCountedPtr<TlsCredentials> credential = |
| 164 |
MakeRefCounted<TlsCredentials>(options); |
| 165 |
ChannelArgs new_args; |
| 166 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 167 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 168 |
EXPECT_NE(connector, nullptr); |
| 169 |
TlsChannelSecurityConnector* tls_connector = |
| 170 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 171 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 172 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 173 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 174 |
distributor->SetKeyMaterials(kRootCertName, root_cert_1_, absl::nullopt); |
| 175 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 176 |
identity_pairs_1_); |
| 177 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 178 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_1_); |
| 179 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_1_); |
| 180 |
} |
| 181 |
|
| 182 |
TEST_F(TlsSecurityConnectorTest, |
| 183 |
SystemRootsWhenCreateChannelSecurityConnector) { |
| 184 |
// Create options watching for no certificates. |
| 185 |
RefCountedPtr<grpc_tls_credentials_options> root_options = |
| 186 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 187 |
RefCountedPtr<TlsCredentials> root_credential = |
| 188 |
MakeRefCounted<TlsCredentials>(root_options); |
| 189 |
ChannelArgs root_new_args; |
| 190 |
RefCountedPtr<grpc_channel_security_connector> root_connector = |
| 191 |
root_credential->create_security_connector(nullptr, "some_target", |
| 192 |
&root_new_args); |
| 193 |
EXPECT_NE(root_connector, nullptr); |
| 194 |
TlsChannelSecurityConnector* tls_root_connector = |
| 195 |
static_cast<TlsChannelSecurityConnector*>(root_connector.get()); |
| 196 |
EXPECT_NE(tls_root_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 197 |
} |
| 198 |
|
| 199 |
TEST_F(TlsSecurityConnectorTest, |
| 200 |
SystemRootsAndIdentityCertsObtainedWhenCreateChannelSecurityConnector) { |
| 201 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 202 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 203 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 204 |
identity_pairs_0_); |
| 205 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 206 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 207 |
// Create options only watching for identity certificates. |
| 208 |
RefCountedPtr<grpc_tls_credentials_options> root_options = |
| 209 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 210 |
root_options->set_certificate_provider(provider); |
| 211 |
root_options->set_watch_identity_pair(true); |
| 212 |
root_options->set_identity_cert_name(kIdentityCertName); |
| 213 |
RefCountedPtr<TlsCredentials> root_credential = |
| 214 |
MakeRefCounted<TlsCredentials>(root_options); |
| 215 |
ChannelArgs root_new_args; |
| 216 |
RefCountedPtr<grpc_channel_security_connector> root_connector = |
| 217 |
root_credential->create_security_connector(nullptr, "some_target", |
| 218 |
&root_new_args); |
| 219 |
EXPECT_NE(root_connector, nullptr); |
| 220 |
TlsChannelSecurityConnector* tls_root_connector = |
| 221 |
static_cast<TlsChannelSecurityConnector*>(root_connector.get()); |
| 222 |
EXPECT_NE(tls_root_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 223 |
EXPECT_EQ(tls_root_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 224 |
// If we have a root update, we shouldn't receive them in security connector, |
| 225 |
// since we claimed to use default system roots. |
| 226 |
distributor->SetKeyMaterials(kRootCertName, root_cert_1_, absl::nullopt); |
| 227 |
EXPECT_NE(tls_root_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 228 |
EXPECT_NE(tls_root_connector->RootCertsForTesting(), root_cert_1_); |
| 229 |
} |
| 230 |
|
| 231 |
TEST_F(TlsSecurityConnectorTest, |
| 232 |
RootCertsObtainedWhenCreateChannelSecurityConnector) { |
| 233 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 234 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 235 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 236 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 237 |
identity_pairs_0_); |
| 238 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 239 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 240 |
// Create options only watching for root certificates. |
| 241 |
RefCountedPtr<grpc_tls_credentials_options> root_options = |
| 242 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 243 |
root_options->set_certificate_provider(provider); |
| 244 |
root_options->set_watch_root_cert(true); |
| 245 |
root_options->set_root_cert_name(kRootCertName); |
| 246 |
RefCountedPtr<TlsCredentials> root_credential = |
| 247 |
MakeRefCounted<TlsCredentials>(root_options); |
| 248 |
ChannelArgs root_new_args; |
| 249 |
RefCountedPtr<grpc_channel_security_connector> root_connector = |
| 250 |
root_credential->create_security_connector(nullptr, "some_target", |
| 251 |
&root_new_args); |
| 252 |
EXPECT_NE(root_connector, nullptr); |
| 253 |
TlsChannelSecurityConnector* tls_root_connector = |
| 254 |
static_cast<TlsChannelSecurityConnector*>(root_connector.get()); |
| 255 |
EXPECT_NE(tls_root_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 256 |
EXPECT_EQ(tls_root_connector->RootCertsForTesting(), root_cert_0_); |
| 257 |
distributor->SetKeyMaterials(kRootCertName, root_cert_1_, absl::nullopt); |
| 258 |
EXPECT_NE(tls_root_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 259 |
EXPECT_EQ(tls_root_connector->RootCertsForTesting(), root_cert_1_); |
| 260 |
} |
| 261 |
|
| 262 |
TEST_F(TlsSecurityConnectorTest, |
| 263 |
CertPartiallyObtainedWhenCreateChannelSecurityConnector) { |
| 264 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 265 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 266 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 267 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 268 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 269 |
// Registered the options watching both certs, but only root certs are |
| 270 |
// available at distributor right now. |
| 271 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 272 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 273 |
options->set_certificate_provider(provider); |
| 274 |
options->set_watch_root_cert(true); |
| 275 |
options->set_watch_identity_pair(true); |
| 276 |
options->set_root_cert_name(kRootCertName); |
| 277 |
options->set_identity_cert_name(kIdentityCertName); |
| 278 |
RefCountedPtr<TlsCredentials> credential = |
| 279 |
MakeRefCounted<TlsCredentials>(options); |
| 280 |
ChannelArgs new_args; |
| 281 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 282 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 283 |
EXPECT_NE(connector, nullptr); |
| 284 |
TlsChannelSecurityConnector* tls_connector = |
| 285 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 286 |
// The client_handshaker_factory_ shouldn't be updated. |
| 287 |
EXPECT_EQ(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 288 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 289 |
// After updating the root certs, the client_handshaker_factory_ should be |
| 290 |
// updated. |
| 291 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 292 |
identity_pairs_0_); |
| 293 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 294 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 295 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 296 |
} |
| 297 |
|
| 298 |
TEST_F(TlsSecurityConnectorTest, |
| 299 |
DistributorHasErrorForChannelSecurityConnector) { |
| 300 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 301 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 302 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 303 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 304 |
identity_pairs_0_); |
| 305 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 306 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 307 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 308 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 309 |
options->set_certificate_provider(provider); |
| 310 |
options->set_watch_root_cert(true); |
| 311 |
options->set_watch_identity_pair(true); |
| 312 |
options->set_root_cert_name(kRootCertName); |
| 313 |
options->set_identity_cert_name(kIdentityCertName); |
| 314 |
RefCountedPtr<TlsCredentials> credential = |
| 315 |
MakeRefCounted<TlsCredentials>(options); |
| 316 |
ChannelArgs new_args; |
| 317 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 318 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 319 |
EXPECT_NE(connector, nullptr); |
| 320 |
TlsChannelSecurityConnector* tls_connector = |
| 321 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 322 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 323 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 324 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 325 |
// Calling SetErrorForCert on distributor shouldn't invalidate the previous |
| 326 |
// valid credentials. |
| 327 |
distributor->SetErrorForCert(kRootCertName, GRPC_ERROR_CREATE(kErrorMessage), |
| 328 |
absl::nullopt); |
| 329 |
distributor->SetErrorForCert(kIdentityCertName, absl::nullopt, |
| 330 |
GRPC_ERROR_CREATE(kErrorMessage)); |
| 331 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 332 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 333 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 334 |
} |
| 335 |
|
| 336 |
TEST_F(TlsSecurityConnectorTest, |
| 337 |
CreateChannelSecurityConnectorFailNoTargetName) { |
| 338 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 339 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 340 |
RefCountedPtr<TlsCredentials> credential = |
| 341 |
MakeRefCounted<TlsCredentials>(options); |
| 342 |
ChannelArgs new_args; |
| 343 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 344 |
credential->create_security_connector(nullptr, nullptr, &new_args); |
| 345 |
EXPECT_EQ(connector, nullptr); |
| 346 |
} |
| 347 |
|
| 348 |
TEST_F(TlsSecurityConnectorTest, |
| 349 |
CreateChannelSecurityConnectorFailNoCredentials) { |
| 350 |
auto connector = |
| 351 |
TlsChannelSecurityConnector::CreateTlsChannelSecurityConnector( |
| 352 |
nullptr, MakeRefCounted<grpc_tls_credentials_options>(), nullptr, |
| 353 |
kTargetName, nullptr, nullptr); |
| 354 |
EXPECT_EQ(connector, nullptr); |
| 355 |
} |
| 356 |
|
| 357 |
TEST_F(TlsSecurityConnectorTest, CreateChannelSecurityConnectorFailNoOptions) { |
| 358 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 359 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 360 |
RefCountedPtr<TlsCredentials> credential = |
| 361 |
MakeRefCounted<TlsCredentials>(options); |
| 362 |
auto connector = |
| 363 |
TlsChannelSecurityConnector::CreateTlsChannelSecurityConnector( |
| 364 |
credential, nullptr, nullptr, kTargetName, nullptr, nullptr); |
| 365 |
EXPECT_EQ(connector, nullptr); |
| 366 |
} |
| 367 |
|
| 368 |
// |
| 369 |
// Tests for Certificate Verifier in ChannelSecurityConnector. |
| 370 |
// |
| 371 |
|
| 372 |
TEST_F(TlsSecurityConnectorTest, |
| 373 |
ChannelSecurityConnectorWithSyncExternalVerifierSucceeds) { |
| 374 |
auto* sync_verifier_ = new SyncExternalVerifier(true); |
| 375 |
ExternalCertificateVerifier core_external_verifier(sync_verifier_->base()); |
| 376 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 377 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 378 |
options->set_verify_server_cert(true); |
| 379 |
options->set_certificate_verifier(core_external_verifier.Ref()); |
| 380 |
options->set_check_call_host(false); |
| 381 |
RefCountedPtr<TlsCredentials> credential = |
| 382 |
MakeRefCounted<TlsCredentials>(options); |
| 383 |
ChannelArgs new_args; |
| 384 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 385 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 386 |
EXPECT_NE(connector, nullptr); |
| 387 |
TlsChannelSecurityConnector* tls_connector = |
| 388 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 389 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 390 |
// Construct a basic TSI Peer. |
| 391 |
tsi_peer peer; |
| 392 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 393 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 394 |
"grpc", strlen("grpc"), |
| 395 |
&peer.properties[0]) == TSI_OK); |
| 396 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 397 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 398 |
&peer.properties[1]) == TSI_OK); |
| 399 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 400 |
ExecCtx exec_ctx; |
| 401 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 402 |
VerifyExpectedErrorCallback, nullptr, grpc_schedule_on_exec_ctx); |
| 403 |
ChannelArgs args; |
| 404 |
tls_connector->check_peer(peer, nullptr, args, &auth_context, |
| 405 |
on_peer_checked); |
| 406 |
} |
| 407 |
|
| 408 |
TEST_F(TlsSecurityConnectorTest, |
| 409 |
ChannelSecurityConnectorWithSyncExternalVerifierFails) { |
| 410 |
auto* sync_verifier_ = new SyncExternalVerifier(false); |
| 411 |
ExternalCertificateVerifier core_external_verifier(sync_verifier_->base()); |
| 412 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 413 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 414 |
options->set_verify_server_cert(true); |
| 415 |
options->set_certificate_verifier(core_external_verifier.Ref()); |
| 416 |
options->set_check_call_host(false); |
| 417 |
RefCountedPtr<TlsCredentials> credential = |
| 418 |
MakeRefCounted<TlsCredentials>(options); |
| 419 |
ChannelArgs new_args; |
| 420 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 421 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 422 |
EXPECT_NE(connector, nullptr); |
| 423 |
TlsChannelSecurityConnector* tls_connector = |
| 424 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 425 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 426 |
// Construct a basic TSI Peer. |
| 427 |
tsi_peer peer; |
| 428 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 429 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 430 |
"grpc", strlen("grpc"), |
| 431 |
&peer.properties[0]) == TSI_OK); |
| 432 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 433 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 434 |
&peer.properties[1]) == TSI_OK); |
| 435 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 436 |
const char* expected_error_msg = |
| 437 |
"Custom verification check failed with error: UNAUTHENTICATED: " |
| 438 |
"SyncExternalVerifier failed"; |
| 439 |
ExecCtx exec_ctx; |
| 440 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 441 |
VerifyExpectedErrorCallback, const_cast<char*>(expected_error_msg), |
| 442 |
grpc_schedule_on_exec_ctx); |
| 443 |
tls_connector->check_peer(peer, nullptr, new_args, &auth_context, |
| 444 |
on_peer_checked); |
| 445 |
} |
| 446 |
|
| 447 |
TEST_F(TlsSecurityConnectorTest, |
| 448 |
CompareChannelSecurityConnectorSucceedsOnSameCredentials) { |
| 449 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 450 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 451 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 452 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 453 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 454 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 455 |
options->set_certificate_provider(provider); |
| 456 |
options->set_watch_root_cert(true); |
| 457 |
options->set_root_cert_name(kRootCertName); |
| 458 |
RefCountedPtr<TlsCredentials> credential = |
| 459 |
MakeRefCounted<TlsCredentials>(options); |
| 460 |
ChannelArgs connector_args; |
| 461 |
ChannelArgs other_connector_args; |
| 462 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 463 |
credential->create_security_connector(nullptr, kTargetName, |
| 464 |
&connector_args); |
| 465 |
RefCountedPtr<grpc_channel_security_connector> other_connector = |
| 466 |
credential->create_security_connector(nullptr, kTargetName, |
| 467 |
&other_connector_args); |
| 468 |
// Comparing the equality of security connectors generated from the same |
| 469 |
// channel credentials with same settings should succeed. |
| 470 |
EXPECT_EQ(connector->cmp(other_connector.get()), 0); |
| 471 |
} |
| 472 |
|
| 473 |
TEST_F(TlsSecurityConnectorTest, |
| 474 |
CompareChannelSecurityConnectorFailsOnDifferentChannelCredentials) { |
| 475 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 476 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 477 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 478 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 479 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 480 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 481 |
options->set_certificate_provider(provider); |
| 482 |
options->set_watch_root_cert(true); |
| 483 |
options->set_root_cert_name(kRootCertName); |
| 484 |
RefCountedPtr<TlsCredentials> credential = |
| 485 |
MakeRefCounted<TlsCredentials>(options); |
| 486 |
ChannelArgs connector_args; |
| 487 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 488 |
credential->create_security_connector(nullptr, kTargetName, |
| 489 |
&connector_args); |
| 490 |
auto other_options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 491 |
other_options->set_certificate_provider(provider); |
| 492 |
other_options->set_watch_root_cert(true); |
| 493 |
other_options->set_root_cert_name(kRootCertName); |
| 494 |
other_options->set_watch_identity_pair(true); |
| 495 |
RefCountedPtr<TlsCredentials> other_credential = |
| 496 |
MakeRefCounted<TlsCredentials>(other_options); |
| 497 |
ChannelArgs other_connector_args; |
| 498 |
RefCountedPtr<grpc_channel_security_connector> other_connector = |
| 499 |
other_credential->create_security_connector(nullptr, kTargetName, |
| 500 |
&other_connector_args); |
| 501 |
// Comparing the equality of security connectors generated from different |
| 502 |
// channel credentials should fail. |
| 503 |
EXPECT_NE(connector->cmp(other_connector.get()), 0); |
| 504 |
} |
| 505 |
|
| 506 |
TEST_F(TlsSecurityConnectorTest, |
| 507 |
CompareChannelSecurityConnectorFailsOnDifferentCallCredentials) { |
| 508 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 509 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 510 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 511 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 512 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 513 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 514 |
options->set_certificate_provider(provider); |
| 515 |
options->set_watch_root_cert(true); |
| 516 |
options->set_root_cert_name(kRootCertName); |
| 517 |
RefCountedPtr<TlsCredentials> credential = |
| 518 |
MakeRefCounted<TlsCredentials>(options); |
| 519 |
ChannelArgs connector_args; |
| 520 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 521 |
credential->create_security_connector(nullptr, kTargetName, |
| 522 |
&connector_args); |
| 523 |
grpc_call_credentials* call_creds = |
| 524 |
grpc_md_only_test_credentials_create("", ""); |
| 525 |
ChannelArgs other_connector_args; |
| 526 |
RefCountedPtr<grpc_channel_security_connector> other_connector = |
| 527 |
credential->create_security_connector( |
| 528 |
RefCountedPtr<grpc_call_credentials>(call_creds), kTargetName, |
| 529 |
&other_connector_args); |
| 530 |
// Comparing the equality of security connectors generated with different call |
| 531 |
// credentials should fail. |
| 532 |
EXPECT_NE(connector->cmp(other_connector.get()), 0); |
| 533 |
} |
| 534 |
|
| 535 |
TEST_F(TlsSecurityConnectorTest, |
| 536 |
CompareChannelSecurityConnectorFailsOnDifferentTargetNames) { |
| 537 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 538 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 539 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 540 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 541 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 542 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 543 |
options->set_certificate_provider(provider); |
| 544 |
options->set_watch_root_cert(true); |
| 545 |
options->set_root_cert_name(kRootCertName); |
| 546 |
RefCountedPtr<TlsCredentials> credential = |
| 547 |
MakeRefCounted<TlsCredentials>(options); |
| 548 |
ChannelArgs connector_args; |
| 549 |
ChannelArgs other_connector_args; |
| 550 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 551 |
credential->create_security_connector(nullptr, kTargetName, |
| 552 |
&connector_args); |
| 553 |
RefCountedPtr<grpc_channel_security_connector> other_connector = |
| 554 |
credential->create_security_connector(nullptr, "", &other_connector_args); |
| 555 |
// Comparing the equality of security connectors generated with different |
| 556 |
// target names should fail. |
| 557 |
EXPECT_NE(connector->cmp(other_connector.get()), 0); |
| 558 |
} |
| 559 |
|
| 560 |
// Tests for ServerSecurityConnector. |
| 561 |
TEST_F(TlsSecurityConnectorTest, |
| 562 |
ChannelSecurityConnectorWithAsyncExternalVerifierSucceeds) { |
| 563 |
auto* async_verifier = new AsyncExternalVerifier(true); |
| 564 |
auto* core_external_verifier = |
| 565 |
new ExternalCertificateVerifier(async_verifier->base()); |
| 566 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 567 |
options->set_verify_server_cert(true); |
| 568 |
options->set_certificate_verifier(core_external_verifier->Ref()); |
| 569 |
options->set_check_call_host(false); |
| 570 |
RefCountedPtr<TlsCredentials> credential = |
| 571 |
MakeRefCounted<TlsCredentials>(options); |
| 572 |
ChannelArgs new_args; |
| 573 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 574 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 575 |
EXPECT_NE(connector, nullptr); |
| 576 |
TlsChannelSecurityConnector* tls_connector = |
| 577 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 578 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 579 |
// Construct a basic TSI Peer. |
| 580 |
tsi_peer peer; |
| 581 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 582 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 583 |
"grpc", strlen("grpc"), |
| 584 |
&peer.properties[0]) == TSI_OK); |
| 585 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 586 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 587 |
&peer.properties[1]) == TSI_OK); |
| 588 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 589 |
ExecCtx exec_ctx; |
| 590 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 591 |
VerifyExpectedErrorCallback, nullptr, grpc_schedule_on_exec_ctx); |
| 592 |
tls_connector->check_peer(peer, nullptr, new_args, &auth_context, |
| 593 |
on_peer_checked); |
| 594 |
core_external_verifier->Unref(); |
| 595 |
} |
| 596 |
|
| 597 |
TEST_F(TlsSecurityConnectorTest, |
| 598 |
ChannelSecurityConnectorWithAsyncExternalVerifierFails) { |
| 599 |
auto* async_verifier = new AsyncExternalVerifier(false); |
| 600 |
auto* core_external_verifier = |
| 601 |
new ExternalCertificateVerifier(async_verifier->base()); |
| 602 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 603 |
options->set_verify_server_cert(true); |
| 604 |
options->set_certificate_verifier(core_external_verifier->Ref()); |
| 605 |
options->set_check_call_host(false); |
| 606 |
RefCountedPtr<TlsCredentials> credential = |
| 607 |
MakeRefCounted<TlsCredentials>(options); |
| 608 |
ChannelArgs new_args; |
| 609 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 610 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 611 |
EXPECT_NE(connector, nullptr); |
| 612 |
TlsChannelSecurityConnector* tls_connector = |
| 613 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 614 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 615 |
// Construct a basic TSI Peer. |
| 616 |
tsi_peer peer; |
| 617 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 618 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 619 |
"grpc", strlen("grpc"), |
| 620 |
&peer.properties[0]) == TSI_OK); |
| 621 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 622 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 623 |
&peer.properties[1]) == TSI_OK); |
| 624 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 625 |
const char* expected_error_msg = |
| 626 |
"Custom verification check failed with error: UNAUTHENTICATED: " |
| 627 |
"AsyncExternalVerifier failed"; |
| 628 |
ExecCtx exec_ctx; |
| 629 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 630 |
VerifyExpectedErrorCallback, const_cast<char*>(expected_error_msg), |
| 631 |
grpc_schedule_on_exec_ctx); |
| 632 |
tls_connector->check_peer(peer, nullptr, new_args, &auth_context, |
| 633 |
on_peer_checked); |
| 634 |
core_external_verifier->Unref(); |
| 635 |
} |
| 636 |
|
| 637 |
TEST_F(TlsSecurityConnectorTest, |
| 638 |
ChannelSecurityConnectorHostnameVerifierSucceeds) { |
| 639 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 640 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 641 |
options->set_verify_server_cert(true); |
| 642 |
options->set_certificate_verifier(hostname_certificate_verifier_.Ref()); |
| 643 |
options->set_check_call_host(false); |
| 644 |
RefCountedPtr<TlsCredentials> credential = |
| 645 |
MakeRefCounted<TlsCredentials>(options); |
| 646 |
ChannelArgs new_args; |
| 647 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 648 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 649 |
EXPECT_NE(connector, nullptr); |
| 650 |
TlsChannelSecurityConnector* tls_connector = |
| 651 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 652 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 653 |
// Construct a full TSI Peer. |
| 654 |
tsi_peer peer; |
| 655 |
GPR_ASSERT(tsi_construct_peer(7, &peer) == TSI_OK); |
| 656 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 657 |
"grpc", strlen("grpc"), |
| 658 |
&peer.properties[0]) == TSI_OK); |
| 659 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 660 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 661 |
&peer.properties[1]) == TSI_OK); |
| 662 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 663 |
TSI_X509_PEM_CERT_PROPERTY, "pem_cert", &peer.properties[2]) == |
| 664 |
TSI_OK); |
| 665 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 666 |
TSI_SECURITY_LEVEL_PEER_PROPERTY, |
| 667 |
tsi_security_level_to_string(TSI_PRIVACY_AND_INTEGRITY), |
| 668 |
&peer.properties[3]) == TSI_OK); |
| 669 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 670 |
TSI_X509_PEM_CERT_CHAIN_PROPERTY, "pem_cert_chain", |
| 671 |
&peer.properties[4]) == TSI_OK); |
| 672 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 673 |
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, "foo.bar.com", |
| 674 |
&peer.properties[5]) == TSI_OK); |
| 675 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 676 |
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, "foo.baz.com", |
| 677 |
&peer.properties[6]) == TSI_OK); |
| 678 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 679 |
ExecCtx exec_ctx; |
| 680 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 681 |
VerifyExpectedErrorCallback, nullptr, grpc_schedule_on_exec_ctx); |
| 682 |
tls_connector->check_peer(peer, nullptr, new_args, &auth_context, |
| 683 |
on_peer_checked); |
| 684 |
} |
| 685 |
|
| 686 |
TEST_F(TlsSecurityConnectorTest, |
| 687 |
ChannelSecurityConnectorHostnameVerifierFails) { |
| 688 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 689 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 690 |
options->set_verify_server_cert(true); |
| 691 |
options->set_certificate_verifier(hostname_certificate_verifier_.Ref()); |
| 692 |
RefCountedPtr<TlsCredentials> credential = |
| 693 |
MakeRefCounted<TlsCredentials>(options); |
| 694 |
ChannelArgs new_args; |
| 695 |
RefCountedPtr<grpc_channel_security_connector> connector = |
| 696 |
credential->create_security_connector(nullptr, kTargetName, &new_args); |
| 697 |
EXPECT_NE(connector, nullptr); |
| 698 |
TlsChannelSecurityConnector* tls_connector = |
| 699 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 700 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 701 |
// Construct a full TSI Peer. |
| 702 |
tsi_peer peer; |
| 703 |
GPR_ASSERT(tsi_construct_peer(7, &peer) == TSI_OK); |
| 704 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 705 |
"grpc", strlen("grpc"), |
| 706 |
&peer.properties[0]) == TSI_OK); |
| 707 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 708 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.com", |
| 709 |
&peer.properties[1]) == TSI_OK); |
| 710 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 711 |
TSI_X509_PEM_CERT_PROPERTY, "pem_cert", &peer.properties[2]) == |
| 712 |
TSI_OK); |
| 713 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 714 |
TSI_SECURITY_LEVEL_PEER_PROPERTY, |
| 715 |
tsi_security_level_to_string(TSI_PRIVACY_AND_INTEGRITY), |
| 716 |
&peer.properties[3]) == TSI_OK); |
| 717 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 718 |
TSI_X509_PEM_CERT_CHAIN_PROPERTY, "pem_cert_chain", |
| 719 |
&peer.properties[4]) == TSI_OK); |
| 720 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 721 |
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, "*.com", |
| 722 |
&peer.properties[5]) == TSI_OK); |
| 723 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 724 |
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, "foo.baz.com", |
| 725 |
&peer.properties[6]) == TSI_OK); |
| 726 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 727 |
const char* expected_error_msg = |
| 728 |
"Custom verification check failed with error: UNAUTHENTICATED: Hostname " |
| 729 |
"Verification " |
| 730 |
"Check failed."; |
| 731 |
ExecCtx exec_ctx; |
| 732 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 733 |
VerifyExpectedErrorCallback, const_cast<char*>(expected_error_msg), |
| 734 |
grpc_schedule_on_exec_ctx); |
| 735 |
tls_connector->check_peer(peer, nullptr, new_args, &auth_context, |
| 736 |
on_peer_checked); |
| 737 |
} |
| 738 |
|
| 739 |
// |
| 740 |
// Tests for Certificate Providers in ServerSecurityConnector. |
| 741 |
// |
| 742 |
|
| 743 |
TEST_F(TlsSecurityConnectorTest, |
| 744 |
RootAndIdentityCertsObtainedWhenCreateServerSecurityConnector) { |
| 745 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 746 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 747 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 748 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 749 |
identity_pairs_0_); |
| 750 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 751 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 752 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 753 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 754 |
options->set_certificate_provider(provider); |
| 755 |
options->set_watch_root_cert(true); |
| 756 |
options->set_watch_identity_pair(true); |
| 757 |
options->set_root_cert_name(kRootCertName); |
| 758 |
options->set_identity_cert_name(kIdentityCertName); |
| 759 |
RefCountedPtr<TlsServerCredentials> credential = |
| 760 |
MakeRefCounted<TlsServerCredentials>(options); |
| 761 |
RefCountedPtr<grpc_server_security_connector> connector = |
| 762 |
credential->create_security_connector(ChannelArgs()); |
| 763 |
EXPECT_NE(connector, nullptr); |
| 764 |
TlsServerSecurityConnector* tls_connector = |
| 765 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 766 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 767 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 768 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 769 |
distributor->SetKeyMaterials(kRootCertName, root_cert_1_, absl::nullopt); |
| 770 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 771 |
identity_pairs_1_); |
| 772 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 773 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_1_); |
| 774 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_1_); |
| 775 |
} |
| 776 |
|
| 777 |
// Note that on server side, we don't have tests watching root certs only, |
| 778 |
// because in TLS, the identity certs should always be presented. If we don't |
| 779 |
// provide, it will try to load certs from some default system locations, and |
| 780 |
// will hence fail on some systems. |
| 781 |
TEST_F(TlsSecurityConnectorTest, |
| 782 |
IdentityCertsObtainedWhenCreateServerSecurityConnector) { |
| 783 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 784 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 785 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 786 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 787 |
identity_pairs_0_); |
| 788 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 789 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 790 |
// Create options only watching for identity certificates. |
| 791 |
RefCountedPtr<grpc_tls_credentials_options> identity_options = |
| 792 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 793 |
identity_options->set_certificate_provider(provider); |
| 794 |
identity_options->set_watch_identity_pair(true); |
| 795 |
identity_options->set_identity_cert_name(kIdentityCertName); |
| 796 |
RefCountedPtr<TlsServerCredentials> identity_credential = |
| 797 |
MakeRefCounted<TlsServerCredentials>(identity_options); |
| 798 |
RefCountedPtr<grpc_server_security_connector> identity_connector = |
| 799 |
identity_credential->create_security_connector(ChannelArgs()); |
| 800 |
EXPECT_NE(identity_connector, nullptr); |
| 801 |
TlsServerSecurityConnector* tls_identity_connector = |
| 802 |
static_cast<TlsServerSecurityConnector*>(identity_connector.get()); |
| 803 |
EXPECT_NE(tls_identity_connector->ServerHandshakerFactoryForTesting(), |
| 804 |
nullptr); |
| 805 |
EXPECT_EQ(tls_identity_connector->KeyCertPairListForTesting(), |
| 806 |
identity_pairs_0_); |
| 807 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 808 |
identity_pairs_1_); |
| 809 |
EXPECT_NE(tls_identity_connector->ServerHandshakerFactoryForTesting(), |
| 810 |
nullptr); |
| 811 |
EXPECT_EQ(tls_identity_connector->KeyCertPairListForTesting(), |
| 812 |
identity_pairs_1_); |
| 813 |
} |
| 814 |
|
| 815 |
TEST_F(TlsSecurityConnectorTest, |
| 816 |
CertPartiallyObtainedWhenCreateServerSecurityConnector) { |
| 817 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 818 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 819 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 820 |
identity_pairs_0_); |
| 821 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 822 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 823 |
// Registered the options watching both certs, but only root certs are |
| 824 |
// available at distributor right now. |
| 825 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 826 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 827 |
options->set_certificate_provider(provider); |
| 828 |
options->set_watch_root_cert(true); |
| 829 |
options->set_watch_identity_pair(true); |
| 830 |
options->set_root_cert_name(kRootCertName); |
| 831 |
options->set_identity_cert_name(kIdentityCertName); |
| 832 |
RefCountedPtr<TlsServerCredentials> credential = |
| 833 |
MakeRefCounted<TlsServerCredentials>(options); |
| 834 |
RefCountedPtr<grpc_server_security_connector> connector = |
| 835 |
credential->create_security_connector(ChannelArgs()); |
| 836 |
EXPECT_NE(connector, nullptr); |
| 837 |
TlsServerSecurityConnector* tls_connector = |
| 838 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 839 |
// The server_handshaker_factory_ shouldn't be updated. |
| 840 |
EXPECT_EQ(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 841 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 842 |
// After updating the root certs, the server_handshaker_factory_ should be |
| 843 |
// updated. |
| 844 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 845 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 846 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 847 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 848 |
} |
| 849 |
|
| 850 |
TEST_F(TlsSecurityConnectorTest, |
| 851 |
DistributorHasErrorForServerSecurityConnector) { |
| 852 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 853 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 854 |
distributor->SetKeyMaterials(kRootCertName, root_cert_0_, absl::nullopt); |
| 855 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 856 |
identity_pairs_0_); |
| 857 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 858 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 859 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 860 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 861 |
options->set_certificate_provider(provider); |
| 862 |
options->set_watch_root_cert(true); |
| 863 |
options->set_watch_identity_pair(true); |
| 864 |
options->set_root_cert_name(kRootCertName); |
| 865 |
options->set_identity_cert_name(kIdentityCertName); |
| 866 |
RefCountedPtr<TlsServerCredentials> credential = |
| 867 |
MakeRefCounted<TlsServerCredentials>(options); |
| 868 |
RefCountedPtr<grpc_server_security_connector> connector = |
| 869 |
credential->create_security_connector(ChannelArgs()); |
| 870 |
EXPECT_NE(connector, nullptr); |
| 871 |
TlsServerSecurityConnector* tls_connector = |
| 872 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 873 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 874 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 875 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 876 |
// Calling SetErrorForCert on distributor shouldn't invalidate the previous |
| 877 |
// valid credentials. |
| 878 |
distributor->SetErrorForCert(kRootCertName, GRPC_ERROR_CREATE(kErrorMessage), |
| 879 |
absl::nullopt); |
| 880 |
distributor->SetErrorForCert(kIdentityCertName, absl::nullopt, |
| 881 |
GRPC_ERROR_CREATE(kErrorMessage)); |
| 882 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 883 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_0_); |
| 884 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), identity_pairs_0_); |
| 885 |
} |
| 886 |
|
| 887 |
TEST_F(TlsSecurityConnectorTest, |
| 888 |
CreateServerSecurityConnectorFailNoCredentials) { |
| 889 |
auto connector = TlsServerSecurityConnector::CreateTlsServerSecurityConnector( |
| 890 |
nullptr, MakeRefCounted<grpc_tls_credentials_options>()); |
| 891 |
EXPECT_EQ(connector, nullptr); |
| 892 |
} |
| 893 |
|
| 894 |
TEST_F(TlsSecurityConnectorTest, CreateServerSecurityConnectorFailNoOptions) { |
| 895 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 896 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 897 |
RefCountedPtr<TlsServerCredentials> credential = |
| 898 |
MakeRefCounted<TlsServerCredentials>(options); |
| 899 |
auto connector = TlsServerSecurityConnector::CreateTlsServerSecurityConnector( |
| 900 |
credential, nullptr); |
| 901 |
EXPECT_EQ(connector, nullptr); |
| 902 |
} |
| 903 |
|
| 904 |
TEST_F(TlsSecurityConnectorTest, |
| 905 |
CompareServerSecurityConnectorSucceedsOnSameCredentials) { |
| 906 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 907 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 908 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 909 |
identity_pairs_0_); |
| 910 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 911 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 912 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 913 |
options->set_certificate_provider(provider); |
| 914 |
options->set_watch_identity_pair(true); |
| 915 |
options->set_identity_cert_name(kIdentityCertName); |
| 916 |
RefCountedPtr<TlsServerCredentials> credential = |
| 917 |
MakeRefCounted<TlsServerCredentials>(options); |
| 918 |
RefCountedPtr<grpc_server_security_connector> connector = |
| 919 |
credential->create_security_connector(ChannelArgs()); |
| 920 |
RefCountedPtr<grpc_server_security_connector> other_connector = |
| 921 |
credential->create_security_connector(ChannelArgs()); |
| 922 |
// Comparing the equality of security connectors generated from the same |
| 923 |
// server credentials with same settings should succeed. |
| 924 |
EXPECT_EQ(connector->cmp(other_connector.get()), 0); |
| 925 |
} |
| 926 |
|
| 927 |
TEST_F(TlsSecurityConnectorTest, |
| 928 |
CompareServerSecurityConnectorFailsOnDifferentServerCredentials) { |
| 929 |
RefCountedPtr<grpc_tls_certificate_distributor> distributor = |
| 930 |
MakeRefCounted<grpc_tls_certificate_distributor>(); |
| 931 |
distributor->SetKeyMaterials(kIdentityCertName, absl::nullopt, |
| 932 |
identity_pairs_0_); |
| 933 |
RefCountedPtr<grpc_tls_certificate_provider> provider = |
| 934 |
MakeRefCounted<TlsTestCertificateProvider>(distributor); |
| 935 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 936 |
options->set_certificate_provider(provider); |
| 937 |
options->set_watch_identity_pair(true); |
| 938 |
options->set_identity_cert_name(kIdentityCertName); |
| 939 |
RefCountedPtr<TlsServerCredentials> credential = |
| 940 |
MakeRefCounted<TlsServerCredentials>(options); |
| 941 |
RefCountedPtr<grpc_server_security_connector> connector = |
| 942 |
credential->create_security_connector(ChannelArgs()); |
| 943 |
RefCountedPtr<TlsServerCredentials> other_credential = |
| 944 |
MakeRefCounted<TlsServerCredentials>(options); |
| 945 |
RefCountedPtr<grpc_server_security_connector> other_connector = |
| 946 |
other_credential->create_security_connector(ChannelArgs()); |
| 947 |
// Comparing the equality of security connectors generated from different |
| 948 |
// server credentials should fail. |
| 949 |
EXPECT_NE(connector->cmp(other_connector.get()), 0); |
| 950 |
} |
| 951 |
|
| 952 |
// |
| 953 |
// Tests for Certificate Verifier in ServerSecurityConnector. |
| 954 |
// |
| 955 |
|
| 956 |
TEST_F(TlsSecurityConnectorTest, |
| 957 |
ServerSecurityConnectorWithSyncExternalVerifierSucceeds) { |
| 958 |
auto* sync_verifier = new SyncExternalVerifier(true); |
| 959 |
ExternalCertificateVerifier core_external_verifier(sync_verifier->base()); |
| 960 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 961 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 962 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 963 |
options->set_certificate_verifier(core_external_verifier.Ref()); |
| 964 |
auto provider = |
| 965 |
MakeRefCounted<StaticDataCertificateProvider>("", PemKeyCertPairList()); |
| 966 |
options->set_certificate_provider(std::move(provider)); |
| 967 |
options->set_watch_identity_pair(true); |
| 968 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 969 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 970 |
// Construct a basic TSI Peer. |
| 971 |
tsi_peer peer; |
| 972 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 973 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 974 |
"grpc", strlen("grpc"), |
| 975 |
&peer.properties[0]) == TSI_OK); |
| 976 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 977 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 978 |
&peer.properties[1]) == TSI_OK); |
| 979 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 980 |
ExecCtx exec_ctx; |
| 981 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 982 |
VerifyExpectedErrorCallback, nullptr, grpc_schedule_on_exec_ctx); |
| 983 |
ChannelArgs args; |
| 984 |
connector->check_peer(peer, nullptr, args, &auth_context, on_peer_checked); |
| 985 |
} |
| 986 |
|
| 987 |
TEST_F(TlsSecurityConnectorTest, |
| 988 |
ServerSecurityConnectorWithSyncExternalVerifierFails) { |
| 989 |
auto* sync_verifier = new SyncExternalVerifier(false); |
| 990 |
ExternalCertificateVerifier core_external_verifier(sync_verifier->base()); |
| 991 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 992 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 993 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 994 |
options->set_certificate_verifier(core_external_verifier.Ref()); |
| 995 |
auto provider = |
| 996 |
MakeRefCounted<StaticDataCertificateProvider>("", PemKeyCertPairList()); |
| 997 |
options->set_certificate_provider(std::move(provider)); |
| 998 |
options->set_watch_identity_pair(true); |
| 999 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 1000 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 1001 |
// Construct a basic TSI Peer. |
| 1002 |
tsi_peer peer; |
| 1003 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 1004 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 1005 |
"grpc", strlen("grpc"), |
| 1006 |
&peer.properties[0]) == TSI_OK); |
| 1007 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 1008 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 1009 |
&peer.properties[1]) == TSI_OK); |
| 1010 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 1011 |
const char* expected_error_msg = |
| 1012 |
"Custom verification check failed with error: UNAUTHENTICATED: " |
| 1013 |
"SyncExternalVerifier failed"; |
| 1014 |
ExecCtx exec_ctx; |
| 1015 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 1016 |
VerifyExpectedErrorCallback, const_cast<char*>(expected_error_msg), |
| 1017 |
grpc_schedule_on_exec_ctx); |
| 1018 |
ChannelArgs args; |
| 1019 |
connector->check_peer(peer, nullptr, args, &auth_context, on_peer_checked); |
| 1020 |
} |
| 1021 |
|
| 1022 |
TEST_F(TlsSecurityConnectorTest, |
| 1023 |
ServerSecurityConnectorWithAsyncExternalVerifierSucceeds) { |
| 1024 |
auto* async_verifier = new AsyncExternalVerifier(true); |
| 1025 |
auto* core_external_verifier = |
| 1026 |
new ExternalCertificateVerifier(async_verifier->base()); |
| 1027 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 1028 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 1029 |
options->set_certificate_verifier(core_external_verifier->Ref()); |
| 1030 |
auto provider = |
| 1031 |
MakeRefCounted<StaticDataCertificateProvider>("", PemKeyCertPairList()); |
| 1032 |
options->set_certificate_provider(std::move(provider)); |
| 1033 |
options->set_watch_identity_pair(true); |
| 1034 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 1035 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 1036 |
// Construct a basic TSI Peer. |
| 1037 |
tsi_peer peer; |
| 1038 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 1039 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 1040 |
"grpc", strlen("grpc"), |
| 1041 |
&peer.properties[0]) == TSI_OK); |
| 1042 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 1043 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 1044 |
&peer.properties[1]) == TSI_OK); |
| 1045 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 1046 |
ExecCtx exec_ctx; |
| 1047 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 1048 |
VerifyExpectedErrorCallback, nullptr, grpc_schedule_on_exec_ctx); |
| 1049 |
ChannelArgs args; |
| 1050 |
connector->check_peer(peer, nullptr, args, &auth_context, on_peer_checked); |
| 1051 |
core_external_verifier->Unref(); |
| 1052 |
} |
| 1053 |
|
| 1054 |
TEST_F(TlsSecurityConnectorTest, |
| 1055 |
ServerSecurityConnectorWithAsyncExternalVerifierFails) { |
| 1056 |
auto* async_verifier = new AsyncExternalVerifier(false); |
| 1057 |
auto* core_external_verifier = |
| 1058 |
new ExternalCertificateVerifier(async_verifier->base()); |
| 1059 |
RefCountedPtr<grpc_tls_credentials_options> options = |
| 1060 |
MakeRefCounted<grpc_tls_credentials_options>(); |
| 1061 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 1062 |
options->set_certificate_verifier(core_external_verifier->Ref()); |
| 1063 |
auto provider = |
| 1064 |
MakeRefCounted<StaticDataCertificateProvider>("", PemKeyCertPairList()); |
| 1065 |
options->set_certificate_provider(std::move(provider)); |
| 1066 |
options->set_watch_identity_pair(true); |
| 1067 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 1068 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 1069 |
// Construct a basic TSI Peer. |
| 1070 |
tsi_peer peer; |
| 1071 |
GPR_ASSERT(tsi_construct_peer(2, &peer) == TSI_OK); |
| 1072 |
GPR_ASSERT(tsi_construct_string_peer_property(TSI_SSL_ALPN_SELECTED_PROTOCOL, |
| 1073 |
"grpc", strlen("grpc"), |
| 1074 |
&peer.properties[0]) == TSI_OK); |
| 1075 |
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring( |
| 1076 |
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, "foo.bar.com", |
| 1077 |
&peer.properties[1]) == TSI_OK); |
| 1078 |
RefCountedPtr<grpc_auth_context> auth_context; |
| 1079 |
const char* expected_error_msg = |
| 1080 |
"Custom verification check failed with error: UNAUTHENTICATED: " |
| 1081 |
"AsyncExternalVerifier failed"; |
| 1082 |
ExecCtx exec_ctx; |
| 1083 |
grpc_closure* on_peer_checked = GRPC_CLOSURE_CREATE( |
| 1084 |
VerifyExpectedErrorCallback, const_cast<char*>(expected_error_msg), |
| 1085 |
grpc_schedule_on_exec_ctx); |
| 1086 |
ChannelArgs args; |
| 1087 |
connector->check_peer(peer, nullptr, args, &auth_context, on_peer_checked); |
| 1088 |
core_external_verifier->Unref(); |
| 1089 |
} |
| 1090 |
|
| 1091 |
} // namespace testing |
| 1092 |
} // namespace grpc_core |
| 1093 |
|
| 1094 |
int main(int argc, char** argv) { |
| 1095 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 1096 |
GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, CA_CERT_PATH); |
| 1097 |
::testing::InitGoogleTest(&argc, argv); |
| 1098 |
grpc_init(); |
| 1099 |
int ret = RUN_ALL_TESTS(); |
| 1100 |
grpc_shutdown(); |
| 1101 |
return ret; |
| 1102 |
} |