| 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 #include "net/ssl/server_bound_cert_service.h" | 5 #include "net/ssl/channel_id_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 const int kValidityPeriodInDays = 365; | 38 const int kValidityPeriodInDays = 365; |
| 39 // When we check the system time, we add this many days to the end of the check | 39 // When we check the system time, we add this many days to the end of the check |
| 40 // so the result will still hold even after chrome has been running for a | 40 // so the result will still hold even after chrome has been running for a |
| 41 // while. | 41 // while. |
| 42 const int kSystemTimeValidityBufferInDays = 90; | 42 const int kSystemTimeValidityBufferInDays = 90; |
| 43 | 43 |
| 44 // Used by the GetDomainBoundCertResult histogram to record the final | 44 // Used by the GetDomainBoundCertResult histogram to record the final |
| 45 // outcome of each GetDomainBoundCert or GetOrCreateDomainBoundCert call. | 45 // outcome of each GetChannelID or GetOrCreateChannelID call. |
| 46 // Do not re-use values. | 46 // Do not re-use values. |
| 47 enum GetCertResult { | 47 enum GetChannelIDResult { |
| 48 // Synchronously found and returned an existing domain bound cert. | 48 // Synchronously found and returned an existing domain bound cert. |
| 49 SYNC_SUCCESS = 0, | 49 SYNC_SUCCESS = 0, |
| 50 // Retrieved or generated and returned a domain bound cert asynchronously. | 50 // Retrieved or generated and returned a domain bound cert asynchronously. |
| 51 ASYNC_SUCCESS = 1, | 51 ASYNC_SUCCESS = 1, |
| 52 // Retrieval/generation request was cancelled before the cert generation | 52 // Retrieval/generation request was cancelled before the cert generation |
| 53 // completed. | 53 // completed. |
| 54 ASYNC_CANCELLED = 2, | 54 ASYNC_CANCELLED = 2, |
| 55 // Cert generation failed. | 55 // Cert generation failed. |
| 56 ASYNC_FAILURE_KEYGEN = 3, | 56 ASYNC_FAILURE_KEYGEN = 3, |
| 57 ASYNC_FAILURE_CREATE_CERT = 4, | 57 ASYNC_FAILURE_CREATE_CERT = 4, |
| 58 ASYNC_FAILURE_EXPORT_KEY = 5, | 58 ASYNC_FAILURE_EXPORT_KEY = 5, |
| 59 ASYNC_FAILURE_UNKNOWN = 6, | 59 ASYNC_FAILURE_UNKNOWN = 6, |
| 60 // GetDomainBoundCert or GetOrCreateDomainBoundCert was called with | 60 // GetChannelID or GetOrCreateDomainBoundCert was called with |
| 61 // invalid arguments. | 61 // invalid arguments. |
| 62 INVALID_ARGUMENT = 7, | 62 INVALID_ARGUMENT = 7, |
| 63 // We don't support any of the cert types the server requested. | 63 // We don't support any of the cert types the server requested. |
| 64 UNSUPPORTED_TYPE = 8, | 64 UNSUPPORTED_TYPE = 8, |
| 65 // Server asked for a different type of certs while we were generating one. | 65 // Server asked for a different type of certs while we were generating one. |
| 66 TYPE_MISMATCH = 9, | 66 TYPE_MISMATCH = 9, |
| 67 // Couldn't start a worker to generate a cert. | 67 // Couldn't start a worker to generate a cert. |
| 68 WORKER_FAILURE = 10, | 68 WORKER_FAILURE = 10, |
| 69 GET_CERT_RESULT_MAX | 69 GET_CHANNEL_ID_RESULT_MAX |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 void RecordGetDomainBoundCertResult(GetCertResult result) { | 72 void RecordGetChannelIDResult(GetChannelIDResult result) { |
| 73 UMA_HISTOGRAM_ENUMERATION("DomainBoundCerts.GetDomainBoundCertResult", result, | 73 UMA_HISTOGRAM_ENUMERATION("DomainBoundCerts.GetDomainBoundCertResult", result, |
| 74 GET_CERT_RESULT_MAX); | 74 GET_CHANNEL_ID_RESULT_MAX); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void RecordGetCertTime(base::TimeDelta request_time) { | 77 void RecordGetChannelIDTime(base::TimeDelta request_time) { |
| 78 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GetCertTime", | 78 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GetCertTime", |
| 79 request_time, | 79 request_time, |
| 80 base::TimeDelta::FromMilliseconds(1), | 80 base::TimeDelta::FromMilliseconds(1), |
| 81 base::TimeDelta::FromMinutes(5), | 81 base::TimeDelta::FromMinutes(5), |
| 82 50); | 82 50); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // On success, returns a ServerBoundCert object and sets |*error| to OK. | 85 // On success, returns a ChannelID object and sets |*error| to OK. |
| 86 // Otherwise, returns NULL, and |*error| will be set to a net error code. | 86 // Otherwise, returns NULL, and |*error| will be set to a net error code. |
| 87 // |serial_number| is passed in because base::RandInt cannot be called from an | 87 // |serial_number| is passed in because base::RandInt cannot be called from an |
| 88 // unjoined thread, due to relying on a non-leaked LazyInstance | 88 // unjoined thread, due to relying on a non-leaked LazyInstance |
| 89 scoped_ptr<ServerBoundCertStore::ServerBoundCert> GenerateCert( | 89 scoped_ptr<ChannelIDStore::ChannelID> GenerateChannelID( |
| 90 const std::string& server_identifier, | 90 const std::string& server_identifier, |
| 91 uint32 serial_number, | 91 uint32 serial_number, |
| 92 int* error) { | 92 int* error) { |
| 93 scoped_ptr<ServerBoundCertStore::ServerBoundCert> result; | 93 scoped_ptr<ChannelIDStore::ChannelID> result; |
| 94 | 94 |
| 95 base::TimeTicks start = base::TimeTicks::Now(); | 95 base::TimeTicks start = base::TimeTicks::Now(); |
| 96 base::Time not_valid_before = base::Time::Now(); | 96 base::Time not_valid_before = base::Time::Now(); |
| 97 base::Time not_valid_after = | 97 base::Time not_valid_after = |
| 98 not_valid_before + base::TimeDelta::FromDays(kValidityPeriodInDays); | 98 not_valid_before + base::TimeDelta::FromDays(kValidityPeriodInDays); |
| 99 std::string der_cert; | 99 std::string der_cert; |
| 100 std::vector<uint8> private_key_info; | 100 std::vector<uint8> private_key_info; |
| 101 scoped_ptr<crypto::ECPrivateKey> key; | 101 scoped_ptr<crypto::ECPrivateKey> key; |
| 102 if (!x509_util::CreateKeyAndDomainBoundCertEC(server_identifier, | 102 if (!x509_util::CreateKeyAndChannelIDEC(server_identifier, |
| 103 serial_number, | 103 serial_number, |
| 104 not_valid_before, | 104 not_valid_before, |
| 105 not_valid_after, | 105 not_valid_after, |
| 106 &key, | 106 &key, |
| 107 &der_cert)) { | 107 &der_cert)) { |
| 108 DLOG(ERROR) << "Unable to create x509 cert for client"; | 108 DLOG(ERROR) << "Unable to create x509 cert for client"; |
| 109 *error = ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED; | 109 *error = ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED; |
| 110 return result.Pass(); | 110 return result.Pass(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 if (!key->ExportEncryptedPrivateKey(ServerBoundCertService::kEPKIPassword, | 113 if (!key->ExportEncryptedPrivateKey(ChannelIDService::kEPKIPassword, |
| 114 1, &private_key_info)) { | 114 1, &private_key_info)) { |
| 115 DLOG(ERROR) << "Unable to export private key"; | 115 DLOG(ERROR) << "Unable to export private key"; |
| 116 *error = ERR_PRIVATE_KEY_EXPORT_FAILED; | 116 *error = ERR_PRIVATE_KEY_EXPORT_FAILED; |
| 117 return result.Pass(); | 117 return result.Pass(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // TODO(rkn): Perhaps ExportPrivateKey should be changed to output a | 120 // TODO(rkn): Perhaps ExportPrivateKey should be changed to output a |
| 121 // std::string* to prevent this copying. | 121 // std::string* to prevent this copying. |
| 122 std::string key_out(private_key_info.begin(), private_key_info.end()); | 122 std::string key_out(private_key_info.begin(), private_key_info.end()); |
| 123 | 123 |
| 124 result.reset(new ServerBoundCertStore::ServerBoundCert( | 124 result.reset(new ChannelIDStore::ChannelID( |
| 125 server_identifier, | 125 server_identifier, |
| 126 not_valid_before, | 126 not_valid_before, |
| 127 not_valid_after, | 127 not_valid_after, |
| 128 key_out, | 128 key_out, |
| 129 der_cert)); | 129 der_cert)); |
| 130 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GenerateCertTime", | 130 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GenerateCertTime", |
| 131 base::TimeTicks::Now() - start, | 131 base::TimeTicks::Now() - start, |
| 132 base::TimeDelta::FromMilliseconds(1), | 132 base::TimeDelta::FromMilliseconds(1), |
| 133 base::TimeDelta::FromMinutes(5), | 133 base::TimeDelta::FromMinutes(5), |
| 134 50); | 134 50); |
| 135 *error = OK; | 135 *error = OK; |
| 136 return result.Pass(); | 136 return result.Pass(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 } // namespace | 139 } // namespace |
| 140 | 140 |
| 141 // Represents the output and result callback of a request. | 141 // Represents the output and result callback of a request. |
| 142 class ServerBoundCertServiceRequest { | 142 class ChannelIDServiceRequest { |
| 143 public: | 143 public: |
| 144 ServerBoundCertServiceRequest(base::TimeTicks request_start, | 144 ChannelIDServiceRequest(base::TimeTicks request_start, |
| 145 const CompletionCallback& callback, | 145 const CompletionCallback& callback, |
| 146 std::string* private_key, | 146 std::string* private_key, |
| 147 std::string* cert) | 147 std::string* cert) |
| 148 : request_start_(request_start), | 148 : request_start_(request_start), |
| 149 callback_(callback), | 149 callback_(callback), |
| 150 private_key_(private_key), | 150 private_key_(private_key), |
| 151 cert_(cert) { | 151 cert_(cert) { |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Ensures that the result callback will never be made. | 154 // Ensures that the result callback will never be made. |
| 155 void Cancel() { | 155 void Cancel() { |
| 156 RecordGetDomainBoundCertResult(ASYNC_CANCELLED); | 156 RecordGetChannelIDResult(ASYNC_CANCELLED); |
| 157 callback_.Reset(); | 157 callback_.Reset(); |
| 158 private_key_ = NULL; | 158 private_key_ = NULL; |
| 159 cert_ = NULL; | 159 cert_ = NULL; |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Copies the contents of |private_key| and |cert| to the caller's output | 162 // Copies the contents of |private_key| and |cert| to the caller's output |
| 163 // arguments and calls the callback. | 163 // arguments and calls the callback. |
| 164 void Post(int error, | 164 void Post(int error, |
| 165 const std::string& private_key, | 165 const std::string& private_key, |
| 166 const std::string& cert) { | 166 const std::string& cert) { |
| 167 switch (error) { | 167 switch (error) { |
| 168 case OK: { | 168 case OK: { |
| 169 base::TimeDelta request_time = base::TimeTicks::Now() - request_start_; | 169 base::TimeDelta request_time = base::TimeTicks::Now() - request_start_; |
| 170 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GetCertTimeAsync", | 170 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GetCertTimeAsync", |
| 171 request_time, | 171 request_time, |
| 172 base::TimeDelta::FromMilliseconds(1), | 172 base::TimeDelta::FromMilliseconds(1), |
| 173 base::TimeDelta::FromMinutes(5), | 173 base::TimeDelta::FromMinutes(5), |
| 174 50); | 174 50); |
| 175 RecordGetCertTime(request_time); | 175 RecordGetChannelIDTime(request_time); |
| 176 RecordGetDomainBoundCertResult(ASYNC_SUCCESS); | 176 RecordGetChannelIDResult(ASYNC_SUCCESS); |
| 177 break; | 177 break; |
| 178 } | 178 } |
| 179 case ERR_KEY_GENERATION_FAILED: | 179 case ERR_KEY_GENERATION_FAILED: |
| 180 RecordGetDomainBoundCertResult(ASYNC_FAILURE_KEYGEN); | 180 RecordGetChannelIDResult(ASYNC_FAILURE_KEYGEN); |
| 181 break; | 181 break; |
| 182 case ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED: | 182 case ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED: |
| 183 RecordGetDomainBoundCertResult(ASYNC_FAILURE_CREATE_CERT); | 183 RecordGetChannelIDResult(ASYNC_FAILURE_CREATE_CERT); |
| 184 break; | 184 break; |
| 185 case ERR_PRIVATE_KEY_EXPORT_FAILED: | 185 case ERR_PRIVATE_KEY_EXPORT_FAILED: |
| 186 RecordGetDomainBoundCertResult(ASYNC_FAILURE_EXPORT_KEY); | 186 RecordGetChannelIDResult(ASYNC_FAILURE_EXPORT_KEY); |
| 187 break; | 187 break; |
| 188 case ERR_INSUFFICIENT_RESOURCES: | 188 case ERR_INSUFFICIENT_RESOURCES: |
| 189 RecordGetDomainBoundCertResult(WORKER_FAILURE); | 189 RecordGetChannelIDResult(WORKER_FAILURE); |
| 190 break; | 190 break; |
| 191 default: | 191 default: |
| 192 RecordGetDomainBoundCertResult(ASYNC_FAILURE_UNKNOWN); | 192 RecordGetChannelIDResult(ASYNC_FAILURE_UNKNOWN); |
| 193 break; | 193 break; |
| 194 } | 194 } |
| 195 if (!callback_.is_null()) { | 195 if (!callback_.is_null()) { |
| 196 *private_key_ = private_key; | 196 *private_key_ = private_key; |
| 197 *cert_ = cert; | 197 *cert_ = cert; |
| 198 callback_.Run(error); | 198 callback_.Run(error); |
| 199 } | 199 } |
| 200 delete this; | 200 delete this; |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool canceled() const { return callback_.is_null(); } | 203 bool canceled() const { return callback_.is_null(); } |
| 204 | 204 |
| 205 private: | 205 private: |
| 206 base::TimeTicks request_start_; | 206 base::TimeTicks request_start_; |
| 207 CompletionCallback callback_; | 207 CompletionCallback callback_; |
| 208 std::string* private_key_; | 208 std::string* private_key_; |
| 209 std::string* cert_; | 209 std::string* cert_; |
| 210 }; | 210 }; |
| 211 | 211 |
| 212 // ServerBoundCertServiceWorker runs on a worker thread and takes care of the | 212 // ChannelIDServiceWorker runs on a worker thread and takes care of the |
| 213 // blocking process of performing key generation. Will take care of deleting | 213 // blocking process of performing key generation. Will take care of deleting |
| 214 // itself once Start() is called. | 214 // itself once Start() is called. |
| 215 class ServerBoundCertServiceWorker { | 215 class ChannelIDServiceWorker { |
| 216 public: | 216 public: |
| 217 typedef base::Callback<void( | 217 typedef base::Callback<void( |
| 218 const std::string&, | 218 const std::string&, |
| 219 int, | 219 int, |
| 220 scoped_ptr<ServerBoundCertStore::ServerBoundCert>)> WorkerDoneCallback; | 220 scoped_ptr<ChannelIDStore::ChannelID>)> WorkerDoneCallback; |
| 221 | 221 |
| 222 ServerBoundCertServiceWorker( | 222 ChannelIDServiceWorker( |
| 223 const std::string& server_identifier, | 223 const std::string& server_identifier, |
| 224 const WorkerDoneCallback& callback) | 224 const WorkerDoneCallback& callback) |
| 225 : server_identifier_(server_identifier), | 225 : server_identifier_(server_identifier), |
| 226 serial_number_(base::RandInt(0, std::numeric_limits<int>::max())), | 226 serial_number_(base::RandInt(0, std::numeric_limits<int>::max())), |
| 227 origin_loop_(base::MessageLoopProxy::current()), | 227 origin_loop_(base::MessageLoopProxy::current()), |
| 228 callback_(callback) { | 228 callback_(callback) { |
| 229 } | 229 } |
| 230 | 230 |
| 231 // Starts the worker on |task_runner|. If the worker fails to start, such as | 231 // Starts the worker on |task_runner|. If the worker fails to start, such as |
| 232 // if the task runner is shutting down, then it will take care of deleting | 232 // if the task runner is shutting down, then it will take care of deleting |
| 233 // itself. | 233 // itself. |
| 234 bool Start(const scoped_refptr<base::TaskRunner>& task_runner) { | 234 bool Start(const scoped_refptr<base::TaskRunner>& task_runner) { |
| 235 DCHECK(origin_loop_->RunsTasksOnCurrentThread()); | 235 DCHECK(origin_loop_->RunsTasksOnCurrentThread()); |
| 236 | 236 |
| 237 return task_runner->PostTask( | 237 return task_runner->PostTask( |
| 238 FROM_HERE, | 238 FROM_HERE, |
| 239 base::Bind(&ServerBoundCertServiceWorker::Run, base::Owned(this))); | 239 base::Bind(&ChannelIDServiceWorker::Run, base::Owned(this))); |
| 240 } | 240 } |
| 241 | 241 |
| 242 private: | 242 private: |
| 243 void Run() { | 243 void Run() { |
| 244 // Runs on a worker thread. | 244 // Runs on a worker thread. |
| 245 int error = ERR_FAILED; | 245 int error = ERR_FAILED; |
| 246 scoped_ptr<ServerBoundCertStore::ServerBoundCert> cert = | 246 scoped_ptr<ChannelIDStore::ChannelID> cert = |
| 247 GenerateCert(server_identifier_, serial_number_, &error); | 247 GenerateChannelID(server_identifier_, serial_number_, &error); |
| 248 DVLOG(1) << "GenerateCert " << server_identifier_ << " returned " << error; | 248 DVLOG(1) << "GenerateCert " << server_identifier_ << " returned " << error; |
| 249 #if defined(USE_NSS) | 249 #if defined(USE_NSS) |
| 250 // Detach the thread from NSPR. | 250 // Detach the thread from NSPR. |
| 251 // Calling NSS functions attaches the thread to NSPR, which stores | 251 // Calling NSS functions attaches the thread to NSPR, which stores |
| 252 // the NSPR thread ID in thread-specific data. | 252 // the NSPR thread ID in thread-specific data. |
| 253 // The threads in our thread pool terminate after we have called | 253 // The threads in our thread pool terminate after we have called |
| 254 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 254 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 255 // segfaults on shutdown when the threads' thread-specific data | 255 // segfaults on shutdown when the threads' thread-specific data |
| 256 // destructors run. | 256 // destructors run. |
| 257 PR_DetachThread(); | 257 PR_DetachThread(); |
| 258 #endif | 258 #endif |
| 259 origin_loop_->PostTask(FROM_HERE, | 259 origin_loop_->PostTask(FROM_HERE, |
| 260 base::Bind(callback_, server_identifier_, error, | 260 base::Bind(callback_, server_identifier_, error, |
| 261 base::Passed(&cert))); | 261 base::Passed(&cert))); |
| 262 } | 262 } |
| 263 | 263 |
| 264 const std::string server_identifier_; | 264 const std::string server_identifier_; |
| 265 // Note that serial_number_ must be initialized on a non-worker thread | 265 // Note that serial_number_ must be initialized on a non-worker thread |
| 266 // (see documentation for GenerateCert). | 266 // (see documentation for GenerateCert). |
| 267 uint32 serial_number_; | 267 uint32 serial_number_; |
| 268 scoped_refptr<base::SequencedTaskRunner> origin_loop_; | 268 scoped_refptr<base::SequencedTaskRunner> origin_loop_; |
| 269 WorkerDoneCallback callback_; | 269 WorkerDoneCallback callback_; |
| 270 | 270 |
| 271 DISALLOW_COPY_AND_ASSIGN(ServerBoundCertServiceWorker); | 271 DISALLOW_COPY_AND_ASSIGN(ChannelIDServiceWorker); |
| 272 }; | 272 }; |
| 273 | 273 |
| 274 // A ServerBoundCertServiceJob is a one-to-one counterpart of an | 274 // A ChannelIDServiceJob is a one-to-one counterpart of an |
| 275 // ServerBoundCertServiceWorker. It lives only on the ServerBoundCertService's | 275 // ChannelIDServiceWorker. It lives only on the ChannelIDService's |
| 276 // origin message loop. | 276 // origin message loop. |
| 277 class ServerBoundCertServiceJob { | 277 class ChannelIDServiceJob { |
| 278 public: | 278 public: |
| 279 ServerBoundCertServiceJob(bool create_if_missing) | 279 ChannelIDServiceJob(bool create_if_missing) |
| 280 : create_if_missing_(create_if_missing) { | 280 : create_if_missing_(create_if_missing) { |
| 281 } | 281 } |
| 282 | 282 |
| 283 ~ServerBoundCertServiceJob() { | 283 ~ChannelIDServiceJob() { |
| 284 if (!requests_.empty()) | 284 if (!requests_.empty()) |
| 285 DeleteAllCanceled(); | 285 DeleteAllCanceled(); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void AddRequest(ServerBoundCertServiceRequest* request, | 288 void AddRequest(ChannelIDServiceRequest* request, |
| 289 bool create_if_missing = false) { | 289 bool create_if_missing = false) { |
| 290 create_if_missing_ |= create_if_missing; | 290 create_if_missing_ |= create_if_missing; |
| 291 requests_.push_back(request); | 291 requests_.push_back(request); |
| 292 } | 292 } |
| 293 | 293 |
| 294 void HandleResult(int error, | 294 void HandleResult(int error, |
| 295 const std::string& private_key, | 295 const std::string& private_key, |
| 296 const std::string& cert) { | 296 const std::string& cert) { |
| 297 PostAll(error, private_key, cert); | 297 PostAll(error, private_key, cert); |
| 298 } | 298 } |
| 299 | 299 |
| 300 bool CreateIfMissing() const { return create_if_missing_; } | 300 bool CreateIfMissing() const { return create_if_missing_; } |
| 301 | 301 |
| 302 private: | 302 private: |
| 303 void PostAll(int error, | 303 void PostAll(int error, |
| 304 const std::string& private_key, | 304 const std::string& private_key, |
| 305 const std::string& cert) { | 305 const std::string& cert) { |
| 306 std::vector<ServerBoundCertServiceRequest*> requests; | 306 std::vector<ChannelIDServiceRequest*> requests; |
| 307 requests_.swap(requests); | 307 requests_.swap(requests); |
| 308 | 308 |
| 309 for (std::vector<ServerBoundCertServiceRequest*>::iterator | 309 for (std::vector<ChannelIDServiceRequest*>::iterator |
| 310 i = requests.begin(); i != requests.end(); i++) { | 310 i = requests.begin(); i != requests.end(); i++) { |
| 311 (*i)->Post(error, private_key, cert); | 311 (*i)->Post(error, private_key, cert); |
| 312 // Post() causes the ServerBoundCertServiceRequest to delete itself. | 312 // Post() causes the ChannelIDServiceRequest to delete itself. |
| 313 } | 313 } |
| 314 } | 314 } |
| 315 | 315 |
| 316 void DeleteAllCanceled() { | 316 void DeleteAllCanceled() { |
| 317 for (std::vector<ServerBoundCertServiceRequest*>::iterator | 317 for (std::vector<ChannelIDServiceRequest*>::iterator |
| 318 i = requests_.begin(); i != requests_.end(); i++) { | 318 i = requests_.begin(); i != requests_.end(); i++) { |
| 319 if ((*i)->canceled()) { | 319 if ((*i)->canceled()) { |
| 320 delete *i; | 320 delete *i; |
| 321 } else { | 321 } else { |
| 322 LOG(DFATAL) << "ServerBoundCertServiceRequest leaked!"; | 322 LOG(DFATAL) << "ChannelIDServiceRequest leaked!"; |
| 323 } | 323 } |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 | 326 |
| 327 std::vector<ServerBoundCertServiceRequest*> requests_; | 327 std::vector<ChannelIDServiceRequest*> requests_; |
| 328 bool create_if_missing_; | 328 bool create_if_missing_; |
| 329 }; | 329 }; |
| 330 | 330 |
| 331 // static | 331 // static |
| 332 const char ServerBoundCertService::kEPKIPassword[] = ""; | 332 const char ChannelIDService::kEPKIPassword[] = ""; |
| 333 | 333 |
| 334 ServerBoundCertService::RequestHandle::RequestHandle() | 334 ChannelIDService::RequestHandle::RequestHandle() |
| 335 : service_(NULL), | 335 : service_(NULL), |
| 336 request_(NULL) {} | 336 request_(NULL) {} |
| 337 | 337 |
| 338 ServerBoundCertService::RequestHandle::~RequestHandle() { | 338 ChannelIDService::RequestHandle::~RequestHandle() { |
| 339 Cancel(); | 339 Cancel(); |
| 340 } | 340 } |
| 341 | 341 |
| 342 void ServerBoundCertService::RequestHandle::Cancel() { | 342 void ChannelIDService::RequestHandle::Cancel() { |
| 343 if (request_) { | 343 if (request_) { |
| 344 service_->CancelRequest(request_); | 344 service_->CancelRequest(request_); |
| 345 request_ = NULL; | 345 request_ = NULL; |
| 346 callback_.Reset(); | 346 callback_.Reset(); |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 | 349 |
| 350 void ServerBoundCertService::RequestHandle::RequestStarted( | 350 void ChannelIDService::RequestHandle::RequestStarted( |
| 351 ServerBoundCertService* service, | 351 ChannelIDService* service, |
| 352 ServerBoundCertServiceRequest* request, | 352 ChannelIDServiceRequest* request, |
| 353 const CompletionCallback& callback) { | 353 const CompletionCallback& callback) { |
| 354 DCHECK(request_ == NULL); | 354 DCHECK(request_ == NULL); |
| 355 service_ = service; | 355 service_ = service; |
| 356 request_ = request; | 356 request_ = request; |
| 357 callback_ = callback; | 357 callback_ = callback; |
| 358 } | 358 } |
| 359 | 359 |
| 360 void ServerBoundCertService::RequestHandle::OnRequestComplete(int result) { | 360 void ChannelIDService::RequestHandle::OnRequestComplete(int result) { |
| 361 request_ = NULL; | 361 request_ = NULL; |
| 362 // Running the callback might delete |this|, so we can't touch any of our | 362 // Running the callback might delete |this|, so we can't touch any of our |
| 363 // members afterwards. Reset callback_ first. | 363 // members afterwards. Reset callback_ first. |
| 364 base::ResetAndReturn(&callback_).Run(result); | 364 base::ResetAndReturn(&callback_).Run(result); |
| 365 } | 365 } |
| 366 | 366 |
| 367 ServerBoundCertService::ServerBoundCertService( | 367 ChannelIDService::ChannelIDService( |
| 368 ServerBoundCertStore* server_bound_cert_store, | 368 ChannelIDStore* channel_id_store, |
| 369 const scoped_refptr<base::TaskRunner>& task_runner) | 369 const scoped_refptr<base::TaskRunner>& task_runner) |
| 370 : server_bound_cert_store_(server_bound_cert_store), | 370 : channel_id_store_(channel_id_store), |
| 371 task_runner_(task_runner), | 371 task_runner_(task_runner), |
| 372 requests_(0), | 372 requests_(0), |
| 373 cert_store_hits_(0), | 373 cert_store_hits_(0), |
| 374 inflight_joins_(0), | 374 inflight_joins_(0), |
| 375 workers_created_(0), | 375 workers_created_(0), |
| 376 weak_ptr_factory_(this) { | 376 weak_ptr_factory_(this) { |
| 377 base::Time start = base::Time::Now(); | 377 base::Time start = base::Time::Now(); |
| 378 base::Time end = start + base::TimeDelta::FromDays( | 378 base::Time end = start + base::TimeDelta::FromDays( |
| 379 kValidityPeriodInDays + kSystemTimeValidityBufferInDays); | 379 kValidityPeriodInDays + kSystemTimeValidityBufferInDays); |
| 380 is_system_time_valid_ = x509_util::IsSupportedValidityRange(start, end); | 380 is_system_time_valid_ = x509_util::IsSupportedValidityRange(start, end); |
| 381 } | 381 } |
| 382 | 382 |
| 383 ServerBoundCertService::~ServerBoundCertService() { | 383 ChannelIDService::~ChannelIDService() { |
| 384 STLDeleteValues(&inflight_); | 384 STLDeleteValues(&inflight_); |
| 385 } | 385 } |
| 386 | 386 |
| 387 //static | 387 //static |
| 388 std::string ServerBoundCertService::GetDomainForHost(const std::string& host) { | 388 std::string ChannelIDService::GetDomainForHost(const std::string& host) { |
| 389 std::string domain = | 389 std::string domain = |
| 390 registry_controlled_domains::GetDomainAndRegistry( | 390 registry_controlled_domains::GetDomainAndRegistry( |
| 391 host, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 391 host, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 392 if (domain.empty()) | 392 if (domain.empty()) |
| 393 return host; | 393 return host; |
| 394 return domain; | 394 return domain; |
| 395 } | 395 } |
| 396 | 396 |
| 397 int ServerBoundCertService::GetOrCreateDomainBoundCert( | 397 int ChannelIDService::GetOrCreateChannelID( |
| 398 const std::string& host, | 398 const std::string& host, |
| 399 std::string* private_key, | 399 std::string* private_key, |
| 400 std::string* cert, | 400 std::string* cert, |
| 401 const CompletionCallback& callback, | 401 const CompletionCallback& callback, |
| 402 RequestHandle* out_req) { | 402 RequestHandle* out_req) { |
| 403 DVLOG(1) << __FUNCTION__ << " " << host; | 403 DVLOG(1) << __FUNCTION__ << " " << host; |
| 404 DCHECK(CalledOnValidThread()); | 404 DCHECK(CalledOnValidThread()); |
| 405 base::TimeTicks request_start = base::TimeTicks::Now(); | 405 base::TimeTicks request_start = base::TimeTicks::Now(); |
| 406 | 406 |
| 407 if (callback.is_null() || !private_key || !cert || host.empty()) { | 407 if (callback.is_null() || !private_key || !cert || host.empty()) { |
| 408 RecordGetDomainBoundCertResult(INVALID_ARGUMENT); | 408 RecordGetChannelIDResult(INVALID_ARGUMENT); |
| 409 return ERR_INVALID_ARGUMENT; | 409 return ERR_INVALID_ARGUMENT; |
| 410 } | 410 } |
| 411 | 411 |
| 412 std::string domain = GetDomainForHost(host); | 412 std::string domain = GetDomainForHost(host); |
| 413 if (domain.empty()) { | 413 if (domain.empty()) { |
| 414 RecordGetDomainBoundCertResult(INVALID_ARGUMENT); | 414 RecordGetChannelIDResult(INVALID_ARGUMENT); |
| 415 return ERR_INVALID_ARGUMENT; | 415 return ERR_INVALID_ARGUMENT; |
| 416 } | 416 } |
| 417 | 417 |
| 418 requests_++; | 418 requests_++; |
| 419 | 419 |
| 420 // See if a request for the same domain is currently in flight. | 420 // See if a request for the same domain is currently in flight. |
| 421 bool create_if_missing = true; | 421 bool create_if_missing = true; |
| 422 if (JoinToInFlightRequest(request_start, domain, private_key, cert, | 422 if (JoinToInFlightRequest(request_start, domain, private_key, cert, |
| 423 create_if_missing, callback, out_req)) { | 423 create_if_missing, callback, out_req)) { |
| 424 return ERR_IO_PENDING; | 424 return ERR_IO_PENDING; |
| 425 } | 425 } |
| 426 | 426 |
| 427 int err = LookupDomainBoundCert(request_start, domain, private_key, cert, | 427 int err = LookupChannelID(request_start, domain, private_key, cert, |
| 428 create_if_missing, callback, out_req); | 428 create_if_missing, callback, out_req); |
| 429 if (err == ERR_FILE_NOT_FOUND) { | 429 if (err == ERR_FILE_NOT_FOUND) { |
| 430 // Sync lookup did not find a valid cert. Start generating a new one. | 430 // Sync lookup did not find a valid cert. Start generating a new one. |
| 431 workers_created_++; | 431 workers_created_++; |
| 432 ServerBoundCertServiceWorker* worker = new ServerBoundCertServiceWorker( | 432 ChannelIDServiceWorker* worker = new ChannelIDServiceWorker( |
| 433 domain, | 433 domain, |
| 434 base::Bind(&ServerBoundCertService::GeneratedServerBoundCert, | 434 base::Bind(&ChannelIDService::GeneratedChannelID, |
| 435 weak_ptr_factory_.GetWeakPtr())); | 435 weak_ptr_factory_.GetWeakPtr())); |
| 436 if (!worker->Start(task_runner_)) { | 436 if (!worker->Start(task_runner_)) { |
| 437 // TODO(rkn): Log to the NetLog. | 437 // TODO(rkn): Log to the NetLog. |
| 438 LOG(ERROR) << "ServerBoundCertServiceWorker couldn't be started."; | 438 LOG(ERROR) << "ChannelIDServiceWorker couldn't be started."; |
| 439 RecordGetDomainBoundCertResult(WORKER_FAILURE); | 439 RecordGetChannelIDResult(WORKER_FAILURE); |
| 440 return ERR_INSUFFICIENT_RESOURCES; | 440 return ERR_INSUFFICIENT_RESOURCES; |
| 441 } | 441 } |
| 442 // We are waiting for cert generation. Create a job & request to track it. | 442 // We are waiting for cert generation. Create a job & request to track it. |
| 443 ServerBoundCertServiceJob* job = | 443 ChannelIDServiceJob* job = new ChannelIDServiceJob(create_if_missing); |
| 444 new ServerBoundCertServiceJob(create_if_missing); | |
| 445 inflight_[domain] = job; | 444 inflight_[domain] = job; |
| 446 | 445 |
| 447 ServerBoundCertServiceRequest* request = new ServerBoundCertServiceRequest( | 446 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( |
| 448 request_start, | 447 request_start, |
| 449 base::Bind(&RequestHandle::OnRequestComplete, | 448 base::Bind(&RequestHandle::OnRequestComplete, |
| 450 base::Unretained(out_req)), | 449 base::Unretained(out_req)), |
| 451 private_key, | 450 private_key, |
| 452 cert); | 451 cert); |
| 453 job->AddRequest(request); | 452 job->AddRequest(request); |
| 454 out_req->RequestStarted(this, request, callback); | 453 out_req->RequestStarted(this, request, callback); |
| 455 return ERR_IO_PENDING; | 454 return ERR_IO_PENDING; |
| 456 } | 455 } |
| 457 | 456 |
| 458 return err; | 457 return err; |
| 459 } | 458 } |
| 460 | 459 |
| 461 int ServerBoundCertService::GetDomainBoundCert( | 460 int ChannelIDService::GetChannelID( |
| 462 const std::string& host, | 461 const std::string& host, |
| 463 std::string* private_key, | 462 std::string* private_key, |
| 464 std::string* cert, | 463 std::string* cert, |
| 465 const CompletionCallback& callback, | 464 const CompletionCallback& callback, |
| 466 RequestHandle* out_req) { | 465 RequestHandle* out_req) { |
| 467 DVLOG(1) << __FUNCTION__ << " " << host; | 466 DVLOG(1) << __FUNCTION__ << " " << host; |
| 468 DCHECK(CalledOnValidThread()); | 467 DCHECK(CalledOnValidThread()); |
| 469 base::TimeTicks request_start = base::TimeTicks::Now(); | 468 base::TimeTicks request_start = base::TimeTicks::Now(); |
| 470 | 469 |
| 471 if (callback.is_null() || !private_key || !cert || host.empty()) { | 470 if (callback.is_null() || !private_key || !cert || host.empty()) { |
| 472 RecordGetDomainBoundCertResult(INVALID_ARGUMENT); | 471 RecordGetChannelIDResult(INVALID_ARGUMENT); |
| 473 return ERR_INVALID_ARGUMENT; | 472 return ERR_INVALID_ARGUMENT; |
| 474 } | 473 } |
| 475 | 474 |
| 476 std::string domain = GetDomainForHost(host); | 475 std::string domain = GetDomainForHost(host); |
| 477 if (domain.empty()) { | 476 if (domain.empty()) { |
| 478 RecordGetDomainBoundCertResult(INVALID_ARGUMENT); | 477 RecordGetChannelIDResult(INVALID_ARGUMENT); |
| 479 return ERR_INVALID_ARGUMENT; | 478 return ERR_INVALID_ARGUMENT; |
| 480 } | 479 } |
| 481 | 480 |
| 482 requests_++; | 481 requests_++; |
| 483 | 482 |
| 484 // See if a request for the same domain currently in flight. | 483 // See if a request for the same domain currently in flight. |
| 485 bool create_if_missing = false; | 484 bool create_if_missing = false; |
| 486 if (JoinToInFlightRequest(request_start, domain, private_key, cert, | 485 if (JoinToInFlightRequest(request_start, domain, private_key, cert, |
| 487 create_if_missing, callback, out_req)) { | 486 create_if_missing, callback, out_req)) { |
| 488 return ERR_IO_PENDING; | 487 return ERR_IO_PENDING; |
| 489 } | 488 } |
| 490 | 489 |
| 491 int err = LookupDomainBoundCert(request_start, domain, private_key, cert, | 490 int err = LookupChannelID(request_start, domain, private_key, cert, |
| 492 create_if_missing, callback, out_req); | 491 create_if_missing, callback, out_req); |
| 493 return err; | 492 return err; |
| 494 } | 493 } |
| 495 | 494 |
| 496 void ServerBoundCertService::GotServerBoundCert( | 495 void ChannelIDService::GotChannelID( |
| 497 int err, | 496 int err, |
| 498 const std::string& server_identifier, | 497 const std::string& server_identifier, |
| 499 base::Time expiration_time, | 498 base::Time expiration_time, |
| 500 const std::string& key, | 499 const std::string& key, |
| 501 const std::string& cert) { | 500 const std::string& cert) { |
| 502 DCHECK(CalledOnValidThread()); | 501 DCHECK(CalledOnValidThread()); |
| 503 | 502 |
| 504 std::map<std::string, ServerBoundCertServiceJob*>::iterator j; | 503 std::map<std::string, ChannelIDServiceJob*>::iterator j; |
| 505 j = inflight_.find(server_identifier); | 504 j = inflight_.find(server_identifier); |
| 506 if (j == inflight_.end()) { | 505 if (j == inflight_.end()) { |
| 507 NOTREACHED(); | 506 NOTREACHED(); |
| 508 return; | 507 return; |
| 509 } | 508 } |
| 510 | 509 |
| 511 if (err == OK) { | 510 if (err == OK) { |
| 512 // Async DB lookup found a valid cert. | 511 // Async DB lookup found a valid cert. |
| 513 DVLOG(1) << "Cert store had valid cert for " << server_identifier; | 512 DVLOG(1) << "Cert store had valid cert for " << server_identifier; |
| 514 cert_store_hits_++; | 513 cert_store_hits_++; |
| 515 // ServerBoundCertServiceRequest::Post will do the histograms and stuff. | 514 // ChannelIDServiceRequest::Post will do the histograms and stuff. |
| 516 HandleResult(OK, server_identifier, key, cert); | 515 HandleResult(OK, server_identifier, key, cert); |
| 517 return; | 516 return; |
| 518 } | 517 } |
| 519 // Async lookup failed or the certificate was missing. Return the error | 518 // Async lookup failed or the certificate was missing. Return the error |
| 520 // directly, unless the certificate was missing and a request asked to create | 519 // directly, unless the certificate was missing and a request asked to create |
| 521 // one. | 520 // one. |
| 522 if (err != ERR_FILE_NOT_FOUND || !j->second->CreateIfMissing()) { | 521 if (err != ERR_FILE_NOT_FOUND || !j->second->CreateIfMissing()) { |
| 523 HandleResult(err, server_identifier, key, cert); | 522 HandleResult(err, server_identifier, key, cert); |
| 524 return; | 523 return; |
| 525 } | 524 } |
| 526 // At least one request asked to create a cert => start generating a new one. | 525 // At least one request asked to create a cert => start generating a new one. |
| 527 workers_created_++; | 526 workers_created_++; |
| 528 ServerBoundCertServiceWorker* worker = new ServerBoundCertServiceWorker( | 527 ChannelIDServiceWorker* worker = new ChannelIDServiceWorker( |
| 529 server_identifier, | 528 server_identifier, |
| 530 base::Bind(&ServerBoundCertService::GeneratedServerBoundCert, | 529 base::Bind(&ChannelIDService::GeneratedChannelID, |
| 531 weak_ptr_factory_.GetWeakPtr())); | 530 weak_ptr_factory_.GetWeakPtr())); |
| 532 if (!worker->Start(task_runner_)) { | 531 if (!worker->Start(task_runner_)) { |
| 533 // TODO(rkn): Log to the NetLog. | 532 // TODO(rkn): Log to the NetLog. |
| 534 LOG(ERROR) << "ServerBoundCertServiceWorker couldn't be started."; | 533 LOG(ERROR) << "ChannelIDServiceWorker couldn't be started."; |
| 535 HandleResult(ERR_INSUFFICIENT_RESOURCES, | 534 HandleResult(ERR_INSUFFICIENT_RESOURCES, |
| 536 server_identifier, | 535 server_identifier, |
| 537 std::string(), | 536 std::string(), |
| 538 std::string()); | 537 std::string()); |
| 539 } | 538 } |
| 540 } | 539 } |
| 541 | 540 |
| 542 ServerBoundCertStore* ServerBoundCertService::GetCertStore() { | 541 ChannelIDStore* ChannelIDService::GetChannelIDStore() { |
| 543 return server_bound_cert_store_.get(); | 542 return channel_id_store_.get(); |
| 544 } | 543 } |
| 545 | 544 |
| 546 void ServerBoundCertService::CancelRequest(ServerBoundCertServiceRequest* req) { | 545 void ChannelIDService::CancelRequest(ChannelIDServiceRequest* req) { |
| 547 DCHECK(CalledOnValidThread()); | 546 DCHECK(CalledOnValidThread()); |
| 548 req->Cancel(); | 547 req->Cancel(); |
| 549 } | 548 } |
| 550 | 549 |
| 551 void ServerBoundCertService::GeneratedServerBoundCert( | 550 void ChannelIDService::GeneratedChannelID( |
| 552 const std::string& server_identifier, | 551 const std::string& server_identifier, |
| 553 int error, | 552 int error, |
| 554 scoped_ptr<ServerBoundCertStore::ServerBoundCert> cert) { | 553 scoped_ptr<ChannelIDStore::ChannelID> cert) { |
| 555 DCHECK(CalledOnValidThread()); | 554 DCHECK(CalledOnValidThread()); |
| 556 | 555 |
| 557 if (error == OK) { | 556 if (error == OK) { |
| 558 // TODO(mattm): we should just Pass() the cert object to | 557 // TODO(mattm): we should just Pass() the cert object to |
| 559 // SetServerBoundCert(). | 558 // SetChannelID(). |
| 560 server_bound_cert_store_->SetServerBoundCert( | 559 channel_id_store_->SetChannelID( |
| 561 cert->server_identifier(), | 560 cert->server_identifier(), |
| 562 cert->creation_time(), | 561 cert->creation_time(), |
| 563 cert->expiration_time(), | 562 cert->expiration_time(), |
| 564 cert->private_key(), | 563 cert->private_key(), |
| 565 cert->cert()); | 564 cert->cert()); |
| 566 | 565 |
| 567 HandleResult(error, server_identifier, cert->private_key(), cert->cert()); | 566 HandleResult(error, server_identifier, cert->private_key(), cert->cert()); |
| 568 } else { | 567 } else { |
| 569 HandleResult(error, server_identifier, std::string(), std::string()); | 568 HandleResult(error, server_identifier, std::string(), std::string()); |
| 570 } | 569 } |
| 571 } | 570 } |
| 572 | 571 |
| 573 void ServerBoundCertService::HandleResult( | 572 void ChannelIDService::HandleResult( |
| 574 int error, | 573 int error, |
| 575 const std::string& server_identifier, | 574 const std::string& server_identifier, |
| 576 const std::string& private_key, | 575 const std::string& private_key, |
| 577 const std::string& cert) { | 576 const std::string& cert) { |
| 578 DCHECK(CalledOnValidThread()); | 577 DCHECK(CalledOnValidThread()); |
| 579 | 578 |
| 580 std::map<std::string, ServerBoundCertServiceJob*>::iterator j; | 579 std::map<std::string, ChannelIDServiceJob*>::iterator j; |
| 581 j = inflight_.find(server_identifier); | 580 j = inflight_.find(server_identifier); |
| 582 if (j == inflight_.end()) { | 581 if (j == inflight_.end()) { |
| 583 NOTREACHED(); | 582 NOTREACHED(); |
| 584 return; | 583 return; |
| 585 } | 584 } |
| 586 ServerBoundCertServiceJob* job = j->second; | 585 ChannelIDServiceJob* job = j->second; |
| 587 inflight_.erase(j); | 586 inflight_.erase(j); |
| 588 | 587 |
| 589 job->HandleResult(error, private_key, cert); | 588 job->HandleResult(error, private_key, cert); |
| 590 delete job; | 589 delete job; |
| 591 } | 590 } |
| 592 | 591 |
| 593 bool ServerBoundCertService::JoinToInFlightRequest( | 592 bool ChannelIDService::JoinToInFlightRequest( |
| 594 const base::TimeTicks& request_start, | 593 const base::TimeTicks& request_start, |
| 595 const std::string& domain, | 594 const std::string& domain, |
| 596 std::string* private_key, | 595 std::string* private_key, |
| 597 std::string* cert, | 596 std::string* cert, |
| 598 bool create_if_missing, | 597 bool create_if_missing, |
| 599 const CompletionCallback& callback, | 598 const CompletionCallback& callback, |
| 600 RequestHandle* out_req) { | 599 RequestHandle* out_req) { |
| 601 ServerBoundCertServiceJob* job = NULL; | 600 ChannelIDServiceJob* job = NULL; |
| 602 std::map<std::string, ServerBoundCertServiceJob*>::const_iterator j = | 601 std::map<std::string, ChannelIDServiceJob*>::const_iterator j = |
| 603 inflight_.find(domain); | 602 inflight_.find(domain); |
| 604 if (j != inflight_.end()) { | 603 if (j != inflight_.end()) { |
| 605 // A request for the same domain is in flight already. We'll attach our | 604 // A request for the same domain is in flight already. We'll attach our |
| 606 // callback, but we'll also mark it as requiring a cert if one's mising. | 605 // callback, but we'll also mark it as requiring a cert if one's mising. |
| 607 job = j->second; | 606 job = j->second; |
| 608 inflight_joins_++; | 607 inflight_joins_++; |
| 609 | 608 |
| 610 ServerBoundCertServiceRequest* request = new ServerBoundCertServiceRequest( | 609 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( |
| 611 request_start, | 610 request_start, |
| 612 base::Bind(&RequestHandle::OnRequestComplete, | 611 base::Bind(&RequestHandle::OnRequestComplete, |
| 613 base::Unretained(out_req)), | 612 base::Unretained(out_req)), |
| 614 private_key, | 613 private_key, |
| 615 cert); | 614 cert); |
| 616 job->AddRequest(request, create_if_missing); | 615 job->AddRequest(request, create_if_missing); |
| 617 out_req->RequestStarted(this, request, callback); | 616 out_req->RequestStarted(this, request, callback); |
| 618 return true; | 617 return true; |
| 619 } | 618 } |
| 620 return false; | 619 return false; |
| 621 } | 620 } |
| 622 | 621 |
| 623 int ServerBoundCertService::LookupDomainBoundCert( | 622 int ChannelIDService::LookupChannelID( |
| 624 const base::TimeTicks& request_start, | 623 const base::TimeTicks& request_start, |
| 625 const std::string& domain, | 624 const std::string& domain, |
| 626 std::string* private_key, | 625 std::string* private_key, |
| 627 std::string* cert, | 626 std::string* cert, |
| 628 bool create_if_missing, | 627 bool create_if_missing, |
| 629 const CompletionCallback& callback, | 628 const CompletionCallback& callback, |
| 630 RequestHandle* out_req) { | 629 RequestHandle* out_req) { |
| 631 // Check if a domain bound cert already exists for this domain. Note that | 630 // Check if a domain bound cert already exists for this domain. Note that |
| 632 // |expiration_time| is ignored, and expired certs are considered valid. | 631 // |expiration_time| is ignored, and expired certs are considered valid. |
| 633 base::Time expiration_time; | 632 base::Time expiration_time; |
| 634 int err = server_bound_cert_store_->GetServerBoundCert( | 633 int err = channel_id_store_->GetChannelID( |
| 635 domain, | 634 domain, |
| 636 &expiration_time /* ignored */, | 635 &expiration_time /* ignored */, |
| 637 private_key, | 636 private_key, |
| 638 cert, | 637 cert, |
| 639 base::Bind(&ServerBoundCertService::GotServerBoundCert, | 638 base::Bind(&ChannelIDService::GotChannelID, |
| 640 weak_ptr_factory_.GetWeakPtr())); | 639 weak_ptr_factory_.GetWeakPtr())); |
| 641 | 640 |
| 642 if (err == OK) { | 641 if (err == OK) { |
| 643 // Sync lookup found a valid cert. | 642 // Sync lookup found a valid cert. |
| 644 DVLOG(1) << "Cert store had valid cert for " << domain; | 643 DVLOG(1) << "Cert store had valid cert for " << domain; |
| 645 cert_store_hits_++; | 644 cert_store_hits_++; |
| 646 RecordGetDomainBoundCertResult(SYNC_SUCCESS); | 645 RecordGetChannelIDResult(SYNC_SUCCESS); |
| 647 base::TimeDelta request_time = base::TimeTicks::Now() - request_start; | 646 base::TimeDelta request_time = base::TimeTicks::Now() - request_start; |
| 648 UMA_HISTOGRAM_TIMES("DomainBoundCerts.GetCertTimeSync", request_time); | 647 UMA_HISTOGRAM_TIMES("DomainBoundCerts.GetCertTimeSync", request_time); |
| 649 RecordGetCertTime(request_time); | 648 RecordGetChannelIDTime(request_time); |
| 650 return OK; | 649 return OK; |
| 651 } | 650 } |
| 652 | 651 |
| 653 if (err == ERR_IO_PENDING) { | 652 if (err == ERR_IO_PENDING) { |
| 654 // We are waiting for async DB lookup. Create a job & request to track it. | 653 // We are waiting for async DB lookup. Create a job & request to track it. |
| 655 ServerBoundCertServiceJob* job = | 654 ChannelIDServiceJob* job = new ChannelIDServiceJob(create_if_missing); |
| 656 new ServerBoundCertServiceJob(create_if_missing); | |
| 657 inflight_[domain] = job; | 655 inflight_[domain] = job; |
| 658 | 656 |
| 659 ServerBoundCertServiceRequest* request = new ServerBoundCertServiceRequest( | 657 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( |
| 660 request_start, | 658 request_start, |
| 661 base::Bind(&RequestHandle::OnRequestComplete, | 659 base::Bind(&RequestHandle::OnRequestComplete, |
| 662 base::Unretained(out_req)), | 660 base::Unretained(out_req)), |
| 663 private_key, | 661 private_key, |
| 664 cert); | 662 cert); |
| 665 job->AddRequest(request); | 663 job->AddRequest(request); |
| 666 out_req->RequestStarted(this, request, callback); | 664 out_req->RequestStarted(this, request, callback); |
| 667 return ERR_IO_PENDING; | 665 return ERR_IO_PENDING; |
| 668 } | 666 } |
| 669 | 667 |
| 670 return err; | 668 return err; |
| 671 } | 669 } |
| 672 | 670 |
| 673 int ServerBoundCertService::cert_count() { | 671 int ChannelIDService::cert_count() { |
| 674 return server_bound_cert_store_->GetCertCount(); | 672 return channel_id_store_->GetCertCount(); |
| 675 } | 673 } |
| 676 | 674 |
| 677 } // namespace net | 675 } // namespace net |
| OLD | NEW |