blob: fd6e662158190d35bc7e53a8af3a75b0239a8e8f [file] [log] [blame]
Paul Crowleyf71ace32016-06-02 11:01:19 -07001/*
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 Biggersba997ee2018-10-23 13:07:43 -070019#include <linux/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070020#include <iomanip>
21#include <sstream>
22#include <string>
23
Paul Crowleyf71ace32016-06-02 11:01:19 -070024#include <openssl/sha.h>
25
26#include <android-base/file.h>
27#include <android-base/logging.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070028#include <keyutils.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070029
Scott Lobdell314dbc72018-12-05 16:01:16 -080030#include "FsCrypt.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070031#include "KeyStorage.h"
32#include "Utils.h"
33
Shivaprasad Hongal92292622018-07-05 14:49:12 -070034#define MAX_USER_ID 0xFFFFFFFF
35
36using android::hardware::keymaster::V4_0::KeyFormat;
37using android::vold::KeyType;
Paul Crowleyf71ace32016-06-02 11:01:19 -070038namespace android {
39namespace vold {
40
Eric Biggersa701c452018-10-23 13:06:55 -070041constexpr int FS_AES_256_XTS_KEY_SIZE = 64;
Pavel Grafove2e2d302017-08-01 17:15:53 +010042
43bool randomKey(KeyBuffer* key) {
Eric Biggersa701c452018-10-23 13:06:55 -070044 *key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE);
Pavel Grafove2e2d302017-08-01 17:15:53 +010045 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070046 // TODO status_t plays badly with PLOG, fix it.
47 LOG(ERROR) << "Random read failed";
48 return false;
49 }
50 return true;
51}
52
53// Get raw keyref - used to make keyname and to pass to ioctl
Eric Biggersba997ee2018-10-23 13:07:43 -070054static std::string generateKeyRef(const uint8_t* key, int length) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070055 SHA512_CTX c;
56
57 SHA512_Init(&c);
58 SHA512_Update(&c, key, length);
59 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
60 SHA512_Final(key_ref1, &c);
61
62 SHA512_Init(&c);
63 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
64 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
65 SHA512_Final(key_ref2, &c);
66
Eric Biggersa701c452018-10-23 13:06:55 -070067 static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor");
68 return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE);
Paul Crowleyf71ace32016-06-02 11:01:19 -070069}
70
Eric Biggersa701c452018-10-23 13:06:55 -070071static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
72 if (key.size() != FS_AES_256_XTS_KEY_SIZE) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070073 LOG(ERROR) << "Wrong size key " << key.size();
74 return false;
75 }
Eric Biggersa701c452018-10-23 13:06:55 -070076 static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!");
77 fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS;
78 fs_key->size = key.size();
79 memset(fs_key->raw, 0, sizeof(fs_key->raw));
80 memcpy(fs_key->raw, key.data(), key.size());
Paul Crowleyf71ace32016-06-02 11:01:19 -070081 return true;
82}
83
Paul Crowley14c8c072018-09-18 13:30:21 -070084static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -070085
86static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070087 std::ostringstream o;
Paul Crowleycd8bfe32017-06-19 16:05:55 -070088 o << prefix << ":";
Chen, Luhai5744dfe2017-08-18 14:49:45 +080089 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070090 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
91 }
92 return o.str();
93}
94
95// Get the keyring we store all keys in
Eric Biggersa701c452018-10-23 13:06:55 -070096static bool fscryptKeyring(key_serial_t* device_keyring) {
97 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -070098 if (*device_keyring == -1) {
99 PLOG(ERROR) << "Unable to find device keyring";
100 return false;
101 }
102 return true;
103}
104
105// Install password into global keyring
106// Return raw key reference for use in policy
Pavel Grafove2e2d302017-08-01 17:15:53 +0100107bool installKey(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggersa701c452018-10-23 13:06:55 -0700108 // Place fscrypt_key into automatically zeroing buffer.
109 KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
110 fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100111
Eric Biggersa701c452018-10-23 13:06:55 -0700112 if (!fillKey(key, &fs_key)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700113 if (is_wrapped_key_supported()) {
114 /* When wrapped key is supported, only the first 32 bytes are
115 the same per boot. The second 32 bytes can change as the ephemeral
116 key is different. */
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800117 *raw_ref = generateKeyRef(fs_key.raw, (fs_key.size)/2);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700118 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800119 *raw_ref = generateKeyRef(fs_key.raw, fs_key.size);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700120 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700121 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700122 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700123 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
124 auto ref = keyname(*name_prefix, *raw_ref);
125 key_serial_t key_id =
Eric Biggersa701c452018-10-23 13:06:55 -0700126 add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700127 if (key_id == -1) {
128 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
129 return false;
130 }
131 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
132 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700133 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700134 return true;
135}
136
137bool evictKey(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700138 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700139 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700140 bool success = true;
141 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
142 auto ref = keyname(*name_prefix, raw_ref);
143 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700144
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700145 // Unlink the key from the keyring. Prefer unlinking to revoking or
146 // invalidating, since unlinking is actually no less secure currently, and
147 // it avoids bugs in certain kernel versions where the keyring key is
148 // referenced from places it shouldn't be.
149 if (keyctl_unlink(key_serial, device_keyring) != 0) {
150 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
151 success = false;
152 } else {
153 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
154 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700155 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700156 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700157}
158
Paul Crowley26a53882017-10-26 11:16:39 -0700159bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
160 const std::string& key_path, const std::string& tmp_path,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700161 std::string* key_ref, bool wrapped_key_supported) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100162 KeyBuffer key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700163 if (pathExists(key_path)) {
164 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley26a53882017-10-26 11:16:39 -0700165 if (!retrieveKey(key_path, key_authentication, &key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700166 } else {
167 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 LOG(ERROR) << "No key found in " << key_path;
169 return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700170 }
171 LOG(INFO) << "Creating new key in " << key_path;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700172 if (wrapped_key_supported) {
173 if(!generateWrappedKey(MAX_USER_ID, KeyType::DE_SYS, &key)) return false;
174 } else {
175 if (!randomKey(&key)) return false;
176 }
Paul Crowley26a53882017-10-26 11:16:39 -0700177 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700178 }
179
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700180 if (wrapped_key_supported) {
181 KeyBuffer ephemeral_wrapped_key;
182 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
183 LOG(ERROR) << "Failed to export key in retrieveAndInstallKey";
184 return false;
185 }
186 key = std::move(ephemeral_wrapped_key);
187 }
188
Paul Crowleyf71ace32016-06-02 11:01:19 -0700189 if (!installKey(key, key_ref)) {
190 LOG(ERROR) << "Failed to install key in " << key_path;
191 return false;
192 }
193 return true;
194}
195
Paul Crowley14c8c072018-09-18 13:30:21 -0700196bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800197 KeyBuffer* key, bool keepOld) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700198 if (pathExists(key_path)) {
199 LOG(DEBUG) << "Key exists, using: " << key_path;
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800200 if (!retrieveKey(key_path, kEmptyAuthentication, key, keepOld)) return false;
Neeraj Sonic480f912018-12-14 15:18:15 +0530201 if (is_metadata_wrapped_key_supported()) {
202 KeyBuffer ephemeral_wrapped_key;
203 if (!getEphemeralWrappedKey(KeyFormat::RAW, *key, &ephemeral_wrapped_key)) {
204 LOG(ERROR) << "Failed to export key for retrieved key";
205 return false;
206 }
207 *key = std::move(ephemeral_wrapped_key);
208 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700209 } else {
210 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700211 LOG(ERROR) << "No key found in " << key_path;
212 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700213 }
214 LOG(INFO) << "Creating new key in " << key_path;
Neeraj Sonic480f912018-12-14 15:18:15 +0530215 if (is_metadata_wrapped_key_supported()) {
216 if(!generateWrappedKey(MAX_USER_ID, KeyType::ME, key)) return false;
217 } else {
218 if (!randomKey(key)) return false;
219 }
220 if (!storeKeyAtomically(key_path, tmp_path,
221 kEmptyAuthentication, *key)) return false;
222 if (is_metadata_wrapped_key_supported()) {
223 KeyBuffer ephemeral_wrapped_key;
224 if (!getEphemeralWrappedKey(KeyFormat::RAW, *key, &ephemeral_wrapped_key)) {
225 LOG(ERROR) << "Failed to export key for generated key";
226 return false;
227 }
228 *key = std::move(ephemeral_wrapped_key);
229 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700230 }
231 return true;
232}
233
Paul Crowleyf71ace32016-06-02 11:01:19 -0700234} // namespace vold
235} // namespace android