1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/payments/content/payment_method_manifest_table.h"
#include <time.h>
#include <string>
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/time/time.h"
#include "components/payments/content/browser_binding/browser_bound_key_metadata.h"
#include "components/payments/core/secure_payment_confirmation_credential.h"
#include "components/webdata/common/web_database.h"
#include "content/public/common/content_features.h"
#include "sql/statement.h"
#include "sql/transaction.h"
namespace payments {
namespace {
// Data valid duration in seconds.
const time_t PAYMENT_METHOD_MANIFEST_VALID_TIME_IN_SECONDS = 90 * 24 * 60 * 60;
WebDatabaseTable::TypeKey GetPaymentMethodManifestKey() {
// We just need a unique constant. Use the address of a static that
// COMDAT folding won't touch in an optimizing linker.
static int table_key = 0;
return reinterpret_cast<void*>(&table_key);
}
} // namespace
PaymentMethodManifestTable::PaymentMethodManifestTable() = default;
PaymentMethodManifestTable::~PaymentMethodManifestTable() = default;
PaymentMethodManifestTable* PaymentMethodManifestTable::FromWebDatabase(
WebDatabase* db) {
return static_cast<PaymentMethodManifestTable*>(
db->GetTable(GetPaymentMethodManifestKey()));
}
WebDatabaseTable::TypeKey PaymentMethodManifestTable::GetTypeKey() const {
return GetPaymentMethodManifestKey();
}
bool PaymentMethodManifestTable::CreateTablesIfNecessary() {
if (!db()->Execute("CREATE TABLE IF NOT EXISTS payment_method_manifest ( "
"expire_date INTEGER NOT NULL DEFAULT 0, "
"method_name VARCHAR, "
"web_app_id VARCHAR)")) {
LOG(ERROR) << "Cannot create the payment_method_manifest table";
return false;
}
// TODO(crbug.com/384940851): Update secure_payment_confirmation_instrument's
// primary key to the pair of (credential_id, relying_party_id).
// The `credential_id` column is 20 bytes for UbiKey on Linux, but the size
// can vary for different authenticators. The relatively small sizes make it
// OK to make `credential_id` the primary key.
if (!db()->Execute(
"CREATE TABLE IF NOT EXISTS secure_payment_confirmation_instrument ( "
"credential_id BLOB NOT NULL PRIMARY KEY, "
"relying_party_id VARCHAR NOT NULL, "
"label VARCHAR NOT NULL, "
"icon BLOB NOT NULL)")) {
LOG(ERROR)
<< "Cannot create the secure_payment_confirmation_instrument table";
return false;
}
if (!db()->DoesColumnExist("secure_payment_confirmation_instrument",
"date_created")) {
if (!db()->Execute(
"ALTER TABLE secure_payment_confirmation_instrument ADD COLUMN "
"date_created INTEGER NOT NULL DEFAULT 0")) {
LOG(ERROR)
<< "Cannot alter the secure_payment_confirmation_instrument table";
return false;
}
}
if (!db()->DoesColumnExist("secure_payment_confirmation_instrument",
"user_id")) {
if (!db()->Execute(
"ALTER TABLE secure_payment_confirmation_instrument ADD COLUMN "
"user_id BLOB")) {
LOG(ERROR)
<< "Cannot alter the secure_payment_confirmation_instrument table";
return false;
}
}
if (!db()->Execute("CREATE TABLE IF NOT EXISTS "
"secure_payment_confirmation_browser_bound_key ( "
"credential_id BLOB NOT NULL, "
"relying_party_id TEXT NOT NULL, "
"browser_bound_key_id BLOB, "
"PRIMARY KEY (credential_id, relying_party_id))")) {
LOG(ERROR)
<< "Cannot create the secure_payment_confirmation_browser_bound_key "
<< "table";
return false;
}
return true;
}
bool PaymentMethodManifestTable::MigrateToVersion(
int version,
bool* update_compatible_version) {
return true;
}
void PaymentMethodManifestTable::RemoveExpiredData() {
const base::Time now_date_in_seconds = base::Time::NowFromSystemTime();
sql::Statement s(db()->GetUniqueStatement(
"DELETE FROM payment_method_manifest WHERE expire_date < ?"));
s.BindTime(0, now_date_in_seconds);
s.Run();
}
bool PaymentMethodManifestTable::ClearSecurePaymentConfirmationCredentials(
base::Time begin,
base::Time end) {
// TODO(crbug.com/384959121): Clear browser bound key identifiers along with
// the associated browser bound keys.
sql::Statement s(db()->GetUniqueStatement(
"DELETE FROM secure_payment_confirmation_instrument WHERE (date_created "
">= ? AND date_created < ?) OR (date_created = 0)"));
s.BindTime(0, begin);
s.BindTime(1, end);
return s.Run();
}
bool PaymentMethodManifestTable::AddManifest(
const std::string& payment_method,
const std::vector<std::string>& web_app_ids) {
sql::Transaction transaction(db());
if (!transaction.Begin())
return false;
sql::Statement s1(db()->GetUniqueStatement(
"DELETE FROM payment_method_manifest WHERE method_name=?"));
s1.BindString(0, payment_method);
if (!s1.Run())
return false;
sql::Statement s2(
db()->GetUniqueStatement("INSERT INTO payment_method_manifest "
"(expire_date, method_name, web_app_id) "
"VALUES (?, ?, ?)"));
const base::Time expire_date =
base::Time::FromTimeT(base::Time::NowFromSystemTime().ToTimeT() +
PAYMENT_METHOD_MANIFEST_VALID_TIME_IN_SECONDS);
for (const auto& id : web_app_ids) {
int index = 0;
s2.BindTime(index++, expire_date);
s2.BindString(index++, payment_method);
s2.BindString(index, id);
if (!s2.Run())
return false;
s2.Reset(true);
}
if (!transaction.Commit())
return false;
return true;
}
std::vector<std::string> PaymentMethodManifestTable::GetManifest(
const std::string& payment_method) {
std::vector<std::string> web_app_ids;
sql::Statement s(
db()->GetUniqueStatement("SELECT web_app_id "
"FROM payment_method_manifest "
"WHERE method_name=?"));
s.BindString(0, payment_method);
while (s.Step()) {
web_app_ids.emplace_back(s.ColumnString(0));
}
return web_app_ids;
}
bool PaymentMethodManifestTable::AddSecurePaymentConfirmationCredential(
const SecurePaymentConfirmationCredential& credential) {
if (!credential.IsValidNewCredential())
return false;
sql::Transaction transaction(db());
if (!transaction.Begin())
return false;
{
// Check for credential identifier reuse by a different relying party.
sql::Statement s0(
db()->GetUniqueStatement("SELECT label "
"FROM secure_payment_confirmation_instrument "
"WHERE credential_id=? "
"AND relying_party_id<>?"));
int index = 0;
s0.BindBlob(index++, credential.credential_id);
s0.BindString(index++, credential.relying_party_id);
if (s0.Step())
return false;
}
{
sql::Statement s1(db()->GetUniqueStatement(
"DELETE FROM secure_payment_confirmation_instrument "
"WHERE credential_id=?"));
s1.BindBlob(0, credential.credential_id);
if (!s1.Run())
return false;
}
{
// The system authenticator will overwrite a discoverable credential with
// the same relying party and user ID, so we also clear any such credential.
sql::Statement s2(db()->GetUniqueStatement(
"DELETE FROM secure_payment_confirmation_instrument "
"WHERE relying_party_id=? "
"AND user_id=?"));
int index = 0;
s2.BindString(index++, credential.relying_party_id);
s2.BindBlob(index++, credential.user_id);
if (!s2.Run())
return false;
}
{
sql::Statement s3(db()->GetUniqueStatement(
"INSERT INTO secure_payment_confirmation_instrument "
"(credential_id, relying_party_id, user_id, label, icon, date_created) "
"VALUES (?, ?, ?, ?, ?, ?)"));
int index = 0;
s3.BindBlob(index++, credential.credential_id);
s3.BindString(index++, credential.relying_party_id);
s3.BindBlob(index++, credential.user_id);
s3.BindString(index++, std::string());
s3.BindBlob(index++, std::vector<uint8_t>());
s3.BindTime(index++, base::Time::Now());
if (!s3.Run())
return false;
}
if (!transaction.Commit())
return false;
return true;
}
std::vector<std::unique_ptr<SecurePaymentConfirmationCredential>>
PaymentMethodManifestTable::GetSecurePaymentConfirmationCredentials(
std::vector<std::vector<uint8_t>> credential_ids,
const std::string& relying_party_id) {
std::vector<std::unique_ptr<SecurePaymentConfirmationCredential>> credentials;
sql::Statement s(
db()->GetUniqueStatement("SELECT relying_party_id, user_id "
"FROM secure_payment_confirmation_instrument "
"WHERE credential_id=? "
"AND relying_party_id=?"));
// The `credential_id` temporary variable is not `const` because it is
// std::move()'d into the credential below.
for (auto& credential_id : credential_ids) {
s.Reset(true);
if (credential_id.empty())
continue;
s.BindBlob(0, credential_id);
s.BindString(1, relying_party_id);
if (!s.Step())
continue;
auto credential = std::make_unique<SecurePaymentConfirmationCredential>();
credential->credential_id = std::move(credential_id);
int index = 0;
credential->relying_party_id = s.ColumnString(index++);
s.ColumnBlobAsVector(index++, &(credential->user_id));
if (!credential->IsValid())
continue;
credentials.push_back(std::move(credential));
}
return credentials;
}
bool PaymentMethodManifestTable::SetBrowserBoundKey(
std::vector<uint8_t> credential_id,
std::string_view relying_party_id,
std::vector<uint8_t> browser_bound_key_id) {
if (credential_id.empty() || relying_party_id.empty() ||
browser_bound_key_id.empty()) {
return false;
}
sql::Statement s(db()->GetUniqueStatement(
"INSERT INTO secure_payment_confirmation_browser_bound_key ( "
"credential_id, relying_party_id, browser_bound_key_id) "
"VALUES (?, ?, ?)"));
int index = 0;
s.BindBlob(index++, std::move(credential_id));
s.BindString(index++, relying_party_id);
s.BindBlob(index++, browser_bound_key_id);
return s.Run();
}
std::optional<std::vector<uint8_t>>
PaymentMethodManifestTable::GetBrowserBoundKey(
std::vector<uint8_t> credential_id,
std::string_view relying_party_id) {
sql::Statement s(db()->GetUniqueStatement(
"SELECT browser_bound_key_id "
"FROM secure_payment_confirmation_browser_bound_key "
"WHERE credential_id = ? AND relying_party_id = ?"));
int index = 0;
s.BindBlob(index++, std::move(credential_id));
s.BindString(index++, relying_party_id);
if (!s.Step()) {
return std::nullopt;
}
if (s.GetColumnType(0) != sql::ColumnType::kBlob) {
return std::nullopt;
}
base::span<const uint8_t> browser_bound_key_span = s.ColumnBlob(0);
return std::vector<uint8_t>(browser_bound_key_span.begin(),
browser_bound_key_span.end());
}
std::vector<BrowserBoundKeyMetadata>
PaymentMethodManifestTable::GetAllBrowserBoundKeys() {
sql::Statement s(db()->GetUniqueStatement(
"SELECT relying_party_id, credential_id, browser_bound_key_id "
"FROM secure_payment_confirmation_browser_bound_key"));
std::vector<BrowserBoundKeyMetadata> browser_bound_keys;
while (s.Step()) {
BrowserBoundKeyMetadata& entry = browser_bound_keys.emplace_back();
entry.passkey.relying_party_id = s.ColumnString(0);
s.ColumnBlobAsVector(1, &entry.passkey.credential_id);
s.ColumnBlobAsVector(2, &entry.browser_bound_key_id);
}
return browser_bound_keys;
}
bool PaymentMethodManifestTable::DeleteBrowserBoundKeys(
std::vector<BrowserBoundKeyMetadata::RelyingPartyAndCredentialId>
passkeys) {
for (auto& passkey : passkeys) {
sql::Statement s(db()->GetUniqueStatement(
"DELETE FROM secure_payment_confirmation_browser_bound_key "
"WHERE relying_party_id = ? AND credential_id = ?"));
s.BindString(0, passkey.relying_party_id);
s.BindBlob(1, std::move(passkey.credential_id));
if (!s.Run()) {
return false;
}
}
return true;
}
bool PaymentMethodManifestTable::ExecuteForTest(const base::cstring_view sql) {
return db()->Execute(sql);
}
bool PaymentMethodManifestTable::RazeForTest() {
return db()->Raze();
}
bool PaymentMethodManifestTable::DoesColumnExistForTest(
const base::cstring_view table_name,
const base::cstring_view column_name) {
return db()->DoesColumnExist(table_name, column_name);
}
} // namespace payments
|