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

Side by Side 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 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/dtls_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 DTLSIdentityRequestResult {
25 int error;
26 std::string certificate;
27 std::string private_key;
28 };
29
30 static void GenerateIdentityWorker(const std::string& common_name,
31 DTLSIdentityRequestResult* 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 a DTLS identity request internal to DTLSIdentityStore.
75 // It has a one-to-one mapping to the external version of the request
76 // DTLSIdentityRequestHandle, which is the target of the DTLSIdentityRequest's
77 // completion callback.
78 // It's deleted automatically when the request is completed.
79 class DTLSIdentityRequest {
80 public:
81 DTLSIdentityRequest(const DTLSIdentityStore::CompletionCallback& callback)
82 : callback_(callback) {}
83
84 private:
85 friend class DTLSIdentityStore;
86
87 void Cancel() { callback_.Reset(); }
88
89 void Post(DTLSIdentityRequestResult* 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 DTLSIdentityStore::CompletionCallback callback_;
97 };
98
99 // The class represents a DTLS 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 // DTLSIdentityRequest.
103 class DTLSIdentityRequestHandle {
104 public:
105 DTLSIdentityRequestHandle(
106 DTLSIdentityStore* store,
107 const DTLSIdentityStore::CompletionCallback& callback)
108 : store_(store), request_(NULL), callback_(callback) {}
Ryan Sleevi 2013/06/24 19:29:16 Was this generated with clang-format? My understa
jiayl 2013/06/25 20:36:32 This is generated from git-cl format, which calls
109
110 private:
111 friend class DTLSIdentityStore;
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 DTLSIdentityRequest* 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(DTLSIdentityRequest* 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 DTLSIdentityStore* store_;
140 DTLSIdentityRequest* request_;
141 DTLSIdentityStore::CompletionCallback callback_;
142
143 DISALLOW_COPY_AND_ASSIGN(DTLSIdentityRequestHandle);
144 };
145
146 DTLSIdentityStore::DTLSIdentityStore()
147 : task_runner_(base::WorkerPool::GetTaskRunner(true)) {}
148
149 DTLSIdentityStore::~DTLSIdentityStore() {}
150
151 bool DTLSIdentityStore::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 DTLSIdentityRequestHandle* handle =
159 new DTLSIdentityRequestHandle(this, callback);
160
161 DTLSIdentityRequest* request = new DTLSIdentityRequest(base::Bind(
162 &DTLSIdentityRequestHandle::OnRequestComplete, base::Owned(handle)));
163 handle->OnRequestStarted(request);
164
165 DTLSIdentityRequestResult* result = new DTLSIdentityRequestResult;
166 bool posted = task_runner_->PostTaskAndReply(
167 FROM_HERE,
168 base::Bind(&GenerateIdentityWorker, common_name, result),
169 base::Bind(&DTLSIdentityRequest::Post,
170 base::Owned(request),
171 base::Owned(result)));
172
Ryan Sleevi 2013/06/24 19:29:16 style nit: Chromium style generally handles the er
jiayl 2013/06/25 20:36:32 Done.
173 if (posted) {
174 *cancel_callback = base::Bind(&DTLSIdentityRequestHandle::Cancel,
175 base::Unretained(handle));
176 }
177 return posted;
178 }
179
180 DTLSIdentityStore::DTLSIdentityStore(
181 const scoped_refptr<base::TaskRunner>& task_runner)
182 : task_runner_(task_runner) {}
183
184 void DTLSIdentityStore::CancelRequestInternal(DTLSIdentityRequest* request) {
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
186 request->Cancel();
187 }
188
189 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698