OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/origin_bound_cert_service.h" | 5 #include "net/base/origin_bound_cert_service.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
| 9 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" |
12 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/stl_util.h" |
| 16 #include "base/threading/worker_pool.h" |
13 #include "crypto/rsa_private_key.h" | 17 #include "crypto/rsa_private_key.h" |
| 18 #include "net/base/net_errors.h" |
14 #include "net/base/origin_bound_cert_store.h" | 19 #include "net/base/origin_bound_cert_store.h" |
15 #include "net/base/x509_certificate.h" | 20 #include "net/base/x509_certificate.h" |
16 | 21 |
| 22 #if defined(USE_NSS) |
| 23 #include <private/pprthred.h> // PR_DetachThread |
| 24 #endif |
| 25 |
17 namespace net { | 26 namespace net { |
18 | 27 |
19 namespace { | 28 namespace { |
20 | 29 |
21 const int kKeySizeInBits = 1024; | 30 const int kKeySizeInBits = 1024; |
22 const int kValidityPeriodInDays = 365; | 31 const int kValidityPeriodInDays = 365; |
23 | 32 |
24 } // namespace | 33 } // namespace |
25 | 34 |
| 35 // Represents the output and result callback of a request. |
| 36 class OriginBoundCertServiceRequest { |
| 37 public: |
| 38 OriginBoundCertServiceRequest(CompletionCallback* callback, |
| 39 std::string* private_key, |
| 40 std::string* cert) |
| 41 : callback_(callback), |
| 42 private_key_(private_key), |
| 43 cert_(cert) { |
| 44 } |
| 45 |
| 46 // Ensures that the result callback will never be made. |
| 47 void Cancel() { |
| 48 callback_ = NULL; |
| 49 private_key_ = NULL; |
| 50 cert_ = NULL; |
| 51 } |
| 52 |
| 53 // Copies the contents of |private_key| and |cert| to the caller's output |
| 54 // arguments and calls the callback. |
| 55 void Post(int error, |
| 56 const std::string& private_key, |
| 57 const std::string& cert) { |
| 58 if (callback_) { |
| 59 *private_key_ = private_key; |
| 60 *cert_ = cert; |
| 61 callback_->Run(error); |
| 62 } |
| 63 delete this; |
| 64 } |
| 65 |
| 66 bool canceled() const { return !callback_; } |
| 67 |
| 68 private: |
| 69 CompletionCallback* callback_; |
| 70 std::string* private_key_; |
| 71 std::string* cert_; |
| 72 }; |
| 73 |
| 74 // OriginBoundCertServiceWorker runs on a worker thread and takes care of the |
| 75 // blocking process of performing key generation. Deletes itself eventually |
| 76 // if Start() succeeds. |
| 77 class OriginBoundCertServiceWorker { |
| 78 public: |
| 79 OriginBoundCertServiceWorker( |
| 80 const std::string& origin, |
| 81 OriginBoundCertService* origin_bound_cert_service) |
| 82 : origin_(origin), |
| 83 serial_number_(base::RandInt(0, std::numeric_limits<int>::max())), |
| 84 origin_loop_(MessageLoop::current()), |
| 85 origin_bound_cert_service_(origin_bound_cert_service), |
| 86 canceled_(false), |
| 87 error_(ERR_FAILED) { |
| 88 } |
| 89 |
| 90 bool Start() { |
| 91 DCHECK_EQ(MessageLoop::current(), origin_loop_); |
| 92 |
| 93 return base::WorkerPool::PostTask( |
| 94 FROM_HERE, |
| 95 NewRunnableMethod(this, &OriginBoundCertServiceWorker::Run), |
| 96 true /* task is slow */); |
| 97 } |
| 98 |
| 99 // Cancel is called from the origin loop when the OriginBoundCertService is |
| 100 // getting deleted. |
| 101 void Cancel() { |
| 102 DCHECK_EQ(MessageLoop::current(), origin_loop_); |
| 103 base::AutoLock locked(lock_); |
| 104 canceled_ = true; |
| 105 } |
| 106 |
| 107 private: |
| 108 void Run() { |
| 109 // Runs on a worker thread. |
| 110 error_ = OriginBoundCertService::GenerateCert(origin_, |
| 111 serial_number_, |
| 112 &private_key_, |
| 113 &cert_); |
| 114 #if defined(USE_NSS) |
| 115 // Detach the thread from NSPR. |
| 116 // Calling NSS functions attaches the thread to NSPR, which stores |
| 117 // the NSPR thread ID in thread-specific data. |
| 118 // The threads in our thread pool terminate after we have called |
| 119 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 120 // segfaults on shutdown when the threads' thread-specific data |
| 121 // destructors run. |
| 122 PR_DetachThread(); |
| 123 #endif |
| 124 Finish(); |
| 125 } |
| 126 |
| 127 // DoReply runs on the origin thread. |
| 128 void DoReply() { |
| 129 DCHECK_EQ(MessageLoop::current(), origin_loop_); |
| 130 { |
| 131 // We lock here because the worker thread could still be in Finished, |
| 132 // after the PostTask, but before unlocking |lock_|. If we do not lock in |
| 133 // this case, we will end up deleting a locked Lock, which can lead to |
| 134 // memory leaks or worse errors. |
| 135 base::AutoLock locked(lock_); |
| 136 if (!canceled_) { |
| 137 origin_bound_cert_service_->HandleResult(origin_, error_, |
| 138 private_key_, cert_); |
| 139 } |
| 140 } |
| 141 delete this; |
| 142 } |
| 143 |
| 144 void Finish() { |
| 145 // Runs on the worker thread. |
| 146 // We assume that the origin loop outlives the OriginBoundCertService. If |
| 147 // the OriginBoundCertService is deleted, it will call Cancel on us. If it |
| 148 // does so before the Acquire, we'll delete ourselves and return. If it's |
| 149 // trying to do so concurrently, then it'll block on the lock and we'll |
| 150 // call PostTask while the OriginBoundCertService (and therefore the |
| 151 // MessageLoop) is still alive. If it does so after this function, we |
| 152 // assume that the MessageLoop will process pending tasks. In which case |
| 153 // we'll notice the |canceled_| flag in DoReply. |
| 154 |
| 155 bool canceled; |
| 156 { |
| 157 base::AutoLock locked(lock_); |
| 158 canceled = canceled_; |
| 159 if (!canceled) { |
| 160 origin_loop_->PostTask( |
| 161 FROM_HERE, |
| 162 NewRunnableMethod(this, &OriginBoundCertServiceWorker::DoReply)); |
| 163 } |
| 164 } |
| 165 if (canceled) |
| 166 delete this; |
| 167 } |
| 168 |
| 169 const std::string origin_; |
| 170 uint32 serial_number_; |
| 171 MessageLoop* const origin_loop_; |
| 172 OriginBoundCertService* const origin_bound_cert_service_; |
| 173 |
| 174 // lock_ protects canceled_. |
| 175 base::Lock lock_; |
| 176 |
| 177 // If canceled_ is true, |
| 178 // * origin_loop_ cannot be accessed by the worker thread, |
| 179 // * origin_bound_cert_service_ cannot be accessed by any thread. |
| 180 bool canceled_; |
| 181 |
| 182 int error_; |
| 183 std::string private_key_; |
| 184 std::string cert_; |
| 185 |
| 186 DISALLOW_COPY_AND_ASSIGN(OriginBoundCertServiceWorker); |
| 187 }; |
| 188 |
| 189 // An OriginBoundCertServiceJob is a one-to-one counterpart of an |
| 190 // OriginBoundCertServiceWorker. It lives only on the OriginBoundCertService's |
| 191 // origin message loop. |
| 192 class OriginBoundCertServiceJob { |
| 193 public: |
| 194 explicit OriginBoundCertServiceJob(OriginBoundCertServiceWorker* worker) |
| 195 : worker_(worker) { |
| 196 } |
| 197 |
| 198 ~OriginBoundCertServiceJob() { |
| 199 if (worker_) { |
| 200 worker_->Cancel(); |
| 201 DeleteAllCanceled(); |
| 202 } |
| 203 } |
| 204 |
| 205 void AddRequest(OriginBoundCertServiceRequest* request) { |
| 206 requests_.push_back(request); |
| 207 } |
| 208 |
| 209 void HandleResult(int error, |
| 210 const std::string& private_key, |
| 211 const std::string& cert) { |
| 212 worker_ = NULL; |
| 213 PostAll(error, private_key, cert); |
| 214 } |
| 215 |
| 216 private: |
| 217 void PostAll(int error, |
| 218 const std::string& private_key, |
| 219 const std::string& cert) { |
| 220 std::vector<OriginBoundCertServiceRequest*> requests; |
| 221 requests_.swap(requests); |
| 222 |
| 223 for (std::vector<OriginBoundCertServiceRequest*>::iterator |
| 224 i = requests.begin(); i != requests.end(); i++) { |
| 225 (*i)->Post(error, private_key, cert); |
| 226 // Post() causes the OriginBoundCertServiceRequest to delete itself. |
| 227 } |
| 228 } |
| 229 |
| 230 void DeleteAllCanceled() { |
| 231 for (std::vector<OriginBoundCertServiceRequest*>::iterator |
| 232 i = requests_.begin(); i != requests_.end(); i++) { |
| 233 if ((*i)->canceled()) { |
| 234 delete *i; |
| 235 } else { |
| 236 LOG(DFATAL) << "OriginBoundCertServiceRequest leaked!"; |
| 237 } |
| 238 } |
| 239 } |
| 240 |
| 241 std::vector<OriginBoundCertServiceRequest*> requests_; |
| 242 OriginBoundCertServiceWorker* worker_; |
| 243 }; |
| 244 |
26 OriginBoundCertService::OriginBoundCertService( | 245 OriginBoundCertService::OriginBoundCertService( |
27 OriginBoundCertStore* origin_bound_cert_store) | 246 OriginBoundCertStore* origin_bound_cert_store) |
28 : origin_bound_cert_store_(origin_bound_cert_store) {} | 247 : origin_bound_cert_store_(origin_bound_cert_store), |
29 | 248 requests_(0), |
30 OriginBoundCertService::~OriginBoundCertService() {} | 249 synchronous_completions_(0), |
31 | 250 inflight_joins_(0) {} |
32 bool OriginBoundCertService::GetOriginBoundCert(const std::string& origin, | 251 |
33 std::string* private_key_result, | 252 OriginBoundCertService::~OriginBoundCertService() { |
34 std::string* cert_result) { | 253 STLDeleteValues(&inflight_); |
35 // Check if origin bound cert already exists for this origin. | 254 } |
| 255 |
| 256 int OriginBoundCertService::GetOriginBoundCert(const std::string& origin, |
| 257 std::string* private_key, |
| 258 std::string* cert, |
| 259 CompletionCallback* callback, |
| 260 RequestHandle* out_req) { |
| 261 |
| 262 DCHECK(CalledOnValidThread()); |
| 263 |
| 264 if (!callback || !private_key || !cert || origin.empty()) { |
| 265 *out_req = NULL; |
| 266 return ERR_INVALID_ARGUMENT; |
| 267 } |
| 268 |
| 269 requests_++; |
| 270 |
| 271 // Check if an origin bound cert already exists for this origin. |
36 if (origin_bound_cert_store_->GetOriginBoundCert(origin, | 272 if (origin_bound_cert_store_->GetOriginBoundCert(origin, |
37 private_key_result, | 273 private_key, |
38 cert_result)) | 274 cert)) { |
39 return true; | 275 synchronous_completions_++; |
40 | 276 *out_req = NULL; |
41 // No origin bound cert exists, we have to create one. | 277 return OK; |
| 278 } |
| 279 |
| 280 // |origin_bound_cert_store_| has no cert for this origin. See if an |
| 281 // identical request is currently in flight. |
| 282 OriginBoundCertServiceJob* job; |
| 283 std::map<std::string, OriginBoundCertServiceJob*>::const_iterator j; |
| 284 j = inflight_.find(origin); |
| 285 if (j != inflight_.end()) { |
| 286 // An identical request is in flight already. We'll just attach our |
| 287 // callback. |
| 288 inflight_joins_++; |
| 289 job = j->second; |
| 290 } else { |
| 291 // Need to make a new request. |
| 292 OriginBoundCertServiceWorker* worker = |
| 293 new OriginBoundCertServiceWorker(origin, this); |
| 294 job = new OriginBoundCertServiceJob(worker); |
| 295 if (!worker->Start()) { |
| 296 delete job; |
| 297 delete worker; |
| 298 *out_req = NULL; |
| 299 // TODO(rkn): Log to the NetLog. |
| 300 LOG(ERROR) << "OriginBoundCertServiceWorker couldn't be started."; |
| 301 return ERR_INSUFFICIENT_RESOURCES; // Just a guess. |
| 302 } |
| 303 inflight_[origin] = job; |
| 304 } |
| 305 |
| 306 OriginBoundCertServiceRequest* request = |
| 307 new OriginBoundCertServiceRequest(callback, private_key, cert); |
| 308 job->AddRequest(request); |
| 309 *out_req = request; |
| 310 return ERR_IO_PENDING; |
| 311 } |
| 312 |
| 313 // static |
| 314 int OriginBoundCertService::GenerateCert(const std::string& origin, |
| 315 uint32 serial_number, |
| 316 std::string* private_key, |
| 317 std::string* cert) { |
42 std::string subject = "CN=OBC"; | 318 std::string subject = "CN=OBC"; |
43 scoped_ptr<crypto::RSAPrivateKey> key( | 319 scoped_ptr<crypto::RSAPrivateKey> key( |
44 crypto::RSAPrivateKey::Create(kKeySizeInBits)); | 320 crypto::RSAPrivateKey::Create(kKeySizeInBits)); |
45 if (!key.get()) { | 321 if (!key.get()) { |
46 LOG(WARNING) << "Unable to create key pair for client"; | 322 LOG(WARNING) << "Unable to create key pair for client"; |
47 return false; | 323 return ERR_KEY_GENERATION_FAILED; |
48 } | 324 } |
| 325 |
49 scoped_refptr<X509Certificate> x509_cert = X509Certificate::CreateSelfSigned( | 326 scoped_refptr<X509Certificate> x509_cert = X509Certificate::CreateSelfSigned( |
50 key.get(), | 327 key.get(), |
51 subject, | 328 subject, |
52 base::RandInt(0, std::numeric_limits<int>::max()), | 329 serial_number, |
53 base::TimeDelta::FromDays(kValidityPeriodInDays)); | 330 base::TimeDelta::FromDays(kValidityPeriodInDays)); |
54 if (!x509_cert) { | 331 if (!x509_cert) { |
55 LOG(WARNING) << "Unable to create x509 cert for client"; | 332 LOG(WARNING) << "Unable to create x509 cert for client"; |
56 return false; | 333 return ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED; |
57 } | 334 } |
58 | 335 |
59 std::vector<uint8> private_key_info; | 336 std::vector<uint8> private_key_info; |
60 if (!key->ExportPrivateKey(&private_key_info)) { | 337 if (!key->ExportPrivateKey(&private_key_info)) { |
61 LOG(WARNING) << "Unable to export private key"; | 338 LOG(WARNING) << "Unable to export private key"; |
62 return false; | 339 return ERR_PRIVATE_KEY_EXPORT_FAILED; |
63 } | 340 } |
64 // TODO(rkn): Perhaps ExportPrivateKey should be changed to output a | 341 // TODO(rkn): Perhaps ExportPrivateKey should be changed to output a |
65 // std::string* to prevent this copying. | 342 // std::string* to prevent this copying. |
66 std::string key_out(private_key_info.begin(), private_key_info.end()); | 343 std::string key_out(private_key_info.begin(), private_key_info.end()); |
67 | 344 |
68 std::string der_cert; | 345 std::string der_cert; |
69 if (!x509_cert->GetDEREncoded(&der_cert)) { | 346 if (!x509_cert->GetDEREncoded(&der_cert)) { |
70 LOG(WARNING) << "Unable to get DER-enconded cert"; | 347 LOG(WARNING) << "Unable to get DER-encoded cert"; |
71 return false; | 348 return ERR_GET_CERT_BYTES_FAILED; |
72 } | 349 } |
73 | 350 |
74 if (!origin_bound_cert_store_->SetOriginBoundCert(origin, | 351 private_key->swap(key_out); |
75 key_out, | 352 cert->swap(der_cert); |
76 der_cert)) { | 353 return OK; |
77 LOG(WARNING) << "Unable to set origin bound certificate"; | |
78 return false; | |
79 } | |
80 | |
81 private_key_result->swap(key_out); | |
82 cert_result->swap(der_cert); | |
83 return true; | |
84 } | 354 } |
85 | 355 |
86 int OriginBoundCertService::GetCertCount() { | 356 void OriginBoundCertService::CancelRequest(RequestHandle req) { |
| 357 DCHECK(CalledOnValidThread()); |
| 358 OriginBoundCertServiceRequest* request = |
| 359 reinterpret_cast<OriginBoundCertServiceRequest*>(req); |
| 360 request->Cancel(); |
| 361 } |
| 362 |
| 363 // HandleResult is called by OriginBoundCertServiceWorker on the origin message |
| 364 // loop. It deletes OriginBoundCertServiceJob. |
| 365 void OriginBoundCertService::HandleResult(const std::string& origin, |
| 366 int error, |
| 367 const std::string& private_key, |
| 368 const std::string& cert) { |
| 369 DCHECK(CalledOnValidThread()); |
| 370 |
| 371 origin_bound_cert_store_->SetOriginBoundCert(origin, private_key, cert); |
| 372 |
| 373 std::map<std::string, OriginBoundCertServiceJob*>::iterator j; |
| 374 j = inflight_.find(origin); |
| 375 if (j == inflight_.end()) { |
| 376 NOTREACHED(); |
| 377 return; |
| 378 } |
| 379 OriginBoundCertServiceJob* job = j->second; |
| 380 inflight_.erase(j); |
| 381 |
| 382 job->HandleResult(error, private_key, cert); |
| 383 delete job; |
| 384 } |
| 385 |
| 386 int OriginBoundCertService::cert_count() { |
87 return origin_bound_cert_store_->GetCertCount(); | 387 return origin_bound_cert_store_->GetCertCount(); |
88 } | 388 } |
89 | 389 |
90 } // namespace net | 390 } // namespace net |
| 391 |
| 392 DISABLE_RUNNABLE_METHOD_REFCOUNT(net::OriginBoundCertServiceWorker); |
OLD | NEW |