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 19 matching lines...) Expand all Loading... |
30 using content::BrowserThread; | 30 using content::BrowserThread; |
31 | 31 |
32 namespace { | 32 namespace { |
33 const char kErrorInternal[] = "Internal Error."; | 33 const char kErrorInternal[] = "Internal Error."; |
34 const char kErrorKeyNotFound[] = "Key not found."; | 34 const char kErrorKeyNotFound[] = "Key not found."; |
35 const char kErrorCertificateNotFound[] = "Certificate could not be found."; | 35 const char kErrorCertificateNotFound[] = "Certificate could not be found."; |
36 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported."; | 36 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported."; |
37 | 37 |
38 // The current maximal RSA modulus length that ChromeOS's TPM supports for key | 38 // The current maximal RSA modulus length that ChromeOS's TPM supports for key |
39 // generation. | 39 // generation. |
40 const unsigned int kMaxRSAModulusLength = 2048; | 40 const unsigned int kMaxRSAModulusLengthBits = 2048; |
41 } | 41 } |
42 | 42 |
43 namespace chromeos { | 43 namespace chromeos { |
44 | 44 |
45 namespace platform_keys { | 45 namespace platform_keys { |
46 | 46 |
47 namespace { | 47 namespace { |
48 | 48 |
49 // Base class to store state that is common to all NSS database operations and | 49 // Base class to store state that is common to all NSS database operations and |
50 // to provide convenience methods to call back. | 50 // to provide convenience methods to call back. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 BrowserThread::PostTask(BrowserThread::IO, | 118 BrowserThread::PostTask(BrowserThread::IO, |
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_bits, |
129 const GenerateKeyCallback& callback); | 129 const subtle::GenerateKeyCallback& callback); |
130 virtual ~GenerateRSAKeyState() {} | 130 virtual ~GenerateRSAKeyState() {} |
131 | 131 |
132 virtual void OnError(const tracked_objects::Location& from, | 132 virtual void OnError(const tracked_objects::Location& from, |
133 const std::string& error_message) OVERRIDE { | 133 const std::string& error_message) OVERRIDE { |
134 CallBack(from, std::string() /* no public key */, error_message); | 134 CallBack(from, std::string() /* no public key */, error_message); |
135 } | 135 } |
136 | 136 |
137 void CallBack(const tracked_objects::Location& from, | 137 void CallBack(const tracked_objects::Location& from, |
138 const std::string& public_key_spki_der, | 138 const std::string& public_key_spki_der, |
139 const std::string& error_message) { | 139 const std::string& error_message) { |
140 origin_task_runner_->PostTask( | 140 origin_task_runner_->PostTask( |
141 from, base::Bind(callback_, public_key_spki_der, error_message)); | 141 from, base::Bind(callback_, public_key_spki_der, error_message)); |
142 } | 142 } |
143 | 143 |
144 unsigned int modulus_length_; | 144 unsigned int modulus_length_bits_; |
145 | 145 |
146 private: | 146 private: |
147 // Must be called on origin thread, use CallBack() therefore. | 147 // Must be called on origin thread, use CallBack() therefore. |
148 GenerateKeyCallback callback_; | 148 subtle::GenerateKeyCallback callback_; |
149 }; | 149 }; |
150 | 150 |
151 class SignState : public NSSOperationState { | 151 class SignState : public NSSOperationState { |
152 public: | 152 public: |
153 SignState(const std::string& public_key, | 153 SignState(const std::string& public_key, |
154 const std::string& data, | 154 const std::string& data, |
155 const SignCallback& callback); | 155 const subtle::SignCallback& callback); |
156 virtual ~SignState() {} | 156 virtual ~SignState() {} |
157 | 157 |
158 virtual void OnError(const tracked_objects::Location& from, | 158 virtual void OnError(const tracked_objects::Location& from, |
159 const std::string& error_message) OVERRIDE { | 159 const std::string& error_message) OVERRIDE { |
160 CallBack(from, std::string() /* no signature */, error_message); | 160 CallBack(from, std::string() /* no signature */, error_message); |
161 } | 161 } |
162 | 162 |
163 void CallBack(const tracked_objects::Location& from, | 163 void CallBack(const tracked_objects::Location& from, |
164 const std::string& signature, | 164 const std::string& signature, |
165 const std::string& error_message) { | 165 const std::string& error_message) { |
166 origin_task_runner_->PostTask( | 166 origin_task_runner_->PostTask( |
167 from, base::Bind(callback_, signature, error_message)); | 167 from, base::Bind(callback_, signature, error_message)); |
168 } | 168 } |
169 | 169 |
170 std::string public_key_; | 170 std::string public_key_; |
171 std::string data_; | 171 std::string data_; |
172 | 172 |
173 private: | 173 private: |
174 // Must be called on origin thread, use CallBack() therefore. | 174 // Must be called on origin thread, use CallBack() therefore. |
175 SignCallback callback_; | 175 subtle::SignCallback callback_; |
176 }; | 176 }; |
177 | 177 |
178 class GetCertificatesState : public NSSOperationState { | 178 class GetCertificatesState : public NSSOperationState { |
179 public: | 179 public: |
180 explicit GetCertificatesState(const GetCertificatesCallback& callback); | 180 explicit GetCertificatesState(const GetCertificatesCallback& callback); |
181 virtual ~GetCertificatesState() {} | 181 virtual ~GetCertificatesState() {} |
182 | 182 |
183 virtual void OnError(const tracked_objects::Location& from, | 183 virtual void OnError(const tracked_objects::Location& from, |
184 const std::string& error_message) OVERRIDE { | 184 const std::string& error_message) OVERRIDE { |
185 CallBack(from, | 185 CallBack(from, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 | 244 |
245 private: | 245 private: |
246 // Must be called on origin thread, use CallBack() therefore. | 246 // Must be called on origin thread, use CallBack() therefore. |
247 RemoveCertificateCallback callback_; | 247 RemoveCertificateCallback callback_; |
248 }; | 248 }; |
249 | 249 |
250 NSSOperationState::NSSOperationState() | 250 NSSOperationState::NSSOperationState() |
251 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 251 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
252 } | 252 } |
253 | 253 |
254 GenerateRSAKeyState::GenerateRSAKeyState(unsigned int modulus_length, | 254 GenerateRSAKeyState::GenerateRSAKeyState( |
255 const GenerateKeyCallback& callback) | 255 unsigned int modulus_length_bits, |
256 : modulus_length_(modulus_length), callback_(callback) { | 256 const subtle::GenerateKeyCallback& callback) |
| 257 : modulus_length_bits_(modulus_length_bits), callback_(callback) { |
257 } | 258 } |
258 | 259 |
259 SignState::SignState(const std::string& public_key, | 260 SignState::SignState(const std::string& public_key, |
260 const std::string& data, | 261 const std::string& data, |
261 const SignCallback& callback) | 262 const subtle::SignCallback& callback) |
262 : public_key_(public_key), data_(data), callback_(callback) { | 263 : public_key_(public_key), data_(data), callback_(callback) { |
263 } | 264 } |
264 | 265 |
265 GetCertificatesState::GetCertificatesState( | 266 GetCertificatesState::GetCertificatesState( |
266 const GetCertificatesCallback& callback) | 267 const GetCertificatesCallback& callback) |
267 : callback_(callback) { | 268 : callback_(callback) { |
268 } | 269 } |
269 | 270 |
270 ImportCertificateState::ImportCertificateState( | 271 ImportCertificateState::ImportCertificateState( |
271 scoped_refptr<net::X509Certificate> certificate, | 272 scoped_refptr<net::X509Certificate> certificate, |
272 const ImportCertificateCallback& callback) | 273 const ImportCertificateCallback& callback) |
273 : certificate_(certificate), callback_(callback) { | 274 : certificate_(certificate), callback_(callback) { |
274 } | 275 } |
275 | 276 |
276 RemoveCertificateState::RemoveCertificateState( | 277 RemoveCertificateState::RemoveCertificateState( |
277 scoped_refptr<net::X509Certificate> certificate, | 278 scoped_refptr<net::X509Certificate> certificate, |
278 const RemoveCertificateCallback& callback) | 279 const RemoveCertificateCallback& callback) |
279 : certificate_(certificate), callback_(callback) { | 280 : certificate_(certificate), callback_(callback) { |
280 } | 281 } |
281 | 282 |
282 // Does the actual key generation on a worker thread. Used by | 283 // Does the actual key generation on a worker thread. Used by |
283 // GenerateRSAKeyWithDB(). | 284 // GenerateRSAKeyWithDB(). |
284 void GenerateRSAKeyOnWorkerThread(scoped_ptr<GenerateRSAKeyState> state) { | 285 void GenerateRSAKeyOnWorkerThread(scoped_ptr<GenerateRSAKeyState> state) { |
285 scoped_ptr<crypto::RSAPrivateKey> rsa_key( | 286 scoped_ptr<crypto::RSAPrivateKey> rsa_key( |
286 crypto::RSAPrivateKey::CreateSensitive(state->slot_.get(), | 287 crypto::RSAPrivateKey::CreateSensitive(state->slot_.get(), |
287 state->modulus_length_)); | 288 state->modulus_length_bits_)); |
288 if (!rsa_key) { | 289 if (!rsa_key) { |
289 LOG(ERROR) << "Couldn't create key."; | 290 LOG(ERROR) << "Couldn't create key."; |
290 state->OnError(FROM_HERE, kErrorInternal); | 291 state->OnError(FROM_HERE, kErrorInternal); |
291 return; | 292 return; |
292 } | 293 } |
293 | 294 |
294 std::vector<uint8> public_key_spki_der; | 295 std::vector<uint8> public_key_spki_der; |
295 if (!rsa_key->ExportPublicKey(&public_key_spki_der)) { | 296 if (!rsa_key->ExportPublicKey(&public_key_spki_der)) { |
296 // TODO(pneubeck): Remove rsa_key from storage. | 297 // TODO(pneubeck): Remove rsa_key from storage. |
297 LOG(ERROR) << "Couldn't export public key."; | 298 LOG(ERROR) << "Couldn't export public key."; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 scoped_refptr<net::X509Certificate> certificate = state->certificate_; | 462 scoped_refptr<net::X509Certificate> certificate = state->certificate_; |
462 bool certificate_found = certificate->os_cert_handle()->isperm; | 463 bool certificate_found = certificate->os_cert_handle()->isperm; |
463 cert_db->DeleteCertAndKeyAsync( | 464 cert_db->DeleteCertAndKeyAsync( |
464 certificate, | 465 certificate, |
465 base::Bind( | 466 base::Bind( |
466 &DidRemoveCertificate, base::Passed(&state), certificate_found)); | 467 &DidRemoveCertificate, base::Passed(&state), certificate_found)); |
467 } | 468 } |
468 | 469 |
469 } // namespace | 470 } // namespace |
470 | 471 |
| 472 namespace subtle { |
| 473 |
471 void GenerateRSAKey(const std::string& token_id, | 474 void GenerateRSAKey(const std::string& token_id, |
472 unsigned int modulus_length, | 475 unsigned int modulus_length_bits, |
473 const GenerateKeyCallback& callback, | 476 const GenerateKeyCallback& callback, |
474 Profile* profile) { | 477 Profile* profile) { |
475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
476 scoped_ptr<GenerateRSAKeyState> state( | 479 scoped_ptr<GenerateRSAKeyState> state( |
477 new GenerateRSAKeyState(modulus_length, callback)); | 480 new GenerateRSAKeyState(modulus_length_bits, callback)); |
478 | 481 |
479 if (modulus_length > kMaxRSAModulusLength) { | 482 if (modulus_length_bits > kMaxRSAModulusLengthBits) { |
480 state->OnError(FROM_HERE, kErrorAlgorithmNotSupported); | 483 state->OnError(FROM_HERE, kErrorAlgorithmNotSupported); |
481 return; | 484 return; |
482 } | 485 } |
483 | 486 |
484 // Get the pointer to |state| before base::Passed releases |state|. | 487 // Get the pointer to |state| before base::Passed releases |state|. |
485 NSSOperationState* state_ptr = state.get(); | 488 NSSOperationState* state_ptr = state.get(); |
486 GetCertDatabase(token_id, | 489 GetCertDatabase(token_id, |
487 base::Bind(&GenerateRSAKeyWithDB, base::Passed(&state)), | 490 base::Bind(&GenerateRSAKeyWithDB, base::Passed(&state)), |
488 profile, | 491 profile, |
489 state_ptr); | 492 state_ptr); |
(...skipping 11 matching lines...) Expand all Loading... |
501 | 504 |
502 // The NSSCertDatabase object is not required. But in case it's not available | 505 // The NSSCertDatabase object is not required. But in case it's not available |
503 // we would get more informative error messages and we can double check that | 506 // we would get more informative error messages and we can double check that |
504 // we use a key of the correct token. | 507 // we use a key of the correct token. |
505 GetCertDatabase(token_id, | 508 GetCertDatabase(token_id, |
506 base::Bind(&RSASignWithDB, base::Passed(&state)), | 509 base::Bind(&RSASignWithDB, base::Passed(&state)), |
507 profile, | 510 profile, |
508 state_ptr); | 511 state_ptr); |
509 } | 512 } |
510 | 513 |
| 514 } // namespace subtle |
| 515 |
511 void GetCertificates(const std::string& token_id, | 516 void GetCertificates(const std::string& token_id, |
512 const GetCertificatesCallback& callback, | 517 const GetCertificatesCallback& callback, |
513 Profile* profile) { | 518 Profile* profile) { |
514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
515 scoped_ptr<GetCertificatesState> state(new GetCertificatesState(callback)); | 520 scoped_ptr<GetCertificatesState> state(new GetCertificatesState(callback)); |
516 // Get the pointer to |state| before base::Passed releases |state|. | 521 // Get the pointer to |state| before base::Passed releases |state|. |
517 NSSOperationState* state_ptr = state.get(); | 522 NSSOperationState* state_ptr = state.get(); |
518 GetCertDatabase(token_id, | 523 GetCertDatabase(token_id, |
519 base::Bind(&GetCertificatesWithDB, base::Passed(&state)), | 524 base::Bind(&GetCertificatesWithDB, base::Passed(&state)), |
520 profile, | 525 profile, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 // we would get more informative error messages. | 559 // we would get more informative error messages. |
555 GetCertDatabase(token_id, | 560 GetCertDatabase(token_id, |
556 base::Bind(&RemoveCertificateWithDB, base::Passed(&state)), | 561 base::Bind(&RemoveCertificateWithDB, base::Passed(&state)), |
557 profile, | 562 profile, |
558 state_ptr); | 563 state_ptr); |
559 } | 564 } |
560 | 565 |
561 } // namespace platform_keys | 566 } // namespace platform_keys |
562 | 567 |
563 } // namespace chromeos | 568 } // namespace chromeos |
OLD | NEW |