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