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