Commit ac31f879 authored by hansberry's avatar hansberry Committed by Commit bot

Tweak CryptauthDeviceManager to store fetched BeaconSeed data in Base64URL...

Tweak CryptauthDeviceManager to store fetched BeaconSeed data in Base64URL encoding, and retrieve it as raw data. This is necessary because CryptauthDeviceManager assumed that BeacondSeed data from the server was Base64 encoded, when in reality it was raw.

BUG=678145

Review-Url: https://codereview.chromium.org/2611913002
Cr-Commit-Position: refs/heads/master@{#441450}
parent 678cad15
...@@ -68,9 +68,14 @@ std::unique_ptr<base::ListValue> BeaconSeedsToListValue( ...@@ -68,9 +68,14 @@ std::unique_ptr<base::ListValue> BeaconSeedsToListValue(
std::unique_ptr<base::DictionaryValue> beacon_seed_value( std::unique_ptr<base::DictionaryValue> beacon_seed_value(
new base::DictionaryValue()); new base::DictionaryValue());
// Note: Seed data is already base-64 encoded, so there is no need to // Note that the |BeaconSeed|s' data is stored in Base64Url encoding because
// convert it. // dictionary values must be valid UTF8 strings.
beacon_seed_value->SetString(kExternalDeviceKeyBeaconSeedData, seed.data()); std::string seed_data_b64;
base::Base64UrlEncode(seed.data(),
base::Base64UrlEncodePolicy::INCLUDE_PADDING,
&seed_data_b64);
beacon_seed_value->SetString(kExternalDeviceKeyBeaconSeedData,
seed_data_b64);
// Set the timestamps as string representations of their numeric value // Set the timestamps as string representations of their numeric value
// since there is no notion of a base::LongValue. // since there is no notion of a base::LongValue.
...@@ -168,19 +173,27 @@ void AddBeaconSeedsToExternalDevice( ...@@ -168,19 +173,27 @@ void AddBeaconSeedsToExternalDevice(
continue; continue;
} }
std::string data, start_time_millis_str, end_time_millis_str; std::string seed_data_b64, start_time_millis_str, end_time_millis_str;
if (!seed_dictionary->GetString(kExternalDeviceKeyBeaconSeedData, &data) if (!seed_dictionary->GetString(kExternalDeviceKeyBeaconSeedData,
|| !seed_dictionary->GetString( &seed_data_b64) ||
kExternalDeviceKeyBeaconSeedStartMs, !seed_dictionary->GetString(kExternalDeviceKeyBeaconSeedStartMs,
&start_time_millis_str) &start_time_millis_str) ||
|| !seed_dictionary->GetString( !seed_dictionary->GetString(kExternalDeviceKeyBeaconSeedEndMs,
kExternalDeviceKeyBeaconSeedEndMs, &end_time_millis_str)) {
&end_time_millis_str)) {
PA_LOG(WARNING) << "Unable to deserialize BeaconSeed due to missing " PA_LOG(WARNING) << "Unable to deserialize BeaconSeed due to missing "
<< "data; skipping."; << "data; skipping.";
continue; continue;
} }
// Seed data is returned as raw data, not in Base64 encoding.
std::string seed_data;
if (!base::Base64UrlDecode(seed_data_b64,
base::Base64UrlDecodePolicy::REQUIRE_PADDING,
&seed_data)) {
PA_LOG(WARNING) << "Decoding seed data failed.";
continue;
}
int64_t start_time_millis, end_time_millis; int64_t start_time_millis, end_time_millis;
if (!base::StringToInt64(start_time_millis_str, &start_time_millis) if (!base::StringToInt64(start_time_millis_str, &start_time_millis)
|| !base::StringToInt64(end_time_millis_str, &end_time_millis)) { || !base::StringToInt64(end_time_millis_str, &end_time_millis)) {
...@@ -190,7 +203,7 @@ void AddBeaconSeedsToExternalDevice( ...@@ -190,7 +203,7 @@ void AddBeaconSeedsToExternalDevice(
} }
cryptauth::BeaconSeed* seed = external_device.add_beacon_seeds(); cryptauth::BeaconSeed* seed = external_device.add_beacon_seeds();
seed->set_data(data); seed->set_data(seed_data);
seed->set_start_time_millis(start_time_millis); seed->set_start_time_millis(start_time_millis);
seed->set_end_time_millis(end_time_millis); seed->set_end_time_millis(end_time_millis);
} }
......
...@@ -258,13 +258,17 @@ void ExpectSyncedDevicesAndPrefAreEqual( ...@@ -258,13 +258,17 @@ void ExpectSyncedDevicesAndPrefAreEqual(
const base::DictionaryValue* seed; const base::DictionaryValue* seed;
ASSERT_TRUE(beacon_seeds_from_prefs->GetDictionary(i, &seed)); ASSERT_TRUE(beacon_seeds_from_prefs->GetDictionary(i, &seed));
std::string data, start_ms, end_ms; std::string data_b64, start_ms, end_ms;
EXPECT_TRUE(seed->GetString("beacon_seed_data", &data)); EXPECT_TRUE(seed->GetString("beacon_seed_data", &data_b64));
EXPECT_TRUE(seed->GetString("beacon_seed_start_ms", &start_ms)); EXPECT_TRUE(seed->GetString("beacon_seed_start_ms", &start_ms));
EXPECT_TRUE(seed->GetString("beacon_seed_end_ms", &end_ms)); EXPECT_TRUE(seed->GetString("beacon_seed_end_ms", &end_ms));
const cryptauth::BeaconSeed& expected_seed = const cryptauth::BeaconSeed& expected_seed =
expected_device.beacon_seeds((int) i); expected_device.beacon_seeds((int) i);
std::string data;
EXPECT_TRUE(base::Base64UrlDecode(
data_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING, &data));
EXPECT_TRUE(expected_seed.has_data()); EXPECT_TRUE(expected_seed.has_data());
EXPECT_EQ(expected_seed.data(), data); EXPECT_EQ(expected_seed.data(), data);
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <cstring> #include <cstring>
#include "base/base64.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/sys_byteorder.h" #include "base/sys_byteorder.h"
......
...@@ -39,12 +39,12 @@ const int64_t kDefaultCurrentPeriodStart = 1577836800000L; ...@@ -39,12 +39,12 @@ const int64_t kDefaultCurrentPeriodStart = 1577836800000L;
// 1:43am on 1/1/2020. // 1:43am on 1/1/2020.
const int64_t kDefaultCurrentTime = 1577843000000L; const int64_t kDefaultCurrentTime = 1577843000000L;
// Base64 encoded values for: "firstSeed", "secondSeed", "thirdSeed" and // The Base64 encoded values of these raw data strings are, respectively:
// "fourthSeed". // "Zmlyc3RTZWVk", "c2Vjb25kU2VlZA==", "dGhpcmRTZWVk","Zm91cnRoU2VlZA==".
const std::string kFirstSeed = "Zmlyc3RTZWVk"; const std::string kFirstSeed = "firstSeed";
const std::string kSecondSeed = "c2Vjb25kU2VlZA=="; const std::string kSecondSeed = "secondSeed";
const std::string kThirdSeed = "dGhpcmRTZWVk"; const std::string kThirdSeed = "thirdSeed";
const std::string kFourthSeed = "Zm91cnRoU2VlZA=="; const std::string kFourthSeed = "fourthSeed";
const std::string kDefaultAdvertisingDevicePublicKey = "publicKey"; const std::string kDefaultAdvertisingDevicePublicKey = "publicKey";
......
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