blob: 365941e61cd830f4491909255cba01a976d99b23 [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"
Shawn Willden6507c272016-01-05 22:51:48 -070029#include "entropy.h"
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070030#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
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
63 ResponseCode initialize(const android::String8& pw, Entropy* entropy);
64
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070065 ResponseCode copyMasterKey(LockedUserState<UserState>* src);
66 ResponseCode copyMasterKeyFile(LockedUserState<UserState>* src);
Shawn Willden6507c272016-01-05 22:51:48 -070067 ResponseCode writeMasterKey(const android::String8& pw, Entropy* entropy);
68 ResponseCode readMasterKey(const android::String8& pw, Entropy* entropy);
69
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070070 const uint8_t* getEncryptionKey() const { return &mMasterKey[0]; }
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:
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 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;
94 int8_t mRetry;
95
96 uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES];
97 uint8_t mSalt[SALT_SIZE];
Shawn Willden6507c272016-01-05 22:51:48 -070098};
99
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700100bool operator<(uid_t userId, const UserState& rhs);
101
102class 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 Willden6507c272016-01-05 22:51:48 -0700132#endif // KEYSTORE_USER_STATE_H_