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