Commit ceb1c173 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

Revert "[Nearby Share] Add ToDictionary, FromDictionary to PrivateCertificate"

This reverts commit 341bb752.

Reason for revert: This broke the CQ.  See:
https://ci.chromium.org/p/chromium/builders/try/win-libfuzzer-asan-rel/472138?
https://ci.chromium.org/p/chromium/builders/try/linux_chromium_tsan_rel_ng/604561?
etc.

Main builders also look to be failing.

Original change's description:
> [Nearby Share] Add ToDictionary, FromDictionary to PrivateCertificate
> 
> ToDictionary and FromDictionary methods convert to/from a Value
> dictionary to facilitate storing PrivateCertificates in Prefs.
> 
> Change-Id: I92117836af94cd224eeea34edc7dd55dd3eb7484
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2284043
> Reviewed-by: Josh Nohle <nohle@chromium.org>
> Commit-Queue: Curt Clemens <cclem@google.com>
> Cr-Commit-Position: refs/heads/master@{#786027}

TBR=nohle@chromium.org,cclem@google.com

Change-Id: I3a840d2bcdc09ff8ac6db1926a1a7339e121ca04
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2286470Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786109}
parent c728a3a4
...@@ -6,12 +6,8 @@ ...@@ -6,12 +6,8 @@
#include <utility> #include <utility>
#include "base/base64url.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/rand_util.h" #include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/util/values/values_util.h"
#include "chrome/browser/nearby_sharing/certificates/common.h" #include "chrome/browser/nearby_sharing/certificates/common.h"
#include "chrome/browser/nearby_sharing/certificates/constants.h" #include "chrome/browser/nearby_sharing/certificates/constants.h"
#include "chrome/browser/nearby_sharing/proto/timestamp.pb.h" #include "chrome/browser/nearby_sharing/proto/timestamp.pb.h"
...@@ -26,17 +22,6 @@ ...@@ -26,17 +22,6 @@
namespace { namespace {
// Dictionary keys used in ToDictionary, FromDictionary
const char kVisibility[] = "visibility";
const char kNotBefore[] = "not_before";
const char kNotAfter[] = "not_after";
const char kKeyPair[] = "key_pair";
const char kSecretKey[] = "secret_key";
const char kMetadataEncryptionKey[] = "metadata_encryption_key";
const char kId[] = "id";
const char kUnencryptedMetadata[] = "unencrypted_metadata";
const char kConsumedSalts[] = "consumed_salts";
std::vector<uint8_t> GenerateRandomBytes(size_t num_bytes) { std::vector<uint8_t> GenerateRandomBytes(size_t num_bytes) {
std::vector<uint8_t> bytes(num_bytes); std::vector<uint8_t> bytes(num_bytes);
crypto::RandBytes(bytes); crypto::RandBytes(bytes);
...@@ -76,63 +61,6 @@ base::Optional<std::vector<uint8_t>> CreateMetadataEncryptionKeyTag( ...@@ -76,63 +61,6 @@ base::Optional<std::vector<uint8_t>> CreateMetadataEncryptionKeyTag(
return result; return result;
} }
std::string EncodeString(const std::string& unencoded_string) {
std::string encoded_string;
base::Base64UrlEncode(unencoded_string,
base::Base64UrlEncodePolicy::INCLUDE_PADDING,
&encoded_string);
return encoded_string;
}
base::Optional<std::string> DecodeString(const std::string* encoded_string) {
if (!encoded_string)
return base::nullopt;
std::string decoded_string;
if (!base::Base64UrlDecode(*encoded_string,
base::Base64UrlDecodePolicy::REQUIRE_PADDING,
&decoded_string)) {
return base::nullopt;
}
return decoded_string;
}
std::string BytesToEncodedString(const std::vector<uint8_t>& bytes) {
return EncodeString(std::string(bytes.begin(), bytes.end()));
}
base::Optional<std::vector<uint8_t>> EncodedStringToBytes(
const std::string* str) {
base::Optional<std::string> decoded_str = DecodeString(str);
return decoded_str ? base::make_optional<std::vector<uint8_t>>(
decoded_str->begin(), decoded_str->end())
: base::nullopt;
}
std::string SaltsToString(const std::set<std::vector<uint8_t>>& salts) {
std::string str;
str.reserve(salts.size() * 2 * kNearbyShareNumBytesMetadataEncryptionKeySalt);
for (const std::vector<uint8_t>& salt : salts) {
str += base::HexEncode(salt);
}
return str;
}
std::set<std::vector<uint8_t>> StringToSalts(const std::string& str) {
const size_t chars_per_salt =
2 * kNearbyShareNumBytesMetadataEncryptionKeySalt;
DCHECK(str.size() % chars_per_salt == 0);
std::set<std::vector<uint8_t>> salts;
for (size_t i = 0; i < str.size(); i += chars_per_salt) {
std::vector<uint8_t> salt;
base::HexStringToBytes(base::StringPiece(&str[i], chars_per_salt), &salt);
salts.insert(std::move(salt));
}
return salts;
}
} // namespace } // namespace
NearbySharePrivateCertificate::NearbySharePrivateCertificate( NearbySharePrivateCertificate::NearbySharePrivateCertificate(
...@@ -269,102 +197,6 @@ NearbySharePrivateCertificate::ToPublicCertificate() { ...@@ -269,102 +197,6 @@ NearbySharePrivateCertificate::ToPublicCertificate() {
return public_certificate; return public_certificate;
} }
base::Value NearbySharePrivateCertificate::ToDictionary() const {
base::Value dict(base::Value::Type::DICTIONARY);
dict.SetIntKey(kVisibility, static_cast<int>(visibility_));
dict.SetKey(kNotBefore, util::TimeToValue(not_before_));
dict.SetKey(kNotAfter, util::TimeToValue(not_after_));
std::vector<uint8_t> key_pair;
key_pair_->ExportPrivateKey(&key_pair);
dict.SetStringKey(kKeyPair, BytesToEncodedString(key_pair));
dict.SetStringKey(kSecretKey, EncodeString(secret_key_->key()));
dict.SetStringKey(kMetadataEncryptionKey,
BytesToEncodedString(metadata_encryption_key_));
dict.SetStringKey(kId, BytesToEncodedString(id_));
dict.SetStringKey(kUnencryptedMetadata,
EncodeString(unencrypted_metadata_.SerializeAsString()));
dict.SetStringKey(kConsumedSalts, SaltsToString(consumed_salts_));
return dict;
}
base::Optional<NearbySharePrivateCertificate>
NearbySharePrivateCertificate::FromDictionary(const base::Value& dict) {
base::Optional<int> int_opt;
const std::string* str_ptr;
base::Optional<std::string> str_opt;
base::Optional<base::Time> time_opt;
base::Optional<std::vector<uint8_t>> bytes_opt;
int_opt = dict.FindIntPath(kVisibility);
if (!int_opt)
return base::nullopt;
NearbyShareVisibility visibility =
static_cast<NearbyShareVisibility>(*int_opt);
time_opt = util::ValueToTime(dict.FindPath(kNotBefore));
if (!time_opt)
return base::nullopt;
base::Time not_before = *time_opt;
time_opt = util::ValueToTime(dict.FindPath(kNotAfter));
if (!time_opt)
return base::nullopt;
base::Time not_after = *time_opt;
bytes_opt = EncodedStringToBytes(dict.FindStringPath(kKeyPair));
if (!bytes_opt)
return base::nullopt;
std::unique_ptr<crypto::ECPrivateKey> key_pair =
crypto::ECPrivateKey::CreateFromPrivateKeyInfo(*bytes_opt);
str_opt = DecodeString(dict.FindStringPath(kSecretKey));
if (!str_opt)
return base::nullopt;
std::unique_ptr<crypto::SymmetricKey> secret_key =
crypto::SymmetricKey::Import(crypto::SymmetricKey::Algorithm::AES,
*str_opt);
bytes_opt = EncodedStringToBytes(dict.FindStringPath(kMetadataEncryptionKey));
if (!bytes_opt)
return base::nullopt;
std::vector<uint8_t> metadata_encryption_key = *bytes_opt;
bytes_opt = EncodedStringToBytes(dict.FindStringPath(kId));
if (!bytes_opt)
return base::nullopt;
std::vector<uint8_t> id = *bytes_opt;
str_opt = DecodeString(dict.FindStringPath(kUnencryptedMetadata));
if (!str_opt)
return base::nullopt;
nearbyshare::proto::EncryptedMetadata unencrypted_metadata;
if (!unencrypted_metadata.ParseFromString(*str_opt))
return base::nullopt;
str_ptr = dict.FindStringPath(kConsumedSalts);
if (!str_ptr)
return base::nullopt;
std::set<std::vector<uint8_t>> consumed_salts = StringToSalts(*str_ptr);
return NearbySharePrivateCertificate(
visibility, not_before, not_after, std::move(key_pair),
std::move(secret_key), std::move(metadata_encryption_key), std::move(id),
std::move(unencrypted_metadata), std::move(consumed_salts));
}
base::Optional<std::vector<uint8_t>> base::Optional<std::vector<uint8_t>>
NearbySharePrivateCertificate::GenerateUnusedSalt() { NearbySharePrivateCertificate::GenerateUnusedSalt() {
if (consumed_salts_.size() >= kNearbyShareMaxNumMetadataEncryptionKeySalts) { if (consumed_salts_.size() >= kNearbyShareMaxNumMetadataEncryptionKeySalts) {
......
...@@ -11,10 +11,8 @@ ...@@ -11,10 +11,8 @@
#include "base/containers/queue.h" #include "base/containers/queue.h"
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/gtest_prod_util.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_encrypted_metadata_key.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_encrypted_metadata_key.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_visibility.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_visibility.h"
#include "chrome/browser/nearby_sharing/proto/encrypted_metadata.pb.h" #include "chrome/browser/nearby_sharing/proto/encrypted_metadata.pb.h"
...@@ -33,11 +31,6 @@ class SymmetricKey; ...@@ -33,11 +31,6 @@ class SymmetricKey;
// metadata encryption key, which can then be advertised. // metadata encryption key, which can then be advertised.
class NearbySharePrivateCertificate { class NearbySharePrivateCertificate {
public: public:
// Inverse operation of ToDictionary(). Returns base::nullopt if the
// conversion is not successful
static base::Optional<NearbySharePrivateCertificate> FromDictionary(
const base::Value& dict);
// Generates a random EC key pair, secret key, and metadata encryption // Generates a random EC key pair, secret key, and metadata encryption
// key. Derives the certificate ID from the secret key. Derives the // key. Derives the certificate ID from the secret key. Derives the
// not-after time from |not_before| and the certificate validity period fixed // not-after time from |not_before| and the certificate validity period fixed
...@@ -87,10 +80,6 @@ class NearbySharePrivateCertificate { ...@@ -87,10 +80,6 @@ class NearbySharePrivateCertificate {
// unsuccessful. // unsuccessful.
base::Optional<nearbyshare::proto::PublicCertificate> ToPublicCertificate(); base::Optional<nearbyshare::proto::PublicCertificate> ToPublicCertificate();
// Converts this private certificate to a dictionary value for storage
// in Prefs.
base::Value ToDictionary() const;
// For testing only. // For testing only.
base::queue<std::vector<uint8_t>>& next_salts_for_testing() { base::queue<std::vector<uint8_t>>& next_salts_for_testing() {
return next_salts_for_testing_; return next_salts_for_testing_;
...@@ -148,8 +137,6 @@ class NearbySharePrivateCertificate { ...@@ -148,8 +137,6 @@ class NearbySharePrivateCertificate {
// For testing only. // For testing only.
base::queue<std::vector<uint8_t>> next_salts_for_testing_; base::queue<std::vector<uint8_t>> next_salts_for_testing_;
base::Optional<base::TimeDelta> offset_for_testing_; base::Optional<base::TimeDelta> offset_for_testing_;
FRIEND_TEST_ALL_PREFIXES(NearbySharePrivateCertificateTest, ToFromDictionary);
}; };
#endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_PRIVATE_CERTIFICATE_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_PRIVATE_CERTIFICATE_H_
...@@ -29,33 +29,6 @@ TEST(NearbySharePrivateCertificateTest, Construction) { ...@@ -29,33 +29,6 @@ TEST(NearbySharePrivateCertificateTest, Construction) {
private_certificate.unencrypted_metadata().SerializeAsString()); private_certificate.unencrypted_metadata().SerializeAsString());
} }
TEST(NearbySharePrivateCertificateTest, ToFromDictionary) {
NearbySharePrivateCertificate before(NearbyShareVisibility::kAllContacts,
GetNearbyShareTestNotBefore(),
GetNearbyShareTestMetadata());
// Generate a few consumed salts.
for (size_t i = 0; i < 5; ++i)
ASSERT_TRUE(before.EncryptMetadataKey());
NearbySharePrivateCertificate after(
*NearbySharePrivateCertificate::FromDictionary(before.ToDictionary()));
EXPECT_EQ(before.id(), after.id());
EXPECT_EQ(before.visibility(), after.visibility());
EXPECT_EQ(before.not_before(), after.not_before());
EXPECT_EQ(before.not_after(), after.not_after());
EXPECT_EQ(before.unencrypted_metadata().SerializeAsString(),
after.unencrypted_metadata().SerializeAsString());
EXPECT_EQ(before.secret_key_->key(), after.secret_key_->key());
EXPECT_EQ(before.metadata_encryption_key_, after.metadata_encryption_key_);
EXPECT_EQ(before.consumed_salts_, after.consumed_salts_);
std::vector<uint8_t> before_private_key, after_private_key;
before.key_pair_->ExportPrivateKey(&before_private_key);
after.key_pair_->ExportPrivateKey(&after_private_key);
EXPECT_EQ(before_private_key, after_private_key);
}
TEST(NearbySharePrivateCertificateTest, EncryptMetadataKey) { TEST(NearbySharePrivateCertificateTest, EncryptMetadataKey) {
NearbySharePrivateCertificate private_certificate( NearbySharePrivateCertificate private_certificate(
NearbyShareVisibility::kAllContacts, GetNearbyShareTestNotBefore(), NearbyShareVisibility::kAllContacts, GetNearbyShareTestNotBefore(),
......
...@@ -7,10 +7,6 @@ ...@@ -7,10 +7,6 @@
// Specifies which contacts can receive the public certificate corresponding // Specifies which contacts can receive the public certificate corresponding
// to the local device's private certificate. // to the local device's private certificate.
enum class NearbyShareVisibility { enum class NearbyShareVisibility { kNoOne, kAllContacts, kSelectedContacts };
kNoOne = 0, // Integer values persisted in prefs; do not renumber.
kAllContacts = 1,
kSelectedContacts = 2
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_VISIBILITY_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_VISIBILITY_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment