blob: 810681381ccf4e046a109dc1ad052adf4123d69f [file] [log] [blame]
[email protected]1df24d0a2014-01-20 21:29:591// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
blundell0b2305c2015-08-25 15:54:425#ifndef COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_
6#define COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_
[email protected]1df24d0a2014-01-20 21:29:597
8#include <string>
9
10#include "base/compiler_specific.h"
[email protected]c69b6c12014-03-07 20:32:3611#include "base/gtest_prod_util.h"
[email protected]1df24d0a2014-01-20 21:29:5912#include "base/time/time.h"
13
14class PrefService;
15class PrefRegistrySimple;
16
[email protected]59b6f672014-07-26 18:35:4717namespace variations {
[email protected]24afce12014-07-25 21:00:3118class VariationsSeed;
[email protected]59b6f672014-07-26 18:35:4719}
20
21namespace chrome_variations {
[email protected]24afce12014-07-25 21:00:3122
[email protected]1df24d0a2014-01-20 21:29:5923// VariationsSeedStore is a helper class for reading and writing the variations
24// seed from Local State.
25class VariationsSeedStore {
26 public:
27 explicit VariationsSeedStore(PrefService* local_state);
28 virtual ~VariationsSeedStore();
29
30 // 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 // successful, |seed| will contain the loaded data and true is returned.
[email protected]59b6f672014-07-26 18:35:4733 bool LoadSeed(variations::VariationsSeed* seed);
[email protected]1df24d0a2014-01-20 21:29:5934
asvitkineb24f45592015-08-07 02:57:2535 // Stores the given seed |data| (serialized protobuf) to local state, along
[email protected]54af732e2014-01-23 22:20:3936 // with a base64-encoded digital signature for seed and the date when it was
asvitkineb24f45592015-08-07 02:57:2537 // fetched. If |is_delta_compressed| is true, treats |data| as being delta
38 // compressed and attempts to decode it first using the store's seed data.
39 // The actual seed data will be base64 encoded for storage. If the string
40 // is invalid, the existing prefs are untouched and false is returned.
41 // Additionally, stores the |country_code| that was received with the seed in
42 // a separate pref. On success and if |parsed_seed| is not NULL, |parsed_seed|
43 // will be filled with the de-serialized decoded protobuf.
44 bool StoreSeedData(const std::string& data,
[email protected]54af732e2014-01-23 22:20:3945 const std::string& base64_seed_signature,
asvitkineb24f45592015-08-07 02:57:2546 const std::string& country_code,
[email protected]6839813b2014-05-12 22:49:4047 const base::Time& date_fetched,
asvitkineb24f45592015-08-07 02:57:2548 bool is_delta_compressed,
[email protected]59b6f672014-07-26 18:35:4749 variations::VariationsSeed* parsed_seed);
[email protected]1df24d0a2014-01-20 21:29:5950
[email protected]344c623a2014-03-11 20:29:3951 // Updates |kVariationsSeedDate| and logs when previous date was from a
52 // different day.
53 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched);
54
[email protected]1df24d0a2014-01-20 21:29:5955 // Returns the serial number of the last loaded or stored seed.
56 const std::string& variations_serial_number() const {
57 return variations_serial_number_;
58 }
59
asvitkineb24f45592015-08-07 02:57:2560 // Returns whether the last loaded or stored seed has the country field set.
61 bool seed_has_country_code() const {
62 return seed_has_country_code_;
63 }
[email protected]1df24d0a2014-01-20 21:29:5964
grt7d521392014-12-15 18:04:2965 // Returns the invalid signature in base64 format, or an empty string if the
66 // signature was valid, missing, or if signature verification is disabled.
67 std::string GetInvalidSignature() const;
68
asvitkineb24f45592015-08-07 02:57:2569 // Registers Local State prefs used by this class.
70 static void RegisterPrefs(PrefRegistrySimple* registry);
71
[email protected]c69b6c12014-03-07 20:32:3672 protected:
73 // Note: UMA histogram enum - don't re-order or remove entries.
74 enum VerifySignatureResult {
75 VARIATIONS_SEED_SIGNATURE_MISSING,
76 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED,
77 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE,
78 VARIATIONS_SEED_SIGNATURE_INVALID_SEED,
79 VARIATIONS_SEED_SIGNATURE_VALID,
80 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE,
81 };
82
83 // Verifies a variations seed (the serialized proto bytes) with the specified
84 // base-64 encoded signature that was received from the server and returns the
85 // result. The signature is assumed to be an "ECDSA with SHA-256" signature
86 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of
87 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature
88 // verification is not enabled.
89 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature(
90 const std::string& seed_bytes,
91 const std::string& base64_seed_signature);
92
[email protected]1df24d0a2014-01-20 21:29:5993 private:
[email protected]c69b6c12014-03-07 20:32:3694 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature);
asvitkineb24f45592015-08-07 02:57:2595 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch);
[email protected]c69b6c12014-03-07 20:32:3696
[email protected]54af732e2014-01-23 22:20:3997 // Clears all prefs related to variations seed storage.
98 void ClearPrefs();
99
asvitkine5ea32c52015-02-12 01:23:11100 // Reads the variations seed data from prefs; returns true on success.
101 bool ReadSeedData(std::string* seed_data);
102
asvitkineb24f45592015-08-07 02:57:25103 // Internal version of |StoreSeedData()| that assumes |seed_data| is not delta
104 // compressed.
105 bool StoreSeedDataNoDelta(
106 const std::string& seed_data,
107 const std::string& base64_seed_signature,
108 const std::string& country_code,
109 const base::Time& date_fetched,
110 variations::VariationsSeed* parsed_seed);
111
112 // Applies a delta-compressed |patch| to |existing_data|, producing the result
113 // in |output|. Returns whether the operation was successful.
114 static bool ApplyDeltaPatch(const std::string& existing_data,
115 const std::string& patch,
116 std::string* output);
117
[email protected]1df24d0a2014-01-20 21:29:59118 // The pref service used to persist the variations seed.
119 PrefService* local_state_;
120
121 // Cached serial number from the most recently fetched variations seed.
122 std::string variations_serial_number_;
123
asvitkineb24f45592015-08-07 02:57:25124 // Whether the most recently fetched variations seed has the country code
125 // field set.
126 bool seed_has_country_code_;
127
grt7d521392014-12-15 18:04:29128 // Keeps track of an invalid signature.
129 std::string invalid_base64_signature_;
130
[email protected]1df24d0a2014-01-20 21:29:59131 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore);
132};
133
134} // namespace chrome_variations
135
blundell0b2305c2015-08-25 15:54:42136#endif // COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_