Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug that validate flag is not set when the SAN(SubjectAltName) matching is performed #16816

Merged
merged 11 commits into from
Jun 22, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ Envoy::Ssl::ClientValidationStatus DefaultCertValidator::verifyCertificate(
validated = Envoy::Ssl::ClientValidationStatus::Validated;
}

if (!subject_alt_name_matchers.empty() && !matchSubjectAltName(cert, subject_alt_name_matchers)) {
stats_.fail_verify_san_.inc();
return Envoy::Ssl::ClientValidationStatus::Failed;
if (!subject_alt_name_matchers.empty()) {
if (!matchSubjectAltName(cert, subject_alt_name_matchers)) {
stats_.fail_verify_san_.inc();
return Envoy::Ssl::ClientValidationStatus::Failed;
}
validated = Envoy::Ssl::ClientValidationStatus::Validated;
}

if (!verify_certificate_hash_list_.empty() || !verify_certificate_spki_list_.empty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ TEST(DefaultCertValidatorTest, TestMatchSubjectAltNameNotMatched) {
EXPECT_FALSE(DefaultCertValidator::matchSubjectAltName(cert.get(), subject_alt_name_matchers));
}

TEST(DefaultCertValidatorTest, TestCertificateVerificationWithSANMatcher) {
Stats::TestUtil::TestStore test_store;
SslStats stats = generateSslStats(test_store);
// Create the default validator object.
auto default_validator =
std::make_unique<Extensions::TransportSockets::Tls::DefaultCertValidator>(
/*CertificateValidationContextConfig=*/nullptr, stats,
Event::GlobalTimeSystem().timeSystem());

bssl::UniquePtr<X509> cert = readCertFromFile(TestEnvironment::substitute(
"{{ test_rundir }}/test/extensions/transport_sockets/tls/test_data/san_dns_cert.pem"));
envoy::type::matcher::v3::StringMatcher matcher;
matcher.MergeFrom(TestUtility::createRegexMatcher(".*.example.com"));
std::vector<Matchers::StringMatcherImpl> san_matchers;
san_matchers.push_back(Matchers::StringMatcherImpl(matcher));
// Verify the certificate with correct SAN regex matcher.
EXPECT_EQ(default_validator->verifyCertificate(cert.get(), /*verify_san_list=*/{}, san_matchers),
Envoy::Ssl::ClientValidationStatus::Validated);
tyxia marked this conversation as resolved.
Show resolved Hide resolved

matcher.MergeFrom(TestUtility::createExactMatcher("hello.example.com"));
std::vector<Matchers::StringMatcherImpl> invalid_san_matchers;
invalid_san_matchers.push_back(Matchers::StringMatcherImpl(matcher));
// Verify the certificate with incorrect SAN exact matcher.
EXPECT_EQ(default_validator->verifyCertificate(cert.get(), /*verify_san_list=*/{},
invalid_san_matchers),
Envoy::Ssl::ClientValidationStatus::Failed);
}

} // namespace Tls
} // namespace TransportSockets
} // namespace Extensions
Expand Down