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

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 namespace {
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 this isn't useful (and makes debug tooling sadder)
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;
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 unused
34 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).
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;
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
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.
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
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
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 typo: becuase
jiayl 2013/06/27 21:08:37 Done.
123 // owned by the Callback held by |request|.
124 store_->CancelRequestInternal(request);
Ami GONE FROM CHROMIUM 2013/06/27 20:05:18 Why go through store_ for this instead of simply c
125 }
126
127 void OnRequestStarted(WebRTCIdentityRequest* request) {
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(request_);
136 request_ = NULL;
137 base::ResetAndReturn(&callback_).Run(error, certificate, private_key);
138 }
139
140 WebRTCIdentityStore* store_;
141 WebRTCIdentityRequest* request_;
142 WebRTCIdentityStore::CompletionCallback callback_;
143
144 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityRequestHandle);
145 };
146
147 WebRTCIdentityStore::WebRTCIdentityStore()
148 : task_runner_(base::WorkerPool::GetTaskRunner(true)) {}
149
150 WebRTCIdentityStore::~WebRTCIdentityStore() {}
151
152 bool WebRTCIdentityStore::RequestIdentity(const GURL& origin,
153 const std::string& identity_name,
154 const std::string& common_name,
155 const CompletionCallback& callback,
156 base::Closure* cancel_callback) {
157 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.
158
159 WebRTCIdentityRequestHandle* handle =
160 new WebRTCIdentityRequestHandle(this, callback);
161
162 WebRTCIdentityRequest* request = new WebRTCIdentityRequest(base::Bind(
163 &WebRTCIdentityRequestHandle::OnRequestComplete, base::Owned(handle)));
164 handle->OnRequestStarted(request);
165
166 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.
167 if (!task_runner_->PostTaskAndReply(
168 FROM_HERE,
169 base::Bind(&GenerateIdentityWorker, common_name, result),
170 base::Bind(&WebRTCIdentityRequest::Post,
171 base::Owned(request),
172 base::Owned(result))))
173 return false;
174
175 *cancel_callback = base::Bind(&WebRTCIdentityRequestHandle::Cancel,
176 base::Unretained(handle));
177 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.
178 }
179
180 WebRTCIdentityStore::WebRTCIdentityStore(
181 const scoped_refptr<base::TaskRunner>& task_runner)
182 : task_runner_(task_runner) {}
183
184 void WebRTCIdentityStore::CancelRequestInternal(
185 WebRTCIdentityRequest* request) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
187 request->Cancel();
188 }
189
190 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698