| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 const unsigned int modulus_length_bits_; | 145 const unsigned int modulus_length_bits_; |
| 146 | 146 |
| 147 private: | 147 private: |
| 148 // Must be called on origin thread, use CallBack() therefore. | 148 // Must be called on origin thread, use CallBack() therefore. |
| 149 subtle::GenerateKeyCallback callback_; | 149 subtle::GenerateKeyCallback callback_; |
| 150 }; | 150 }; |
| 151 | 151 |
| 152 class SignState : public NSSOperationState { | 152 class SignState : public NSSOperationState { |
| 153 public: | 153 public: |
| 154 SignState(const std::string& public_key, | 154 SignState(const std::string& public_key, |
| 155 HashAlgorithm hash_algorithm, |
| 155 const std::string& data, | 156 const std::string& data, |
| 156 const subtle::SignCallback& callback); | 157 const subtle::SignCallback& callback); |
| 157 virtual ~SignState() {} | 158 virtual ~SignState() {} |
| 158 | 159 |
| 159 virtual void OnError(const tracked_objects::Location& from, | 160 virtual void OnError(const tracked_objects::Location& from, |
| 160 const std::string& error_message) OVERRIDE { | 161 const std::string& error_message) OVERRIDE { |
| 161 CallBack(from, std::string() /* no signature */, error_message); | 162 CallBack(from, std::string() /* no signature */, error_message); |
| 162 } | 163 } |
| 163 | 164 |
| 164 void CallBack(const tracked_objects::Location& from, | 165 void CallBack(const tracked_objects::Location& from, |
| 165 const std::string& signature, | 166 const std::string& signature, |
| 166 const std::string& error_message) { | 167 const std::string& error_message) { |
| 167 origin_task_runner_->PostTask( | 168 origin_task_runner_->PostTask( |
| 168 from, base::Bind(callback_, signature, error_message)); | 169 from, base::Bind(callback_, signature, error_message)); |
| 169 } | 170 } |
| 170 | 171 |
| 171 const std::string public_key_; | 172 const std::string public_key_; |
| 173 HashAlgorithm hash_algorithm_; |
| 172 const std::string data_; | 174 const std::string data_; |
| 173 | 175 |
| 174 private: | 176 private: |
| 175 // Must be called on origin thread, use CallBack() therefore. | 177 // Must be called on origin thread, use CallBack() therefore. |
| 176 subtle::SignCallback callback_; | 178 subtle::SignCallback callback_; |
| 177 }; | 179 }; |
| 178 | 180 |
| 179 class GetCertificatesState : public NSSOperationState { | 181 class GetCertificatesState : public NSSOperationState { |
| 180 public: | 182 public: |
| 181 explicit GetCertificatesState(const GetCertificatesCallback& callback); | 183 explicit GetCertificatesState(const GetCertificatesCallback& callback); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 254 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 253 } | 255 } |
| 254 | 256 |
| 255 GenerateRSAKeyState::GenerateRSAKeyState( | 257 GenerateRSAKeyState::GenerateRSAKeyState( |
| 256 unsigned int modulus_length_bits, | 258 unsigned int modulus_length_bits, |
| 257 const subtle::GenerateKeyCallback& callback) | 259 const subtle::GenerateKeyCallback& callback) |
| 258 : modulus_length_bits_(modulus_length_bits), callback_(callback) { | 260 : modulus_length_bits_(modulus_length_bits), callback_(callback) { |
| 259 } | 261 } |
| 260 | 262 |
| 261 SignState::SignState(const std::string& public_key, | 263 SignState::SignState(const std::string& public_key, |
| 264 HashAlgorithm hash_algorithm, |
| 262 const std::string& data, | 265 const std::string& data, |
| 263 const subtle::SignCallback& callback) | 266 const subtle::SignCallback& callback) |
| 264 : public_key_(public_key), data_(data), callback_(callback) { | 267 : public_key_(public_key), |
| 268 hash_algorithm_(hash_algorithm), |
| 269 data_(data), |
| 270 callback_(callback) { |
| 265 } | 271 } |
| 266 | 272 |
| 267 GetCertificatesState::GetCertificatesState( | 273 GetCertificatesState::GetCertificatesState( |
| 268 const GetCertificatesCallback& callback) | 274 const GetCertificatesCallback& callback) |
| 269 : callback_(callback) { | 275 : callback_(callback) { |
| 270 } | 276 } |
| 271 | 277 |
| 272 ImportCertificateState::ImportCertificateState( | 278 ImportCertificateState::ImportCertificateState( |
| 273 scoped_refptr<net::X509Certificate> certificate, | 279 scoped_refptr<net::X509Certificate> certificate, |
| 274 const ImportCertificateCallback& callback) | 280 const ImportCertificateCallback& callback) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 public_key_uint8, public_key_uint8 + state->public_key_.size()); | 332 public_key_uint8, public_key_uint8 + state->public_key_.size()); |
| 327 | 333 |
| 328 // TODO(pneubeck): This searches all slots. Change to look only at |slot_|. | 334 // TODO(pneubeck): This searches all slots. Change to look only at |slot_|. |
| 329 scoped_ptr<crypto::RSAPrivateKey> rsa_key( | 335 scoped_ptr<crypto::RSAPrivateKey> rsa_key( |
| 330 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key_vector)); | 336 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key_vector)); |
| 331 if (!rsa_key || rsa_key->key()->pkcs11Slot != state->slot_) { | 337 if (!rsa_key || rsa_key->key()->pkcs11Slot != state->slot_) { |
| 332 state->OnError(FROM_HERE, kErrorKeyNotFound); | 338 state->OnError(FROM_HERE, kErrorKeyNotFound); |
| 333 return; | 339 return; |
| 334 } | 340 } |
| 335 | 341 |
| 342 SECOidTag sign_alg_tag = SEC_OID_UNKNOWN; |
| 343 switch (state->hash_algorithm_) { |
| 344 case HASH_ALGORITHM_SHA1: |
| 345 sign_alg_tag = SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION; |
| 346 break; |
| 347 case HASH_ALGORITHM_SHA256: |
| 348 sign_alg_tag = SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION; |
| 349 break; |
| 350 case HASH_ALGORITHM_SHA384: |
| 351 sign_alg_tag = SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION; |
| 352 break; |
| 353 case HASH_ALGORITHM_SHA512: |
| 354 sign_alg_tag = SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION; |
| 355 break; |
| 356 } |
| 357 |
| 336 SECItem sign_result = {siBuffer, NULL, 0}; | 358 SECItem sign_result = {siBuffer, NULL, 0}; |
| 337 if (SEC_SignData(&sign_result, | 359 if (SEC_SignData(&sign_result, |
| 338 reinterpret_cast<const unsigned char*>(state->data_.data()), | 360 reinterpret_cast<const unsigned char*>(state->data_.data()), |
| 339 state->data_.size(), | 361 state->data_.size(), |
| 340 rsa_key->key(), | 362 rsa_key->key(), |
| 341 SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION) != SECSuccess) { | 363 sign_alg_tag) != SECSuccess) { |
| 342 LOG(ERROR) << "Couldn't sign."; | 364 LOG(ERROR) << "Couldn't sign."; |
| 343 state->OnError(FROM_HERE, kErrorInternal); | 365 state->OnError(FROM_HERE, kErrorInternal); |
| 344 return; | 366 return; |
| 345 } | 367 } |
| 346 | 368 |
| 347 std::string signature(reinterpret_cast<const char*>(sign_result.data), | 369 std::string signature(reinterpret_cast<const char*>(sign_result.data), |
| 348 sign_result.len); | 370 sign_result.len); |
| 349 state->CallBack(FROM_HERE, signature, std::string() /* no error */); | 371 state->CallBack(FROM_HERE, signature, std::string() /* no error */); |
| 350 } | 372 } |
| 351 | 373 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 // Get the pointer to |state| before base::Passed releases |state|. | 510 // Get the pointer to |state| before base::Passed releases |state|. |
| 489 NSSOperationState* state_ptr = state.get(); | 511 NSSOperationState* state_ptr = state.get(); |
| 490 GetCertDatabase(token_id, | 512 GetCertDatabase(token_id, |
| 491 base::Bind(&GenerateRSAKeyWithDB, base::Passed(&state)), | 513 base::Bind(&GenerateRSAKeyWithDB, base::Passed(&state)), |
| 492 browser_context, | 514 browser_context, |
| 493 state_ptr); | 515 state_ptr); |
| 494 } | 516 } |
| 495 | 517 |
| 496 void Sign(const std::string& token_id, | 518 void Sign(const std::string& token_id, |
| 497 const std::string& public_key, | 519 const std::string& public_key, |
| 520 HashAlgorithm hash_algorithm, |
| 498 const std::string& data, | 521 const std::string& data, |
| 499 const SignCallback& callback, | 522 const SignCallback& callback, |
| 500 BrowserContext* browser_context) { | 523 BrowserContext* browser_context) { |
| 501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 502 scoped_ptr<SignState> state(new SignState(public_key, data, callback)); | 525 scoped_ptr<SignState> state( |
| 526 new SignState(public_key, hash_algorithm, data, callback)); |
| 503 // Get the pointer to |state| before base::Passed releases |state|. | 527 // Get the pointer to |state| before base::Passed releases |state|. |
| 504 NSSOperationState* state_ptr = state.get(); | 528 NSSOperationState* state_ptr = state.get(); |
| 505 | 529 |
| 506 // The NSSCertDatabase object is not required. But in case it's not available | 530 // The NSSCertDatabase object is not required. But in case it's not available |
| 507 // we would get more informative error messages and we can double check that | 531 // we would get more informative error messages and we can double check that |
| 508 // we use a key of the correct token. | 532 // we use a key of the correct token. |
| 509 GetCertDatabase(token_id, | 533 GetCertDatabase(token_id, |
| 510 base::Bind(&RSASignWithDB, base::Passed(&state)), | 534 base::Bind(&RSASignWithDB, base::Passed(&state)), |
| 511 browser_context, | 535 browser_context, |
| 512 state_ptr); | 536 state_ptr); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 // we would get more informative error messages. | 584 // we would get more informative error messages. |
| 561 GetCertDatabase(token_id, | 585 GetCertDatabase(token_id, |
| 562 base::Bind(&RemoveCertificateWithDB, base::Passed(&state)), | 586 base::Bind(&RemoveCertificateWithDB, base::Passed(&state)), |
| 563 browser_context, | 587 browser_context, |
| 564 state_ptr); | 588 state_ptr); |
| 565 } | 589 } |
| 566 | 590 |
| 567 } // namespace platform_keys | 591 } // namespace platform_keys |
| 568 | 592 |
| 569 } // namespace chromeos | 593 } // namespace chromeos |
| OLD | NEW |