blob: 75d99d91d57dbd4f3331aa99b92068cd691b706b [file] [log] [blame]
Shawn Willden6507c272016-01-05 22:51:48 -07001/*
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 Danisevskisff3d7f42018-10-08 07:15:09 -070028#include "blob.h"
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070029#include "keystore_utils.h"
30
31#include <android-base/logging.h>
32#include <condition_variable>
33#include <keystore/keystore_concurrency.h>
34#include <mutex>
35#include <set>
Branden Archerf5953d72019-01-10 09:08:18 -080036#include <vector>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070037
38namespace keystore {
39
40class UserState;
41
42template <typename UserState> using LockedUserState = ProxyLock<UnlockProxyLockHelper<UserState>>;
Shawn Willden6507c272016-01-05 22:51:48 -070043
44class UserState {
45 public:
Chih-Hung Hsiehd7791be2016-07-12 11:58:02 -070046 explicit UserState(uid_t userId);
Shawn Willden6507c272016-01-05 22:51:48 -070047
48 bool initialize();
49
50 uid_t getUserId() const { return mUserId; }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070051 const std::string& getUserDirName() const { return mMasterKeyEntry.user_dir(); }
Shawn Willden6507c272016-01-05 22:51:48 -070052
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070053 std::string getMasterKeyFileName() const { return mMasterKeyEntry.getKeyBlobPath(); }
Shawn Willden6507c272016-01-05 22:51:48 -070054
55 void setState(State state);
56 State getState() const { return mState; }
57
Shawn Willden6507c272016-01-05 22:51:48 -070058 void zeroizeMasterKeysInMemory();
59 bool deleteMasterKey();
60
Branden Archer44d1afa2018-12-28 09:10:49 -080061 ResponseCode initialize(const android::String8& pw);
Shawn Willden6507c272016-01-05 22:51:48 -070062
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070063 ResponseCode copyMasterKey(LockedUserState<UserState>* src);
64 ResponseCode copyMasterKeyFile(LockedUserState<UserState>* src);
Branden Archer44d1afa2018-12-28 09:10:49 -080065 ResponseCode writeMasterKey(const android::String8& pw);
66 ResponseCode readMasterKey(const android::String8& pw);
Shawn Willden6507c272016-01-05 22:51:48 -070067
Branden Archerf5953d72019-01-10 09:08:18 -080068 const std::vector<uint8_t>& getEncryptionKey() const { return mMasterKey; }
Shawn Willden6507c272016-01-05 22:51:48 -070069
70 bool reset();
71
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070072 bool operator<(const UserState& rhs) const;
73 bool operator<(uid_t userId) const;
74
Shawn Willden6507c272016-01-05 22:51:48 -070075 private:
Shawn Willden17b87092019-10-01 17:43:43 -060076 static constexpr int SHA1_DIGEST_SIZE_BYTES = 16;
77 static constexpr int SHA256_DIGEST_SIZE_BYTES = 32;
Branden Archercefbcbc2018-12-28 12:24:53 -080078
Shawn Willden17b87092019-10-01 17:43:43 -060079 static constexpr int MASTER_KEY_SIZE_BYTES = kAes256KeySizeBytes;
80 static constexpr int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8;
Shawn Willden6507c272016-01-05 22:51:48 -070081
Shawn Willden17b87092019-10-01 17:43:43 -060082 static constexpr size_t SALT_SIZE = 16;
Shawn Willden6507c272016-01-05 22:51:48 -070083
Branden Archerf5953d72019-01-10 09:08:18 -080084 void generateKeyFromPassword(std::vector<uint8_t>& key, const android::String8& pw,
Shawn Willden6507c272016-01-05 22:51:48 -070085 uint8_t* salt);
Branden Archer44d1afa2018-12-28 09:10:49 -080086 bool generateSalt();
87 bool generateMasterKey();
Shawn Willden6507c272016-01-05 22:51:48 -070088 void setupMasterKeys();
89
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070090 KeyBlobEntry mMasterKeyEntry;
91
Shawn Willden6507c272016-01-05 22:51:48 -070092 uid_t mUserId;
Shawn Willden6507c272016-01-05 22:51:48 -070093 State mState;
Shawn Willden6507c272016-01-05 22:51:48 -070094
Branden Archerf5953d72019-01-10 09:08:18 -080095 std::vector<uint8_t> mMasterKey;
Shawn Willden6507c272016-01-05 22:51:48 -070096 uint8_t mSalt[SALT_SIZE];
Shawn Willden6507c272016-01-05 22:51:48 -070097};
98
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070099bool operator<(uid_t userId, const UserState& rhs);
100
101class UserStateDB {
102 public:
103 LockedUserState<UserState> getUserState(uid_t userId);
104 LockedUserState<UserState> getUserStateByUid(uid_t uid);
105 LockedUserState<const UserState> getUserState(uid_t userId) const;
106 LockedUserState<const UserState> getUserStateByUid(uid_t uid) const;
107
108 private:
109 mutable std::set<const UserState*> locked_state_;
110 mutable std::mutex locked_state_mutex_;
111 mutable std::condition_variable locked_state_mutex_cond_var_;
112
113 template <typename UserState>
114 LockedUserState<UserState> get(std::unique_lock<std::mutex> lock, UserState* entry) const {
115 locked_state_mutex_cond_var_.wait(
116 lock, [&] { return locked_state_.find(entry) == locked_state_.end(); });
117 locked_state_.insert(entry);
118 return {entry, [&](UserState* entry) {
119 std::unique_lock<std::mutex> lock(locked_state_mutex_);
120 locked_state_.erase(entry);
121 lock.unlock();
122 locked_state_mutex_cond_var_.notify_all();
123 }};
124 }
125
126 std::map<uid_t, UserState> mMasterKeys;
127};
128
129} // namespace keystore
130
Shawn Willden6507c272016-01-05 22:51:48 -0700131#endif // KEYSTORE_USER_STATE_H_