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