15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef NET_CERT_NSS_CERT_DATABASE_H_
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define NET_CERT_NSS_CERT_DATABASE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback_forward.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/weak_ptr.h"
155e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string16.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "crypto/scoped_nss_types.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/base/net_errors.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h"
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/cert_type.h"
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/cert/x509_certificate.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace base {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class TaskRunner;
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <class ObserverType> class ObserverListThreadSafe;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CryptoModule;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Provides functions to manipulate the NSS certificate stores.
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Forwards notifications about certificate changes to the global CertDatabase
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// singleton.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT NSSCertDatabase {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NET_EXPORT Observer {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~Observer() {}
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Will be called when a new certificate is added.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called with |cert| == NULL after importing a list of certificates
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // in ImportFromPKCS12().
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnCertAdded(const X509Certificate* cert) {}
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Will be called when a certificate is removed.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnCertRemoved(const X509Certificate* cert) {}
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Will be called when a CA certificate is changed.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called with |cert| == NULL after importing a list of certificates
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // in ImportCACerts().
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    virtual void OnCACertChanged(const X509Certificate* cert) {}
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   protected:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Observer() {}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(Observer);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stores per-certificate error codes for import failures.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct NET_EXPORT ImportCertFailure {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ImportCertFailure(const scoped_refptr<X509Certificate>& cert, int err);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~ImportCertFailure();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<X509Certificate> certificate;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int net_error;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<ImportCertFailure> ImportCertFailureList;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constants that define which usages a certificate is trusted for.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // They are used in combination with CertType to specify trust for each type
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of certificate.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For a CA_CERT, they specify that the CA is trusted for issuing server and
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // client certs of each type.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For SERVER_CERT, only TRUSTED_SSL makes sense, and specifies the cert is
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // trusted as a server.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For EMAIL_CERT, only TRUSTED_EMAIL makes sense, and specifies the cert is
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // trusted for email.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DISTRUSTED_* specifies that the cert should not be trusted for the given
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // usage, regardless of whether it would otherwise inherit trust from the
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // issuer chain.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use TRUST_DEFAULT to inherit trust as normal.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: The actual constants are defined using an enum instead of static
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // consts due to compilation/linkage constraints with template functions.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef uint32 TrustBits;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum {
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TRUST_DEFAULT         =      0,
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TRUSTED_SSL           = 1 << 0,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TRUSTED_EMAIL         = 1 << 1,
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TRUSTED_OBJ_SIGN      = 1 << 2,
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISTRUSTED_SSL        = 1 << 3,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISTRUSTED_EMAIL      = 1 << 4,
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISTRUSTED_OBJ_SIGN   = 1 << 5,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::Callback<void(scoped_ptr<CertificateList> certs)>
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ListCertsCallback;
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  typedef base::Callback<void(bool)> DeleteCertCallback;
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Creates a NSSCertDatabase that will store public information (such as
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // certificates and trust records) in |public_slot|, and private information
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // (such as keys) in |private_slot|.
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // In general, code should avoid creating an NSSCertDatabase directly,
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // as doing so requires making opinionated decisions about where to store
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // data, and instead prefer to be passed an existing NSSCertDatabase
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // instance.
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // |public_slot| must not be NULL, |private_slot| can be NULL. Both slots can
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // be identical.
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  NSSCertDatabase(crypto::ScopedPK11Slot public_slot,
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  crypto::ScopedPK11Slot private_slot);
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual ~NSSCertDatabase();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get a list of unique certificates in the certificate database (one
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instance of all certificates).
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // DEPRECATED by |ListCerts|. See http://crbug.com/340460.
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ListCertsSync(CertificateList* certs);
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Asynchronously get a list of unique certificates in the certificate
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // database (one instance of all certificates). Note that the callback may be
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // run even after the database is deleted.
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ListCerts(const ListCertsCallback& callback);
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Get a list of certificates in the certificate database of the given slot.
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Note that the callback may be run even after the database is deleted.
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Must be called on the IO thread and it calls |callback| on the IO thread.
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This does not block by retrieving the certs asynchronously on a worker
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // thread. Never calls |callback| synchronously.
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void ListCertsInSlot(const ListCertsCallback& callback,
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                               PK11SlotInfo* slot);
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if defined(OS_CHROMEOS)
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Get the slot for system-wide key data. May be NULL if the system token was
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // not explicitly set.
1375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Note: The System slot is set after the NSSCertDatabase is constructed and
1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // this call returns synchronously. Thus, it is possible to call this function
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // before SetSystemSlot is called and get a NULL result.
1405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // See https://crbug.com/399554 .
1415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual crypto::ScopedPK11Slot GetSystemSlot() const;
1425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
1435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Get the default slot for public key data.
1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  crypto::ScopedPK11Slot GetPublicSlot() const;
146a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Get the default slot for private key or mixed private/public key data.
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Can return NULL.
1495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  crypto::ScopedPK11Slot GetPrivateSlot() const;
150a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the default module for public key data.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The returned pointer must be stored in a scoped_refptr<CryptoModule>.
153a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // DEPRECATED: use GetPublicSlot instead.
154a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // TODO(mattm): remove usage of this method and remove it.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CryptoModule* GetPublicModule() const;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the default module for private key or mixed private/public key data.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The returned pointer must be stored in a scoped_refptr<CryptoModule>.
159a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // DEPRECATED: use GetPrivateSlot instead.
160a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // TODO(mattm): remove usage of this method and remove it.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CryptoModule* GetPrivateModule() const;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get all modules.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |need_rw| is true, only writable modules will be returned.
165a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // TODO(mattm): come up with better alternative to CryptoModuleList.
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ListModules(CryptoModuleList* modules, bool need_rw) const;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Import certificates and private keys from PKCS #12 blob into the module.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |is_extractable| is false, mark the private key as being unextractable
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from the module.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns OK or a network error code such as ERR_PKCS12_IMPORT_BAD_PASSWORD
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or ERR_PKCS12_IMPORT_ERROR. |imported_certs|, if non-NULL, returns a list
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of certs that were imported.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int ImportFromPKCS12(CryptoModule* module,
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const std::string& data,
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       const base::string16& password,
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       bool is_extractable,
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       CertificateList* imported_certs);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Export the given certificates and private keys into a PKCS #12 blob,
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // storing into |output|.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of certificates successfully exported.
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  int ExportToPKCS12(const CertificateList& certs,
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                     const base::string16& password,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     std::string* output) const;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Uses similar logic to nsNSSCertificateDB::handleCACertDownload to find the
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // root.  Assumes the list is an ordered hierarchy with the root being either
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the first or last element.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(mattm): improve this to handle any order.
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  X509Certificate* FindRootInList(const CertificateList& certificates) const;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Import CA certificates.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tries to import all the certificates given.  The root will be trusted
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // according to |trust_bits|.  Any certificates that could not be imported
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will be listed in |not_imported|.
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if there is an internal error, otherwise true is returned and
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |not_imported| should be checked for any certificates that were not
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // imported.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ImportCACerts(const CertificateList& certificates,
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     TrustBits trust_bits,
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     ImportCertFailureList* not_imported);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Import server certificate.  The first cert should be the server cert.  Any
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // additional certs should be intermediate/CA certs and will be imported but
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not given any trust.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Any certificates that could not be imported will be listed in
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |not_imported|.
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |trust_bits| can be set to explicitly trust or distrust the certificate, or
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // use TRUST_DEFAULT to inherit trust as normal.
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if there is an internal error, otherwise true is returned and
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |not_imported| should be checked for any certificates that were not
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // imported.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ImportServerCert(const CertificateList& certificates,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        TrustBits trust_bits,
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        ImportCertFailureList* not_imported);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get trust bits for certificate.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TrustBits GetCertTrust(const X509Certificate* cert, CertType type) const;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IsUntrusted returns true if |cert| is specifically untrusted. These
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // certificates are stored in the database for the specific purpose of
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // rejecting them.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsUntrusted(const X509Certificate* cert) const;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set trust values for certificate.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success or false on failure.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetCertTrust(const X509Certificate* cert,
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    CertType type,
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    TrustBits trust_bits);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delete certificate and associated private key (if one exists).
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |cert| is still valid when this function returns. Returns true on
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // success.
235cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool DeleteCertAndKey(X509Certificate* cert);
236cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
237cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Like DeleteCertAndKey but does not block by running the removal on a worker
238cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // thread. This must be called on IO thread and it will run |callback| on IO
239cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // thread. Never calls |callback| synchronously.
240cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void DeleteCertAndKeyAsync(const scoped_refptr<X509Certificate>& cert,
241cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             const DeleteCertCallback& callback);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check whether cert is stored in a readonly slot.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsReadOnly(const X509Certificate* cert) const;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
246f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Check whether cert is stored in a hardware slot.
247f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool IsHardwareBacked(const X509Certificate* cert) const;
248f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Overrides task runner that's used for running slow tasks.
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetSlowTaskRunnerForTest(
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const scoped_refptr<base::TaskRunner>& task_runner);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
254cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Certificate listing implementation used by |ListCerts*| and
255cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |ListCertsSync|. Static so it may safely be used on the worker thread.
256cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // If |slot| is NULL, obtains the certs of all slots, otherwise only of
257cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |slot|.
258cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static void ListCertsImpl(crypto::ScopedPK11Slot slot,
259cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            CertificateList* certs);
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Gets task runner that should be used for slow tasks like certificate
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // listing. Defaults to a base::WorkerPool runner, but may be overriden
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // in tests (see SetSlowTaskRunnerForTest).
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const;
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Registers |observer| to receive notifications of certificate changes.  The
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // thread on which this is called is the thread on which |observer| will be
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // called back with notifications.
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // NOTE: Observers registered here will only receive notifications generated
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // directly through the NSSCertDatabase, but not those from the CertDatabase.
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // CertDatabase observers will receive all certificate notifications.
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void AddObserver(Observer* observer);
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Unregisters |observer| from receiving notifications.  This must be called
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // on the same thread on which AddObserver() was called.
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void RemoveObserver(Observer* observer);
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
279cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Notifies observers of the removal of |cert| and calls |callback| with
280cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |success| as argument.
281cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert,
282cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                    const DeleteCertCallback& callback,
283cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                    bool success);
284cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Broadcasts notifications to all registered observers.
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NotifyObserversOfCertAdded(const X509Certificate* cert);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NotifyObserversOfCertRemoved(const X509Certificate* cert);
2881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void NotifyObserversOfCACertChanged(const X509Certificate* cert);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
290cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Certificate removal implementation used by |DeleteCertAndKey*|. Static so
291cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // it may safely be used on the worker thread.
292cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert);
293cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  crypto::ScopedPK11Slot public_slot_;
2955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  crypto::ScopedPK11Slot private_slot_;
2965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // A helper observer that forwards events from this database to CertDatabase.
298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<Observer> cert_notification_forwarder_;
299116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Task runner that should be used in tests if set.
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<base::TaskRunner> slow_task_runner_for_test_;
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::WeakPtrFactory<NSSCertDatabase> weak_factory_;
306cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase);
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif  // NET_CERT_NSS_CERT_DATABASE_H_
313