Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef KEYSTORE_USER_STATE_H_ |
| 18 | #define KEYSTORE_USER_STATE_H_ |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <openssl/aes.h> |
| 23 | |
| 24 | #include <utils/String8.h> |
| 25 | |
| 26 | #include <keystore/keystore.h> |
| 27 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 28 | #include "blob.h" |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 29 | #include "entropy.h" |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 30 | #include "keystore_utils.h" |
| 31 | |
| 32 | #include <android-base/logging.h> |
| 33 | #include <condition_variable> |
| 34 | #include <keystore/keystore_concurrency.h> |
| 35 | #include <mutex> |
| 36 | #include <set> |
| 37 | |
| 38 | namespace keystore { |
| 39 | |
| 40 | class UserState; |
| 41 | |
| 42 | template <typename UserState> using LockedUserState = ProxyLock<UnlockProxyLockHelper<UserState>>; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 43 | |
| 44 | class UserState { |
| 45 | public: |
Chih-Hung Hsieh | d7791be | 2016-07-12 11:58:02 -0700 | [diff] [blame] | 46 | explicit UserState(uid_t userId); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 47 | |
| 48 | bool initialize(); |
| 49 | |
| 50 | uid_t getUserId() const { return mUserId; } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 51 | const std::string& getUserDirName() const { return mMasterKeyEntry.user_dir(); } |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 52 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 53 | std::string getMasterKeyFileName() const { return mMasterKeyEntry.getKeyBlobPath(); } |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 54 | |
| 55 | void setState(State state); |
| 56 | State getState() const { return mState; } |
| 57 | |
| 58 | int8_t getRetry() const { return mRetry; } |
| 59 | |
| 60 | void zeroizeMasterKeysInMemory(); |
| 61 | bool deleteMasterKey(); |
| 62 | |
| 63 | ResponseCode initialize(const android::String8& pw, Entropy* entropy); |
| 64 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 65 | ResponseCode copyMasterKey(LockedUserState<UserState>* src); |
| 66 | ResponseCode copyMasterKeyFile(LockedUserState<UserState>* src); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 67 | ResponseCode writeMasterKey(const android::String8& pw, Entropy* entropy); |
| 68 | ResponseCode readMasterKey(const android::String8& pw, Entropy* entropy); |
| 69 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 70 | const uint8_t* getEncryptionKey() const { return &mMasterKey[0]; } |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 71 | |
| 72 | bool reset(); |
| 73 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 74 | bool operator<(const UserState& rhs) const; |
| 75 | bool operator<(uid_t userId) const; |
| 76 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 77 | private: |
| 78 | static const int MASTER_KEY_SIZE_BYTES = 16; |
| 79 | static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8; |
| 80 | |
| 81 | static const int MAX_RETRY = 4; |
| 82 | static const size_t SALT_SIZE = 16; |
| 83 | |
| 84 | void generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw, |
| 85 | uint8_t* salt); |
| 86 | bool generateSalt(Entropy* entropy); |
| 87 | bool generateMasterKey(Entropy* entropy); |
| 88 | void setupMasterKeys(); |
| 89 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 90 | KeyBlobEntry mMasterKeyEntry; |
| 91 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 92 | uid_t mUserId; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 93 | State mState; |
| 94 | int8_t mRetry; |
| 95 | |
| 96 | uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES]; |
| 97 | uint8_t mSalt[SALT_SIZE]; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame^] | 100 | bool operator<(uid_t userId, const UserState& rhs); |
| 101 | |
| 102 | class UserStateDB { |
| 103 | public: |
| 104 | LockedUserState<UserState> getUserState(uid_t userId); |
| 105 | LockedUserState<UserState> getUserStateByUid(uid_t uid); |
| 106 | LockedUserState<const UserState> getUserState(uid_t userId) const; |
| 107 | LockedUserState<const UserState> getUserStateByUid(uid_t uid) const; |
| 108 | |
| 109 | private: |
| 110 | mutable std::set<const UserState*> locked_state_; |
| 111 | mutable std::mutex locked_state_mutex_; |
| 112 | mutable std::condition_variable locked_state_mutex_cond_var_; |
| 113 | |
| 114 | template <typename UserState> |
| 115 | LockedUserState<UserState> get(std::unique_lock<std::mutex> lock, UserState* entry) const { |
| 116 | locked_state_mutex_cond_var_.wait( |
| 117 | lock, [&] { return locked_state_.find(entry) == locked_state_.end(); }); |
| 118 | locked_state_.insert(entry); |
| 119 | return {entry, [&](UserState* entry) { |
| 120 | std::unique_lock<std::mutex> lock(locked_state_mutex_); |
| 121 | locked_state_.erase(entry); |
| 122 | lock.unlock(); |
| 123 | locked_state_mutex_cond_var_.notify_all(); |
| 124 | }}; |
| 125 | } |
| 126 | |
| 127 | std::map<uid_t, UserState> mMasterKeys; |
| 128 | }; |
| 129 | |
| 130 | } // namespace keystore |
| 131 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 132 | #endif // KEYSTORE_USER_STATE_H_ |