| OLD | NEW |
| (Empty) |
| 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_BASE_DEFAULT_SERVER_BOUND_CERT_STORE_H_ | |
| 6 #define NET_BASE_DEFAULT_SERVER_BOUND_CERT_STORE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/scoped_vector.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "net/base/net_export.h" | |
| 19 #include "net/base/server_bound_cert_store.h" | |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 // This class is the system for storing and retrieving server bound certs. | |
| 24 // Modeled after the CookieMonster class, it has an in-memory cert store, | |
| 25 // and synchronizes server bound certs to an optional permanent storage that | |
| 26 // implements the PersistentStore interface. The use case is described in | |
| 27 // http://balfanz.github.com/tls-obc-spec/draft-balfanz-tls-obc-00.html | |
| 28 class NET_EXPORT DefaultServerBoundCertStore : public ServerBoundCertStore { | |
| 29 public: | |
| 30 class PersistentStore; | |
| 31 | |
| 32 // The key for each ServerBoundCert* in ServerBoundCertMap is the | |
| 33 // corresponding server. | |
| 34 typedef std::map<std::string, ServerBoundCert*> ServerBoundCertMap; | |
| 35 | |
| 36 // The store passed in should not have had Init() called on it yet. This | |
| 37 // class will take care of initializing it. The backing store is NOT owned by | |
| 38 // this class, but it must remain valid for the duration of the | |
| 39 // DefaultServerBoundCertStore's existence. If |store| is NULL, then no | |
| 40 // backing store will be updated. | |
| 41 explicit DefaultServerBoundCertStore(PersistentStore* store); | |
| 42 | |
| 43 virtual ~DefaultServerBoundCertStore(); | |
| 44 | |
| 45 // ServerBoundCertStore implementation. | |
| 46 virtual bool GetServerBoundCert( | |
| 47 const std::string& server_identifier, | |
| 48 SSLClientCertType* type, | |
| 49 base::Time* expiration_time, | |
| 50 std::string* private_key_result, | |
| 51 std::string* cert_result, | |
| 52 const GetCertCallback& callback) OVERRIDE; | |
| 53 virtual void SetServerBoundCert( | |
| 54 const std::string& server_identifier, | |
| 55 SSLClientCertType type, | |
| 56 base::Time creation_time, | |
| 57 base::Time expiration_time, | |
| 58 const std::string& private_key, | |
| 59 const std::string& cert) OVERRIDE; | |
| 60 virtual void DeleteServerBoundCert( | |
| 61 const std::string& server_identifier, | |
| 62 const base::Closure& callback) OVERRIDE; | |
| 63 virtual void DeleteAllCreatedBetween( | |
| 64 base::Time delete_begin, | |
| 65 base::Time delete_end, | |
| 66 const base::Closure& callback) OVERRIDE; | |
| 67 virtual void DeleteAll(const base::Closure& callback) OVERRIDE; | |
| 68 virtual void GetAllServerBoundCerts( | |
| 69 const GetCertListCallback& callback) OVERRIDE; | |
| 70 virtual int GetCertCount() OVERRIDE; | |
| 71 virtual void SetForceKeepSessionState() OVERRIDE; | |
| 72 | |
| 73 private: | |
| 74 class Task; | |
| 75 class GetServerBoundCertTask; | |
| 76 class SetServerBoundCertTask; | |
| 77 class DeleteServerBoundCertTask; | |
| 78 class DeleteAllCreatedBetweenTask; | |
| 79 class GetAllServerBoundCertsTask; | |
| 80 | |
| 81 static const size_t kMaxCerts; | |
| 82 | |
| 83 // Deletes all of the certs. Does not delete them from |store_|. | |
| 84 void DeleteAllInMemory(); | |
| 85 | |
| 86 // Called by all non-static functions to ensure that the cert store has | |
| 87 // been initialized. | |
| 88 // TODO(mattm): since we load asynchronously now, maybe we should start | |
| 89 // loading immediately on construction, or provide some method to initiate | |
| 90 // loading? | |
| 91 void InitIfNecessary() { | |
| 92 if (!initialized_) { | |
| 93 if (store_) { | |
| 94 InitStore(); | |
| 95 } else { | |
| 96 loaded_ = true; | |
| 97 } | |
| 98 initialized_ = true; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Initializes the backing store and reads existing certs from it. | |
| 103 // Should only be called by InitIfNecessary(). | |
| 104 void InitStore(); | |
| 105 | |
| 106 // Callback for backing store loading completion. | |
| 107 void OnLoaded(scoped_ptr<ScopedVector<ServerBoundCert> > certs); | |
| 108 | |
| 109 // Syncronous methods which do the actual work. Can only be called after | |
| 110 // initialization is complete. | |
| 111 void SyncSetServerBoundCert( | |
| 112 const std::string& server_identifier, | |
| 113 SSLClientCertType type, | |
| 114 base::Time creation_time, | |
| 115 base::Time expiration_time, | |
| 116 const std::string& private_key, | |
| 117 const std::string& cert); | |
| 118 void SyncDeleteServerBoundCert(const std::string& server_identifier); | |
| 119 void SyncDeleteAllCreatedBetween(base::Time delete_begin, | |
| 120 base::Time delete_end); | |
| 121 void SyncGetAllServerBoundCerts(ServerBoundCertList* cert_list); | |
| 122 | |
| 123 // Add |task| to |waiting_tasks_|. | |
| 124 void EnqueueTask(scoped_ptr<Task> task); | |
| 125 // If already initialized, run |task| immediately. Otherwise add it to | |
| 126 // |waiting_tasks_|. | |
| 127 void RunOrEnqueueTask(scoped_ptr<Task> task); | |
| 128 | |
| 129 // Deletes the cert for the specified server, if such a cert exists, from the | |
| 130 // in-memory store. Deletes it from |store_| if |store_| is not NULL. | |
| 131 void InternalDeleteServerBoundCert(const std::string& server); | |
| 132 | |
| 133 // Takes ownership of *cert. | |
| 134 // Adds the cert for the specified server to the in-memory store. Deletes it | |
| 135 // from |store_| if |store_| is not NULL. | |
| 136 void InternalInsertServerBoundCert(const std::string& server_identifier, | |
| 137 ServerBoundCert* cert); | |
| 138 | |
| 139 // Indicates whether the cert store has been initialized. This happens | |
| 140 // lazily in InitIfNecessary(). | |
| 141 bool initialized_; | |
| 142 | |
| 143 // Indicates whether loading from the backend store is completed and | |
| 144 // calls may be immediately processed. | |
| 145 bool loaded_; | |
| 146 | |
| 147 // Tasks that are waiting to be run once we finish loading. | |
| 148 ScopedVector<Task> waiting_tasks_; | |
| 149 base::TimeTicks waiting_tasks_start_time_; | |
| 150 | |
| 151 scoped_refptr<PersistentStore> store_; | |
| 152 | |
| 153 ServerBoundCertMap server_bound_certs_; | |
| 154 | |
| 155 base::WeakPtrFactory<DefaultServerBoundCertStore> weak_ptr_factory_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(DefaultServerBoundCertStore); | |
| 158 }; | |
| 159 | |
| 160 typedef base::RefCountedThreadSafe<DefaultServerBoundCertStore::PersistentStore> | |
| 161 RefcountedPersistentStore; | |
| 162 | |
| 163 class NET_EXPORT DefaultServerBoundCertStore::PersistentStore | |
| 164 : public RefcountedPersistentStore { | |
| 165 public: | |
| 166 typedef base::Callback<void(scoped_ptr<ScopedVector<ServerBoundCert> >)> | |
| 167 LoadedCallback; | |
| 168 | |
| 169 // Initializes the store and retrieves the existing certs. This will be | |
| 170 // called only once at startup. Note that the certs are individually allocated | |
| 171 // and that ownership is transferred to the caller upon return. | |
| 172 // The |loaded_callback| must not be called synchronously. | |
| 173 virtual void Load(const LoadedCallback& loaded_callback) = 0; | |
| 174 | |
| 175 virtual void AddServerBoundCert(const ServerBoundCert& cert) = 0; | |
| 176 | |
| 177 virtual void DeleteServerBoundCert(const ServerBoundCert& cert) = 0; | |
| 178 | |
| 179 // When invoked, instructs the store to keep session related data on | |
| 180 // destruction. | |
| 181 virtual void SetForceKeepSessionState() = 0; | |
| 182 | |
| 183 protected: | |
| 184 friend class base::RefCountedThreadSafe<PersistentStore>; | |
| 185 | |
| 186 PersistentStore(); | |
| 187 virtual ~PersistentStore(); | |
| 188 | |
| 189 private: | |
| 190 DISALLOW_COPY_AND_ASSIGN(PersistentStore); | |
| 191 }; | |
| 192 | |
| 193 } // namespace net | |
| 194 | |
| 195 #endif // NET_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ | |
| OLD | NEW |