blob: b0671e39dad43531081c4858b167bff8462de500 [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
58 int8_t getRetry() const { return mRetry; }
59
60 void zeroizeMasterKeysInMemory();
61 bool deleteMasterKey();
62
Branden Archer44d1afa2018-12-28 09:10:49 -080063 ResponseCode initialize(const android::String8& pw);
Shawn Willden6507c272016-01-05 22:51:48 -070064
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070065 ResponseCode copyMasterKey(LockedUserState<UserState>* src);
66 ResponseCode copyMasterKeyFile(LockedUserState<UserState>* src);
Branden Archer44d1afa2018-12-28 09:10:49 -080067 ResponseCode writeMasterKey(const android::String8& pw);
68 ResponseCode readMasterKey(const android::String8& pw);
Shawn Willden6507c272016-01-05 22:51:48 -070069
Branden Archerf5953d72019-01-10 09:08:18 -080070 const std::vector<uint8_t>& getEncryptionKey() const { return mMasterKey; }
Shawn Willden6507c272016-01-05 22:51:48 -070071
72 bool reset();
73
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070074 bool operator<(const UserState& rhs) const;
75 bool operator<(uid_t userId) const;
76
Shawn Willden6507c272016-01-05 22:51:48 -070077 private:
Branden Archercefbcbc2018-12-28 12:24:53 -080078 static const int SHA1_DIGEST_SIZE_BYTES = 16;
79 static const int SHA256_DIGEST_SIZE_BYTES = 32;
80
Branden Archerd0315732019-01-10 14:56:05 -080081 static const int MASTER_KEY_SIZE_BYTES = SHA256_DIGEST_SIZE_BYTES;
Shawn Willden6507c272016-01-05 22:51:48 -070082 static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8;
83
84 static const int MAX_RETRY = 4;
85 static const size_t SALT_SIZE = 16;
86
Branden Archerf5953d72019-01-10 09:08:18 -080087 void generateKeyFromPassword(std::vector<uint8_t>& key, const android::String8& pw,
Shawn Willden6507c272016-01-05 22:51:48 -070088 uint8_t* salt);
Branden Archer44d1afa2018-12-28 09:10:49 -080089 bool generateSalt();
90 bool generateMasterKey();
Shawn Willden6507c272016-01-05 22:51:48 -070091 void setupMasterKeys();
92
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070093 KeyBlobEntry mMasterKeyEntry;
94
Shawn Willden6507c272016-01-05 22:51:48 -070095 uid_t mUserId;
Shawn Willden6507c272016-01-05 22:51:48 -070096 State mState;
97 int8_t mRetry;
98
Branden Archerf5953d72019-01-10 09:08:18 -080099 std::vector<uint8_t> mMasterKey;
Shawn Willden6507c272016-01-05 22:51:48 -0700100 uint8_t mSalt[SALT_SIZE];
Shawn Willden6507c272016-01-05 22:51:48 -0700101};
102
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700103bool operator<(uid_t userId, const UserState& rhs);
104
105class UserStateDB {
106 public:
107 LockedUserState<UserState> getUserState(uid_t userId);
108 LockedUserState<UserState> getUserStateByUid(uid_t uid);
109 LockedUserState<const UserState> getUserState(uid_t userId) const;
110 LockedUserState<const UserState> getUserStateByUid(uid_t uid) const;
111
112 private:
113 mutable std::set<const UserState*> locked_state_;
114 mutable std::mutex locked_state_mutex_;
115 mutable std::condition_variable locked_state_mutex_cond_var_;
116
117 template <typename UserState>
118 LockedUserState<UserState> get(std::unique_lock<std::mutex> lock, UserState* entry) const {
119 locked_state_mutex_cond_var_.wait(
120 lock, [&] { return locked_state_.find(entry) == locked_state_.end(); });
121 locked_state_.insert(entry);
122 return {entry, [&](UserState* entry) {
123 std::unique_lock<std::mutex> lock(locked_state_mutex_);
124 locked_state_.erase(entry);
125 lock.unlock();
126 locked_state_mutex_cond_var_.notify_all();
127 }};
128 }
129
130 std::map<uid_t, UserState> mMasterKeys;
131};
132
133} // namespace keystore
134
Shawn Willden6507c272016-01-05 22:51:48 -0700135#endif // KEYSTORE_USER_STATE_H_