2016-04-18 05:11:31 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-08 00:53:00 +00:00
|
|
|
#include "shell/browser/certificate_manager_model.h"
|
2016-04-18 05:11:31 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "content/public/browser/browser_context.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2016-04-18 05:11:31 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/resource_context.h"
|
|
|
|
#include "crypto/nss_util.h"
|
|
|
|
#include "crypto/nss_util_internal.h"
|
|
|
|
#include "net/base/net_errors.h"
|
|
|
|
#include "net/cert/nss_cert_database.h"
|
|
|
|
#include "net/cert/x509_certificate.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
net::NSSCertDatabase* g_nss_cert_database = nullptr;
|
|
|
|
|
|
|
|
net::NSSCertDatabase* GetNSSCertDatabaseForResourceContext(
|
|
|
|
content::ResourceContext* context,
|
2019-05-03 19:08:41 +00:00
|
|
|
base::OnceCallback<void(net::NSSCertDatabase*)> callback) {
|
2016-04-18 05:11:31 +00:00
|
|
|
// This initialization is not thread safe. This CHECK ensures that this code
|
|
|
|
// is only run on a single thread.
|
|
|
|
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
|
|
|
if (!g_nss_cert_database) {
|
|
|
|
// Linux has only a single persistent slot compared to ChromeOS's separate
|
|
|
|
// public and private slot.
|
|
|
|
// Redirect any slot usage to this persistent slot on Linux.
|
2020-09-19 02:10:41 +00:00
|
|
|
crypto::EnsureNSSInit();
|
2016-04-18 05:11:31 +00:00
|
|
|
g_nss_cert_database = new net::NSSCertDatabase(
|
2019-10-28 22:12:35 +00:00
|
|
|
crypto::ScopedPK11Slot(PK11_GetInternalKeySlot()) /* public slot */,
|
|
|
|
crypto::ScopedPK11Slot(PK11_GetInternalKeySlot()) /* private slot */);
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|
|
|
|
return g_nss_cert_database;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// CertificateManagerModel is created on the UI thread. It needs a
|
|
|
|
// NSSCertDatabase handle (and on ChromeOS it needs to get the TPM status) which
|
|
|
|
// needs to be done on the IO thread.
|
|
|
|
//
|
|
|
|
// The initialization flow is roughly:
|
|
|
|
//
|
|
|
|
// UI thread IO Thread
|
|
|
|
//
|
|
|
|
// CertificateManagerModel::Create
|
|
|
|
// \--------------------------------------v
|
|
|
|
// CertificateManagerModel::GetCertDBOnIOThread
|
|
|
|
// |
|
|
|
|
// GetNSSCertDatabaseForResourceContext
|
|
|
|
// |
|
|
|
|
// CertificateManagerModel::DidGetCertDBOnIOThread
|
|
|
|
// v--------------------------------------/
|
|
|
|
// CertificateManagerModel::DidGetCertDBOnUIThread
|
|
|
|
// |
|
|
|
|
// new CertificateManagerModel
|
|
|
|
// |
|
|
|
|
// callback
|
|
|
|
|
|
|
|
// static
|
2018-04-18 01:57:05 +00:00
|
|
|
void CertificateManagerModel::Create(content::BrowserContext* browser_context,
|
2020-04-13 20:52:07 +00:00
|
|
|
CreationCallback callback) {
|
2016-04-18 05:11:31 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2022-05-17 16:48:40 +00:00
|
|
|
content::GetIOThreadTaskRunner({})->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(&CertificateManagerModel::GetCertDBOnIOThread,
|
2020-04-13 20:52:07 +00:00
|
|
|
browser_context->GetResourceContext(),
|
|
|
|
std::move(callback)));
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CertificateManagerModel::CertificateManagerModel(
|
|
|
|
net::NSSCertDatabase* nss_cert_database,
|
|
|
|
bool is_user_db_available)
|
2018-04-18 01:57:05 +00:00
|
|
|
: cert_db_(nss_cert_database), is_user_db_available_(is_user_db_available) {
|
2016-04-18 05:11:31 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
}
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
CertificateManagerModel::~CertificateManagerModel() = default;
|
2016-04-18 05:11:31 +00:00
|
|
|
|
2018-03-12 06:28:55 +00:00
|
|
|
int CertificateManagerModel::ImportFromPKCS12(
|
|
|
|
PK11SlotInfo* slot_info,
|
|
|
|
const std::string& data,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& password,
|
2018-03-12 06:28:55 +00:00
|
|
|
bool is_extractable,
|
|
|
|
net::ScopedCERTCertificateList* imported_certs) {
|
2018-04-18 01:57:05 +00:00
|
|
|
return cert_db_->ImportFromPKCS12(slot_info, data, password, is_extractable,
|
|
|
|
imported_certs);
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CertificateManagerModel::ImportUserCert(const std::string& data) {
|
|
|
|
return cert_db_->ImportUserCert(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateManagerModel::ImportCACerts(
|
2018-03-12 06:28:55 +00:00
|
|
|
const net::ScopedCERTCertificateList& certificates,
|
2016-04-18 05:11:31 +00:00
|
|
|
net::NSSCertDatabase::TrustBits trust_bits,
|
|
|
|
net::NSSCertDatabase::ImportCertFailureList* not_imported) {
|
|
|
|
return cert_db_->ImportCACerts(certificates, trust_bits, not_imported);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateManagerModel::ImportServerCert(
|
2018-03-12 06:28:55 +00:00
|
|
|
const net::ScopedCERTCertificateList& certificates,
|
2016-04-18 05:11:31 +00:00
|
|
|
net::NSSCertDatabase::TrustBits trust_bits,
|
|
|
|
net::NSSCertDatabase::ImportCertFailureList* not_imported) {
|
2018-04-18 01:57:05 +00:00
|
|
|
return cert_db_->ImportServerCert(certificates, trust_bits, not_imported);
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateManagerModel::SetCertTrust(
|
2018-03-12 06:28:55 +00:00
|
|
|
CERTCertificate* cert,
|
2016-04-18 05:11:31 +00:00
|
|
|
net::CertType type,
|
|
|
|
net::NSSCertDatabase::TrustBits trust_bits) {
|
|
|
|
return cert_db_->SetCertTrust(cert, type, trust_bits);
|
|
|
|
}
|
|
|
|
|
2018-03-12 06:28:55 +00:00
|
|
|
bool CertificateManagerModel::Delete(CERTCertificate* cert) {
|
2016-04-18 05:11:31 +00:00
|
|
|
return cert_db_->DeleteCertAndKey(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CertificateManagerModel::DidGetCertDBOnUIThread(
|
|
|
|
net::NSSCertDatabase* cert_db,
|
|
|
|
bool is_user_db_available,
|
2020-04-13 20:52:07 +00:00
|
|
|
CreationCallback callback) {
|
2016-04-18 05:11:31 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
|
2021-06-15 00:37:55 +00:00
|
|
|
auto model = base::WrapUnique(
|
2018-04-18 01:57:05 +00:00
|
|
|
new CertificateManagerModel(cert_db, is_user_db_available));
|
2020-04-13 20:52:07 +00:00
|
|
|
std::move(callback).Run(std::move(model));
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CertificateManagerModel::DidGetCertDBOnIOThread(
|
2020-04-13 20:52:07 +00:00
|
|
|
CreationCallback callback,
|
2016-04-18 05:11:31 +00:00
|
|
|
net::NSSCertDatabase* cert_db) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
|
|
|
bool is_user_db_available = !!cert_db->GetPublicSlot();
|
2022-05-17 16:48:40 +00:00
|
|
|
content::GetUIThreadTaskRunner({})->PostTask(
|
|
|
|
FROM_HERE,
|
2019-05-03 19:08:41 +00:00
|
|
|
base::BindOnce(&CertificateManagerModel::DidGetCertDBOnUIThread, cert_db,
|
2020-04-13 20:52:07 +00:00
|
|
|
is_user_db_available, std::move(callback)));
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CertificateManagerModel::GetCertDBOnIOThread(
|
|
|
|
content::ResourceContext* context,
|
2020-04-13 20:52:07 +00:00
|
|
|
CreationCallback callback) {
|
2016-04-18 05:11:31 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2020-04-13 20:52:07 +00:00
|
|
|
|
2021-06-16 22:43:51 +00:00
|
|
|
auto split_callback = base::SplitOnceCallback(base::BindOnce(
|
|
|
|
&CertificateManagerModel::DidGetCertDBOnIOThread, std::move(callback)));
|
2020-04-13 20:52:07 +00:00
|
|
|
|
2021-06-16 22:43:51 +00:00
|
|
|
net::NSSCertDatabase* cert_db = GetNSSCertDatabaseForResourceContext(
|
|
|
|
context, std::move(split_callback.first));
|
2020-04-13 20:52:07 +00:00
|
|
|
|
|
|
|
// If the NSS database was already available, |cert_db| is non-null and
|
|
|
|
// |did_get_cert_db_callback| has not been called. Call it explicitly.
|
2016-04-18 05:11:31 +00:00
|
|
|
if (cert_db)
|
2021-06-16 22:43:51 +00:00
|
|
|
std::move(split_callback.second).Run(cert_db);
|
2016-04-18 05:11:31 +00:00
|
|
|
}
|