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 | |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 24 | #include <thread> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| 27 | #include <errno.h> |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <unistd.h> |
| 33 | |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 34 | #include <openssl/err.h> |
| 35 | #include <openssl/evp.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 36 | #include <openssl/sha.h> |
| 37 | |
| 38 | #include <android-base/file.h> |
| 39 | #include <android-base/logging.h> |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Daniel Rosenberg | d2906b8 | 2019-06-07 14:18:14 -0700 | [diff] [blame] | 41 | #include <android-base/unique_fd.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 42 | |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 43 | #include <cutils/properties.h> |
| 44 | |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 45 | #include <hardware/hw_auth_token.h> |
Shawn Willden | 35f0f22 | 2020-01-16 13:21:42 -0700 | [diff] [blame] | 46 | #include <keymasterV4_1/authorization_set.h> |
| 47 | #include <keymasterV4_1/keymaster_utils.h> |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 48 | |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 49 | extern "C" { |
| 50 | |
| 51 | #include "crypto_scrypt.h" |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 54 | namespace android { |
| 55 | namespace vold { |
| 56 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 57 | const KeyAuthentication kEmptyAuthentication{"", ""}; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 58 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 59 | static constexpr size_t AES_KEY_BYTES = 32; |
| 60 | static constexpr size_t GCM_NONCE_BYTES = 12; |
| 61 | static constexpr size_t GCM_MAC_BYTES = 16; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 62 | static constexpr size_t SALT_BYTES = 1 << 4; |
| 63 | static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14; |
| 64 | static constexpr size_t STRETCHED_BYTES = 1 << 6; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 65 | |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 66 | static constexpr uint32_t AUTH_TIMEOUT = 30; // Seconds |
Paul Crowley | b3de337 | 2016-04-27 12:58:41 -0700 | [diff] [blame] | 67 | |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 68 | static const char* kCurrentVersion = "1"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 69 | static const char* kRmPath = "/system/bin/rm"; |
| 70 | static const char* kSecdiscardPath = "/system/bin/secdiscard"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 71 | static const char* kStretch_none = "none"; |
| 72 | static const char* kStretch_nopassword = "nopassword"; |
| 73 | static const std::string kStretchPrefix_scrypt = "scrypt "; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 74 | static const char* kHashPrefix_secdiscardable = "Android secdiscardable SHA512"; |
| 75 | static const char* kHashPrefix_keygen = "Android key wrapping key generation SHA512"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 76 | static const char* kFn_encrypted_key = "encrypted_key"; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 77 | static const char* kFn_keymaster_key_blob = "keymaster_key_blob"; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 78 | static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 79 | static const char* kFn_salt = "salt"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 80 | static const char* kFn_secdiscardable = "secdiscardable"; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 81 | static const char* kFn_stretching = "stretching"; |
| 82 | static const char* kFn_version = "version"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 83 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 84 | 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] | 85 | if (actual != expected) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 86 | LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got " |
| 87 | << actual; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 93 | 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] | 94 | SHA512_CTX c; |
| 95 | |
| 96 | SHA512_Init(&c); |
| 97 | // Personalise the hashing by introducing a fixed prefix. |
| 98 | // Hashing applications should use personalization except when there is a |
| 99 | // 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] | 100 | std::string hashingPrefix = prefix; |
| 101 | hashingPrefix.resize(SHA512_CBLOCK); |
| 102 | SHA512_Update(&c, hashingPrefix.data(), hashingPrefix.size()); |
| 103 | SHA512_Update(&c, tohash.data(), tohash.size()); |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 104 | res->assign(SHA512_DIGEST_LENGTH, '\0'); |
| 105 | SHA512_Final(reinterpret_cast<uint8_t*>(&(*res)[0]), &c); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 108 | static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth, |
| 109 | const std::string& appId, std::string* key) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 110 | auto paramBuilder = km::AuthorizationSetBuilder() |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 111 | .AesEncryptionKey(AES_KEY_BYTES * 8) |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 112 | .GcmModeMinMacLen(GCM_MAC_BYTES * 8) |
| 113 | .Authorization(km::TAG_APPLICATION_ID, km::support::blob2hidlVec(appId)); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 114 | if (auth.token.empty()) { |
| 115 | LOG(DEBUG) << "Creating key that doesn't need auth token"; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 116 | paramBuilder.Authorization(km::TAG_NO_AUTH_REQUIRED); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 117 | } else { |
| 118 | LOG(DEBUG) << "Auth token required for key"; |
| 119 | if (auth.token.size() != sizeof(hw_auth_token_t)) { |
| 120 | LOG(ERROR) << "Auth token should be " << sizeof(hw_auth_token_t) << " bytes, was " |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 121 | << auth.token.size() << " bytes"; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 122 | return false; |
| 123 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 124 | const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data()); |
Paul Crowley | d281de7 | 2018-08-30 15:25:19 -0700 | [diff] [blame] | 125 | auto user_id = at->user_id; // Make a copy because at->user_id is unaligned. |
| 126 | paramBuilder.Authorization(km::TAG_USER_SECURE_ID, user_id); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 127 | paramBuilder.Authorization(km::TAG_USER_AUTH_TYPE, km::HardwareAuthenticatorType::PASSWORD); |
| 128 | paramBuilder.Authorization(km::TAG_AUTH_TIMEOUT, AUTH_TIMEOUT); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 129 | } |
Shawn Willden | 8431fe2 | 2018-12-06 07:45:02 -0700 | [diff] [blame] | 130 | |
| 131 | auto paramsWithRollback = paramBuilder; |
| 132 | paramsWithRollback.Authorization(km::TAG_ROLLBACK_RESISTANCE); |
| 133 | |
| 134 | // Generate rollback-resistant key if possible. |
| 135 | return keymaster.generateKey(paramsWithRollback, key) || |
| 136 | keymaster.generateKey(paramBuilder, key); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 139 | bool generateWrappedStorageKey(KeyBuffer* key) { |
| 140 | Keymaster keymaster; |
| 141 | if (!keymaster) return false; |
| 142 | std::string key_temp; |
| 143 | auto paramBuilder = km::AuthorizationSetBuilder().AesEncryptionKey(AES_KEY_BYTES * 8); |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 144 | paramBuilder.Authorization(km::TAG_STORAGE_KEY); |
Gaurav Kashyap | 75736a8 | 2020-09-11 15:24:01 -0700 | [diff] [blame] | 145 | auto paramsWithRollback = paramBuilder; |
| 146 | paramsWithRollback.Authorization(km::TAG_ROLLBACK_RESISTANCE); |
| 147 | if (!keymaster.generateKey(paramsWithRollback, &key_temp)) { |
| 148 | if (!keymaster.generateKey(paramBuilder, &key_temp)) return false; |
| 149 | } |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 150 | *key = KeyBuffer(key_temp.size()); |
| 151 | memcpy(reinterpret_cast<void*>(key->data()), key_temp.c_str(), key->size()); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | bool exportWrappedStorageKey(const KeyBuffer& kmKey, KeyBuffer* key) { |
| 156 | Keymaster keymaster; |
| 157 | if (!keymaster) return false; |
| 158 | std::string key_temp; |
| 159 | |
| 160 | if (!keymaster.exportKey(kmKey, &key_temp)) return false; |
| 161 | *key = KeyBuffer(key_temp.size()); |
| 162 | memcpy(reinterpret_cast<void*>(key->data()), key_temp.c_str(), key->size()); |
| 163 | return true; |
| 164 | } |
| 165 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 166 | static std::pair<km::AuthorizationSet, km::HardwareAuthToken> beginParams( |
| 167 | const KeyAuthentication& auth, const std::string& appId) { |
| 168 | auto paramBuilder = km::AuthorizationSetBuilder() |
| 169 | .GcmModeMacLen(GCM_MAC_BYTES * 8) |
| 170 | .Authorization(km::TAG_APPLICATION_ID, km::support::blob2hidlVec(appId)); |
| 171 | km::HardwareAuthToken authToken; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 172 | if (!auth.token.empty()) { |
| 173 | LOG(DEBUG) << "Supplying auth token to Keymaster"; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 174 | authToken = km::support::hidlVec2AuthToken(km::support::blob2hidlVec(auth.token)); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 175 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 176 | return {paramBuilder, authToken}; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 179 | static bool readFileToString(const std::string& filename, std::string* result) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 180 | if (!android::base::ReadFileToString(filename, result)) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 181 | PLOG(ERROR) << "Failed to read from " << filename; |
| 182 | return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 187 | static bool readRandomBytesOrLog(size_t count, std::string* out) { |
| 188 | auto status = ReadRandomBytes(count, *out); |
| 189 | if (status != OK) { |
| 190 | LOG(ERROR) << "Random read failed with status: " << status; |
| 191 | return false; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | bool createSecdiscardable(const std::string& filename, std::string* hash) { |
| 197 | std::string secdiscardable; |
| 198 | if (!readRandomBytesOrLog(SECDISCARDABLE_BYTES, &secdiscardable)) return false; |
| 199 | if (!writeStringToFile(secdiscardable, filename)) return false; |
| 200 | hashWithPrefix(kHashPrefix_secdiscardable, secdiscardable, hash); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool readSecdiscardable(const std::string& filename, std::string* hash) { |
| 205 | std::string secdiscardable; |
| 206 | if (!readFileToString(filename, &secdiscardable)) return false; |
| 207 | hashWithPrefix(kHashPrefix_secdiscardable, secdiscardable, hash); |
| 208 | return true; |
| 209 | } |
| 210 | |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 211 | static void deferedKmDeleteKey(const std::string& kmkey) { |
| 212 | while (!android::base::WaitForProperty("vold.checkpoint_committed", "1")) { |
| 213 | LOG(ERROR) << "Wait for boot timed out"; |
| 214 | } |
| 215 | Keymaster keymaster; |
| 216 | if (!keymaster || !keymaster.deleteKey(kmkey)) { |
| 217 | LOG(ERROR) << "Defered Key deletion failed during upgrade"; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | bool kmDeleteKey(Keymaster& keymaster, const std::string& kmKey) { |
| 222 | bool needs_cp = cp_needsCheckpoint(); |
| 223 | |
| 224 | if (needs_cp) { |
| 225 | std::thread(deferedKmDeleteKey, kmKey).detach(); |
| 226 | LOG(INFO) << "Deferring Key deletion during upgrade"; |
| 227 | return true; |
| 228 | } else { |
| 229 | return keymaster.deleteKey(kmKey); |
| 230 | } |
| 231 | } |
| 232 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 233 | static KeymasterOperation begin(Keymaster& keymaster, const std::string& dir, |
| 234 | km::KeyPurpose purpose, const km::AuthorizationSet& keyParams, |
| 235 | const km::AuthorizationSet& opParams, |
| 236 | const km::HardwareAuthToken& authToken, |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 237 | km::AuthorizationSet* outParams, bool keepOld) { |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 238 | auto kmKeyPath = dir + "/" + kFn_keymaster_key_blob; |
| 239 | std::string kmKey; |
| 240 | if (!readFileToString(kmKeyPath, &kmKey)) return KeymasterOperation(); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 241 | km::AuthorizationSet inParams(keyParams); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 242 | inParams.append(opParams.begin(), opParams.end()); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 243 | for (;;) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 244 | auto opHandle = keymaster.begin(purpose, kmKey, inParams, authToken, outParams); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 245 | if (opHandle) { |
| 246 | return opHandle; |
| 247 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 248 | if (opHandle.errorCode() != km::ErrorCode::KEY_REQUIRES_UPGRADE) return opHandle; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 249 | LOG(DEBUG) << "Upgrading key: " << dir; |
| 250 | std::string newKey; |
| 251 | if (!keymaster.upgradeKey(kmKey, keyParams, &newKey)) return KeymasterOperation(); |
| 252 | auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 253 | if (!writeStringToFile(newKey, newKeyPath)) return KeymasterOperation(); |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 254 | if (!keepOld) { |
| 255 | if (rename(newKeyPath.c_str(), kmKeyPath.c_str()) != 0) { |
| 256 | PLOG(ERROR) << "Unable to move upgraded key to location: " << kmKeyPath; |
| 257 | return KeymasterOperation(); |
| 258 | } |
Woody Lin | 37c82f5 | 2019-03-11 20:58:20 +0800 | [diff] [blame] | 259 | if (!android::vold::FsyncDirectory(dir)) { |
| 260 | LOG(ERROR) << "Key dir sync failed: " << dir; |
| 261 | return KeymasterOperation(); |
| 262 | } |
Daniel Rosenberg | a48730a | 2019-06-06 20:38:38 -0700 | [diff] [blame] | 263 | if (!kmDeleteKey(keymaster, kmKey)) { |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 264 | LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir; |
| 265 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 266 | } |
| 267 | kmKey = newKey; |
| 268 | LOG(INFO) << "Key upgraded: " << dir; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir, |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 273 | const km::AuthorizationSet& keyParams, |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 274 | const km::HardwareAuthToken& authToken, const KeyBuffer& message, |
| 275 | std::string* ciphertext, bool keepOld) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 276 | km::AuthorizationSet opParams; |
| 277 | km::AuthorizationSet outParams; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 278 | auto opHandle = begin(keymaster, dir, km::KeyPurpose::ENCRYPT, keyParams, opParams, authToken, |
| 279 | &outParams, keepOld); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 280 | if (!opHandle) return false; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 281 | auto nonceBlob = outParams.GetTagValue(km::TAG_NONCE); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 282 | if (!nonceBlob.isOk()) { |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 283 | LOG(ERROR) << "GCM encryption but no nonce generated"; |
| 284 | return false; |
| 285 | } |
| 286 | // 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] | 287 | std::string nonce(reinterpret_cast<const char*>(&nonceBlob.value()[0]), |
| 288 | nonceBlob.value().size()); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 289 | if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false; |
| 290 | std::string body; |
| 291 | if (!opHandle.updateCompletely(message, &body)) return false; |
| 292 | |
| 293 | std::string mac; |
| 294 | if (!opHandle.finish(&mac)) return false; |
| 295 | if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false; |
| 296 | *ciphertext = nonce + body + mac; |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir, |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 301 | const km::AuthorizationSet& keyParams, |
| 302 | const km::HardwareAuthToken& authToken, |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 303 | const std::string& ciphertext, KeyBuffer* message, |
| 304 | bool keepOld) { |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 305 | auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES); |
| 306 | auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 307 | auto opParams = km::AuthorizationSetBuilder().Authorization(km::TAG_NONCE, |
| 308 | km::support::blob2hidlVec(nonce)); |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 309 | auto opHandle = begin(keymaster, dir, km::KeyPurpose::DECRYPT, keyParams, opParams, authToken, |
| 310 | nullptr, keepOld); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 311 | if (!opHandle) return false; |
| 312 | if (!opHandle.updateCompletely(bodyAndMac, message)) return false; |
| 313 | if (!opHandle.finish(nullptr)) return false; |
| 314 | return true; |
| 315 | } |
| 316 | |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 317 | static std::string getStretching(const KeyAuthentication& auth) { |
| 318 | if (!auth.usesKeymaster()) { |
| 319 | return kStretch_none; |
| 320 | } else if (auth.secret.empty()) { |
| 321 | return kStretch_nopassword; |
| 322 | } else { |
| 323 | char paramstr[PROPERTY_VALUE_MAX]; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 324 | |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 325 | property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS); |
| 326 | return std::string() + kStretchPrefix_scrypt + paramstr; |
| 327 | } |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 330 | static bool stretchingNeedsSalt(const std::string& stretching) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 331 | return stretching != kStretch_nopassword && stretching != kStretch_none; |
| 332 | } |
| 333 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 334 | static bool stretchSecret(const std::string& stretching, const std::string& secret, |
| 335 | const std::string& salt, std::string* stretched) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 336 | if (stretching == kStretch_nopassword) { |
| 337 | if (!secret.empty()) { |
Paul Crowley | d9b9295 | 2016-03-04 13:45:00 -0800 | [diff] [blame] | 338 | LOG(WARNING) << "Password present but stretching is nopassword"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 339 | // Continue anyway |
| 340 | } |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 341 | stretched->clear(); |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 342 | } else if (stretching == kStretch_none) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 343 | *stretched = secret; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 344 | } else if (std::equal(kStretchPrefix_scrypt.begin(), kStretchPrefix_scrypt.end(), |
| 345 | stretching.begin())) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 346 | int Nf, rf, pf; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 347 | if (!parse_scrypt_parameters(stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf, |
| 348 | &rf, &pf)) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 349 | LOG(ERROR) << "Unable to parse scrypt params in stretching: " << stretching; |
| 350 | return false; |
| 351 | } |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 352 | stretched->assign(STRETCHED_BYTES, '\0'); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 353 | if (crypto_scrypt(reinterpret_cast<const uint8_t*>(secret.data()), secret.size(), |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 354 | reinterpret_cast<const uint8_t*>(salt.data()), salt.size(), 1 << Nf, |
| 355 | 1 << rf, 1 << pf, reinterpret_cast<uint8_t*>(&(*stretched)[0]), |
| 356 | stretched->size()) != 0) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 357 | LOG(ERROR) << "scrypt failed with params: " << stretching; |
| 358 | return false; |
| 359 | } |
| 360 | } else { |
| 361 | LOG(ERROR) << "Unknown stretching type: " << stretching; |
| 362 | return false; |
| 363 | } |
| 364 | return true; |
| 365 | } |
| 366 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 367 | static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching, |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 368 | const std::string& salt, const std::string& secdiscardable_hash, |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 369 | std::string* appId) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 370 | std::string stretched; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 371 | if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 372 | *appId = secdiscardable_hash + stretched; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | |
| 376 | static void logOpensslError() { |
| 377 | LOG(ERROR) << "Openssl error: " << ERR_get_error(); |
| 378 | } |
| 379 | |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 380 | static bool encryptWithoutKeymaster(const std::string& preKey, const KeyBuffer& plaintext, |
| 381 | std::string* ciphertext) { |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 382 | std::string key; |
| 383 | hashWithPrefix(kHashPrefix_keygen, preKey, &key); |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 384 | key.resize(AES_KEY_BYTES); |
| 385 | if (!readRandomBytesOrLog(GCM_NONCE_BYTES, ciphertext)) return false; |
| 386 | auto ctx = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>( |
| 387 | EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); |
| 388 | if (!ctx) { |
| 389 | logOpensslError(); |
| 390 | return false; |
| 391 | } |
| 392 | if (1 != EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_gcm(), NULL, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 393 | reinterpret_cast<const uint8_t*>(key.data()), |
| 394 | reinterpret_cast<const uint8_t*>(ciphertext->data()))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 395 | logOpensslError(); |
| 396 | return false; |
| 397 | } |
| 398 | ciphertext->resize(GCM_NONCE_BYTES + plaintext.size() + GCM_MAC_BYTES); |
| 399 | int outlen; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 400 | if (1 != EVP_EncryptUpdate( |
| 401 | ctx.get(), reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES), |
| 402 | &outlen, reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size())) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 403 | logOpensslError(); |
| 404 | return false; |
| 405 | } |
| 406 | if (outlen != static_cast<int>(plaintext.size())) { |
| 407 | LOG(ERROR) << "GCM ciphertext length should be " << plaintext.size() << " was " << outlen; |
| 408 | return false; |
| 409 | } |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 410 | if (1 != EVP_EncryptFinal_ex( |
| 411 | ctx.get(), |
| 412 | reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES + plaintext.size()), |
| 413 | &outlen)) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 414 | logOpensslError(); |
| 415 | return false; |
| 416 | } |
| 417 | if (outlen != 0) { |
| 418 | LOG(ERROR) << "GCM EncryptFinal should be 0, was " << outlen; |
| 419 | return false; |
| 420 | } |
| 421 | 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] | 422 | reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES + |
| 423 | plaintext.size()))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 424 | logOpensslError(); |
| 425 | return false; |
| 426 | } |
| 427 | return true; |
| 428 | } |
| 429 | |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 430 | static bool decryptWithoutKeymaster(const std::string& preKey, const std::string& ciphertext, |
| 431 | KeyBuffer* plaintext) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 432 | if (ciphertext.size() < GCM_NONCE_BYTES + GCM_MAC_BYTES) { |
| 433 | LOG(ERROR) << "GCM ciphertext too small: " << ciphertext.size(); |
| 434 | return false; |
| 435 | } |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 436 | std::string key; |
| 437 | hashWithPrefix(kHashPrefix_keygen, preKey, &key); |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 438 | key.resize(AES_KEY_BYTES); |
| 439 | auto ctx = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>( |
| 440 | EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); |
| 441 | if (!ctx) { |
| 442 | logOpensslError(); |
| 443 | return false; |
| 444 | } |
| 445 | if (1 != EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_gcm(), NULL, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 446 | reinterpret_cast<const uint8_t*>(key.data()), |
| 447 | reinterpret_cast<const uint8_t*>(ciphertext.data()))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 448 | logOpensslError(); |
| 449 | return false; |
| 450 | } |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 451 | *plaintext = KeyBuffer(ciphertext.size() - GCM_NONCE_BYTES - GCM_MAC_BYTES); |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 452 | int outlen; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 453 | if (1 != EVP_DecryptUpdate(ctx.get(), reinterpret_cast<uint8_t*>(&(*plaintext)[0]), &outlen, |
| 454 | reinterpret_cast<const uint8_t*>(ciphertext.data() + GCM_NONCE_BYTES), |
| 455 | plaintext->size())) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 456 | logOpensslError(); |
| 457 | return false; |
| 458 | } |
| 459 | if (outlen != static_cast<int>(plaintext->size())) { |
| 460 | LOG(ERROR) << "GCM plaintext length should be " << plaintext->size() << " was " << outlen; |
| 461 | return false; |
| 462 | } |
| 463 | 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] | 464 | const_cast<void*>(reinterpret_cast<const void*>( |
| 465 | ciphertext.data() + GCM_NONCE_BYTES + plaintext->size())))) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 466 | logOpensslError(); |
| 467 | return false; |
| 468 | } |
| 469 | if (1 != EVP_DecryptFinal_ex(ctx.get(), |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 470 | reinterpret_cast<uint8_t*>(&(*plaintext)[0] + plaintext->size()), |
| 471 | &outlen)) { |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 472 | logOpensslError(); |
| 473 | return false; |
| 474 | } |
| 475 | if (outlen != 0) { |
| 476 | LOG(ERROR) << "GCM EncryptFinal should be 0, was " << outlen; |
| 477 | return false; |
| 478 | } |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 479 | return true; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 482 | bool pathExists(const std::string& path) { |
| 483 | return access(path.c_str(), F_OK) == 0; |
| 484 | } |
| 485 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 486 | bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBuffer& key) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 487 | if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) { |
| 488 | PLOG(ERROR) << "key mkdir " << dir; |
| 489 | return false; |
| 490 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 491 | if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 492 | std::string secdiscardable_hash; |
| 493 | if (!createSecdiscardable(dir + "/" + kFn_secdiscardable, &secdiscardable_hash)) return false; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 494 | std::string stretching = getStretching(auth); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 495 | if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 496 | std::string salt; |
| 497 | if (stretchingNeedsSalt(stretching)) { |
| 498 | if (ReadRandomBytes(SALT_BYTES, salt) != OK) { |
| 499 | LOG(ERROR) << "Random read failed"; |
| 500 | return false; |
| 501 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 502 | if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 503 | } |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 504 | std::string appId; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 505 | if (!generateAppId(auth, stretching, salt, secdiscardable_hash, &appId)) return false; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 506 | std::string encryptedKey; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 507 | if (auth.usesKeymaster()) { |
| 508 | Keymaster keymaster; |
| 509 | if (!keymaster) return false; |
| 510 | std::string kmKey; |
| 511 | if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false; |
| 512 | if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 513 | km::AuthorizationSet keyParams; |
| 514 | km::HardwareAuthToken authToken; |
| 515 | std::tie(keyParams, authToken) = beginParams(auth, appId); |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 516 | if (!encryptWithKeymasterKey(keymaster, dir, keyParams, authToken, key, &encryptedKey, |
| 517 | false)) |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 518 | return false; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 519 | } else { |
| 520 | if (!encryptWithoutKeymaster(appId, key, &encryptedKey)) return false; |
| 521 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 522 | if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false; |
Paul Crowley | 621d9b9 | 2018-12-07 15:36:09 -0800 | [diff] [blame] | 523 | if (!FsyncDirectory(dir)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 527 | bool storeKeyAtomically(const std::string& key_path, const std::string& tmp_path, |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 528 | const KeyAuthentication& auth, const KeyBuffer& key) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 529 | if (pathExists(key_path)) { |
| 530 | LOG(ERROR) << "Already exists, cannot create key at: " << key_path; |
| 531 | return false; |
| 532 | } |
| 533 | if (pathExists(tmp_path)) { |
| 534 | LOG(DEBUG) << "Already exists, destroying: " << tmp_path; |
| 535 | destroyKey(tmp_path); // May be partially created so ignore errors |
| 536 | } |
| 537 | if (!storeKey(tmp_path, auth, key)) return false; |
| 538 | if (rename(tmp_path.c_str(), key_path.c_str()) != 0) { |
| 539 | PLOG(ERROR) << "Unable to move new key to location: " << key_path; |
| 540 | return false; |
| 541 | } |
| 542 | LOG(DEBUG) << "Created key: " << key_path; |
| 543 | return true; |
| 544 | } |
| 545 | |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 546 | bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffer* key, |
| 547 | bool keepOld) { |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 548 | std::string version; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 549 | if (!readFileToString(dir + "/" + kFn_version, &version)) return false; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 550 | if (version != kCurrentVersion) { |
| 551 | LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version; |
| 552 | return false; |
| 553 | } |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 554 | std::string secdiscardable_hash; |
| 555 | if (!readSecdiscardable(dir + "/" + kFn_secdiscardable, &secdiscardable_hash)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 556 | std::string stretching; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 557 | if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 558 | std::string salt; |
| 559 | if (stretchingNeedsSalt(stretching)) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 560 | if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 561 | } |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 562 | std::string appId; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 563 | if (!generateAppId(auth, stretching, salt, secdiscardable_hash, &appId)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 564 | std::string encryptedMessage; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 565 | if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 566 | if (auth.usesKeymaster()) { |
| 567 | Keymaster keymaster; |
| 568 | if (!keymaster) return false; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 569 | km::AuthorizationSet keyParams; |
| 570 | km::HardwareAuthToken authToken; |
| 571 | std::tie(keyParams, authToken) = beginParams(auth, appId); |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 572 | if (!decryptWithKeymasterKey(keymaster, dir, keyParams, authToken, encryptedMessage, key, |
| 573 | keepOld)) |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 574 | return false; |
Paul Crowley | 6ab2cab | 2017-01-04 22:32:40 -0800 | [diff] [blame] | 575 | } else { |
| 576 | if (!decryptWithoutKeymaster(appId, encryptedMessage, key)) return false; |
| 577 | } |
| 578 | return true; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 581 | static bool deleteKey(const std::string& dir) { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 582 | std::string kmKey; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 583 | if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 584 | Keymaster keymaster; |
| 585 | if (!keymaster) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 586 | if (!keymaster.deleteKey(kmKey)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | |
Rubin Xu | 2436e27 | 2017-04-27 20:43:10 +0100 | [diff] [blame] | 590 | bool runSecdiscardSingle(const std::string& file) { |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 591 | if (ForkExecvp(std::vector<std::string>{kSecdiscardPath, "--", file}) != 0) { |
Rubin Xu | 2436e27 | 2017-04-27 20:43:10 +0100 | [diff] [blame] | 592 | LOG(ERROR) << "secdiscard failed"; |
| 593 | return false; |
| 594 | } |
| 595 | return true; |
| 596 | } |
| 597 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 598 | static bool recursiveDeleteKey(const std::string& dir) { |
| 599 | if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 600 | LOG(ERROR) << "recursive delete failed"; |
| 601 | return false; |
| 602 | } |
| 603 | return true; |
| 604 | } |
| 605 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 606 | bool destroyKey(const std::string& dir) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 607 | bool success = true; |
| 608 | // Try each thing, even if previous things failed. |
Paul Crowley | ff19b05 | 2017-10-26 11:28:55 -0700 | [diff] [blame] | 609 | bool uses_km = pathExists(dir + "/" + kFn_keymaster_key_blob); |
| 610 | if (uses_km) { |
| 611 | success &= deleteKey(dir); |
| 612 | } |
| 613 | auto secdiscard_cmd = std::vector<std::string>{ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 614 | kSecdiscardPath, |
| 615 | "--", |
| 616 | dir + "/" + kFn_encrypted_key, |
| 617 | dir + "/" + kFn_secdiscardable, |
Paul Crowley | ff19b05 | 2017-10-26 11:28:55 -0700 | [diff] [blame] | 618 | }; |
| 619 | if (uses_km) { |
| 620 | secdiscard_cmd.emplace_back(dir + "/" + kFn_keymaster_key_blob); |
| 621 | } |
| 622 | if (ForkExecvp(secdiscard_cmd) != 0) { |
| 623 | LOG(ERROR) << "secdiscard failed"; |
| 624 | success = false; |
| 625 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 626 | success &= recursiveDeleteKey(dir); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 627 | return success; |
| 628 | } |
| 629 | |
| 630 | } // namespace vold |
| 631 | } // namespace android |