Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/sqlite_channel_id_store.h" | 5 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/location.h" | |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/sequenced_task_runner.h" | |
| 17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 18 #include "base/threading/thread.h" | |
| 19 #include "base/threading/thread_restrictions.h" | |
| 20 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
| 21 #include "net/cookies/cookie_util.h" | 21 #include "net/cookies/cookie_util.h" |
| 22 #include "net/ssl/ssl_client_cert_type.h" | 22 #include "net/ssl/ssl_client_cert_type.h" |
| 23 #include "sql/error_delegate_util.h" | 23 #include "sql/error_delegate_util.h" |
| 24 #include "sql/meta_table.h" | 24 #include "sql/meta_table.h" |
| 25 #include "sql/statement.h" | 25 #include "sql/statement.h" |
| 26 #include "sql/transaction.h" | 26 #include "sql/transaction.h" |
| 27 #include "third_party/sqlite/sqlite3.h" | |
| 28 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 29 #include "webkit/browser/quota/special_storage_policy.h" | 28 |
| 29 namespace net { | |
| 30 | 30 |
| 31 // This class is designed to be shared between any calling threads and the | 31 // This class is designed to be shared between any calling threads and the |
| 32 // background task runner. It batches operations and commits them on a timer. | 32 // background task runner. It batches operations and commits them on a timer. |
| 33 class SQLiteChannelIDStore::Backend | 33 class SQLiteChannelIDStore::Backend |
| 34 : public base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend> { | 34 : public base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend> { |
| 35 public: | 35 public: |
| 36 Backend( | 36 Backend( |
| 37 const base::FilePath& path, | 37 const base::FilePath& path, |
| 38 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 38 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) |
| 39 quota::SpecialStoragePolicy* special_storage_policy) | |
| 40 : path_(path), | 39 : path_(path), |
| 41 num_pending_(0), | 40 num_pending_(0), |
| 42 force_keep_session_state_(false), | 41 force_keep_session_state_(false), |
| 43 background_task_runner_(background_task_runner), | 42 background_task_runner_(background_task_runner), |
| 44 special_storage_policy_(special_storage_policy), | |
| 45 corruption_detected_(false) {} | 43 corruption_detected_(false) {} |
| 46 | 44 |
| 47 // Creates or loads the SQLite database. | 45 // Creates or loads the SQLite database. |
| 48 void Load(const LoadedCallback& loaded_callback); | 46 void Load(const LoadedCallback& loaded_callback); |
| 49 | 47 |
| 50 // Batch a channel ID addition. | 48 // Batch a channel ID addition. |
| 51 void AddChannelID( | 49 void AddChannelID(const DefaultChannelIDStore::ChannelID& channel_id); |
| 52 const net::DefaultChannelIDStore::ChannelID& channel_id); | |
| 53 | 50 |
| 54 // Batch a channel ID deletion. | 51 // Batch a channel ID deletion. |
| 55 void DeleteChannelID( | 52 void DeleteChannelID(const DefaultChannelIDStore::ChannelID& channel_id); |
| 56 const net::DefaultChannelIDStore::ChannelID& channel_id); | |
| 57 | 53 |
| 58 // Commit any pending operations and close the database. This must be called | 54 // Commit any pending operations and close the database. This must be called |
| 59 // before the object is destructed. | 55 // before the object is destructed. |
| 60 void Close(); | 56 void Close(); |
| 61 | 57 |
| 62 void SetForceKeepSessionState(); | 58 void SetForceKeepSessionState(); |
| 63 | 59 |
| 64 private: | 60 private: |
| 65 void LoadOnDBThread( | 61 void LoadInBackground( |
| 66 ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids); | 62 ScopedVector<DefaultChannelIDStore::ChannelID>* channel_ids); |
|
mmenke
2014/07/31 15:24:07
Should include the header for ScopedVector
mef
2014/07/31 21:12:22
Done.
| |
| 67 | 63 |
| 68 friend class base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend>; | 64 friend class base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend>; |
| 69 | 65 |
| 70 // You should call Close() before destructing this object. | 66 // You should call Close() before destructing this object. |
| 71 ~Backend() { | 67 ~Backend() { |
| 72 DCHECK(!db_.get()) << "Close should have already been called."; | 68 DCHECK(!db_.get()) << "Close should have already been called."; |
| 73 DCHECK(num_pending_ == 0 && pending_.empty()); | 69 DCHECK(num_pending_ == 0 && pending_.empty()); |
|
mmenke
2014/07/31 15:24:07
nit: This should be split into two DCHECKs.
mef
2014/07/31 21:12:22
Done.
| |
| 74 } | 70 } |
|
mmenke
2014/07/31 15:24:06
Destructor should go before other methods in this
mef
2014/07/31 21:12:22
Done.
| |
| 75 | 71 |
| 76 // Database upgrade statements. | 72 // Database upgrade statements. |
| 77 bool EnsureDatabaseVersion(); | 73 bool EnsureDatabaseVersion(); |
| 78 | 74 |
| 79 class PendingOperation { | 75 class PendingOperation { |
| 80 public: | 76 public: |
| 81 typedef enum { | 77 typedef enum { CHANNEL_ID_ADD, CHANNEL_ID_DELETE } OperationType; |
|
mmenke
2014/07/31 15:24:07
nit: While here, enums are generally defined usin
mef
2014/07/31 21:12:22
Done.
| |
| 82 CHANNEL_ID_ADD, | |
| 83 CHANNEL_ID_DELETE | |
| 84 } OperationType; | |
| 85 | 78 |
| 86 PendingOperation( | 79 PendingOperation(OperationType op, |
| 87 OperationType op, | 80 const DefaultChannelIDStore::ChannelID& channel_id) |
| 88 const net::DefaultChannelIDStore::ChannelID& channel_id) | |
| 89 : op_(op), channel_id_(channel_id) {} | 81 : op_(op), channel_id_(channel_id) {} |
| 90 | 82 |
| 91 OperationType op() const { return op_; } | 83 OperationType op() const { return op_; } |
| 92 const net::DefaultChannelIDStore::ChannelID& channel_id() const { | 84 const DefaultChannelIDStore::ChannelID& channel_id() const { |
| 93 return channel_id_; | 85 return channel_id_; |
| 94 } | 86 } |
| 95 | 87 |
| 96 private: | 88 private: |
| 97 OperationType op_; | 89 OperationType op_; |
| 98 net::DefaultChannelIDStore::ChannelID channel_id_; | 90 DefaultChannelIDStore::ChannelID channel_id_; |
| 99 }; | 91 }; |
| 100 | 92 |
| 101 private: | 93 private: |
| 102 // Batch a channel id operation (add or delete). | 94 // Batch a channel id operation (add or delete). |
| 103 void BatchOperation( | 95 void BatchOperation(PendingOperation::OperationType op, |
| 104 PendingOperation::OperationType op, | 96 const DefaultChannelIDStore::ChannelID& channel_id); |
| 105 const net::DefaultChannelIDStore::ChannelID& channel_id); | |
| 106 // Commit our pending operations to the database. | 97 // Commit our pending operations to the database. |
| 107 void Commit(); | 98 void Commit(); |
| 108 // Close() executed on the background thread. | 99 // Close() executed on the background task runner. |
| 109 void InternalBackgroundClose(); | 100 void InternalBackgroundClose(); |
| 110 | 101 |
| 111 void DeleteCertificatesOnShutdown(); | 102 void DeleteCertificatesOnShutdown(); |
| 112 | 103 |
| 113 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 104 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
| 114 void KillDatabase(); | 105 void KillDatabase(); |
| 115 | 106 |
| 116 base::FilePath path_; | 107 base::FilePath path_; |
|
mmenke
2014/07/31 15:24:06
const?
mef
2014/07/31 21:12:22
Done.
| |
| 117 scoped_ptr<sql::Connection> db_; | 108 scoped_ptr<sql::Connection> db_; |
| 118 sql::MetaTable meta_table_; | 109 sql::MetaTable meta_table_; |
| 119 | 110 |
| 120 typedef std::list<PendingOperation*> PendingOperationsList; | 111 typedef std::list<PendingOperation*> PendingOperationsList; |
| 121 PendingOperationsList pending_; | 112 PendingOperationsList pending_; |
| 122 PendingOperationsList::size_type num_pending_; | 113 PendingOperationsList::size_type num_pending_; |
| 123 // True if the persistent store should skip clear on exit rules. | 114 // True if the persistent store should skip clear on exit rules. |
| 124 bool force_keep_session_state_; | 115 bool force_keep_session_state_; |
| 125 // Guard |pending_|, |num_pending_| and |force_keep_session_state_|. | 116 // Guard |pending_|, |num_pending_| and |force_keep_session_state_|. |
| 126 base::Lock lock_; | 117 base::Lock lock_; |
| 127 | 118 |
| 128 // Cache of origins we have channel IDs stored for. | 119 // Cache of origins we have channel IDs stored for. |
| 129 std::set<std::string> channel_id_origins_; | 120 std::set<std::string> channel_id_origins_; |
|
mmenke
2014/07/31 15:24:07
Should include <string>
mef
2014/07/31 21:12:22
Done.
| |
| 130 | 121 |
| 131 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 122 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 132 | 123 |
| 133 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | |
| 134 | |
| 135 // Indicates if the kill-database callback has been scheduled. | 124 // Indicates if the kill-database callback has been scheduled. |
| 136 bool corruption_detected_; | 125 bool corruption_detected_; |
| 137 | 126 |
| 138 DISALLOW_COPY_AND_ASSIGN(Backend); | 127 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 139 }; | 128 }; |
| 140 | 129 |
| 141 // Version number of the database. | 130 // Version number of the database. |
| 142 static const int kCurrentVersionNumber = 4; | 131 static const int kCurrentVersionNumber = 4; |
| 143 static const int kCompatibleVersionNumber = 1; | 132 static const int kCompatibleVersionNumber = 1; |
|
mmenke
2014/07/31 15:24:06
Get rid of the static, and move into the namespace
mef
2014/07/31 21:12:22
Done.
| |
| 144 | 133 |
| 145 namespace { | 134 namespace { |
|
mmenke
2014/07/31 15:24:06
This should go at the top of the file.
mef
2014/07/31 21:12:22
Done.
| |
| 146 | 135 |
| 147 // Initializes the certs table, returning true on success. | 136 // Initializes the certs table, returning true on success. |
| 148 bool InitTable(sql::Connection* db) { | 137 bool InitTable(sql::Connection* db) { |
| 149 // The table is named "origin_bound_certs" for backwards compatability before | 138 // The table is named "origin_bound_certs" for backwards compatability before |
| 150 // we renamed this class to SQLiteChannelIDStore. Likewise, the primary | 139 // we renamed this class to SQLiteChannelIDStore. Likewise, the primary |
| 151 // key is "origin", but now can be other things like a plain domain. | 140 // key is "origin", but now can be other things like a plain domain. |
| 152 if (!db->DoesTableExist("origin_bound_certs")) { | 141 if (!db->DoesTableExist("origin_bound_certs")) { |
| 153 if (!db->Execute("CREATE TABLE origin_bound_certs (" | 142 if (!db->Execute( |
| 154 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," | 143 "CREATE TABLE origin_bound_certs (" |
| 155 "private_key BLOB NOT NULL," | 144 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," |
| 156 "cert BLOB NOT NULL," | 145 "private_key BLOB NOT NULL," |
| 157 "cert_type INTEGER," | 146 "cert BLOB NOT NULL," |
| 158 "expiration_time INTEGER," | 147 "cert_type INTEGER," |
| 159 "creation_time INTEGER)")) | 148 "expiration_time INTEGER," |
| 149 "creation_time INTEGER)")) | |
| 160 return false; | 150 return false; |
| 161 } | 151 } |
| 162 | 152 |
| 163 return true; | 153 return true; |
| 164 } | 154 } |
| 165 | 155 |
| 166 } // namespace | 156 } // namespace |
| 167 | 157 |
| 168 void SQLiteChannelIDStore::Backend::Load( | 158 void SQLiteChannelIDStore::Backend::Load( |
| 169 const LoadedCallback& loaded_callback) { | 159 const LoadedCallback& loaded_callback) { |
| 170 // This function should be called only once per instance. | 160 // This function should be called only once per instance. |
| 171 DCHECK(!db_.get()); | 161 DCHECK(!db_.get()); |
| 172 scoped_ptr<ScopedVector<net::DefaultChannelIDStore::ChannelID> > | 162 scoped_ptr<ScopedVector<DefaultChannelIDStore::ChannelID> > channel_ids( |
| 173 channel_ids(new ScopedVector<net::DefaultChannelIDStore::ChannelID>()); | 163 new ScopedVector<DefaultChannelIDStore::ChannelID>()); |
| 174 ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids_ptr = | 164 ScopedVector<DefaultChannelIDStore::ChannelID>* channel_ids_ptr = |
| 175 channel_ids.get(); | 165 channel_ids.get(); |
| 176 | 166 |
| 177 background_task_runner_->PostTaskAndReply( | 167 background_task_runner_->PostTaskAndReply( |
| 178 FROM_HERE, | 168 FROM_HERE, |
| 179 base::Bind(&Backend::LoadOnDBThread, this, channel_ids_ptr), | 169 base::Bind(&Backend::LoadInBackground, this, channel_ids_ptr), |
| 180 base::Bind(loaded_callback, base::Passed(&channel_ids))); | 170 base::Bind(loaded_callback, base::Passed(&channel_ids))); |
| 181 } | 171 } |
| 182 | 172 |
| 183 void SQLiteChannelIDStore::Backend::LoadOnDBThread( | 173 void SQLiteChannelIDStore::Backend::LoadInBackground( |
| 184 ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) { | 174 ScopedVector<DefaultChannelIDStore::ChannelID>* channel_ids) { |
| 185 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 175 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 186 | 176 |
| 187 // This method should be called only once per instance. | 177 // This method should be called only once per instance. |
| 188 DCHECK(!db_.get()); | 178 DCHECK(!db_.get()); |
| 189 | 179 |
| 190 base::TimeTicks start = base::TimeTicks::Now(); | 180 base::TimeTicks start = base::TimeTicks::Now(); |
| 191 | 181 |
| 192 // Ensure the parent directory for storing certs is created before reading | 182 // Ensure the parent directory for storing certs is created before reading |
| 193 // from it. | 183 // from it. |
| 194 const base::FilePath dir = path_.DirName(); | 184 const base::FilePath dir = path_.DirName(); |
| 195 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) | 185 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) |
| 196 return; | 186 return; |
| 197 | 187 |
| 198 int64 db_size = 0; | 188 int64 db_size = 0; |
| 199 if (base::GetFileSize(path_, &db_size)) | 189 if (base::GetFileSize(path_, &db_size)) |
| 200 UMA_HISTOGRAM_COUNTS("DomainBoundCerts.DBSizeInKB", db_size / 1024 ); | 190 UMA_HISTOGRAM_COUNTS("DomainBoundCerts.DBSizeInKB", db_size / 1024); |
| 201 | 191 |
| 202 db_.reset(new sql::Connection); | 192 db_.reset(new sql::Connection); |
| 203 db_->set_histogram_tag("DomainBoundCerts"); | 193 db_->set_histogram_tag("DomainBoundCerts"); |
| 204 | 194 |
| 205 // Unretained to avoid a ref loop with db_. | 195 // Unretained to avoid a ref loop with db_. |
| 206 db_->set_error_callback( | 196 db_->set_error_callback( |
| 207 base::Bind(&SQLiteChannelIDStore::Backend::DatabaseErrorCallback, | 197 base::Bind(&SQLiteChannelIDStore::Backend::DatabaseErrorCallback, |
| 208 base::Unretained(this))); | 198 base::Unretained(this))); |
| 209 | 199 |
| 210 if (!db_->Open(path_)) { | 200 if (!db_->Open(path_)) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 232 "creation_time FROM origin_bound_certs")); | 222 "creation_time FROM origin_bound_certs")); |
| 233 if (!smt.is_valid()) { | 223 if (!smt.is_valid()) { |
| 234 if (corruption_detected_) | 224 if (corruption_detected_) |
| 235 KillDatabase(); | 225 KillDatabase(); |
| 236 meta_table_.Reset(); | 226 meta_table_.Reset(); |
| 237 db_.reset(); | 227 db_.reset(); |
| 238 return; | 228 return; |
| 239 } | 229 } |
| 240 | 230 |
| 241 while (smt.Step()) { | 231 while (smt.Step()) { |
| 242 net::SSLClientCertType type = | 232 SSLClientCertType type = static_cast<SSLClientCertType>(smt.ColumnInt(3)); |
| 243 static_cast<net::SSLClientCertType>(smt.ColumnInt(3)); | 233 if (type != CLIENT_CERT_ECDSA_SIGN) |
| 244 if (type != net::CLIENT_CERT_ECDSA_SIGN) | |
| 245 continue; | 234 continue; |
| 246 std::string private_key_from_db, cert_from_db; | 235 std::string private_key_from_db, cert_from_db; |
| 247 smt.ColumnBlobAsString(1, &private_key_from_db); | 236 smt.ColumnBlobAsString(1, &private_key_from_db); |
| 248 smt.ColumnBlobAsString(2, &cert_from_db); | 237 smt.ColumnBlobAsString(2, &cert_from_db); |
| 249 scoped_ptr<net::DefaultChannelIDStore::ChannelID> channel_id( | 238 scoped_ptr<DefaultChannelIDStore::ChannelID> channel_id( |
| 250 new net::DefaultChannelIDStore::ChannelID( | 239 new DefaultChannelIDStore::ChannelID( |
| 251 smt.ColumnString(0), // origin | 240 smt.ColumnString(0), // origin |
| 252 base::Time::FromInternalValue(smt.ColumnInt64(5)), | 241 base::Time::FromInternalValue(smt.ColumnInt64(5)), |
| 253 base::Time::FromInternalValue(smt.ColumnInt64(4)), | 242 base::Time::FromInternalValue(smt.ColumnInt64(4)), |
| 254 private_key_from_db, | 243 private_key_from_db, |
| 255 cert_from_db)); | 244 cert_from_db)); |
| 256 channel_id_origins_.insert(channel_id->server_identifier()); | 245 channel_id_origins_.insert(channel_id->server_identifier()); |
| 257 channel_ids->push_back(channel_id.release()); | 246 channel_ids->push_back(channel_id.release()); |
| 258 } | 247 } |
| 259 | 248 |
| 260 UMA_HISTOGRAM_COUNTS_10000("DomainBoundCerts.DBLoadedCount", | 249 UMA_HISTOGRAM_COUNTS_10000( |
| 261 channel_ids->size()); | 250 "DomainBoundCerts.DBLoadedCount", |
| 251 static_cast<base::HistogramBase::Sample>(channel_ids->size())); | |
| 262 base::TimeDelta load_time = base::TimeTicks::Now() - start; | 252 base::TimeDelta load_time = base::TimeTicks::Now() - start; |
| 263 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.DBLoadTime", | 253 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.DBLoadTime", |
| 264 load_time, | 254 load_time, |
| 265 base::TimeDelta::FromMilliseconds(1), | 255 base::TimeDelta::FromMilliseconds(1), |
| 266 base::TimeDelta::FromMinutes(1), | 256 base::TimeDelta::FromMinutes(1), |
| 267 50); | 257 50); |
| 268 DVLOG(1) << "loaded " << channel_ids->size() << " in " | 258 DVLOG(1) << "loaded " << channel_ids->size() << " in " |
| 269 << load_time.InMilliseconds() << " ms"; | 259 << load_time.InMilliseconds() << " ms"; |
| 270 } | 260 } |
| 271 | 261 |
| 272 bool SQLiteChannelIDStore::Backend::EnsureDatabaseVersion() { | 262 bool SQLiteChannelIDStore::Backend::EnsureDatabaseVersion() { |
| 273 // Version check. | 263 // Version check. |
| 274 if (!meta_table_.Init( | 264 if (!meta_table_.Init( |
| 275 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 265 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
| 276 return false; | 266 return false; |
| 277 } | 267 } |
| 278 | 268 |
| 279 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 269 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
| 280 LOG(WARNING) << "Server bound cert database is too new."; | 270 LOG(WARNING) << "Server bound cert database is too new."; |
| 281 return false; | 271 return false; |
| 282 } | 272 } |
| 283 | 273 |
| 284 int cur_version = meta_table_.GetVersionNumber(); | 274 int cur_version = meta_table_.GetVersionNumber(); |
| 285 if (cur_version == 1) { | 275 if (cur_version == 1) { |
| 286 sql::Transaction transaction(db_.get()); | 276 sql::Transaction transaction(db_.get()); |
| 287 if (!transaction.Begin()) | 277 if (!transaction.Begin()) |
| 288 return false; | 278 return false; |
| 289 if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN cert_type " | 279 if (!db_->Execute( |
| 290 "INTEGER")) { | 280 "ALTER TABLE origin_bound_certs ADD COLUMN cert_type " |
| 281 "INTEGER")) { | |
| 291 LOG(WARNING) << "Unable to update server bound cert database to " | 282 LOG(WARNING) << "Unable to update server bound cert database to " |
| 292 << "version 2."; | 283 << "version 2."; |
| 293 return false; | 284 return false; |
| 294 } | 285 } |
| 295 // All certs in version 1 database are rsa_sign, which are unsupported. | 286 // All certs in version 1 database are rsa_sign, which are unsupported. |
| 296 // Just discard them all. | 287 // Just discard them all. |
| 297 if (!db_->Execute("DELETE from origin_bound_certs")) { | 288 if (!db_->Execute("DELETE from origin_bound_certs")) { |
| 298 LOG(WARNING) << "Unable to update server bound cert database to " | 289 LOG(WARNING) << "Unable to update server bound cert database to " |
| 299 << "version 2."; | 290 << "version 2."; |
| 300 return false; | 291 return false; |
| 301 } | 292 } |
| 302 ++cur_version; | 293 ++cur_version; |
| 303 meta_table_.SetVersionNumber(cur_version); | 294 meta_table_.SetVersionNumber(cur_version); |
| 304 meta_table_.SetCompatibleVersionNumber( | 295 meta_table_.SetCompatibleVersionNumber( |
| 305 std::min(cur_version, kCompatibleVersionNumber)); | 296 std::min(cur_version, kCompatibleVersionNumber)); |
| 306 transaction.Commit(); | 297 transaction.Commit(); |
| 307 } | 298 } |
| 308 | 299 |
| 309 if (cur_version <= 3) { | 300 if (cur_version <= 3) { |
| 310 sql::Transaction transaction(db_.get()); | 301 sql::Transaction transaction(db_.get()); |
| 311 if (!transaction.Begin()) | 302 if (!transaction.Begin()) |
| 312 return false; | 303 return false; |
| 313 | 304 |
| 314 if (cur_version == 2) { | 305 if (cur_version == 2) { |
| 315 if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN " | 306 if (!db_->Execute( |
| 316 "expiration_time INTEGER")) { | 307 "ALTER TABLE origin_bound_certs ADD COLUMN " |
| 308 "expiration_time INTEGER")) { | |
| 317 LOG(WARNING) << "Unable to update server bound cert database to " | 309 LOG(WARNING) << "Unable to update server bound cert database to " |
| 318 << "version 4."; | 310 << "version 4."; |
| 319 return false; | 311 return false; |
| 320 } | 312 } |
| 321 } | 313 } |
| 322 | 314 |
| 323 if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN " | 315 if (!db_->Execute( |
| 324 "creation_time INTEGER")) { | 316 "ALTER TABLE origin_bound_certs ADD COLUMN " |
| 317 "creation_time INTEGER")) { | |
| 325 LOG(WARNING) << "Unable to update server bound cert database to " | 318 LOG(WARNING) << "Unable to update server bound cert database to " |
| 326 << "version 4."; | 319 << "version 4."; |
| 327 return false; | 320 return false; |
| 328 } | 321 } |
| 329 | 322 |
| 330 sql::Statement smt(db_->GetUniqueStatement( | 323 sql::Statement smt( |
|
mmenke
2014/07/31 15:24:06
"smt" seems to violate the naming guidelines. Sug
mef
2014/07/31 21:12:22
Done.
| |
| 331 "SELECT origin, cert FROM origin_bound_certs")); | 324 db_->GetUniqueStatement("SELECT origin, cert FROM origin_bound_certs")); |
| 332 sql::Statement update_expires_smt(db_->GetUniqueStatement( | 325 sql::Statement update_expires_smt(db_->GetUniqueStatement( |
| 333 "UPDATE origin_bound_certs SET expiration_time = ? WHERE origin = ?")); | 326 "UPDATE origin_bound_certs SET expiration_time = ? WHERE origin = ?")); |
| 334 sql::Statement update_creation_smt(db_->GetUniqueStatement( | 327 sql::Statement update_creation_smt(db_->GetUniqueStatement( |
| 335 "UPDATE origin_bound_certs SET creation_time = ? WHERE origin = ?")); | 328 "UPDATE origin_bound_certs SET creation_time = ? WHERE origin = ?")); |
| 336 if (!smt.is_valid() || | 329 if (!smt.is_valid() || !update_expires_smt.is_valid() || |
| 337 !update_expires_smt.is_valid() || | |
| 338 !update_creation_smt.is_valid()) { | 330 !update_creation_smt.is_valid()) { |
| 339 LOG(WARNING) << "Unable to update server bound cert database to " | 331 LOG(WARNING) << "Unable to update server bound cert database to " |
| 340 << "version 4."; | 332 << "version 4."; |
| 341 return false; | 333 return false; |
| 342 } | 334 } |
| 343 | 335 |
| 344 while (smt.Step()) { | 336 while (smt.Step()) { |
| 345 std::string origin = smt.ColumnString(0); | 337 std::string origin = smt.ColumnString(0); |
| 346 std::string cert_from_db; | 338 std::string cert_from_db; |
| 347 smt.ColumnBlobAsString(1, &cert_from_db); | 339 smt.ColumnBlobAsString(1, &cert_from_db); |
| 348 // Parse the cert and extract the real value and then update the DB. | 340 // Parse the cert and extract the real value and then update the DB. |
| 349 scoped_refptr<net::X509Certificate> cert( | 341 scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes( |
| 350 net::X509Certificate::CreateFromBytes( | 342 cert_from_db.data(), static_cast<int>(cert_from_db.size()))); |
| 351 cert_from_db.data(), cert_from_db.size())); | |
| 352 if (cert.get()) { | 343 if (cert.get()) { |
| 353 if (cur_version == 2) { | 344 if (cur_version == 2) { |
| 354 update_expires_smt.Reset(true); | 345 update_expires_smt.Reset(true); |
| 355 update_expires_smt.BindInt64(0, | 346 update_expires_smt.BindInt64(0, |
| 356 cert->valid_expiry().ToInternalValue()); | 347 cert->valid_expiry().ToInternalValue()); |
| 357 update_expires_smt.BindString(1, origin); | 348 update_expires_smt.BindString(1, origin); |
| 358 if (!update_expires_smt.Run()) { | 349 if (!update_expires_smt.Run()) { |
| 359 LOG(WARNING) << "Unable to update server bound cert database to " | 350 LOG(WARNING) << "Unable to update server bound cert database to " |
| 360 << "version 4."; | 351 << "version 4."; |
| 361 return false; | 352 return false; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 382 meta_table_.SetVersionNumber(cur_version); | 373 meta_table_.SetVersionNumber(cur_version); |
| 383 meta_table_.SetCompatibleVersionNumber( | 374 meta_table_.SetCompatibleVersionNumber( |
| 384 std::min(cur_version, kCompatibleVersionNumber)); | 375 std::min(cur_version, kCompatibleVersionNumber)); |
| 385 transaction.Commit(); | 376 transaction.Commit(); |
| 386 } | 377 } |
| 387 | 378 |
| 388 // Put future migration cases here. | 379 // Put future migration cases here. |
| 389 | 380 |
| 390 // When the version is too old, we just try to continue anyway, there should | 381 // When the version is too old, we just try to continue anyway, there should |
| 391 // not be a released product that makes a database too old for us to handle. | 382 // not be a released product that makes a database too old for us to handle. |
| 392 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) << | 383 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) |
| 393 "Server bound cert database version " << cur_version << | 384 << "Server bound cert database version " << cur_version |
| 394 " is too old to handle."; | 385 << " is too old to handle."; |
| 395 | 386 |
| 396 return true; | 387 return true; |
| 397 } | 388 } |
| 398 | 389 |
| 399 void SQLiteChannelIDStore::Backend::DatabaseErrorCallback( | 390 void SQLiteChannelIDStore::Backend::DatabaseErrorCallback( |
| 400 int error, | 391 int error, |
| 401 sql::Statement* stmt) { | 392 sql::Statement* stmt) { |
| 402 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 393 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 403 | 394 |
| 404 if (!sql::IsErrorCatastrophic(error)) | 395 if (!sql::IsErrorCatastrophic(error)) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 425 // This Backend will now be in-memory only. In a future run the database | 416 // This Backend will now be in-memory only. In a future run the database |
| 426 // will be recreated. Hopefully things go better then! | 417 // will be recreated. Hopefully things go better then! |
| 427 bool success = db_->RazeAndClose(); | 418 bool success = db_->RazeAndClose(); |
| 428 UMA_HISTOGRAM_BOOLEAN("DomainBoundCerts.KillDatabaseResult", success); | 419 UMA_HISTOGRAM_BOOLEAN("DomainBoundCerts.KillDatabaseResult", success); |
| 429 meta_table_.Reset(); | 420 meta_table_.Reset(); |
| 430 db_.reset(); | 421 db_.reset(); |
| 431 } | 422 } |
| 432 } | 423 } |
| 433 | 424 |
| 434 void SQLiteChannelIDStore::Backend::AddChannelID( | 425 void SQLiteChannelIDStore::Backend::AddChannelID( |
| 435 const net::DefaultChannelIDStore::ChannelID& channel_id) { | 426 const DefaultChannelIDStore::ChannelID& channel_id) { |
| 436 BatchOperation(PendingOperation::CHANNEL_ID_ADD, channel_id); | 427 BatchOperation(PendingOperation::CHANNEL_ID_ADD, channel_id); |
| 437 } | 428 } |
| 438 | 429 |
| 439 void SQLiteChannelIDStore::Backend::DeleteChannelID( | 430 void SQLiteChannelIDStore::Backend::DeleteChannelID( |
| 440 const net::DefaultChannelIDStore::ChannelID& channel_id) { | 431 const DefaultChannelIDStore::ChannelID& channel_id) { |
| 441 BatchOperation(PendingOperation::CHANNEL_ID_DELETE, channel_id); | 432 BatchOperation(PendingOperation::CHANNEL_ID_DELETE, channel_id); |
| 442 } | 433 } |
| 443 | 434 |
| 444 void SQLiteChannelIDStore::Backend::BatchOperation( | 435 void SQLiteChannelIDStore::Backend::BatchOperation( |
| 445 PendingOperation::OperationType op, | 436 PendingOperation::OperationType op, |
| 446 const net::DefaultChannelIDStore::ChannelID& channel_id) { | 437 const DefaultChannelIDStore::ChannelID& channel_id) { |
| 447 // Commit every 30 seconds. | 438 // Commit every 30 seconds. |
| 448 static const int kCommitIntervalMs = 30 * 1000; | 439 static const int kCommitIntervalMs = 30 * 1000; |
| 449 // Commit right away if we have more than 512 outstanding operations. | 440 // Commit right away if we have more than 512 outstanding operations. |
| 450 static const size_t kCommitAfterBatchSize = 512; | 441 static const size_t kCommitAfterBatchSize = 512; |
| 451 | 442 |
| 452 // We do a full copy of the cert here, and hopefully just here. | 443 // We do a full copy of the cert here, and hopefully just here. |
| 453 scoped_ptr<PendingOperation> po(new PendingOperation(op, channel_id)); | 444 scoped_ptr<PendingOperation> po(new PendingOperation(op, channel_id)); |
| 454 | 445 |
| 455 PendingOperationsList::size_type num_pending; | 446 PendingOperationsList::size_type num_pending; |
| 456 { | 447 { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 479 { | 470 { |
| 480 base::AutoLock locked(lock_); | 471 base::AutoLock locked(lock_); |
| 481 pending_.swap(ops); | 472 pending_.swap(ops); |
| 482 num_pending_ = 0; | 473 num_pending_ = 0; |
| 483 } | 474 } |
| 484 | 475 |
| 485 // Maybe an old timer fired or we are already Close()'ed. | 476 // Maybe an old timer fired or we are already Close()'ed. |
| 486 if (!db_.get() || ops.empty()) | 477 if (!db_.get() || ops.empty()) |
| 487 return; | 478 return; |
| 488 | 479 |
| 489 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 480 sql::Statement add_smt(db_->GetCachedStatement( |
| 481 SQL_FROM_HERE, | |
| 490 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, " | 482 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, " |
| 491 "expiration_time, creation_time) VALUES (?,?,?,?,?,?)")); | 483 "expiration_time, creation_time) VALUES (?,?,?,?,?,?)")); |
| 492 if (!add_smt.is_valid()) | 484 if (!add_smt.is_valid()) |
| 493 return; | 485 return; |
| 494 | 486 |
| 495 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 487 sql::Statement del_smt(db_->GetCachedStatement( |
| 496 "DELETE FROM origin_bound_certs WHERE origin=?")); | 488 SQL_FROM_HERE, "DELETE FROM origin_bound_certs WHERE origin=?")); |
| 497 if (!del_smt.is_valid()) | 489 if (!del_smt.is_valid()) |
| 498 return; | 490 return; |
| 499 | 491 |
| 500 sql::Transaction transaction(db_.get()); | 492 sql::Transaction transaction(db_.get()); |
| 501 if (!transaction.Begin()) | 493 if (!transaction.Begin()) |
| 502 return; | 494 return; |
| 503 | 495 |
| 504 for (PendingOperationsList::iterator it = ops.begin(); | 496 for (PendingOperationsList::iterator it = ops.begin(); it != ops.end(); |
| 505 it != ops.end(); ++it) { | 497 ++it) { |
| 506 // Free the certs as we commit them to the database. | 498 // Free the certs as we commit them to the database. |
| 507 scoped_ptr<PendingOperation> po(*it); | 499 scoped_ptr<PendingOperation> po(*it); |
| 508 switch (po->op()) { | 500 switch (po->op()) { |
| 509 case PendingOperation::CHANNEL_ID_ADD: { | 501 case PendingOperation::CHANNEL_ID_ADD: { |
| 510 channel_id_origins_.insert(po->channel_id().server_identifier()); | 502 channel_id_origins_.insert(po->channel_id().server_identifier()); |
| 511 add_smt.Reset(true); | 503 add_smt.Reset(true); |
| 512 add_smt.BindString(0, po->channel_id().server_identifier()); | 504 add_smt.BindString(0, po->channel_id().server_identifier()); |
| 513 const std::string& private_key = po->channel_id().private_key(); | 505 const std::string& private_key = po->channel_id().private_key(); |
| 514 add_smt.BindBlob(1, private_key.data(), private_key.size()); | 506 add_smt.BindBlob( |
| 507 1, private_key.data(), static_cast<int>(private_key.size())); | |
| 515 const std::string& cert = po->channel_id().cert(); | 508 const std::string& cert = po->channel_id().cert(); |
| 516 add_smt.BindBlob(2, cert.data(), cert.size()); | 509 add_smt.BindBlob(2, cert.data(), static_cast<int>(cert.size())); |
| 517 add_smt.BindInt(3, net::CLIENT_CERT_ECDSA_SIGN); | 510 add_smt.BindInt(3, CLIENT_CERT_ECDSA_SIGN); |
| 518 add_smt.BindInt64(4, | 511 add_smt.BindInt64(4, |
| 519 po->channel_id().expiration_time().ToInternalValue()); | 512 po->channel_id().expiration_time().ToInternalValue()); |
| 520 add_smt.BindInt64(5, | 513 add_smt.BindInt64(5, |
| 521 po->channel_id().creation_time().ToInternalValue()); | 514 po->channel_id().creation_time().ToInternalValue()); |
| 522 if (!add_smt.Run()) | 515 if (!add_smt.Run()) |
| 523 NOTREACHED() << "Could not add a server bound cert to the DB."; | 516 NOTREACHED() << "Could not add a server bound cert to the DB."; |
| 524 break; | 517 break; |
| 525 } | 518 } |
| 526 case PendingOperation::CHANNEL_ID_DELETE: | 519 case PendingOperation::CHANNEL_ID_DELETE: |
| 527 channel_id_origins_.erase(po->channel_id().server_identifier()); | 520 channel_id_origins_.erase(po->channel_id().server_identifier()); |
| 528 del_smt.Reset(true); | 521 del_smt.Reset(true); |
| 529 del_smt.BindString(0, po->channel_id().server_identifier()); | 522 del_smt.BindString(0, po->channel_id().server_identifier()); |
| 530 if (!del_smt.Run()) | 523 if (!del_smt.Run()) |
| 531 NOTREACHED() << "Could not delete a server bound cert from the DB."; | 524 NOTREACHED() << "Could not delete a server bound cert from the DB."; |
| 532 break; | 525 break; |
| 533 | 526 |
| 534 default: | 527 default: |
| 535 NOTREACHED(); | 528 NOTREACHED(); |
| 536 break; | 529 break; |
| 537 } | 530 } |
| 538 } | 531 } |
| 539 transaction.Commit(); | 532 transaction.Commit(); |
| 540 } | 533 } |
| 541 | 534 |
| 542 // Fire off a close message to the background thread. We could still have a | 535 // Fire off a close message to the background task runner. We could still have a |
| 543 // pending commit timer that will be holding a reference on us, but if/when | 536 // pending commit timer that will be holding a reference on us, but if/when |
| 544 // this fires we will already have been cleaned up and it will be ignored. | 537 // this fires we will already have been cleaned up and it will be ignored. |
| 545 void SQLiteChannelIDStore::Backend::Close() { | 538 void SQLiteChannelIDStore::Backend::Close() { |
| 546 // Must close the backend on the background thread. | 539 // Must close the backend on the background task runner. |
| 547 background_task_runner_->PostTask( | 540 background_task_runner_->PostTask( |
| 548 FROM_HERE, base::Bind(&Backend::InternalBackgroundClose, this)); | 541 FROM_HERE, base::Bind(&Backend::InternalBackgroundClose, this)); |
| 549 } | 542 } |
| 550 | 543 |
| 551 void SQLiteChannelIDStore::Backend::InternalBackgroundClose() { | 544 void SQLiteChannelIDStore::Backend::InternalBackgroundClose() { |
| 552 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 545 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 553 // Commit any pending operations | 546 // Commit any pending operations |
| 554 Commit(); | 547 Commit(); |
| 555 | 548 |
| 556 if (!force_keep_session_state_ && | |
| 557 special_storage_policy_.get() && | |
| 558 special_storage_policy_->HasSessionOnlyOrigins()) { | |
| 559 DeleteCertificatesOnShutdown(); | |
| 560 } | |
| 561 | |
| 562 db_.reset(); | 549 db_.reset(); |
| 563 } | 550 } |
| 564 | 551 |
| 565 void SQLiteChannelIDStore::Backend::DeleteCertificatesOnShutdown() { | |
| 566 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | |
| 567 | |
| 568 if (!db_.get()) | |
| 569 return; | |
| 570 | |
| 571 if (channel_id_origins_.empty()) | |
| 572 return; | |
| 573 | |
| 574 if (!special_storage_policy_.get()) | |
| 575 return; | |
| 576 | |
| 577 sql::Statement del_smt(db_->GetCachedStatement( | |
| 578 SQL_FROM_HERE, "DELETE FROM origin_bound_certs WHERE origin=?")); | |
| 579 if (!del_smt.is_valid()) { | |
| 580 LOG(WARNING) << "Unable to delete certificates on shutdown."; | |
| 581 return; | |
| 582 } | |
| 583 | |
| 584 sql::Transaction transaction(db_.get()); | |
| 585 if (!transaction.Begin()) { | |
| 586 LOG(WARNING) << "Unable to delete certificates on shutdown."; | |
| 587 return; | |
| 588 } | |
| 589 | |
| 590 for (std::set<std::string>::iterator it = channel_id_origins_.begin(); | |
| 591 it != channel_id_origins_.end(); ++it) { | |
| 592 const GURL url(net::cookie_util::CookieOriginToURL(*it, true)); | |
| 593 if (!url.is_valid() || !special_storage_policy_->IsStorageSessionOnly(url)) | |
| 594 continue; | |
| 595 del_smt.Reset(true); | |
| 596 del_smt.BindString(0, *it); | |
| 597 if (!del_smt.Run()) | |
| 598 NOTREACHED() << "Could not delete a certificate from the DB."; | |
| 599 } | |
| 600 | |
| 601 if (!transaction.Commit()) | |
| 602 LOG(WARNING) << "Unable to delete certificates on shutdown."; | |
| 603 } | |
| 604 | |
| 605 void SQLiteChannelIDStore::Backend::SetForceKeepSessionState() { | 552 void SQLiteChannelIDStore::Backend::SetForceKeepSessionState() { |
| 606 base::AutoLock locked(lock_); | 553 base::AutoLock locked(lock_); |
| 607 force_keep_session_state_ = true; | 554 force_keep_session_state_ = true; |
| 608 } | 555 } |
| 609 | 556 |
| 610 SQLiteChannelIDStore::SQLiteChannelIDStore( | 557 SQLiteChannelIDStore::SQLiteChannelIDStore( |
| 611 const base::FilePath& path, | 558 const base::FilePath& path, |
| 612 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 559 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) |
| 613 quota::SpecialStoragePolicy* special_storage_policy) | 560 : backend_(new Backend(path, background_task_runner)) { |
| 614 : backend_(new Backend(path, | 561 } |
| 615 background_task_runner, | |
| 616 special_storage_policy)) {} | |
| 617 | 562 |
| 618 void SQLiteChannelIDStore::Load( | 563 void SQLiteChannelIDStore::Load(const LoadedCallback& loaded_callback) { |
| 619 const LoadedCallback& loaded_callback) { | |
| 620 backend_->Load(loaded_callback); | 564 backend_->Load(loaded_callback); |
| 621 } | 565 } |
| 622 | 566 |
| 623 void SQLiteChannelIDStore::AddChannelID( | 567 void SQLiteChannelIDStore::AddChannelID( |
| 624 const net::DefaultChannelIDStore::ChannelID& channel_id) { | 568 const DefaultChannelIDStore::ChannelID& channel_id) { |
| 625 backend_->AddChannelID(channel_id); | 569 backend_->AddChannelID(channel_id); |
| 626 } | 570 } |
| 627 | 571 |
| 628 void SQLiteChannelIDStore::DeleteChannelID( | 572 void SQLiteChannelIDStore::DeleteChannelID( |
| 629 const net::DefaultChannelIDStore::ChannelID& channel_id) { | 573 const DefaultChannelIDStore::ChannelID& channel_id) { |
| 630 backend_->DeleteChannelID(channel_id); | 574 backend_->DeleteChannelID(channel_id); |
| 631 } | 575 } |
| 632 | 576 |
| 633 void SQLiteChannelIDStore::SetForceKeepSessionState() { | 577 void SQLiteChannelIDStore::SetForceKeepSessionState() { |
| 634 backend_->SetForceKeepSessionState(); | 578 backend_->SetForceKeepSessionState(); |
| 635 } | 579 } |
| 636 | 580 |
| 637 SQLiteChannelIDStore::~SQLiteChannelIDStore() { | 581 SQLiteChannelIDStore::~SQLiteChannelIDStore() { |
| 638 backend_->Close(); | 582 backend_->Close(); |
| 639 // We release our reference to the Backend, though it will probably still have | 583 // We release our reference to the Backend, though it will probably still have |
| 640 // a reference if the background thread has not run Close() yet. | 584 // a reference if the background task runner has not run Close() yet. |
| 641 } | 585 } |
| 586 | |
| 587 } // namespace net | |
| OLD | NEW |