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 5 #ifndef NET_CERT_CERT_DATABASE_H_ 6 #define NET_CERT_CERT_DATABASE_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "net/base/net_export.h" 12 #include "net/cert/x509_certificate.h" 13 14 template <typename T> struct DefaultSingletonTraits; 15 template <class ObserverType> class ObserverListThreadSafe; 16 17 namespace net { 18 19 // This class provides cross-platform functions to verify and add user 20 // certificates, and to observe changes to the underlying certificate stores. 21 22 // TODO(gauravsh): This class could be augmented with methods 23 // for all operations that manipulate the underlying system 24 // certificate store. 25 26 class NET_EXPORT CertDatabase { 27 public: 28 // A CertDatabase::Observer will be notified on certificate database changes. 29 // The change could be either a new user certificate is added or trust on 30 // a certificate is changed. Observers can register themselves 31 // via CertDatabase::AddObserver, and can un-register with 32 // CertDatabase::RemoveObserver. 33 class NET_EXPORT Observer { 34 public: 35 virtual ~Observer() {} 36 37 // Will be called when a new certificate is added. 38 virtual void OnCertAdded(const X509Certificate* cert) {} 39 40 // Will be called when a certificate is removed. 41 virtual void OnCertRemoved(const X509Certificate* cert) {} 42 43 // Will be called when a certificate's trust is changed. 44 virtual void OnCertTrustChanged(const X509Certificate* cert) {} 45 46 protected: 47 Observer() {} 48 49 private: 50 DISALLOW_COPY_AND_ASSIGN(Observer); 51 }; 52 53 // Returns the CertDatabase singleton. 54 static CertDatabase* GetInstance(); 55 56 // Check whether this is a valid user cert that we have the private key for. 57 // Returns OK or a network error code such as ERR_CERT_CONTAINS_ERRORS. 58 int CheckUserCert(X509Certificate* cert); 59 60 // Store user (client) certificate. Assumes CheckUserCert has already passed. 61 // Returns OK, or ERR_ADD_USER_CERT_FAILED if there was a problem saving to 62 // the platform cert database, or possibly other network error codes. 63 int AddUserCert(X509Certificate* cert); 64 65 // Registers |observer| to receive notifications of certificate changes. The 66 // thread on which this is called is the thread on which |observer| will be 67 // called back with notifications. 68 void AddObserver(Observer* observer); 69 70 // Unregisters |observer| from receiving notifications. This must be called 71 // on the same thread on which AddObserver() was called. 72 void RemoveObserver(Observer* observer); 73 74 #if defined(OS_MACOSX) && !defined(OS_IOS) 75 // Configures the current message loop to observe and forward events from 76 // Keychain services. The MessageLoop must have an associated CFRunLoop, 77 // which means that this must be called from a MessageLoop of TYPE_UI. 78 void SetMessageLoopForKeychainEvents(); 79 #endif 80 81 private: 82 friend struct DefaultSingletonTraits<CertDatabase>; 83 84 CertDatabase(); 85 ~CertDatabase(); 86 87 // Broadcasts notifications to all registered observers. 88 void NotifyObserversOfCertAdded(const X509Certificate* cert); 89 void NotifyObserversOfCertRemoved(const X509Certificate* cert); 90 void NotifyObserversOfCertTrustChanged(const X509Certificate* cert); 91 92 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; 93 94 #if defined(USE_NSS) || (defined(OS_MACOSX) && !defined(OS_IOS)) 95 class Notifier; 96 friend class Notifier; 97 scoped_ptr<Notifier> notifier_; 98 #endif 99 100 DISALLOW_COPY_AND_ASSIGN(CertDatabase); 101 }; 102 103 } // namespace net 104 105 #endif // NET_CERT_CERT_DATABASE_H_ 106