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 | |
| 19 | #include <iomanip> |
| 20 | #include <sstream> |
| 21 | #include <string> |
| 22 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 23 | #include <openssl/sha.h> |
| 24 | |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
Elliott Hughes | c3bda18 | 2017-05-09 17:01:04 -0700 | [diff] [blame] | 27 | #include <keyutils.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 28 | |
| 29 | #include "KeyStorage.h" |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 30 | #include "Ext4Crypt.h" |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 31 | #include "Utils.h" |
| 32 | |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 33 | #define MAX_USER_ID 0xFFFFFFFF |
| 34 | |
| 35 | using android::hardware::keymaster::V4_0::KeyFormat; |
| 36 | using android::vold::KeyType; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 40 | // ext4enc:TODO get this const from somewhere good |
| 41 | const int EXT4_KEY_DESCRIPTOR_SIZE = 8; |
| 42 | |
| 43 | // ext4enc:TODO Include structure from somewhere sensible |
| 44 | // MUST be in sync with ext4_crypto.c in kernel |
| 45 | constexpr int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1; |
| 46 | constexpr int EXT4_AES_256_XTS_KEY_SIZE = 64; |
| 47 | constexpr int EXT4_MAX_KEY_SIZE = 64; |
| 48 | struct ext4_encryption_key { |
| 49 | uint32_t mode; |
| 50 | char raw[EXT4_MAX_KEY_SIZE]; |
| 51 | uint32_t size; |
| 52 | }; |
| 53 | |
| 54 | bool randomKey(KeyBuffer* key) { |
| 55 | *key = KeyBuffer(EXT4_AES_256_XTS_KEY_SIZE); |
| 56 | if (ReadRandomBytes(key->size(), key->data()) != 0) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 57 | // TODO status_t plays badly with PLOG, fix it. |
| 58 | LOG(ERROR) << "Random read failed"; |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | // Get raw keyref - used to make keyname and to pass to ioctl |
| 65 | static std::string generateKeyRef(const char* key, int length) { |
| 66 | SHA512_CTX c; |
| 67 | |
| 68 | SHA512_Init(&c); |
| 69 | SHA512_Update(&c, key, length); |
| 70 | unsigned char key_ref1[SHA512_DIGEST_LENGTH]; |
| 71 | SHA512_Final(key_ref1, &c); |
| 72 | |
| 73 | SHA512_Init(&c); |
| 74 | SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH); |
| 75 | unsigned char key_ref2[SHA512_DIGEST_LENGTH]; |
| 76 | SHA512_Final(key_ref2, &c); |
| 77 | |
| 78 | static_assert(EXT4_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, |
| 79 | "Hash too short for descriptor"); |
| 80 | return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE); |
| 81 | } |
| 82 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 83 | static bool fillKey(const KeyBuffer& key, ext4_encryption_key* ext4_key) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 84 | if (key.size() != EXT4_AES_256_XTS_KEY_SIZE) { |
| 85 | LOG(ERROR) << "Wrong size key " << key.size(); |
| 86 | return false; |
| 87 | } |
| 88 | static_assert(EXT4_AES_256_XTS_KEY_SIZE <= sizeof(ext4_key->raw), "Key too long!"); |
| 89 | ext4_key->mode = EXT4_ENCRYPTION_MODE_AES_256_XTS; |
| 90 | ext4_key->size = key.size(); |
| 91 | memset(ext4_key->raw, 0, sizeof(ext4_key->raw)); |
| 92 | memcpy(ext4_key->raw, key.data(), key.size()); |
| 93 | return true; |
| 94 | } |
| 95 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 96 | static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr}; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 97 | |
| 98 | 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] | 99 | std::ostringstream o; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 100 | o << prefix << ":"; |
Chen, Luhai | 5744dfe | 2017-08-18 14:49:45 +0800 | [diff] [blame] | 101 | for (unsigned char i : raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 102 | o << std::hex << std::setw(2) << std::setfill('0') << (int)i; |
| 103 | } |
| 104 | return o.str(); |
| 105 | } |
| 106 | |
| 107 | // Get the keyring we store all keys in |
| 108 | static bool e4cryptKeyring(key_serial_t* device_keyring) { |
| 109 | *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0); |
| 110 | if (*device_keyring == -1) { |
| 111 | PLOG(ERROR) << "Unable to find device keyring"; |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | // Install password into global keyring |
| 118 | // Return raw key reference for use in policy |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 119 | bool installKey(const KeyBuffer& key, std::string* raw_ref) { |
| 120 | // Place ext4_encryption_key into automatically zeroing buffer. |
| 121 | KeyBuffer ext4KeyBuffer(sizeof(ext4_encryption_key)); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 122 | ext4_encryption_key& ext4_key = *reinterpret_cast<ext4_encryption_key*>(ext4KeyBuffer.data()); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 123 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 124 | if (!fillKey(key, &ext4_key)) return false; |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 125 | if (is_wrapped_key_supported()) { |
| 126 | /* When wrapped key is supported, only the first 32 bytes are |
| 127 | the same per boot. The second 32 bytes can change as the ephemeral |
| 128 | key is different. */ |
| 129 | *raw_ref = generateKeyRef(ext4_key.raw, (ext4_key.size)/2); |
| 130 | } else { |
| 131 | *raw_ref = generateKeyRef(ext4_key.raw, ext4_key.size); |
| 132 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 133 | key_serial_t device_keyring; |
| 134 | if (!e4cryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 135 | for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) { |
| 136 | auto ref = keyname(*name_prefix, *raw_ref); |
| 137 | key_serial_t key_id = |
| 138 | add_key("logon", ref.c_str(), (void*)&ext4_key, sizeof(ext4_key), device_keyring); |
| 139 | if (key_id == -1) { |
| 140 | PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring; |
| 141 | return false; |
| 142 | } |
| 143 | LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring |
| 144 | << " in process " << getpid(); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 145 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | bool evictKey(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 150 | key_serial_t device_keyring; |
| 151 | if (!e4cryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 152 | bool success = true; |
| 153 | for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) { |
| 154 | auto ref = keyname(*name_prefix, raw_ref); |
| 155 | auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 156 | |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 157 | // Unlink the key from the keyring. Prefer unlinking to revoking or |
| 158 | // invalidating, since unlinking is actually no less secure currently, and |
| 159 | // it avoids bugs in certain kernel versions where the keyring key is |
| 160 | // referenced from places it shouldn't be. |
| 161 | if (keyctl_unlink(key_serial, device_keyring) != 0) { |
| 162 | PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref; |
| 163 | success = false; |
| 164 | } else { |
| 165 | LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref; |
| 166 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 167 | } |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 168 | return success; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 171 | bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication, |
| 172 | const std::string& key_path, const std::string& tmp_path, |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 173 | std::string* key_ref, bool wrapped_key_supported) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 174 | KeyBuffer key; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 175 | if (pathExists(key_path)) { |
| 176 | LOG(DEBUG) << "Key exists, using: " << key_path; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 177 | if (!retrieveKey(key_path, key_authentication, &key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 178 | } else { |
| 179 | if (!create_if_absent) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 180 | LOG(ERROR) << "No key found in " << key_path; |
| 181 | return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 182 | } |
| 183 | LOG(INFO) << "Creating new key in " << key_path; |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 184 | if (wrapped_key_supported) { |
| 185 | if(!generateWrappedKey(MAX_USER_ID, KeyType::DE_SYS, &key)) return false; |
| 186 | } else { |
| 187 | if (!randomKey(&key)) return false; |
| 188 | } |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 189 | if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 192 | if (wrapped_key_supported) { |
| 193 | KeyBuffer ephemeral_wrapped_key; |
| 194 | if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) { |
| 195 | LOG(ERROR) << "Failed to export key in retrieveAndInstallKey"; |
| 196 | return false; |
| 197 | } |
| 198 | key = std::move(ephemeral_wrapped_key); |
| 199 | } |
| 200 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 201 | if (!installKey(key, key_ref)) { |
| 202 | LOG(ERROR) << "Failed to install key in " << key_path; |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 208 | bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path, |
| 209 | KeyBuffer* key) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 210 | if (pathExists(key_path)) { |
| 211 | LOG(DEBUG) << "Key exists, using: " << key_path; |
| 212 | if (!retrieveKey(key_path, kEmptyAuthentication, key)) return false; |
| 213 | } else { |
| 214 | if (!create_if_absent) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 215 | LOG(ERROR) << "No key found in " << key_path; |
| 216 | return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 217 | } |
| 218 | LOG(INFO) << "Creating new key in " << key_path; |
| 219 | if (!randomKey(key)) return false; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 220 | if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 225 | } // namespace vold |
| 226 | } // namespace android |