OLD | NEW |
---|---|
(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 DTLSIdentityStore; | |
86 | |
87 void Cancel() { | |
88 callback_.Reset(); | |
89 } | |
90 | |
91 void Post(DTLSIdentityRequestResult* result) { | |
92 if (callback_.is_null()) | |
93 return; | |
94 callback_.Run(result->error, result->certificate, result->private_key); | |
95 // "this" will be deleted after this point. | |
96 } | |
97 | |
98 DTLSIdentityStore::CompletionCallback callback_; | |
99 }; | |
100 | |
101 // The class represents a DTLS identity request which calls back to the external | |
102 // client when the request completes. | |
103 // Its lifetime is tied with the Callback held by the corresponding | |
104 // DTLSIdentityRequest. | |
105 class DTLSIdentityRequestHandle { | |
106 public: | |
107 DTLSIdentityRequestHandle( | |
108 DTLSIdentityStore* store, | |
109 const DTLSIdentityStore::CompletionCallback& callback) : | |
110 store_(store), request_(NULL), callback_(callback) { | |
Ryan Sleevi
2013/06/17 18:13:23
STYLE: Please see http://google-styleguide.googlec
jiayl
2013/06/17 18:58:00
Done.
| |
111 } | |
112 | |
113 private: | |
114 friend DTLSIdentityStore; | |
115 | |
116 // Cancel the request. Does nothing if the request finished or was already | |
117 // cancelled. | |
118 void Cancel() { | |
119 if (request_) { | |
Ryan Sleevi
2013/06/17 18:13:23
Try to handle errors immediately, to reduce the le
jiayl
2013/06/17 18:58:00
Done.
| |
120 callback_.Reset(); | |
121 DTLSIdentityRequest* request = request_; | |
122 request_ = NULL; | |
123 // "this" will be deleted after the following call, becuase "this" is | |
124 // owned by the Callback held by |request|. | |
125 store_->CancelRequestInternal(request); | |
126 } | |
127 } | |
128 | |
129 void OnRequestStarted(DTLSIdentityRequest* request) { | |
130 DCHECK_NE(static_cast<DTLSIdentityRequest*>(NULL), request); | |
Ryan Sleevi
2013/06/17 18:13:23
DCHECK(request)
jiayl
2013/06/17 18:58:00
Done.
| |
131 request_ = request; | |
132 } | |
133 void OnRequestComplete(int error, | |
134 const std::string& certificate, | |
135 const std::string& private_key) { | |
136 DCHECK_NE(static_cast<DTLSIdentityRequest*>(NULL), request_); | |
137 request_ = NULL; | |
138 base::ResetAndReturn(&callback_).Run(error, certificate, private_key); | |
139 } | |
140 | |
141 DTLSIdentityStore* store_; | |
142 DTLSIdentityRequest* request_; | |
143 DTLSIdentityStore::CompletionCallback callback_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(DTLSIdentityRequestHandle); | |
146 }; | |
147 | |
148 DTLSIdentityStore::DTLSIdentityStore() | |
149 : task_runner_(base::WorkerPool::GetTaskRunner(true)) { | |
150 } | |
151 | |
152 DTLSIdentityStore::~DTLSIdentityStore() {} | |
153 | |
154 base::Closure DTLSIdentityStore::RequestIdentity(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 DTLSIdentityRequestHandle* handle = new DTLSIdentityRequestHandle( | |
161 this, callback); | |
162 | |
163 DTLSIdentityRequest* request = new DTLSIdentityRequest( | |
164 base::Bind(&DTLSIdentityRequestHandle::OnRequestComplete, | |
165 base::Owned(handle))); | |
166 handle->OnRequestStarted(request); | |
167 | |
168 DTLSIdentityRequestResult* result = new DTLSIdentityRequestResult; | |
169 task_runner_->PostTaskAndReply( | |
Ryan Sleevi
2013/06/17 18:13:23
SECURITY BUG:
If this fails to Post, then |request
jiayl
2013/06/17 18:58:00
Fixed now by changing the return type to boolean a
| |
170 FROM_HERE, | |
171 base::Bind(&GenerateIdentityWorker, common_name, result), | |
172 base::Bind(&DTLSIdentityRequest::Post, | |
173 base::Owned(request), base::Owned(result))); | |
174 | |
175 return base::Bind(&DTLSIdentityRequestHandle::Cancel, | |
176 base::Unretained(handle)); | |
177 } | |
178 | |
179 DTLSIdentityStore::DTLSIdentityStore( | |
180 const scoped_refptr<base::TaskRunner>& task_runner) | |
181 : task_runner_(task_runner) { | |
182 } | |
183 | |
184 void DTLSIdentityStore::CancelRequestInternal(DTLSIdentityRequest* request) { | |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
186 request->Cancel(); | |
187 } | |
188 | |
189 } // namespace content | |
OLD | NEW |