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

Unified Diff: content/browser/media/dtls_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/dtls_identity_store.cc
diff --git a/content/browser/media/dtls_identity_store.cc b/content/browser/media/dtls_identity_store.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4681631791e2fc65974579802d37f267cc680300
--- /dev/null
+++ b/content/browser/media/dtls_identity_store.cc
@@ -0,0 +1,107 @@
+// 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/dtls_identity_store.h"
+
+#include <cert.h>
+
+#include "base/bind.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/ec_private_key.h"
+#include "googleurl/src/gurl.h"
+#include "net/cert/x509_util_nss.h"
+
+namespace {
jam 2013/06/06 16:22:32 put this in the content namespace as well, so that
jiayl 2013/06/06 17:07:37 Done.
+
+static void GenerateIdentityWorker(
+ const GURL& origin,
+ const std::string& identity_name,
+ const std::string& common_name,
+ const content::DTLSIdentityStore::OnCompleteCallback& callback) {
+ std::string certificate;
+ std::vector<uint8> private_key_info;
+
+ int serial_number = base::RandInt(0, std::numeric_limits<int>::max());
+ base::Time not_valid_before = base::Time::Now();
+ base::Time not_valid_after = not_valid_before + base::TimeDelta::FromDays(30);
+
+ scoped_ptr<crypto::ECPrivateKey> key(crypto::ECPrivateKey::Create());
+ if (!key.get()) {
+ DLOG(ERROR) << "Unable to create key pair for client";
+ return;
+ }
+
+ CERTCertificate* cert =
+ net::x509_util::CreateSelfSignedCert(key->public_key(),
+ key->key(),
+ "CN=" + common_name,
+ serial_number,
+ not_valid_before,
+ not_valid_after);
+ if (cert == NULL) {
+ DLOG(ERROR) << "Unable to create x509 cert for client";
+ return;
+ }
+ certificate.append(reinterpret_cast<char*>(cert->derCert.data),
+ cert->derCert.len);
+ CERT_DestroyCertificate(cert);
+ cert = NULL;
+
+ if (!key->ExportEncryptedPrivateKey("", 1, &private_key_info)) {
+ DLOG(ERROR) << "Unable to export private key";
+ return;
+ }
+
+ std::string priveta_key(private_key_info.begin(), private_key_info.end());
+
+ LOG(INFO) << "DTLSIdentityStore: a new identity is gnerated.";
jam 2013/06/06 16:22:32 here and below, does this need to be LOG and not D
jiayl 2013/06/06 17:07:37 Done.
+ content::BrowserThread::PostTask(
+ content::BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(callback, certificate, priveta_key));
+}
+
+} // namespace
+
+namespace content {
+
+DTLSIdentityStore* DTLSIdentityStore::GetInstance() {
+ return Singleton<DTLSIdentityStore>::get();
+}
+
+DTLSIdentityStore::DTLSIdentityStore()
+ : task_runner_(base::WorkerPool::GetTaskRunner(true)) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+}
+
+DTLSIdentityStore::DTLSIdentityStore(
+ const scoped_refptr<base::TaskRunner>& task_runner)
+ : task_runner_(task_runner) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+}
+
+DTLSIdentityStore::~DTLSIdentityStore() {}
+
+void DTLSIdentityStore::GetOrGenerateIdentity(
+ const GURL& origin,
+ const std::string& identity_name,
+ const std::string& common_name,
+ const OnCompleteCallback& callback) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ LOG(INFO) << "DTLSIdentityStore: start generating identity.";
+ // TODO(jiayl): find the cert in the persistent store and generate a new one
+ // only when not found.
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&GenerateIdentityWorker,
+ origin, identity_name, common_name, callback));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698