certificate_manager_model.h 4.9 KB

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