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/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 WebRTCIdentityRequestResult { |
| 25 int error; |
| 26 std::string certificate; |
| 27 std::string private_key; |
| 28 }; |
| 29 |
| 30 static void GenerateIdentityWorker(const std::string& common_name, |
| 31 WebRTCIdentityRequestResult* 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 // The class represents an identity request internal to WebRTCIdentityStore. |
| 75 // It has a one-to-one mapping to the external version of the request |
| 76 // WebRTCIdentityRequestHandle, which is the target of the |
| 77 // WebRTCIdentityRequest's completion callback. |
| 78 // It's deleted automatically when the request is completed. |
| 79 class WebRTCIdentityRequest { |
| 80 public: |
| 81 WebRTCIdentityRequest(const WebRTCIdentityStore::CompletionCallback& callback) |
| 82 : callback_(callback) {} |
| 83 |
| 84 private: |
| 85 friend class WebRTCIdentityStore; |
| 86 |
| 87 void Cancel() { callback_.Reset(); } |
| 88 |
| 89 void Post(WebRTCIdentityRequestResult* result) { |
| 90 if (callback_.is_null()) |
| 91 return; |
| 92 callback_.Run(result->error, result->certificate, result->private_key); |
| 93 // "this" will be deleted after this point. |
| 94 } |
| 95 |
| 96 WebRTCIdentityStore::CompletionCallback callback_; |
| 97 }; |
| 98 |
| 99 // The class represents an identity request which calls back to the external |
| 100 // client when the request completes. |
| 101 // Its lifetime is tied with the Callback held by the corresponding |
| 102 // WebRTCIdentityRequest. |
| 103 class WebRTCIdentityRequestHandle { |
| 104 public: |
| 105 WebRTCIdentityRequestHandle( |
| 106 WebRTCIdentityStore* store, |
| 107 const WebRTCIdentityStore::CompletionCallback& callback) |
| 108 : store_(store), request_(NULL), callback_(callback) {} |
| 109 |
| 110 private: |
| 111 friend class WebRTCIdentityStore; |
| 112 |
| 113 // Cancel the request. Does nothing if the request finished or was already |
| 114 // cancelled. |
| 115 void Cancel() { |
| 116 if (!request_) |
| 117 return; |
| 118 |
| 119 callback_.Reset(); |
| 120 WebRTCIdentityRequest* request = request_; |
| 121 request_ = NULL; |
| 122 // "this" will be deleted after the following call, becuase "this" is |
| 123 // owned by the Callback held by |request|. |
| 124 store_->CancelRequestInternal(request); |
| 125 } |
| 126 |
| 127 void OnRequestStarted(WebRTCIdentityRequest* request) { |
| 128 DCHECK(request); |
| 129 request_ = request; |
| 130 } |
| 131 void OnRequestComplete(int error, |
| 132 const std::string& certificate, |
| 133 const std::string& private_key) { |
| 134 DCHECK(request_); |
| 135 request_ = NULL; |
| 136 base::ResetAndReturn(&callback_).Run(error, certificate, private_key); |
| 137 } |
| 138 |
| 139 WebRTCIdentityStore* store_; |
| 140 WebRTCIdentityRequest* request_; |
| 141 WebRTCIdentityStore::CompletionCallback callback_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityRequestHandle); |
| 144 }; |
| 145 |
| 146 WebRTCIdentityStore::WebRTCIdentityStore() |
| 147 : task_runner_(base::WorkerPool::GetTaskRunner(true)) {} |
| 148 |
| 149 WebRTCIdentityStore::~WebRTCIdentityStore() {} |
| 150 |
| 151 bool WebRTCIdentityStore::RequestIdentity(const GURL& origin, |
| 152 const std::string& identity_name, |
| 153 const std::string& common_name, |
| 154 const CompletionCallback& callback, |
| 155 base::Closure* cancel_callback) { |
| 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 157 |
| 158 WebRTCIdentityRequestHandle* handle = |
| 159 new WebRTCIdentityRequestHandle(this, callback); |
| 160 |
| 161 WebRTCIdentityRequest* request = new WebRTCIdentityRequest(base::Bind( |
| 162 &WebRTCIdentityRequestHandle::OnRequestComplete, base::Owned(handle))); |
| 163 handle->OnRequestStarted(request); |
| 164 |
| 165 WebRTCIdentityRequestResult* result = new WebRTCIdentityRequestResult; |
| 166 if (!task_runner_->PostTaskAndReply( |
| 167 FROM_HERE, |
| 168 base::Bind(&GenerateIdentityWorker, common_name, result), |
| 169 base::Bind(&WebRTCIdentityRequest::Post, |
| 170 base::Owned(request), |
| 171 base::Owned(result)))) |
| 172 return false; |
| 173 |
| 174 *cancel_callback = base::Bind(&WebRTCIdentityRequestHandle::Cancel, |
| 175 base::Unretained(handle)); |
| 176 return true; |
| 177 } |
| 178 |
| 179 WebRTCIdentityStore::WebRTCIdentityStore( |
| 180 const scoped_refptr<base::TaskRunner>& task_runner) |
| 181 : task_runner_(task_runner) {} |
| 182 |
| 183 void WebRTCIdentityStore::CancelRequestInternal( |
| 184 WebRTCIdentityRequest* request) { |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 186 request->Cancel(); |
| 187 } |
| 188 |
| 189 } // namespace content |
OLD | NEW |