| 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 <cert.h> | 7 #include <cert.h> |
| 8 #include <cryptohi.h> | 8 #include <cryptohi.h> |
| 9 #include <keyhi.h> | 9 #include <keyhi.h> |
| 10 #include <secder.h> | 10 #include <secder.h> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "chrome/browser/browser_process_platform_part_chromeos.h" | 24 #include "chrome/browser/browser_process_platform_part_chromeos.h" |
| 25 #include "chrome/browser/chromeos/net/client_cert_filter_chromeos.h" | 25 #include "chrome/browser/chromeos/net/client_cert_filter_chromeos.h" |
| 26 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 26 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 27 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 27 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 28 #include "chrome/browser/extensions/api/enterprise_platform_keys/enterprise_plat
form_keys_api.h" | 28 #include "chrome/browser/extensions/api/enterprise_platform_keys/enterprise_plat
form_keys_api.h" |
| 29 #include "chrome/browser/net/nss_context.h" | 29 #include "chrome/browser/net/nss_context.h" |
| 30 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 31 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 31 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 32 #include "content/public/browser/browser_context.h" | 32 #include "content/public/browser/browser_context.h" |
| 33 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 34 #include "crypto/nss_key_util.h" | 34 #include "crypto/rsa_private_key.h" |
| 35 #include "crypto/scoped_nss_types.h" | |
| 36 #include "net/base/crypto_module.h" | 35 #include "net/base/crypto_module.h" |
| 37 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
| 38 #include "net/cert/cert_database.h" | 37 #include "net/cert/cert_database.h" |
| 39 #include "net/cert/nss_cert_database.h" | 38 #include "net/cert/nss_cert_database.h" |
| 40 #include "net/cert/x509_certificate.h" | 39 #include "net/cert/x509_certificate.h" |
| 41 #include "net/cert/x509_util_nss.h" | 40 #include "net/cert/x509_util_nss.h" |
| 42 #include "net/ssl/client_cert_store_chromeos.h" | 41 #include "net/ssl/client_cert_store_chromeos.h" |
| 43 #include "net/ssl/ssl_cert_request_info.h" | 42 #include "net/ssl/ssl_cert_request_info.h" |
| 44 | 43 |
| 45 using content::BrowserContext; | 44 using content::BrowserContext; |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 : certificate_(certificate), callback_(callback) { | 393 : certificate_(certificate), callback_(callback) { |
| 395 } | 394 } |
| 396 | 395 |
| 397 GetTokensState::GetTokensState(const GetTokensCallback& callback) | 396 GetTokensState::GetTokensState(const GetTokensCallback& callback) |
| 398 : callback_(callback) { | 397 : callback_(callback) { |
| 399 } | 398 } |
| 400 | 399 |
| 401 // Does the actual key generation on a worker thread. Used by | 400 // Does the actual key generation on a worker thread. Used by |
| 402 // GenerateRSAKeyWithDB(). | 401 // GenerateRSAKeyWithDB(). |
| 403 void GenerateRSAKeyOnWorkerThread(scoped_ptr<GenerateRSAKeyState> state) { | 402 void GenerateRSAKeyOnWorkerThread(scoped_ptr<GenerateRSAKeyState> state) { |
| 404 crypto::ScopedSECKEYPublicKey public_key; | 403 scoped_ptr<crypto::RSAPrivateKey> rsa_key( |
| 405 crypto::ScopedSECKEYPrivateKey private_key; | 404 crypto::RSAPrivateKey::CreateSensitive(state->slot_.get(), |
| 406 if (!crypto::GenerateRSAKeyPairNSS( | 405 state->modulus_length_bits_)); |
| 407 state->slot_.get(), state->modulus_length_bits_, true /* permanent */, | 406 if (!rsa_key) { |
| 408 &public_key, &private_key)) { | |
| 409 LOG(ERROR) << "Couldn't create key."; | 407 LOG(ERROR) << "Couldn't create key."; |
| 410 state->OnError(FROM_HERE, kErrorInternal); | 408 state->OnError(FROM_HERE, kErrorInternal); |
| 411 return; | 409 return; |
| 412 } | 410 } |
| 413 | 411 |
| 414 crypto::ScopedSECItem public_key_der( | 412 std::vector<uint8> public_key_spki_der; |
| 415 SECKEY_EncodeDERSubjectPublicKeyInfo(public_key.get())); | 413 if (!rsa_key->ExportPublicKey(&public_key_spki_der)) { |
| 416 if (!public_key_der) { | 414 // TODO(pneubeck): Remove rsa_key from storage. |
| 417 // TODO(pneubeck): Remove private_key and public_key from storage. | |
| 418 LOG(ERROR) << "Couldn't export public key."; | 415 LOG(ERROR) << "Couldn't export public key."; |
| 419 state->OnError(FROM_HERE, kErrorInternal); | 416 state->OnError(FROM_HERE, kErrorInternal); |
| 420 return; | 417 return; |
| 421 } | 418 } |
| 422 state->CallBack( | 419 state->CallBack( |
| 423 FROM_HERE, | 420 FROM_HERE, |
| 424 std::string(reinterpret_cast<const char*>(public_key_der->data), | 421 std::string(public_key_spki_der.begin(), public_key_spki_der.end()), |
| 425 public_key_der->len), | |
| 426 std::string() /* no error */); | 422 std::string() /* no error */); |
| 427 } | 423 } |
| 428 | 424 |
| 429 // Continues generating a RSA key with the obtained NSSCertDatabase. Used by | 425 // Continues generating a RSA key with the obtained NSSCertDatabase. Used by |
| 430 // GenerateRSAKey(). | 426 // GenerateRSAKey(). |
| 431 void GenerateRSAKeyWithDB(scoped_ptr<GenerateRSAKeyState> state, | 427 void GenerateRSAKeyWithDB(scoped_ptr<GenerateRSAKeyState> state, |
| 432 net::NSSCertDatabase* cert_db) { | 428 net::NSSCertDatabase* cert_db) { |
| 433 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 429 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 434 // Only the slot and not the NSSCertDatabase is required. Ignore |cert_db|. | 430 // Only the slot and not the NSSCertDatabase is required. Ignore |cert_db|. |
| 435 base::WorkerPool::PostTask( | 431 base::WorkerPool::PostTask( |
| 436 FROM_HERE, | 432 FROM_HERE, |
| 437 base::Bind(&GenerateRSAKeyOnWorkerThread, base::Passed(&state)), | 433 base::Bind(&GenerateRSAKeyOnWorkerThread, base::Passed(&state)), |
| 438 true /*task is slow*/); | 434 true /*task is slow*/); |
| 439 } | 435 } |
| 440 | 436 |
| 441 // Does the actual signing on a worker thread. Used by SignRSAWithDB(). | 437 // Does the actual signing on a worker thread. Used by SignRSAWithDB(). |
| 442 void SignRSAOnWorkerThread(scoped_ptr<SignRSAState> state) { | 438 void SignRSAOnWorkerThread(scoped_ptr<SignRSAState> state) { |
| 443 const uint8* public_key_uint8 = | 439 const uint8* public_key_uint8 = |
| 444 reinterpret_cast<const uint8*>(state->public_key_.data()); | 440 reinterpret_cast<const uint8*>(state->public_key_.data()); |
| 445 std::vector<uint8> public_key_vector( | 441 std::vector<uint8> public_key_vector( |
| 446 public_key_uint8, public_key_uint8 + state->public_key_.size()); | 442 public_key_uint8, public_key_uint8 + state->public_key_.size()); |
| 447 | 443 |
| 448 // TODO(pneubeck): This searches all slots. Change to look only at |slot_|. | 444 // TODO(pneubeck): This searches all slots. Change to look only at |slot_|. |
| 449 crypto::ScopedSECKEYPrivateKey rsa_key( | 445 scoped_ptr<crypto::RSAPrivateKey> rsa_key( |
| 450 crypto::FindNSSKeyFromPublicKeyInfo(public_key_vector)); | 446 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key_vector)); |
| 451 | 447 |
| 452 // Fail if the key was not found. If a specific slot was requested, also fail | 448 // Fail if the key was not found. If a specific slot was requested, also fail |
| 453 // if the key was found in the wrong slot. | 449 // if the key was found in the wrong slot. |
| 454 if (!rsa_key || SECKEY_GetPrivateKeyType(rsa_key.get()) != rsaKey || | 450 if (!rsa_key || |
| 455 (state->slot_ && rsa_key->pkcs11Slot != state->slot_)) { | 451 (state->slot_ && rsa_key->key()->pkcs11Slot != state->slot_)) { |
| 456 state->OnError(FROM_HERE, kErrorKeyNotFound); | 452 state->OnError(FROM_HERE, kErrorKeyNotFound); |
| 457 return; | 453 return; |
| 458 } | 454 } |
| 459 | 455 |
| 460 std::string signature_str; | 456 std::string signature_str; |
| 461 if (state->sign_direct_pkcs_padded_) { | 457 if (state->sign_direct_pkcs_padded_) { |
| 462 static_assert( | 458 static_assert( |
| 463 sizeof(*state->data_.data()) == sizeof(char), | 459 sizeof(*state->data_.data()) == sizeof(char), |
| 464 "Can't reinterpret data if it's characters are not 8 bit large."); | 460 "Can't reinterpret data if it's characters are not 8 bit large."); |
| 465 SECItem input = {siBuffer, | 461 SECItem input = {siBuffer, |
| 466 reinterpret_cast<unsigned char*>( | 462 reinterpret_cast<unsigned char*>( |
| 467 const_cast<char*>(state->data_.data())), | 463 const_cast<char*>(state->data_.data())), |
| 468 state->data_.size()}; | 464 state->data_.size()}; |
| 469 | 465 |
| 470 // Compute signature of hash. | 466 // Compute signature of hash. |
| 471 int signature_len = PK11_SignatureLen(rsa_key.get()); | 467 int signature_len = PK11_SignatureLen(rsa_key->key()); |
| 472 if (signature_len <= 0) { | 468 if (signature_len <= 0) { |
| 473 state->OnError(FROM_HERE, kErrorInternal); | 469 state->OnError(FROM_HERE, kErrorInternal); |
| 474 return; | 470 return; |
| 475 } | 471 } |
| 476 | 472 |
| 477 std::vector<unsigned char> signature(signature_len); | 473 std::vector<unsigned char> signature(signature_len); |
| 478 SECItem signature_output = { | 474 SECItem signature_output = { |
| 479 siBuffer, vector_as_array(&signature), signature.size()}; | 475 siBuffer, vector_as_array(&signature), signature.size()}; |
| 480 if (PK11_Sign(rsa_key.get(), &signature_output, &input) == SECSuccess) | 476 if (PK11_Sign(rsa_key->key(), &signature_output, &input) == SECSuccess) |
| 481 signature_str.assign(signature.begin(), signature.end()); | 477 signature_str.assign(signature.begin(), signature.end()); |
| 482 } else { | 478 } else { |
| 483 SECOidTag sign_alg_tag = SEC_OID_UNKNOWN; | 479 SECOidTag sign_alg_tag = SEC_OID_UNKNOWN; |
| 484 switch (state->hash_algorithm_) { | 480 switch (state->hash_algorithm_) { |
| 485 case HASH_ALGORITHM_SHA1: | 481 case HASH_ALGORITHM_SHA1: |
| 486 sign_alg_tag = SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION; | 482 sign_alg_tag = SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION; |
| 487 break; | 483 break; |
| 488 case HASH_ALGORITHM_SHA256: | 484 case HASH_ALGORITHM_SHA256: |
| 489 sign_alg_tag = SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION; | 485 sign_alg_tag = SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION; |
| 490 break; | 486 break; |
| 491 case HASH_ALGORITHM_SHA384: | 487 case HASH_ALGORITHM_SHA384: |
| 492 sign_alg_tag = SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION; | 488 sign_alg_tag = SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION; |
| 493 break; | 489 break; |
| 494 case HASH_ALGORITHM_SHA512: | 490 case HASH_ALGORITHM_SHA512: |
| 495 sign_alg_tag = SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION; | 491 sign_alg_tag = SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION; |
| 496 break; | 492 break; |
| 497 case HASH_ALGORITHM_NONE: | 493 case HASH_ALGORITHM_NONE: |
| 498 NOTREACHED(); | 494 NOTREACHED(); |
| 499 break; | 495 break; |
| 500 } | 496 } |
| 501 | 497 |
| 502 SECItem sign_result = {siBuffer, nullptr, 0}; | 498 SECItem sign_result = {siBuffer, nullptr, 0}; |
| 503 if (SEC_SignData( | 499 if (SEC_SignData( |
| 504 &sign_result, | 500 &sign_result, |
| 505 reinterpret_cast<const unsigned char*>(state->data_.data()), | 501 reinterpret_cast<const unsigned char*>(state->data_.data()), |
| 506 state->data_.size(), rsa_key.get(), sign_alg_tag) == SECSuccess) { | 502 state->data_.size(), rsa_key->key(), sign_alg_tag) == SECSuccess) { |
| 507 signature_str.assign(sign_result.data, | 503 signature_str.assign(sign_result.data, |
| 508 sign_result.data + sign_result.len); | 504 sign_result.data + sign_result.len); |
| 509 } | 505 } |
| 510 } | 506 } |
| 511 | 507 |
| 512 if (signature_str.empty()) { | 508 if (signature_str.empty()) { |
| 513 LOG(ERROR) << "Couldn't sign."; | 509 LOG(ERROR) << "Couldn't sign."; |
| 514 state->OnError(FROM_HERE, kErrorInternal); | 510 state->OnError(FROM_HERE, kErrorInternal); |
| 515 return; | 511 return; |
| 516 } | 512 } |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 NSSOperationState* state_ptr = state.get(); | 876 NSSOperationState* state_ptr = state.get(); |
| 881 GetCertDatabase(std::string() /* don't get any specific slot */, | 877 GetCertDatabase(std::string() /* don't get any specific slot */, |
| 882 base::Bind(&GetTokensWithDB, base::Passed(&state)), | 878 base::Bind(&GetTokensWithDB, base::Passed(&state)), |
| 883 browser_context, | 879 browser_context, |
| 884 state_ptr); | 880 state_ptr); |
| 885 } | 881 } |
| 886 | 882 |
| 887 } // namespace platform_keys | 883 } // namespace platform_keys |
| 888 | 884 |
| 889 } // namespace chromeos | 885 } // namespace chromeos |
| OLD | NEW |