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