blob: acc42db830eea4dd2649a58ecbba9be3552bde40 [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
19#include <iomanip>
20#include <sstream>
21#include <string>
22
Eric Biggersf3dc4202019-09-30 13:05:58 -070023#include <fcntl.h>
Eric Biggers3e9c9962019-12-16 15:55:12 -080024#include <linux/fscrypt.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070025#include <openssl/sha.h>
Eric Biggersf3dc4202019-09-30 13:05:58 -070026#include <sys/ioctl.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070027
28#include <android-base/file.h>
29#include <android-base/logging.h>
Nikita Ioffe78f80612020-04-20 22:21:49 +010030#include <android-base/properties.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070031#include <keyutils.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070032
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080033#include <fscrypt_uapi.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070034#include "KeyStorage.h"
35#include "Utils.h"
36
37namespace android {
38namespace vold {
39
Paul Crowley4eac2642020-02-12 11:04:05 -080040const KeyGeneration neverGen() {
41 return KeyGeneration{0, false, false};
42}
43
44static bool randomKey(size_t size, KeyBuffer* key) {
45 *key = KeyBuffer(size);
Pavel Grafove2e2d302017-08-01 17:15:53 +010046 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070047 // TODO status_t plays badly with PLOG, fix it.
48 LOG(ERROR) << "Random read failed";
49 return false;
50 }
51 return true;
52}
53
Paul Crowley4eac2642020-02-12 11:04:05 -080054bool generateStorageKey(const KeyGeneration& gen, KeyBuffer* key) {
55 if (!gen.allow_gen) return false;
56 if (gen.use_hw_wrapped_key) {
57 if (gen.keysize != FSCRYPT_MAX_KEY_SIZE) {
58 LOG(ERROR) << "Cannot generate a wrapped key " << gen.keysize << " bytes long";
59 return false;
60 }
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080061 return generateWrappedStorageKey(key);
Paul Crowley4eac2642020-02-12 11:04:05 -080062 } else {
63 return randomKey(gen.keysize, key);
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080064 }
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080065}
66
Eric Biggersf3dc4202019-09-30 13:05:58 -070067// Return true if the kernel supports the ioctls to add/remove fscrypt keys
68// directly to/from the filesystem.
69bool isFsKeyringSupported(void) {
70 static bool initialized = false;
71 static bool supported;
72
73 if (!initialized) {
74 android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
75
76 // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY
77 // if the ioctl isn't supported. Otherwise it will fail with another
78 // error code such as EFAULT.
79 errno = 0;
80 (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
81 if (errno == ENOTTY) {
82 LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to "
83 "session keyring";
84 supported = false;
85 } else {
86 if (errno != EFAULT) {
87 PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY";
88 }
89 LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY";
90 supported = true;
Nikita Ioffe78f80612020-04-20 22:21:49 +010091 android::base::SetProperty("ro.crypto.uses_fs_ioc_add_encryption_key", "true");
Eric Biggersf3dc4202019-09-30 13:05:58 -070092 }
93 // There's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, since it's
94 // guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is. There's
95 // also no need to check for support on external volumes separately from
96 // /data, since either the kernel supports the ioctls on all
97 // fscrypt-capable filesystems or it doesn't.
98
99 initialized = true;
100 }
101 return supported;
102}
103
Paul Crowleyf71ace32016-06-02 11:01:19 -0700104// Get raw keyref - used to make keyname and to pass to ioctl
Eric Biggersba997ee2018-10-23 13:07:43 -0700105static std::string generateKeyRef(const uint8_t* key, int length) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700106 SHA512_CTX c;
107
108 SHA512_Init(&c);
109 SHA512_Update(&c, key, length);
110 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
111 SHA512_Final(key_ref1, &c);
112
113 SHA512_Init(&c);
114 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
115 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
116 SHA512_Final(key_ref2, &c);
117
Eric Biggers506342f2019-12-17 13:11:25 -0800118 static_assert(FSCRYPT_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
119 "Hash too short for descriptor");
120 return std::string((char*)key_ref2, FSCRYPT_KEY_DESCRIPTOR_SIZE);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700121}
122
Eric Biggersa701c452018-10-23 13:06:55 -0700123static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
Eric Biggers506342f2019-12-17 13:11:25 -0800124 if (key.size() != FSCRYPT_MAX_KEY_SIZE) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700125 LOG(ERROR) << "Wrong size key " << key.size();
126 return false;
127 }
Eric Biggers506342f2019-12-17 13:11:25 -0800128 static_assert(FSCRYPT_MAX_KEY_SIZE == sizeof(fs_key->raw), "Mismatch of max key sizes");
129 fs_key->mode = 0; // unused by kernel
Eric Biggersa701c452018-10-23 13:06:55 -0700130 memcpy(fs_key->raw, key.data(), key.size());
Eric Biggers506342f2019-12-17 13:11:25 -0800131 fs_key->size = key.size();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700132 return true;
133}
134
Paul Crowley14c8c072018-09-18 13:30:21 -0700135static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700136
Eric Biggersf3dc4202019-09-30 13:05:58 -0700137static std::string keyrefstring(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700138 std::ostringstream o;
Chen, Luhai5744dfe2017-08-18 14:49:45 +0800139 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700140 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
141 }
142 return o.str();
143}
144
Eric Biggersf3dc4202019-09-30 13:05:58 -0700145static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) {
146 return prefix + ":" + keyrefstring(raw_ref);
147}
148
149// Get the ID of the keyring we store all fscrypt keys in when the kernel is too
150// old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY.
Eric Biggersa701c452018-10-23 13:06:55 -0700151static bool fscryptKeyring(key_serial_t* device_keyring) {
152 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700153 if (*device_keyring == -1) {
154 PLOG(ERROR) << "Unable to find device keyring";
155 return false;
156 }
157 return true;
158}
159
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000160// Add an encryption key of type "logon" to the global session keyring.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700161static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
Eric Biggersa701c452018-10-23 13:06:55 -0700162 // Place fscrypt_key into automatically zeroing buffer.
163 KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
164 fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100165
Eric Biggersa701c452018-10-23 13:06:55 -0700166 if (!fillKey(key, &fs_key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700167 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700168 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700169 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700170 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700171 key_serial_t key_id =
Eric Biggersa701c452018-10-23 13:06:55 -0700172 add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700173 if (key_id == -1) {
174 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
175 return false;
176 }
177 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
178 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700179 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700180 return true;
181}
182
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000183// Installs fscrypt-provisioning key into session level kernel keyring.
184// This allows for the given key to be installed back into filesystem keyring.
185// For more context see reloadKeyFromSessionKeyring.
186static bool installProvisioningKey(const KeyBuffer& key, const std::string& ref,
187 const fscrypt_key_specifier& key_spec) {
188 key_serial_t device_keyring;
189 if (!fscryptKeyring(&device_keyring)) return false;
190
191 // Place fscrypt_provisioning_key_payload into automatically zeroing buffer.
192 KeyBuffer buf(sizeof(fscrypt_provisioning_key_payload) + key.size(), 0);
193 fscrypt_provisioning_key_payload& provisioning_key =
194 *reinterpret_cast<fscrypt_provisioning_key_payload*>(buf.data());
195 memcpy(provisioning_key.raw, key.data(), key.size());
196 provisioning_key.type = key_spec.type;
197
198 key_serial_t key_id = add_key("fscrypt-provisioning", ref.c_str(), (void*)&provisioning_key,
199 buf.size(), device_keyring);
200 if (key_id == -1) {
201 PLOG(ERROR) << "Failed to insert fscrypt-provisioning key for " << ref
202 << " into session keyring";
203 return false;
204 }
205 LOG(DEBUG) << "Added fscrypt-provisioning key for " << ref << " to session keyring";
206 return true;
207}
208
Eric Biggers83a73d72019-09-30 13:06:47 -0700209// Build a struct fscrypt_key_specifier for use in the key management ioctls.
Paul Crowley77df7f22020-01-23 15:29:30 -0800210static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
211 switch (policy.options.version) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700212 case 1:
Paul Crowley77df7f22020-01-23 15:29:30 -0800213 if (policy.key_raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700214 LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
Paul Crowley77df7f22020-01-23 15:29:30 -0800215 << policy.key_raw_ref.size();
Eric Biggers83a73d72019-09-30 13:06:47 -0700216 return false;
217 }
218 spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
Paul Crowley77df7f22020-01-23 15:29:30 -0800219 memcpy(spec->u.descriptor, policy.key_raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700220 return true;
221 case 2:
Paul Crowley77df7f22020-01-23 15:29:30 -0800222 if (policy.key_raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700223 LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
Paul Crowley77df7f22020-01-23 15:29:30 -0800224 << policy.key_raw_ref.size();
Eric Biggers83a73d72019-09-30 13:06:47 -0700225 return false;
226 }
227 spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
Paul Crowley77df7f22020-01-23 15:29:30 -0800228 memcpy(spec->u.identifier, policy.key_raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700229 return true;
230 default:
Paul Crowley77df7f22020-01-23 15:29:30 -0800231 LOG(ERROR) << "Invalid encryption policy version: " << policy.options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700232 return false;
233 }
234}
235
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000236// Installs key into keyring of a filesystem mounted on |mountpoint|.
237//
238// It's callers responsibility to fill key specifier, and either arg->raw or arg->key_id.
239//
240// In case arg->key_spec.type equals to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
241// arg->key_spec.u.identifier will be populated with raw key reference generated
242// by kernel.
243//
244// For documentation on difference between arg->raw and arg->key_id see
245// https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#fs-ioc-add-encryption-key
246static bool installFsKeyringKey(const std::string& mountpoint, const EncryptionOptions& options,
247 fscrypt_add_key_arg* arg) {
248 if (options.use_hw_wrapped_key) arg->flags |= FSCRYPT_ADD_KEY_FLAG_WRAPPED;
249
250 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
251 if (fd == -1) {
252 PLOG(ERROR) << "Failed to open " << mountpoint << " to install key";
253 return false;
254 }
255
256 if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) {
257 PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint;
258 return false;
259 }
260
261 return true;
262}
263
Paul Crowley77df7f22020-01-23 15:29:30 -0800264bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
265 const KeyBuffer& key, EncryptionPolicy* policy) {
266 policy->options = options;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700267 // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we
268 // have to copy the raw key into it.
269 KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0);
270 struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data();
271
Eric Biggers83a73d72019-09-30 13:06:47 -0700272 // Initialize the "key specifier", which is like a name for the key.
Paul Crowley77df7f22020-01-23 15:29:30 -0800273 switch (options.version) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700274 case 1:
275 // A key for a v1 policy is specified by an arbitrary 8-byte
276 // "descriptor", which must be provided by userspace. We use the
277 // first 8 bytes from the double SHA-512 of the key itself.
Paul Crowley77df7f22020-01-23 15:29:30 -0800278 policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
Eric Biggers83a73d72019-09-30 13:06:47 -0700279 if (!isFsKeyringSupported()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800280 return installKeyLegacy(key, policy->key_raw_ref);
Eric Biggers83a73d72019-09-30 13:06:47 -0700281 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800282 if (!buildKeySpecifier(&arg->key_spec, *policy)) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700283 return false;
284 }
285 break;
286 case 2:
287 // A key for a v2 policy is specified by an 16-byte "identifier",
288 // which is a cryptographic hash of the key itself which the kernel
289 // computes and returns. Any user-provided value is ignored; we
290 // just need to set the specifier type to indicate that we're adding
291 // this type of key.
292 arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
293 break;
294 default:
Paul Crowley77df7f22020-01-23 15:29:30 -0800295 LOG(ERROR) << "Invalid encryption policy version: " << options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700296 return false;
297 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700298
299 arg->raw_size = key.size();
300 memcpy(arg->raw, key.data(), key.size());
301
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000302 if (!installFsKeyringKey(mountpoint, options, arg)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700303
Eric Biggers83a73d72019-09-30 13:06:47 -0700304 if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
305 // Retrieve the key identifier that the kernel computed.
Paul Crowley77df7f22020-01-23 15:29:30 -0800306 policy->key_raw_ref =
307 std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700308 }
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000309 std::string ref = keyrefstring(policy->key_raw_ref);
310 LOG(DEBUG) << "Installed fscrypt key with ref " << ref << " to " << mountpoint;
311
312 if (!installProvisioningKey(key, ref, arg->key_spec)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700313 return true;
314}
315
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000316// Remove an encryption key of type "logon" from the global session keyring.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700317static bool evictKeyLegacy(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700318 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700319 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700320 bool success = true;
321 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700322 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700323 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700324
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700325 // Unlink the key from the keyring. Prefer unlinking to revoking or
326 // invalidating, since unlinking is actually no less secure currently, and
327 // it avoids bugs in certain kernel versions where the keyring key is
328 // referenced from places it shouldn't be.
329 if (keyctl_unlink(key_serial, device_keyring) != 0) {
330 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
331 success = false;
332 } else {
333 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
334 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700335 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700336 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700337}
338
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000339static bool evictProvisioningKey(const std::string& ref) {
340 key_serial_t device_keyring;
341 if (!fscryptKeyring(&device_keyring)) {
342 return false;
343 }
344
345 auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
346 if (key_serial == -1 && errno != ENOKEY) {
347 PLOG(ERROR) << "Error searching session keyring for fscrypt-provisioning key for " << ref;
348 return false;
349 }
350
351 if (key_serial != -1 && keyctl_unlink(key_serial, device_keyring) != 0) {
352 PLOG(ERROR) << "Failed to unlink fscrypt-provisioning key for " << ref
353 << " from session keyring";
354 return false;
355 }
356 return true;
357}
358
Paul Crowley77df7f22020-01-23 15:29:30 -0800359bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
360 if (policy.options.version == 1 && !isFsKeyringSupported()) {
361 return evictKeyLegacy(policy.key_raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700362 }
363
364 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
365 if (fd == -1) {
366 PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key";
367 return false;
368 }
369
370 struct fscrypt_remove_key_arg arg;
371 memset(&arg, 0, sizeof(arg));
372
Paul Crowley77df7f22020-01-23 15:29:30 -0800373 if (!buildKeySpecifier(&arg.key_spec, policy)) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700374 return false;
375 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700376
Paul Crowley77df7f22020-01-23 15:29:30 -0800377 std::string ref = keyrefstring(policy.key_raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700378
379 if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
380 PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
381 return false;
382 }
383
384 LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint;
385 if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
386 // Should never happen because keys are only added/removed as root.
387 LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!";
388 } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
389 LOG(ERROR) << "Files still open after removing key with ref " << ref
390 << ". These files were not locked!";
391 }
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000392
393 if (!evictProvisioningKey(ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700394 return true;
395}
396
Paul Crowley4eac2642020-02-12 11:04:05 -0800397bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
398 const KeyAuthentication& key_authentication, const KeyGeneration& gen,
399 KeyBuffer* key, bool keepOld) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700400 if (pathExists(key_path)) {
401 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley77df7f22020-01-23 15:29:30 -0800402 if (!retrieveKey(key_path, key_authentication, key, keepOld)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700403 } else {
Paul Crowley4eac2642020-02-12 11:04:05 -0800404 if (!gen.allow_gen) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700405 LOG(ERROR) << "No key found in " << key_path;
406 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700407 }
408 LOG(INFO) << "Creating new key in " << key_path;
Paul Crowley4eac2642020-02-12 11:04:05 -0800409 if (!generateStorageKey(gen, key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800410 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, *key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700411 }
412 return true;
413}
414
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000415bool reloadKeyFromSessionKeyring(const std::string& mountpoint, const EncryptionPolicy& policy) {
416 key_serial_t device_keyring;
417 if (!fscryptKeyring(&device_keyring)) {
418 return false;
419 }
420
421 std::string ref = keyrefstring(policy.key_raw_ref);
422 auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
423 if (key_serial == -1) {
424 PLOG(ERROR) << "Failed to find fscrypt-provisioning key for " << ref
425 << " in session keyring";
426 return false;
427 }
428
429 LOG(DEBUG) << "Installing fscrypt-provisioning key for " << ref << " back into " << mountpoint
430 << " fs-keyring";
431
432 struct fscrypt_add_key_arg arg;
433 memset(&arg, 0, sizeof(arg));
434 if (!buildKeySpecifier(&arg.key_spec, policy)) return false;
435 arg.key_id = key_serial;
436 if (!installFsKeyringKey(mountpoint, policy.options, &arg)) return false;
437
438 return true;
439}
440
Paul Crowleyf71ace32016-06-02 11:01:19 -0700441} // namespace vold
442} // namespace android