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 // Flush the backing store (if any) to disk and post the given task when done. | |
46 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE. | |
47 // It may be posted to the current thread, or it may run on the thread that | |
48 // actually does the flushing. Your Task should generally post a notification | |
49 // to the thread you actually want to be notified on. | |
50 void FlushStore(const base::Closure& completion_task); | |
51 | |
52 // ServerBoundCertStore implementation. | |
53 virtual bool GetServerBoundCert( | |
54 const std::string& server_identifier, | |
55 SSLClientCertType* type, | |
56 base::Time* expiration_time, | |
57 std::string* private_key_result, | |
58 std::string* cert_result, | |
59 const GetCertCallback& callback) OVERRIDE; | |
60 virtual void SetServerBoundCert( | |
61 const std::string& server_identifier, | |
62 SSLClientCertType type, | |
63 base::Time creation_time, | |
64 base::Time expiration_time, | |
65 const std::string& private_key, | |
66 const std::string& cert) OVERRIDE; | |
67 virtual void DeleteServerBoundCert( | |
68 const std::string& server_identifier, | |
69 const base::Closure& callback) OVERRIDE; | |
70 virtual void DeleteAllCreatedBetween( | |
71 base::Time delete_begin, | |
72 base::Time delete_end, | |
73 const base::Closure& callback) OVERRIDE; | |
74 virtual void DeleteAll(const base::Closure& callback) OVERRIDE; | |
75 virtual void GetAllServerBoundCerts( | |
76 const GetCertListCallback& callback) OVERRIDE; | |
77 virtual int GetCertCount() OVERRIDE; | |
78 virtual void SetForceKeepSessionState() OVERRIDE; | |
79 | |
80 private: | |
81 class Task; | |
82 class GetServerBoundCertTask; | |
83 class SetServerBoundCertTask; | |
84 class DeleteServerBoundCertTask; | |
85 class DeleteAllCreatedBetweenTask; | |
86 class GetAllServerBoundCertsTask; | |
87 | |
88 static const size_t kMaxCerts; | |
89 | |
90 // Deletes all of the certs. Does not delete them from |store_|. | |
91 void DeleteAllInMemory(); | |
92 | |
93 // Called by all non-static functions to ensure that the cert store has | |
94 // been initialized. | |
95 // TODO(mattm): since we load asynchronously now, maybe we should start | |
96 // loading immediately on construction, or provide some method to initiate | |
97 // loading? | |
98 void InitIfNecessary() { | |
99 if (!initialized_) { | |
100 if (store_) { | |
101 InitStore(); | |
102 } else { | |
103 loaded_ = true; | |
104 } | |
105 initialized_ = true; | |
106 } | |
107 } | |
108 | |
109 // Initializes the backing store and reads existing certs from it. | |
110 // Should only be called by InitIfNecessary(). | |
111 void InitStore(); | |
112 | |
113 // Callback for backing store loading completion. | |
114 void OnLoaded(scoped_ptr<ScopedVector<ServerBoundCert> > certs); | |
115 | |
116 // Syncronous methods which do the actual work. Can only be called after | |
117 // initialization is complete. | |
118 void SyncSetServerBoundCert( | |
119 const std::string& server_identifier, | |
120 SSLClientCertType type, | |
121 base::Time creation_time, | |
122 base::Time expiration_time, | |
123 const std::string& private_key, | |
124 const std::string& cert); | |
125 void SyncDeleteServerBoundCert(const std::string& server_identifier); | |
126 void SyncDeleteAllCreatedBetween(base::Time delete_begin, | |
127 base::Time delete_end); | |
128 void SyncGetAllServerBoundCerts(ServerBoundCertList* cert_list); | |
129 | |
130 // Add |task| to |waiting_tasks_|. | |
131 void EnqueueTask(scoped_ptr<Task> task); | |
132 // If already initialized, run |task| immediately. Otherwise add it to | |
133 // |waiting_tasks_|. | |
134 void RunOrEnqueueTask(scoped_ptr<Task> task); | |
135 | |
136 // Deletes the cert for the specified server, if such a cert exists, from the | |
137 // in-memory store. Deletes it from |store_| if |store_| is not NULL. | |
138 void InternalDeleteServerBoundCert(const std::string& server); | |
139 | |
140 // Takes ownership of *cert. | |
141 // Adds the cert for the specified server to the in-memory store. Deletes it | |
142 // from |store_| if |store_| is not NULL. | |
143 void InternalInsertServerBoundCert(const std::string& server_identifier, | |
144 ServerBoundCert* cert); | |
145 | |
146 // Indicates whether the cert store has been initialized. This happens | |
147 // lazily in InitIfNecessary(). | |
148 bool initialized_; | |
149 | |
150 // Indicates whether loading from the backend store is completed and | |
151 // calls may be immediately processed. | |
152 bool loaded_; | |
153 | |
154 // Tasks that are waiting to be run once we finish loading. | |
155 ScopedVector<Task> waiting_tasks_; | |
156 base::TimeTicks waiting_tasks_start_time_; | |
157 | |
158 scoped_refptr<PersistentStore> store_; | |
159 | |
160 ServerBoundCertMap server_bound_certs_; | |
161 | |
162 base::WeakPtrFactory<DefaultServerBoundCertStore> weak_ptr_factory_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(DefaultServerBoundCertStore); | |
165 }; | |
166 | |
167 typedef base::RefCountedThreadSafe<DefaultServerBoundCertStore::PersistentStore> | |
168 RefcountedPersistentStore; | |
169 | |
170 class NET_EXPORT DefaultServerBoundCertStore::PersistentStore | |
171 : public RefcountedPersistentStore { | |
172 public: | |
173 typedef base::Callback<void(scoped_ptr<ScopedVector<ServerBoundCert> >)> | |
174 LoadedCallback; | |
175 | |
176 // Initializes the store and retrieves the existing certs. This will be | |
177 // called only once at startup. Note that the certs are individually allocated | |
178 // and that ownership is transferred to the caller upon return. | |
179 // The |loaded_callback| must not be called synchronously. | |
180 virtual void Load(const LoadedCallback& loaded_callback) = 0; | |
181 | |
182 virtual void AddServerBoundCert(const ServerBoundCert& cert) = 0; | |
183 | |
184 virtual void DeleteServerBoundCert(const ServerBoundCert& cert) = 0; | |
185 | |
186 // When invoked, instructs the store to keep session related data on | |
187 // destruction. | |
188 virtual void SetForceKeepSessionState() = 0; | |
189 | |
190 // Flush the store and post the given Task when complete. | |
191 virtual void Flush(const base::Closure& completion_task) = 0; | |
192 | |
193 protected: | |
194 friend class base::RefCountedThreadSafe<PersistentStore>; | |
195 | |
196 PersistentStore(); | |
197 virtual ~PersistentStore(); | |
198 | |
199 private: | |
200 DISALLOW_COPY_AND_ASSIGN(PersistentStore); | |
201 }; | |
202 | |
203 } // namespace net | |
204 | |
205 #endif // NET_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ | |
OLD | NEW |