OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" | 5 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 FROM_HERE, | 119 FROM_HERE, |
120 base::Bind(&GetCertDatabaseOnIOThread, | 120 base::Bind(&GetCertDatabaseOnIOThread, |
121 profile->GetResourceContext(), | 121 profile->GetResourceContext(), |
122 callback, | 122 callback, |
123 state)); | 123 state)); |
124 } | 124 } |
125 | 125 |
126 class GenerateRSAKeyState : public NSSOperationState { | 126 class GenerateRSAKeyState : public NSSOperationState { |
127 public: | 127 public: |
128 GenerateRSAKeyState(unsigned int modulus_length, | 128 GenerateRSAKeyState(unsigned int modulus_length, |
| 129 unsigned long public_exponent, |
129 const GenerateKeyCallback& callback); | 130 const GenerateKeyCallback& callback); |
130 virtual ~GenerateRSAKeyState() {} | 131 virtual ~GenerateRSAKeyState() {} |
131 | 132 |
132 virtual void OnError(const tracked_objects::Location& from, | 133 virtual void OnError(const tracked_objects::Location& from, |
133 const std::string& error_message) OVERRIDE { | 134 const std::string& error_message) OVERRIDE { |
134 CallBack(from, std::string() /* no public key */, error_message); | 135 CallBack(from, std::string() /* no public key */, error_message); |
135 } | 136 } |
136 | 137 |
137 void CallBack(const tracked_objects::Location& from, | 138 void CallBack(const tracked_objects::Location& from, |
138 const std::string& public_key_spki_der, | 139 const std::string& public_key_spki_der, |
139 const std::string& error_message) { | 140 const std::string& error_message) { |
140 origin_task_runner_->PostTask( | 141 origin_task_runner_->PostTask( |
141 from, base::Bind(callback_, public_key_spki_der, error_message)); | 142 from, base::Bind(callback_, public_key_spki_der, error_message)); |
142 } | 143 } |
143 | 144 |
144 unsigned int modulus_length_; | 145 const unsigned int modulus_length_; |
| 146 const long public_exponent_; |
145 | 147 |
146 private: | 148 private: |
147 // Must be called on origin thread, use CallBack() therefore. | 149 // Must be called on origin thread, use CallBack() therefore. |
148 GenerateKeyCallback callback_; | 150 GenerateKeyCallback callback_; |
149 }; | 151 }; |
150 | 152 |
151 class SignState : public NSSOperationState { | 153 class SignState : public NSSOperationState { |
152 public: | 154 public: |
153 SignState(const std::string& public_key, | 155 SignState(const std::string& public_key, |
154 const std::string& data, | 156 const std::string& data, |
155 const SignCallback& callback); | 157 const SignCallback& callback); |
156 virtual ~SignState() {} | 158 virtual ~SignState() {} |
157 | 159 |
158 virtual void OnError(const tracked_objects::Location& from, | 160 virtual void OnError(const tracked_objects::Location& from, |
159 const std::string& error_message) OVERRIDE { | 161 const std::string& error_message) OVERRIDE { |
160 CallBack(from, std::string() /* no signature */, error_message); | 162 CallBack(from, std::string() /* no signature */, error_message); |
161 } | 163 } |
162 | 164 |
163 void CallBack(const tracked_objects::Location& from, | 165 void CallBack(const tracked_objects::Location& from, |
164 const std::string& signature, | 166 const std::string& signature, |
165 const std::string& error_message) { | 167 const std::string& error_message) { |
166 origin_task_runner_->PostTask( | 168 origin_task_runner_->PostTask( |
167 from, base::Bind(callback_, signature, error_message)); | 169 from, base::Bind(callback_, signature, error_message)); |
168 } | 170 } |
169 | 171 |
170 std::string public_key_; | 172 const std::string public_key_; |
171 std::string data_; | 173 const std::string data_; |
172 | 174 |
173 private: | 175 private: |
174 // Must be called on origin thread, use CallBack() therefore. | 176 // Must be called on origin thread, use CallBack() therefore. |
175 SignCallback callback_; | 177 SignCallback callback_; |
176 }; | 178 }; |
177 | 179 |
178 class GetCertificatesState : public NSSOperationState { | 180 class GetCertificatesState : public NSSOperationState { |
179 public: | 181 public: |
180 explicit GetCertificatesState(const GetCertificatesCallback& callback); | 182 explicit GetCertificatesState(const GetCertificatesCallback& callback); |
181 virtual ~GetCertificatesState() {} | 183 virtual ~GetCertificatesState() {} |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 private: | 247 private: |
246 // Must be called on origin thread, use CallBack() therefore. | 248 // Must be called on origin thread, use CallBack() therefore. |
247 RemoveCertificateCallback callback_; | 249 RemoveCertificateCallback callback_; |
248 }; | 250 }; |
249 | 251 |
250 NSSOperationState::NSSOperationState() | 252 NSSOperationState::NSSOperationState() |
251 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 253 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
252 } | 254 } |
253 | 255 |
254 GenerateRSAKeyState::GenerateRSAKeyState(unsigned int modulus_length, | 256 GenerateRSAKeyState::GenerateRSAKeyState(unsigned int modulus_length, |
| 257 unsigned long public_exponent, |
255 const GenerateKeyCallback& callback) | 258 const GenerateKeyCallback& callback) |
256 : modulus_length_(modulus_length), callback_(callback) { | 259 : modulus_length_(modulus_length), |
| 260 public_exponent_(public_exponent), |
| 261 callback_(callback) { |
257 } | 262 } |
258 | 263 |
259 SignState::SignState(const std::string& public_key, | 264 SignState::SignState(const std::string& public_key, |
260 const std::string& data, | 265 const std::string& data, |
261 const SignCallback& callback) | 266 const SignCallback& callback) |
262 : public_key_(public_key), data_(data), callback_(callback) { | 267 : public_key_(public_key), data_(data), callback_(callback) { |
263 } | 268 } |
264 | 269 |
265 GetCertificatesState::GetCertificatesState( | 270 GetCertificatesState::GetCertificatesState( |
266 const GetCertificatesCallback& callback) | 271 const GetCertificatesCallback& callback) |
267 : callback_(callback) { | 272 : callback_(callback) { |
268 } | 273 } |
269 | 274 |
270 ImportCertificateState::ImportCertificateState( | 275 ImportCertificateState::ImportCertificateState( |
271 scoped_refptr<net::X509Certificate> certificate, | 276 scoped_refptr<net::X509Certificate> certificate, |
272 const ImportCertificateCallback& callback) | 277 const ImportCertificateCallback& callback) |
273 : certificate_(certificate), callback_(callback) { | 278 : certificate_(certificate), callback_(callback) { |
274 } | 279 } |
275 | 280 |
276 RemoveCertificateState::RemoveCertificateState( | 281 RemoveCertificateState::RemoveCertificateState( |
277 scoped_refptr<net::X509Certificate> certificate, | 282 scoped_refptr<net::X509Certificate> certificate, |
278 const RemoveCertificateCallback& callback) | 283 const RemoveCertificateCallback& callback) |
279 : certificate_(certificate), callback_(callback) { | 284 : certificate_(certificate), callback_(callback) { |
280 } | 285 } |
281 | 286 |
282 // Does the actual key generation on a worker thread. Used by | 287 // Does the actual key generation on a worker thread. Used by |
283 // GenerateRSAKeyWithDB(). | 288 // GenerateRSAKeyWithDB(). |
284 void GenerateRSAKeyOnWorkerThread(scoped_ptr<GenerateRSAKeyState> state) { | 289 void GenerateRSAKeyOnWorkerThread(scoped_ptr<GenerateRSAKeyState> state) { |
285 scoped_ptr<crypto::RSAPrivateKey> rsa_key( | 290 scoped_ptr<crypto::RSAPrivateKey> rsa_key( |
286 crypto::RSAPrivateKey::CreateSensitive(state->slot_.get(), | 291 crypto::RSAPrivateKey::CreateSensitive( |
287 state->modulus_length_)); | 292 state->slot_.get(), state->modulus_length_, state->public_exponent_)); |
288 if (!rsa_key) { | 293 if (!rsa_key) { |
289 LOG(ERROR) << "Couldn't create key."; | 294 LOG(ERROR) << "Couldn't create key."; |
290 state->OnError(FROM_HERE, kErrorInternal); | 295 state->OnError(FROM_HERE, kErrorInternal); |
291 return; | 296 return; |
292 } | 297 } |
293 | 298 |
294 std::vector<uint8> public_key_spki_der; | 299 std::vector<uint8> public_key_spki_der; |
295 if (!rsa_key->ExportPublicKey(&public_key_spki_der)) { | 300 if (!rsa_key->ExportPublicKey(&public_key_spki_der)) { |
296 // TODO(pneubeck): Remove rsa_key from storage. | 301 // TODO(pneubeck): Remove rsa_key from storage. |
297 LOG(ERROR) << "Couldn't export public key."; | 302 LOG(ERROR) << "Couldn't export public key."; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 cert_db->DeleteCertAndKeyAsync( | 468 cert_db->DeleteCertAndKeyAsync( |
464 certificate, | 469 certificate, |
465 base::Bind( | 470 base::Bind( |
466 &DidRemoveCertificate, base::Passed(&state), certificate_found)); | 471 &DidRemoveCertificate, base::Passed(&state), certificate_found)); |
467 } | 472 } |
468 | 473 |
469 } // namespace | 474 } // namespace |
470 | 475 |
471 void GenerateRSAKey(const std::string& token_id, | 476 void GenerateRSAKey(const std::string& token_id, |
472 unsigned int modulus_length, | 477 unsigned int modulus_length, |
| 478 unsigned long public_exponent, |
473 const GenerateKeyCallback& callback, | 479 const GenerateKeyCallback& callback, |
474 Profile* profile) { | 480 Profile* profile) { |
475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
476 scoped_ptr<GenerateRSAKeyState> state( | 482 scoped_ptr<GenerateRSAKeyState> state( |
477 new GenerateRSAKeyState(modulus_length, callback)); | 483 new GenerateRSAKeyState(modulus_length, public_exponent, callback)); |
478 | 484 |
479 if (modulus_length > kMaxRSAModulusLength) { | 485 if (modulus_length > kMaxRSAModulusLength) { |
480 state->OnError(FROM_HERE, kErrorAlgorithmNotSupported); | 486 state->OnError(FROM_HERE, kErrorAlgorithmNotSupported); |
481 return; | 487 return; |
482 } | 488 } |
483 | 489 |
484 // Get the pointer to |state| before base::Passed releases |state|. | 490 // Get the pointer to |state| before base::Passed releases |state|. |
485 NSSOperationState* state_ptr = state.get(); | 491 NSSOperationState* state_ptr = state.get(); |
486 GetCertDatabase(token_id, | 492 GetCertDatabase(token_id, |
487 base::Bind(&GenerateRSAKeyWithDB, base::Passed(&state)), | 493 base::Bind(&GenerateRSAKeyWithDB, base::Passed(&state)), |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 // we would get more informative error messages. | 560 // we would get more informative error messages. |
555 GetCertDatabase(token_id, | 561 GetCertDatabase(token_id, |
556 base::Bind(&RemoveCertificateWithDB, base::Passed(&state)), | 562 base::Bind(&RemoveCertificateWithDB, base::Passed(&state)), |
557 profile, | 563 profile, |
558 state_ptr); | 564 state_ptr); |
559 } | 565 } |
560 | 566 |
561 } // namespace platform_keys | 567 } // namespace platform_keys |
562 | 568 |
563 } // namespace chromeos | 569 } // namespace chromeos |
OLD | NEW |