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