blob: a1dc6a2b40717cc5af6de2abd5a2fe45df12122d [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>
36
37namespace keystore {
38
39class UserState;
40
41template <typename UserState> using LockedUserState = ProxyLock<UnlockProxyLockHelper<UserState>>;
Shawn Willden6507c272016-01-05 22:51:48 -070042
43class UserState {
44 public:
Chih-Hung Hsiehd7791be2016-07-12 11:58:02 -070045 explicit UserState(uid_t userId);
Shawn Willden6507c272016-01-05 22:51:48 -070046
47 bool initialize();
48
49 uid_t getUserId() const { return mUserId; }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070050 const std::string& getUserDirName() const { return mMasterKeyEntry.user_dir(); }
Shawn Willden6507c272016-01-05 22:51:48 -070051
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070052 std::string getMasterKeyFileName() const { return mMasterKeyEntry.getKeyBlobPath(); }
Shawn Willden6507c272016-01-05 22:51:48 -070053
54 void setState(State state);
55 State getState() const { return mState; }
56
57 int8_t getRetry() const { return mRetry; }
58
59 void zeroizeMasterKeysInMemory();
60 bool deleteMasterKey();
61
Branden Archer44d1afa2018-12-28 09:10:49 -080062 ResponseCode initialize(const android::String8& pw);
Shawn Willden6507c272016-01-05 22:51:48 -070063
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070064 ResponseCode copyMasterKey(LockedUserState<UserState>* src);
65 ResponseCode copyMasterKeyFile(LockedUserState<UserState>* src);
Branden Archer44d1afa2018-12-28 09:10:49 -080066 ResponseCode writeMasterKey(const android::String8& pw);
67 ResponseCode readMasterKey(const android::String8& pw);
Shawn Willden6507c272016-01-05 22:51:48 -070068
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070069 const uint8_t* getEncryptionKey() const { return &mMasterKey[0]; }
Shawn Willden6507c272016-01-05 22:51:48 -070070
71 bool reset();
72
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070073 bool operator<(const UserState& rhs) const;
74 bool operator<(uid_t userId) const;
75
Shawn Willden6507c272016-01-05 22:51:48 -070076 private:
Branden Archercefbcbc2018-12-28 12:24:53 -080077 static const int SHA1_DIGEST_SIZE_BYTES = 16;
78 static const int SHA256_DIGEST_SIZE_BYTES = 32;
79
80 static const int MASTER_KEY_SIZE_BYTES = SHA1_DIGEST_SIZE_BYTES;
Shawn Willden6507c272016-01-05 22:51:48 -070081 static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8;
82
83 static const int MAX_RETRY = 4;
84 static const size_t SALT_SIZE = 16;
85
86 void generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw,
87 uint8_t* salt);
Branden Archer44d1afa2018-12-28 09:10:49 -080088 bool generateSalt();
89 bool generateMasterKey();
Shawn Willden6507c272016-01-05 22:51:48 -070090 void setupMasterKeys();
91
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070092 KeyBlobEntry mMasterKeyEntry;
93
Shawn Willden6507c272016-01-05 22:51:48 -070094 uid_t mUserId;
Shawn Willden6507c272016-01-05 22:51:48 -070095 State mState;
96 int8_t mRetry;
97
98 uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES];
99 uint8_t mSalt[SALT_SIZE];
Shawn Willden6507c272016-01-05 22:51:48 -0700100};
101
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700102bool operator<(uid_t userId, const UserState& rhs);
103
104class UserStateDB {
105 public:
106 LockedUserState<UserState> getUserState(uid_t userId);
107 LockedUserState<UserState> getUserStateByUid(uid_t uid);
108 LockedUserState<const UserState> getUserState(uid_t userId) const;
109 LockedUserState<const UserState> getUserStateByUid(uid_t uid) const;
110
111 private:
112 mutable std::set<const UserState*> locked_state_;
113 mutable std::mutex locked_state_mutex_;
114 mutable std::condition_variable locked_state_mutex_cond_var_;
115
116 template <typename UserState>
117 LockedUserState<UserState> get(std::unique_lock<std::mutex> lock, UserState* entry) const {
118 locked_state_mutex_cond_var_.wait(
119 lock, [&] { return locked_state_.find(entry) == locked_state_.end(); });
120 locked_state_.insert(entry);
121 return {entry, [&](UserState* entry) {
122 std::unique_lock<std::mutex> lock(locked_state_mutex_);
123 locked_state_.erase(entry);
124 lock.unlock();
125 locked_state_mutex_cond_var_.notify_all();
126 }};
127 }
128
129 std::map<uid_t, UserState> mMasterKeys;
130};
131
132} // namespace keystore
133
Shawn Willden6507c272016-01-05 22:51:48 -0700134#endif // KEYSTORE_USER_STATE_H_