pki: Pass CertificateTrust by value After https://boringssl-review.googlesource.com/c/boringssl/+/104988, CertificateTrust holds a std::shared_ptr<const TrustedSubtreeSet>, so avoid unnecessary copies where ownership is transferred. Bug: 575507260 Change-Id: Ia8052ce65a9a6c2f62d856ce1a46dd976a6a6964 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106748 Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com> Commit-Queue: Matt Mueller <mattm@google.com> Auto-Submit: Lily Chen <chlily@google.com> Reviewed-by: Matt Mueller <mattm@google.com>
diff --git a/pki/path_builder.cc b/pki/path_builder.cc index a29e7d4..10c69ed 100644 --- a/pki/path_builder.cc +++ b/pki/path_builder.cc
@@ -19,6 +19,7 @@ #include <memory> #include <set> #include <unordered_set> +#include <utility> #include <openssl/base.h> #include <openssl/pki/verify_error.h> @@ -297,9 +298,13 @@ SortRemainingIssuers(); // Still have issuers that haven't been returned yet, return the highest - // priority one (head of remaining list). A reference to the returned issuer - // is retained, since `present_issuers_` points to data owned by it. - *out = issuers_[cur_issuer_++]; + // priority one (head of remaining list). A reference to the returned + // issuer's cert is retained, since `present_issuers_` points to data owned + // by it, while `trust` can be moved out because this entry won't ever be + // returned again from GetNextIssuer. + IssuerEntry &issuer = issuers_[cur_issuer_++]; + *out = {issuer.cert, std::move(issuer.trust), + issuer.trust_and_key_id_match_ordering}; return; } @@ -687,7 +692,7 @@ delegate->DebugLog("CertPathIter returning path:\n" + PathDebugString(*out_certs)); } - *out_last_cert_trust = next_issuer_.trust; + *out_last_cert_trust = std::move(next_issuer_.trust); next_issuer_ = IssuerEntry(); return true; }
diff --git a/pki/trust_store.h b/pki/trust_store.h index 6a5f976..3607559 100644 --- a/pki/trust_store.h +++ b/pki/trust_store.h
@@ -18,6 +18,7 @@ #include <array> #include <memory> #include <optional> +#include <utility> #include <openssl/base.h> #include <openssl/pool.h> @@ -320,12 +321,13 @@ TrustAnchor() = default; // Creates a TrustAnchor with no associated MTCAnchor. - explicit TrustAnchor(CertificateTrust trust) : TrustAnchor(trust, nullptr) {} + explicit TrustAnchor(CertificateTrust trust) + : TrustAnchor(std::move(trust), nullptr) {} // Creates a TrustAnchor with an associated MTCAnchor. TrustAnchor(CertificateTrust trust, std::shared_ptr<const MTCAnchor> mtc_anchor) - : trust_(trust), mtc_anchor_(std::move(mtc_anchor)) {} + : trust_(std::move(trust)), mtc_anchor_(std::move(mtc_anchor)) {} // TODO(nharper): Temporarily add ParsedCertificate member and accessor.
diff --git a/pki/trust_store_collection.cc b/pki/trust_store_collection.cc index bcc7019..29e43a8 100644 --- a/pki/trust_store_collection.cc +++ b/pki/trust_store_collection.cc
@@ -14,6 +14,8 @@ #include "trust_store_collection.h" +#include <utility> + #include <openssl/base.h> BSSL_NAMESPACE_BEGIN @@ -41,7 +43,7 @@ // * If multiple stores consider it trusted, use the trust result from the // last one if (!cur_trust.HasUnspecifiedTrust()) { - result = cur_trust; + result = std::move(cur_trust); if (result.IsDistrusted()) { break; }
diff --git a/pki/trust_store_in_memory.cc b/pki/trust_store_in_memory.cc index e1f2b62..fbe744f 100644 --- a/pki/trust_store_in_memory.cc +++ b/pki/trust_store_in_memory.cc
@@ -17,6 +17,8 @@ #include <openssl/span.h> #include <optional> +#include <string_view> +#include <utility> BSSL_NAMESPACE_BEGIN @@ -109,14 +111,13 @@ } void TrustStoreInMemory::AddCertificate( - std::shared_ptr<const ParsedCertificate> cert, - const CertificateTrust &trust) { - Entry entry; - entry.cert = std::move(cert); - entry.trust = trust; + std::shared_ptr<const ParsedCertificate> cert, CertificateTrust trust) { + std::string_view normalized_subject = + BytesAsStringView(cert->normalized_subject()); // TODO(mattm): should this check for duplicate certificates? - entries_.emplace(BytesAsStringView(entry.cert->normalized_subject()), entry); + entries_.emplace(normalized_subject, + Entry{std::move(cert), std::move(trust)}); } const TrustStoreInMemory::Entry *TrustStoreInMemory::GetEntry(
diff --git a/pki/trust_store_in_memory.h b/pki/trust_store_in_memory.h index 76ed035..e79a74c 100644 --- a/pki/trust_store_in_memory.h +++ b/pki/trust_store_in_memory.h
@@ -42,7 +42,7 @@ // Adds a certificate with the specified trust settings. Both trusted and // distrusted certificates require a full DER match. void AddCertificate(std::shared_ptr<const ParsedCertificate> cert, - const CertificateTrust &trust); + CertificateTrust trust); // Adds a certificate as a trust anchor (only the SPKI and subject will be // used during verification).
diff --git a/pki/trust_store_in_memory_unittest.cc b/pki/trust_store_in_memory_unittest.cc index e394bf2..a1ef67e 100644 --- a/pki/trust_store_in_memory_unittest.cc +++ b/pki/trust_store_in_memory_unittest.cc
@@ -17,6 +17,7 @@ #include <memory> #include <optional> #include <string> +#include <utility> #include <vector> #include <gtest/gtest.h> @@ -118,6 +119,22 @@ trust.ToDebugString()); } +TEST_F(TrustStoreInMemoryTest, AddCertificateMovesTrust) { + TrustStoreInMemory in_memory; + CertificateTrust trust = CertificateTrust::ForTrustAnchor(); + trust.trusted_subtrees = TrustedSubtreeSet::Create({}); + ASSERT_TRUE(trust.trusted_subtrees); + std::weak_ptr<const TrustedSubtreeSet> weak_subtrees = trust.trusted_subtrees; + EXPECT_EQ(weak_subtrees.use_count(), 1); + + in_memory.AddCertificate(newroot_, std::move(trust)); + EXPECT_EQ(weak_subtrees.use_count(), 1); + + CertificateTrust stored = in_memory.GetTrust(newroot_.get()); + EXPECT_EQ(stored.trusted_subtrees.get(), weak_subtrees.lock().get()); + EXPECT_EQ(weak_subtrees.use_count(), 2); +} + TEST_F(TrustStoreInMemoryTest, IsEmptyClear) { TrustStoreInMemory in_memory;