-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sync] Share helpers for explicit passphrase mojo between Ash and Lacros
This CL intoduces "chromeos" subfolder under components/sync, moves there functions for conversions between Nigori and its mojo representation, that used to be duplicated in Ash and Lacros, and adds test coverage for them. Bug: 1233545 Change-Id: I4174ed10b0e07090d8567b6727fc06b72a995ed9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3568461 Reviewed-by: Marc Treib <treib@chromium.org> Reviewed-by: Hidehiko Abe <hidehiko@chromium.org> Commit-Queue: Maksim Moskvitin <mmoskvitin@google.com> Cr-Commit-Position: refs/heads/main@{#989467}
- Loading branch information
Maksim Moskvitin
authored and
Chromium LUCI CQ
committed
Apr 6, 2022
1 parent
8dfcc68
commit ad2943a
Showing
12 changed files
with
163 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2022 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
static_library("chromeos") { | ||
sources = [ | ||
"explicit_passphrase_mojo_utils.cc", | ||
"explicit_passphrase_mojo_utils.h", | ||
] | ||
public_deps = [ "//chromeos/crosapi/mojom" ] | ||
deps = [ "//components/sync/engine" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
include_rules = [ | ||
"+chromeos/crosapi/mojom", | ||
"+components/sync/engine", | ||
] |
38 changes: 38 additions & 0 deletions
38
components/sync/chromeos/explicit_passphrase_mojo_utils.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/sync/chromeos/explicit_passphrase_mojo_utils.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "components/sync/engine/nigori/nigori.h" | ||
|
||
namespace syncer { | ||
|
||
crosapi::mojom::NigoriKeyPtr NigoriToMojo(const Nigori& nigori) { | ||
std::string deprecated_user_key; | ||
std::string encryption_key; | ||
std::string mac_key; | ||
nigori.ExportKeys(&deprecated_user_key, &encryption_key, &mac_key); | ||
|
||
crosapi::mojom::NigoriKeyPtr mojo_result = crosapi::mojom::NigoriKey::New(); | ||
mojo_result->encryption_key = | ||
std::vector<uint8_t>(encryption_key.begin(), encryption_key.end()); | ||
mojo_result->mac_key = std::vector<uint8_t>(mac_key.begin(), mac_key.end()); | ||
return mojo_result; | ||
} | ||
|
||
std::unique_ptr<Nigori> NigoriFromMojo( | ||
const crosapi::mojom::NigoriKey& mojo_nigori_key) { | ||
const std::string encryption_key(mojo_nigori_key.encryption_key.begin(), | ||
mojo_nigori_key.encryption_key.end()); | ||
const std::string mac_key(mojo_nigori_key.mac_key.begin(), | ||
mojo_nigori_key.mac_key.end()); | ||
// |user_key| is deprecated, it's safe to pass empty string. | ||
return Nigori::CreateByImport( | ||
/*user_key=*/std::string(), encryption_key, mac_key); | ||
} | ||
|
||
} // namespace syncer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_SYNC_CHROMEOS_EXPLICIT_PASSPHRASE_MOJO_UTILS_H_ | ||
#define COMPONENTS_SYNC_CHROMEOS_EXPLICIT_PASSPHRASE_MOJO_UTILS_H_ | ||
|
||
#include <memory> | ||
|
||
#include "chromeos/crosapi/mojom/sync.mojom.h" | ||
|
||
namespace syncer { | ||
|
||
class Nigori; | ||
|
||
// Converts |nigori| into its mojo representation. | ||
crosapi::mojom::NigoriKeyPtr NigoriToMojo(const Nigori& nigori); | ||
|
||
// Creates Nigori from its mojo representation. Returns nullptr if | ||
// |mojo_nigori_key| doesn't represent a valid Nigori. | ||
std::unique_ptr<Nigori> NigoriFromMojo( | ||
const crosapi::mojom::NigoriKey& mojo_nigori_key); | ||
|
||
} // namespace syncer | ||
|
||
#endif // COMPONENTS_SYNC_CHROMEOS_EXPLICIT_PASSPHRASE_MOJO_UTILS_H_ |
62 changes: 62 additions & 0 deletions
62
components/sync/chromeos/explicit_passphrase_mojo_utils_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/sync/chromeos/explicit_passphrase_mojo_utils.h" | ||
|
||
#include <string> | ||
|
||
#include "chromeos/crosapi/mojom/sync.mojom.h" | ||
#include "components/sync/engine/nigori/key_derivation_params.h" | ||
#include "components/sync/engine/nigori/nigori.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace syncer { | ||
|
||
namespace { | ||
|
||
using testing::Eq; | ||
using testing::IsNull; | ||
using testing::NotNull; | ||
|
||
TEST(ExplicitPassphraseMojoUtilsTest, ShouldConvertNigoriToMojoAndBack) { | ||
auto nigori1 = Nigori::CreateByDerivation( | ||
KeyDerivationParams::CreateForPbkdf2(), "password"); | ||
ASSERT_THAT(nigori1, NotNull()); | ||
|
||
auto mojo_nigori_key = NigoriToMojo(*nigori1); | ||
ASSERT_FALSE(mojo_nigori_key.is_null()); | ||
|
||
auto nigori2 = NigoriFromMojo(*mojo_nigori_key); | ||
ASSERT_THAT(nigori2, NotNull()); | ||
|
||
std::string deprecated_user_key1; | ||
std::string encryption_key1; | ||
std::string mac_key1; | ||
nigori1->ExportKeys(&deprecated_user_key1, &encryption_key1, &mac_key1); | ||
|
||
std::string deprecated_user_key2; | ||
std::string encryption_key2; | ||
std::string mac_key2; | ||
nigori2->ExportKeys(&deprecated_user_key2, &encryption_key2, &mac_key2); | ||
// Don't check user key, because it's deprecated and safe to ignore. | ||
EXPECT_THAT(encryption_key1, Eq(encryption_key2)); | ||
EXPECT_THAT(mac_key1, Eq(mac_key2)); | ||
} | ||
|
||
TEST(ExplicitPassphraseMojoUtilsTest, ShouldFailMojoToNigoriIfMojoEmpty) { | ||
auto empty_mojo_nigori_key = crosapi::mojom::NigoriKey::New(); | ||
EXPECT_THAT(NigoriFromMojo(*empty_mojo_nigori_key), IsNull()); | ||
} | ||
|
||
TEST(ExplicitPassphraseMojoUtilsTest, ShouldFailMojoToNigoriIfMojoNotValid) { | ||
auto invalid_mojo_nigori_key = crosapi::mojom::NigoriKey::New(); | ||
invalid_mojo_nigori_key->encryption_key = {1, 2, 3}; | ||
invalid_mojo_nigori_key->mac_key = {1, 2, 3}; | ||
EXPECT_THAT(NigoriFromMojo(*invalid_mojo_nigori_key), IsNull()); | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace syncer |