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