| Index: net/util/sqlite/sqlite_server_bound_cert_store.cc
|
| diff --git a/chrome/browser/net/sqlite_server_bound_cert_store.cc b/net/util/sqlite/sqlite_server_bound_cert_store.cc
|
| similarity index 74%
|
| rename from chrome/browser/net/sqlite_server_bound_cert_store.cc
|
| rename to net/util/sqlite/sqlite_server_bound_cert_store.cc
|
| index fbac4c36d72405ba4183f7446f913c31cc91ff69..2855de22ae14df0091bf12873f01e9686f4b31b6 100644
|
| --- a/chrome/browser/net/sqlite_server_bound_cert_store.cc
|
| +++ b/net/util/sqlite/sqlite_server_bound_cert_store.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "chrome/browser/net/sqlite_server_bound_cert_store.h"
|
| +#include "net/util/sqlite/sqlite_server_bound_cert_store.h"
|
|
|
| #include <list>
|
| #include <set>
|
| @@ -11,12 +11,12 @@
|
| #include "base/bind.h"
|
| #include "base/file_util.h"
|
| #include "base/files/file_path.h"
|
| +#include "base/location.h"
|
| #include "base/logging.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/metrics/histogram.h"
|
| +#include "base/sequenced_task_runner.h"
|
| #include "base/strings/string_util.h"
|
| -#include "base/threading/thread.h"
|
| -#include "base/threading/thread_restrictions.h"
|
| #include "net/cert/x509_certificate.h"
|
| #include "net/cookies/cookie_util.h"
|
| #include "net/ssl/ssl_client_cert_type.h"
|
| @@ -24,9 +24,9 @@
|
| #include "sql/meta_table.h"
|
| #include "sql/statement.h"
|
| #include "sql/transaction.h"
|
| -#include "third_party/sqlite/sqlite3.h"
|
| #include "url/gurl.h"
|
| -#include "webkit/browser/quota/special_storage_policy.h"
|
| +
|
| +namespace net {
|
|
|
| // This class is designed to be shared between any calling threads and the
|
| // background task runner. It batches operations and commits them on a timer.
|
| @@ -35,13 +35,11 @@ class SQLiteServerBoundCertStore::Backend
|
| public:
|
| Backend(
|
| const base::FilePath& path,
|
| - const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
|
| - quota::SpecialStoragePolicy* special_storage_policy)
|
| + const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
|
| : path_(path),
|
| num_pending_(0),
|
| force_keep_session_state_(false),
|
| background_task_runner_(background_task_runner),
|
| - special_storage_policy_(special_storage_policy),
|
| corruption_detected_(false) {}
|
|
|
| // Creates or loads the SQLite database.
|
| @@ -49,11 +47,11 @@ class SQLiteServerBoundCertStore::Backend
|
|
|
| // Batch a server bound cert addition.
|
| void AddServerBoundCert(
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert);
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert);
|
|
|
| // Batch a server bound cert deletion.
|
| void DeleteServerBoundCert(
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert);
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert);
|
|
|
| // Commit any pending operations and close the database. This must be called
|
| // before the object is destructed.
|
| @@ -62,8 +60,8 @@ class SQLiteServerBoundCertStore::Backend
|
| void SetForceKeepSessionState();
|
|
|
| private:
|
| - void LoadOnDBThread(
|
| - ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs);
|
| + void LoadInBackground(
|
| + ScopedVector<DefaultServerBoundCertStore::ServerBoundCert>* certs);
|
|
|
| friend class base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend>;
|
|
|
| @@ -78,34 +76,29 @@ class SQLiteServerBoundCertStore::Backend
|
|
|
| class PendingOperation {
|
| public:
|
| - typedef enum {
|
| - CERT_ADD,
|
| - CERT_DELETE
|
| - } OperationType;
|
| -
|
| - PendingOperation(
|
| - OperationType op,
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert)
|
| + typedef enum { CERT_ADD, CERT_DELETE } OperationType;
|
| +
|
| + PendingOperation(OperationType op,
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert)
|
| : op_(op), cert_(cert) {}
|
|
|
| OperationType op() const { return op_; }
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert() const {
|
| - return cert_;
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert() const {
|
| + return cert_;
|
| }
|
|
|
| private:
|
| OperationType op_;
|
| - net::DefaultServerBoundCertStore::ServerBoundCert cert_;
|
| + DefaultServerBoundCertStore::ServerBoundCert cert_;
|
| };
|
|
|
| private:
|
| // Batch a server bound cert operation (add or delete).
|
| - void BatchOperation(
|
| - PendingOperation::OperationType op,
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert);
|
| + void BatchOperation(PendingOperation::OperationType op,
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert);
|
| // Commit our pending operations to the database.
|
| void Commit();
|
| - // Close() executed on the background thread.
|
| + // Close() executed on the background task runner.
|
| void InternalBackgroundClose();
|
|
|
| void DeleteCertificatesOnShutdown();
|
| @@ -130,8 +123,6 @@ class SQLiteServerBoundCertStore::Backend
|
|
|
| scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
|
|
|
| - scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
|
| -
|
| // Indicates if the kill-database callback has been scheduled.
|
| bool corruption_detected_;
|
|
|
| @@ -150,13 +141,14 @@ bool InitTable(sql::Connection* db) {
|
| // we renamed this class to SQLiteServerBoundCertStore. Likewise, the primary
|
| // key is "origin", but now can be other things like a plain domain.
|
| if (!db->DoesTableExist("origin_bound_certs")) {
|
| - if (!db->Execute("CREATE TABLE origin_bound_certs ("
|
| - "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
|
| - "private_key BLOB NOT NULL,"
|
| - "cert BLOB NOT NULL,"
|
| - "cert_type INTEGER,"
|
| - "expiration_time INTEGER,"
|
| - "creation_time INTEGER)"))
|
| + if (!db->Execute(
|
| + "CREATE TABLE origin_bound_certs ("
|
| + "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
|
| + "private_key BLOB NOT NULL,"
|
| + "cert BLOB NOT NULL,"
|
| + "cert_type INTEGER,"
|
| + "expiration_time INTEGER,"
|
| + "creation_time INTEGER)"))
|
| return false;
|
| }
|
|
|
| @@ -169,20 +161,19 @@ void SQLiteServerBoundCertStore::Backend::Load(
|
| const LoadedCallback& loaded_callback) {
|
| // This function should be called only once per instance.
|
| DCHECK(!db_.get());
|
| - scoped_ptr<ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> >
|
| - certs(new ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>(
|
| - ));
|
| - ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs_ptr =
|
| + scoped_ptr<ScopedVector<DefaultServerBoundCertStore::ServerBoundCert> > certs(
|
| + new ScopedVector<DefaultServerBoundCertStore::ServerBoundCert>());
|
| + ScopedVector<DefaultServerBoundCertStore::ServerBoundCert>* certs_ptr =
|
| certs.get();
|
|
|
| background_task_runner_->PostTaskAndReply(
|
| FROM_HERE,
|
| - base::Bind(&Backend::LoadOnDBThread, this, certs_ptr),
|
| + base::Bind(&Backend::LoadInBackground, this, certs_ptr),
|
| base::Bind(loaded_callback, base::Passed(&certs)));
|
| }
|
|
|
| -void SQLiteServerBoundCertStore::Backend::LoadOnDBThread(
|
| - ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs) {
|
| +void SQLiteServerBoundCertStore::Backend::LoadInBackground(
|
| + ScopedVector<DefaultServerBoundCertStore::ServerBoundCert>* certs) {
|
| DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
|
|
|
| // This method should be called only once per instance.
|
| @@ -198,7 +189,7 @@ void SQLiteServerBoundCertStore::Backend::LoadOnDBThread(
|
|
|
| int64 db_size = 0;
|
| if (base::GetFileSize(path_, &db_size))
|
| - UMA_HISTOGRAM_COUNTS("DomainBoundCerts.DBSizeInKB", db_size / 1024 );
|
| + UMA_HISTOGRAM_COUNTS("DomainBoundCerts.DBSizeInKB", db_size / 1024);
|
|
|
| db_.reset(new sql::Connection);
|
| db_->set_histogram_tag("DomainBoundCerts");
|
| @@ -240,15 +231,14 @@ void SQLiteServerBoundCertStore::Backend::LoadOnDBThread(
|
| }
|
|
|
| while (smt.Step()) {
|
| - net::SSLClientCertType type =
|
| - static_cast<net::SSLClientCertType>(smt.ColumnInt(3));
|
| - if (type != net::CLIENT_CERT_ECDSA_SIGN)
|
| + SSLClientCertType type = static_cast<SSLClientCertType>(smt.ColumnInt(3));
|
| + if (type != CLIENT_CERT_ECDSA_SIGN)
|
| continue;
|
| std::string private_key_from_db, cert_from_db;
|
| smt.ColumnBlobAsString(1, &private_key_from_db);
|
| smt.ColumnBlobAsString(2, &cert_from_db);
|
| - scoped_ptr<net::DefaultServerBoundCertStore::ServerBoundCert> cert(
|
| - new net::DefaultServerBoundCertStore::ServerBoundCert(
|
| + scoped_ptr<DefaultServerBoundCertStore::ServerBoundCert> cert(
|
| + new DefaultServerBoundCertStore::ServerBoundCert(
|
| smt.ColumnString(0), // origin
|
| base::Time::FromInternalValue(smt.ColumnInt64(5)),
|
| base::Time::FromInternalValue(smt.ColumnInt64(4)),
|
| @@ -272,7 +262,7 @@ void SQLiteServerBoundCertStore::Backend::LoadOnDBThread(
|
| bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() {
|
| // Version check.
|
| if (!meta_table_.Init(
|
| - db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) {
|
| + db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) {
|
| return false;
|
| }
|
|
|
| @@ -286,8 +276,9 @@ bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() {
|
| sql::Transaction transaction(db_.get());
|
| if (!transaction.Begin())
|
| return false;
|
| - if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN cert_type "
|
| - "INTEGER")) {
|
| + if (!db_->Execute(
|
| + "ALTER TABLE origin_bound_certs ADD COLUMN cert_type "
|
| + "INTEGER")) {
|
| LOG(WARNING) << "Unable to update server bound cert database to "
|
| << "version 2.";
|
| return false;
|
| @@ -312,29 +303,30 @@ bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() {
|
| return false;
|
|
|
| if (cur_version == 2) {
|
| - if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN "
|
| - "expiration_time INTEGER")) {
|
| + if (!db_->Execute(
|
| + "ALTER TABLE origin_bound_certs ADD COLUMN "
|
| + "expiration_time INTEGER")) {
|
| LOG(WARNING) << "Unable to update server bound cert database to "
|
| << "version 4.";
|
| return false;
|
| }
|
| }
|
|
|
| - if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN "
|
| - "creation_time INTEGER")) {
|
| + if (!db_->Execute(
|
| + "ALTER TABLE origin_bound_certs ADD COLUMN "
|
| + "creation_time INTEGER")) {
|
| LOG(WARNING) << "Unable to update server bound cert database to "
|
| << "version 4.";
|
| return false;
|
| }
|
|
|
| - sql::Statement smt(db_->GetUniqueStatement(
|
| - "SELECT origin, cert FROM origin_bound_certs"));
|
| + sql::Statement smt(
|
| + db_->GetUniqueStatement("SELECT origin, cert FROM origin_bound_certs"));
|
| sql::Statement update_expires_smt(db_->GetUniqueStatement(
|
| "UPDATE origin_bound_certs SET expiration_time = ? WHERE origin = ?"));
|
| sql::Statement update_creation_smt(db_->GetUniqueStatement(
|
| "UPDATE origin_bound_certs SET creation_time = ? WHERE origin = ?"));
|
| - if (!smt.is_valid() ||
|
| - !update_expires_smt.is_valid() ||
|
| + if (!smt.is_valid() || !update_expires_smt.is_valid() ||
|
| !update_creation_smt.is_valid()) {
|
| LOG(WARNING) << "Unable to update server bound cert database to "
|
| << "version 4.";
|
| @@ -346,9 +338,8 @@ bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() {
|
| std::string cert_from_db;
|
| smt.ColumnBlobAsString(1, &cert_from_db);
|
| // Parse the cert and extract the real value and then update the DB.
|
| - scoped_refptr<net::X509Certificate> cert(
|
| - net::X509Certificate::CreateFromBytes(
|
| - cert_from_db.data(), cert_from_db.size()));
|
| + scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes(
|
| + cert_from_db.data(), cert_from_db.size()));
|
| if (cert.get()) {
|
| if (cur_version == 2) {
|
| update_expires_smt.Reset(true);
|
| @@ -389,9 +380,9 @@ bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() {
|
|
|
| // When the version is too old, we just try to continue anyway, there should
|
| // not be a released product that makes a database too old for us to handle.
|
| - LOG_IF(WARNING, cur_version < kCurrentVersionNumber) <<
|
| - "Server bound cert database version " << cur_version <<
|
| - " is too old to handle.";
|
| + LOG_IF(WARNING, cur_version < kCurrentVersionNumber)
|
| + << "Server bound cert database version " << cur_version
|
| + << " is too old to handle.";
|
|
|
| return true;
|
| }
|
| @@ -432,18 +423,18 @@ void SQLiteServerBoundCertStore::Backend::KillDatabase() {
|
| }
|
|
|
| void SQLiteServerBoundCertStore::Backend::AddServerBoundCert(
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| BatchOperation(PendingOperation::CERT_ADD, cert);
|
| }
|
|
|
| void SQLiteServerBoundCertStore::Backend::DeleteServerBoundCert(
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| BatchOperation(PendingOperation::CERT_DELETE, cert);
|
| }
|
|
|
| void SQLiteServerBoundCertStore::Backend::BatchOperation(
|
| PendingOperation::OperationType op,
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| // Commit every 30 seconds.
|
| static const int kCommitIntervalMs = 30 * 1000;
|
| // Commit right away if we have more than 512 outstanding operations.
|
| @@ -486,14 +477,15 @@ void SQLiteServerBoundCertStore::Backend::Commit() {
|
| if (!db_.get() || ops.empty())
|
| return;
|
|
|
| - sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE,
|
| + sql::Statement add_smt(db_->GetCachedStatement(
|
| + SQL_FROM_HERE,
|
| "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, "
|
| "expiration_time, creation_time) VALUES (?,?,?,?,?,?)"));
|
| if (!add_smt.is_valid())
|
| return;
|
|
|
| - sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE,
|
| - "DELETE FROM origin_bound_certs WHERE origin=?"));
|
| + sql::Statement del_smt(db_->GetCachedStatement(
|
| + SQL_FROM_HERE, "DELETE FROM origin_bound_certs WHERE origin=?"));
|
| if (!del_smt.is_valid())
|
| return;
|
|
|
| @@ -501,8 +493,8 @@ void SQLiteServerBoundCertStore::Backend::Commit() {
|
| if (!transaction.Begin())
|
| return;
|
|
|
| - for (PendingOperationsList::iterator it = ops.begin();
|
| - it != ops.end(); ++it) {
|
| + for (PendingOperationsList::iterator it = ops.begin(); it != ops.end();
|
| + ++it) {
|
| // Free the certs as we commit them to the database.
|
| scoped_ptr<PendingOperation> po(*it);
|
| switch (po->op()) {
|
| @@ -514,7 +506,7 @@ void SQLiteServerBoundCertStore::Backend::Commit() {
|
| add_smt.BindBlob(1, private_key.data(), private_key.size());
|
| const std::string& cert = po->cert().cert();
|
| add_smt.BindBlob(2, cert.data(), cert.size());
|
| - add_smt.BindInt(3, net::CLIENT_CERT_ECDSA_SIGN);
|
| + add_smt.BindInt(3, CLIENT_CERT_ECDSA_SIGN);
|
| add_smt.BindInt64(4, po->cert().expiration_time().ToInternalValue());
|
| add_smt.BindInt64(5, po->cert().creation_time().ToInternalValue());
|
| if (!add_smt.Run())
|
| @@ -537,11 +529,11 @@ void SQLiteServerBoundCertStore::Backend::Commit() {
|
| transaction.Commit();
|
| }
|
|
|
| -// Fire off a close message to the background thread. We could still have a
|
| +// Fire off a close message to the background task runner. We could still have a
|
| // pending commit timer that will be holding a reference on us, but if/when
|
| // this fires we will already have been cleaned up and it will be ignored.
|
| void SQLiteServerBoundCertStore::Backend::Close() {
|
| - // Must close the backend on the background thread.
|
| + // Must close the backend on the background task runner.
|
| background_task_runner_->PostTask(
|
| FROM_HERE, base::Bind(&Backend::InternalBackgroundClose, this));
|
| }
|
| @@ -551,55 +543,9 @@ void SQLiteServerBoundCertStore::Backend::InternalBackgroundClose() {
|
| // Commit any pending operations
|
| Commit();
|
|
|
| - if (!force_keep_session_state_ &&
|
| - special_storage_policy_.get() &&
|
| - special_storage_policy_->HasSessionOnlyOrigins()) {
|
| - DeleteCertificatesOnShutdown();
|
| - }
|
| -
|
| db_.reset();
|
| }
|
|
|
| -void SQLiteServerBoundCertStore::Backend::DeleteCertificatesOnShutdown() {
|
| - DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
|
| -
|
| - if (!db_.get())
|
| - return;
|
| -
|
| - if (cert_origins_.empty())
|
| - return;
|
| -
|
| - if (!special_storage_policy_.get())
|
| - return;
|
| -
|
| - sql::Statement del_smt(db_->GetCachedStatement(
|
| - SQL_FROM_HERE, "DELETE FROM origin_bound_certs WHERE origin=?"));
|
| - if (!del_smt.is_valid()) {
|
| - LOG(WARNING) << "Unable to delete certificates on shutdown.";
|
| - return;
|
| - }
|
| -
|
| - sql::Transaction transaction(db_.get());
|
| - if (!transaction.Begin()) {
|
| - LOG(WARNING) << "Unable to delete certificates on shutdown.";
|
| - return;
|
| - }
|
| -
|
| - for (std::set<std::string>::iterator it = cert_origins_.begin();
|
| - it != cert_origins_.end(); ++it) {
|
| - const GURL url(net::cookie_util::CookieOriginToURL(*it, true));
|
| - if (!url.is_valid() || !special_storage_policy_->IsStorageSessionOnly(url))
|
| - continue;
|
| - del_smt.Reset(true);
|
| - del_smt.BindString(0, *it);
|
| - if (!del_smt.Run())
|
| - NOTREACHED() << "Could not delete a certificate from the DB.";
|
| - }
|
| -
|
| - if (!transaction.Commit())
|
| - LOG(WARNING) << "Unable to delete certificates on shutdown.";
|
| -}
|
| -
|
| void SQLiteServerBoundCertStore::Backend::SetForceKeepSessionState() {
|
| base::AutoLock locked(lock_);
|
| force_keep_session_state_ = true;
|
| @@ -607,24 +553,21 @@ void SQLiteServerBoundCertStore::Backend::SetForceKeepSessionState() {
|
|
|
| SQLiteServerBoundCertStore::SQLiteServerBoundCertStore(
|
| const base::FilePath& path,
|
| - const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
|
| - quota::SpecialStoragePolicy* special_storage_policy)
|
| - : backend_(new Backend(path,
|
| - background_task_runner,
|
| - special_storage_policy)) {}
|
| + const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
|
| + : backend_(new Backend(path, background_task_runner)) {
|
| +}
|
|
|
| -void SQLiteServerBoundCertStore::Load(
|
| - const LoadedCallback& loaded_callback) {
|
| +void SQLiteServerBoundCertStore::Load(const LoadedCallback& loaded_callback) {
|
| backend_->Load(loaded_callback);
|
| }
|
|
|
| void SQLiteServerBoundCertStore::AddServerBoundCert(
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| backend_->AddServerBoundCert(cert);
|
| }
|
|
|
| void SQLiteServerBoundCertStore::DeleteServerBoundCert(
|
| - const net::DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| + const DefaultServerBoundCertStore::ServerBoundCert& cert) {
|
| backend_->DeleteServerBoundCert(cert);
|
| }
|
|
|
| @@ -635,5 +578,7 @@ void SQLiteServerBoundCertStore::SetForceKeepSessionState() {
|
| SQLiteServerBoundCertStore::~SQLiteServerBoundCertStore() {
|
| backend_->Close();
|
| // We release our reference to the Backend, though it will probably still have
|
| - // a reference if the background thread has not run Close() yet.
|
| + // a reference if the background task runner has not run Close() yet.
|
| }
|
| +
|
| +} // namespace net
|
|
|