| 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 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
| 6 #define COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 6 #define COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/variations/metrics.h" |
| 15 | 16 |
| 16 class PrefService; | 17 class PrefService; |
| 17 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
| 18 | 19 |
| 19 namespace variations { | 20 namespace variations { |
| 20 | 21 |
| 21 class VariationsSeed; | 22 class VariationsSeed; |
| 22 | 23 |
| 23 // VariationsSeedStore is a helper class for reading and writing the variations | 24 // VariationsSeedStore is a helper class for reading and writing the variations |
| 24 // seed from Local State. | 25 // seed from Local State. |
| 25 class VariationsSeedStore { | 26 class VariationsSeedStore { |
| 26 public: | 27 public: |
| 27 explicit VariationsSeedStore(PrefService* local_state); | 28 explicit VariationsSeedStore(PrefService* local_state); |
| 28 virtual ~VariationsSeedStore(); | 29 virtual ~VariationsSeedStore(); |
| 29 | 30 |
| 30 // Loads the variations seed data from local state into |seed|. If there is a | 31 // Loads the variations seed data from local state into |seed|. If there is a |
| 31 // problem with loading, the pref value is cleared and false is returned. If | 32 // problem with loading, the pref value is cleared and false is returned. If |
| 32 // successful, |seed| will contain the loaded data and true is returned. | 33 // successful, |seed| will contain the loaded data and true is returned. |
| 33 bool LoadSeed(VariationsSeed* seed); | 34 bool LoadSeed(VariationsSeed* seed) WARN_UNUSED_RESULT; |
| 34 | 35 |
| 35 // Stores the given seed |data| (serialized protobuf) to local state, along | 36 // Stores the given seed |data| (serialized protobuf) to local state, along |
| 36 // with a base64-encoded digital signature for seed and the date when it was | 37 // with a base64-encoded digital signature for seed and the date when it was |
| 37 // fetched. If |is_gzip_compressed| is true, treats |data| as being gzip | 38 // fetched. If |is_gzip_compressed| is true, treats |data| as being gzip |
| 38 // compressed and decompresses it before any other processing. | 39 // compressed and decompresses it before any other processing. |
| 39 // If |is_delta_compressed| is true, treats |data| as being delta | 40 // If |is_delta_compressed| is true, treats |data| as being delta |
| 40 // compressed and attempts to decode it first using the store's seed data. | 41 // compressed and attempts to decode it first using the store's seed data. |
| 41 // The actual seed data will be base64 encoded for storage. If the string | 42 // The actual seed data will be base64 encoded for storage. If the string |
| 42 // is invalid, the existing prefs are untouched and false is returned. | 43 // is invalid, the existing prefs are untouched and false is returned. |
| 43 // Additionally, stores the |country_code| that was received with the seed in | 44 // Additionally, stores the |country_code| that was received with the seed in |
| 44 // a separate pref. On success and if |parsed_seed| is not NULL, |parsed_seed| | 45 // a separate pref. On success and if |parsed_seed| is not NULL, |parsed_seed| |
| 45 // will be filled with the de-serialized decoded protobuf. | 46 // will be filled with the de-serialized decoded protobuf. |
| 46 bool StoreSeedData(const std::string& data, | 47 bool StoreSeedData(const std::string& data, |
| 47 const std::string& base64_seed_signature, | 48 const std::string& base64_seed_signature, |
| 48 const std::string& country_code, | 49 const std::string& country_code, |
| 49 const base::Time& date_fetched, | 50 const base::Time& date_fetched, |
| 50 bool is_delta_compressed, | 51 bool is_delta_compressed, |
| 51 bool is_gzip_compressed, | 52 bool is_gzip_compressed, |
| 52 VariationsSeed* parsed_seed); | 53 VariationsSeed* parsed_seed) WARN_UNUSED_RESULT; |
| 53 | 54 |
| 54 // Updates |kVariationsSeedDate| and logs when previous date was from a | 55 // Updates |kVariationsSeedDate| and logs when previous date was from a |
| 55 // different day. | 56 // different day. |
| 56 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); | 57 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); |
| 57 | 58 |
| 58 // Reports to UMA that the seed format specified by the server is unsupported. | 59 // Reports to UMA that the seed format specified by the server is unsupported. |
| 59 void ReportUnsupportedSeedFormatError(); | 60 void ReportUnsupportedSeedFormatError(); |
| 60 | 61 |
| 61 // Returns the serial number of the last loaded or stored seed. | 62 // Returns the serial number of the last loaded or stored seed. |
| 62 const std::string& variations_serial_number() const { | 63 const std::string& variations_serial_number() const { |
| 63 return variations_serial_number_; | 64 return variations_serial_number_; |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Returns the invalid signature in base64 format, or an empty string if the | 67 // Returns the invalid signature in base64 format, or an empty string if the |
| 67 // signature was valid, missing, or if signature verification is disabled. | 68 // signature was valid, missing, or if signature verification is disabled. |
| 68 std::string GetInvalidSignature() const; | 69 std::string GetInvalidSignature() const; |
| 69 | 70 |
| 70 // Registers Local State prefs used by this class. | 71 // Registers Local State prefs used by this class. |
| 71 static void RegisterPrefs(PrefRegistrySimple* registry); | 72 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 72 | 73 |
| 73 protected: | 74 protected: |
| 74 // Note: UMA histogram enum - don't re-order or remove entries. | 75 // Whether signature verification is enabled. Overridable for tests. |
| 75 enum VerifySignatureResult { | 76 virtual bool SignatureVerificationEnabled(); |
| 76 VARIATIONS_SEED_SIGNATURE_MISSING, | |
| 77 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED, | |
| 78 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | |
| 79 VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | |
| 80 VARIATIONS_SEED_SIGNATURE_VALID, | |
| 81 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE, | |
| 82 }; | |
| 83 | |
| 84 // Verifies a variations seed (the serialized proto bytes) with the specified | |
| 85 // base-64 encoded signature that was received from the server and returns the | |
| 86 // result. The signature is assumed to be an "ECDSA with SHA-256" signature | |
| 87 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of | |
| 88 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature | |
| 89 // verification is not enabled. | |
| 90 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | |
| 91 const std::string& seed_bytes, | |
| 92 const std::string& base64_seed_signature); | |
| 93 | 77 |
| 94 private: | 78 private: |
| 95 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); | 79 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); |
| 96 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch); | 80 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch); |
| 97 | 81 |
| 98 // Clears all prefs related to variations seed storage. | 82 // Clears all prefs related to variations seed storage. |
| 99 void ClearPrefs(); | 83 void ClearPrefs(); |
| 100 | 84 |
| 101 #if defined(OS_ANDROID) | 85 #if defined(OS_ANDROID) |
| 102 // Imports the variations seed data from Java side during the first | 86 // Imports the variations seed data from Java side during the first |
| 103 // Chrome for Android run. | 87 // Chrome for Android run. |
| 104 void ImportFirstRunJavaSeed(); | 88 void ImportFirstRunJavaSeed(); |
| 105 #endif // OS_ANDROID | 89 #endif // OS_ANDROID |
| 106 | 90 |
| 107 // Reads the variations seed data from prefs; returns true on success. | 91 // Reads the variations seed data from prefs into |seed_data|, and returns the |
| 108 bool ReadSeedData(std::string* seed_data); | 92 // result of the load. The value stored into |seed_data| should only be used |
| 93 // if the result is SUCCESS. |
| 94 // Side-effect: If the read fails, clears the prefs associated with the seed. |
| 95 LoadSeedResult ReadSeedData(std::string* seed_data) WARN_UNUSED_RESULT; |
| 109 | 96 |
| 110 // Internal version of |StoreSeedData()| that assumes |seed_data| is not delta | 97 // Internal version of |StoreSeedData()| that assumes |seed_data| is not delta |
| 111 // compressed. | 98 // compressed. |
| 112 bool StoreSeedDataNoDelta(const std::string& seed_data, | 99 bool StoreSeedDataNoDelta(const std::string& seed_data, |
| 113 const std::string& base64_seed_signature, | 100 const std::string& base64_seed_signature, |
| 114 const std::string& country_code, | 101 const std::string& country_code, |
| 115 const base::Time& date_fetched, | 102 const base::Time& date_fetched, |
| 116 VariationsSeed* parsed_seed); | 103 VariationsSeed* parsed_seed) WARN_UNUSED_RESULT; |
| 117 | 104 |
| 118 // Applies a delta-compressed |patch| to |existing_data|, producing the result | 105 // Applies a delta-compressed |patch| to |existing_data|, producing the result |
| 119 // in |output|. Returns whether the operation was successful. | 106 // in |output|. Returns whether the operation was successful. |
| 120 static bool ApplyDeltaPatch(const std::string& existing_data, | 107 static bool ApplyDeltaPatch(const std::string& existing_data, |
| 121 const std::string& patch, | 108 const std::string& patch, |
| 122 std::string* output); | 109 std::string* output) WARN_UNUSED_RESULT; |
| 123 | 110 |
| 124 // The pref service used to persist the variations seed. | 111 // The pref service used to persist the variations seed. |
| 125 PrefService* local_state_; | 112 PrefService* local_state_; |
| 126 | 113 |
| 127 // Cached serial number from the most recently fetched variations seed. | 114 // Cached serial number from the most recently fetched variations seed. |
| 128 std::string variations_serial_number_; | 115 std::string variations_serial_number_; |
| 129 | 116 |
| 130 // Keeps track of an invalid signature. | 117 // Keeps track of an invalid signature. |
| 131 std::string invalid_base64_signature_; | 118 std::string invalid_base64_signature_; |
| 132 | 119 |
| 133 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); | 120 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); |
| 134 }; | 121 }; |
| 135 | 122 |
| 136 } // namespace variations | 123 } // namespace variations |
| 137 | 124 |
| 138 #endif // COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 125 #endif // COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
| OLD | NEW |