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> |
| 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 Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 36 | #include <cutils/properties.h> |
| 37 | |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 38 | #include <hardware/hw_auth_token.h> |
| 39 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 40 | #include <keymaster/authorization_set.h> |
| 41 | |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 42 | extern "C" { |
| 43 | |
| 44 | #include "crypto_scrypt.h" |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 47 | namespace android { |
| 48 | namespace vold { |
| 49 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 50 | const KeyAuthentication kEmptyAuthentication{"", ""}; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 51 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 52 | static constexpr size_t AES_KEY_BYTES = 32; |
| 53 | static constexpr size_t GCM_NONCE_BYTES = 12; |
| 54 | static constexpr size_t GCM_MAC_BYTES = 16; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 55 | static constexpr size_t SALT_BYTES = 1 << 4; |
| 56 | static constexpr size_t SECDISCARDABLE_BYTES = 1 << 14; |
| 57 | static constexpr size_t STRETCHED_BYTES = 1 << 6; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 58 | |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 59 | static const char* kCurrentVersion = "1"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 60 | static const char* kRmPath = "/system/bin/rm"; |
| 61 | static const char* kSecdiscardPath = "/system/bin/secdiscard"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 62 | static const char* kStretch_none = "none"; |
| 63 | static const char* kStretch_nopassword = "nopassword"; |
| 64 | static const std::string kStretchPrefix_scrypt = "scrypt "; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 65 | static const char* kFn_encrypted_key = "encrypted_key"; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 66 | static const char* kFn_keymaster_key_blob = "keymaster_key_blob"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 67 | static const char* kFn_salt = "salt"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 68 | static const char* kFn_secdiscardable = "secdiscardable"; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 69 | static const char* kFn_stretching = "stretching"; |
| 70 | static const char* kFn_version = "version"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 71 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 72 | 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] | 73 | if (actual != expected) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 74 | LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected << " got " |
| 75 | << actual; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 81 | static std::string hashSecdiscardable(const std::string& secdiscardable) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 82 | SHA512_CTX c; |
| 83 | |
| 84 | SHA512_Init(&c); |
| 85 | // Personalise the hashing by introducing a fixed prefix. |
| 86 | // Hashing applications should use personalization except when there is a |
| 87 | // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 88 | std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512"; |
| 89 | secdiscardableHashingPrefix.resize(SHA512_CBLOCK); |
| 90 | SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size()); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 91 | SHA512_Update(&c, secdiscardable.data(), secdiscardable.size()); |
| 92 | std::string res(SHA512_DIGEST_LENGTH, '\0'); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 93 | SHA512_Final(reinterpret_cast<uint8_t*>(&res[0]), &c); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 94 | return res; |
| 95 | } |
| 96 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 97 | static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth, |
| 98 | const std::string& appId, std::string* key) { |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 99 | auto paramBuilder = keymaster::AuthorizationSetBuilder() |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 100 | .AesEncryptionKey(AES_KEY_BYTES * 8) |
| 101 | .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM) |
| 102 | .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8) |
| 103 | .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE); |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 104 | addStringParam(¶mBuilder, keymaster::TAG_APPLICATION_ID, appId); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 105 | if (auth.token.empty()) { |
| 106 | LOG(DEBUG) << "Creating key that doesn't need auth token"; |
| 107 | paramBuilder.Authorization(keymaster::TAG_NO_AUTH_REQUIRED); |
| 108 | } else { |
| 109 | LOG(DEBUG) << "Auth token required for key"; |
| 110 | if (auth.token.size() != sizeof(hw_auth_token_t)) { |
| 111 | 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^] | 112 | << auth.token.size() << " bytes"; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 113 | return false; |
| 114 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 115 | const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data()); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 116 | paramBuilder.Authorization(keymaster::TAG_USER_SECURE_ID, at->user_id); |
| 117 | paramBuilder.Authorization(keymaster::TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD); |
| 118 | paramBuilder.Authorization(keymaster::TAG_AUTH_TIMEOUT, 5); |
| 119 | } |
| 120 | return keymaster.generateKey(paramBuilder.build(), key); |
| 121 | } |
| 122 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 123 | static keymaster::AuthorizationSetBuilder beginParams(const KeyAuthentication& auth, |
| 124 | const std::string& appId) { |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 125 | auto paramBuilder = keymaster::AuthorizationSetBuilder() |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 126 | .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM) |
| 127 | .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8) |
| 128 | .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE); |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 129 | addStringParam(¶mBuilder, keymaster::TAG_APPLICATION_ID, appId); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 130 | if (!auth.token.empty()) { |
| 131 | LOG(DEBUG) << "Supplying auth token to Keymaster"; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 132 | addStringParam(¶mBuilder, keymaster::TAG_AUTH_TOKEN, auth.token); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 133 | } |
| 134 | return paramBuilder; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 137 | static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& key, |
| 138 | const KeyAuthentication& auth, const std::string& appId, |
| 139 | const std::string& message, std::string* ciphertext) { |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 140 | auto params = beginParams(auth, appId).build(); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 141 | keymaster::AuthorizationSet outParams; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 142 | auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, &outParams); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 143 | if (!opHandle) return false; |
| 144 | keymaster_blob_t nonceBlob; |
| 145 | if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 146 | LOG(ERROR) << "GCM encryption but no nonce generated"; |
| 147 | return false; |
| 148 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 149 | // nonceBlob here is just a pointer into existing data, must not be freed |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 150 | std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 151 | if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 152 | std::string body; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 153 | if (!opHandle.updateCompletely(message, &body)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 154 | |
| 155 | std::string mac; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 156 | if (!opHandle.finishWithOutput(&mac)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 157 | if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 158 | *ciphertext = nonce + body + mac; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 162 | static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& key, |
| 163 | const KeyAuthentication& auth, const std::string& appId, |
| 164 | const std::string& ciphertext, std::string* message) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 165 | auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 166 | auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES); |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 167 | auto params = addStringParam(beginParams(auth, appId), keymaster::TAG_NONCE, nonce).build(); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 168 | auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params); |
| 169 | if (!opHandle) return false; |
| 170 | if (!opHandle.updateCompletely(bodyAndMac, message)) return false; |
| 171 | if (!opHandle.finish()) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 175 | static bool readFileToString(const std::string& filename, std::string* result) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 176 | if (!android::base::ReadFileToString(filename, result)) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 177 | PLOG(ERROR) << "Failed to read from " << filename; |
| 178 | return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 183 | static bool writeStringToFile(const std::string& payload, const std::string& filename) { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 184 | if (!android::base::WriteStringToFile(payload, filename)) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 185 | PLOG(ERROR) << "Failed to write to " << filename; |
| 186 | return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 191 | static std::string getStretching() { |
| 192 | char paramstr[PROPERTY_VALUE_MAX]; |
| 193 | |
| 194 | property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS); |
| 195 | return std::string() + kStretchPrefix_scrypt + paramstr; |
| 196 | } |
| 197 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 198 | static bool stretchingNeedsSalt(const std::string& stretching) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 199 | return stretching != kStretch_nopassword && stretching != kStretch_none; |
| 200 | } |
| 201 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 202 | static bool stretchSecret(const std::string& stretching, const std::string& secret, |
| 203 | const std::string& salt, std::string* stretched) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 204 | if (stretching == kStretch_nopassword) { |
| 205 | if (!secret.empty()) { |
Paul Crowley | d9b9295 | 2016-03-04 13:45:00 -0800 | [diff] [blame] | 206 | LOG(WARNING) << "Password present but stretching is nopassword"; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 207 | // Continue anyway |
| 208 | } |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 209 | stretched->clear(); |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 210 | } else if (stretching == kStretch_none) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 211 | *stretched = secret; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 212 | } else if (std::equal(kStretchPrefix_scrypt.begin(), kStretchPrefix_scrypt.end(), |
| 213 | stretching.begin())) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 214 | int Nf, rf, pf; |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 215 | if (!parse_scrypt_parameters(stretching.substr(kStretchPrefix_scrypt.size()).c_str(), &Nf, |
| 216 | &rf, &pf)) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 217 | LOG(ERROR) << "Unable to parse scrypt params in stretching: " << stretching; |
| 218 | return false; |
| 219 | } |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 220 | stretched->assign(STRETCHED_BYTES, '\0'); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 221 | if (crypto_scrypt(reinterpret_cast<const uint8_t*>(secret.data()), secret.size(), |
| 222 | reinterpret_cast<const uint8_t*>(salt.data()), salt.size(), |
| 223 | 1 << Nf, 1 << rf, 1 << pf, |
| 224 | reinterpret_cast<uint8_t*>(&(*stretched)[0]), stretched->size()) != 0) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 225 | LOG(ERROR) << "scrypt failed with params: " << stretching; |
| 226 | return false; |
| 227 | } |
| 228 | } else { |
| 229 | LOG(ERROR) << "Unknown stretching type: " << stretching; |
| 230 | return false; |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 235 | static bool generateAppId(const KeyAuthentication& auth, const std::string& stretching, |
| 236 | const std::string& salt, const std::string& secdiscardable, |
| 237 | std::string* appId) { |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 238 | std::string stretched; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 239 | if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false; |
| 240 | *appId = hashSecdiscardable(secdiscardable) + stretched; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 241 | return true; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 244 | 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] | 245 | if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) { |
| 246 | PLOG(ERROR) << "key mkdir " << dir; |
| 247 | return false; |
| 248 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 249 | if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 250 | std::string secdiscardable; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 251 | if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 252 | // TODO status_t plays badly with PLOG, fix it. |
| 253 | LOG(ERROR) << "Random read failed"; |
| 254 | return false; |
| 255 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 256 | if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 257 | std::string stretching = auth.secret.empty() ? kStretch_nopassword : getStretching(); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 258 | if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 259 | std::string salt; |
| 260 | if (stretchingNeedsSalt(stretching)) { |
| 261 | if (ReadRandomBytes(SALT_BYTES, salt) != OK) { |
| 262 | LOG(ERROR) << "Random read failed"; |
| 263 | return false; |
| 264 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 265 | if (!writeStringToFile(salt, dir + "/" + kFn_salt)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 266 | } |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 267 | std::string appId; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 268 | if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 269 | Keymaster keymaster; |
| 270 | if (!keymaster) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 271 | std::string kmKey; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 272 | if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 273 | if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 274 | std::string encryptedKey; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 275 | if (!encryptWithKeymasterKey(keymaster, kmKey, auth, appId, key, &encryptedKey)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 276 | if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 280 | bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, std::string* key) { |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 281 | std::string version; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 282 | if (!readFileToString(dir + "/" + kFn_version, &version)) return false; |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 283 | if (version != kCurrentVersion) { |
| 284 | LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version; |
| 285 | return false; |
| 286 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 287 | std::string secdiscardable; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 288 | if (!readFileToString(dir + "/" + kFn_secdiscardable, &secdiscardable)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 289 | std::string stretching; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 290 | if (!readFileToString(dir + "/" + kFn_stretching, &stretching)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 291 | std::string salt; |
| 292 | if (stretchingNeedsSalt(stretching)) { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 293 | if (!readFileToString(dir + "/" + kFn_salt, &salt)) return false; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 294 | } |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 295 | std::string appId; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 296 | if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 297 | std::string kmKey; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 298 | if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 299 | std::string encryptedMessage; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 300 | if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 301 | Keymaster keymaster; |
| 302 | if (!keymaster) return false; |
Paul Crowley | 320e5e1 | 2016-03-04 14:07:05 -0800 | [diff] [blame] | 303 | return decryptWithKeymasterKey(keymaster, kmKey, auth, appId, encryptedMessage, key); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 306 | static bool deleteKey(const std::string& dir) { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 307 | std::string kmKey; |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 308 | if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 309 | Keymaster keymaster; |
| 310 | if (!keymaster) return false; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 311 | if (!keymaster.deleteKey(kmKey)) return false; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 315 | static bool secdiscardSecdiscardable(const std::string& dir) { |
| 316 | if (ForkExecvp( |
| 317 | std::vector<std::string>{kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 318 | LOG(ERROR) << "secdiscard failed"; |
| 319 | return false; |
| 320 | } |
| 321 | return true; |
| 322 | } |
| 323 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 324 | static bool recursiveDeleteKey(const std::string& dir) { |
| 325 | if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 326 | LOG(ERROR) << "recursive delete failed"; |
| 327 | return false; |
| 328 | } |
| 329 | return true; |
| 330 | } |
| 331 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame^] | 332 | bool destroyKey(const std::string& dir) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 333 | bool success = true; |
| 334 | // Try each thing, even if previous things failed. |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 335 | success &= deleteKey(dir); |
| 336 | success &= secdiscardSecdiscardable(dir); |
| 337 | success &= recursiveDeleteKey(dir); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 338 | return success; |
| 339 | } |
| 340 | |
| 341 | } // namespace vold |
| 342 | } // namespace android |