Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [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 "KeyUtil.h" |
| 18 | |
Eric Biggers | ba997ee | 2018-10-23 13:07:43 -0700 | [diff] [blame] | 19 | #include <linux/fs.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 20 | #include <iomanip> |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 24 | #include <openssl/sha.h> |
| 25 | |
| 26 | #include <android-base/file.h> |
| 27 | #include <android-base/logging.h> |
Elliott Hughes | c3bda18 | 2017-05-09 17:01:04 -0700 | [diff] [blame] | 28 | #include <keyutils.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 29 | |
| 30 | #include "KeyStorage.h" |
| 31 | #include "Utils.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace vold { |
| 35 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 36 | constexpr int FS_AES_256_XTS_KEY_SIZE = 64; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 37 | |
| 38 | bool randomKey(KeyBuffer* key) { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 39 | *key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 40 | if (ReadRandomBytes(key->size(), key->data()) != 0) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 41 | // TODO status_t plays badly with PLOG, fix it. |
| 42 | LOG(ERROR) << "Random read failed"; |
| 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | // Get raw keyref - used to make keyname and to pass to ioctl |
Eric Biggers | ba997ee | 2018-10-23 13:07:43 -0700 | [diff] [blame] | 49 | static std::string generateKeyRef(const uint8_t* key, int length) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 50 | SHA512_CTX c; |
| 51 | |
| 52 | SHA512_Init(&c); |
| 53 | SHA512_Update(&c, key, length); |
| 54 | unsigned char key_ref1[SHA512_DIGEST_LENGTH]; |
| 55 | SHA512_Final(key_ref1, &c); |
| 56 | |
| 57 | SHA512_Init(&c); |
| 58 | SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH); |
| 59 | unsigned char key_ref2[SHA512_DIGEST_LENGTH]; |
| 60 | SHA512_Final(key_ref2, &c); |
| 61 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 62 | static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor"); |
| 63 | return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 66 | static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) { |
| 67 | if (key.size() != FS_AES_256_XTS_KEY_SIZE) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 68 | LOG(ERROR) << "Wrong size key " << key.size(); |
| 69 | return false; |
| 70 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 71 | static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!"); |
| 72 | fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 73 | fs_key->size = key.size(); |
| 74 | memset(fs_key->raw, 0, sizeof(fs_key->raw)); |
| 75 | memcpy(fs_key->raw, key.data(), key.size()); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 79 | static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr}; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 80 | |
| 81 | static std::string keyname(const std::string& prefix, const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 82 | std::ostringstream o; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 83 | o << prefix << ":"; |
Chen, Luhai | 5744dfe | 2017-08-18 14:49:45 +0800 | [diff] [blame] | 84 | for (unsigned char i : raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 85 | o << std::hex << std::setw(2) << std::setfill('0') << (int)i; |
| 86 | } |
| 87 | return o.str(); |
| 88 | } |
| 89 | |
| 90 | // Get the keyring we store all keys in |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 91 | static bool fscryptKeyring(key_serial_t* device_keyring) { |
| 92 | *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 93 | if (*device_keyring == -1) { |
| 94 | PLOG(ERROR) << "Unable to find device keyring"; |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // Install password into global keyring |
| 101 | // Return raw key reference for use in policy |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 102 | bool installKey(const KeyBuffer& key, std::string* raw_ref) { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 103 | // Place fscrypt_key into automatically zeroing buffer. |
| 104 | KeyBuffer fsKeyBuffer(sizeof(fscrypt_key)); |
| 105 | fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data()); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 106 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 107 | if (!fillKey(key, &fs_key)) return false; |
| 108 | *raw_ref = generateKeyRef(fs_key.raw, fs_key.size); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 109 | key_serial_t device_keyring; |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 110 | if (!fscryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 111 | for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) { |
| 112 | auto ref = keyname(*name_prefix, *raw_ref); |
| 113 | key_serial_t key_id = |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 114 | add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring); |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 115 | if (key_id == -1) { |
| 116 | PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring; |
| 117 | return false; |
| 118 | } |
| 119 | LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring |
| 120 | << " in process " << getpid(); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 121 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool evictKey(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 126 | key_serial_t device_keyring; |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 127 | if (!fscryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 128 | bool success = true; |
| 129 | for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) { |
| 130 | auto ref = keyname(*name_prefix, raw_ref); |
| 131 | auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 132 | |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 133 | // Unlink the key from the keyring. Prefer unlinking to revoking or |
| 134 | // invalidating, since unlinking is actually no less secure currently, and |
| 135 | // it avoids bugs in certain kernel versions where the keyring key is |
| 136 | // referenced from places it shouldn't be. |
| 137 | if (keyctl_unlink(key_serial, device_keyring) != 0) { |
| 138 | PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref; |
| 139 | success = false; |
| 140 | } else { |
| 141 | LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref; |
| 142 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 143 | } |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 144 | return success; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 147 | bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication, |
| 148 | const std::string& key_path, const std::string& tmp_path, |
| 149 | std::string* key_ref) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 150 | KeyBuffer key; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 151 | if (pathExists(key_path)) { |
| 152 | LOG(DEBUG) << "Key exists, using: " << key_path; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 153 | if (!retrieveKey(key_path, key_authentication, &key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 154 | } else { |
| 155 | if (!create_if_absent) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 156 | LOG(ERROR) << "No key found in " << key_path; |
| 157 | return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 158 | } |
| 159 | LOG(INFO) << "Creating new key in " << key_path; |
| 160 | if (!randomKey(&key)) return false; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 161 | if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (!installKey(key, key_ref)) { |
| 165 | LOG(ERROR) << "Failed to install key in " << key_path; |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 171 | bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path, |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 172 | KeyBuffer* key, bool keepOld) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 173 | if (pathExists(key_path)) { |
| 174 | LOG(DEBUG) << "Key exists, using: " << key_path; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 175 | if (!retrieveKey(key_path, kEmptyAuthentication, key, keepOld)) return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 176 | } else { |
| 177 | if (!create_if_absent) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 178 | LOG(ERROR) << "No key found in " << key_path; |
| 179 | return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 180 | } |
| 181 | LOG(INFO) << "Creating new key in " << key_path; |
| 182 | if (!randomKey(key)) return false; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 183 | if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 188 | } // namespace vold |
| 189 | } // namespace android |