OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include "content/browser/media/dtls_identity_store.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/rand_util.h" |
| 12 #include "base/task_runner.h" |
| 13 #include "base/threading/worker_pool.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "crypto/rsa_private_key.h" |
| 16 #include "googleurl/src/gurl.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/cert/x509_certificate.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 namespace { |
| 23 |
| 24 struct DTLSIdentityRequestResult { |
| 25 int error; |
| 26 std::string certificate; |
| 27 std::string private_key; |
| 28 }; |
| 29 |
| 30 static void GenerateIdentityWorker(const std::string& common_name, |
| 31 DTLSIdentityRequestResult* result) { |
| 32 result->error = net::OK; |
| 33 std::string certificate; |
| 34 std::vector<uint8> private_key_info; |
| 35 |
| 36 int serial_number = base::RandInt(0, std::numeric_limits<int>::max()); |
| 37 |
| 38 scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(1024)); |
| 39 if (!key.get()) { |
| 40 DLOG(ERROR) << "Unable to create key pair for client"; |
| 41 result->error = net::ERR_KEY_GENERATION_FAILED; |
| 42 return; |
| 43 } |
| 44 |
| 45 scoped_refptr<net::X509Certificate> cert = |
| 46 net::X509Certificate::CreateSelfSigned(key.get(), |
| 47 "CN=" + common_name, |
| 48 serial_number, |
| 49 base::TimeDelta::FromDays(30)); |
| 50 if (!cert) { |
| 51 DLOG(ERROR) << "Unable to create x509 cert for client"; |
| 52 result->error = net::ERR_SELF_SIGNED_CERT_GENERATION_FAILED; |
| 53 return; |
| 54 } |
| 55 if (!net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), |
| 56 &result->certificate)) { |
| 57 DLOG(ERROR) << "Unable to get the DER decoded data from the cert."; |
| 58 result->error = net::ERR_SELF_SIGNED_CERT_GENERATION_FAILED; |
| 59 return; |
| 60 } |
| 61 |
| 62 if (!key->ExportPrivateKey(&private_key_info)) { |
| 63 DLOG(ERROR) << "Unable to export private key"; |
| 64 result->error = net::ERR_PRIVATE_KEY_EXPORT_FAILED; |
| 65 return; |
| 66 } |
| 67 |
| 68 result->private_key = |
| 69 std::string(private_key_info.begin(), private_key_info.end()); |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 class DTLSIdentityRequest { |
| 75 public: |
| 76 DTLSIdentityRequest(const DTLSIdentityStore::CompletionCallback& callback) |
| 77 : callback_(callback) {} |
| 78 |
| 79 void Cancel() { |
| 80 callback_.Reset(); |
| 81 } |
| 82 |
| 83 void Post(DTLSIdentityRequestResult* result) { |
| 84 if (callback_.is_null()) |
| 85 return; |
| 86 callback_.Run(result->error, result->certificate, result->private_key); |
| 87 } |
| 88 |
| 89 private: |
| 90 DTLSIdentityStore::CompletionCallback callback_; |
| 91 }; |
| 92 |
| 93 DTLSIdentityStore::RequestHandle::RequestHandle() |
| 94 : store_(NULL), |
| 95 request_(NULL) {} |
| 96 |
| 97 DTLSIdentityStore::RequestHandle::~RequestHandle() { |
| 98 Cancel(); |
| 99 } |
| 100 |
| 101 void DTLSIdentityStore::RequestHandle::Cancel() { |
| 102 if (request_) { |
| 103 store_->CancelRequest(request_); |
| 104 request_ = NULL; |
| 105 callback_.Reset(); |
| 106 } |
| 107 } |
| 108 |
| 109 void DTLSIdentityStore::RequestHandle::RequestStarted( |
| 110 DTLSIdentityStore* store, |
| 111 DTLSIdentityRequest* request, |
| 112 const CompletionCallback& callback) { |
| 113 DCHECK(!request_); |
| 114 store_ = store; |
| 115 request_ = request; |
| 116 callback_ = callback; |
| 117 } |
| 118 |
| 119 void DTLSIdentityStore::RequestHandle::OnRequestComplete( |
| 120 int error, |
| 121 const std::string& certificate, |
| 122 const std::string& private_key) { |
| 123 request_ = NULL; |
| 124 base::ResetAndReturn(&callback_).Run(error, certificate, private_key); |
| 125 } |
| 126 |
| 127 DTLSIdentityStore::DTLSIdentityStore() |
| 128 : task_runner_(base::WorkerPool::GetTaskRunner(true)) { |
| 129 } |
| 130 |
| 131 DTLSIdentityStore::~DTLSIdentityStore() {} |
| 132 |
| 133 void DTLSIdentityStore::RequestIdentity(const GURL& origin, |
| 134 const std::string& identity_name, |
| 135 const std::string& common_name, |
| 136 const CompletionCallback& callback, |
| 137 RequestHandle* out_request) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 139 // TODO(jiayl): find the cert in the persistent store and generate a new one |
| 140 // only when not found. |
| 141 DTLSIdentityRequest* request = new DTLSIdentityRequest( |
| 142 base::Bind(&RequestHandle::OnRequestComplete, |
| 143 base::Unretained(out_request))); |
| 144 out_request->RequestStarted(this, request, callback); |
| 145 |
| 146 DTLSIdentityRequestResult* result = new DTLSIdentityRequestResult; |
| 147 task_runner_->PostTaskAndReply( |
| 148 FROM_HERE, |
| 149 base::Bind(&GenerateIdentityWorker, common_name, result), |
| 150 base::Bind(&DTLSIdentityRequest::Post, |
| 151 base::Owned(request), base::Owned(result))); |
| 152 } |
| 153 |
| 154 void DTLSIdentityStore::CancelRequest(DTLSIdentityRequest* request) { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 156 request->Cancel(); |
| 157 } |
| 158 |
| 159 DTLSIdentityStore::DTLSIdentityStore( |
| 160 const scoped_refptr<base::TaskRunner>& task_runner) |
| 161 : task_runner_(task_runner) { |
| 162 } |
| 163 |
| 164 } // namespace content |
OLD | NEW |