blob: 3058d726de6916fa85bebcfa626fb7a3041f2cf6 [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>
24#include <linux/fs.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>
Elliott Hughesc3bda182017-05-09 17:01:04 -070030#include <keyutils.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070031
Scott Lobdell314dbc72018-12-05 16:01:16 -080032#include "FsCrypt.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070033#include "KeyStorage.h"
34#include "Utils.h"
Eric Biggersf3dc4202019-09-30 13:05:58 -070035#include "fscrypt_uapi.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070036
Shivaprasad Hongal92292622018-07-05 14:49:12 -070037#define MAX_USER_ID 0xFFFFFFFF
38
39using android::hardware::keymaster::V4_0::KeyFormat;
40using android::vold::KeyType;
Paul Crowleyf71ace32016-06-02 11:01:19 -070041namespace android {
42namespace vold {
43
Eric Biggersa701c452018-10-23 13:06:55 -070044constexpr int FS_AES_256_XTS_KEY_SIZE = 64;
Pavel Grafove2e2d302017-08-01 17:15:53 +010045
46bool randomKey(KeyBuffer* key) {
Eric Biggersa701c452018-10-23 13:06:55 -070047 *key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE);
Pavel Grafove2e2d302017-08-01 17:15:53 +010048 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070049 // TODO status_t plays badly with PLOG, fix it.
50 LOG(ERROR) << "Random read failed";
51 return false;
52 }
53 return true;
54}
55
Eric Biggersf3dc4202019-09-30 13:05:58 -070056// Return true if the kernel supports the ioctls to add/remove fscrypt keys
57// directly to/from the filesystem.
58bool isFsKeyringSupported(void) {
59 static bool initialized = false;
60 static bool supported;
61
62 if (!initialized) {
63 android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
64
65 // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY
66 // if the ioctl isn't supported. Otherwise it will fail with another
67 // error code such as EFAULT.
68 errno = 0;
69 (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
70 if (errno == ENOTTY) {
71 LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to "
72 "session keyring";
73 supported = false;
74 } else {
75 if (errno != EFAULT) {
76 PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY";
77 }
78 LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY";
79 supported = true;
80 }
81 // There's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, since it's
82 // guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is. There's
83 // also no need to check for support on external volumes separately from
84 // /data, since either the kernel supports the ioctls on all
85 // fscrypt-capable filesystems or it doesn't.
86
87 initialized = true;
88 }
89 return supported;
90}
91
Paul Crowleyf71ace32016-06-02 11:01:19 -070092// Get raw keyref - used to make keyname and to pass to ioctl
Eric Biggersba997ee2018-10-23 13:07:43 -070093static std::string generateKeyRef(const uint8_t* key, int length) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070094 SHA512_CTX c;
95
96 SHA512_Init(&c);
97 SHA512_Update(&c, key, length);
98 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
99 SHA512_Final(key_ref1, &c);
100
101 SHA512_Init(&c);
102 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
103 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
104 SHA512_Final(key_ref2, &c);
105
Eric Biggersa701c452018-10-23 13:06:55 -0700106 static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor");
107 return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700108}
109
Eric Biggersa701c452018-10-23 13:06:55 -0700110static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
111 if (key.size() != FS_AES_256_XTS_KEY_SIZE) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700112 LOG(ERROR) << "Wrong size key " << key.size();
113 return false;
114 }
Eric Biggersa701c452018-10-23 13:06:55 -0700115 static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!");
116 fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS;
117 fs_key->size = key.size();
118 memset(fs_key->raw, 0, sizeof(fs_key->raw));
119 memcpy(fs_key->raw, key.data(), key.size());
Paul Crowleyf71ace32016-06-02 11:01:19 -0700120 return true;
121}
122
Paul Crowley14c8c072018-09-18 13:30:21 -0700123static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700124
Eric Biggersf3dc4202019-09-30 13:05:58 -0700125static std::string keyrefstring(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700126 std::ostringstream o;
Chen, Luhai5744dfe2017-08-18 14:49:45 +0800127 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700128 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
129 }
130 return o.str();
131}
132
Eric Biggersf3dc4202019-09-30 13:05:58 -0700133static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) {
134 return prefix + ":" + keyrefstring(raw_ref);
135}
136
137// Get the ID of the keyring we store all fscrypt keys in when the kernel is too
138// old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY.
Eric Biggersa701c452018-10-23 13:06:55 -0700139static bool fscryptKeyring(key_serial_t* device_keyring) {
140 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700141 if (*device_keyring == -1) {
142 PLOG(ERROR) << "Unable to find device keyring";
143 return false;
144 }
145 return true;
146}
147
Eric Biggersf3dc4202019-09-30 13:05:58 -0700148// Add an encryption key to the legacy global session keyring.
149static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
Eric Biggersa701c452018-10-23 13:06:55 -0700150 // Place fscrypt_key into automatically zeroing buffer.
151 KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
152 fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100153
Eric Biggersa701c452018-10-23 13:06:55 -0700154 if (!fillKey(key, &fs_key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700155 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700156 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700157 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700158 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700159 key_serial_t key_id =
Eric Biggersa701c452018-10-23 13:06:55 -0700160 add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700161 if (key_id == -1) {
162 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
163 return false;
164 }
165 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
166 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700167 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700168 return true;
169}
170
Eric Biggers83a73d72019-09-30 13:06:47 -0700171// Build a struct fscrypt_key_specifier for use in the key management ioctls.
172static bool buildKeySpecifier(fscrypt_key_specifier* spec, const std::string& raw_ref,
173 int policy_version) {
174 switch (policy_version) {
175 case 1:
176 if (raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
177 LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
178 << raw_ref.size();
179 return false;
180 }
181 spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
182 memcpy(spec->u.descriptor, raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
183 return true;
184 case 2:
185 if (raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
186 LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
187 << raw_ref.size();
188 return false;
189 }
190 spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
191 memcpy(spec->u.identifier, raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
192 return true;
193 default:
194 LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
195 return false;
196 }
197}
198
Eric Biggersf3dc4202019-09-30 13:05:58 -0700199// Install a file-based encryption key to the kernel, for use by encrypted files
Eric Biggers83a73d72019-09-30 13:06:47 -0700200// on the specified filesystem using the specified encryption policy version.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700201//
Eric Biggers83a73d72019-09-30 13:06:47 -0700202// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
203// Otherwise we add the key to the legacy global session keyring.
204//
205// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
206// the kernel supports.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700207//
208// Returns %true on success, %false on failure. On success also sets *raw_ref
209// to the raw key reference for use in the encryption policy.
Eric Biggers83a73d72019-09-30 13:06:47 -0700210bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_version,
211 std::string* raw_ref) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700212 // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we
213 // have to copy the raw key into it.
214 KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0);
215 struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data();
216
Eric Biggers83a73d72019-09-30 13:06:47 -0700217 // Initialize the "key specifier", which is like a name for the key.
218 switch (policy_version) {
219 case 1:
220 // A key for a v1 policy is specified by an arbitrary 8-byte
221 // "descriptor", which must be provided by userspace. We use the
222 // first 8 bytes from the double SHA-512 of the key itself.
Steven Lavered747be2019-10-24 16:40:01 -0700223 if (is_wrapped_key_supported()) {
224 /* When wrapped key is supported, only the first 32 bytes are
225 the same per boot. The second 32 bytes can change as the ephemeral
226 key is different. */
227 *raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size()/2);
228 } else {
229 *raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
230 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700231 if (!isFsKeyringSupported()) {
232 return installKeyLegacy(key, *raw_ref);
233 }
234 if (!buildKeySpecifier(&arg->key_spec, *raw_ref, policy_version)) {
235 return false;
236 }
237 break;
238 case 2:
239 // A key for a v2 policy is specified by an 16-byte "identifier",
240 // which is a cryptographic hash of the key itself which the kernel
241 // computes and returns. Any user-provided value is ignored; we
242 // just need to set the specifier type to indicate that we're adding
243 // this type of key.
244 arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
245 break;
246 default:
247 LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
248 return false;
249 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700250
Eric Biggers83a73d72019-09-30 13:06:47 -0700251 // Provide the raw key.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700252 arg->raw_size = key.size();
253 memcpy(arg->raw, key.data(), key.size());
254
255 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
256 if (fd == -1) {
257 PLOG(ERROR) << "Failed to open " << mountpoint << " to install key";
258 return false;
259 }
260
261 if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700262 PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700263 return false;
264 }
265
Eric Biggers83a73d72019-09-30 13:06:47 -0700266 if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
267 // Retrieve the key identifier that the kernel computed.
268 *raw_ref = std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
269 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700270 LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(*raw_ref) << " to "
271 << mountpoint;
272 return true;
273}
274
275// Remove an encryption key from the legacy global session keyring.
276static bool evictKeyLegacy(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700277 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700278 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700279 bool success = true;
280 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700281 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700282 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700283
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700284 // Unlink the key from the keyring. Prefer unlinking to revoking or
285 // invalidating, since unlinking is actually no less secure currently, and
286 // it avoids bugs in certain kernel versions where the keyring key is
287 // referenced from places it shouldn't be.
288 if (keyctl_unlink(key_serial, device_keyring) != 0) {
289 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
290 success = false;
291 } else {
292 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
293 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700294 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700295 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700296}
297
Eric Biggersf3dc4202019-09-30 13:05:58 -0700298// Evict a file-based encryption key from the kernel.
299//
300// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we
301// remove the key from the legacy global session keyring.
302//
303// In the latter case, the caller is responsible for dropping caches.
Eric Biggers83a73d72019-09-30 13:06:47 -0700304bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int policy_version) {
305 if (policy_version == 1 && !isFsKeyringSupported()) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700306 return evictKeyLegacy(raw_ref);
307 }
308
309 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
310 if (fd == -1) {
311 PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key";
312 return false;
313 }
314
315 struct fscrypt_remove_key_arg arg;
316 memset(&arg, 0, sizeof(arg));
317
Eric Biggers83a73d72019-09-30 13:06:47 -0700318 if (!buildKeySpecifier(&arg.key_spec, raw_ref, policy_version)) {
319 return false;
320 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700321
322 std::string ref = keyrefstring(raw_ref);
323
324 if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
325 PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
326 return false;
327 }
328
329 LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint;
330 if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
331 // Should never happen because keys are only added/removed as root.
332 LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!";
333 } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
334 LOG(ERROR) << "Files still open after removing key with ref " << ref
335 << ". These files were not locked!";
336 }
337 return true;
338}
339
Paul Crowley26a53882017-10-26 11:16:39 -0700340bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
341 const std::string& key_path, const std::string& tmp_path,
Eric Biggers83a73d72019-09-30 13:06:47 -0700342 const std::string& volume_uuid, int policy_version,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700343 std::string* key_ref, bool wrapped_key_supported) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100344 KeyBuffer key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700345 if (pathExists(key_path)) {
346 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley26a53882017-10-26 11:16:39 -0700347 if (!retrieveKey(key_path, key_authentication, &key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700348 } else {
349 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700350 LOG(ERROR) << "No key found in " << key_path;
351 return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700352 }
353 LOG(INFO) << "Creating new key in " << key_path;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700354 if (wrapped_key_supported) {
355 if(!generateWrappedKey(MAX_USER_ID, KeyType::DE_SYS, &key)) return false;
356 } else {
357 if (!randomKey(&key)) return false;
358 }
Paul Crowley26a53882017-10-26 11:16:39 -0700359 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700360 }
361
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700362 if (wrapped_key_supported) {
363 KeyBuffer ephemeral_wrapped_key;
364 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
365 LOG(ERROR) << "Failed to export key in retrieveAndInstallKey";
366 return false;
367 }
368 key = std::move(ephemeral_wrapped_key);
369 }
370
Eric Biggers83a73d72019-09-30 13:06:47 -0700371 if (!installKey(key, BuildDataPath(volume_uuid), policy_version, key_ref)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700372 LOG(ERROR) << "Failed to install key in " << key_path;
373 return false;
374 }
375 return true;
376}
377
Paul Crowley14c8c072018-09-18 13:30:21 -0700378bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800379 KeyBuffer* key, bool keepOld) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700380 if (pathExists(key_path)) {
381 LOG(DEBUG) << "Key exists, using: " << key_path;
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800382 if (!retrieveKey(key_path, kEmptyAuthentication, key, keepOld)) return false;
Neeraj Sonic480f912018-12-14 15:18:15 +0530383 if (is_metadata_wrapped_key_supported()) {
384 KeyBuffer ephemeral_wrapped_key;
385 if (!getEphemeralWrappedKey(KeyFormat::RAW, *key, &ephemeral_wrapped_key)) {
386 LOG(ERROR) << "Failed to export key for retrieved key";
387 return false;
388 }
389 *key = std::move(ephemeral_wrapped_key);
390 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700391 } else {
392 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700393 LOG(ERROR) << "No key found in " << key_path;
394 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700395 }
396 LOG(INFO) << "Creating new key in " << key_path;
Neeraj Sonic480f912018-12-14 15:18:15 +0530397 if (is_metadata_wrapped_key_supported()) {
398 if(!generateWrappedKey(MAX_USER_ID, KeyType::ME, key)) return false;
399 } else {
400 if (!randomKey(key)) return false;
401 }
402 if (!storeKeyAtomically(key_path, tmp_path,
403 kEmptyAuthentication, *key)) return false;
404 if (is_metadata_wrapped_key_supported()) {
405 KeyBuffer ephemeral_wrapped_key;
406 if (!getEphemeralWrappedKey(KeyFormat::RAW, *key, &ephemeral_wrapped_key)) {
407 LOG(ERROR) << "Failed to export key for generated key";
408 return false;
409 }
410 *key = std::move(ephemeral_wrapped_key);
411 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700412 }
413 return true;
414}
415
Paul Crowleyf71ace32016-06-02 11:01:19 -0700416} // namespace vold
417} // namespace android