Skip to content

Commit 6ca78e3

Browse files
Various small cleanups to PlatformBridgeCertValidator (#2700)
Remove unused next_iteration_callback_ member. Remove unused config_ member. Make some members const. Use hostname instead of host_name. Pass in the list of subject alt names instead of passing in the transport socket options. Risk Level: Low Testing: No behavior change Docs Changes: N/A Release Notes: N/A Signed-off-by: Ryan Hamilton rch@google.com
1 parent 5f347d3 commit 6ca78e3

2 files changed

Lines changed: 42 additions & 47 deletions

File tree

library/common/extensions/cert_validator/platform_bridge/platform_bridge_cert_validator.cc

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ namespace Tls {
1414
PlatformBridgeCertValidator::PlatformBridgeCertValidator(
1515
const Envoy::Ssl::CertificateValidationContextConfig* config, SslStats& stats,
1616
const envoy_cert_validator* platform_validator)
17-
: config_(config), stats_(stats), platform_validator_(platform_validator) {
17+
: allow_untrusted_certificate_(config != nullptr &&
18+
config->trustChainVerification() ==
19+
envoy::extensions::transport_sockets::tls::v3::
20+
CertificateValidationContext::ACCEPT_UNTRUSTED),
21+
platform_validator_(platform_validator), stats_(stats) {
1822
ENVOY_BUG(config != nullptr && config->caCert().empty() &&
1923
config->certificateRevocationList().empty(),
2024
"Invalid certificate validation context config.");
21-
if (config_ != nullptr) {
22-
allow_untrusted_certificate_ = config_->trustChainVerification() ==
23-
envoy::extensions::transport_sockets::tls::v3::
24-
CertificateValidationContext::ACCEPT_UNTRUSTED;
25-
}
2625
}
2726

2827
PlatformBridgeCertValidator::~PlatformBridgeCertValidator() {
@@ -34,17 +33,12 @@ PlatformBridgeCertValidator::~PlatformBridgeCertValidator() {
3433
}
3534
}
3635

37-
int PlatformBridgeCertValidator::initializeSslContexts(std::vector<SSL_CTX*> /*contexts*/,
38-
bool /*handshaker_provides_certificates*/) {
39-
return SSL_VERIFY_PEER;
40-
}
41-
4236
ValidationResults PlatformBridgeCertValidator::doVerifyCertChain(
4337
STACK_OF(X509) & cert_chain, Ssl::ValidateResultCallbackPtr callback,
4438
Ssl::SslExtendedSocketInfo* ssl_extended_info,
4539
const Network::TransportSocketOptionsConstSharedPtr& transport_socket_options,
4640
SSL_CTX& /*ssl_ctx*/, const CertValidator::ExtraValidationContext& /*validation_context*/,
47-
bool is_server, absl::string_view host_name) {
41+
bool is_server, absl::string_view hostname) {
4842
ASSERT(!is_server);
4943
if (sk_X509_num(&cert_chain) == 0) {
5044
if (ssl_extended_info) {
@@ -78,10 +72,18 @@ ValidationResults PlatformBridgeCertValidator::doVerifyCertChain(
7872
!transport_socket_options->verifySubjectAltNameListOverride().empty()) {
7973
host = transport_socket_options->verifySubjectAltNameListOverride()[0];
8074
} else {
81-
host = host_name;
75+
host = hostname;
8276
}
77+
78+
std::vector<std::string> subject_alt_names;
79+
if (transport_socket_options != nullptr) {
80+
subject_alt_names = transport_socket_options->verifySubjectAltNameListOverride();
81+
} else {
82+
subject_alt_names = {std::string(hostname)};
83+
}
84+
8385
auto validation = std::make_unique<PendingValidation>(
84-
*this, std::move(certs), host, std::move(transport_socket_options), std::move(callback));
86+
*this, std::move(certs), host, std::move(subject_alt_names), std::move(callback));
8587
PendingValidation* validation_ptr = validation.get();
8688
validations_.insert(std::move(validation));
8789
std::thread verification_thread(&PendingValidation::verifyCertsByPlatform, validation_ptr);
@@ -91,16 +93,16 @@ ValidationResults PlatformBridgeCertValidator::doVerifyCertChain(
9193
}
9294

9395
void PlatformBridgeCertValidator::verifyCertChainByPlatform(
94-
std::vector<envoy_data>& cert_chain, const std::string& host_name,
96+
const std::vector<envoy_data>& cert_chain, const std::string& hostname,
9597
const std::vector<std::string>& subject_alt_names, PendingValidation& pending_validation) {
9698
ASSERT(!cert_chain.empty());
97-
ENVOY_LOG(trace, "Start verifyCertChainByPlatform for host {}", host_name);
99+
ENVOY_LOG(trace, "Start verifyCertChainByPlatform for host {}", hostname);
98100
// This is running in a stand alone thread other than the engine thread.
99101
envoy_data leaf_cert_der = cert_chain[0];
100102
bssl::UniquePtr<X509> leaf_cert(d2i_X509(
101103
nullptr, const_cast<const unsigned char**>(&leaf_cert_der.bytes), leaf_cert_der.length));
102104
envoy_cert_validation_result result =
103-
platform_validator_->validate_cert(cert_chain.data(), cert_chain.size(), host_name.c_str());
105+
platform_validator_->validate_cert(cert_chain.data(), cert_chain.size(), hostname.c_str());
104106
bool success = result.result == ENVOY_SUCCESS;
105107
if (!success) {
106108
ENVOY_LOG(debug, result.error_details);
@@ -126,20 +128,15 @@ void PlatformBridgeCertValidator::verifyCertChainByPlatform(
126128
}
127129

128130
void PlatformBridgeCertValidator::PendingValidation::verifyCertsByPlatform() {
129-
parent_.verifyCertChainByPlatform(
130-
certs_, host_name_,
131-
(transport_socket_options_ != nullptr
132-
? transport_socket_options_->verifySubjectAltNameListOverride()
133-
: std::vector<std::string>{host_name_}),
134-
*this);
131+
parent_.verifyCertChainByPlatform(certs_, hostname_, subject_alt_names_, *this);
135132
}
136133

137134
void PlatformBridgeCertValidator::PendingValidation::postVerifyResultAndCleanUp(
138135
bool success, absl::string_view error_details, uint8_t tls_alert,
139136
OptRef<Stats::Counter> error_counter) {
140137
ENVOY_LOG(trace,
141138
"Finished platform cert validation for {}, post result callback to network thread",
142-
host_name_);
139+
hostname_);
143140

144141
if (parent_.platform_validator_->validation_cleanup) {
145142
parent_.platform_validator_->validation_cleanup();
@@ -153,7 +150,7 @@ void PlatformBridgeCertValidator::PendingValidation::postVerifyResultAndCleanUp(
153150
if (weak_alive_indicator.expired()) {
154151
return;
155152
}
156-
ENVOY_LOG(trace, "Got validation result for {} from platform", host_name_);
153+
ENVOY_LOG(trace, "Got validation result for {} from platform", hostname_);
157154
parent_.validation_threads_[thread_id].join();
158155
parent_.validation_threads_.erase(thread_id);
159156
if (error_counter.has_value()) {

library/common/extensions/cert_validator/platform_bridge/platform_bridge_cert_validator.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,53 +56,51 @@ class PlatformBridgeCertValidator : public CertValidator, Logger::Loggable<Logge
5656
const Network::TransportSocketOptionsConstSharedPtr& transport_socket_options,
5757
SSL_CTX& ssl_ctx,
5858
const CertValidator::ExtraValidationContext& validation_context, bool is_server,
59-
absl::string_view host_name) override;
60-
// As CA path will not be configured, make sure the return value won’t be SSL_VERIFY_NONE because
61-
// of that, so that doVerifyCertChain() will be called from the TLS stack.
62-
int initializeSslContexts(std::vector<SSL_CTX*> contexts,
63-
bool handshaker_provides_certificates) override;
59+
absl::string_view hostname) override;
60+
// Returns SSL_VERIFY_PEER so that doVerifyCertChain() will be called from the TLS stack.
61+
int initializeSslContexts(std::vector<SSL_CTX*> /*contexts*/,
62+
bool /*handshaker_provides_certificates*/) override {
63+
return SSL_VERIFY_PEER;
64+
}
6465

6566
private:
6667
class PendingValidation {
6768
public:
6869
PendingValidation(PlatformBridgeCertValidator& parent, std::vector<envoy_data> certs,
69-
absl::string_view host_name,
70-
const Network::TransportSocketOptionsConstSharedPtr transport_socket_options,
70+
absl::string_view hostname, std::vector<std::string> subject_alt_names,
7171
Ssl::ValidateResultCallbackPtr result_callback)
72-
: parent_(parent), certs_(std::move(certs)), host_name_(host_name),
73-
result_callback_(std::move(result_callback)),
74-
transport_socket_options_(std::move(transport_socket_options)) {}
72+
: parent_(parent), certs_(std::move(certs)), hostname_(hostname),
73+
subject_alt_names_(std::move(subject_alt_names)),
74+
result_callback_(std::move(result_callback)) {}
7575

7676
// Ensure that this class is never moved or copied to guarantee pointer stability.
7777
PendingValidation(const PendingValidation&) = delete;
7878
PendingValidation(PendingValidation&&) = delete;
7979

80+
// Calls into platform APIs in a stand-alone thread to verify the given certs.
81+
// Once the validation is done, the result will be posted back to the current
82+
// thread to trigger callback and update verify stats.
8083
void verifyCertsByPlatform();
8184

8285
void postVerifyResultAndCleanUp(bool success, absl::string_view error_details,
8386
uint8_t tls_alert, OptRef<Stats::Counter> error_counter);
8487

8588
private:
86-
Event::SchedulableCallbackPtr next_iteration_callback_;
8789
PlatformBridgeCertValidator& parent_;
88-
std::vector<envoy_data> certs_;
89-
std::string host_name_;
90+
const std::vector<envoy_data> certs_;
91+
const std::string hostname_;
92+
const std::vector<std::string> subject_alt_names_;
9093
Ssl::ValidateResultCallbackPtr result_callback_;
91-
const Network::TransportSocketOptionsConstSharedPtr transport_socket_options_;
9294
};
9395

94-
// Calls into platform APIs in a stand-alone thread to verify the given certs.
95-
// Once the validation is done, the result will be posted back to the current
96-
// thread to trigger callback and update verify stats.
97-
void verifyCertChainByPlatform(std::vector<envoy_data>& cert_chain, const std::string& host_name,
96+
void verifyCertChainByPlatform(const std::vector<envoy_data>& cert_chain,
97+
const std::string& hostname,
9898
const std::vector<std::string>& subject_alt_names,
9999
PendingValidation& pending_validation);
100100

101-
const Envoy::Ssl::CertificateValidationContextConfig* config_;
102-
SslStats& stats_;
103-
bool allow_untrusted_certificate_ = false;
104-
// latches the platform extension API.
101+
const bool allow_untrusted_certificate_;
105102
const envoy_cert_validator* platform_validator_;
103+
SslStats& stats_;
106104
absl::flat_hash_map<std::thread::id, std::thread> validation_threads_;
107105
absl::flat_hash_set<std::unique_ptr<PendingValidation>> validations_;
108106
std::shared_ptr<size_t> alive_indicator_{new size_t(1)};

0 commit comments

Comments
 (0)