Chromium Code Reviews| 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)); | |
|
Ryan Sleevi
2013/06/17 23:08:34
DESIGN BUG: This forces WebRTC keys into the TPM o
jiayl
2013/06/20 01:04:03
Since Ryan has started two other CLs to address th
| |
| 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), | |
| 111 request_(NULL), | |
| 112 callback_(callback) {} | |
| 113 | |
| 114 private: | |
| 115 friend DTLSIdentityStore; | |
| 116 | |
| 117 // Cancel the request. Does nothing if the request finished or was already | |
| 118 // cancelled. | |
| 119 void Cancel() { | |
| 120 if (!request_) | |
| 121 return; | |
| 122 | |
| 123 callback_.Reset(); | |
| 124 DTLSIdentityRequest* request = request_; | |
| 125 request_ = NULL; | |
| 126 // "this" will be deleted after the following call, becuase "this" is | |
| 127 // owned by the Callback held by |request|. | |
| 128 store_->CancelRequestInternal(request); | |
| 129 } | |
| 130 | |
| 131 void OnRequestStarted(DTLSIdentityRequest* request) { | |
| 132 DCHECK(request); | |
| 133 request_ = request; | |
| 134 } | |
| 135 void OnRequestComplete(int error, | |
| 136 const std::string& certificate, | |
| 137 const std::string& private_key) { | |
| 138 DCHECK(request_); | |
| 139 request_ = NULL; | |
| 140 base::ResetAndReturn(&callback_).Run(error, certificate, private_key); | |
| 141 } | |
| 142 | |
| 143 DTLSIdentityStore* store_; | |
| 144 DTLSIdentityRequest* request_; | |
| 145 DTLSIdentityStore::CompletionCallback callback_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(DTLSIdentityRequestHandle); | |
| 148 }; | |
| 149 | |
| 150 DTLSIdentityStore::DTLSIdentityStore() | |
| 151 : task_runner_(base::WorkerPool::GetTaskRunner(true)) {} | |
|
jam
2013/06/18 00:03:18
why are you using base::WorkerPool instead of Sequ
jiayl
2013/06/18 01:07:55
It's not necessary to enforce the an order on the
jam
2013/06/18 23:22:45
sequencing is not the only reason to use sequenced
| |
| 152 | |
| 153 DTLSIdentityStore::~DTLSIdentityStore() {} | |
| 154 | |
| 155 bool DTLSIdentityStore::RequestIdentity(const GURL& origin, | |
| 156 const std::string& identity_name, | |
| 157 const std::string& common_name, | |
| 158 const CompletionCallback& callback, | |
| 159 base::Closure* canceller) { | |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 161 | |
| 162 DTLSIdentityRequestHandle* handle = new DTLSIdentityRequestHandle( | |
| 163 this, callback); | |
| 164 | |
| 165 DTLSIdentityRequest* request = new DTLSIdentityRequest( | |
| 166 base::Bind(&DTLSIdentityRequestHandle::OnRequestComplete, | |
| 167 base::Owned(handle))); | |
| 168 handle->OnRequestStarted(request); | |
| 169 | |
| 170 DTLSIdentityRequestResult* result = new DTLSIdentityRequestResult; | |
| 171 bool posted = task_runner_->PostTaskAndReply( | |
| 172 FROM_HERE, | |
| 173 base::Bind(&GenerateIdentityWorker, common_name, result), | |
| 174 base::Bind(&DTLSIdentityRequest::Post, | |
| 175 base::Owned(request), base::Owned(result))); | |
| 176 | |
| 177 if (posted && canceller) { | |
|
Ryan Sleevi
2013/06/17 23:08:34
This check indicates that the caller need not supp
jiayl
2013/06/18 01:07:55
Done.
| |
| 178 *canceller = base::Bind(&DTLSIdentityRequestHandle::Cancel, | |
| 179 base::Unretained(handle)); | |
| 180 } | |
| 181 return posted; | |
| 182 } | |
| 183 | |
| 184 DTLSIdentityStore::DTLSIdentityStore( | |
| 185 const scoped_refptr<base::TaskRunner>& task_runner) | |
| 186 : task_runner_(task_runner) { | |
| 187 } | |
| 188 | |
| 189 void DTLSIdentityStore::CancelRequestInternal(DTLSIdentityRequest* request) { | |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 191 request->Cancel(); | |
| 192 } | |
| 193 | |
| 194 } // namespace content | |
| OLD | NEW |