certificate_manager_model.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/certificate_manager_model.h"
  5. #include <utility>
  6. #include "base/functional/bind.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "content/public/browser/browser_task_traits.h"
  9. #include "content/public/browser/browser_thread.h"
  10. #include "content/public/browser/resource_context.h"
  11. #include "crypto/nss_util.h"
  12. #include "crypto/nss_util_internal.h"
  13. #include "net/base/net_errors.h"
  14. #include "net/cert/nss_cert_database.h"
  15. #include "net/cert/x509_certificate.h"
  16. using content::BrowserThread;
  17. namespace {
  18. net::NSSCertDatabase* g_nss_cert_database = nullptr;
  19. net::NSSCertDatabase* GetNSSCertDatabase(
  20. base::OnceCallback<void(net::NSSCertDatabase*)> callback) {
  21. // This initialization is not thread safe. This CHECK ensures that this code
  22. // is only run on a single thread.
  23. CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
  24. if (!g_nss_cert_database) {
  25. // Linux has only a single persistent slot compared to ChromeOS's separate
  26. // public and private slot.
  27. // Redirect any slot usage to this persistent slot on Linux.
  28. crypto::EnsureNSSInit();
  29. g_nss_cert_database = new net::NSSCertDatabase(
  30. crypto::ScopedPK11Slot(PK11_GetInternalKeySlot()) /* public slot */,
  31. crypto::ScopedPK11Slot(PK11_GetInternalKeySlot()) /* private slot */);
  32. }
  33. return g_nss_cert_database;
  34. }
  35. } // namespace
  36. // CertificateManagerModel is created on the UI thread. It needs a
  37. // NSSCertDatabase handle (and on ChromeOS it needs to get the TPM status) which
  38. // needs to be done on the IO thread.
  39. //
  40. // The initialization flow is roughly:
  41. //
  42. // UI thread IO Thread
  43. //
  44. // CertificateManagerModel::Create
  45. // \--------------------------------------v
  46. // CertificateManagerModel::GetCertDBOnIOThread
  47. // |
  48. // GetNSSCertDatabase
  49. // |
  50. // CertificateManagerModel::DidGetCertDBOnIOThread
  51. // v--------------------------------------/
  52. // CertificateManagerModel::DidGetCertDBOnUIThread
  53. // |
  54. // new CertificateManagerModel
  55. // |
  56. // callback
  57. // static
  58. void CertificateManagerModel::Create(CreationCallback callback) {
  59. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  60. content::GetIOThreadTaskRunner({})->PostTask(
  61. FROM_HERE, base::BindOnce(&CertificateManagerModel::GetCertDBOnIOThread,
  62. std::move(callback)));
  63. }
  64. CertificateManagerModel::CertificateManagerModel(
  65. net::NSSCertDatabase* nss_cert_database,
  66. bool is_user_db_available)
  67. : cert_db_(nss_cert_database), is_user_db_available_(is_user_db_available) {
  68. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  69. }
  70. CertificateManagerModel::~CertificateManagerModel() = default;
  71. int CertificateManagerModel::ImportFromPKCS12(
  72. PK11SlotInfo* slot_info,
  73. const std::string& data,
  74. const std::u16string& password,
  75. bool is_extractable,
  76. net::ScopedCERTCertificateList* imported_certs) {
  77. return cert_db_->ImportFromPKCS12(slot_info, data, password, is_extractable,
  78. imported_certs);
  79. }
  80. int CertificateManagerModel::ImportUserCert(const std::string& data) {
  81. return cert_db_->ImportUserCert(data);
  82. }
  83. bool CertificateManagerModel::ImportCACerts(
  84. const net::ScopedCERTCertificateList& certificates,
  85. net::NSSCertDatabase::TrustBits trust_bits,
  86. net::NSSCertDatabase::ImportCertFailureList* not_imported) {
  87. return cert_db_->ImportCACerts(certificates, trust_bits, not_imported);
  88. }
  89. bool CertificateManagerModel::ImportServerCert(
  90. const net::ScopedCERTCertificateList& certificates,
  91. net::NSSCertDatabase::TrustBits trust_bits,
  92. net::NSSCertDatabase::ImportCertFailureList* not_imported) {
  93. return cert_db_->ImportServerCert(certificates, trust_bits, not_imported);
  94. }
  95. bool CertificateManagerModel::SetCertTrust(
  96. CERTCertificate* cert,
  97. net::CertType type,
  98. net::NSSCertDatabase::TrustBits trust_bits) {
  99. return cert_db_->SetCertTrust(cert, type, trust_bits);
  100. }
  101. bool CertificateManagerModel::Delete(CERTCertificate* cert) {
  102. return cert_db_->DeleteCertAndKey(cert);
  103. }
  104. // static
  105. void CertificateManagerModel::DidGetCertDBOnUIThread(
  106. net::NSSCertDatabase* cert_db,
  107. bool is_user_db_available,
  108. CreationCallback callback) {
  109. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  110. auto model = base::WrapUnique(
  111. new CertificateManagerModel(cert_db, is_user_db_available));
  112. std::move(callback).Run(std::move(model));
  113. }
  114. // static
  115. void CertificateManagerModel::DidGetCertDBOnIOThread(
  116. CreationCallback callback,
  117. net::NSSCertDatabase* cert_db) {
  118. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  119. bool is_user_db_available = !!cert_db->GetPublicSlot();
  120. content::GetUIThreadTaskRunner({})->PostTask(
  121. FROM_HERE,
  122. base::BindOnce(&CertificateManagerModel::DidGetCertDBOnUIThread, cert_db,
  123. is_user_db_available, std::move(callback)));
  124. }
  125. // static
  126. void CertificateManagerModel::GetCertDBOnIOThread(CreationCallback callback) {
  127. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  128. auto split_callback = base::SplitOnceCallback(base::BindOnce(
  129. &CertificateManagerModel::DidGetCertDBOnIOThread, std::move(callback)));
  130. net::NSSCertDatabase* cert_db =
  131. GetNSSCertDatabase(std::move(split_callback.first));
  132. // If the NSS database was already available, |cert_db| is non-null and
  133. // |did_get_cert_db_callback| has not been called. Call it explicitly.
  134. if (cert_db)
  135. std::move(split_callback.second).Run(cert_db);
  136. }