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 | |
| 28 | #include "entropy.h" |
| 29 | |
| 30 | class UserState { |
| 31 | public: |
Chih-Hung Hsieh | d7791be | 2016-07-12 11:58:02 -0700 | [diff] [blame] | 32 | explicit UserState(uid_t userId); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 33 | ~UserState(); |
| 34 | |
| 35 | bool initialize(); |
| 36 | |
| 37 | uid_t getUserId() const { return mUserId; } |
| 38 | const char* getUserDirName() const { return mUserDir; } |
| 39 | |
| 40 | const char* getMasterKeyFileName() const { return mMasterKeyFile; } |
| 41 | |
| 42 | void setState(State state); |
| 43 | State getState() const { return mState; } |
| 44 | |
| 45 | int8_t getRetry() const { return mRetry; } |
| 46 | |
| 47 | void zeroizeMasterKeysInMemory(); |
| 48 | bool deleteMasterKey(); |
| 49 | |
| 50 | ResponseCode initialize(const android::String8& pw, Entropy* entropy); |
| 51 | |
| 52 | ResponseCode copyMasterKey(UserState* src); |
| 53 | ResponseCode copyMasterKeyFile(UserState* src); |
| 54 | ResponseCode writeMasterKey(const android::String8& pw, Entropy* entropy); |
| 55 | ResponseCode readMasterKey(const android::String8& pw, Entropy* entropy); |
| 56 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 57 | auto& getEncryptionKey() const { return mMasterKey; } |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 58 | |
| 59 | bool reset(); |
| 60 | |
| 61 | private: |
| 62 | static const int MASTER_KEY_SIZE_BYTES = 16; |
| 63 | static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8; |
| 64 | |
| 65 | static const int MAX_RETRY = 4; |
| 66 | static const size_t SALT_SIZE = 16; |
| 67 | |
| 68 | void generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw, |
| 69 | uint8_t* salt); |
| 70 | bool generateSalt(Entropy* entropy); |
| 71 | bool generateMasterKey(Entropy* entropy); |
| 72 | void setupMasterKeys(); |
| 73 | |
| 74 | uid_t mUserId; |
| 75 | |
| 76 | char* mUserDir; |
| 77 | char* mMasterKeyFile; |
| 78 | |
| 79 | State mState; |
| 80 | int8_t mRetry; |
| 81 | |
| 82 | uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES]; |
| 83 | uint8_t mSalt[SALT_SIZE]; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | #endif // KEYSTORE_USER_STATE_H_ |