Chromium Code Reviews| Index: net/extras/sqlite/sqlite_channel_id_store.cc |
| diff --git a/net/extras/sqlite/sqlite_channel_id_store.cc b/net/extras/sqlite/sqlite_channel_id_store.cc |
| index d7edf9c170dd39710a5a46f45e71b2c6cd6e8fd3..f59bf4bd9554da5f1360e955daed2ff5c73d1157 100644 |
| --- a/net/extras/sqlite/sqlite_channel_id_store.cc |
| +++ b/net/extras/sqlite/sqlite_channel_id_store.cc |
| @@ -17,6 +17,7 @@ |
| #include "base/metrics/histogram.h" |
| #include "base/sequenced_task_runner.h" |
| #include "base/strings/string_util.h" |
| +#include "net/cert/asn1_util.h" |
| #include "net/cert/x509_certificate.h" |
| #include "net/cookies/cookie_util.h" |
| #include "net/ssl/ssl_client_cert_type.h" |
| @@ -29,22 +30,21 @@ |
| namespace { |
| // Version number of the database. |
| -const int kCurrentVersionNumber = 4; |
| +const int kCurrentVersionNumber = 5; |
| const int kCompatibleVersionNumber = 1; |
|
mattm
2015/04/10 01:00:27
Should be 5 also?
nharper
2015/04/25 02:59:18
I need it to be both 1 and 5, depending on the con
mattm
2015/04/27 20:55:53
Hm, it looks to me that in this CL kCompatibleVers
nharper
2015/04/29 22:07:15
Now that I re-read the code and what I wrote, I re
|
| -// Initializes the certs table, returning true on success. |
| +// Creates the new channel_id table (if it doesn't exist) and drops the old |
| +// origin_bound_certs table (if it exists), returning true on success. Success |
| +// means that the new table exists and the old table doesn't exist at the |
| +// end of the function, regardless of what sql commands are executed. |
| +/* |
| bool InitTable(sql::Connection* db) { |
| - // The table is named "origin_bound_certs" for backwards compatability before |
| - // we renamed this class to SQLiteChannelIDStore. Likewise, the primary |
| - // key is "origin", but now can be other things like a plain domain. |
| - if (!db->DoesTableExist("origin_bound_certs")) { |
| + if (!db->DoesTableExist("channel_id")) { |
| if (!db->Execute( |
| - "CREATE TABLE origin_bound_certs (" |
| - "origin TEXT NOT NULL UNIQUE PRIMARY KEY," |
| + "CREATE TABLE channel_id (" |
| + "host TEXT NOT NULL UNIQUE PRIMARY KEY," |
| "private_key BLOB NOT NULL," |
| - "cert BLOB NOT NULL," |
| - "cert_type INTEGER," |
| - "expiration_time INTEGER," |
| + "public_key BLOB NOT NULL," |
| "creation_time INTEGER)")) { |
| return false; |
| } |
| @@ -52,6 +52,7 @@ bool InitTable(sql::Connection* db) { |
| return true; |
| } |
| +*/ |
|
Ryan Sleevi
2015/04/09 22:40:09
This code is commented out.
nharper
2015/04/10 00:32:08
Sorry - removed.
|
| } // namespace |
| @@ -208,7 +209,7 @@ void SQLiteChannelIDStore::Backend::LoadInBackground( |
| return; |
| } |
| - if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { |
| + if (!EnsureDatabaseVersion()) { |
|
Ryan Sleevi
2015/04/09 22:40:09
Why no InitTable() call?
nharper
2015/04/10 00:32:08
It didn't make sense as its own function - I rolle
|
| NOTREACHED() << "Unable to open cert DB."; |
| if (corruption_detected_) |
| KillDatabase(); |
| @@ -221,8 +222,7 @@ void SQLiteChannelIDStore::Backend::LoadInBackground( |
| // Slurp all the certs into the out-vector. |
| sql::Statement smt(db_->GetUniqueStatement( |
| - "SELECT origin, private_key, cert, cert_type, expiration_time, " |
| - "creation_time FROM origin_bound_certs")); |
| + "SELECT host, private_key, public_key, creation_time FROM channel_id")); |
| if (!smt.is_valid()) { |
| if (corruption_detected_) |
| KillDatabase(); |
| @@ -232,19 +232,14 @@ void SQLiteChannelIDStore::Backend::LoadInBackground( |
| } |
| while (smt.Step()) { |
| - SSLClientCertType type = static_cast<SSLClientCertType>(smt.ColumnInt(3)); |
| - if (type != CLIENT_CERT_ECDSA_SIGN) |
| - continue; |
| - std::string private_key_from_db, cert_from_db; |
| + std::string private_key_from_db, public_key_from_db; |
| smt.ColumnBlobAsString(1, &private_key_from_db); |
| - smt.ColumnBlobAsString(2, &cert_from_db); |
| + smt.ColumnBlobAsString(2, &public_key_from_db); |
| scoped_ptr<DefaultChannelIDStore::ChannelID> channel_id( |
| new DefaultChannelIDStore::ChannelID( |
| - smt.ColumnString(0), // origin |
| - base::Time::FromInternalValue(smt.ColumnInt64(5)), |
| - base::Time::FromInternalValue(smt.ColumnInt64(4)), |
| - private_key_from_db, |
| - cert_from_db)); |
| + smt.ColumnString(0), // host |
| + base::Time::FromInternalValue(smt.ColumnInt64(3)), |
| + private_key_from_db, public_key_from_db)); |
| channel_ids->push_back(channel_id.release()); |
| } |
| @@ -274,64 +269,32 @@ bool SQLiteChannelIDStore::Backend::EnsureDatabaseVersion() { |
| } |
| int cur_version = meta_table_.GetVersionNumber(); |
| - if (cur_version == 1) { |
| - sql::Transaction transaction(db_.get()); |
| - if (!transaction.Begin()) |
| - return false; |
| - if (!db_->Execute( |
| - "ALTER TABLE origin_bound_certs ADD COLUMN cert_type " |
| - "INTEGER")) { |
| - LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 2."; |
| - return false; |
| - } |
| - // All certs in version 1 database are rsa_sign, which are unsupported. |
| - // Just discard them all. |
| - if (!db_->Execute("DELETE from origin_bound_certs")) { |
| - LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 2."; |
| - return false; |
| - } |
| - ++cur_version; |
| - meta_table_.SetVersionNumber(cur_version); |
| - meta_table_.SetCompatibleVersionNumber( |
| - std::min(cur_version, kCompatibleVersionNumber)); |
| - transaction.Commit(); |
| - } |
| - |
| - if (cur_version <= 3) { |
| - sql::Transaction transaction(db_.get()); |
| - if (!transaction.Begin()) |
| - return false; |
| - if (cur_version == 2) { |
| - if (!db_->Execute( |
| - "ALTER TABLE origin_bound_certs ADD COLUMN " |
| - "expiration_time INTEGER")) { |
| - LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 4."; |
| - return false; |
| - } |
| - } |
| + sql::Transaction transaction(db_.get()); |
| + if (!transaction.Begin()) |
| + return false; |
| + // Create new table if it doesn't already exist |
| + if (!db_->DoesTableExist("channel_id")) { |
| if (!db_->Execute( |
| - "ALTER TABLE origin_bound_certs ADD COLUMN " |
| - "creation_time INTEGER")) { |
| - LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 4."; |
| + "CREATE TABLE channel_id (" |
| + "host TEXT NOT NULL UNIQUE PRIMARY KEY," |
| + "private_key BLOB NOT NULL," |
| + "public_key BLOB NOT NULL," |
| + "creation_time INTEGER)")) { |
| return false; |
| } |
| + } |
| - sql::Statement statement( |
| - db_->GetUniqueStatement("SELECT origin, cert FROM origin_bound_certs")); |
| - sql::Statement update_expires_statement(db_->GetUniqueStatement( |
| - "UPDATE origin_bound_certs SET expiration_time = ? WHERE origin = ?")); |
| - sql::Statement update_creation_statement(db_->GetUniqueStatement( |
| - "UPDATE origin_bound_certs SET creation_time = ? WHERE origin = ?")); |
| - if (!statement.is_valid() || !update_expires_statement.is_valid() || |
| - !update_creation_statement.is_valid()) { |
| + // Migrate from previous versions to new version if possible |
| + if (cur_version >= 2 && cur_version <= 4) { |
| + sql::Statement statement(db_->GetUniqueStatement( |
| + "SELECT origin, cert, private_key, cert_type FROM origin_bound_certs")); |
| + sql::Statement insert_statement( |
| + db_->GetUniqueStatement("INSERT INTO channel_id VALUES (?, ?, ?, ?)")); |
| + if (!statement.is_valid() || !insert_statement.is_valid()) { |
| LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 4."; |
| + << "version 5."; |
| return false; |
| } |
| @@ -339,29 +302,30 @@ bool SQLiteChannelIDStore::Backend::EnsureDatabaseVersion() { |
| std::string origin = statement.ColumnString(0); |
| std::string cert_from_db; |
| statement.ColumnBlobAsString(1, &cert_from_db); |
| + std::string private_key; |
| + statement.ColumnBlobAsString(2, &private_key); |
| + if (statement.ColumnInt64(3) != CLIENT_CERT_ECDSA_SIGN) { |
| + continue; |
| + } |
| // Parse the cert and extract the real value and then update the DB. |
| scoped_refptr<X509Certificate> cert(X509Certificate::CreateFromBytes( |
| cert_from_db.data(), static_cast<int>(cert_from_db.size()))); |
| if (cert.get()) { |
| - if (cur_version == 2) { |
| - update_expires_statement.Reset(true); |
| - update_expires_statement.BindInt64( |
| - 0, cert->valid_expiry().ToInternalValue()); |
| - update_expires_statement.BindString(1, origin); |
| - if (!update_expires_statement.Run()) { |
| - LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 4."; |
| - return false; |
| - } |
| + insert_statement.Reset(true); |
| + insert_statement.BindString(0, origin); |
| + insert_statement.BindBlob(1, private_key.data(), |
| + static_cast<int>(private_key.size())); |
| + base::StringPiece spki; |
| + if (!asn1::ExtractSPKIFromDERCert(cert_from_db, &spki)) { |
| + LOG(WARNING) << "Unable to extract SPKI from cert when migrating " |
| + "channel id database to version 5."; |
| + return false; |
| } |
| - |
| - update_creation_statement.Reset(true); |
| - update_creation_statement.BindInt64( |
| - 0, cert->valid_start().ToInternalValue()); |
| - update_creation_statement.BindString(1, origin); |
| - if (!update_creation_statement.Run()) { |
| - LOG(WARNING) << "Unable to update server bound cert database to " |
| - << "version 4."; |
| + insert_statement.BindBlob(2, spki.data(), spki.size()); |
| + insert_statement.BindInt64(3, cert->valid_start().ToInternalValue()); |
| + if (!insert_statement.Run()) { |
| + LOG(WARNING) << "Unable to update channel id database to " |
| + << "version 5."; |
| return false; |
| } |
| } else { |
| @@ -371,22 +335,22 @@ bool SQLiteChannelIDStore::Backend::EnsureDatabaseVersion() { |
| << statement.ColumnString(0); |
| } |
| } |
| + } |
| - cur_version = 4; |
| - meta_table_.SetVersionNumber(cur_version); |
| - meta_table_.SetCompatibleVersionNumber( |
| - std::min(cur_version, kCompatibleVersionNumber)); |
| - transaction.Commit(); |
| + if (cur_version < kCurrentVersionNumber) { |
| + sql::Statement statement( |
| + db_->GetUniqueStatement("DROP TABLE origin_bound_certs")); |
| + if (!statement.Run()) { |
| + LOG(WARNING) << "Error dropping old origin_bound_certs table"; |
| + return false; |
| + } |
| + meta_table_.SetVersionNumber(kCurrentVersionNumber); |
| + meta_table_.SetCompatibleVersionNumber(kCurrentVersionNumber); |
|
mattm
2015/04/10 01:00:27
kCompatibleVersionNumber
nharper
2015/04/25 02:59:18
The compatible version number here needs to be 5,
mattm
2015/04/27 20:55:53
Yeah, I think it should be 5 here also. I don't se
nharper
2015/04/29 22:07:15
I figured out my mistake and set it to 5.
|
| } |
| + transaction.Commit(); |
| // Put future migration cases here. |
| - // When the version is too old, we just try to continue anyway, there should |
| - // not be a released product that makes a database too old for us to handle. |
| - LOG_IF(WARNING, cur_version < kCurrentVersionNumber) |
| - << "Server bound cert database version " << cur_version |
| - << " is too old to handle."; |
| - |
| return true; |
| } |
| @@ -493,13 +457,13 @@ void SQLiteChannelIDStore::Backend::Commit() { |
| sql::Statement add_statement(db_->GetCachedStatement( |
| SQL_FROM_HERE, |
| - "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, " |
| - "expiration_time, creation_time) VALUES (?,?,?,?,?,?)")); |
| + "INSERT INTO channel_id (host, private_key, public_key, " |
| + "creation_time) VALUES (?,?,?,?)")); |
| if (!add_statement.is_valid()) |
| return; |
| sql::Statement del_statement(db_->GetCachedStatement( |
| - SQL_FROM_HERE, "DELETE FROM origin_bound_certs WHERE origin=?")); |
| + SQL_FROM_HERE, "DELETE FROM channel_id WHERE host=?")); |
| if (!del_statement.is_valid()) |
| return; |
| @@ -518,13 +482,11 @@ void SQLiteChannelIDStore::Backend::Commit() { |
| const std::string& private_key = po->channel_id().private_key(); |
| add_statement.BindBlob( |
| 1, private_key.data(), static_cast<int>(private_key.size())); |
| - const std::string& cert = po->channel_id().cert(); |
| - add_statement.BindBlob(2, cert.data(), static_cast<int>(cert.size())); |
| - add_statement.BindInt(3, CLIENT_CERT_ECDSA_SIGN); |
| - add_statement.BindInt64( |
| - 4, po->channel_id().expiration_time().ToInternalValue()); |
| + const std::string& public_key = po->channel_id().public_key(); |
| + add_statement.BindBlob(2, public_key.data(), |
| + static_cast<int>(public_key.size())); |
| add_statement.BindInt64( |
| - 5, po->channel_id().creation_time().ToInternalValue()); |
| + 3, po->channel_id().creation_time().ToInternalValue()); |
| if (!add_statement.Run()) |
| NOTREACHED() << "Could not add a server bound cert to the DB."; |
| break; |
| @@ -568,7 +530,7 @@ void SQLiteChannelIDStore::Backend::BackgroundDeleteAllInList( |
| return; |
| sql::Statement del_smt(db_->GetCachedStatement( |
| - SQL_FROM_HERE, "DELETE FROM origin_bound_certs WHERE origin=?")); |
| + SQL_FROM_HERE, "DELETE FROM channel_id WHERE host=?")); |
| if (!del_smt.is_valid()) { |
| LOG(WARNING) << "Unable to delete channel ids."; |
| return; |