Commit c6944329 authored by Matt Mueller's avatar Matt Mueller Committed by Commit Bot

Make CertPathBuilder::Run return Result rather than being an out-param of the constructor.

Bug: 410574
Change-Id: I407332f05d591709f5602d92e5697caa48140ab3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1731102
Commit-Queue: Matt Mueller <mattm@chromium.org>
Reviewed-by: default avatarEric Roman <eroman@chromium.org>
Reviewed-by: default avatarDoug Steedman <dougsteed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683697}
parent f497c3fa
...@@ -303,14 +303,13 @@ CastCertError VerifyDeviceCertUsingCustomTrustStore( ...@@ -303,14 +303,13 @@ CastCertError VerifyDeviceCertUsingCustomTrustStore(
net::der::GeneralizedTime verification_time; net::der::GeneralizedTime verification_time;
if (!net::der::EncodeTimeAsGeneralizedTime(time, &verification_time)) if (!net::der::EncodeTimeAsGeneralizedTime(time, &verification_time))
return CastCertError::ERR_UNEXPECTED; return CastCertError::ERR_UNEXPECTED;
net::CertPathBuilder::Result result;
net::CertPathBuilder path_builder( net::CertPathBuilder path_builder(
target_cert.get(), trust_store, &path_builder_delegate, verification_time, target_cert.get(), trust_store, &path_builder_delegate, verification_time,
net::KeyPurpose::CLIENT_AUTH, net::InitialExplicitPolicy::kFalse, net::KeyPurpose::CLIENT_AUTH, net::InitialExplicitPolicy::kFalse,
{net::AnyPolicy()}, net::InitialPolicyMappingInhibit::kFalse, {net::AnyPolicy()}, net::InitialPolicyMappingInhibit::kFalse,
net::InitialAnyPolicyInhibit::kFalse, &result); net::InitialAnyPolicyInhibit::kFalse);
path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source);
path_builder.Run(); net::CertPathBuilder::Result result = path_builder.Run();
if (!result.HasValidPath()) if (!result.HasValidPath())
return MapToCastError(result); return MapToCastError(result);
......
...@@ -137,13 +137,12 @@ bool VerifyCRL(const Crl& crl, ...@@ -137,13 +137,12 @@ bool VerifyCRL(const Crl& crl,
net::SimplePathBuilderDelegate path_builder_delegate( net::SimplePathBuilderDelegate path_builder_delegate(
2048, net::SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); 2048, net::SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1);
net::CertPathBuilder::Result result;
net::CertPathBuilder path_builder( net::CertPathBuilder path_builder(
parsed_cert.get(), trust_store, &path_builder_delegate, verification_time, parsed_cert.get(), trust_store, &path_builder_delegate, verification_time,
net::KeyPurpose::ANY_EKU, net::InitialExplicitPolicy::kFalse, net::KeyPurpose::ANY_EKU, net::InitialExplicitPolicy::kFalse,
{net::AnyPolicy()}, net::InitialPolicyMappingInhibit::kFalse, {net::AnyPolicy()}, net::InitialPolicyMappingInhibit::kFalse,
net::InitialAnyPolicyInhibit::kFalse, &result); net::InitialAnyPolicyInhibit::kFalse);
path_builder.Run(); net::CertPathBuilder::Result result = path_builder.Run();
if (!result.HasValidPath()) { if (!result.HasValidPath()) {
VLOG(2) << "CRL - Issuer certificate verification failed."; VLOG(2) << "CRL - Issuer certificate verification failed.";
// TODO(crbug.com/634443): Log the error information. // TODO(crbug.com/634443): Log the error information.
......
...@@ -421,26 +421,25 @@ struct BuildPathAttempt { ...@@ -421,26 +421,25 @@ struct BuildPathAttempt {
SimplePathBuilderDelegate::DigestPolicy digest_policy; SimplePathBuilderDelegate::DigestPolicy digest_policy;
}; };
void TryBuildPath(const scoped_refptr<ParsedCertificate>& target, CertPathBuilder::Result TryBuildPath(
CertIssuerSourceStatic* intermediates, const scoped_refptr<ParsedCertificate>& target,
SystemTrustStore* ssl_trust_store, CertIssuerSourceStatic* intermediates,
base::Time verification_time, SystemTrustStore* ssl_trust_store,
base::TimeTicks deadline, base::Time verification_time,
VerificationType verification_type, base::TimeTicks deadline,
SimplePathBuilderDelegate::DigestPolicy digest_policy, VerificationType verification_type,
int flags, SimplePathBuilderDelegate::DigestPolicy digest_policy,
const std::string& ocsp_response, int flags,
const CRLSet* crl_set, const std::string& ocsp_response,
CertNetFetcher* net_fetcher, const CRLSet* crl_set,
const EVRootCAMetadata* ev_metadata, CertNetFetcher* net_fetcher,
CertPathBuilder::Result* result, const EVRootCAMetadata* ev_metadata,
bool* checked_revocation) { bool* checked_revocation) {
der::GeneralizedTime der_verification_time; der::GeneralizedTime der_verification_time;
if (!der::EncodeTimeAsGeneralizedTime(verification_time, if (!der::EncodeTimeAsGeneralizedTime(verification_time,
&der_verification_time)) { &der_verification_time)) {
// This shouldn't be possible. // This shouldn't be possible.
*result = CertPathBuilder::Result(); return CertPathBuilder::Result();
return;
} }
// Path building will require candidate paths to conform to at least one of // Path building will require candidate paths to conform to at least one of
...@@ -462,8 +461,7 @@ void TryBuildPath(const scoped_refptr<ParsedCertificate>& target, ...@@ -462,8 +461,7 @@ void TryBuildPath(const scoped_refptr<ParsedCertificate>& target,
target, ssl_trust_store->GetTrustStore(), &path_builder_delegate, target, ssl_trust_store->GetTrustStore(), &path_builder_delegate,
der_verification_time, KeyPurpose::SERVER_AUTH, der_verification_time, KeyPurpose::SERVER_AUTH,
InitialExplicitPolicy::kFalse, user_initial_policy_set, InitialExplicitPolicy::kFalse, user_initial_policy_set,
InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse, InitialPolicyMappingInhibit::kFalse, InitialAnyPolicyInhibit::kFalse);
result);
// Allow the path builder to discover the explicitly provided intermediates in // Allow the path builder to discover the explicitly provided intermediates in
// |input_cert|. // |input_cert|.
...@@ -481,7 +479,7 @@ void TryBuildPath(const scoped_refptr<ParsedCertificate>& target, ...@@ -481,7 +479,7 @@ void TryBuildPath(const scoped_refptr<ParsedCertificate>& target,
path_builder.SetIterationLimit(kPathBuilderIterationLimit); path_builder.SetIterationLimit(kPathBuilderIterationLimit);
path_builder.SetDeadline(deadline); path_builder.SetDeadline(deadline);
path_builder.Run(); return path_builder.Run();
} }
int AssignVerifyResult(X509Certificate* input_cert, int AssignVerifyResult(X509Certificate* input_cert,
...@@ -652,11 +650,11 @@ int CertVerifyProcBuiltin::VerifyInternal( ...@@ -652,11 +650,11 @@ int CertVerifyProcBuiltin::VerifyInternal(
verification_type = cur_attempt.verification_type; verification_type = cur_attempt.verification_type;
// Run the attempt through the path builder. // Run the attempt through the path builder.
TryBuildPath(target, &intermediates, ssl_trust_store.get(), result = TryBuildPath(
verification_time, deadline, cur_attempt.verification_type, target, &intermediates, ssl_trust_store.get(), verification_time,
cur_attempt.digest_policy, flags, ocsp_response, crl_set, deadline, cur_attempt.verification_type, cur_attempt.digest_policy,
net_fetcher_.get(), ev_metadata, &result, flags, ocsp_response, crl_set, net_fetcher_.get(), ev_metadata,
&checked_revocation_for_some_path); &checked_revocation_for_some_path);
if (result.HasValidPath() || result.exceeded_deadline) if (result.HasValidPath() || result.exceeded_deadline)
break; break;
......
...@@ -545,8 +545,7 @@ CertPathBuilder::CertPathBuilder( ...@@ -545,8 +545,7 @@ CertPathBuilder::CertPathBuilder(
InitialExplicitPolicy initial_explicit_policy, InitialExplicitPolicy initial_explicit_policy,
const std::set<der::Input>& user_initial_policy_set, const std::set<der::Input>& user_initial_policy_set,
InitialPolicyMappingInhibit initial_policy_mapping_inhibit, InitialPolicyMappingInhibit initial_policy_mapping_inhibit,
InitialAnyPolicyInhibit initial_any_policy_inhibit, InitialAnyPolicyInhibit initial_any_policy_inhibit)
Result* result)
: cert_path_iter_(new CertPathIter(std::move(cert), trust_store)), : cert_path_iter_(new CertPathIter(std::move(cert), trust_store)),
delegate_(delegate), delegate_(delegate),
time_(time), time_(time),
...@@ -554,10 +553,8 @@ CertPathBuilder::CertPathBuilder( ...@@ -554,10 +553,8 @@ CertPathBuilder::CertPathBuilder(
initial_explicit_policy_(initial_explicit_policy), initial_explicit_policy_(initial_explicit_policy),
user_initial_policy_set_(user_initial_policy_set), user_initial_policy_set_(user_initial_policy_set),
initial_policy_mapping_inhibit_(initial_policy_mapping_inhibit), initial_policy_mapping_inhibit_(initial_policy_mapping_inhibit),
initial_any_policy_inhibit_(initial_any_policy_inhibit), initial_any_policy_inhibit_(initial_any_policy_inhibit) {
out_result_(result) {
DCHECK(delegate); DCHECK(delegate);
*result = Result();
// The TrustStore also implements the CertIssuerSource interface. // The TrustStore also implements the CertIssuerSource interface.
AddCertIssuerSource(trust_store); AddCertIssuerSource(trust_store);
} }
...@@ -577,7 +574,8 @@ void CertPathBuilder::SetDeadline(base::TimeTicks deadline) { ...@@ -577,7 +574,8 @@ void CertPathBuilder::SetDeadline(base::TimeTicks deadline) {
deadline_ = deadline; deadline_ = deadline;
} }
void CertPathBuilder::Run() { CertPathBuilder::Result CertPathBuilder::Run() {
Result result;
uint32_t iteration_count = 0; uint32_t iteration_count = 0;
while (true) { while (true) {
...@@ -589,13 +587,13 @@ void CertPathBuilder::Run() { ...@@ -589,13 +587,13 @@ void CertPathBuilder::Run() {
&iteration_count, max_iteration_count_)) { &iteration_count, max_iteration_count_)) {
// No more paths to check. // No more paths to check.
if (max_iteration_count_ > 0 && iteration_count > max_iteration_count_) { if (max_iteration_count_ > 0 && iteration_count > max_iteration_count_) {
out_result_->exceeded_iteration_limit = true; result.exceeded_iteration_limit = true;
} }
if (!deadline_.is_null() && base::TimeTicks::Now() > deadline_) { if (!deadline_.is_null() && base::TimeTicks::Now() > deadline_) {
out_result_->exceeded_deadline = true; result.exceeded_deadline = true;
} }
RecordIterationCountHistogram(iteration_count); RecordIterationCountHistogram(iteration_count);
return; return result;
} }
// Verify the entire certificate chain. // Verify the entire certificate chain.
...@@ -613,25 +611,26 @@ void CertPathBuilder::Run() { ...@@ -613,25 +611,26 @@ void CertPathBuilder::Run() {
bool path_is_good = result_path->IsValid(); bool path_is_good = result_path->IsValid();
AddResultPath(std::move(result_path)); AddResultPath(std::move(result_path), &result);
if (path_is_good) { if (path_is_good) {
RecordIterationCountHistogram(iteration_count); RecordIterationCountHistogram(iteration_count);
// Found a valid path, return immediately. // Found a valid path, return immediately.
// TODO(mattm): add debug/test mode that tries all possible paths. // TODO(mattm): add debug/test mode that tries all possible paths.
return; return result;
} }
// Path did not verify. Try more paths. // Path did not verify. Try more paths.
} }
} }
void CertPathBuilder::AddResultPath( void CertPathBuilder::AddResultPath(
std::unique_ptr<CertPathBuilderResultPath> result_path) { std::unique_ptr<CertPathBuilderResultPath> result_path,
Result* out_result) {
// TODO(mattm): set best_result_index based on number or severity of errors. // TODO(mattm): set best_result_index based on number or severity of errors.
if (result_path->IsValid()) if (result_path->IsValid())
out_result_->best_result_index = out_result_->paths.size(); out_result->best_result_index = out_result->paths.size();
// TODO(mattm): add flag to only return a single path or all attempted paths? // TODO(mattm): add flag to only return a single path or all attempted paths?
out_result_->paths.push_back(std::move(result_path)); out_result->paths.push_back(std::move(result_path));
} }
} // namespace net } // namespace net
...@@ -141,16 +141,14 @@ class NET_EXPORT CertPathBuilder { ...@@ -141,16 +141,14 @@ class NET_EXPORT CertPathBuilder {
}; };
// Creates a CertPathBuilder that attempts to find a path from |cert| to a // Creates a CertPathBuilder that attempts to find a path from |cert| to a
// trust anchor in |trust_store| and is valid at |time|. Details of attempted // trust anchor in |trust_store| and is valid at |time|.
// path(s) are stored in |*result|.
// //
// The caller must keep |trust_store|, |delegate| and |*result| valid for the // The caller must keep |trust_store| and |delegate| valid for the lifetime
// lifetime of the CertPathBuilder. // of the CertPathBuilder.
// //
// See VerifyCertificateChain() for a more detailed explanation of the // See VerifyCertificateChain() for a more detailed explanation of the
// same-named parameters not defined below. // same-named parameters not defined below.
// //
// * |result|: Storage for the result of path building.
// * |delegate|: Must be non-null. The delegate is called at various points in // * |delegate|: Must be non-null. The delegate is called at various points in
// path building to verify specific parts of certificates or the // path building to verify specific parts of certificates or the
// final chain. See CertPathBuilderDelegate and // final chain. See CertPathBuilderDelegate and
...@@ -163,8 +161,7 @@ class NET_EXPORT CertPathBuilder { ...@@ -163,8 +161,7 @@ class NET_EXPORT CertPathBuilder {
InitialExplicitPolicy initial_explicit_policy, InitialExplicitPolicy initial_explicit_policy,
const std::set<der::Input>& user_initial_policy_set, const std::set<der::Input>& user_initial_policy_set,
InitialPolicyMappingInhibit initial_policy_mapping_inhibit, InitialPolicyMappingInhibit initial_policy_mapping_inhibit,
InitialAnyPolicyInhibit initial_any_policy_inhibit, InitialAnyPolicyInhibit initial_any_policy_inhibit);
Result* result);
~CertPathBuilder(); ~CertPathBuilder();
// Adds a CertIssuerSource to provide intermediates for use in path building. // Adds a CertIssuerSource to provide intermediates for use in path building.
...@@ -188,13 +185,12 @@ class NET_EXPORT CertPathBuilder { ...@@ -188,13 +185,12 @@ class NET_EXPORT CertPathBuilder {
// Executes verification of the target certificate. // Executes verification of the target certificate.
// //
// Upon return results are written to the |result| object passed into the // Run must not be called more than once on each CertPathBuilder instance.
// constructor. Run must not be called more than once on each CertPathBuilder Result Run();
// instance.
void Run();
private: private:
void AddResultPath(std::unique_ptr<CertPathBuilderResultPath> result_path); void AddResultPath(std::unique_ptr<CertPathBuilderResultPath> result_path,
Result* out_result);
std::unique_ptr<CertPathIter> cert_path_iter_; std::unique_ptr<CertPathIter> cert_path_iter_;
CertPathBuilderDelegate* delegate_; CertPathBuilderDelegate* delegate_;
...@@ -207,7 +203,7 @@ class NET_EXPORT CertPathBuilder { ...@@ -207,7 +203,7 @@ class NET_EXPORT CertPathBuilder {
uint32_t max_iteration_count_ = 0; uint32_t max_iteration_count_ = 0;
base::TimeTicks deadline_; base::TimeTicks deadline_;
Result* out_result_; Result out_result_;
DISALLOW_COPY_AND_ASSIGN(CertPathBuilder); DISALLOW_COPY_AND_ASSIGN(CertPathBuilder);
}; };
......
...@@ -157,15 +157,14 @@ class PathBuilderPkitsTestDelegate { ...@@ -157,15 +157,14 @@ class PathBuilderPkitsTestDelegate {
1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); 1024, SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1);
} }
CertPathBuilder::Result result;
CertPathBuilder path_builder( CertPathBuilder path_builder(
std::move(target_cert), &trust_store, path_builder_delegate.get(), std::move(target_cert), &trust_store, path_builder_delegate.get(),
info.time, KeyPurpose::ANY_EKU, info.initial_explicit_policy, info.time, KeyPurpose::ANY_EKU, info.initial_explicit_policy,
info.initial_policy_set, info.initial_policy_mapping_inhibit, info.initial_policy_set, info.initial_policy_mapping_inhibit,
info.initial_inhibit_any_policy, &result); info.initial_inhibit_any_policy);
path_builder.AddCertIssuerSource(&cert_issuer_source); path_builder.AddCertIssuerSource(&cert_issuer_source);
path_builder.Run(); CertPathBuilder::Result result = path_builder.Run();
if (info.should_validate != result.HasValidPath()) { if (info.should_validate != result.HasValidPath()) {
for (size_t i = 0; i < result.paths.size(); ++i) { for (size_t i = 0; i < result.paths.size(); ++i) {
......
This diff is collapsed.
...@@ -42,16 +42,15 @@ class PathBuilderTestDelegate { ...@@ -42,16 +42,15 @@ class PathBuilderTestDelegate {
for (size_t i = 1; i < test.chain.size(); ++i) for (size_t i = 1; i < test.chain.size(); ++i)
intermediate_cert_issuer_source.AddCert(test.chain[i]); intermediate_cert_issuer_source.AddCert(test.chain[i]);
CertPathBuilder::Result result;
// First cert in the |chain| is the target. // First cert in the |chain| is the target.
CertPathBuilder path_builder( CertPathBuilder path_builder(
test.chain.front(), &trust_store, &path_builder_delegate, test.time, test.chain.front(), &trust_store, &path_builder_delegate, test.time,
test.key_purpose, test.initial_explicit_policy, test.key_purpose, test.initial_explicit_policy,
test.user_initial_policy_set, test.initial_policy_mapping_inhibit, test.user_initial_policy_set, test.initial_policy_mapping_inhibit,
test.initial_any_policy_inhibit, &result); test.initial_any_policy_inhibit);
path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source);
path_builder.Run(); CertPathBuilder::Result result = path_builder.Run();
EXPECT_EQ(!test.HasHighSeverityErrors(), result.HasValidPath()); EXPECT_EQ(!test.HasHighSeverityErrors(), result.HasValidPath());
} }
}; };
......
...@@ -166,12 +166,11 @@ bool VerifyUsingPathBuilder( ...@@ -166,12 +166,11 @@ bool VerifyUsingPathBuilder(
// Verify the chain. // Verify the chain.
net::SimplePathBuilderDelegate delegate( net::SimplePathBuilderDelegate delegate(
2048, net::SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1); 2048, net::SimplePathBuilderDelegate::DigestPolicy::kWeakAllowSha1);
net::CertPathBuilder::Result result;
net::CertPathBuilder path_builder( net::CertPathBuilder path_builder(
target_cert, ssl_trust_store->GetTrustStore(), &delegate, time, target_cert, ssl_trust_store->GetTrustStore(), &delegate, time,
net::KeyPurpose::SERVER_AUTH, net::InitialExplicitPolicy::kFalse, net::KeyPurpose::SERVER_AUTH, net::InitialExplicitPolicy::kFalse,
{net::AnyPolicy()}, net::InitialPolicyMappingInhibit::kFalse, {net::AnyPolicy()}, net::InitialPolicyMappingInhibit::kFalse,
net::InitialAnyPolicyInhibit::kFalse, &result); net::InitialAnyPolicyInhibit::kFalse);
path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source);
std::unique_ptr<net::CertIssuerSourceAia> aia_cert_issuer_source; std::unique_ptr<net::CertIssuerSourceAia> aia_cert_issuer_source;
...@@ -182,7 +181,7 @@ bool VerifyUsingPathBuilder( ...@@ -182,7 +181,7 @@ bool VerifyUsingPathBuilder(
} }
// Run the path builder. // Run the path builder.
path_builder.Run(); net::CertPathBuilder::Result result = path_builder.Run();
// TODO(crbug.com/634443): Display any errors/warnings associated with path // TODO(crbug.com/634443): Display any errors/warnings associated with path
// building that were not part of a particular // building that were not part of a particular
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment