Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: chrome/browser/net/sqlite_origin_bound_cert_store.cc

Issue 8662036: Support EC certs in OriginBoundCertService and OriginBoundCertStore. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review changes Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/net/sqlite_origin_bound_cert_store_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_origin_bound_cert_store.h" 5 #include "chrome/browser/net/sqlite_origin_bound_cert_store.h"
6 6
7 #include <list> 7 #include <list>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/string_util.h" 15 #include "base/string_util.h"
16 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
17 #include "base/threading/thread_restrictions.h" 17 #include "base/threading/thread_restrictions.h"
18 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" 18 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
19 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
20 #include "net/base/ssl_client_cert_type.h"
20 #include "sql/meta_table.h" 21 #include "sql/meta_table.h"
21 #include "sql/statement.h" 22 #include "sql/statement.h"
22 #include "sql/transaction.h" 23 #include "sql/transaction.h"
23 24
24 using content::BrowserThread; 25 using content::BrowserThread;
25 26
26 // This class is designed to be shared between any calling threads and the 27 // This class is designed to be shared between any calling threads and the
27 // database thread. It batches operations and commits them on a timer. 28 // database thread. It batches operations and commits them on a timer.
28 class SQLiteOriginBoundCertStore::Backend 29 class SQLiteOriginBoundCertStore::Backend
29 : public base::RefCountedThreadSafe<SQLiteOriginBoundCertStore::Backend> { 30 : public base::RefCountedThreadSafe<SQLiteOriginBoundCertStore::Backend> {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 PendingOperationsList::size_type num_pending_; 110 PendingOperationsList::size_type num_pending_;
110 // True if the persistent store should be deleted upon destruction. 111 // True if the persistent store should be deleted upon destruction.
111 bool clear_local_state_on_exit_; 112 bool clear_local_state_on_exit_;
112 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. 113 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|.
113 base::Lock lock_; 114 base::Lock lock_;
114 115
115 DISALLOW_COPY_AND_ASSIGN(Backend); 116 DISALLOW_COPY_AND_ASSIGN(Backend);
116 }; 117 };
117 118
118 // Version number of the database. 119 // Version number of the database.
119 static const int kCurrentVersionNumber = 1; 120 static const int kCurrentVersionNumber = 2;
120 static const int kCompatibleVersionNumber = 1; 121 static const int kCompatibleVersionNumber = 1;
121 122
122 namespace { 123 namespace {
123 124
124 // Initializes the certs table, returning true on success. 125 // Initializes the certs table, returning true on success.
125 bool InitTable(sql::Connection* db) { 126 bool InitTable(sql::Connection* db) {
126 if (!db->DoesTableExist("origin_bound_certs")) { 127 if (!db->DoesTableExist("origin_bound_certs")) {
127 if (!db->Execute("CREATE TABLE origin_bound_certs (" 128 if (!db->Execute("CREATE TABLE origin_bound_certs ("
128 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 129 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
129 "private_key BLOB NOT NULL," 130 "private_key BLOB NOT NULL,"
130 "cert BLOB NOT NULL)")) 131 "cert BLOB NOT NULL,"
132 "cert_type INTEGER)"))
131 return false; 133 return false;
132 } 134 }
133 135
134 return true; 136 return true;
135 } 137 }
136 138
137 } // namespace 139 } // namespace
138 140
139 bool SQLiteOriginBoundCertStore::Backend::Load( 141 bool SQLiteOriginBoundCertStore::Backend::Load(
140 std::vector<net::DefaultOriginBoundCertStore::OriginBoundCert*>* certs) { 142 std::vector<net::DefaultOriginBoundCertStore::OriginBoundCert*>* certs) {
(...skipping 21 matching lines...) Expand all
162 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { 164 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) {
163 NOTREACHED() << "Unable to open cert DB."; 165 NOTREACHED() << "Unable to open cert DB.";
164 db_.reset(); 166 db_.reset();
165 return false; 167 return false;
166 } 168 }
167 169
168 db_->Preload(); 170 db_->Preload();
169 171
170 // Slurp all the certs into the out-vector. 172 // Slurp all the certs into the out-vector.
171 sql::Statement smt(db_->GetUniqueStatement( 173 sql::Statement smt(db_->GetUniqueStatement(
172 "SELECT origin, private_key, cert FROM origin_bound_certs")); 174 "SELECT origin, private_key, cert, cert_type FROM origin_bound_certs"));
173 if (!smt) { 175 if (!smt) {
174 NOTREACHED() << "select statement prep failed"; 176 NOTREACHED() << "select statement prep failed";
175 db_.reset(); 177 db_.reset();
176 return false; 178 return false;
177 } 179 }
178 180
179 while (smt.Step()) { 181 while (smt.Step()) {
180 std::string private_key_from_db, cert_from_db; 182 std::string private_key_from_db, cert_from_db;
181 smt.ColumnBlobAsString(1, &private_key_from_db); 183 smt.ColumnBlobAsString(1, &private_key_from_db);
182 smt.ColumnBlobAsString(2, &cert_from_db); 184 smt.ColumnBlobAsString(2, &cert_from_db);
183 scoped_ptr<net::DefaultOriginBoundCertStore::OriginBoundCert> cert( 185 scoped_ptr<net::DefaultOriginBoundCertStore::OriginBoundCert> cert(
184 new net::DefaultOriginBoundCertStore::OriginBoundCert( 186 new net::DefaultOriginBoundCertStore::OriginBoundCert(
185 smt.ColumnString(0), // origin 187 smt.ColumnString(0), // origin
188 static_cast<net::SSLClientCertType>(smt.ColumnInt(3)),
186 private_key_from_db, 189 private_key_from_db,
187 cert_from_db)); 190 cert_from_db));
188 certs->push_back(cert.release()); 191 certs->push_back(cert.release());
189 } 192 }
190 193
191 return true; 194 return true;
192 } 195 }
193 196
194 bool SQLiteOriginBoundCertStore::Backend::EnsureDatabaseVersion() { 197 bool SQLiteOriginBoundCertStore::Backend::EnsureDatabaseVersion() {
195 // Version check. 198 // Version check.
196 if (!meta_table_.Init( 199 if (!meta_table_.Init(
197 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { 200 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) {
198 return false; 201 return false;
199 } 202 }
200 203
201 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { 204 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
202 LOG(WARNING) << "Origin bound cert database is too new."; 205 LOG(WARNING) << "Origin bound cert database is too new.";
203 return false; 206 return false;
204 } 207 }
205 208
206 int cur_version = meta_table_.GetVersionNumber(); 209 int cur_version = meta_table_.GetVersionNumber();
210 if (cur_version == 1) {
211 sql::Transaction transaction(db_.get());
212 if (!transaction.Begin())
213 return false;
214 if (!db_->Execute("ALTER TABLE origin_bound_certs ADD COLUMN cert_type "
215 "INTEGER")) {
216 LOG(WARNING) << "Unable to update origin bound cert database to "
217 << "version 2.";
218 return false;
219 }
220 // All certs in version 1 database are rsa_sign, which has a value of 1.
221 if (!db_->Execute("UPDATE origin_bound_certs SET cert_type = 1")) {
222 LOG(WARNING) << "Unable to update origin bound cert database to "
223 << "version 2.";
224 return false;
225 }
226 ++cur_version;
227 meta_table_.SetVersionNumber(cur_version);
228 meta_table_.SetCompatibleVersionNumber(
229 std::min(cur_version, kCompatibleVersionNumber));
230 transaction.Commit();
231 }
207 232
208 // Put future migration cases here. 233 // Put future migration cases here.
209 234
210 // When the version is too old, we just try to continue anyway, there should 235 // When the version is too old, we just try to continue anyway, there should
211 // not be a released product that makes a database too old for us to handle. 236 // not be a released product that makes a database too old for us to handle.
212 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) << 237 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) <<
213 "Origin bound cert database version " << cur_version << 238 "Origin bound cert database version " << cur_version <<
214 " is too old to handle."; 239 " is too old to handle.";
215 240
216 return true; 241 return true;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 base::AutoLock locked(lock_); 291 base::AutoLock locked(lock_);
267 pending_.swap(ops); 292 pending_.swap(ops);
268 num_pending_ = 0; 293 num_pending_ = 0;
269 } 294 }
270 295
271 // Maybe an old timer fired or we are already Close()'ed. 296 // Maybe an old timer fired or we are already Close()'ed.
272 if (!db_.get() || ops.empty()) 297 if (!db_.get() || ops.empty())
273 return; 298 return;
274 299
275 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, 300 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE,
276 "INSERT INTO origin_bound_certs (origin, private_key, cert) " 301 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type) "
277 "VALUES (?,?,?)")); 302 "VALUES (?,?,?,?)"));
278 if (!add_smt) { 303 if (!add_smt) {
279 NOTREACHED(); 304 NOTREACHED();
280 return; 305 return;
281 } 306 }
282 307
283 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, 308 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE,
284 "DELETE FROM origin_bound_certs WHERE origin=?")); 309 "DELETE FROM origin_bound_certs WHERE origin=?"));
285 if (!del_smt) { 310 if (!del_smt) {
286 NOTREACHED(); 311 NOTREACHED();
287 return; 312 return;
288 } 313 }
289 314
290 sql::Transaction transaction(db_.get()); 315 sql::Transaction transaction(db_.get());
291 if (!transaction.Begin()) { 316 if (!transaction.Begin()) {
292 NOTREACHED(); 317 NOTREACHED();
293 return; 318 return;
294 } 319 }
295 for (PendingOperationsList::iterator it = ops.begin(); 320 for (PendingOperationsList::iterator it = ops.begin();
296 it != ops.end(); ++it) { 321 it != ops.end(); ++it) {
297 // Free the certs as we commit them to the database. 322 // Free the certs as we commit them to the database.
298 scoped_ptr<PendingOperation> po(*it); 323 scoped_ptr<PendingOperation> po(*it);
299 switch (po->op()) { 324 switch (po->op()) {
300 case PendingOperation::CERT_ADD: { 325 case PendingOperation::CERT_ADD: {
301 add_smt.Reset(); 326 add_smt.Reset();
302 add_smt.BindString(0, po->cert().origin()); 327 add_smt.BindString(0, po->cert().origin());
303 const std::string& private_key = po->cert().private_key(); 328 const std::string& private_key = po->cert().private_key();
304 add_smt.BindBlob(1, private_key.data(), private_key.size()); 329 add_smt.BindBlob(1, private_key.data(), private_key.size());
305 const std::string& cert = po->cert().cert(); 330 const std::string& cert = po->cert().cert();
306 add_smt.BindBlob(2, cert.data(), cert.size()); 331 add_smt.BindBlob(2, cert.data(), cert.size());
332 add_smt.BindInt(3, po->cert().type());
307 if (!add_smt.Run()) 333 if (!add_smt.Run())
308 NOTREACHED() << "Could not add an origin bound cert to the DB."; 334 NOTREACHED() << "Could not add an origin bound cert to the DB.";
309 break; 335 break;
310 } 336 }
311 case PendingOperation::CERT_DELETE: 337 case PendingOperation::CERT_DELETE:
312 del_smt.Reset(); 338 del_smt.Reset();
313 del_smt.BindString(0, po->cert().origin()); 339 del_smt.BindString(0, po->cert().origin());
314 if (!del_smt.Run()) 340 if (!del_smt.Run())
315 NOTREACHED() << "Could not delete an origin bound cert from the DB."; 341 NOTREACHED() << "Could not delete an origin bound cert from the DB.";
316 break; 342 break;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 if (backend_.get()) 425 if (backend_.get())
400 backend_->SetClearLocalStateOnExit(clear_local_state); 426 backend_->SetClearLocalStateOnExit(clear_local_state);
401 } 427 }
402 428
403 void SQLiteOriginBoundCertStore::Flush(const base::Closure& completion_task) { 429 void SQLiteOriginBoundCertStore::Flush(const base::Closure& completion_task) {
404 if (backend_.get()) 430 if (backend_.get())
405 backend_->Flush(completion_task); 431 backend_->Flush(completion_task);
406 else if (!completion_task.is_null()) 432 else if (!completion_task.is_null())
407 MessageLoop::current()->PostTask(FROM_HERE, completion_task); 433 MessageLoop::current()->PostTask(FROM_HERE, completion_task);
408 } 434 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/net/sqlite_origin_bound_cert_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698