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