Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [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 ANDROID_TWRP_KEYSTORAGE_H |
| 18 | #define ANDROID_TWRP_KEYSTORAGE_H |
| 19 | |
Peter Cai | 90edd2e | 2019-05-23 16:32:22 +0800 | [diff] [blame] | 20 | #include "Keymaster4.h" |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 21 | #include "KeyBuffer.h" |
Peter Cai | 90edd2e | 2019-05-23 16:32:22 +0800 | [diff] [blame] | 22 | #include <ext4_utils/ext4_crypt.h> |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 23 | |
| 24 | #include <string> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace vold { |
| 28 | |
Peter Cai | 90edd2e | 2019-05-23 16:32:22 +0800 | [diff] [blame] | 29 | namespace km = ::android::hardware::keymaster::V4_0; |
| 30 | |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 31 | // Represents the information needed to decrypt a disk encryption key. |
| 32 | // If "token" is nonempty, it is passed in as a required Gatekeeper auth token. |
| 33 | // If "token" and "secret" are nonempty, "secret" is appended to the application-specific |
| 34 | // binary needed to unlock. |
| 35 | // If only "secret" is nonempty, it is used to decrypt in a non-Keymaster process. |
| 36 | class KeyAuthentication { |
| 37 | public: |
| 38 | KeyAuthentication(std::string t, std::string s) : token{t}, secret{s} {}; |
| 39 | |
| 40 | bool usesKeymaster() const { return !token.empty() || secret.empty(); }; |
| 41 | |
| 42 | const std::string token; |
| 43 | const std::string secret; |
| 44 | }; |
| 45 | |
Peter Cai | 90edd2e | 2019-05-23 16:32:22 +0800 | [diff] [blame] | 46 | enum class KeyType { |
| 47 | DE_SYS, |
| 48 | DE_USER, |
| 49 | CE_USER |
| 50 | }; |
| 51 | |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 52 | extern const KeyAuthentication kEmptyAuthentication; |
| 53 | |
| 54 | // Checks if path "path" exists. |
| 55 | bool pathExists(const std::string& path); |
| 56 | |
| 57 | bool createSecdiscardable(const std::string& path, std::string* hash); |
| 58 | bool readSecdiscardable(const std::string& path, std::string* hash); |
| 59 | |
| 60 | // Create a directory at the named path, and store "key" in it, |
| 61 | // in such a way that it can only be retrieved via Keymaster and |
| 62 | // can be securely deleted. |
| 63 | // It's safe to move/rename the directory after creation. |
| 64 | bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBuffer& key); |
| 65 | |
| 66 | // Create a directory at the named path, and store "key" in it as storeKey |
| 67 | // This version creates the key in "tmp_path" then atomically renames "tmp_path" |
| 68 | // to "key_path" thereby ensuring that the key is either stored entirely or |
| 69 | // not at all. |
| 70 | bool storeKeyAtomically(const std::string& key_path, const std::string& tmp_path, |
| 71 | const KeyAuthentication& auth, const KeyBuffer& key); |
| 72 | |
| 73 | // Retrieve the key from the named directory. |
| 74 | bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffer* key); |
| 75 | |
| 76 | // Securely destroy the key stored in the named directory and delete the directory. |
| 77 | bool destroyKey(const std::string& dir); |
| 78 | |
| 79 | bool runSecdiscardSingle(const std::string& file); |
Peter Cai | 90edd2e | 2019-05-23 16:32:22 +0800 | [diff] [blame] | 80 | |
| 81 | bool generateWrappedKey(userid_t user_id, KeyType key_type, KeyBuffer* key); |
| 82 | bool getEphemeralWrappedKey(km::KeyFormat format, KeyBuffer& kmKey, KeyBuffer* key); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 83 | } // namespace vold |
| 84 | } // namespace android |
| 85 | |
| 86 | #endif |