Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #define LOG_TAG "keystore" |
| 18 | |
| 19 | #include <arpa/inet.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <string.h> |
| 23 | |
Logan Chien | cdc813f | 2018-04-23 13:52:28 +0800 | [diff] [blame] | 24 | #include <log/log.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 25 | |
| 26 | #include "blob.h" |
| 27 | #include "entropy.h" |
| 28 | |
| 29 | #include "keystore_utils.h" |
| 30 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 31 | #include <openssl/evp.h> |
| 32 | |
| 33 | #include <istream> |
| 34 | #include <ostream> |
| 35 | #include <streambuf> |
| 36 | #include <string> |
| 37 | |
| 38 | #include <android-base/logging.h> |
| 39 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | constexpr size_t kGcmIvSizeBytes = 96 / 8; |
| 43 | |
| 44 | template <typename T, void (*FreeFunc)(T*)> struct OpenSslObjectDeleter { |
| 45 | void operator()(T* p) { FreeFunc(p); } |
| 46 | }; |
| 47 | |
| 48 | #define DEFINE_OPENSSL_OBJECT_POINTER(name) \ |
| 49 | typedef OpenSslObjectDeleter<name, name##_free> name##_Delete; \ |
| 50 | typedef std::unique_ptr<name, name##_Delete> name##_Ptr; |
| 51 | |
| 52 | DEFINE_OPENSSL_OBJECT_POINTER(EVP_CIPHER_CTX); |
| 53 | |
Shawn Willden | 0ed642c | 2017-05-26 10:13:28 -0600 | [diff] [blame] | 54 | #if defined(__clang__) |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 55 | #define OPTNONE __attribute__((optnone)) |
Shawn Willden | 0ed642c | 2017-05-26 10:13:28 -0600 | [diff] [blame] | 56 | #elif defined(__GNUC__) |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 57 | #define OPTNONE __attribute__((optimize("O0"))) |
Shawn Willden | 0ed642c | 2017-05-26 10:13:28 -0600 | [diff] [blame] | 58 | #else |
| 59 | #error Need a definition for OPTNONE |
| 60 | #endif |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 61 | |
| 62 | class ArrayEraser { |
| 63 | public: |
| 64 | ArrayEraser(uint8_t* arr, size_t size) : mArr(arr), mSize(size) {} |
| 65 | OPTNONE ~ArrayEraser() { std::fill(mArr, mArr + mSize, 0); } |
| 66 | |
| 67 | private: |
Shawn Willden | 0ed642c | 2017-05-26 10:13:28 -0600 | [diff] [blame] | 68 | volatile uint8_t* mArr; |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 69 | size_t mSize; |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * Encrypt 'len' data at 'in' with AES-GCM, using 128-bit key at 'key', 96-bit IV at 'iv' and write |
| 74 | * output to 'out' (which may be the same location as 'in') and 128-bit tag to 'tag'. |
| 75 | */ |
| 76 | ResponseCode AES_gcm_encrypt(const uint8_t* in, uint8_t* out, size_t len, const uint8_t* key, |
| 77 | const uint8_t* iv, uint8_t* tag) { |
| 78 | const EVP_CIPHER* cipher = EVP_aes_128_gcm(); |
| 79 | EVP_CIPHER_CTX_Ptr ctx(EVP_CIPHER_CTX_new()); |
| 80 | |
| 81 | EVP_EncryptInit_ex(ctx.get(), cipher, nullptr /* engine */, key, iv); |
| 82 | EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding needed with GCM */); |
| 83 | |
| 84 | std::unique_ptr<uint8_t[]> out_tmp(new uint8_t[len]); |
| 85 | uint8_t* out_pos = out_tmp.get(); |
| 86 | int out_len; |
| 87 | |
| 88 | EVP_EncryptUpdate(ctx.get(), out_pos, &out_len, in, len); |
| 89 | out_pos += out_len; |
| 90 | EVP_EncryptFinal_ex(ctx.get(), out_pos, &out_len); |
| 91 | out_pos += out_len; |
| 92 | if (out_pos - out_tmp.get() != static_cast<ssize_t>(len)) { |
| 93 | ALOGD("Encrypted ciphertext is the wrong size, expected %zu, got %zd", len, |
| 94 | out_pos - out_tmp.get()); |
| 95 | return ResponseCode::SYSTEM_ERROR; |
| 96 | } |
| 97 | |
| 98 | std::copy(out_tmp.get(), out_pos, out); |
| 99 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, kGcmTagLength, tag); |
| 100 | |
| 101 | return ResponseCode::NO_ERROR; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Decrypt 'len' data at 'in' with AES-GCM, using 128-bit key at 'key', 96-bit IV at 'iv', checking |
| 106 | * 128-bit tag at 'tag' and writing plaintext to 'out' (which may be the same location as 'in'). |
| 107 | */ |
| 108 | ResponseCode AES_gcm_decrypt(const uint8_t* in, uint8_t* out, size_t len, const uint8_t* key, |
| 109 | const uint8_t* iv, const uint8_t* tag) { |
| 110 | const EVP_CIPHER* cipher = EVP_aes_128_gcm(); |
| 111 | EVP_CIPHER_CTX_Ptr ctx(EVP_CIPHER_CTX_new()); |
| 112 | |
| 113 | EVP_DecryptInit_ex(ctx.get(), cipher, nullptr /* engine */, key, iv); |
| 114 | EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding needed with GCM */); |
| 115 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, kGcmTagLength, const_cast<uint8_t*>(tag)); |
| 116 | |
| 117 | std::unique_ptr<uint8_t[]> out_tmp(new uint8_t[len]); |
| 118 | ArrayEraser out_eraser(out_tmp.get(), len); |
| 119 | uint8_t* out_pos = out_tmp.get(); |
| 120 | int out_len; |
| 121 | |
| 122 | EVP_DecryptUpdate(ctx.get(), out_pos, &out_len, in, len); |
| 123 | out_pos += out_len; |
| 124 | if (!EVP_DecryptFinal_ex(ctx.get(), out_pos, &out_len)) { |
| 125 | ALOGD("Failed to decrypt blob; ciphertext or tag is likely corrupted"); |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 126 | return ResponseCode::VALUE_CORRUPTED; |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 127 | } |
| 128 | out_pos += out_len; |
| 129 | if (out_pos - out_tmp.get() != static_cast<ssize_t>(len)) { |
| 130 | ALOGD("Encrypted plaintext is the wrong size, expected %zu, got %zd", len, |
| 131 | out_pos - out_tmp.get()); |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 132 | return ResponseCode::VALUE_CORRUPTED; |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | std::copy(out_tmp.get(), out_pos, out); |
| 136 | |
| 137 | return ResponseCode::NO_ERROR; |
| 138 | } |
| 139 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 140 | class ArrayStreamBuffer : public std::streambuf { |
| 141 | public: |
| 142 | template <typename T, size_t size> ArrayStreamBuffer(const T (&data)[size]) { |
| 143 | static_assert(sizeof(T) == 1, "Array element size too large"); |
| 144 | std::streambuf::char_type* d = const_cast<std::streambuf::char_type*>( |
| 145 | reinterpret_cast<const std::streambuf::char_type*>(&data[0])); |
| 146 | setg(d, d, d + size); |
| 147 | setp(d, d + size); |
| 148 | } |
| 149 | |
| 150 | protected: |
| 151 | pos_type seekoff(off_type off, std::ios_base::seekdir dir, |
| 152 | std::ios_base::openmode which = std::ios_base::in | |
| 153 | std::ios_base::out) override { |
| 154 | bool in = which & std::ios_base::in; |
| 155 | bool out = which & std::ios_base::out; |
| 156 | if ((!in && !out) || (in && out && dir == std::ios_base::cur)) return -1; |
| 157 | std::streambuf::char_type* newPosPtr; |
| 158 | switch (dir) { |
| 159 | case std::ios_base::beg: |
| 160 | newPosPtr = pbase(); |
| 161 | break; |
| 162 | case std::ios_base::cur: |
| 163 | // if dir == cur then in xor out due to |
| 164 | // if ((!in && !out) || (in && out && dir == std::ios_base::cur)) return -1; above |
| 165 | if (in) |
| 166 | newPosPtr = gptr(); |
| 167 | else |
| 168 | newPosPtr = pptr(); |
| 169 | break; |
| 170 | case std::ios_base::end: |
| 171 | // in and out bounds are the same and cannot change, so we can take either range |
| 172 | // regardless of the value of "which" |
| 173 | newPosPtr = epptr(); |
| 174 | break; |
| 175 | } |
| 176 | newPosPtr += off; |
| 177 | if (newPosPtr < pbase() || newPosPtr > epptr()) return -1; |
| 178 | if (in) { |
| 179 | gbump(newPosPtr - gptr()); |
| 180 | } |
| 181 | if (out) { |
| 182 | pbump(newPosPtr - pptr()); |
| 183 | } |
| 184 | return newPosPtr - pbase(); |
| 185 | } |
| 186 | }; |
| 187 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 188 | } // namespace |
| 189 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 190 | Blob::Blob(const uint8_t* value, size_t valueLength, const uint8_t* info, uint8_t infoLength, |
| 191 | BlobType type) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 192 | mBlob = std::make_unique<blobv3>(); |
| 193 | memset(mBlob.get(), 0, sizeof(blobv3)); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 194 | if (valueLength > kValueSize) { |
| 195 | valueLength = kValueSize; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 196 | ALOGW("Provided blob length too large"); |
| 197 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 198 | if (infoLength + valueLength > kValueSize) { |
| 199 | infoLength = kValueSize - valueLength; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 200 | ALOGW("Provided info length too large"); |
| 201 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 202 | mBlob->length = valueLength; |
| 203 | memcpy(mBlob->value, value, valueLength); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 204 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 205 | mBlob->info = infoLength; |
| 206 | memcpy(mBlob->value + valueLength, info, infoLength); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 207 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 208 | mBlob->version = CURRENT_BLOB_VERSION; |
| 209 | mBlob->type = uint8_t(type); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 210 | |
| 211 | if (type == TYPE_MASTER_KEY) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 212 | mBlob->flags = KEYSTORE_FLAG_ENCRYPTED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 213 | } else { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 214 | mBlob->flags = KEYSTORE_FLAG_NONE; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 218 | Blob::Blob(blobv3 b) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 219 | mBlob = std::make_unique<blobv3>(b); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | Blob::Blob() { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 223 | if (mBlob) *mBlob = {}; |
| 224 | } |
| 225 | |
| 226 | Blob::Blob(const Blob& rhs) { |
| 227 | if (rhs.mBlob) { |
| 228 | mBlob = std::make_unique<blobv3>(*rhs.mBlob); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | Blob::Blob(Blob&& rhs) : mBlob(std::move(rhs.mBlob)) {} |
| 233 | |
| 234 | Blob& Blob::operator=(const Blob& rhs) { |
| 235 | if (&rhs != this) { |
| 236 | if (mBlob) *mBlob = {}; |
| 237 | if (rhs) { |
| 238 | mBlob = std::make_unique<blobv3>(*rhs.mBlob); |
| 239 | } else { |
| 240 | mBlob = {}; |
| 241 | } |
| 242 | } |
| 243 | return *this; |
| 244 | } |
| 245 | |
| 246 | Blob& Blob::operator=(Blob&& rhs) { |
| 247 | if (mBlob) *mBlob = {}; |
| 248 | mBlob = std::move(rhs.mBlob); |
| 249 | return *this; |
| 250 | } |
| 251 | |
| 252 | template <typename BlobType> static bool rawBlobIsEncrypted(const BlobType& blob) { |
| 253 | if (blob.version < 2) return true; |
| 254 | |
| 255 | return blob.flags & (KEYSTORE_FLAG_ENCRYPTED | KEYSTORE_FLAG_SUPER_ENCRYPTED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | bool Blob::isEncrypted() const { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 259 | if (mBlob->version < 2) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 263 | return mBlob->flags & KEYSTORE_FLAG_ENCRYPTED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 266 | bool Blob::isSuperEncrypted() const { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 267 | return mBlob->flags & KEYSTORE_FLAG_SUPER_ENCRYPTED; |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 270 | bool Blob::isCriticalToDeviceEncryption() const { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 271 | return mBlob->flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION; |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 274 | inline uint8_t setFlag(uint8_t flags, bool set, KeyStoreFlag flag) { |
| 275 | return set ? (flags | flag) : (flags & ~flag); |
| 276 | } |
| 277 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 278 | void Blob::setEncrypted(bool encrypted) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 279 | mBlob->flags = setFlag(mBlob->flags, encrypted, KEYSTORE_FLAG_ENCRYPTED); |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 282 | void Blob::setSuperEncrypted(bool superEncrypted) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 283 | mBlob->flags = setFlag(mBlob->flags, superEncrypted, KEYSTORE_FLAG_SUPER_ENCRYPTED); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 284 | } |
Rubin Xu | 4847795 | 2017-04-19 14:16:57 +0100 | [diff] [blame] | 285 | |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 286 | void Blob::setCriticalToDeviceEncryption(bool critical) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 287 | mBlob->flags = setFlag(mBlob->flags, critical, KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void Blob::setFallback(bool fallback) { |
| 291 | if (fallback) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 292 | mBlob->flags |= KEYSTORE_FLAG_FALLBACK; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 293 | } else { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 294 | mBlob->flags &= ~KEYSTORE_FLAG_FALLBACK; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 298 | static ResponseCode writeBlob(const std::string& filename, Blob blob, blobv3* rawBlob, |
| 299 | const uint8_t* aes_key, State state, Entropy* entropy) { |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 300 | ALOGV("writing blob %s", filename.c_str()); |
| 301 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 302 | const size_t dataLength = rawBlob->length; |
| 303 | rawBlob->length = htonl(rawBlob->length); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 304 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 305 | if (blob.isEncrypted() || blob.isSuperEncrypted()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 306 | if (state != STATE_NO_ERROR) { |
| 307 | ALOGD("couldn't insert encrypted blob while not unlocked"); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 308 | return ResponseCode::LOCKED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 311 | memset(rawBlob->initialization_vector, 0, AES_BLOCK_SIZE); |
| 312 | if (!entropy->generate_random_data(rawBlob->initialization_vector, kGcmIvSizeBytes)) { |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 313 | ALOGW("Could not read random data for: %s", filename.c_str()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 314 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 315 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 316 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 317 | auto rc = AES_gcm_encrypt(rawBlob->value /* in */, rawBlob->value /* out */, dataLength, |
| 318 | aes_key, rawBlob->initialization_vector, rawBlob->aead_tag); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 319 | if (rc != ResponseCode::NO_ERROR) return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 322 | size_t fileLength = offsetof(blobv3, value) + dataLength + rawBlob->info; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 323 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 324 | int out = |
| 325 | TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 326 | if (out < 0) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 327 | ALOGW("could not open file: %s: %s", filename.c_str(), strerror(errno)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 328 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 329 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 330 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 331 | const size_t writtenBytes = writeFully(out, reinterpret_cast<uint8_t*>(rawBlob), fileLength); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 332 | if (close(out) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 333 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 334 | } |
| 335 | if (writtenBytes != fileLength) { |
| 336 | ALOGW("blob not fully written %zu != %zu", writtenBytes, fileLength); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 337 | unlink(filename.c_str()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 338 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 339 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 340 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 343 | ResponseCode LockedKeyBlobEntry::writeBlobs(Blob keyBlob, Blob characteristicsBlob, |
| 344 | const uint8_t* aes_key, State state, |
| 345 | Entropy* entropy) const { |
| 346 | if (entry_ == nullptr) { |
| 347 | return ResponseCode::SYSTEM_ERROR; |
| 348 | } |
| 349 | ResponseCode rc; |
| 350 | if (keyBlob) { |
| 351 | blobv3* rawBlob = keyBlob.mBlob.get(); |
| 352 | rc = writeBlob(entry_->getKeyBlobPath(), std::move(keyBlob), rawBlob, aes_key, state, |
| 353 | entropy); |
| 354 | if (rc != ResponseCode::NO_ERROR) { |
| 355 | return rc; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (characteristicsBlob) { |
| 360 | blobv3* rawBlob = characteristicsBlob.mBlob.get(); |
| 361 | rc = writeBlob(entry_->getCharacteristicsBlobPath(), std::move(characteristicsBlob), |
| 362 | rawBlob, aes_key, state, entropy); |
| 363 | } |
| 364 | return rc; |
| 365 | } |
| 366 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 367 | ResponseCode Blob::readBlob(const std::string& filename, const uint8_t* aes_key, State state) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 368 | ResponseCode rc; |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 369 | ALOGV("reading blob %s", filename.c_str()); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 370 | std::unique_ptr<blobv3> rawBlob = std::make_unique<blobv3>(); |
| 371 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 372 | const int in = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 373 | if (in < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 374 | return (errno == ENOENT) ? ResponseCode::KEY_NOT_FOUND : ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 375 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 376 | |
| 377 | // fileLength may be less than sizeof(mBlob) |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 378 | const size_t fileLength = readFully(in, (uint8_t*)rawBlob.get(), sizeof(blobv3)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 379 | if (close(in) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 380 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if (fileLength == 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 384 | return ResponseCode::VALUE_CORRUPTED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 387 | if (rawBlobIsEncrypted(*rawBlob)) { |
| 388 | if (state == STATE_LOCKED) { |
| 389 | mBlob = std::move(rawBlob); |
| 390 | return ResponseCode::LOCKED; |
| 391 | } |
Janis Danisevskis | 36316d6 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 392 | if (state == STATE_UNINITIALIZED) return ResponseCode::UNINITIALIZED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 395 | if (fileLength < offsetof(blobv3, value)) return ResponseCode::VALUE_CORRUPTED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 396 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 397 | if (rawBlob->version == 3) { |
| 398 | const ssize_t encryptedLength = ntohl(rawBlob->length); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 399 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 400 | if (rawBlobIsEncrypted(*rawBlob)) { |
| 401 | rc = AES_gcm_decrypt(rawBlob->value /* in */, rawBlob->value /* out */, encryptedLength, |
| 402 | aes_key, rawBlob->initialization_vector, rawBlob->aead_tag); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 403 | if (rc != ResponseCode::NO_ERROR) return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 404 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 405 | } else if (rawBlob->version < 3) { |
| 406 | blobv2& v2blob = reinterpret_cast<blobv2&>(*rawBlob); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 407 | const size_t headerLength = offsetof(blobv2, encrypted); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 408 | const ssize_t encryptedLength = fileLength - headerLength - v2blob.info; |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 409 | if (encryptedLength < 0) return ResponseCode::VALUE_CORRUPTED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 410 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 411 | if (rawBlobIsEncrypted(*rawBlob)) { |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 412 | if (encryptedLength % AES_BLOCK_SIZE != 0) { |
| 413 | return ResponseCode::VALUE_CORRUPTED; |
| 414 | } |
| 415 | |
| 416 | AES_KEY key; |
| 417 | AES_set_decrypt_key(aes_key, kAesKeySize * 8, &key); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 418 | AES_cbc_encrypt(v2blob.encrypted, v2blob.encrypted, encryptedLength, &key, |
| 419 | v2blob.vector, AES_DECRYPT); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 420 | key = {}; // clear key |
| 421 | |
| 422 | uint8_t computedDigest[MD5_DIGEST_LENGTH]; |
| 423 | ssize_t digestedLength = encryptedLength - MD5_DIGEST_LENGTH; |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 424 | MD5(v2blob.digested, digestedLength, computedDigest); |
| 425 | if (memcmp(v2blob.digest, computedDigest, MD5_DIGEST_LENGTH) != 0) { |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 426 | return ResponseCode::VALUE_CORRUPTED; |
| 427 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 428 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 431 | const ssize_t maxValueLength = fileLength - offsetof(blobv3, value) - rawBlob->info; |
| 432 | rawBlob->length = ntohl(rawBlob->length); |
| 433 | if (rawBlob->length < 0 || rawBlob->length > maxValueLength || |
| 434 | rawBlob->length + rawBlob->info + AES_BLOCK_SIZE > |
| 435 | static_cast<ssize_t>(sizeof(rawBlob->value))) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 436 | return ResponseCode::VALUE_CORRUPTED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 437 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 438 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 439 | if (rawBlob->info != 0 && rawBlob->version < 3) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 440 | // move info from after padding to after data |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 441 | memmove(rawBlob->value + rawBlob->length, rawBlob->value + maxValueLength, rawBlob->info); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 442 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 443 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 444 | mBlob = std::move(rawBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 445 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 446 | } |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 447 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 448 | std::tuple<ResponseCode, Blob, Blob> LockedKeyBlobEntry::readBlobs(const uint8_t* aes_key, |
| 449 | State state) const { |
| 450 | std::tuple<ResponseCode, Blob, Blob> result; |
| 451 | auto& [rc, keyBlob, characteristicsBlob] = result; |
| 452 | if (entry_ == nullptr) return rc = ResponseCode::SYSTEM_ERROR, result; |
| 453 | |
| 454 | rc = keyBlob.readBlob(entry_->getKeyBlobPath(), aes_key, state); |
| 455 | if (rc != ResponseCode::NO_ERROR && rc != ResponseCode::UNINITIALIZED) { |
| 456 | return result; |
| 457 | } |
| 458 | |
| 459 | if (entry_->hasCharacteristicsBlob()) { |
| 460 | characteristicsBlob.readBlob(entry_->getCharacteristicsBlobPath(), aes_key, state); |
| 461 | } |
| 462 | return result; |
| 463 | } |
| 464 | |
| 465 | ResponseCode LockedKeyBlobEntry::deleteBlobs() const { |
| 466 | if (entry_ == nullptr) return ResponseCode::NO_ERROR; |
| 467 | |
| 468 | // always try to delete both |
| 469 | ResponseCode rc1 = (unlink(entry_->getKeyBlobPath().c_str()) && errno != ENOENT) |
| 470 | ? ResponseCode::SYSTEM_ERROR |
| 471 | : ResponseCode::NO_ERROR; |
| 472 | if (rc1 != ResponseCode::NO_ERROR) { |
| 473 | ALOGW("Failed to delete key blob file \"%s\"", entry_->getKeyBlobPath().c_str()); |
| 474 | } |
| 475 | ResponseCode rc2 = (unlink(entry_->getCharacteristicsBlobPath().c_str()) && errno != ENOENT) |
| 476 | ? ResponseCode::SYSTEM_ERROR |
| 477 | : ResponseCode::NO_ERROR; |
| 478 | if (rc2 != ResponseCode::NO_ERROR) { |
| 479 | ALOGW("Failed to delete key characteristics file \"%s\"", |
| 480 | entry_->getCharacteristicsBlobPath().c_str()); |
| 481 | } |
| 482 | // then report the first error that occured |
| 483 | if (rc1 != ResponseCode::NO_ERROR) return rc1; |
| 484 | return rc2; |
| 485 | } |
| 486 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 487 | keystore::SecurityLevel Blob::getSecurityLevel() const { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 488 | return keystore::flagsToSecurityLevel(mBlob->flags); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void Blob::setSecurityLevel(keystore::SecurityLevel secLevel) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 492 | mBlob->flags &= ~(KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX); |
| 493 | mBlob->flags |= keystore::securityLevelToFlags(secLevel); |
| 494 | } |
| 495 | |
| 496 | std::tuple<bool, keystore::AuthorizationSet, keystore::AuthorizationSet> |
| 497 | Blob::getKeyCharacteristics() const { |
| 498 | std::tuple<bool, keystore::AuthorizationSet, keystore::AuthorizationSet> result; |
| 499 | auto& [success, hwEnforced, swEnforced] = result; |
| 500 | success = false; |
| 501 | ArrayStreamBuffer buf(mBlob->value); |
| 502 | std::istream in(&buf); |
| 503 | |
| 504 | // only the characteristics cache has both sets |
| 505 | if (getType() == TYPE_KEY_CHARACTERISTICS_CACHE) { |
| 506 | hwEnforced.Deserialize(&in); |
| 507 | } else if (getType() != TYPE_KEY_CHARACTERISTICS) { |
| 508 | // if its not the cache and not the legacy characteristics file we have no business |
| 509 | // here |
| 510 | return result; |
| 511 | } |
| 512 | swEnforced.Deserialize(&in); |
| 513 | success = !in.bad(); |
| 514 | |
| 515 | return result; |
| 516 | } |
| 517 | bool Blob::putKeyCharacteristics(const keystore::AuthorizationSet& hwEnforced, |
| 518 | const keystore::AuthorizationSet& swEnforced) { |
| 519 | if (!mBlob) mBlob = std::make_unique<blobv3>(); |
| 520 | mBlob->version = CURRENT_BLOB_VERSION; |
| 521 | ArrayStreamBuffer buf(mBlob->value); |
| 522 | std::ostream out(&buf); |
| 523 | hwEnforced.Serialize(&out); |
| 524 | swEnforced.Serialize(&out); |
| 525 | if (out.bad()) return false; |
| 526 | setType(TYPE_KEY_CHARACTERISTICS_CACHE); |
| 527 | mBlob->length = out.tellp(); |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | void LockedKeyBlobEntry::put(const KeyBlobEntry& entry) { |
| 532 | std::unique_lock<std::mutex> lock(locked_blobs_mutex_); |
| 533 | locked_blobs_.erase(entry); |
| 534 | lock.unlock(); |
| 535 | locked_blobs_mutex_cond_var_.notify_all(); |
| 536 | } |
| 537 | |
| 538 | LockedKeyBlobEntry::~LockedKeyBlobEntry() { |
| 539 | if (entry_ != nullptr) put(*entry_); |
| 540 | } |
| 541 | |
| 542 | LockedKeyBlobEntry LockedKeyBlobEntry::get(KeyBlobEntry entry) { |
| 543 | std::unique_lock<std::mutex> lock(locked_blobs_mutex_); |
| 544 | locked_blobs_mutex_cond_var_.wait( |
| 545 | lock, [&] { return locked_blobs_.find(entry) == locked_blobs_.end(); }); |
| 546 | auto [iterator, success] = locked_blobs_.insert(std::move(entry)); |
| 547 | if (!success) return {}; |
| 548 | return LockedKeyBlobEntry(*iterator); |
| 549 | } |
| 550 | |
| 551 | std::set<KeyBlobEntry> LockedKeyBlobEntry::locked_blobs_; |
| 552 | std::mutex LockedKeyBlobEntry::locked_blobs_mutex_; |
| 553 | std::condition_variable LockedKeyBlobEntry::locked_blobs_mutex_cond_var_; |
| 554 | |
| 555 | /* Here is the encoding of key names. This is necessary in order to allow arbitrary |
| 556 | * characters in key names. Characters in [0-~] are not encoded. Others are encoded |
| 557 | * into two bytes. The first byte is one of [+-.] which represents the first |
| 558 | * two bits of the character. The second byte encodes the rest of the bits into |
| 559 | * [0-o]. Therefore in the worst case the length of a key gets doubled. Note |
| 560 | * that Base64 cannot be used here due to the need of prefix match on keys. */ |
| 561 | |
| 562 | static std::string encodeKeyName(const std::string& keyName) { |
| 563 | std::string encodedName; |
| 564 | encodedName.reserve(keyName.size() * 2); |
| 565 | auto in = keyName.begin(); |
| 566 | while (in != keyName.end()) { |
| 567 | if (*in < '0' || *in > '~') { |
| 568 | encodedName.append(1, '+' + (uint8_t(*in) >> 6)); |
| 569 | encodedName.append(1, '0' + (*in & 0x3F)); |
| 570 | } else { |
| 571 | encodedName.append(1, *in); |
| 572 | } |
| 573 | ++in; |
| 574 | } |
| 575 | return encodedName; |
| 576 | } |
| 577 | |
| 578 | static std::string decodeKeyName(const std::string& encodedName) { |
| 579 | std::string decodedName; |
| 580 | decodedName.reserve(encodedName.size()); |
| 581 | auto in = encodedName.begin(); |
| 582 | bool multichar = false; |
| 583 | char c; |
| 584 | while (in != encodedName.end()) { |
| 585 | if (multichar) { |
| 586 | multichar = false; |
| 587 | decodedName.append(1, c | *in); |
| 588 | } else if (*in >= '+' && *in <= '.') { |
| 589 | multichar = true; |
| 590 | c = (*in - '+') << 6; |
| 591 | } else { |
| 592 | decodedName.append(1, *in); |
| 593 | } |
| 594 | ++in; |
| 595 | } |
| 596 | // mulitchars at the end get truncated |
| 597 | return decodedName; |
| 598 | } |
| 599 | |
| 600 | std::string KeyBlobEntry::getKeyBlobBaseName() const { |
| 601 | std::stringstream s; |
| 602 | if (masterkey_) { |
| 603 | s << alias_; |
| 604 | } else { |
| 605 | s << uid_ << "_" << encodeKeyName(alias_); |
| 606 | } |
| 607 | return s.str(); |
| 608 | } |
| 609 | |
| 610 | std::string KeyBlobEntry::getKeyBlobPath() const { |
| 611 | std::stringstream s; |
| 612 | if (masterkey_) { |
| 613 | s << user_dir_ << "/" << alias_; |
| 614 | } else { |
| 615 | s << user_dir_ << "/" << uid_ << "_" << encodeKeyName(alias_); |
| 616 | } |
| 617 | return s.str(); |
| 618 | } |
| 619 | |
| 620 | std::string KeyBlobEntry::getCharacteristicsBlobBaseName() const { |
| 621 | std::stringstream s; |
| 622 | if (!masterkey_) s << "." << uid_ << "_chr_" << encodeKeyName(alias_); |
| 623 | return s.str(); |
| 624 | } |
| 625 | |
| 626 | std::string KeyBlobEntry::getCharacteristicsBlobPath() const { |
| 627 | std::stringstream s; |
| 628 | if (!masterkey_) |
| 629 | s << user_dir_ << "/" |
| 630 | << "." << uid_ << "_chr_" << encodeKeyName(alias_); |
| 631 | return s.str(); |
| 632 | } |
| 633 | |
| 634 | bool KeyBlobEntry::hasKeyBlob() const { |
| 635 | return !access(getKeyBlobPath().c_str(), R_OK | W_OK); |
| 636 | } |
| 637 | bool KeyBlobEntry::hasCharacteristicsBlob() const { |
| 638 | return !access(getCharacteristicsBlobPath().c_str(), R_OK | W_OK); |
| 639 | } |
| 640 | |
| 641 | static std::tuple<bool, uid_t, std::string> filename2UidAlias(const std::string& filepath) { |
| 642 | std::tuple<bool, uid_t, std::string> result; |
| 643 | |
| 644 | auto& [success, uid, alias] = result; |
| 645 | |
| 646 | success = false; |
| 647 | |
| 648 | auto filenamebase = filepath.find_last_of('/'); |
| 649 | std::string filename = |
| 650 | filenamebase == std::string::npos ? filepath : filepath.substr(filenamebase + 1); |
| 651 | |
| 652 | if (filename[0] == '.') return result; |
| 653 | |
| 654 | auto sep = filename.find('_'); |
| 655 | if (sep == std::string::npos) return result; |
| 656 | |
| 657 | std::stringstream s(filename.substr(0, sep)); |
| 658 | s >> uid; |
| 659 | if (!s) return result; |
| 660 | |
| 661 | alias = decodeKeyName(filename.substr(sep + 1)); |
| 662 | success = true; |
| 663 | return result; |
| 664 | } |
| 665 | |
| 666 | std::tuple<ResponseCode, std::list<LockedKeyBlobEntry>> |
| 667 | LockedKeyBlobEntry::list(const std::string& user_dir, |
| 668 | std::function<bool(uid_t, const std::string&)> filter) { |
| 669 | std::list<LockedKeyBlobEntry> matches; |
| 670 | |
| 671 | // This is a fence against any concurrent database accesses during database iteration. |
| 672 | // Only the keystore thread can lock entries. So it cannot be starved |
| 673 | // by workers grabbing new individual locks. We just wait here until all |
| 674 | // workers have relinquished their locked files. |
| 675 | std::unique_lock<std::mutex> lock(locked_blobs_mutex_); |
| 676 | locked_blobs_mutex_cond_var_.wait(lock, [&] { return locked_blobs_.empty(); }); |
| 677 | |
| 678 | DIR* dir = opendir(user_dir.c_str()); |
| 679 | if (!dir) { |
| 680 | ALOGW("can't open directory for user: %s", strerror(errno)); |
| 681 | return std::tuple<ResponseCode, std::list<LockedKeyBlobEntry>&&>{ResponseCode::SYSTEM_ERROR, |
| 682 | std::move(matches)}; |
| 683 | } |
| 684 | |
| 685 | struct dirent* file; |
| 686 | while ((file = readdir(dir)) != nullptr) { |
| 687 | // We only care about files. |
| 688 | if (file->d_type != DT_REG) { |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | // Skip anything that starts with a "." |
| 693 | if (file->d_name[0] == '.') { |
| 694 | continue; |
| 695 | } |
| 696 | |
| 697 | auto [success, uid, alias] = filename2UidAlias(file->d_name); |
| 698 | |
| 699 | if (!success) { |
| 700 | ALOGW("could not parse key filename \"%s\"", file->d_name); |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | if (!filter(uid, alias)) continue; |
| 705 | |
| 706 | auto [iterator, dummy] = locked_blobs_.emplace(alias, user_dir, uid); |
| 707 | matches.push_back(*iterator); |
| 708 | } |
| 709 | closedir(dir); |
| 710 | return std::tuple<ResponseCode, std::list<LockedKeyBlobEntry>&&>{ResponseCode::NO_ERROR, |
| 711 | std::move(matches)}; |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 712 | } |