Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 | |
| 5 | #ifndef U2FD_USER_STATE_H_ |
| 6 | #define U2FD_USER_STATE_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/files/file_util.h> |
| 12 | #include <base/optional.h> |
| 13 | #include <brillo/secure_blob.h> |
| 14 | #include <dbus/bus.h> |
| 15 | #include <session_manager/dbus-proxies.h> |
| 16 | |
| 17 | namespace u2f { |
| 18 | |
| 19 | // Encapsulates access to user-specific U2F state. This class is not |
| 20 | // thread-safe. |
| 21 | class UserState { |
| 22 | public: |
Louis Collard | 243acd6 | 2019-04-25 15:42:57 +0800 | [diff] [blame] | 23 | // Constructs a new UserState object using the specified dbus object. |
| 24 | // The counter values returned by this object will be >= counter_min. |
Ben Chan | b50bc5d | 2019-09-19 12:07:32 -0700 | [diff] [blame] | 25 | UserState(org::chromium::SessionManagerInterfaceProxy* sm_proxy, |
| 26 | uint32_t counter_min); |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 27 | |
Louis Collard | aef4b48 | 2019-08-30 14:57:20 +0800 | [diff] [blame] | 28 | virtual ~UserState() = default; |
| 29 | |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 30 | // Get*() methods return base::nullopt if user state is currently |
| 31 | // unavailable. |
| 32 | |
| 33 | // Get the user secret. |
Louis Collard | aef4b48 | 2019-08-30 14:57:20 +0800 | [diff] [blame] | 34 | virtual base::Optional<brillo::SecureBlob> GetUserSecret(); |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 35 | |
Louis Collard | a98adda | 2019-08-01 17:08:29 +0800 | [diff] [blame] | 36 | // Returns the current counter value. The returned value must not be |
| 37 | // returned externally until the counter has succesfully been |
| 38 | // incremented (and persisted to disk). |
Louis Collard | aef4b48 | 2019-08-30 14:57:20 +0800 | [diff] [blame] | 39 | virtual base::Optional<std::vector<uint8_t>> GetCounter(); |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 40 | |
Louis Collard | a98adda | 2019-08-01 17:08:29 +0800 | [diff] [blame] | 41 | // Increments the counter value, which is subsequently immediately |
| 42 | // flushed to disk. Returns true on success, false if the counter |
| 43 | // could not be persisted to disk. |
Louis Collard | aef4b48 | 2019-08-30 14:57:20 +0800 | [diff] [blame] | 44 | virtual bool IncrementCounter(); |
| 45 | |
Yicheng Li | 30b6abc | 2020-11-13 14:51:15 -0800 | [diff] [blame] | 46 | // Sets a callback that is invoked when a primary session started, with the |
| 47 | // username. |
Yicheng Li | 9e90296 | 2020-11-01 11:07:11 -0800 | [diff] [blame] | 48 | virtual void SetSessionStartedCallback( |
| 49 | base::RepeatingCallback<void(const std::string&)> callback); |
Yicheng Li | 30b6abc | 2020-11-13 14:51:15 -0800 | [diff] [blame] | 50 | // Sets a callback that is invoked when the user session stopped. |
Yicheng Li | 9e90296 | 2020-11-01 11:07:11 -0800 | [diff] [blame] | 51 | virtual void SetSessionStoppedCallback( |
| 52 | base::RepeatingCallback<void()> callback); |
| 53 | |
Yicheng Li | 30b6abc | 2020-11-13 14:51:15 -0800 | [diff] [blame] | 54 | // Returns if there is a known primary session username. |
| 55 | virtual bool HasUser(); |
Yicheng Li | 67abd18 | 2020-11-18 15:31:41 -0800 | [diff] [blame] | 56 | // Returns the known primary session username. |
| 57 | virtual base::Optional<std::string> GetUser(); |
| 58 | // Returns the sanitized username. |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 59 | virtual base::Optional<std::string> GetSanitizedUser(); |
| 60 | |
Louis Collard | aef4b48 | 2019-08-30 14:57:20 +0800 | [diff] [blame] | 61 | protected: |
| 62 | // Constructor for use by mock objects. |
| 63 | UserState(); |
Louis Collard | a98adda | 2019-08-01 17:08:29 +0800 | [diff] [blame] | 64 | |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 65 | private: |
| 66 | // Handler for the SessionStateChanged signal. |
| 67 | void OnSessionStateChanged(const std::string& state); |
| 68 | |
| 69 | // Fetches the sanitized username for the primary session. |
| 70 | void UpdatePrimarySessionSanitizedUser(); |
| 71 | |
| 72 | // Attempts to load state for the current primary session. |
| 73 | void LoadState(); |
| 74 | |
| 75 | // The following methods assume that a sanitized_user_ is currently available. |
| 76 | |
| 77 | // Loads an existing user secret if available, or creates a new secret if not. |
| 78 | void LoadOrCreateUserSecret(); |
| 79 | |
| 80 | // Loads an existing user secret from disk. |
| 81 | void LoadUserSecret(const base::FilePath& path); |
| 82 | |
| 83 | // Creates a new user secret and stores it on disk, blocks until the secret |
| 84 | // has been flushed to disk. |
| 85 | void CreateUserSecret(const base::FilePath& path); |
| 86 | |
| 87 | // Loads the counter from disk. |
| 88 | void LoadCounter(); |
| 89 | |
| 90 | // Persists the counter to disk, blocks until the counter has been flushed to |
| 91 | // disk. |
| 92 | bool PersistCounter(); |
| 93 | |
Yicheng Li | 9e90296 | 2020-11-01 11:07:11 -0800 | [diff] [blame] | 94 | // Current username, if any. |
| 95 | base::Optional<std::string> user_; |
| 96 | |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 97 | // Current sanitized username, if any. |
| 98 | base::Optional<std::string> sanitized_user_; |
| 99 | |
| 100 | base::Optional<brillo::SecureBlob> user_secret_; |
| 101 | base::Optional<uint32_t> counter_; |
| 102 | |
Louis Collard | d0c2361 | 2019-05-24 10:04:48 +0800 | [diff] [blame] | 103 | org::chromium::SessionManagerInterfaceProxy* sm_proxy_; |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 104 | base::WeakPtrFactory<UserState> weak_ptr_factory_; |
Louis Collard | 243acd6 | 2019-04-25 15:42:57 +0800 | [diff] [blame] | 105 | |
Yicheng Li | 9e90296 | 2020-11-01 11:07:11 -0800 | [diff] [blame] | 106 | base::RepeatingCallback<void(const std::string&)> session_started_callback_; |
| 107 | base::RepeatingCallback<void()> session_stopped_callback_; |
| 108 | |
Louis Collard | 243acd6 | 2019-04-25 15:42:57 +0800 | [diff] [blame] | 109 | const uint32_t counter_min_; |
Louis Collard | f59aa94 | 2019-02-25 17:50:14 +0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace u2f |
| 113 | |
| 114 | #endif // U2FD_USER_STATE_H_ |