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_BASE_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ | 5 #ifndef NET_BASE_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ |
6 #define NET_BASE_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ | 6 #define NET_BASE_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
18 #include "net/base/origin_bound_cert_store.h" | 18 #include "net/base/origin_bound_cert_store.h" |
19 | 19 |
20 class Task; | 20 class Task; |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 // This class is the system for storing and retrieving origin bound certs. | 24 // This class is the system for storing and retrieving server bound certs. |
25 // Modeled after the CookieMonster class, it has an in-memory cert store, | 25 // Modeled after the CookieMonster class, it has an in-memory cert store, |
26 // and synchronizes origin bound certs to an optional permanent storage that | 26 // and synchronizes server bound certs to an optional permanent storage that |
27 // implements the PersistentStore interface. The use case is described in | 27 // implements the PersistentStore interface. The use case is described in |
28 // http://balfanz.github.com/tls-obc-spec/draft-balfanz-tls-obc-00.html | 28 // http://balfanz.github.com/tls-obc-spec/draft-balfanz-tls-obc-00.html |
29 // | 29 // |
30 // This class can be accessed by multiple threads. For example, it can be used | 30 // This class can be accessed by multiple threads. For example, it can be used |
31 // by IO and origin bound cert management UI. | 31 // by IO and server bound cert management UI. |
32 class NET_EXPORT DefaultOriginBoundCertStore : public OriginBoundCertStore { | 32 class NET_EXPORT DefaultServerBoundCertStore : public ServerBoundCertStore { |
33 public: | 33 public: |
34 class PersistentStore; | 34 class PersistentStore; |
35 | 35 |
36 // The key for each OriginBoundCert* in OriginBoundCertMap is the | 36 // The key for each ServerBoundCert* in ServerBoundCertMap is the |
37 // corresponding origin. | 37 // corresponding server. |
38 typedef std::map<std::string, OriginBoundCert*> OriginBoundCertMap; | 38 typedef std::map<std::string, ServerBoundCert*> ServerBoundCertMap; |
39 | 39 |
40 // The store passed in should not have had Init() called on it yet. This | 40 // The store passed in should not have had Init() called on it yet. This |
41 // class will take care of initializing it. The backing store is NOT owned by | 41 // class will take care of initializing it. The backing store is NOT owned by |
42 // this class, but it must remain valid for the duration of the | 42 // this class, but it must remain valid for the duration of the |
43 // DefaultOriginBoundCertStore's existence. If |store| is NULL, then no | 43 // DefaultServerBoundCertStore's existence. If |store| is NULL, then no |
44 // backing store will be updated. | 44 // backing store will be updated. |
45 explicit DefaultOriginBoundCertStore(PersistentStore* store); | 45 explicit DefaultServerBoundCertStore(PersistentStore* store); |
46 | 46 |
47 virtual ~DefaultOriginBoundCertStore(); | 47 virtual ~DefaultServerBoundCertStore(); |
48 | 48 |
49 // Flush the backing store (if any) to disk and post the given task when done. | 49 // Flush the backing store (if any) to disk and post the given task when done. |
50 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE. | 50 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE. |
51 // It may be posted to the current thread, or it may run on the thread that | 51 // It may be posted to the current thread, or it may run on the thread that |
52 // actually does the flushing. Your Task should generally post a notification | 52 // actually does the flushing. Your Task should generally post a notification |
53 // to the thread you actually want to be notified on. | 53 // to the thread you actually want to be notified on. |
54 void FlushStore(const base::Closure& completion_task); | 54 void FlushStore(const base::Closure& completion_task); |
55 | 55 |
56 // OriginBoundCertStore implementation. | 56 // ServerBoundCertStore implementation. |
57 virtual bool GetOriginBoundCert( | 57 virtual bool GetServerBoundCert( |
58 const std::string& origin, | 58 const std::string& server, |
59 SSLClientCertType* type, | 59 SSLClientCertType* type, |
60 base::Time* creation_time, | 60 base::Time* creation_time, |
61 base::Time* expiration_time, | 61 base::Time* expiration_time, |
62 std::string* private_key_result, | 62 std::string* private_key_result, |
63 std::string* cert_result) OVERRIDE; | 63 std::string* cert_result) OVERRIDE; |
64 virtual void SetOriginBoundCert( | 64 virtual void SetServerBoundCert( |
65 const std::string& origin, | 65 const std::string& server, |
66 SSLClientCertType type, | 66 SSLClientCertType type, |
67 base::Time creation_time, | 67 base::Time creation_time, |
68 base::Time expiration_time, | 68 base::Time expiration_time, |
69 const std::string& private_key, | 69 const std::string& private_key, |
70 const std::string& cert) OVERRIDE; | 70 const std::string& cert) OVERRIDE; |
71 virtual void DeleteOriginBoundCert(const std::string& origin) OVERRIDE; | 71 virtual void DeleteServerBoundCert(const std::string& server) OVERRIDE; |
72 virtual void DeleteAllCreatedBetween(base::Time delete_begin, | 72 virtual void DeleteAllCreatedBetween(base::Time delete_begin, |
73 base::Time delete_end) OVERRIDE; | 73 base::Time delete_end) OVERRIDE; |
74 virtual void DeleteAll() OVERRIDE; | 74 virtual void DeleteAll() OVERRIDE; |
75 virtual void GetAllOriginBoundCerts( | 75 virtual void GetAllServerBoundCerts( |
76 std::vector<OriginBoundCert>* origin_bound_certs) OVERRIDE; | 76 std::vector<ServerBoundCert>* server_bound_certs) OVERRIDE; |
77 virtual int GetCertCount() OVERRIDE; | 77 virtual int GetCertCount() OVERRIDE; |
78 | 78 |
79 private: | 79 private: |
80 static const size_t kMaxCerts; | 80 static const size_t kMaxCerts; |
81 | 81 |
82 // Deletes all of the certs. Does not delete them from |store_|. | 82 // Deletes all of the certs. Does not delete them from |store_|. |
83 void DeleteAllInMemory(); | 83 void DeleteAllInMemory(); |
84 | 84 |
85 // Called by all non-static functions to ensure that the cert store has | 85 // Called by all non-static functions to ensure that the cert store has |
86 // been initialized. This is not done during creating so it doesn't block | 86 // been initialized. This is not done during creating so it doesn't block |
87 // the window showing. | 87 // the window showing. |
88 // Note: this method should always be called with lock_ held. | 88 // Note: this method should always be called with lock_ held. |
89 void InitIfNecessary() { | 89 void InitIfNecessary() { |
90 if (!initialized_) { | 90 if (!initialized_) { |
91 if (store_) | 91 if (store_) |
92 InitStore(); | 92 InitStore(); |
93 initialized_ = true; | 93 initialized_ = true; |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 // Initializes the backing store and reads existing certs from it. | 97 // Initializes the backing store and reads existing certs from it. |
98 // Should only be called by InitIfNecessary(). | 98 // Should only be called by InitIfNecessary(). |
99 void InitStore(); | 99 void InitStore(); |
100 | 100 |
101 // Deletes the cert for the specified origin, if such a cert exists, from the | 101 // Deletes the cert for the specified server, if such a cert exists, from the |
102 // in-memory store. Deletes it from |store_| if |store_| is not NULL. | 102 // in-memory store. Deletes it from |store_| if |store_| is not NULL. |
103 void InternalDeleteOriginBoundCert(const std::string& origin); | 103 void InternalDeleteServerBoundCert(const std::string& server); |
104 | 104 |
105 // Takes ownership of *cert. | 105 // Takes ownership of *cert. |
106 // Adds the cert for the specified origin to the in-memory store. Deletes it | 106 // Adds the cert for the specified server to the in-memory store. Deletes it |
107 // from |store_| if |store_| is not NULL. | 107 // from |store_| if |store_| is not NULL. |
108 void InternalInsertOriginBoundCert(const std::string& origin, | 108 void InternalInsertServerBoundCert(const std::string& server, |
109 OriginBoundCert* cert); | 109 ServerBoundCert* cert); |
110 | 110 |
111 // Indicates whether the cert store has been initialized. This happens | 111 // Indicates whether the cert store has been initialized. This happens |
112 // Lazily in InitStoreIfNecessary(). | 112 // Lazily in InitStoreIfNecessary(). |
113 bool initialized_; | 113 bool initialized_; |
114 | 114 |
115 scoped_refptr<PersistentStore> store_; | 115 scoped_refptr<PersistentStore> store_; |
116 | 116 |
117 OriginBoundCertMap origin_bound_certs_; | 117 ServerBoundCertMap server_bound_certs_; |
118 | 118 |
119 // Lock for thread-safety | 119 // Lock for thread-safety |
120 base::Lock lock_; | 120 base::Lock lock_; |
121 | 121 |
122 DISALLOW_COPY_AND_ASSIGN(DefaultOriginBoundCertStore); | 122 DISALLOW_COPY_AND_ASSIGN(DefaultServerBoundCertStore); |
123 }; | 123 }; |
124 | 124 |
125 typedef base::RefCountedThreadSafe<DefaultOriginBoundCertStore::PersistentStore> | 125 typedef base::RefCountedThreadSafe<DefaultServerBoundCertStore::PersistentStore> |
126 RefcountedPersistentStore; | 126 RefcountedPersistentStore; |
127 | 127 |
128 class NET_EXPORT DefaultOriginBoundCertStore::PersistentStore | 128 class NET_EXPORT DefaultServerBoundCertStore::PersistentStore |
129 : public RefcountedPersistentStore { | 129 : public RefcountedPersistentStore { |
130 public: | 130 public: |
131 virtual ~PersistentStore() {} | 131 virtual ~PersistentStore() {} |
132 | 132 |
133 // Initializes the store and retrieves the existing certs. This will be | 133 // Initializes the store and retrieves the existing certs. This will be |
134 // called only once at startup. Note that the certs are individually allocated | 134 // called only once at startup. Note that the certs are individually allocated |
135 // and that ownership is transferred to the caller upon return. | 135 // and that ownership is transferred to the caller upon return. |
136 virtual bool Load( | 136 virtual bool Load( |
137 std::vector<OriginBoundCert*>* certs) = 0; | 137 std::vector<ServerBoundCert*>* certs) = 0; |
138 | 138 |
139 virtual void AddOriginBoundCert(const OriginBoundCert& cert) = 0; | 139 virtual void AddServerBoundCert(const ServerBoundCert& cert) = 0; |
140 | 140 |
141 virtual void DeleteOriginBoundCert(const OriginBoundCert& cert) = 0; | 141 virtual void DeleteServerBoundCert(const ServerBoundCert& cert) = 0; |
142 | 142 |
143 // Sets the value of the user preference whether the persistent storage | 143 // Sets the value of the user preference whether the persistent storage |
144 // must be deleted upon destruction. | 144 // must be deleted upon destruction. |
145 virtual void SetClearLocalStateOnExit(bool clear_local_state) = 0; | 145 virtual void SetClearLocalStateOnExit(bool clear_local_state) = 0; |
146 | 146 |
147 // Flush the store and post the given Task when complete. | 147 // Flush the store and post the given Task when complete. |
148 virtual void Flush(const base::Closure& completion_task) = 0; | 148 virtual void Flush(const base::Closure& completion_task) = 0; |
149 | 149 |
150 protected: | 150 protected: |
151 PersistentStore(); | 151 PersistentStore(); |
152 | 152 |
153 private: | 153 private: |
154 DISALLOW_COPY_AND_ASSIGN(PersistentStore); | 154 DISALLOW_COPY_AND_ASSIGN(PersistentStore); |
155 }; | 155 }; |
156 | 156 |
157 } // namespace net | 157 } // namespace net |
158 | 158 |
159 #endif // NET_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ | 159 #endif // NET_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ |
OLD | NEW |