blob: 5234c5662fd6171e834ea1ab3dd4774cbc56207c [file] [log] [blame]
Paul Crowley1ef25582016-01-21 20:26:12 +00001/*
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 Crowley63c18d32016-02-10 14:02:47 +000020#include "ScryptParameters.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000021#include "Utils.h"
22
23#include <vector>
24
25#include <errno.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30
31#include <openssl/sha.h>
32
33#include <android-base/file.h>
34#include <android-base/logging.h>
35
Paul Crowley63c18d32016-02-10 14:02:47 +000036#include <cutils/properties.h>
37
Paul Crowley320e5e12016-03-04 14:07:05 -080038#include <hardware/hw_auth_token.h>
39
Paul Crowley1ef25582016-01-21 20:26:12 +000040#include <keymaster/authorization_set.h>
41
Paul Crowley63c18d32016-02-10 14:02:47 +000042extern "C" {
43
44#include "crypto_scrypt.h"
Paul Crowley63c18d32016-02-10 14:02:47 +000045}
46
Paul Crowley1ef25582016-01-21 20:26:12 +000047namespace android {
48namespace vold {
49
Paul Crowleydf528a72016-03-09 09:31:37 -080050const KeyAuthentication kEmptyAuthentication{"", ""};
Paul Crowley05720802016-02-08 15:55:41 +000051
Paul Crowley1ef25582016-01-21 20:26:12 +000052static constexpr size_t AES_KEY_BYTES = 32;
53static constexpr size_t GCM_NONCE_BYTES = 12;
54static constexpr size_t GCM_MAC_BYTES = 16;
Paul Crowleydf528a72016-03-09 09:31:37 -080055static constexpr size_t SALT_BYTES = 1 << 4;
56static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14;
57static constexpr size_t STRETCHED_BYTES = 1 << 6;
Paul Crowley1ef25582016-01-21 20:26:12 +000058
Paul Crowleyb3de3372016-04-27 12:58:41 -070059static constexpr uint32_t AUTH_TIMEOUT = 30; // Seconds
60
Paul Crowley05720802016-02-08 15:55:41 +000061static const char* kCurrentVersion = "1";
Paul Crowley1ef25582016-01-21 20:26:12 +000062static const char* kRmPath = "/system/bin/rm";
63static const char* kSecdiscardPath = "/system/bin/secdiscard";
Paul Crowley63c18d32016-02-10 14:02:47 +000064static const char* kStretch_none = "none";
65static const char* kStretch_nopassword = "nopassword";
66static const std::string kStretchPrefix_scrypt = "scrypt ";
Paul Crowley1ef25582016-01-21 20:26:12 +000067static const char* kFn_encrypted_key = "encrypted_key";
Paul Crowley05720802016-02-08 15:55:41 +000068static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
Paul Crowley63c18d32016-02-10 14:02:47 +000069static const char* kFn_salt = "salt";
Paul Crowley1ef25582016-01-21 20:26:12 +000070static const char* kFn_secdiscardable = "secdiscardable";
Paul Crowley05720802016-02-08 15:55:41 +000071static const char* kFn_stretching = "stretching";
72static const char* kFn_version = "version";
Paul Crowley1ef25582016-01-21 20:26:12 +000073
Paul Crowley13ffd8e2016-01-27 14:30:22 +000074static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
Paul Crowley1ef25582016-01-21 20:26:12 +000075 if (actual != expected) {
Paul Crowleydf528a72016-03-09 09:31:37 -080076 LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got "
77 << actual;
Paul Crowley1ef25582016-01-21 20:26:12 +000078 return false;
79 }
80 return true;
81}
82
Paul Crowleydf528a72016-03-09 09:31:37 -080083static std::string hashSecdiscardable(const std::string& secdiscardable) {
Paul Crowley1ef25582016-01-21 20:26:12 +000084 SHA512_CTX c;
85
86 SHA512_Init(&c);
87 // Personalise the hashing by introducing a fixed prefix.
88 // Hashing applications should use personalization except when there is a
89 // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
Paul Crowley13ffd8e2016-01-27 14:30:22 +000090 std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
91 secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
92 SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
Paul Crowley1ef25582016-01-21 20:26:12 +000093 SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
94 std::string res(SHA512_DIGEST_LENGTH, '\0');
Paul Crowleydf528a72016-03-09 09:31:37 -080095 SHA512_Final(reinterpret_cast<uint8_t*>(&res[0]), &c);
Paul Crowley1ef25582016-01-21 20:26:12 +000096 return res;
97}
98
Paul Crowleydf528a72016-03-09 09:31:37 -080099static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth,
100 const std::string& appId, std::string* key) {
Paul Crowley320e5e12016-03-04 14:07:05 -0800101 auto paramBuilder = keymaster::AuthorizationSetBuilder()
Paul Crowleydf528a72016-03-09 09:31:37 -0800102 .AesEncryptionKey(AES_KEY_BYTES * 8)
103 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
104 .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
105 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
Paul Crowleya051eb72016-03-08 16:08:32 -0800106 addStringParam(&paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
Paul Crowley320e5e12016-03-04 14:07:05 -0800107 if (auth.token.empty()) {
108 LOG(DEBUG) << "Creating key that doesn't need auth token";
109 paramBuilder.Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
110 } else {
111 LOG(DEBUG) << "Auth token required for key";
112 if (auth.token.size() != sizeof(hw_auth_token_t)) {
113 LOG(ERROR) << "Auth token should be " << sizeof(hw_auth_token_t) << " bytes, was "
Paul Crowleydf528a72016-03-09 09:31:37 -0800114 << auth.token.size() << " bytes";
Paul Crowley320e5e12016-03-04 14:07:05 -0800115 return false;
116 }
Paul Crowleydf528a72016-03-09 09:31:37 -0800117 const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data());
Paul Crowley320e5e12016-03-04 14:07:05 -0800118 paramBuilder.Authorization(keymaster::TAG_USER_SECURE_ID, at->user_id);
119 paramBuilder.Authorization(keymaster::TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD);
Paul Crowleyb3de3372016-04-27 12:58:41 -0700120 paramBuilder.Authorization(keymaster::TAG_AUTH_TIMEOUT, AUTH_TIMEOUT);
Paul Crowley320e5e12016-03-04 14:07:05 -0800121 }
122 return keymaster.generateKey(paramBuilder.build(), key);
123}
124
Paul Crowleydf528a72016-03-09 09:31:37 -0800125static keymaster::AuthorizationSetBuilder beginParams(const KeyAuthentication& auth,
126 const std::string& appId) {
Paul Crowley320e5e12016-03-04 14:07:05 -0800127 auto paramBuilder = keymaster::AuthorizationSetBuilder()
Paul Crowleydf528a72016-03-09 09:31:37 -0800128 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
129 .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
130 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
Paul Crowleya051eb72016-03-08 16:08:32 -0800131 addStringParam(&paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
Paul Crowley320e5e12016-03-04 14:07:05 -0800132 if (!auth.token.empty()) {
133 LOG(DEBUG) << "Supplying auth token to Keymaster";
Paul Crowleya051eb72016-03-08 16:08:32 -0800134 addStringParam(&paramBuilder, keymaster::TAG_AUTH_TOKEN, auth.token);
Paul Crowley320e5e12016-03-04 14:07:05 -0800135 }
136 return paramBuilder;
Paul Crowley1ef25582016-01-21 20:26:12 +0000137}
138
Paul Crowleydf528a72016-03-09 09:31:37 -0800139static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
140 const KeyAuthentication& auth, const std::string& appId,
141 const std::string& message, std::string* ciphertext) {
Paul Crowley320e5e12016-03-04 14:07:05 -0800142 auto params = beginParams(auth, appId).build();
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000143 keymaster::AuthorizationSet outParams;
Paul Crowleya051eb72016-03-08 16:08:32 -0800144 auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, &outParams);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000145 if (!opHandle) return false;
146 keymaster_blob_t nonceBlob;
147 if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000148 LOG(ERROR) << "GCM encryption but no nonce generated";
149 return false;
150 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000151 // nonceBlob here is just a pointer into existing data, must not be freed
Paul Crowleydf528a72016-03-09 09:31:37 -0800152 std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000153 if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000154 std::string body;
Paul Crowleya051eb72016-03-08 16:08:32 -0800155 if (!opHandle.updateCompletely(message, &body)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000156
157 std::string mac;
Paul Crowleya051eb72016-03-08 16:08:32 -0800158 if (!opHandle.finishWithOutput(&mac)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000159 if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
Paul Crowleya051eb72016-03-08 16:08:32 -0800160 *ciphertext = nonce + body + mac;
Paul Crowley1ef25582016-01-21 20:26:12 +0000161 return true;
162}
163
Paul Crowleydf528a72016-03-09 09:31:37 -0800164static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
165 const KeyAuthentication& auth, const std::string& appId,
166 const std::string& ciphertext, std::string* message) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000167 auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000168 auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
Paul Crowley320e5e12016-03-04 14:07:05 -0800169 auto params = addStringParam(beginParams(auth, appId), keymaster::TAG_NONCE, nonce).build();
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000170 auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params);
171 if (!opHandle) return false;
172 if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
173 if (!opHandle.finish()) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000174 return true;
175}
176
Paul Crowleydf528a72016-03-09 09:31:37 -0800177static bool readFileToString(const std::string& filename, std::string* result) {
Paul Crowleya051eb72016-03-08 16:08:32 -0800178 if (!android::base::ReadFileToString(filename, result)) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800179 PLOG(ERROR) << "Failed to read from " << filename;
180 return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000181 }
182 return true;
183}
184
Paul Crowleydf528a72016-03-09 09:31:37 -0800185static bool writeStringToFile(const std::string& payload, const std::string& filename) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000186 if (!android::base::WriteStringToFile(payload, filename)) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800187 PLOG(ERROR) << "Failed to write to " << filename;
188 return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000189 }
190 return true;
191}
192
Paul Crowley63c18d32016-02-10 14:02:47 +0000193static std::string getStretching() {
194 char paramstr[PROPERTY_VALUE_MAX];
195
196 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
197 return std::string() + kStretchPrefix_scrypt + paramstr;
198}
199
Paul Crowleydf528a72016-03-09 09:31:37 -0800200static bool stretchingNeedsSalt(const std::string& stretching) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000201 return stretching != kStretch_nopassword && stretching != kStretch_none;
202}
203
Paul Crowleydf528a72016-03-09 09:31:37 -0800204static bool stretchSecret(const std::string& stretching, const std::string& secret,
205 const std::string& salt, std::string* stretched) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000206 if (stretching == kStretch_nopassword) {
207 if (!secret.empty()) {
Paul Crowleyd9b92952016-03-04 13:45:00 -0800208 LOG(WARNING) << "Password present but stretching is nopassword";
Paul Crowley63c18d32016-02-10 14:02:47 +0000209 // Continue anyway
210 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800211 stretched->clear();
Paul Crowley63c18d32016-02-10 14:02:47 +0000212 } else if (stretching == kStretch_none) {
Paul Crowleya051eb72016-03-08 16:08:32 -0800213 *stretched = secret;
Paul Crowleydf528a72016-03-09 09:31:37 -0800214 } else if (std::equal(kStretchPrefix_scrypt.begin(), kStretchPrefix_scrypt.end(),
215 stretching.begin())) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000216 int Nf, rf, pf;
Paul Crowleydf528a72016-03-09 09:31:37 -0800217 if (!parse_scrypt_parameters(stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf,
218 &rf, &pf)) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000219 LOG(ERROR) << "Unable to parse scrypt params in stretching: " << stretching;
220 return false;
221 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800222 stretched->assign(STRETCHED_BYTES, '\0');
Paul Crowleydf528a72016-03-09 09:31:37 -0800223 if (crypto_scrypt(reinterpret_cast<const uint8_t*>(secret.data()), secret.size(),
224 reinterpret_cast<const uint8_t*>(salt.data()), salt.size(),
225 1 << Nf, 1 << rf, 1 << pf,
226 reinterpret_cast<uint8_t*>(&(*stretched)[0]), stretched->size()) != 0) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000227 LOG(ERROR) << "scrypt failed with params: " << stretching;
228 return false;
229 }
230 } else {
231 LOG(ERROR) << "Unknown stretching type: " << stretching;
232 return false;
233 }
234 return true;
235}
236
Paul Crowleydf528a72016-03-09 09:31:37 -0800237static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching,
238 const std::string& salt, const std::string& secdiscardable,
239 std::string* appId) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000240 std::string stretched;
Paul Crowleya051eb72016-03-08 16:08:32 -0800241 if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false;
242 *appId = hashSecdiscardable(secdiscardable) + stretched;
Paul Crowley63c18d32016-02-10 14:02:47 +0000243 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000244}
245
Paul Crowleydf528a72016-03-09 09:31:37 -0800246bool storeKey(const std::string& dir, const KeyAuthentication& auth, const std::string& key) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000247 if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
248 PLOG(ERROR) << "key mkdir " << dir;
249 return false;
250 }
Paul Crowleydf528a72016-03-09 09:31:37 -0800251 if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000252 std::string secdiscardable;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000253 if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000254 // TODO status_t plays badly with PLOG, fix it.
255 LOG(ERROR) << "Random read failed";
256 return false;
257 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000258 if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000259 std::string stretching = auth.secret.empty() ? kStretch_nopassword : getStretching();
Paul Crowleydf528a72016-03-09 09:31:37 -0800260 if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000261 std::string salt;
262 if (stretchingNeedsSalt(stretching)) {
263 if (ReadRandomBytes(SALT_BYTES, salt) != OK) {
264 LOG(ERROR) << "Random read failed";
265 return false;
266 }
Paul Crowleydf528a72016-03-09 09:31:37 -0800267 if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000268 }
Paul Crowley320e5e12016-03-04 14:07:05 -0800269 std::string appId;
Paul Crowleya051eb72016-03-08 16:08:32 -0800270 if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000271 Keymaster keymaster;
272 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000273 std::string kmKey;
Paul Crowleya051eb72016-03-08 16:08:32 -0800274 if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000275 if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
Paul Crowley320e5e12016-03-04 14:07:05 -0800276 std::string encryptedKey;
Paul Crowleya051eb72016-03-08 16:08:32 -0800277 if (!encryptWithKeymasterKey(keymaster, kmKey, auth, appId, key, &encryptedKey)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000278 if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000279 return true;
280}
281
Paul Crowleydf528a72016-03-09 09:31:37 -0800282bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, std::string* key) {
Paul Crowley05720802016-02-08 15:55:41 +0000283 std::string version;
Paul Crowleya051eb72016-03-08 16:08:32 -0800284 if (!readFileToString(dir + "/" + kFn_version, &version)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000285 if (version != kCurrentVersion) {
286 LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version;
287 return false;
288 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000289 std::string secdiscardable;
Paul Crowleya051eb72016-03-08 16:08:32 -0800290 if (!readFileToString(dir + "/" + kFn_secdiscardable, &secdiscardable)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000291 std::string stretching;
Paul Crowleya051eb72016-03-08 16:08:32 -0800292 if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000293 std::string salt;
294 if (stretchingNeedsSalt(stretching)) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800295 if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000296 }
Paul Crowley320e5e12016-03-04 14:07:05 -0800297 std::string appId;
Paul Crowleya051eb72016-03-08 16:08:32 -0800298 if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000299 std::string kmKey;
Paul Crowleya051eb72016-03-08 16:08:32 -0800300 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000301 std::string encryptedMessage;
Paul Crowleya051eb72016-03-08 16:08:32 -0800302 if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000303 Keymaster keymaster;
304 if (!keymaster) return false;
Paul Crowley320e5e12016-03-04 14:07:05 -0800305 return decryptWithKeymasterKey(keymaster, kmKey, auth, appId, encryptedMessage, key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000306}
307
Paul Crowleydf528a72016-03-09 09:31:37 -0800308static bool deleteKey(const std::string& dir) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000309 std::string kmKey;
Paul Crowleya051eb72016-03-08 16:08:32 -0800310 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000311 Keymaster keymaster;
312 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000313 if (!keymaster.deleteKey(kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000314 return true;
315}
316
Paul Crowleydf528a72016-03-09 09:31:37 -0800317static bool secdiscardSecdiscardable(const std::string& dir) {
318 if (ForkExecvp(
319 std::vector<std::string>{kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000320 LOG(ERROR) << "secdiscard failed";
321 return false;
322 }
323 return true;
324}
325
Paul Crowleydf528a72016-03-09 09:31:37 -0800326static bool recursiveDeleteKey(const std::string& dir) {
327 if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000328 LOG(ERROR) << "recursive delete failed";
329 return false;
330 }
331 return true;
332}
333
Paul Crowleydf528a72016-03-09 09:31:37 -0800334bool destroyKey(const std::string& dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000335 bool success = true;
336 // Try each thing, even if previous things failed.
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000337 success &= deleteKey(dir);
338 success &= secdiscardSecdiscardable(dir);
339 success &= recursiveDeleteKey(dir);
Paul Crowley1ef25582016-01-21 20:26:12 +0000340 return success;
341}
342
343} // namespace vold
344} // namespace android