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

Side by Side Diff: components/autofill/core/browser/webdata/autofill_table.cc

Issue 2698103002: Allow embedder to use custom cryptography in Autofill table. (Closed)
Patch Set: Changed the ifdef name space Created 3 years, 10 months 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/autofill/core/browser/webdata/autofill_table.h" 5 #include "components/autofill/core/browser/webdata/autofill_table.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 12 matching lines...) Expand all
23 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
25 #include "base/time/time.h" 25 #include "base/time/time.h"
26 #include "components/autofill/core/browser/autofill_country.h" 26 #include "components/autofill/core/browser/autofill_country.h"
27 #include "components/autofill/core/browser/autofill_profile.h" 27 #include "components/autofill/core/browser/autofill_profile.h"
28 #include "components/autofill/core/browser/autofill_type.h" 28 #include "components/autofill/core/browser/autofill_type.h"
29 #include "components/autofill/core/browser/credit_card.h" 29 #include "components/autofill/core/browser/credit_card.h"
30 #include "components/autofill/core/browser/personal_data_manager.h" 30 #include "components/autofill/core/browser/personal_data_manager.h"
31 #include "components/autofill/core/browser/webdata/autofill_change.h" 31 #include "components/autofill/core/browser/webdata/autofill_change.h"
32 #include "components/autofill/core/browser/webdata/autofill_entry.h" 32 #include "components/autofill/core/browser/webdata/autofill_entry.h"
33 #include "components/autofill/core/browser/webdata/autofill_table_encryptor.h"
34 #include "components/autofill/core/browser/webdata/autofill_table_encryptor_fact ory.h"
33 #include "components/autofill/core/common/autofill_clock.h" 35 #include "components/autofill/core/common/autofill_clock.h"
34 #include "components/autofill/core/common/autofill_switches.h" 36 #include "components/autofill/core/common/autofill_switches.h"
35 #include "components/autofill/core/common/autofill_util.h" 37 #include "components/autofill/core/common/autofill_util.h"
36 #include "components/autofill/core/common/form_field_data.h" 38 #include "components/autofill/core/common/form_field_data.h"
37 #include "components/os_crypt/os_crypt.h"
38 #include "components/sync/base/model_type.h" 39 #include "components/sync/base/model_type.h"
39 #include "components/sync/protocol/entity_metadata.pb.h" 40 #include "components/sync/protocol/entity_metadata.pb.h"
40 #include "components/sync/protocol/model_type_state.pb.h" 41 #include "components/sync/protocol/model_type_state.pb.h"
41 #include "components/webdata/common/web_database.h" 42 #include "components/webdata/common/web_database.h"
42 #include "sql/statement.h" 43 #include "sql/statement.h"
43 #include "sql/transaction.h" 44 #include "sql/transaction.h"
44 #include "ui/base/l10n/l10n_util.h" 45 #include "ui/base/l10n/l10n_util.h"
45 #include "url/gurl.h" 46 #include "url/gurl.h"
46 47
47 using base::ASCIIToUTF16; 48 using base::ASCIIToUTF16;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 profile->set_use_count(s.ColumnInt64(index++)); 123 profile->set_use_count(s.ColumnInt64(index++));
123 profile->set_use_date(Time::FromTimeT(s.ColumnInt64(index++))); 124 profile->set_use_date(Time::FromTimeT(s.ColumnInt64(index++)));
124 profile->set_modification_date(Time::FromTimeT(s.ColumnInt64(index++))); 125 profile->set_modification_date(Time::FromTimeT(s.ColumnInt64(index++)));
125 profile->set_origin(s.ColumnString(index++)); 126 profile->set_origin(s.ColumnString(index++));
126 profile->set_language_code(s.ColumnString(index++)); 127 profile->set_language_code(s.ColumnString(index++));
127 128
128 return profile; 129 return profile;
129 } 130 }
130 131
131 void BindEncryptedCardToColumn(sql::Statement* s, 132 void BindEncryptedCardToColumn(sql::Statement* s,
132 int column_index, 133 int column_index,
133 const base::string16& number) { 134 const base::string16& number,
135 const AutofillTableEncryptor& encryptor) {
134 std::string encrypted_data; 136 std::string encrypted_data;
135 OSCrypt::EncryptString16(number, &encrypted_data); 137 encryptor.EncryptString16(number, &encrypted_data);
136 s->BindBlob(column_index, encrypted_data.data(), 138 s->BindBlob(column_index, encrypted_data.data(),
137 static_cast<int>(encrypted_data.length())); 139 static_cast<int>(encrypted_data.length()));
138 } 140 }
139 141
140 void BindCreditCardToStatement(const CreditCard& credit_card, 142 void BindCreditCardToStatement(const CreditCard& credit_card,
141 const Time& modification_date, 143 const Time& modification_date,
142 sql::Statement* s) { 144 sql::Statement* s,
145 const AutofillTableEncryptor& encryptor) {
143 DCHECK(base::IsValidGUID(credit_card.guid())); 146 DCHECK(base::IsValidGUID(credit_card.guid()));
144 int index = 0; 147 int index = 0;
145 s->BindString(index++, credit_card.guid()); 148 s->BindString(index++, credit_card.guid());
146 149
147 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_NAME_FULL)); 150 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_NAME_FULL));
148 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_MONTH)); 151 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_MONTH));
149 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 152 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR));
150 BindEncryptedCardToColumn(s, index++, 153 BindEncryptedCardToColumn(
151 credit_card.GetRawInfo(CREDIT_CARD_NUMBER)); 154 s, index++, credit_card.GetRawInfo(CREDIT_CARD_NUMBER), encryptor);
152 155
153 s->BindInt64(index++, credit_card.use_count()); 156 s->BindInt64(index++, credit_card.use_count());
154 s->BindInt64(index++, credit_card.use_date().ToTimeT()); 157 s->BindInt64(index++, credit_card.use_date().ToTimeT());
155 s->BindInt64(index++, modification_date.ToTimeT()); 158 s->BindInt64(index++, modification_date.ToTimeT());
156 s->BindString(index++, credit_card.origin()); 159 s->BindString(index++, credit_card.origin());
157 s->BindString(index++, credit_card.billing_address_id()); 160 s->BindString(index++, credit_card.billing_address_id());
158 } 161 }
159 162
160 base::string16 UnencryptedCardFromColumn(const sql::Statement& s, 163 base::string16 UnencryptedCardFromColumn(
161 int column_index) { 164 const sql::Statement& s,
165 int column_index,
166 const AutofillTableEncryptor& encryptor) {
162 base::string16 credit_card_number; 167 base::string16 credit_card_number;
163 int encrypted_number_len = s.ColumnByteLength(column_index); 168 int encrypted_number_len = s.ColumnByteLength(column_index);
164 if (encrypted_number_len) { 169 if (encrypted_number_len) {
165 std::string encrypted_number; 170 std::string encrypted_number;
166 encrypted_number.resize(encrypted_number_len); 171 encrypted_number.resize(encrypted_number_len);
167 memcpy(&encrypted_number[0], s.ColumnBlob(column_index), 172 memcpy(&encrypted_number[0], s.ColumnBlob(column_index),
168 encrypted_number_len); 173 encrypted_number_len);
169 OSCrypt::DecryptString16(encrypted_number, &credit_card_number); 174 encryptor.DecryptString16(encrypted_number, &credit_card_number);
170 } 175 }
171 return credit_card_number; 176 return credit_card_number;
172 } 177 }
173 178
174 std::unique_ptr<CreditCard> CreditCardFromStatement(const sql::Statement& s) { 179 std::unique_ptr<CreditCard> CreditCardFromStatement(
180 const sql::Statement& s,
181 const AutofillTableEncryptor& encryptor) {
175 std::unique_ptr<CreditCard> credit_card(new CreditCard); 182 std::unique_ptr<CreditCard> credit_card(new CreditCard);
176 183
177 int index = 0; 184 int index = 0;
178 credit_card->set_guid(s.ColumnString(index++)); 185 credit_card->set_guid(s.ColumnString(index++));
179 DCHECK(base::IsValidGUID(credit_card->guid())); 186 DCHECK(base::IsValidGUID(credit_card->guid()));
180 187
181 credit_card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++)); 188 credit_card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++));
182 credit_card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); 189 credit_card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++));
183 credit_card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, 190 credit_card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR,
184 s.ColumnString16(index++)); 191 s.ColumnString16(index++));
185 credit_card->SetRawInfo(CREDIT_CARD_NUMBER, 192 credit_card->SetRawInfo(CREDIT_CARD_NUMBER,
186 UnencryptedCardFromColumn(s, index++)); 193 UnencryptedCardFromColumn(s, index++, encryptor));
187 credit_card->set_use_count(s.ColumnInt64(index++)); 194 credit_card->set_use_count(s.ColumnInt64(index++));
188 credit_card->set_use_date(Time::FromTimeT(s.ColumnInt64(index++))); 195 credit_card->set_use_date(Time::FromTimeT(s.ColumnInt64(index++)));
189 credit_card->set_modification_date(Time::FromTimeT(s.ColumnInt64(index++))); 196 credit_card->set_modification_date(Time::FromTimeT(s.ColumnInt64(index++)));
190 credit_card->set_origin(s.ColumnString(index++)); 197 credit_card->set_origin(s.ColumnString(index++));
191 credit_card->set_billing_address_id(s.ColumnString(index++)); 198 credit_card->set_billing_address_id(s.ColumnString(index++));
192 199
193 return credit_card; 200 return credit_card;
194 } 201 }
195 202
196 bool AddAutofillProfileNamesToProfile(sql::Connection* db, 203 bool AddAutofillProfileNamesToProfile(sql::Connection* db,
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 } 399 }
393 400
394 return result; 401 return result;
395 } 402 }
396 403
397 } // namespace 404 } // namespace
398 405
399 // static 406 // static
400 const size_t AutofillTable::kMaxDataLength = 1024; 407 const size_t AutofillTable::kMaxDataLength = 1024;
401 408
402 AutofillTable::AutofillTable() { 409 AutofillTable::AutofillTable()
410 : autofill_table_encryptor_(
411 AutofillTableEncryptorFactory::GetInstance()->Create()) {
412 DCHECK(autofill_table_encryptor_);
403 } 413 }
404 414
405 AutofillTable::~AutofillTable() { 415 AutofillTable::~AutofillTable() {
406 } 416 }
407 417
408 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) { 418 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) {
409 return static_cast<AutofillTable*>(db->GetTable(GetKey())); 419 return static_cast<AutofillTable*>(db->GetTable(GetKey()));
410 } 420 }
411 421
412 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { 422 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const {
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after
1158 return s4.Run(); 1168 return s4.Run();
1159 } 1169 }
1160 1170
1161 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) { 1171 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) {
1162 sql::Statement s(db_->GetUniqueStatement( 1172 sql::Statement s(db_->GetUniqueStatement(
1163 "INSERT INTO credit_cards" 1173 "INSERT INTO credit_cards"
1164 "(guid, name_on_card, expiration_month, expiration_year, " 1174 "(guid, name_on_card, expiration_month, expiration_year, "
1165 " card_number_encrypted, use_count, use_date, date_modified, origin," 1175 " card_number_encrypted, use_count, use_date, date_modified, origin,"
1166 " billing_address_id)" 1176 " billing_address_id)"
1167 "VALUES (?,?,?,?,?,?,?,?,?,?)")); 1177 "VALUES (?,?,?,?,?,?,?,?,?,?)"));
1168 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s); 1178 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s,
1179 *autofill_table_encryptor_);
1169 1180
1170 if (!s.Run()) 1181 if (!s.Run())
1171 return false; 1182 return false;
1172 1183
1173 DCHECK_GT(db_->GetLastChangeCount(), 0); 1184 DCHECK_GT(db_->GetLastChangeCount(), 0);
1174 return true; 1185 return true;
1175 } 1186 }
1176 1187
1177 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard( 1188 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard(
1178 const std::string& guid) { 1189 const std::string& guid) {
1179 DCHECK(base::IsValidGUID(guid)); 1190 DCHECK(base::IsValidGUID(guid));
1180 sql::Statement s(db_->GetUniqueStatement( 1191 sql::Statement s(db_->GetUniqueStatement(
1181 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1192 "SELECT guid, name_on_card, expiration_month, expiration_year, "
1182 "card_number_encrypted, use_count, use_date, date_modified, " 1193 "card_number_encrypted, use_count, use_date, date_modified, "
1183 "origin, billing_address_id " 1194 "origin, billing_address_id "
1184 "FROM credit_cards " 1195 "FROM credit_cards "
1185 "WHERE guid = ?")); 1196 "WHERE guid = ?"));
1186 s.BindString(0, guid); 1197 s.BindString(0, guid);
1187 1198
1188 if (!s.Step()) 1199 if (!s.Step())
1189 return std::unique_ptr<CreditCard>(); 1200 return std::unique_ptr<CreditCard>();
1190 1201
1191 return CreditCardFromStatement(s); 1202 return CreditCardFromStatement(s, *autofill_table_encryptor_);
1192 } 1203 }
1193 1204
1194 bool AutofillTable::GetCreditCards( 1205 bool AutofillTable::GetCreditCards(
1195 std::vector<std::unique_ptr<CreditCard>>* credit_cards) { 1206 std::vector<std::unique_ptr<CreditCard>>* credit_cards) {
1196 DCHECK(credit_cards); 1207 DCHECK(credit_cards);
1197 credit_cards->clear(); 1208 credit_cards->clear();
1198 1209
1199 sql::Statement s(db_->GetUniqueStatement( 1210 sql::Statement s(db_->GetUniqueStatement(
1200 "SELECT guid " 1211 "SELECT guid "
1201 "FROM credit_cards " 1212 "FROM credit_cards "
(...skipping 28 matching lines...) Expand all
1230 "exp_year," // 9 1241 "exp_year," // 9
1231 "metadata.billing_address_id " // 10 1242 "metadata.billing_address_id " // 10
1232 "FROM masked_credit_cards masked " 1243 "FROM masked_credit_cards masked "
1233 "LEFT OUTER JOIN unmasked_credit_cards USING (id) " 1244 "LEFT OUTER JOIN unmasked_credit_cards USING (id) "
1234 "LEFT OUTER JOIN server_card_metadata metadata USING (id)")); 1245 "LEFT OUTER JOIN server_card_metadata metadata USING (id)"));
1235 while (s.Step()) { 1246 while (s.Step()) {
1236 int index = 0; 1247 int index = 0;
1237 1248
1238 // If the card_number_encrypted field is nonempty, we can assume this card 1249 // If the card_number_encrypted field is nonempty, we can assume this card
1239 // is a full card, otherwise it's masked. 1250 // is a full card, otherwise it's masked.
1240 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); 1251 base::string16 full_card_number =
1252 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_);
1241 base::string16 last_four = s.ColumnString16(index++); 1253 base::string16 last_four = s.ColumnString16(index++);
1242 CreditCard::RecordType record_type = full_card_number.empty() ? 1254 CreditCard::RecordType record_type = full_card_number.empty() ?
1243 CreditCard::MASKED_SERVER_CARD : 1255 CreditCard::MASKED_SERVER_CARD :
1244 CreditCard::FULL_SERVER_CARD; 1256 CreditCard::FULL_SERVER_CARD;
1245 std::string server_id = s.ColumnString(index++); 1257 std::string server_id = s.ColumnString(index++);
1246 1258
1247 std::unique_ptr<CreditCard> card = 1259 std::unique_ptr<CreditCard> card =
1248 base::MakeUnique<CreditCard>(record_type, server_id); 1260 base::MakeUnique<CreditCard>(record_type, server_id);
1249 card->SetRawInfo( 1261 card->SetRawInfo(
1250 CREDIT_CARD_NUMBER, 1262 CREDIT_CARD_NUMBER,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1337 MaskServerCreditCard(masked.server_id()); 1349 MaskServerCreditCard(masked.server_id());
1338 sql::Statement s(db_->GetUniqueStatement( 1350 sql::Statement s(db_->GetUniqueStatement(
1339 "INSERT INTO unmasked_credit_cards(" 1351 "INSERT INTO unmasked_credit_cards("
1340 "id," 1352 "id,"
1341 "card_number_encrypted," 1353 "card_number_encrypted,"
1342 "unmask_date)" 1354 "unmask_date)"
1343 "VALUES (?,?,?)")); 1355 "VALUES (?,?,?)"));
1344 s.BindString(0, masked.server_id()); 1356 s.BindString(0, masked.server_id());
1345 1357
1346 std::string encrypted_data; 1358 std::string encrypted_data;
1347 OSCrypt::EncryptString16(full_number, &encrypted_data); 1359 autofill_table_encryptor_->EncryptString16(full_number, &encrypted_data);
1348 s.BindBlob(1, encrypted_data.data(), 1360 s.BindBlob(1, encrypted_data.data(),
1349 static_cast<int>(encrypted_data.length())); 1361 static_cast<int>(encrypted_data.length()));
1350 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date 1362 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date
1351 1363
1352 s.Run(); 1364 s.Run();
1353 1365
1354 CreditCard unmasked = masked; 1366 CreditCard unmasked = masked;
1355 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); 1367 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD);
1356 unmasked.SetNumber(full_number); 1368 unmasked.SetNumber(full_number);
1357 unmasked.RecordAndLogUse(); 1369 unmasked.RecordAndLogUse();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1471 sql::Statement s(db_->GetUniqueStatement( 1483 sql::Statement s(db_->GetUniqueStatement(
1472 "UPDATE credit_cards " 1484 "UPDATE credit_cards "
1473 "SET guid=?, name_on_card=?, expiration_month=?," 1485 "SET guid=?, name_on_card=?, expiration_month=?,"
1474 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," 1486 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?,"
1475 "date_modified=?, origin=?, billing_address_id=?" 1487 "date_modified=?, origin=?, billing_address_id=?"
1476 "WHERE guid=?1")); 1488 "WHERE guid=?1"));
1477 BindCreditCardToStatement(credit_card, 1489 BindCreditCardToStatement(credit_card,
1478 update_modification_date 1490 update_modification_date
1479 ? AutofillClock::Now() 1491 ? AutofillClock::Now()
1480 : old_credit_card->modification_date(), 1492 : old_credit_card->modification_date(),
1481 &s); 1493 &s, *autofill_table_encryptor_);
1482 1494
1483 bool result = s.Run(); 1495 bool result = s.Run();
1484 DCHECK_GT(db_->GetLastChangeCount(), 0); 1496 DCHECK_GT(db_->GetLastChangeCount(), 0);
1485 return result; 1497 return result;
1486 } 1498 }
1487 1499
1488 bool AutofillTable::RemoveCreditCard(const std::string& guid) { 1500 bool AutofillTable::RemoveCreditCard(const std::string& guid) {
1489 DCHECK(base::IsValidGUID(guid)); 1501 DCHECK(base::IsValidGUID(guid));
1490 sql::Statement s(db_->GetUniqueStatement( 1502 sql::Statement s(db_->GetUniqueStatement(
1491 "DELETE FROM credit_cards WHERE guid = ?")); 1503 "DELETE FROM credit_cards WHERE guid = ?"));
(...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after
2526 if (!db_->Execute("DROP TABLE masked_credit_cards") || 2538 if (!db_->Execute("DROP TABLE masked_credit_cards") ||
2527 !db_->Execute("ALTER TABLE masked_credit_cards_temp " 2539 !db_->Execute("ALTER TABLE masked_credit_cards_temp "
2528 "RENAME TO masked_credit_cards")) { 2540 "RENAME TO masked_credit_cards")) {
2529 return false; 2541 return false;
2530 } 2542 }
2531 2543
2532 return transaction.Commit(); 2544 return transaction.Commit();
2533 } 2545 }
2534 2546
2535 } // namespace autofill 2547 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698