certificate_manager_model.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef ELECTRON_SHELL_BROWSER_CERTIFICATE_MANAGER_MODEL_H_
  5. #define ELECTRON_SHELL_BROWSER_CERTIFICATE_MANAGER_MODEL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/functional/callback_forward.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "net/cert/nss_cert_database.h"
  11. // CertificateManagerModel provides the data to be displayed in the certificate
  12. // manager dialog, and processes changes from the view.
  13. class CertificateManagerModel {
  14. public:
  15. using CreationCallback =
  16. base::OnceCallback<void(std::unique_ptr<CertificateManagerModel>)>;
  17. // Creates a CertificateManagerModel. The model will be passed to the callback
  18. // when it is ready.
  19. static void Create(CreationCallback callback);
  20. // disable copy
  21. CertificateManagerModel(const CertificateManagerModel&) = delete;
  22. CertificateManagerModel& operator=(const CertificateManagerModel&) = delete;
  23. ~CertificateManagerModel();
  24. bool is_user_db_available() const { return is_user_db_available_; }
  25. // Accessor for read-only access to the underlying NSSCertDatabase.
  26. const net::NSSCertDatabase* cert_db() const { return cert_db_; }
  27. // Import private keys and certificates from PKCS #12 encoded
  28. // |data|, using the given |password|. If |is_extractable| is false,
  29. // mark the private key as unextractable from the module.
  30. // Returns a net error code on failure.
  31. int ImportFromPKCS12(PK11SlotInfo* slot_info,
  32. const std::string& data,
  33. const std::u16string& password,
  34. bool is_extractable,
  35. net::ScopedCERTCertificateList* imported_certs);
  36. // Import user certificate from DER encoded |data|.
  37. // Returns a net error code on failure.
  38. int ImportUserCert(const std::string& data);
  39. // Import CA certificates.
  40. // Tries to import all the certificates given. The root will be trusted
  41. // according to |trust_bits|. Any certificates that could not be imported
  42. // will be listed in |not_imported|.
  43. // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
  44. // Returns false if there is an internal error, otherwise true is returned and
  45. // |not_imported| should be checked for any certificates that were not
  46. // imported.
  47. bool ImportCACerts(const net::ScopedCERTCertificateList& certificates,
  48. net::NSSCertDatabase::TrustBits trust_bits,
  49. net::NSSCertDatabase::ImportCertFailureList* not_imported);
  50. // Import server certificate. The first cert should be the server cert. Any
  51. // additional certs should be intermediate/CA certs and will be imported but
  52. // not given any trust.
  53. // Any certificates that could not be imported will be listed in
  54. // |not_imported|.
  55. // |trust_bits| can be set to explicitly trust or distrust the certificate, or
  56. // use TRUST_DEFAULT to inherit trust as normal.
  57. // Returns false if there is an internal error, otherwise true is returned and
  58. // |not_imported| should be checked for any certificates that were not
  59. // imported.
  60. bool ImportServerCert(
  61. const net::ScopedCERTCertificateList& certificates,
  62. net::NSSCertDatabase::TrustBits trust_bits,
  63. net::NSSCertDatabase::ImportCertFailureList* not_imported);
  64. // Set trust values for certificate.
  65. // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
  66. // Returns true on success or false on failure.
  67. bool SetCertTrust(CERTCertificate* cert,
  68. net::CertType type,
  69. net::NSSCertDatabase::TrustBits trust_bits);
  70. // Delete the cert. Returns true on success. |cert| is still valid when this
  71. // function returns.
  72. bool Delete(CERTCertificate* cert);
  73. private:
  74. CertificateManagerModel(net::NSSCertDatabase* nss_cert_database,
  75. bool is_user_db_available);
  76. // Methods used during initialization, see the comment at the top of the .cc
  77. // file for details.
  78. static void DidGetCertDBOnUIThread(net::NSSCertDatabase* cert_db,
  79. bool is_user_db_available,
  80. CreationCallback callback);
  81. static void DidGetCertDBOnIOThread(CreationCallback callback,
  82. net::NSSCertDatabase* cert_db);
  83. static void GetCertDBOnIOThread(CreationCallback callback);
  84. raw_ptr<net::NSSCertDatabase> cert_db_;
  85. // Whether the certificate database has a public slot associated with the
  86. // profile. If not set, importing certificates is not allowed with this model.
  87. bool is_user_db_available_;
  88. };
  89. #endif // ELECTRON_SHELL_BROWSER_CERTIFICATE_MANAGER_MODEL_H_