blob: e6c91f5287f116180ae5d8ca97ab9fc331a8a6b7 [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>
Paul Crowleydff8c722016-05-16 08:14:56 -070026#include <stdio.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000027#include <sys/stat.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <unistd.h>
31
32#include <openssl/sha.h>
33
34#include <android-base/file.h>
35#include <android-base/logging.h>
36
Paul Crowley63c18d32016-02-10 14:02:47 +000037#include <cutils/properties.h>
38
Paul Crowley320e5e12016-03-04 14:07:05 -080039#include <hardware/hw_auth_token.h>
40
Paul Crowley1ef25582016-01-21 20:26:12 +000041#include <keymaster/authorization_set.h>
42
Paul Crowley63c18d32016-02-10 14:02:47 +000043extern "C" {
44
45#include "crypto_scrypt.h"
Paul Crowley63c18d32016-02-10 14:02:47 +000046}
47
Paul Crowley1ef25582016-01-21 20:26:12 +000048namespace android {
49namespace vold {
50
Paul Crowleydf528a72016-03-09 09:31:37 -080051const KeyAuthentication kEmptyAuthentication{"", ""};
Paul Crowley05720802016-02-08 15:55:41 +000052
Paul Crowley1ef25582016-01-21 20:26:12 +000053static constexpr size_t AES_KEY_BYTES = 32;
54static constexpr size_t GCM_NONCE_BYTES = 12;
55static constexpr size_t GCM_MAC_BYTES = 16;
Paul Crowleydf528a72016-03-09 09:31:37 -080056static constexpr size_t SALT_BYTES = 1 << 4;
57static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14;
58static constexpr size_t STRETCHED_BYTES = 1 << 6;
Paul Crowley1ef25582016-01-21 20:26:12 +000059
Paul Crowleyb3de3372016-04-27 12:58:41 -070060static constexpr uint32_t AUTH_TIMEOUT = 30; // Seconds
61
Paul Crowley05720802016-02-08 15:55:41 +000062static const char* kCurrentVersion = "1";
Paul Crowley1ef25582016-01-21 20:26:12 +000063static const char* kRmPath = "/system/bin/rm";
64static const char* kSecdiscardPath = "/system/bin/secdiscard";
Paul Crowley63c18d32016-02-10 14:02:47 +000065static const char* kStretch_none = "none";
66static const char* kStretch_nopassword = "nopassword";
67static const std::string kStretchPrefix_scrypt = "scrypt ";
Paul Crowley1ef25582016-01-21 20:26:12 +000068static const char* kFn_encrypted_key = "encrypted_key";
Paul Crowley05720802016-02-08 15:55:41 +000069static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
Paul Crowleydff8c722016-05-16 08:14:56 -070070static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
Paul Crowley63c18d32016-02-10 14:02:47 +000071static const char* kFn_salt = "salt";
Paul Crowley1ef25582016-01-21 20:26:12 +000072static const char* kFn_secdiscardable = "secdiscardable";
Paul Crowley05720802016-02-08 15:55:41 +000073static const char* kFn_stretching = "stretching";
74static const char* kFn_version = "version";
Paul Crowley1ef25582016-01-21 20:26:12 +000075
Paul Crowley13ffd8e2016-01-27 14:30:22 +000076static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
Paul Crowley1ef25582016-01-21 20:26:12 +000077 if (actual != expected) {
Paul Crowleydf528a72016-03-09 09:31:37 -080078 LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got "
79 << actual;
Paul Crowley1ef25582016-01-21 20:26:12 +000080 return false;
81 }
82 return true;
83}
84
Paul Crowleydf528a72016-03-09 09:31:37 -080085static std::string hashSecdiscardable(const std::string& secdiscardable) {
Paul Crowley1ef25582016-01-21 20:26:12 +000086 SHA512_CTX c;
87
88 SHA512_Init(&c);
89 // Personalise the hashing by introducing a fixed prefix.
90 // Hashing applications should use personalization except when there is a
91 // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
Paul Crowley13ffd8e2016-01-27 14:30:22 +000092 std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
93 secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
94 SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
Paul Crowley1ef25582016-01-21 20:26:12 +000095 SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
96 std::string res(SHA512_DIGEST_LENGTH, '\0');
Paul Crowleydf528a72016-03-09 09:31:37 -080097 SHA512_Final(reinterpret_cast<uint8_t*>(&res[0]), &c);
Paul Crowley1ef25582016-01-21 20:26:12 +000098 return res;
99}
100
Paul Crowleydf528a72016-03-09 09:31:37 -0800101static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth,
102 const std::string& appId, std::string* key) {
Paul Crowley320e5e12016-03-04 14:07:05 -0800103 auto paramBuilder = keymaster::AuthorizationSetBuilder()
Paul Crowleydf528a72016-03-09 09:31:37 -0800104 .AesEncryptionKey(AES_KEY_BYTES * 8)
105 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
106 .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
107 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
Paul Crowleya051eb72016-03-08 16:08:32 -0800108 addStringParam(&paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
Paul Crowley320e5e12016-03-04 14:07:05 -0800109 if (auth.token.empty()) {
110 LOG(DEBUG) << "Creating key that doesn't need auth token";
111 paramBuilder.Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
112 } else {
113 LOG(DEBUG) << "Auth token required for key";
114 if (auth.token.size() != sizeof(hw_auth_token_t)) {
115 LOG(ERROR) << "Auth token should be " << sizeof(hw_auth_token_t) << " bytes, was "
Paul Crowleydf528a72016-03-09 09:31:37 -0800116 << auth.token.size() << " bytes";
Paul Crowley320e5e12016-03-04 14:07:05 -0800117 return false;
118 }
Paul Crowleydf528a72016-03-09 09:31:37 -0800119 const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data());
Paul Crowley320e5e12016-03-04 14:07:05 -0800120 paramBuilder.Authorization(keymaster::TAG_USER_SECURE_ID, at->user_id);
121 paramBuilder.Authorization(keymaster::TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD);
Paul Crowleyb3de3372016-04-27 12:58:41 -0700122 paramBuilder.Authorization(keymaster::TAG_AUTH_TIMEOUT, AUTH_TIMEOUT);
Paul Crowley320e5e12016-03-04 14:07:05 -0800123 }
124 return keymaster.generateKey(paramBuilder.build(), key);
125}
126
Paul Crowleydff8c722016-05-16 08:14:56 -0700127static keymaster::AuthorizationSet beginParams(const KeyAuthentication& auth,
128 const std::string& appId) {
Paul Crowley320e5e12016-03-04 14:07:05 -0800129 auto paramBuilder = keymaster::AuthorizationSetBuilder()
Paul Crowleydf528a72016-03-09 09:31:37 -0800130 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
131 .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
132 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
Paul Crowleya051eb72016-03-08 16:08:32 -0800133 addStringParam(&paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
Paul Crowley320e5e12016-03-04 14:07:05 -0800134 if (!auth.token.empty()) {
135 LOG(DEBUG) << "Supplying auth token to Keymaster";
Paul Crowleya051eb72016-03-08 16:08:32 -0800136 addStringParam(&paramBuilder, keymaster::TAG_AUTH_TOKEN, auth.token);
Paul Crowley320e5e12016-03-04 14:07:05 -0800137 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700138 return paramBuilder.build();
Paul Crowley1ef25582016-01-21 20:26:12 +0000139}
140
Paul Crowleydf528a72016-03-09 09:31:37 -0800141static bool readFileToString(const std::string& filename, std::string* result) {
Paul Crowleya051eb72016-03-08 16:08:32 -0800142 if (!android::base::ReadFileToString(filename, result)) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800143 PLOG(ERROR) << "Failed to read from " << filename;
144 return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000145 }
146 return true;
147}
148
Paul Crowleydf528a72016-03-09 09:31:37 -0800149static bool writeStringToFile(const std::string& payload, const std::string& filename) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000150 if (!android::base::WriteStringToFile(payload, filename)) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800151 PLOG(ERROR) << "Failed to write to " << filename;
152 return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000153 }
154 return true;
155}
156
Paul Crowleydff8c722016-05-16 08:14:56 -0700157static KeymasterOperation begin(Keymaster& keymaster, const std::string& dir,
158 keymaster_purpose_t purpose,
159 const keymaster::AuthorizationSet &keyParams,
160 const keymaster::AuthorizationSet &opParams,
161 keymaster::AuthorizationSet* outParams) {
162 auto kmKeyPath = dir + "/" + kFn_keymaster_key_blob;
163 std::string kmKey;
164 if (!readFileToString(kmKeyPath, &kmKey)) return KeymasterOperation();
165 keymaster::AuthorizationSet inParams;
166 inParams.push_back(keyParams);
167 inParams.push_back(opParams);
168 for (;;) {
169 auto opHandle = keymaster.begin(purpose, kmKey, inParams, outParams);
170 if (opHandle) {
171 return opHandle;
172 }
173 if (opHandle.error() != KM_ERROR_KEY_REQUIRES_UPGRADE) return opHandle;
174 LOG(DEBUG) << "Upgrading key: " << dir;
175 std::string newKey;
176 if (!keymaster.upgradeKey(kmKey, keyParams, &newKey)) return KeymasterOperation();
177 auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
178 if (!writeStringToFile(newKey, newKeyPath)) return KeymasterOperation();
179 if (rename(newKeyPath.c_str(), kmKeyPath.c_str()) != 0) {
180 PLOG(ERROR) << "Unable to move upgraded key to location: " << kmKeyPath;
181 return KeymasterOperation();
182 }
183 if (!keymaster.deleteKey(kmKey)) {
184 LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir;
185 }
186 kmKey = newKey;
187 LOG(INFO) << "Key upgraded: " << dir;
188 }
189}
190
191static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
192 const keymaster::AuthorizationSet &keyParams,
193 const std::string& message, std::string* ciphertext) {
194 keymaster::AuthorizationSet opParams;
195 keymaster::AuthorizationSet outParams;
196 auto opHandle = begin(keymaster, dir, KM_PURPOSE_ENCRYPT, keyParams, opParams, &outParams);
197 if (!opHandle) return false;
198 keymaster_blob_t nonceBlob;
199 if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
200 LOG(ERROR) << "GCM encryption but no nonce generated";
201 return false;
202 }
203 // nonceBlob here is just a pointer into existing data, must not be freed
204 std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length);
205 if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
206 std::string body;
207 if (!opHandle.updateCompletely(message, &body)) return false;
208
209 std::string mac;
210 if (!opHandle.finish(&mac)) return false;
211 if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
212 *ciphertext = nonce + body + mac;
213 return true;
214}
215
216static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
217 const keymaster::AuthorizationSet &keyParams,
218 const std::string& ciphertext, std::string* message) {
219 auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
220 auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
221 auto opParams = addStringParam(keymaster::AuthorizationSetBuilder(),
222 keymaster::TAG_NONCE, nonce).build();
223 auto opHandle = begin(keymaster, dir, KM_PURPOSE_DECRYPT, keyParams, opParams, nullptr);
224 if (!opHandle) return false;
225 if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
226 if (!opHandle.finish(nullptr)) return false;
227 return true;
228}
229
Paul Crowley63c18d32016-02-10 14:02:47 +0000230static std::string getStretching() {
231 char paramstr[PROPERTY_VALUE_MAX];
232
233 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
234 return std::string() + kStretchPrefix_scrypt + paramstr;
235}
236
Paul Crowleydf528a72016-03-09 09:31:37 -0800237static bool stretchingNeedsSalt(const std::string& stretching) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000238 return stretching != kStretch_nopassword && stretching != kStretch_none;
239}
240
Paul Crowleydf528a72016-03-09 09:31:37 -0800241static bool stretchSecret(const std::string& stretching, const std::string& secret,
242 const std::string& salt, std::string* stretched) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000243 if (stretching == kStretch_nopassword) {
244 if (!secret.empty()) {
Paul Crowleyd9b92952016-03-04 13:45:00 -0800245 LOG(WARNING) << "Password present but stretching is nopassword";
Paul Crowley63c18d32016-02-10 14:02:47 +0000246 // Continue anyway
247 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800248 stretched->clear();
Paul Crowley63c18d32016-02-10 14:02:47 +0000249 } else if (stretching == kStretch_none) {
Paul Crowleya051eb72016-03-08 16:08:32 -0800250 *stretched = secret;
Paul Crowleydf528a72016-03-09 09:31:37 -0800251 } else if (std::equal(kStretchPrefix_scrypt.begin(), kStretchPrefix_scrypt.end(),
252 stretching.begin())) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000253 int Nf, rf, pf;
Paul Crowleydf528a72016-03-09 09:31:37 -0800254 if (!parse_scrypt_parameters(stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf,
255 &rf, &pf)) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000256 LOG(ERROR) << "Unable to parse scrypt params in stretching: " << stretching;
257 return false;
258 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800259 stretched->assign(STRETCHED_BYTES, '\0');
Paul Crowleydf528a72016-03-09 09:31:37 -0800260 if (crypto_scrypt(reinterpret_cast<const uint8_t*>(secret.data()), secret.size(),
261 reinterpret_cast<const uint8_t*>(salt.data()), salt.size(),
262 1 << Nf, 1 << rf, 1 << pf,
263 reinterpret_cast<uint8_t*>(&(*stretched)[0]), stretched->size()) != 0) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000264 LOG(ERROR) << "scrypt failed with params: " << stretching;
265 return false;
266 }
267 } else {
268 LOG(ERROR) << "Unknown stretching type: " << stretching;
269 return false;
270 }
271 return true;
272}
273
Paul Crowleydf528a72016-03-09 09:31:37 -0800274static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching,
275 const std::string& salt, const std::string& secdiscardable,
276 std::string* appId) {
Paul Crowley63c18d32016-02-10 14:02:47 +0000277 std::string stretched;
Paul Crowleya051eb72016-03-08 16:08:32 -0800278 if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false;
279 *appId = hashSecdiscardable(secdiscardable) + stretched;
Paul Crowley63c18d32016-02-10 14:02:47 +0000280 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000281}
282
Paul Crowleydf528a72016-03-09 09:31:37 -0800283bool storeKey(const std::string& dir, const KeyAuthentication& auth, const std::string& key) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000284 if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
285 PLOG(ERROR) << "key mkdir " << dir;
286 return false;
287 }
Paul Crowleydf528a72016-03-09 09:31:37 -0800288 if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000289 std::string secdiscardable;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000290 if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000291 // TODO status_t plays badly with PLOG, fix it.
292 LOG(ERROR) << "Random read failed";
293 return false;
294 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000295 if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000296 std::string stretching = auth.secret.empty() ? kStretch_nopassword : getStretching();
Paul Crowleydf528a72016-03-09 09:31:37 -0800297 if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000298 std::string salt;
299 if (stretchingNeedsSalt(stretching)) {
300 if (ReadRandomBytes(SALT_BYTES, salt) != OK) {
301 LOG(ERROR) << "Random read failed";
302 return false;
303 }
Paul Crowleydf528a72016-03-09 09:31:37 -0800304 if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000305 }
Paul Crowley320e5e12016-03-04 14:07:05 -0800306 std::string appId;
Paul Crowleya051eb72016-03-08 16:08:32 -0800307 if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000308 Keymaster keymaster;
309 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000310 std::string kmKey;
Paul Crowleya051eb72016-03-08 16:08:32 -0800311 if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000312 if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
Paul Crowleydff8c722016-05-16 08:14:56 -0700313 auto keyParams = beginParams(auth, appId);
Paul Crowley320e5e12016-03-04 14:07:05 -0800314 std::string encryptedKey;
Paul Crowleydff8c722016-05-16 08:14:56 -0700315 if (!encryptWithKeymasterKey(keymaster, dir, keyParams, key, &encryptedKey)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000316 if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000317 return true;
318}
319
Paul Crowleydf528a72016-03-09 09:31:37 -0800320bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, std::string* key) {
Paul Crowley05720802016-02-08 15:55:41 +0000321 std::string version;
Paul Crowleya051eb72016-03-08 16:08:32 -0800322 if (!readFileToString(dir + "/" + kFn_version, &version)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000323 if (version != kCurrentVersion) {
324 LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version;
325 return false;
326 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000327 std::string secdiscardable;
Paul Crowleya051eb72016-03-08 16:08:32 -0800328 if (!readFileToString(dir + "/" + kFn_secdiscardable, &secdiscardable)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000329 std::string stretching;
Paul Crowleya051eb72016-03-08 16:08:32 -0800330 if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000331 std::string salt;
332 if (stretchingNeedsSalt(stretching)) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800333 if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false;
Paul Crowley63c18d32016-02-10 14:02:47 +0000334 }
Paul Crowley320e5e12016-03-04 14:07:05 -0800335 std::string appId;
Paul Crowleya051eb72016-03-08 16:08:32 -0800336 if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000337 std::string encryptedMessage;
Paul Crowleya051eb72016-03-08 16:08:32 -0800338 if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000339 Keymaster keymaster;
340 if (!keymaster) return false;
Paul Crowleydff8c722016-05-16 08:14:56 -0700341 auto keyParams = beginParams(auth, appId);
342 return decryptWithKeymasterKey(keymaster, dir, keyParams, encryptedMessage, key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000343}
344
Paul Crowleydf528a72016-03-09 09:31:37 -0800345static bool deleteKey(const std::string& dir) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000346 std::string kmKey;
Paul Crowleya051eb72016-03-08 16:08:32 -0800347 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000348 Keymaster keymaster;
349 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000350 if (!keymaster.deleteKey(kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000351 return true;
352}
353
Paul Crowleybeb33a62016-07-07 10:06:30 -0700354static bool runSecdiscard(const std::string& dir) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800355 if (ForkExecvp(
Paul Crowleybeb33a62016-07-07 10:06:30 -0700356 std::vector<std::string>{kSecdiscardPath, "--",
357 dir + "/" + kFn_encrypted_key,
358 dir + "/" + kFn_keymaster_key_blob,
359 dir + "/" + kFn_secdiscardable,
360 }) != 0) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000361 LOG(ERROR) << "secdiscard failed";
362 return false;
363 }
364 return true;
365}
366
Paul Crowleydf528a72016-03-09 09:31:37 -0800367static bool recursiveDeleteKey(const std::string& dir) {
368 if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000369 LOG(ERROR) << "recursive delete failed";
370 return false;
371 }
372 return true;
373}
374
Paul Crowleydf528a72016-03-09 09:31:37 -0800375bool destroyKey(const std::string& dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000376 bool success = true;
377 // Try each thing, even if previous things failed.
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000378 success &= deleteKey(dir);
Paul Crowleybeb33a62016-07-07 10:06:30 -0700379 success &= runSecdiscard(dir);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000380 success &= recursiveDeleteKey(dir);
Paul Crowley1ef25582016-01-21 20:26:12 +0000381 return success;
382}
383
384} // namespace vold
385} // namespace android