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

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

Powered by Google App Engine
This is Rietveld 408576698