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/webrtc_identity_store.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" |
| 11 #include "base/threading/worker_pool.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "crypto/rsa_private_key.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/net_errors.h" |
| 16 #include "net/cert/x509_util.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 struct WebRTCIdentityRequestResult { |
| 21 int error; |
| 22 std::string certificate; |
| 23 std::string private_key; |
| 24 }; |
| 25 |
| 26 static void GenerateIdentityWorker(const std::string& common_name, |
| 27 WebRTCIdentityRequestResult* result) { |
| 28 result->error = net::OK; |
| 29 int serial_number = base::RandInt(0, std::numeric_limits<int>::max()); |
| 30 |
| 31 scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(1024)); |
| 32 if (!key.get()) { |
| 33 DLOG(ERROR) << "Unable to create key pair for client"; |
| 34 result->error = net::ERR_KEY_GENERATION_FAILED; |
| 35 return; |
| 36 } |
| 37 |
| 38 base::Time now = base::Time::Now(); |
| 39 bool success = |
| 40 net::x509_util::CreateSelfSignedCert(key.get(), |
| 41 "CN=" + common_name, |
| 42 serial_number, |
| 43 now, |
| 44 now + base::TimeDelta::FromDays(30), |
| 45 &result->certificate); |
| 46 if (!success) { |
| 47 DLOG(ERROR) << "Unable to create x509 cert for client"; |
| 48 result->error = net::ERR_SELF_SIGNED_CERT_GENERATION_FAILED; |
| 49 return; |
| 50 } |
| 51 |
| 52 std::vector<uint8> private_key_info; |
| 53 if (!key->ExportPrivateKey(&private_key_info)) { |
| 54 DLOG(ERROR) << "Unable to export private key"; |
| 55 result->error = net::ERR_PRIVATE_KEY_EXPORT_FAILED; |
| 56 return; |
| 57 } |
| 58 |
| 59 result->private_key = |
| 60 std::string(private_key_info.begin(), private_key_info.end()); |
| 61 } |
| 62 |
| 63 // The class represents an identity request internal to WebRTCIdentityStore. |
| 64 // It has a one-to-one mapping to the external version of the request |
| 65 // WebRTCIdentityRequestHandle, which is the target of the |
| 66 // WebRTCIdentityRequest's completion callback. |
| 67 // It's deleted automatically when the request is completed. |
| 68 class WebRTCIdentityRequest { |
| 69 public: |
| 70 WebRTCIdentityRequest(const WebRTCIdentityStore::CompletionCallback& callback) |
| 71 : callback_(callback) {} |
| 72 |
| 73 void Cancel() { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 75 callback_.Reset(); |
| 76 } |
| 77 |
| 78 private: |
| 79 friend class WebRTCIdentityStore; |
| 80 |
| 81 void Post(WebRTCIdentityRequestResult* result) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 83 if (callback_.is_null()) |
| 84 return; |
| 85 callback_.Run(result->error, result->certificate, result->private_key); |
| 86 // "this" will be deleted after this point. |
| 87 } |
| 88 |
| 89 WebRTCIdentityStore::CompletionCallback callback_; |
| 90 }; |
| 91 |
| 92 // The class represents an identity request which calls back to the external |
| 93 // client when the request completes. |
| 94 // Its lifetime is tied with the Callback held by the corresponding |
| 95 // WebRTCIdentityRequest. |
| 96 class WebRTCIdentityRequestHandle { |
| 97 public: |
| 98 WebRTCIdentityRequestHandle( |
| 99 WebRTCIdentityStore* store, |
| 100 const WebRTCIdentityStore::CompletionCallback& callback) |
| 101 : store_(store), request_(NULL), callback_(callback) {} |
| 102 |
| 103 private: |
| 104 friend class WebRTCIdentityStore; |
| 105 |
| 106 // Cancel the request. Does nothing if the request finished or was already |
| 107 // cancelled. |
| 108 void Cancel() { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 110 if (!request_) |
| 111 return; |
| 112 |
| 113 callback_.Reset(); |
| 114 WebRTCIdentityRequest* request = request_; |
| 115 request_ = NULL; |
| 116 // "this" will be deleted after the following call, because "this" is |
| 117 // owned by the Callback held by |request|. |
| 118 request->Cancel(); |
| 119 } |
| 120 |
| 121 void OnRequestStarted(WebRTCIdentityRequest* request) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 123 DCHECK(request); |
| 124 request_ = request; |
| 125 } |
| 126 |
| 127 void OnRequestComplete(int error, |
| 128 const std::string& certificate, |
| 129 const std::string& private_key) { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 131 DCHECK(request_); |
| 132 request_ = NULL; |
| 133 base::ResetAndReturn(&callback_).Run(error, certificate, private_key); |
| 134 } |
| 135 |
| 136 WebRTCIdentityStore* store_; |
| 137 WebRTCIdentityRequest* request_; |
| 138 WebRTCIdentityStore::CompletionCallback callback_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityRequestHandle); |
| 141 }; |
| 142 |
| 143 WebRTCIdentityStore::WebRTCIdentityStore() |
| 144 : task_runner_(base::WorkerPool::GetTaskRunner(true)) {} |
| 145 |
| 146 WebRTCIdentityStore::~WebRTCIdentityStore() {} |
| 147 |
| 148 base::Closure WebRTCIdentityStore::RequestIdentity( |
| 149 const GURL& origin, |
| 150 const std::string& identity_name, |
| 151 const std::string& common_name, |
| 152 const CompletionCallback& callback) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 154 |
| 155 WebRTCIdentityRequestHandle* handle = |
| 156 new WebRTCIdentityRequestHandle(this, callback); |
| 157 |
| 158 WebRTCIdentityRequest* request = new WebRTCIdentityRequest(base::Bind( |
| 159 &WebRTCIdentityRequestHandle::OnRequestComplete, base::Owned(handle))); |
| 160 handle->OnRequestStarted(request); |
| 161 |
| 162 WebRTCIdentityRequestResult* result = new WebRTCIdentityRequestResult(); |
| 163 if (!task_runner_->PostTaskAndReply( |
| 164 FROM_HERE, |
| 165 base::Bind(&GenerateIdentityWorker, common_name, result), |
| 166 base::Bind(&WebRTCIdentityRequest::Post, |
| 167 base::Owned(request), |
| 168 base::Owned(result)))) |
| 169 return base::Closure(); |
| 170 |
| 171 return base::Bind(&WebRTCIdentityRequestHandle::Cancel, |
| 172 base::Unretained(handle)); |
| 173 } |
| 174 |
| 175 void WebRTCIdentityStore::SetTaskRunnerForTesting( |
| 176 const scoped_refptr<base::TaskRunner>& task_runner) { |
| 177 task_runner_ = task_runner; |
| 178 } |
| 179 |
| 180 } // namespace content |
OLD | NEW |