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" |
| 30 | #include "Utils.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace vold { |
| 34 | |
| 35 | bool randomKey(std::string* key) { |
| 36 | if (ReadRandomBytes(EXT4_AES_256_XTS_KEY_SIZE, *key) != 0) { |
| 37 | // TODO status_t plays badly with PLOG, fix it. |
| 38 | LOG(ERROR) << "Random read failed"; |
| 39 | return false; |
| 40 | } |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | // Get raw keyref - used to make keyname and to pass to ioctl |
| 45 | static std::string generateKeyRef(const char* key, int length) { |
| 46 | SHA512_CTX c; |
| 47 | |
| 48 | SHA512_Init(&c); |
| 49 | SHA512_Update(&c, key, length); |
| 50 | unsigned char key_ref1[SHA512_DIGEST_LENGTH]; |
| 51 | SHA512_Final(key_ref1, &c); |
| 52 | |
| 53 | SHA512_Init(&c); |
| 54 | SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH); |
| 55 | unsigned char key_ref2[SHA512_DIGEST_LENGTH]; |
| 56 | SHA512_Final(key_ref2, &c); |
| 57 | |
| 58 | static_assert(EXT4_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, |
| 59 | "Hash too short for descriptor"); |
| 60 | return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE); |
| 61 | } |
| 62 | |
| 63 | static bool fillKey(const std::string& key, ext4_encryption_key* ext4_key) { |
| 64 | if (key.size() != EXT4_AES_256_XTS_KEY_SIZE) { |
| 65 | LOG(ERROR) << "Wrong size key " << key.size(); |
| 66 | return false; |
| 67 | } |
| 68 | static_assert(EXT4_AES_256_XTS_KEY_SIZE <= sizeof(ext4_key->raw), "Key too long!"); |
| 69 | ext4_key->mode = EXT4_ENCRYPTION_MODE_AES_256_XTS; |
| 70 | ext4_key->size = key.size(); |
| 71 | memset(ext4_key->raw, 0, sizeof(ext4_key->raw)); |
| 72 | memcpy(ext4_key->raw, key.data(), key.size()); |
| 73 | return true; |
| 74 | } |
| 75 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 76 | std::string keyname(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 77 | std::ostringstream o; |
| 78 | o << "ext4:"; |
| 79 | for (auto i : raw_ref) { |
| 80 | o << std::hex << std::setw(2) << std::setfill('0') << (int)i; |
| 81 | } |
| 82 | return o.str(); |
| 83 | } |
| 84 | |
| 85 | // Get the keyring we store all keys in |
| 86 | static bool e4cryptKeyring(key_serial_t* device_keyring) { |
| 87 | *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0); |
| 88 | if (*device_keyring == -1) { |
| 89 | PLOG(ERROR) << "Unable to find device keyring"; |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | // Install password into global keyring |
| 96 | // Return raw key reference for use in policy |
| 97 | bool installKey(const std::string& key, std::string* raw_ref) { |
| 98 | ext4_encryption_key ext4_key; |
| 99 | if (!fillKey(key, &ext4_key)) return false; |
| 100 | *raw_ref = generateKeyRef(ext4_key.raw, ext4_key.size); |
| 101 | auto ref = keyname(*raw_ref); |
| 102 | key_serial_t device_keyring; |
| 103 | if (!e4cryptKeyring(&device_keyring)) return false; |
| 104 | key_serial_t key_id = |
| 105 | add_key("logon", ref.c_str(), (void*)&ext4_key, sizeof(ext4_key), device_keyring); |
| 106 | if (key_id == -1) { |
| 107 | PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring; |
| 108 | return false; |
| 109 | } |
| 110 | LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring |
| 111 | << " in process " << getpid(); |
| 112 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool evictKey(const std::string& raw_ref) { |
| 117 | auto ref = keyname(raw_ref); |
| 118 | key_serial_t device_keyring; |
| 119 | if (!e4cryptKeyring(&device_keyring)) return false; |
| 120 | auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0); |
| 121 | |
| 122 | // Unlink the key from the keyring. Prefer unlinking to revoking or |
| 123 | // invalidating, since unlinking is actually no less secure currently, and |
| 124 | // it avoids bugs in certain kernel versions where the keyring key is |
| 125 | // referenced from places it shouldn't be. |
| 126 | if (keyctl_unlink(key_serial, device_keyring) != 0) { |
| 127 | PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref; |
| 128 | return false; |
| 129 | } |
| 130 | LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref; |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool retrieveAndInstallKey(bool create_if_absent, const std::string& key_path, |
| 135 | const std::string& tmp_path, std::string* key_ref) { |
| 136 | std::string key; |
| 137 | if (pathExists(key_path)) { |
| 138 | LOG(DEBUG) << "Key exists, using: " << key_path; |
| 139 | if (!retrieveKey(key_path, kEmptyAuthentication, &key)) return false; |
| 140 | } else { |
| 141 | if (!create_if_absent) { |
| 142 | LOG(ERROR) << "No key found in " << key_path; |
| 143 | return false; |
| 144 | } |
| 145 | LOG(INFO) << "Creating new key in " << key_path; |
| 146 | if (!randomKey(&key)) return false; |
| 147 | if (!storeKeyAtomically(key_path, tmp_path, |
| 148 | kEmptyAuthentication, key)) return false; |
| 149 | } |
| 150 | |
| 151 | if (!installKey(key, key_ref)) { |
| 152 | LOG(ERROR) << "Failed to install key in " << key_path; |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 158 | bool retrieveKey(bool create_if_absent, const std::string& key_path, |
| 159 | const std::string& tmp_path, std::string* key) { |
| 160 | if (pathExists(key_path)) { |
| 161 | LOG(DEBUG) << "Key exists, using: " << key_path; |
| 162 | if (!retrieveKey(key_path, kEmptyAuthentication, key)) return false; |
| 163 | } else { |
| 164 | if (!create_if_absent) { |
| 165 | LOG(ERROR) << "No key found in " << key_path; |
| 166 | return false; |
| 167 | } |
| 168 | LOG(INFO) << "Creating new key in " << key_path; |
| 169 | if (!randomKey(key)) return false; |
| 170 | if (!storeKeyAtomically(key_path, tmp_path, |
| 171 | kEmptyAuthentication, *key)) return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 176 | } // namespace vold |
| 177 | } // namespace android |