2016-01-21 21:26:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "KeyStorage.h"
|
|
|
|
|
|
|
|
#include "Keymaster.h"
|
2016-02-10 15:02:47 +01:00
|
|
|
#include "ScryptParameters.h"
|
2016-01-21 21:26:12 +01:00
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-05-16 17:14:56 +02:00
|
|
|
#include <stdio.h>
|
2016-01-21 21:26:12 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
|
2016-02-10 15:02:47 +01:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2016-03-04 23:07:05 +01:00
|
|
|
#include <hardware/hw_auth_token.h>
|
|
|
|
|
2016-10-26 15:27:10 +02:00
|
|
|
#include <keystore/authorization_set.h>
|
|
|
|
#include <keystore/keystore_hidl_support.h>
|
2016-01-21 21:26:12 +01:00
|
|
|
|
2016-02-10 15:02:47 +01:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
#include "crypto_scrypt.h"
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:26:12 +01:00
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
2016-10-26 15:27:10 +02:00
|
|
|
using namespace keystore;
|
2016-01-21 21:26:12 +01:00
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
const KeyAuthentication kEmptyAuthentication{"", ""};
|
2016-02-08 16:55:41 +01:00
|
|
|
|
2016-01-21 21:26:12 +01:00
|
|
|
static constexpr size_t AES_KEY_BYTES = 32;
|
|
|
|
static constexpr size_t GCM_NONCE_BYTES = 12;
|
|
|
|
static constexpr size_t GCM_MAC_BYTES = 16;
|
2016-03-09 18:31:37 +01:00
|
|
|
static constexpr size_t SALT_BYTES = 1 << 4;
|
|
|
|
static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14;
|
|
|
|
static constexpr size_t STRETCHED_BYTES = 1 << 6;
|
2016-01-21 21:26:12 +01:00
|
|
|
|
2016-04-27 21:58:41 +02:00
|
|
|
static constexpr uint32_t AUTH_TIMEOUT = 30; // Seconds
|
|
|
|
|
2016-02-08 16:55:41 +01:00
|
|
|
static const char* kCurrentVersion = "1";
|
2016-01-21 21:26:12 +01:00
|
|
|
static const char* kRmPath = "/system/bin/rm";
|
|
|
|
static const char* kSecdiscardPath = "/system/bin/secdiscard";
|
2016-02-10 15:02:47 +01:00
|
|
|
static const char* kStretch_none = "none";
|
|
|
|
static const char* kStretch_nopassword = "nopassword";
|
|
|
|
static const std::string kStretchPrefix_scrypt = "scrypt ";
|
2016-01-21 21:26:12 +01:00
|
|
|
static const char* kFn_encrypted_key = "encrypted_key";
|
2016-02-08 16:55:41 +01:00
|
|
|
static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
|
2016-05-16 17:14:56 +02:00
|
|
|
static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
|
2016-02-10 15:02:47 +01:00
|
|
|
static const char* kFn_salt = "salt";
|
2016-01-21 21:26:12 +01:00
|
|
|
static const char* kFn_secdiscardable = "secdiscardable";
|
2016-02-08 16:55:41 +01:00
|
|
|
static const char* kFn_stretching = "stretching";
|
|
|
|
static const char* kFn_version = "version";
|
2016-01-21 21:26:12 +01:00
|
|
|
|
2016-01-27 15:30:22 +01:00
|
|
|
static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
|
2016-01-21 21:26:12 +01:00
|
|
|
if (actual != expected) {
|
2016-03-09 18:31:37 +01:00
|
|
|
LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got "
|
|
|
|
<< actual;
|
2016-01-21 21:26:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static std::string hashSecdiscardable(const std::string& secdiscardable) {
|
2016-01-21 21:26:12 +01:00
|
|
|
SHA512_CTX c;
|
|
|
|
|
|
|
|
SHA512_Init(&c);
|
|
|
|
// Personalise the hashing by introducing a fixed prefix.
|
|
|
|
// Hashing applications should use personalization except when there is a
|
|
|
|
// specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
|
2016-01-27 15:30:22 +01:00
|
|
|
std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
|
|
|
|
secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
|
|
|
|
SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
|
2016-01-21 21:26:12 +01:00
|
|
|
SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
|
|
|
|
std::string res(SHA512_DIGEST_LENGTH, '\0');
|
2016-03-09 18:31:37 +01:00
|
|
|
SHA512_Final(reinterpret_cast<uint8_t*>(&res[0]), &c);
|
2016-01-21 21:26:12 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth,
|
|
|
|
const std::string& appId, std::string* key) {
|
2016-10-26 15:27:10 +02:00
|
|
|
auto paramBuilder = AuthorizationSetBuilder()
|
2016-03-09 18:31:37 +01:00
|
|
|
.AesEncryptionKey(AES_KEY_BYTES * 8)
|
2016-10-26 15:27:10 +02:00
|
|
|
.Authorization(TAG_BLOCK_MODE, BlockMode::GCM)
|
|
|
|
.Authorization(TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
|
|
|
|
.Authorization(TAG_PADDING, PaddingMode::NONE)
|
|
|
|
.Authorization(TAG_APPLICATION_ID, blob2hidlVec(appId));
|
2016-03-04 23:07:05 +01:00
|
|
|
if (auth.token.empty()) {
|
|
|
|
LOG(DEBUG) << "Creating key that doesn't need auth token";
|
2016-10-26 15:27:10 +02:00
|
|
|
paramBuilder.Authorization(TAG_NO_AUTH_REQUIRED);
|
2016-03-04 23:07:05 +01:00
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "Auth token required for key";
|
|
|
|
if (auth.token.size() != sizeof(hw_auth_token_t)) {
|
|
|
|
LOG(ERROR) << "Auth token should be " << sizeof(hw_auth_token_t) << " bytes, was "
|
2016-03-09 18:31:37 +01:00
|
|
|
<< auth.token.size() << " bytes";
|
2016-03-04 23:07:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 18:31:37 +01:00
|
|
|
const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data());
|
2016-10-26 15:27:10 +02:00
|
|
|
paramBuilder.Authorization(TAG_USER_SECURE_ID, at->user_id);
|
|
|
|
paramBuilder.Authorization(TAG_USER_AUTH_TYPE, HardwareAuthenticatorType::PASSWORD);
|
|
|
|
paramBuilder.Authorization(TAG_AUTH_TIMEOUT, AUTH_TIMEOUT);
|
2016-03-04 23:07:05 +01:00
|
|
|
}
|
2016-10-26 15:27:10 +02:00
|
|
|
return keymaster.generateKey(paramBuilder, key);
|
2016-03-04 23:07:05 +01:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:27:10 +02:00
|
|
|
static AuthorizationSet beginParams(const KeyAuthentication& auth,
|
2016-05-16 17:14:56 +02:00
|
|
|
const std::string& appId) {
|
2016-10-26 15:27:10 +02:00
|
|
|
auto paramBuilder = AuthorizationSetBuilder()
|
|
|
|
.Authorization(TAG_BLOCK_MODE, BlockMode::GCM)
|
|
|
|
.Authorization(TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
|
|
|
|
.Authorization(TAG_PADDING, PaddingMode::NONE)
|
|
|
|
.Authorization(TAG_APPLICATION_ID, blob2hidlVec(appId));
|
2016-03-04 23:07:05 +01:00
|
|
|
if (!auth.token.empty()) {
|
|
|
|
LOG(DEBUG) << "Supplying auth token to Keymaster";
|
2016-10-26 15:27:10 +02:00
|
|
|
paramBuilder.Authorization(TAG_AUTH_TOKEN, blob2hidlVec(auth.token));
|
2016-03-04 23:07:05 +01:00
|
|
|
}
|
2016-10-26 15:27:10 +02:00
|
|
|
return paramBuilder;
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:14:56 +02:00
|
|
|
static bool readFileToString(const std::string& filename, std::string* result) {
|
|
|
|
if (!android::base::ReadFileToString(filename, result)) {
|
|
|
|
PLOG(ERROR) << "Failed to read from " << filename;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool writeStringToFile(const std::string& payload, const std::string& filename) {
|
|
|
|
if (!android::base::WriteStringToFile(payload, filename)) {
|
|
|
|
PLOG(ERROR) << "Failed to write to " << filename;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeymasterOperation begin(Keymaster& keymaster, const std::string& dir,
|
2016-10-26 15:27:10 +02:00
|
|
|
KeyPurpose purpose,
|
|
|
|
const AuthorizationSet &keyParams,
|
|
|
|
const AuthorizationSet &opParams,
|
|
|
|
AuthorizationSet* outParams) {
|
2016-05-16 17:14:56 +02:00
|
|
|
auto kmKeyPath = dir + "/" + kFn_keymaster_key_blob;
|
|
|
|
std::string kmKey;
|
|
|
|
if (!readFileToString(kmKeyPath, &kmKey)) return KeymasterOperation();
|
2016-10-26 15:27:10 +02:00
|
|
|
AuthorizationSet inParams(keyParams);
|
|
|
|
inParams.append(opParams.begin(), opParams.end());
|
2016-05-16 17:14:56 +02:00
|
|
|
for (;;) {
|
|
|
|
auto opHandle = keymaster.begin(purpose, kmKey, inParams, outParams);
|
|
|
|
if (opHandle) {
|
|
|
|
return opHandle;
|
|
|
|
}
|
2016-10-26 15:27:10 +02:00
|
|
|
if (opHandle.error() != ErrorCode::KEY_REQUIRES_UPGRADE) return opHandle;
|
2016-05-16 17:14:56 +02:00
|
|
|
LOG(DEBUG) << "Upgrading key: " << dir;
|
|
|
|
std::string newKey;
|
|
|
|
if (!keymaster.upgradeKey(kmKey, keyParams, &newKey)) return KeymasterOperation();
|
|
|
|
auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
|
|
|
|
if (!writeStringToFile(newKey, newKeyPath)) return KeymasterOperation();
|
|
|
|
if (rename(newKeyPath.c_str(), kmKeyPath.c_str()) != 0) {
|
|
|
|
PLOG(ERROR) << "Unable to move upgraded key to location: " << kmKeyPath;
|
|
|
|
return KeymasterOperation();
|
|
|
|
}
|
|
|
|
if (!keymaster.deleteKey(kmKey)) {
|
|
|
|
LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir;
|
|
|
|
}
|
|
|
|
kmKey = newKey;
|
|
|
|
LOG(INFO) << "Key upgraded: " << dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
|
2016-10-26 15:27:10 +02:00
|
|
|
const AuthorizationSet &keyParams,
|
2016-03-09 18:31:37 +01:00
|
|
|
const std::string& message, std::string* ciphertext) {
|
2016-10-26 15:27:10 +02:00
|
|
|
AuthorizationSet opParams;
|
|
|
|
AuthorizationSet outParams;
|
|
|
|
auto opHandle = begin(keymaster, dir, KeyPurpose::ENCRYPT, keyParams, opParams, &outParams);
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!opHandle) return false;
|
2016-10-26 15:27:10 +02:00
|
|
|
auto nonceBlob = outParams.GetTagValue(TAG_NONCE);
|
|
|
|
if (!nonceBlob.isOk()) {
|
2016-01-21 21:26:12 +01:00
|
|
|
LOG(ERROR) << "GCM encryption but no nonce generated";
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-27 15:30:22 +01:00
|
|
|
// nonceBlob here is just a pointer into existing data, must not be freed
|
2016-10-26 15:27:10 +02:00
|
|
|
std::string nonce(reinterpret_cast<const char*>(&nonceBlob.value()[0]), nonceBlob.value().size());
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
std::string body;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!opHandle.updateCompletely(message, &body)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
|
|
|
|
std::string mac;
|
2016-05-16 17:14:56 +02:00
|
|
|
if (!opHandle.finish(&mac)) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
|
2016-03-09 01:08:32 +01:00
|
|
|
*ciphertext = nonce + body + mac;
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:14:56 +02:00
|
|
|
static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
|
2016-10-26 15:27:10 +02:00
|
|
|
const AuthorizationSet &keyParams,
|
2016-03-09 18:31:37 +01:00
|
|
|
const std::string& ciphertext, std::string* message) {
|
2016-01-21 21:26:12 +01:00
|
|
|
auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
|
2016-01-27 15:30:22 +01:00
|
|
|
auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
|
2016-10-26 15:27:10 +02:00
|
|
|
auto opParams = AuthorizationSetBuilder()
|
|
|
|
.Authorization(TAG_NONCE, blob2hidlVec(nonce));
|
|
|
|
auto opHandle = begin(keymaster, dir, KeyPurpose::DECRYPT, keyParams, opParams, nullptr);
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!opHandle) return false;
|
|
|
|
if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
|
2016-05-16 17:14:56 +02:00
|
|
|
if (!opHandle.finish(nullptr)) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:02:47 +01:00
|
|
|
static std::string getStretching() {
|
|
|
|
char paramstr[PROPERTY_VALUE_MAX];
|
|
|
|
|
|
|
|
property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
|
|
|
|
return std::string() + kStretchPrefix_scrypt + paramstr;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool stretchingNeedsSalt(const std::string& stretching) {
|
2016-02-10 15:02:47 +01:00
|
|
|
return stretching != kStretch_nopassword && stretching != kStretch_none;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool stretchSecret(const std::string& stretching, const std::string& secret,
|
|
|
|
const std::string& salt, std::string* stretched) {
|
2016-02-10 15:02:47 +01:00
|
|
|
if (stretching == kStretch_nopassword) {
|
|
|
|
if (!secret.empty()) {
|
2016-03-04 22:45:00 +01:00
|
|
|
LOG(WARNING) << "Password present but stretching is nopassword";
|
2016-02-10 15:02:47 +01:00
|
|
|
// Continue anyway
|
|
|
|
}
|
2016-03-09 01:08:32 +01:00
|
|
|
stretched->clear();
|
2016-02-10 15:02:47 +01:00
|
|
|
} else if (stretching == kStretch_none) {
|
2016-03-09 01:08:32 +01:00
|
|
|
*stretched = secret;
|
2016-03-09 18:31:37 +01:00
|
|
|
} else if (std::equal(kStretchPrefix_scrypt.begin(), kStretchPrefix_scrypt.end(),
|
|
|
|
stretching.begin())) {
|
2016-02-10 15:02:47 +01:00
|
|
|
int Nf, rf, pf;
|
2016-03-09 18:31:37 +01:00
|
|
|
if (!parse_scrypt_parameters(stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf,
|
|
|
|
&rf, &pf)) {
|
2016-02-10 15:02:47 +01:00
|
|
|
LOG(ERROR) << "Unable to parse scrypt params in stretching: " << stretching;
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 01:08:32 +01:00
|
|
|
stretched->assign(STRETCHED_BYTES, '\0');
|
2016-03-09 18:31:37 +01:00
|
|
|
if (crypto_scrypt(reinterpret_cast<const uint8_t*>(secret.data()), secret.size(),
|
|
|
|
reinterpret_cast<const uint8_t*>(salt.data()), salt.size(),
|
|
|
|
1 << Nf, 1 << rf, 1 << pf,
|
|
|
|
reinterpret_cast<uint8_t*>(&(*stretched)[0]), stretched->size()) != 0) {
|
2016-02-10 15:02:47 +01:00
|
|
|
LOG(ERROR) << "scrypt failed with params: " << stretching;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unknown stretching type: " << stretching;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching,
|
|
|
|
const std::string& salt, const std::string& secdiscardable,
|
|
|
|
std::string* appId) {
|
2016-02-10 15:02:47 +01:00
|
|
|
std::string stretched;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false;
|
|
|
|
*appId = hashSecdiscardable(secdiscardable) + stretched;
|
2016-02-10 15:02:47 +01:00
|
|
|
return true;
|
2016-02-08 16:55:41 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool storeKey(const std::string& dir, const KeyAuthentication& auth, const std::string& key) {
|
2016-01-21 21:26:12 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
|
|
|
|
PLOG(ERROR) << "key mkdir " << dir;
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 18:31:37 +01:00
|
|
|
if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
std::string secdiscardable;
|
2016-01-27 15:30:22 +01:00
|
|
|
if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
|
2016-01-21 21:26:12 +01:00
|
|
|
// TODO status_t plays badly with PLOG, fix it.
|
|
|
|
LOG(ERROR) << "Random read failed";
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
|
2016-02-10 15:02:47 +01:00
|
|
|
std::string stretching = auth.secret.empty() ? kStretch_nopassword : getStretching();
|
2016-03-09 18:31:37 +01:00
|
|
|
if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
|
2016-02-10 15:02:47 +01:00
|
|
|
std::string salt;
|
|
|
|
if (stretchingNeedsSalt(stretching)) {
|
|
|
|
if (ReadRandomBytes(SALT_BYTES, salt) != OK) {
|
|
|
|
LOG(ERROR) << "Random read failed";
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 18:31:37 +01:00
|
|
|
if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false;
|
2016-02-10 15:02:47 +01:00
|
|
|
}
|
2016-03-04 23:07:05 +01:00
|
|
|
std::string appId;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
Keymaster keymaster;
|
|
|
|
if (!keymaster) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
std::string kmKey;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
|
2016-05-16 17:14:56 +02:00
|
|
|
auto keyParams = beginParams(auth, appId);
|
2016-03-04 23:07:05 +01:00
|
|
|
std::string encryptedKey;
|
2016-05-16 17:14:56 +02:00
|
|
|
if (!encryptWithKeymasterKey(keymaster, dir, keyParams, key, &encryptedKey)) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, std::string* key) {
|
2016-02-08 16:55:41 +01:00
|
|
|
std::string version;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!readFileToString(dir + "/" + kFn_version, &version)) return false;
|
2016-02-08 16:55:41 +01:00
|
|
|
if (version != kCurrentVersion) {
|
|
|
|
LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version;
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-21 21:26:12 +01:00
|
|
|
std::string secdiscardable;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!readFileToString(dir + "/" + kFn_secdiscardable, &secdiscardable)) return false;
|
2016-02-10 15:02:47 +01:00
|
|
|
std::string stretching;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false;
|
2016-02-10 15:02:47 +01:00
|
|
|
std::string salt;
|
|
|
|
if (stretchingNeedsSalt(stretching)) {
|
2016-03-09 18:31:37 +01:00
|
|
|
if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false;
|
2016-02-10 15:02:47 +01:00
|
|
|
}
|
2016-03-04 23:07:05 +01:00
|
|
|
std::string appId;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
std::string encryptedMessage;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
Keymaster keymaster;
|
|
|
|
if (!keymaster) return false;
|
2016-05-16 17:14:56 +02:00
|
|
|
auto keyParams = beginParams(auth, appId);
|
|
|
|
return decryptWithKeymasterKey(keymaster, dir, keyParams, encryptedMessage, key);
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool deleteKey(const std::string& dir) {
|
2016-01-27 15:30:22 +01:00
|
|
|
std::string kmKey;
|
2016-03-09 01:08:32 +01:00
|
|
|
if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
Keymaster keymaster;
|
|
|
|
if (!keymaster) return false;
|
2016-01-27 15:30:22 +01:00
|
|
|
if (!keymaster.deleteKey(kmKey)) return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:06:30 +02:00
|
|
|
static bool runSecdiscard(const std::string& dir) {
|
2016-03-09 18:31:37 +01:00
|
|
|
if (ForkExecvp(
|
2016-07-07 19:06:30 +02:00
|
|
|
std::vector<std::string>{kSecdiscardPath, "--",
|
|
|
|
dir + "/" + kFn_encrypted_key,
|
|
|
|
dir + "/" + kFn_keymaster_key_blob,
|
|
|
|
dir + "/" + kFn_secdiscardable,
|
|
|
|
}) != 0) {
|
2016-01-21 21:26:12 +01:00
|
|
|
LOG(ERROR) << "secdiscard failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool recursiveDeleteKey(const std::string& dir) {
|
|
|
|
if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) {
|
2016-01-21 21:26:12 +01:00
|
|
|
LOG(ERROR) << "recursive delete failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool destroyKey(const std::string& dir) {
|
2016-01-21 21:26:12 +01:00
|
|
|
bool success = true;
|
|
|
|
// Try each thing, even if previous things failed.
|
2016-01-27 15:30:22 +01:00
|
|
|
success &= deleteKey(dir);
|
2016-07-07 19:06:30 +02:00
|
|
|
success &= runSecdiscard(dir);
|
2016-01-27 15:30:22 +01:00
|
|
|
success &= recursiveDeleteKey(dir);
|
2016-01-21 21:26:12 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|