Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Unified Diff: content/browser/media/webrtc_identity_store.cc

Issue 15969025: Generates the DTLS identity in browser process and returns it to render process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/media/webrtc_identity_store.cc
diff --git a/content/browser/media/webrtc_identity_store.cc b/content/browser/media/webrtc_identity_store.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c4b688fe5560aeef798f7ee31d0b36c1476aeaee
--- /dev/null
+++ b/content/browser/media/webrtc_identity_store.cc
@@ -0,0 +1,190 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/media/webrtc_identity_store.h"
+
+#include "base/bind.h"
+#include "base/callback_helpers.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/rand_util.h"
+#include "base/task_runner.h"
+#include "base/threading/worker_pool.h"
+#include "content/public/browser/browser_thread.h"
+#include "crypto/rsa_private_key.h"
+#include "googleurl/src/gurl.h"
+#include "net/base/net_errors.h"
+#include "net/cert/x509_certificate.h"
+
+namespace content {
+
+namespace {
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 this isn't useful (and makes debug tooling sadder)
+
+struct WebRTCIdentityRequestResult {
+ int error;
+ std::string certificate;
+ std::string private_key;
+};
+
+static void GenerateIdentityWorker(const std::string& common_name,
+ WebRTCIdentityRequestResult* result) {
+ result->error = net::OK;
+ std::string certificate;
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 unused
+ std::vector<uint8> private_key_info;
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 can move to just before first use (l.62).
+
+ int serial_number = base::RandInt(0, std::numeric_limits<int>::max());
+
+ scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(1024));
+ if (!key.get()) {
+ DLOG(ERROR) << "Unable to create key pair for client";
+ result->error = net::ERR_KEY_GENERATION_FAILED;
+ return;
+ }
+
+ scoped_refptr<net::X509Certificate> cert =
+ net::X509Certificate::CreateSelfSigned(key.get(),
+ "CN=" + common_name,
+ serial_number,
+ base::TimeDelta::FromDays(30));
+ if (!cert) {
+ DLOG(ERROR) << "Unable to create x509 cert for client";
+ result->error = net::ERR_SELF_SIGNED_CERT_GENERATION_FAILED;
+ return;
+ }
+ if (!net::X509Certificate::GetDEREncoded(cert->os_cert_handle(),
+ &result->certificate)) {
+ DLOG(ERROR) << "Unable to get the DER decoded data from the cert.";
+ result->error = net::ERR_SELF_SIGNED_CERT_GENERATION_FAILED;
+ return;
+ }
+
+ if (!key->ExportPrivateKey(&private_key_info)) {
+ DLOG(ERROR) << "Unable to export private key";
+ result->error = net::ERR_PRIVATE_KEY_EXPORT_FAILED;
+ return;
+ }
+
+ result->private_key =
+ std::string(private_key_info.begin(), private_key_info.end());
+}
+
+} // namespace
+
+// The class represents an identity request internal to WebRTCIdentityStore.
+// It has a one-to-one mapping to the external version of the request
+// WebRTCIdentityRequestHandle, which is the target of the
+// WebRTCIdentityRequest's completion callback.
+// It's deleted automatically when the request is completed.
+class WebRTCIdentityRequest {
+ public:
+ WebRTCIdentityRequest(const WebRTCIdentityStore::CompletionCallback& callback)
+ : callback_(callback) {}
+
+ private:
+ friend class WebRTCIdentityStore;
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 This is a strange combination of lines considering
jiayl 2013/06/27 21:08:37 This is to make sure WebRTCIdentityRequestHandle c
+
+ void Cancel() { callback_.Reset(); }
+
+ void Post(WebRTCIdentityRequestResult* result) {
+ if (callback_.is_null())
+ return;
+ callback_.Run(result->error, result->certificate, result->private_key);
+ // "this" will be deleted after this point.
+ }
+
+ WebRTCIdentityStore::CompletionCallback callback_;
+};
+
+// The class represents an identity request which calls back to the external
+// client when the request completes.
+// Its lifetime is tied with the Callback held by the corresponding
+// WebRTCIdentityRequest.
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 I'm confused as to why this class exists. Why not
jiayl 2013/06/27 21:08:37 When the certificate caching is in place, we'll wa
Ami GONE FROM CHROMIUM 2013/06/27 21:30:32 I don't quite get this, and in general I prefer to
+class WebRTCIdentityRequestHandle {
+ public:
+ WebRTCIdentityRequestHandle(
+ WebRTCIdentityStore* store,
+ const WebRTCIdentityStore::CompletionCallback& callback)
+ : store_(store), request_(NULL), callback_(callback) {}
+
+ private:
+ friend class WebRTCIdentityStore;
+
+ // Cancel the request. Does nothing if the request finished or was already
+ // cancelled.
+ void Cancel() {
+ if (!request_)
+ return;
+
+ callback_.Reset();
+ WebRTCIdentityRequest* request = request_;
+ request_ = NULL;
+ // "this" will be deleted after the following call, becuase "this" is
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 typo: becuase
jiayl 2013/06/27 21:08:37 Done.
+ // owned by the Callback held by |request|.
+ store_->CancelRequestInternal(request);
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 Why go through store_ for this instead of simply c
+ }
+
+ void OnRequestStarted(WebRTCIdentityRequest* request) {
+ DCHECK(request);
+ request_ = request;
+ }
+
+ void OnRequestComplete(int error,
+ const std::string& certificate,
+ const std::string& private_key) {
+ DCHECK(request_);
+ request_ = NULL;
+ base::ResetAndReturn(&callback_).Run(error, certificate, private_key);
+ }
+
+ WebRTCIdentityStore* store_;
+ WebRTCIdentityRequest* request_;
+ WebRTCIdentityStore::CompletionCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityRequestHandle);
+};
+
+WebRTCIdentityStore::WebRTCIdentityStore()
+ : task_runner_(base::WorkerPool::GetTaskRunner(true)) {}
+
+WebRTCIdentityStore::~WebRTCIdentityStore() {}
+
+bool WebRTCIdentityStore::RequestIdentity(const GURL& origin,
+ const std::string& identity_name,
+ const std::string& common_name,
+ const CompletionCallback& callback,
+ base::Closure* cancel_callback) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 I think it is true that all the methods in this cl
jiayl 2013/06/27 21:08:37 Done.
+
+ WebRTCIdentityRequestHandle* handle =
+ new WebRTCIdentityRequestHandle(this, callback);
+
+ WebRTCIdentityRequest* request = new WebRTCIdentityRequest(base::Bind(
+ &WebRTCIdentityRequestHandle::OnRequestComplete, base::Owned(handle)));
+ handle->OnRequestStarted(request);
+
+ WebRTCIdentityRequestResult* result = new WebRTCIdentityRequestResult;
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 nit: always include the ()'s on a new expression.
jiayl 2013/06/27 21:08:37 Done.
+ if (!task_runner_->PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&GenerateIdentityWorker, common_name, result),
+ base::Bind(&WebRTCIdentityRequest::Post,
+ base::Owned(request),
+ base::Owned(result))))
+ return false;
+
+ *cancel_callback = base::Bind(&WebRTCIdentityRequestHandle::Cancel,
+ base::Unretained(handle));
+ return true;
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 fwiw: since this method returns true iff *cancel_c
jiayl 2013/06/27 21:08:37 Done.
+}
+
+WebRTCIdentityStore::WebRTCIdentityStore(
+ const scoped_refptr<base::TaskRunner>& task_runner)
+ : task_runner_(task_runner) {}
+
+void WebRTCIdentityStore::CancelRequestInternal(
+ WebRTCIdentityRequest* request) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ request->Cancel();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698