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