| OLD | NEW |
| 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/webdata/keyword_table.h" | 5 #include "chrome/browser/webdata/keyword_table.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/history/history_database.h" | 12 #include "chrome/browser/history/history_database.h" |
| 13 #include "chrome/browser/protector/protector.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 14 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "crypto/hmac.h" | |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 17 | 17 |
| 18 using base::Time; | 18 using base::Time; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // ID of the url column in keywords. | 22 // ID of the url column in keywords. |
| 23 const int kUrlIdPosition = 18; | 23 const int kUrlIdPosition = 18; |
| 24 | 24 |
| 25 // Keys used in the meta table. | 25 // Keys used in the meta table. |
| 26 const char kDefaultSearchProviderKey[] = "Default Search Provider ID"; | 26 const char kDefaultSearchProviderKey[] = "Default Search Provider ID"; |
| 27 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; | 27 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; |
| 28 | 28 |
| 29 // Key for signing the default search provider backup. | |
| 30 // TOOD(avayvod): Add key for Google Chrome to internal repository. | |
| 31 const char kDefaultSearchProviderBackupSigningKey[] = | |
| 32 "Please, don't change default search engine!"; | |
| 33 | |
| 34 // Meta table key to store backup value for the default search provider. | 29 // Meta table key to store backup value for the default search provider. |
| 35 const char kDefaultSearchProviderBackupKey[] = | 30 const char kDefaultSearchProviderBackupKey[] = |
| 36 "Default Search Provider ID Backup"; | 31 "Default Search Provider ID Backup"; |
| 37 | 32 |
| 38 // Meta table key to store backup value signature for the default search | 33 // Meta table key to store backup value signature for the default search |
| 39 // provider. | 34 // provider. |
| 40 const char kDefaultSearchProviderBackupSignatureKey[] = | 35 const char kDefaultSearchProviderBackupSignatureKey[] = |
| 41 "Default Search Provider ID Backup Signature"; | 36 "Default Search Provider ID Backup Signature"; |
| 42 | 37 |
| 43 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { | 38 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 69 s->BindInt(13, url.logo_id()); | 64 s->BindInt(13, url.logo_id()); |
| 70 s->BindBool(14, url.created_by_policy()); | 65 s->BindBool(14, url.created_by_policy()); |
| 71 s->BindString(15, url.instant_url() ? url.instant_url()->url() : | 66 s->BindString(15, url.instant_url() ? url.instant_url()->url() : |
| 72 std::string()); | 67 std::string()); |
| 73 s->BindInt64(16, url.last_modified().ToTimeT()); | 68 s->BindInt64(16, url.last_modified().ToTimeT()); |
| 74 s->BindString(17, url.sync_guid()); | 69 s->BindString(17, url.sync_guid()); |
| 75 } | 70 } |
| 76 | 71 |
| 77 // Signs search provider id and returns its signature. | 72 // Signs search provider id and returns its signature. |
| 78 std::string GetSearchProviderIDSignature(int64 id) { | 73 std::string GetSearchProviderIDSignature(int64 id) { |
| 79 crypto::HMAC hmac(crypto::HMAC::SHA256); | 74 return protector::SignSetting(base::Int64ToString(id)); |
| 80 DCHECK(hmac.Init(kDefaultSearchProviderBackupSigningKey)); | |
| 81 | |
| 82 std::string id_to_sign(base::Int64ToString(id)); | |
| 83 std::vector<unsigned char> digest(hmac.DigestLength()); | |
| 84 DCHECK(hmac.Sign(id_to_sign, &digest[0], digest.size())); | |
| 85 | |
| 86 return std::string(&digest[0], &digest[0] + digest.size()); | |
| 87 } | 75 } |
| 88 | 76 |
| 89 // Checks if signature for search provider id is correct and returns the | 77 // Checks if signature for search provider id is correct and returns the |
| 90 // result. | 78 // result. |
| 91 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { | 79 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { |
| 92 return signature == GetSearchProviderIDSignature(id); | 80 return signature == GetSearchProviderIDSignature(id); |
| 93 } | 81 } |
| 94 | 82 |
| 95 } // anonymous namespace | 83 } // anonymous namespace |
| 96 | 84 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 247 } |
| 260 | 248 |
| 261 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 249 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
| 262 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 250 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
| 263 SetDefaultSearchProviderBackupID(id); | 251 SetDefaultSearchProviderBackupID(id); |
| 264 } | 252 } |
| 265 | 253 |
| 266 int64 KeywordTable::GetDefaultSearchProviderID() { | 254 int64 KeywordTable::GetDefaultSearchProviderID() { |
| 267 int64 value = 0; | 255 int64 value = 0; |
| 268 meta_table_->GetValue(kDefaultSearchProviderKey, &value); | 256 meta_table_->GetValue(kDefaultSearchProviderKey, &value); |
| 257 return value; |
| 258 } |
| 259 |
| 260 int64 KeywordTable::GetDefaultSearchProviderIDBackup() { |
| 269 int64 backup_value = 0; | 261 int64 backup_value = 0; |
| 270 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); | 262 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); |
| 271 std::string backup_signature; | 263 std::string backup_signature; |
| 272 meta_table_->GetValue( | 264 meta_table_->GetValue( |
| 273 kDefaultSearchProviderBackupSignatureKey, &backup_signature); | 265 kDefaultSearchProviderBackupSignatureKey, &backup_signature); |
| 274 if (!IsSearchProviderIDValid(backup_value, backup_signature)) { | 266 if (!IsSearchProviderIDValid(backup_value, backup_signature)) |
| 275 // TODO(avayvod): Notify UI about the setting having been hijacked and | 267 return 0; |
| 276 // the backup value lost. | 268 return backup_value; |
| 277 return value; | 269 } |
| 278 } | 270 |
| 279 if (value != backup_value) { | 271 bool KeywordTable::DidDefaultSearchProviderChange() { |
| 280 // TODO(avayvod): Notify UI about the setting having been hijacked. | 272 int64 backup_value = 0; |
| 281 return backup_value; | 273 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); |
| 282 } | 274 std::string backup_signature; |
| 283 return value; | 275 meta_table_->GetValue( |
| 276 kDefaultSearchProviderBackupSignatureKey, &backup_signature); |
| 277 if (IsSearchProviderIDValid(backup_value, backup_signature) && |
| 278 backup_value == GetDefaultSearchProviderID()) |
| 279 return false; |
| 280 return true; |
| 284 } | 281 } |
| 285 | 282 |
| 286 bool KeywordTable::SetBuiltinKeywordVersion(int version) { | 283 bool KeywordTable::SetBuiltinKeywordVersion(int version) { |
| 287 return meta_table_->SetValue(kBuiltinKeywordVersion, version); | 284 return meta_table_->SetValue(kBuiltinKeywordVersion, version); |
| 288 } | 285 } |
| 289 | 286 |
| 290 int KeywordTable::GetBuiltinKeywordVersion() { | 287 int KeywordTable::GetBuiltinKeywordVersion() { |
| 291 int version = 0; | 288 int version = 0; |
| 292 if (!meta_table_->GetValue(kBuiltinKeywordVersion, &version)) | 289 if (!meta_table_->GetValue(kBuiltinKeywordVersion, &version)) |
| 293 return 0; | 290 return 0; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return | 378 return |
| 382 meta_table_->SetValue(kDefaultSearchProviderBackupKey, id) && | 379 meta_table_->SetValue(kDefaultSearchProviderBackupKey, id) && |
| 383 SetDefaultSearchProviderBackupIDSignature(id); | 380 SetDefaultSearchProviderBackupIDSignature(id); |
| 384 } | 381 } |
| 385 | 382 |
| 386 bool KeywordTable::SetDefaultSearchProviderBackupIDSignature(int64 id) { | 383 bool KeywordTable::SetDefaultSearchProviderBackupIDSignature(int64 id) { |
| 387 return meta_table_->SetValue( | 384 return meta_table_->SetValue( |
| 388 kDefaultSearchProviderBackupSignatureKey, | 385 kDefaultSearchProviderBackupSignatureKey, |
| 389 GetSearchProviderIDSignature(id)); | 386 GetSearchProviderIDSignature(id)); |
| 390 } | 387 } |
| OLD | NEW |