Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [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 | #include "KeyStorage.h" |
| 18 | |
Daniel Rosenberg | d2906b8 | 2019-06-07 14:18:14 -0700 | [diff] [blame] | 19 | #include "Checkpoint.h" |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 20 | #include "Keystore.h" |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 21 | #include "Utils.h" |
| 22 | |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Seth Moore | 5a43d61 | 2021-01-19 17:51:51 +0000 | [diff] [blame] | 24 | #include <memory> |
| 25 | #include <mutex> |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 26 | #include <thread> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | #include <errno.h> |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <unistd.h> |
| 35 | |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 36 | #include <openssl/err.h> |
| 37 | #include <openssl/evp.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 38 | #include <openssl/sha.h> |
| 39 | |
| 40 | #include <android-base/file.h> |
| 41 | #include <android-base/logging.h> |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Daniel Rosenberg | d2906b8 | 2019-06-07 14:18:14 -0700 | [diff] [blame] | 43 | #include <android-base/unique_fd.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 44 | |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 45 | #include <cutils/properties.h> |
| 46 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 47 | namespace android { |
| 48 | namespace vold { |
| 49 | |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame] | 50 | const KeyAuthentication kEmptyAuthentication{""}; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 51 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 52 | static constexpr size_t AES_KEY_BYTES = 32; |
| 53 | static constexpr size_t GCM_NONCE_BYTES = 12; |
| 54 | static constexpr size_t GCM_MAC_BYTES = 16; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 55 | static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14; |
Paul Crowley | b3de337 | 2016-04-27 12:58:41 -0700 | [diff] [blame] | 56 | |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 57 | static const char* kCurrentVersion = "1"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 58 | static const char* kRmPath = "/system/bin/rm"; |
| 59 | static const char* kSecdiscardPath = "/system/bin/secdiscard"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 60 | static const char* kStretch_none = "none"; |
| 61 | static const char* kStretch_nopassword = "nopassword"; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 62 | static const char* kHashPrefix_secdiscardable = "Android secdiscardable SHA512"; |
| 63 | static const char* kHashPrefix_keygen = "Android key wrapping key generation SHA512"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 64 | static const char* kFn_encrypted_key = "encrypted_key"; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 65 | static const char* kFn_keymaster_key_blob = "keymaster_key_blob"; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 66 | static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 67 | static const char* kFn_secdiscardable = "secdiscardable"; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 68 | static const char* kFn_stretching = "stretching"; |
| 69 | static const char* kFn_version = "version"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 70 | |
Pig | adb9199 | 2020-09-25 22:56:33 +0800 | [diff] [blame^] | 71 | static const int32_t KM_TAG_FBE_ICE = static_cast<int32_t>(7 << 28) | 16201; |
| 72 | |
Seth Moore | 5a43d61 | 2021-01-19 17:51:51 +0000 | [diff] [blame] | 73 | namespace { |
| 74 | |
| 75 | // Storage binding info for ensuring key encryption keys include a |
| 76 | // platform-provided seed in their derivation. |
| 77 | struct StorageBindingInfo { |
| 78 | enum class State { |
| 79 | UNINITIALIZED, |
| 80 | IN_USE, // key storage keys are bound to seed |
| 81 | NOT_USED, // key storage keys are NOT bound to seed |
| 82 | }; |
| 83 | |
| 84 | // Binding seed mixed into all key storage keys. |
| 85 | std::vector<uint8_t> seed; |
| 86 | |
| 87 | // State tracker for the key storage key binding. |
| 88 | State state = State::UNINITIALIZED; |
| 89 | |
| 90 | std::mutex guard; |
| 91 | }; |
| 92 | |
| 93 | // Never freed as the dtor is non-trivial. |
| 94 | StorageBindingInfo& storage_binding_info = *new StorageBindingInfo; |
| 95 | |
| 96 | } // namespace |
| 97 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 98 | static bool checkSize(const std::string& kind, size_t actual, size_t expected) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 99 | if (actual != expected) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 100 | LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got " |
| 101 | << actual; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 107 | static void hashWithPrefix(char const* prefix, const std::string& tohash, std::string* res) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 108 | SHA512_CTX c; |
| 109 | |
| 110 | SHA512_Init(&c); |
| 111 | // Personalise the hashing by introducing a fixed prefix. |
| 112 | // Hashing applications should use personalization except when there is a |
| 113 | // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 114 | std::string hashingPrefix = prefix; |
| 115 | hashingPrefix.resize(SHA512_CBLOCK); |
| 116 | SHA512_Update(&c, hashingPrefix.data(), hashingPrefix.size()); |
| 117 | SHA512_Update(&c, tohash.data(), tohash.size()); |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 118 | res->assign(SHA512_DIGEST_LENGTH, '\0'); |
| 119 | SHA512_Final(reinterpret_cast<uint8_t*>(&(*res)[0]), &c); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 122 | // Generates a keystore key, using rollback resistance if supported. |
| 123 | static bool generateKeystoreKey(Keystore& keystore, const km::AuthorizationSetBuilder& paramBuilder, |
| 124 | std::string* key) { |
Eric Biggers | b2024e0 | 2021-03-15 12:44:36 -0700 | [diff] [blame] | 125 | auto paramsWithRollback = paramBuilder; |
| 126 | paramsWithRollback.Authorization(km::TAG_ROLLBACK_RESISTANCE); |
| 127 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 128 | if (!keystore.generateKey(paramsWithRollback, key)) { |
| 129 | LOG(WARNING) << "Failed to generate rollback-resistant key. This is expected if keystore " |
Eric Biggers | b2024e0 | 2021-03-15 12:44:36 -0700 | [diff] [blame] | 130 | "doesn't support rollback resistance. Falling back to " |
| 131 | "non-rollback-resistant key."; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 132 | if (!keystore.generateKey(paramBuilder, key)) return false; |
Eric Biggers | b2024e0 | 2021-03-15 12:44:36 -0700 | [diff] [blame] | 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 137 | static bool generateKeyStorageKey(Keystore& keystore, const std::string& appId, std::string* key) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 138 | auto paramBuilder = km::AuthorizationSetBuilder() |
| 139 | .AesEncryptionKey(AES_KEY_BYTES * 8) |
| 140 | .GcmModeMinMacLen(GCM_MAC_BYTES * 8) |
| 141 | .Authorization(km::TAG_APPLICATION_ID, appId) |
| 142 | .Authorization(km::TAG_NO_AUTH_REQUIRED); |
Satya Tangirala | 6b98fb6 | 2021-05-11 19:48:47 -0700 | [diff] [blame] | 143 | LOG(DEBUG) << "Generating \"key storage\" key"; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 144 | return generateKeystoreKey(keystore, paramBuilder, key); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 147 | bool generateWrappedStorageKey(KeyBuffer* key) { |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 148 | Keystore keystore; |
| 149 | if (!keystore) return false; |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 150 | std::string key_temp; |
| 151 | auto paramBuilder = km::AuthorizationSetBuilder().AesEncryptionKey(AES_KEY_BYTES * 8); |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 152 | paramBuilder.Authorization(km::TAG_STORAGE_KEY); |
Pig | adb9199 | 2020-09-25 22:56:33 +0800 | [diff] [blame^] | 153 | |
| 154 | km::KeyParameter param1; |
| 155 | param1.tag = (km::Tag) (KM_TAG_FBE_ICE); |
| 156 | param1.value = km::KeyParameterValue::make<km::KeyParameterValue::boolValue>(true); |
| 157 | paramBuilder.push_back(param1); |
| 158 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 159 | if (!generateKeystoreKey(keystore, paramBuilder, &key_temp)) return false; |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 160 | *key = KeyBuffer(key_temp.size()); |
| 161 | memcpy(reinterpret_cast<void*>(key->data()), key_temp.c_str(), key->size()); |
| 162 | return true; |
| 163 | } |
| 164 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 165 | bool exportWrappedStorageKey(const KeyBuffer& ksKey, KeyBuffer* key) { |
| 166 | Keystore keystore; |
| 167 | if (!keystore) return false; |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 168 | std::string key_temp; |
| 169 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 170 | if (!keystore.exportKey(ksKey, &key_temp)) return false; |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 171 | *key = KeyBuffer(key_temp.size()); |
| 172 | memcpy(reinterpret_cast<void*>(key->data()), key_temp.c_str(), key->size()); |
| 173 | return true; |
| 174 | } |
| 175 | |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame] | 176 | static km::AuthorizationSet beginParams(const std::string& appId) { |
| 177 | return km::AuthorizationSetBuilder() |
| 178 | .GcmModeMacLen(GCM_MAC_BYTES * 8) |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 179 | .Authorization(km::TAG_APPLICATION_ID, appId); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 182 | static bool readFileToString(const std::string& filename, std::string* result) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 183 | if (!android::base::ReadFileToString(filename, result)) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 184 | PLOG(ERROR) << "Failed to read from " << filename; |
| 185 | return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 190 | static bool readRandomBytesOrLog(size_t count, std::string* out) { |
| 191 | auto status = ReadRandomBytes(count, *out); |
| 192 | if (status != OK) { |
| 193 | LOG(ERROR) << "Random read failed with status: " << status; |
| 194 | return false; |
| 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | bool createSecdiscardable(const std::string& filename, std::string* hash) { |
| 200 | std::string secdiscardable; |
| 201 | if (!readRandomBytesOrLog(SECDISCARDABLE_BYTES, &secdiscardable)) return false; |
| 202 | if (!writeStringToFile(secdiscardable, filename)) return false; |
| 203 | hashWithPrefix(kHashPrefix_secdiscardable, secdiscardable, hash); |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool readSecdiscardable(const std::string& filename, std::string* hash) { |
| 208 | std::string secdiscardable; |
| 209 | if (!readFileToString(filename, &secdiscardable)) return false; |
| 210 | hashWithPrefix(kHashPrefix_secdiscardable, secdiscardable, hash); |
| 211 | return true; |
| 212 | } |
| 213 | |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 214 | static std::mutex key_upgrade_lock; |
| 215 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 216 | // List of key directories that have had their Keystore key upgraded during |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 217 | // this boot and written to "keymaster_key_blob_upgraded", but replacing the old |
| 218 | // key was delayed due to an active checkpoint. Protected by key_upgrade_lock. |
Eric Biggers | 107d21d | 2021-06-08 12:55:00 -0700 | [diff] [blame] | 219 | // A directory can be in this list at most once. |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 220 | static std::vector<std::string> key_dirs_to_commit; |
| 221 | |
| 222 | // Replaces |dir|/keymaster_key_blob with |dir|/keymaster_key_blob_upgraded and |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 223 | // deletes the old key from Keystore. |
| 224 | static bool CommitUpgradedKey(Keystore& keystore, const std::string& dir) { |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 225 | auto blob_file = dir + "/" + kFn_keymaster_key_blob; |
| 226 | auto upgraded_blob_file = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 227 | |
| 228 | std::string blob; |
| 229 | if (!readFileToString(blob_file, &blob)) return false; |
| 230 | |
| 231 | if (rename(upgraded_blob_file.c_str(), blob_file.c_str()) != 0) { |
| 232 | PLOG(ERROR) << "Failed to rename " << upgraded_blob_file << " to " << blob_file; |
| 233 | return false; |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 234 | } |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 235 | // Ensure that the rename is persisted before deleting the Keystore key. |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 236 | if (!FsyncDirectory(dir)) return false; |
| 237 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 238 | if (!keystore || !keystore.deleteKey(blob)) { |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 239 | LOG(WARNING) << "Failed to delete old key " << blob_file |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 240 | << " from Keystore; continuing anyway"; |
| 241 | // Continue on, but the space in Keystore used by the old key won't be freed. |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 242 | } |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | static void DeferredCommitKeys() { |
| 247 | android::base::WaitForProperty("vold.checkpoint_committed", "1"); |
| 248 | LOG(INFO) << "Committing upgraded keys"; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 249 | Keystore keystore; |
| 250 | if (!keystore) { |
| 251 | LOG(ERROR) << "Failed to open Keystore; old keys won't be deleted from Keystore"; |
| 252 | // Continue on, but the space in Keystore used by the old keys won't be freed. |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 253 | } |
| 254 | std::lock_guard<std::mutex> lock(key_upgrade_lock); |
| 255 | for (auto& dir : key_dirs_to_commit) { |
| 256 | LOG(INFO) << "Committing upgraded key " << dir; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 257 | CommitUpgradedKey(keystore, dir); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 258 | } |
| 259 | key_dirs_to_commit.clear(); |
| 260 | } |
| 261 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 262 | // Returns true if the Keystore key in |dir| has already been upgraded and is |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 263 | // pending being committed. Assumes that key_upgrade_lock is held. |
| 264 | static bool IsKeyCommitPending(const std::string& dir) { |
| 265 | for (const auto& dir_to_commit : key_dirs_to_commit) { |
| 266 | if (IsSameFile(dir, dir_to_commit)) return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 271 | // Schedules the upgraded Keystore key in |dir| to be committed later. Assumes |
Eric Biggers | 107d21d | 2021-06-08 12:55:00 -0700 | [diff] [blame] | 272 | // that key_upgrade_lock is held and that a commit isn't already pending for the |
| 273 | // directory. |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 274 | static void ScheduleKeyCommit(const std::string& dir) { |
| 275 | if (key_dirs_to_commit.empty()) std::thread(DeferredCommitKeys).detach(); |
| 276 | key_dirs_to_commit.push_back(dir); |
| 277 | } |
| 278 | |
| 279 | static void CancelPendingKeyCommit(const std::string& dir) { |
| 280 | std::lock_guard<std::mutex> lock(key_upgrade_lock); |
| 281 | for (auto it = key_dirs_to_commit.begin(); it != key_dirs_to_commit.end(); it++) { |
| 282 | if (IsSameFile(*it, dir)) { |
| 283 | LOG(DEBUG) << "Cancelling pending commit of upgraded key " << dir |
| 284 | << " because it is being destroyed"; |
| 285 | key_dirs_to_commit.erase(it); |
| 286 | break; |
| 287 | } |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Satya Tangirala | 0f890a9 | 2021-06-08 12:55:24 -0700 | [diff] [blame] | 291 | bool RenameKeyDir(const std::string& old_name, const std::string& new_name) { |
Satya Tangirala | 9475b11 | 2021-05-13 00:43:03 -0700 | [diff] [blame] | 292 | std::lock_guard<std::mutex> lock(key_upgrade_lock); |
| 293 | |
Eric Biggers | 107d21d | 2021-06-08 12:55:00 -0700 | [diff] [blame] | 294 | // Find the entry in key_dirs_to_commit (if any) for this directory so that |
| 295 | // we can update it if the rename succeeds. We don't allow duplicates in |
| 296 | // this list, so there can be at most one such entry. |
| 297 | auto it = key_dirs_to_commit.begin(); |
| 298 | for (; it != key_dirs_to_commit.end(); it++) { |
| 299 | if (IsSameFile(old_name, *it)) break; |
| 300 | } |
| 301 | |
Satya Tangirala | 0f890a9 | 2021-06-08 12:55:24 -0700 | [diff] [blame] | 302 | if (rename(old_name.c_str(), new_name.c_str()) != 0) { |
| 303 | PLOG(ERROR) << "Failed to rename key directory \"" << old_name << "\" to \"" << new_name |
| 304 | << "\""; |
| 305 | return false; |
| 306 | } |
Satya Tangirala | 9475b11 | 2021-05-13 00:43:03 -0700 | [diff] [blame] | 307 | |
Eric Biggers | 107d21d | 2021-06-08 12:55:00 -0700 | [diff] [blame] | 308 | if (it != key_dirs_to_commit.end()) *it = new_name; |
| 309 | |
Satya Tangirala | 9475b11 | 2021-05-13 00:43:03 -0700 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 313 | // Deletes a leftover upgraded key, if present. An upgraded key can be left |
| 314 | // over if an update failed, or if we rebooted before committing the key in a |
| 315 | // freak accident. Either way, we can re-upgrade the key if we need to. |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 316 | static void DeleteUpgradedKey(Keystore& keystore, const std::string& path) { |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 317 | if (pathExists(path)) { |
| 318 | LOG(DEBUG) << "Deleting leftover upgraded key " << path; |
| 319 | std::string blob; |
| 320 | if (!android::base::ReadFileToString(path, &blob)) { |
| 321 | LOG(WARNING) << "Failed to read leftover upgraded key " << path |
| 322 | << "; continuing anyway"; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 323 | } else if (!keystore.deleteKey(blob)) { |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 324 | LOG(WARNING) << "Failed to delete leftover upgraded key " << path |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 325 | << " from Keystore; continuing anyway"; |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 326 | } |
| 327 | if (unlink(path.c_str()) != 0) { |
| 328 | LOG(WARNING) << "Failed to unlink leftover upgraded key " << path |
| 329 | << "; continuing anyway"; |
| 330 | } |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 334 | // Begins a Keystore operation using the key stored in |dir|. |
| 335 | static KeystoreOperation BeginKeystoreOp(Keystore& keystore, const std::string& dir, |
| 336 | const km::AuthorizationSet& keyParams, |
| 337 | const km::AuthorizationSet& opParams, |
| 338 | km::AuthorizationSet* outParams) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 339 | km::AuthorizationSet inParams(keyParams); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 340 | inParams.append(opParams.begin(), opParams.end()); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 341 | |
| 342 | auto blob_file = dir + "/" + kFn_keymaster_key_blob; |
| 343 | auto upgraded_blob_file = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 344 | |
| 345 | std::lock_guard<std::mutex> lock(key_upgrade_lock); |
| 346 | |
| 347 | std::string blob; |
| 348 | bool already_upgraded = IsKeyCommitPending(dir); |
| 349 | if (already_upgraded) { |
| 350 | LOG(DEBUG) |
| 351 | << blob_file |
| 352 | << " was already upgraded and is waiting to be committed; using the upgraded blob"; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 353 | if (!readFileToString(upgraded_blob_file, &blob)) return KeystoreOperation(); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 354 | } else { |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 355 | DeleteUpgradedKey(keystore, upgraded_blob_file); |
| 356 | if (!readFileToString(blob_file, &blob)) return KeystoreOperation(); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 357 | } |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 358 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 359 | auto opHandle = keystore.begin(blob, inParams, outParams); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 360 | if (!opHandle) return opHandle; |
| 361 | |
| 362 | // If key blob wasn't upgraded, nothing left to do. |
| 363 | if (!opHandle.getUpgradedBlob()) return opHandle; |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 364 | |
| 365 | if (already_upgraded) { |
| 366 | LOG(ERROR) << "Unexpected case; already-upgraded key " << upgraded_blob_file |
| 367 | << " still requires upgrade"; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 368 | return KeystoreOperation(); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 369 | } |
| 370 | LOG(INFO) << "Upgrading key: " << blob_file; |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 371 | if (!writeStringToFile(*opHandle.getUpgradedBlob(), upgraded_blob_file)) |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 372 | return KeystoreOperation(); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 373 | if (cp_needsCheckpoint()) { |
| 374 | LOG(INFO) << "Wrote upgraded key to " << upgraded_blob_file |
| 375 | << "; delaying commit due to checkpoint"; |
| 376 | ScheduleKeyCommit(dir); |
| 377 | } else { |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 378 | if (!CommitUpgradedKey(keystore, dir)) return KeystoreOperation(); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 379 | LOG(INFO) << "Key upgraded: " << blob_file; |
| 380 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 381 | return opHandle; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 384 | static bool encryptWithKeystoreKey(Keystore& keystore, const std::string& dir, |
| 385 | const km::AuthorizationSet& keyParams, const KeyBuffer& message, |
| 386 | std::string* ciphertext) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 387 | km::AuthorizationSet opParams = |
Haiping Yang | c0a46c8 | 2021-08-23 01:24:25 +0000 | [diff] [blame] | 388 | km::AuthorizationSetBuilder().Authorization(km::TAG_PURPOSE, km::KeyPurpose::ENCRYPT); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 389 | km::AuthorizationSet outParams; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 390 | auto opHandle = BeginKeystoreOp(keystore, dir, keyParams, opParams, &outParams); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 391 | if (!opHandle) return false; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 392 | auto nonceBlob = outParams.GetTagValue(km::TAG_NONCE); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 393 | if (!nonceBlob) { |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 394 | LOG(ERROR) << "GCM encryption but no nonce generated"; |
| 395 | return false; |
| 396 | } |
| 397 | // nonceBlob here is just a pointer into existing data, must not be freed |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 398 | std::string nonce(nonceBlob.value().get().begin(), nonceBlob.value().get().end()); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 399 | if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false; |
| 400 | std::string body; |
| 401 | if (!opHandle.updateCompletely(message, &body)) return false; |
| 402 | |
| 403 | std::string mac; |
| 404 | if (!opHandle.finish(&mac)) return false; |
| 405 | if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false; |
| 406 | *ciphertext = nonce + body + mac; |
| 407 | return true; |
| 408 | } |
| 409 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 410 | static bool decryptWithKeystoreKey(Keystore& keystore, const std::string& dir, |
| 411 | const km::AuthorizationSet& keyParams, |
| 412 | const std::string& ciphertext, KeyBuffer* message) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 413 | const std::string nonce = ciphertext.substr(0, GCM_NONCE_BYTES); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 414 | auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 415 | auto opParams = km::AuthorizationSetBuilder() |
| 416 | .Authorization(km::TAG_NONCE, nonce) |
| 417 | .Authorization(km::TAG_PURPOSE, km::KeyPurpose::DECRYPT); |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 418 | auto opHandle = BeginKeystoreOp(keystore, dir, keyParams, opParams, nullptr); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 419 | if (!opHandle) return false; |
| 420 | if (!opHandle.updateCompletely(bodyAndMac, message)) return false; |
| 421 | if (!opHandle.finish(nullptr)) return false; |
| 422 | return true; |
| 423 | } |
| 424 | |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 425 | static std::string getStretching(const KeyAuthentication& auth) { |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 426 | if (auth.usesKeystore()) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 427 | return kStretch_nopassword; |
| 428 | } else { |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame] | 429 | return kStretch_none; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 430 | } |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 433 | static bool stretchSecret(const std::string& stretching, const std::string& secret, |
Satya Tangirala | 478cea9 | 2021-04-07 14:30:25 -0700 | [diff] [blame] | 434 | std::string* stretched) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 435 | if (stretching == kStretch_nopassword) { |
| 436 | if (!secret.empty()) { |
Paul Crowley | d9b9295 | 2016-03-04 13:45:00 -0800 | [diff] [blame] | 437 | LOG(WARNING) << "Password present but stretching is nopassword"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 438 | // Continue anyway |
| 439 | } |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 440 | stretched->clear(); |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 441 | } else if (stretching == kStretch_none) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 442 | *stretched = secret; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 443 | } else { |
| 444 | LOG(ERROR) << "Unknown stretching type: " << stretching; |
| 445 | return false; |
| 446 | } |
| 447 | return true; |
| 448 | } |
| 449 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 450 | static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching, |
Satya Tangirala | 478cea9 | 2021-04-07 14:30:25 -0700 | [diff] [blame] | 451 | const std::string& secdiscardable_hash, std::string* appId) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 452 | std::string stretched; |
Satya Tangirala | 478cea9 | 2021-04-07 14:30:25 -0700 | [diff] [blame] | 453 | if (!stretchSecret(stretching, auth.secret, &stretched)) return false; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 454 | *appId = secdiscardable_hash + stretched; |
Seth Moore | 5a43d61 | 2021-01-19 17:51:51 +0000 | [diff] [blame] | 455 | |
| 456 | const std::lock_guard<std::mutex> scope_lock(storage_binding_info.guard); |
| 457 | switch (storage_binding_info.state) { |
| 458 | case StorageBindingInfo::State::UNINITIALIZED: |
| 459 | storage_binding_info.state = StorageBindingInfo::State::NOT_USED; |
| 460 | break; |
| 461 | case StorageBindingInfo::State::IN_USE: |
| 462 | appId->append(storage_binding_info.seed.begin(), storage_binding_info.seed.end()); |
| 463 | break; |
| 464 | case StorageBindingInfo::State::NOT_USED: |
| 465 | // noop |
| 466 | break; |
| 467 | } |
| 468 | |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
| 472 | static void logOpensslError() { |
| 473 | LOG(ERROR) << "Openssl error: " << ERR_get_error(); |
| 474 | } |
| 475 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 476 | static bool encryptWithoutKeystore(const std::string& preKey, const KeyBuffer& plaintext, |
| 477 | std::string* ciphertext) { |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 478 | std::string key; |
| 479 | hashWithPrefix(kHashPrefix_keygen, preKey, &key); |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 480 | key.resize(AES_KEY_BYTES); |
| 481 | if (!readRandomBytesOrLog(GCM_NONCE_BYTES, ciphertext)) return false; |
| 482 | auto ctx = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>( |
| 483 | EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); |
| 484 | if (!ctx) { |
| 485 | logOpensslError(); |
| 486 | return false; |
| 487 | } |
| 488 | if (1 != EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_gcm(), NULL, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 489 | reinterpret_cast<const uint8_t*>(key.data()), |
| 490 | reinterpret_cast<const uint8_t*>(ciphertext->data()))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 491 | logOpensslError(); |
| 492 | return false; |
| 493 | } |
| 494 | ciphertext->resize(GCM_NONCE_BYTES + plaintext.size() + GCM_MAC_BYTES); |
| 495 | int outlen; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 496 | if (1 != EVP_EncryptUpdate( |
| 497 | ctx.get(), reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES), |
| 498 | &outlen, reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size())) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 499 | logOpensslError(); |
| 500 | return false; |
| 501 | } |
| 502 | if (outlen != static_cast<int>(plaintext.size())) { |
| 503 | LOG(ERROR) << "GCM ciphertext length should be " << plaintext.size() << " was " << outlen; |
| 504 | return false; |
| 505 | } |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 506 | if (1 != EVP_EncryptFinal_ex( |
| 507 | ctx.get(), |
| 508 | reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES + plaintext.size()), |
| 509 | &outlen)) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 510 | logOpensslError(); |
| 511 | return false; |
| 512 | } |
| 513 | if (outlen != 0) { |
| 514 | LOG(ERROR) << "GCM EncryptFinal should be 0, was " << outlen; |
| 515 | return false; |
| 516 | } |
| 517 | if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, GCM_MAC_BYTES, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 518 | reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES + |
| 519 | plaintext.size()))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 520 | logOpensslError(); |
| 521 | return false; |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 526 | static bool decryptWithoutKeystore(const std::string& preKey, const std::string& ciphertext, |
| 527 | KeyBuffer* plaintext) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 528 | if (ciphertext.size() < GCM_NONCE_BYTES + GCM_MAC_BYTES) { |
| 529 | LOG(ERROR) << "GCM ciphertext too small: " << ciphertext.size(); |
| 530 | return false; |
| 531 | } |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 532 | std::string key; |
| 533 | hashWithPrefix(kHashPrefix_keygen, preKey, &key); |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 534 | key.resize(AES_KEY_BYTES); |
| 535 | auto ctx = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>( |
| 536 | EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); |
| 537 | if (!ctx) { |
| 538 | logOpensslError(); |
| 539 | return false; |
| 540 | } |
| 541 | if (1 != EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_gcm(), NULL, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 542 | reinterpret_cast<const uint8_t*>(key.data()), |
| 543 | reinterpret_cast<const uint8_t*>(ciphertext.data()))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 544 | logOpensslError(); |
| 545 | return false; |
| 546 | } |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 547 | *plaintext = KeyBuffer(ciphertext.size() - GCM_NONCE_BYTES - GCM_MAC_BYTES); |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 548 | int outlen; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 549 | if (1 != EVP_DecryptUpdate(ctx.get(), reinterpret_cast<uint8_t*>(&(*plaintext)[0]), &outlen, |
| 550 | reinterpret_cast<const uint8_t*>(ciphertext.data() + GCM_NONCE_BYTES), |
| 551 | plaintext->size())) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 552 | logOpensslError(); |
| 553 | return false; |
| 554 | } |
| 555 | if (outlen != static_cast<int>(plaintext->size())) { |
| 556 | LOG(ERROR) << "GCM plaintext length should be " << plaintext->size() << " was " << outlen; |
| 557 | return false; |
| 558 | } |
| 559 | if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, GCM_MAC_BYTES, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 560 | const_cast<void*>(reinterpret_cast<const void*>( |
| 561 | ciphertext.data() + GCM_NONCE_BYTES + plaintext->size())))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 562 | logOpensslError(); |
| 563 | return false; |
| 564 | } |
| 565 | if (1 != EVP_DecryptFinal_ex(ctx.get(), |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 566 | reinterpret_cast<uint8_t*>(&(*plaintext)[0] + plaintext->size()), |
| 567 | &outlen)) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 568 | logOpensslError(); |
| 569 | return false; |
| 570 | } |
| 571 | if (outlen != 0) { |
| 572 | LOG(ERROR) << "GCM EncryptFinal should be 0, was " << outlen; |
| 573 | return false; |
| 574 | } |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 575 | return true; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Satya Tangirala | 351a4af | 2021-06-08 12:55:37 -0700 | [diff] [blame] | 578 | // Creates a directory at the given path |dir| and stores |key| in it, in such a |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 579 | // way that it can only be retrieved via Keystore (if no secret is given in |
Satya Tangirala | 351a4af | 2021-06-08 12:55:37 -0700 | [diff] [blame] | 580 | // |auth|) or with the given secret (if a secret is given in |auth|), and can be |
| 581 | // securely deleted. If a storage binding seed has been set, then the storage |
| 582 | // binding seed will be required to retrieve the key as well. |
| 583 | static bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBuffer& key) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 584 | if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) { |
| 585 | PLOG(ERROR) << "key mkdir " << dir; |
| 586 | return false; |
| 587 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 588 | if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 589 | std::string secdiscardable_hash; |
| 590 | if (!createSecdiscardable(dir + "/" + kFn_secdiscardable, &secdiscardable_hash)) return false; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 591 | std::string stretching = getStretching(auth); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 592 | if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 593 | std::string appId; |
Satya Tangirala | 478cea9 | 2021-04-07 14:30:25 -0700 | [diff] [blame] | 594 | if (!generateAppId(auth, stretching, secdiscardable_hash, &appId)) return false; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 595 | std::string encryptedKey; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 596 | if (auth.usesKeystore()) { |
| 597 | Keystore keystore; |
| 598 | if (!keystore) return false; |
| 599 | std::string ksKey; |
| 600 | if (!generateKeyStorageKey(keystore, appId, &ksKey)) return false; |
| 601 | if (!writeStringToFile(ksKey, dir + "/" + kFn_keymaster_key_blob)) return false; |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame] | 602 | km::AuthorizationSet keyParams = beginParams(appId); |
David Anderson | e179157 | 2021-11-05 18:57:49 -0700 | [diff] [blame] | 603 | if (!encryptWithKeystoreKey(keystore, dir, keyParams, key, &encryptedKey)) { |
| 604 | LOG(ERROR) << "encryptWithKeystoreKey failed"; |
| 605 | return false; |
| 606 | } |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 607 | } else { |
David Anderson | e179157 | 2021-11-05 18:57:49 -0700 | [diff] [blame] | 608 | if (!encryptWithoutKeystore(appId, key, &encryptedKey)) { |
| 609 | LOG(ERROR) << "encryptWithoutKeystore failed"; |
| 610 | return false; |
| 611 | } |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 612 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 613 | if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false; |
Paul Crowley | 621d9b9 | 2018-12-07 15:36:09 -0800 | [diff] [blame] | 614 | if (!FsyncDirectory(dir)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 615 | return true; |
| 616 | } |
| 617 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 618 | bool storeKeyAtomically(const std::string& key_path, const std::string& tmp_path, |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 619 | const KeyAuthentication& auth, const KeyBuffer& key) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 620 | if (pathExists(key_path)) { |
| 621 | LOG(ERROR) << "Already exists, cannot create key at: " << key_path; |
| 622 | return false; |
| 623 | } |
| 624 | if (pathExists(tmp_path)) { |
| 625 | LOG(DEBUG) << "Already exists, destroying: " << tmp_path; |
| 626 | destroyKey(tmp_path); // May be partially created so ignore errors |
| 627 | } |
| 628 | if (!storeKey(tmp_path, auth, key)) return false; |
Satya Tangirala | 9475b11 | 2021-05-13 00:43:03 -0700 | [diff] [blame] | 629 | |
Satya Tangirala | 0f890a9 | 2021-06-08 12:55:24 -0700 | [diff] [blame] | 630 | if (!RenameKeyDir(tmp_path, key_path)) return false; |
| 631 | |
Eric Biggers | 3345a2a | 2021-02-16 15:59:17 -0800 | [diff] [blame] | 632 | if (!FsyncParentDirectory(key_path)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 633 | LOG(DEBUG) << "Created key: " << key_path; |
| 634 | return true; |
| 635 | } |
| 636 | |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 637 | bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffer* key) { |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 638 | std::string version; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 639 | if (!readFileToString(dir + "/" + kFn_version, &version)) return false; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 640 | if (version != kCurrentVersion) { |
| 641 | LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version; |
| 642 | return false; |
| 643 | } |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 644 | std::string secdiscardable_hash; |
| 645 | if (!readSecdiscardable(dir + "/" + kFn_secdiscardable, &secdiscardable_hash)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 646 | std::string stretching; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 647 | if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 648 | std::string appId; |
Satya Tangirala | 478cea9 | 2021-04-07 14:30:25 -0700 | [diff] [blame] | 649 | if (!generateAppId(auth, stretching, secdiscardable_hash, &appId)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 650 | std::string encryptedMessage; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 651 | if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 652 | if (auth.usesKeystore()) { |
| 653 | Keystore keystore; |
| 654 | if (!keystore) return false; |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame] | 655 | km::AuthorizationSet keyParams = beginParams(appId); |
David Anderson | e179157 | 2021-11-05 18:57:49 -0700 | [diff] [blame] | 656 | if (!decryptWithKeystoreKey(keystore, dir, keyParams, encryptedMessage, key)) { |
| 657 | LOG(ERROR) << "decryptWithKeystoreKey failed"; |
| 658 | return false; |
| 659 | } |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 660 | } else { |
David Anderson | e179157 | 2021-11-05 18:57:49 -0700 | [diff] [blame] | 661 | if (!decryptWithoutKeystore(appId, encryptedMessage, key)) { |
| 662 | LOG(ERROR) << "decryptWithoutKeystore failed"; |
| 663 | return false; |
| 664 | } |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 665 | } |
| 666 | return true; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 669 | static bool DeleteKeystoreKey(const std::string& blob_file) { |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 670 | std::string blob; |
| 671 | if (!readFileToString(blob_file, &blob)) return false; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 672 | Keystore keystore; |
| 673 | if (!keystore) return false; |
| 674 | LOG(DEBUG) << "Deleting key " << blob_file << " from Keystore"; |
| 675 | if (!keystore.deleteKey(blob)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 676 | return true; |
| 677 | } |
| 678 | |
Rubin Xu | 2436e27 | 2017-04-27 20:43:10 +0100 | [diff] [blame] | 679 | bool runSecdiscardSingle(const std::string& file) { |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 680 | if (ForkExecvp(std::vector<std::string>{kSecdiscardPath, "--", file}) != 0) { |
Rubin Xu | 2436e27 | 2017-04-27 20:43:10 +0100 | [diff] [blame] | 681 | LOG(ERROR) << "secdiscard failed"; |
| 682 | return false; |
| 683 | } |
| 684 | return true; |
| 685 | } |
| 686 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 687 | static bool recursiveDeleteKey(const std::string& dir) { |
| 688 | if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 689 | LOG(ERROR) << "recursive delete failed"; |
| 690 | return false; |
| 691 | } |
| 692 | return true; |
| 693 | } |
| 694 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 695 | bool destroyKey(const std::string& dir) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 696 | bool success = true; |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 697 | |
| 698 | CancelPendingKeyCommit(dir); |
| 699 | |
Paul Crowley | ff19b05 | 2017-10-26 11:28:55 -0700 | [diff] [blame] | 700 | auto secdiscard_cmd = std::vector<std::string>{ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 701 | kSecdiscardPath, |
| 702 | "--", |
| 703 | dir + "/" + kFn_encrypted_key, |
| 704 | dir + "/" + kFn_secdiscardable, |
Paul Crowley | ff19b05 | 2017-10-26 11:28:55 -0700 | [diff] [blame] | 705 | }; |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 706 | // Try each thing, even if previous things failed. |
| 707 | |
| 708 | for (auto& fn : {kFn_keymaster_key_blob, kFn_keymaster_key_blob_upgraded}) { |
| 709 | auto blob_file = dir + "/" + fn; |
| 710 | if (pathExists(blob_file)) { |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 711 | success &= DeleteKeystoreKey(blob_file); |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 712 | secdiscard_cmd.push_back(blob_file); |
| 713 | } |
Paul Crowley | ff19b05 | 2017-10-26 11:28:55 -0700 | [diff] [blame] | 714 | } |
| 715 | if (ForkExecvp(secdiscard_cmd) != 0) { |
| 716 | LOG(ERROR) << "secdiscard failed"; |
| 717 | success = false; |
| 718 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 719 | success &= recursiveDeleteKey(dir); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 720 | return success; |
| 721 | } |
| 722 | |
Seth Moore | 5a43d61 | 2021-01-19 17:51:51 +0000 | [diff] [blame] | 723 | bool setKeyStorageBindingSeed(const std::vector<uint8_t>& seed) { |
| 724 | const std::lock_guard<std::mutex> scope_lock(storage_binding_info.guard); |
| 725 | switch (storage_binding_info.state) { |
| 726 | case StorageBindingInfo::State::UNINITIALIZED: |
| 727 | storage_binding_info.state = StorageBindingInfo::State::IN_USE; |
| 728 | storage_binding_info.seed = seed; |
Keith Mok | e860025 | 2021-09-01 18:37:48 +0000 | [diff] [blame] | 729 | android::base::SetProperty("vold.storage_seed_bound", "1"); |
Seth Moore | 5a43d61 | 2021-01-19 17:51:51 +0000 | [diff] [blame] | 730 | return true; |
| 731 | case StorageBindingInfo::State::IN_USE: |
| 732 | LOG(ERROR) << "key storage binding seed already set"; |
| 733 | return false; |
| 734 | case StorageBindingInfo::State::NOT_USED: |
| 735 | LOG(ERROR) << "key storage already in use without binding"; |
| 736 | return false; |
| 737 | } |
| 738 | return false; |
| 739 | } |
| 740 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 741 | } // namespace vold |
| 742 | } // namespace android |