Skip to content

Commit

Permalink
PlatformKeys: Replace std::string with std::vector<uint8_t> (8)
Browse files Browse the repository at this point in the history
Replace some std::string's with std::vector<uint8_t>'s where
the stored data is actually binary. (Part 8)

Bug: b/192071491
Test: Compilation, *PlatformKey* browser and unit tests
Change-Id: If22fe76c1b6f2a233767a5e0afa791b2e3e8e5cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4208958
Reviewed-by: Pavol Marko <pmarko@chromium.org>
Reviewed-by: Roland Bock <rbock@google.com>
Commit-Queue: Michael Ershov <miersh@google.com>
Cr-Commit-Position: refs/heads/main@{#1100355}
  • Loading branch information
Michael Ershov authored and Chromium LUCI CQ committed Feb 2, 2023
1 parent ad7635d commit 23bd6da
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ void CertProvisioningWorkerDynamic::GenerateRegularKey() {
}

void CertProvisioningWorkerDynamic::OnGenerateRegularKeyDone(
const std::string& public_key_spki_der,
std::vector<uint8_t> public_key_spki_der,
chromeos::platform_keys::Status status) {
if (status != chromeos::platform_keys::Status::kSuccess ||
public_key_spki_der.empty()) {
Expand All @@ -394,7 +394,7 @@ void CertProvisioningWorkerDynamic::OnGenerateRegularKeyDone(
}

key_location_ = KeyLocation::kPkcs11Token;
public_key_ = StrToBytes(public_key_spki_der);
public_key_ = std::move(public_key_spki_der);
MarkRegularKey();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CertProvisioningWorkerDynamic : public CertProvisioningWorker {
void GenerateKey();

void GenerateRegularKey();
void OnGenerateRegularKeyDone(const std::string& public_key_spki_der,
void OnGenerateRegularKeyDone(std::vector<uint8_t> public_key_spki_der,
chromeos::platform_keys::Status status);

void GenerateKeyForVa();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ TEST_F(CertProvisioningWorkerDynamicTest, NoVaSuccess) {
/*sw_backed=*/false,
/*callback=*/_))
.Times(1)
.WillOnce(RunOnceCallback<3>(GetPublicKey(), Status::kSuccess));
.WillOnce(RunOnceCallback<3>(GetPublicKeyBin(), Status::kSuccess));

EXPECT_CALL(*key_permissions_manager_,
AllowKeyForUsage(/*callback=*/_, KeyUsage::kCorporate,
Expand Down Expand Up @@ -1033,7 +1033,7 @@ TEST_F(CertProvisioningWorkerDynamicTest, NoVaTooManyTwoProofsOfPossession) {
/*sw_backed=*/false,
/*callback=*/_))
.Times(1)
.WillOnce(RunOnceCallback<3>(GetPublicKey(), Status::kSuccess));
.WillOnce(RunOnceCallback<3>(GetPublicKeyBin(), Status::kSuccess));

EXPECT_CALL(*key_permissions_manager_,
AllowKeyForUsage(/*callback=*/_, KeyUsage::kCorporate,
Expand Down Expand Up @@ -1634,7 +1634,7 @@ TEST_F(CertProvisioningWorkerDynamicTest, RetryUploadProofOfPossession) {
/*sw_backed=*/false,
/*callback=*/_))
.Times(1)
.WillOnce(RunOnceCallback<3>(GetPublicKey(), Status::kSuccess));
.WillOnce(RunOnceCallback<3>(GetPublicKeyBin(), Status::kSuccess));

EXPECT_CALL(*key_permissions_manager_,
AllowKeyForUsage(/*callback=*/_, KeyUsage::kCorporate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ void CertProvisioningWorkerStatic::GenerateRegularKey() {
}

void CertProvisioningWorkerStatic::OnGenerateRegularKeyDone(
const std::string& public_key_spki_der,
std::vector<uint8_t> public_key_spki_der,
chromeos::platform_keys::Status status) {
if (status != chromeos::platform_keys::Status::kSuccess ||
public_key_spki_der.empty()) {
Expand All @@ -394,7 +394,7 @@ void CertProvisioningWorkerStatic::OnGenerateRegularKeyDone(
return;
}

public_key_ = StrToBytes(public_key_spki_der);
public_key_ = std::move(public_key_spki_der);
UpdateState(FROM_HERE, CertProvisioningWorkerState::kKeypairGenerated);
DoStep();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CertProvisioningWorkerStatic : public CertProvisioningWorker {
void GenerateKey();

void GenerateRegularKey();
void OnGenerateRegularKeyDone(const std::string& public_key_spki_der,
void OnGenerateRegularKeyDone(std::vector<uint8_t> public_key_spki_der,
chromeos::platform_keys::Status status);

void GenerateKeyForVa();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ TEST_F(CertProvisioningWorkerStaticTest, NoVaSuccess) {
/*sw_backed=*/false,
/*callback=*/_))
.Times(1)
.WillOnce(RunOnceCallback<3>(GetPublicKey(), Status::kSuccess));
.WillOnce(RunOnceCallback<3>(GetPublicKeyBin(), Status::kSuccess));

EXPECT_START_CSR_OK_WITHOUT_VA(
StartCsr(Eq(std::ref(provisioning_process)), /*callback=*/_),
Expand Down
5 changes: 2 additions & 3 deletions chrome/browser/ash/crosapi/keystore_service_ash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,12 @@ void KeystoreServiceAsh::GenerateKey(
// static
void KeystoreServiceAsh::DidGenerateKey(
GenerateKeyCallback callback,
const std::string& public_key,
std::vector<uint8_t> public_key,
chromeos::platform_keys::Status status) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
crosapi::mojom::KeystoreBinaryResultPtr result_ptr;
if (status == chromeos::platform_keys::Status::kSuccess) {
result_ptr = mojom::KeystoreBinaryResult::NewBlob(
std::vector<uint8_t>(public_key.begin(), public_key.end()));
result_ptr = mojom::KeystoreBinaryResult::NewBlob(std::move(public_key));
} else {
result_ptr = mojom::KeystoreBinaryResult::NewError(
chromeos::platform_keys::StatusToKeystoreError(status));
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ash/crosapi/keystore_service_ash.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class KeystoreServiceAsh : public mojom::KeystoreService, public KeyedService {
static void DidRemoveCertificate(RemoveCertificateCallback callback,
chromeos::platform_keys::Status status);
static void DidGenerateKey(GenerateKeyCallback callback,
const std::string& public_key,
std::vector<uint8_t> public_key,
chromeos::platform_keys::Status status);
static void DidRemoveKey(RemoveKeyCallback callback,
chromeos::platform_keys::Status status);
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/ash/crosapi/keystore_service_ash_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ TEST_F(KeystoreServiceAshTest, UserKeystoreRsaAlgoGenerateKeySuccess) {
platform_keys_service_,
GenerateRSAKey(TokenId::kUser, modulus_length, /*sw_backed=*/false,
/*callback=*/_))
.WillOnce(RunOnceCallback<3>(GetPublicKeyStr(), Status::kSuccess));
.WillOnce(RunOnceCallback<3>(GetPublicKeyBin(), Status::kSuccess));
CallbackObserver<mojom::KeystoreBinaryResultPtr> observer;
keystore_service_.GenerateKey(
mojom::KeystoreType::kUser,
Expand All @@ -260,7 +260,7 @@ TEST_F(KeystoreServiceAshTest, DeviceKeystoreEcAlgoGenerateKeySuccess) {

EXPECT_CALL(platform_keys_service_,
GenerateECKey(TokenId::kSystem, named_curve, /*callback=*/_))
.WillOnce(RunOnceCallback<2>(GetPublicKeyStr(), Status::kSuccess));
.WillOnce(RunOnceCallback<2>(GetPublicKeyBin(), Status::kSuccess));

CallbackObserver<mojom::KeystoreBinaryResultPtr> observer;
keystore_service_.GenerateKey(mojom::KeystoreType::kDevice,
Expand All @@ -273,7 +273,8 @@ TEST_F(KeystoreServiceAshTest, DeviceKeystoreEcAlgoGenerateKeySuccess) {

TEST_F(KeystoreServiceAshTest, UserKeystoreUnsupportedEcCurveGenerateKeyFail) {
EXPECT_CALL(platform_keys_service_, GenerateECKey)
.WillOnce(RunOnceCallback<2>("", Status::kErrorInternal));
.WillOnce(
RunOnceCallback<2>(std::vector<uint8_t>(), Status::kErrorInternal));

CallbackObserver<mojom::KeystoreBinaryResultPtr> observer;
keystore_service_.GenerateKey(mojom::KeystoreType::kUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ class KeyPermissionsManagerBrowserTestBase
virtual KeyPermissionsManager* GetKeyPermissionsManager() = 0;

std::vector<uint8_t> GenerateKey() {
test_util::GenerateKeyExecutionWaiter generate_key_waiter;
base::test::TestFuture<std::vector<uint8_t>,
chromeos::platform_keys::Status>
generate_key_waiter;
GetPlatformKeysService()->GenerateRSAKey(GetToken(),
/*modulus_length_bits=*/2048,
/*sw_backed=*/false,
generate_key_waiter.GetCallback());
EXPECT_TRUE(generate_key_waiter.Wait());
const std::string& pub_key = generate_key_waiter.public_key_spki_der();
return std::vector<uint8_t>(pub_key.begin(), pub_key.end());
return std::get<std::vector<uint8_t>>(generate_key_waiter.Take());
}

// Returns all keys on the token.
Expand Down
8 changes: 3 additions & 5 deletions chrome/browser/ash/platform_keys/platform_keys_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ class NSSCertDatabase;
class ClientCertStore;
} // namespace net

namespace ash {
namespace platform_keys {
namespace ash::platform_keys {

using GenerateKeyCallback =
base::OnceCallback<void(const std::string& public_key_spki_der,
base::OnceCallback<void(std::vector<uint8_t> public_key_spki_der,
chromeos::platform_keys::Status status)>;

using SignCallback =
Expand Down Expand Up @@ -411,7 +410,6 @@ class PlatformKeysServiceImpl final : public PlatformKeysService {
base::WeakPtrFactory<PlatformKeysServiceImpl> weak_factory_{this};
};

} // namespace platform_keys
} // namespace ash
} // namespace ash::platform_keys

#endif // CHROME_BROWSER_ASH_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_

0 comments on commit 23bd6da

Please sign in to comment.