1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/sqlite_server_bound_cert_store.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/file_path.h"
13 #include "base/file_util.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/metrics/histogram.h"
17 #include "base/string_util.h"
18 #include "base/threading/thread.h"
19 #include "base/threading/thread_restrictions.h"
20 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
21 #include "chrome/browser/net/clear_on_exit_policy.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "net/base/ssl_client_cert_type.h"
24 #include "net/base/x509_certificate.h"
25 #include "sql/meta_table.h"
26 #include "sql/statement.h"
27 #include "sql/transaction.h"
29 using content::BrowserThread
;
31 // This class is designed to be shared between any calling threads and the
32 // database thread. It batches operations and commits them on a timer.
33 class SQLiteServerBoundCertStore::Backend
34 : public base::RefCountedThreadSafe
<SQLiteServerBoundCertStore::Backend
> {
36 Backend(const FilePath
& path
, ClearOnExitPolicy
* clear_on_exit_policy
)
40 force_keep_session_state_(false),
41 clear_on_exit_policy_(clear_on_exit_policy
) {
44 // Creates or load the SQLite database.
46 std::vector
<net::DefaultServerBoundCertStore::ServerBoundCert
*>* certs
);
48 // Batch a server bound cert addition.
49 void AddServerBoundCert(
50 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
);
52 // Batch a server bound cert deletion.
53 void DeleteServerBoundCert(
54 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
);
56 // Commit pending operations as soon as possible.
57 void Flush(const base::Closure
& completion_task
);
59 // Commit any pending operations and close the database. This must be called
60 // before the object is destructed.
63 void SetForceKeepSessionState();
66 friend class base::RefCountedThreadSafe
<SQLiteServerBoundCertStore::Backend
>;
68 // You should call Close() before destructing this object.
70 DCHECK(!db_
.get()) << "Close should have already been called.";
71 DCHECK(num_pending_
== 0 && pending_
.empty());
74 // Database upgrade statements.
75 bool EnsureDatabaseVersion();
77 class PendingOperation
{
86 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
)
87 : op_(op
), cert_(cert
) {}
89 OperationType
op() const { return op_
; }
90 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert() const {
96 net::DefaultServerBoundCertStore::ServerBoundCert cert_
;
100 // Batch a server bound cert operation (add or delete)
102 PendingOperation::OperationType op
,
103 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
);
104 // Commit our pending operations to the database.
106 // Close() executed on the background thread.
107 void InternalBackgroundClose();
109 void DeleteCertificatesOnShutdown();
112 scoped_ptr
<sql::Connection
> db_
;
113 sql::MetaTable meta_table_
;
115 typedef std::list
<PendingOperation
*> PendingOperationsList
;
116 PendingOperationsList pending_
;
117 PendingOperationsList::size_type num_pending_
;
118 // True if the persistent store should skip clear on exit rules.
119 bool force_keep_session_state_
;
120 // Guard |pending_|, |num_pending_| and |force_keep_session_state_|.
123 // Cache of origins we have certificates stored for.
124 std::set
<std::string
> cert_origins_
;
126 scoped_refptr
<ClearOnExitPolicy
> clear_on_exit_policy_
;
128 DISALLOW_COPY_AND_ASSIGN(Backend
);
131 // Version number of the database.
132 static const int kCurrentVersionNumber
= 4;
133 static const int kCompatibleVersionNumber
= 1;
137 // Initializes the certs table, returning true on success.
138 bool InitTable(sql::Connection
* db
) {
139 // The table is named "origin_bound_certs" for backwards compatability before
140 // we renamed this class to SQLiteServerBoundCertStore. Likewise, the primary
141 // key is "origin", but now can be other things like a plain domain.
142 if (!db
->DoesTableExist("origin_bound_certs")) {
143 if (!db
->Execute("CREATE TABLE origin_bound_certs ("
144 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
145 "private_key BLOB NOT NULL,"
146 "cert BLOB NOT NULL,"
148 "expiration_time INTEGER,"
149 "creation_time INTEGER)"))
158 bool SQLiteServerBoundCertStore::Backend::Load(
159 std::vector
<net::DefaultServerBoundCertStore::ServerBoundCert
*>* certs
) {
160 // This function should be called only once per instance.
163 // TODO(paivanof@gmail.com): We do a lot of disk access in this function,
164 // thus we do an exception to allow IO on the UI thread. This code will be
165 // moved to the DB thread as part of http://crbug.com/89665.
166 base::ThreadRestrictions::ScopedAllowIO allow_io
;
168 base::TimeTicks start
= base::TimeTicks::Now();
170 // Ensure the parent directory for storing certs is created before reading
172 const FilePath dir
= path_
.DirName();
173 if (!file_util::PathExists(dir
) && !file_util::CreateDirectory(dir
))
177 if (file_util::GetFileSize(path_
, &db_size
))
178 UMA_HISTOGRAM_COUNTS("DomainBoundCerts.DBSizeInKB", db_size
/ 1024 );
180 db_
.reset(new sql::Connection
);
181 if (!db_
->Open(path_
)) {
182 NOTREACHED() << "Unable to open cert DB.";
187 if (!EnsureDatabaseVersion() || !InitTable(db_
.get())) {
188 NOTREACHED() << "Unable to open cert DB.";
195 // Slurp all the certs into the out-vector.
196 sql::Statement
smt(db_
->GetUniqueStatement(
197 "SELECT origin, private_key, cert, cert_type, expiration_time, "
198 "creation_time FROM origin_bound_certs"));
199 if (!smt
.is_valid()) {
205 std::string private_key_from_db
, cert_from_db
;
206 smt
.ColumnBlobAsString(1, &private_key_from_db
);
207 smt
.ColumnBlobAsString(2, &cert_from_db
);
208 scoped_ptr
<net::DefaultServerBoundCertStore::ServerBoundCert
> cert(
209 new net::DefaultServerBoundCertStore::ServerBoundCert(
210 smt
.ColumnString(0), // origin
211 static_cast<net::SSLClientCertType
>(smt
.ColumnInt(3)),
212 base::Time::FromInternalValue(smt
.ColumnInt64(5)),
213 base::Time::FromInternalValue(smt
.ColumnInt64(4)),
216 cert_origins_
.insert(cert
->server_identifier());
217 certs
->push_back(cert
.release());
220 UMA_HISTOGRAM_COUNTS_10000("DomainBoundCerts.DBLoadedCount", certs
->size());
221 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.DBLoadTime",
222 base::TimeTicks::Now() - start
,
223 base::TimeDelta::FromMilliseconds(1),
224 base::TimeDelta::FromMinutes(1),
229 bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() {
231 if (!meta_table_
.Init(
232 db_
.get(), kCurrentVersionNumber
, kCompatibleVersionNumber
)) {
236 if (meta_table_
.GetCompatibleVersionNumber() > kCurrentVersionNumber
) {
237 LOG(WARNING
) << "Server bound cert database is too new.";
241 int cur_version
= meta_table_
.GetVersionNumber();
242 if (cur_version
== 1) {
243 sql::Transaction
transaction(db_
.get());
244 if (!transaction
.Begin())
246 if (!db_
->Execute("ALTER TABLE origin_bound_certs ADD COLUMN cert_type "
248 LOG(WARNING
) << "Unable to update server bound cert database to "
252 // All certs in version 1 database are rsa_sign, which has a value of 1.
253 if (!db_
->Execute("UPDATE origin_bound_certs SET cert_type = 1")) {
254 LOG(WARNING
) << "Unable to update server bound cert database to "
259 meta_table_
.SetVersionNumber(cur_version
);
260 meta_table_
.SetCompatibleVersionNumber(
261 std::min(cur_version
, kCompatibleVersionNumber
));
262 transaction
.Commit();
265 if (cur_version
<= 3) {
266 sql::Transaction
transaction(db_
.get());
267 if (!transaction
.Begin())
270 if (cur_version
== 2) {
271 if (!db_
->Execute("ALTER TABLE origin_bound_certs ADD COLUMN "
272 "expiration_time INTEGER")) {
273 LOG(WARNING
) << "Unable to update server bound cert database to "
279 if (!db_
->Execute("ALTER TABLE origin_bound_certs ADD COLUMN "
280 "creation_time INTEGER")) {
281 LOG(WARNING
) << "Unable to update server bound cert database to "
286 sql::Statement
smt(db_
->GetUniqueStatement(
287 "SELECT origin, cert FROM origin_bound_certs"));
288 sql::Statement
update_expires_smt(db_
->GetUniqueStatement(
289 "UPDATE origin_bound_certs SET expiration_time = ? WHERE origin = ?"));
290 sql::Statement
update_creation_smt(db_
->GetUniqueStatement(
291 "UPDATE origin_bound_certs SET creation_time = ? WHERE origin = ?"));
292 if (!smt
.is_valid() ||
293 !update_expires_smt
.is_valid() ||
294 !update_creation_smt
.is_valid()) {
295 LOG(WARNING
) << "Unable to update server bound cert database to "
301 std::string origin
= smt
.ColumnString(0);
302 std::string cert_from_db
;
303 smt
.ColumnBlobAsString(1, &cert_from_db
);
304 // Parse the cert and extract the real value and then update the DB.
305 scoped_refptr
<net::X509Certificate
> cert(
306 net::X509Certificate::CreateFromBytes(
307 cert_from_db
.data(), cert_from_db
.size()));
309 if (cur_version
== 2) {
310 update_expires_smt
.Reset(true);
311 update_expires_smt
.BindInt64(0,
312 cert
->valid_expiry().ToInternalValue());
313 update_expires_smt
.BindString(1, origin
);
314 if (!update_expires_smt
.Run()) {
315 LOG(WARNING
) << "Unable to update server bound cert database to "
321 update_creation_smt
.Reset(true);
322 update_creation_smt
.BindInt64(0, cert
->valid_start().ToInternalValue());
323 update_creation_smt
.BindString(1, origin
);
324 if (!update_creation_smt
.Run()) {
325 LOG(WARNING
) << "Unable to update server bound cert database to "
330 // If there's a cert we can't parse, just leave it. It'll get replaced
331 // with a new one if we ever try to use it.
332 LOG(WARNING
) << "Error parsing cert for database upgrade for origin "
333 << smt
.ColumnString(0);
338 meta_table_
.SetVersionNumber(cur_version
);
339 meta_table_
.SetCompatibleVersionNumber(
340 std::min(cur_version
, kCompatibleVersionNumber
));
341 transaction
.Commit();
344 // Put future migration cases here.
346 // When the version is too old, we just try to continue anyway, there should
347 // not be a released product that makes a database too old for us to handle.
348 LOG_IF(WARNING
, cur_version
< kCurrentVersionNumber
) <<
349 "Server bound cert database version " << cur_version
<<
350 " is too old to handle.";
355 void SQLiteServerBoundCertStore::Backend::AddServerBoundCert(
356 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
) {
357 BatchOperation(PendingOperation::CERT_ADD
, cert
);
360 void SQLiteServerBoundCertStore::Backend::DeleteServerBoundCert(
361 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
) {
362 BatchOperation(PendingOperation::CERT_DELETE
, cert
);
365 void SQLiteServerBoundCertStore::Backend::BatchOperation(
366 PendingOperation::OperationType op
,
367 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
) {
368 // Commit every 30 seconds.
369 static const int kCommitIntervalMs
= 30 * 1000;
370 // Commit right away if we have more than 512 outstanding operations.
371 static const size_t kCommitAfterBatchSize
= 512;
372 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB
));
374 // We do a full copy of the cert here, and hopefully just here.
375 scoped_ptr
<PendingOperation
> po(new PendingOperation(op
, cert
));
377 PendingOperationsList::size_type num_pending
;
379 base::AutoLock
locked(lock_
);
380 pending_
.push_back(po
.release());
381 num_pending
= ++num_pending_
;
384 if (num_pending
== 1) {
385 // We've gotten our first entry for this batch, fire off the timer.
386 BrowserThread::PostDelayedTask(
387 BrowserThread::DB
, FROM_HERE
,
388 base::Bind(&Backend::Commit
, this),
389 base::TimeDelta::FromMilliseconds(kCommitIntervalMs
));
390 } else if (num_pending
== kCommitAfterBatchSize
) {
391 // We've reached a big enough batch, fire off a commit now.
392 BrowserThread::PostTask(
393 BrowserThread::DB
, FROM_HERE
,
394 base::Bind(&Backend::Commit
, this));
398 void SQLiteServerBoundCertStore::Backend::Commit() {
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
401 PendingOperationsList ops
;
403 base::AutoLock
locked(lock_
);
408 // Maybe an old timer fired or we are already Close()'ed.
409 if (!db_
.get() || ops
.empty())
412 sql::Statement
add_smt(db_
->GetCachedStatement(SQL_FROM_HERE
,
413 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, "
414 "expiration_time, creation_time) VALUES (?,?,?,?,?,?)"));
415 if (!add_smt
.is_valid())
418 sql::Statement
del_smt(db_
->GetCachedStatement(SQL_FROM_HERE
,
419 "DELETE FROM origin_bound_certs WHERE origin=?"));
420 if (!del_smt
.is_valid())
423 sql::Transaction
transaction(db_
.get());
424 if (!transaction
.Begin())
427 for (PendingOperationsList::iterator it
= ops
.begin();
428 it
!= ops
.end(); ++it
) {
429 // Free the certs as we commit them to the database.
430 scoped_ptr
<PendingOperation
> po(*it
);
432 case PendingOperation::CERT_ADD
: {
433 cert_origins_
.insert(po
->cert().server_identifier());
435 add_smt
.BindString(0, po
->cert().server_identifier());
436 const std::string
& private_key
= po
->cert().private_key();
437 add_smt
.BindBlob(1, private_key
.data(), private_key
.size());
438 const std::string
& cert
= po
->cert().cert();
439 add_smt
.BindBlob(2, cert
.data(), cert
.size());
440 add_smt
.BindInt(3, po
->cert().type());
441 add_smt
.BindInt64(4, po
->cert().expiration_time().ToInternalValue());
442 add_smt
.BindInt64(5, po
->cert().creation_time().ToInternalValue());
444 NOTREACHED() << "Could not add a server bound cert to the DB.";
447 case PendingOperation::CERT_DELETE
:
448 cert_origins_
.erase(po
->cert().server_identifier());
450 del_smt
.BindString(0, po
->cert().server_identifier());
452 NOTREACHED() << "Could not delete a server bound cert from the DB.";
460 transaction
.Commit();
463 void SQLiteServerBoundCertStore::Backend::Flush(
464 const base::Closure
& completion_task
) {
465 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB
));
466 BrowserThread::PostTask(
467 BrowserThread::DB
, FROM_HERE
, base::Bind(&Backend::Commit
, this));
468 if (!completion_task
.is_null()) {
469 // We want the completion task to run immediately after Commit() returns.
470 // Posting it from here means there is less chance of another task getting
471 // onto the message queue first, than if we posted it from Commit() itself.
472 BrowserThread::PostTask(BrowserThread::DB
, FROM_HERE
, completion_task
);
476 // Fire off a close message to the background thread. We could still have a
477 // pending commit timer that will be holding a reference on us, but if/when
478 // this fires we will already have been cleaned up and it will be ignored.
479 void SQLiteServerBoundCertStore::Backend::Close() {
480 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB
));
481 // Must close the backend on the background thread.
482 BrowserThread::PostTask(
483 BrowserThread::DB
, FROM_HERE
,
484 base::Bind(&Backend::InternalBackgroundClose
, this));
487 void SQLiteServerBoundCertStore::Backend::InternalBackgroundClose() {
488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
489 // Commit any pending operations
492 if (!force_keep_session_state_
&& clear_on_exit_policy_
.get() &&
493 clear_on_exit_policy_
->HasClearOnExitOrigins()) {
494 DeleteCertificatesOnShutdown();
500 void SQLiteServerBoundCertStore::Backend::DeleteCertificatesOnShutdown() {
501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
506 if (cert_origins_
.empty())
509 sql::Statement
del_smt(db_
->GetCachedStatement(
510 SQL_FROM_HERE
, "DELETE FROM origin_bound_certs WHERE origin=?"));
511 if (!del_smt
.is_valid()) {
512 LOG(WARNING
) << "Unable to delete certificates on shutdown.";
516 sql::Transaction
transaction(db_
.get());
517 if (!transaction
.Begin()) {
518 LOG(WARNING
) << "Unable to delete certificates on shutdown.";
522 for (std::set
<std::string
>::iterator it
= cert_origins_
.begin();
523 it
!= cert_origins_
.end(); ++it
) {
524 if (!clear_on_exit_policy_
->ShouldClearOriginOnExit(*it
, true))
527 del_smt
.BindString(0, *it
);
529 NOTREACHED() << "Could not delete a certificate from the DB.";
532 if (!transaction
.Commit())
533 LOG(WARNING
) << "Unable to delete certificates on shutdown.";
536 void SQLiteServerBoundCertStore::Backend::SetForceKeepSessionState() {
537 base::AutoLock
locked(lock_
);
538 force_keep_session_state_
= true;
541 SQLiteServerBoundCertStore::SQLiteServerBoundCertStore(
542 const FilePath
& path
,
543 ClearOnExitPolicy
* clear_on_exit_policy
)
544 : backend_(new Backend(path
, clear_on_exit_policy
)) {
547 bool SQLiteServerBoundCertStore::Load(
548 std::vector
<net::DefaultServerBoundCertStore::ServerBoundCert
*>* certs
) {
549 return backend_
->Load(certs
);
552 void SQLiteServerBoundCertStore::AddServerBoundCert(
553 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
) {
555 backend_
->AddServerBoundCert(cert
);
558 void SQLiteServerBoundCertStore::DeleteServerBoundCert(
559 const net::DefaultServerBoundCertStore::ServerBoundCert
& cert
) {
561 backend_
->DeleteServerBoundCert(cert
);
564 void SQLiteServerBoundCertStore::SetForceKeepSessionState() {
566 backend_
->SetForceKeepSessionState();
569 void SQLiteServerBoundCertStore::Flush(const base::Closure
& completion_task
) {
571 backend_
->Flush(completion_task
);
572 else if (!completion_task
.is_null())
573 MessageLoop::current()->PostTask(FROM_HERE
, completion_task
);
576 SQLiteServerBoundCertStore::~SQLiteServerBoundCertStore() {
577 if (backend_
.get()) {
579 // Release our reference, it will probably still have a reference if the
580 // background thread has not run Close() yet.